~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to module/apparmor/match/pcre_exec.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-04-27 10:38:07 UTC
  • mfrom: (5.1.118 natty)
  • Revision ID: james.westby@ubuntu.com-20110427103807-ym3rhwys6o84ith0
Tags: 2.6.1-2
debian/copyright: clarify for some full organization names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  This is a modified version of pcre.c containing only the code/data
3
 
 *  required to support pcre_exec()
4
 
 */
5
 
 
6
 
 
7
 
/*************************************************
8
 
*      Perl-Compatible Regular Expressions       *
9
 
*************************************************/
10
 
 
11
 
/*
12
 
This is a library of functions to support regular expressions whose syntax
13
 
and semantics are as close as possible to those of the Perl 5 language. See
14
 
the file Tech.Notes for some information on the internals.
15
 
 
16
 
Written by: Philip Hazel <ph10@cam.ac.uk>
17
 
 
18
 
           Copyright (c) 1997-2001 University of Cambridge
19
 
 
20
 
-----------------------------------------------------------------------------
21
 
Permission is granted to anyone to use this software for any purpose on any
22
 
computer system, and to redistribute it freely, subject to the following
23
 
restrictions:
24
 
 
25
 
1. This software is distributed in the hope that it will be useful,
26
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
 
 
29
 
2. The origin of this software must not be misrepresented, either by
30
 
   explicit claim or by omission.
31
 
 
32
 
3. Altered versions must be plainly marked as such, and must not be
33
 
   misrepresented as being the original software.
34
 
 
35
 
4. If PCRE is embedded in any software that is released under the GNU
36
 
   General Purpose Licence (GPL), then the terms of that licence shall
37
 
   supersede any condition above with which it is incompatible.
38
 
-----------------------------------------------------------------------------
39
 
*/
40
 
 
41
 
 
42
 
/* Define DEBUG to get debugging output on stdout. */
43
 
 
44
 
/* #define DEBUG */
45
 
 
46
 
/* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef
47
 
inline, and there are *still* stupid compilers about that don't like indented
48
 
pre-processor statements. I suppose it's only been 10 years... */
49
 
 
50
 
#ifdef DEBUG
51
 
#define DPRINTF(p) PCRE_PRINTF p
52
 
#else
53
 
#define DPRINTF(p) /*nothing*/
54
 
#endif
55
 
 
56
 
/* Include the internals header, which itself includes Standard C headers plus
57
 
the external pcre header. */
58
 
 
59
 
#include "pcre_exec.h"
60
 
 
61
 
 
62
 
/* ----  CODE DELETED ---- */
63
 
 
64
 
 
65
 
/* Min and max values for the common repeats; for the maxima, 0 => infinity */
66
 
 
67
 
static const char rep_min[] = { 0, 0, 1, 1, 0, 0 };
68
 
static const char rep_max[] = { 0, 0, 0, 0, 1, 1 };
69
 
 
70
 
 
71
 
/* ----  CODE DELETED ---- */
72
 
 
73
 
 
74
 
/* Structure for building a chain of data that actually lives on the
75
 
 * stack, for holding the values of the subject pointer at the start of each
76
 
 * subpattern, so as to detect when an empty string has been matched by a
77
 
 * subpattern - to break infinite loops. */
78
 
 
79
 
typedef struct eptrblock {
80
 
          struct eptrblock *prev;
81
 
            const uschar *saved_eptr;
82
 
} eptrblock;
83
 
 
84
 
/* Flag bits for the match() function */
85
 
 
86
 
#define match_condassert   0x01    /* Called to check a condition assertion */
87
 
#define match_isgroup      0x02    /* Set if start of bracketed group */
88
 
 
89
 
 
90
 
/* ----  CODE DELETED ---- */
91
 
 
92
 
 
93
 
/*************************************************
94
 
 * *               Global variables                 *
95
 
 * *************************************************/
96
 
 
97
 
/* PCRE is thread-clean and doesn't use any global variables in the normal
98
 
 * sense. However, it calls memory allocation and free functions via the two
99
 
 * indirections below, which are can be changed by the caller, but are shared
100
 
 * between all threads. */
101
 
 
102
 
#ifdef __KERNEL__
103
 
static void *kern_malloc(size_t sz)
104
 
{
105
 
                return kmalloc(sz, GFP_KERNEL);
106
 
}
107
 
void  *(*pcre_malloc)(size_t) = kern_malloc;
108
 
void  (*pcre_free)(const void *) = kfree;
109
 
#else
110
 
void  *(*pcre_malloc)(size_t) = malloc;
111
 
void  (*pcre_free)(const void *) = free;
112
 
#endif
113
 
 
114
 
 
115
 
/*************************************************
116
 
 * *    Macros and tables for character handling    *
117
 
 * *************************************************/
118
 
 
119
 
/* When UTF-8 encoding is being used, a character is no longer just a single
120
 
 * byte. The macros for character handling generate simple sequences when used in
121
 
 * byte-mode, and more complicated ones for UTF-8 characters. */
122
 
 
123
 
#ifndef SUPPORT_UTF8
124
 
#define GETCHARINC(c, eptr) c = *eptr++;
125
 
#define GETCHARLEN(c, eptr, len) c = *eptr;
126
 
#define BACKCHAR(eptr)
127
 
#endif
128
 
 
129
 
/* ----  CODE DELETED ---- */
130
 
 
131
 
#ifdef DEBUG
132
 
/*************************************************
133
 
*        Debugging function to print chars       *
134
 
*************************************************/
135
 
 
136
 
/* Print a sequence of chars in printable format, stopping at the end of the
137
 
subject if the requested.
138
 
 
139
 
Arguments:
140
 
  p           points to characters
141
 
  length      number to print
142
 
  is_subject  TRUE if printing from within md->start_subject
143
 
  md          pointer to matching data block, if is_subject is TRUE
144
 
 
145
 
Returns:     nothing
146
 
*/
147
 
 
148
 
static void
149
 
pchars(const uschar *p, int length, BOOL is_subject, match_data *md)
150
 
{
151
 
int c;
152
 
if (is_subject && length > md->end_subject - p) length = md->end_subject - p;
153
 
while (length-- > 0)
154
 
  if (isprint(c = *(p++))) PCRE_PRINTF("%c", c); else PCRE_PRINTF("\\x%02x", c);
155
 
}
156
 
#endif /* DEBUG */
157
 
 
158
 
/* ----  CODE DELETED ---- */
159
 
 
160
 
 
161
 
/*************************************************
162
 
*          Match a back-reference                *
163
 
*************************************************/
164
 
 
165
 
/* If a back reference hasn't been set, the length that is passed is greater
166
 
than the number of characters left in the string, so the match fails.
167
 
 
168
 
Arguments:
169
 
  offset      index into the offset vector
170
 
  eptr        points into the subject
171
 
  length      length to be matched
172
 
  md          points to match data block
173
 
  ims         the ims flags
174
 
 
175
 
Returns:      TRUE if matched
176
 
*/
177
 
 
178
 
static BOOL
179
 
match_ref(int offset, register const uschar *eptr, int length, match_data *md,
180
 
  unsigned long int ims)
181
 
{
182
 
const uschar *p = md->start_subject + md->offset_vector[offset];
183
 
 
184
 
#ifdef DEBUG
185
 
if (eptr >= md->end_subject)
186
 
  PCRE_PRINTF("matching subject <null>");
187
 
else
188
 
  {
189
 
  PCRE_PRINTF("matching subject ");
190
 
  pchars(eptr, length, TRUE, md);
191
 
  }
192
 
PCRE_PRINTF(" against backref ");
193
 
pchars(p, length, FALSE, md);
194
 
PCRE_PRINTF("\n");
195
 
#endif
196
 
 
197
 
/* Always fail if not enough characters left */
198
 
 
199
 
if (length > md->end_subject - eptr) return FALSE;
200
 
 
201
 
/* Separate the caselesss case for speed */
202
 
 
203
 
if ((ims & PCRE_CASELESS) != 0)
204
 
  {
205
 
  while (length-- > 0)
206
 
    if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE;
207
 
  }
208
 
else
209
 
  { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }
210
 
 
211
 
return TRUE;
212
 
}
213
 
 
214
 
 
215
 
/*************************************************
216
 
*         Match from current position            *
217
 
*************************************************/
218
 
 
219
 
/* On entry ecode points to the first opcode, and eptr to the first character
220
 
in the subject string, while eptrb holds the value of eptr at the start of the
221
 
last bracketed group - used for breaking infinite loops matching zero-length
222
 
strings.
223
 
 
224
 
Arguments:
225
 
   eptr        pointer in subject
226
 
   ecode       position in code
227
 
   offset_top  current top pointer
228
 
   md          pointer to "static" info for the match
229
 
   ims         current /i, /m, and /s options
230
 
   eptrb       pointer to chain of blocks containing eptr at start of
231
 
                 brackets - for testing for empty matches
232
 
   flags       can contain
233
 
                 match_condassert - this is an assertion condition
234
 
                 match_isgroup - this is the start of a bracketed group
235
 
 
236
 
Returns:       TRUE if matched
237
 
*/
238
 
 
239
 
static BOOL
240
 
match(register const uschar *eptr, register const uschar *ecode,
241
 
  int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,
242
 
  int flags)
243
 
