1 /*
2  * Copyright 2014 Tresys Technology, LLC. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice,
8  *       this list of conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice,
11  *       this list of conditions and the following disclaimer in the documentation
12  *       and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17  * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * The views and conclusions contained in the software and documentation are those
26  * of the authors and should not be interpreted as representing official policies,
27  * either expressed or implied, of Tresys Technology, LLC.
28  */
29 
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include "cil_mem.h"
34 #include "cil_strpool.h"
35 
36 #include "cil_log.h"
37 #define CIL_STRPOOL_TABLE_SIZE 1 << 15
38 
39 struct cil_strpool_entry {
40 	char *str;
41 };
42 
43 static hashtab_t cil_strpool_tab = NULL;
44 
cil_strpool_hash(hashtab_t h,hashtab_key_t key)45 static unsigned int cil_strpool_hash(hashtab_t h, hashtab_key_t key)
46 {
47 	char *p, *keyp;
48 	size_t size;
49 	unsigned int val;
50 
51 	val = 0;
52 	keyp = (char*)key;
53 	size = strlen(keyp);
54 	for (p = keyp; ((size_t) (p - keyp)) < size; p++)
55 		val =
56 		    (val << 4 | (val >> (8 * sizeof(unsigned int) - 4))) ^ (*p);
57 	return val & (h->size - 1);
58 }
59 
cil_strpool_compare(hashtab_t h,hashtab_key_t key1,hashtab_key_t key2)60 static int cil_strpool_compare(hashtab_t h __attribute__ ((unused)), hashtab_key_t key1, hashtab_key_t key2)
61 {
62 	char *keyp1 = (char*)key1;
63 	char *keyp2 = (char*)key2;
64 	return strcmp(keyp1, keyp2);
65 }
66 
cil_strpool_add(const char * str)67 char *cil_strpool_add(const char *str)
68 {
69 	struct cil_strpool_entry *strpool_ref = NULL;
70 
71 	strpool_ref = hashtab_search(cil_strpool_tab, (hashtab_key_t)str);
72 	if (strpool_ref == NULL) {
73 		strpool_ref = cil_malloc(sizeof(*strpool_ref));
74 		strpool_ref->str = cil_strdup(str);
75 		int rc = hashtab_insert(cil_strpool_tab, (hashtab_key_t)strpool_ref->str, strpool_ref);
76 		if (rc != SEPOL_OK) {
77 			(*cil_mem_error_handler)();
78 		}
79 	}
80 
81 	return strpool_ref->str;
82 }
83 
cil_strpool_entry_destroy(hashtab_key_t k,hashtab_datum_t d,void * args)84 static int cil_strpool_entry_destroy(hashtab_key_t k __attribute__ ((unused)), hashtab_datum_t d, void *args __attribute__ ((unused)))
85 {
86 	struct cil_strpool_entry *strpool_ref = (struct cil_strpool_entry*)d;
87 	free(strpool_ref->str);
88 	free(strpool_ref);
89 	return SEPOL_OK;
90 }
91 
cil_strpool_init(void)92 void cil_strpool_init(void)
93 {
94 	cil_strpool_tab = hashtab_create(cil_strpool_hash, cil_strpool_compare, CIL_STRPOOL_TABLE_SIZE);
95 	if (cil_strpool_tab == NULL) {
96 		(*cil_mem_error_handler)();
97 	}
98 }
99 
cil_strpool_destroy(void)100 void cil_strpool_destroy(void)
101 {
102 	hashtab_map(cil_strpool_tab, cil_strpool_entry_destroy, NULL);
103 	hashtab_destroy(cil_strpool_tab);
104 }
105