Loading...
Searching...
No Matches
bits.c
Go to the documentation of this file.
#define _GNU_SOURCE /* strdup */
#include "plugins_types.h"
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "libyang.h"
/* additional internal headers for some useful simple macros */
#include "compat.h"
#include "ly_common.h"
#include "plugins_internal.h" /* LY_TYPE_*_STR */
#define BITS_LAST_BIT_POSITION(type_bits) (type_bits->bits[LY_ARRAY_COUNT(type_bits->bits) - 1].position)
#ifdef IS_BIG_ENDIAN
# define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + (size - 1) - idx)
#else
# define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + idx)
#endif
LIBYANG_API_DEF size_t
lyplg_type_bits_bitmap_size(const struct lysc_type_bits *type)
{
size_t needed_bytes, size;
LY_CHECK_ARG_RET(NULL, type, type->basetype == LY_TYPE_BITS, 0);
/* minimum needed bytes to hold all the bit positions (which start at 0) */
needed_bytes = ((BITS_LAST_BIT_POSITION(type) + 1) / 8) + ((BITS_LAST_BIT_POSITION(type) + 1) % 8 ? 1 : 0);
LY_CHECK_ERR_RET(!needed_bytes, LOGINT(NULL), 0);
if ((needed_bytes == 1) || (needed_bytes == 2)) {
/* uint8_t or uint16_t */
size = needed_bytes;
} else if (needed_bytes < 5) {
/* uint32_t */
size = 4;
} else if (needed_bytes < 9) {
/* uint64_t */
size = 8;
} else {
/* no basic type, do not round */
size = needed_bytes;
}
return size;
}
LIBYANG_API_DEF ly_bool
lyplg_type_bits_is_bit_set(const char *bitmap, size_t size, uint32_t bit_position)
{
char bitmask;
/* find the byte with our bit */
(void)size;
bitmap = BITS_BITMAP_BYTE(bitmap, size, bit_position / 8);
bit_position %= 8;
/* generate bitmask */
bitmask = 1;
bitmask <<= bit_position;
/* check if bit set */
if (*bitmap & bitmask) {
return 1;
}
return 0;
}
static void
bits_bit_set(char *bitmap, size_t size, uint32_t bit_position)
{
char bitmask;
/* find the byte with our bit */
(void)size;
bitmap = BITS_BITMAP_BYTE(bitmap, size, bit_position / 8);
bit_position %= 8;
/* generate bitmask */
bitmask = 1;
bitmask <<= bit_position;
/* set the bit */
*bitmap |= bitmask;
}
static LY_ERR
bits_str2bitmap(const char *value, size_t value_len, struct lysc_type_bits *type, char *bitmap, struct ly_err_item **err)
{
size_t idx_start, idx_end;
LY_ARRAY_COUNT_TYPE u;
ly_bool found;
idx_start = idx_end = 0;
while (idx_end < value_len) {
/* skip whitespaces */
while ((idx_end < value_len) && isspace(value[idx_end])) {
++idx_end;
}
if (idx_end == value_len) {
break;
}
/* parse bit name */
idx_start = idx_end;
while ((idx_end < value_len) && !isspace(value[idx_end])) {
++idx_end;
}
/* find the bit */
found = 0;
LY_ARRAY_FOR(type->bits, u) {
if (!ly_strncmp(type->bits[u].name, value + idx_start, idx_end - idx_start)) {
found = 1;
break;
}
}
/* check if name exists */
if (!found) {
return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid bit \"%.*s\".", (int)(idx_end - idx_start),
value + idx_start);
}
/* check for duplication */
if (lyplg_type_bits_is_bit_set(bitmap, lyplg_type_bits_bitmap_size(type), type->bits[u].position)) {
return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Duplicate bit \"%s\".", type->bits[u].name);
}
/* set the bit */
bits_bit_set(bitmap, lyplg_type_bits_bitmap_size(type), type->bits[u].position);
}
return LY_SUCCESS;
}
static void
bits_add_item(uint32_t position, struct lysc_type_bits *type, struct lysc_type_bitenum_item **items)
{
LY_ARRAY_COUNT_TYPE u;
/* find the bit item */
LY_ARRAY_FOR(type->bits, u) {
if (type->bits[u].position == position) {
break;
}
}
/* add it at the end */
items[LY_ARRAY_COUNT(items)] = &type->bits[u];
LY_ARRAY_INCREMENT(items);
}
static void
bits_bitmap2items(const char *bitmap, struct lysc_type_bits *type, struct lysc_type_bitenum_item **items)
{
size_t i, bitmap_size = lyplg_type_bits_bitmap_size(type);
uint32_t bit_pos;
uint8_t bitmask;
const uint8_t *byte;
bit_pos = 0;
for (i = 0; i < bitmap_size; ++i) {
/* check this byte (but not necessarily all bits in the last byte) */
byte = (uint8_t *)BITS_BITMAP_BYTE(bitmap, bitmap_size, i);
for (bitmask = 1; bitmask; bitmask <<= 1) {
if (*byte & bitmask) {
/* add this bit */
bits_add_item(bit_pos, type, items);
}
if (bit_pos == BITS_LAST_BIT_POSITION(type)) {
/* we have checked the last valid bit */
break;
}
++bit_pos;
}
}
}
static LY_ERR
bits_items2canon(struct lysc_type_bitenum_item **items, char **canonical)
{
char *ret;
size_t ret_len;
LY_ARRAY_COUNT_TYPE u;
*canonical = NULL;
/* init value */
ret = strdup("");
LY_CHECK_RET(!ret, LY_EMEM);
ret_len = 0;
LY_ARRAY_FOR(items, u) {
if (!ret_len) {
ret = ly_realloc(ret, strlen(items[u]->name) + 1);
LY_CHECK_RET(!ret, LY_EMEM);
strcpy(ret, items[u]->name);
ret_len = strlen(ret);
} else {
ret = ly_realloc(ret, ret_len + 1 + strlen(items[u]->name) + 1);
LY_CHECK_RET(!ret, LY_EMEM);
sprintf(ret + ret_len, " %s", items[u]->name);
ret_len += 1 + strlen(items[u]->name);
}
}
*canonical = ret;
return LY_SUCCESS;
}
LIBYANG_API_DEF 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)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type_bits *type_bits = (struct lysc_type_bits *)type;
struct lyd_value_bits *val;
/* init storage */
memset(storage, 0, sizeof *storage);
LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
storage->realtype = type;
if (format == LY_VALUE_LYB) {
/* validation */
if (value_len != lyplg_type_bits_bitmap_size(type_bits)) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB bits value size %zu (expected %zu).",
value_len, lyplg_type_bits_bitmap_size(type_bits));
goto cleanup;
}
/* store value (bitmap) */
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
val->bitmap = (char *)value;
options &= ~LYPLG_TYPE_STORE_DYNAMIC;
} else {
val->bitmap = malloc(value_len);
LY_CHECK_ERR_GOTO(!val->bitmap, ret = LY_EMEM, cleanup);
memcpy(val->bitmap, value, value_len);
}
/* allocate and fill the bit item array */
LY_ARRAY_CREATE_GOTO(ctx, val->items, LY_ARRAY_COUNT(type_bits->bits), ret, cleanup);
bits_bitmap2items(val->bitmap, type_bits, val->items);
/* success */
goto cleanup;
}
/* check hints */
ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
LY_CHECK_GOTO(ret, cleanup);
/* allocate the bitmap */
val->bitmap = malloc(lyplg_type_bits_bitmap_size(type_bits));
LY_CHECK_ERR_GOTO(!val->bitmap, ret = LY_EMEM, cleanup);
memset(val->bitmap, 0, lyplg_type_bits_bitmap_size(type_bits));
/* fill the bitmap */
ret = bits_str2bitmap(value, value_len, type_bits, val->bitmap, err);
LY_CHECK_GOTO(ret, cleanup);
/* allocate and fill the bit item array */
LY_ARRAY_CREATE_GOTO(ctx, val->items, LY_ARRAY_COUNT(type_bits->bits), ret, cleanup);
bits_bitmap2items(val->bitmap, type_bits, val->items);
if (format == LY_VALUE_CANON) {
/* store canonical value */
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
options &= ~LYPLG_TYPE_STORE_DYNAMIC;
LY_CHECK_GOTO(ret, cleanup);
} else {
ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
}
}
cleanup:
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
free((void *)value);
}
if (ret) {
lyplg_type_free_bits(ctx, storage);
}
return ret;
}
LIBYANG_API_DEF LY_ERR
lyplg_type_compare_bits(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
{
struct lyd_value_bits *v1, *v2;
struct lysc_type_bits *type_bits = (struct lysc_type_bits *)val1->realtype;
LYD_VALUE_GET(val1, v1);
LYD_VALUE_GET(val2, v2);
if (memcmp(v1->bitmap, v2->bitmap, lyplg_type_bits_bitmap_size(type_bits))) {
return LY_ENOT;
}
return LY_SUCCESS;
}
LIBYANG_API_DEF int
lyplg_type_sort_bits(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
{
struct lyd_value_binary *v1, *v2;
struct lysc_type_bits *type_bits = (struct lysc_type_bits *)val1->realtype;
int cmp;
LYD_VALUE_GET(val1, v1);
LYD_VALUE_GET(val2, v2);
cmp = memcmp(v1->data, v2->data, lyplg_type_bits_bitmap_size(type_bits));
return cmp;
}
LIBYANG_API_DEF 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)
{
struct lysc_type_bits *type_bits = (struct lysc_type_bits *)value->realtype;
struct lyd_value_bits *val;
char *ret;
LYD_VALUE_GET(value, val);
if (format == LY_VALUE_LYB) {
*dynamic = 0;
if (value_len) {
*value_len = lyplg_type_bits_bitmap_size(type_bits);
}
return val->bitmap;
}
/* generate canonical value if not already */
if (!value->_canonical) {
/* get the canonical value */
if (bits_items2canon(val->items, &ret)) {
return NULL;
}
/* store it */
if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
LOGMEM(ctx);
return NULL;
}
}
/* use the cached canonical value */
if (dynamic) {
*dynamic = 0;
}
if (value_len) {
*value_len = strlen(value->_canonical);
}
return value->_canonical;
}
LIBYANG_API_DEF LY_ERR
lyplg_type_dup_bits(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
{
LY_ERR ret;
struct lysc_type_bits *type_bits = (struct lysc_type_bits *)original->realtype;
LY_ARRAY_COUNT_TYPE u;
struct lyd_value_bits *orig_val, *dup_val;
memset(dup, 0, sizeof *dup);
/* optional canonical value */
ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
LY_CHECK_GOTO(ret, error);
/* allocate value */
LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
LYD_VALUE_GET(original, orig_val);
/* duplicate bitmap */
dup_val->bitmap = malloc(lyplg_type_bits_bitmap_size(type_bits));
LY_CHECK_ERR_GOTO(!dup_val->bitmap, ret = LY_EMEM, error);
memcpy(dup_val->bitmap, orig_val->bitmap, lyplg_type_bits_bitmap_size(type_bits));
/* duplicate bit item pointers */
LY_ARRAY_CREATE_GOTO(ctx, dup_val->items, LY_ARRAY_COUNT(orig_val->items), ret, error);
LY_ARRAY_FOR(orig_val->items, u) {
LY_ARRAY_INCREMENT(dup_val->items);
dup_val->items[u] = orig_val->items[u];
}
dup->realtype = original->realtype;
return LY_SUCCESS;
error:
lyplg_type_free_bits(ctx, dup);
return ret;
}
LIBYANG_API_DEF void
lyplg_type_free_bits(const struct ly_ctx *ctx, struct lyd_value *value)
{
struct lyd_value_bits *val;
lydict_remove(ctx, value->_canonical);
value->_canonical = NULL;
LYD_VALUE_GET(value, val);
if (val) {
free(val->bitmap);
LY_ARRAY_FREE(val->items);
LYPLG_TYPE_VAL_INLINE_DESTROY(val);
}
}
const struct lyplg_type_record plugins_bits[] = {
{
.module = "",
.revision = NULL,
.name = LY_TYPE_BITS_STR,
.plugin.id = "libyang 2 - bits, version 1",
.plugin.store = lyplg_type_store_bits,
.plugin.validate = NULL,
.plugin.compare = lyplg_type_compare_bits,
.plugin.sort = lyplg_type_sort_bits,
.plugin.print = lyplg_type_print_bits,
.plugin.duplicate = lyplg_type_dup_bits,
.plugin.free = lyplg_type_free_bits,
.plugin.lyb_data_len = -1,
},
{0}
};
lyplg_type_sort_bits
LIBYANG_API_DEF int lyplg_type_sort_bits(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
Definition bits.c:386
plugins_bits
const struct lyplg_type_record plugins_bits[]
Plugin information for bits type implementation.
Definition bits.c:504
lyplg_type_store_bits
LIBYANG_API_DEF 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)
Definition bits.c:289
BITS_BITMAP_BYTE
#define BITS_BITMAP_BYTE(bitmap, size, idx)
Get a specific byte in a bitmap.
Definition bits.c:51
BITS_LAST_BIT_POSITION
#define BITS_LAST_BIT_POSITION(type_bits)
Get the position of the last bit.
Definition bits.c:43
lyplg_type_print_bits
LIBYANG_API_DEF 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)
Definition bits.c:401
lyplg_type_compare_bits
LIBYANG_API_DEF LY_ERR lyplg_type_compare_bits(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
Definition bits.c:371
ly_ctx
libyang context handler.
lydict_insert
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...
lydict_remove
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 ...
lydict_insert_zc
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
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
LYVE_DATA
@ LYVE_DATA
Definition log.h:274
LY_EMEM
@ LY_EMEM
Definition log.h:239
LY_ENOT
@ LY_ENOT
Definition log.h:251
LY_EVALID
@ LY_EVALID
Definition log.h:245
LY_SUCCESS
@ LY_SUCCESS
Definition log.h:238
ly_err_item
Libyang full error structure.
Definition log.h:282
lyplg_type_dup_bits
LIBYANG_API_DEF 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.
Definition bits.c:443
lyplg_type_free_bits
LIBYANG_API_DEF 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.
Definition bits.c:483
lyplg_type_record::revision
const char *const char * revision
Definition plugins_types.h:638
LYPLG_TYPE_VAL_INLINE_PREPARE
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
Definition plugins_types.h:194
LYPLG_TYPE_VAL_INLINE_DESTROY
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
Definition plugins_types.h:206
lyplg_type_bits_is_bit_set
LIBYANG_API_DEF 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.
Definition bits.c:83
lyplg_type_check_hints
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.
ly_err_new
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.
lyplg_type_bits_bitmap_size
LIBYANG_API_DEF size_t lyplg_type_bits_bitmap_size(const struct lysc_type_bits *type)
Get the bitmap size of a bits value bitmap.
Definition bits.c:55
lyplg_type_record
Definition plugins_types.h:635
LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_DYNAMIC
Definition plugins_types.h:471
lysc_type::basetype
LY_DATA_TYPE basetype
Definition tree_schema.h:1287
lysc_type_bits::basetype
LY_DATA_TYPE basetype
Definition tree_schema.h:1352
lysc_type_bits::bits
struct lysc_type_bitenum_item * bits
Definition tree_schema.h:1355
lysc_type_bitenum_item::name
const char * name
Definition tree_schema.h:1325
lysc_node
Compiled YANG data node.
Definition tree_schema.h:1421
lysc_type
Definition tree_schema.h:1283
lysc_type_bitenum_item
Definition tree_schema.h:1324
lysc_type_bits
Definition tree_schema.h:1348
LY_ARRAY_FREE
#define LY_ARRAY_FREE(ARRAY)
Free the space allocated for the (sized array).
Definition tree_edit.h:231
LY_ARRAY_INCREMENT
#define LY_ARRAY_INCREMENT(ARRAY)
Increment the items counter in a (sized array).
Definition tree_edit.h:197
LY_ARRAY_CREATE_GOTO
#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,...
Definition tree_edit.h:186
LY_ARRAY_COUNT
#define LY_ARRAY_COUNT(ARRAY)
Get the number of records in the ARRAY.
Definition tree.h:148
LY_ARRAY_FOR
#define LY_ARRAY_FOR(ARRAY,...)
Sized-array iterator (for-loop).
Definition tree.h:167
LY_VALUE_FORMAT
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
LY_ARRAY_COUNT_TYPE
#define LY_ARRAY_COUNT_TYPE
Type (i.e. size) of the sized array's size counter.
Definition tree.h:104
LY_TYPE_BITS
@ LY_TYPE_BITS
Definition tree.h:210
LY_VALUE_CANON
@ LY_VALUE_CANON
Definition tree.h:235
LY_VALUE_LYB
@ LY_VALUE_LYB
Definition tree.h:240
libyang.h
The main libyang public header.
ly_bool
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
plugins_types.h
API for (user) types plugins.
lyd_value::realtype
const struct lysc_type * realtype
Definition tree_data.h:571
lyd_value_binary::data
void * data
Definition tree_data.h:650
lyd_value_bits::items
struct lysc_type_bitenum_item ** items
Definition tree_data.h:642
lyd_value_bits::bitmap
char * bitmap
Definition tree_data.h:639
LYD_VALUE_GET
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:610
lyd_value::_canonical
const char * _canonical
Definition tree_data.h:568
lyd_value
YANG data representation.
Definition tree_data.h:567
lyd_value_binary
Special lyd_value structure for built-in binary values.
Definition tree_data.h:649
lyd_value_bits
Special lyd_value structure for built-in bits values.
Definition tree_data.h:638
ly_realloc
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,...
LOGMEM
#define LOGMEM(CTX)
Definition tree_edit.h:22