28 #include "ly_common.h"
29 #include "plugins_internal.h"
40 static void lyplg_type_free_bits(
const struct ly_ctx *ctx,
struct lyd_value *value);
45 #define BITS_LAST_BIT_POSITION(type_bits) ((type_bits)->bits[LYA_COUNT((type_bits)->bits) - 1].position)
51 # define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + (size - 1) - idx)
53 # define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + idx)
63 size = size_bits / 8 + ((size_bits % 8) ? 1 : 0);
72 bitmask <<= bit_position;
75 if (*bitmap & bitmask) {
89 bits_bit_set(
char *bitmap, uint32_t size_bits, uint32_t bit_position)
95 size = size_bits / 8 + ((size_bits % 8) ? 1 : 0);
104 bitmask <<= bit_position;
131 bits_str2bitmap(
const char *value, uint32_t value_len,
struct lysc_type_bits *type,
char *bitmap,
struct ly_err_item **err)
133 uint32_t idx_start, idx_end;
137 idx_start = idx_end = 0;
138 while (idx_end < value_len) {
140 while ((idx_end < value_len) && isspace(value[idx_end])) {
143 if (idx_end == value_len) {
149 while ((idx_end < value_len) && !isspace(value[idx_end])) {
156 if (!ly_strncmp(type->
bits[u].
name, value + idx_start, idx_end - idx_start)) {
197 if (type->
bits[u].position == position) {
225 uint32_t bit_pos, i, bitmap_size;
232 for (i = 0; i < bitmap_size; ++i) {
235 for (bitmask = 1; bitmask; bitmask <<= 1) {
236 if (*
byte & bitmask) {
238 LY_CHECK_RET(bits_add_item(bit_pos, type, items, err));
264 uint32_t ret_size, ret_len;
276 LY_CHECK_ERR_RET(ly_append_str(&ret, &ret_size, &ret_len,
"%s%s", ret_len ?
" " :
"", items[u]->
name),
285 lyplg_type_store_bits(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint64_t value_size_bits,
286 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
287 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
296 memset(storage, 0,
sizeof *storage);
304 LY_CHECK_GOTO(ret, cleanup);
309 val->
bitmap = (
char *)value;
312 val->
bitmap = malloc(value_size);
314 memcpy(val->
bitmap, value, value_size);
319 LY_CHECK_GOTO(ret = bits_bitmap2items(val->
bitmap, type_bits, val->
items, err), cleanup);
327 LY_CHECK_GOTO(ret, cleanup);
334 ret = bits_str2bitmap(value, value_size, type_bits, val->
bitmap, err);
335 LY_CHECK_GOTO(ret, cleanup);
339 LY_CHECK_GOTO(ret = bits_bitmap2items(val->
bitmap, type_bits, val->
items, err), cleanup);
346 LY_CHECK_GOTO(ret, cleanup);
349 LY_CHECK_GOTO(ret, cleanup);
359 lyplg_type_free_bits(ctx, storage);
365 lyplg_type_compare_bits(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
368 uint32_t bitmap_size;
385 uint32_t bitmap_size;
392 return memcmp(v1->
data, v2->
data, bitmap_size);
397 void *UNUSED(prefix_data),
ly_bool *dynamic, uint64_t *value_len_bits)
406 if (value_len_bits) {
415 if (bits_items2canon(val->
items, &ret)) {
430 if (value_len_bits) {
431 *value_len_bits = strlen(value->
_canonical) * 8;
442 uint32_t bitmap_size;
444 memset(dup, 0,
sizeof *dup);
450 LY_CHECK_GOTO(ret, error);
454 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
459 dup_val->
bitmap = malloc(bitmap_size);
474 lyplg_type_free_bits(ctx, dup);
504 .name = LY_TYPE_BITS_STR,
506 .plugin.id =
"ly2 bits",
507 .plugin.lyb_size = lyplg_type_lyb_size_bits,
508 .plugin.store = lyplg_type_store_bits,
509 .plugin.validate_value = NULL,
510 .plugin.validate_tree = NULL,
511 .plugin.compare = lyplg_type_compare_bits,
512 .plugin.sort = lyplg_type_sort_bits,
513 .plugin.print = lyplg_type_print_bits,
514 .plugin.duplicate = lyplg_type_dup_bits,
515 .plugin.free = lyplg_type_free_bits,
const struct lyplg_type_record plugins_bits[]
Plugin information for bits type implementation.
#define BITS_BITMAP_BYTE(bitmap, size, idx)
Get a specific byte in a bitmap.
#define BITS_LAST_BIT_POSITION(type_bits)
Get the position of the last bit.
#define LYA_PREALLOC(ARRAY, COUNT, EACTION)
Allocate memory of a sized array or resize an existing array.
#define LYA_INCREMENT(ARRAY)
Increment the items counter of a sized array.
#define LYA_COUNT(ARRAY)
Get the number of records in the ARRAY.
#define LYA_FOR(ARRAY, INDEX)
Helper macro to go through sized-arrays with a numeric iterator.
#define LYA_FREE(ARRAY)
Free the space allocated for a sized array.
#define LYA_COUNT_T
Type (i.e. size) of the sized array's size counter.
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_remove(const struct ly_ctx *ctx, const char *value)
Remove specified string from the dictionary. It decrement reference counter for the string and if it ...
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.
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
LIBYANG_API_DEF ly_bool lyplg_type_bits_is_bit_set(const char *bitmap, uint32_t size_bits, uint32_t bit_position)
Check whether a particular bit of a bitmap is set.
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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
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 an error structure.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
LIBYANG_API_DECL LY_ERR LIBYANG_API_DECL LY_ERR ly_err_new_no_mem(struct ly_err_item **err)
Create and fill an error structure with a no-memory error.
@ LYPLG_LYB_SIZE_FIXED_BITS
#define LYPLG_TYPE_STORE_DYNAMIC
struct lysc_type_bitenum_item * bits
uint8_t ly_bool
Type to indicate boolean value.
API for (user) types plugins.
const struct lysc_type * realtype
struct lysc_type_bitenum_item ** items
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
YANG data representation.
Special lyd_value structure for built-in binary values.
Special lyd_value structure for built-in bits values.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.