Coverage Report

Created: 2024-09-16 06:12

/src/git/kwset.c
Line
Count
Source (jump to first uncovered line)
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 <https://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 "git-compat-util.h"
36
37
#include "kwset.h"
38
#include "compat/obstack.h"
39
40
0
#define NCHAR (UCHAR_MAX + 1)
41
/* adapter for `xmalloc()`, which takes `size_t`, not `long` */
42
static void *obstack_chunk_alloc(long size)
43
0
{
44
0
  if (size < 0)
45
0
    BUG("Cannot allocate a negative amount: %ld", size);
46
0
  return xmalloc(size);
47
0
}
48
0
#define obstack_chunk_free free
49
50
0
#define U(c) ((unsigned char) (c))
51
52
/* For case-insensitive kwset */
53
const unsigned char tolower_trans_tbl[256] = {
54
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55
  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
56
  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
57
  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
58
   ' ',  '!',  '"',  '#',  '$',  '%',  '&', 0x27,
59
   '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
60
   '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
61
   '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
62
   '@',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
63
   'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
64
   'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
65
   'x',  'y',  'z',  '[', 0x5c,  ']',  '^',  '_',
66
   '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
67
   'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
68
   'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
69
   'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f,
70
  0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
71
  0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
72
  0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
73
  0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
74
  0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
75
  0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
76
  0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
77
  0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
78
  0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
79
  0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
80
  0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
81
  0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
82
  0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
83
  0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
84
  0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
85
  0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
86
};
87
88
/* Balanced tree of edges and labels leaving a given trie node. */
89
struct tree
90
{
91
  struct tree *llink;   /* Left link; MUST be first field. */
92
  struct tree *rlink;   /* Right link (to larger labels). */
93
  struct trie *trie;    /* Trie node pointed to by this edge. */
94
  unsigned char label;    /* Label on this edge. */
95
  char balance;     /* Difference in depths of subtrees. */
96
};
97
98
/* Node of a trie representing a set of reversed keywords. */
99
struct trie
100
{
101
  unsigned int accepting; /* Word index of accepted word, or zero. */
102
  struct tree *links;   /* Tree of edges leaving this node. */
103
  struct trie *parent;    /* Parent of this node. */
104
  struct trie *next;    /* List of all trie nodes in level order. */
105
  struct trie *fail;    /* Aho-Corasick failure function. */
106
  int depth;      /* Depth of this node from the root. */
107
  int shift;      /* Shift function for search failures. */
108
  int maxshift;     /* Max shift of self and descendants. */
109
};
110
111
/* Structure returned opaquely to the caller, containing everything. */
112
struct kwset
113
{
114
  struct obstack obstack; /* Obstack for node allocation. */
115
  int words;      /* Number of words in the trie. */
116
  struct trie *trie;    /* The trie itself. */
117
  int mind;     /* Minimum depth of an accepting node. */
118
  int maxd;     /* Maximum depth of any node. */
119
  unsigned char delta[NCHAR]; /* Delta table for rapid search. */
120
  struct trie *next[NCHAR]; /* Table of children of the root. */
121
  char *target;     /* Target string if there's only one. */
122
  int mind2;      /* Used in Boyer-Moore search for one string. */
123
  unsigned char const *trans;  /* Character translation table. */
124
};
125
126
/* Allocate and initialize a keyword set object, returning an opaque
127
   pointer to it.  Return NULL if memory is not available. */
128
kwset_t
129
kwsalloc (unsigned char const *trans)
130
0
{
131
0
  struct kwset *kwset;
132
133
0
  kwset = (struct kwset *) xmalloc(sizeof (struct kwset));
134
135
0
  obstack_init(&kwset->obstack);
136
0
  kwset->words = 0;
137
0
  kwset->trie
138
0
    = (struct trie *) obstack_alloc(&kwset->obstack, sizeof (struct trie));
139
0
  if (!kwset->trie)
140
0
    {
141
0
      kwsfree((kwset_t) kwset);
142
0
      return NULL;
143
0
    }
144
0
  kwset->trie->accepting = 0;
145
0
  kwset->trie->links = NULL;
146
0
  kwset->trie->parent = NULL;
147
0
  kwset->trie->next = NULL;
148
0
  kwset->trie->fail = NULL;
149
0
  kwset->trie->depth = 0;
150
0
  kwset->trie->shift = 0;
151
0
  kwset->mind = INT_MAX;
152
0
  kwset->maxd = -1;
153
0
  kwset->target = NULL;
154
0
  kwset->trans = trans;
155
156
0
  return (kwset_t) kwset;
157
0
}
158
159
/* This upper bound is valid for CHAR_BIT >= 4 and
160
   exact for CHAR_BIT in { 4..11, 13, 15, 17, 19 }. */
