2 MiddleMan filtering proxy server
3 Copyright (C) 2002 Jason McLaughlin
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 return a slot index for a string
29 unsigned int hash_key(int max, char *string)
47 HASH_TABLE *hash_create(int size)
49 HASH_TABLE *hash_table;
51 hash_table = xmalloc(sizeof(HASH_TABLE));
55 hash_table->size = size;
57 hash_table->hash_list = xmalloc(sizeof(struct HASH_LIST *) * size);
59 memset(hash_table->hash_list, 0, sizeof(struct HASH_LIST *) * size);
65 insert item into hash table
67 void hash_insert(HASH_TABLE * hash_table, char *ref, void *string)
70 struct HASH_LIST *hash_list;
74 if (hash_search(hash_table, ref) != NULL)
77 key = hash_key(hash_table->size, ref);
79 hash_list = hash_table->hash_list[key];
81 HASH_LIST_ADD(hash_list, ref, string);
83 if (hash_table->hash_list[key] == NULL)
84 hash_table->hash_list[key] = hash_list;
88 purge item from hash table
90 void hash_purge(HASH_TABLE * hash_table, char *ref)
93 struct HASH_LIST *hash_list, *prev = NULL;
97 key = hash_key(hash_table->size, ref);
99 hash_list = hash_table->hash_list[key];
101 while (hash_list != NULL) {
102 if (!strcasecmp(hash_list->ref, ref)) {
103 if (hash_table->hash_list[key] == hash_list)
104 hash_table->hash_list[key] = hash_list->next;
105 else if (prev != NULL)
106 prev->next = hash_list->next;
108 xfree(hash_list->ref);
109 xfree(hash_list->data);
116 hash_list = hash_list->next;
121 search for item in hash table
123 char *hash_search(HASH_TABLE * hash_table, char *ref)
126 struct HASH_LIST *hash_list;
130 key = hash_key(hash_table->size, ref);
132 hash_list = hash_table->hash_list[key];
134 while (hash_list != NULL) {
135 if (!strcasecmp(hash_list->ref, ref)) {
138 return hash_list->data;
141 hash_list = hash_list->next;
150 wipe out a hash table and free all used memory
152 void hash_destroy(HASH_TABLE * hash_table)
155 struct HASH_LIST *hash_list, *tmp_list;
159 for (i = 0; i < hash_table->size; i++) {
160 hash_list = hash_table->hash_list[i];
162 while (hash_list != NULL) {
163 tmp_list = hash_list->next;
165 xfree(hash_list->ref);
166 xfree(hash_list->data);
169 hash_list = tmp_list;
173 xfree(hash_table->hash_list);
178 remove old items from hash table
180 int hash_expire(HASH_TABLE * hash_table, unsigned int age)
184 struct HASH_LIST *hash_list, *tmp_list, *prev = NULL;
190 for (i = 0; i < hash_table->size; i++) {
191 hash_list = hash_table->hash_list[i];
193 while (hash_list != NULL) {
194 tmp_list = hash_list->next;
196 if (hash_list->age <= (t - age)) {
197 if (hash_table->hash_list[i] == hash_list)
198 hash_table->hash_list[i] = tmp_list;
199 else if (prev != NULL)
200 prev->next = tmp_list;
202 xfree(hash_list->ref);
203 xfree(hash_list->data);
210 hash_list = tmp_list;