{
244
 
unsigned long int original_ims = ims;   /* Save for resetting on ')' */
245
 
eptrblock newptrb;
246
 
 
247
 
/* At the start of a bracketed group, add the current subject pointer to the
248
 
stack of such pointers, to be re-instated at the end of the group when we hit
249
 
the closing ket. When match() is called in other circumstances, we don't add to
250
 
the stack. */
251
 
 
252
 
if ((flags & match_isgroup) != 0)
253
 
  {
254
 
  newptrb.prev = eptrb;
255
 
  newptrb.saved_eptr = eptr;
256
 
  eptrb = &newptrb;
257
 
  }
258
 
 
259
 
/* Now start processing the operations. */
260
 
 
261
 
for (;;)
262
 
  {
263
 
  int op = (int)*ecode;
264
 
  int min, max, ctype;
265
 
  register int i;
266
 
  register int c;
267
 
  BOOL minimize = FALSE;
268
 
 
269
 
  /* Opening capturing bracket. If there is space in the offset vector, save
270
 
  the current subject position in the working slot at the top of the vector. We
271
 
  mustn't change the current values of the data slot, because they may be set
272
 
  from a previous iteration of this group, and be referred to by a reference
273
 
  inside the group.
274
 
 
275
 
  If the bracket fails to match, we need to restore this value and also the
276
 
  values of the final offsets, in case they were set by a previous iteration of
277
 
  the same bracket.
278
 
 
279
 
  If there isn't enough space in the offset vector, treat this as if it were a
280
 
  non-capturing bracket. Don't worry about setting the flag for the error case
281
 
  here; that is handled in the code for KET. */
282
 
 
283
 
  if (op > OP_BRA)
284
 
    {
285
 
    int offset;
286
 
    int number = op - OP_BRA;
287
 
 
288
 
    /* For extended extraction brackets (large number), we have to fish out the
289
 
    number from a dummy opcode at the start. */
290
 
 
291
 
    if (number > EXTRACT_BASIC_MAX) number = (ecode[4] << 8) | ecode[5];
292
 
    offset = number << 1;
293
 
 
294
 
#ifdef DEBUG
295
 
    PCRE_PRINTF("start bracket %d subject=", number);
296
 
    pchars(eptr, 16, TRUE, md);
297
 
    PCRE_PRINTF("\n");
298
 
#endif
299
 
 
300
 
    if (offset < md->offset_max)
301
 
      {
302
 
      int save_offset1 = md->offset_vector[offset];
303
 
      int save_offset2 = md->offset_vector[offset+1];
304
 
      int save_offset3 = md->offset_vector[md->offset_end - number];
305
 
 
306
 
      DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
307
 
      md->offset_vector[md->offset_end - number] = eptr - md->start_subject;
308
 
 
309
 
      do
310
 
        {
311
 
        if (match(eptr, ecode+3, offset_top, md, ims, eptrb, match_isgroup))
312
 
          return TRUE;
313
 
        ecode += (ecode[1] << 8) + ecode[2];
314
 
        }
315
 
      while (*ecode == OP_ALT);
316
 
 
317
 
      DPRINTF(("bracket %d failed\n", number));
318
 
 
319
 
      md->offset_vector[offset] = save_offset1;
320
 
      md->offset_vector[offset+1] = save_offset2;
321
 
      md->offset_vector[md->offset_end - number] = save_offset3;
322
 
 
323
 
      return FALSE;
324
 
      }
325
 
 
326
 
    /* Insufficient room for saving captured contents */
327
 
 
328
 
    else op = OP_BRA;
329
 
    }
330
 
 
331
 
  /* Other types of node can be handled by a switch */
332
 
 
333
 
  switch(op)
334
 
    {
335
 
    case OP_BRA:     /* Non-capturing bracket: optimized */
336
 
    DPRINTF(("start bracket 0\n"));
337
 
    do
338
 
      {
339
 
      if (match(eptr, ecode+3, offset_top, md, ims, eptrb, match_isgroup))
340
 
        return TRUE;
341
 
      ecode += (ecode[1] << 8) + ecode[2];
342
 
      }
343
 
    while (*ecode == OP_ALT);
344
 
    DPRINTF(("bracket 0 failed\n"));
345
 
    return FALSE;
346
 
 
347
 
    /* Conditional group: compilation checked that there are no more than
348
 
    two branches. If the condition is false, skipping the first branch takes us
349
 
    past the end if there is only one branch, but that's OK because that is
350
 
    exactly what going to the ket would do. */
351
 
 
352
 
    case OP_COND:
353
 
    if (ecode[3] == OP_CREF)         /* Condition is extraction test */
354
 
      {
355
 
      int offset = (ecode[4] << 9) | (ecode[5] << 1); /* Doubled ref number */
356
 
      return match(eptr,
357
 
        ecode + ((offset < offset_top && md->offset_vector[offset] >= 0)?
358
 
          6 : 3 + (ecode[1] << 8) + ecode[2]),
359
 
        offset_top, md, ims, eptrb, match_isgroup);
360
 
      }
361
 
 
362
 
    /* The condition is an assertion. Call match() to evaluate it - setting
363
 
    the final argument TRUE causes it to stop at the end of an assertion. */
364
 
 
365
 
    else
366
 
      {
367
 
      if (match(eptr, ecode+3, offset_top, md, ims, NULL,
368
 
          match_condassert | match_isgroup))
369
 
        {
370
 
        ecode += 3 + (ecode[4] << 8) + ecode[5];
371
 
        while (*ecode == OP_ALT) ecode += (ecode[1] << 8) + ecode[2];
372
 
        }
373
 
      else ecode += (ecode[1] << 8) + ecode[2];
374
 
      return match(eptr, ecode+3, offset_top, md, ims, eptrb, match_isgroup);
375
 
      }
376
 
    /* Control never reaches here */
377
 
 
378
 
    /* Skip over conditional reference or large extraction number data if
379
 
    encountered. */
380
 
 
381
 
    case OP_CREF:
382
 
    case OP_BRANUMBER:
383
 
    ecode += 3;
384
 
    break;
385
 
 
386
 
    /* End of the pattern. If PCRE_NOTEMPTY is set, fail if we have matched
387
 
    an empty string - recursion will then try other alternatives, if any. */
388
 
 
389
 
    case OP_END:
390
 
    if (md->notempty && eptr == md->start_match) return FALSE;
391
 
    md->end_match_ptr = eptr;          /* Record where we ended */
392
 
    md->end_offset_top = offset_top;   /* and how many extracts were taken */
393
 
    return TRUE;
394
 
 
395
 
    /* Change option settings */
396
 
 
397
 
    case OP_OPT:
398
 
    ims = ecode[1];
399
 
    ecode += 2;
400
 
    DPRINTF(("ims set to %02lx\n", ims));
401
 
    break;
402
 
 
403
 
    /* Assertion brackets. Check the alternative branches in turn - the
404
 
    matching won't pass the KET for an assertion. If any one branch matches,
405
 
    the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
406
 
    start of each branch to move the current point backwards, so the code at
407
 
    this level is identical to the lookahead case. */
408
 
 
409
 
    case OP_ASSERT:
410
 
    case OP_ASSERTBACK:
411
 
    do
412
 
      {
413
 
      if (match(eptr, ecode+3, offset_top, md, ims, NULL, match_isgroup)) break;
414
 
      ecode += (ecode[1] << 8) + ecode[2];
415
 
      }
416
 
    while (*ecode == OP_ALT);
417
 
    if (*ecode == OP_KET) return FALSE;
418
 
 
419
 
    /* If checking an assertion for a condition, return TRUE. */
420
 
 
421
 
    if ((flags & match_condassert) != 0) return TRUE;
422
 
 
423
 
    /* Continue from after the assertion, updating the offsets high water
424
 
    mark, since extracts may have been taken during the assertion. */
425
 
 
426
 
    do ecode += (ecode[1] << 8) + ecode[2]; while (*ecode == OP_ALT);
427
 
    ecode += 3;
428
 
    offset_top = md->end_offset_top;
429
 
    continue;
430
 
 
431
 
    /* Negative assertion: all branches must fail to match */
432
 
 
433
 
    case OP_ASSERT_NOT:
434
 
    case OP_ASSERTBACK_NOT:
435
 
    do
436
 
      {
437
 
      if (match(eptr, ecode+3, offset_top, md, ims, NULL, match_isgroup))
438
 
        return FALSE;
439
 
      ecode += (ecode[1] << 8) + ecode[2];
440
 
      }
441
 
    while (*ecode == OP_ALT);
442
 
 
443
 
    if ((flags & match_condassert) != 0) return TRUE;
444
 
 
445
 
    ecode += 3;
446
 
    continue;
447
 
 
448
 
    /* Move the subject pointer back. This occurs only at the start of
449
 
    each branch of a lookbehind assertion. If we are too close to the start to
450
 
    move back, this match function fails. When working with UTF-8 we move
451
 
    back a number of characters, not bytes. */
452
 
 
453
 
    case OP_REVERSE:
454
 
#ifdef SUPPORT_UTF8
455
 
    c = (ecode[1] << 8) + ecode[2];
456
 
    for (i = 0; i < c; i++)
457
 
      {
458
 
      eptr--;
459
 
      BACKCHAR(eptr)
460
 
      }
461
 
#else
462
 
    eptr -= (ecode[1] << 8) + ecode[2];
463
 
#endif
464
 
 
465
 
    if (eptr < md->start_subject) return FALSE;
466
 
    ecode += 3;
467
 
    break;
468
 
 
469
 
    /* Recursion matches the current regex, nested. If there are any capturing
470
 
    brackets started but not finished, we have to save their starting points
471
 
    and reinstate them after the recursion. However, we don't know how many
472
 
    such there are (offset_top records the completed total) so we just have
473
 
    to save all the potential data. There may be up to 99 such values, which
474
 
    is a bit large to put on the stack, but using malloc for small numbers
475
 
    seems expensive. As a compromise, the stack is used when there are fewer
476
 
    than 16 values to store; otherwise malloc is used. A problem is what to do
477
 
    if the malloc fails ... there is no way of returning to the top level with
478
 
    an error. Save the top 15 values on the stack, and accept that the rest
479
 
    may be wrong. */
480
 
 
481
 
    case OP_RECURSE:
482
 
      {
483
 
      BOOL rc;
484
 
      int *save;
485
 
      int stacksave[15];
486
 
 
487
 
      c = md->offset_max;
488
 
 
489
 
      if (c < 16) save = stacksave; else
490
 
        {
491
 
        save = (int *)(pcre_malloc)((c+1) * sizeof(int));
492
 
        if (save == NULL)
493
 
          {
494
 
          save = stacksave;
495
 
          c = 15;
496
 
          }
497
 
        }
498
 
 
499
 
      for (i = 1; i <= c; i++)
500
 
        save[i] = md->offset_vector[md->offset_end - i];
501
 
      rc = match(eptr, md->start_pattern, offset_top, md, ims, eptrb,
502
 
        match_isgroup);
503
 
      for (i = 1; i <= c; i++)
504
 
        md->offset_vector[md->offset_end - i] = save[i];
505
 
      if (save != stacksave) (pcre_free)(save);
506
 
      if (!rc) return FALSE;
507
 
 
508
 
      /* In case the recursion has set more capturing values, save the final
509
 
      number, then move along the subject till after the recursive match,
510
 
      and advance one byte in the pattern code. */
511
 
 
512
 
      offset_top = md->end_offset_top;
513
 
      eptr = md->end_match_ptr;
514
 
      ecode++;
515
 
      }
516
 
    break;
517
 
 
518
 
    /* "Once" brackets are like assertion brackets except that after a match,
519
 
    the point in the subject string is not moved back. Thus there can never be
520
 
    a move back into the brackets. Check the alternative branches in turn - the
521
 
    matching won't pass the KET for this kind of subpattern. If any one branch
522
 
    matches, we carry on as at the end of a normal bracket, leaving the subject
523
 
    pointer. */
524
 
 
525
 
    case OP_ONCE:
526
 
      {
527
 
      const uschar *prev = ecode;
528
 
      const uschar *saved_eptr = eptr;
529
 
 
530
 
      do
531
 
        {
532
 
        if (match(eptr, ecode+3, offset_top, md, ims, eptrb, match_isgroup))
533
 
          break;
534
 
        ecode += (ecode[1] << 8) + ecode[2];
535
 
        }
536
 
      while (*ecode == OP_ALT);
537
 
 
538
 
      /* If hit the end of the group (which could be repeated), fail */
539
 
 
540
 
      if (*ecode != OP_ONCE && *ecode != OP_ALT) return FALSE;
541
 
 
542
 
      /* Continue as from after the assertion, updating the offsets high water
543
 
      mark, since extracts may have been taken. */
544
 
 
545
 
      do ecode += (ecode[1] << 8) + ecode[2]; while (*ecode == OP_ALT);
546
 
 
547
 
      offset_top = md->end_offset_top;
548
 
      eptr = md->end_match_ptr;
549
 
 
550
 
      /* For a non-repeating ket, just continue at this level. This also
551
 
      happens for a repeating ket if no characters were matched in the group.
552
 
      This is the forcible breaking of infinite loops as implemented in Perl
553
 
      5.005. If there is an options reset, it will get obeyed in the normal
554
 
      course of events. */
555
 
 
556
 
      if (*ecode == OP_KET || eptr == saved_eptr)
557
 
        {
558
 
        ecode += 3;
559
 
        break;
560
 
        }
561
 
 
562
 
      /* The repeating kets try the rest of the pattern or restart from the
563
 
      preceding bracket, in the appropriate order. We need to reset any options
564
 
      that changed within the bracket before re-running it, so check the next
565
 
      opcode. */
566
 
 
567
 
      if (ecode[3] == OP_OPT)
568
 
        {
569
 
        ims = (ims & ~PCRE_IMS) | ecode[4];
570
 
        DPRINTF(("ims set to %02lx at group repeat\n", ims));
571
 
        }
572
 
 
573
 
      if (*ecode == OP_KETRMIN)
574
 
        {
575
 
        if (match(eptr, ecode+3, offset_top, md, ims, eptrb, 0) ||
576
 
            match(eptr, prev, offset_top, md, ims, eptrb, match_isgroup))
577
 
              return TRUE;
578
 
        }
579
 
      else  /* OP_KETRMAX */
580
 
        {
581
 
        if (match(eptr, prev, offset_top, md, ims, eptrb, match_isgroup) ||
582
 
            match(eptr, ecode+3, offset_top, md, ims, eptrb, 0)) return TRUE;
583
 
        }
584
 
      }
585
 
    return FALSE;
586
 
 
587
 
    /* An alternation is the end of a branch; scan along to find the end of the
588
 
    bracketed group and go to there. */
589
 
 
590
 
    case OP_ALT:
591
 
    do ecode += (ecode[1] << 8) + ecode[2]; while (*ecode == OP_ALT);
592
 
    break;
593
 
 
594
 
    /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating
595
 
    that it may occur zero times. It may repeat infinitely, or not at all -
596
 
    i.e. it could be ()* or ()? in the pattern. Brackets with fixed upper
597
 
    repeat limits are compiled as a number of copies, with the optional ones
598
 
    preceded by BRAZERO or BRAMINZERO. */
599
 
 
600
 
    case OP_BRAZERO:
601
 
      {
602
 
      const uschar *next = ecode+1;
603
 
      if (match(eptr, next, offset_top, md, ims, eptrb, match_isgroup))
604
 
        return TRUE;
605
 
      do next += (next[1] << 8) + next[2]; while (*next == OP_ALT);
606
 
      ecode = next + 3;
607
 
      }
608
 
    break;
609
 
 
610
 
    case OP_BRAMINZERO:
611
 
      {
612
 
      const uschar *next = ecode+1;
613
 
      do next += (next[1] << 8) + next[2]; while (*next == OP_ALT);
614
 
      if (match(eptr, next+3, offset_top, md, ims, eptrb, match_isgroup))
615
 
        return TRUE;
616
 
      ecode++;
617
 
      }
618
 
    break;
619
 
 
620
 
    /* End of a group, repeated or non-repeating. If we are at the end of
621
 
    an assertion "group", stop matching and return TRUE, but record the
622
 
    current high water mark for use by positive assertions. Do this also
623
 
    for the "once" (not-backup up) groups. */
624
 
 
625
 
    case OP_KET:
626
 
    case OP_KETRMIN:
627
 
    case OP_KETRMAX:
628
 
      {
629
 
      const uschar *prev = ecode - (ecode[1] << 8) - ecode[2];
630
 
      const uschar *saved_eptr = eptrb->saved_eptr;
631
 
 
632
 
      eptrb = eptrb->prev;    /* Back up the stack of bracket start pointers */
633
 
 
634
 
      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
635
 
          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||
636
 
          *prev == OP_ONCE)
637
 
        {
638
 
        md->end_match_ptr = eptr;      /* For ONCE */
639
 
        md->end_offset_top = offset_top;
640
 
        return TRUE;
641
 
        }
642
 
 
643
 
      /* In all other cases except a conditional group we have to check the
644
 
      group number back at the start and if necessary complete handling an
645
 
      extraction by setting the offsets and bumping the high water mark. */
646
 
 
647
 
      if (*prev != OP_COND)
648
 
        {
649
 
        int offset;
650
 
        int number = *prev - OP_BRA;
651
 
 
652
 
        /* For extended extraction brackets (large number), we have to fish out
653
 
        the number from a dummy opcode at the start. */
654
 
 
655
 
        if (number > EXTRACT_BASIC_MAX) number = (prev[4] << 8) | prev[5];
656
 
        offset = number << 1;
657
 
 
658
 
#ifdef DEBUG
659
 
        PCRE_PRINTF("end bracket %d", number);
660
 
        PCRE_PRINTF("\n");
661
 
#endif
662
 
 
663
 
        if (number > 0)
664
 
          {
665
 
          if (offset >= md->offset_max) md->offset_overflow = TRUE; else
666
 
            {
667
 
            md->offset_vector[offset] =
668
 
              md->offset_vector[md->offset_end - number];
669
 
            md->offset_vector[offset+1] = eptr - md->start_subject;
670
 
            if (offset_top <= offset) offset_top = offset + 2;
671
 
            }
672
 
          }
673
 
        }
674
 
 
675
 
      /* Reset the value of the ims flags, in case they got changed during
676
 
      the group. */
677
 
 
678
 
      ims = original_ims;
679
 
      DPRINTF(("ims reset to %02lx\n", ims));
680
 
 
681
 
      /* For a non-repeating ket, just continue at this level. This also
682
 
      happens for a repeating ket if no characters were matched in the group.
683
 
      This is the forcible breaking of infinite loops as implemented in Perl
684
 
      5.005. If there is an options reset, it will get obeyed in the normal
685
 
      course of events. */
686
 
 
687
 
      if (*ecode == OP_KET || eptr == saved_eptr)
688
 
        {
689
 
        ecode += 3;
690
 
        break;
691
 
        }
692
 
 
693
 
      /* The repeating kets try the rest of the pattern or restart from the
694
 
      preceding bracket, in the appropriate order. */
695
 
 
696
 
      if (*ecode == OP_KETRMIN)
697
 
        {
698
 
        if (match(eptr, ecode+3, offset_top, md, ims, eptrb, 0) ||
699
 
            match(eptr, prev, offset_top, md, ims, eptrb, match_isgroup))
700
 
              return TRUE;
701
 
        }
702
 
      else  /* OP_KETRMAX */
703
 
        {
704
 
        if (match(eptr, prev, offset_top, md, ims, eptrb, match_isgroup) ||
705
 
            match(eptr, ecode+3, offset_top, md, ims, eptrb, 0)) return TRUE;
706
 
        }
707
 
      }
708
 
    return FALSE;
709
 
 
710
 
    /* Start of subject unless notbol, or after internal newline if multiline */
711
 
 
712
 
    case OP_CIRC:
713
 
    if (md->notbol && eptr == md->start_subject) return FALSE;
714
 
    if ((ims & PCRE_MULTILINE) != 0)
715
 
      {
716
 
      if (eptr != md->start_subject && eptr[-1] != NEWLINE) return FALSE;
717
 
      ecode++;
718
 
      break;
719
 
      }
720
 
    /* ... else fall through */
721
 
 
722
 
    /* Start of subject assertion */
723
 
 
724
 
    case OP_SOD:
725
 
    if (eptr != md->start_subject) return FALSE;
726
 
    ecode++;
727
 
    break;
728
 
 
729
 
    /* Assert before internal newline if multiline, or before a terminating
730
 
    newline unless endonly is set, else end of subject unless noteol is set. */
731
 
 
732
 
    case OP_DOLL:
733
 
    if ((ims & PCRE_MULTILINE) != 0)
734
 
      {
735
 
      if (eptr < md->end_subject) { if (*eptr != NEWLINE) return FALSE; }
736
 
        else { if (md->noteol) return FALSE; }
737
 
      ecode++;
738
 
      break;
739
 
      }
740
 
    else
741
 
      {
742
 
      if (md->noteol) return FALSE;
743
 
      if (!md->endonly)
744
 
        {
745
 
        if (eptr < md->end_subject - 1 ||
746
 
           (eptr == md->end_subject - 1 && *eptr != NEWLINE)) return FALSE;
747
 
 
748
 
        ecode++;
749
 
        break;
750
 
        }
751
 
      }
752
 
    /* ... else fall through */
753
 
 
754
 
    /* End of subject assertion (\z) */
755
 
 
756
 
    case OP_EOD:
757
 
    if (eptr < md->end_subject) return FALSE;
758
 
    ecode++;
759
 
    break;
760
 
 
761
 
    /* End of subject or ending \n assertion (\Z) */
762
 
 
763
 
    case OP_EODN:
764
 
    if (eptr < md->end_subject - 1 ||
765
 
       (eptr == md->end_subject - 1 && *eptr != NEWLINE)) return FALSE;
766
 
    ecode++;
767
 
    break;
768
 
 
769
 
    /* Word boundary assertions */
770
 
 
771
 
    case OP_NOT_WORD_BOUNDARY:
772
 
    case OP_WORD_BOUNDARY:
773
 
      {
774
 
      BOOL prev_is_word = (eptr != md->start_subject) &&
775
 
        ((md->ctypes[eptr[-1]] & ctype_word) != 0);
776
 
      BOOL cur_is_word = (eptr < md->end_subject) &&
777
 
        ((md->ctypes[*eptr] & ctype_word) != 0);
778
 
      if ((*ecode++ == OP_WORD_BOUNDARY)?
779
 
           cur_is_word == prev_is_word : cur_is_word != prev_is_word)
780
 
        return FALSE;
781
 
      }
782
 
    break;
783
 
 
784
 
    /* Match a single character type; inline for speed */
785
 
 
786
 
    case OP_ANY:
787
 
    if ((ims & PCRE_DOTALL) == 0 && eptr < md->end_subject && *eptr == NEWLINE)
788
 
      return FALSE;
789
 
    if (eptr++ >= md->end_subject) return FALSE;
790
 
#ifdef SUPPORT_UTF8
791
 
    if (md->utf8)
792
 
      while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
793
 
#endif
794
 
    ecode++;
795
 
    break;
796
 
 
797
 
    case OP_NOT_DIGIT:
798
 
    if (eptr >= md->end_subject ||
799
 
       (md->ctypes[*eptr++] & ctype_digit) != 0)
800
 
      return FALSE;
801
 
    ecode++;
802
 
    break;
803
 
 
804
 
    case OP_DIGIT:
805
 
    if (eptr >= md->end_subject ||
806
 
       (md->ctypes[*eptr++] & ctype_digit) == 0)
807
 
      return FALSE;
808
 
    ecode++;
809
 
    break;
810
 
 
811
 
    case OP_NOT_WHITESPACE:
812
 
    if (eptr >= md->end_subject ||
813
 
       (md->ctypes[*eptr++] & ctype_space) != 0)
814
 
      return FALSE;
815
 
    ecode++;
816
 
    break;
817
 
 
818
 
    case OP_WHITESPACE:
819
 
    if (eptr >= md->end_subject ||
820
 
       (md->ctypes[*eptr++] & ctype_space) == 0)
821
 
      return FALSE;
822
 
    ecode++;
823
 
    break;
824
 
 
825
 
    case OP_NOT_WORDCHAR:
826
 
    if (eptr >= md->end_subject ||
827
 
       (md->ctypes[*eptr++] & ctype_word) != 0)
828
 
      return FALSE;
829
 
    ecode++;
830
 
    break;
831
 
 
832
 
    case OP_WORDCHAR:
833
 
    if (eptr >= md->end_subject ||
834
 
       (md->ctypes[*eptr++] & ctype_word) == 0)
835
 
      return FALSE;
836
 
    ecode++;
837
 
    break;
838
 
 
839
 
    /* Match a back reference, possibly repeatedly. Look past the end of the
840
 
    item to see if there is repeat information following. The code is similar
841
 
    to that for character classes, but repeated for efficiency. Then obey
842
 
    similar code to character type repeats - written out again for speed.
843
 
    However, if the referenced string is the empty string, always treat
844
 
    it as matched, any number of times (otherwise there could be infinite
845
 
    loops). */
846
 
 
847
 
    case OP_REF:
848
 
      {
849
 
      int length;
850
 
      int offset = (ecode[1] << 9) | (ecode[2] << 1); /* Doubled ref number */
851
 
      ecode += 3;                                     /* Advance past item */
852
 
 
853
 
      /* If the reference is unset, set the length to be longer than the amount
854
 
      of subject left; this ensures that every attempt at a match fails. We
855
 
      can't just fail here, because of the possibility of quantifiers with zero
856
 
      minima. */
857
 
 
858
 
      length = (offset >= offset_top || md->offset_vector[offset] < 0)?
859
 
        md->end_subject - eptr + 1 :
860
 
        md->offset_vector[offset+1] - md->offset_vector[offset];
861
 
 
862
 
      /* Set up for repetition, or handle the non-repeated case */
863
 
 
864
 
      switch (*ecode)
865
 
        {
866
 
        case OP_CRSTAR:
867
 
        case OP_CRMINSTAR:
868
 
        case OP_CRPLUS:
869
 
        case OP_CRMINPLUS:
870
 
        case OP_CRQUERY:
871
 
        case OP_CRMINQUERY:
872
 
        c = *ecode++ - OP_CRSTAR;
873
 
        minimize = (c & 1) != 0;
874
 
        min = rep_min[c];                 /* Pick up values from tables; */
875
 
        max = rep_max[c];                 /* zero for max => infinity */
876
 
        if (max == 0) max = INT_MAX;
877
 
        break;
878
 
 
879
 
        case OP_CRRANGE:
880
 
        case OP_CRMINRANGE:
881
 
        minimize = (*ecode == OP_CRMINRANGE);
882
 
        min = (ecode[1] << 8) + ecode[2];
883
 
        max = (ecode[3] << 8) + ecode[4];
884
 
        if (max == 0) max = INT_MAX;
885
 
        ecode += 5;
886
 
        break;
887
 
 
888
 
        default:               /* No repeat follows */
889
 
        if (!match_ref(offset, eptr, length, md, ims)) return FALSE;
890
 
        eptr += length;
891
 
        continue;              /* With the main loop */
892
 
        }
893
 
 
894
 
      /* If the length of the reference is zero, just continue with the
895
 
      main loop. */
896
 
 
897
 
      if (length == 0) continue;
898
 
 
899
 
      /* First, ensure the minimum number of matches are present. We get back
900
 
      the length of the reference string explicitly rather than passing the
901
 
      address of eptr, so that eptr can be a register variable. */
902
 
 
903
 
      for (i = 1; i <= min; i++)
904
 
        {
905
 
        if (!match_ref(offset, eptr, length, md, ims)) return FALSE;
906
 
        eptr += length;
907
 
        }
908
 
 
909
 
      /* If min = max, continue at the same level without recursion.
910
 
      They are not both allowed to be zero. */
911
 
 
912
 
      if (min == max) continue;
913
 
 
914
 
      /* If minimizing, keep trying and advancing the pointer */
915
 
 
916
 
      if (minimize)
917
 
        {
918
 
        for (i = min;; i++)
919
 
          {
920
 
          if (match(eptr, ecode, offset_top, md, ims, eptrb, 0))
921
 
            return TRUE;
922
 
          if (i >= max || !match_ref(offset, eptr, length, md, ims))
923
 
            return FALSE;
924
 
          eptr += length;
925
 
          }
926
 
        /* Control never gets here */
927
 
        }
928
 
 
929
 
      /* If maximizing, find the longest string and work backwards */
930
 
 
931
 
      else
932
 
        {
933
 
        const uschar *pp = eptr;
934
 
        for (i = min; i < max; i++)
935
 
          {
936
 
          if (!match_ref(offset, eptr, length, md, ims)) break;
937
 
          eptr += length;
938
 
          }
939
 
        while (eptr >= pp)
940
 
          {
941
 
          if (match(eptr, ecode, offset_top, md, ims, eptrb, 0))
942
 
            return TRUE;
943
 
          eptr -= length;
944
 
          }
945
 
        return FALSE;
946
 
        }
947
 
      }
948
 
    /* Control never gets here */
949
 
 
950
 
 
951
 
 
952
 
    /* Match a character class, possibly repeatedly. Look past the end of the
953
 
    item to see if there is repeat information following. Then obey similar
954
 
    code to character type repeats - written out again for speed. */
955
 
 
956
 
    case OP_CLASS:
957
 
      {
958
 
      const uschar *data = ecode + 1;  /* Save for matching */
959
 
      ecode += 33;                     /* Advance past the item */
960
 
 
961
 
      switch (*ecode)
962
 
        {
963
 
        case OP_CRSTAR:
964
 
        case OP_CRMINSTAR:
965
 
        case OP_CRPLUS:
966
 
        case OP_CRMINPLUS:
967
 
        case OP_CRQUERY:
968
 
        case OP_CRMINQUERY:
969
 
        c = *ecode++ - OP_CRSTAR;
970
 
        minimize = (c & 1) != 0;
971
 
        min = rep_min[c];                 /* Pick up values from tables; */
972
 
        max = rep_max[c];                 /* zero for max => infinity */
973
 
        if (max == 0) max = INT_MAX;
974
 
        break;
975
 
 
976
 
        case OP_CRRANGE:
977
 
        case OP_CRMINRANGE:
978
 
        minimize = (*ecode == OP_CRMINRANGE);
979
 
        min = (ecode[1] << 8) + ecode[2];
980
 
        max = (ecode[3] << 8) + ecode[4];
981
 
        if (max == 0) max = INT_MAX;
982
 
        ecode += 5;
983
 
        break;
984
 
 
985
 
        default:               /* No repeat follows */
986
 
        min = max = 1;
987
 
        break;
988
 
        }
989
 
 
990
 
      /* First, ensure the minimum number of matches are present. */
991
 
 
992
 
      for (i = 1; i <= min; i++)
993
 
        {
994
 
        if (eptr >= md->end_subject) return FALSE;
995
 
        GETCHARINC(c, eptr)         /* Get character; increment eptr */
996
 
 
997
 
#ifdef SUPPORT_UTF8
998
 
        /* We do not yet support class members > 255 */
999
 
        if (c > 255) return FALSE;
1000
 
#endif
1001
 
 
1002
 
        if ((data[c/8] & (1 << (c&7))) != 0) continue;
1003
 
        return FALSE;
1004
 
        }
1005
 
 
1006
 
      /* If max == min we can continue with the main loop without the
1007
 
      need to recurse. */
1008
 
 
1009
 
      if (min == max) continue;
1010
 
 
1011
 
      /* If minimizing, keep testing the rest of the expression and advancing
1012
 
      the pointer while it matches the class. */
1013
 
 
1014
 
      if (minimize)
1015
 
        {
1016
 
        for (i = min;; i++)
1017
 
          {
1018
 
          if (match(eptr, ecode, offset_top, md, ims, eptrb, 0))
1019
 
            return TRUE;
1020
 
          if (i >= max || eptr >= md->end_subject) return FALSE;
1021
 
          GETCHARINC(c, eptr)       /* Get character; increment eptr */
1022
 
 
1023
 
#ifdef SUPPORT_UTF8
1024
 
          /* We do not yet support class members > 255 */
1025
 
          if (c > 255) return FALSE;
1026
 
#endif
1027
 
          if ((data[c/8] & (1 << (c&7))) != 0) continue;
1028
 
          return FALSE;
1029
 
          }
1030
 
        /* Control never gets here */
1031
 
        }
1032
 
 
1033
 
      /* If maximizing, find the longest possible run, then work backwards. */
1034
 
 
1035
 
      else
1036
 
        {
1037
 
        const uschar *pp = eptr;
1038
 
        int len = 1;
1039
 
        for (i = min; i < max; i++)
1040
 
          {
1041
 
          if (eptr >= md->end_subject) break;
1042
 
          GETCHARLEN(c, eptr, len)  /* Get character, set length if UTF-8 */
1043
 
 
1044
 
#ifdef SUPPORT_UTF8
1045
 
          /* We do not yet support class members > 255 */
1046
 
          if (c > 255) break;
1047
 
#endif
1048
 
          if ((data[c/8] & (1 << (c&7))) == 0) break;
1049
 
          eptr += len;
1050
 
          }
1051
 
 
1052
 
        while (eptr >= pp)
1053
 
          {
1054
 
          if (match(eptr--, ecode, offset_top, md, ims, eptrb, 0))
1055
 
            return TRUE;
1056
 
 
1057
 
#ifdef SUPPORT_UTF8
1058
 
          BACKCHAR(eptr)
1059
 
#endif
1060
 
          }
1061
 
        return FALSE;
1062
 
        }
1063
 
      }
1064
 
    /* Control never gets here */
1065
 
 
1066
 
    /* Match a run of characters */
1067
 
 
1068
 
    case OP_CHARS:
1069
 
      {
1070
 
      register int length = ecode[1];
1071
 
      ecode += 2;
1072
 
 
1073
 
#ifdef DEBUG    /* Sigh. Some compilers never learn. */
1074
 
      if (eptr >= md->end_subject)
1075
 
        PCRE_PRINTF("matching subject <null> against pattern ");
1076
 
      else
1077
 
        {
1078
 
        PCRE_PRINTF("matching subject ");
1079
 
        pchars(eptr, length, TRUE, md);
1080
 
        PCRE_PRINTF(" against pattern ");
1081
 
        }
1082
 
      pchars(ecode, length, FALSE, md);
1083
 
      PCRE_PRINTF("\n");
1084
 
#endif
1085
 
 
1086
 
      if (length > md->end_subject - eptr) return FALSE;
1087
 
      if ((ims & PCRE_CASELESS) != 0)
1088
 
        {
1089
 
        while (length-- > 0)
1090
 
          if (md->lcc[*ecode++] != md->lcc[*eptr++])
1091
 
            return FALSE;
1092
 
        }
1093
 
      else
1094
 
        {
1095
 
        while (length-- > 0) if (*ecode++ != *eptr++) return FALSE;
1096
 
        }
1097
 
      }
1098
 
    break;
1099
 
 
1100
 
    /* Match a single character repeatedly; different opcodes share code. */
1101
 
 
1102
 
    case OP_EXACT:
1103
 
    min = max = (ecode[1] << 8) + ecode[2];
1104
 
    ecode += 3;
1105
 
    goto REPEATCHAR;
1106
 
 
1107
 
    case OP_UPTO:
1108
 
    case OP_MINUPTO:
1109
 
    min = 0;
1110
 
    max = (ecode[1] << 8) + ecode[2];
1111
 
    minimize = *ecode == OP_MINUPTO;
1112
 
    ecode += 3;
1113
 
    goto REPEATCHAR;
1114
 
 
1115
 
    case OP_STAR:
1116
 
    case OP_MINSTAR:
1117
 
    case OP_PLUS:
1118
 
    case OP_MINPLUS:
1119
 
    case OP_QUERY:
1120
 
    case OP_MINQUERY:
1121
 
    c = *ecode++ - OP_STAR;
1122
 
    minimize = (c & 1) != 0;
1123
 
    min = rep_min[c];                 /* Pick up values from tables; */
1124
 
    max = rep_max[c];                 /* zero for max => infinity */
1125
 
    if (max == 0) max = INT_MAX;
1126
 
 
1127
 
    /* Common code for all repeated single-character matches. We can give
1128
 
    up quickly if there are fewer than the minimum number of characters left in
1129
 
    the subject. */
1130
 
 
1131
 
    REPEATCHAR:
1132
 
    if (min > md->end_subject - eptr) return FALSE;
1133
 
    c = *ecode++;
1134
 
 
1135
 
    /* The code is duplicated for the caseless and caseful cases, for speed,
1136
 
    since matching characters is likely to be quite common. First, ensure the
1137
 
    minimum number of matches are present. If min = max, continue at the same
1138
 
    level without recursing. Otherwise, if minimizing, keep trying the rest of
1139
 
    the expression and advancing one matching character if failing, up to the
1140
 
    maximum. Alternatively, if maximizing, find the maximum number of
1141
 
    characters and work backwards. */
1142
 
 
1143
 
    DPRINTF(("matching %c{%d,%d} against subject %.*s\n", c, min, max,
1144
 
      max, eptr));
1145
 
 
1146
 
    if ((ims & PCRE_CASELESS) != 0)
1147
 
      {
1148
 
      c = md->lcc[c];
1149
 
      for (i = 1; i <= min; i++)
1150
 
        if (c != md->lcc[*eptr++]) return FALSE;
1151
 
      if (min == max) continue;
1152
 
      if (minimize)
1153
 
        {
1154
 
        for (i = min;; i++)
1155
 
          {
1156
 
          if (match(eptr, ecode, offset_top, md, ims, eptrb, 0))
1157
 
            return TRUE;
1158
 
          if (i >= max || eptr >= md->end_subject ||
1159
 
              c != md->lcc[*eptr++])
1160
 
            return FALSE;
1161
 
          }
1162
 
        /* Control never gets here */
1163
 
        }
1164
 
      else
1165
 
        {
1166
 
        const uschar *pp = eptr;
1167
 
        for (i = min; i < max; i++)
1168
 
          {
1169
 
          if (eptr >= md->end_subject || c != md->lcc[*eptr]) break;
1170
 
          eptr++;
1171
 
          }
1172
 
        while (eptr >= pp)
1173
 
          if (match(eptr--, ecode, offset_top, md, ims, eptrb, 0))
1174
 
            return TRUE;
1175
 
        return FALSE;
1176
 
        }
1177
 
      /* Control never gets here */
1178
 
      }
1179
 
 
1180
 
    /* Caseful comparisons */
1181
 
 
1182
 
    else
1183
 
      {
1184
 
      for (i = 1; i <= min; i++) if (c != *eptr++) return FALSE;
1185
 
      if (min == max) continue;
1186
 
      if (minimize)
1187
 
        {
1188
 
        for (i = min;; i++)
1189
 
          {
1190
 
          if (match(eptr, ecode, offset_top, md, ims, eptrb, 0))
1191
 
            return TRUE;
1192
 
          if (i >= max || eptr >= md->end_subject || c != *eptr++) return FALSE;
1193
 
          }
1194
 
        /* Control never gets here */
1195
 
        }
1196
 
      else
1197
 
        {
1198
 
        const uschar *pp = eptr;
1199
 
        for (i = min; i < max; i++)
1200
 
          {
1201
 
          if (eptr >= md->end_subject || c != *eptr) break;
1202
 
          eptr++;
1203
 
          }
1204
 
        while (eptr >= pp)
1205
 
         if (match(eptr--, ecode, offset_top, md, ims, eptrb, 0))
1206
 
           return TRUE;
1207
 
        return FALSE;
1208
 
        }
1209
 
      }
1210
 
    /* Control never gets here */
1211
 
 
1212
 
    /* Match a negated single character */
1213
 
 
1214
 
    case OP_NOT:
1215
 
    if (eptr >= md->end_subject) return FALSE;
1216
 
    ecode++;
1217
 
    if ((ims & PCRE_CASELESS) != 0)
1218
 
      {
1219
 
      if (md->lcc[*ecode++] == md->lcc[*eptr++]) return FALSE;
1220
 
      }
1221
 
    else
1222
 
      {
1223
 
      if (*ecode++ == *eptr++) return FALSE;
1224
 
      }
1225
 
    break;
1226
 
 
1227
 
    /* Match a negated single character repeatedly. This is almost a repeat of
1228
 
    the code for a repeated single character, but I haven't found a nice way of
1229
 
    commoning these up that doesn't require a test of the positive/negative
1230
 
    option for each character match. Maybe that wouldn't add very much to the
1231
 
    time taken, but character matching *is* what this is all about... */
1232
 
 
1233
 
    case OP_NOTEXACT:
1234
 
    min = max = (ecode[1] << 8) + ecode[2];
1235
 
    ecode += 3;
1236
 
    goto REPEATNOTCHAR;
1237
 
 
1238
 
    case OP_NOTUPTO:
1239
 
    case OP_NOTMINUPTO:
1240
 
    min = 0;
1241
 
    max = (ecode[1] << 8) + ecode[2];
1242
 
    minimize = *ecode == OP_NOTMINUPTO;
1243
 
    ecode += 3;
1244
 
    goto REPEATNOTCHAR;
1245
 
 
1246
 
    case OP_NOTSTAR:
1247
 
    case OP_NOTMINSTAR:
1248
 
    case OP_NOTPLUS:
1249
 
    case OP_NOTMINPLUS:
1250
 
    case OP_NOTQUERY:
1251
 
    case OP_NOTMINQUERY:
1252
 
    c = *ecode++ - OP_NOTSTAR;
1253
 
    minimize = (c & 1) != 0;
1254
 
    min = rep_min[c];                 /* Pick up values from tables; */
1255
 
    max = rep_max[c];                 /* zero for max => infinity */
1256
 
    if (max == 0) max = INT_MAX;
1257
 
 
1258
 
    /* Common code for all repeated single-character matches. We can give
1259
 
    up quickly if there are fewer than the minimum number of characters left in
1260
 
    the subject. */
1261
 
 
1262
 
    REPEATNOTCHAR:
1263
 
    if (min > md->end_subject - eptr) return FALSE;
1264
 
    c = *ecode++;
1265
 
 
1266
 
    /* The code is duplicated for the caseless and caseful cases, for speed,
1267
 
    since matching characters is likely to be quite common. First, ensure the
1268
 
    minimum number of matches are present. If min = max, continue at the same
1269
 
    level without recursing. Otherwise, if minimizing, keep trying the rest of
1270
 
    the expression and advancing one matching character if failing, up to the
1271
 
    maximum. Alternatively, if maximizing, find the maximum number of
1272
 
    characters and work backwards. */
1273
 
 
1274
 
    DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", c, min, max,
1275
 
      max, eptr));
