16cd6a6acSopenharmony_ci/* Author : Stephen Smalley, <sds@tycho.nsa.gov> */ 26cd6a6acSopenharmony_ci 36cd6a6acSopenharmony_ci/* FLASK */ 46cd6a6acSopenharmony_ci 56cd6a6acSopenharmony_ci/* 66cd6a6acSopenharmony_ci * A hash table (hashtab) maintains associations between 76cd6a6acSopenharmony_ci * key values and datum values. The type of the key values 86cd6a6acSopenharmony_ci * and the type of the datum values is arbitrary. The 96cd6a6acSopenharmony_ci * functions for hash computation and key comparison are 106cd6a6acSopenharmony_ci * provided by the creator of the table. 116cd6a6acSopenharmony_ci */ 126cd6a6acSopenharmony_ci 136cd6a6acSopenharmony_ci#ifndef _SEPOL_POLICYDB_HASHTAB_H_ 146cd6a6acSopenharmony_ci#define _SEPOL_POLICYDB_HASHTAB_H_ 156cd6a6acSopenharmony_ci 166cd6a6acSopenharmony_ci#include <sepol/errcodes.h> 176cd6a6acSopenharmony_ci 186cd6a6acSopenharmony_ci#include <stdint.h> 196cd6a6acSopenharmony_ci#include <stdio.h> 206cd6a6acSopenharmony_ci 216cd6a6acSopenharmony_ci#ifdef __cplusplus 226cd6a6acSopenharmony_ciextern "C" { 236cd6a6acSopenharmony_ci#endif 246cd6a6acSopenharmony_ci 256cd6a6acSopenharmony_citypedef char *hashtab_key_t; /* generic key type */ 266cd6a6acSopenharmony_citypedef const char *const_hashtab_key_t; /* constant generic key type */ 276cd6a6acSopenharmony_citypedef void *hashtab_datum_t; /* generic datum type */ 286cd6a6acSopenharmony_ci 296cd6a6acSopenharmony_citypedef struct hashtab_node *hashtab_ptr_t; 306cd6a6acSopenharmony_ci 316cd6a6acSopenharmony_citypedef struct hashtab_node { 326cd6a6acSopenharmony_ci hashtab_key_t key; 336cd6a6acSopenharmony_ci hashtab_datum_t datum; 346cd6a6acSopenharmony_ci hashtab_ptr_t next; 356cd6a6acSopenharmony_ci} hashtab_node_t; 366cd6a6acSopenharmony_ci 376cd6a6acSopenharmony_citypedef struct hashtab_val { 386cd6a6acSopenharmony_ci hashtab_ptr_t *htable; /* hash table */ 396cd6a6acSopenharmony_ci unsigned int size; /* number of slots in hash table */ 406cd6a6acSopenharmony_ci uint32_t nel; /* number of elements in hash table */ 416cd6a6acSopenharmony_ci unsigned int (*hash_value) (struct hashtab_val * h, const_hashtab_key_t key); /* hash function */ 426cd6a6acSopenharmony_ci int (*keycmp) (struct hashtab_val * h, const_hashtab_key_t key1, const_hashtab_key_t key2); /* key comparison function */ 436cd6a6acSopenharmony_ci} hashtab_val_t; 446cd6a6acSopenharmony_ci 456cd6a6acSopenharmony_citypedef hashtab_val_t *hashtab_t; 466cd6a6acSopenharmony_ci 476cd6a6acSopenharmony_ci/* 486cd6a6acSopenharmony_ci Creates a new hash table with the specified characteristics. 496cd6a6acSopenharmony_ci 506cd6a6acSopenharmony_ci Returns NULL if insufficient space is available or 516cd6a6acSopenharmony_ci the new hash table otherwise. 526cd6a6acSopenharmony_ci */ 536cd6a6acSopenharmony_ciextern hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, 546cd6a6acSopenharmony_ci const_hashtab_key_t 556cd6a6acSopenharmony_ci key), 566cd6a6acSopenharmony_ci int (*keycmp) (hashtab_t h, 576cd6a6acSopenharmony_ci const_hashtab_key_t key1, 586cd6a6acSopenharmony_ci const_hashtab_key_t key2), 596cd6a6acSopenharmony_ci unsigned int size); 606cd6a6acSopenharmony_ci/* 616cd6a6acSopenharmony_ci Inserts the specified (key, datum) pair into the specified hash table. 626cd6a6acSopenharmony_ci 636cd6a6acSopenharmony_ci Returns SEPOL_ENOMEM if insufficient space is available or 646cd6a6acSopenharmony_ci SEPOL_EEXIST if there is already an entry with the same key or 656cd6a6acSopenharmony_ci SEPOL_OK otherwise. 666cd6a6acSopenharmony_ci */ 676cd6a6acSopenharmony_ciextern int hashtab_insert(hashtab_t h, hashtab_key_t k, hashtab_datum_t d); 686cd6a6acSopenharmony_ci 696cd6a6acSopenharmony_ci/* 706cd6a6acSopenharmony_ci Removes the entry with the specified key from the hash table. 716cd6a6acSopenharmony_ci Applies the specified destroy function to (key,datum,args) for 726cd6a6acSopenharmony_ci the entry. 736cd6a6acSopenharmony_ci 746cd6a6acSopenharmony_ci Returns SEPOL_ENOENT if no entry has the specified key or 756cd6a6acSopenharmony_ci SEPOL_OK otherwise. 766cd6a6acSopenharmony_ci */ 776cd6a6acSopenharmony_ciextern int hashtab_remove(hashtab_t h, hashtab_key_t k, 786cd6a6acSopenharmony_ci void (*destroy) (hashtab_key_t k, 796cd6a6acSopenharmony_ci hashtab_datum_t d, 806cd6a6acSopenharmony_ci void *args), void *args); 816cd6a6acSopenharmony_ci 826cd6a6acSopenharmony_ci/* 836cd6a6acSopenharmony_ci Searches for the entry with the specified key in the hash table. 846cd6a6acSopenharmony_ci 856cd6a6acSopenharmony_ci Returns NULL if no entry has the specified key or 866cd6a6acSopenharmony_ci the datum of the entry otherwise. 876cd6a6acSopenharmony_ci */ 886cd6a6acSopenharmony_ciextern hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t k); 896cd6a6acSopenharmony_ci 906cd6a6acSopenharmony_ci/* 916cd6a6acSopenharmony_ci Destroys the specified hash table. 926cd6a6acSopenharmony_ci */ 936cd6a6acSopenharmony_ciextern void hashtab_destroy(hashtab_t h); 946cd6a6acSopenharmony_ci 956cd6a6acSopenharmony_ci/* 966cd6a6acSopenharmony_ci Applies the specified apply function to (key,datum,args) 976cd6a6acSopenharmony_ci for each entry in the specified hash table. 986cd6a6acSopenharmony_ci 996cd6a6acSopenharmony_ci The order in which the function is applied to the entries 1006cd6a6acSopenharmony_ci is dependent upon the internal structure of the hash table. 1016cd6a6acSopenharmony_ci 1026cd6a6acSopenharmony_ci If apply returns a non-zero status, then hashtab_map will cease 1036cd6a6acSopenharmony_ci iterating through the hash table and will propagate the error 1046cd6a6acSopenharmony_ci return to its caller. 1056cd6a6acSopenharmony_ci */ 1066cd6a6acSopenharmony_ciextern int hashtab_map(hashtab_t h, 1076cd6a6acSopenharmony_ci int (*apply) (hashtab_key_t k, 1086cd6a6acSopenharmony_ci hashtab_datum_t d, 1096cd6a6acSopenharmony_ci void *args), void *args); 1106cd6a6acSopenharmony_ci 1116cd6a6acSopenharmony_ciextern void hashtab_hash_eval(hashtab_t h, char *tag); 1126cd6a6acSopenharmony_ci 1136cd6a6acSopenharmony_ci#ifdef __cplusplus 1146cd6a6acSopenharmony_ci} 1156cd6a6acSopenharmony_ci#endif 1166cd6a6acSopenharmony_ci 1176cd6a6acSopenharmony_ci#endif 118