/[pcre]/code/trunk/pcre_exec.c
ViewVC logotype

Diff of /code/trunk/pcre_exec.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 77 by nigel, Sat Feb 24 21:40:45 2007 UTC revision 1431 by ph10, Thu Jan 2 17:41:28 2014 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2005 University of Cambridge             Copyright (c) 1997-2014 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 37  POSSIBILITY OF SUCH DAMAGE. Line 37  POSSIBILITY OF SUCH DAMAGE.
37  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
38  */  */
39    
   
40  /* This module contains pcre_exec(), the externally visible function that does  /* This module contains pcre_exec(), the externally visible function that does
41  pattern matching using an NFA algorithm, trying to mimic Perl as closely as  pattern matching using an NFA algorithm, trying to mimic Perl as closely as
42  possible. There are also some static supporting functions. */  possible. There are also some static supporting functions. */
43    
44    #ifdef HAVE_CONFIG_H
45    #include "config.h"
46    #endif
47    
48    #define NLBLOCK md             /* Block containing newline information */
49    #define PSSTART start_subject  /* Field containing processed string start */
50    #define PSEND   end_subject    /* Field containing processed string end */
51    
52  #include "pcre_internal.h"  #include "pcre_internal.h"
53    
54    /* Undefine some potentially clashing cpp symbols */
55    
56  /* Structure for building a chain of data that actually lives on the  #undef min
57  stack, for holding the values of the subject pointer at the start of each  #undef max
 subpattern, so as to detect when an empty string has been matched by a  
 subpattern - to break infinite loops. When NO_RECURSE is set, these blocks  
 are on the heap, not on the stack. */  
   
 typedef struct eptrblock {  
   struct eptrblock *epb_prev;  
   const uschar *epb_saved_eptr;  
 } eptrblock;  
58    
59  /* Flag bits for the match() function */  /* The md->capture_last field uses the lower 16 bits for the last captured
60    substring (which can never be greater than 65535) and a bit in the top half
61    to mean "capture vector overflowed". This odd way of doing things was
62    implemented when it was realized that preserving and restoring the overflow bit
63    whenever the last capture number was saved/restored made for a neater
64    interface, and doing it this way saved on (a) another variable, which would
65    have increased the stack frame size (a big NO-NO in PCRE) and (b) another
66    separate set of save/restore instructions. The following defines are used in
67    implementing this. */
68    
69    #define CAPLMASK    0x0000ffff    /* The bits used for last_capture */
70    #define OVFLMASK    0xffff0000    /* The bits used for the overflow flag */
71    #define OVFLBIT     0x00010000    /* The bit that is set for overflow */
72    
73    /* Values for setting in md->match_function_type to indicate two special types
74    of call to match(). We do it this way to save on using another stack variable,
75    as stack usage is to be discouraged. */
76    
77  #define match_condassert   0x01    /* Called to check a condition assertion */  #define MATCH_CONDASSERT     1  /* Called to check a condition assertion */
78  #define match_isgroup      0x02    /* Set if start of bracketed group */  #define MATCH_CBEGROUP       2  /* Could-be-empty unlimited repeat group */
79    
80  /* Non-error returns from the match() function. Error returns are externally  /* Non-error returns from the match() function. Error returns are externally
81  defined PCRE_ERROR_xxx codes, which are all negative. */  defined PCRE_ERROR_xxx codes, which are all negative. */
# Line 68  defined PCRE_ERROR_xxx codes, which are Line 83  defined PCRE_ERROR_xxx codes, which are
83  #define MATCH_MATCH        1  #define MATCH_MATCH        1
84  #define MATCH_NOMATCH      0  #define MATCH_NOMATCH      0
85    
86    /* Special internal returns from the match() function. Make them sufficiently
87    negative to avoid the external error codes. */
88    
89    #define MATCH_ACCEPT       (-999)
90    #define MATCH_KETRPOS      (-998)
91    #define MATCH_ONCE         (-997)
92    /* The next 5 must be kept together and in sequence so that a test that checks
93    for any one of them can use a range. */
94    #define MATCH_COMMIT       (-996)
95    #define MATCH_PRUNE        (-995)
96    #define MATCH_SKIP         (-994)
97    #define MATCH_SKIP_ARG     (-993)
98    #define MATCH_THEN         (-992)
99    #define MATCH_BACKTRACK_MAX MATCH_THEN
100    #define MATCH_BACKTRACK_MIN MATCH_COMMIT
101    
102  /* Maximum number of ints of offset to save on the stack for recursive calls.  /* Maximum number of ints of offset to save on the stack for recursive calls.
103  If the offset vector is bigger, malloc is used. This should be a multiple of 3,  If the offset vector is bigger, malloc is used. This should be a multiple of 3,
104  because the offset vector is always a multiple of 3 long. */  because the offset vector is always a multiple of 3 long. */
# Line 76  because the offset vector is always a mu Line 107  because the offset vector is always a mu
107    
108  /* Min and max values for the common repeats; for the maxima, 0 => infinity */  /* Min and max values for the common repeats; for the maxima, 0 => infinity */
109    
110  static const char rep_min[] = { 0, 0, 1, 1, 0, 0 };  static const char rep_min[] = { 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, };
111  static const char rep_max[] = { 0, 0, 0, 0, 1, 1 };  static const char rep_max[] = { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, };
   
112    
113    #ifdef PCRE_DEBUG
 #ifdef DEBUG  
