29#include "plugins_internal.h"
40static 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) {
89bits_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;
131bits_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));
276 ret = ly_realloc(ret, strlen(items[u]->
name) + 1);
278 strcpy(ret, items[u]->
name);
280 ret_len = strlen(ret);
282 ret = ly_realloc(ret, ret_len + 1 + strlen(items[u]->
name) + 1);
284 sprintf(ret + ret_len,
" %s", items[u]->
name);
286 ret_len += 1 + strlen(items[u]->
name);
295lyplg_type_store_bits(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint64_t value_size_bits,
296 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
297 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
306 memset(storage, 0,
sizeof *storage);
308 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
314 LY_CHECK_GOTO(ret, cleanup);
319 val->
bitmap = (
char *)value;
320 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
322 val->
bitmap = malloc(value_size);
324 memcpy(val->
bitmap, value, value_size);
329 LY_CHECK_GOTO(ret = bits_bitmap2items(val->
bitmap, type_bits, val->
items, err), cleanup);
337 LY_CHECK_GOTO(ret, cleanup);
344 ret = bits_str2bitmap(value, value_size, type_bits, val->
bitmap, err);
345 LY_CHECK_GOTO(ret, cleanup);
349 LY_CHECK_GOTO(ret = bits_bitmap2items(val->
bitmap, type_bits, val->
items, err), cleanup);
355 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
356 LY_CHECK_GOTO(ret, cleanup);
358 ret =
lydict_insert(ctx, value_size ? value :
"", value_size, &storage->_canonical);
369 lyplg_type_free_bits(ctx, storage);
375lyplg_type_compare_bits(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
378 uint32_t bitmap_size;
395 uint32_t bitmap_size;
402 return memcmp(v1->
data, v2->
data, bitmap_size);
407 void *UNUSED(prefix_data),
ly_bool *dynamic, uint64_t *value_len_bits)
416 if (value_len_bits) {
425 if (bits_items2canon(val->
items, &ret)) {
440 if (value_len_bits) {
441 *value_len_bits = strlen(value->
_canonical) * 8;
452 uint32_t bitmap_size;
454 memset(dup, 0,
sizeof *dup);
460 LY_CHECK_GOTO(ret, error);
464 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
469 dup_val->
bitmap = malloc(bitmap_size);
484 lyplg_type_free_bits(ctx, dup);
514 .name = LY_TYPE_BITS_STR,
516 .plugin.id =
"ly2 bits",
517 .plugin.lyb_size = lyplg_type_lyb_size_bits,
518 .plugin.store = lyplg_type_store_bits,
519 .plugin.validate_value = NULL,
520 .plugin.validate_tree = NULL,
521 .plugin.compare = lyplg_type_compare_bits,
522 .plugin.sort = lyplg_type_sort_bits,
523 .plugin.print = lyplg_type_print_bits,
524 .plugin.duplicate = lyplg_type_dup_bits,
525 .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.
const char *const char * revision
#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 error structure.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
@ 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.