1/* 2 * This file has been copied from commit e7ac713d^ in the GNU grep git 3 * repository. A few small changes have been made to adapt the code to 4 * Git. 5 */ 6 7/* kwset.c - search for any of a set of keywords. 8 Copyright 1989, 1998, 2000, 2005 Free Software Foundation, Inc. 9 10 This program is free software; you can redistribute it and/or modify 11 it under the terms of the GNU General Public License as published by 12 the Free Software Foundation; either version 2, or (at your option) 13 any later version. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, see <http://www.gnu.org/licenses/>. */ 22 23/* Written August 1989 by Mike Haertel. 24 The author may be reached (Email) at the address mike@ai.mit.edu, 25 or (US mail) as Mike Haertel c/o Free Software Foundation. */ 26 27/* The algorithm implemented by these routines bears a startling resemblance 28 to one discovered by Beate Commentz-Walter, although it is not identical. 29 See "A String Matching Algorithm Fast on the Average," Technical Report, 30 IBM-Germany, Scientific Center Heidelberg, Tiergartenstrasse 15, D-6900 31 Heidelberg, Germany. See also Aho, A.V., and M. Corasick, "Efficient 32 String Matching: An Aid to Bibliographic Search," CACM June 1975, 33 Vol. 18, No. 6, which describes the failure function used below. */ 34 35#include"cache.h" 36 37#include"kwset.h" 38#include"compat/obstack.h" 39 40#define NCHAR (UCHAR_MAX + 1) 41#define obstack_chunk_alloc xmalloc 42#define obstack_chunk_free free 43 44#define U(c) ((unsigned char) (c)) 45 46/* Balanced tree of edges and labels leaving a given trie node. */ 47struct tree 48{ 49struct tree *llink;/* Left link; MUST be first field. */ 50struct tree *rlink;/* Right link (to larger labels). */ 51struct trie *trie;/* Trie node pointed to by this edge. */ 52unsigned char label;/* Label on this edge. */ 53char balance;/* Difference in depths of subtrees. */ 54}; 55 56/* Node of a trie representing a set of reversed keywords. */ 57struct trie 58{ 59unsigned int accepting;/* Word index of accepted word, or zero. */ 60struct tree *links;/* Tree of edges leaving this node. */ 61struct trie *parent;/* Parent of this node. */ 62struct trie *next;/* List of all trie nodes in level order. */ 63struct trie *fail;/* Aho-Corasick failure function. */ 64int depth;/* Depth of this node from the root. */ 65int shift;/* Shift function for search failures. */ 66int maxshift;/* Max shift of self and descendants. */ 67}; 68 69/* Structure returned opaquely to the caller, containing everything. */ 70struct kwset 71{ 72struct obstack obstack;/* Obstack for node allocation. */ 73int words;/* Number of words in the trie. */ 74struct trie *trie;/* The trie itself. */ 75int mind;/* Minimum depth of an accepting node. */ 76int maxd;/* Maximum depth of any node. */ 77unsigned char delta[NCHAR];/* Delta table for rapid search. */ 78struct trie *next[NCHAR];/* Table of children of the root. */ 79char*target;/* Target string if there's only one. */ 80int mind2;/* Used in Boyer-Moore search for one string. */ 81unsigned char const*trans;/* Character translation table. */ 82}; 83 84/* Allocate and initialize a keyword set object, returning an opaque 85 pointer to it. Return NULL if memory is not available. */ 86kwset_t 87kwsalloc(unsigned char const*trans) 88{ 89struct kwset *kwset; 90 91 kwset = (struct kwset *)xmalloc(sizeof(struct kwset)); 92 93obstack_init(&kwset->obstack); 94 kwset->words =0; 95 kwset->trie 96= (struct trie *)obstack_alloc(&kwset->obstack,sizeof(struct trie)); 97if(!kwset->trie) 98{ 99kwsfree((kwset_t) kwset); 100return NULL; 101} 102 kwset->trie->accepting =0; 103 kwset->trie->links = NULL; 104 kwset->trie->parent = NULL; 105 kwset->trie->next = NULL; 106 kwset->trie->fail = NULL; 107 kwset->trie->depth =0; 108 kwset->trie->shift =0; 109 kwset->mind = INT_MAX; 110 kwset->maxd = -1; 111 kwset->target = NULL; 112 kwset->trans = trans; 113 114return(kwset_t) kwset; 115} 116 117/* This upper bound is valid for CHAR_BIT >= 4 and 118 exact for CHAR_BIT in { 4..11, 13, 15, 17, 19 }. */ 119#define DEPTH_SIZE (CHAR_BIT + CHAR_BIT/2) 120 121/* Add the given string to the contents of the keyword set. Return NULL 122 for success, an error message otherwise. */ 123const char* 124kwsincr(kwset_t kws,char const*text,size_t len) 125{ 126struct kwset *kwset; 127registerstruct trie *trie; 128registerunsigned char label; 129registerstruct tree *link; 130registerint depth; 131struct tree *links[DEPTH_SIZE]; 132enum{ L, R } dirs[DEPTH_SIZE]; 133struct tree *t, *r, *l, *rl, *lr; 134 135 kwset = (struct kwset *) kws; 136 trie = kwset->trie; 137 text += len; 138 139/* Descend the trie (built of reversed keywords) character-by-character, 140 installing new nodes when necessary. */ 141while(len--) 142{ 143 label = kwset->trans ? kwset->trans[U(*--text)] : *--text; 144 145/* Descend the tree of outgoing links for this trie node, 146 looking for the current character and keeping track 147 of the path followed. */ 148 link = trie->links; 149 links[0] = (struct tree *) &trie->links; 150 dirs[0] = L; 151 depth =1; 152 153while(link && label != link->label) 154{ 155 links[depth] = link; 156if(label < link->label) 157 dirs[depth++] = L, link = link->llink; 158else 159 dirs[depth++] = R, link = link->rlink; 160} 161 162/* The current character doesn't have an outgoing link at 163 this trie node, so build a new trie node and install 164 a link in the current trie node's tree. */ 165if(!link) 166{ 167 link = (struct tree *)obstack_alloc(&kwset->obstack, 168sizeof(struct tree)); 169if(!link) 170return"memory exhausted"; 171 link->llink = NULL; 172 link->rlink = NULL; 173 link->trie = (struct trie *)obstack_alloc(&kwset->obstack, 174sizeof(struct trie)); 175if(!link->trie) 176{ 177obstack_free(&kwset->obstack, link); 178return"memory exhausted"; 179} 180 link->trie->accepting =0; 181 link->trie->links = NULL; 182 link->trie->parent = trie; 183 link->trie->next = NULL; 184 link->trie->fail = NULL; 185 link->trie->depth = trie->depth +1; 186 link->trie->shift =0; 187 link->label = label; 188 link->balance =0; 189 190/* Install the new tree node in its parent. */ 191if(dirs[--depth] == L) 192 links[depth]->llink = link; 193else 194 links[depth]->rlink = link; 195 196/* Back up the tree fixing the balance flags. */ 197while(depth && !links[depth]->balance) 198{ 199if(dirs[depth] == L) 200--links[depth]->balance; 201else 202++links[depth]->balance; 203--depth; 204} 205 206/* Rebalance the tree by pointer rotations if necessary. */ 207if(depth && ((dirs[depth] == L && --links[depth]->balance) 208|| (dirs[depth] == R && ++links[depth]->balance))) 209{ 210switch(links[depth]->balance) 211{ 212case(char) -2: 213switch(dirs[depth +1]) 214{ 215case L: 216 r = links[depth], t = r->llink, rl = t->rlink; 217 t->rlink = r, r->llink = rl; 218 t->balance = r->balance =0; 219break; 220case R: 221 r = links[depth], l = r->llink, t = l->rlink; 222 rl = t->rlink, lr = t->llink; 223 t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl; 224 l->balance = t->balance !=1?0: -1; 225 r->balance = t->balance != (char) -1?0:1; 226 t->balance =0; 227break; 228default: 229abort(); 230} 231break; 232case2: 233switch(dirs[depth +1]) 234{ 235case R: 236 l = links[depth], t = l->rlink, lr = t->llink; 237 t->llink = l, l->rlink = lr; 238 t->balance = l->balance =0; 239break; 240case L: 241 l = links[depth], r = l->rlink, t = r->llink; 242 lr = t->llink, rl = t->rlink; 243 t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl; 244 l->balance = t->balance !=1?0: -1; 245 r->balance = t->balance != (char) -1?0:1; 246 t->balance =0; 247break; 248default: 249abort(); 250} 251break; 252default: 253abort(); 254} 255 256if(dirs[depth -1] == L) 257 links[depth -1]->llink = t; 258else 259 links[depth -1]->rlink = t; 260} 261} 262 263 trie = link->trie; 264} 265 266/* Mark the node we finally reached as accepting, encoding the 267 index number of this word in the keyword set so far. */ 268if(!trie->accepting) 269 trie->accepting =1+2* kwset->words; 270++kwset->words; 271 272/* Keep track of the longest and shortest string of the keyword set. */ 273if(trie->depth < kwset->mind) 274 kwset->mind = trie->depth; 275if(trie->depth > kwset->maxd) 276 kwset->maxd = trie->depth; 277 278return NULL; 279} 280 281/* Enqueue the trie nodes referenced from the given tree in the 282 given queue. */ 283static void 284enqueue(struct tree *tree,struct trie **last) 285{ 286if(!tree) 287return; 288enqueue(tree->llink, last); 289enqueue(tree->rlink, last); 290(*last) = (*last)->next = tree->trie; 291} 292 293/* Compute the Aho-Corasick failure function for the trie nodes referenced 294 from the given tree, given the failure function for their parent as 295 well as a last resort failure node. */ 296static void 297treefails(registerstruct tree const*tree,struct trie const*fail, 298struct trie *recourse) 299{ 300registerstruct tree *link; 301 302if(!tree) 303return; 304 305treefails(tree->llink, fail, recourse); 306treefails(tree->rlink, fail, recourse); 307 308/* Find, in the chain of fails going back to the root, the first 309 node that has a descendant on the current label. */ 310while(fail) 311{ 312 link = fail->links; 313while(link && tree->label != link->label) 314if(tree->label < link->label) 315 link = link->llink; 316else 317 link = link->rlink; 318if(link) 319{ 320 tree->trie->fail = link->trie; 321return; 322} 323 fail = fail->fail; 324} 325 326 tree->trie->fail = recourse; 327} 328 329/* Set delta entries for the links of the given tree such that 330 the preexisting delta value is larger than the current depth. */ 331static void 332treedelta(registerstruct tree const*tree, 333registerunsigned int depth, 334unsigned char delta[]) 335{ 336if(!tree) 337return; 338treedelta(tree->llink, depth, delta); 339treedelta(tree->rlink, depth, delta); 340if(depth < delta[tree->label]) 341 delta[tree->label] = depth; 342} 343 344/* Return true if A has every label in B. */ 345static int 346hasevery(registerstruct tree const*a,registerstruct tree const*b) 347{ 348if(!b) 349return1; 350if(!hasevery(a, b->llink)) 351return0; 352if(!hasevery(a, b->rlink)) 353return0; 354while(a && b->label != a->label) 355if(b->label < a->label) 356 a = a->llink; 357else 358 a = a->rlink; 359return!!a; 360} 361 362/* Compute a vector, indexed by character code, of the trie nodes 363 referenced from the given tree. */ 364static void 365treenext(struct tree const*tree,struct trie *next[]) 366{ 367if(!tree) 368return; 369treenext(tree->llink, next); 370treenext(tree->rlink, next); 371 next[tree->label] = tree->trie; 372} 373 374/* Compute the shift for each trie node, as well as the delta 375 table and next cache for the given keyword set. */ 376const char* 377kwsprep(kwset_t kws) 378{ 379registerstruct kwset *kwset; 380registerint i; 381registerstruct trie *curr; 382registerunsigned char const*trans; 383unsigned char delta[NCHAR]; 384 385 kwset = (struct kwset *) kws; 386 387/* Initial values for the delta table; will be changed later. The 388 delta entry for a given character is the smallest depth of any 389 node at which an outgoing edge is labeled by that character. */ 390memset(delta, kwset->mind < UCHAR_MAX ? kwset->mind : UCHAR_MAX, NCHAR); 391 392/* Check if we can use the simple boyer-moore algorithm, instead 393 of the hairy commentz-walter algorithm. */ 394if(kwset->words ==1&& kwset->trans == NULL) 395{ 396char c; 397 398/* Looking for just one string. Extract it from the trie. */ 399 kwset->target =obstack_alloc(&kwset->obstack, kwset->mind); 400if(!kwset->target) 401return"memory exhausted"; 402for(i = kwset->mind -1, curr = kwset->trie; i >=0; --i) 403{ 404 kwset->target[i] = curr->links->label; 405 curr = curr->links->trie; 406} 407/* Build the Boyer Moore delta. Boy that's easy compared to CW. */ 408for(i =0; i < kwset->mind; ++i) 409 delta[U(kwset->target[i])] = kwset->mind - (i +1); 410/* Find the minimal delta2 shift that we might make after 411 a backwards match has failed. */ 412 c = kwset->target[kwset->mind -1]; 413for(i = kwset->mind -2; i >=0; --i) 414if(kwset->target[i] == c) 415break; 416 kwset->mind2 = kwset->mind - (i +1); 417} 418else 419{ 420registerstruct trie *fail; 421struct trie *last, *next[NCHAR]; 422 423/* Traverse the nodes of the trie in level order, simultaneously 424 computing the delta table, failure function, and shift function. */ 425for(curr = last = kwset->trie; curr; curr = curr->next) 426{ 427/* Enqueue the immediate descendants in the level order queue. */ 428enqueue(curr->links, &last); 429 430 curr->shift = kwset->mind; 431 curr->maxshift = kwset->mind; 432 433/* Update the delta table for the descendants of this node. */ 434treedelta(curr->links, curr->depth, delta); 435 436/* Compute the failure function for the descendants of this node. */ 437treefails(curr->links, curr->fail, kwset->trie); 438 439/* Update the shifts at each node in the current node's chain 440 of fails back to the root. */ 441for(fail = curr->fail; fail; fail = fail->fail) 442{ 443/* If the current node has some outgoing edge that the fail 444 doesn't, then the shift at the fail should be no larger 445 than the difference of their depths. */ 446if(!hasevery(fail->links, curr->links)) 447if(curr->depth - fail->depth < fail->shift) 448 fail->shift = curr->depth - fail->depth; 449 450/* If the current node is accepting then the shift at the 451 fail and its descendants should be no larger than the 452 difference of their depths. */ 453if(curr->accepting && fail->maxshift > curr->depth - fail->depth) 454 fail->maxshift = curr->depth - fail->depth; 455} 456} 457 458/* Traverse the trie in level order again, fixing up all nodes whose 459 shift exceeds their inherited maxshift. */ 460for(curr = kwset->trie->next; curr; curr = curr->next) 461{ 462if(curr->maxshift > curr->parent->maxshift) 463 curr->maxshift = curr->parent->maxshift; 464if(curr->shift > curr->maxshift) 465 curr->shift = curr->maxshift; 466} 467 468/* Create a vector, indexed by character code, of the outgoing links 469 from the root node. */ 470for(i =0; i < NCHAR; ++i) 471 next[i] = NULL; 472treenext(kwset->trie->links, next); 473 474if((trans = kwset->trans) != NULL) 475for(i =0; i < NCHAR; ++i) 476 kwset->next[i] = next[U(trans[i])]; 477else 478memcpy(kwset->next, next, NCHAR *sizeof(struct trie *)); 479} 480 481/* Fix things up for any translation table. */ 482if((trans = kwset->trans) != NULL) 483for(i =0; i < NCHAR; ++i) 484 kwset->delta[i] = delta[U(trans[i])]; 485else 486memcpy(kwset->delta, delta, NCHAR); 487 488return NULL; 489} 490 491/* Fast boyer-moore search. */ 492static size_t 493bmexec(kwset_t kws,char const*text,size_t size) 494{ 495struct kwset const*kwset; 496registerunsigned char const*d1; 497registerchar const*ep, *sp, *tp; 498registerint d, gc, i, len, md2; 499 500 kwset = (struct kwset const*) kws; 501 len = kwset->mind; 502 503if(len ==0) 504return0; 505if(len > size) 506return-1; 507if(len ==1) 508{ 509 tp =memchr(text, kwset->target[0], size); 510return tp ? tp - text : -1; 511} 512 513 d1 = kwset->delta; 514 sp = kwset->target + len; 515 gc =U(sp[-2]); 516 md2 = kwset->mind2; 517 tp = text + len; 518 519/* Significance of 12: 1 (initial offset) + 10 (skip loop) + 1 (md2). */ 520if(size >12* len) 521/* 11 is not a bug, the initial offset happens only once. */ 522for(ep = text + size -11* len;;) 523{ 524while(tp <= ep) 525{ 526 d = d1[U(tp[-1])], tp += d; 527 d = d1[U(tp[-1])], tp += d; 528if(d ==0) 529goto found; 530 d = d1[U(tp[-1])], tp += d; 531 d = d1[U(tp[-1])], tp += d; 532 d = d1[U(tp[-1])], tp += d; 533if(d ==0) 534goto found; 535 d = d1[U(tp[-1])], tp += d; 536 d = d1[U(tp[-1])], tp += d; 537 d = d1[U(tp[-1])], tp += d; 538if(d ==0) 539goto found; 540 d = d1[U(tp[-1])], tp += d; 541 d = d1[U(tp[-1])], tp += d; 542} 543break; 544 found: 545if(U(tp[-2]) == gc) 546{ 547for(i =3; i <= len &&U(tp[-i]) ==U(sp[-i]); ++i) 548; 549if(i > len) 550return tp - len - text; 551} 552 tp += md2; 553} 554 555/* Now we have only a few characters left to search. We 556 carefully avoid ever producing an out-of-bounds pointer. */ 557 ep = text + size; 558 d = d1[U(tp[-1])]; 559while(d <= ep - tp) 560{ 561 d = d1[U((tp += d)[-1])]; 562if(d !=0) 563continue; 564if(U(tp[-2]) == gc) 565{ 566for(i =3; i <= len &&U(tp[-i]) ==U(sp[-i]); ++i) 567; 568if(i > len) 569return tp - len - text; 570} 571 d = md2; 572} 573 574return-1; 575} 576 577/* Hairy multiple string search. */ 578static size_t 579cwexec(kwset_t kws,char const*text,size_t len,struct kwsmatch *kwsmatch) 580{ 581struct kwset const*kwset; 582struct trie *const*next; 583struct trie const*trie; 584struct trie const*accept; 585char const*beg, *lim, *mch, *lmch; 586registerunsigned char c; 587registerunsigned char const*delta; 588registerint d; 589registerchar const*end, *qlim; 590registerstruct tree const*tree; 591registerunsigned char const*trans; 592 593 accept = NULL; 594 595/* Initialize register copies and look for easy ways out. */ 596 kwset = (struct kwset *) kws; 597if(len < kwset->mind) 598return-1; 599 next = kwset->next; 600 delta = kwset->delta; 601 trans = kwset->trans; 602 lim = text + len; 603 end = text; 604if((d = kwset->mind) !=0) 605 mch = NULL; 606else 607{ 608 mch = text, accept = kwset->trie; 609goto match; 610} 611 612if(len >=4* kwset->mind) 613 qlim = lim -4* kwset->mind; 614else 615 qlim = NULL; 616 617while(lim - end >= d) 618{ 619if(qlim && end <= qlim) 620{ 621 end += d -1; 622while((d = delta[c = *end]) && end < qlim) 623{ 624 end += d; 625 end += delta[U(*end)]; 626 end += delta[U(*end)]; 627} 628++end; 629} 630else 631 d = delta[c = (end += d)[-1]]; 632if(d) 633continue; 634 beg = end -1; 635 trie = next[c]; 636if(trie->accepting) 637{ 638 mch = beg; 639 accept = trie; 640} 641 d = trie->shift; 642while(beg > text) 643{ 644 c = trans ? trans[U(*--beg)] : *--beg; 645 tree = trie->links; 646while(tree && c != tree->label) 647if(c < tree->label) 648 tree = tree->llink; 649else 650 tree = tree->rlink; 651if(tree) 652{ 653 trie = tree->trie; 654if(trie->accepting) 655{ 656 mch = beg; 657 accept = trie; 658} 659} 660else 661break; 662 d = trie->shift; 663} 664if(mch) 665goto match; 666} 667return-1; 668 669 match: 670/* Given a known match, find the longest possible match anchored 671 at or before its starting point. This is nearly a verbatim 672 copy of the preceding main search loops. */ 673if(lim - mch > kwset->maxd) 674 lim = mch + kwset->maxd; 675 lmch = NULL; 676 d =1; 677while(lim - end >= d) 678{ 679if((d = delta[c = (end += d)[-1]]) !=0) 680continue; 681 beg = end -1; 682if(!(trie = next[c])) 683{ 684 d =1; 685continue; 686} 687if(trie->accepting && beg <= mch) 688{ 689 lmch = beg; 690 accept = trie; 691} 692 d = trie->shift; 693while(beg > text) 694{ 695 c = trans ? trans[U(*--beg)] : *--beg; 696 tree = trie->links; 697while(tree && c != tree->label) 698if(c < tree->label) 699 tree = tree->llink; 700else 701 tree = tree->rlink; 702if(tree) 703{ 704 trie = tree->trie; 705if(trie->accepting && beg <= mch) 706{ 707 lmch = beg; 708 accept = trie; 709} 710} 711else 712break; 713 d = trie->shift; 714} 715if(lmch) 716{ 717 mch = lmch; 718goto match; 719} 720if(!d) 721 d =1; 722} 723 724if(kwsmatch) 725{ 726 kwsmatch->index = accept->accepting /2; 727 kwsmatch->offset[0] = mch - text; 728 kwsmatch->size[0] = accept->depth; 729} 730return mch - text; 731} 732 733/* Search through the given text for a match of any member of the 734 given keyword set. Return a pointer to the first character of 735 the matching substring, or NULL if no match is found. If FOUNDLEN 736 is non-NULL store in the referenced location the length of the 737 matching substring. Similarly, if FOUNDIDX is non-NULL, store 738 in the referenced location the index number of the particular 739 keyword matched. */ 740size_t 741kwsexec(kwset_t kws,char const*text,size_t size, 742struct kwsmatch *kwsmatch) 743{ 744struct kwset const*kwset = (struct kwset *) kws; 745if(kwset->words ==1&& kwset->trans == NULL) 746{ 747size_t ret =bmexec(kws, text, size); 748if(kwsmatch != NULL && ret != (size_t) -1) 749{ 750 kwsmatch->index =0; 751 kwsmatch->offset[0] = ret; 752 kwsmatch->size[0] = kwset->mind; 753} 754return ret; 755} 756else 757returncwexec(kws, text, size, kwsmatch); 758} 759 760/* Free the components of the given keyword set. */ 761void 762kwsfree(kwset_t kws) 763{ 764struct kwset *kwset; 765 766 kwset = (struct kwset *) kws; 767obstack_free(&kwset->obstack, NULL); 768free(kws); 769}