xref: /aosp_15_r20/external/selinux/libsepol/src/symtab.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 
2 /* Author : Stephen Smalley, <[email protected]> */
3 
4 /* FLASK */
5 
6 /*
7  * Implementation of the symbol table type.
8  */
9 
10 #include <string.h>
11 
12 #include "private.h"
13 
14 #include <sepol/policydb/hashtab.h>
15 #include <sepol/policydb/symtab.h>
16 
17 ignore_unsigned_overflow_
symhash(hashtab_t h,const_hashtab_key_t key)18 static unsigned int symhash(hashtab_t h, const_hashtab_key_t key)
19 {
20 	unsigned int hash = 5381;
21 	unsigned char c;
22 
23 	while ((c = *(unsigned const char *)key++))
24 		hash = ((hash << 5) + hash) ^ c;
25 
26 	return hash & (h->size - 1);
27 }
28 
symcmp(hashtab_t h,const_hashtab_key_t key1,const_hashtab_key_t key2)29 static int symcmp(hashtab_t h
30 		  __attribute__ ((unused)), const_hashtab_key_t key1,
31 		  const_hashtab_key_t key2)
32 {
33 	return strcmp(key1, key2);
34 }
35 
symtab_init(symtab_t * s,unsigned int size)36 int symtab_init(symtab_t * s, unsigned int size)
37 {
38 	s->table = hashtab_create(symhash, symcmp, size);
39 	if (!s->table)
40 		return -1;
41 	s->nprim = 0;
42 	return 0;
43 }
44 
symtab_destroy(symtab_t * s)45 void symtab_destroy(symtab_t * s)
46 {
47 	if (!s)
48 		return;
49 	if (s->table)
50 		hashtab_destroy(s->table);
51 	return;
52 }
53 /* FLASK */
54