28 #include "ly_common.h"
29 #include "plugins_internal.h"
43 #define BITS_LAST_BIT_POSITION(type_bits) (type_bits->bits[LY_ARRAY_COUNT(type_bits->bits) - 1].position)
49 # define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + (size - 1) - idx)
51 # define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + idx)
54 LIBYANG_API_DEF
size_t
57 size_t needed_bytes, size;
63 LY_CHECK_ERR_RET(!needed_bytes, LOGINT(NULL), 0);
65 if ((needed_bytes == 1) || (needed_bytes == 2)) {
68 }
else if (needed_bytes < 5) {
71 }
else if (needed_bytes < 9) {
94 bitmask <<= bit_position;
97 if (*bitmap & bitmask) {
111 bits_bit_set(
char *bitmap,
size_t size, uint32_t bit_position)
122 bitmask <<= bit_position;
139 bits_str2bitmap(
const char *value,
size_t value_len,
struct lysc_type_bits *type,
char *bitmap,
struct ly_err_item **err)
141 size_t idx_start, idx_end;
145 idx_start = idx_end = 0;
146 while (idx_end < value_len) {
148 while ((idx_end < value_len) && isspace(value[idx_end])) {
151 if (idx_end == value_len) {
157 while ((idx_end < value_len) && !isspace(value[idx_end])) {
164 if (!ly_strncmp(type->
bits[u].
name, value + idx_start, idx_end - idx_start)) {
202 if (type->
bits[u].position == position) {
228 for (i = 0; i < bitmap_size; ++i) {
231 for (bitmask = 1; bitmask; bitmask <<= 1) {
232 if (*byte & bitmask) {
234 bits_add_item(bit_pos, type, items);
272 strcpy(ret, items[u]->
name);
274 ret_len = strlen(ret);
278 sprintf(ret + ret_len,
" %s", items[u]->
name);
280 ret_len += 1 + strlen(items[u]->
name);
290 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
291 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
299 memset(storage, 0,
sizeof *storage);
301 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
314 val->
bitmap = (
char *)value;
315 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
317 val->
bitmap = malloc(value_len);
319 memcpy(val->
bitmap, value, value_len);
324 bits_bitmap2items(val->
bitmap, type_bits, val->
items);
332 LY_CHECK_GOTO(ret, cleanup);
340 ret = bits_str2bitmap(value, value_len, type_bits, val->
bitmap, err);
341 LY_CHECK_GOTO(ret, cleanup);
345 bits_bitmap2items(val->
bitmap, type_bits, val->
items);
351 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
352 LY_CHECK_GOTO(ret, cleanup);
400 LIBYANG_API_DEF
const void *
402 void *UNUSED(prefix_data),
ly_bool *dynamic,
size_t *value_len)
421 if (bits_items2canon(val->items, &ret)) {
450 memset(dup, 0,
sizeof *dup);
454 LY_CHECK_GOTO(ret, error);
458 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
464 LY_CHECK_ERR_GOTO(!dup_val->bitmap, ret =
LY_EMEM, error);
471 dup_val->items[u] = orig_val->items[u];
508 .name = LY_TYPE_BITS_STR,
510 .plugin.id =
"libyang 2 - bits, version 1",
512 .plugin.validate = NULL,
518 .plugin.lyb_data_len = -1,
struct lysc_type * realtype
LIBYANG_API_DEF int lyplg_type_sort_bits(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of the lyplg_type_sort_clb for the built-in bits type.
struct lysc_type_bitenum_item * bits
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
uint8_t ly_bool
Type to indicate boolean value.
LIBYANG_API_DECL const void * lyplg_type_print_bits(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, size_t *value_len)
Implementation of the lyplg_type_print_clb for the built-in bits type.
#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.
LIBYANG_API_DECL ly_bool lyplg_type_bits_is_bit_set(const char *bitmap, size_t size, uint32_t bit_position)
Check whether a particular bit of a bitmap is set.
YANG data representation.
LIBYANG_API_DECL void lyplg_type_free_bits(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of the lyplg_type_free_clb for the built-in bits type.
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.
LIBYANG_API_DECL size_t lyplg_type_bits_bitmap_size(const struct lysc_type_bits *type)
Get the bitmap size of a bits value bitmap.
#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...
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, size_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.
struct lysc_type_bits * type_bits
#define LY_ARRAY_COUNT(ARRAY)
Get the number of records in the ARRAY.
LIBYANG_API_DECL LY_ERR lyplg_type_compare_bits(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of the lyplg_type_compare_clb for the built-in bits type.
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.
LIBYANG_API_DECL LY_ERR lyplg_type_store_bits(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
Implementation of the lyplg_type_store_clb for the built-in bits type.
#define LY_ARRAY_INCREMENT(ARRAY)
Increment the items counter in a (sized array).
LIBYANG_API_DECL LY_ERR lyplg_type_dup_bits(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of the lyplg_type_dup_clb for the built-in bits type.
Special lyd_value structure for built-in bits values.
API for (user) types plugins.
LY_ERR
libyang's error codes returned by the libyang functions.
Special lyd_value structure for built-in binary values.