1276
 
 
1277
 
    if ((ims & PCRE_CASELESS) != 0)
1278
 
      {
1279
 
      c = md->lcc[c];
1280
 
      for (i = 1; i <= min; i++)
1281
 
        if (c == md->lcc[*eptr++]) return FALSE;
1282
 
      if (min == max) continue;
1283
 
      if (minimize)
1284
 
        {
1285
 
        for (i = min;; i++)
1286
 
          {
1287
 
          if (match(eptr, ecode, offset_top, md, ims, eptrb, 0))
1288
 
            return TRUE;
1289
 
          if (i >= max || eptr >= md->end_subject ||
1290
 
              c == md->lcc[*eptr++])
1291
 
            return FALSE;
1292
 
          }
1293
 
        /* Control never gets here */
1294
 
        }
1295
 
      else
1296
 
        {
1297
 
        const uschar *pp = eptr;
1298
 
        for (i = min; i < max; i++)
1299
 
          {
1300
 
          if (eptr >= md->end_subject || c == md->lcc[*eptr]) break;
1301
 
          eptr++;
1302
 
          }
1303
 
        while (eptr >= pp)
1304
 
          if (match(eptr--, ecode, offset_top, md, ims, eptrb, 0))
1305
 
            return TRUE;
1306
 
        return FALSE;
1307
 
        }
1308
 
      /* Control never gets here */
1309
 
      }
