1 /* Author : Stephen Smalley, <sds@tycho.nsa.gov> */
2
3 /* FLASK */
4
5 /*
6 * An extensible bitmap is a bitmap that supports an
7 * arbitrary number of bits. Extensible bitmaps are
8 * used to represent sets of values, such as types,
9 * roles, categories, and classes.
10 *
11 * Each extensible bitmap is implemented as a linked
12 * list of bitmap nodes, where each bitmap node has
13 * an explicitly specified starting bit position within
14 * the total bitmap.
15 */
16
17 #ifndef _SEPOL_POLICYDB_EBITMAP_H_
18 #define _SEPOL_POLICYDB_EBITMAP_H_
19
20 #include <stdint.h>
21 #include <string.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 #define MAPTYPE uint64_t /* portion of bitmap in each node */
28 #define MAPSIZE (sizeof(MAPTYPE) * 8) /* number of bits in node bitmap */
29 #define MAPBIT 1ULL /* a bit in the node bitmap */
30
31 typedef struct ebitmap_node {
32 uint32_t startbit; /* starting position in the total bitmap */
33 MAPTYPE map; /* this node's portion of the bitmap */
34 struct ebitmap_node *next;
35 } ebitmap_node_t;
36
37 typedef struct ebitmap {
38 ebitmap_node_t *node; /* first node in the bitmap */
39 uint32_t highbit; /* highest position in the total bitmap */
40 } ebitmap_t;
41
42 #define ebitmap_length(e) ((e)->highbit)
43 #define ebitmap_startbit(e) ((e)->node ? (e)->node->startbit : 0)
44 #define ebitmap_startnode(e) ((e)->node)
45
ebitmap_start(const ebitmap_t * e,ebitmap_node_t ** n)46 static inline unsigned int ebitmap_start(const ebitmap_t * e,
47 ebitmap_node_t ** n)
48 {
49
50 *n = e->node;
51 return ebitmap_startbit(e);
52 }
53
ebitmap_init(ebitmap_t * e)54 static inline void ebitmap_init(ebitmap_t * e)
55 {
56 memset(e, 0, sizeof(*e));
57 }
58
ebitmap_next(ebitmap_node_t ** n,unsigned int bit)59 static inline unsigned int ebitmap_next(ebitmap_node_t ** n, unsigned int bit)
60 {
61 if ((bit == ((*n)->startbit + MAPSIZE - 1)) && (*n)->next) {
62 *n = (*n)->next;
63 return (*n)->startbit;
64 }
65
66 return (bit + 1);
67 }
68
ebitmap_node_get_bit(ebitmap_node_t * n,unsigned int bit)69 static inline int ebitmap_node_get_bit(ebitmap_node_t * n, unsigned int bit)
70 {
71 if (n->map & (MAPBIT << (bit - n->startbit)))
72 return 1;
73 return 0;
74 }
75
76 #define ebitmap_for_each_bit(e, n, bit) \
77 for (bit = ebitmap_start(e, &n); bit < ebitmap_length(e); bit = ebitmap_next(&n, bit)) \
78
79 extern int ebitmap_cmp(const ebitmap_t * e1, const ebitmap_t * e2);
80 extern int ebitmap_or(ebitmap_t * dst, const ebitmap_t * e1, const ebitmap_t * e2);
81 extern int ebitmap_union(ebitmap_t * dst, const ebitmap_t * e1);
82 extern int ebitmap_and(ebitmap_t *dst, ebitmap_t *e1, ebitmap_t *e2);
83 extern int ebitmap_xor(ebitmap_t *dst, ebitmap_t *e1, ebitmap_t *e2);
84 extern int ebitmap_not(ebitmap_t *dst, ebitmap_t *e1, unsigned int maxbit);
85 extern int ebitmap_andnot(ebitmap_t *dst, ebitmap_t *e1, ebitmap_t *e2, unsigned int maxbit);
86 extern unsigned int ebitmap_cardinality(ebitmap_t *e1);
87 extern int ebitmap_hamming_distance(ebitmap_t * e1, ebitmap_t * e2);
88 extern int ebitmap_cpy(ebitmap_t * dst, const ebitmap_t * src);
89 extern int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2);
90 extern int ebitmap_match_any(const ebitmap_t *e1, const ebitmap_t *e2);
91 extern int ebitmap_get_bit(const ebitmap_t * e, unsigned int bit);
92 extern int ebitmap_set_bit(ebitmap_t * e, unsigned int bit, int value);
93 extern void ebitmap_destroy(ebitmap_t * e);
94 extern int ebitmap_read(ebitmap_t * e, void *fp);
95
96 #ifdef __cplusplus
97 }
98 #endif
99
100 #endif /* _EBITMAP_H_ */
101
102 /* FLASK */
103