114  /*************************************************  /*************************************************
115  *        Debugging function to print chars       *  *        Debugging function to print chars       *
116  *************************************************/  *************************************************/
# Line 99  Returns:     nothing Line 128  Returns:     nothing
128  */  */
129    
130  static void  static void
131  pchars(const uschar *p, int length, BOOL is_subject, match_data *md)  pchars(const pcre_uchar *p, int length, BOOL is_subject, match_data *md)
132  {  {
133  int c;  pcre_uint32 c;
134    BOOL utf = md->utf;
135  if (is_subject && length > md->end_subject - p) length = md->end_subject - p;  if (is_subject && length > md->end_subject - p) length = md->end_subject - p;
136  while (length-- > 0)  while (length-- > 0)
137    if (isprint(c = *(p++))) printf("%c", c); else printf("\\x%02x", c);    if (isprint(c = UCHAR21INCTEST(p))) printf("%c", (char)c); else printf("\\x{%02x}", c);
138  }  }
139  #endif  #endif
140    
# Line 114  while (length-- > 0) Line 144  while (length-- > 0)
144  *          Match a back-reference                *  *          Match a back-reference                *
145  *************************************************/  *************************************************/
146    
147  /* If a back reference hasn't been set, the length that is passed is greater  /* Normally, if a back reference hasn't been set, the length that is passed is
148  than the number of characters left in the string, so the match fails.  negative, so the match always fails. However, in JavaScript compatibility mode,
149    the length passed is zero. Note that in caseless UTF-8 mode, the number of
150    subject bytes matched may be different to the number of reference bytes.
151    
152  Arguments:  Arguments:
153    offset      index into the offset vector    offset      index into the offset vector
154    eptr        points into the subject    eptr        pointer into the subject
155    length      length to be matched    length      length of reference to be matched (number of bytes)
156    md          points to match data block    md          points to match data block
157    ims         the ims flags    caseless    TRUE if caseless
158    
159  Returns:      TRUE if matched  Returns:      >= 0 the number of subject bytes matched
160                  -1 no match
161                  -2 partial match; always given if at end subject
162  */  */
163    
164  static BOOL  static int
165  match_ref(int offset, register const uschar *eptr, int length, match_data *md,  match_ref(int offset, register PCRE_PUCHAR eptr, int length, match_data *md,
166    unsigned long int ims)    BOOL caseless)
167  {  {
168  const uschar *p = md->start_subject + md->offset_vector[offset];  PCRE_PUCHAR eptr_start = eptr;
169    register PCRE_PUCHAR p = md->start_subject + md->offset_vector[offset];
170    #if defined SUPPORT_UTF && defined SUPPORT_UCP
171    BOOL utf = md->utf;
172    #endif
173    
174  #ifdef DEBUG  #ifdef PCRE_DEBUG
175  if (eptr >= md->end_subject)  if (eptr >= md->end_subject)
176    printf("matching subject <null>");    printf("matching subject <null>");
177  else  else
# Line 146  pchars(p, length, FALSE, md); Line 184  pchars(p, length, FALSE, md);
184  printf("\n");  printf("\n");
185  #endif  #endif
186    
187  /* Always fail if not enough characters left */  /* Always fail if reference not set (and not JavaScript compatible - in that
188    case the length is passed as zero). */
189    
190  if (length > md->end_subject - eptr) return FALSE;  if (length < 0) return -1;
191    
192  /* Separate the caselesss case for speed */  /* Separate the caseless case for speed. In UTF-8 mode we can only do this
193    properly if Unicode properties are supported. Otherwise, we can check only
194    ASCII characters. */
195    
196  if ((ims & PCRE_CASELESS) != 0)  if (caseless)
197    {    {
198    while (length-- > 0)  #if defined SUPPORT_UTF && defined SUPPORT_UCP
199      if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE;    if (utf)
200        {
201        /* Match characters up to the end of the reference. NOTE: the number of
202        data units matched may differ, because in UTF-8 there are some characters
203        whose upper and lower case versions code have different numbers of bytes.
204        For example, U+023A (2 bytes in UTF-8) is the upper case version of U+2C65
205        (3 bytes in UTF-8); a sequence of 3 of the former uses 6 bytes, as does a
206        sequence of two of the latter. It is important, therefore, to check the
207        length along the reference, not along the subject (earlier code did this
208        wrong). */
209    
210        PCRE_PUCHAR endptr = p + length;
211        while (p < endptr)
212          {
213          pcre_uint32 c, d;
214          const ucd_record *ur;
215          if (eptr >= md->end_subject) return -2;   /* Partial match */
216          GETCHARINC(c, eptr);
217          GETCHARINC(d, p);
218          ur = GET_UCD(d);
219          if (c != d && c != d + ur->other_case)
220            {
221            const pcre_uint32 *pp = PRIV(ucd_caseless_sets) + ur->caseset;
222            for (;;)
223              {
224              if (c < *pp) return -1;
225              if (c == *pp++) break;
226              }
227            }
228          }
229        }
230      else
231    #endif
232    
233      /* The same code works when not in UTF-8 mode and in UTF-8 mode when there
234      is no UCP support. */
235        {
236        while (length-- > 0)
237          {
238          pcre_uint32 cc, cp;
239          if (eptr >= md->end_subject) return -2;   /* Partial match */
240          cc = UCHAR21TEST(eptr);
241          cp = UCHAR21TEST(p);
242          if (TABLE_GET(cp, md->lcc, cp) != TABLE_GET(cc, md->lcc, cc)) return -1;
243          p++;
244          eptr++;
245          }
246        }
247    }    }
248    
249    /* In the caseful case, we can just compare the bytes, whether or not we
250    are in UTF-8 mode. */
251    
252  else  else
253    { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }    {
254      while (length-- > 0)
255        {
256        if (eptr >= md->end_subject) return -2;   /* Partial match */
257        if (UCHAR21INCTEST(p) != UCHAR21INCTEST(eptr)) return -1;
258        }
259      }
260    
261  return TRUE;  return (int)(eptr - eptr_start);
262  }  }
263    
264    
# Line 169  return TRUE; Line 267  return TRUE;
267  ****************************************************************************  ****************************************************************************
268                     RECURSION IN THE match() FUNCTION                     RECURSION IN THE match() FUNCTION
269    
270  The match() function is highly recursive. Some regular expressions can cause  The match() function is highly recursive, though not every recursive call
271  it to recurse thousands of times. I was writing for Unix, so I just let it  increases the recursive depth. Nevertheless, some regular expressions can cause
272  call itself recursively. This uses the stack for saving everything that has  it to recurse to a great depth. I was writing for Unix, so I just let it call
273  to be saved for a recursive call. On Unix, the stack can be large, and this  itself recursively. This uses the stack for saving everything that has to be
274  works fine.  saved for a recursive call. On Unix, the stack can be large, and this works
275    fine.
276  It turns out that on non-Unix systems there are problems with programs that  
277  use a lot of stack. (This despite the fact that every last chip has oodles  It turns out that on some non-Unix-like systems there are problems with
278  of memory these days, and techniques for extending the stack have been known  programs that use a lot of stack. (This despite the fact that every last chip
279  for decades.) So....  has oodles of memory these days, and techniques for extending the stack have
280    been known for decades.) So....
281    
282  There is a fudge, triggered by defining NO_RECURSE, which avoids recursive  There is a fudge, triggered by defining NO_RECURSE, which avoids recursive
283  calls by keeping local variables that need to be preserved in blocks of memory  calls by keeping local variables that need to be preserved in blocks of memory
284  obtained from malloc instead instead of on the stack. Macros are used to  obtained from malloc() instead instead of on the stack. Macros are used to
285  achieve this so that the actual code doesn't look very different to what it  achieve this so that the actual code doesn't look very different to what it
286  always used to.  always used to.
287    
288    The original heap-recursive code used longjmp(). However, it seems that this
289    can be very slow on some operating systems. Following a suggestion from Stan
290    Switzer, the use of longjmp() has been abolished, at the cost of having to
291    provide a unique number for each call to RMATCH. There is no way of generating
292    a sequence of numbers at compile time in C. I have given them names, to make
293    them stand out more clearly.
294    
295    Crude tests on x86 Linux show a small speedup of around 5-8%. However, on
296    FreeBSD, avoiding longjmp() more than halves the time taken to run the standard
297    tests. Furthermore, not using longjmp() means that local dynamic variables
298    don't have indeterminate values; this has meant that the frame size can be
299    reduced because the result can be "passed back" by straight setting of the
300    variable instead of being passed in the frame.
301  ****************************************************************************  ****************************************************************************
302  ***************************************************************************/  ***************************************************************************/
303    
304    /* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN
305    below must be updated in sync.  */
306    
307  /* These versions of the macros use the stack, as normal */  enum { RM1=1, RM2,  RM3,  RM4,  RM5,  RM6,  RM7,  RM8,  RM9,  RM10,
308           RM11,  RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,
309           RM21,  RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
310           RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
311           RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
312           RM51,  RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60,
313           RM61,  RM62, RM63, RM64, RM65, RM66, RM67 };
314    
315    /* These versions of the macros use the stack, as normal. There are debugging
316    versions and production versions. Note that the "rw" argument of RMATCH isn't
317    actually used in this definition. */
318    
319  #ifndef NO_RECURSE  #ifndef NO_RECURSE
320  #define REGISTER register  #define REGISTER register
321  #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg) rx = match(ra,rb,rc,rd,re,rf,rg)  
322    #ifdef PCRE_DEBUG
323    #define RMATCH(ra,rb,rc,rd,re,rw) \
324      { \
325      printf("match() called in line %d\n", __LINE__); \
326      rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1); \
327      printf("to line %d\n", __LINE__); \
328      }
329    #define RRETURN(ra) \
330      { \
331      printf("match() returned %d from line %d\n", ra, __LINE__); \
332      return ra; \
333      }
334    #else
335    #define RMATCH(ra,rb,rc,rd,re,rw) \
336      rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1)
337  #define RRETURN(ra) return ra  #define RRETURN(ra) return ra
338    #endif
339    
340  #else  #else
341    
342    
343  /* These versions of the macros manage a private stack on the heap. Note  /* These versions of the macros manage a private stack on the heap. Note that
344  that the rd argument of RMATCH isn't actually used. It's the md argument of  the "rd" argument of RMATCH isn't actually used in this definition. It's the md
345  match(), which never changes. */  argument of match(), which never changes. */
346    
347  #define REGISTER  #define REGISTER
348    
349  #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg)\  #define RMATCH(ra,rb,rc,rd,re,rw)\
350    {\    {\
351    heapframe *newframe = (pcre_stack_malloc)(sizeof(heapframe));\    heapframe *newframe = frame->Xnextframe;\
352    if (setjmp(frame->Xwhere) == 0)\    if (newframe == NULL)\
353      {\      {\
354      newframe->Xeptr = ra;\      newframe = (heapframe *)(PUBL(stack_malloc))(sizeof(heapframe));\
355      newframe->Xecode = rb;\      if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\
356      newframe->Xoffset_top = rc;\      newframe->Xnextframe = NULL;\
357      newframe->Xims = re;\      frame->Xnextframe = newframe;\
     newframe->Xeptrb = rf;\  
     newframe->Xflags = rg;\  
     newframe->Xprevframe = frame;\  
     frame = newframe;\  
     DPRINTF(("restarting from line %d\n", __LINE__));\  
     goto HEAP_RECURSE;\  
     }\  
   else\  
     {\  
     DPRINTF(("longjumped back to line %d\n", __LINE__));\  
     frame = md->thisframe;\  
     rx = frame->Xresult;\  
358      }\      }\
359      frame->Xwhere = rw;\
360      newframe->Xeptr = ra;\
361      newframe->Xecode = rb;\
362      newframe->Xmstart = mstart;\
363      newframe->Xoffset_top = rc;\
364      newframe->Xeptrb = re;\
365      newframe->Xrdepth = frame->Xrdepth + 1;\
366      newframe->Xprevframe = frame;\
367      frame = newframe;\
368      DPRINTF(("restarting from line %d\n", __LINE__));\
369      goto HEAP_RECURSE;\
370      L_##rw:\
371      DPRINTF(("jumped back to line %d\n", __LINE__));\
372    }    }
373    
374  #define RRETURN(ra)\  #define RRETURN(ra)\
375    {\    {\
376    heapframe *newframe = frame;\    heapframe *oldframe = frame;\
377    frame = newframe->Xprevframe;\    frame = oldframe->Xprevframe;\
   (pcre_stack_free)(newframe);\  
378    if (frame != NULL)\    if (frame != NULL)\
379      {\      {\
380      frame->Xresult = ra;\      rrc = ra;\
381      md->thisframe = frame;\      goto HEAP_RETURN;\
     longjmp(frame->Xwhere, 1);\  
382      }\      }\
383    return ra;\    return ra;\
384    }    }
# Line 247  match(), which never changes. */ Line 388  match(), which never changes. */
388    
389  typedef struct heapframe {  typedef struct heapframe {
390    struct heapframe *Xprevframe;    struct heapframe *Xprevframe;
391      struct heapframe *Xnextframe;
392    
393    /* Function arguments that may change */    /* Function arguments that may change */
394    
395    const uschar *Xeptr;    PCRE_PUCHAR Xeptr;
396    const uschar *Xecode;    const pcre_uchar *Xecode;
397      PCRE_PUCHAR Xmstart;
398    int Xoffset_top;    int Xoffset_top;
   long int Xims;  
399    eptrblock *Xeptrb;    eptrblock *Xeptrb;
400    int Xflags;    unsigned int Xrdepth;
401    
402    /* Function local variables */    /* Function local variables */
403    
404    const uschar *Xcallpat;    PCRE_PUCHAR Xcallpat;
405    const uschar *Xcharptr;  #ifdef SUPPORT_UTF
406    const uschar *Xdata;    PCRE_PUCHAR Xcharptr;
407    const uschar *Xnext;  #endif
408    const uschar *Xpp;    PCRE_PUCHAR Xdata;
409    const uschar *Xprev;    PCRE_PUCHAR Xnext;
410    const uschar *Xsaved_eptr;    PCRE_PUCHAR Xpp;
411      PCRE_PUCHAR Xprev;
412      PCRE_PUCHAR Xsaved_eptr;
413    
414    recursion_info Xnew_recursive;    recursion_info Xnew_recursive;
415    
416    BOOL Xcur_is_word;    BOOL Xcur_is_word;
417    BOOL Xcondition;    BOOL Xcondition;
   BOOL Xminimize;  
418    BOOL Xprev_is_word;    BOOL Xprev_is_word;
419    
   unsigned long int Xoriginal_ims;  
   
420  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
421    int Xprop_type;    int Xprop_type;
422      unsigned int Xprop_value;
423    int Xprop_fail_result;    int Xprop_fail_result;
424    int Xprop_category;    int Xoclength;
425    int Xprop_chartype;    pcre_uchar Xocchars[6];
   int Xprop_othercase;  
   int Xprop_test_against;  
   int *Xprop_test_variable;  
426  #endif  #endif
427    
428      int Xcodelink;
429    int Xctype;    int Xctype;
430    int Xfc;    unsigned int Xfc;
431    int Xfi;    int Xfi;
432    int Xlength;    int Xlength;
433    int Xmax;    int Xmax;
434    int Xmin;    int Xmin;
435    int Xnumber;    unsigned int Xnumber;
436    int Xoffset;    int Xoffset;
437    int Xop;    unsigned int Xop;
438    int Xsave_capture_last;    pcre_int32 Xsave_capture_last;
439    int Xsave_offset1, Xsave_offset2, Xsave_offset3;    int Xsave_offset1, Xsave_offset2, Xsave_offset3;
440    int Xstacksave[REC_STACK_SAVE_MAX];    int Xstacksave[REC_STACK_SAVE_MAX];
441    
442    eptrblock Xnewptrb;    eptrblock Xnewptrb;
443    
444    /* Place to pass back result, and where to jump back to */    /* Where to jump back to */
445    
446    int  Xresult;    int Xwhere;
   jmp_buf Xwhere;  
447    
448  } heapframe;  } heapframe;
449    
# Line 320  typedef struct heapframe { Line 459  typedef struct heapframe {
459  *         Match from current position            *  *         Match from current position            *
460  *************************************************/  *************************************************/
461    
462  /* On entry ecode points to the first opcode, and eptr to the first character  /* This function is called recursively in many circumstances. Whenever it
 in the subject string, while eptrb holds the value of eptr at the start of the  
 last bracketed group - used for breaking infinite loops matching zero-length  
 strings. This function is called recursively in many circumstances. Whenever it  
463  returns a negative (error) response, the outer incarnation must also return the  returns a negative (error) response, the outer incarnation must also return the
464  same response.  same response. */
465    
466    /* These macros pack up tests that are used for partial matching, and which
467    appear several times in the code. We set the "hit end" flag if the pointer is
468    at the end of the subject and also past the start of the subject (i.e.
469    something has been matched). For hard partial matching, we then return
470    immediately. The second one is used when we already know we are past the end of
471    the subject. */
472    
473    #define CHECK_PARTIAL()\
474      if (md->partial != 0 && eptr >= md->end_subject && \
475          eptr > md->start_used_ptr) \
476        { \
477        md->hitend = TRUE; \
478        if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
479        }
480    
481    #define SCHECK_PARTIAL()\
482      if (md->partial != 0 && eptr > md->start_used_ptr) \
483        { \
484        md->hitend = TRUE; \
485        if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
486        }
487    
488  Performance note: It might be tempting to extract commonly used fields from the  
489  md structure (e.g. utf8, end_subject) into individual variables to improve  /* Performance note: It might be tempting to extract commonly used fields from
490    the md structure (e.g. utf, end_subject) into individual variables to improve
491  performance. Tests using gcc on a SPARC disproved this; in the first case, it  performance. Tests using gcc on a SPARC disproved this; in the first case, it
492  made performance worse.  made performance worse.
493    
494  Arguments:  Arguments:
495     eptr        pointer in subject     eptr        pointer to current character in subject
496     ecode       position in code     ecode       pointer to current position in compiled code
497       mstart      pointer to the current match start position (can be modified
498                     by encountering \K)
499     offset_top  current top pointer     offset_top  current top pointer
500     md          pointer to "static" info for the match     md          pointer to "static" info for the match
    ims         current /i, /m, and /s options  
501     eptrb       pointer to chain of blocks containing eptr at start of     eptrb       pointer to chain of blocks containing eptr at start of
502                   brackets - for testing for empty matches                   brackets - for testing for empty matches
503     flags       can contain     rdepth      the recursion depth
                  match_condassert - this is an assertion condition  
                  match_isgroup - this is the start of a bracketed group  
504    
505  Returns:       MATCH_MATCH if matched            )  these values are >= 0  Returns:       MATCH_MATCH if matched            )  these values are >= 0
506                 MATCH_NOMATCH if failed to match  )                 MATCH_NOMATCH if failed to match  )
507                   a negative MATCH_xxx value for PRUNE, SKIP, etc
508                 a negative PCRE_ERROR_xxx value if aborted by an error condition                 a negative PCRE_ERROR_xxx value if aborted by an error condition
509                   (e.g. stopped by recursion limit)                   (e.g. stopped by repeated call or recursion limit)
510  */  */
511    
512  static int  static int
513  match(REGISTER const uschar *eptr, REGISTER const uschar *ecode,  match(REGISTER PCRE_PUCHAR eptr, REGISTER const pcre_uchar *ecode,
514    int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,    PCRE_PUCHAR mstart, int offset_top, match_data *md, eptrblock *eptrb,
515    int flags)    unsigned int rdepth)
516  {  {
517  /* These variables do not need to be preserved over recursion in this function,  /* These variables do not need to be preserved over recursion in this function,
518  so they can be ordinary variables in all cases. Mark them with "register"  so they can be ordinary variables in all cases. Mark some of them with
519  because they are used a lot in loops. */  "register" because they are used a lot in loops. */
520    
521  register int  rrc;    /* Returns from recursive calls */  register int  rrc;         /* Returns from recursive calls */
522  register int  i;      /* Used for loops not involving calls to RMATCH() */  register int  i;           /* Used for loops not involving calls to RMATCH() */
523  register int  c;      /* Character values not kept over RMATCH() calls */  register pcre_uint32 c;    /* Character values not kept over RMATCH() calls */
524  register BOOL utf8;   /* Local copy of UTF-8 flag for speed */  register BOOL utf;         /* Local copy of UTF flag for speed */
525    
526    BOOL minimize, possessive; /* Quantifier options */
527    BOOL caseless;
528    int condcode;
529    
530  /* When recursion is not being used, all "local" variables that have to be  /* When recursion is not being used, all "local" variables that have to be
531  preserved over calls to RMATCH() are part of a "frame" which is obtained from  preserved over calls to RMATCH() are part of a "frame". We set up the top-level
532  heap storage. Set up the top-level frame here; others are obtained from the  frame on the stack here; subsequent instantiations are obtained from the heap
533  heap whenever RMATCH() does a "recursion". See the macro definitions above. */  whenever RMATCH() does a "recursion". See the macro definitions above. Putting
534    the top-level on the stack rather than malloc-ing them all gives a performance
535    boost in many cases where there is not much "recursion". */
536    
537  #ifdef NO_RECURSE  #ifdef NO_RECURSE
538  heapframe *frame = (pcre_stack_malloc)(sizeof(heapframe));  heapframe *frame = (heapframe *)md->match_frames_base;
 frame->Xprevframe = NULL;            /* Marks the top level */  
539    
540  /* Copy in the original argument variables */  /* Copy in the original argument variables */
541    
542  frame->Xeptr = eptr;  frame->Xeptr = eptr;
543  frame->Xecode = ecode;  frame->Xecode = ecode;
544    frame->Xmstart = mstart;
545  frame->Xoffset_top = offset_top;  frame->Xoffset_top = offset_top;
 frame->Xims = ims;  
546  frame->Xeptrb = eptrb;  frame->Xeptrb = eptrb;
547  frame->Xflags = flags;  frame->Xrdepth = rdepth;
548    
549  /* This is where control jumps back to to effect "recursion" */  /* This is where control jumps back to to effect "recursion" */
550    
# Line 390  HEAP_RECURSE: Line 554  HEAP_RECURSE:
554    
555  #define eptr               frame->Xeptr  #define eptr               frame->Xeptr
556  #define ecode              frame->Xecode  #define ecode              frame->Xecode
557    #define mstart             frame->Xmstart
558  #define offset_top         frame->Xoffset_top  #define offset_top         frame->Xoffset_top
 #define ims                frame->Xims  
559  #define eptrb              frame->Xeptrb  #define eptrb              frame->Xeptrb
560  #define flags              frame->Xflags  #define rdepth             frame->Xrdepth
561    
562  /* Ditto for the local variables */  /* Ditto for the local variables */
563    
564  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
565  #define charptr            frame->Xcharptr  #define charptr            frame->Xcharptr
566  #endif  #endif
567  #define callpat            frame->Xcallpat  #define callpat            frame->Xcallpat
568    #define codelink           frame->Xcodelink
569  #define data               frame->Xdata  #define data               frame->Xdata
570  #define next               frame->Xnext  #define next               frame->Xnext
571  #define pp                 frame->Xpp  #define pp                 frame->Xpp
# Line 411  HEAP_RECURSE: Line 576  HEAP_RECURSE:
576    
577  #define cur_is_word        frame->Xcur_is_word  #define cur_is_word        frame->Xcur_is_word
578  #define condition          frame->Xcondition  #define condition          frame->Xcondition
 #define minimize           frame->Xminimize  
579  #define prev_is_word       frame->Xprev_is_word  #define prev_is_word       frame->Xprev_is_word
580    
 #define original_ims       frame->Xoriginal_ims  
   
581  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
582  #define prop_type          frame->Xprop_type  #define prop_type          frame->Xprop_type
583    #define prop_value         frame->Xprop_value
584  #define prop_fail_result   frame->Xprop_fail_result  #define prop_fail_result   frame->Xprop_fail_result
585  #define prop_category      frame->Xprop_category  #define oclength           frame->Xoclength
586  #define prop_chartype      frame->Xprop_chartype  #define occhars            frame->Xocchars
 #define prop_othercase     frame->Xprop_othercase  
 #define prop_test_against  frame->Xprop_test_against  
 #define prop_test_variable frame->Xprop_test_variable  
587  #endif  #endif
588    
589  #define ctype              frame->Xctype  #define ctype              frame->Xctype
# Line 447  HEAP_RECURSE: Line 607  HEAP_RECURSE:
607  get preserved during recursion in the normal way. In this environment, fi and  get preserved during recursion in the normal way. In this environment, fi and
608  i, and fc and c, can be the same variables. */  i, and fc and c, can be the same variables. */
609    
610  #else  #else         /* NO_RECURSE not defined */
611  #define fi i  #define fi i
612  #define fc c  #define fc c
613    
614    /* Many of the following variables are used only in small blocks of the code.
615    My normal style of coding would have declared them within each of those blocks.
616    However, in order to accommodate the version of this code that uses an external
617    "stack" implemented on the heap, it is easier to declare them all here, so the
618    declarations can be cut out in a block. The only declarations within blocks
619    below are for variables that do not have to be preserved over a recursive call
620    to RMATCH(). */
621    
622  #ifdef SUPPORT_UTF8                /* Many of these variables are used ony */  #ifdef SUPPORT_UTF
623  const uschar *charptr;             /* small blocks of the code. My normal  */  const pcre_uchar *charptr;
624  #endif                             /* style of coding would have declared  */  #endif
625  const uschar *callpat;             /* them within each of those blocks.    */  const pcre_uchar *callpat;
626  const uschar *data;                /* However, in order to accommodate the */  const pcre_uchar *data;
627  const uschar *next;                /* version of this code that uses an    */  const pcre_uchar *next;
628  const uschar *pp;                  /* external "stack" implemented on the  */  PCRE_PUCHAR       pp;
629  const uschar *prev;                /* heap, it is easier to declare them   */  const pcre_uchar *prev;
630  const uschar *saved_eptr;          /* all here, so the declarations can    */  PCRE_PUCHAR       saved_eptr;
631                                     /* be cut out in a block. The only      */  
632  recursion_info new_recursive;      /* declarations within blocks below are */  recursion_info new_recursive;
                                    /* for variables that do not have to    */  
 BOOL cur_is_word;                  /* be preserved over a recursive call   */  
 BOOL condition;                    /* to RMATCH().                         */  
 BOOL minimize;  
 BOOL prev_is_word;  
633    
634  unsigned long int original_ims;  BOOL cur_is_word;
635    BOOL condition;
636    BOOL prev_is_word;
637    
638  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
639  int prop_type;  int prop_type;
640    unsigned int prop_value;
641  int prop_fail_result;  int prop_fail_result;
642  int prop_category;  int oclength;
643  int prop_chartype;  pcre_uchar occhars[6];
 int prop_othercase;  
 int prop_test_against;  
 int *prop_test_variable;  
644  #endif  #endif
645    
646    int codelink;
647  int ctype;  int ctype;
648  int length;  int length;
649  int max;  int max;
650  int min;  int min;
651  int number;  unsigned int number;
652  int offset;  int offset;
653  int op;  unsigned int op;
654  int save_capture_last;  pcre_int32 save_capture_last;
655  int save_offset1, save_offset2, save_offset3;  int save_offset1, save_offset2, save_offset3;
656  int stacksave[REC_STACK_SAVE_MAX];  int stacksave[REC_STACK_SAVE_MAX];
657    
658  eptrblock newptrb;  eptrblock newptrb;
659  #endif  
660    /* There is a special fudge for calling match() in a way that causes it to
661    measure the size of its basic stack frame when the stack is being used for
662    recursion. The second argument (ecode) being NULL triggers this behaviour. It
663    cannot normally ever be NULL. The return is the negated value of the frame
664    size. */
665    
666    if (ecode == NULL)
667      {
668      if (rdepth == 0)
669        return match((PCRE_PUCHAR)&rdepth, NULL, NULL, 0, NULL, NULL, 1);
670      else
671        {
672        int len = (char *)&rdepth - (char *)eptr;
673        return (len > 0)? -len : len;
674        }
675      }
676    #endif     /* NO_RECURSE */
677    
678    /* To save space on the stack and in the heap frame, I have doubled up on some
679    of the local variables that are used only in localised parts of the code, but
680    still need to be preserved over recursive calls of match(). These macros define
681    the alternative names that are used. */
682    
683    #define allow_zero    cur_is_word
684    #define cbegroup      condition
685    #define code_offset   codelink
686    #define condassert    condition
687    #define matched_once  prev_is_word
688    #define foc           number
689    #define save_mark     data
690    
691  /* These statements are here to stop the compiler complaining about unitialized  /* These statements are here to stop the compiler complaining about unitialized
692  variables. */  variables. */
693    
694  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
695    prop_value = 0;
696  prop_fail_result = 0;  prop_fail_result = 0;
 prop_test_against = 0;  
 prop_test_variable = NULL;  
697  #endif  #endif
698    
 /* OK, now we can get on with the real code of the function. Recursion is  
 specified by the macros RMATCH and RRETURN. When NO_RECURSE is *not* defined,  
 these just turn into a recursive call to match() and a "return", respectively.  
 However, RMATCH isn't like a function call because it's quite a complicated  
 macro. It has to be used in one particular way. This shouldn't, however, impact  
 performance when true recursion is being used. */  
699    
700  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);  /* This label is used for tail recursion, which is used in a few cases even
701    when NO_RECURSE is not defined, in order to reduce the amount of stack that is
702    used. Thanks to Ian Taylor for noticing this possibility and sending the
703    original patch. */
704    
705    TAIL_RECURSE:
706    
707    /* OK, now we can get on with the real code of the function. Recursive calls
708    are specified by the macro RMATCH and RRETURN is used to return. When
709    NO_RECURSE is *not* defined, these just turn into a recursive call to match()
710    and a "return", respectively (possibly with some debugging if PCRE_DEBUG is
711    defined). However, RMATCH isn't like a function call because it's quite a
712    complicated macro. It has to be used in one particular way. This shouldn't,
713    however, impact performance when true recursion is being used. */
714    
715    #ifdef SUPPORT_UTF
716    utf = md->utf;       /* Local copy of the flag */
717    #else
718    utf = FALSE;
719    #endif
720    
721  original_ims = ims;    /* Save for resetting on ')' */  /* First check that we haven't called match() too many times, or that we
722  utf8 = md->utf8;       /* Local copy of the flag */  haven't exceeded the recursive call limit. */
723    
724  /* At the start of a bracketed group, add the current subject pointer to the  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
725  stack of such pointers, to be re-instated at the end of the group when we hit  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
726  the closing ket. When match() is called in other circumstances, we don't add to  
727  this stack. */  /* At the start of a group with an unlimited repeat that may match an empty
728    string, the variable md->match_function_type is set to MATCH_CBEGROUP. It is
729    done this way to save having to use another function argument, which would take
730    up space on the stack. See also MATCH_CONDASSERT below.
731    
732    When MATCH_CBEGROUP is set, add the current subject pointer to the chain of
733    such remembered pointers, to be checked when we hit the closing ket, in order
734    to break infinite loops that match no characters. When match() is called in
735    other circumstances, don't add to the chain. The MATCH_CBEGROUP feature must
736    NOT be used with tail recursion, because the memory block that is used is on
737    the stack, so a new one may be required for each match(). */
738    
739  if ((flags & match_isgroup) != 0)  if (md->match_function_type == MATCH_CBEGROUP)
740    {    {
   newptrb.epb_prev = eptrb;  
741    newptrb.epb_saved_eptr = eptr;    newptrb.epb_saved_eptr = eptr;
742      newptrb.epb_prev = eptrb;
743    eptrb = &newptrb;    eptrb = &newptrb;
744      md->match_function_type = 0;
745    }    }
746    
747  /* Now start processing the operations. */  /* Now start processing the opcodes. */
748    
749  for (;;)  for (;;)
750    {    {
751      minimize = possessive = FALSE;
752    op = *ecode;    op = *ecode;
   minimize = FALSE;  
753    
754    /* For partial matching, remember if we ever hit the end of the subject after    switch(op)
755    matching at least one subject character. */      {
756        case OP_MARK:
757        md->nomatch_mark = ecode + 2;
758        md->mark = NULL;    /* In case previously set by assertion */
759        RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
760          eptrb, RM55);
761        if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
762             md->mark == NULL) md->mark = ecode + 2;
763    
764        /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
765        argument, and we must check whether that argument matches this MARK's
766        argument. It is passed back in md->start_match_ptr (an overloading of that
767        variable). If it does match, we reset that variable to the current subject
768        position and return MATCH_SKIP. Otherwise, pass back the return code
769        unaltered. */
770    
771        else if (rrc == MATCH_SKIP_ARG &&
772            STRCMP_UC_UC_TEST(ecode + 2, md->start_match_ptr) == 0)
773          {
774          md->start_match_ptr = eptr;
775          RRETURN(MATCH_SKIP);
776          }
777        RRETURN(rrc);
778    
779    if (md->partial &&      case OP_FAIL:
780        eptr >= md->end_subject &&      RRETURN(MATCH_NOMATCH);
       eptr > md->start_match)  
     md->hitend = TRUE;  
   
   /* Opening capturing bracket. If there is space in the offset vector, save  
   the current subject position in the working slot at the top of the vector. We  
   mustn't change the current values of the data slot, because they may be set  
   from a previous iteration of this group, and be referred to by a reference  
   inside the group.  
   
   If the bracket fails to match, we need to restore this value and also the  
   values of the final offsets, in case they were set by a previous iteration of  
   the same bracket.  
   
   If there isn't enough space in the offset vector, treat this as if it were a  
   non-capturing bracket. Don't worry about setting the flag for the error case  
   here; that is handled in the code for KET. */  
781    
782    if (op > OP_BRA)      case OP_COMMIT:
783      {      RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
784      number = op - OP_BRA;        eptrb, RM52);
785        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
786        RRETURN(MATCH_COMMIT);
787    
788        case OP_PRUNE:
789        RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
790          eptrb, RM51);
791        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
792        RRETURN(MATCH_PRUNE);
793    
794        case OP_PRUNE_ARG:
795        md->nomatch_mark = ecode + 2;
796        md->mark = NULL;    /* In case previously set by assertion */
797        RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
798          eptrb, RM56);
799        if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
800             md->mark == NULL) md->mark = ecode + 2;
801        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
802        RRETURN(MATCH_PRUNE);
803    
804        case OP_SKIP:
805        RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
806          eptrb, RM53);
807        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
808        md->start_match_ptr = eptr;   /* Pass back current position */
809        RRETURN(MATCH_SKIP);
810    
811        /* Note that, for Perl compatibility, SKIP with an argument does NOT set
812        nomatch_mark. When a pattern match ends with a SKIP_ARG for which there was
813        not a matching mark, we have to re-run the match, ignoring the SKIP_ARG
814        that failed and any that precede it (either they also failed, or were not
815        triggered). To do this, we maintain a count of executed SKIP_ARGs. If a
816        SKIP_ARG gets to top level, the match is re-run with md->ignore_skip_arg
817        set to the count of the one that failed. */
818    
819        case OP_SKIP_ARG:
820        md->skip_arg_count++;
821        if (md->skip_arg_count <= md->ignore_skip_arg)
822          {
823          ecode += PRIV(OP_lengths)[*ecode] + ecode[1];
824          break;
825          }
826        RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
827          eptrb, RM57);
828        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
829    
830        /* Pass back the current skip name by overloading md->start_match_ptr and
831        returning the special MATCH_SKIP_ARG return code. This will either be
832        caught by a matching MARK, or get to the top, where it causes a rematch
833        with md->ignore_skip_arg set to the value of md->skip_arg_count. */
834    
835        md->start_match_ptr = ecode + 2;
836        RRETURN(MATCH_SKIP_ARG);
837    
838        /* For THEN (and THEN_ARG) we pass back the address of the opcode, so that
839        the branch in which it occurs can be determined. Overload the start of
840        match pointer to do this. */
841    
842        case OP_THEN:
843        RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
844          eptrb, RM54);
845        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
846        md->start_match_ptr = ecode;
847        RRETURN(MATCH_THEN);
848    
849        case OP_THEN_ARG:
850        md->nomatch_mark = ecode + 2;
851        md->mark = NULL;    /* In case previously set by assertion */
852        RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top,
853          md, eptrb, RM58);
854        if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
855             md->mark == NULL) md->mark = ecode + 2;
856        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
857        md->start_match_ptr = ecode;
858        RRETURN(MATCH_THEN);
859    
860        /* Handle an atomic group that does not contain any capturing parentheses.
861        This can be handled like an assertion. Prior to 8.13, all atomic groups
862        were handled this way. In 8.13, the code was changed as below for ONCE, so
863        that backups pass through the group and thereby reset captured values.
864        However, this uses a lot more stack, so in 8.20, atomic groups that do not
865        contain any captures generate OP_ONCE_NC, which can be handled in the old,
866        less stack intensive way.
867    
868        Check the alternative branches in turn - the matching won't pass the KET
869        for this kind of subpattern. If any one branch matches, we carry on as at
870        the end of a normal bracket, leaving the subject pointer, but resetting
871        the start-of-match value in case it was changed by \K. */
872    
873        case OP_ONCE_NC:
874        prev = ecode;
875        saved_eptr = eptr;
876        save_mark = md->mark;
877        do
878          {
879          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM64);
880          if (rrc == MATCH_MATCH)  /* Note: _not_ MATCH_ACCEPT */
881            {
882            mstart = md->start_match_ptr;
883            break;
884            }
885          if (rrc == MATCH_THEN)
886            {
887            next = ecode + GET(ecode,1);
888            if (md->start_match_ptr < next &&
889                (*ecode == OP_ALT || *next == OP_ALT))
890              rrc = MATCH_NOMATCH;
891            }
892    
893          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
894          ecode += GET(ecode,1);
895          md->mark = save_mark;
896          }
897        while (*ecode == OP_ALT);
898    
899        /* If hit the end of the group (which could be repeated), fail */
900    
901        if (*ecode != OP_ONCE_NC && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
902    
903        /* Continue as from after the group, updating the offsets high water
904        mark, since extracts may have been taken. */
905    
906        do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
907    
908        offset_top = md->end_offset_top;
909        eptr = md->end_match_ptr;
910    
911        /* For a non-repeating ket, just continue at this level. This also
912        happens for a repeating ket if no characters were matched in the group.
913        This is the forcible breaking of infinite loops as implemented in Perl
914        5.005. */
915    
916      /* For extended extraction brackets (large number), we have to fish out the      if (*ecode == OP_KET || eptr == saved_eptr)
917      number from a dummy opcode at the start. */        {
918          ecode += 1+LINK_SIZE;
919          break;
920          }
921    
922        /* The repeating kets try the rest of the pattern or restart from the
923        preceding bracket, in the appropriate order. The second "call" of match()
924        uses tail recursion, to avoid using another stack frame. */
925    
926        if (*ecode == OP_KETRMIN)
927          {
928          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM65);
929          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
930          ecode = prev;
931          goto TAIL_RECURSE;
932          }
933        else  /* OP_KETRMAX */
934          {
935          RMATCH(eptr, prev, offset_top, md, eptrb, RM66);
936          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
937          ecode += 1 + LINK_SIZE;
938          goto TAIL_RECURSE;
939          }
940        /* Control never gets here */
941    
942      if (number > EXTRACT_BASIC_MAX)      /* Handle a capturing bracket, other than those that are possessive with an
943        number = GET2(ecode, 2+LINK_SIZE);      unlimited repeat. If there is space in the offset vector, save the current
944        subject position in the working slot at the top of the vector. We mustn't
945        change the current values of the data slot, because they may be set from a
946        previous iteration of this group, and be referred to by a reference inside
947        the group. A failure to match might occur after the group has succeeded,
948        if something later on doesn't match. For this reason, we need to restore
949        the working value and also the values of the final offsets, in case they
950        were set by a previous iteration of the same bracket.
951    
952        If there isn't enough space in the offset vector, treat this as if it were
953        a non-capturing bracket. Don't worry about setting the flag for the error
954        case here; that is handled in the code for KET. */
955    
956        case OP_CBRA:
957        case OP_SCBRA:
958        number = GET2(ecode, 1+LINK_SIZE);
959      offset = number << 1;      offset = number << 1;
960    
961  #ifdef DEBUG  #ifdef PCRE_DEBUG
962      printf("start bracket %d subject=", number);      printf("start bracket %d\n", number);
963        printf("subject=");
964      pchars(eptr, 16, TRUE, md);      pchars(eptr, 16, TRUE, md);
965      printf("\n");      printf("\n");
966  #endif  #endif
# Line 580  for (;;) Line 971  for (;;)
971        save_offset2 = md->offset_vector[offset+1];        save_offset2 = md->offset_vector[offset+1];
972        save_offset3 = md->offset_vector[md->offset_end - number];        save_offset3 = md->offset_vector[md->offset_end - number];
973        save_capture_last = md->capture_last;        save_capture_last = md->capture_last;
974          save_mark = md->mark;
975    
976        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
977        md->offset_vector[md->offset_end - number] = eptr - md->start_subject;        md->offset_vector[md->offset_end - number] =
978            (int)(eptr - md->start_subject);
979    
980        do        for (;;)
981          {          {
982          RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,          if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
983            match_isgroup);          RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
984              eptrb, RM1);
985            if (rrc == MATCH_ONCE) break;  /* Backing up through an atomic group */
986    
987            /* If we backed up to a THEN, check whether it is within the current
988            branch by comparing the address of the THEN that is passed back with
989            the end of the branch. If it is within the current branch, and the
990            branch is one of two or more alternatives (it either starts or ends
991            with OP_ALT), we have reached the limit of THEN's action, so convert
992            the return code to NOMATCH, which will cause normal backtracking to
993            happen from now on. Otherwise, THEN is passed back to an outer
994            alternative. This implements Perl's treatment of parenthesized groups,
995            where a group not containing | does not affect the current alternative,
996            that is, (X) is NOT the same as (X|(*F)). */
997    
998            if (rrc == MATCH_THEN)
999              {
1000              next = ecode + GET(ecode,1);
1001              if (md->start_match_ptr < next &&
1002                  (*ecode == OP_ALT || *next == OP_ALT))
1003                rrc = MATCH_NOMATCH;
1004              }
1005    
1006            /* Anything other than NOMATCH is passed back. */
1007    
1008          if (rrc != MATCH_NOMATCH) RRETURN(rrc);          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1009          md->capture_last = save_capture_last;          md->capture_last = save_capture_last;
1010          ecode += GET(ecode, 1);          ecode += GET(ecode, 1);
1011            md->mark = save_mark;
1012            if (*ecode != OP_ALT) break;
1013          }          }
       while (*ecode == OP_ALT);  
