/[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 172 by ph10, Tue Jun 5 10:40:13 2007 UTC revision 645 by ph10, Sun Jul 31 17:02:18 2011 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-2007 University of Cambridge             Copyright (c) 1997-2011 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 42  POSSIBILITY OF SUCH DAMAGE. Line 42  POSSIBILITY OF SUCH DAMAGE.
42  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
43  possible. There are also some static supporting functions. */  possible. There are also some static supporting functions. */
44    
45    #ifdef HAVE_CONFIG_H
46    #include "config.h"
47    #endif
48    
49  #define NLBLOCK md             /* Block containing newline information */  #define NLBLOCK md             /* Block containing newline information */
50  #define PSSTART start_subject  /* Field containing processed string start */  #define PSSTART start_subject  /* Field containing processed string start */
51  #define PSEND   end_subject    /* Field containing processed string end */  #define PSEND   end_subject    /* Field containing processed string end */
# Line 53  possible. There are also some static sup Line 57  possible. There are also some static sup
57  #undef min  #undef min
58  #undef max  #undef max
59    
60  /* The chain of eptrblocks for tail recursions uses memory in stack workspace,  /* Values for setting in md->match_function_type to indicate two special types
61  obtained at top level, the size of which is defined by EPTR_WORK_SIZE. */  of call to match(). We do it this way to save on using another stack variable,
62    as stack usage is to be discouraged. */
 #define EPTR_WORK_SIZE (1000)  
   
 /* Flag bits for the match() function */  
63    
64  #define match_condassert     0x01  /* Called to check a condition assertion */  #define MATCH_CONDASSERT     1  /* Called to check a condition assertion */
65  #define match_cbegroup       0x02  /* Could-be-empty unlimited repeat group */  #define MATCH_CBEGROUP       2  /* Could-be-empty unlimited repeat group */
 #define match_tail_recursed  0x04  /* Tail recursive call */  
66    
67  /* Non-error returns from the match() function. Error returns are externally  /* Non-error returns from the match() function. Error returns are externally
68  defined PCRE_ERROR_xxx codes, which are all negative. */  defined PCRE_ERROR_xxx codes, which are all negative. */
# Line 70  defined PCRE_ERROR_xxx codes, which are Line 70  defined PCRE_ERROR_xxx codes, which are
70  #define MATCH_MATCH        1  #define MATCH_MATCH        1
71  #define MATCH_NOMATCH      0  #define MATCH_NOMATCH      0
72    
73    /* Special internal returns from the match() function. Make them sufficiently
74    negative to avoid the external error codes. */
75    
76    #define MATCH_ACCEPT       (-999)
77    #define MATCH_COMMIT       (-998)
78    #define MATCH_KETRPOS      (-997)
79    #define MATCH_ONCE         (-996)
80    #define MATCH_PRUNE        (-995)
81    #define MATCH_SKIP         (-994)
82    #define MATCH_SKIP_ARG     (-993)
83    #define MATCH_THEN         (-992)
84    
85    /* This is a convenience macro for code that occurs many times. */
86    
87    #define MRRETURN(ra) \
88      { \
89      md->mark = markptr; \
90      RRETURN(ra); \
91      }
92    
93  /* 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.
94  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,
95  because the offset vector is always a multiple of 3 long. */  because the offset vector is always a multiple of 3 long. */
# Line 83  static const char rep_max[] = { 0, 0, 0, Line 103  static const char rep_max[] = { 0, 0, 0,
103    
104    
105    
106  #ifdef DEBUG  #ifdef PCRE_DEBUG
107  /*************************************************  /*************************************************
108  *        Debugging function to print chars       *  *        Debugging function to print chars       *
109  *************************************************/  *************************************************/
# Line 116  while (length-- > 0) Line 136  while (length-- > 0)
136  *          Match a back-reference                *  *          Match a back-reference                *
137  *************************************************/  *************************************************/
138    
139  /* 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
140  than the number of characters left in the string, so the match fails.  negative, so the match always fails. However, in JavaScript compatibility mode,
141    the length passed is zero. Note that in caseless UTF-8 mode, the number of
142    subject bytes matched may be different to the number of reference bytes.
143    
144  Arguments:  Arguments:
145    offset      index into the offset vector    offset      index into the offset vector
146    eptr        points into the subject    eptr        pointer into the subject
147    length      length to be matched    length      length of reference to be matched (number of bytes)
148    md          points to match data block    md          points to match data block
149    ims         the ims flags    caseless    TRUE if caseless
150    
151  Returns:      TRUE if matched  Returns:      < 0 if not matched, otherwise the number of subject bytes matched
152  */  */
153    
154  static BOOL  static int
155  match_ref(int offset, register USPTR eptr, int length, match_data *md,  match_ref(int offset, register USPTR eptr, int length, match_data *md,
156    unsigned long int ims)    BOOL caseless)
157  {  {
158  USPTR p = md->start_subject + md->offset_vector[offset];  USPTR eptr_start = eptr;
159    register USPTR p = md->start_subject + md->offset_vector[offset];
160    
161  #ifdef DEBUG  #ifdef PCRE_DEBUG
162  if (eptr >= md->end_subject)  if (eptr >= md->end_subject)
163    printf("matching subject <null>");    printf("matching subject <null>");
164  else  else
# Line 148  pchars(p, length, FALSE, md); Line 171  pchars(p, length, FALSE, md);
171  printf("\n");  printf("\n");
172  #endif  #endif
173    
174  /* Always fail if not enough characters left */  /* Always fail if reference not set (and not JavaScript compatible). */
175    
176  if (length > md->end_subject - eptr) return FALSE;  if (length < 0) return -1;
177    
178  /* Separate the caselesss case for speed */  /* Separate the caseless case for speed. In UTF-8 mode we can only do this
179    properly if Unicode properties are supported. Otherwise, we can check only
180    ASCII characters. */
181    
182  if ((ims & PCRE_CASELESS) != 0)  if (caseless)
183    {    {
184    while (length-- > 0)  #ifdef SUPPORT_UTF8
185      if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE;  #ifdef SUPPORT_UCP
186      if (md->utf8)
187        {
188        /* Match characters up to the end of the reference. NOTE: the number of
189        bytes matched may differ, because there are some characters whose upper and
190        lower case versions code as different numbers of bytes. For example, U+023A
191        (2 bytes in UTF-8) is the upper case version of U+2C65 (3 bytes in UTF-8);
192        a sequence of 3 of the former uses 6 bytes, as does a sequence of two of
193        the latter. It is important, therefore, to check the length along the
194        reference, not along the subject (earlier code did this wrong). */
195    
196        USPTR endptr = p + length;
197        while (p < endptr)
198          {
199          int c, d;
200          if (eptr >= md->end_subject) return -1;
201          GETCHARINC(c, eptr);
202          GETCHARINC(d, p);
203          if (c != d && c != UCD_OTHERCASE(d)) return -1;
204          }
205        }
206      else
207    #endif
208    #endif
209    
210      /* The same code works when not in UTF-8 mode and in UTF-8 mode when there
211      is no UCP support. */
212        {
213        if (eptr + length > md->end_subject) return -1;
214        while (length-- > 0)
215          { if (md->lcc[*p++] != md->lcc[*eptr++]) return -1; }
216        }
217    }    }
218    
219    /* In the caseful case, we can just compare the bytes, whether or not we
220    are in UTF-8 mode. */
221    
222  else  else
223    { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }    {
224      if (eptr + length > md->end_subject) return -1;
225      while (length-- > 0) if (*p++ != *eptr++) return -1;
226      }
227    
228  return TRUE;  return eptr - eptr_start;
229  }  }
230    
231    
# Line 205  variable instead of being passed in the Line 268  variable instead of being passed in the
268  ****************************************************************************  ****************************************************************************
269  ***************************************************************************/  ***************************************************************************/
270    
271    /* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN
272  /* Numbers for RMATCH calls */  below must be updated in sync.  */
273    
274  enum { RM1=1, RM2,  RM3,  RM4,  RM5,  RM6,  RM7,  RM8,  RM9,  RM10,  enum { RM1=1, RM2,  RM3,  RM4,  RM5,  RM6,  RM7,  RM8,  RM9,  RM10,
275         RM11,  RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,         RM11,  RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,
276         RM21,  RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,         RM21,  RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
277         RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,         RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
278         RM41,  RM42, RM43, RM44, RM45, RM46, RM47 };         RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
279           RM51,  RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60,
280           RM61,  RM62, RM63 };
281    
282  /* These versions of the macros use the stack, as normal. There are debugging  /* These versions of the macros use the stack, as normal. There are debugging
283  versions and production versions. Note that the "rw" argument of RMATCH isn't  versions and production versions. Note that the "rw" argument of RMATCH isn't
284  actuall used in this definition. */  actually used in this definition. */
285    
286  #ifndef NO_RECURSE  #ifndef NO_RECURSE
287  #define REGISTER register  #define REGISTER register
288    
289  #ifdef DEBUG  #ifdef PCRE_DEBUG
290  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \  #define RMATCH(ra,rb,rc,rd,re,rw) \
291    { \    { \
292    printf("match() called in line %d\n", __LINE__); \    printf("match() called in line %d\n", __LINE__); \
293    rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1); \    rrc = match(ra,rb,mstart,markptr,rc,rd,re,rdepth+1); \
294    printf("to line %d\n", __LINE__); \    printf("to line %d\n", __LINE__); \
295    }    }
296  #define RRETURN(ra) \  #define RRETURN(ra) \
# Line 235  actuall used in this definition. */ Line 299  actuall used in this definition. */
299    return ra; \    return ra; \
300    }    }
301  #else  #else
302  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \  #define RMATCH(ra,rb,rc,rd,re,rw) \
303    rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1)    rrc = match(ra,rb,mstart,markptr,rc,rd,re,rdepth+1)
304  #define RRETURN(ra) return ra  #define RRETURN(ra) return ra
305  #endif  #endif
306    
# Line 249  argument of match(), which never changes Line 313  argument of match(), which never changes
313    
314  #define REGISTER  #define REGISTER
315    
316  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw)\  #define RMATCH(ra,rb,rc,rd,re,rw)\
317    {\    {\
318    heapframe *newframe = (pcre_stack_malloc)(sizeof(heapframe));\    heapframe *newframe = (heapframe *)(pcre_stack_malloc)(sizeof(heapframe));\
319      if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\
320    frame->Xwhere = rw; \    frame->Xwhere = rw; \
321    newframe->Xeptr = ra;\    newframe->Xeptr = ra;\
322    newframe->Xecode = rb;\    newframe->Xecode = rb;\
323    newframe->Xmstart = mstart;\    newframe->Xmstart = mstart;\
324      newframe->Xmarkptr = markptr;\
325    newframe->Xoffset_top = rc;\    newframe->Xoffset_top = rc;\
326    newframe->Xims = re;\    newframe->Xeptrb = re;\
   newframe->Xeptrb = rf;\  
   newframe->Xflags = rg;\  
327    newframe->Xrdepth = frame->Xrdepth + 1;\    newframe->Xrdepth = frame->Xrdepth + 1;\
328    newframe->Xprevframe = frame;\    newframe->Xprevframe = frame;\
329    frame = newframe;\    frame = newframe;\
# Line 271  argument of match(), which never changes Line 335  argument of match(), which never changes
335    
336  #define RRETURN(ra)\  #define RRETURN(ra)\
337    {\    {\
338    heapframe *newframe = frame;\    heapframe *oldframe = frame;\
339    frame = newframe->Xprevframe;\    frame = oldframe->Xprevframe;\
340    (pcre_stack_free)(newframe);\    (pcre_stack_free)(oldframe);\
341    if (frame != NULL)\    if (frame != NULL)\
342      {\      {\
343      rrc = ra;\      rrc = ra;\
# Line 290  typedef struct heapframe { Line 354  typedef struct heapframe {
354    
355    /* Function arguments that may change */    /* Function arguments that may change */
356    
357    const uschar *Xeptr;    USPTR Xeptr;
358    const uschar *Xecode;    const uschar *Xecode;
359    const uschar *Xmstart;    USPTR Xmstart;
360      USPTR Xmarkptr;
361    int Xoffset_top;    int Xoffset_top;
   long int Xims;  
362    eptrblock *Xeptrb;    eptrblock *Xeptrb;
   int Xflags;  
363    unsigned int Xrdepth;    unsigned int Xrdepth;
364    
365    /* Function local variables */    /* Function local variables */
366    
367    const uschar *Xcallpat;    USPTR Xcallpat;
368    const uschar *Xcharptr;  #ifdef SUPPORT_UTF8
369    const uschar *Xdata;    USPTR Xcharptr;
370    const uschar *Xnext;  #endif
371    const uschar *Xpp;    USPTR Xdata;
372    const uschar *Xprev;    USPTR Xnext;
373    const uschar *Xsaved_eptr;    USPTR Xpp;
374      USPTR Xprev;
375      USPTR Xsaved_eptr;
376    
377    recursion_info Xnew_recursive;    recursion_info Xnew_recursive;
378    
# Line 315  typedef struct heapframe { Line 380  typedef struct heapframe {
380    BOOL Xcondition;    BOOL Xcondition;
381    BOOL Xprev_is_word;    BOOL Xprev_is_word;
382    
   unsigned long int Xoriginal_ims;  
   
383  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
384    int Xprop_type;    int Xprop_type;
385    int Xprop_value;    int Xprop_value;
386    int Xprop_fail_result;    int Xprop_fail_result;
   int Xprop_category;  
   int Xprop_chartype;  
   int Xprop_script;  
387    int Xoclength;    int Xoclength;
388    uschar Xocchars[8];    uschar Xocchars[8];
389  #endif  #endif
390    
391      int Xcodelink;
392    int Xctype;    int Xctype;
393    unsigned int Xfc;    unsigned int Xfc;
394    int Xfi;    int Xfi;
# Line 363  typedef struct heapframe { Line 424  typedef struct heapframe {
424    
425  /* This function is called recursively in many circumstances. Whenever it  /* This function is called recursively in many circumstances. Whenever it
426  returns a negative (error) response, the outer incarnation must also return the  returns a negative (error) response, the outer incarnation must also return the
427  same response.  same response. */
428    
429    /* These macros pack up tests that are used for partial matching, and which
430    appears several times in the code. We set the "hit end" flag if the pointer is
431    at the end of the subject and also past the start of the subject (i.e.
432    something has been matched). For hard partial matching, we then return
433    immediately. The second one is used when we already know we are past the end of
434    the subject. */
435    
436    #define CHECK_PARTIAL()\
437      if (md->partial != 0 && eptr >= md->end_subject && \
438          eptr > md->start_used_ptr) \
439        { \
440        md->hitend = TRUE; \
441        if (md->partial > 1) MRRETURN(PCRE_ERROR_PARTIAL); \
442        }
443    
444    #define SCHECK_PARTIAL()\
445      if (md->partial != 0 && eptr > md->start_used_ptr) \
446        { \
447        md->hitend = TRUE; \
448        if (md->partial > 1) MRRETURN(PCRE_ERROR_PARTIAL); \
449        }
450    
451    
452  Performance note: It might be tempting to extract commonly used fields from the  /* Performance note: It might be tempting to extract commonly used fields from
453  md structure (e.g. utf8, end_subject) into individual variables to improve  the md structure (e.g. utf8, end_subject) into individual variables to improve
454  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
455  made performance worse.  made performance worse.
456    
# Line 375  Arguments: Line 459  Arguments:
459     ecode       pointer to current position in compiled code     ecode       pointer to current position in compiled code
460     mstart      pointer to the current match start position (can be modified     mstart      pointer to the current match start position (can be modified
461                   by encountering \K)                   by encountering \K)
462       markptr     pointer to the most recent MARK name, or NULL
463     offset_top  current top pointer     offset_top  current top pointer
464     md          pointer to "static" info for the match     md          pointer to "static" info for the match
    ims         current /i, /m, and /s options  
465     eptrb       pointer to chain of blocks containing eptr at start of     eptrb       pointer to chain of blocks containing eptr at start of
466                   brackets - for testing for empty matches                   brackets - for testing for empty matches
    flags       can contain  
                  match_condassert - this is an assertion condition  
                  match_cbegroup - this is the start of an unlimited repeat  
                    group that can match an empty string  
                  match_tail_recursed - this is a tail_recursed group  
467     rdepth      the recursion depth     rdepth      the recursion depth
468    
469  Returns:       MATCH_MATCH if matched            )  these values are >= 0  Returns:       MATCH_MATCH if matched            )  these values are >= 0
470                 MATCH_NOMATCH if failed to match  )                 MATCH_NOMATCH if failed to match  )
471                   a negative MATCH_xxx value for PRUNE, SKIP, etc
472                 a negative PCRE_ERROR_xxx value if aborted by an error condition                 a negative PCRE_ERROR_xxx value if aborted by an error condition
473                   (e.g. stopped by repeated call or recursion limit)                   (e.g. stopped by repeated call or recursion limit)
474  */  */
475    
476  static int  static int
477  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, const uschar *mstart,  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, USPTR mstart,
478    int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,    const uschar *markptr, int offset_top, match_data *md, eptrblock *eptrb,
479    int flags, unsigned int rdepth)    unsigned int rdepth)
480  {  {
481  /* 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,
482  so they can be ordinary variables in all cases. Mark some of them with  so they can be ordinary variables in all cases. Mark some of them with
# Line 408  register unsigned int c;   /* Character Line 488  register unsigned int c;   /* Character
488  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */
489    
490  BOOL minimize, possessive; /* Quantifier options */  BOOL minimize, possessive; /* Quantifier options */
491    BOOL caseless;
492    int condcode;
493    
494  /* 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
495  preserved over calls to RMATCH() are part of a "frame" which is obtained from  preserved over calls to RMATCH() are part of a "frame" which is obtained from
# Line 415  heap storage. Set up the top-level frame Line 497  heap storage. Set up the top-level frame
497  heap whenever RMATCH() does a "recursion". See the macro definitions above. */  heap whenever RMATCH() does a "recursion". See the macro definitions above. */
498    
499  #ifdef NO_RECURSE  #ifdef NO_RECURSE
500  heapframe *frame = (pcre_stack_malloc)(sizeof(heapframe));  heapframe *frame = (heapframe *)(pcre_stack_malloc)(sizeof(heapframe));
501    if (frame == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
502  frame->Xprevframe = NULL;            /* Marks the top level */  frame->Xprevframe = NULL;            /* Marks the top level */
503    
504  /* Copy in the original argument variables */  /* Copy in the original argument variables */
# Line 423  frame->Xprevframe = NULL;            /* Line 506  frame->Xprevframe = NULL;            /*
506  frame->Xeptr = eptr;  frame->Xeptr = eptr;
507  frame->Xecode = ecode;  frame->Xecode = ecode;
508  frame->Xmstart = mstart;  frame->Xmstart = mstart;
509    frame->Xmarkptr = markptr;
510  frame->Xoffset_top = offset_top;  frame->Xoffset_top = offset_top;
 frame->Xims = ims;  
511  frame->Xeptrb = eptrb;  frame->Xeptrb = eptrb;
 frame->Xflags = flags;  
512  frame->Xrdepth = rdepth;  frame->Xrdepth = rdepth;
513    
514  /* This is where control jumps back to to effect "recursion" */  /* This is where control jumps back to to effect "recursion" */
# Line 438  HEAP_RECURSE: Line 520  HEAP_RECURSE:
520  #define eptr               frame->Xeptr  #define eptr               frame->Xeptr
521  #define ecode              frame->Xecode  #define ecode              frame->Xecode
522  #define mstart             frame->Xmstart  #define mstart             frame->Xmstart
523    #define markptr            frame->Xmarkptr
524  #define offset_top         frame->Xoffset_top  #define offset_top         frame->Xoffset_top
 #define ims                frame->Xims  
525  #define eptrb              frame->Xeptrb  #define eptrb              frame->Xeptrb
 #define flags              frame->Xflags  
526  #define rdepth             frame->Xrdepth  #define rdepth             frame->Xrdepth
527    
528  /* Ditto for the local variables */  /* Ditto for the local variables */
# Line 450  HEAP_RECURSE: Line 531  HEAP_RECURSE:
531  #define charptr            frame->Xcharptr  #define charptr            frame->Xcharptr
532  #endif  #endif
533  #define callpat            frame->Xcallpat  #define callpat            frame->Xcallpat
534    #define codelink           frame->Xcodelink
535  #define data               frame->Xdata  #define data               frame->Xdata
536  #define next               frame->Xnext  #define next               frame->Xnext
537  #define pp                 frame->Xpp  #define pp                 frame->Xpp
# Line 462  HEAP_RECURSE: Line 544  HEAP_RECURSE:
544  #define condition          frame->Xcondition  #define condition          frame->Xcondition
545  #define prev_is_word       frame->Xprev_is_word  #define prev_is_word       frame->Xprev_is_word
546    
 #define original_ims       frame->Xoriginal_ims  
   
547  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
548  #define prop_type          frame->Xprop_type  #define prop_type          frame->Xprop_type
549  #define prop_value         frame->Xprop_value  #define prop_value         frame->Xprop_value
550  #define prop_fail_result   frame->Xprop_fail_result  #define prop_fail_result   frame->Xprop_fail_result
 #define prop_category      frame->Xprop_category  
 #define prop_chartype      frame->Xprop_chartype  
 #define prop_script        frame->Xprop_script  
551  #define oclength           frame->Xoclength  #define oclength           frame->Xoclength
552  #define occhars            frame->Xocchars  #define occhars            frame->Xocchars
553  #endif  #endif
# Line 500  i, and fc and c, can be the same variabl Line 577  i, and fc and c, can be the same variabl
577  #define fi i  #define fi i
578  #define fc c  #define fc c
579    
580    /* Many of the following variables are used only in small blocks of the code.
581    My normal style of coding would have declared them within each of those blocks.
582    However, in order to accommodate the version of this code that uses an external
583    "stack" implemented on the heap, it is easier to declare them all here, so the
584    declarations can be cut out in a block. The only declarations within blocks
585    below are for variables that do not have to be preserved over a recursive call
586    to RMATCH(). */
587    
588    #ifdef SUPPORT_UTF8
589    const uschar *charptr;
590    #endif
591    const uschar *callpat;
592    const uschar *data;
593    const uschar *next;
594    USPTR         pp;
595    const uschar *prev;
596    USPTR         saved_eptr;
597    
598    recursion_info new_recursive;
599    
600  #ifdef SUPPORT_UTF8                /* Many of these variables are used only  */  BOOL cur_is_word;
 const uschar *charptr;             /* in small blocks of the code. My normal */  
 #endif                             /* style of coding would have declared    */  
 const uschar *callpat;             /* them within each of those blocks.      */  
 const uschar *data;                /* However, in order to accommodate the   */  
 const uschar *next;                /* version of this code that uses an      */  
 USPTR         pp;                  /* external "stack" implemented on the    */  
 const uschar *prev;                /* heap, it is easier to declare them all */  
 USPTR         saved_eptr;          /* here, so the declarations can be cut   */  
                                    /* out in a block. The only declarations  */  
 recursion_info new_recursive;      /* within blocks below are for variables  */  
                                    /* that do not have to be preserved over  */  
 BOOL cur_is_word;                  /* a recursive call to RMATCH().          */  
601  BOOL condition;  BOOL condition;
602  BOOL prev_is_word;  BOOL prev_is_word;
603    
 unsigned long int original_ims;  
   
604  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
605  int prop_type;  int prop_type;
606  int prop_value;  int prop_value;
607  int prop_fail_result;  int prop_fail_result;
 int prop_category;  
 int prop_chartype;  
 int prop_script;  
608  int oclength;  int oclength;
609  uschar occhars[8];  uschar occhars[8];
610  #endif  #endif
611    
612    int codelink;
613  int ctype;  int ctype;
614  int length;  int length;
615  int max;  int max;
# Line 544  int stacksave[REC_STACK_SAVE_MAX]; Line 624  int stacksave[REC_STACK_SAVE_MAX];
624  eptrblock newptrb;  eptrblock newptrb;
625  #endif     /* NO_RECURSE */  #endif     /* NO_RECURSE */
626    
627    /* To save space on the stack and in the heap frame, I have doubled up on some
628    of the local variables that are used only in localised parts of the code, but
629    still need to be preserved over recursive calls of match(). These macros define
630    the alternative names that are used. */
631    
632    #define allow_zero    cur_is_word
633    #define cbegroup      condition
634    #define code_offset   codelink
635    #define condassert    condition
636    #define matched_once  prev_is_word
637    
638  /* These statements are here to stop the compiler complaining about unitialized  /* These statements are here to stop the compiler complaining about unitialized
639  variables. */  variables. */
640    
# Line 563  TAIL_RECURSE: Line 654  TAIL_RECURSE:
654  /* OK, now we can get on with the real code of the function. Recursive calls  /* OK, now we can get on with the real code of the function. Recursive calls
655  are specified by the macro RMATCH and RRETURN is used to return. When  are specified by the macro RMATCH and RRETURN is used to return. When
656  NO_RECURSE is *not* defined, these just turn into a recursive call to match()  NO_RECURSE is *not* defined, these just turn into a recursive call to match()
657  and a "return", respectively (possibly with some debugging if DEBUG is  and a "return", respectively (possibly with some debugging if PCRE_DEBUG is
658  defined). However, RMATCH isn't like a function call because it's quite a  defined). However, RMATCH isn't like a function call because it's quite a
659  complicated macro. It has to be used in one particular way. This shouldn't,  complicated macro. It has to be used in one particular way. This shouldn't,
660  however, impact performance when true recursion is being used. */  however, impact performance when true recursion is being used. */
# Line 580  haven't exceeded the recursive call limi Line 671  haven't exceeded the recursive call limi
671  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
672  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
673    
 original_ims = ims;    /* Save for resetting on ')' */  
   
674  /* At the start of a group with an unlimited repeat that may match an empty  /* At the start of a group with an unlimited repeat that may match an empty
675  string, the match_cbegroup flag is set. When this is the case, add the current  string, the variable md->match_function_type is set to MATCH_CBEGROUP. It is
676  subject pointer to the chain of such remembered pointers, to be checked when we  done this way to save having to use another function argument, which would take
677  hit the closing ket, in order to break infinite loops that match no characters.  up space on the stack. See also MATCH_CONDASSERT below.
678  When match() is called in other circumstances, don't add to the chain. If this  
679  is a tail recursion, use a block from the workspace, as the one on the stack is  When MATCH_CBEGROUP is set, add the current subject pointer to the chain of
680  already used. */  such remembered pointers, to be checked when we hit the closing ket, in order
681    to break infinite loops that match no characters. When match() is called in
682    other circumstances, don't add to the chain. The MATCH_CBEGROUP feature must
683    NOT be used with tail recursion, because the memory block that is used is on
684    the stack, so a new one may be required for each match(). */
685    
686  if ((flags & match_cbegroup) != 0)  if (md->match_function_type == MATCH_CBEGROUP)
687    {    {
688    eptrblock *p;    newptrb.epb_saved_eptr = eptr;
689    if ((flags & match_tail_recursed) != 0)    newptrb.epb_prev = eptrb;
690      {    eptrb = &newptrb;
691      if (md->eptrn >= EPTR_WORK_SIZE) RRETURN(PCRE_ERROR_NULLWSLIMIT);    md->match_function_type = 0;
     p = md->eptrchain + md->eptrn++;  
     }  
   else p = &newptrb;  
   p->epb_saved_eptr = eptr;  
   p->epb_prev = eptrb;  
   eptrb = p;  
692    }    }
693    
694  /* Now start processing the opcodes. */  /* Now start processing the opcodes. */
# Line 611  for (;;) Line 698  for (;;)
698    minimize = possessive = FALSE;    minimize = possessive = FALSE;
699    op = *ecode;    op = *ecode;
700    
   /* For partial matching, remember if we ever hit the end of the subject after  
   matching at least one subject character. */  
   
   if (md->partial &&  
       eptr >= md->end_subject &&  
       eptr > mstart)  
     md->hitend = TRUE;  
   
701    switch(op)    switch(op)
702      {      {
703      /* Handle a capturing bracket. If there is space in the offset vector, save      case OP_MARK:
704      the current subject position in the working slot at the top of the vector.      markptr = ecode + 2;
705      We mustn't change the current values of the data slot, because they may be      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
706      set from a previous iteration of this group, and be referred to by a        eptrb, RM55);
707      reference inside the group.  
708        /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
709      If the bracket fails to match, we need to restore this value and also the      argument, and we must check whether that argument matches this MARK's
710      values of the final offsets, in case they were set by a previous iteration      argument. It is passed back in md->start_match_ptr (an overloading of that
711      of the same bracket.      variable). If it does match, we reset that variable to the current subject
712        position and return MATCH_SKIP. Otherwise, pass back the return code
713        unaltered. */
714    
715        if (rrc == MATCH_SKIP_ARG &&
716            strcmp((char *)markptr, (char *)(md->start_match_ptr)) == 0)
717          {
718          md->start_match_ptr = eptr;
719          RRETURN(MATCH_SKIP);
720          }
721    
722        if (md->mark == NULL) md->mark = markptr;
723        RRETURN(rrc);
724    
725        case OP_FAIL:
726        MRRETURN(MATCH_NOMATCH);
727    
728        /* COMMIT overrides PRUNE, SKIP, and THEN */
729    
730        case OP_COMMIT:
731        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
732          eptrb, RM52);
733        if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE &&
734            rrc != MATCH_SKIP && rrc != MATCH_SKIP_ARG &&
735            rrc != MATCH_THEN)
736          RRETURN(rrc);
737        MRRETURN(MATCH_COMMIT);
738    
739        /* PRUNE overrides THEN */
740    
741        case OP_PRUNE:
742        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
743          eptrb, RM51);
744        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
745        MRRETURN(MATCH_PRUNE);
746    
747        case OP_PRUNE_ARG:
748        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
749          eptrb, RM56);
750        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
751        md->mark = ecode + 2;
752        RRETURN(MATCH_PRUNE);
753    
754        /* SKIP overrides PRUNE and THEN */
755    
756        case OP_SKIP:
757        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
758          eptrb, RM53);
759        if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
760          RRETURN(rrc);
761        md->start_match_ptr = eptr;   /* Pass back current position */
762        MRRETURN(MATCH_SKIP);
763    
764        case OP_SKIP_ARG:
765        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
766          eptrb, RM57);
767        if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
768          RRETURN(rrc);
769    
770        /* Pass back the current skip name by overloading md->start_match_ptr and
771        returning the special MATCH_SKIP_ARG return code. This will either be
772        caught by a matching MARK, or get to the top, where it is treated the same
773        as PRUNE. */
774    
775        md->start_match_ptr = ecode + 2;
776        RRETURN(MATCH_SKIP_ARG);
777    
778        /* For THEN (and THEN_ARG) we pass back the address of the bracket or
779        the alt that is at the start of the current branch. This makes it possible
780        to skip back past alternatives that precede the THEN within the current
781        branch. */
782    
783        case OP_THEN:
784        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
785          eptrb, RM54);
786        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
787        md->start_match_ptr = ecode - GET(ecode, 1);
788        MRRETURN(MATCH_THEN);
789    
790        case OP_THEN_ARG:
791        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1+LINK_SIZE],
792          offset_top, md, eptrb, RM58);
793        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
794        md->start_match_ptr = ecode - GET(ecode, 1);
795        md->mark = ecode + LINK_SIZE + 2;
796        RRETURN(MATCH_THEN);
797    
798        /* Handle a capturing bracket, other than those that are possessive with an
799        unlimited repeat. If there is space in the offset vector, save the current
800        subject position in the working slot at the top of the vector. We mustn't
801        change the current values of the data slot, because they may be set from a
802        previous iteration of this group, and be referred to by a reference inside
803        the group. A failure to match might occur after the group has succeeded,
804        if something later on doesn't match. For this reason, we need to restore
805        the working value and also the values of the final offsets, in case they
806        were set by a previous iteration of the same bracket.
807    
808      If there isn't enough space in the offset vector, treat this as if it were      If there isn't enough space in the offset vector, treat this as if it were
809      a non-capturing bracket. Don't worry about setting the flag for the error      a non-capturing bracket. Don't worry about setting the flag for the error
# Line 640  for (;;) Line 814  for (;;)
814      number = GET2(ecode, 1+LINK_SIZE);      number = GET2(ecode, 1+LINK_SIZE);
815      offset = number << 1;      offset = number << 1;
816    
817  #ifdef DEBUG  #ifdef PCRE_DEBUG
818      printf("start bracket %d\n", number);      printf("start bracket %d\n", number);
819      printf("subject=");      printf("subject=");
820      pchars(eptr, 16, TRUE, md);      pchars(eptr, 16, TRUE, md);
# Line 655  for (;;) Line 829  for (;;)
829        save_capture_last = md->capture_last;        save_capture_last = md->capture_last;
830    
831        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
832        md->offset_vector[md->offset_end - number] = eptr - md->start_subject;        md->offset_vector[md->offset_end - number] =
833            (int)(eptr - md->start_subject);
834    
835        flags = (op == OP_SCBRA)? match_cbegroup : 0;        for (;;)
       do  
836          {          {
837            if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
838          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
839            ims, eptrb, flags, RM1);            eptrb, RM1);
840          if (rrc != MATCH_NOMATCH) RRETURN(rrc);          if (rrc == MATCH_ONCE) break;  /* Backing up through an atomic group */
841            if (rrc != MATCH_NOMATCH &&
842                (rrc != MATCH_THEN || md->start_match_ptr != ecode))
843              RRETURN(rrc);
844          md->capture_last = save_capture_last;          md->capture_last = save_capture_last;
845          ecode += GET(ecode, 1);          ecode += GET(ecode, 1);
846            if (*ecode != OP_ALT) break;
847          }          }
       while (*ecode == OP_ALT);  