1310
 
 
1311
 
    /* Caseful comparisons */
1312
 
 
1313
 
    else
1314
 
      {
1315
 
      for (i = 1; i <= min; i++) if (c == *eptr++) return FALSE;
1316
 
      if (min == max) continue;
1317
 
      if (minimize)
1318
 
        {
1319
 
        for (i = min;; i++)
1320
 
          {
1321
 
          if (match(eptr, ecode, offset_top, md, ims, eptrb, 0))
1322
 
            return TRUE;
1323
 
          if (i >= max || eptr >= md->end_subject || c == *eptr++) return FALSE;
1324
 
          }
1325
 
        /* Control never gets here */
1326
 
        }
1327
 
      else
1328
 
        {
1329
 
        const uschar *pp = eptr;
1330
 
        for (i = min; i < max; i++)
1331
 
          {
1332
 
          if (eptr >= md->end_subject || c == *eptr) break;
1333
 
          eptr++;
1334
 
          }
1335
 
        while (eptr >= pp)
1336
 
         if (match(eptr--, ecode, offset_top, md, ims, eptrb, 0))
1337
 
           return TRUE;
1338
 
        return FALSE;
1339
 
        }
1340
 
      }
1341
 
    /* Control never gets here */
1342
 
 
1343
 
    /* Match a single character type repeatedly; several different opcodes
1344
 
    share code. This is very similar to the code for single characters, but we
1345
 
    repeat it in the interests of efficiency. */
