-
Notifications
You must be signed in to change notification settings - Fork 46
/
ac.c
619 lines (532 loc) · 14 KB
/
ac.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
* ac.c
*
* Implementation of the Aho-Corasick algorithm.
*
* NOTES:
* 8/94 - Original Implementation (Sean Davis)
* 9/94 - Redid Implementation (James Knight)
* 3/96 - Modularized the code (James Knight)
* 7/96 - Finished the modularization (James Knight)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ac.h"
/*
* ac_alloc
*
* Creates a new AC_STRUCT structure and initializes its fields.
*
* Parameters: none.
*
* Returns: A dynamically allocated AC_STRUCT structure.
*/
AC_STRUCT *ac_alloc(void)
{
AC_STRUCT *node;
if ((node = malloc(sizeof(AC_STRUCT))) == NULL)
return NULL;
memset(node, 0, sizeof(AC_STRUCT));
if ((node->tree = malloc(sizeof(ACTREE_NODE))) == NULL) {
free(node);
return NULL;
}
memset(node->tree, 0, sizeof(ACTREE_NODE));
return node;
}
/*
* ac_add_string
*
* Adds a string to the AC_STRUCT structure's keyword tree.
*
* NOTE: The `id' value given must be unique to any of the strings
* added to the tree, and must be a small integer greater than
* 0 (since it is used to index an array holding information
* about each of the strings).
*
* The best id's to use are to number the strings from 1 to K.
*
* Parameters: node - an AC_STRUCT structure
* P - the sequence
* M - the sequence length
* id - the sequence identifier
*
* Returns: non-zero on success, zero on error.
*/
int ac_add_string(AC_STRUCT *node, char *P, int M, int id)
{
int i, j, newsize;
AC_TREE tnode, child, back, newnode, list, tail;
/*
* Return a zero if a previous error had occurred, or if the
* given id equals zero. An id value of zero is used by the
* algorithm to signal that no pattern ends at a node in the
* keyword tree. So, it can't be used as a pattern's id.
*/
if (node->errorflag || id == 0)
return 0;
P--; /* Shift to make sequence be P[1],...,P[M] */
/*
* Allocate space for the new string's information.
*/
if (node->Psize <= id) {
if (node->Psize == 0) {
newsize = (id >= 16 ? id + 1 : 16);
node->Plengths = malloc(newsize * sizeof(int));
}
else {
newsize = node->Psize + id + 1;
node->Plengths = realloc(node->Plengths, newsize * sizeof(int));
}
if (node->Plengths == NULL) {
node->errorflag = 1;
return 0;
}
for (i=node->Psize; i < newsize; i++)
node->Plengths[i] = 0;
node->Psize = newsize;
}
if (node->Plengths[id] != 0) {
fprintf(stderr, "Error in Aho-Corasick preprocessing. "
"Duplicate identifiers\n");
return 0;
}
/*
* Add the string to the keyword tree.
*/
tnode = node->tree;
for (i=1; i <= M; i++) {
/*
* Find the child whose character is P[i].
*/
back = NULL;
child = tnode->children;
while (child != NULL && child->ch < P[i]) {
back = child;
child = child->sibling;
}
if (child == NULL || child->ch != P[i])
break;
tnode = child;
#ifdef STATS
node->prep_old_edges++;
#endif
}
/*
* If only part of the pattern exists in the tree, add the
* rest of the pattern to the tree.
*/
if (i <= M) {
list = tail = NULL;
for (j=i; j <= M; j++) {
if ((newnode = malloc(sizeof(ACTREE_NODE))) == NULL)
break;
memset(newnode, 0, sizeof(ACTREE_NODE));
newnode->ch = P[j];
if (list == NULL)
list = tail = newnode;
else
tail = tail->children = newnode;
#ifdef STATS
node->prep_new_edges++;
#endif
}
if (j <= M) {
while (list != NULL) {
tail = list->children;
free(list);
list = tail;
}
return 0;
}
list->sibling = child;
if (back == NULL)
tnode->children = list;
else
back->sibling = list;
tnode = tail;
}
tnode->matchid = id;
node->Plengths[id] = M;
node->ispreprocessed = 0;
return 1;
}
/*
* ac_del_string
*
* Deletes a string from the keyword tree.
*
* Parameters: node - an AC_STRUCT structure
* P - the sequence to be deleted
* M - its length
* id - its identifier
*
* Returns: non-zero on success, zero on error.
*/
int ac_del_string(AC_STRUCT *node, char *P, int M, int id)
{
int i, flag;
AC_TREE tnode, tlast, tback, child, back;
if (node->errorflag || id > node->Psize || node->Plengths[id] == 0)
return 0;
P--; /* Shift to make sequence be P[1],...,P[M] */
/*
* Scan the tree for the path corresponding to the keyword to be deleted.
*/
flag = 1;
tlast = tnode = node->tree;
tback = NULL;
for (i=1; i <= M; i++) {
/*
* Find the child matching P[i]. It must be there.
*/
child = tnode->children;
back = NULL;
while (child != NULL && child->ch != P[i]) {
back = child;
child = child->sibling;
}
if (child == NULL) {
fprintf(stderr, "Error in Aho-Corasick preprocessing. String to be "
"deleted is not in tree.\n");
return 0;
}
/*
* Try to find the point where the pattern to be deleted branches off
* from the paths of the other patterns in the tree. This point must
* be at the latest node which satisfies one of these two conditions:
*
* 1) Another pattern ends at that node (and so
* `child->matchid != 0'). In this case, the branch point is
* just below this node and so the children of this node
* should be removed.
* 2) A node has other siblings. In this case, the node itself
* is the branch point, and it and its children should be
* removed.
*/
if (i < M && child->matchid != 0) {
flag = 1;
tlast = child;
}
else if (back != NULL || child->sibling != NULL) {
flag = 2;
tlast = child;
tback = (back == NULL ? tnode : back);
}
tnode = child;
}
/*
* If the node corresponding to the end of the keyword has children,
* then the tree should not be altered, except to remove the keyword's
* identifier from the tree.
*
* Otherwise, apply the appropriate removal, as described above.
*/
if (tnode->children != NULL) {
tnode->matchid = 0;
}
else {
if (flag == 1) {
child = tlast->children;
tlast->children = NULL;
tlast = child;
}
else {
if (tback->children == tlast)
tback->children = tlast->sibling;
else
tback->sibling = tlast->sibling;
}
while (tlast != NULL) {
child = tlast->children;
free(tlast);
tlast = child;
}
}
node->Plengths[id] = 0;
node->ispreprocessed = 0;
return 1;
}
/*
* ac_prep
*
* Compute the failure and output links for the keyword tree.
*
* Parameters: node - an AC_STRUCT structure
*
* Returns: non-zero on success, zero on error.
*/
int ac_prep(AC_STRUCT *node)
{
char x;
AC_TREE v, vprime, w, wprime, root, front, back, child;
if (node->errorflag)
return 0;
/*
* The failure link and output link computation requires a breadth-first
* traversal of the keyword tree. And, to do that, we need a queue of
* the nodes yet to be processed.
*
* The `faillink' fields will be used as the pointers for the queue
* of nodes to be computed (since the failure link is only set after
* the node is removed from the queue).
*
* The `outlink' fields will be used as the pointers to a node's parent
* for nodes in the queue (since the output link is also only set after
* the node is removed from the queue).
*/
root = node->tree;
front = back = root;
front->faillink = NULL;
front->outlink = NULL;
while (front != NULL) {
v = front;
x = v->ch;
vprime = v->outlink;
/*
* Add the node's children to the queue.
*/
for (child=v->children; child != NULL; child=child->sibling) {
child->outlink = v;
back->faillink = child;
back = child;
}
back->faillink = NULL;
front = front->faillink;
v->faillink = v->outlink = NULL;
/*
* Set the failure and output links.
*/
if (v == root)
;
else if (vprime == root)
v->faillink = root;
else {
/*
* Find the find link in the failure link chain which has a child
* labeled with x.
*/
wprime = NULL;
w = vprime->faillink;
while (1) {
wprime = w->children;
while (wprime != NULL && wprime->ch < x)
wprime = wprime->sibling;
if ((wprime != NULL && wprime->ch == x) || w == root)
break;
w = w->faillink;
#ifdef STATS
node->prep_fail_compares++;
#endif
}
#ifdef STATS
node->prep_fail_compares++;
#endif
if (wprime != NULL && wprime->ch == x)
v->faillink = wprime;
else
v->faillink = root;
if (v->matchid != 0) {
if (v->faillink->matchid != 0)
v->outlink = v->faillink;
else
v->outlink = v->faillink->outlink;
}
}
}
node->ispreprocessed = 1;
node->initflag = 0;
return 1;
}
/*
* ac_search_init
*
* Initializes the variables used during an Aho-Corasick search.
* See ac_search for an example of how it should be used.
*
* Parameters: node - an AC_STRUCT structure
* T - the sequence to be searched
* N - the length of the sequence
*
* Returns: nothing.
*/
void ac_search_init(AC_STRUCT *node, char *T, int N)
{
if (node->errorflag)
return;
else if (!node->ispreprocessed) {
fprintf(stderr, "Error in Aho-Corasick search. The preprocessing "
"has not been completed.\n");
return;
}
node->T = T - 1; /* Shift to make sequence be T[1],...,T[N] */
node->N = N;
node->c = 1;
node->w = node->tree;
node->output = NULL;
node->initflag = 1;
node->endflag = 0;
}
/*
* ac_search
*
* Scans a text to look for the next occurrence of one of the patterns
* in the text. An example of how this search should be used is the
* following:
*
* s = T;
* len = N;
* contflag = 0;
* ac_search_init(node, T, N);
* while ((s = ac_search(node, &matchlen, &matchid) != NULL) {
* >>> Pattern `matchid' matched from `s' to `s + matchlen - 1'. <<<
* }
*
* where `node', `T' and `N' are assumed to be initialized appropriately.
*
* Parameters: node - a preprocessed AC_STRUCT structure
* length_out - where to store the new match's length
* id_out - where to store the identifier of the
* pattern that matched
*
* Returns: the left end of the text that matches a pattern, or NULL
* if no match occurs. (It also stores values in `*length_out',
* and `*id_out' giving the match's length and pattern identifier.
*/
char *ac_search(AC_STRUCT *node, int *length_out, int *id_out)
{
int c, N, id;
char *T;
AC_TREE w, wprime, root;
if (node->errorflag)
return NULL;
else if (!node->ispreprocessed) {
fprintf(stderr, "Error in Aho-Corasick search. The preprocessing "
"has not been completed.\n");
return NULL;
}
else if (!node->initflag) {
fprintf(stderr, "Error in Aho-Corasick search. ac_search_init was not "
"called.\n");
return NULL;
}
else if (node->endflag)
return NULL;
T = node->T;
N = node->N;
c = node->c;
w = node->w;
root = node->tree;
/*
* If the last call to ac_search returned a match, check for another
* match ending at the same right endpoint (denoted by a non-NULL
* output link).
*/
if (node->output != NULL) {
node->output = node->output->outlink;
#ifdef STATS
node->outlinks_traversed++;
#endif
if (node->output != NULL) {
id = node->output->matchid;
if (id_out)
*id_out = id;
if (length_out)
*length_out = node->Plengths[id];
return &T[c] - node->Plengths[id];
}
}
/*
* Run the search algorithm, stopping at the first position where a
* match to one of the patterns occurs.
*/
while (c <= N) {
/*
* Try to match the next input character to a child in the tree.
*/
wprime = w->children;
while (wprime != NULL && wprime->ch != T[c])
wprime = wprime->sibling;
#ifdef STATS
node->num_compares++;
#endif
/*
* If the match fails, then either use the failure link (if not
* at the root), or move to the next character since no prefix
* of any pattern ends with character T[c].
*/
if (wprime == NULL) {
if (w == root)
c++;
else {
w = w->faillink;
#ifdef STATS
node->num_failures++;
#endif
}
}
else {
/*
* If we could match the input, move down the tree and to the
* next input character, and see if that match completes the
* match to a pattern (when matchid != 0 or outlink != NULL).
*/
c++;
w = wprime;
#ifdef STATS
node->edges_traversed++;
#endif
if (w->matchid != 0)
node->output = w;
else if (w->outlink != NULL) {
node->output = w->outlink;
#ifdef STATS
node->outlinks_traversed++;
#endif
}
if (node->output != NULL) {
id = node->output->matchid;
if (id_out)
*id_out = id;
if (length_out)
*length_out = node->Plengths[id];
node->w = w;
node->c = c;
return &T[c] - node->Plengths[id];
}
}
}
node->c = c;
node->endflag = 1;
return NULL;
}
/*
* ac_free
*
* Free up the allocated AC_STRUCT structure.
*
* Parameters: node - a AC_STRUCT structure
*
* Returns: nothing.
*/
void ac_free(AC_STRUCT *node)
{
AC_TREE front, back, next;
if (node == NULL)
return;
if (node->tree != NULL) {
front = back = node->tree;
while (front != NULL) {
back->sibling = front->children;
while (back->sibling != NULL)
back = back->sibling;
next = front->sibling;
free(front);
front = next;
}
}
if (node->Plengths != NULL)
free(node->Plengths);
free(node);
}