848    
849        DPRINTF(("bracket %d failed\n", number));        DPRINTF(("bracket %d failed\n", number));
   
850        md->offset_vector[offset] = save_offset1;        md->offset_vector[offset] = save_offset1;
851        md->offset_vector[offset+1] = save_offset2;        md->offset_vector[offset+1] = save_offset2;
852        md->offset_vector[md->offset_end - number] = save_offset3;        md->offset_vector[md->offset_end - number] = save_offset3;
853    
854        RRETURN(MATCH_NOMATCH);        /* At this point, rrc will be one of MATCH_ONCE, MATCH_NOMATCH, or
855          MATCH_THEN. */
856    
857          if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
858          RRETURN(((rrc == MATCH_ONCE)? MATCH_ONCE:MATCH_NOMATCH));
859        }        }
860    
861      /* Insufficient room for saving captured contents. Treat as a non-capturing      /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
862      bracket. */      as a non-capturing bracket. */
863    
864        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
865        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
866    
867      DPRINTF(("insufficient capture room: treat as non-capturing\n"));      DPRINTF(("insufficient capture room: treat as non-capturing\n"));
868    
869      /* Non-capturing bracket. Loop for all the alternatives. When we get to the      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
870      final alternative within the brackets, we would return the result of a      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
871      recursive call to match() whatever happened. We can reduce stack usage by  
872      turning this into a tail recursion. */      /* Non-capturing or atomic group, except for possessive with unlimited
873        repeat. Loop for all the alternatives. When we get to the final alternative
874        within the brackets, we used to return the result of a recursive call to
875        match() whatever happened so it was possible to reduce stack usage by
876        turning this into a tail recursion, except in the case of a possibly empty
877        group. However, now that there is the possiblity of (*THEN) occurring in
878        the final alternative, this optimization is no longer possible.
879    
880        MATCH_ONCE is returned when the end of an atomic group is successfully
881        reached, but subsequent matching fails. It passes back up the tree (causing
882        captured values to be reset) until the original atomic group level is
883        reached. This is tested by comparing md->once_target with the start of the
884        group. At this point, the return is converted into MATCH_NOMATCH so that
885        previous backup points can be taken. */
886    
887        case OP_ONCE:
888      case OP_BRA:      case OP_BRA:
889      case OP_SBRA:      case OP_SBRA:
890      DPRINTF(("start non-capturing bracket\n"));      DPRINTF(("start non-capturing bracket\n"));
891      flags = (op >= OP_SBRA)? match_cbegroup : 0;  
892      for (;;)      for (;;)
893        {        {
894        if (ecode[GET(ecode, 1)] != OP_ALT)        if (op >= OP_SBRA || op == OP_ONCE) md->match_function_type = MATCH_CBEGROUP;
895          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, eptrb,
896            RM2);
897          if (rrc != MATCH_NOMATCH &&
898              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
899            {
900            if (rrc == MATCH_ONCE)
901              {
902              const uschar *scode = ecode;
903              if (*scode != OP_ONCE)           /* If not at start, find it */
904                {
905                while (*scode == OP_ALT) scode += GET(scode, 1);
906                scode -= GET(scode, 1);
907                }
908              if (md->once_target == scode) rrc = MATCH_NOMATCH;
909              }
910            RRETURN(rrc);
911            }
912          ecode += GET(ecode, 1);
913          if (*ecode != OP_ALT) break;
914          }
915        if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
916        RRETURN(MATCH_NOMATCH);
917    
918        /* Handle possessive capturing brackets with an unlimited repeat. We come
919        here from BRAZERO with allow_zero set TRUE. The offset_vector values are
920        handled similarly to the normal case above. However, the matching is
921        different. The end of these brackets will always be OP_KETRPOS, which
922        returns MATCH_KETRPOS without going further in the pattern. By this means
923        we can handle the group by iteration rather than recursion, thereby
924        reducing the amount of stack needed. */
925    
926        case OP_CBRAPOS:
927        case OP_SCBRAPOS:
928        allow_zero = FALSE;
929    
930        POSSESSIVE_CAPTURE:
931        number = GET2(ecode, 1+LINK_SIZE);
932        offset = number << 1;
933    
934    #ifdef PCRE_DEBUG
935        printf("start possessive bracket %d\n", number);
936        printf("subject=");
937        pchars(eptr, 16, TRUE, md);
938        printf("\n");
939    #endif
940    
941        if (offset < md->offset_max)
942          {
943          matched_once = FALSE;
944          code_offset = ecode - md->start_code;
945    
946          save_offset1 = md->offset_vector[offset];
947          save_offset2 = md->offset_vector[offset+1];
948          save_offset3 = md->offset_vector[md->offset_end - number];
949          save_capture_last = md->capture_last;
950    
951          DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
952    
953          /* Each time round the loop, save the current subject position for use
954          when the group matches. For MATCH_MATCH, the group has matched, so we
955          restart it with a new subject starting position, remembering that we had
956          at least one match. For MATCH_NOMATCH, carry on with the alternatives, as
957          usual. If we haven't matched any alternatives in any iteration, check to
958          see if a previous iteration matched. If so, the group has matched;
959          continue from afterwards. Otherwise it has failed; restore the previous
960          capture values before returning NOMATCH. */
961    
962          for (;;)
963            {
964            md->offset_vector[md->offset_end - number] =
965              (int)(eptr - md->start_subject);
966            if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
967            RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
968              eptrb, RM63);
969            if (rrc == MATCH_KETRPOS)
970              {
971              offset_top = md->end_offset_top;
972              eptr = md->end_match_ptr;
973              ecode = md->start_code + code_offset;
974              save_capture_last = md->capture_last;
975              matched_once = TRUE;
976              continue;
977              }
978            if (rrc != MATCH_NOMATCH &&
979                (rrc != MATCH_THEN || md->start_match_ptr != ecode))
980              RRETURN(rrc);
981            md->capture_last = save_capture_last;
982            ecode += GET(ecode, 1);
983            if (*ecode != OP_ALT) break;
984            }
985    
986          if (!matched_once)
987            {
988            md->offset_vector[offset] = save_offset1;
989            md->offset_vector[offset+1] = save_offset2;
990            md->offset_vector[md->offset_end - number] = save_offset3;
991            }
992    
993          if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
994          if (allow_zero || matched_once)
995          {          {
996          ecode += _pcre_OP_lengths[*ecode];          ecode += 1 + LINK_SIZE;
997          flags |= match_tail_recursed;          break;
         DPRINTF(("bracket 0 tail recursion\n"));  
         goto TAIL_RECURSE;  
998          }          }
999    
1000        /* For non-final alternatives, continue the loop for a NOMATCH result;        RRETURN(MATCH_NOMATCH);
1001        otherwise return. */        }
1002    
1003        /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
1004        as a non-capturing bracket. */
1005    
1006        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1007          eptrb, flags, RM2);      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1008        if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
1009        DPRINTF(("insufficient capture room: treat as non-capturing\n"));
1010    
1011        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1012        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1013    
1014        /* Non-capturing possessive bracket with unlimited repeat. We come here
1015        from BRAZERO with allow_zero = TRUE. The code is similar to the above,
1016        without the capturing complication. It is written out separately for speed
1017        and cleanliness. */
1018    
1019        case OP_BRAPOS:
1020        case OP_SBRAPOS:
1021        allow_zero = FALSE;
1022    
1023        POSSESSIVE_NON_CAPTURE:
1024        matched_once = FALSE;
1025        code_offset = ecode - md->start_code;
1026    
1027        for (;;)
1028          {
1029          if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1030          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
1031            eptrb, RM48);
1032          if (rrc == MATCH_KETRPOS)
1033            {
1034            offset_top = md->end_offset_top;
1035            eptr = md->end_match_ptr;
1036            ecode = md->start_code + code_offset;
1037            matched_once = TRUE;
1038            continue;
1039            }
1040          if (rrc != MATCH_NOMATCH &&
1041              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1042            RRETURN(rrc);
1043        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1044          if (*ecode != OP_ALT) break;
1045          }
1046    
1047        if (matched_once || allow_zero)
1048          {
1049          ecode += 1 + LINK_SIZE;
1050          break;
1051        }        }
1052        RRETURN(MATCH_NOMATCH);
1053    
1054      /* Control never reaches here. */      /* Control never reaches here. */
1055    
1056      /* Conditional group: compilation checked that there are no more than      /* Conditional group: compilation checked that there are no more than
1057      two branches. If the condition is false, skipping the first branch takes us      two branches. If the condition is false, skipping the first branch takes us
1058      past the end if there is only one branch, but that's OK because that is      past the end if there is only one branch, but that's OK because that is
1059      exactly what going to the ket would do. As there is only one branch to be      exactly what going to the ket would do. */
     obeyed, we can use tail recursion to avoid using another stack frame. */  