161
#define DEPTH_SIZE (CHAR_BIT + CHAR_BIT/2)
162
163
/* Add the given string to the contents of the keyword set.  Return NULL
164
   for success, an error message otherwise. */
165
const char *
166
kwsincr (kwset_t kws, char const *text, size_t len)
167
0
{
168
0
  struct kwset *kwset;
169
0
  register struct trie *trie;
170
0
  register unsigned char label;
171
0
  register struct tree *link;
172
0
  register int depth;
173
0
  struct tree *links[DEPTH_SIZE];
174
0
  enum { L, R } dirs[DEPTH_SIZE];
175
0
  struct tree *t, *r, *l, *rl, *lr;
176
177
0
  kwset = (struct kwset *) kws;
178
0
  trie = kwset->trie;
179
0
  text += len;
180
181
  /* Descend the trie (built of reversed keywords) character-by-character,
182
     installing new nodes when necessary. */
183
0
  while (len--)
184
0
    {
185
0
      label = kwset->trans ? kwset->trans[U(*--text)] : *--text;
186
187
      /* Descend the tree of outgoing links for this trie node,
188
   looking for the current character and keeping track
189
   of the path followed. */
190
0
      link = trie->links;
191
0
      links[0] = (struct tree *) &trie->links;
192
0
      dirs[0] = L;
193
0
      depth = 1;
194
195
0
      while (link && label != link->label)
196
0
  {
197
0
    links[depth] = link;
198
0
    if (label < link->label)
199
0
      dirs[depth++] = L, link = link->llink;
200
0
    else
201
0
      dirs[depth++] = R, link = link->rlink;
202
0
  }
203
204
      /* The current character doesn't have an outgoing link at
205
   this trie node, so build a new trie node and install
206
   a link in the current trie node's tree. */
207
0
      if (!link)
208
0
  {
209
0
    link = (struct tree *) obstack_alloc(&kwset->obstack,
210
0
                 sizeof (struct tree));
211
0
    if (!link)
212
0
      return "memory exhausted";
213
0
    link->llink = NULL;
214
0
    link->rlink = NULL;
215
0
    link->trie = (struct trie *) obstack_alloc(&kwset->obstack,
216
0
                 sizeof (struct trie));
217
0
    if (!link->trie)
218
0
      {
219
0
        obstack_free(&kwset->obstack, link);
220
0
        return "memory exhausted";
221
0
      }
222
0
    link->trie->accepting = 0;
223
0
    link->trie->links = NULL;
224
0
    link->trie->parent = trie;
225
0
    link->trie->next = NULL;
226
0
    link->trie->fail = NULL;
227
0
    link->trie->depth = trie->depth + 1;
228
0
    link->trie->shift = 0;
229
0
    link->label = label;
230
0
    link->balance = 0;
231
232
    /* Install the new tree node in its parent. */
233
0
    if (dirs[--depth] == L)
234
0
      links[depth]->llink = link;
235
0
    else
236
0
      links[depth]->rlink = link;
237
238
    /* Back up the tree fixing the balance flags. */
239
0
    while (depth && !links[depth]->balance)
240
0
      {
241
0
        if (dirs[depth] == L)
242
0
    --links[depth]->balance;
243
0
        else
244
0
    ++links[depth]->balance;
245
0
        --depth;
246
0
      }
247
248
    /* Rebalance the tree by pointer rotations if necessary. */
249
0
    if (depth && ((dirs[depth] == L && --links[depth]->balance)
250
0
      || (dirs[depth] == R && ++links[depth]->balance)))
251
0
      {
252
0
        switch (links[depth]->balance)
253
0
    {
254
0
    case (char) -2:
255
0
      switch (dirs[depth + 1])
256
0
        {
257
0
        case L:
258
0
          r = links[depth], t = r->llink, rl = t->rlink;
259
0
          t->rlink = r, r->llink = rl;
260
0
          t->balance = r->balance = 0;
261
0
          break;
262
0
        case R:
263
0
          r = links[depth], l = r->llink, t = l->rlink;
264
0
          rl = t->rlink, lr = t->llink;
265
0
          t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl;
266
0
          l->balance = t->balance != 1 ? 0 : -1;
267
0
          r->balance = t->balance != (char) -1 ? 0 : 1;
268
0
          t->balance = 0;
269
0
          break;
270
0
        default:
271
0
          abort ();
272
0
        }
273
0
      break;
274
0
    case 2:
275
0
      switch (dirs[depth + 1])
276
0
        {
277
0
        case R:
278
0
          l = links[depth], t = l->rlink, lr = t->llink;
279
0
          t->llink = l, l->rlink = lr;
280
0
          t->balance = l->balance = 0;
281
0
          break;
282
0
        case L:
283
0
          l = links[depth], r = l->rlink, t = r->llink;
284
0
          lr = t->llink, rl = t->rlink;
285
0
          t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl;
286
0
          l->balance = t->balance != 1 ? 0 : -1;
287
0
          r->balance = t->balance != (char) -1 ? 0 : 1;
288
0
          t->balance = 0;
289
0
          break;
290
0
        default:
291
0
          abort ();
292
0
        }
293
0
      break;
294
0
    default:
295
0
      abort ();
296
0
    }
297
298
0
        if (dirs[depth - 1] == L)
299
0
    links[depth - 1]->llink = t;
300
0
        else
301
0
    links[depth - 1]->rlink = t;
302
0
      }
303
0
  }
304
305
0
      trie = link->trie;
306
0
    }
307
308
  /* Mark the node we finally reached as accepting, encoding the
309
     index number of this word in the keyword set so far. */
310
0
  if (!trie->accepting)
311
0
    trie->accepting = 1 + 2 * kwset->words;
312
0
  ++kwset->words;
313
314
  /* Keep track of the longest and shortest string of the keyword set. */
315
0
  if (trie->depth < kwset->mind)
316
0
    kwset->mind = trie->depth;
317
0
  if (trie->depth > kwset->maxd)
318
0
    kwset->maxd = trie->depth;
319
320
0
  return NULL;
321
0
}
322
323
/* Enqueue the trie nodes referenced from the given tree in the
324
   given queue. */
