libyang  6.4.26
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
bits.c
Go to the documentation of this file.
1 
16 #define _GNU_SOURCE /* strdup */
17 
18 #include "plugins_types.h"
19 
20 #include <ctype.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "compat.h"
26 #include "dict.h"
27 #include "ly_array.h"
28 #include "ly_common.h"
29 #include "plugins_internal.h"
30 
40 static void lyplg_type_free_bits(const struct ly_ctx *ctx, struct lyd_value *value);
41 
45 #define BITS_LAST_BIT_POSITION(type_bits) ((type_bits)->bits[LYA_COUNT((type_bits)->bits) - 1].position)
46 
50 #ifdef IS_BIG_ENDIAN
51 # define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + (size - 1) - idx)
52 #else
53 # define BITS_BITMAP_BYTE(bitmap, size, idx) (bitmap + idx)
54 #endif
55 
56 LIBYANG_API_DEF ly_bool
57 lyplg_type_bits_is_bit_set(const char *bitmap, uint32_t size_bits, uint32_t bit_position)
58 {
59  char bitmask;
60  uint32_t size;
61 
62  /* get size in bytes */
63  size = size_bits / 8 + ((size_bits % 8) ? 1 : 0);
64 
65  /* find the byte with our bit */
66  (void)size;
67  bitmap = BITS_BITMAP_BYTE(bitmap, size, bit_position / 8);
68  bit_position %= 8;
69 
70  /* generate bitmask */
71  bitmask = 1;
72  bitmask <<= bit_position;
73 
74  /* check if bit set */
75  if (*bitmap & bitmask) {
76  return 1;
77  }
78  return 0;
79 }
80 
88 static void
89 bits_bit_set(char *bitmap, uint32_t size_bits, uint32_t bit_position)
90 {
91  char bitmask;
92  uint32_t size;
93 
94  /* get size in bytes */
95  size = size_bits / 8 + ((size_bits % 8) ? 1 : 0);
96 
97  /* find the byte with our bit */
98  (void)size;
99  bitmap = BITS_BITMAP_BYTE(bitmap, size, bit_position / 8);
100  bit_position %= 8;
101 
102  /* generate bitmask */
103  bitmask = 1;
104  bitmask <<= bit_position;
105 
106  /* set the bit */
107  *bitmap |= bitmask;
108 }
109 
110 static void
111 lyplg_type_lyb_size_bits(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint64_t *fixed_size_bits)
112 {
113  const struct lysc_type_bits *type_bits = (struct lysc_type_bits *)type;
114 
115  /* position of the last bit, positions start at 0, add 1 */
116  *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
117  *fixed_size_bits = BITS_LAST_BIT_POSITION(type_bits) + 1;
118 }
119 
130 static LY_ERR
131 bits_str2bitmap(const char *value, uint32_t value_len, struct lysc_type_bits *type, char *bitmap, struct ly_err_item **err)
132 {
133  uint32_t idx_start, idx_end;
134  LYA_COUNT_T u;
135  ly_bool found;
136 
137  idx_start = idx_end = 0;
138  while (idx_end < value_len) {
139  /* skip whitespaces */
140  while ((idx_end < value_len) && isspace(value[idx_end])) {
141  ++idx_end;
142  }
143  if (idx_end == value_len) {
144  break;
145  }
146 
147  /* parse bit name */
148  idx_start = idx_end;
149  while ((idx_end < value_len) && !isspace(value[idx_end])) {
150  ++idx_end;
151  }
152 
153  /* find the bit */
154  found = 0;
155  LYA_FOR(type->bits, u) {
156  if (!ly_strncmp(type->bits[u].name, value + idx_start, idx_end - idx_start)) {
157  found = 1;
158  break;
159  }
160  }
161 
162  /* check if name exists */
163  if (!found) {
164  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid bit \"%.*s\".", (int)(idx_end - idx_start),
165  value + idx_start);
166  }
167 
168  /* check for duplication */
169  if (lyplg_type_bits_is_bit_set(bitmap, BITS_LAST_BIT_POSITION(type) + 1, type->bits[u].position)) {
170  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Duplicate bit \"%s\".", type->bits[u].name);
171  }
172 
173  /* set the bit */
174  bits_bit_set(bitmap, BITS_LAST_BIT_POSITION(type) + 1, type->bits[u].position);
175  }
176 
177  return LY_SUCCESS;
178 }
179 
189 static LY_ERR
190 bits_add_item(uint32_t position, struct lysc_type_bits *type, struct lysc_type_bitenum_item **items,
191  struct ly_err_item **err)
192 {
193  LYA_COUNT_T u;
194 
195  /* find the bit item */
196  LYA_FOR(type->bits, u) {
197  if (type->bits[u].position == position) {
198  break;
199  }
200  }
201  if (u == LYA_COUNT(type->bits)) {
202  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid bit at position #%" PRIu32 ".", position);
203  }
204 
205  /* add it at the end */
206  items[LYA_COUNT(items)] = &type->bits[u];
207  LYA_INCREMENT(items);
208 
209  return LY_SUCCESS;
210 }
211 
221 static LY_ERR
222 bits_bitmap2items(const char *bitmap, struct lysc_type_bits *type, struct lysc_type_bitenum_item **items,
223  struct ly_err_item **err)
224 {
225  uint32_t bit_pos, i, bitmap_size;
226  uint8_t bitmask;
227  const uint8_t *byte;
228 
229  bitmap_size = LYPLG_BITS2BYTES(BITS_LAST_BIT_POSITION(type) + 1);
230 
231  bit_pos = 0;
232  for (i = 0; i < bitmap_size; ++i) {
233  /* check this byte (but not necessarily all bits in the last byte) */
234  byte = (uint8_t *)BITS_BITMAP_BYTE(bitmap, bitmap_size, i);
235  for (bitmask = 1; bitmask; bitmask <<= 1) {
236  if (*byte & bitmask) {
237  /* add this bit */
238  LY_CHECK_RET(bits_add_item(bit_pos, type, items, err));
239  }
240 
241  if (bit_pos == BITS_LAST_BIT_POSITION(type)) {
242  /* we have checked the last valid bit */
243  break;
244  }
245 
246  ++bit_pos;
247  }
248  }
249 
250  return LY_SUCCESS;
251 }
252 
260 static LY_ERR
261 bits_items2canon(struct lysc_type_bitenum_item **items, char **canonical)
262 {
263  char *ret;
264  uint32_t ret_size, ret_len;
265  LYA_COUNT_T u;
266 
267  *canonical = NULL;
268 
269  /* init value */
270  ret = strdup("");
271  LY_CHECK_RET(!ret, LY_EMEM);
272  ret_size = 1;
273  ret_len = 0;
274 
275  LYA_FOR(items, u) {
276  LY_CHECK_ERR_RET(ly_append_str(&ret, &ret_size, &ret_len, "%s%s", ret_len ? " " : "", items[u]->name),
277  free(ret), LY_EMEM);
278  }
279 
280  *canonical = ret;
281  return LY_SUCCESS;
282 }
283 
284 static LY_ERR
285 lyplg_type_store_bits(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
286  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
287  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
288  struct ly_err_item **err)
289 {
290  LY_ERR ret = LY_SUCCESS;
291  struct lysc_type_bits *type_bits = (struct lysc_type_bits *)type;
292  struct lyd_value_bits *val;
293  uint32_t value_size;
294 
295  /* init storage */
296  memset(storage, 0, sizeof *storage);
297  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
298  LY_CHECK_ERR_GOTO(!val, ret = ly_err_new_no_mem(err), cleanup);
299  storage->realtype = type;
300 
301  /* check value length */
302  ret = lyplg_type_check_value_size("bits", format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS,
303  BITS_LAST_BIT_POSITION(type_bits) + 1, &value_size, err);
304  LY_CHECK_GOTO(ret, cleanup);
305 
306  if (format == LY_VALUE_LYB) {
307  /* store value (bitmap) */
308  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
309  val->bitmap = (char *)value;
310  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
311  } else {
312  val->bitmap = malloc(value_size);
313  LY_CHECK_ERR_GOTO(!val->bitmap, ret = ly_err_new_no_mem(err), cleanup);
314  memcpy(val->bitmap, value, value_size);
315  }
316 
317  /* allocate and fill the bit item array */
318  LYA_PREALLOC(val->items, LYA_COUNT(type_bits->bits), ret = ly_err_new_no_mem(err); goto cleanup);
319  LY_CHECK_GOTO(ret = bits_bitmap2items(val->bitmap, type_bits, val->items, err), cleanup);
320 
321  /* success */
322  goto cleanup;
323  }
324 
325  /* check hints */
326  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
327  LY_CHECK_GOTO(ret, cleanup);
328 
329  /* allocate the bitmap */
330  val->bitmap = calloc(1, LYPLG_BITS2BYTES(BITS_LAST_BIT_POSITION(type_bits) + 1));
331  LY_CHECK_ERR_GOTO(!val->bitmap, ret = ly_err_new_no_mem(err), cleanup);
332 
333  /* fill the bitmap */
334  ret = bits_str2bitmap(value, value_size, type_bits, val->bitmap, err);
335  LY_CHECK_GOTO(ret, cleanup);
336 
337  /* allocate and fill the bit item array */
338  LYA_PREALLOC(val->items, LYA_COUNT(type_bits->bits), ret = ly_err_new_no_mem(err); goto cleanup);
339  LY_CHECK_GOTO(ret = bits_bitmap2items(val->bitmap, type_bits, val->items, err), cleanup);
340 
341  if (format == LY_VALUE_CANON) {
342  /* store canonical value */
343  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
344  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
345  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
346  LY_CHECK_GOTO(ret, cleanup);
347  } else {
348  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
349  LY_CHECK_GOTO(ret, cleanup);
350  }
351  }
352 
353 cleanup:
354  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
355  free((void *)value);
356  }
357 
358  if (ret) {
359  lyplg_type_free_bits(ctx, storage);
360  }
361  return ret;
362 }
363 
364 static LY_ERR
365 lyplg_type_compare_bits(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
366 {
367  struct lyd_value_bits *v1, *v2;
368  uint32_t bitmap_size;
369 
370  LYD_VALUE_GET(val1, v1);
371  LYD_VALUE_GET(val2, v2);
372 
373  bitmap_size = LYPLG_BITS2BYTES(BITS_LAST_BIT_POSITION((struct lysc_type_bits *)val1->realtype) + 1);
374 
375  if (memcmp(v1->bitmap, v2->bitmap, bitmap_size)) {
376  return LY_ENOT;
377  }
378  return LY_SUCCESS;
379 }
380 
381 static int
382 lyplg_type_sort_bits(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
383 {
384  struct lyd_value_binary *v1, *v2;
385  uint32_t bitmap_size;
386 
387  LYD_VALUE_GET(val1, v1);
388  LYD_VALUE_GET(val2, v2);
389 
390  bitmap_size = LYPLG_BITS2BYTES(BITS_LAST_BIT_POSITION((struct lysc_type_bits *)val1->realtype) + 1);
391 
392  return memcmp(v1->data, v2->data, bitmap_size);
393 }
394 
395 static const void *
396 lyplg_type_print_bits(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
397  void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_len_bits)
398 {
399  struct lyd_value_bits *val;
400  char *ret;
401 
402  LYD_VALUE_GET(value, val);
403 
404  if (format == LY_VALUE_LYB) {
405  *dynamic = 0;
406  if (value_len_bits) {
407  *value_len_bits = BITS_LAST_BIT_POSITION((struct lysc_type_bits *)value->realtype) + 1;
408  }
409  return val->bitmap;
410  }
411 
412  /* generate canonical value if not already */
413  if (!value->_canonical) {
414  /* get the canonical value */
415  if (bits_items2canon(val->items, &ret)) {
416  return NULL;
417  }
418 
419  /* store it */
420  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
421  LOGMEM(ctx);
422  return NULL;
423  }
424  }
425 
426  /* use the cached canonical value */
427  if (dynamic) {
428  *dynamic = 0;
429  }
430  if (value_len_bits) {
431  *value_len_bits = strlen(value->_canonical) * 8;
432  }
433  return value->_canonical;
434 }
435 
436 static LY_ERR
437 lyplg_type_dup_bits(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
438 {
439  LY_ERR ret;
440  LYA_COUNT_T u;
441  struct lyd_value_bits *orig_val, *dup_val;
442  uint32_t bitmap_size;
443 
444  memset(dup, 0, sizeof *dup);
445 
446  bitmap_size = LYPLG_BITS2BYTES(BITS_LAST_BIT_POSITION((struct lysc_type_bits *)original->realtype) + 1);
447 
448  /* optional canonical value */
449  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
450  LY_CHECK_GOTO(ret, error);
451 
452  /* allocate value */
453  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
454  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
455 
456  LYD_VALUE_GET(original, orig_val);
457 
458  /* duplicate bitmap */
459  dup_val->bitmap = malloc(bitmap_size);
460  LY_CHECK_ERR_GOTO(!dup_val->bitmap, ret = LY_EMEM, error);
461  memcpy(dup_val->bitmap, orig_val->bitmap, bitmap_size);
462 
463  /* duplicate bit item pointers */
464  LYA_PREALLOC(dup_val->items, LYA_COUNT(orig_val->items), LOGMEM(ctx); ret = LY_EMEM; goto error);
465  LYA_FOR(orig_val->items, u) {
466  LYA_INCREMENT(dup_val->items);
467  dup_val->items[u] = orig_val->items[u];
468  }
469 
470  dup->realtype = original->realtype;
471  return LY_SUCCESS;
472 
473 error:
474  lyplg_type_free_bits(ctx, dup);
475  return ret;
476 }
477 
478 static void
479 lyplg_type_free_bits(const struct ly_ctx *ctx, struct lyd_value *value)
480 {
481  struct lyd_value_bits *val;
482 
483  lydict_remove(ctx, value->_canonical);
484  value->_canonical = NULL;
485  LYD_VALUE_GET(value, val);
486  if (val) {
487  free(val->bitmap);
488  LYA_FREE(val->items);
490  }
491 }
492 
500 const struct lyplg_type_record plugins_bits[] = {
501  {
502  .module = "",
503  .revision = NULL,
504  .name = LY_TYPE_BITS_STR,
505 
506  .plugin.id = "ly2 bits",
507  .plugin.lyb_size = lyplg_type_lyb_size_bits,
508  .plugin.store = lyplg_type_store_bits,
509  .plugin.validate_value = NULL,
510  .plugin.validate_tree = NULL,
511  .plugin.compare = lyplg_type_compare_bits,
512  .plugin.sort = lyplg_type_sort_bits,
513  .plugin.print = lyplg_type_print_bits,
514  .plugin.duplicate = lyplg_type_dup_bits,
515  .plugin.free = lyplg_type_free_bits,
516  },
517  {0}
518 };
const struct lyplg_type_record plugins_bits[]
Plugin information for bits type implementation.
Definition: bits.c:500
#define BITS_BITMAP_BYTE(bitmap, size, idx)
Get a specific byte in a bitmap.
Definition: bits.c:53
#define BITS_LAST_BIT_POSITION(type_bits)
Get the position of the last bit.
Definition: bits.c:45
libyang dictionary
#define LYA_PREALLOC(ARRAY, COUNT, EACTION)
Allocate memory of a sized array or resize an existing array.
Definition: ly_array.h:97
#define LYA_INCREMENT(ARRAY)
Increment the items counter of a sized array.
Definition: ly_array.h:166
#define LYA_COUNT(ARRAY)
Get the number of records in the ARRAY.
Definition: ly_array.h:78
#define LYA_FOR(ARRAY, INDEX)
Helper macro to go through sized-arrays with a numeric iterator.
Definition: ly_array.h:55
#define LYA_FREE(ARRAY)
Free the space allocated for a sized array.
Definition: ly_array.h:199
#define LYA_COUNT_T
Type (i.e. size) of the sized array's size counter.
Definition: ly_array.h:38
libyang context handler.
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.
Definition: log.h:252
@ LYVE_DATA
Definition: log.h:290
@ LY_EMEM
Definition: log.h:254
@ LY_ENOT
Definition: log.h:266
@ LY_EVALID
Definition: log.h:260
@ LY_SUCCESS
Definition: log.h:253
Libyang full error structure.
Definition: log.h:298
const char * module
#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.
Definition: bits.c:57
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 an error structure.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
LIBYANG_API_DECL LY_ERR LIBYANG_API_DECL LY_ERR ly_err_new_no_mem(struct ly_err_item **err)
Create and fill an error structure with a no-memory error.
@ LYPLG_LYB_SIZE_FIXED_BITS
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1278
struct lysc_type_bitenum_item * bits
Definition: tree_schema.h:1346
const char * name
Definition: tree_schema.h:1340
Compiled YANG data node.
Definition: tree_schema.h:1412
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
libyang sized array API.
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:614
struct lysc_type_bitenum_item ** items
Definition: tree_data.h:685
char * bitmap
Definition: tree_data.h:682
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:653
const char * _canonical
Definition: tree_data.h:611
YANG data representation.
Definition: tree_data.h:610
Special lyd_value structure for built-in binary values.
Definition: tree_data.h:692
Special lyd_value structure for built-in bits values.
Definition: tree_data.h:681
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: utils.h:93
@ LY_VALUE_CANON
Definition: utils.h:94
@ LY_VALUE_LYB
Definition: utils.h:100