1060    
1061      case OP_COND:      case OP_COND:
1062      case OP_SCOND:      case OP_SCOND:
1063      if (ecode[LINK_SIZE+1] == OP_RREF)         /* Recursion test */      codelink = GET(ecode, 1);
1064    
1065        /* Because of the way auto-callout works during compile, a callout item is
1066        inserted between OP_COND and an assertion condition. */
1067    
1068        if (ecode[LINK_SIZE+1] == OP_CALLOUT)
1069        {        {
1070        offset = GET2(ecode, LINK_SIZE + 2);     /* Recursion group number*/        if (pcre_callout != NULL)
1071        condition = md->recursive != NULL &&          {
1072          (offset == RREF_ANY || offset == md->recursive->group_num);          pcre_callout_block cb;
1073        ecode += condition? 3 : GET(ecode, 1);          cb.version          = 2;   /* Version 1 of the callout block */
1074            cb.callout_number   = ecode[LINK_SIZE+2];
1075            cb.offset_vector    = md->offset_vector;
1076            cb.subject          = (PCRE_SPTR)md->start_subject;
1077            cb.subject_length   = (int)(md->end_subject - md->start_subject);
1078            cb.start_match      = (int)(mstart - md->start_subject);
1079            cb.current_position = (int)(eptr - md->start_subject);
1080            cb.pattern_position = GET(ecode, LINK_SIZE + 3);
1081            cb.next_item_length = GET(ecode, 3 + 2*LINK_SIZE);
1082            cb.capture_top      = offset_top/2;
1083            cb.capture_last     = md->capture_last;
1084            cb.callout_data     = md->callout_data;
1085            cb.mark             = markptr;
1086            if ((rrc = (*pcre_callout)(&cb)) > 0) MRRETURN(MATCH_NOMATCH);
1087            if (rrc < 0) RRETURN(rrc);
1088            }
1089          ecode += _pcre_OP_lengths[OP_CALLOUT];
1090          }
1091    
1092        condcode = ecode[LINK_SIZE+1];
1093    
1094        /* Now see what the actual condition is */
1095    
1096        if (condcode == OP_RREF || condcode == OP_NRREF)    /* Recursion test */
1097          {
1098          if (md->recursive == NULL)                /* Not recursing => FALSE */
1099            {
1100            condition = FALSE;
1101            ecode += GET(ecode, 1);
1102            }
1103          else
1104            {
1105            int recno = GET2(ecode, LINK_SIZE + 2);   /* Recursion group number*/
1106            condition =  (recno == RREF_ANY || recno == md->recursive->group_num);
1107    
1108            /* If the test is for recursion into a specific subpattern, and it is
1109            false, but the test was set up by name, scan the table to see if the
1110            name refers to any other numbers, and test them. The condition is true
1111            if any one is set. */
1112    
1113            if (!condition && condcode == OP_NRREF && recno != RREF_ANY)
1114              {
1115              uschar *slotA = md->name_table;
1116              for (i = 0; i < md->name_count; i++)
1117                {
1118                if (GET2(slotA, 0) == recno) break;
1119                slotA += md->name_entry_size;
1120                }
1121    
1122              /* Found a name for the number - there can be only one; duplicate
1123              names for different numbers are allowed, but not vice versa. First
1124              scan down for duplicates. */
1125    
1126              if (i < md->name_count)
1127                {
1128                uschar *slotB = slotA;
1129                while (slotB > md->name_table)
1130                  {
1131                  slotB -= md->name_entry_size;
1132                  if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1133                    {
1134                    condition = GET2(slotB, 0) == md->recursive->group_num;
1135                    if (condition) break;
1136                    }
1137                  else break;
1138                  }
1139    
1140                /* Scan up for duplicates */
1141    
1142                if (!condition)
1143                  {
1144                  slotB = slotA;
1145                  for (i++; i < md->name_count; i++)
1146                    {
1147                    slotB += md->name_entry_size;
1148                    if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1149                      {
1150                      condition = GET2(slotB, 0) == md->recursive->group_num;
1151                      if (condition) break;
1152                      }
1153                    else break;
1154                    }
1155                  }
1156                }
1157              }
1158    
1159            /* Chose branch according to the condition */
1160    
1161            ecode += condition? 3 : GET(ecode, 1);
1162            }
1163        }        }
1164    
1165      else if (ecode[LINK_SIZE+1] == OP_CREF)    /* Group used test */      else if (condcode == OP_CREF || condcode == OP_NCREF)  /* Group used test */
1166        {        {
1167        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */
1168        condition = offset < offset_top && md->offset_vector[offset] >= 0;        condition = offset < offset_top && md->offset_vector[offset] >= 0;
1169    
1170          /* If the numbered capture is unset, but the reference was by name,
1171          scan the table to see if the name refers to any other numbers, and test
1172          them. The condition is true if any one is set. This is tediously similar
1173          to the code above, but not close enough to try to amalgamate. */
1174    
1175          if (!condition && condcode == OP_NCREF)
1176            {
1177            int refno = offset >> 1;
1178            uschar *slotA = md->name_table;
1179    
1180            for (i = 0; i < md->name_count; i++)
1181              {
1182              if (GET2(slotA, 0) == refno) break;
1183              slotA += md->name_entry_size;
1184              }
1185    
1186            /* Found a name for the number - there can be only one; duplicate names
1187            for different numbers are allowed, but not vice versa. First scan down
1188            for duplicates. */
1189    
1190            if (i < md->name_count)
1191              {
1192              uschar *slotB = slotA;
1193              while (slotB > md->name_table)
1194                {
1195                slotB -= md->name_entry_size;
1196                if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1197                  {
1198                  offset = GET2(slotB, 0) << 1;
1199                  condition = offset < offset_top &&
1200                    md->offset_vector[offset] >= 0;
1201                  if (condition) break;
1202                  }
1203                else break;
1204                }
1205    
1206              /* Scan up for duplicates */
1207    
1208              if (!condition)
1209                {
1210                slotB = slotA;
1211                for (i++; i < md->name_count; i++)
1212                  {
1213                  slotB += md->name_entry_size;
1214                  if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1215                    {
1216                    offset = GET2(slotB, 0) << 1;
1217                    condition = offset < offset_top &&
1218                      md->offset_vector[offset] >= 0;
1219                    if (condition) break;
1220                    }
1221                  else break;
1222                  }
1223                }
1224              }
1225            }
1226    
1227          /* Chose branch according to the condition */
1228    
1229        ecode += condition? 3 : GET(ecode, 1);        ecode += condition? 3 : GET(ecode, 1);
1230        }        }
1231    
1232      else if (ecode[LINK_SIZE+1] == OP_DEF)     /* DEFINE - always false */      else if (condcode == OP_DEF)     /* DEFINE - always false */
1233        {        {
1234        condition = FALSE;        condition = FALSE;
1235        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1236        }        }
1237    
1238      /* The condition is an assertion. Call match() to evaluate it - setting      /* The condition is an assertion. Call match() to evaluate it - setting
1239      the final argument match_condassert causes it to stop at the end of an      md->match_function_type to MATCH_CONDASSERT causes it to stop at the end of
1240      assertion. */      an assertion. */
1241    
1242      else      else
1243        {        {
1244        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        md->match_function_type = MATCH_CONDASSERT;
1245            match_condassert, RM3);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM3);
1246        if (rrc == MATCH_MATCH)        if (rrc == MATCH_MATCH)
1247          {          {
1248            if (md->end_offset_top > offset_top)
1249              offset_top = md->end_offset_top;  /* Captures may have happened */
1250          condition = TRUE;          condition = TRUE;
1251          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
1252          while (*ecode == OP_ALT) ecode += GET(ecode, 1);          while (*ecode == OP_ALT) ecode += GET(ecode, 1);
1253          }          }
1254        else if (rrc != MATCH_NOMATCH)        else if (rrc != MATCH_NOMATCH &&
1255                  (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1256          {          {
1257          RRETURN(rrc);         /* Need braces because of following else */          RRETURN(rrc);         /* Need braces because of following else */
1258          }          }
1259        else        else
1260          {          {
1261          condition = FALSE;          condition = FALSE;
1262          ecode += GET(ecode, 1);          ecode += codelink;
1263          }          }
1264        }        }
1265    
1266      /* We are now at the branch that is to be obeyed. As there is only one,      /* We are now at the branch that is to be obeyed. As there is only one,
1267      we can use tail recursion to avoid using another stack frame. If the second      we used to use tail recursion to avoid using another stack frame, except
1268      alternative doesn't exist, we can just plough on. */      when there was unlimited repeat of a possibly empty group. However, that
1269        strategy no longer works because of the possibilty of (*THEN) being
1270        encountered in the branch. A recursive call to match() is always required,
1271        unless the second alternative doesn't exist, in which case we can just
1272        plough on. */
1273    
1274      if (condition || *ecode == OP_ALT)      if (condition || *ecode == OP_ALT)
1275        {        {
1276        ecode += 1 + LINK_SIZE;        if (op == OP_SCOND) md->match_function_type = MATCH_CBEGROUP;
1277        flags = match_tail_recursed | ((op == OP_SCOND)? match_cbegroup : 0);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM49);
1278        goto TAIL_RECURSE;        if (rrc == MATCH_THEN && md->start_match_ptr == ecode)
1279            rrc = MATCH_NOMATCH;
1280          RRETURN(rrc);
1281        }        }
1282      else      else                         /* Condition false & no alternative */
1283        {        {
1284        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1285        }        }
1286      break;      break;
1287    
1288    
1289      /* End of the pattern. If we are in a top-level recursion, we should      /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
1290      restore the offsets appropriately and continue from after the call. */      to close any currently open capturing brackets. */
1291    
1292      case OP_END:      case OP_CLOSE:
1293      if (md->recursive != NULL && md->recursive->group_num == 0)      number = GET2(ecode, 1);
1294        offset = number << 1;
1295    
1296    #ifdef PCRE_DEBUG
1297          printf("end bracket %d at *ACCEPT", number);
1298          printf("\n");
1299    #endif
1300    
1301        md->capture_last = number;
1302        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1303        {        {
1304        recursion_info *rec = md->recursive;        md->offset_vector[offset] =
1305        DPRINTF(("End of pattern in a (?0) recursion\n"));          md->offset_vector[md->offset_end - number];
1306        md->recursive = rec->prevrec;        md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1307        memmove(md->offset_vector, rec->offset_save,        if (offset_top <= offset) offset_top = offset + 2;
         rec->saved_max * sizeof(int));  
       mstart = rec->save_start;  
       ims = original_ims;  
       ecode = rec->after_call;  
       break;  
1308        }        }
1309        ecode += 3;
1310        break;
1311    
1312    
1313        /* End of the pattern, either real or forced. */
1314    
1315        case OP_END:
1316        case OP_ACCEPT:
1317        case OP_ASSERT_ACCEPT:
1318    
1319        /* If we have matched an empty string, fail if not in an assertion and not
1320        in a recursion if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART
1321        is set and we have matched at the start of the subject. In both cases,
1322        backtracking will then try other alternatives, if any. */
1323    
1324        if (eptr == mstart && op != OP_ASSERT_ACCEPT &&
1325             md->recursive == NULL &&
1326             (md->notempty ||
1327               (md->notempty_atstart &&
1328                 mstart == md->start_subject + md->start_offset)))
1329          MRRETURN(MATCH_NOMATCH);
1330    
1331      /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty      /* Otherwise, we have a match. */
     string - backtracking will then try other alternatives, if any. */  
1332    
     if (md->notempty && eptr == mstart) RRETURN(MATCH_NOMATCH);  
