29 #include "ly_common.h"
30 #include "plugins_internal.h"
41 static void lyplg_type_free_bits(
const struct ly_ctx *ctx,
struct lyd_value *value);
46 #define BITS_LAST_BIT_POSITION(type_bits) ((type_bits)->bits[LY_ARRAY_COUNT((type_bits)->bits) - 1].position)
52 # define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + (size - 1) - idx)
54 # define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + idx)
64 size = size_bits / 8 + (size_bits % 8) ? 1 : 0;
73 bitmask <<= bit_position;
76 if (*bitmap & bitmask) {
90 bits_bit_set(
char *bitmap, uint32_t size_bits, uint32_t bit_position)
96 size = size_bits / 8 + (size_bits % 8) ? 1 : 0;
105 bitmask <<= bit_position;
132 bits_str2bitmap(
const char *value, uint32_t value_len,
struct lysc_type_bits *type,
char *bitmap,
struct ly_err_item **err)
134 uint32_t idx_start, idx_end;
138 idx_start = idx_end = 0;
139 while (idx_end < value_len) {
141 while ((idx_end < value_len) && isspace(value[idx_end])) {
144 if (idx_end == value_len) {
150 while ((idx_end < value_len) && !isspace(value[idx_end])) {
157 if (!ly_strncmp(type->
bits[u].
name, value + idx_start, idx_end - idx_start)) {
195 if (type->
bits[u].position == position) {
225 for (bitmask = 1; bitmask; bitmask <<= 1) {
226 if (*byte & bitmask) {
228 bits_add_item(bit_pos, type, items);
266 strcpy(ret, items[u]->
name);
268 ret_len = strlen(ret);
272 sprintf(ret + ret_len,
" %s", items[u]->
name);
274 ret_len += 1 + strlen(items[u]->
name);
283 lyplg_type_store_bits(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint32_t value_size_bits,
284 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
294 memset(storage, 0,
sizeof *storage);
296 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
302 LY_CHECK_GOTO(ret, cleanup);
307 val->
bitmap = (
char *)value;
308 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
310 val->
bitmap = malloc(value_size);
312 memcpy(val->
bitmap, value, value_size);
317 bits_bitmap2items(val->
bitmap, type_bits, val->
items);
325 LY_CHECK_GOTO(ret, cleanup);
332 ret = bits_str2bitmap(value, value_size, type_bits, val->
bitmap, err);
333 LY_CHECK_GOTO(ret, cleanup);
337 bits_bitmap2items(val->
bitmap, type_bits, val->
items);
341 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
343 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
344 LY_CHECK_GOTO(ret, cleanup);
352 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
357 lyplg_type_free_bits(ctx, storage);
363 lyplg_type_compare_bits(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
395 void *UNUSED(prefix_data),
ly_bool *dynamic, uint32_t *value_len_bits)
404 if (value_len_bits) {
413 if (bits_items2canon(val->
items, &ret)) {
428 if (value_len_bits) {
429 *value_len_bits = strlen(value->
_canonical) * 8;
442 memset(dup, 0,
sizeof *dup);
448 LY_CHECK_GOTO(ret, error);
452 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
457 dup_val->
bitmap = malloc(bitmap_size);
472 lyplg_type_free_bits(ctx, dup);
477 lyplg_type_free_bits(
const struct ly_ctx *ctx,
struct lyd_value *value)
502 .name = LY_TYPE_BITS_STR,
504 .plugin.id =
"ly2 bits",
505 .plugin.lyb_size = lyplg_type_lyb_size_bits,
506 .plugin.store = lyplg_type_store_bits,
507 .plugin.validate_value = NULL,
508 .plugin.validate_tree = NULL,
509 .plugin.compare = lyplg_type_compare_bits,
510 .plugin.sort = lyplg_type_sort_bits,
511 .plugin.print = lyplg_type_print_bits,
512 .plugin.duplicate = lyplg_type_dup_bits,
513 .plugin.free = lyplg_type_free_bits,
struct lysc_type * realtype
LIBYANG_API_DECL 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.
struct lysc_type_bitenum_item * bits
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.
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
YANG extension compiled instance.
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
#define BITS_BITMAP_BYTE(bitmap, size, idx)
Get a specific byte in a bitmap.
#define LY_ARRAY_CREATE_GOTO(CTX, ARRAY, SIZE, RET, GOTO)
Allocate a (sized array) for the specified number of items. If the ARRAY already exists, it is resized (space for SIZE items is added).
struct lyplg_type_record plugins_bits[]
Plugin information for bits type implementation.
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
The main libyang public header.
struct lysc_type_bitenum_item ** items
#define BITS_LAST_BIT_POSITION(type_bits)
Get the position of the last bit.
YANG data representation.
Libyang full error structure.
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
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 LY_ARRAY_FREE(ARRAY)
Free the space allocated for the (sized array).
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(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...
#define LY_ARRAY_COUNT(ARRAY)
Get the number of records in the ARRAY.
void * ly_realloc(void *ptr, size_t size)
Wrapper for realloc() call. The only difference is that if it fails to allocate the requested memory...
#define LY_ARRAY_FOR(ARRAY,...)
Sized-array iterator (for-loop).
#define LY_ARRAY_COUNT_TYPE
Type (i.e. size) of the sized array's size counter.
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.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
return memcmp(v1->data, v2->data, bitmap_size)
#define LY_ARRAY_INCREMENT(ARRAY)
Increment the items counter in a (sized array).
Special lyd_value structure for built-in bits values.
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.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
Special lyd_value structure for built-in binary values.