1346
 
 
1347
 
    case OP_TYPEEXACT:
1348
 
    min = max = (ecode[1] << 8) + ecode[2];
1349
 
    minimize = TRUE;
1350
 
    ecode += 3;
1351
 
    goto REPEATTYPE;
1352
 
 
1353
 
    case OP_TYPEUPTO:
1354
 
    case OP_TYPEMINUPTO:
1355
 
    min = 0;
1356
 
    max = (ecode[1] << 8) + ecode[2];
1357
 
    minimize = *ecode == OP_TYPEMINUPTO;
1358
 
    ecode += 3;
1359
 
    goto REPEATTYPE;
1360
 
 
1361
 
    case OP_TYPESTAR:
1362
 
    case OP_TYPEMINSTAR:
1363
 
    case OP_TYPEPLUS:
1364
 
    case OP_TYPEMINPLUS:
1365
 
    case OP_TYPEQUERY:
1366
 
    case OP_TYPEMINQUERY:
1367
 
    c = *ecode++ - OP_TYPESTAR;
1368
 
    minimize = (c & 1) != 0;
1369
 
    min = rep_min[c];                 /* Pick up values from tables; */
1370
 
    max = rep_max[c];                 /* zero for max => infinity */
1371
 
    if (max == 0) max = INT_MAX;
