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