325
static void
326
enqueue (struct tree *tree, struct trie **last)
327
0
{
328
0
  if (!tree)
329
0
    return;
330
0
  enqueue(tree->llink, last);
331
0
  enqueue(tree->rlink, last);
332
0
  (*last) = (*last)->next = tree->trie;
333
0
}
334
335
/* Compute the Aho-Corasick failure function for the trie nodes referenced
336
   from the given tree, given the failure function for their parent as
337
   well as a last resort failure node. */
338
static void
339
treefails (register struct tree const *tree, struct trie const *fail,
340
     struct trie *recourse)
341
0
{
342
0
  register struct tree *link;
343
344
0
  if (!tree)
345
0
    return;
346
347
0
  treefails(tree->llink, fail, recourse);
348
0
  treefails(tree->rlink, fail, recourse);
349
350
  /* Find, in the chain of fails going back to the root, the first
351
     node that has a descendant on the current label. */
352
0
  while (fail)
353
0
    {
354
0
      link = fail->links;
355
0
      while (link && tree->label != link->label)
356
0
  if (tree->label < link->label)
357
0
    link = link->llink;
358
0
  else
359
0
    link = link->rlink;
360
0
      if (link)
361
0
  {
362
0
    tree->trie->fail = link->trie;
363
0
    return;
364
0
  }
365
0
      fail = fail->fail;
366
0
    }
367
368
0
  tree->trie->fail = recourse;
369
0
}
370
371
/* Set delta entries for the links of the given tree such that
372
   the preexisting delta value is larger than the current depth. */