1014    
1015        DPRINTF(("bracket %d failed\n", number));        DPRINTF(("bracket %d failed\n", number));
   
1016        md->offset_vector[offset] = save_offset1;        md->offset_vector[offset] = save_offset1;
1017        md->offset_vector[offset+1] = save_offset2;        md->offset_vector[offset+1] = save_offset2;
1018        md->offset_vector[md->offset_end - number] = save_offset3;        md->offset_vector[md->offset_end - number] = save_offset3;
1019    
1020        RRETURN(MATCH_NOMATCH);        /* At this point, rrc will be one of MATCH_ONCE or MATCH_NOMATCH. */
1021    
1022          RRETURN(rrc);
1023        }        }
1024    
1025      /* Insufficient room for saving captured contents */      /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
1026        as a non-capturing bracket. */
1027    
1028      else op = OP_BRA;      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1029      }      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1030    
1031    /* Other types of node can be handled by a switch */      DPRINTF(("insufficient capture room: treat as non-capturing\n"));
1032    
1033    switch(op)      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1034      {      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1035      case OP_BRA:     /* Non-capturing bracket: optimized */  
1036      DPRINTF(("start bracket 0\n"));      /* Non-capturing or atomic group, except for possessive with unlimited
1037      do      repeat and ONCE group with no captures. Loop for all the alternatives.
1038        {  
1039        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,      When we get to the final alternative within the brackets, we used to return
1040          match_isgroup);      the result of a recursive call to match() whatever happened so it was
1041        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      possible to reduce stack usage by turning this into a tail recursion,
1042        ecode += GET(ecode, 1);      except in the case of a possibly empty group. However, now that there is
1043        }      the possiblity of (*THEN) occurring in the final alternative, this
1044      while (*ecode == OP_ALT);      optimization is no longer always possible.
1045      DPRINTF(("bracket 0 failed\n"));  
1046      RRETURN(MATCH_NOMATCH);      We can optimize if we know there are no (*THEN)s in the pattern; at present
1047        this is the best that can be done.
1048    
1049        MATCH_ONCE is returned when the end of an atomic group is successfully
1050        reached, but subsequent matching fails. It passes back up the tree (causing
1051        captured values to be reset) until the original atomic group level is
1052        reached. This is tested by comparing md->once_target with the start of the
1053        group. At this point, the return is converted into MATCH_NOMATCH so that
1054        previous backup points can be taken. */
1055    
1056      /* Conditional group: compilation checked that there are no more than      case OP_ONCE:
1057      two branches. If the condition is false, skipping the first branch takes us      case OP_BRA:
1058      past the end if there is only one branch, but that's OK because that is      case OP_SBRA:
1059      exactly what going to the ket would do. */      DPRINTF(("start non-capturing bracket\n"));
1060    
1061      case OP_COND:      for (;;)
     if (ecode[LINK_SIZE+1] == OP_CREF) /* Condition extract or recurse test */  
1062        {        {
1063        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */        if (op >= OP_SBRA || op == OP_ONCE)
1064        condition = (offset == CREF_RECURSE * 2)?          md->match_function_type = MATCH_CBEGROUP;
         (md->recursive != NULL) :  
         (offset < offset_top && md->offset_vector[offset] >= 0);  
       RMATCH(rrc, eptr, ecode + (condition?  
         (LINK_SIZE + 4) : (LINK_SIZE + 1 + GET(ecode, 1))),  
         offset_top, md, ims, eptrb, match_isgroup);  
       RRETURN(rrc);  
       }  
1065    
1066      /* The condition is an assertion. Call match() to evaluate it - setting        /* If this is not a possibly empty group, and there are no (*THEN)s in
1067      the final argument TRUE causes it to stop at the end of an assertion. */        the pattern, and this is the final alternative, optimize as described
1068          above. */
1069    
1070      else        else if (!md->hasthen && ecode[GET(ecode, 1)] != OP_ALT)
       {  
       RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,  
           match_condassert | match_isgroup);  
       if (rrc == MATCH_MATCH)  
         {  
         ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE+2);  
         while (*ecode == OP_ALT) ecode += GET(ecode, 1);  
         }  
       else if (rrc != MATCH_NOMATCH)  
1071          {          {
1072          RRETURN(rrc);         /* Need braces because of following else */          ecode += PRIV(OP_lengths)[*ecode];
1073            goto TAIL_RECURSE;
1074          }          }
       else ecode += GET(ecode, 1);  
       RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,  
         match_isgroup);  
       RRETURN(rrc);  
       }  
     /* Control never reaches here */  
   
     /* Skip over conditional reference or large extraction number data if  
     encountered. */  
   
     case OP_CREF:  
     case OP_BRANUMBER:  
     ecode += 3;  
     break;  
   
     /* End of the pattern. If we are in a recursion, we should restore the  
     offsets appropriately and continue from after the call. */  
