-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.c
228 lines (218 loc) · 7.53 KB
/
trie.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// trie.c -- Basic implementation of a trie abstract data type
// Copyright (C) 2008-2009 Markus Gutschke <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// In addition to these license terms, the author grants the following
// additional rights:
//
// If you modify this program, or any covered work, by linking or
// combining it with the OpenSSL project's OpenSSL library (or a
// modified version of that library), containing parts covered by the
// terms of the OpenSSL or SSLeay licenses, the author
// grants you additional permission to convey the resulting work.
// Corresponding Source for a non-source form of such a combination
// shall include the source code for the parts of OpenSSL used as well
// as that of the covered work.
//
// You may at your option choose to remove this additional permission from
// the work, or from any part of it.
//
// It is possible to build this program in a way that it loads OpenSSL
// libraries at run-time. If doing so, the following notices are required
// by the OpenSSL and SSLeay licenses:
//
// This product includes software developed by the OpenSSL Project
// for use in the OpenSSL Toolkit. (http://www.openssl.org/)
//
// This product includes cryptographic software written by Eric Young
// ([email protected])
//
//
// The most up-to-date version of this program is always available from
// http://shellinabox.com
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "libhttp/trie.h"
#include "logging/logging.h"
struct Trie *newTrie(void (*destructor)(void *, char *), void *arg) {
struct Trie *trie;
check(trie = malloc(sizeof(struct Trie)));
initTrie(trie, destructor, arg);
return trie;
}
void initTrie(struct Trie *trie, void (*destructor)(void *, char *),
void *arg) {
trie->destructor = destructor;
trie->arg = arg;
trie->key = NULL;
trie->value = NULL;
trie->idx = -1;
trie->ch = '\000';
trie->children = NULL;
trie->numChildren = 0;
}
void destroyTrie(struct Trie *trie) {
if (trie) {
free(trie->key);
for (int i = 0; i < trie->numChildren; i++) {
if (trie->destructor && !trie->children[i].ch) {
trie->destructor(trie->arg, trie->children[i].value);
} else {
check(!trie->children[i].value);
}
destroyTrie(&trie->children[i]);
}
free(trie->children);
}
}
void deleteTrie(struct Trie *trie) {
destroyTrie(trie);
free(trie);
}
static void addLeafToTrie(struct Trie *trie, char ch, const char *key, int len,
void *value) {
check (len >= 0);
if (len) {
check(trie->key = malloc(len));
memcpy(trie->key, key, len);
} else {
trie->key = NULL;
}
trie->value = NULL;
trie->idx = len;
trie->ch = ch;
check(trie->children = malloc(sizeof(struct Trie)));
trie->numChildren = 1;
initTrie(trie->children, trie->destructor, trie->arg);
trie->children->value = value;
}
void addToTrie(struct Trie *trie, const char *key, char *value) {
if (trie->numChildren == 0) {
addLeafToTrie(trie, '\000', key, strlen(key), value);
} else {
nextNode:;
int len = strlen(key);
for (int i = 0; i < trie->idx; i++) {
if (key[i] != trie->key[i]) {
struct Trie *child;
check(child = malloc(2*sizeof(struct Trie)));
child->destructor = trie->destructor;
child->arg = trie->arg;
check(child->key = malloc(trie->idx - i - 1));
memcpy(child->key, trie->key + i + 1, trie->idx - i - 1);
child->value = trie->value;
child->idx = trie->idx - i - 1;
child->ch = trie->key[i];
child->children = trie->children;
child->numChildren = trie->numChildren;
trie->value = NULL;
trie->idx = i;
trie->children = child;
trie->numChildren = 2;
child++;
child->destructor = trie->destructor;
child->arg = trie->arg;
if (key[i]) {
addLeafToTrie(child, key[i], key + i + 1, len - i - 1, value);
} else {
initTrie(child, trie->destructor, trie->arg);
child->value = value;
}
return;
}
}
for (int i = 0; i < trie->numChildren; i++) {
if (key[trie->idx] == trie->children[i].ch) {
if (trie->children[i].ch) {
key += trie->idx + 1;
trie = &trie->children[i];
goto nextNode;
} else {
if (trie->destructor) {
trie->destructor(trie->arg, trie->children[i].value);
}
trie->children[i].value = value;
return;
}
}
}
key += trie->idx;
len -= trie->idx;
check(trie->children = realloc(
trie->children, ++trie->numChildren*sizeof(struct Trie)));
struct Trie *newNode = &trie->children[trie->numChildren-1];
if (*key) {
newNode->destructor = trie->destructor;
newNode->arg = trie->arg;
addLeafToTrie(newNode, *key, key + 1, len - 1, value);
} else {
initTrie(newNode, trie->destructor, trie->arg);
newNode->value = value;
}
}
}
char *getFromTrie(const struct Trie *trie, const char *key, char **diff) {
if (diff) {
*diff = NULL;
}
struct Trie *partial = NULL;
char *partialKey = NULL;
for (;;) {
if (trie->idx > 0) {
if (memcmp(trie->key, key, trie->idx)) {
if (diff && partial != NULL) {
*diff = partialKey;
return partial->value;
}
return NULL;
}
key += trie->idx;
}
for (int i = 0; ; i++) {
if (i >= trie->numChildren) {
if (diff && partial != NULL) {
// If the caller provided a "diff" pointer, then we allow partial
// matches for the longest possible prefix that is a key in the
// trie. Upon return, the "diff" pointer points to the first
// character in the key does not match.
*diff = partialKey;
return partial->value;
}
return NULL;
} else if (*key == trie->children[i].ch) {
if (!*key) {
if (diff) {
*diff = (char *)key;
}
return trie->children[i].value;
}
for (int j = i + 1; j < trie->numChildren; j++) {
if (!trie->children[j].ch) {
partial = trie->children + j;
partialKey = (char *)key;
break;
}
}
trie = &trie->children[i];
key++;
break;
} else if (!trie->children[i].ch) {
partial = trie->children + i;
partialKey = (char *)key;
}
}
}
}