1333      md->end_match_ptr = eptr;           /* Record where we ended */      md->end_match_ptr = eptr;           /* Record where we ended */
1334      md->end_offset_top = offset_top;    /* and how many extracts were taken */      md->end_offset_top = offset_top;    /* and how many extracts were taken */
1335      md->start_match_ptr = mstart;  /* and the start (\K can modify) */      md->start_match_ptr = mstart;       /* and the start (\K can modify) */
     RRETURN(MATCH_MATCH);  
1336    
1337      /* Change option settings */      /* For some reason, the macros don't work properly if an expression is
1338        given as the argument to MRRETURN when the heap is in use. */
1339    
1340      case OP_OPT:      rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
1341      ims = ecode[1];      MRRETURN(rrc);
     ecode += 2;  
     DPRINTF(("ims set to %02lx\n", ims));  
     break;  
1342    
1343      /* Assertion brackets. Check the alternative branches in turn - the      /* Assertion brackets. Check the alternative branches in turn - the
1344      matching won't pass the KET for an assertion. If any one branch matches,      matching won't pass the KET for an assertion. If any one branch matches,
1345      the assertion is true. Lookbehind assertions have an OP_REVERSE item at the      the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
1346      start of each branch to move the current point backwards, so the code at      start of each branch to move the current point backwards, so the code at
1347      this level is identical to the lookahead case. */      this level is identical to the lookahead case. When the assertion is part
1348        of a condition, we want to return immediately afterwards. The caller of
1349        this incarnation of the match() function will have set MATCH_CONDASSERT in
1350        md->match_function type, and one of these opcodes will be the first opcode
1351        that is processed. We use a local variable that is preserved over calls to
1352        match() to remember this case. */
1353    
1354      case OP_ASSERT:      case OP_ASSERT:
1355      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1356        if (md->match_function_type == MATCH_CONDASSERT)
1357          {
1358          condassert = TRUE;
1359          md->match_function_type = 0;
1360          }
1361        else condassert = FALSE;
1362    
1363      do      do
1364        {        {
1365        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
1366          RM4);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1367        if (rrc == MATCH_MATCH) break;          {
1368        if (rrc != MATCH_NOMATCH) RRETURN(rrc);          mstart = md->start_match_ptr;   /* In case \K reset it */
1369            markptr = md->mark;
1370            break;
1371            }
1372          if (rrc != MATCH_NOMATCH &&
1373              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1374            RRETURN(rrc);
1375        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1376        }        }
1377      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1378      if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);  
1379        if (*ecode == OP_KET) MRRETURN(MATCH_NOMATCH);
1380    
1381      /* If checking an assertion for a condition, return MATCH_MATCH. */      /* If checking an assertion for a condition, return MATCH_MATCH. */
1382    
1383      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);
1384    
1385      /* Continue from after the assertion, updating the offsets high water      /* Continue from after the assertion, updating the offsets high water
1386      mark, since extracts may have been taken during the assertion. */      mark, since extracts may have been taken during the assertion. */
# Line 847  for (;;) Line 1390  for (;;)
1390      offset_top = md->end_offset_top;      offset_top = md->end_offset_top;
1391      continue;      continue;
1392    
1393      /* Negative assertion: all branches must fail to match */      /* Negative assertion: all branches must fail to match. Encountering SKIP,
1394        PRUNE, or COMMIT means we must assume failure without checking subsequent
1395        branches. */
1396    
1397      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1398      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1399        if (md->match_function_type == MATCH_CONDASSERT)
1400          {
1401          condassert = TRUE;
1402          md->match_function_type = 0;
1403          }
1404        else condassert = FALSE;
1405    
1406      do      do
1407        {        {
1408        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
1409          RM5);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) MRRETURN(MATCH_NOMATCH);
1410        if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);        if (rrc == MATCH_SKIP || rrc == MATCH_PRUNE || rrc == MATCH_COMMIT)
1411        if (rrc != MATCH_NOMATCH) RRETURN(rrc);          {
1412            do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1413            break;
1414            }
1415          if (rrc != MATCH_NOMATCH &&
1416              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1417            RRETURN(rrc);
1418        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1419        }        }
1420      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1421    
1422      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);  /* Condition assertion */
1423    
1424      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1425      continue;      continue;
# Line 879  for (;;) Line 1437  for (;;)
1437        while (i-- > 0)        while (i-- > 0)
1438          {          {
1439          eptr--;          eptr--;
1440          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);          if (eptr < md->start_subject) MRRETURN(MATCH_NOMATCH);
1441          BACKCHAR(eptr)          BACKCHAR(eptr);
1442          }          }
1443        }        }
1444      else      else
# Line 890  for (;;) Line 1448  for (;;)
1448    
1449        {        {
1450        eptr -= GET(ecode, 1);        eptr -= GET(ecode, 1);
1451        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);        if (eptr < md->start_subject) MRRETURN(MATCH_NOMATCH);
1452        }        }
1453    
1454      /* Skip to next op code */      /* Save the earliest consulted character, then skip to next op code */
1455    
1456        if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
1457      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1458      break;      break;
1459    
# Line 906  for (;;) Line 1465  for (;;)
1465      if (pcre_callout != NULL)      if (pcre_callout != NULL)
1466        {        {
1467        pcre_callout_block cb;        pcre_callout_block cb;
1468        cb.version          = 1;   /* Version 1 of the callout block */        cb.version          = 2;   /* Version 1 of the callout block */
1469        cb.callout_number   = ecode[1];        cb.callout_number   = ecode[1];
1470        cb.offset_vector    = md->offset_vector;        cb.offset_vector    = md->offset_vector;
1471        cb.subject          = (PCRE_SPTR)md->start_subject;        cb.subject          = (PCRE_SPTR)md->start_subject;
1472        cb.subject_length   = md->end_subject - md->start_subject;        cb.subject_length   = (int)(md->end_subject - md->start_subject);
1473        cb.start_match      = mstart - md->start_subject;        cb.start_match      = (int)(mstart - md->start_subject);
1474        cb.current_position = eptr - md->start_subject;        cb.current_position = (int)(eptr - md->start_subject);
1475        cb.pattern_position = GET(ecode, 2);        cb.pattern_position = GET(ecode, 2);
1476        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
1477        cb.capture_top      = offset_top/2;        cb.capture_top      = offset_top/2;
1478        cb.capture_last     = md->capture_last;        cb.capture_last     = md->capture_last;
1479        cb.callout_data     = md->callout_data;        cb.callout_data     = md->callout_data;
1480        if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);        cb.mark             = markptr;
1481          if ((rrc = (*pcre_callout)(&cb)) > 0) MRRETURN(MATCH_NOMATCH);
1482        if (rrc < 0) RRETURN(rrc);        if (rrc < 0) RRETURN(rrc);
1483        }        }
1484      ecode += 2 + 2*LINK_SIZE;      ecode += 2 + 2*LINK_SIZE;
# Line 928  for (;;) Line 1488  for (;;)
1488      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
1489      whole pattern. (This is so that it works from duplicated subpatterns.)      whole pattern. (This is so that it works from duplicated subpatterns.)
1490    
1491      If there are any capturing brackets started but not finished, we have to      The state of the capturing groups is preserved over recursion, and
1492      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
1493      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
1494      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
1495      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
1496      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
1497      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.  
1498    
1499      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
1500      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
1501      for the original version of this logic. */      for the original version of this logic. It has, however, been hacked around
1502        a lot, so he is not to blame for the current way it works. */
1503    
1504      case OP_RECURSE:      case OP_RECURSE:
1505        {        {
1506          recursion_info *ri;
1507          int recno;
1508    
1509        callpat = md->start_code + GET(ecode, 1);        callpat = md->start_code + GET(ecode, 1);
1510        new_recursive.group_num = (callpat == md->start_code)? 0 :        recno = (callpat == md->start_code)? 0 :
1511          GET2(callpat, 1 + LINK_SIZE);          GET2(callpat, 1 + LINK_SIZE);
1512    
1513          /* Check for repeating a recursion without advancing the subject pointer.
1514          This should catch convoluted mutual recursions. (Some simple cases are
1515          caught at compile time.) */
1516    
1517          for (ri = md->recursive; ri != NULL; ri = ri->prevrec)
1518            if (recno == ri->group_num && eptr == ri->subject_position)
1519              RRETURN(PCRE_ERROR_RECURSELOOP);
1520    
1521        /* Add to "recursing stack" */        /* Add to "recursing stack" */
1522    
1523          new_recursive.group_num = recno;
1524          new_recursive.subject_position = eptr;
1525        new_recursive.prevrec = md->recursive;        new_recursive.prevrec = md->recursive;
1526        md->recursive = &new_recursive;        md->recursive = &new_recursive;
1527    
1528        /* Find where to continue from afterwards */        /* Where to continue from afterwards */
1529    
1530        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       new_recursive.after_call = ecode;  
1531    
1532        /* Now save the offset data. */        /* Now save the offset data */
1533    
1534        new_recursive.saved_max = md->offset_end;        new_recursive.saved_max = md->offset_end;
1535        if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)        if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)
# Line 970  for (;;) Line 1540  for (;;)
1540            (int *)(pcre_malloc)(new_recursive.saved_max * sizeof(int));            (int *)(pcre_malloc)(new_recursive.saved_max * sizeof(int));
1541          if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);          if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
1542          }          }
   
1543        memcpy(new_recursive.offset_save, md->offset_vector,        memcpy(new_recursive.offset_save, md->offset_vector,
1544              new_recursive.saved_max * sizeof(int));              new_recursive.saved_max * sizeof(int));
       new_recursive.save_start = mstart;  
       mstart = eptr;  
1545    
1546        /* OK, now we can do the recursion. For each top-level alternative we        /* OK, now we can do the recursion. After processing each alternative,
1547        restore the offset and recursion data. */        restore the offset data. If there were nested recursions, md->recursive
1548          might be changed, so reset it before looping. */
1549    
1550        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1551        flags = (*callpat >= OP_SBRA)? match_cbegroup : 0;        cbegroup = (*callpat >= OP_SBRA);
1552        do        do
1553          {          {
1554            if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
1555          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,
1556            md, ims, eptrb, flags, RM6);            md, eptrb, RM6);
1557          if (rrc == MATCH_MATCH)          memcpy(md->offset_vector, new_recursive.offset_save,
1558                new_recursive.saved_max * sizeof(int));
1559            if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1560            {            {
1561            DPRINTF(("Recursion matched\n"));            DPRINTF(("Recursion matched\n"));
1562            md->recursive = new_recursive.prevrec;            md->recursive = new_recursive.prevrec;
1563            if (new_recursive.offset_save != stacksave)            if (new_recursive.offset_save != stacksave)
1564              (pcre_free)(new_recursive.offset_save);              (pcre_free)(new_recursive.offset_save);
1565            RRETURN(MATCH_MATCH);  
1566              /* Set where we got to in the subject, and reset the start in case
1567              it was changed by \K. This *is* propagated back out of a recursion,
1568              for Perl compatibility. */
1569    
1570              eptr = md->end_match_ptr;
1571              mstart = md->start_match_ptr;
1572              goto RECURSION_MATCHED;        /* Exit loop; end processing */
1573            }            }
1574          else if (rrc != MATCH_NOMATCH)          else if (rrc != MATCH_NOMATCH &&
1575                    (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1576            {            {
1577            DPRINTF(("Recursion gave error %d\n", rrc));            DPRINTF(("Recursion gave error %d\n", rrc));
1578              if (new_recursive.offset_save != stacksave)
1579                (pcre_free)(new_recursive.offset_save);
1580            RRETURN(rrc);            RRETURN(rrc);
1581            }            }
1582    
1583          md->recursive = &new_recursive;          md->recursive = &new_recursive;
         memcpy(md->offset_vector, new_recursive.offset_save,  
             new_recursive.saved_max * sizeof(int));  
1584          callpat += GET(callpat, 1);          callpat += GET(callpat, 1);
1585          }          }
1586        while (*callpat == OP_ALT);        while (*callpat == OP_ALT);
# Line 1010  for (;;) Line 1589  for (;;)
1589        md->recursive = new_recursive.prevrec;        md->recursive = new_recursive.prevrec;
1590        if (new_recursive.offset_save != stacksave)        if (new_recursive.offset_save != stacksave)
1591          (pcre_free)(new_recursive.offset_save);          (pcre_free)(new_recursive.offset_save);
1592        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
1593        }        }
     /* Control never reaches here */  
1594    
1595      /* "Once" brackets are like assertion brackets except that after a match,      RECURSION_MATCHED:
1596      the point in the subject string is not moved back. Thus there can never be      break;
1597      a move back into the brackets. Friedl calls these "atomic" subpatterns.  
1598      Check the alternative branches in turn - the matching won't pass the KET      /* An alternation is the end of a branch; scan along to find the end of the
1599      for this kind of subpattern. If any one branch matches, we carry on as at      bracketed group and go to there. */
     the end of a normal bracket, leaving the subject pointer. */  
1600    
1601      case OP_ONCE:      case OP_ALT:
1602      prev = ecode;      do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1603      saved_eptr = eptr;      break;
1604    
1605      do      /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
1606        {      indicating that it may occur zero times. It may repeat infinitely, or not
1607        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,      at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets
1608          eptrb, 0, RM7);      with fixed upper repeat limits are compiled as a number of copies, with the
1609        if (rrc == MATCH_MATCH) break;      optional ones preceded by BRAZERO or BRAMINZERO. */
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       ecode += GET(ecode,1);  
       }  
     while (*ecode == OP_ALT);  
1610    
1611      /* If hit the end of the group (which could be repeated), fail */      case OP_BRAZERO:
1612        next = ecode + 1;
1613        RMATCH(eptr, next, offset_top, md, eptrb, RM10);
1614        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1615        do next += GET(next, 1); while (*next == OP_ALT);
1616        ecode = next + 1 + LINK_SIZE;
1617        break;
1618    
1619      if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);      case OP_BRAMINZERO:
1620        next = ecode + 1;
1621        do next += GET(next, 1); while (*next == OP_ALT);
1622        RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
1623        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1624        ecode++;
1625        break;
1626    
1627      /* Continue as from after the assertion, updating the offsets high water      case OP_SKIPZERO:
1628      mark, since extracts may have been taken. */      next = ecode+1;
1629        do next += GET(next,1); while (*next == OP_ALT);
1630        ecode = next + 1 + LINK_SIZE;
1631        break;
1632    
1633      do ecode += GET(ecode, 1); while (*ecode == OP_ALT);      /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
1634        here; just jump to the group, with allow_zero set TRUE. */
1635    
1636      offset_top = md->end_offset_top;      case OP_BRAPOSZERO:
1637      eptr = md->end_match_ptr;      op = *(++ecode);
1638        allow_zero = TRUE;
1639        if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
1640          goto POSSESSIVE_NON_CAPTURE;
1641    
1642      /* For a non-repeating ket, just continue at this level. This also      /* End of a group, repeated or non-repeating. */
     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. */  
1643    
1644      if (*ecode == OP_KET || eptr == saved_eptr)      case OP_KET:
1645        {      case OP_KETRMIN:
1646        ecode += 1+LINK_SIZE;      case OP_KETRMAX:
1647        break;      case OP_KETRPOS:
1648        }      prev = ecode - GET(ecode, 1);
1649    
1650      /* The repeating kets try the rest of the pattern or restart from the      /* If this was a group that remembered the subject start, in order to break
1651      preceding bracket, in the appropriate order. The second "call" of match()      infinite repeats of empty string matches, retrieve the subject start from
1652      uses tail recursion, to avoid using another stack frame. We need to reset      the chain. Otherwise, set it NULL. */
     any options that changed within the bracket before re-running it, so  
     check the next opcode. */  
1653    
1654      if (ecode[1+LINK_SIZE] == OP_OPT)      if (*prev >= OP_SBRA || *prev == OP_ONCE)
       {  
       ims = (ims & ~PCRE_IMS) | ecode[4];  
       DPRINTF(("ims set to %02lx at group repeat\n", ims));  
       }  
   
     if (*ecode == OP_KETRMIN)  
       {  
       RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0,  
         RM8);  
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       ecode = prev;  
       flags = match_tail_recursed;  
       goto TAIL_RECURSE;  
       }  
     else  /* OP_KETRMAX */  
       {  
       RMATCH(eptr, prev, offset_top, md, ims, eptrb, match_cbegroup, RM9);  
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       ecode += 1 + LINK_SIZE;  
       flags = match_tail_recursed;  
       goto TAIL_RECURSE;  
       }  
     /* Control never gets here */  
   
     /* An alternation is the end of a branch; scan along to find the end of the  
     bracketed group and go to there. */  
   
     case OP_ALT:  
     do ecode += GET(ecode,1); while (*ecode == OP_ALT);  
     break;  
   
     /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating  
     that it may occur zero times. It may repeat infinitely, or not at all -  
     i.e. it could be ()* or ()? in the pattern. Brackets with fixed upper  
     repeat limits are compiled as a number of copies, with the optional ones  
     preceded by BRAZERO or BRAMINZERO. */  
   
     case OP_BRAZERO:  
       {  
       next = ecode+1;  
       RMATCH(eptr, next, offset_top, md, ims, eptrb, 0, RM10);  
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       do next += GET(next,1); while (*next == OP_ALT);  
       ecode = next + 1 + LINK_SIZE;  
       }  
     break;  
   
     case OP_BRAMINZERO:  
       {  
       next = ecode+1;  
       do next += GET(next, 1); while (*next == OP_ALT);  
       RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0, RM11);  
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       ecode++;  
       }  
     break;  
   
     /* End of a group, repeated or non-repeating. */  
   
     case OP_KET:  
     case OP_KETRMIN:  
     case OP_KETRMAX:  
     prev = ecode - GET(ecode, 1);  
   
     /* If this was a group that remembered the subject start, in order to break  
     infinite repeats of empty string matches, retrieve the subject start from  
     the chain. Otherwise, set it NULL. */  
   
     if (*prev >= OP_SBRA)  
