Stephen Smalley | 53bb2a1 | 2017-08-17 14:16:06 -0400 | [diff] [blame] | 1 | /* Author : Stephen Smalley, <sds@tycho.nsa.gov> */ |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 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> |
dcashman | ed7a6ba | 2014-12-16 11:44:41 -0800 | [diff] [blame] | 22 | |
Stephen Smalley | cf8625b | 2016-11-29 10:57:48 -0500 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 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 | |
Thiébaud Weksteen | 454466e | 2021-10-27 04:50:56 +0000 | [diff] [blame^] | 42 | #define ebitmap_is_empty(e) (((e)->highbit) == 0) |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 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 | |
| 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 | |
| 55 | static inline void ebitmap_init(ebitmap_t * e) |
| 56 | { |
| 57 | memset(e, 0, sizeof(*e)); |
| 58 | } |
| 59 | |
| 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 | |
Thiébaud Weksteen | 454466e | 2021-10-27 04:50:56 +0000 | [diff] [blame^] | 70 | static inline int ebitmap_node_get_bit(const ebitmap_node_t * n, unsigned int bit) |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 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 | |
Ondrej Mosnacek | 3e506bd | 2019-05-14 10:14:16 +0200 | [diff] [blame] | 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 | |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 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); |
Thiébaud Weksteen | 454466e | 2021-10-27 04:50:56 +0000 | [diff] [blame^] | 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); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 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); |
James Carter | 49f7ebb | 2015-06-09 10:09:15 -0400 | [diff] [blame] | 94 | extern int ebitmap_match_any(const ebitmap_t *e1, const ebitmap_t *e2); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 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); |
Thiébaud Weksteen | 454466e | 2021-10-27 04:50:56 +0000 | [diff] [blame^] | 97 | extern unsigned int ebitmap_highest_set_bit(const ebitmap_t * e); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 98 | extern void ebitmap_destroy(ebitmap_t * e); |
| 99 | extern int ebitmap_read(ebitmap_t * e, void *fp); |
| 100 | |
Stephen Smalley | cf8625b | 2016-11-29 10:57:48 -0500 | [diff] [blame] | 101 | #ifdef __cplusplus |
| 102 | } |
| 103 | #endif |
| 104 | |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 105 | #endif /* _EBITMAP_H_ */ |
| 106 | |
| 107 | /* FLASK */ |