16 #include <sys/cdefs.h>
31 #include "plugins_internal.h"
45 #define BITS_LAST_BIT_POSITION(type_bits) (type_bits->bits[LY_ARRAY_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)
59 size_t needed_bytes, size;
65 LY_CHECK_ERR_RET(!needed_bytes, LOGINT(NULL), 0);
67 if ((needed_bytes == 1) || (needed_bytes == 2)) {
70 }
else if (needed_bytes < 5) {
73 }
else if (needed_bytes < 9) {
96 bitmask <<= bit_position;
99 if (*bitmap & bitmask) {
113 bits_bit_set(
char *bitmap,
size_t size, uint32_t bit_position)
124 bitmask <<= bit_position;
141 bits_str2bitmap(
const char *value,
size_t value_len,
struct lysc_type_bits *type,
char *bitmap,
struct ly_err_item **err)
143 size_t idx_start, idx_end;
147 idx_start = idx_end = 0;
148 while (idx_end < value_len) {
150 while ((idx_end < value_len) && isspace(value[idx_end])) {
153 if (idx_end == value_len) {
159 while ((idx_end < value_len) && !isspace(value[idx_end])) {
166 if (!ly_strncmp(type->
bits[u].
name, value + idx_start, idx_end - idx_start)) {
204 if (type->
bits[u].position == position) {
230 for (i = 0; i < bitmap_size; ++i) {
233 for (bitmask = 1; bitmask; bitmask <<= 1) {
234 if (*
byte & bitmask) {
236 bits_add_item(bit_pos, type, items);
272 ret =
ly_realloc(ret, strlen(items[u]->name) + 1);
274 strcpy(ret, items[u]->name);
276 ret_len = strlen(ret);
278 ret =
ly_realloc(ret, ret_len + 1 + strlen(items[u]->name) + 1);
280 sprintf(ret + ret_len,
" %s", items[u]->name);
282 ret_len += 1 + strlen(items[u]->name);
292 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
293 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
301 memset(storage, 0,
sizeof *storage);
303 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
316 val->
bitmap = (
char *)value;
319 val->
bitmap = malloc(value_len);
321 memcpy(val->
bitmap, value, value_len);
326 bits_bitmap2items(val->
bitmap, type_bits, val->
items);
334 LY_CHECK_GOTO(ret, cleanup);
342 ret = bits_str2bitmap(value, value_len, type_bits, val->
bitmap, err);
343 LY_CHECK_GOTO(ret, cleanup);
347 bits_bitmap2items(val->
bitmap, type_bits, val->
items);
354 LY_CHECK_GOTO(ret, cleanup);
393 void *UNUSED(prefix_data),
ly_bool *dynamic,
size_t *value_len)
412 if (bits_items2canon(val->items, &ret)) {
441 memset(dup, 0,
sizeof *dup);
445 LY_CHECK_GOTO(ret, error);
449 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
455 LY_CHECK_ERR_GOTO(!dup_val->bitmap, ret =
LY_EMEM, error);
462 dup_val->items[u] = orig_val->items[u];
498 .name = LY_TYPE_BITS_STR,
500 .plugin.id =
"libyang 2 - bits, version 1",
502 .plugin.validate = NULL,
API 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 *UNUSED(prefix_data), uint32_t hints, const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
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.
API const void * lyplg_type_print_bits(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
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 ...
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 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...
LY_ERR
libyang's error codes returned by the libyang functions.
Libyang full error structure.
API 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.
API 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.
API LY_ERR lyplg_type_compare_bits(const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of the lyplg_type_compare_clb for the built-in bits type.
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
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.
API size_t lyplg_type_bits_bitmap_size(const struct lysc_type_bits *type)
Get the bitmap size of a bits value bitmap.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_msg,...)
Create and fill error structure.
API 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.
#define LYPLG_TYPE_STORE_DYNAMIC
struct lysc_type_bitenum_item * bits
#define LY_ARRAY_FREE(ARRAY)
Free the space allocated for the (sized array).
#define LY_ARRAY_INCREMENT(ARRAY)
Increment the items counter in a (sized array).
#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,...
#define LY_ARRAY_COUNT(ARRAY)
Get the number of records in the ARRAY.
#define LY_ARRAY_FOR(ARRAY,...)
Sized-array iterator (for-loop).
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
#define LY_ARRAY_COUNT_TYPE
Type (i.e. size) of the sized array's size counter.
The main libyang public header.
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 bits values.
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,...