blob: 81d0c7a67347809112328e36d8c777f94aeaf22a [file] [log] [blame]
Stephen Smalley53bb2a12017-08-17 14:16:06 -04001/* Author : Stephen Smalley, <sds@tycho.nsa.gov> */
Joshua Brindle13cd4c82008-08-19 15:30:36 -04002
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>
dcashmaned7a6ba2014-12-16 11:44:41 -080022
Stephen Smalleycf8625b2016-11-29 10:57:48 -050023#ifdef __cplusplus
24extern "C" {
25#endif
Joshua Brindle13cd4c82008-08-19 15:30:36 -040026
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
31typedef 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
37typedef 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 Weksteen454466e2021-10-27 04:50:56 +000042#define ebitmap_is_empty(e) (((e)->highbit) == 0)
Joshua Brindle13cd4c82008-08-19 15:30:36 -040043#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
47static 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
55static inline void ebitmap_init(ebitmap_t * e)
56{
57 memset(e, 0, sizeof(*e));
58}
59
60static 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 Weksteen454466e2021-10-27 04:50:56 +000070static inline int ebitmap_node_get_bit(const ebitmap_node_t * n, unsigned int bit)
Joshua Brindle13cd4c82008-08-19 15:30:36 -040071{
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 Mosnacek3e506bd2019-05-14 10:14:16 +020080#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 Brindle13cd4c82008-08-19 15:30:36 -040083extern int ebitmap_cmp(const ebitmap_t * e1, const ebitmap_t * e2);
84extern int ebitmap_or(ebitmap_t * dst, const ebitmap_t * e1, const ebitmap_t * e2);
85extern int ebitmap_union(ebitmap_t * dst, const ebitmap_t * e1);
Thiébaud Weksteen454466e2021-10-27 04:50:56 +000086extern int ebitmap_and(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2);
87extern int ebitmap_xor(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2);
88extern int ebitmap_not(ebitmap_t *dst, const ebitmap_t *e1, unsigned int maxbit);
89extern int ebitmap_andnot(ebitmap_t *dst, const ebitmap_t *e1, const ebitmap_t *e2, unsigned int maxbit);
90extern unsigned int ebitmap_cardinality(const ebitmap_t *e1);
91extern int ebitmap_hamming_distance(const ebitmap_t * e1, const ebitmap_t * e2);
Joshua Brindle13cd4c82008-08-19 15:30:36 -040092extern int ebitmap_cpy(ebitmap_t * dst, const ebitmap_t * src);
93extern int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2);
James Carter49f7ebb2015-06-09 10:09:15 -040094extern int ebitmap_match_any(const ebitmap_t *e1, const ebitmap_t *e2);
Joshua Brindle13cd4c82008-08-19 15:30:36 -040095extern int ebitmap_get_bit(const ebitmap_t * e, unsigned int bit);
96extern int ebitmap_set_bit(ebitmap_t * e, unsigned int bit, int value);
Thiébaud Weksteen454466e2021-10-27 04:50:56 +000097extern unsigned int ebitmap_highest_set_bit(const ebitmap_t * e);
Joshua Brindle13cd4c82008-08-19 15:30:36 -040098extern void ebitmap_destroy(ebitmap_t * e);
99extern int ebitmap_read(ebitmap_t * e, void *fp);
100
Stephen Smalleycf8625b2016-11-29 10:57:48 -0500101#ifdef __cplusplus
102}
103#endif
104
Joshua Brindle13cd4c82008-08-19 15:30:36 -0400105#endif /* _EBITMAP_H_ */
106
107/* FLASK */