1655        {        {
1656        saved_eptr = eptrb->epb_saved_eptr;   /* Value at start of group */        saved_eptr = eptrb->epb_saved_eptr;   /* Value at start of group */
1657        eptrb = eptrb->epb_prev;              /* Backup to previous group */        eptrb = eptrb->epb_prev;              /* Backup to previous group */
# Line 1143  for (;;) Line 1660  for (;;)
1660    
1661      /* If we are at the end of an assertion group, stop matching and return      /* If we are at the end of an assertion group, stop matching and return
1662      MATCH_MATCH, but record the current high water mark for use by positive      MATCH_MATCH, but record the current high water mark for use by positive
1663      assertions. Do this also for the "once" (atomic) groups. */      assertions. We also need to record the match start in case it was changed
1664        by \K. */
1665    
1666      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
1667          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT)
         *prev == OP_ONCE)  
1668        {        {
1669        md->end_match_ptr = eptr;      /* For ONCE */        md->end_match_ptr = eptr;      /* For ONCE */
1670        md->end_offset_top = offset_top;        md->end_offset_top = offset_top;
1671        RRETURN(MATCH_MATCH);        md->start_match_ptr = mstart;
1672          MRRETURN(MATCH_MATCH);         /* Sets md->mark */
1673        }        }
1674    
1675      /* For capturing groups we have to check the group number back at the start      /* For capturing groups we have to check the group number back at the start
1676      and if necessary complete handling an extraction by setting the offsets and      and if necessary complete handling an extraction by setting the offsets and
1677      bumping the high water mark. Note that whole-pattern recursion is coded as      bumping the high water mark. Whole-pattern recursion is coded as a recurse
1678      a recurse into group 0, so it won't be picked up here. Instead, we catch it      into group 0, so it won't be picked up here. Instead, we catch it when the
1679      when the OP_END is reached. Other recursion is handled here. */      OP_END is reached. Other recursion is handled here. We just have to record
1680        the current subject position and start match pointer and give a MATCH
1681        return. */
1682    
1683      if (*prev == OP_CBRA || *prev == OP_SCBRA)      if (*prev == OP_CBRA || *prev == OP_SCBRA ||
1684            *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
1685        {        {
1686        number = GET2(prev, 1+LINK_SIZE);        number = GET2(prev, 1+LINK_SIZE);
1687        offset = number << 1;        offset = number << 1;
1688    
1689  #ifdef DEBUG  #ifdef PCRE_DEBUG
1690        printf("end bracket %d", number);        printf("end bracket %d", number);
1691        printf("\n");        printf("\n");
1692  #endif  #endif
1693    
1694          /* Handle a recursively called group. */
1695    
1696          if (md->recursive != NULL && md->recursive->group_num == number)
1697            {
1698            md->end_match_ptr = eptr;
1699            md->start_match_ptr = mstart;
1700            RRETURN(MATCH_MATCH);
1701            }
1702    
1703          /* Deal with capturing */
1704    
1705        md->capture_last = number;        md->capture_last = number;
1706        if (offset >= md->offset_max) md->offset_overflow = TRUE; else        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1707          {          {
1708            /* If offset is greater than offset_top, it means that we are
1709            "skipping" a capturing group, and that group's offsets must be marked
1710            unset. In earlier versions of PCRE, all the offsets were unset at the
1711            start of matching, but this doesn't work because atomic groups and
1712            assertions can cause a value to be set that should later be unset.
1713            Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as
1714            part of the atomic group, but this is not on the final matching path,
1715            so must be unset when 2 is set. (If there is no group 2, there is no
1716            problem, because offset_top will then be 2, indicating no capture.) */
1717    
1718            if (offset > offset_top)
1719              {
1720              register int *iptr = md->offset_vector + offset_top;
1721              register int *iend = md->offset_vector + offset;
1722              while (iptr < iend) *iptr++ = -1;
1723              }
1724    
1725            /* Now make the extraction */
1726    
1727          md->offset_vector[offset] =          md->offset_vector[offset] =
1728            md->offset_vector[md->offset_end - number];            md->offset_vector[md->offset_end - number];
1729          md->offset_vector[offset+1] = eptr - md->start_subject;          md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1730          if (offset_top <= offset) offset_top = offset + 2;          if (offset_top <= offset) offset_top = offset + 2;
1731          }          }
1732          }
1733    
1734        /* Handle a recursively called group. Restore the offsets      /* For an ordinary non-repeating ket, just continue at this level. This
1735        appropriately and continue from after the call. */      also happens for a repeating ket if no characters were matched in the
1736        group. This is the forcible breaking of infinite loops as implemented in
1737        Perl 5.005. For a non-repeating atomic group, establish a backup point by
1738        processing the rest of the pattern at a lower level. If this results in a
1739        NOMATCH return, pass MATCH_ONCE back to the original OP_ONCE level, thereby
1740        bypassing intermediate backup points, but resetting any captures that
1741        happened along the way. */
1742    
1743        if (md->recursive != NULL && md->recursive->group_num == number)      if (*ecode == OP_KET || eptr == saved_eptr)
1744          {
1745          if (*prev == OP_ONCE)
1746          {          {
1747          recursion_info *rec = md->recursive;          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
1748          DPRINTF(("Recursion (%d) succeeded - continuing\n", number));          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1749          md->recursive = rec->prevrec;          md->once_target = prev;  /* Level at which to change to MATCH_NOMATCH */
1750          mstart = rec->save_start;          RRETURN(MATCH_ONCE);
         memcpy(md->offset_vector, rec->offset_save,  
           rec->saved_max * sizeof(int));  
         ecode = rec->after_call;  
         ims = original_ims;  
         break;  
1751          }          }
1752          ecode += 1 + LINK_SIZE;    /* Carry on at this level */
1753          break;
1754        }        }
1755    
1756      /* For both capturing and non-capturing groups, reset the value of the ims      /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
1757      flags, in case they got changed during the group. */      and return the MATCH_KETRPOS. This makes it possible to do the repeats one
1758        at a time from the outer level, thus saving stack. */
     ims = original_ims;  
     DPRINTF(("ims reset to %02lx\n", ims));  
   
     /* 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. */  
1759    
1760      if (*ecode == OP_KET || eptr == saved_eptr)      if (*ecode == OP_KETRPOS)
1761        {        {
1762        ecode += 1 + LINK_SIZE;        md->end_match_ptr = eptr;
1763        break;        md->end_offset_top = offset_top;
1764          RRETURN(MATCH_KETRPOS);
1765        }        }
1766    
1767      /* The repeating kets try the rest of the pattern or restart from the      /* The normal repeating kets try the rest of the pattern or restart from
1768      preceding bracket, in the appropriate order. In the second case, we can use      the preceding bracket, in the appropriate order. In the second case, we can
1769      tail recursion to avoid using another stack frame. */      use tail recursion to avoid using another stack frame, unless we have an
1770        an atomic group or an unlimited repeat of a group that can match an empty
1771      flags = (*prev >= OP_SBRA)? match_cbegroup : 0;      string. */
1772    
1773      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1774        {        {
1775        RMATCH(eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
         RM12);  
1776        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1777          if (*prev == OP_ONCE)
1778            {
1779            RMATCH(eptr, prev, offset_top, md, eptrb, RM8);
1780            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1781            md->once_target = prev;  /* Level at which to change to MATCH_NOMATCH */
1782            RRETURN(MATCH_ONCE);
1783            }
1784          if (*prev >= OP_SBRA)    /* Could match an empty string */
1785            {
1786            md->match_function_type = MATCH_CBEGROUP;
1787            RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
1788            RRETURN(rrc);
1789            }
1790        ecode = prev;        ecode = prev;
       flags |= match_tail_recursed;  
1791        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1792        }        }
1793      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1794        {        {
1795        RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM13);        if (*prev >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1796          RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
1797          if (rrc == MATCH_ONCE && md->once_target == prev) rrc = MATCH_NOMATCH;
1798        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1799          if (*prev == OP_ONCE)
1800            {
1801            RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM9);
1802            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1803            md->once_target = prev;
1804            RRETURN(MATCH_ONCE);
1805            }
1806        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = match_tail_recursed;  
1807        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1808        }        }
1809      /* Control never gets here */      /* Control never gets here */
1810    
1811      /* Start of subject unless notbol, or after internal newline if multiline */      /* Not multiline mode: start of subject assertion, unless notbol. */
1812    
1813      case OP_CIRC:      case OP_CIRC:
1814      if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);      if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
     if ((ims & PCRE_MULTILINE) != 0)  
       {  
       if (eptr != md->start_subject &&  
           (eptr == md->end_subject || !WAS_NEWLINE(eptr)))  
         RRETURN(MATCH_NOMATCH);  
       ecode++;  
       break;  
       }  
     /* ... else fall through */  
1815    
1816      /* Start of subject assertion */      /* Start of subject assertion */
1817    
1818      case OP_SOD:      case OP_SOD:
1819      if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);      if (eptr != md->start_subject) MRRETURN(MATCH_NOMATCH);
1820        ecode++;
1821        break;
1822    
1823        /* Multiline mode: start of subject unless notbol, or after any newline. */
1824    
1825        case OP_CIRCM:
1826        if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
1827        if (eptr != md->start_subject &&
1828            (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
1829          MRRETURN(MATCH_NOMATCH);
1830      ecode++;      ecode++;
1831      break;      break;
1832    
1833      /* Start of match assertion */      /* Start of match assertion */
1834    
1835      case OP_SOM:      case OP_SOM:
1836      if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);      if (eptr != md->start_subject + md->start_offset) MRRETURN(MATCH_NOMATCH);
1837      ecode++;      ecode++;
1838      break;      break;
1839    
# Line 1274  for (;;) Line 1844  for (;;)
1844      ecode++;      ecode++;
1845      break;      break;
1846    
1847      /* Assert before internal newline if multiline, or before a terminating      /* Multiline mode: assert before any newline, or before end of subject
1848      newline unless endonly is set, else end of subject unless noteol is set. */      unless noteol is set. */
1849    
1850      case OP_DOLL:      case OP_DOLLM:
1851      if ((ims & PCRE_MULTILINE) != 0)      if (eptr < md->end_subject)
1852        {        { if (!IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH); }
       if (eptr < md->end_subject)  
         { if (!IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); }  
       else  
         { if (md->noteol) RRETURN(MATCH_NOMATCH); }  
       ecode++;  
       break;  
       }  
1853      else      else
1854        {        {
1855        if (md->noteol) RRETURN(MATCH_NOMATCH);        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1856        if (!md->endonly)        SCHECK_PARTIAL();
         {  
         if (eptr != md->end_subject &&  
             (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))  
           RRETURN(MATCH_NOMATCH);  
         ecode++;  
         break;  
         }  
1857        }        }
1858        ecode++;
1859        break;
1860    
1861        /* Not multiline mode: assert before a terminating newline or before end of
1862        subject unless noteol is set. */
1863    
1864        case OP_DOLL:
1865        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1866        if (!md->endonly) goto ASSERT_NL_OR_EOS;
1867    
1868      /* ... else fall through for endonly */      /* ... else fall through for endonly */
1869    
1870      /* End of subject assertion (\z) */      /* End of subject assertion (\z) */
1871    
1872      case OP_EOD:      case OP_EOD:
1873      if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr < md->end_subject) MRRETURN(MATCH_NOMATCH);
1874        SCHECK_PARTIAL();
1875      ecode++;      ecode++;
1876      break;      break;
1877    
1878      /* End of subject or ending \n assertion (\Z) */      /* End of subject or ending \n assertion (\Z) */
1879    
1880      case OP_EODN:      case OP_EODN:
1881      if (eptr != md->end_subject &&      ASSERT_NL_OR_EOS:
1882        if (eptr < md->end_subject &&
1883          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
1884        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
1885    
1886        /* Either at end of string or \n before end. */
1887    
1888        SCHECK_PARTIAL();
1889      ecode++;      ecode++;
1890      break;      break;
1891    
# Line 1325  for (;;) Line 1897  for (;;)
1897    
1898        /* Find out if the previous and current characters are "word" characters.        /* Find out if the previous and current characters are "word" characters.
1899        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
1900        be "non-word" characters. */        be "non-word" characters. Remember the earliest consulted character for
1901          partial matching. */
1902    
1903  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1904        if (utf8)        if (utf8)
1905          {          {
1906            /* Get status of previous character */
1907    
1908          if (eptr == md->start_subject) prev_is_word = FALSE; else          if (eptr == md->start_subject) prev_is_word = FALSE; else
1909            {            {
1910            const uschar *lastptr = eptr - 1;            USPTR lastptr = eptr - 1;
1911            while((*lastptr & 0xc0) == 0x80) lastptr--;            while((*lastptr & 0xc0) == 0x80) lastptr--;
1912              if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
1913            GETCHAR(c, lastptr);            GETCHAR(c, lastptr);
1914    #ifdef SUPPORT_UCP
1915              if (md->use_ucp)
1916                {
1917                if (c == '_') prev_is_word = TRUE; else
1918                  {
1919                  int cat = UCD_CATEGORY(c);
1920                  prev_is_word = (cat == ucp_L || cat == ucp_N);
1921                  }
1922                }
1923              else
1924    #endif
1925            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
1926            }            }
1927          if (eptr >= md->end_subject) cur_is_word = FALSE; else  
1928            /* Get status of next character */
1929    
1930            if (eptr >= md->end_subject)
1931              {
1932              SCHECK_PARTIAL();
1933              cur_is_word = FALSE;
1934              }
1935            else
1936            {            {
1937            GETCHAR(c, eptr);            GETCHAR(c, eptr);
1938    #ifdef SUPPORT_UCP
1939              if (md->use_ucp)
1940                {
1941                if (c == '_') cur_is_word = TRUE; else
1942                  {
1943                  int cat = UCD_CATEGORY(c);
1944                  cur_is_word = (cat == ucp_L || cat == ucp_N);
1945                  }
1946                }
1947              else
1948    #endif
1949            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
1950            }            }
1951          }          }
1952        else        else
1953  #endif  #endif
1954    
1955        /* More streamlined when not in UTF-8 mode */        /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for
1956          consistency with the behaviour of \w we do use it in this case. */
1957    
1958          {          {
1959          prev_is_word = (eptr != md->start_subject) &&          /* Get status of previous character */
1960            ((md->ctypes[eptr[-1]] & ctype_word) != 0);  
1961          cur_is_word = (eptr < md->end_subject) &&          if (eptr == md->start_subject) prev_is_word = FALSE; else
1962            ((md->ctypes[*eptr] & ctype_word) != 0);            {
1963              if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
1964    #ifdef SUPPORT_UCP
1965              if (md->use_ucp)
1966                {
1967                c = eptr[-1];
1968                if (c == '_') prev_is_word = TRUE; else
1969                  {
1970                  int cat = UCD_CATEGORY(c);
1971                  prev_is_word = (cat == ucp_L || cat == ucp_N);
1972                  }
1973                }
1974              else
1975    #endif
1976              prev_is_word = ((md->ctypes[eptr[-1]] & ctype_word) != 0);
1977              }
1978    
1979            /* Get status of next character */
1980    
1981            if (eptr >= md->end_subject)
1982              {
1983              SCHECK_PARTIAL();
1984              cur_is_word = FALSE;
1985              }
1986            else
1987    #ifdef SUPPORT_UCP
1988            if (md->use_ucp)
1989              {
1990              c = *eptr;
1991              if (c == '_') cur_is_word = TRUE; else
1992                {
1993                int cat = UCD_CATEGORY(c);
1994                cur_is_word = (cat == ucp_L || cat == ucp_N);
1995                }
1996              }
1997            else
1998    #endif
1999            cur_is_word = ((md->ctypes[*eptr] & ctype_word) != 0);
2000          }          }
2001    
2002        /* Now see if the situation is what we want */        /* Now see if the situation is what we want */
2003    
2004        if ((*ecode++ == OP_WORD_BOUNDARY)?        if ((*ecode++ == OP_WORD_BOUNDARY)?
2005             cur_is_word == prev_is_word : cur_is_word != prev_is_word)             cur_is_word == prev_is_word : cur_is_word != prev_is_word)
2006          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2007        }        }
2008      break;      break;
2009    
2010      /* Match a single character type; inline for speed */      /* Match a single character type; inline for speed */
2011    
2012      case OP_ANY:      case OP_ANY:
2013      if ((ims & PCRE_DOTALL) == 0)      if (IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH);
2014        /* Fall through */
2015    
2016        case OP_ALLANY:
2017        if (eptr++ >= md->end_subject)
2018        {        {
2019        if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);        SCHECK_PARTIAL();
2020          MRRETURN(MATCH_NOMATCH);
2021        }        }
2022      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
     if (utf8)  
       while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;  