1372
 
 
1373
 
    /* Common code for all repeated single character type matches */
1374
 
 
1375
 
    REPEATTYPE:
1376
 
    ctype = *ecode++;      /* Code for the character type */
1377
 
 
1378
 
    /* First, ensure the minimum number of matches are present. Use inline
1379
 
    code for maximizing the speed, and do the type test once at the start
1380
 
    (i.e. keep it out of the loop). Also we can test that there are at least
1381
 
    the minimum number of bytes before we start, except when doing '.' in
1382
 
    UTF8 mode. Leave the test in in all cases; in the special case we have
1383
 
    to test after each character. */
1384
 
 
1385
 
    if (min > md->end_subject - eptr) return FALSE;
1386
 
    if (min > 0) switch(ctype)
1387
 
      {
1388
 
      case OP_ANY:
1389
 
#ifdef SUPPORT_UTF8
1390
 
      if (md->utf8)
1391
 
        {
1392
 
        for (i = 1; i <= min; i++)
1393
 
          {
1394
 
          if (eptr >= md->end_subject ||
1395
 
             (*eptr++ == NEWLINE && (ims & PCRE_DOTALL) == 0))
1396
 
            return FALSE;
1397
 
          while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
1398
 
          }
1399
 
        break;
1400
 
        }
1401
 
#endif
1402
 
      /* Non-UTF8 can be faster */
1403
 
      if ((ims & PCRE_DOTALL) == 0)
1404
 
        { for (i = 1; i <= min; i++) if (*eptr++ == NEWLINE) return FALSE; }
1405
 
      else eptr += min;
1406
 
      break;
1407
 
 
1408
 
      case OP_NOT_DIGIT:
1409
 
      for (i = 1; i <= min; i++)
1410
 
        if ((md->ctypes[*eptr++] & ctype_digit) != 0) return FALSE;
1411
 
      break;
1412
 
 
1413
 
      case OP_DIGIT:
1414
 
      for (i = 1; i <= min; i++)
1415
 
        if ((md->ctypes[*eptr++] & ctype_digit) == 0) return FALSE;
1416
 
      break;
1417
 
 
1418
 
      case OP_NOT_WHITESPACE:
1419
 
      for (i = 1; i <= min; i++)
1420
 
        if ((md->ctypes[*eptr++] & ctype_space) != 0) return FALSE;
1421
 
      break;
1422
 
 
1423
 
      case OP_WHITESPACE:
1424
 
      for (i = 1; i <= min; i++)
1425
 
        if ((md->ctypes[*eptr++] & ctype_space) == 0) return FALSE;
1426
 
      break;
1427
 
 
1428
 
      case OP_NOT_WORDCHAR:
1429
 
      for (i = 1; i <= min; i++)
1430
 
        if ((md->ctypes[*eptr++] & ctype_word) != 0)
1431
 
          return FALSE;
1432
 
      break;
1433
 
 
1434
 
      case OP_WORDCHAR:
1435
 
      for (i = 1; i <= min; i++)
1436
 
        if ((md->ctypes[*eptr++] & ctype_word) == 0)
1437
 
          return FALSE;
1438
 
      break;
1439
 
      }
1440
 
 
1441
 
    /* If min = max, continue at the same level without recursing */
1442
 
 
1443
 
    if (min == max) continue;
1444
 
 
1445
 
    /* If minimizing, we have to test the rest of the pattern before each
1446
 
    subsequent match. */
1447
 
 
1448
 
    if (minimize)