1075    
1076      case OP_END:        /* In all other cases, we have to make another call to match(). */
     if (md->recursive != NULL && md->recursive->group_num == 0)  
       {  
       recursion_info *rec = md->recursive;  
       DPRINTF(("Hit the end in a (?0) recursion\n"));  
       md->recursive = rec->prevrec;  
       memmove(md->offset_vector, rec->offset_save,  
         rec->saved_max * sizeof(int));  
       md->start_match = rec->save_start;  
       ims = original_ims;  
       ecode = rec->after_call;  
       break;  
       }  
1077    
1078      /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty        save_mark = md->mark;
1079      string - backtracking will then try other alternatives, if any. */        save_capture_last = md->capture_last;
1080          RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, eptrb,
1081            RM2);
1082    
1083      if (md->notempty && eptr == md->start_match) RRETURN(MATCH_NOMATCH);        /* See comment in the code for capturing groups above about handling
1084      md->end_match_ptr = eptr;          /* Record where we ended */        THEN. */
     md->end_offset_top = offset_top;   /* and how many extracts were taken */  
     RRETURN(MATCH_MATCH);  
   
     /* Change option settings */  
   
     case OP_OPT:  
     ims = ecode[1];  
     ecode += 2;  
     DPRINTF(("ims set to %02lx\n", ims));  
     break;  
1085    
1086      /* Assertion brackets. Check the alternative branches in turn - the        if (rrc == MATCH_THEN)
1087      matching won't pass the KET for an assertion. If any one branch matches,          {
1088      the assertion is true. Lookbehind assertions have an OP_REVERSE item at the          next = ecode + GET(ecode,1);
1089      start of each branch to move the current point backwards, so the code at          if (md->start_match_ptr < next &&
1090      this level is identical to the lookahead case. */              (*ecode == OP_ALT || *next == OP_ALT))
1091              rrc = MATCH_NOMATCH;
1092            }
1093    
1094      case OP_ASSERT:        if (rrc != MATCH_NOMATCH)
1095      case OP_ASSERTBACK:          {
1096      do          if (rrc == MATCH_ONCE)
1097        {            {
1098        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,            const pcre_uchar *scode = ecode;
1099          match_isgroup);            if (*scode != OP_ONCE)           /* If not at start, find it */
1100        if (rrc == MATCH_MATCH) break;              {
1101        if (rrc != MATCH_NOMATCH) RRETURN(rrc);              while (*scode == OP_ALT) scode += GET(scode, 1);
1102                scode -= GET(scode, 1);
1103                }
1104              if (md->once_target == scode) rrc = MATCH_NOMATCH;
1105              }
1106            RRETURN(rrc);
1107            }
1108        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1109          md->mark = save_mark;
1110          if (*ecode != OP_ALT) break;
1111          md->capture_last = save_capture_last;
1112        }        }
     while (*ecode == OP_ALT);  
     if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);  
   
     /* If checking an assertion for a condition, return MATCH_MATCH. */  
1113    
1114      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      RRETURN(MATCH_NOMATCH);
1115    
1116      /* Continue from after the assertion, updating the offsets high water      /* Handle possessive capturing brackets with an unlimited repeat. We come
1117      mark, since extracts may have been taken during the assertion. */      here from BRAZERO with allow_zero set TRUE. The offset_vector values are
1118        handled similarly to the normal case above. However, the matching is
1119        different. The end of these brackets will always be OP_KETRPOS, which
1120        returns MATCH_KETRPOS without going further in the pattern. By this means
1121        we can handle the group by iteration rather than recursion, thereby
1122        reducing the amount of stack needed. */
1123    
1124        case OP_CBRAPOS:
1125        case OP_SCBRAPOS:
1126        allow_zero = FALSE;
1127    
1128      do ecode += GET(ecode,1); while (*ecode == OP_ALT);      POSSESSIVE_CAPTURE:
1129      ecode += 1 + LINK_SIZE;      number = GET2(ecode, 1+LINK_SIZE);
1130      offset_top = md->end_offset_top;      offset = number << 1;
     continue;  
1131    
1132      /* Negative assertion: all branches must fail to match */  #ifdef PCRE_DEBUG
1133        printf("start possessive bracket %d\n", number);
1134        printf("subject=");
1135        pchars(eptr, 16, TRUE, md);
1136        printf("\n");
1137    #endif
1138    
1139      case OP_ASSERT_NOT:      if (offset < md->offset_max)
     case OP_ASSERTBACK_NOT:  
     do  
1140        {        {
1141        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        matched_once = FALSE;
1142          match_isgroup);        code_offset = (int)(ecode - md->start_code);
       if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);  
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       ecode += GET(ecode,1);  
       }  
     while (*ecode == OP_ALT);  
1143    
1144      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);        save_offset1 = md->offset_vector[offset];
1145          save_offset2 = md->offset_vector[offset+1];
1146          save_offset3 = md->offset_vector[md->offset_end - number];
1147          save_capture_last = md->capture_last;
1148    
1149      ecode += 1 + LINK_SIZE;        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
     continue;  
1150    
1151      /* Move the subject pointer back. This occurs only at the start of        /* Each time round the loop, save the current subject position for use
1152      each branch of a lookbehind assertion. If we are too close to the start to        when the group matches. For MATCH_MATCH, the group has matched, so we
1153      move back, this match function fails. When working with UTF-8 we move        restart it with a new subject starting position, remembering that we had
1154      back a number of characters, not bytes. */        at least one match. For MATCH_NOMATCH, carry on with the alternatives, as
1155          usual. If we haven't matched any alternatives in any iteration, check to
1156          see if a previous iteration matched. If so, the group has matched;
1157          continue from afterwards. Otherwise it has failed; restore the previous
1158          capture values before returning NOMATCH. */
1159    
1160          for (;;)
1161            {
1162            md->offset_vector[md->offset_end - number] =
1163              (int)(eptr - md->start_subject);
1164            if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1165            RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
1166              eptrb, RM63);
1167            if (rrc == MATCH_KETRPOS)
1168              {
1169              offset_top = md->end_offset_top;
1170              eptr = md->end_match_ptr;
1171              ecode = md->start_code + code_offset;
1172              save_capture_last = md->capture_last;
1173              matched_once = TRUE;
1174              mstart = md->start_match_ptr;    /* In case \K changed it */
1175              continue;
1176              }
1177    
1178            /* See comment in the code for capturing groups above about handling
1179            THEN. */
1180    
1181            if (rrc == MATCH_THEN)
1182              {
1183              next = ecode + GET(ecode,1);
1184              if (md->start_match_ptr < next &&
1185                  (*ecode == OP_ALT || *next == OP_ALT))
1186                rrc = MATCH_NOMATCH;
1187              }
1188    
1189      case OP_REVERSE:          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1190  #ifdef SUPPORT_UTF8          md->capture_last = save_capture_last;
1191      if (utf8)          ecode += GET(ecode, 1);
1192        {          if (*ecode != OP_ALT) break;
1193        c = GET(ecode,1);          }
1194        for (i = 0; i < c; i++)  
1195          if (!matched_once)
1196          {          {
1197          eptr--;          md->offset_vector[offset] = save_offset1;
1198          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);          md->offset_vector[offset+1] = save_offset2;
1199          BACKCHAR(eptr)          md->offset_vector[md->offset_end - number] = save_offset3;
1200          }          }
       }  
     else  
 #endif  
1201    
1202      /* No UTF-8 support, or not in UTF-8 mode: count is byte count */        if (allow_zero || matched_once)
1203            {
1204            ecode += 1 + LINK_SIZE;
1205            break;
1206            }
1207    
1208        {        RRETURN(MATCH_NOMATCH);
       eptr -= GET(ecode,1);  
       if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);  
1209        }        }
1210    
1211      /* Skip to next op code */      /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
1212        as a non-capturing bracket. */
1213    
1214      ecode += 1 + LINK_SIZE;      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1215      break;      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1216    
1217      /* The callout item calls an external function, if one is provided, passing      DPRINTF(("insufficient capture room: treat as non-capturing\n"));
     details of the match so far. This is mainly for debugging, though the  
     function is able to force a failure. */  
1218    
1219      case OP_CALLOUT:      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1220      if (pcre_callout != NULL)      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
       {  
       pcre_callout_block cb;  
       cb.version          = 1;   /* Version 1 of the callout block */  
       cb.callout_number   = ecode[1];  
       cb.offset_vector    = md->offset_vector;  
       cb.subject          = (const char *)md->start_subject;  
       cb.subject_length   = md->end_subject - md->start_subject;  
       cb.start_match      = md->start_match - md->start_subject;  
       cb.current_position = eptr - md->start_subject;  
       cb.pattern_position = GET(ecode, 2);  
       cb.next_item_length = GET(ecode, 2 + LINK_SIZE);  
       cb.capture_top      = offset_top/2;  
       cb.capture_last     = md->capture_last;  
       cb.callout_data     = md->callout_data;  
       if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);  
       if (rrc < 0) RRETURN(rrc);  
       }  
     ecode += 2 + 2*LINK_SIZE;  
     break;  
1221    
1222      /* Recursion either matches the current regex, or some subexpression. The      /* Non-capturing possessive bracket with unlimited repeat. We come here
1223      offset data is the offset to the starting bracket from the start of the      from BRAZERO with allow_zero = TRUE. The code is similar to the above,
1224      whole pattern. (This is so that it works from duplicated subpatterns.)      without the capturing complication. It is written out separately for speed
1225        and cleanliness. */
1226    
1227      If there are any capturing brackets started but not finished, we have to      case OP_BRAPOS:
1228      save their starting points and reinstate them after the recursion. However,      case OP_SBRAPOS:
1229      we don't know how many such there are (offset_top records the completed      allow_zero = FALSE;
     total) so we just have to save all the potential data. There may be up to  
     65535 such values, which is too large to put on the stack, but using malloc  
     for small numbers seems expensive. As a compromise, the stack is used when  
     there are no more than REC_STACK_SAVE_MAX values to store; otherwise malloc  
     is used. A problem is what to do if the malloc fails ... there is no way of  
     returning to the top level with an error. Save the top REC_STACK_SAVE_MAX  
     values on the stack, and accept that the rest may be wrong.  
1230    
1231      There are also other values that have to be saved. We use a chained      POSSESSIVE_NON_CAPTURE:
1232      sequence of blocks that actually live on the stack. Thanks to Robin Houston      matched_once = FALSE;
1233      for the original version of this logic. */      code_offset = (int)(ecode - md->start_code);
1234        save_capture_last = md->capture_last;
1235    
1236      case OP_RECURSE:      for (;;)
1237        {        {
1238        callpat = md->start_code + GET(ecode, 1);        if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1239        new_recursive.group_num = *callpat - OP_BRA;        RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
1240            eptrb, RM48);
1241          if (rrc == MATCH_KETRPOS)
1242            {
1243            offset_top = md->end_offset_top;
1244            eptr = md->end_match_ptr;
1245            ecode = md->start_code + code_offset;
1246            matched_once = TRUE;
1247            mstart = md->start_match_ptr;   /* In case \K reset it */
1248            continue;
1249            }
1250    
1251        /* For extended extraction brackets (large number), we have to fish out        /* See comment in the code for capturing groups above about handling
1252        the number from a dummy opcode at the start. */        THEN. */
1253    
1254        if (new_recursive.group_num > EXTRACT_BASIC_MAX)        if (rrc == MATCH_THEN)
1255          new_recursive.group_num = GET2(callpat, 2+LINK_SIZE);          {
1256            next = ecode + GET(ecode,1);
1257            if (md->start_match_ptr < next &&
1258                (*ecode == OP_ALT || *next == OP_ALT))
1259              rrc = MATCH_NOMATCH;
1260            }
1261    
1262        /* Add to "recursing stack" */        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1263          ecode += GET(ecode, 1);
1264          if (*ecode != OP_ALT) break;
1265          md->capture_last = save_capture_last;
1266          }
1267    
1268        new_recursive.prevrec = md->recursive;      if (matched_once || allow_zero)
1269        md->recursive = &new_recursive;        {
1270          ecode += 1 + LINK_SIZE;
1271          break;
1272          }
1273        RRETURN(MATCH_NOMATCH);
1274    
1275        /* Find where to continue from afterwards */      /* Control never reaches here. */
1276    
1277        ecode += 1 + LINK_SIZE;      /* Conditional group: compilation checked that there are no more than two
1278        new_recursive.after_call = ecode;      branches. If the condition is false, skipping the first branch takes us
1279        past the end of the item if there is only one branch, but that's exactly
1280        what we want. */
1281    
1282        /* Now save the offset data. */      case OP_COND:
1283        case OP_SCOND:
1284    
1285        new_recursive.saved_max = md->offset_end;      /* The variable codelink will be added to ecode when the condition is
1286        if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)      false, to get to the second branch. Setting it to the offset to the ALT
1287          new_recursive.offset_save = stacksave;      or KET, then incrementing ecode achieves this effect. We now have ecode
1288        else      pointing to the condition or callout. */
1289    
1290        codelink = GET(ecode, 1);   /* Offset to the second branch */
1291        ecode += 1 + LINK_SIZE;     /* From this opcode */
1292    
1293        /* Because of the way auto-callout works during compile, a callout item is
1294        inserted between OP_COND and an assertion condition. */
1295    
1296        if (*ecode == OP_CALLOUT)
1297          {
1298          if (PUBL(callout) != NULL)
1299            {
1300            PUBL(callout_block) cb;
1301            cb.version          = 2;   /* Version 1 of the callout block */
1302            cb.callout_number   = ecode[1];
1303            cb.offset_vector    = md->offset_vector;
1304    #if defined COMPILE_PCRE8
1305            cb.subject          = (PCRE_SPTR)md->start_subject;
1306    #elif defined COMPILE_PCRE16
1307            cb.subject          = (PCRE_SPTR16)md->start_subject;
1308    #elif defined COMPILE_PCRE32
1309            cb.subject          = (PCRE_SPTR32)md->start_subject;
1310    #endif
1311            cb.subject_length   = (int)(md->end_subject - md->start_subject);
1312            cb.start_match      = (int)(mstart - md->start_subject);
1313            cb.current_position = (int)(eptr - md->start_subject);
1314            cb.pattern_position = GET(ecode, 2);
1315            cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
1316            cb.capture_top      = offset_top/2;
1317            cb.capture_last     = md->capture_last & CAPLMASK;
1318            /* Internal change requires this for API compatibility. */
1319            if (cb.capture_last == 0) cb.capture_last = -1;
1320            cb.callout_data     = md->callout_data;
1321            cb.mark             = md->nomatch_mark;
1322            if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH);
1323            if (rrc < 0) RRETURN(rrc);
1324            }
1325    
1326          /* Advance ecode past the callout, so it now points to the condition. We
1327          must adjust codelink so that the value of ecode+codelink is unchanged. */
1328    
1329          ecode += PRIV(OP_lengths)[OP_CALLOUT];
1330          codelink -= PRIV(OP_lengths)[OP_CALLOUT];
1331          }
1332    
1333        /* Test the various possible conditions */
1334    
1335        condition = FALSE;
1336        switch(condcode = *ecode)
1337          {
1338          case OP_RREF:         /* Numbered group recursion test */
1339          if (md->recursive != NULL)     /* Not recursing => FALSE */
1340          {          {
1341          new_recursive.offset_save =          unsigned int recno = GET2(ecode, 1);   /* Recursion group number*/
1342            (int *)(pcre_malloc)(new_recursive.saved_max * sizeof(int));          condition = (recno == RREF_ANY || recno == md->recursive->group_num);
         if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);  
1343          }          }
1344          break;
1345    
1346        memcpy(new_recursive.offset_save, md->offset_vector,        case OP_DNRREF:       /* Duplicate named group recursion test */
1347              new_recursive.saved_max * sizeof(int));        if (md->recursive != NULL)
       new_recursive.save_start = md->start_match;  
       md->start_match = eptr;  
   
       /* OK, now we can do the recursion. For each top-level alternative we  
       restore the offset and recursion data. */  
   
       DPRINTF(("Recursing into group %d\n", new_recursive.group_num));  
       do  
1348          {          {
1349          RMATCH(rrc, eptr, callpat + 1 + LINK_SIZE, offset_top, md, ims,          int count = GET2(ecode, 1 + IMM2_SIZE);
1350              eptrb, match_isgroup);          pcre_uchar *slot = md->name_table + GET2(ecode, 1) * md->name_entry_size;
1351          if (rrc == MATCH_MATCH)          while (count-- > 0)
1352            {            {
1353            md->recursive = new_recursive.prevrec;            unsigned int recno = GET2(slot, 0);
1354            if (new_recursive.offset_save != stacksave)            condition = recno == md->recursive->group_num;
1355              (pcre_free)(new_recursive.offset_save);            if (condition) break;
1356            RRETURN(MATCH_MATCH);            slot += md->name_entry_size;
1357            }            }
         else if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
   
         md->recursive = &new_recursive;  
         memcpy(md->offset_vector, new_recursive.offset_save,  
             new_recursive.saved_max * sizeof(int));  
         callpat += GET(callpat, 1);  
1358          }          }
1359        while (*callpat == OP_ALT);        break;
   
       DPRINTF(("Recursion didn't match\n"));  
       md->recursive = new_recursive.prevrec;  
       if (new_recursive.offset_save != stacksave)  
         (pcre_free)(new_recursive.offset_save);  
       RRETURN(MATCH_NOMATCH);  
       }  
     /* Control never reaches here */  
   
     /* "Once" brackets are like assertion brackets except that after a match,  
     the point in the subject string is not moved back. Thus there can never be  
     a move back into the brackets. Friedl calls these "atomic" subpatterns.  
     Check the alternative branches in turn - the matching won't pass the KET  
     for this kind of subpattern. If any one branch matches, we carry on as at  
     the end of a normal bracket, leaving the subject pointer. */  