2023      ecode++;      ecode++;
2024      break;      break;
2025    
# Line 1380  for (;;) Line 2027  for (;;)
2027      any byte, even newline, independent of the setting of PCRE_DOTALL. */      any byte, even newline, independent of the setting of PCRE_DOTALL. */
2028    
2029      case OP_ANYBYTE:      case OP_ANYBYTE:
2030      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr++ >= md->end_subject)
2031          {
2032          SCHECK_PARTIAL();
2033          MRRETURN(MATCH_NOMATCH);
2034          }
2035      ecode++;      ecode++;
2036      break;      break;
2037    
2038      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2039      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2040          {
2041          SCHECK_PARTIAL();
2042          MRRETURN(MATCH_NOMATCH);
2043          }
2044      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2045      if (      if (
2046  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1393  for (;;) Line 2048  for (;;)
2048  #endif  #endif
2049         (md->ctypes[c] & ctype_digit) != 0         (md->ctypes[c] & ctype_digit) != 0
2050         )         )
2051        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2052      ecode++;      ecode++;
2053      break;      break;
2054    
2055      case OP_DIGIT:      case OP_DIGIT:
2056      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2057          {
2058          SCHECK_PARTIAL();
2059          MRRETURN(MATCH_NOMATCH);
2060          }
2061      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2062      if (      if (
2063  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1406  for (;;) Line 2065  for (;;)
2065  #endif  #endif
2066         (md->ctypes[c] & ctype_digit) == 0         (md->ctypes[c] & ctype_digit) == 0
2067         )         )
2068        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2069      ecode++;      ecode++;
2070      break;      break;
2071    
2072      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2073      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2074          {
2075          SCHECK_PARTIAL();
2076          MRRETURN(MATCH_NOMATCH);
2077          }
2078      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2079      if (      if (
2080  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1419  for (;;) Line 2082  for (;;)
2082  #endif  #endif
2083         (md->ctypes[c] & ctype_space) != 0         (md->ctypes[c] & ctype_space) != 0
2084         )         )
2085        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2086      ecode++;      ecode++;
2087      break;      break;
2088    
2089      case OP_WHITESPACE:      case OP_WHITESPACE:
2090      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2091          {
2092          SCHECK_PARTIAL();
2093          MRRETURN(MATCH_NOMATCH);
2094          }
2095      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2096      if (      if (
2097  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1432  for (;;) Line 2099  for (;;)
2099  #endif  #endif
2100         (md->ctypes[c] & ctype_space) == 0         (md->ctypes[c] & ctype_space) == 0
2101         )         )
2102        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2103      ecode++;      ecode++;
2104      break;      break;
2105    
2106      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2107      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2108          {
2109          SCHECK_PARTIAL();
2110          MRRETURN(MATCH_NOMATCH);
2111          }
2112      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2113      if (      if (
2114  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1445  for (;;) Line 2116  for (;;)
2116  #endif  #endif
2117         (md->ctypes[c] & ctype_word) != 0         (md->ctypes[c] & ctype_word) != 0
2118         )         )
2119        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2120      ecode++;      ecode++;
2121      break;      break;
2122    
2123      case OP_WORDCHAR:      case OP_WORDCHAR:
2124      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2125          {
2126          SCHECK_PARTIAL();
2127          MRRETURN(MATCH_NOMATCH);
2128          }
2129      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2130      if (      if (
2131  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1458  for (;;) Line 2133  for (;;)
2133  #endif  #endif
2134         (md->ctypes[c] & ctype_word) == 0         (md->ctypes[c] & ctype_word) == 0
2135         )         )
2136        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2137      ecode++;      ecode++;
2138      break;      break;
2139    
2140      case OP_ANYNL:      case OP_ANYNL:
2141      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2142          {
2143          SCHECK_PARTIAL();
2144          MRRETURN(MATCH_NOMATCH);
2145          }
2146      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2147      switch(c)      switch(c)
2148        {        {
2149        default: RRETURN(MATCH_NOMATCH);        default: MRRETURN(MATCH_NOMATCH);
2150    
2151        case 0x000d:        case 0x000d:
2152        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
2153        break;        break;
2154    
2155        case 0x000a:        case 0x000a:
2156          break;
2157    
2158        case 0x000b:        case 0x000b:
2159        case 0x000c:        case 0x000c:
2160        case 0x0085:        case 0x0085:
2161        case 0x2028:        case 0x2028:
2162        case 0x2029:        case 0x2029:
2163          if (md->bsr_anycrlf) MRRETURN(MATCH_NOMATCH);
2164          break;
2165          }
2166        ecode++;
2167        break;
2168    
2169        case OP_NOT_HSPACE:
2170        if (eptr >= md->end_subject)
2171          {
2172          SCHECK_PARTIAL();
2173          MRRETURN(MATCH_NOMATCH);
2174          }
2175        GETCHARINCTEST(c, eptr);
2176        switch(c)
2177          {
2178          default: break;
2179          case 0x09:      /* HT */
2180          case 0x20:      /* SPACE */
2181          case 0xa0:      /* NBSP */
2182          case 0x1680:    /* OGHAM SPACE MARK */
2183          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
2184          case 0x2000:    /* EN QUAD */
2185          case 0x2001:    /* EM QUAD */
2186          case 0x2002:    /* EN SPACE */
2187          case 0x2003:    /* EM SPACE */
2188          case 0x2004:    /* THREE-PER-EM SPACE */
2189          case 0x2005:    /* FOUR-PER-EM SPACE */
2190          case 0x2006:    /* SIX-PER-EM SPACE */
2191          case 0x2007:    /* FIGURE SPACE */
2192          case 0x2008:    /* PUNCTUATION SPACE */
2193          case 0x2009:    /* THIN SPACE */
2194          case 0x200A:    /* HAIR SPACE */
2195          case 0x202f:    /* NARROW NO-BREAK SPACE */
2196          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
2197          case 0x3000:    /* IDEOGRAPHIC SPACE */
2198          MRRETURN(MATCH_NOMATCH);
2199          }
2200        ecode++;
2201        break;
2202    
2203        case OP_HSPACE:
2204        if (eptr >= md->end_subject)
2205          {
2206          SCHECK_PARTIAL();
2207          MRRETURN(MATCH_NOMATCH);
2208          }
2209        GETCHARINCTEST(c, eptr);
2210        switch(c)
2211          {
2212          default: MRRETURN(MATCH_NOMATCH);
2213          case 0x09:      /* HT */
2214          case 0x20:      /* SPACE */
2215          case 0xa0:      /* NBSP */
2216          case 0x1680:    /* OGHAM SPACE MARK */
2217          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
2218          case 0x2000:    /* EN QUAD */
2219          case 0x2001:    /* EM QUAD */
2220          case 0x2002:    /* EN SPACE */
2221          case 0x2003:    /* EM SPACE */
2222          case 0x2004:    /* THREE-PER-EM SPACE */
2223          case 0x2005:    /* FOUR-PER-EM SPACE */
2224          case 0x2006:    /* SIX-PER-EM SPACE */
2225          case 0x2007:    /* FIGURE SPACE */
2226          case 0x2008:    /* PUNCTUATION SPACE */
2227          case 0x2009:    /* THIN SPACE */
2228          case 0x200A:    /* HAIR SPACE */
2229          case 0x202f:    /* NARROW NO-BREAK SPACE */
2230          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
2231          case 0x3000:    /* IDEOGRAPHIC SPACE */
2232          break;
2233          }
2234        ecode++;
2235        break;
2236    
2237        case OP_NOT_VSPACE:
2238        if (eptr >= md->end_subject)
2239          {
2240          SCHECK_PARTIAL();
2241          MRRETURN(MATCH_NOMATCH);
2242          }
2243        GETCHARINCTEST(c, eptr);
2244        switch(c)
2245          {
2246          default: break;
2247          case 0x0a:      /* LF */
2248          case 0x0b:      /* VT */
2249          case 0x0c:      /* FF */
2250          case 0x0d:      /* CR */
2251          case 0x85:      /* NEL */
2252          case 0x2028:    /* LINE SEPARATOR */
2253          case 0x2029:    /* PARAGRAPH SEPARATOR */
2254          MRRETURN(MATCH_NOMATCH);
2255          }
2256        ecode++;
2257        break;
2258    
2259        case OP_VSPACE:
2260        if (eptr >= md->end_subject)
2261          {
2262          SCHECK_PARTIAL();
2263          MRRETURN(MATCH_NOMATCH);
2264          }
2265        GETCHARINCTEST(c, eptr);
2266        switch(c)
2267          {
2268          default: MRRETURN(MATCH_NOMATCH);
2269          case 0x0a:      /* LF */
2270          case 0x0b:      /* VT */
2271          case 0x0c:      /* FF */
2272          case 0x0d:      /* CR */
2273          case 0x85:      /* NEL */
2274          case 0x2028:    /* LINE SEPARATOR */
2275          case 0x2029:    /* PARAGRAPH SEPARATOR */
2276        break;        break;
2277        }        }
2278      ecode++;      ecode++;
# Line 1488  for (;;) Line 2284  for (;;)
2284    
2285      case OP_PROP:      case OP_PROP:
2286      case OP_NOTPROP:      case OP_NOTPROP:
2287      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2288          {
2289          SCHECK_PARTIAL();
2290          MRRETURN(MATCH_NOMATCH);
2291          }
2292      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2293        {        {
2294        int chartype, script;        const ucd_record *prop = GET_UCD(c);
       int category = _pcre_ucp_findprop(c, &chartype, &script);  
2295    
2296        switch(ecode[1])        switch(ecode[1])
2297          {          {
2298          case PT_ANY:          case PT_ANY:
2299          if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);          if (op == OP_NOTPROP) MRRETURN(MATCH_NOMATCH);
2300          break;          break;
2301    
2302          case PT_LAMP:          case PT_LAMP:
2303          if ((chartype == ucp_Lu ||          if ((prop->chartype == ucp_Lu ||
2304               chartype == ucp_Ll ||               prop->chartype == ucp_Ll ||
2305               chartype == ucp_Lt) == (op == OP_NOTPROP))               prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
2306            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2307           break;          break;
2308    
2309          case PT_GC:          case PT_GC:
2310          if ((ecode[2] != category) == (op == OP_PROP))          if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))
2311            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2312          break;          break;
2313    
2314          case PT_PC:          case PT_PC:
2315          if ((ecode[2] != chartype) == (op == OP_PROP))          if ((ecode[2] != prop->chartype) == (op == OP_PROP))
2316            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2317          break;          break;
2318    
2319          case PT_SC:          case PT_SC:
2320          if ((ecode[2] != script) == (op == OP_PROP))          if ((ecode[2] != prop->script) == (op == OP_PROP))
2321            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2322          break;          break;
2323    
2324            /* These are specials */
2325    
2326            case PT_ALNUM:
2327            if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2328                 _pcre_ucp_gentype[prop->chartype] == ucp_N) == (op == OP_NOTPROP))
2329              MRRETURN(MATCH_NOMATCH);
2330            break;
2331    
2332            case PT_SPACE:    /* Perl space */
2333            if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2334                 c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2335                   == (op == OP_NOTPROP))
2336              MRRETURN(MATCH_NOMATCH);
2337            break;
2338    
2339            case PT_PXSPACE:  /* POSIX space */
2340            if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2341                 c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2342                 c == CHAR_FF || c == CHAR_CR)
2343                   == (op == OP_NOTPROP))
2344              MRRETURN(MATCH_NOMATCH);
2345            break;
2346    
2347            case PT_WORD:
2348            if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2349                 _pcre_ucp_gentype[prop->chartype] == ucp_N ||
2350                 c == CHAR_UNDERSCORE) == (op == OP_NOTPROP))
2351              MRRETURN(MATCH_NOMATCH);
2352            break;
2353    
2354            /* This should never occur */
2355    
2356          default:          default:
2357          RRETURN(PCRE_ERROR_INTERNAL);          RRETURN(PCRE_ERROR_INTERNAL);
2358          }          }
# Line 1534  for (;;) Line 2365  for (;;)
2365      is in the binary; otherwise a compile-time error occurs. */      is in the binary; otherwise a compile-time error occurs. */
2366    
2367      case OP_EXTUNI:      case OP_EXTUNI:
2368      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2369          {
2370          SCHECK_PARTIAL();
2371          MRRETURN(MATCH_NOMATCH);
2372          }
2373      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2374        if (UCD_CATEGORY(c) == ucp_M) MRRETURN(MATCH_NOMATCH);
2375        while (eptr < md->end_subject)
2376        {        {
2377        int chartype, script;        int len = 1;
2378        int category = _pcre_ucp_findprop(c, &chartype, &script);        if (!utf8) c = *eptr; else { GETCHARLEN(c, eptr, len); }
2379        if (category == ucp_M) RRETURN(MATCH_NOMATCH);        if (UCD_CATEGORY(c) != ucp_M) break;
2380        while (eptr < md->end_subject)        eptr += len;
         {  
         int len = 1;  
         if (!utf8) c = *eptr; else  
           {  
           GETCHARLEN(c, eptr, len);  
           }  
         category = _pcre_ucp_findprop(c, &chartype, &script);  
         if (category != ucp_M) break;  
         eptr += len;  
         }  
2381        }        }
2382      ecode++;      ecode++;
2383      break;      break;
# Line 1566  for (;;) Line 2393  for (;;)
2393      loops). */      loops). */
2394    
2395      case OP_REF:      case OP_REF:
2396        {      case OP_REFI:
2397        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */      caseless = op == OP_REFI;
2398        ecode += 3;                                 /* Advance past item */      offset = GET2(ecode, 1) << 1;               /* Doubled ref number */
2399        ecode += 3;
2400    
2401        /* 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];  
2402    
2403        /* Set up for repetition, or handle the non-repeated case */      (a) In the default, Perl-compatible state, set the length negative;
2404        this ensures that every attempt at a match fails. We can't just fail
2405        here, because of the possibility of quantifiers with zero minima.
2406    
2407        switch (*ecode)      (b) If the JavaScript compatibility flag is set, set the length to zero
2408          {      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;  
2409    
2410          case OP_CRRANGE:      Otherwise, set the length to the length of what was matched by the
2411          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;  
2412    
2413          default:               /* No repeat follows */      if (offset >= offset_top || md->offset_vector[offset] < 0)
2414          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);        length = (md->jscript_compat)? 0 : -1;
2415          eptr += length;      else
2416          continue;              /* With the main loop */        length = md->offset_vector[offset+1] - md->offset_vector[offset];
2417    
2418        /* Set up for repetition, or handle the non-repeated case */
2419    
2420        switch (*ecode)
2421          {
2422          case OP_CRSTAR:
2423          case OP_CRMINSTAR:
2424          case OP_CRPLUS:
2425          case OP_CRMINPLUS:
2426          case OP_CRQUERY:
2427          case OP_CRMINQUERY:
2428          c = *ecode++ - OP_CRSTAR;
2429          minimize = (c & 1) != 0;
2430          min = rep_min[c];                 /* Pick up values from tables; */
2431          max = rep_max[c];                 /* zero for max => infinity */
2432          if (max == 0) max = INT_MAX;
2433          break;
2434    
2435          case OP_CRRANGE:
2436          case OP_CRMINRANGE:
2437          minimize = (*ecode == OP_CRMINRANGE);
2438          min = GET2(ecode, 1);
2439          max = GET2(ecode, 3);
2440          if (max == 0) max = INT_MAX;
2441          ecode += 5;
2442          break;
2443    
2444          default:               /* No repeat follows */
2445          if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
2446            {
2447            CHECK_PARTIAL();
2448            MRRETURN(MATCH_NOMATCH);
2449          }          }
2450          eptr += length;
2451          continue;              /* With the main loop */
2452          }
2453    
2454        /* If the length of the reference is zero, just continue with the      /* Handle repeated back references. If the length of the reference is
2455        main loop. */      zero, just continue with the main loop. */
2456    
2457        if (length == 0) continue;      if (length == 0) continue;
2458    
2459        /* First, ensure the minimum number of matches are present. We get back      /* First, ensure the minimum number of matches are present. We get back
2460        the length of the reference string explicitly rather than passing the      the length of the reference string explicitly rather than passing the
2461        address of eptr, so that eptr can be a register variable. */      address of eptr, so that eptr can be a register variable. */
2462    
2463        for (i = 1; i <= min; i++)      for (i = 1; i <= min; i++)
2464          {
2465          int slength;
2466          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2467          {          {
2468          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);          CHECK_PARTIAL();
2469          eptr += length;          MRRETURN(MATCH_NOMATCH);
2470          }          }
2471          eptr += slength;
2472          }
2473    
2474        /* If min = max, continue at the same level without recursion.      /* If min = max, continue at the same level without recursion.
2475        They are not both allowed to be zero. */      They are not both allowed to be zero. */
2476    
2477        if (min == max) continue;      if (min == max) continue;
2478    
2479        /* If minimizing, keep trying and advancing the pointer */      /* If minimizing, keep trying and advancing the pointer */
2480    
2481        if (minimize)      if (minimize)
2482          {
2483          for (fi = min;; fi++)
2484          {          {
2485          for (fi = min;; fi++)          int slength;
2486            RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
2487            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2488            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2489            if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2490            {            {
2491            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM14);            CHECK_PARTIAL();
2492            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            MRRETURN(MATCH_NOMATCH);
           if (fi >= max || !match_ref(offset, eptr, length, md, ims))  
             RRETURN(MATCH_NOMATCH);  
           eptr += length;  
2493            }            }
2494          /* Control never gets here */          eptr += slength;
2495          }          }
2496          /* Control never gets here */
2497          }
2498    
2499        /* If maximizing, find the longest string and work backwards */      /* If maximizing, find the longest string and work backwards */
2500    
2501        else      else
2502          {
2503          pp = eptr;
2504          for (i = min; i < max; i++)
2505          {          {
2506          pp = eptr;          int slength;
2507          for (i = min; i < max; i++)          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2508            {            {
2509            if (!match_ref(offset, eptr, length, md, ims)) break;            CHECK_PARTIAL();
2510            eptr += length;            break;
           }  
         while (eptr >= pp)  
           {  
           RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM15);  
           if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
           eptr -= length;  
2511            }            }
2512          RRETURN(MATCH_NOMATCH);          eptr += slength;
2513            }
2514          while (eptr >= pp)
2515            {
2516            RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
2517            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2518            eptr -= length;
2519          }          }
2520          MRRETURN(MATCH_NOMATCH);
2521        }        }
2522      /* Control never gets here */      /* Control never gets here */
2523    
   
   
2524      /* Match a bit-mapped character class, possibly repeatedly. This op code is      /* Match a bit-mapped character class, possibly repeatedly. This op code is
2525      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,
2526      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 1723  for (;;) Line 2575  for (;;)
2575          {          {
2576          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2577            {            {
2578            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2579                {
2580                SCHECK_PARTIAL();
2581                MRRETURN(MATCH_NOMATCH);
2582                }
2583            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
2584            if (c > 255)            if (c > 255)
2585              {              {
2586              if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);              if (op == OP_CLASS) MRRETURN(MATCH_NOMATCH);
2587              }              }
2588            else            else
2589              {              {
2590              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);              if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2591              }              }
2592            }            }
2593          }          }
# Line 1741  for (;;) Line 2597  for (;;)
2597          {          {
2598          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2599            {            {
2600            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2601                {
2602                SCHECK_PARTIAL();
2603                MRRETURN(MATCH_NOMATCH);
2604                }
2605            c = *eptr++;            c = *eptr++;
2606            if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);            if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2607            }            }
2608          }          }
2609    
# Line 1763  for (;;) Line 2623  for (;;)
2623            {            {
2624            for (fi = min;; fi++)            for (fi = min;; fi++)
2625              {              {
2626              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM16);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
2627              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2628              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2629                if (eptr >= md->end_subject)
2630                  {
2631                  SCHECK_PARTIAL();
2632                  MRRETURN(MATCH_NOMATCH);
2633                  }
2634              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
2635              if (c > 255)              if (c > 255)
2636                {                {
2637                if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);                if (op == OP_CLASS) MRRETURN(MATCH_NOMATCH);
2638                }                }
2639              else              else
2640                {                {
2641                if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);                if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2642                }                }
2643              }              }
2644            }            }
# Line 1783  for (;;) Line 2648  for (;;)
2648            {            {
2649            for (fi = min;; fi++)            for (fi = min;; fi++)
2650              {              {
2651              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM17);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
2652              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2653              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2654                if (eptr >= md->end_subject)
2655                  {
2656                  SCHECK_PARTIAL();
2657                  MRRETURN(MATCH_NOMATCH);
2658                  }
2659              c = *eptr++;              c = *eptr++;
2660              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);              if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2661              }              }
2662            }            }
2663          /* Control never gets here */          /* Control never gets here */
# Line 1806  for (;;) Line 2676  for (;;)
2676            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2677              {              {
2678              int len = 1;              int len = 1;
2679              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
2680                  {
2681                  SCHECK_PARTIAL();
2682                  break;
2683                  }
2684              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
2685              if (c > 255)              if (c > 255)
2686                {                {
# Line 1820  for (;;) Line 2694  for (;;)
2694              }              }
2695            for (;;)            for (;;)
2696              {              {
2697              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM18);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
2698              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2699              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2700              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 1832  for (;;) Line 2706  for (;;)
2706            {            {
2707            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2708              {              {
2709              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
2710                  {
2711                  SCHECK_PARTIAL();
2712                  break;
2713                  }
2714              c = *eptr;              c = *eptr;
2715              if ((data[c/8] & (1 << (c&7))) == 0) break;              if ((data[c/8] & (1 << (c&7))) == 0) break;
2716              eptr++;              eptr++;
2717              }              }
2718            while (eptr >= pp)            while (eptr >= pp)
2719              {              {
2720              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM19);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM19);
2721              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2722              eptr--;              eptr--;
2723              }              }
2724            }            }
2725    
2726          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2727          }          }
2728        }        }
2729      /* Control never gets here */      /* Control never gets here */
2730    
2731    
2732      /* Match an extended character class. This opcode is encountered only      /* Match an extended character class. This opcode is encountered only
2733      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
2734        mode, because Unicode properties are supported in non-UTF-8 mode. */
2735    
2736  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2737      case OP_XCLASS:      case OP_XCLASS:
# Line 1893  for (;;) Line 2772  for (;;)
2772    
2773        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
2774          {          {
2775          if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);          if (eptr >= md->end_subject)
2776          GETCHARINC(c, eptr);            {
2777          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);            SCHECK_PARTIAL();
2778              MRRETURN(MATCH_NOMATCH);
2779              }
2780            GETCHARINCTEST(c, eptr);
2781            if (!_pcre_xclass(c, data)) MRRETURN(MATCH_NOMATCH);
2782          }          }
2783    
2784        /* 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 1910  for (;;) Line 2793  for (;;)
2793          {          {
2794          for (fi = min;; fi++)          for (fi = min;; fi++)
2795            {            {
2796            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM20);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM20);
2797            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2798            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2799            GETCHARINC(c, eptr);            if (eptr >= md->end_subject)
2800            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);              {
2801                SCHECK_PARTIAL();
2802                MRRETURN(MATCH_NOMATCH);
2803                }
2804              GETCHARINCTEST(c, eptr);
2805              if (!_pcre_xclass(c, data)) MRRETURN(MATCH_NOMATCH);
2806            }            }
2807          /* Control never gets here */          /* Control never gets here */
2808          }          }
# Line 1927  for (;;) Line 2815  for (;;)
2815          for (i = min; i < max; i++)          for (i = min; i < max; i++)
2816            {            {
2817            int len = 1;            int len = 1;
2818            if (eptr >= md->end_subject) break;            if (eptr >= md->end_subject)
2819            GETCHARLEN(c, eptr, len);              {
2820                SCHECK_PARTIAL();
2821                break;
2822                }
2823              GETCHARLENTEST(c, eptr, len);
2824            if (!_pcre_xclass(c, data)) break;            if (!_pcre_xclass(c, data)) break;
2825            eptr += len;            eptr += len;
2826            }            }
2827          for(;;)          for(;;)
2828            {            {
2829            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM21);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM21);
2830            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2831            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
2832            BACKCHAR(eptr)            if (utf8) BACKCHAR(eptr);
2833            }            }
2834          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2835          }          }
2836    
2837        /* Control never gets here */        /* Control never gets here */
# Line 1955  for (;;) Line 2847  for (;;)
2847        length = 1;        length = 1;
2848        ecode++;        ecode++;
2849        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2850        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
2851        while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);          {
2852            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2853            MRRETURN(MATCH_NOMATCH);
2854            }
2855          while (length-- > 0) if (*ecode++ != *eptr++) MRRETURN(MATCH_NOMATCH);
2856        }        }
2857      else      else
2858  #endif  #endif
2859    
2860      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2861        {        {
2862        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
2863        if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);          {
2864            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2865            MRRETURN(MATCH_NOMATCH);
2866            }
2867          if (ecode[1] != *eptr++) MRRETURN(MATCH_NOMATCH);
2868        ecode += 2;        ecode += 2;
2869        }        }
2870      break;      break;
2871    
2872      /* Match a single character, caselessly */      /* Match a single character, caselessly */
2873    
2874      case OP_CHARNC:      case OP_CHARI:
2875  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2876      if (utf8)      if (utf8)
2877        {        {
# Line 1979  for (;;) Line 2879  for (;;)
2879        ecode++;        ecode++;
2880        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2881    
2882        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
2883            {
2884            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2885            MRRETURN(MATCH_NOMATCH);
2886            }
2887    
2888        /* 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
2889        can use the fast lookup table. */        can use the fast lookup table. */
2890    
2891        if (fc < 128)        if (fc < 128)
2892          {          {
2893          if (md->lcc[*ecode++] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          if (md->lcc[*ecode++] != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
2894          }          }
2895    
2896        /* Otherwise we must pick up the subject character */        /* Otherwise we must pick up the subject character */
# Line 2003  for (;;) Line 2907  for (;;)
2907          if (fc != dc)          if (fc != dc)
2908            {            {
2909  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2910            if (dc != _pcre_ucp_othercase(fc))            if (dc != UCD_OTHERCASE(fc))
2911  #endif  #endif
2912              RRETURN(MATCH_NOMATCH);              MRRETURN(MATCH_NOMATCH);
2913            }            }
2914          }          }
2915        }        }
# Line 2014  for (;;) Line 2918  for (;;)
2918    
2919      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2920        {        {
2921        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
2922        if (md->lcc[ecode[1]] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          {
2923            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2924            MRRETURN(MATCH_NOMATCH);
2925            }
2926          if (md->lcc[ecode[1]] != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
2927        ecode += 2;        ecode += 2;
2928        }        }
2929      break;      break;
# Line 2023  for (;;) Line 2931  for (;;)
2931      /* Match a single character repeatedly. */      /* Match a single character repeatedly. */
2932    
2933      case OP_EXACT:      case OP_EXACT:
2934        case OP_EXACTI:
2935      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
2936      ecode += 3;      ecode += 3;
2937      goto REPEATCHAR;      goto REPEATCHAR;
2938    
2939      case OP_POSUPTO:      case OP_POSUPTO:
2940        case OP_POSUPTOI:
2941      possessive = TRUE;      possessive = TRUE;
2942      /* Fall through */      /* Fall through */
2943    
2944      case OP_UPTO:      case OP_UPTO:
2945        case OP_UPTOI:
2946      case OP_MINUPTO:      case OP_MINUPTO:
2947        case OP_MINUPTOI:
2948      min = 0;      min = 0;
2949      max = GET2(ecode, 1);      max = GET2(ecode, 1);
2950      minimize = *ecode == OP_MINUPTO;      minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI;
2951      ecode += 3;      ecode += 3;
2952      goto REPEATCHAR;      goto REPEATCHAR;
2953    
2954      case OP_POSSTAR:      case OP_POSSTAR:
2955        case OP_POSSTARI:
2956      possessive = TRUE;      possessive = TRUE;
2957      min = 0;      min = 0;
2958      max = INT_MAX;      max = INT_MAX;
# Line 2047  for (;;) Line 2960  for (;;)
2960      goto REPEATCHAR;      goto REPEATCHAR;
2961    
2962      case OP_POSPLUS:      case OP_POSPLUS:
2963        case OP_POSPLUSI:
2964      possessive = TRUE;      possessive = TRUE;
2965      min = 1;      min = 1;
2966      max = INT_MAX;      max = INT_MAX;
# Line 2054  for (;;) Line 2968  for (;;)
2968      goto REPEATCHAR;      goto REPEATCHAR;
2969    
2970      case OP_POSQUERY:      case OP_POSQUERY:
2971        case OP_POSQUERYI:
2972      possessive = TRUE;      possessive = TRUE;
2973      min = 0;      min = 0;
2974      max = 1;      max = 1;
# Line 2061  for (;;) Line 2976  for (;;)
2976      goto REPEATCHAR;      goto REPEATCHAR;
2977    
2978      case OP_STAR:      case OP_STAR:
2979        case OP_STARI:
2980      case OP_MINSTAR:      case OP_MINSTAR:
2981        case OP_MINSTARI:
2982      case OP_PLUS:      case OP_PLUS:
2983        case OP_PLUSI:
2984      case OP_MINPLUS:      case OP_MINPLUS:
2985        case OP_MINPLUSI:
2986      case OP_QUERY:      case OP_QUERY:
2987        case OP_QUERYI:
2988      case OP_MINQUERY:      case OP_MINQUERY:
2989      c = *ecode++ - OP_STAR;      case OP_MINQUERYI:
2990        c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI);
2991      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
2992      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
2993      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
2994      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
2995    
2996      /* Common code for all repeated single-character matches. We can give      /* Common code for all repeated single-character matches. */
     up quickly if there are fewer than the minimum number of characters left in  
     the subject. */  
2997    
2998      REPEATCHAR:      REPEATCHAR:
2999  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 2083  for (;;) Line 3002  for (;;)
3002        length = 1;        length = 1;
3003        charptr = ecode;        charptr = ecode;
3004        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
       if (min * length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3005        ecode += length;        ecode += length;
3006    
3007        /* Handle multibyte character matching specially here. There is        /* Handle multibyte character matching specially here. There is
# Line 2093  for (;;) Line 3011  for (;;)
3011          {          {
3012  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3013          unsigned int othercase;          unsigned int othercase;
3014          if ((ims & PCRE_CASELESS) != 0 &&          if (op >= OP_STARI &&     /* Caseless */
3015              (othercase = _pcre_ucp_othercase(fc)) != NOTACHAR)              (othercase = UCD_OTHERCASE(fc)) != fc)
3016            oclength = _pcre_ord2utf8(othercase, occhars);            oclength = _pcre_ord2utf8(othercase, occhars);
3017          else oclength = 0;          else oclength = 0;
3018  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3019    
3020          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3021            {            {
3022            if (memcmp(eptr, charptr, length) == 0) eptr += length;            if (eptr <= md->end_subject - length &&
3023                memcmp(eptr, charptr, length) == 0) eptr += length;
3024  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3025            /* Need braces because of following else */            else if (oclength > 0 &&
3026            else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }                     eptr <= md->end_subject - oclength &&
3027                       memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3028    #endif  /* SUPPORT_UCP */
3029            else            else
3030              {              {
3031              if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);              CHECK_PARTIAL();
3032              eptr += oclength;              MRRETURN(MATCH_NOMATCH);
3033              }              }
 #else   /* without SUPPORT_UCP */  
           else { RRETURN(MATCH_NOMATCH); }  
 #endif  /* SUPPORT_UCP */  
3034            }            }
3035    
3036          if (min == max) continue;          if (min == max) continue;
# Line 2121  for (;;) Line 3039  for (;;)
3039            {            {
3040            for (fi = min;; fi++)            for (fi = min;; fi++)
3041              {              {
3042              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM22);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM22);
3043              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3044              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3045              if (memcmp(eptr, charptr, length) == 0) eptr += length;              if (eptr <= md->end_subject - length &&
3046                  memcmp(eptr, charptr, length) == 0) eptr += length;
3047  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3048              /* Need braces because of following else */              else if (oclength > 0 &&
3049              else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }                       eptr <= md->end_subject - oclength &&
3050                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3051    #endif  /* SUPPORT_UCP */
3052              else              else
3053                {                {
3054                if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);                CHECK_PARTIAL();
3055                eptr += oclength;                MRRETURN(MATCH_NOMATCH);
3056                }                }
 #else   /* without SUPPORT_UCP */  
             else { RRETURN (MATCH_NOMATCH); }  
 #endif  /* SUPPORT_UCP */  
3057              }              }
3058            /* Control never gets here */            /* Control never gets here */
3059            }            }
# Line 2145  for (;;) Line 3063  for (;;)
3063            pp = eptr;            pp = eptr;
3064            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3065              {              {
3066              if (eptr > md->end_subject - length) break;              if (eptr <= md->end_subject - length &&
3067              if (memcmp(eptr, charptr, length) == 0) eptr += length;                  memcmp(eptr, charptr, length) == 0) eptr += length;
3068  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3069              else if (oclength == 0) break;              else if (oclength > 0 &&
3070                         eptr <= md->end_subject - oclength &&
3071                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3072    #endif  /* SUPPORT_UCP */
3073              else              else
3074                {                {
3075                if (memcmp(eptr, occhars, oclength) != 0) break;                CHECK_PARTIAL();
3076                eptr += oclength;                break;
3077                }                }
 #else   /* without SUPPORT_UCP */  
             else break;  
 #endif  /* SUPPORT_UCP */  
3078              }              }
3079    
3080            if (possessive) continue;            if (possessive) continue;
3081    
3082            for(;;)            for(;;)
3083             {              {
3084             RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM23);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM23);
3085             if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3086             if (eptr == pp) RRETURN(MATCH_NOMATCH);              if (eptr == pp) { MRRETURN(MATCH_NOMATCH); }
3087  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3088             eptr--;              eptr--;
3089             BACKCHAR(eptr);              BACKCHAR(eptr);
3090  #else   /* without SUPPORT_UCP */  #else   /* without SUPPORT_UCP */
3091             eptr -= length;              eptr -= length;
3092  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3093             }              }
3094            }            }
3095          /* Control never gets here */          /* Control never gets here */
3096          }          }
# Line 2184  for (;;) Line 3103  for (;;)
3103  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
3104    
3105      /* When not in UTF-8 mode, load a single-byte character. */      /* When not in UTF-8 mode, load a single-byte character. */
3106        {  
3107        if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);      fc = *ecode++;
       fc = *ecode++;  
       }  
