• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Author : Stephen Smalley, <sds@epoch.ncsc.mil> */
3 
4 /* FLASK */
5 
6 /*
7  * A hash table (hashtab) maintains associations between
8  * key values and datum values.  The type of the key values
9  * and the type of the datum values is arbitrary.  The
10  * functions for hash computation and key comparison are
11  * provided by the creator of the table.
12  */
13 
14 #ifndef _SEPOL_POLICYDB_HASHTAB_H_
15 #define _SEPOL_POLICYDB_HASHTAB_H_
16 
17 #include <sepol/errcodes.h>
18 
19 #include <stdint.h>
20 #include <stdio.h>
21 
22 typedef char *hashtab_key_t;	/* generic key type */
23 typedef void *hashtab_datum_t;	/* generic datum type */
24 
25 typedef struct hashtab_node *hashtab_ptr_t;
26 
27 typedef struct hashtab_node {
28 	hashtab_key_t key;
29 	hashtab_datum_t datum;
30 	hashtab_ptr_t next;
31 } hashtab_node_t;
32 
33 typedef struct hashtab_val {
34 	hashtab_ptr_t *htable;	/* hash table */
35 	unsigned int size;	/* number of slots in hash table */
36 	uint32_t nel;		/* number of elements in hash table */
37 	unsigned int (*hash_value) (struct hashtab_val * h, hashtab_key_t key);	/* hash function */
38 	int (*keycmp) (struct hashtab_val * h, hashtab_key_t key1, hashtab_key_t key2);	/* key comparison function */
39 } hashtab_val_t;
40 
41 typedef hashtab_val_t *hashtab_t;
42 
43 /*
44    Creates a new hash table with the specified characteristics.
45 
46    Returns NULL if insufficent space is available or
47    the new hash table otherwise.
48  */
49 extern hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
50 							    const hashtab_key_t
51 							    key),
52 				int (*keycmp) (hashtab_t h,
53 					       const hashtab_key_t key1,
54 					       const hashtab_key_t key2),
55 				unsigned int size);
56 /*
57    Inserts the specified (key, datum) pair into the specified hash table.
58 
59    Returns SEPOL_ENOMEM if insufficient space is available or
60    SEPOL_EEXIST  if there is already an entry with the same key or
61    SEPOL_OK otherwise.
62  */
63 extern int hashtab_insert(hashtab_t h, hashtab_key_t k, hashtab_datum_t d);
64 
65 /*
66    Removes the entry with the specified key from the hash table.
67    Applies the specified destroy function to (key,datum,args) for
68    the entry.
69 
70    Returns SEPOL_ENOENT if no entry has the specified key or
71    SEPOL_OK otherwise.
72  */
73 extern int hashtab_remove(hashtab_t h, hashtab_key_t k,
74 			  void (*destroy) (hashtab_key_t k,
75 					   hashtab_datum_t d,
76 					   void *args), void *args);
77 
78 /*
79    Insert or replace the specified (key, datum) pair in the specified
80    hash table.  If an entry for the specified key already exists,
81    then the specified destroy function is applied to (key,datum,args)
82    for the entry prior to replacing the entry's contents.
83 
84    Returns SEPOL_ENOMEM if insufficient space is available or
85    SEPOL_OK otherwise.
86  */
87 extern int hashtab_replace(hashtab_t h, hashtab_key_t k, hashtab_datum_t d,
88 			   void (*destroy) (hashtab_key_t k,
89 					    hashtab_datum_t d,
90 					    void *args), void *args);
91 
92 /*
93    Searches for the entry with the specified key in the hash table.
94 
95    Returns NULL if no entry has the specified key or
96    the datum of the entry otherwise.
97  */
98 extern hashtab_datum_t hashtab_search(hashtab_t h, const hashtab_key_t k);
99 
100 /*
101    Destroys the specified hash table.
102  */
103 extern void hashtab_destroy(hashtab_t h);
104 
105 /*
106    Applies the specified apply function to (key,datum,args)
107    for each entry in the specified hash table.
108 
109    The order in which the function is applied to the entries
110    is dependent upon the internal structure of the hash table.
111 
112    If apply returns a non-zero status, then hashtab_map will cease
113    iterating through the hash table and will propagate the error
114    return to its caller.
115  */
116 extern int hashtab_map(hashtab_t h,
117 		       int (*apply) (hashtab_key_t k,
118 				     hashtab_datum_t d,
119 				     void *args), void *args);
120 
121 /*
122    Same as hashtab_map, except that if apply returns a non-zero status,
123    then the (key,datum) pair will be removed from the hashtab and the
124    destroy function will be applied to (key,datum,args).
125  */
126 extern void hashtab_map_remove_on_error(hashtab_t h,
127 					int (*apply) (hashtab_key_t k,
128 						      hashtab_datum_t d,
129 						      void *args),
130 					void (*destroy) (hashtab_key_t k,
131 							 hashtab_datum_t d,
132 							 void *args),
133 					void *args);
134 
135 extern void hashtab_hash_eval(hashtab_t h, char *tag);
136 
137 #endif
138