summaryrefslogtreecommitdiff
path: root/nest/a-path.c
blob: 6bb1828592ffba165d76098c72c03ca2f34e9d07 (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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/*
 *	BIRD -- Path Operations
 *
 *	(c) 2000 Martin Mares <mj@ucw.cz>
 *	(c) 2000 Pavel Machek <pavel@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "nest/bird.h"
#include "nest/route.h"
#include "nest/attrs.h"
#include "lib/resource.h"
#include "lib/unaligned.h"
#include "lib/string.h"
#include "filter/data.h"

// static inline void put_as(byte *data, u32 as) { put_u32(data, as); }
// static inline u32 get_as(byte *data) { return get_u32(data); }

#define put_as put_u32
#define get_as get_u32
#define BS  4	/* Default block size of ASN (autonomous system number) */

#define BAD(DSC, VAL) ({ err_dsc = DSC; err_val = VAL; goto bad; })

int
as_path_valid(byte *data, uint len, int bs, int sets, int confed, char *err, uint elen)
{
  byte *pos = data;
  char *err_dsc = NULL;
  uint err_val = 0;

  while (len)
  {
    if (len < 2)
      BAD("segment framing error", 0);

    /* Process one AS path segment */
    uint type = pos[0];
    uint slen = 2 + bs * pos[1];

    if (len < slen)
      BAD("segment framing error", len);

    switch (type)
    {
    case AS_PATH_SET:
      if (!sets)
	BAD("AS_SET segment", type);
      break;

    case AS_PATH_SEQUENCE:
      break;

    case AS_PATH_CONFED_SEQUENCE:
      if (!confed)
	BAD("AS_CONFED_SEQUENCE segment", type);
      break;

    case AS_PATH_CONFED_SET:
      if (!sets || !confed)
	BAD("AS_CONFED_SET segment", type);
      break;

    default:
      BAD("unknown segment", type);
    }

    if (pos[1] == 0)
      BAD("zero-length segment", type);

    pos += slen;
    len -= slen;
  }

  return 1;

bad:
  if (err)
    if (bsnprintf(err, elen, "%s (%u) at %d", err_dsc, err_val, (int) (pos - data)) < 0)
      err[0] = 0;

  return 0;
}

int
as_path_16to32(byte *dst, const byte *src, uint len)
{
  byte *dst0 = dst;
  const byte *end = src + len;
  uint i, n;

  while (src < end)
  {
    n = src[1];
    *dst++ = *src++;
    *dst++ = *src++;

    for (i = 0; i < n; i++)
    {
      put_u32(dst, get_u16(src));
      src += 2;
      dst += 4;
    }
  }

  return dst - dst0;
}

int
as_path_32to16(byte *dst, const byte *src, uint len)
{
  byte *dst0 = dst;
  const byte *end = src + len;
  uint i, n;

  while (src < end)
  {
    n = src[1];
    *dst++ = *src++;
    *dst++ = *src++;

    for (i = 0; i < n; i++)
    {
      put_u16(dst, get_u32(src));
      src += 4;
      dst += 2;
    }
  }

  return dst - dst0;
}

int
as_path_contains_as4(const struct adata *path)
{
  const byte *pos = path->data;
  const byte *end = pos + path->length;
  uint i, n;

  while (pos < end)
  {
    n = pos[1];
    pos += 2;

    for (i = 0; i < n; i++)
    {
      if (get_as(pos) > 0xFFFF)
	return 1;

      pos += BS;
    }
  }

  return 0;
}

int
as_path_contains_confed(const struct adata *path)
{
  const byte *pos = path->data;
  const byte *end = pos + path->length;

  while (pos < end)
  {
    uint type = pos[0];
    uint slen = 2 + BS * pos[1];

    if ((type == AS_PATH_CONFED_SEQUENCE) ||
	(type == AS_PATH_CONFED_SET))
      return 1;

    pos += slen;
  }

  return 0;
}

struct adata *
as_path_strip_confed(struct linpool *pool, const struct adata *path)
{
  struct adata *res = lp_alloc_adata(pool, path->length);
  const byte *src = path->data;
  const byte *end = src + path->length;
  byte *dst = res->data;

  while (src < end)
  {
    uint type = src[0];
    uint slen = 2 + BS * src[1];

    /* Copy regular segments */
    if ((type == AS_PATH_SET) || (type == AS_PATH_SEQUENCE))
    {
      memcpy(dst, src, slen);
      dst += slen;
    }

    src += slen;
  }

  /* Fix the result length */
  res->length = dst - res->data;

  return res;
}

struct adata *
as_path_prepend2(struct linpool *pool, const struct adata *op, int seq, u32 as)
{
  struct adata *np;
  const byte *pos = op->data;
  uint len = op->length;

  if (len && (pos[0] == seq) && (pos[1] < 255))
  {
    /* Starting with matching segment => just prepend the AS number */
    np = lp_alloc_adata(pool, len + BS);
    np->data[0] = seq;
    np->data[1] = pos[1] + 1;
    put_as(np->data + 2, as);

    uint dlen = BS * pos[1];
    memcpy(np->data + 2 + BS, pos + 2, dlen);
    ADVANCE(pos, len, 2 + dlen);
  }
  else
  {
    /* Create a new path segment */
    np = lp_alloc_adata(pool, len + 2 + BS);
    np->data[0] = seq;
    np->data[1] = 1;
    put_as(np->data + 2, as);
  }

  if (len)
  {
    byte *dst = np->data + 2 + BS * np->data[1];

    memcpy(dst, pos, len);
  }

  return np;
}


struct adata *
as_path_to_old(struct linpool *pool, const struct adata *path)
{
  struct adata *res = lp_alloc_adata(pool, path->length);
  byte *pos = res->data;
  byte *end = pos + res->length;
  uint i, n;
  u32 as;

  /* Copy the whole path */
  memcpy(res->data, path->data, path->length);

  /* Replace 32-bit AS numbers with AS_TRANS */
  while (pos < end)
  {
    n = pos[1];
    pos += 2;

    for (i = 0; i < n; i++)
    {
      as = get_as(pos);
      if (as > 0xFFFF)
	put_as(pos, AS_TRANS);

      pos += BS;
    }
  }

  return res;
}

/*
 * Cut the path to the length @num, measured to the usual path metric. Note that
 * AS_CONFED_* segments have zero length and must be added if they are on edge.
 */
struct adata *
as_path_cut(struct linpool *pool, const struct adata *path, uint num)
{
  const byte *pos = path->data;
  const byte *end = pos + path->length;

  while (pos < end)
  {
    uint t = pos[0];
    uint l = pos[1];
    uint n = 0;

    switch (t)
    {
    case AS_PATH_SET:			n = 1; break;
    case AS_PATH_SEQUENCE:		n = l; break;
    case AS_PATH_CONFED_SEQUENCE:	n = 0; break;
    case AS_PATH_CONFED_SET:		n = 0; break;
    default: bug("as_path_cut: Invalid path segment");
    }

    /* Cannot add whole segment, so try partial one and finish */
    if (num < n)
    {
      const byte *nend = pos;
      if (num)
	nend += 2 + BS * num;

      struct adata *res = lp_alloc_adata(pool, path->length);
      res->length = nend - (const byte *) path->data;
      memcpy(res->data, path->data, res->length);

      if (num)
      {
	byte *dpos = ((byte *) res->data) + (pos - (const byte *) path->data);
	dpos[1] = num;
      }

      return res;
    }

    num -= n;
    pos += 2 + BS * l;
  }

  struct adata *res = lp_alloc_adata(pool, path->length);
  res->length = path->length;
  memcpy(res->data, path->data, res->length);
  return res;
}

/*
 * Merge (concatenate) paths @p1 and @p2 and return the result.
 * In contrast to other as_path_* functions, @p1 and @p2 may be reused.
 */
const struct adata *
as_path_merge(struct linpool *pool, const struct adata *p1, const struct adata *p2)
{
  if (p1->length == 0)
    return p2;

  if (p2->length == 0)
    return p1;

  struct adata *res = lp_alloc_adata(pool, p1->length + p2->length);
  memcpy(res->data, p1->data, p1->length);
  memcpy(res->data + p1->length, p2->data, p2->length);

  return res;
}

void
as_path_format(const struct adata *path, byte *bb, uint size)
{
  buffer buf = { .start = bb, .pos = bb, .end = bb + size }, *b = &buf;
  const byte *pos = path->data;
  const byte *end = pos + path->length;
  const char *ops, *cls;

  b->pos[0] = 0;

  while (pos < end)
  {
    uint type = pos[0];
    uint len  = pos[1];
    pos += 2;

    switch (type)
    {
    case AS_PATH_SET:			ops = "{";	cls = "}";	break;
    case AS_PATH_SEQUENCE:		ops = NULL;	cls = NULL;	break;
    case AS_PATH_CONFED_SEQUENCE:	ops = "(";	cls = ")";	break;
    case AS_PATH_CONFED_SET:		ops = "({";	cls = "})";	break;
    default: bug("Invalid path segment");
    }

    if (ops)
      buffer_puts(b, ops);

    while (len--)
    {
      buffer_print(b, len ? "%u " : "%u", get_as(pos));
      pos += BS;
    }

    if (cls)
      buffer_puts(b, cls);

    if (pos < end)
      buffer_puts(b, " ");
  }

  /* Handle overflow */
  if (b->pos == b->end)
    strcpy(b->end - 12, "...");
}

int
as_path_getlen(const struct adata *path)
{
  const byte *pos = path->data;
  const byte *end = pos + path->length;
  uint res = 0;

  while (pos < end)
  {
    uint t = pos[0];
    uint l = pos[1];
    uint n = 0;

    switch (t)
    {
    case AS_PATH_SET:			n = 1; break;
    case AS_PATH_SEQUENCE:		n = l; break;
    case AS_PATH_CONFED_SEQUENCE:	n = 0; break;
    case AS_PATH_CONFED_SET:		n = 0; break;
    default: bug("as_path_getlen: Invalid path segment");
    }

    res += n;
    pos += 2 + BS * l;
  }

  return res;
}

int
as_path_get_last(const struct adata *path, u32 *orig_as)
{
  const byte *pos = path->data;
  const byte *end = pos + path->length;
  int found = 0;
  u32 val = 0;

  while (pos < end)
  {
    uint type = pos[0];
    uint len  = pos[1];
    pos += 2;

    if (!len)
      continue;

    switch (type)
    {
    case AS_PATH_SET:
    case AS_PATH_CONFED_SET:
      found = 0;
      break;

    case AS_PATH_SEQUENCE:
    case AS_PATH_CONFED_SEQUENCE:
      val = get_as(pos + BS * (len - 1));
      found = 1;
      break;

    default:
      bug("Invalid path segment");
    }

    pos += BS * len;
  }

  if (found)
    *orig_as = val;
  return found;
}

u32
as_path_get_last_nonaggregated(const struct adata *path)
{
  const byte *pos = path->data;
  const byte *end = pos + path->length;
  u32 val = 0;

  while (pos < end)
  {
    uint type = pos[0];
    uint len  = pos[1];
    pos += 2;

    if (!len)
      continue;

    switch (type)
    {
    case AS_PATH_SET:
    case AS_PATH_CONFED_SET:
      return val;

    case AS_PATH_SEQUENCE:
    case AS_PATH_CONFED_SEQUENCE:
      val = get_as(pos + BS * (len - 1));
      break;

    default:
      bug("Invalid path segment");
    }

    pos += BS * len;
  }

  return val;
}

int
as_path_get_first(const struct adata *path, u32 *last_as)
{
  const u8 *p = path->data;

  if ((path->length == 0) || (p[0] != AS_PATH_SEQUENCE) || (p[1] == 0))
    return 0;

  *last_as = get_as(p+2);
  return 1;
}

int
as_path_get_first_regular(const struct adata *path, u32 *last_as)
{
  const byte *pos = path->data;
  const byte *end = pos + path->length;

  while (pos < end)
  {
    uint type = pos[0];
    uint len  = pos[1];
    pos += 2;

    switch (type)
    {
    case AS_PATH_SET:
      return 0;

    case AS_PATH_SEQUENCE:
      if (len == 0)
	return 0;

      *last_as = get_as(pos);
      return 1;

    case AS_PATH_CONFED_SEQUENCE:
    case AS_PATH_CONFED_SET:
      break;

    default:
      bug("Invalid path segment");
    }

    pos += BS * len;
  }

  return 0;
}

int
as_path_contains(const struct adata *path, u32 as, int min)
{
  const u8 *p = path->data;
  const u8 *q = p+path->length;
  int num = 0;
  int i, n;

  while (p<q)
    {
      n = p[1];
      p += 2;
      for(i=0; i<n; i++)
	{
	  if (get_as(p) == as)
	    if (++num == min)
	      return 1;
	  p += BS;
	}
    }
  return 0;
}

int
as_path_match_set(const struct adata *path, const struct f_tree *set)
{
  const u8 *p = path->data;
  const u8 *q = p+path->length;
  int i, n;

  while (p<q)
    {
      n = p[1];
      p += 2;
      for (i=0; i<n; i++)
	{
	  struct f_val v = {T_INT, .val.i = get_as(p)};
	  if (find_tree(set, &v))
	    return 1;
	  p += BS;
	}
    }

  return 0;
}

const struct adata *
as_path_filter(struct linpool *pool, const struct adata *path, const struct f_val *set, int pos)
{
  ASSERT((set->type == T_SET) || (set->type == T_INT));

  if (!path)
    return NULL;

  int len = path->length;
  const u8 *p = path->data;
  const u8 *q = path->data + len;
  u8 *d, *d2;
  int i, bt, sn, dn;
  u8 buf[len];

  d = buf;
  while (p<q)
    {
      /* Read block header (type and length) */
      bt = p[0];
      sn = p[1];
      dn = 0;
      p += 2;
      d2 = d + 2;

      for (i = 0; i < sn; i++)
	{
	  u32 as = get_as(p);
	  int match;

	  if (set->type == T_SET)
	    {
	      struct f_val v = {T_INT, .val.i = as};
	      match = !!find_tree(set->val.t, &v);
	    }
	  else /* T_INT */
	    match = (as == set->val.i);

	  if (match == pos)
	    {
	      put_as(d2, as);
	      d2 += BS;
	      dn++;
	    }

	  p += BS;
	}

      if (dn > 0)
	{
	  /* Nonempty block, set block header and advance */
	  d[0] = bt;
	  d[1] = dn;
	  d = d2;
	}
  }

  uint nl = d - buf;
  if (nl == path->length)
    return path;

  struct adata *res = lp_alloc(pool, sizeof(struct adata) + nl);
  res->length = nl;
  memcpy(res->data, buf, nl);

  return res;
}

int
as_path_walk(const struct adata *path, uint *pos, uint *val)
{
  if (!path)
    return 0;

  const u8 *p = path->data;
  const u8 *q = p + path->length;
  uint n, x = *pos;

  while (p < q)
  {
    n = p[1];
    p += 2;

    if (x < n)
    {
      *val = get_as(p + x * BS);
      *pos += 1;
      return 1;
    }

    p += n * BS;
    x -= n;
  }

  return 0;
}


struct pm_pos
{
  u8 set;
  u8 mark;
  union
  {
    const char *sp;
    u32 asn;
  } val;
};

static int
parse_path(const struct adata *path, struct pm_pos *pp)
{
  const byte *pos = path->data;
  const byte *end = pos + path->length;
  struct pm_pos *op = pp;
  uint i;

  while (pos < end)
  {
    uint type = pos[0];
    uint len  = pos[1];
    pos += 2;

    switch (type)
    {
    case AS_PATH_SET:
    case AS_PATH_CONFED_SET:
      pp->set = 1;
      pp->mark = 0;
      pp->val.sp = pos - 1;
      pp++;

      pos += BS * len;
      break;

    case AS_PATH_SEQUENCE:
    case AS_PATH_CONFED_SEQUENCE:
      for (i = 0; i < len; i++)
      {
	pp->set = 0;
	pp->mark = 0;
	pp->val.asn = get_as(pos);
	pp++;

	pos += BS;
      }
      break;

    default:
      bug("Invalid path segment");
    }
  }

  return pp - op;
}

static int
pm_match_val(const struct pm_pos *pos, u32 asn, u32 asn2)
{
  u32 gas;
  if (! pos->set)
    return ((pos->val.asn >= asn) && (pos->val.asn <= asn2));

  const u8 *p = pos->val.sp;
  int len = *p++;
  int i;

  for (i = 0; i < len; i++)
  {
    gas = get_as(p + i * BS);

    if ((gas >= asn) && (gas <= asn2))
      return 1;
  }

  return 0;
}

static int
pm_match_set(const struct pm_pos *pos, const struct f_tree *set)
{
  struct f_val asn = { .type = T_INT };

  if (! pos->set)
  {
    asn.val.i = pos->val.asn;
    return !!find_tree(set, &asn);
  }

  const u8 *p = pos->val.sp;
  int len = *p++;
  int i;

  for (i = 0; i < len; i++)
  {
    asn.val.i = get_as(p + i * BS);
    if (find_tree(set, &asn))
      return 1;
  }

  return 0;
}

static inline int
pm_match(const struct pm_pos *pos, const struct f_path_mask_item *mask, u32 asn, u32 asn2)
{
  return ((mask->kind == PM_QUESTION) ||
	  ((mask->kind != PM_ASN_SET) ?
	   pm_match_val(pos, asn, asn2) :
	   pm_match_set(pos, mask->set)));
}

static void
pm_mark(struct pm_pos *pos, int *i, int plen, int *nl, int *nh)
{
  int j = *i;

  if (pos[j].set)
    do { pos[j].mark = 1; j++; }
    while ((j < plen) && pos[j].set);
  else
    j++;

  pos[j].mark = 1;

  /* Update low, high based on first and last marked pos */
  int l = pos[*i].set ? *i : j;

  *nl = (*nl < 0) ? l : MIN(*nl, l);
  *nh = MAX(*nh, j);
  *i = j;
}

/* AS path matching is nontrivial. Because AS path can
 * contain sets, it is not a plain wildcard matching. A set
 * in an AS path is interpreted as it might represent any
 * sequence of AS numbers from that set (possibly with
 * repetitions). So it is also a kind of a pattern,
 * more complicated than a path mask.
 *
 * The algorithm for AS path matching is a variant
 * of nondeterministic finite state machine, where
 * positions in AS path are states, and items in
 * path mask are input for that finite state machine.
 * During execution of the algorithm we maintain a set
 * of marked states - a state is marked if it can be
 * reached by any walk through NFSM with regard to
 * currently processed part of input. When we process
 * next part of mask, we advance each marked state.
 * We start with marked first position, when we
 * run out of marked positions, we reject. When
 * we process the whole mask, we accept if final position
 * (auxiliary position after last real position in AS path)
 * is marked.
 */
int
as_path_match(const struct adata *path, const struct f_path_mask *mask)
{
  struct pm_pos pos[2048 + 1];
  int plen = parse_path(path, pos);
  int l, h, i, nh, nl, last, loop;
  u32 val = 0;
  u32 val2 = 0;

  /* l and h are bound of interval of positions where
     are marked states */

  pos[plen].set = 0;
  pos[plen].mark = 0;

  l = h = loop = 0;
  pos[0].mark = 1;

  for (uint m=0; m < mask->len; m++)
    {
      /* We remove this mark to not step after pos[plen] */
      pos[plen].mark = 0;

      switch (mask->item[m].kind)
	{
	case PM_ASTERISK:
	  for (i = l; i <= plen; i++)
	    pos[i].mark = 1;
	  h = plen;
	  break;

	case PM_LOOP:
	  loop = 1;
	  break;

	case PM_ASN:	/* Define single ASN as ASN..ASN - very narrow interval */
	  val2 = val = mask->item[m].asn;
	  goto step;
	case PM_ASN_EXPR:
	  bug("Expressions should be evaluated on AS path mask construction.");
	case PM_ASN_RANGE:
	  val = mask->item[m].from;
	  val2 = mask->item[m].to;
	  goto step;
	case PM_QUESTION:
	case PM_ASN_SET:
	step:
	  nh = nl = -1;
	  last = plen;
	  for (i = h; i >= l; i--)
	    if (pos[i].mark)
	      {
		pos[i].mark = 0;
		int j = i;

	      match:
		if (pm_match(pos + j, &mask->item[m], val, val2))
		{
		  pm_mark(pos, &j, plen, &nl, &nh);
		  if (loop && (j < last))
		    goto match;
		}

		last = i;
	      }

	  if (nh < 0)
	    return 0;

	  h = nh;
	  l = nl;
	  loop = 0;
	  break;
	}
    }

  return pos[plen].mark;
}