3108    
3109      /* The value of fc at this point is always less than 256, though we may or      /* The value of fc at this point is always less than 256, though we may or
3110      may not be in UTF-8 mode. The code is duplicated for the caseless and      may not be in UTF-8 mode. The code is duplicated for the caseless and
# Line 2201  for (;;) Line 3118  for (;;)
3118      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3119        max, eptr));        max, eptr));
3120    
3121      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_STARI)  /* Caseless */
3122        {        {
3123        fc = md->lcc[fc];        fc = md->lcc[fc];
3124        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
3125          if (fc != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          {
3126            if (eptr >= md->end_subject)
3127              {
3128              SCHECK_PARTIAL();
3129              MRRETURN(MATCH_NOMATCH);
3130              }
3131            if (fc != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3132            }
3133        if (min == max) continue;        if (min == max) continue;
3134        if (minimize)        if (minimize)
3135          {          {
3136          for (fi = min;; fi++)          for (fi = min;; fi++)
3137            {            {
3138            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM24);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM24);
3139            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3140            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3141                fc != md->lcc[*eptr++])            if (eptr >= md->end_subject)
3142              RRETURN(MATCH_NOMATCH);              {
3143                SCHECK_PARTIAL();
3144                MRRETURN(MATCH_NOMATCH);
3145                }
3146              if (fc != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3147            }            }
3148          /* Control never gets here */          /* Control never gets here */
3149          }          }
# Line 2224  for (;;) Line 3152  for (;;)
3152          pp = eptr;          pp = eptr;
3153          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3154            {            {
3155            if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break;            if (eptr >= md->end_subject)
3156                {
3157                SCHECK_PARTIAL();
3158                break;
3159                }
3160              if (fc != md->lcc[*eptr]) break;
3161            eptr++;            eptr++;
3162            }            }
3163    
3164          if (possessive) continue;          if (possessive) continue;
3165    
3166          while (eptr >= pp)          while (eptr >= pp)
3167            {            {
3168            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM25);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM25);
3169            eptr--;            eptr--;
3170            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3171            }            }
3172          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3173          }          }
3174        /* Control never gets here */        /* Control never gets here */
3175        }        }
# Line 2243  for (;;) Line 3178  for (;;)
3178    
3179      else      else
3180        {        {
3181        for (i = 1; i <= min; i++) if (fc != *eptr++) RRETURN(MATCH_NOMATCH);        for (i = 1; i <= min; i++)
3182            {
3183            if (eptr >= md->end_subject)
3184              {
3185              SCHECK_PARTIAL();
3186              MRRETURN(MATCH_NOMATCH);
3187              }
3188            if (fc != *eptr++) MRRETURN(MATCH_NOMATCH);
3189            }
3190    
3191        if (min == max) continue;        if (min == max) continue;
3192    
3193        if (minimize)        if (minimize)
3194          {          {
3195          for (fi = min;; fi++)          for (fi = min;; fi++)
3196            {            {
3197            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM26);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM26);
3198            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3199            if (fi >= max || eptr >= md->end_subject || fc != *eptr++)            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3200              RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
3201                {
3202                SCHECK_PARTIAL();
3203                MRRETURN(MATCH_NOMATCH);
3204                }
3205              if (fc != *eptr++) MRRETURN(MATCH_NOMATCH);
3206            }            }
3207          /* Control never gets here */          /* Control never gets here */
3208          }          }
# Line 2261  for (;;) Line 3211  for (;;)
3211          pp = eptr;          pp = eptr;
3212          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3213            {            {
3214            if (eptr >= md->end_subject || fc != *eptr) break;            if (eptr >= md->end_subject)
3215                {
3216                SCHECK_PARTIAL();
3217                break;
3218                }
3219              if (fc != *eptr) break;
3220            eptr++;            eptr++;
3221            }            }
3222          if (possessive) continue;          if (possessive) continue;
3223    
3224          while (eptr >= pp)          while (eptr >= pp)
3225            {            {
3226            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM27);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM27);
3227            eptr--;            eptr--;
3228            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3229            }            }
3230          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3231          }          }
3232        }        }
3233      /* Control never gets here */      /* Control never gets here */
# Line 2280  for (;;) Line 3236  for (;;)
3236      checking can be multibyte. */      checking can be multibyte. */
3237    
3238      case OP_NOT:      case OP_NOT:
3239      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      case OP_NOTI:
3240        if (eptr >= md->end_subject)
3241          {
3242          SCHECK_PARTIAL();
3243          MRRETURN(MATCH_NOMATCH);
3244          }
3245      ecode++;      ecode++;
3246      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
3247      if ((ims & PCRE_CASELESS) != 0)      if (op == OP_NOTI)         /* The caseless case */
3248        {        {
3249  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3250        if (c < 256)        if (c < 256)
3251  #endif  #endif
3252        c = md->lcc[c];        c = md->lcc[c];
3253        if (md->lcc[*ecode++] == c) RRETURN(MATCH_NOMATCH);        if (md->lcc[*ecode++] == c) MRRETURN(MATCH_NOMATCH);
3254        }        }
3255      else      else    /* Caseful */
3256        {        {
3257        if (*ecode++ == c) RRETURN(MATCH_NOMATCH);        if (*ecode++ == c) MRRETURN(MATCH_NOMATCH);
3258        }        }
3259      break;      break;
3260    
# Line 2305  for (;;) Line 3266  for (;;)
3266      about... */      about... */
3267    
3268      case OP_NOTEXACT:      case OP_NOTEXACT:
3269        case OP_NOTEXACTI:
3270      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
3271      ecode += 3;      ecode += 3;
3272      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3273    
3274      case OP_NOTUPTO:      case OP_NOTUPTO:
3275        case OP_NOTUPTOI:
3276      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
3277        case OP_NOTMINUPTOI:
3278      min = 0;      min = 0;
3279      max = GET2(ecode, 1);      max = GET2(ecode, 1);
3280      minimize = *ecode == OP_NOTMINUPTO;      minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI;
3281      ecode += 3;      ecode += 3;
3282      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3283    
3284      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
3285        case OP_NOTPOSSTARI:
3286      possessive = TRUE;      possessive = TRUE;
3287      min = 0;      min = 0;
3288      max = INT_MAX;      max = INT_MAX;
# Line 2325  for (;;) Line 3290  for (;;)
3290      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3291    
3292      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
3293        case OP_NOTPOSPLUSI:
3294      possessive = TRUE;      possessive = TRUE;
3295      min = 1;      min = 1;
3296      max = INT_MAX;      max = INT_MAX;
# Line 2332  for (;;) Line 3298  for (;;)
3298      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3299    
3300      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
3301        case OP_NOTPOSQUERYI:
3302      possessive = TRUE;      possessive = TRUE;
3303      min = 0;      min = 0;
3304      max = 1;      max = 1;
# Line 2339  for (;;) Line 3306  for (;;)
3306      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3307    
3308      case OP_NOTPOSUPTO:      case OP_NOTPOSUPTO:
3309        case OP_NOTPOSUPTOI:
3310      possessive = TRUE;      possessive = TRUE;
3311      min = 0;      min = 0;
3312      max = GET2(ecode, 1);      max = GET2(ecode, 1);
# Line 2346  for (;;) Line 3314  for (;;)
3314      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3315    
3316      case OP_NOTSTAR:      case OP_NOTSTAR:
3317        case OP_NOTSTARI:
3318      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
3319        case OP_NOTMINSTARI:
3320      case OP_NOTPLUS:      case OP_NOTPLUS:
3321        case OP_NOTPLUSI:
3322      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
3323        case OP_NOTMINPLUSI:
3324      case OP_NOTQUERY:      case OP_NOTQUERY:
3325        case OP_NOTQUERYI:
3326      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
3327      c = *ecode++ - OP_NOTSTAR;      case OP_NOTMINQUERYI:
3328        c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
3329      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3330      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3331      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3332      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
3333    
3334      /* Common code for all repeated single-byte matches. We can give up quickly      /* Common code for all repeated single-byte matches. */
     if there are fewer than the minimum number of bytes left in the  
     subject. */  
