Coverage Report

Created: 2026-01-09 07:10

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