1360    
1361      case OP_ONCE:        case OP_CREF:         /* Numbered group used test */
1362        {        offset = GET2(ecode, 1) << 1;  /* Doubled ref number */
1363        prev = ecode;        condition = offset < offset_top && md->offset_vector[offset] >= 0;
1364        saved_eptr = eptr;        break;
1365    
1366        do        case OP_DNCREF:      /* Duplicate named group used test */
1367          {          {
1368          RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,          int count = GET2(ecode, 1 + IMM2_SIZE);
1369            eptrb, match_isgroup);          pcre_uchar *slot = md->name_table + GET2(ecode, 1) * md->name_entry_size;
1370          if (rrc == MATCH_MATCH) break;          while (count-- > 0)
1371          if (rrc != MATCH_NOMATCH) RRETURN(rrc);            {
1372          ecode += GET(ecode,1);            offset = GET2(slot, 0) << 1;
1373              condition = offset < offset_top && md->offset_vector[offset] >= 0;
1374              if (condition) break;
1375              slot += md->name_entry_size;
1376              }
1377          }          }
1378        while (*ecode == OP_ALT);        break;
   
       /* If hit the end of the group (which could be repeated), fail */  
1379    
1380        if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);        case OP_DEF:     /* DEFINE - always false */
1381          break;
1382    
1383        /* Continue as from after the assertion, updating the offsets high water        /* The condition is an assertion. Call match() to evaluate it - setting
1384        mark, since extracts may have been taken. */        md->match_function_type to MATCH_CONDASSERT causes it to stop at the end
1385          of an assertion. */
1386    
1387          default:
1388          md->match_function_type = MATCH_CONDASSERT;
1389          RMATCH(eptr, ecode, offset_top, md, NULL, RM3);
1390          if (rrc == MATCH_MATCH)
1391            {
1392            if (md->end_offset_top > offset_top)
1393              offset_top = md->end_offset_top;  /* Captures may have happened */
1394            condition = TRUE;
1395    
1396        do ecode += GET(ecode,1); while (*ecode == OP_ALT);          /* Advance ecode past the assertion to the start of the first branch,
1397            but adjust it so that the general choosing code below works. */
1398    
1399        offset_top = md->end_offset_top;          ecode += GET(ecode, 1);
1400        eptr = md->end_match_ptr;          while (*ecode == OP_ALT) ecode += GET(ecode, 1);
1401            ecode += 1 + LINK_SIZE - PRIV(OP_lengths)[condcode];
1402            }
1403    
1404        /* For a non-repeating ket, just continue at this level. This also        /* PCRE doesn't allow the effect of (*THEN) to escape beyond an
1405        happens for a repeating ket if no characters were matched in the group.        assertion; it is therefore treated as NOMATCH. Any other return is an
1406        This is the forcible breaking of infinite loops as implemented in Perl        error. */
       5.005. If there is an options reset, it will get obeyed in the normal  
       course of events. */  
1407    
1408        if (*ecode == OP_KET || eptr == saved_eptr)        else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
1409          {          {
1410          ecode += 1+LINK_SIZE;          RRETURN(rrc);         /* Need braces because of following else */
         break;  
1411          }          }
1412          break;
1413          }
1414    
1415        /* The repeating kets try the rest of the pattern or restart from the      /* Choose branch according to the condition */
       preceding bracket, in the appropriate order. We need to reset any options  
       that changed within the bracket before re-running it, so check the next  
       opcode. */  
1416    
1417        if (ecode[1+LINK_SIZE] == OP_OPT)      ecode += condition? PRIV(OP_lengths)[condcode] : codelink;
         {  
         ims = (ims & ~PCRE_IMS) | ecode[4];  
         DPRINTF(("ims set to %02lx at group repeat\n", ims));  
         }  
1418    
1419        if (*ecode == OP_KETRMIN)      /* We are now at the branch that is to be obeyed. As there is only one, we
1420          {      can use tail recursion to avoid using another stack frame, except when
1421          RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0);      there is unlimited repeat of a possibly empty group. In the latter case, a
1422          if (rrc != MATCH_NOMATCH) RRETURN(rrc);      recursive call to match() is always required, unless the second alternative
1423          RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);      doesn't exist, in which case we can just plough on. Note that, for
1424          if (rrc != MATCH_NOMATCH) RRETURN(rrc);      compatibility with Perl, the | in a conditional group is NOT treated as
1425          }      creating two alternatives. If a THEN is encountered in the branch, it
1426        else  /* OP_KETRMAX */      propagates out to the enclosing alternative (unless nested in a deeper set
1427        of alternatives, of course). */
1428    
1429        if (condition || ecode[-(1+LINK_SIZE)] == OP_ALT)
1430          {
1431          if (op != OP_SCOND)
1432          {          {
1433          RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);          goto TAIL_RECURSE;
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
         RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);  
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
1434          }          }
       }  
     RRETURN(MATCH_NOMATCH);  
   
     /* An alternation is the end of a branch; scan along to find the end of the  
     bracketed group and go to there. */  
1435    
1436      case OP_ALT:        md->match_function_type = MATCH_CBEGROUP;
1437      do ecode += GET(ecode,1); while (*ecode == OP_ALT);        RMATCH(eptr, ecode, offset_top, md, eptrb, RM49);
1438      break;        RRETURN(rrc);
1439          }
1440    
1441      /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating       /* Condition false & no alternative; continue after the group. */
     that it may occur zero times. It may repeat infinitely, or not at all -  
     i.e. it could be ()* or ()? in the pattern. Brackets with fixed upper  
     repeat limits are compiled as a number of copies, with the optional ones  
     preceded by BRAZERO or BRAMINZERO. */  
1442    
1443      case OP_BRAZERO:      else
1444        {        {
       next = ecode+1;  
       RMATCH(rrc, eptr, next, offset_top, md, ims, eptrb, match_isgroup);  
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       do next += GET(next,1); while (*next == OP_ALT);  
       ecode = next + 1+LINK_SIZE;  
1445        }        }
1446      break;      break;
1447    
1448      case OP_BRAMINZERO:  
1449        /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
1450        to close any currently open capturing brackets. */
1451    
1452        case OP_CLOSE:
1453        number = GET2(ecode, 1);   /* Must be less than 65536 */
1454        offset = number << 1;
1455    
1456    #ifdef PCRE_DEBUG
1457          printf("end bracket %d at *ACCEPT", number);
1458          printf("\n");
1459    #endif
1460    
1461        md->capture_last = (md->capture_last & OVFLMASK) | number;
1462        if (offset >= md->offset_max) md->capture_last |= OVFLBIT; else
1463        {        {
1464        next = ecode+1;        md->offset_vector[offset] =
1465        do next += GET(next,1); while (*next == OP_ALT);          md->offset_vector[md->offset_end - number];
1466        RMATCH(rrc, eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb,        md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1467          match_isgroup);        if (offset_top <= offset) offset_top = offset + 2;
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       ecode++;  
1468        }        }
1469        ecode += 1 + IMM2_SIZE;
1470      break;      break;
1471    
     /* End of a group, repeated or non-repeating. If we are at the end of  
     an assertion "group", stop matching and return MATCH_MATCH, but record the  
     current high water mark for use by positive assertions. Do this also  
     for the "once" (not-backup up) groups. */  
1472    
1473      case OP_KET:      /* End of the pattern, either real or forced. */
     case OP_KETRMIN:  
     case OP_KETRMAX:  
       {  
       prev = ecode - GET(ecode, 1);  
       saved_eptr = eptrb->epb_saved_eptr;  
1474    
1475        /* Back up the stack of bracket start pointers. */      case OP_END:
1476        case OP_ACCEPT:
1477        case OP_ASSERT_ACCEPT:
1478    
1479        eptrb = eptrb->epb_prev;      /* If we have matched an empty string, fail if not in an assertion and not
1480        in a recursion if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART
1481        is set and we have matched at the start of the subject. In both cases,
1482        backtracking will then try other alternatives, if any. */
1483    
1484        if (eptr == mstart && op != OP_ASSERT_ACCEPT &&
1485             md->recursive == NULL &&
1486             (md->notempty ||
1487               (md->notempty_atstart &&
1488                 mstart == md->start_subject + md->start_offset)))
1489          RRETURN(MATCH_NOMATCH);
1490    
1491        if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||      /* Otherwise, we have a match. */
           *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||  
           *prev == OP_ONCE)  
         {  
         md->end_match_ptr = eptr;      /* For ONCE */  
         md->end_offset_top = offset_top;  
         RRETURN(MATCH_MATCH);  
         }  
1492    
1493        /* In all other cases except a conditional group we have to check the      md->end_match_ptr = eptr;           /* Record where we ended */
1494        group number back at the start and if necessary complete handling an      md->end_offset_top = offset_top;    /* and how many extracts were taken */
1495        extraction by setting the offsets and bumping the high water mark. */      md->start_match_ptr = mstart;       /* and the start (\K can modify) */
1496    
1497        if (*prev != OP_COND)      /* For some reason, the macros don't work properly if an expression is
1498          {      given as the argument to RRETURN when the heap is in use. */
         number = *prev - OP_BRA;  
1499    
1500          /* For extended extraction brackets (large number), we have to fish out      rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
1501          the number from a dummy opcode at the start. */      RRETURN(rrc);
1502    
1503          if (number > EXTRACT_BASIC_MAX) number = GET2(prev, 2+LINK_SIZE);      /* Assertion brackets. Check the alternative branches in turn - the
1504          offset = number << 1;      matching won't pass the KET for an assertion. If any one branch matches,
1505        the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
1506        start of each branch to move the current point backwards, so the code at
1507        this level is identical to the lookahead case. When the assertion is part
1508        of a condition, we want to return immediately afterwards. The caller of
1509        this incarnation of the match() function will have set MATCH_CONDASSERT in
1510        md->match_function type, and one of these opcodes will be the first opcode
1511        that is processed. We use a local variable that is preserved over calls to
1512        match() to remember this case. */
1513    
1514  #ifdef DEBUG      case OP_ASSERT:
1515          printf("end bracket %d", number);      case OP_ASSERTBACK:
1516          printf("\n");      save_mark = md->mark;
1517  #endif      if (md->match_function_type == MATCH_CONDASSERT)
1518          {
1519          condassert = TRUE;
1520          md->match_function_type = 0;
1521          }
1522        else condassert = FALSE;
1523    
1524          /* Test for a numbered group. This includes groups called as a result      /* Loop for each branch */
         of recursion. Note that whole-pattern recursion is coded as a recurse  
         into group 0, so it won't be picked up here. Instead, we catch it when  
         the OP_END is reached. */  
1525    
1526          if (number > 0)      do
1527            {        {
1528            md->capture_last = number;        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
           if (offset >= md->offset_max) md->offset_overflow = TRUE; else  
             {  
             md->offset_vector[offset] =  
               md->offset_vector[md->offset_end - number];  
             md->offset_vector[offset+1] = eptr - md->start_subject;  
             if (offset_top <= offset) offset_top = offset + 2;  
             }  
1529    
1530            /* Handle a recursively called group. Restore the offsets        /* A match means that the assertion is true; break out of the loop
1531            appropriately and continue from after the call. */        that matches its alternatives. */
1532    
1533            if (md->recursive != NULL && md->recursive->group_num == number)        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1534              {          {
1535              recursion_info *rec = md->recursive;          mstart = md->start_match_ptr;   /* In case \K reset it */
1536              DPRINTF(("Recursion (%d) succeeded - continuing\n", number));          break;
             md->recursive = rec->prevrec;  
             md->start_match = rec->save_start;  
             memcpy(md->offset_vector, rec->offset_save,  
               rec->saved_max * sizeof(int));  
             ecode = rec->after_call;  
             ims = original_ims;  
             break;  
             }  
           }  
1537          }          }
1538    
1539        /* Reset the value of the ims flags, in case they got changed during        /* If not matched, restore the previous mark setting. */
       the group. */  
1540    
1541        ims = original_ims;        md->mark = save_mark;
       DPRINTF(("ims reset to %02lx\n", ims));  
1542    
1543        /* For a non-repeating ket, just continue at this level. This also        /* See comment in the code for capturing groups above about handling
1544        happens for a repeating ket if no characters were matched in the group.        THEN. */
       This is the forcible breaking of infinite loops as implemented in Perl  
       5.005. If there is an options reset, it will get obeyed in the normal  
       course of events. */  
1545    
1546        if (*ecode == OP_KET || eptr == saved_eptr)        if (rrc == MATCH_THEN)
1547          {          {
1548          ecode += 1 + LINK_SIZE;          next = ecode + GET(ecode,1);
1549          break;          if (md->start_match_ptr < next &&
1550                (*ecode == OP_ALT || *next == OP_ALT))
1551              rrc = MATCH_NOMATCH;
1552          }          }
1553    
1554        /* The repeating kets try the rest of the pattern or restart from the        /* Anything other than NOMATCH causes the entire assertion to fail,
1555        preceding bracket, in the appropriate order. */        passing back the return code. This includes COMMIT, SKIP, PRUNE and an
1556          uncaptured THEN, which means they take their normal effect. This
1557          consistent approach does not always have exactly the same effect as in
1558          Perl. */
1559    
1560        if (*ecode == OP_KETRMIN)        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1561          {        ecode += GET(ecode, 1);
         RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);  
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
         RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);  
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
         }  
       else  /* OP_KETRMAX */  
         {  
         RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);  
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
         RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);  
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
         }  
1562        }        }
1563        while (*ecode == OP_ALT);   /* Continue for next alternative */
1564    
1565      RRETURN(MATCH_NOMATCH);      /* If we have tried all the alternative branches, the assertion has
1566        failed. If not, we broke out after a match. */
     /* Start of subject unless notbol, or after internal newline if multiline */  
1567    
1568      case OP_CIRC:      if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);
     if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);  
     if ((ims & PCRE_MULTILINE) != 0)  
       {  
       if (eptr != md->start_subject && eptr[-1] != NEWLINE)  
         RRETURN(MATCH_NOMATCH);  
       ecode++;  
       break;  
       }  
     /* ... else fall through */  
1569    
1570      /* Start of subject assertion */      /* If checking an assertion for a condition, return MATCH_MATCH. */
1571    
1572      case OP_SOD:      if (condassert) RRETURN(MATCH_MATCH);
     if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);  
     ecode++;  
     break;  
1573    
1574      /* Start of match assertion */      /* Continue from after a successful assertion, updating the offsets high
1575        water mark, since extracts may have been taken during the assertion. */
1576    
1577      case OP_SOM:      do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1578      if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);      ecode += 1 + LINK_SIZE;
1579      ecode++;      offset_top = md->end_offset_top;
1580      break;      continue;
1581    
1582      /* Assert before internal newline if multiline, or before a terminating      /* Negative assertion: all branches must fail to match for the assertion to
1583      newline unless endonly is set, else end of subject unless noteol is set. */      succeed. */
1584    
1585      case OP_DOLL:      case OP_ASSERT_NOT:
1586      if ((ims & PCRE_MULTILINE) != 0)      case OP_ASSERTBACK_NOT:
1587        save_mark = md->mark;
1588        if (md->match_function_type == MATCH_CONDASSERT)
1589        {        {
1590        if (eptr < md->end_subject)        condassert = TRUE;
1591          { if (*eptr != NEWLINE) RRETURN(MATCH_NOMATCH); }        md->match_function_type = 0;
       else  
         { if (md->noteol) RRETURN(MATCH_NOMATCH); }  
       ecode++;  
       break;  
1592        }        }
1593      else      else condassert = FALSE;
1594    
1595        /* Loop for each alternative branch. */
1596    
1597        do
1598        {        {
1599        if (md->noteol) RRETURN(MATCH_NOMATCH);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
1600        if (!md->endonly)        md->mark = save_mark;   /* Always restore the mark setting */
1601    
1602          switch(rrc)
1603          {          {
1604          if (eptr < md->end_subject - 1 ||          case MATCH_MATCH:            /* A successful match means */
1605             (eptr == md->end_subject - 1 && *eptr != NEWLINE))          case MATCH_ACCEPT:           /* the assertion has failed. */
1606            RRETURN(MATCH_NOMATCH);          RRETURN(MATCH_NOMATCH);
1607          ecode++;  
1608            case MATCH_NOMATCH:          /* Carry on with next branch */
1609          break;          break;
         }  
       }  
     /* ... else fall through */  
1610    
1611      /* End of subject assertion (\z) */          /* See comment in the code for capturing groups above about handling
1612            THEN. */
1613    
1614      case OP_EOD:          case MATCH_THEN:
1615      if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);          next = ecode + GET(ecode,1);
1616      ecode++;          if (md->start_match_ptr < next &&
1617      break;              (*ecode == OP_ALT || *next == OP_ALT))
1618              {
1619              rrc = MATCH_NOMATCH;
1620              break;
1621              }
1622            /* Otherwise fall through. */
1623    
1624      /* End of subject or ending \n assertion (\Z) */          /* COMMIT, SKIP, PRUNE, and an uncaptured THEN cause the whole
1625            assertion to fail to match, without considering any more alternatives.
1626            Failing to match means the assertion is true. This is a consistent
1627            approach, but does not always have the same effect as in Perl. */
1628    
1629            case MATCH_COMMIT:
1630            case MATCH_SKIP:
1631            case MATCH_SKIP_ARG:
1632            case MATCH_PRUNE:
1633            do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1634            goto NEG_ASSERT_TRUE;   /* Break out of alternation loop */
1635    
1636      case OP_EODN:          /* Anything else is an error */
     if (eptr < md->end_subject - 1 ||  
        (eptr == md->end_subject - 1 && *eptr != NEWLINE)) RRETURN(MATCH_NOMATCH);  
     ecode++;  
     break;  
1637    
1638      /* Word boundary assertions */          default:
1639            RRETURN(rrc);
1640            }
1641    
1642      case OP_NOT_WORD_BOUNDARY:        /* Continue with next branch */
     case OP_WORD_BOUNDARY:  
       {  
1643    
1644        /* Find out if the previous and current characters are "word" characters.        ecode += GET(ecode,1);
1645        It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to        }
1646        be "non-word" characters. */      while (*ecode == OP_ALT);
1647    
1648  #ifdef SUPPORT_UTF8      /* All branches in the assertion failed to match. */
1649        if (utf8)  
1650          {      NEG_ASSERT_TRUE:
1651          if (eptr == md->start_subject) prev_is_word = FALSE; else      if (condassert) RRETURN(MATCH_MATCH);  /* Condition assertion */
1652            {      ecode += 1 + LINK_SIZE;                /* Continue with current branch */
1653            const uschar *lastptr = eptr - 1;      continue;
           while((*lastptr & 0xc0) == 0x80) lastptr--;  
           GETCHAR(c, lastptr);  
           prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;  
           }  
         if (eptr >= md->end_subject) cur_is_word = FALSE; else  
           {  
           GETCHAR(c, eptr);  
           cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;  
           }  
         }  
       else  
 #endif  
1654    
1655        /* More streamlined when not in UTF-8 mode */      /* Move the subject pointer back. This occurs only at the start of
1656        each branch of a lookbehind assertion. If we are too close to the start to
1657        move back, this match function fails. When working with UTF-8 we move
1658        back a number of characters, not bytes. */
1659    
1660        case OP_REVERSE:
1661    #ifdef SUPPORT_UTF
1662        if (utf)
1663          {
1664          i = GET(ecode, 1);
1665          while (i-- > 0)
1666          {          {
1667          prev_is_word = (eptr != md->start_subject) &&          eptr--;
1668            ((md->ctypes[eptr[-1]] & ctype_word) != 0);          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1669          cur_is_word = (eptr < md->end_subject) &&          BACKCHAR(eptr);
           ((md->ctypes[*eptr] & ctype_word) != 0);  
1670          }          }
1671          }
1672        else
1673    #endif
1674    
1675        /* Now see if the situation is what we want */      /* No UTF-8 support, or not in UTF-8 mode: count is byte count */
1676    
1677        if ((*ecode++ == OP_WORD_BOUNDARY)?        {
1678             cur_is_word == prev_is_word : cur_is_word != prev_is_word)        eptr -= GET(ecode, 1);
1679          RRETURN(MATCH_NOMATCH);        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1680        }        }
     break;  
