Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 1 | /* Author : Stephen Smalley, <sds@epoch.ncsc.mil> */ |
| 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 | #include <sys/cdefs.h> |
| 23 | |
| 24 | __BEGIN_DECLS |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 25 | |
| 26 | #define MAPTYPE uint64_t /* portion of bitmap in each node */ |
| 27 | #define MAPSIZE (sizeof(MAPTYPE) * 8) /* number of bits in node bitmap */ |
| 28 | #define MAPBIT 1ULL /* a bit in the node bitmap */ |
| 29 | |
| 30 | typedef struct ebitmap_node { |
| 31 | uint32_t startbit; /* starting position in the total bitmap */ |
| 32 | MAPTYPE map; /* this node's portion of the bitmap */ |
| 33 | struct ebitmap_node *next; |
| 34 | } ebitmap_node_t; |
| 35 | |
| 36 | typedef struct ebitmap { |
| 37 | ebitmap_node_t *node; /* first node in the bitmap */ |
| 38 | uint32_t highbit; /* highest position in the total bitmap */ |
| 39 | } ebitmap_t; |
| 40 | |
| 41 | #define ebitmap_length(e) ((e)->highbit) |
| 42 | #define ebitmap_startbit(e) ((e)->node ? (e)->node->startbit : 0) |
| 43 | #define ebitmap_startnode(e) ((e)->node) |
| 44 | |
| 45 | static inline unsigned int ebitmap_start(const ebitmap_t * e, |
| 46 | ebitmap_node_t ** n) |
| 47 | { |
| 48 | |
| 49 | *n = e->node; |
| 50 | return ebitmap_startbit(e); |
| 51 | } |
| 52 | |
| 53 | static inline void ebitmap_init(ebitmap_t * e) |
| 54 | { |
| 55 | memset(e, 0, sizeof(*e)); |
| 56 | } |
| 57 | |
| 58 | static inline unsigned int ebitmap_next(ebitmap_node_t ** n, unsigned int bit) |
| 59 | { |
| 60 | if ((bit == ((*n)->startbit + MAPSIZE - 1)) && (*n)->next) { |
| 61 | *n = (*n)->next; |
| 62 | return (*n)->startbit; |
| 63 | } |
| 64 | |
| 65 | return (bit + 1); |
| 66 | } |
| 67 | |
| 68 | static inline int ebitmap_node_get_bit(ebitmap_node_t * n, unsigned int bit) |
| 69 | { |
| 70 | if (n->map & (MAPBIT << (bit - n->startbit))) |
| 71 | return 1; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | #define ebitmap_for_each_bit(e, n, bit) \ |
| 76 | for (bit = ebitmap_start(e, &n); bit < ebitmap_length(e); bit = ebitmap_next(&n, bit)) \ |
| 77 | |
| 78 | extern int ebitmap_cmp(const ebitmap_t * e1, const ebitmap_t * e2); |
| 79 | extern int ebitmap_or(ebitmap_t * dst, const ebitmap_t * e1, const ebitmap_t * e2); |
| 80 | extern int ebitmap_union(ebitmap_t * dst, const ebitmap_t * e1); |
Steve Lawrence | 2f68def | 2011-10-18 08:34:41 -0400 | [diff] [blame] | 81 | extern int ebitmap_and(ebitmap_t *dst, ebitmap_t *e1, ebitmap_t *e2); |
| 82 | extern int ebitmap_xor(ebitmap_t *dst, ebitmap_t *e1, ebitmap_t *e2); |
| 83 | extern int ebitmap_not(ebitmap_t *dst, ebitmap_t *e1, unsigned int maxbit); |
| 84 | extern int ebitmap_andnot(ebitmap_t *dst, ebitmap_t *e1, ebitmap_t *e2, unsigned int maxbit); |
| 85 | extern unsigned int ebitmap_cardinality(ebitmap_t *e1); |
| 86 | extern int ebitmap_hamming_distance(ebitmap_t * e1, ebitmap_t * e2); |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 87 | extern int ebitmap_cpy(ebitmap_t * dst, const ebitmap_t * src); |
| 88 | extern int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2); |
| 89 | extern int ebitmap_get_bit(const ebitmap_t * e, unsigned int bit); |
| 90 | extern int ebitmap_set_bit(ebitmap_t * e, unsigned int bit, int value); |
| 91 | extern void ebitmap_destroy(ebitmap_t * e); |
| 92 | extern int ebitmap_read(ebitmap_t * e, void *fp); |
| 93 | |
dcashman | ed7a6ba | 2014-12-16 11:44:41 -0800 | [diff] [blame^] | 94 | __END_DECLS |
Joshua Brindle | 13cd4c8 | 2008-08-19 15:30:36 -0400 | [diff] [blame] | 95 | #endif /* _EBITMAP_H_ */ |
| 96 | |
| 97 | /* FLASK */ |