1449
 
      {
1450
 
      for (i = min;; i++)
1451
 
        {
1452
 
        if (match(eptr, ecode, offset_top, md, ims, eptrb, 0)) return TRUE;
1453
 
        if (i >= max || eptr >= md->end_subject) return FALSE;
1454
 
 
1455
 
        c = *eptr++;
1456
 
        switch(ctype)
1457
 
          {
1458
 
          case OP_ANY:
1459
 
          if ((ims & PCRE_DOTALL) == 0 && c == NEWLINE) return FALSE;
1460
 
#ifdef SUPPORT_UTF8
1461
 
          if (md->utf8)
1462
 
            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
1463
 
#endif
1464
 
          break;
1465
 
 
1466
 
          case OP_NOT_DIGIT:
1467
 
          if ((md->ctypes[c] & ctype_digit) != 0) return FALSE;
1468
 
          break;
1469
 
 
1470
 
          case OP_DIGIT:
1471
 
          if ((md->ctypes[c] & ctype_digit) == 0) return FALSE;
1472
 
          break;
1473
 
 
1474
 
          case OP_NOT_WHITESPACE:
1475
 
          if ((md->ctypes[c] & ctype_space) != 0) return FALSE;
1476
 
          break;
1477
 
 
1478
 
          case OP_WHITESPACE:
1479
 
          if  ((md->ctypes[c] & ctype_space) == 0) return FALSE;
1480
 
          break;
1481
 
 
1482
 
          case OP_NOT_WORDCHAR:
1483
 
          if ((md->ctypes[c] & ctype_word) != 0) return FALSE;
1484
 
          break;
1485
 
 
1486
 
          case OP_WORDCHAR:
1487
 
          if ((md->ctypes[c] & ctype_word) == 0) return FALSE;
1488
 
          break;
1489
 
          }
1490
 
        }
1491
 
      /* Control never gets here */
1492
 
      }
1493
 
 
1494
 
    /* If maximizing it is worth using inline code for speed, doing the type
1495
 
    test once at the start (i.e. keep it out of the loop). */
1496
 
 
1497
 
    else
1498
 
      {
1499
 
      const uschar *pp = eptr;
1500
 
      switch(ctype)
1501
 
        {
1502
 
        case OP_ANY:
1503
 
 
1504
 
        /* Special code is required for UTF8, but when the maximum is unlimited
1505
 
        we don't need it. */
1506
 
 
1507
 
#ifdef SUPPORT_UTF8
1508
 
        if (md->utf8 && max < INT_MAX)
1509
 
          {
1510
 
          if ((ims & PCRE_DOTALL) == 0)
1511
 
            {
1512
 
            for (i = min; i < max; i++)
1513
 
              {
1514
 
              if (eptr >= md->end_subject || *eptr++ == NEWLINE) break;
1515
 
              while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
1516
 
              }
1517
 
            }
1518
 
          else
1519
 
            {
1520
 
            for (i = min; i < max; i++)
1521
 
              {
1522
 
              eptr++;
1523
 
              while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
1524
 
              }
1525
 
            }
1526
 
          break;
1527
 
          }
1528
 
#endif
1529
 
        /* Non-UTF8 can be faster */
1530
 
        if ((ims & PCRE_DOTALL) == 0)
1531
 
          {
1532
 
          for (i = min; i < max; i++)
1533
 
            {
1534
 
            if (eptr >= md->end_subject || *eptr == NEWLINE) break;
1535
 
            eptr++;
1536
 
            }
1537
 
          }
1538
 
        else
1539
 
          {
1540
 
          c = max - min;
1541
 
          if (c > md->end_subject - eptr) c = md->end_subject - eptr;
1542
 
          eptr += c;
1543
 
          }
1544
 
        break;
1545
 
 
1546
 
        case OP_NOT_DIGIT:
1547
 
        for (i = min; i < max; i++)
1548
 
          {
1549
 
          if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_digit) != 0)
1550
 
            break;
1551
 
          eptr++;
1552
 
          }
1553
 
        break;
1554
 
 
1555
 
        case OP_DIGIT:
1556
 
        for (i = min; i < max; i++)
1557
 
          {
1558
 
          if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_digit) == 0)
1559
 
            break;
1560
 
          eptr++;
1561
 
          }
1562
 
        break;
1563
 
 
1564
 
        case OP_NOT_WHITESPACE:
1565
 
        for (i = min; i < max; i++)
1566
 
          {
1567
 
          if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_space) != 0)
1568
 
            break;
1569
 
          eptr++;
1570
 
          }
1571
 
        break;
1572
 
 
1573
 
        case OP_WHITESPACE:
1574
 
        for (i = min; i < max; i++)
1575
 
          {
1576
 
          if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_space) == 0)
1577
 
            break;
1578
 
          eptr++;
1579
 
          }
1580
 
        break;
1581
 
 
1582
 
        case OP_NOT_WORDCHAR:
1583
 
        for (i = min; i < max; i++)
1584
 
          {
1585
 
          if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_word) != 0)
1586
 
            break;
1587
 
          eptr++;
1588
 
          }
1589
 
        break;
1590
 
 
1591
 
        case OP_WORDCHAR:
1592
 
        for (i = min; i < max; i++)
1593
 
          {
1594
 
          if (eptr >= md->end_subject || (md->ctypes[*eptr] & ctype_word) == 0)
1595
 
            break;
1596
 
          eptr++;
1597
 
          }
1598
 
        break;
1599
 
        }
1600
 
 
1601
 
      while (eptr >= pp)
1602
 
        {
1603
 
        if (match(eptr--, ecode, offset_top, md, ims, eptrb, 0))
1604
 
          return TRUE;
1605
 
#ifdef SUPPORT_UTF8
1606
 
        if (md->utf8)
1607
 
          while (eptr > pp && (*eptr & 0xc0) == 0x80) eptr--;
1608
 
#endif
1609
 
        }
1610
 
      return FALSE;
1611
 
      }
1612
 
    /* Control never gets here */
1613
 
 
1614
 
    /* There's been some horrible disaster. */
1615
 
 
1616
 
    default:
1617
 
    DPRINTF(("Unknown opcode %d\n", *ecode));
1618
 
    md->errorcode = PCRE_ERROR_UNKNOWN_NODE;
1619
 
    return FALSE;
1620
 
    }
1621
 
 
1622
 
  /* Do not stick any code in here without much thought; it is assumed
1623
 
  that "continue" in the code above comes out to here to repeat the main
1624
 
  loop. */
1625
 
 
1626
 
  }             /* End of main loop */
1627
 
/* Control never reaches here */
1628
 
}
1629
 
 
1630
 
 
1631
 
/*************************************************
1632
 
*         Execute a Regular Expression           *
1633
 
*************************************************/
1634
 
 
1635
 
/* This function applies a compiled re to a subject string and picks out
1636
 
portions of the string if it matches. Two elements in the vector are set for
1637
 
each substring: the offsets to the start and end of the substring.
1638
 
 
1639
 
Arguments:
1640
 
  external_re     points to the compiled expression
1641
 
  external_extra  points to "hints" from pcre_study() or is NULL
1642
 
  subject         points to the subject string
1643
 
  length          length of subject string (may contain binary zeros)
1644
 
  start_offset    where to start in the subject string
1645
 
  options         option bits
1646
 
  offsets         points to a vector of ints to be filled in with offsets
1647
 
  offsetcount     the number of elements in the vector
1648
 
 
1649
 
Returns:          > 0 => success; value is the number of elements filled in
1650
 
                  = 0 => success, but offsets is not big enough
1651
 
                   -1 => failed to match
1652
 
                 < -1 => some kind of unexpected problem
1653
 
*/
1654
 
 
1655
 
int
1656
 
pcre_exec(const pcre *external_re, const pcre_extra *external_extra,
1657
 
  const char *subject, int length, int start_offset, int options, int *offsets,
1658
 
  int offsetcount)
1659
 
