• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_is_empty(e) (((e)->highbit) == 0)
43 #define ebitmap_length(e) ((e)->highbit)
44 #define ebitmap_startbit(e) ((e)->node ? (e)->node->startbit : 0)
45 #define ebitmap_startnode(e) ((e)->node)
46 
ebitmap_start(const ebitmap_t * e,ebitmap_node_t ** n)47 static inline unsigned int ebitmap_start(const ebitmap_t * e,
48 					 ebitmap_node_t ** n)
49 {
50 
51 	*n = e->node;
52 	return ebitmap_startbit(e);
53 }
54 
ebitmap_init(ebitmap_t * e)55 static inline void ebitmap_init(ebitmap_t * e)
56 {
57 	memset(e, 0, sizeof(*e));
58 }
59 
ebitmap_next(ebitmap_node_t ** n,unsigned int bit)60 static inline unsigned int ebitmap_next(ebitmap_node_t ** n, unsigned int bit)
61 {
62 	if ((bit == ((*n)->startbit + MAPSIZE - 1)) && (*n)->next) {
63 		*n = (*n)->next;
64 		return (*n)->startbit;
65 	}
66 
67 	return (bit + 1);
68 }
69 
ebitmap_node_get_bit(const ebitmap_node_t * n,unsigned int bit)70 static inline int ebitmap_node_get_bit(const ebitmap_node_t * n, unsigned int bit)
71 {
72 	if (n->map & (MAPBIT << (bit - n->startbit)))
73 		return 1;
74 	return 0;
75 }
76 
77 #define ebitmap_for_each_bit(e, n, bit) \
78 	for (bit = ebitmap_start(e, &n); bit < ebitmap_length(e); bit = ebitmap_next(&n, bit)) \
79 
80 #define ebitmap_for_each_positive_bit(e, n, bit) \
81 	ebitmap_for_each_bit(e, n, bit) if (ebitmap_node_get_bit(n, bit)) \
82 
83 extern int ebitmap_cmp(const ebitmap_t * e1, const ebitmap_t * e2);
84 extern int ebitmap_or(ebitmap_t * dst, const ebitmap_t * e1, const ebitmap_t * e2);
85 extern int ebitmap_union(ebitmap_t * dst, const ebitmap_t * e1);
86 extern int ebitmap_and(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2);
87 extern int ebitmap_xor(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2);
88 extern int ebitmap_not(ebitmap_t *dst, const ebitmap_t *e1, unsigned int maxbit);
89 extern int ebitmap_andnot(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2, unsigned int maxbit);
90 extern unsigned int ebitmap_cardinality(const ebitmap_t *e1);
91 extern int ebitmap_hamming_distance(const ebitmap_t * e1, const ebitmap_t * e2);
92 extern int ebitmap_cpy(ebitmap_t * dst, const ebitmap_t * src);
93 extern int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2);
94 extern int ebitmap_match_any(const ebitmap_t *e1, const ebitmap_t *e2);
95 extern int ebitmap_get_bit(const ebitmap_t * e, unsigned int bit);
96 extern int ebitmap_set_bit(ebitmap_t * e, unsigned int bit, int value);
97 extern unsigned int ebitmap_highest_set_bit(const ebitmap_t * e);
98 extern void ebitmap_destroy(ebitmap_t * e);
99 extern int ebitmap_read(ebitmap_t * e, void *fp);
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif				/* _EBITMAP_H_ */
106 
107 /* FLASK */
108