summaryrefslogtreecommitdiff
path: root/src/aho-corasick.c
blob: c69fafd3e8afe7ae88c7f870fb9b1ab9dc3d31d0 (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// TODO ac_match_range() -- match within a range of a buffer, file, ...
// TODO streaming support -- match over a socket
// TODO ac_strerror() -- report errors better

// Aho-Corasick searching algorithm implementation
// https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include "aho-corasick.h"

struct pattern_node {
	ac_node_t    *children[AC_ALPHABET_SIZE];
	ac_node_t    *fail;
	ac_pattern_t *matches;
};

struct pattern_entry {
	const char   *id;
	uint8_t      *pattern;
	size_t        len;
	ac_pattern_t *next;
};

struct ac_context {
	ac_node_t *root;
};

static ac_node_t *node_new(void) {
	return calloc(1, sizeof(ac_node_t));
}

ac_context_t *ac_new(void) {
	ac_context_t *ctx = calloc(1, sizeof(ac_context_t));

	ctx->root = node_new();

	return ctx;
}

static void free_node(ac_node_t *node) {
	if (!node) {
		return;
	}

	for (int i = 0; i < AC_ALPHABET_SIZE; i++) {
		free_node(node->children[i]);
	}

	ac_pattern_t *p = node->matches;
	while(p) {
		ac_pattern_t *next = p->next;
		free(p->pattern);
		free(p);
		p = next;
	}

	free(node);
}

void ac_free(ac_context_t *ctx) {
	if (!ctx) {
		return;
	}

	free_node(ctx->root);
	free(ctx);
}

int ac_add_pattern(ac_context_t *ctx, const char *id, const uint8_t *pattern, size_t len) {
	if (!ctx || !id || !pattern || len == 0) {
		return -1;
	}

	ac_node_t *node = ctx->root;

	for (size_t i = 0; i < len; i++) {
		uint8_t c = pattern[i];

		if (!node->children[c]) {
			node->children[c] = node_new();
		}

		node = node->children[c];
	}

	for (ac_pattern_t *p = node->matches; p; p = p->next) {
		if (p->len == len &&
			memcmp(p->pattern, pattern, len) == 0 &&
			strcmp(p->id, id) == 0) {
			return 0; // duplicate pattern. don't add.
		}
	}

	ac_pattern_t *entry = malloc(sizeof(*entry));
	entry->id = id;
	entry->len = len;
	entry->pattern = malloc(len);
	if (!entry->pattern) {
		return -1;
	} else {
		memcpy(entry->pattern, pattern, len);
	}
	entry->next = node->matches;
	node->matches = entry;

	return 0;
}

struct node_queue {
	ac_node_t **data;
	size_t      head;
	size_t      tail;
	size_t      cap;
};

static struct node_queue *queue_new(size_t cap) {
	struct node_queue *q = calloc(1, sizeof(*q));

	if (cap == 0) {
		cap = 64;
	}

	q->data = malloc(sizeof(void *) * cap);
	q->cap = cap;

	return q;
}

static void queue_free(struct node_queue *q) {
	free(q->data);
	free(q);
}

static void queue_push(struct node_queue *q, ac_node_t *node) {
	if (q->tail >= q->cap) {
		size_t new_cap = q->cap ? q->cap * 2 : 64;
		ac_node_t **new_data = realloc(q->data, sizeof(void *) * new_cap);

		if (!new_data) {
			fprintf(stderr, "queue_push: realloc failure\n");
			exit(EXIT_FAILURE); // TODO error flag instead of exit
		}

		q->data = new_data;
		q->cap = new_cap;
	}

	q->data[q->tail++] = node;
}

static ac_node_t *queue_pop(struct node_queue *q) {
	return q->head < q->tail ? q->data[q->head++] : NULL;
}

static bool queue_empty(struct node_queue *q) {
	return q->head == q->tail;
}

int ac_build(ac_context_t *ctx) {
	if (!ctx || !ctx->root) {
		return -1;
	}

	struct node_queue *q = queue_new(0);
	if (!q) {
		return -1;
	}

	ac_node_t *root = ctx->root;

	for (int i = 0; i < AC_ALPHABET_SIZE; i++) {
		if (root->children[i]) {
			root->children[i]->fail = root;
			queue_push(q, root->children[i]);
		}
	}

	while (!queue_empty(q)) {
		ac_node_t *node = queue_pop(q);

		for (int i = 0; i < AC_ALPHABET_SIZE; i++) {
			ac_node_t *child = node->children[i];
			if (!child) {
				continue;
			}

			ac_node_t *fail = node->fail;
			while (fail && !fail->children[i]) {
				fail = fail->fail;
			}

			if (fail) {
				child->fail = fail->children[i];
			} else {
				child->fail = root;
			}

			queue_push(q, child);
		}
	}

	queue_free(q);

	return 0;
}

int ac_match(ac_context_t *ctx, const uint8_t *data, size_t len, ac_callback callback, void *user_data) {
	if (!ctx || !data || !callback) {
		// TODO error context
		return -1;
	}

	ac_node_t *node = ctx->root;

	for (size_t i = 0; i < len; i++) {
		uint8_t c = data[i];

		while (node && !node->children[c]) {
			node = node->fail;
		}

		if (!node) {
			node = ctx->root;
		} else {
			node = node->children[c];
		}

		ac_node_t *temp = node;
		while (temp) {
			for (ac_pattern_t *p = temp->matches; p; p = p->next) {
				ac_match_t match = {
					.id     = p->id,
					.offset = i + 1 - p->len,
					.len    = p->len
				};

				callback(&match, user_data);
			}

			temp = temp->fail;
		}
	}

	return 0;
}

int ac_match_fd(ac_context_t *ctx, int fd, ac_callback callback, void *user_data) {
	if (!ctx || fd < 0 || !callback) {
		// TODO error context
		return -1;
	}

	char buffer[AC_BUF_SIZE];
	size_t offset = 0;
	ssize_t n;

	ac_node_t *node = ctx->root;

	while ((n = read(fd, buffer, sizeof(buffer))) > 0) {
		for (ssize_t i = 0; i < n; i++) {
			uint8_t c = buffer[i];

			while (node && !node->children[c]) {
				node = node->fail;
			}

			if (!node) {
				node = ctx->root;
			} else {
				node = node->children[c];
			}

			ac_node_t *temp = node;
			while (temp) {
				for (ac_pattern_t *p = temp->matches; p; p = p->next) {
					ac_match_t match = {
						.id     = p->id,
						.offset = (offset + i + 1) - p->len,
						.len    = p->len
					};

					callback(&match, user_data);
				}

				temp = temp->fail;
			}
		}

		offset += n;
	}

	return (n < 0) ? -1 : 0;
}

int ac_match_path(ac_context_t *ctx, const char *path, ac_callback callback, void *user_data) {
	if (!ctx || !path || !callback) {
		return -1;
	}

	int fd = open(path, O_RDONLY);
	if (fd < 0) {
		return -1;
	}

	int ret = ac_match_fd(ctx, fd, callback, user_data);

	close(fd);

	return ret;
}