1 // hashmap.c -- Basic implementation of a hashmap abstract data type
2 // Copyright (C) 2008-2009 Markus Gutschke <markus@shellinabox.com>
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 2 as
6 // published by the Free Software Foundation.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License along
14 // with this program; if not, write to the Free Software Foundation, Inc.,
15 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 // In addition to these license terms, the author grants the following
20 // If you modify this program, or any covered work, by linking or
21 // combining it with the OpenSSL project's OpenSSL library (or a
22 // modified version of that library), containing parts covered by the
23 // terms of the OpenSSL or SSLeay licenses, the author
24 // grants you additional permission to convey the resulting work.
25 // Corresponding Source for a non-source form of such a combination
26 // shall include the source code for the parts of OpenSSL used as well
27 // as that of the covered work.
29 // You may at your option choose to remove this additional permission from
30 // the work, or from any part of it.
32 // It is possible to build this program in a way that it loads OpenSSL
33 // libraries at run-time. If doing so, the following notices are required
34 // by the OpenSSL and SSLeay licenses:
36 // This product includes software developed by the OpenSSL Project
37 // for use in the OpenSSL Toolkit. (http://www.openssl.org/)
39 // This product includes cryptographic software written by Eric Young
40 // (eay@cryptsoft.com)
43 // The most up-to-date version of this program is always available from
44 // http://shellinabox.com
51 #include "libhttp/hashmap.h"
52 #include "logging/logging.h"
54 struct HashMap *newHashMap(void (*destructor)(void *arg, char *key,
57 struct HashMap *hashmap;
58 check(hashmap = malloc(sizeof(struct HashMap)));
59 initHashMap(hashmap, destructor, arg);
63 void initHashMap(struct HashMap *hashmap,
64 void (*destructor)(void *arg, char *key, char *value),
66 hashmap->destructor = destructor;
68 hashmap->entries = NULL;
70 hashmap->numEntries = 0;
73 void destroyHashMap(struct HashMap *hashmap) {
75 for (int i = 0; i < hashmap->mapSize; i++) {
76 if (hashmap->entries[i]) {
77 for (int j = 0; hashmap->entries[i][j].key; j++) {
78 if (hashmap->destructor) {
79 hashmap->destructor(hashmap->arg,
80 (char *)hashmap->entries[i][j].key,
81 (char *)hashmap->entries[i][j].value);
84 free(hashmap->entries[i]);
87 free(hashmap->entries);
91 void deleteHashMap(struct HashMap *hashmap) {
92 destroyHashMap(hashmap);
96 static unsigned int stringHashFunc(const char *s) {
99 h = 31*h + *(unsigned char *)s++;
104 const void *addToHashMap(struct HashMap *hashmap, const char *key,
106 if (hashmap->numEntries + 1 > (hashmap->mapSize * 8)/10) {
107 struct HashMap newMap;
108 newMap.numEntries = hashmap->numEntries;
109 if (hashmap->mapSize == 0) {
111 } else if (hashmap->mapSize < 1024) {
112 newMap.mapSize = 2*hashmap->mapSize;
114 newMap.mapSize = hashmap->mapSize + 1024;
116 check(newMap.entries = calloc(sizeof(void *), newMap.mapSize));
117 for (int i = 0; i < hashmap->mapSize; i++) {
118 if (!hashmap->entries[i]) {
121 for (int j = 0; hashmap->entries[i][j].key; j++) {
122 addToHashMap(&newMap, hashmap->entries[i][j].key,
123 hashmap->entries[i][j].value);
125 free(hashmap->entries[i]);
127 free(hashmap->entries);
128 hashmap->entries = newMap.entries;
129 hashmap->mapSize = newMap.mapSize;
130 hashmap->numEntries = newMap.numEntries;
132 unsigned hash = stringHashFunc(key);
133 int idx = hash % hashmap->mapSize;
135 if (hashmap->entries[idx]) {
136 for (i = 0; hashmap->entries[idx][i].key; i++) {
137 if (!strcmp(hashmap->entries[idx][i].key, key)) {
138 if (hashmap->destructor) {
139 hashmap->destructor(hashmap->arg,
140 (char *)hashmap->entries[idx][i].key,
141 (char *)hashmap->entries[idx][i].value);
143 hashmap->entries[idx][i].key = key;
144 hashmap->entries[idx][i].value = value;
149 check(hashmap->entries[idx] = realloc(hashmap->entries[idx],
150 (i+2)*sizeof(*hashmap->entries[idx])));
151 hashmap->entries[idx][i].key = key;
152 hashmap->entries[idx][i].value = value;
153 memset(&hashmap->entries[idx][i+1], 0, sizeof(*hashmap->entries[idx]));
154 hashmap->numEntries++;
158 void deleteFromHashMap(struct HashMap *hashmap, const char *key) {
159 if (hashmap->mapSize == 0) {
162 unsigned hash = stringHashFunc(key);
163 int idx = hash % hashmap->mapSize;
164 if (!hashmap->entries[idx]) {
167 for (int i = 0; hashmap->entries[idx][i].key; i++) {
168 if (!strcmp(hashmap->entries[idx][i].key, key)) {
170 while (hashmap->entries[idx][j].key) {
173 if (hashmap->destructor) {
174 hashmap->destructor(hashmap->arg,
175 (char *)hashmap->entries[idx][i].key,
176 (char *)hashmap->entries[idx][i].value);
179 memcpy(&hashmap->entries[idx][i], &hashmap->entries[idx][j-1],
180 sizeof(*hashmap->entries[idx]));
182 memset(&hashmap->entries[idx][j-1], 0, sizeof(*hashmap->entries[idx]));
183 check(--hashmap->numEntries >= 0);
188 char **getRefFromHashMap(const struct HashMap *hashmap, const char *key) {
189 if (hashmap->mapSize == 0) {
192 unsigned hash = stringHashFunc(key);
193 int idx = hash % hashmap->mapSize;
194 if (!hashmap->entries[idx]) {
197 for (int i = 0; hashmap->entries[idx][i].key; i++) {
198 if (!strcmp(hashmap->entries[idx][i].key, key)) {
199 return (char **)&hashmap->entries[idx][i].value;
205 const char *getFromHashMap(const struct HashMap *hashmap, const char *key) {
206 char **ref = getRefFromHashMap(hashmap, key);
207 return ref ? *ref : NULL;
210 void iterateOverHashMap(struct HashMap *hashmap,
211 int (*fnc)(void *arg, const char *key, char **value),
213 for (int i = 0; i < hashmap->mapSize; i++) {
214 if (hashmap->entries[i]) {
216 while (hashmap->entries[i][count].key) {
219 for (int j = 0; j < count; j++) {
220 if (!fnc(arg, hashmap->entries[i][j].key,
221 (char **)&hashmap->entries[i][j].value)) {
222 if (hashmap->destructor) {
223 hashmap->destructor(hashmap->arg,
224 (char *)hashmap->entries[i][j].key,
225 (char *)hashmap->entries[i][j].value);
228 memcpy(&hashmap->entries[i][j], &hashmap->entries[i][count-1],
229 sizeof(*hashmap->entries[i]));
231 memset(&hashmap->entries[i][count-1], 0,
232 sizeof(*hashmap->entries[i]));
241 int getHashmapSize(const struct HashMap *hashmap) {
242 return hashmap->numEntries;