3335    
3336      REPEATNOTCHAR:      REPEATNOTCHAR:
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3337      fc = *ecode++;      fc = *ecode++;
3338    
3339      /* The code is duplicated for the caseless and caseful cases, for speed,      /* The code is duplicated for the caseless and caseful cases, for speed,
# Line 2376  for (;;) Line 3347  for (;;)
3347      DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max,      DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3348        max, eptr));        max, eptr));
3349    
3350      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_NOTSTARI)     /* Caseless */
3351        {        {
3352        fc = md->lcc[fc];        fc = md->lcc[fc];
3353    
# Line 2387  for (;;) Line 3358  for (;;)
3358          register unsigned int d;          register unsigned int d;
3359          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3360            {            {
3361              if (eptr >= md->end_subject)
3362                {
3363                SCHECK_PARTIAL();
3364                MRRETURN(MATCH_NOMATCH);
3365                }
3366            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3367            if (d < 256) d = md->lcc[d];            if (d < 256) d = md->lcc[d];
3368            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) MRRETURN(MATCH_NOMATCH);
3369            }            }
3370          }          }
3371        else        else
# Line 2398  for (;;) Line 3374  for (;;)
3374        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3375          {          {
3376          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3377            if (fc == md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);            {
3378              if (eptr >= md->end_subject)
3379                {
3380                SCHECK_PARTIAL();
3381                MRRETURN(MATCH_NOMATCH);
3382                }
3383              if (fc == md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3384              }
3385          }          }
3386    
3387        if (min == max) continue;        if (min == max) continue;
# Line 2412  for (;;) Line 3395  for (;;)
3395            register unsigned int d;            register unsigned int d;
3396            for (fi = min;; fi++)            for (fi = min;; fi++)
3397              {              {
3398              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM28);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM28);
3399              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3400                if (fi >= max) MRRETURN(MATCH_NOMATCH);
3401                if (eptr >= md->end_subject)
3402                  {
3403                  SCHECK_PARTIAL();
3404                  MRRETURN(MATCH_NOMATCH);
3405                  }
3406              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3407              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3408              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) MRRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
3409              }              }
3410            }            }
3411          else          else
# Line 2426  for (;;) Line 3414  for (;;)
3414            {            {
3415            for (fi = min;; fi++)            for (fi = min;; fi++)
3416              {              {
3417              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM29);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM29);
3418              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3419              if (fi >= max || eptr >= md->end_subject || fc == md->lcc[*eptr++])              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3420                RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3421                  {
3422                  SCHECK_PARTIAL();
3423                  MRRETURN(MATCH_NOMATCH);
3424                  }
3425                if (fc == md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3426              }              }
3427            }            }
3428          /* Control never gets here */          /* Control never gets here */
# Line 2449  for (;;) Line 3442  for (;;)
3442            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3443              {              {
3444              int len = 1;              int len = 1;
3445              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3446                  {
3447                  SCHECK_PARTIAL();
3448                  break;
3449                  }
3450              GETCHARLEN(d, eptr, len);              GETCHARLEN(d, eptr, len);
3451              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3452              if (fc == d) break;              if (fc == d) break;
# Line 2458  for (;;) Line 3455  for (;;)
3455          if (possessive) continue;          if (possessive) continue;
3456          for(;;)          for(;;)
3457              {              {
3458              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM30);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM30);
3459              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3460              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3461              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2470  for (;;) Line 3467  for (;;)
3467            {            {
3468            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3469              {              {
3470              if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break;              if (eptr >= md->end_subject)
3471                  {
3472                  SCHECK_PARTIAL();
3473                  break;
3474                  }
3475                if (fc == md->lcc[*eptr]) break;
3476              eptr++;              eptr++;
3477              }              }
3478            if (possessive) continue;            if (possessive) continue;
3479            while (eptr >= pp)            while (eptr >= pp)
3480              {              {
3481              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM31);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM31);
3482              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3483              eptr--;              eptr--;
3484              }              }
3485            }            }
3486    
3487          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3488          }          }
3489        /* Control never gets here */        /* Control never gets here */
3490        }        }
# Line 2498  for (;;) Line 3500  for (;;)
3500          register unsigned int d;          register unsigned int d;
3501          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3502            {            {
3503              if (eptr >= md->end_subject)
3504                {
3505                SCHECK_PARTIAL();
3506                MRRETURN(MATCH_NOMATCH);
3507                }
3508            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3509            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) MRRETURN(MATCH_NOMATCH);
3510            }            }
3511          }          }
3512        else        else
# Line 2507  for (;;) Line 3514  for (;;)
3514        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3515          {          {
3516          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3517            if (fc == *eptr++) RRETURN(MATCH_NOMATCH);            {
3518              if (eptr >= md->end_subject)
3519                {
3520                SCHECK_PARTIAL();
3521                MRRETURN(MATCH_NOMATCH);
3522                }
3523              if (fc == *eptr++) MRRETURN(MATCH_NOMATCH);
3524              }
3525          }          }
3526    
3527        if (min == max) continue;        if (min == max) continue;
# Line 2521  for (;;) Line 3535  for (;;)
3535            register unsigned int d;            register unsigned int d;
3536            for (fi = min;; fi++)            for (fi = min;; fi++)
3537              {              {
3538              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM32);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM32);
3539              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3540                if (fi >= max) MRRETURN(MATCH_NOMATCH);
3541                if (eptr >= md->end_subject)
3542                  {
3543                  SCHECK_PARTIAL();
3544                  MRRETURN(MATCH_NOMATCH);
3545                  }
3546              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3547              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) MRRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
3548              }              }
3549            }            }
3550          else          else
# Line 2534  for (;;) Line 3553  for (;;)
3553            {            {
3554            for (fi = min;; fi++)            for (fi = min;; fi++)
3555              {              {
3556              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM33);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM33);
3557              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3558              if (fi >= max || eptr >= md->end_subject || fc == *eptr++)              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3559                RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3560                  {
3561                  SCHECK_PARTIAL();
3562                  MRRETURN(MATCH_NOMATCH);
3563                  }
3564                if (fc == *eptr++) MRRETURN(MATCH_NOMATCH);
3565              }              }
3566            }            }
3567          /* Control never gets here */          /* Control never gets here */
# Line 2557  for (;;) Line 3581  for (;;)
3581            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3582              {              {
3583              int len = 1;              int len = 1;
3584              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3585                  {
3586                  SCHECK_PARTIAL();
3587                  break;
3588                  }
3589              GETCHARLEN(d, eptr, len);              GETCHARLEN(d, eptr, len);
3590              if (fc == d) break;              if (fc == d) break;
3591              eptr += len;              eptr += len;
# Line 2565  for (;;) Line 3593  for (;;)
3593            if (possessive) continue;            if (possessive) continue;
3594            for(;;)            for(;;)
3595              {              {
3596              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM34);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM34);
3597              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3598              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3599              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2577  for (;;) Line 3605  for (;;)
3605            {            {
3606            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3607              {              {
3608              if (eptr >= md->end_subject || fc == *eptr) break;              if (eptr >= md->end_subject)
3609                  {
3610                  SCHECK_PARTIAL();
3611                  break;
3612                  }
3613                if (fc == *eptr) break;
3614              eptr++;              eptr++;
3615              }              }
3616            if (possessive) continue;            if (possessive) continue;
3617            while (eptr >= pp)            while (eptr >= pp)
3618              {              {
3619              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM35);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM35);
3620              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3621              eptr--;              eptr--;
3622              }              }
3623            }            }
3624    
3625          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3626          }          }
3627        }        }
3628      /* Control never gets here */      /* Control never gets here */
# Line 2671  for (;;) Line 3704  for (;;)
3704    
3705      /* First, ensure the minimum number of matches are present. Use inline      /* First, ensure the minimum number of matches are present. Use inline
3706      code for maximizing the speed, and do the type test once at the start      code for maximizing the speed, and do the type test once at the start
3707      (i.e. keep it out of the loop). Also we can test that there are at least      (i.e. keep it out of the loop). Separate the UTF-8 code completely as that
     the minimum number of bytes before we start. This isn't as effective in  
     UTF-8 mode, but it does no harm. Separate the UTF-8 code completely as that  
3708      is tidier. Also separate the UCP code, which can be the same for both UTF-8      is tidier. Also separate the UCP code, which can be the same for both UTF-8
3709      and single-bytes. */      and single-bytes. */
3710    
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3711      if (min > 0)      if (min > 0)
3712        {        {
3713  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 2686  for (;;) Line 3716  for (;;)
3716          switch(prop_type)          switch(prop_type)
3717            {            {
3718            case PT_ANY:            case PT_ANY:
3719            if (prop_fail_result) RRETURN(MATCH_NOMATCH);            if (prop_fail_result) MRRETURN(MATCH_NOMATCH);
3720            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3721              {              {
3722              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3723              GETCHARINC(c, eptr);                {
3724                  SCHECK_PARTIAL();
3725                  MRRETURN(MATCH_NOMATCH);
3726                  }
3727                GETCHARINCTEST(c, eptr);
3728              }              }
3729            break;            break;
3730    
3731            case PT_LAMP:            case PT_LAMP:
3732            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3733              {              {
3734              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              int chartype;
3735              GETCHARINC(c, eptr);              if (eptr >= md->end_subject)
3736              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);                {
3737              if ((prop_chartype == ucp_Lu ||                SCHECK_PARTIAL();
3738                   prop_chartype == ucp_Ll ||                MRRETURN(MATCH_NOMATCH);
3739                   prop_chartype == ucp_Lt) == prop_fail_result)                }
3740                RRETURN(MATCH_NOMATCH);              GETCHARINCTEST(c, eptr);
3741                chartype = UCD_CHARTYPE(c);
3742                if ((chartype == ucp_Lu ||
3743                     chartype == ucp_Ll ||
3744                     chartype == ucp_Lt) == prop_fail_result)
3745                  MRRETURN(MATCH_NOMATCH);
3746              }              }
3747            break;            break;
3748    
3749            case PT_GC:            case PT_GC:
3750            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3751              {              {
3752              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3753              GETCHARINC(c, eptr);                {
3754              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);                SCHECK_PARTIAL();
3755              if ((prop_category == prop_value) == prop_fail_result)                MRRETURN(MATCH_NOMATCH);
3756                RRETURN(MATCH_NOMATCH);                }
3757                GETCHARINCTEST(c, eptr);
3758                if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result)
3759                  MRRETURN(MATCH_NOMATCH);
3760              }              }
3761            break;            break;
3762    
3763            case PT_PC:            case PT_PC:
3764            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3765              {              {
3766              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3767              GETCHARINC(c, eptr);                {
3768              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);                SCHECK_PARTIAL();
3769              if ((prop_chartype == prop_value) == prop_fail_result)                MRRETURN(MATCH_NOMATCH);
3770                RRETURN(MATCH_NOMATCH);                }
3771                GETCHARINCTEST(c, eptr);
3772                if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result)
3773                  MRRETURN(MATCH_NOMATCH);
3774              }              }
3775            break;            break;
3776    
3777            case PT_SC:            case PT_SC:
3778            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3779              {              {
3780              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3781              GETCHARINC(c, eptr);                {
3782              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);                SCHECK_PARTIAL();
3783              if ((prop_script == prop_value) == prop_fail_result)                MRRETURN(MATCH_NOMATCH);
3784                RRETURN(MATCH_NOMATCH);                }
3785                GETCHARINCTEST(c, eptr);
3786                if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result)
3787                  MRRETURN(MATCH_NOMATCH);
3788                }
3789              break;
3790    
3791              case PT_ALNUM:
3792              for (i = 1; i <= min; i++)
3793                {
3794                int category;
3795                if (eptr >= md->end_subject)
3796                  {
3797                  SCHECK_PARTIAL();
3798                  MRRETURN(MATCH_NOMATCH);
3799                  }
3800                GETCHARINCTEST(c, eptr);
3801                category = UCD_CATEGORY(c);
3802                if ((category == ucp_L || category == ucp_N) == prop_fail_result)
3803                  MRRETURN(MATCH_NOMATCH);
3804                }
3805              break;
3806    
3807              case PT_SPACE:    /* Perl space */
3808              for (i = 1; i <= min; i++)
3809                {
3810                if (eptr >= md->end_subject)
3811                  {
3812                  SCHECK_PARTIAL();
3813                  MRRETURN(MATCH_NOMATCH);
3814                  }
3815                GETCHARINCTEST(c, eptr);
3816                if ((UCD_CATEGORY(c) == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
3817                     c == CHAR_FF || c == CHAR_CR)
3818                       == prop_fail_result)
3819                  MRRETURN(MATCH_NOMATCH);
3820              }              }
3821            break;            break;
3822    
3823              case PT_PXSPACE:  /* POSIX space */
3824              for (i = 1; i <= min; i++)
3825                {
3826                if (eptr >= md->end_subject)
3827                  {
3828                  SCHECK_PARTIAL();
3829                  MRRETURN(MATCH_NOMATCH);
3830                  }
3831                GETCHARINCTEST(c, eptr);
3832                if ((UCD_CATEGORY(c) == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
3833                     c == CHAR_VT || c == CHAR_FF || c == CHAR_CR)
3834                       == prop_fail_result)
3835                  MRRETURN(MATCH_NOMATCH);
3836                }
3837              break;
3838    
3839              case PT_WORD:
3840              for (i = 1; i <= min; i++)
3841                {
3842                int ca