libyang  2.0.7
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 
15 #define _GNU_SOURCE /* asprintf, strdup */
16 #include <sys/cdefs.h>
17 
18 #include "plugins_types.h"
19 
20 #include <ctype.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "libyang.h"
27 
28 /* additional internal headers for some useful simple macros */
29 #include "common.h"
30 #include "compat.h"
31 #include "plugins_internal.h" /* LY_TYPE_*_STR */
32 
45 #define BITS_LAST_BIT_POSITION(type_bits) (type_bits->bits[LY_ARRAY_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 API size_t
58 {
59  size_t needed_bytes, size;
60 
61  LY_CHECK_ARG_RET(NULL, type, type->basetype == LY_TYPE_BITS, 0);
62 
63  /* minimum needed bytes to hold all the bit positions */
64  needed_bytes = (BITS_LAST_BIT_POSITION(type) / 8) + (BITS_LAST_BIT_POSITION(type) % 8 ? 1 : 0);
65  LY_CHECK_ERR_RET(!needed_bytes, LOGINT(NULL), 0);
66 
67  if ((needed_bytes == 1) || (needed_bytes == 2)) {
68  /* uint8_t or uint16_t */
69  size = needed_bytes;
70  } else if (needed_bytes < 5) {
71  /* uint32_t */
72  size = 4;
73  } else if (needed_bytes < 9) {
74  /* uint64_t */
75  size = 8;
76  } else {
77  /* no basic type, do not round */
78  size = needed_bytes;
79  }
80 
81  return size;
82 }
83 
84 API ly_bool
85 lyplg_type_bits_is_bit_set(const char *bitmap, size_t size, uint32_t bit_position)
86 {
87  char bitmask;
88 
89  /* find the byte with our bit */
90  (void)size;
91  bitmap = BITS_BITMAP_BYTE(bitmap, size, bit_position / 8);
92  bit_position %= 8;
93 
94  /* generate bitmask */
95  bitmask = 1;
96  bitmask <<= bit_position;
97 
98  /* check if bit set */
99  if (*bitmap & bitmask) {
100  return 1;
101  }
102  return 0;
103 }
104 
112 static void
113 bits_bit_set(char *bitmap, size_t size, uint32_t bit_position)
114 {
115  char bitmask;
116 
117  /* find the byte with our bit */
118  (void)size;
119  bitmap = BITS_BITMAP_BYTE(bitmap, size, bit_position / 8);
120  bit_position %= 8;
121 
122  /* generate bitmask */
123  bitmask = 1;
124  bitmask <<= bit_position;
125 
126  /* set the bit */
127  *bitmap |= bitmask;
128 }
129 
140 static LY_ERR
141 bits_str2bitmap(const char *value, size_t value_len, struct lysc_type_bits *type, char *bitmap, struct ly_err_item **err)
142 {
143  size_t idx_start, idx_end;
145  ly_bool found;
146 
147  idx_start = idx_end = 0;
148  while (idx_end < value_len) {
149  /* skip whitespaces */
150  while ((idx_end < value_len) && isspace(value[idx_end])) {
151  ++idx_end;
152  }
153  if (idx_end == value_len) {
154  break;
155  }
156 
157  /* parse bit name */
158  idx_start = idx_end;
159  while ((idx_end < value_len) && !isspace(value[idx_end])) {
160  ++idx_end;
161  }
162 
163  /* find the bit */
164  found = 0;
165  LY_ARRAY_FOR(type->bits, u) {
166  if (!ly_strncmp(type->bits[u].name, value + idx_start, idx_end - idx_start)) {
167  found = 1;
168  break;
169  }
170  }
171 
172  /* check if name exists */
173  if (!found) {
174  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid bit \"%.*s\".", (int)(idx_end - idx_start),
175  value + idx_start);
176  }
177 
178  /* check for duplication */
179  if (lyplg_type_bits_is_bit_set(bitmap, lyplg_type_bits_bitmap_size(type), type->bits[u].position)) {
180  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Duplicate bit \"%s\".", type->bits[u].name);
181  }
182 
183  /* set the bit */
184  bits_bit_set(bitmap, lyplg_type_bits_bitmap_size(type), type->bits[u].position);
185  }
186 
187  return LY_SUCCESS;
188 }
189 
197 static void
198 bits_add_item(uint32_t position, struct lysc_type_bits *type, struct lysc_type_bitenum_item **items)
199 {
201 
202  /* find the bit item */
203  LY_ARRAY_FOR(type->bits, u) {
204  if (type->bits[u].position == position) {
205  break;
206  }
207  }
208 
209  /* add it at the end */
210  items[LY_ARRAY_COUNT(items)] = &type->bits[u];
211  LY_ARRAY_INCREMENT(items);
212 }
213 
221 static void
222 bits_bitmap2items(const char *bitmap, struct lysc_type_bits *type, struct lysc_type_bitenum_item **items)
223 {
224  size_t i, bitmap_size = lyplg_type_bits_bitmap_size(type);
225  uint32_t bit_pos;
226  char bitmask;
227  const char *byte;
228 
229  bit_pos = 0;
230  for (i = 0; i < bitmap_size; ++i) {
231  /* check this byte (but not necessarily all bits in the last byte) */
232  byte = BITS_BITMAP_BYTE(bitmap, bitmap_size, i);
233  for (bitmask = 1; bitmask; bitmask <<= 1) {
234  if (*byte & bitmask) {
235  /* add this bit */
236  bits_add_item(bit_pos, type, items);
237  }
238 
239  if (bit_pos == BITS_LAST_BIT_POSITION(type)) {
240  /* we have checked the last valid bit */
241  break;
242  }
243 
244  ++bit_pos;
245  }
246  }
247 }
248 
256 static LY_ERR
257 bits_items2canon(struct lysc_type_bitenum_item **items, char **canonical)
258 {
259  char *ret;
260  size_t ret_len;
262 
263  *canonical = NULL;
264 
265  /* init value */
266  ret = strdup("");
267  LY_CHECK_RET(!ret, LY_EMEM);
268  ret_len = 0;
269 
270  LY_ARRAY_FOR(items, u) {
271  if (!ret_len) {
272  ret = ly_realloc(ret, strlen(items[u]->name) + 1);
273  LY_CHECK_RET(!ret, LY_EMEM);
274  strcpy(ret, items[u]->name);
275 
276  ret_len = strlen(ret);
277  } else {
278  ret = ly_realloc(ret, ret_len + 1 + strlen(items[u]->name) + 1);
279  LY_CHECK_RET(!ret, LY_EMEM);
280  sprintf(ret + ret_len, " %s", items[u]->name);
281 
282  ret_len += 1 + strlen(items[u]->name);
283  }
284  }
285 
286  *canonical = ret;
287  return LY_SUCCESS;
288 }
289 
290 API LY_ERR
291 lyplg_type_store_bits(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
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),
294  struct ly_err_item **err)
295 {
296  LY_ERR ret = LY_SUCCESS;
297  struct lysc_type_bits *type_bits = (struct lysc_type_bits *)type;
298  struct lyd_value_bits *val;
299 
300  /* init storage */
301  memset(storage, 0, sizeof *storage);
302  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
303  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
304  storage->realtype = type;
305 
306  if (format == LY_VALUE_LYB) {
307  /* validation */
308  if (value_len != lyplg_type_bits_bitmap_size(type_bits)) {
309  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB bits value size %zu (expected %zu).",
310  value_len, lyplg_type_bits_bitmap_size(type_bits));
311  goto cleanup;
312  }
313 
314  /* store value (bitmap) */
315  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
316  val->bitmap = (char *)value;
317  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
318  } else {
319  val->bitmap = malloc(value_len);
320  LY_CHECK_ERR_GOTO(!val->bitmap, ret = LY_EMEM, cleanup);
321  memcpy(val->bitmap, value, value_len);
322  }
323 
324  /* allocate and fill the bit item array */
325  LY_ARRAY_CREATE_GOTO(ctx, val->items, LY_ARRAY_COUNT(type_bits->bits), ret, cleanup);
326  bits_bitmap2items(val->bitmap, type_bits, val->items);
327 
328  /* success */
329  goto cleanup;
330  }
331 
332  /* check hints */
333  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
334  LY_CHECK_GOTO(ret, cleanup);
335 
336  /* allocate the bitmap */
337  val->bitmap = malloc(lyplg_type_bits_bitmap_size(type_bits));
338  LY_CHECK_ERR_GOTO(!val->bitmap, ret = LY_EMEM, cleanup);
339  memset(val->bitmap, 0, lyplg_type_bits_bitmap_size(type_bits));
340 
341  /* fill the bitmap */
342  ret = bits_str2bitmap(value, value_len, type_bits, val->bitmap, err);
343  LY_CHECK_GOTO(ret, cleanup);
344 
345  /* allocate and fill the bit item array */
346  LY_ARRAY_CREATE_GOTO(ctx, val->items, LY_ARRAY_COUNT(type_bits->bits), ret, cleanup);
347  bits_bitmap2items(val->bitmap, type_bits, val->items);
348 
349  if (format == LY_VALUE_CANON) {
350  /* store canonical value */
351  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
352  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
353  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
354  LY_CHECK_GOTO(ret, cleanup);
355  } else {
356  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
357  LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
358  }
359  }
360 
361 cleanup:
362  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
363  free((void *)value);
364  }
365 
366  if (ret) {
367  lyplg_type_free_bits(ctx, storage);
368  }
369  return ret;
370 }
371 
372 API LY_ERR
373 lyplg_type_compare_bits(const struct lyd_value *val1, const struct lyd_value *val2)
374 {
375  struct lyd_value_bits *v1, *v2;
376  struct lysc_type_bits *type_bits = (struct lysc_type_bits *)val1->realtype;
377 
378  if (val1->realtype != val2->realtype) {
379  return LY_ENOT;
380  }
381 
382  LYD_VALUE_GET(val1, v1);
383  LYD_VALUE_GET(val2, v2);
384 
385  if (memcmp(v1->bitmap, v2->bitmap, lyplg_type_bits_bitmap_size(type_bits))) {
386  return LY_ENOT;
387  }
388  return LY_SUCCESS;
389 }
390 
391 API const void *
392 lyplg_type_print_bits(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
393  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
394 {
395  struct lysc_type_bits *type_bits = (struct lysc_type_bits *)value->realtype;
396  struct lyd_value_bits *val;
397  char *ret;
398 
399  LYD_VALUE_GET(value, val);
400 
401  if (format == LY_VALUE_LYB) {
402  *dynamic = 0;
403  if (value_len) {
404  *value_len = lyplg_type_bits_bitmap_size(type_bits);
405  }
406  return val->bitmap;
407  }
408 
409  /* generate canonical value if not already */
410  if (!value->_canonical) {
411  /* get the canonical value */
412  if (bits_items2canon(val->items, &ret)) {
413  return NULL;
414  }
415 
416  /* store it */
417  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
418  LOGMEM(ctx);
419  return NULL;
420  }
421  }
422 
423  /* use the cached canonical value */
424  if (dynamic) {
425  *dynamic = 0;
426  }
427  if (value_len) {
428  *value_len = strlen(value->_canonical);
429  }
430  return value->_canonical;
431 }
432 
433 API LY_ERR
434 lyplg_type_dup_bits(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
435 {
436  LY_ERR ret;
437  struct lysc_type_bits *type_bits = (struct lysc_type_bits *)original->realtype;
439  struct lyd_value_bits *orig_val, *dup_val;
440 
441  memset(dup, 0, sizeof *dup);
442 
443  /* optional canonical value */
444  ret = lydict_insert(ctx, original->_canonical, ly_strlen(original->_canonical), &dup->_canonical);
445  LY_CHECK_GOTO(ret, error);
446 
447  /* allocate value */
448  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
449  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
450 
451  LYD_VALUE_GET(original, orig_val);
452 
453  /* duplicate bitmap */
454  dup_val->bitmap = malloc(lyplg_type_bits_bitmap_size(type_bits));
455  LY_CHECK_ERR_GOTO(!dup_val->bitmap, ret = LY_EMEM, error);
456  memcpy(dup_val->bitmap, orig_val->bitmap, lyplg_type_bits_bitmap_size(type_bits));
457 
458  /* duplicate bit item pointers */
459  LY_ARRAY_CREATE_GOTO(ctx, dup_val->items, LY_ARRAY_COUNT(orig_val->items), ret, error);
460  LY_ARRAY_FOR(orig_val->items, u) {
461  LY_ARRAY_INCREMENT(dup_val->items);
462  dup_val->items[u] = orig_val->items[u];
463  }
464 
465  dup->realtype = original->realtype;
466  return LY_SUCCESS;
467 
468 error:
469  lyplg_type_free_bits(ctx, dup);
470  return ret;
471 }
472 
473 API void
474 lyplg_type_free_bits(const struct ly_ctx *ctx, struct lyd_value *value)
475 {
476  struct lyd_value_bits *val;
477 
478  lydict_remove(ctx, value->_canonical);
479  LYD_VALUE_GET(value, val);
480  if (val) {
481  free(val->bitmap);
482  LY_ARRAY_FREE(val->items);
484  }
485 }
486 
494 const struct lyplg_type_record plugins_bits[] = {
495  {
496  .module = "",
497  .revision = NULL,
498  .name = LY_TYPE_BITS_STR,
499 
500  .plugin.id = "libyang 2 - bits, version 1",
501  .plugin.store = lyplg_type_store_bits,
502  .plugin.validate = NULL,
503  .plugin.compare = lyplg_type_compare_bits,
504  .plugin.sort = NULL,
505  .plugin.print = lyplg_type_print_bits,
506  .plugin.duplicate = lyplg_type_dup_bits,
507  .plugin.free = lyplg_type_free_bits
508  },
509  {0}
510 };
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)
Definition: bits.c:291
const struct lyplg_type_record plugins_bits[]
Plugin information for bits type implementation.
Definition: bits.c:494
#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
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)
Definition: bits.c:392
libyang context handler.
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.
Definition: log.h:242
@ LYVE_DATA
Definition: log.h:279
@ LY_EMEM
Definition: log.h:244
@ LY_ENOT
Definition: log.h:256
@ LY_EVALID
Definition: log.h:250
@ LY_SUCCESS
Definition: log.h:243
Libyang full error structure.
Definition: log.h:287
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.
Definition: bits.c:474
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.
Definition: bits.c:434
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.
Definition: bits.c:373
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.
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.
Definition: bits.c:57
#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.
Definition: bits.c:85
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1531
LY_DATA_TYPE basetype
Definition: tree_schema.h:1585
struct lysc_type_bitenum_item * bits
Definition: tree_schema.h:1587
Compiled YANG data node.
Definition: tree_schema.h:1644
#define LY_ARRAY_FREE(ARRAY)
Free the space allocated for the (sized array).
Definition: tree_edit.h:234
#define LY_ARRAY_INCREMENT(ARRAY)
Increment the items counter in a (sized array).
Definition: tree_edit.h:200
#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:189
#define LY_ARRAY_COUNT(ARRAY)
Get the number of records in the ARRAY.
Definition: tree.h:148
#define LY_ARRAY_FOR(ARRAY,...)
Sized-array iterator (for-loop).
Definition: tree.h:167
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
#define LY_ARRAY_COUNT_TYPE
Type (i.e. size) of the sized array's size counter.
Definition: tree.h:104
@ LY_TYPE_BITS
Definition: tree.h:211
@ LY_VALUE_CANON
Definition: tree.h:236
@ LY_VALUE_LYB
Definition: tree.h:241
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:535
struct lysc_type_bitenum_item ** items
Definition: tree_data.h:605
char * bitmap
Definition: tree_data.h:602
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:573
const char * _canonical
Definition: tree_data.h:532
YANG data representation.
Definition: tree_data.h:531
Special lyd_value structure for built-in bits values.
Definition: tree_data.h:601
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 LOGMEM(CTX)
Definition: tree_edit.h:25