1681    
1682      /* Match a single character type; inline for speed */      /* Save the earliest consulted character, then skip to next op code */
1683    
1684      case OP_ANY:      if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
1685      if ((ims & PCRE_DOTALL) == 0 && eptr < md->end_subject && *eptr == NEWLINE)      ecode += 1 + LINK_SIZE;
       RRETURN(MATCH_NOMATCH);  
     if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);  
 #ifdef SUPPORT_UTF8  
     if (utf8)  
       while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;  
 #endif  
     ecode++;  
1686      break;      break;
1687    
1688      /* Match a single byte, even in UTF-8 mode. This opcode really does match      /* The callout item calls an external function, if one is provided, passing
1689      any byte, even newline, independent of the setting of PCRE_DOTALL. */      details of the match so far. This is mainly for debugging, though the
1690        function is able to force a failure. */
     case OP_ANYBYTE:  
     if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);  
     ecode++;  
     break;  
1691    
1692      case OP_NOT_DIGIT:      case OP_CALLOUT:
1693      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (PUBL(callout) != NULL)
1694      GETCHARINCTEST(c, eptr);        {
1695      if (        PUBL(callout_block) cb;
1696  #ifdef SUPPORT_UTF8        cb.version          = 2;   /* Version 1 of the callout block */
1697          cb.callout_number   = ecode[1];
1698          cb.offset_vector    = md->offset_vector;
1699    #if defined COMPILE_PCRE8
1700          cb.subject          = (PCRE_SPTR)md->start_subject;
1701    #elif defined COMPILE_PCRE16
1702          cb.subject          = (PCRE_SPTR16)md->start_subject;
1703    #elif defined COMPILE_PCRE32
1704          cb.subject          = (PCRE_SPTR32)md->start_subject;
1705    #endif
1706          cb.subject_length   = (int)(md->end_subject - md->start_subject);
1707          cb.start_match      = (int)(mstart - md->start_subject);
1708          cb.current_position = (int)(eptr - md->start_subject);
1709          cb.pattern_position = GET(ecode, 2);
1710          cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
1711          cb.capture_top      = offset_top/2;
1712          cb.capture_last     = md->capture_last & CAPLMASK;
1713          /* Internal change requires this for API compatibility. */
1714          if (cb.capture_last == 0) cb.capture_last = -1;
1715          cb.callout_data     = md->callout_data;
1716          cb.mark             = md->nomatch_mark;
1717          if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH);
1718          if (rrc < 0) RRETURN(rrc);
1719          }
1720        ecode += 2 + 2*LINK_SIZE;
1721        break;
1722    
1723        /* Recursion either matches the current regex, or some subexpression. The
1724        offset data is the offset to the starting bracket from the start of the
1725        whole pattern. (This is so that it works from duplicated subpatterns.)
1726    
1727        The state of the capturing groups is preserved over recursion, and
1728        re-instated afterwards. We don't know how many are started and not yet
1729        finished (offset_top records the completed total) so we just have to save
1730        all the potential data. There may be up to 65535 such values, which is too
1731        large to put on the stack, but using malloc for small numbers seems
1732        expensive. As a compromise, the stack is used when there are no more than
1733        REC_STACK_SAVE_MAX values to store; otherwise malloc is used.
1734    
1735        There are also other values that have to be saved. We use a chained
1736        sequence of blocks that actually live on the stack. Thanks to Robin Houston
1737        for the original version of this logic. It has, however, been hacked around
1738        a lot, so he is not to blame for the current way it works. */
1739    
1740        case OP_RECURSE:
1741          {
1742          recursion_info *ri;
1743          unsigned int recno;
1744    
1745          callpat = md->start_code + GET(ecode, 1);
1746          recno = (callpat == md->start_code)? 0 :
1747            GET2(callpat, 1 + LINK_SIZE);
1748    
1749          /* Check for repeating a recursion without advancing the subject pointer.
1750          This should catch convoluted mutual recursions. (Some simple cases are
1751          caught at compile time.) */
1752    
1753          for (ri = md->recursive; ri != NULL; ri = ri->prevrec)
1754            if (recno == ri->group_num && eptr == ri->subject_position)
1755              RRETURN(PCRE_ERROR_RECURSELOOP);
1756    
1757          /* Add to "recursing stack" */
1758    
1759          new_recursive.group_num = recno;
1760          new_recursive.saved_capture_last = md->capture_last;
1761          new_recursive.subject_position = eptr;
1762          new_recursive.prevrec = md->recursive;
1763          md->recursive = &new_recursive;
1764    
1765          /* Where to continue from afterwards */
1766    
1767          ecode += 1 + LINK_SIZE;
1768    
1769          /* Now save the offset data */
1770    
1771          new_recursive.saved_max = md->offset_end;
1772          if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)
1773            new_recursive.offset_save = stacksave;
1774          else
1775            {
1776            new_recursive.offset_save =
1777              (int *)(PUBL(malloc))(new_recursive.saved_max * sizeof(int));
1778            if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
1779            }
1780          memcpy(new_recursive.offset_save, md->offset_vector,
1781                new_recursive.saved_max * sizeof(int));
1782    
1783          /* OK, now we can do the recursion. After processing each alternative,
1784          restore the offset data and the last captured value. If there were nested
1785          recursions, md->recursive might be changed, so reset it before looping.
1786          */
1787    
1788          DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1789          cbegroup = (*callpat >= OP_SBRA);
1790          do
1791            {
1792            if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
1793            RMATCH(eptr, callpat + PRIV(OP_lengths)[*callpat], offset_top,
1794              md, eptrb, RM6);
1795            memcpy(md->offset_vector, new_recursive.offset_save,
1796                new_recursive.saved_max * sizeof(int));
1797            md->capture_last = new_recursive.saved_capture_last;
1798            md->recursive = new_recursive.prevrec;
1799            if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1800              {
1801              DPRINTF(("Recursion matched\n"));
1802              if (new_recursive.offset_save != stacksave)
1803                (PUBL(free))(new_recursive.offset_save);
1804    
1805              /* Set where we got to in the subject, and reset the start in case
1806              it was changed by \K. This *is* propagated back out of a recursion,
1807              for Perl compatibility. */
1808    
1809              eptr = md->end_match_ptr;
1810              mstart = md->start_match_ptr;
1811              goto RECURSION_MATCHED;        /* Exit loop; end processing */
1812              }
1813    
1814            /* PCRE does not allow THEN, SKIP, PRUNE or COMMIT to escape beyond a
1815            recursion; they cause a NOMATCH for the entire recursion. These codes
1816            are defined in a range that can be tested for. */
1817    
1818            if (rrc >= MATCH_BACKTRACK_MIN && rrc <= MATCH_BACKTRACK_MAX)
1819              RRETURN(MATCH_NOMATCH);
1820    
1821            /* Any return code other than NOMATCH is an error. */
1822    
1823            if (rrc != MATCH_NOMATCH)
1824              {
1825              DPRINTF(("Recursion gave error %d\n", rrc));
1826              if (new_recursive.offset_save != stacksave)
1827                (PUBL(free))(new_recursive.offset_save);
1828              RRETURN(rrc);
1829              }
1830    
1831            md->recursive = &new_recursive;
1832            callpat += GET(callpat, 1);
1833            }
1834          while (*callpat == OP_ALT);
1835    
1836          DPRINTF(("Recursion didn't match\n"));
1837          md->recursive = new_recursive.prevrec;
1838          if (new_recursive.offset_save != stacksave)
1839            (PUBL(free))(new_recursive.offset_save);
1840          RRETURN(MATCH_NOMATCH);
1841          }
1842    
1843        RECURSION_MATCHED:
1844        break;
1845    
1846        /* An alternation is the end of a branch; scan along to find the end of the
1847        bracketed group and go to there. */
1848    
1849        case OP_ALT:
1850        do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1851        break;
1852    
1853        /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
1854        indicating that it may occur zero times. It may repeat infinitely, or not
1855        at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets
1856        with fixed upper repeat limits are compiled as a number of copies, with the
1857        optional ones preceded by BRAZERO or BRAMINZERO. */
1858    
1859        case OP_BRAZERO:
1860        next = ecode + 1;
1861        RMATCH(eptr, next, offset_top, md, eptrb, RM10);
1862        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1863        do next += GET(next, 1); while (*next == OP_ALT);
1864        ecode = next + 1 + LINK_SIZE;
1865        break;
1866    
1867        case OP_BRAMINZERO:
1868        next = ecode + 1;
1869        do next += GET(next, 1); while (*next == OP_ALT);
1870        RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
1871        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1872        ecode++;
1873        break;
1874    
1875        case OP_SKIPZERO:
1876        next = ecode+1;
1877        do next += GET(next,1); while (*next == OP_ALT);
1878        ecode = next + 1 + LINK_SIZE;
1879        break;
1880    
1881        /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
1882        here; just jump to the group, with allow_zero set TRUE. */
1883    
1884        case OP_BRAPOSZERO:
1885        op = *(++ecode);
1886        allow_zero = TRUE;
1887        if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
1888          goto POSSESSIVE_NON_CAPTURE;
1889    
1890        /* End of a group, repeated or non-repeating. */
1891    
1892        case OP_KET:
1893        case OP_KETRMIN:
1894        case OP_KETRMAX:
1895        case OP_KETRPOS:
1896        prev = ecode - GET(ecode, 1);
1897    
1898        /* If this was a group that remembered the subject start, in order to break
1899        infinite repeats of empty string matches, retrieve the subject start from
1900        the chain. Otherwise, set it NULL. */
1901    
1902        if (*prev >= OP_SBRA || *prev == OP_ONCE)
1903          {
1904          saved_eptr = eptrb->epb_saved_eptr;   /* Value at start of group */
1905          eptrb = eptrb->epb_prev;              /* Backup to previous group */
1906          }
1907        else saved_eptr = NULL;
1908    
1909        /* If we are at the end of an assertion group or a non-capturing atomic
1910        group, stop matching and return MATCH_MATCH, but record the current high
1911        water mark for use by positive assertions. We also need to record the match
1912        start in case it was changed by \K. */
1913    
1914        if ((*prev >= OP_ASSERT && *prev <= OP_ASSERTBACK_NOT) ||
1915             *prev == OP_ONCE_NC)
1916          {
1917          md->end_match_ptr = eptr;      /* For ONCE_NC */
1918          md->end_offset_top = offset_top;
1919          md->start_match_ptr = mstart;
1920          RRETURN(MATCH_MATCH);         /* Sets md->mark */
1921          }
1922    
1923        /* For capturing groups we have to check the group number back at the start
1924        and if necessary complete handling an extraction by setting the offsets and
1925        bumping the high water mark. Whole-pattern recursion is coded as a recurse
1926        into group 0, so it won't be picked up here. Instead, we catch it when the
1927        OP_END is reached. Other recursion is handled here. We just have to record
1928        the current subject position and start match pointer and give a MATCH
1929        return. */
1930    
1931        if (*prev == OP_CBRA || *prev == OP_SCBRA ||
1932            *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
1933          {
1934          number = GET2(prev, 1+LINK_SIZE);
1935          offset = number << 1;
1936    
1937    #ifdef PCRE_DEBUG
1938          printf("end bracket %d", number);
1939          printf("\n");
1940    #endif
1941    
1942          /* Handle a recursively called group. */
1943    
1944          if (md->recursive != NULL && md->recursive->group_num == number)
1945            {
1946            md->end_match_ptr = eptr;
1947            md->start_match_ptr = mstart;
1948            RRETURN(MATCH_MATCH);
1949            }
1950    
1951          /* Deal with capturing */
1952    
1953          md->capture_last = (md->capture_last & OVFLMASK) | number;
1954          if (offset >= md->offset_max) md->capture_last |= OVFLBIT; else
1955            {
1956            /* If offset is greater than offset_top, it means that we are
1957            "skipping" a capturing group, and that group's offsets must be marked
1958            unset. In earlier versions of PCRE, all the offsets were unset at the
1959            start of matching, but this doesn't work because atomic groups and
1960            assertions can cause a value to be set that should later be unset.
1961            Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as
1962            part of the atomic group, but this is not on the final matching path,
1963            so must be unset when 2 is set. (If there is no group 2, there is no
1964            problem, because offset_top will then be 2, indicating no capture.) */
1965    
1966            if (offset > offset_top)
1967              {
1968              register int *iptr = md->offset_vector + offset_top;
1969              register int *iend = md->offset_vector + offset;
1970              while (iptr < iend) *iptr++ = -1;
1971              }
1972    
1973            /* Now make the extraction */
1974    
1975            md->offset_vector[offset] =
1976              md->offset_vector[md->offset_end - number];
1977            md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1978            if (offset_top <= offset) offset_top = offset + 2;
1979            }
1980          }
1981    
1982        /* For an ordinary non-repeating ket, just continue at this level. This
1983        also happens for a repeating ket if no characters were matched in the
1984        group. This is the forcible breaking of infinite loops as implemented in
1985        Perl 5.005. For a non-repeating atomic group that includes captures,
1986        establish a backup point by processing the rest of the pattern at a lower
1987        level. If this results in a NOMATCH return, pass MATCH_ONCE back to the
1988        original OP_ONCE level, thereby bypassing intermediate backup points, but
1989        resetting any captures that happened along the way. */
1990    
1991        if (*ecode == OP_KET || eptr == saved_eptr)
1992          {
1993          if (*prev == OP_ONCE)
1994            {
1995            RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
1996            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1997            md->once_target = prev;  /* Level at which to change to MATCH_NOMATCH */
1998            RRETURN(MATCH_ONCE);
1999            }
2000          ecode += 1 + LINK_SIZE;    /* Carry on at this level */
2001          break;
2002          }
2003    
2004        /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
2005        and return the MATCH_KETRPOS. This makes it possible to do the repeats one
2006        at a time from the outer level, thus saving stack. */
2007    
2008        if (*ecode == OP_KETRPOS)
2009          {
2010          md->start_match_ptr = mstart;    /* In case \K reset it */
2011          md->end_match_ptr = eptr;
2012          md->end_offset_top = offset_top;
2013          RRETURN(MATCH_KETRPOS);
2014          }
2015    
2016        /* The normal repeating kets try the rest of the pattern or restart from
2017        the preceding bracket, in the appropriate order. In the second case, we can
2018        use tail recursion to avoid using another stack frame, unless we have an
2019        an atomic group or an unlimited repeat of a group that can match an empty
2020        string. */
2021    
2022        if (*ecode == OP_KETRMIN)
2023          {
2024          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
2025          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2026          if (*prev == OP_ONCE)
2027            {
2028            RMATCH(eptr, prev, offset_top, md, eptrb, RM8);
2029            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2030            md->once_target = prev;  /* Level at which to change to MATCH_NOMATCH */
2031            RRETURN(MATCH_ONCE);
2032            }
2033          if (*prev >= OP_SBRA)    /* Could match an empty string */
2034            {
2035            RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
2036            RRETURN(rrc);
2037            }
2038          ecode = prev;
2039          goto TAIL_RECURSE;
2040          }
2041        else  /* OP_KETRMAX */
2042          {
2043          RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
2044          if (rrc == MATCH_ONCE && md->once_target == prev) rrc = MATCH_NOMATCH;
2045          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2046          if (*prev == OP_ONCE)
2047            {
2048            RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM9);
2049            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2050            md->once_target = prev;
2051            RRETURN(MATCH_ONCE);
2052            }
2053          ecode += 1 + LINK_SIZE;
2054          goto TAIL_RECURSE;
2055          }
2056        /* Control never gets here */
2057    
2058        /* Not multiline mode: start of subject assertion, unless notbol. */
2059    
2060        case OP_CIRC:
2061        if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
2062    
2063        /* Start of subject assertion */
2064    
2065        case OP_SOD:
2066        if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);
2067        ecode++;
2068        break;
2069    
2070        /* Multiline mode: start of subject unless notbol, or after any newline. */
2071    
2072        case OP_CIRCM:
2073        if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
2074        if (eptr != md->start_subject &&
2075            (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
2076          RRETURN(MATCH_NOMATCH);
2077        ecode++;
2078        break;
2079    
2080        /* Start of match assertion */
2081    
2082        case OP_SOM:
2083        if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);
2084        ecode++;
2085        break;
2086    
2087        /* Reset the start of match point */
2088    
2089        case OP_SET_SOM:
2090        mstart = eptr;
2091        ecode++;
2092        break;
2093    
2094        /* Multiline mode: assert before any newline, or before end of subject
2095        unless noteol is set. */
2096    
2097        case OP_DOLLM:
2098        if (eptr < md->end_subject)
2099          {
2100          if (!IS_NEWLINE(eptr))
2101            {
2102            if (md->partial != 0 &&
2103                eptr + 1 >= md->end_subject &&
2104                NLBLOCK->nltype == NLTYPE_FIXED &&
2105                NLBLOCK->nllen == 2 &&
2106                UCHAR21TEST(eptr) == NLBLOCK->nl[0])
2107              {
2108              md->hitend = TRUE;
2109              if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
2110              }
2111            RRETURN(MATCH_NOMATCH);
2112            }
2113          }
2114        else
2115          {
2116          if (md->noteol) RRETURN(MATCH_NOMATCH);
2117          SCHECK_PARTIAL();
2118          }
2119        ecode++;
2120        break;
2121    
2122        /* Not multiline mode: assert before a terminating newline or before end of
2123        subject unless noteol is set. */
2124    
2125        case OP_DOLL:
2126        if (md->noteol) RRETURN(MATCH_NOMATCH);
2127        if (!md->endonly) goto ASSERT_NL_OR_EOS;
2128    
2129        /* ... else fall through for endonly */
2130    
2131        /* End of subject assertion (\z) */
2132    
2133        case OP_EOD:
2134        if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);
2135        SCHECK_PARTIAL();
2136        ecode++;
2137        break;
2138    
2139        /* End of subject or ending \n assertion (\Z) */
2140    
2141        case OP_EODN:
2142        ASSERT_NL_OR_EOS:
2143        if (eptr < md->end_subject &&
2144            (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
2145          {
2146          if (md->partial != 0 &&
2147              eptr + 1 >= md->end_subject &&
2148              NLBLOCK->nltype == NLTYPE_FIXED &&
2149              NLBLOCK->nllen == 2 &&
2150              UCHAR21TEST(eptr) == NLBLOCK->nl[0])
2151            {
2152            md->hitend = TRUE;
2153            if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
2154            }
2155          RRETURN(MATCH_NOMATCH);
2156          }
2157    
2158        /* Either at end of string or \n before end. */
2159    
2160        SCHECK_PARTIAL();
2161        ecode++;
2162        break;
2163    
2164        /* Word boundary assertions */
2165    
2166        case OP_NOT_WORD_BOUNDARY:
2167        case OP_WORD_BOUNDARY:
2168          {
2169    
2170          /* Find out if the previous and current characters are "word" characters.
2171          It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to
2172          be "non-word" characters. Remember the earliest consulted character for
2173          partial matching. */
2174    
2175    #ifdef SUPPORT_UTF
2176          if (utf)
2177            {
2178            /* Get status of previous character */
2179    
2180            if (eptr == md->start_subject) prev_is_word = FALSE; else
2181              {
2182              PCRE_PUCHAR lastptr = eptr - 1;
2183              BACKCHAR(lastptr);
2184              if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
2185              GETCHAR(c, lastptr);
2186    #ifdef SUPPORT_UCP
2187              if (md->use_ucp)
2188                {
2189                if (c == '_') prev_is_word = TRUE; else
2190                  {
2191                  int cat = UCD_CATEGORY(c);
2192                  prev_is_word = (cat == ucp_L || cat == ucp_N);
2193                  }
2194                }
2195              else
2196    #endif
2197              prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
2198              }
2199    
2200            /* Get status of next character */
2201    
2202            if (eptr >= md->end_subject)
2203              {
2204              SCHECK_PARTIAL();
2205              cur_is_word = FALSE;
2206              }
2207            else
2208              {
2209              GETCHAR(c, eptr);
2210    #ifdef SUPPORT_UCP
2211              if (md->use_ucp)
2212                {
2213                if (c == '_') cur_is_word = TRUE; else
2214                  {
2215                  int cat = UCD_CATEGORY(c);
2216                  cur_is_word = (cat == ucp_L || cat == ucp_N);
2217                  }
2218                }
2219              else
2220    #endif
2221              cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
2222              }
2223            }
2224          else
2225    #endif
2226    
2227          /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for
2228          consistency with the behaviour of \w we do use it in this case. */
2229    
2230            {
2231            /* Get status of previous character */
2232    
2233            if (eptr == md->start_subject) prev_is_word = FALSE; else
2234              {
2235              if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
2236    #ifdef SUPPORT_UCP
2237              if (md->use_ucp)
2238                {
2239                c = eptr[-1];
2240                if (c == '_') prev_is_word = TRUE; else
2241                  {
2242                  int cat = UCD_CATEGORY(c);
2243                  prev_is_word = (cat == ucp_L || cat == ucp_N);
2244                  }
2245                }
2246              else
2247    #endif
2248              prev_is_word = MAX_255(eptr[-1])
2249                && ((md->ctypes[eptr[-1]] & ctype_word) != 0);
2250              }
2251    
2252            /* Get status of next character */
2253    
2254            if (eptr >= md->end_subject)
2255              {
2256              SCHECK_PARTIAL();
2257              cur_is_word = FALSE;
2258              }
2259            else
2260    #ifdef SUPPORT_UCP
2261            if (md->use_ucp)
2262              {
2263              c = *eptr;
2264              if (c == '_') cur_is_word = TRUE; else
2265                {
2266                int cat = UCD_CATEGORY(c);
2267                cur_is_word = (cat == ucp_L || cat == ucp_N);
2268                }
2269              }
2270            else
2271    #endif
2272            cur_is_word = MAX_255(*eptr)
2273              && ((md->ctypes[*eptr] & ctype_word) != 0);
2274            }
2275    
2276          /* Now see if the situation is what we want */
2277    
2278          if ((*ecode++ == OP_WORD_BOUNDARY)?
2279               cur_is_word == prev_is_word : cur_is_word != prev_is_word)
2280            RRETURN(MATCH_NOMATCH);
2281          }
2282        break;
2283    
2284        /* Match any single character type except newline; have to take care with
2285        CRLF newlines and partial matching. */
2286    
2287        case OP_ANY:
2288        if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
2289        if (md->partial != 0 &&
2290            eptr + 1 >= md->end_subject &&
2291            NLBLOCK->nltype == NLTYPE_FIXED &&
2292            NLBLOCK->nllen == 2 &&
2293            UCHAR21TEST(eptr) == NLBLOCK->nl[0])
2294          {
2295          md->hitend = TRUE;
2296          if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
2297          }
2298    
2299        /* Fall through */
2300    
2301        /* Match any single character whatsoever. */
2302    
2303        case OP_ALLANY:
2304        if (eptr >= md->end_subject)   /* DO NOT merge the eptr++ here; it must */
2305          {                            /* not be updated before SCHECK_PARTIAL. */
2306          SCHECK_PARTIAL();
2307          RRETURN(MATCH_NOMATCH);
2308          }
2309        eptr++;
2310    #ifdef SUPPORT_UTF
2311        if (utf) ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
2312    #endif
2313        ecode++;
2314        break;
2315    
2316        /* Match a single byte, even in UTF-8 mode. This opcode really does match
2317        any byte, even newline, independent of the setting of PCRE_DOTALL. */
2318    
2319        case OP_ANYBYTE:
2320        if (eptr >= md->end_subject)   /* DO NOT merge the eptr++ here; it must */
2321          {                            /* not be updated before SCHECK_PARTIAL. */
2322          SCHECK_PARTIAL();
2323          RRETURN(MATCH_NOMATCH);
2324          }
2325        eptr++;
2326        ecode++;
2327        break;
2328    
2329        case OP_NOT_DIGIT:
2330        if (eptr >= md->end_subject)
2331          {
2332          SCHECK_PARTIAL();
2333          RRETURN(MATCH_NOMATCH);
2334          }
2335        GETCHARINCTEST(c, eptr);
2336        if (
2337    #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2338         c < 256 &&         c < 256 &&
2339  #endif  #endif
2340         (md->ctypes[c] & ctype_digit) != 0         (md->ctypes[c] & ctype_digit) != 0
# Line 1279  for (;;) Line 2344  for (;;)
2344      break;      break;
2345    
2346      case OP_DIGIT:      case OP_DIGIT:
2347      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2348          {
2349          SCHECK_PARTIAL();
2350          RRETURN(MATCH_NOMATCH);
2351          }
2352      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2353      if (      if (
2354  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2355         c >= 256 ||         c > 255 ||
2356  #endif  #endif
2357         (md->ctypes[c] & ctype_digit) == 0         (md->ctypes[c] & ctype_digit) == 0
2358         )         )
# Line 1292  for (;;) Line 2361  for (;;)
2361      break;      break;
2362    
2363      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2364      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2365          {
2366          SCHECK_PARTIAL();
2367          RRETURN(MATCH_NOMATCH);
2368          }
2369      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2370      if (      if (
2371  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2372         c < 256 &&         c < 256 &&
2373  #endif  #endif
2374         (md->ctypes[c] & ctype_space) != 0         (md->ctypes[c] & ctype_space) != 0
# Line 1305  for (;;) Line 2378  for (;;)
2378      break;      break;
2379    
2380      case OP_WHITESPACE:      case OP_WHITESPACE:
2381      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2382          {
2383          SCHECK_PARTIAL();
2384          RRETURN(MATCH_NOMATCH);
2385          }
2386      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2387      if (      if (
2388  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2389         c >= 256 ||         c > 255 ||
2390  #endif  #endif
2391         (md->ctypes[c] & ctype_space) == 0         (md->ctypes[c] & ctype_space) == 0
2392         )         )
# Line 1318  for (;;) Line 2395  for (;;)
2395      break;      break;
2396    
2397      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2398      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2399          {
2400          SCHECK_PARTIAL();
2401          RRETURN(MATCH_NOMATCH);
2402          }
2403      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2404      if (      if (
2405  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2406         c < 256 &&         c < 256 &&
2407  #endif  #endif
2408         (md->ctypes[c] & ctype_word) != 0         (md->ctypes[c] & ctype_word) != 0
# Line 1331  for (;;) Line 2412  for (;;)
2412      break;      break;
2413    
2414      case OP_WORDCHAR:      case OP_WORDCHAR:
2415      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2416          {
2417          SCHECK_PARTIAL();
2418          RRETURN(MATCH_NOMATCH);
2419          }
2420      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2421      if (      if (
2422  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2423         c >= 256 ||         c > 255 ||
2424  #endif  #endif
2425         (md->ctypes[c] & ctype_word) == 0         (md->ctypes[c] & ctype_word) == 0
2426         )         )
# Line 1343  for (;;) Line 2428  for (;;)
2428      ecode++;      ecode++;
2429      break;      break;
2430    
2431        case OP_ANYNL:
2432        if (eptr >= md->end_subject)
2433          {
2434          SCHECK_PARTIAL();
2435          RRETURN(MATCH_NOMATCH);
2436          }
2437        GETCHARINCTEST(c, eptr);
2438        switch(c)
2439          {
2440          default: RRETURN(MATCH_NOMATCH);
2441    
2442          case CHAR_CR:
2443          if (eptr >= md->end_subject)
2444            {
2445            SCHECK_PARTIAL();
2446            }
2447          else if (UCHAR21TEST(eptr) == CHAR_LF) eptr++;
2448          break;
2449    
2450          case CHAR_LF:
2451          break;
2452    
2453          case CHAR_VT:
2454          case CHAR_FF:
2455          case CHAR_NEL:
2456    #ifndef EBCDIC
2457          case 0x2028:
2458          case 0x2029:
2459    #endif  /* Not EBCDIC */
2460          if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
2461          break;
2462          }
2463        ecode++;
2464        break;
2465    
2466        case OP_NOT_HSPACE:
2467        if (eptr >= md->end_subject)
2468          {
2469          SCHECK_PARTIAL();
2470          RRETURN(MATCH_NOMATCH);
2471          }
2472        GETCHARINCTEST(c, eptr);
2473        switch(c)
2474          {
2475          HSPACE_CASES: RRETURN(MATCH_NOMATCH);  /* Byte and multibyte cases */
2476          default: break;
2477          }
2478        ecode++;
2479        break;
2480    
2481        case OP_HSPACE:
2482        if (eptr >= md->end_subject)
2483          {
2484          SCHECK_PARTIAL();
2485          RRETURN(MATCH_NOMATCH);
2486          }
2487        GETCHARINCTEST(c, eptr);
2488        switch(c)
2489          {
2490          HSPACE_CASES: break;  /* Byte and multibyte cases */
2491          default: RRETURN(MATCH_NOMATCH);
2492          }
2493        ecode++;
2494        break;
2495    
2496        case OP_NOT_VSPACE:
2497        if (eptr >= md->end_subject)
2498          {
2499          SCHECK_PARTIAL();
2500          RRETURN(MATCH_NOMATCH);
2501          }
2502        GETCHARINCTEST(c, eptr);
2503        switch(c)
2504          {
2505          VSPACE_CASES: RRETURN(MATCH_NOMATCH);
2506          default: break;
2507          }
2508        ecode++;
2509        break;
2510    
2511        case OP_VSPACE:
2512        if (eptr >= md->end_subject)
2513          {
2514          SCHECK_PARTIAL();
2515          RRETURN(MATCH_NOMATCH);
2516          }
2517        GETCHARINCTEST(c, eptr);
2518        switch(c)
2519          {
2520          VSPACE_CASES: break;
2521          default: RRETURN(MATCH_NOMATCH);
2522          }
2523        ecode++;
2524        break;
2525    
2526  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2527      /* Check the next character by Unicode property. We will get here only      /* Check the next character by Unicode property. We will get here only
2528      if the support is in the binary; otherwise a compile-time error occurs. */      if the support is in the binary; otherwise a compile-time error occurs. */
2529    
2530      case OP_PROP:      case OP_PROP:
2531      case OP_NOTPROP:      case OP_NOTPROP:
2532      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2533          {
2534          SCHECK_PARTIAL();
2535          RRETURN(MATCH_NOMATCH);
2536          }
2537      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2538        {        {
2539        int chartype, rqdtype;        const pcre_uint32 *cp;
2540        int othercase;        const ucd_record *prop = GET_UCD(c);
       int category = ucp_findchar(c, &chartype, &othercase);  
   
       rqdtype = *(++ecode);  
       ecode++;  
2541    
2542        if (rqdtype >= 128)        switch(ecode[1])
2543          {          {
2544          if ((rqdtype - 128 != category) == (op == OP_PROP))          case PT_ANY:
2545            if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
2546            break;
2547    
2548            case PT_LAMP:
2549            if ((prop->chartype == ucp_Lu ||
2550                 prop->chartype == ucp_Ll ||
2551                 prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
2552            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
2553          }          break;
2554        else  
2555          {          case PT_GC:
2556          if ((rqdtype != chartype) == (op == OP_PROP))          if ((ecode[2] != PRIV(ucp_gentype)[prop->chartype]) == (op == OP_PROP))
2557              RRETURN(MATCH_NOMATCH);
2558            break;
2559    
2560            case PT_PC:
2561            if ((ecode[2] != prop->chartype) == (op == OP_PROP))
2562              RRETURN(MATCH_NOMATCH);
2563            break;
2564    
2565            case PT_SC:
2566            if ((ecode[2] != prop->script) == (op == OP_PROP))
2567              RRETURN(MATCH_NOMATCH);
2568            break;
2569    
2570            /* These are specials */
2571    
2572            case PT_ALNUM:
2573            if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2574                 PRIV(ucp_gentype)[prop->chartype] == ucp_N) == (op == OP_NOTPROP))
2575              RRETURN(MATCH_NOMATCH);
2576            break;
2577    
2578            /* Perl space used to exclude VT, but from Perl 5.18 it is included,
2579            which means that Perl space and POSIX space are now identical. PCRE
2580            was changed at release 8.34. */
2581    
2582            case PT_SPACE:    /* Perl space */
2583            case PT_PXSPACE:  /* POSIX space */
2584            switch(c)
2585              {
2586              HSPACE_CASES:
2587              VSPACE_CASES:
2588              if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
2589              break;
2590    
2591              default:
2592              if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z) ==
2593                (op == OP_NOTPROP)) RRETURN(MATCH_NOMATCH);
2594              break;
2595              }
2596            break;
2597    
2598            case PT_WORD:
2599            if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2600                 PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2601                 c == CHAR_UNDERSCORE) == (op == OP_NOTPROP))
2602              RRETURN(MATCH_NOMATCH);
2603            break;
2604    
2605            case PT_CLIST:
2606            cp = PRIV(ucd_caseless_sets) + ecode[2];
2607            for (;;)
2608              {
2609              if (c < *cp)
2610                { if (op == OP_PROP) { RRETURN(MATCH_NOMATCH); } else break; }
2611              if (c == *cp++)
2612                { if (op == OP_PROP) break; else { RRETURN(MATCH_NOMATCH); } }
2613              }
2614            break;
2615    
2616            case PT_UCNC:
2617            if ((c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
2618                 c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
2619                 c >= 0xe000) == (op == OP_NOTPROP))
2620            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
2621            break;
2622    
2623            /* This should never occur */
2624    
2625            default:
2626            RRETURN(PCRE_ERROR_INTERNAL);
2627          }          }
2628    
2629          ecode += 3;
2630        }        }
2631      break;      break;
2632    
# Line 1376  for (;;) Line 2634  for (;;)
2634      is in the binary; otherwise a compile-time error occurs. */      is in the binary; otherwise a compile-time error occurs. */
2635    
2636      case OP_EXTUNI:      case OP_EXTUNI:
2637      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
     GETCHARINCTEST(c, eptr);  
2638        {        {
2639        int chartype;        SCHECK_PARTIAL();
2640        int othercase;        RRETURN(MATCH_NOMATCH);
2641        int category = ucp_findchar(c, &chartype, &othercase);        }
2642        if (category == ucp_M) RRETURN(MATCH_NOMATCH);      else
2643          {
2644          int lgb, rgb;
2645          GETCHARINCTEST(c, eptr);
2646          lgb = UCD_GRAPHBREAK(c);
2647        while (eptr < md->end_subject)        while (eptr < md->end_subject)
2648          {          {
2649          int len = 1;          int len = 1;
2650          if (!utf8) c = *eptr; else          if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
2651            {          rgb = UCD_GRAPHBREAK(c);
2652            GETCHARLEN(c, eptr, len);          if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
2653            }          lgb = rgb;
         category = ucp_findchar(c, &chartype, &othercase);  
         if (category != ucp_M) break;  
2654          eptr += len;          eptr += len;
2655          }          }
2656        }        }
2657        CHECK_PARTIAL();
2658      ecode++;      ecode++;
2659      break;      break;
2660  #endif  #endif  /* SUPPORT_UCP */
2661    
2662    
2663      /* Match a back reference, possibly repeatedly. Look past the end of the      /* Match a back reference, possibly repeatedly. Look past the end of the
# Line 1406  for (;;) Line 2666  for (;;)
2666      similar code to character type repeats - written out again for speed.      similar code to character type repeats - written out again for speed.
2667      However, if the referenced string is the empty string, always treat      However, if the referenced string is the empty string, always treat
2668      it as matched, any number of times (otherwise there could be infinite      it as matched, any number of times (otherwise there could be infinite
2669      loops). */      loops). If the reference is unset, there are two possibilities:
2670    
2671      case OP_REF:      (a) In the default, Perl-compatible state, set the length negative;
2672        this ensures that every attempt at a match fails. We can't just fail
2673        here, because of the possibility of quantifiers with zero minima.
2674    
2675        (b) If the JavaScript compatibility flag is set, set the length to zero
2676        so that the back reference matches an empty string.
2677    
2678        Otherwise, set the length to the length of what was matched by the
2679        referenced subpattern.
2680    
2681        The OP_REF and OP_REFI opcodes are used for a reference to a numbered group
2682        or to a non-duplicated named group. For a duplicated named group, OP_DNREF
2683        and OP_DNREFI are used. In this case we must scan the list of groups to
2684        which the name refers, and use the first one that is set. */
2685    
2686        case OP_DNREF:
2687        case OP_DNREFI:
2688        caseless = op == OP_DNREFI;
2689        {        {
2690        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */        int count = GET2(ecode, 1+IMM2_SIZE);
2691        ecode += 3;                                 /* Advance past item */        pcre_uchar *slot = md->name_table + GET2(ecode, 1) * md->name_entry_size;
2692          ecode += 1 + 2*IMM2_SIZE;
2693    
2694          /* Setting the default length first and initializing 'offset' avoids
2695          compiler warnings in the REF_REPEAT code. */
2696    
2697          length = (md->jscript_compat)? 0 : -1;
2698          offset = 0;
2699    
2700          while (count-- > 0)
2701            {
2702            offset = GET2(slot, 0) << 1;
2703            if (offset < offset_top && md->offset_vector[offset] >= 0)
2704              {
2705              length = md->offset_vector[offset+1] - md->offset_vector[offset];
2706              break;
2707              }
2708            slot += md->name_entry_size;
2709            }
2710          }
2711        goto REF_REPEAT;
2712    
2713        /* If the reference is unset, set the length to be longer than the amount      case OP_REF:
2714        of subject left; this ensures that every attempt at a match fails. We      case OP_REFI:
2715        can't just fail here, because of the possibility of quantifiers with zero      caseless = op == OP_REFI;
2716        minima. */      offset = GET2(ecode, 1) << 1;               /* Doubled ref number */
2717        ecode += 1 + IMM2_SIZE;
2718        length = (offset >= offset_top || md->offset_vector[offset] < 0)?      if (offset >= offset_top || md->offset_vector[offset] < 0)
2719          md->end_subject - eptr + 1 :        length = (md->jscript_compat)? 0 : -1;
2720          md->offset_vector[offset+1] - md->offset_vector[offset];      else
2721          length = md->offset_vector[offset+1] - md->offset_vector[offset];
2722    
2723        /* Set up for repetition, or handle the non-repeated case */      /* Set up for repetition, or handle the non-repeated case */
2724    
2725        switch (*ecode)      REF_REPEAT:
2726          {      switch (*ecode)
2727          case OP_CRSTAR:        {
2728          case OP_CRMINSTAR:        case OP_CRSTAR:
2729          case OP_CRPLUS:        case OP_CRMINSTAR:
2730          case OP_CRMINPLUS:        case OP_CRPLUS:
2731          case OP_CRQUERY:        case OP_CRMINPLUS:
2732          case OP_CRMINQUERY:        case OP_CRQUERY:
2733          c = *ecode++ - OP_CRSTAR;        case OP_CRMINQUERY:
2734          minimize = (c & 1) != 0;        c = *ecode++ - OP_CRSTAR;
2735          min = rep_min[c];                 /* Pick up values from tables; */        minimize = (c & 1) != 0;
2736          max = rep_max[c];                 /* zero for max => infinity */        min = rep_min[c];                 /* Pick up values from tables; */
2737          if (max == 0) max = INT_MAX;        max = rep_max[c];                 /* zero for max => infinity */
2738          break;        if (max == 0) max = INT_MAX;
2739          break;
2740    
2741          case OP_CRRANGE:        case OP_CRRANGE:
2742          case OP_CRMINRANGE:        case OP_CRMINRANGE:
2743          minimize = (*ecode == OP_CRMINRANGE);        minimize = (*ecode == OP_CRMINRANGE);
2744          min = GET2(ecode, 1);        min = GET2(ecode, 1);
2745          max = GET2(ecode, 3);        max = GET2(ecode, 1 + IMM2_SIZE);
2746          if (max == 0) max = INT_MAX;        if (max == 0) max = INT_MAX;
2747          ecode += 5;        ecode += 1 + 2 * IMM2_SIZE;
2748          break;        break;
2749    
2750          default:               /* No repeat follows */        default:               /* No repeat follows */
2751          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);        if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
2752          eptr += length;          {
2753          continue;              /* With the main loop */          if (length == -2) eptr = md->end_subject;   /* Partial match */
2754            CHECK_PARTIAL();
2755            RRETURN(MATCH_NOMATCH);
2756          }          }
2757          eptr += length;
2758          continue;              /* With the main loop */
2759          }
2760    
2761        /* If the length of the reference is zero, just continue with the      /* Handle repeated back references. If the length of the reference is
2762        main loop. */      zero, just continue with the main loop. If the length is negative, it
2763        means the reference is unset in non-Java-compatible mode. If the minimum is
2764        zero, we can continue at the same level without recursion. For any other
2765        minimum, carrying on will result in NOMATCH. */
2766    
2767        if (length == 0) continue;      if (length == 0) continue;
2768        if (length < 0 && min == 0) continue;
2769    
2770        /* First, ensure the minimum number of matches are present. We get back      /* First, ensure the minimum number of matches are present. We get back
2771        the length of the reference string explicitly rather than passing the      the length of the reference string explicitly rather than passing the
2772        address of eptr, so that eptr can be a register variable. */      address of eptr, so that eptr can be a register variable. */
2773    
2774        for (i = 1; i <= min; i++)      for (i = 1; i <= min; i++)
2775          {
2776          int slength;
2777          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2778          {          {
2779          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);          if (slength == -2) eptr = md->end_subject;   /* Partial match */
2780          eptr += length;          CHECK_PARTIAL();
2781            RRETURN(MATCH_NOMATCH);
2782          }          }
2783          eptr += slength;
2784          }
2785    
2786        /* If min = max, continue at the same level without recursion.      /* If min = max, continue at the same level without recursion.
2787        They are not both allowed to be zero. */      They are not both allowed to be zero. */
2788    
2789        if (min == max) continue;      if (min == max) continue;
2790    
2791        /* If minimizing, keep trying and advancing the pointer */      /* If minimizing, keep trying and advancing the pointer */
2792    
2793        if (minimize)      if (minimize)
2794          {
2795          for (fi = min;; fi++)
2796          {          {
2797          for (fi = min;; fi++)          int slength;
2798            RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
2799            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2800            if (fi >= max) RRETURN(MATCH_NOMATCH);
2801            if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2802            {            {
2803            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            if (slength == -2) eptr = md->end_subject;   /* Partial match */
2804            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            CHECK_PARTIAL();
2805            if (fi >= max || !match_ref(offset, eptr, length, md, ims))            RRETURN(MATCH_NOMATCH);
             RRETURN(MATCH_NOMATCH);  
           eptr += length;  
2806            }            }
2807          /* Control never gets here */          eptr += slength;
2808          }          }
2809          /* Control never gets here */
2810          }
2811    
2812        /* If maximizing, find the longest string and work backwards */      /* If maximizing, find the longest string and work backwards */
2813    
2814        else      else
2815          {
2816          pp = eptr;
2817          for (i = min; i < max; i++)
2818          {          {
2819          pp = eptr;          int slength;
2820          for (i = min; i < max; i++)          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
           {  
           if (!match_ref(offset, eptr, length, md, ims)) break;  
           eptr += length;  
           }  
         while (eptr >= pp)  
2821            {            {
2822            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            /* Can't use CHECK_PARTIAL because we don't want to update eptr in
2823            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            the soft partial matching case. */
2824            eptr -= length;  
2825              if (slength == -2 && md->partial != 0 &&
2826                  md->end_subject > md->start_used_ptr)
2827                {
2828                md->hitend = TRUE;
2829                if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
2830                }
2831              break;
2832            }            }
2833          RRETURN(MATCH_NOMATCH);          eptr += slength;
2834            }
2835    
2836          while (eptr >= pp)
2837            {
2838            RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
2839            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2840            eptr -= length;
2841          }          }
2842          RRETURN(MATCH_NOMATCH);
2843        }        }
2844      /* Control never gets here */      /* Control never gets here */
2845    
   
   
2846      /* Match a bit-mapped character class, possibly repeatedly. This op code is      /* Match a bit-mapped character class, possibly repeatedly. This op code is
2847      used when all the characters in the class have values in the range 0-255,      used when all the characters in the class have values in the range 0-255,
2848      and either the matching is caseful, or the characters are in the range      and either the matching is caseful, or the characters are in the range
# Line 1526  for (;;) Line 2857  for (;;)
2857      case OP_NCLASS:      case OP_NCLASS:
2858      case OP_CLASS:      case OP_CLASS:
2859        {        {
2860          /* The data variable is saved across frames, so the byte map needs to
2861          be stored there. */
2862    #define BYTE_MAP ((pcre_uint8 *)data)
2863        data = ecode + 1;                /* Save for matching */        data = ecode + 1;                /* Save for matching */
2864        ecode += 33;                     /* Advance past the item */        ecode += 1 + (32 / sizeof(pcre_uchar)); /* Advance past the item */
2865    
2866        switch (*ecode)        switch (*ecode)
2867          {          {
# Line 1537  for (;;) Line 2871  for (;;)
2871          case OP_CRMINPLUS:          case OP_CRMINPLUS:
2872          case OP_CRQUERY:          case OP_CRQUERY:
2873          case OP_CRMINQUERY:          case OP_CRMINQUERY:
2874            case OP_CRPOSSTAR:
2875            case OP_CRPOSPLUS:
2876            case OP_CRPOSQUERY:
2877          c = *ecode++ - OP_CRSTAR;          c = *ecode++ - OP_CRSTAR;
2878          minimize = (c & 1) != 0;          if (c < OP_CRPOSSTAR - OP_CRSTAR) minimize = (c & 1) != 0;
2879            else possessive = TRUE;
2880          min = rep_min[c];                 /* Pick up values from tables; */          min = rep_min[c];                 /* Pick up values from tables; */
2881          max = rep_max[c];                 /* zero for max => infinity */          max = rep_max[c];                 /* zero for max => infinity */
2882          if (max == 0) max = INT_MAX;          if (max == 0) max = INT_MAX;
# Line 1546  for (;;) Line 2884  for (;;)
2884    
2885          case OP_CRRANGE:          case OP_CRRANGE:
2886          case OP_CRMINRANGE:          case OP_CRMINRANGE:
2887            case OP_CRPOSRANGE:
2888          minimize = (*ecode == OP_CRMINRANGE);          minimize = (*ecode == OP_CRMINRANGE);
2889            possessive = (*ecode == OP_CRPOSRANGE);
2890          min = GET2(ecode, 1);          min = GET2(ecode, 1);
2891          max = GET2(ecode, 3);          max = GET2(ecode, 1 + IMM2_SIZE);
2892          if (max == 0) max = INT_MAX;          if (max == 0) max = INT_MAX;
2893          ecode += 5;          ecode += 1 + 2 * IMM2_SIZE;
2894          break;          break;
2895    
2896          default:               /* No repeat follows */          default:               /* No repeat follows */
# Line 1560  for (;;) Line 2900  for (;;)
2900    
2901        /* First, ensure the minimum number of matches are present. */        /* First, ensure the minimum number of matches are present. */
2902    
2903  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2904        /* UTF-8 mode */        if (utf)
       if (utf8)  
2905          {          {
2906          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2907            {            {
2908            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2909                {
2910                SCHECK_PARTIAL();
2911                RRETURN(MATCH_NOMATCH);
2912                }
2913            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
2914            if (c > 255)            if (c > 255)
2915              {              {
2916              if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);              if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2917              }              }
2918            else            else
2919              {              if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
             if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);  
             }  
2920            }            }
2921          }          }
2922        else        else
2923  #endif  #endif
2924        /* Not UTF-8 mode */        /* Not UTF mode */
2925          {          {
2926          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2927            {            {
2928            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2929                {
2930                SCHECK_PARTIAL();
2931                RRETURN(MATCH_NOMATCH);
2932                }
2933            c = *eptr++;            c = *eptr++;
2934            if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);  #ifndef COMPILE_PCRE8
2935              if (c > 255)
2936                {
2937                if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2938                }
2939              else
2940    #endif
2941                if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2942            }            }
2943          }          }
2944    
# Line 1600  for (;;) Line 2952  for (;;)
2952    
2953        if (minimize)        if (minimize)
2954          {          {
2955  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2956          /* UTF-8 mode */          if (utf)
         if (utf8)  
2957            {            {
2958            for (fi = min;; fi++)            for (fi = min;; fi++)
2959              {              {
2960              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
2961              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2962              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) RRETURN(MATCH_NOMATCH);
2963                if (eptr >= md->end_subject)
2964                  {
2965                  SCHECK_PARTIAL();
2966                  RRETURN(MATCH_NOMATCH);
2967                  }
2968              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
2969              if (c > 255)              if (c > 255)
2970                {                {
2971                if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);                if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2972                }                }
2973              else              else
2974                {                if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
               if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);  
               }  
2975              }              }
2976            }            }
2977          else          else
2978  #endif  #endif
2979          /* Not UTF-8 mode */          /* Not UTF mode */
2980            {            {
2981            for (fi = min;; fi++)            for (fi = min;; fi++)
2982              {              {
2983              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
2984              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2985              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) RRETURN(MATCH_NOMATCH);
2986                if (eptr >= md->end_subject)
2987                  {
2988                  SCHECK_PARTIAL();
2989                  RRETURN(MATCH_NOMATCH);
2990                  }
2991              c = *eptr++;              c = *eptr++;
2992              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);  #ifndef COMPILE_PCRE8
2993                if (c > 255)
2994                  {
2995                  if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2996                  }
2997                else
2998    #endif
2999                  if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
3000              }              }
3001            }            }
3002          /* Control never gets here */          /* Control never gets here */
# Line 1642  for (;;) Line 3008  for (;;)
3008          {          {
3009          pp = eptr;          pp = eptr;
3010    
3011  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3012          /* UTF-8 mode */          if (utf)
         if (utf8)  
3013            {            {
3014            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3015              {              {
3016              int len = 1;              int len = 1;
3017              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3018                  {
3019                  SCHECK_PARTIAL();
3020                  break;
3021                  }
3022              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
3023              if (c > 255)              if (c > 255)
3024                {                {
3025                if (op == OP_CLASS) break;                if (op == OP_CLASS) break;
3026                }                }
3027              else              else
3028                {                if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break;
               if ((data[c/8] & (1 << (c&7))) == 0) break;  
               }  
3029              eptr += len;              eptr += len;
3030              }              }
3031    
3032              if (possessive) continue;    /* No backtracking */
3033    
3034            for (;;)            for (;;)
3035              {              {
3036              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
3037              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3038              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3039              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 1671  for (;;) Line 3041  for (;;)
3041   &