373
static void
374
treedelta (register struct tree const *tree,
375
     register unsigned int depth,
376
     unsigned char delta[])
377
0
{
378
0
  if (!tree)
379
0
    return;
380
0
  treedelta(tree->llink, depth, delta);
381
0
  treedelta(tree->rlink, depth, delta);
382
0
  if (depth < delta[tree->label])
383
0
    delta[tree->label] = depth;
384
0
}
385
386
/* Return true if A has every label in B. */
387
static int
388
hasevery (register struct tree const *a, register struct tree const *b)
389
0
{
390
0
  if (!b)
391
0
    return 1;
392
0
  if (!hasevery(a, b->llink))
393
0
    return 0;
394
0
  if (!hasevery(a, b->rlink))
395
0
    return 0;
396
0
  while (a && b->label != a->label)
397
0
    if (b->label < a->label)
398
0
      a = a->llink;
399
0
    else
400
0
      a = a->rlink;
401
0
  return !!a;
402
0
}
403
404
/* Compute a vector, indexed by character code, of the trie nodes
405
   referenced from the given tree. */
406
static void
407
treenext (struct tree const *tree, struct trie *next[])
408
0
{
409
0
  if (!tree)
410
0
    return;
411
0
  treenext(tree->llink, next);
412
0
  treenext(tree->rlink, next);
413
0
  next[tree->label] = tree->trie;
414
0
}
415
416
/* Compute the shift for each trie node, as well as the delta
417
   table and next cache for the given keyword set. */