{
1660
 
int resetcount, ocount;
1661
 
int first_char = -1;
1662
 
int req_char = -1;
1663
 
int req_char2 = -1;
1664
 
unsigned long int ims = 0;
1665
 
match_data match_block;
1666
 
const uschar *start_bits = NULL;
1667
 
const uschar *start_match = (const uschar *)subject + start_offset;
1668
 
const uschar *end_subject;
1669
 
const uschar *req_char_ptr = start_match - 1;
1670
 
const real_pcre *re = (const real_pcre *)external_re;
1671
 
const real_pcre_extra *extra = (const real_pcre_extra *)external_extra;
1672
 
BOOL using_temporary_offsets = FALSE;
1673
 
BOOL anchored;
1674
 
BOOL startline;
1675
 
 
1676
 
if ((options & ~PUBLIC_EXEC_OPTIONS) != 0) return PCRE_ERROR_BADOPTION;
1677
 
 
1678
 
if (re == NULL || subject == NULL ||
1679
 
   (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;
1680
 
if (re->magic_number != MAGIC_NUMBER) return PCRE_ERROR_BADMAGIC;
1681
 
 
1682
 
anchored = ((re->options | options) & PCRE_ANCHORED) != 0;
1683
 
startline = (re->options & PCRE_STARTLINE) != 0;
1684
 
 
1685
 
match_block.start_pattern = re->code;
1686
 
match_block.start_subject = (const uschar *)subject;
1687
 
match_block.end_subject = match_block.start_subject + length;
1688
 
end_subject = match_block.end_subject;
1689
 
 
1690
 
match_block.endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;
1691
 
match_block.utf8 = (re->options & PCRE_UTF8) != 0;
1692
 
 
1693
 
match_block.notbol = (options & PCRE_NOTBOL) != 0;
1694
 
match_block.noteol = (options & PCRE_NOTEOL) != 0;
1695
 
match_block.notempty = (options & PCRE_NOTEMPTY) != 0;
1696
 
 
1697
 
match_block.errorcode = PCRE_ERROR_NOMATCH;     /* Default error */
1698
 
 
1699
 
match_block.lcc = re->tables + lcc_offset;
1700
 
match_block.ctypes = re->tables + ctypes_offset;
1701
 
 
1702
 
/* The ims options can vary during the matching as a result of the presence
1703
 
of (?ims) items in the pattern. They are kept in a local variable so that
1704
 
restoring at the exit of a group is easy. */
1705
 
 
1706
 
ims = re->options & (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL);
1707
 
 
1708
 
/* If the expression has got more back references than the offsets supplied can
1709
 
hold, we get a temporary bit of working store to use during the matching.
1710
 
Otherwise, we can use the vector supplied, rounding down its size to a multiple
1711
 
of 3. */
1712
 
 
1713
 
ocount = offsetcount - (offsetcount % 3);
1714
 
 
1715
 
if (re->top_backref > 0 && re->top_backref >= ocount/3)
1716
 
  {
1717
 
  ocount = re->top_backref * 3 + 3;
1718
 
  match_block.offset_vector = (int *)(pcre_malloc)(ocount * sizeof(int));
1719
 
  if (match_block.offset_vector == NULL) return PCRE_ERROR_NOMEMORY;
1720
 
  using_temporary_offsets = TRUE;
1721
 
  DPRINTF(("Got memory to hold back references\n"));
1722
 
  }
1723
 
else match_block.offset_vector = offsets;
1724
 
 
1725
 
match_block.offset_end = ocount;
1726
 
match_block.offset_max = (2*ocount)/3;
1727
 
match_block.offset_overflow = FALSE;
1728
 
 
1729
 
/* Compute the minimum number of offsets that we need to reset each time. Doing
1730
 
this makes a huge difference to execution time when there aren't many brackets
1731
 
in the pattern. */
1732
 
 
1733
 
resetcount = 2 + re->top_bracket * 2;
1734
 
if (resetcount > offsetcount) resetcount = ocount;
1735
 
 
1736
 
/* Reset the working variable associated with each extraction. These should
1737
 
never be used unless previously set, but they get saved and restored, and so we
1738
 
initialize them to avoid reading uninitialized locations. */
1739
 
 
1740
 
if (match_block.offset_vector != NULL)
1741
 
  {
1742
 
  register int *iptr = match_block.offset_vector + ocount;
1743
 
  register int *iend = iptr - resetcount/2 + 1;
1744
 
  while (--iptr >= iend) *iptr = -1;
1745
 
  }
1746
 
 
1747
 
/* Set up the first character to match, if available. The first_char value is
1748
 
never set for an anchored regular expression, but the anchoring may be forced
1749
 
at run time, so we have to test for anchoring. The first char may be unset for
1750
 
an unanchored pattern, of course. If there's no first char and the pattern was
1751
 
studied, there may be a bitmap of possible first characters. */
1752
 
 
1753
 
if (!anchored)
1754
 
  {
1755
 
  if ((re->options & PCRE_FIRSTSET) != 0)
1756
 
    {
1757
 
    first_char = re->first_char;
1758
 
    if ((ims & PCRE_CASELESS) != 0) first_char = match_block.lcc[first_char];
1759
 
    }
1760
 
  else
1761
 
    if (!startline && extra != NULL &&
1762
 
      (extra->options & PCRE_STUDY_MAPPED) != 0)
1763
 
        start_bits = extra->start_bits;
1764
 
  }
1765
 
 
1766
 
/* For anchored or unanchored matches, there may be a "last known required
1767
 
character" set. If the PCRE_CASELESS is set, implying that the match starts
1768
 
caselessly, or if there are any changes of this flag within the regex, set up
1769
 
both cases of the character. Otherwise set the two values the same, which will
1770
 
avoid duplicate testing (which takes significant time). This covers the vast
1771
 
majority of cases. It will be suboptimal when the case flag changes in a regex
1772
 
and the required character in fact is caseful. */
1773
 
 
1774
 
if ((re->options & PCRE_REQCHSET) != 0)
1775
 
  {
1776
 
  req_char = re->req_char;
1777
 
  req_char2 = ((re->options & (PCRE_CASELESS | PCRE_ICHANGED)) != 0)?
1778
 
    (re->tables + fcc_offset)[req_char] : req_char;
1779
 
  }
1780
 
 
1781
 
/* Loop for handling unanchored repeated matching attempts; for anchored regexs
1782
 
the loop runs just once. */
1783
 
 
1784
 
do
1785
 
  {
1786
 
  int rc;
1787
 
  register int *iptr = match_block.offset_vector;
1788
 
  register int *iend = iptr + resetcount;
1789
 
 
1790
 
  /* Reset the maximum number of extractions we might see. */
1791
 
 
1792
 
  while (iptr < iend) *iptr++ = -1;
1793
 
 
1794
 
  /* Advance to a unique first char if possible */
1795
 
 
1796
 
  if (first_char >= 0)
1797
 
    {
1798
 
    if ((ims & PCRE_CASELESS) != 0)
1799
 
      while (start_match < end_subject &&
1800
 
             match_block.lcc[*start_match] != first_char)
1801
 
        start_match++;
1802
 
    else
1803
 
      while (start_match < end_subject && *start_match != first_char)
1804
 
        start_match++;
1805
 
    }
1806
 
 
1807
 
  /* Or to just after \n for a multiline match if possible */
1808
 
 
1809
 
  else if (startline)
1810
 
    {
1811
 
    if (start_match > match_block.start_subject + start_offset)
1812
 
      {
1813
 
      while (start_match < end_subject && start_match[-1] != NEWLINE)
1814
 
        start_match++;
1815
 
      }
1816
 
    }
1817
 
 
1818
 
  /* Or to a non-unique first char after study */
1819
 
 
1820
 
  else if (start_bits != NULL)
1821
 
    {
1822
 
    while (start_match < end_subject)
1823
 
      {
1824
 
      register int c = *start_match;
1825
 
      if ((start_bits[c/8] & (1 << (c&7))) == 0) start_match++; else break;
1826
 
      }
1827
 
    }
1828
 
 
1829
 
#ifdef DEBUG  /* Sigh. Some compilers never learn. */
1830
 
  PCRE_PRINTF(">>>> Match against: ");
1831
 
  pchars(start_match, end_subject - start_match, TRUE, &match_block);
1832
 
  PCRE_PRINTF("\n");
1833
 
#endif
1834
 
 
1835
 
  /* If req_char is set, we know that that character must appear in the subject
1836
 
  for the match to succeed. If the first character is set, req_char must be
1837
 
  later in the subject; otherwise the test starts at the match point. This
1838
 
  optimization can save a huge amount of backtracking in patterns with nested
1839
 
  unlimited repeats that aren't going to match. We don't know what the state of
1840
 
  case matching may be when this character is hit, so test for it in both its
1841
 
  cases if necessary. However, the different cased versions will not be set up
1842
 
  unless PCRE_CASELESS was given or the casing state changes within the regex.
1843
 
  Writing separate code makes it go faster, as does using an autoincrement and
1844
 
  backing off on a match. */
1845
 
 
1846
 
  if (req_char >= 0)
1847
 
    {
1848
 
    register const uschar *p = start_match + ((first_char >= 0)? 1 : 0);
1849
 
 
1850
 
    /* We don't need to repeat the search if we haven't yet reached the
1851
 
    place we found it at last time. */
1852
 
 
1853
 
    if (p > req_char_ptr)
1854
 
      {
1855
 
      /* Do a single test if no case difference is set up */
1856
 
 
1857
 
      if (req_char == req_char2)
1858
 
        {
1859
 
        while (p < end_subject)
1860
 
          {
1861
 
          if (*p++ == req_char) { p--; break; }
1862
 
          }
1863
 
        }
1864
 
 
1865
 
      /* Otherwise test for either case */
1866
 
 
1867
 
      else
1868
 
        {
1869
 
        while (p < end_subject)
1870
 
          {
1871
 
          register int pp = *p++;
1872
 
          if (pp == req_char || pp == req_char2) { p--; break; }
1873
 
          }
1874
 
        }
1875
 
 
1876
 
      /* If we can't find the required character, break the matching loop */
1877
 
 
1878
 
      if (p >= end_subject) break;
1879
 
 
1880
 
      /* If we have found the required character, save the point where we
1881
 
      found it, so that we don't search again next time round the loop if
1882
 
      the start hasn't passed this character yet. */
1883
 
 
1884
 
      req_char_ptr = p;
1885
 
      }
1886
 
    }
1887
 
 
1888
 
  /* When a match occurs, substrings will be set for all internal extractions;
1889
 
  we just need to set up the whole thing as substring 0 before returning. If
1890
 
  there were too many extractions, set the return code to zero. In the case
1891
 
  where we had to get some local store to hold offsets for backreferences, copy
1892
 
  those back references that we can. In this case there need not be overflow
1893
 
  if certain parts of the pattern were not used. */
1894
 
 
1895
 
  match_block.start_match = start_match;
1896
 
  if (!match(start_match, re->code, 2, &match_block, ims, NULL, match_isgroup))
1897
 
    continue;
1898
 
 
1899
 
  /* Copy the offset information from temporary store if necessary */
1900
 
 
1901
 
  if (using_temporary_offsets)
1902
 
    {
1903
 
    if (offsetcount >= 4)
1904
 
      {
1905
 
      memcpy(offsets + 2, match_block.offset_vector + 2,
1906
 
        (offsetcount - 2) * sizeof(int));
1907
 
      DPRINTF(("Copied offsets from temporary memory\n"));
1908
 
      }
1909
 
    if (match_block.end_offset_top > offsetcount)
1910
 
      match_block.offset_overflow = TRUE;
1911
 
 
1912
 
    DPRINTF(("Freeing temporary memory\n"));
1913
 
    (pcre_free)(match_block.offset_vector);
1914
 
    }
1915
 
 
1916
 
  rc = match_block.offset_overflow? 0 : match_block.end_offset_top/2;
1917
 
 
1918
 
  if (offsetcount < 2) rc = 0; else
1919
 
    {
1920
 
    offsets[0] = start_match - match_block.start_subject;
1921
 
    offsets[1] = match_block.end_match_ptr - match_block.start_subject;
1922
 
    }
1923
 
 
1924
 
  DPRINTF((">>>> returning %d\n", rc));
1925
 
  return rc;
1926
 
  }
1927
 
 
1928
 
/* This "while" is the end of the "do" above */
1929
 
 
1930
 
while (!anchored &&
1931
 
       match_block.errorcode == PCRE_ERROR_NOMATCH &&
1932
 
       start_match++ < end_subject);
1933
 
 
1934
 
if (using_temporary_offsets)
1935
 
  {
1936
 
  DPRINTF(("Freeing temporary memory\n"));
1937
 
  (pcre_free)(match_block.offset_vector);
1938
 
  }
1939
 
 
1940
 
DPRINTF((">>>> returning %d\n", match_block.errorcode));
1941
 
 
1942
 
return match_block.errorcode;
1943
 
}
1944
 
 
1945
 
/* End of pcre.c */