418
const char *
419
kwsprep (kwset_t kws)
420
0
{
421
0
  register struct kwset *kwset;
422
0
  register int i;
423
0
  register struct trie *curr;
424
0
  register unsigned char const *trans;
425
0
  unsigned char delta[NCHAR];
426
427
0
  kwset = (struct kwset *) kws;
428
429
  /* Initial values for the delta table; will be changed later.  The
430
     delta entry for a given character is the smallest depth of any
431
     node at which an outgoing edge is labeled by that character. */
432
0
  memset(delta, kwset->mind < UCHAR_MAX ? kwset->mind : UCHAR_MAX, NCHAR);
433
434
  /* Check if we can use the simple boyer-moore algorithm, instead
435
     of the hairy commentz-walter algorithm. */
436
0
  if (kwset->words == 1 && kwset->trans == NULL)
437
0
    {
438
0
      char c;
439
440
      /* Looking for just one string.  Extract it from the trie. */
441
0
      kwset->target = obstack_alloc(&kwset->obstack, kwset->mind);
442
0
      if (!kwset->target)
443
0
  return "memory exhausted";
444
0
      for (i = kwset->mind - 1, curr = kwset->trie; i >= 0; --i)
445
0
  {
446
0
    kwset->target[i] = curr->links->label;
447
0
    curr = curr->links->trie;
448
0
  }
449
      /* Build the Boyer Moore delta.  Boy that's easy compared to CW. */
450
0
      for (i = 0; i < kwset->mind; ++i)
451
0
  delta[U(kwset->target[i])] = kwset->mind - (i + 1);
452
      /* Find the minimal delta2 shift that we might make after
453
   a backwards match has failed. */
454
0
      c = kwset->target[kwset->mind - 1];
455
0
      for (i = kwset->mind - 2; i >= 0; --i)
456
0
  if (kwset->target[i] == c)
457
0
    break;
458
0
      kwset->mind2 = kwset->mind - (i + 1);
459
0
    }
460
0
  else
461
0
    {
462
0
      register struct trie *fail;
463
0
      struct trie *last, *next[NCHAR];
464
465
      /* Traverse the nodes of the trie in level order, simultaneously
466
   computing the delta table, failure function, and shift function. */
467
0
      for (curr = last = kwset->trie; curr; curr = curr->next)
468
0
  {
469
    /* Enqueue the immediate descendants in the level order queue. */
470
0
    enqueue(curr->links, &last);
471
472
0
    curr->shift = kwset->mind;
473
0
    curr->maxshift = kwset->mind;
474
475
    /* Update the delta table for the descendants of this node. */
476
0
    treedelta(curr->links, curr->depth, delta);
477
478
    /* Compute the failure function for the descendants of this node. */
479
0
    treefails(curr->links, curr->fail, kwset->trie);
480
481
    /* Update the shifts at each node in the current node's chain
482
       of fails back to the root. */
483
0
    for (fail = curr->fail; fail; fail = fail->fail)
484
0
      {
485
        /* If the current node has some outgoing edge that the fail
486
     doesn't, then the shift at the fail should be no larger
487
     than the difference of their depths. */
488
0
        if (!hasevery(fail->links, curr->links))
489
0
    if (curr->depth - fail->depth < fail->shift)
490
0
      fail->shift = curr->depth - fail->depth;
491
492
        /* If the current node is accepting then the shift at the
493
     fail and its descendants should be no larger than the
494
     difference of their depths. */
495
0
        if (curr->accepting && fail->maxshift > curr->depth - fail->depth)
496
0
    fail->maxshift = curr->depth - fail->depth;
497
0
      }
498
0
  }
499
500
      /* Traverse the trie in level order again, fixing up all nodes whose
501
   shift exceeds their inherited maxshift. */
502
0
      for (curr = kwset->trie->next; curr; curr = curr->next)
503
0
  {
504
0
    if (curr->maxshift > curr->parent->maxshift)
505
0
      curr->maxshift = curr->parent->maxshift;
506
0
    if (curr->shift > curr->maxshift)
507
0
      curr->shift = curr->maxshift;
508
0
  }
509
510
      /* Create a vector, indexed by character code, of the outgoing links
511
   from the root node. */
512
0
      for (i = 0; i < NCHAR; ++i)
513
0
  next[i] = NULL;
514
0
      treenext(kwset->trie->links, next);
515
516
0
      if ((trans = kwset->trans))
517
0
  for (i = 0; i < NCHAR; ++i)
518
0
    kwset->next[i] = next[U(trans[i])];
519
0
      else
520
0
  COPY_ARRAY(kwset->next, next, NCHAR);
521
0
    }
522
523
  /* Fix things up for any translation table. */
524
0
  if ((trans = kwset->trans))
525
0
    for (i = 0; i < NCHAR; ++i)
526
0
      kwset->delta[i] = delta[U(trans[i])];
527
0
  else
528
0
    memcpy(kwset->delta, delta, NCHAR);
529
530
0
  return NULL;
531
0
}
532
533
/* Fast boyer-moore search. */
534
static size_t
535
bmexec (kwset_t kws, char const *text, size_t size)
536
0
{
537
0
  struct kwset const *kwset;
538
0
  register unsigned char const *d1;
539
0
  register char const *ep, *sp, *tp;
540
0
  register int d, gc, i, len, md2;
541
542
0
  kwset = (struct kwset const *) kws;
543
0
  len = kwset->mind;
544
545
0
  if (len == 0)
546
0
    return 0;
547
0
  if (len > size)
548
0
    return -1;
549
0
  if (len == 1)
550
0
    {
551
0
      tp = memchr (text, kwset->target[0], size);
552
0
      return tp ? tp - text : -1;
553
0
    }
554
555
0
  d1 = kwset->delta;
556
0
  sp = kwset->target + len;
557
0
  gc = U(sp[-2]);
558
0
  md2 = kwset->mind2;
559
0
  tp = text + len;
560
561
  /* Significance of 12: 1 (initial offset) + 10 (skip loop) + 1 (md2). */
562
0
  if (size > 12 * len)
563
    /* 11 is not a bug, the initial offset happens only once. */
564
0
    for (ep = text + size - 11 * len;;)
565
0
      {
566
0
  while (tp <= ep)
567
0
    {
568
0
      d = d1[U(tp[-1])], tp += d;
569
0
      d = d1[U(tp[-1])], tp += d;
570
0
      if (d == 0)
571
0
        goto found;
572
0
      d = d1[U(tp[-1])], tp += d;
573
0
      d = d1[U(tp[-1])], tp += d;
574
0
      d = d1[U(tp[-1])], tp += d;
575
0
      if (d == 0)
576
0
        goto found;
577
0
      d = d1[U(tp[-1])], tp += d;
578
0
      d = d1[U(tp[-1])], tp += d;
579
0
      d = d1[U(tp[-1])], tp += d;
580
0
      if (d == 0)
581
0
        goto found;
582
0
      d = d1[U(tp[-1])], tp += d;
583
0
      d = d1[U(tp[-1])], tp += d;
584
0
    }
585
0
  break;
586
0
      found:
587
0
  if (U(tp[-2]) == gc)
588
0
    {
589
0
      for (i = 3; i <= len && U(tp[-i]) == U(sp[-i]); ++i)
590
0
        ;
591
0
      if (i > len)
592
0
        return tp - len - text;
593
0
    }
594
0
  tp += md2;
595
0
      }
596
597
  /* Now we have only a few characters left to search.  We
598
     carefully avoid ever producing an out-of-bounds pointer. */
599
0
  ep = text + size;
600
0
  d = d1[U(tp[-1])];
601
0
  while (d <= ep - tp)
602
0
    {
603
0
      d = d1[U((tp += d)[-1])];
604
0
      if (d != 0)
605
0
  continue;
606
0
      if (U(tp[-2]) == gc)
607
0
  {
608
0
    for (i = 3; i <= len && U(tp[-i]) == U(sp[-i]); ++i)
609
0
      ;
610
0
    if (i > len)
611
0
      return tp - len - text;
612
0
  }
613
0
      d = md2;
614
0
    }
615
616
0
  return -1;
617
0
}
618
619
/* Hairy multiple string search. */
620
static size_t
621
cwexec (kwset_t kws, char const *text, size_t len, struct kwsmatch *kwsmatch)
622
0
{
623
0
  struct kwset const *kwset;
624
0
  struct trie * const *next;
625
0
  struct trie const *trie;
626
0
  struct trie const *accept;
627
0
  char const *beg, *lim, *mch, *lmch;
628
0
  register unsigned char c;
629
0
  register unsigned char const *delta;
630
0
  register int d;
631
0
  register char const *end, *qlim;
632
0
  register struct tree const *tree;
633
0
  register unsigned char const *trans;
634
635
0
  accept = NULL;
636
637
  /* Initialize register copies and look for easy ways out. */
638
0
  kwset = (struct kwset *) kws;
639
0
  if (len < kwset->mind)
640
0
    return -1;
641
0
  next = kwset->next;
642
0
  delta = kwset->delta;
643
0
  trans = kwset->trans;
644
0
  lim = text + len;
645
0
  end = text;
646
0
  if ((d = kwset->mind) != 0)
647
0
    mch = NULL;
648
0
  else
649
0
    {
650
0
      mch = text, accept = kwset->trie;
651
0
      goto match;
652
0
    }
653
654
0
  if (len >= 4 * kwset->mind)
655
0
    qlim = lim - 4 * kwset->mind;
656
0
  else
657
0
    qlim = NULL;
658
659
0
  while (lim - end >= d)
660
0
    {
661
0
      if (qlim && end <= qlim)
662
0
  {
663
0
    end += d - 1;
664
0
    while ((d = delta[c = *end]) && end < qlim)
665
0
      {
666
0
        end += d;
667
0
        end += delta[U(*end)];
668
0
        end += delta[U(*end)];
669
0
      }
670
0
    ++end;
671
0
  }
672
0
      else
673
0
  d = delta[c = (end += d)[-1]];
674
0
      if (d)
675
0
  continue;
676
0
      beg = end - 1;
677
0
      trie = next[c];
678
0
      if (trie->accepting)
679
0
  {
680
0
    mch = beg;
681
0
    accept = trie;
682
0
  }
683
0
      d = trie->shift;
684
0
      while (beg > text)
685
0
  {
686
0
    c = trans ? trans[U(*--beg)] : *--beg;
687
0
    tree = trie->links;
688
0
    while (tree && c != tree->label)
689
0
      if (c < tree->label)
690
0
        tree = tree->llink;
691
0
      else
692
0
        tree = tree->rlink;
693
0
    if (tree)
694
0
      {
695
0
        trie = tree->trie;
696
0
        if (trie->accepting)
697
0
    {
698
0
      mch = beg;
699
0
      accept = trie;
700
0
    }
701
0
      }
702
0
    else
703
0
      break;
704
0
    d = trie->shift;
705
0
  }
706
0
      if (mch)
707
0
  goto match;
708
0
    }
709
0
  return -1;
710
711
0
 match:
712
  /* Given a known match, find the longest possible match anchored
713
     at or before its starting point.  This is nearly a verbatim
714
     copy of the preceding main search loops. */
715
0
  if (lim - mch > kwset->maxd)
716
0
    lim = mch + kwset->maxd;
717
0
  lmch = NULL;
718
0
  d = 1;
719
0
  while (lim - end >= d)
720
0
    {
721
0
      if ((d = delta[c = (end += d)[-1]]) != 0)
722
0
  continue;
723
0
      beg = end - 1;
724
0
      if (!(trie = next[c]))
725
0
  {
726
0
    d = 1;
727
0
    continue;
728
0
  }
729
0
      if (trie->accepting && beg <= mch)
730
0
  {
731
0
    lmch = beg;
732
0
    accept = trie;
733
0
  }
734
0
      d = trie->shift;
735
0
      while (beg > text)
736
0
  {
737
0
    c = trans ? trans[U(*--beg)] : *--beg;
738
0
    tree = trie->links;
739
0
    while (tree && c != tree->label)
740
0
      if (c < tree->label)
741
0
        tree = tree->llink;
742
0
      else
743
0
        tree = tree->rlink;
744
0
    if (tree)
745
0
      {
746
0
        trie = tree->trie;
747
0
        if (trie->accepting && beg <= mch)
748
0
    {
749
0
      lmch = beg;
750
0
      accept = trie;
751
0
    }
752
0
      }
753
0
    else
754
0
      break;
755
0
    d = trie->shift;
756
0
  }
757
0
      if (lmch)
758
0
  {
759
0
    mch = lmch;
760
0
    goto match;
761
0
  }
762
0
      if (!d)
763
0
  d = 1;
764
0
    }
765
766
0
  if (kwsmatch)
767
0
    {
768
0
      kwsmatch->index = accept->accepting / 2;
769
0
      kwsmatch->offset[0] = mch - text;
770
0
      kwsmatch->size[0] = accept->depth;
771
0
    }
772
0
  return mch - text;
773
0
}
774
775
/* Search through the given text for a match of any member of the
776
   given keyword set.  Return a pointer to the first character of
777
   the matching substring, or NULL if no match is found.  If FOUNDLEN
778
   is non-NULL store in the referenced location the length of the
779
   matching substring.  Similarly, if FOUNDIDX is non-NULL, store
780
   in the referenced location the index number of the particular
781
   keyword matched. */
782
size_t
783
kwsexec (kwset_t kws, char const *text, size_t size,
784
   struct kwsmatch *kwsmatch)
785
0
{
786
0
  struct kwset const *kwset = (struct kwset *) kws;
787
0
  if (kwset->words == 1 && kwset->trans == NULL)
788
0
    {
789
0
      size_t ret = bmexec (kws, text, size);
790
0
      if (kwsmatch != NULL && ret != (size_t) -1)
791
0
  {
792
0
    kwsmatch->index = 0;
793
0
    kwsmatch->offset[0] = ret;
794
0
    kwsmatch->size[0] = kwset->mind;
795
0
  }
796
0
      return ret;
797
0
    }
798
0
  else
799
0
    return cwexec(kws, text, size, kwsmatch);
800
0
}
801
802
/* Free the components of the given keyword set. */
803
void
804
kwsfree (kwset_t kws)
805
0
{
806
0
  struct kwset *kwset;
807
808
0
  kwset = (struct kwset *) kws;
809
0
  obstack_free(&kwset->obstack, NULL);
810
0
  free(kws);
811
0
}