/[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 359 by ph10, Wed Jul 9 16:20:19 2008 UTC revision 617 by ph10, Tue Jul 12 11:00:10 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-2008 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 57  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  /* Flag bits for the match() function */  /* Values for setting in md->match_function_type to indicate two special types
61    of call to match(). We do it this way to save on using another stack variable,
62    as stack usage is to be discouraged. */
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 */
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 71  defined PCRE_ERROR_xxx codes, which are Line 73  defined PCRE_ERROR_xxx codes, which are
73  /* Special internal returns from the match() function. Make them sufficiently  /* Special internal returns from the match() function. Make them sufficiently
74  negative to avoid the external error codes. */  negative to avoid the external error codes. */
75    
76  #define MATCH_COMMIT       (-999)  #define MATCH_ACCEPT       (-999)
77  #define MATCH_PRUNE        (-998)  #define MATCH_COMMIT       (-998)
78  #define MATCH_SKIP         (-997)  #define MATCH_KETRPOS      (-997)
79  #define MATCH_THEN         (-996)  #define MATCH_PRUNE        (-996)
80    #define MATCH_SKIP         (-995)
81    #define MATCH_SKIP_ARG     (-994)
82    #define MATCH_THEN         (-993)
83    
84    /* This is a convenience macro for code that occurs many times. */
85    
86    #define MRRETURN(ra) \
87      { \
88      md->mark = markptr; \
89      RRETURN(ra); \
90      }
91    
92  /* 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.
93  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,
# Line 89  static const char rep_max[] = { 0, 0, 0, Line 102  static const char rep_max[] = { 0, 0, 0,
102    
103    
104    
105  #ifdef DEBUG  #ifdef PCRE_DEBUG
106  /*************************************************  /*************************************************
107  *        Debugging function to print chars       *  *        Debugging function to print chars       *
108  *************************************************/  *************************************************/
# Line 122  while (length-- > 0) Line 135  while (length-- > 0)
135  *          Match a back-reference                *  *          Match a back-reference                *
136  *************************************************/  *************************************************/
137    
138  /* 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
139  than the number of characters left in the string, so the match fails.  negative, so the match always fails. However, in JavaScript compatibility mode,
140    the length passed is zero. Note that in caseless UTF-8 mode, the number of
141    subject bytes matched may be different to the number of reference bytes.
142    
143  Arguments:  Arguments:
144    offset      index into the offset vector    offset      index into the offset vector
145    eptr        points into the subject    eptr        pointer into the subject
146    length      length to be matched    length      length of reference to be matched (number of bytes)
147    md          points to match data block    md          points to match data block
148    ims         the ims flags    caseless    TRUE if caseless
149    
150  Returns:      TRUE if matched  Returns:      < 0 if not matched, otherwise the number of subject bytes matched
151  */  */
152    
153  static BOOL  static int
154  match_ref(int offset, register USPTR eptr, int length, match_data *md,  match_ref(int offset, register USPTR eptr, int length, match_data *md,
155    unsigned long int ims)    BOOL caseless)
156  {  {
157  USPTR p = md->start_subject + md->offset_vector[offset];  USPTR eptr_start = eptr;
158    register USPTR p = md->start_subject + md->offset_vector[offset];
159    
160  #ifdef DEBUG  #ifdef PCRE_DEBUG
161  if (eptr >= md->end_subject)  if (eptr >= md->end_subject)
162    printf("matching subject <null>");    printf("matching subject <null>");
163  else  else
# Line 154  pchars(p, length, FALSE, md); Line 170  pchars(p, length, FALSE, md);
170  printf("\n");  printf("\n");
171  #endif  #endif
172    
173  /* Always fail if not enough characters left */  /* Always fail if reference not set (and not JavaScript compatible). */
174    
175  if (length > md->end_subject - eptr) return FALSE;  if (length < 0) return -1;
176    
177  /* Separate the caseless case for speed. In UTF-8 mode we can only do this  /* Separate the caseless case for speed. In UTF-8 mode we can only do this
178  properly if Unicode properties are supported. Otherwise, we can check only  properly if Unicode properties are supported. Otherwise, we can check only
179  ASCII characters. */  ASCII characters. */
180    
181  if ((ims & PCRE_CASELESS) != 0)  if (caseless)
182    {    {
183  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
184  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
185    if (md->utf8)    if (md->utf8)
186      {      {
187      USPTR endptr = eptr + length;      /* Match characters up to the end of the reference. NOTE: the number of
188      while (eptr < endptr)      bytes matched may differ, because there are some characters whose upper and
189        lower case versions code as different numbers of bytes. For example, U+023A
190        (2 bytes in UTF-8) is the upper case version of U+2C65 (3 bytes in UTF-8);
191        a sequence of 3 of the former uses 6 bytes, as does a sequence of two of
192        the latter. It is important, therefore, to check the length along the
193        reference, not along the subject (earlier code did this wrong). */
194    
195        USPTR endptr = p + length;
196        while (p < endptr)
197        {        {
198        int c, d;        int c, d;
199          if (eptr >= md->end_subject) return -1;
200        GETCHARINC(c, eptr);        GETCHARINC(c, eptr);
201        GETCHARINC(d, p);        GETCHARINC(d, p);
202        if (c != d && c != UCD_OTHERCASE(d)) return FALSE;        if (c != d && c != UCD_OTHERCASE(d)) return -1;
203        }        }
204      }      }
205    else    else
# Line 183  if ((ims & PCRE_CASELESS) != 0) Line 208  if ((ims & PCRE_CASELESS) != 0)
208    
209    /* The same code works when not in UTF-8 mode and in UTF-8 mode when there    /* The same code works when not in UTF-8 mode and in UTF-8 mode when there
210    is no UCP support. */    is no UCP support. */
211        {
212    while (length-- > 0)      if (eptr + length > md->end_subject) return -1;
213      { if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE; }      while (length-- > 0)
214          { if (md->lcc[*p++] != md->lcc[*eptr++]) return -1; }
215        }
216    }    }
217    
218  /* In the caseful case, we can just compare the bytes, whether or not we  /* In the caseful case, we can just compare the bytes, whether or not we
219  are in UTF-8 mode. */  are in UTF-8 mode. */
220    
221  else  else
222    { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }    {
223      if (eptr + length > md->end_subject) return -1;
224      while (length-- > 0) if (*p++ != *eptr++) return -1;
225      }
226    
227  return TRUE;  return eptr - eptr_start;
228  }  }
229    
230    
# Line 245  enum { RM1=1, RM2, RM3, RM4, RM5, RM Line 275  enum { RM1=1, RM2, RM3, RM4, RM5, RM
275         RM21,  RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,         RM21,  RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
276         RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,         RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
277         RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,         RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
278         RM51,  RM52, RM53, RM54 };         RM51,  RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60,
279           RM61,  RM62, RM63};
280    
281  /* 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
282  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
283  actuall used in this definition. */  actually used in this definition. */
284    
285  #ifndef NO_RECURSE  #ifndef NO_RECURSE
286  #define REGISTER register  #define REGISTER register
287    
288  #ifdef DEBUG  #ifdef PCRE_DEBUG
289  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \  #define RMATCH(ra,rb,rc,rd,re,rw) \
290    { \    { \
291    printf("match() called in line %d\n", __LINE__); \    printf("match() called in line %d\n", __LINE__); \
292    rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1); \    rrc = match(ra,rb,mstart,markptr,rc,rd,re,rdepth+1); \
293    printf("to line %d\n", __LINE__); \    printf("to line %d\n", __LINE__); \
294    }    }
295  #define RRETURN(ra) \  #define RRETURN(ra) \
# Line 267  actuall used in this definition. */ Line 298  actuall used in this definition. */
298    return ra; \    return ra; \
299    }    }
300  #else  #else
301  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \  #define RMATCH(ra,rb,rc,rd,re,rw) \
302    rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1)    rrc = match(ra,rb,mstart,markptr,rc,rd,re,rdepth+1)
303  #define RRETURN(ra) return ra  #define RRETURN(ra) return ra
304  #endif  #endif
305    
# Line 281  argument of match(), which never changes Line 312  argument of match(), which never changes
312    
313  #define REGISTER  #define REGISTER
314    
315  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw)\  #define RMATCH(ra,rb,rc,rd,re,rw)\
316    {\    {\
317    heapframe *newframe = (pcre_stack_malloc)(sizeof(heapframe));\    heapframe *newframe = (heapframe *)(pcre_stack_malloc)(sizeof(heapframe));\
318      if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\
319    frame->Xwhere = rw; \    frame->Xwhere = rw; \
320    newframe->Xeptr = ra;\    newframe->Xeptr = ra;\
321    newframe->Xecode = rb;\    newframe->Xecode = rb;\
322    newframe->Xmstart = mstart;\    newframe->Xmstart = mstart;\
323      newframe->Xmarkptr = markptr;\
324    newframe->Xoffset_top = rc;\    newframe->Xoffset_top = rc;\
325    newframe->Xims = re;\    newframe->Xeptrb = re;\
   newframe->Xeptrb = rf;\  
   newframe->Xflags = rg;\  
326    newframe->Xrdepth = frame->Xrdepth + 1;\    newframe->Xrdepth = frame->Xrdepth + 1;\
327    newframe->Xprevframe = frame;\    newframe->Xprevframe = frame;\
328    frame = newframe;\    frame = newframe;\
# Line 303  argument of match(), which never changes Line 334  argument of match(), which never changes
334    
335  #define RRETURN(ra)\  #define RRETURN(ra)\
336    {\    {\
337    heapframe *newframe = frame;\    heapframe *oldframe = frame;\
338    frame = newframe->Xprevframe;\    frame = oldframe->Xprevframe;\
339    (pcre_stack_free)(newframe);\    (pcre_stack_free)(oldframe);\
340    if (frame != NULL)\    if (frame != NULL)\
341      {\      {\
342      rrc = ra;\      rrc = ra;\
# Line 322  typedef struct heapframe { Line 353  typedef struct heapframe {
353    
354    /* Function arguments that may change */    /* Function arguments that may change */
355    
356    const uschar *Xeptr;    USPTR Xeptr;
357    const uschar *Xecode;    const uschar *Xecode;
358    const uschar *Xmstart;    USPTR Xmstart;
359      USPTR Xmarkptr;
360    int Xoffset_top;    int Xoffset_top;
   long int Xims;  
361    eptrblock *Xeptrb;    eptrblock *Xeptrb;
   int Xflags;  
362    unsigned int Xrdepth;    unsigned int Xrdepth;
363    
364    /* Function local variables */    /* Function local variables */
365    
366    const uschar *Xcallpat;    USPTR Xcallpat;
367    const uschar *Xcharptr;  #ifdef SUPPORT_UTF8
368    const uschar *Xdata;    USPTR Xcharptr;
369    const uschar *Xnext;  #endif
370    const uschar *Xpp;    USPTR Xdata;
371    const uschar *Xprev;    USPTR Xnext;
372    const uschar *Xsaved_eptr;    USPTR Xpp;
373      USPTR Xprev;
374      USPTR Xsaved_eptr;
375    
376    recursion_info Xnew_recursive;    recursion_info Xnew_recursive;
377    
# Line 347  typedef struct heapframe { Line 379  typedef struct heapframe {
379    BOOL Xcondition;    BOOL Xcondition;
380    BOOL Xprev_is_word;    BOOL Xprev_is_word;
381    
   unsigned long int Xoriginal_ims;  
   
382  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
383    int Xprop_type;    int Xprop_type;
384    int Xprop_value;    int Xprop_value;
# Line 360  typedef struct heapframe { Line 390  typedef struct heapframe {
390    uschar Xocchars[8];    uschar Xocchars[8];
391  #endif  #endif
392    
393      int Xcodelink;
394    int Xctype;    int Xctype;
395    unsigned int Xfc;    unsigned int Xfc;
396    int Xfi;    int Xfi;
# Line 395  typedef struct heapframe { Line 426  typedef struct heapframe {
426    
427  /* This function is called recursively in many circumstances. Whenever it  /* This function is called recursively in many circumstances. Whenever it
428  returns a negative (error) response, the outer incarnation must also return the  returns a negative (error) response, the outer incarnation must also return the
429  same response.  same response. */
430    
431    /* These macros pack up tests that are used for partial matching, and which
432    appears several times in the code. We set the "hit end" flag if the pointer is
433    at the end of the subject and also past the start of the subject (i.e.
434    something has been matched). For hard partial matching, we then return
435    immediately. The second one is used when we already know we are past the end of
436    the subject. */
437    
438    #define CHECK_PARTIAL()\
439      if (md->partial != 0 && eptr >= md->end_subject && \
440          eptr > md->start_used_ptr) \
441        { \
442        md->hitend = TRUE; \
443        if (md->partial > 1) MRRETURN(PCRE_ERROR_PARTIAL); \
444        }
445    
446    #define SCHECK_PARTIAL()\
447      if (md->partial != 0 && eptr > md->start_used_ptr) \
448        { \
449        md->hitend = TRUE; \
450        if (md->partial > 1) MRRETURN(PCRE_ERROR_PARTIAL); \
451        }
452    
453    
454  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
455  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
456  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
457  made performance worse.  made performance worse.
458    
# Line 407  Arguments: Line 461  Arguments:
461     ecode       pointer to current position in compiled code     ecode       pointer to current position in compiled code
462     mstart      pointer to the current match start position (can be modified     mstart      pointer to the current match start position (can be modified
463                   by encountering \K)                   by encountering \K)
464       markptr     pointer to the most recent MARK name, or NULL
465     offset_top  current top pointer     offset_top  current top pointer
466     md          pointer to "static" info for the match     md          pointer to "static" info for the match
    ims         current /i, /m, and /s options  
467     eptrb       pointer to chain of blocks containing eptr at start of     eptrb       pointer to chain of blocks containing eptr at start of
468                   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  
469     rdepth      the recursion depth     rdepth      the recursion depth
470    
471  Returns:       MATCH_MATCH if matched            )  these values are >= 0  Returns:       MATCH_MATCH if matched            )  these values are >= 0
472                 MATCH_NOMATCH if failed to match  )                 MATCH_NOMATCH if failed to match  )
473                   a negative MATCH_xxx value for PRUNE, SKIP, etc
474                 a negative PCRE_ERROR_xxx value if aborted by an error condition                 a negative PCRE_ERROR_xxx value if aborted by an error condition
475                   (e.g. stopped by repeated call or recursion limit)                   (e.g. stopped by repeated call or recursion limit)
476  */  */
477    
478  static int  static int
479  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, const uschar *mstart,  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, USPTR mstart,
480    int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,    const uschar *markptr, int offset_top, match_data *md, eptrblock *eptrb,
481    int flags, unsigned int rdepth)    unsigned int rdepth)
482  {  {
483  /* 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,
484  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 439  register unsigned int c; /* Character Line 490  register unsigned int c; /* Character
490  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */
491    
492  BOOL minimize, possessive; /* Quantifier options */  BOOL minimize, possessive; /* Quantifier options */
493    BOOL caseless;
494    int condcode;
495    
496  /* 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
497  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 446  heap storage. Set up the top-level frame Line 499  heap storage. Set up the top-level frame
499  heap whenever RMATCH() does a "recursion". See the macro definitions above. */  heap whenever RMATCH() does a "recursion". See the macro definitions above. */
500    
501  #ifdef NO_RECURSE  #ifdef NO_RECURSE
502  heapframe *frame = (pcre_stack_malloc)(sizeof(heapframe));  heapframe *frame = (heapframe *)(pcre_stack_malloc)(sizeof(heapframe));
503    if (frame == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
504  frame->Xprevframe = NULL;            /* Marks the top level */  frame->Xprevframe = NULL;            /* Marks the top level */
505    
506  /* Copy in the original argument variables */  /* Copy in the original argument variables */
# Line 454  frame->Xprevframe = NULL; /* Line 508  frame->Xprevframe = NULL; /*
508  frame->Xeptr = eptr;  frame->Xeptr = eptr;
509  frame->Xecode = ecode;  frame->Xecode = ecode;
510  frame->Xmstart = mstart;  frame->Xmstart = mstart;
511    frame->Xmarkptr = markptr;
512  frame->Xoffset_top = offset_top;  frame->Xoffset_top = offset_top;
 frame->Xims = ims;  
513  frame->Xeptrb = eptrb;  frame->Xeptrb = eptrb;
 frame->Xflags = flags;  
514  frame->Xrdepth = rdepth;  frame->Xrdepth = rdepth;
515    
516  /* This is where control jumps back to to effect "recursion" */  /* This is where control jumps back to to effect "recursion" */
# Line 469  HEAP_RECURSE: Line 522  HEAP_RECURSE:
522  #define eptr               frame->Xeptr  #define eptr               frame->Xeptr
523  #define ecode              frame->Xecode  #define ecode              frame->Xecode
524  #define mstart             frame->Xmstart  #define mstart             frame->Xmstart
525    #define markptr            frame->Xmarkptr
526  #define offset_top         frame->Xoffset_top  #define offset_top         frame->Xoffset_top
 #define ims                frame->Xims  
527  #define eptrb              frame->Xeptrb  #define eptrb              frame->Xeptrb
 #define flags              frame->Xflags  
528  #define rdepth             frame->Xrdepth  #define rdepth             frame->Xrdepth
529    
530  /* Ditto for the local variables */  /* Ditto for the local variables */
# Line 481  HEAP_RECURSE: Line 533  HEAP_RECURSE:
533  #define charptr            frame->Xcharptr  #define charptr            frame->Xcharptr
534  #endif  #endif
535  #define callpat            frame->Xcallpat  #define callpat            frame->Xcallpat
536    #define codelink           frame->Xcodelink
537  #define data               frame->Xdata  #define data               frame->Xdata
538  #define next               frame->Xnext  #define next               frame->Xnext
539  #define pp                 frame->Xpp  #define pp                 frame->Xpp
# Line 493  HEAP_RECURSE: Line 546  HEAP_RECURSE:
546  #define condition          frame->Xcondition  #define condition          frame->Xcondition
547  #define prev_is_word       frame->Xprev_is_word  #define prev_is_word       frame->Xprev_is_word
548    
 #define original_ims       frame->Xoriginal_ims  
   
549  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
550  #define prop_type          frame->Xprop_type  #define prop_type          frame->Xprop_type
551  #define prop_value         frame->Xprop_value  #define prop_value         frame->Xprop_value
# Line 531  i, and fc and c, can be the same variabl Line 582  i, and fc and c, can be the same variabl
582  #define fi i  #define fi i
583  #define fc c  #define fc c
584    
585    /* Many of the following variables are used only in small blocks of the code.
586  #ifdef SUPPORT_UTF8                /* Many of these variables are used only  */  My normal style of coding would have declared them within each of those blocks.
587  const uschar *charptr;             /* in small blocks of the code. My normal */  However, in order to accommodate the version of this code that uses an external
588  #endif                             /* style of coding would have declared    */  "stack" implemented on the heap, it is easier to declare them all here, so the
589  const uschar *callpat;             /* them within each of those blocks.      */  declarations can be cut out in a block. The only declarations within blocks
590  const uschar *data;                /* However, in order to accommodate the   */  below are for variables that do not have to be preserved over a recursive call
591  const uschar *next;                /* version of this code that uses an      */  to RMATCH(). */
592  USPTR         pp;                  /* external "stack" implemented on the    */  
593  const uschar *prev;                /* heap, it is easier to declare them all */  #ifdef SUPPORT_UTF8
594  USPTR         saved_eptr;          /* here, so the declarations can be cut   */  const uschar *charptr;
595                                     /* out in a block. The only declarations  */  #endif
596  recursion_info new_recursive;      /* within blocks below are for variables  */  const uschar *callpat;
597                                     /* that do not have to be preserved over  */  const uschar *data;
598  BOOL cur_is_word;                  /* a recursive call to RMATCH().          */  const uschar *next;
599    USPTR         pp;
600    const uschar *prev;
601    USPTR         saved_eptr;
602    
603    recursion_info new_recursive;
604    
605    BOOL cur_is_word;
606  BOOL condition;  BOOL condition;
607  BOOL prev_is_word;  BOOL prev_is_word;
608    
 unsigned long int original_ims;  
   
609  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
610  int prop_type;  int prop_type;
611  int prop_value;  int prop_value;
# Line 561  int oclength; Line 617  int oclength;
617  uschar occhars[8];  uschar occhars[8];
618  #endif  #endif
619    
620    int codelink;
621  int ctype;  int ctype;
622  int length;  int length;
623  int max;  int max;
# Line 575  int stacksave[REC_STACK_SAVE_MAX]; Line 632  int stacksave[REC_STACK_SAVE_MAX];
632  eptrblock newptrb;  eptrblock newptrb;
633  #endif     /* NO_RECURSE */  #endif     /* NO_RECURSE */
634    
635    /* To save space on the stack and in the heap frame, I have doubled up on some
636    of the local variables that are used only in localised parts of the code, but
637    still need to be preserved over recursive calls of match(). These macros define
638    the alternative names that are used. */
639    
640    #define allow_zero    cur_is_word
641    #define cbegroup      condition
642    #define code_offset   codelink
643    #define condassert    condition
644    #define matched_once  prev_is_word
645    
646  /* These statements are here to stop the compiler complaining about unitialized  /* These statements are here to stop the compiler complaining about unitialized
647  variables. */  variables. */
648    
# Line 594  TAIL_RECURSE: Line 662  TAIL_RECURSE:
662  /* 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
663  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
664  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()
665  and a "return", respectively (possibly with some debugging if DEBUG is  and a "return", respectively (possibly with some debugging if PCRE_DEBUG is
666  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
667  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,
668  however, impact performance when true recursion is being used. */  however, impact performance when true recursion is being used. */
# Line 611  haven't exceeded the recursive call limi Line 679  haven't exceeded the recursive call limi
679  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
680  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
681    
 original_ims = ims;    /* Save for resetting on ')' */  
   
682  /* 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
683  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
684  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
685  hit the closing ket, in order to break infinite loops that match no characters.  up space on the stack. See also MATCH_CONDASSERT below.
686  When match() is called in other circumstances, don't add to the chain. The  
687  match_cbegroup flag must NOT be used with tail recursion, because the memory  When MATCH_CBEGROUP is set, add the current subject pointer to the chain of
688  block that is used is on the stack, so a new one may be required for each  such remembered pointers, to be checked when we hit the closing ket, in order
689  match(). */  to break infinite loops that match no characters. When match() is called in
690    other circumstances, don't add to the chain. The MATCH_CBEGROUP feature must
691    NOT be used with tail recursion, because the memory block that is used is on
692    the stack, so a new one may be required for each match(). */
693    
694  if ((flags & match_cbegroup) != 0)  if (md->match_function_type == MATCH_CBEGROUP)
695    {    {
696    newptrb.epb_saved_eptr = eptr;    newptrb.epb_saved_eptr = eptr;
697    newptrb.epb_prev = eptrb;    newptrb.epb_prev = eptrb;
698    eptrb = &newptrb;    eptrb = &newptrb;
699      md->match_function_type = 0;
700    }    }
701    
702  /* Now start processing the opcodes. */  /* Now start processing the opcodes. */
# Line 635  for (;;) Line 705  for (;;)
705    {    {
706    minimize = possessive = FALSE;    minimize = possessive = FALSE;
707    op = *ecode;    op = *ecode;
708    
   /* 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;  
   
709    switch(op)    switch(op)
710      {      {
711        case OP_MARK:
712        markptr = ecode + 2;
713        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
714          eptrb, RM55);
715    
716        /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
717        argument, and we must check whether that argument matches this MARK's
718        argument. It is passed back in md->start_match_ptr (an overloading of that
719        variable). If it does match, we reset that variable to the current subject
720        position and return MATCH_SKIP. Otherwise, pass back the return code
721        unaltered. */
722    
723        if (rrc == MATCH_SKIP_ARG &&
724            strcmp((char *)markptr, (char *)(md->start_match_ptr)) == 0)
725          {
726          md->start_match_ptr = eptr;
727          RRETURN(MATCH_SKIP);
728          }
729    
730        if (md->mark == NULL) md->mark = markptr;
731        RRETURN(rrc);
732    
733      case OP_FAIL:      case OP_FAIL:
734      RRETURN(MATCH_NOMATCH);      MRRETURN(MATCH_NOMATCH);
735    
736        /* COMMIT overrides PRUNE, SKIP, and THEN */
737    
738        case OP_COMMIT:
739        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
740          eptrb, RM52);
741        if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE &&
742            rrc != MATCH_SKIP && rrc != MATCH_SKIP_ARG &&
743            rrc != MATCH_THEN)
744          RRETURN(rrc);
745        MRRETURN(MATCH_COMMIT);
746    
747        /* PRUNE overrides THEN */
748    
749      case OP_PRUNE:      case OP_PRUNE:
750      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
751        ims, eptrb, flags, RM51);        eptrb, RM51);
752      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
753        MRRETURN(MATCH_PRUNE);
754    
755        case OP_PRUNE_ARG:
756        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
757          eptrb, RM56);
758        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
759        md->mark = ecode + 2;
760      RRETURN(MATCH_PRUNE);      RRETURN(MATCH_PRUNE);
761    
762      case OP_COMMIT:      /* SKIP overrides PRUNE and THEN */
     RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,  
       ims, eptrb, flags, RM52);  
     if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
     RRETURN(MATCH_COMMIT);  
763    
764      case OP_SKIP:      case OP_SKIP:
765      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
766        ims, eptrb, flags, RM53);        eptrb, RM53);
767      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
768          RRETURN(rrc);
769      md->start_match_ptr = eptr;   /* Pass back current position */      md->start_match_ptr = eptr;   /* Pass back current position */
770      RRETURN(MATCH_SKIP);      MRRETURN(MATCH_SKIP);
771    
772        case OP_SKIP_ARG:
773        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
774          eptrb, RM57);
775        if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
776          RRETURN(rrc);
777    
778        /* Pass back the current skip name by overloading md->start_match_ptr and
779        returning the special MATCH_SKIP_ARG return code. This will either be
780        caught by a matching MARK, or get to the top, where it is treated the same
781        as PRUNE. */
782    
783        md->start_match_ptr = ecode + 2;
784        RRETURN(MATCH_SKIP_ARG);
785    
786        /* For THEN (and THEN_ARG) we pass back the address of the bracket or
787        the alt that is at the start of the current branch. This makes it possible
788        to skip back past alternatives that precede the THEN within the current
789        branch. */
790    
791      case OP_THEN:      case OP_THEN:
792      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
793        ims, eptrb, flags, RM54);        eptrb, RM54);
794      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
795        md->start_match_ptr = ecode - GET(ecode, 1);
796        MRRETURN(MATCH_THEN);
797    
798        case OP_THEN_ARG:
799        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1+LINK_SIZE],
800          offset_top, md, eptrb, RM58);
801        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
802        md->start_match_ptr = ecode - GET(ecode, 1);
803        md->mark = ecode + LINK_SIZE + 2;
804      RRETURN(MATCH_THEN);      RRETURN(MATCH_THEN);
805    
806      /* Handle a capturing bracket. If there is space in the offset vector, save      /* Handle a capturing bracket, other than those that are possessive with an
807      the current subject position in the working slot at the top of the vector.      unlimited repeat. If there is space in the offset vector, save the current
808      We mustn't change the current values of the data slot, because they may be      subject position in the working slot at the top of the vector. We mustn't
809      set from a previous iteration of this group, and be referred to by a      change the current values of the data slot, because they may be set from a
810      reference inside the group.      previous iteration of this group, and be referred to by a reference inside
811        the group. A failure to match might occur after the group has succeeded,
812      If the bracket fails to match, we need to restore this value and also the      if something later on doesn't match. For this reason, we need to restore
813      values of the final offsets, in case they were set by a previous iteration      the working value and also the values of the final offsets, in case they
814      of the same bracket.      were set by a previous iteration of the same bracket.
815    
816      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
817      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 692  for (;;) Line 821  for (;;)
821      case OP_SCBRA:      case OP_SCBRA:
822      number = GET2(ecode, 1+LINK_SIZE);      number = GET2(ecode, 1+LINK_SIZE);
823      offset = number << 1;      offset = number << 1;
824    
825  #ifdef DEBUG  #ifdef PCRE_DEBUG
826      printf("start bracket %d\n", number);      printf("start bracket %d\n", number);
827      printf("subject=");      printf("subject=");
828      pchars(eptr, 16, TRUE, md);      pchars(eptr, 16, TRUE, md);
# Line 708  for (;;) Line 837  for (;;)
837        save_capture_last = md->capture_last;        save_capture_last = md->capture_last;
838    
839        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
840        md->offset_vector[md->offset_end - number] = eptr - md->start_subject;        md->offset_vector[md->offset_end - number] =
841            (int)(eptr - md->start_subject);
842    
843        flags = (op == OP_SCBRA)? match_cbegroup : 0;        for (;;)
       do  
844          {          {
845          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,          if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
846            ims, eptrb, flags, RM1);          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
847          if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);            eptrb, RM1);
848            if (rrc != MATCH_NOMATCH &&
849                (rrc != MATCH_THEN || md->start_match_ptr != ecode))
850              RRETURN(rrc);
851          md->capture_last = save_capture_last;          md->capture_last = save_capture_last;
852          ecode += GET(ecode, 1);          ecode += GET(ecode, 1);
853            if (*ecode != OP_ALT) break;
854          }          }
       while (*ecode == OP_ALT);  
855    
856        DPRINTF(("bracket %d failed\n", number));        DPRINTF(("bracket %d failed\n", number));
857    
# Line 727  for (;;) Line 859  for (;;)
859        md->offset_vector[offset+1] = save_offset2;        md->offset_vector[offset+1] = save_offset2;
860        md->offset_vector[md->offset_end - number] = save_offset3;        md->offset_vector[md->offset_end - number] = save_offset3;
861    
862          if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
863        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
864        }        }
865    
# Line 741  for (;;) Line 874  for (;;)
874      /* VVVVVVVVVVVVVVVVVVVVVVVVV */      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
875      /* VVVVVVVVVVVVVVVVVVVVVVVVV */      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
876    
877      /* Non-capturing bracket. Loop for all the alternatives. When we get to the      /* Non-capturing bracket, except for possessive with unlimited repeat. Loop
878      final alternative within the brackets, we would return the result of a      for all the alternatives. When we get to the final alternative within the
879      recursive call to match() whatever happened. We can reduce stack usage by      brackets, we used to return the result of a recursive call to match()
880      turning this into a tail recursion, except in the case when match_cbegroup      whatever happened so it was possible to reduce stack usage by turning this
881      is set.*/      into a tail recursion, except in the case of a possibly empty group.
882        However, now that there is the possiblity of (*THEN) occurring in the final
883        alternative, this optimization is no longer possible. */
884    
885      case OP_BRA:      case OP_BRA:
886      case OP_SBRA:      case OP_SBRA:
887      DPRINTF(("start non-capturing bracket\n"));      DPRINTF(("start non-capturing bracket\n"));
     flags = (op >= OP_SBRA)? match_cbegroup : 0;  
888      for (;;)      for (;;)
889        {        {
890        if (ecode[GET(ecode, 1)] != OP_ALT)   /* Final alternative */        if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
891          {        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, eptrb,
892          if (flags == 0)    /* Not a possibly empty group */          RM2);
893            {        if (rrc != MATCH_NOMATCH &&
894            ecode += _pcre_OP_lengths[*ecode];            (rrc != MATCH_THEN || md->start_match_ptr != ecode))
895            DPRINTF(("bracket 0 tail recursion\n"));          RRETURN(rrc);
896            goto TAIL_RECURSE;        ecode += GET(ecode, 1);
897            }        if (*ecode != OP_ALT) break;
898          }
899    
900          /* Possibly empty group; can't use tail recursion. */      if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
901        RRETURN(MATCH_NOMATCH);
902    
903          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,      /* Handle possessive capturing brackets with an unlimited repeat. We come
904            eptrb, flags, RM48);      here from BRAZERO with allow_zero set TRUE. The offset_vector values are
905          RRETURN(rrc);      handled similarly to the normal case above. However, the matching is
906        different. The end of these brackets will always be OP_KETRPOS, which
907        returns MATCH_KETRPOS without going further in the pattern. By this means
908        we can handle the group by iteration rather than recursion, thereby
909        reducing the amount of stack needed. */
910    
911        case OP_CBRAPOS:
912        case OP_SCBRAPOS:
913        allow_zero = FALSE;
914    
915        POSSESSIVE_CAPTURE:
916        number = GET2(ecode, 1+LINK_SIZE);
917        offset = number << 1;
918    
919    #ifdef PCRE_DEBUG
920        printf("start possessive bracket %d\n", number);
921        printf("subject=");
922        pchars(eptr, 16, TRUE, md);
923        printf("\n");
924    #endif
925    
926        if (offset < md->offset_max)
927          {
928          matched_once = FALSE;
929          code_offset = ecode - md->start_code;
930    
931          save_offset1 = md->offset_vector[offset];
932          save_offset2 = md->offset_vector[offset+1];
933          save_offset3 = md->offset_vector[md->offset_end - number];
934          save_capture_last = md->capture_last;
935    
936          DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
937    
938          /* Each time round the loop, save the current subject position for use
939          when the group matches. For MATCH_MATCH, the group has matched, so we
940          restart it with a new subject starting position, remembering that we had
941          at least one match. For MATCH_NOMATCH, carry on with the alternatives, as
942          usual. If we haven't matched any alternatives in any iteration, check to
943          see if a previous iteration matched. If so, the group has matched;
944          continue from afterwards. Otherwise it has failed; restore the previous
945          capture values before returning NOMATCH. */
946    
947          for (;;)
948            {
949            md->offset_vector[md->offset_end - number] =
950              (int)(eptr - md->start_subject);
951            if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
952            RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
953              eptrb, RM63);
954            if (rrc == MATCH_KETRPOS)
955              {
956              offset_top = md->end_offset_top;
957              eptr = md->end_match_ptr;
958              ecode = md->start_code + code_offset;
959              save_capture_last = md->capture_last;
960              matched_once = TRUE;
961              continue;
962              }
963            if (rrc != MATCH_NOMATCH &&
964                (rrc != MATCH_THEN || md->start_match_ptr != ecode))
965              RRETURN(rrc);
966            md->capture_last = save_capture_last;
967            ecode += GET(ecode, 1);
968            if (*ecode != OP_ALT) break;
969          }          }
970    
971        /* For non-final alternatives, continue the loop for a NOMATCH result;        if (!matched_once)
972        otherwise return. */          {
973            md->offset_vector[offset] = save_offset1;
974            md->offset_vector[offset+1] = save_offset2;
975            md->offset_vector[md->offset_end - number] = save_offset3;
976            }
977    
978          if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
979          if (allow_zero || matched_once)
980            {
981            ecode += 1 + LINK_SIZE;
982            break;
983            }
984    
985          RRETURN(MATCH_NOMATCH);
986          }
987    
988        /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
989        as a non-capturing bracket. */
990    
991        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
992        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
993    
994        DPRINTF(("insufficient capture room: treat as non-capturing\n"));
995    
996        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
997        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
998    
999        /* Non-capturing possessive bracket with unlimited repeat. We come here
1000        from BRAZERO with allow_zero = TRUE. The code is similar to the above,
1001        without the capturing complication. It is written out separately for speed
1002        and cleanliness. */
1003    
1004        case OP_BRAPOS:
1005        case OP_SBRAPOS:
1006        allow_zero = FALSE;
1007    
1008        POSSESSIVE_NON_CAPTURE:
1009        matched_once = FALSE;
1010        code_offset = ecode - md->start_code;
1011    
1012        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,      for (;;)
1013          eptrb, flags, RM2);        {
1014        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);        if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1015          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
1016            eptrb, RM48);
1017          if (rrc == MATCH_KETRPOS)
1018            {
1019            offset_top = md->end_offset_top;
1020            eptr = md->end_match_ptr;
1021            ecode = md->start_code + code_offset;
1022            matched_once = TRUE;
1023            continue;
1024            }
1025          if (rrc != MATCH_NOMATCH &&
1026              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1027            RRETURN(rrc);
1028        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1029          if (*ecode != OP_ALT) break;
1030        }        }
1031    
1032        if (matched_once || allow_zero)
1033          {
1034          ecode += 1 + LINK_SIZE;
1035          break;
1036          }
1037        RRETURN(MATCH_NOMATCH);
1038    
1039      /* Control never reaches here. */      /* Control never reaches here. */
1040    
1041      /* Conditional group: compilation checked that there are no more than      /* Conditional group: compilation checked that there are no more than
1042      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
1043      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
1044      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. */  
1045    
1046      case OP_COND:      case OP_COND:
1047      case OP_SCOND:      case OP_SCOND:
1048      if (ecode[LINK_SIZE+1] == OP_RREF)         /* Recursion test */      codelink = GET(ecode, 1);
1049    
1050        /* Because of the way auto-callout works during compile, a callout item is
1051        inserted between OP_COND and an assertion condition. */
1052    
1053        if (ecode[LINK_SIZE+1] == OP_CALLOUT)
1054        {        {
1055        offset = GET2(ecode, LINK_SIZE + 2);     /* Recursion group number*/        if (pcre_callout != NULL)
1056        condition = md->recursive != NULL &&          {
1057          (offset == RREF_ANY || offset == md->recursive->group_num);          pcre_callout_block cb;
1058        ecode += condition? 3 : GET(ecode, 1);          cb.version          = 1;   /* Version 1 of the callout block */
1059            cb.callout_number   = ecode[LINK_SIZE+2];
1060            cb.offset_vector    = md->offset_vector;
1061            cb.subject          = (PCRE_SPTR)md->start_subject;
1062            cb.subject_length   = (int)(md->end_subject - md->start_subject);
1063            cb.start_match      = (int)(mstart - md->start_subject);
1064            cb.current_position = (int)(eptr - md->start_subject);
1065            cb.pattern_position = GET(ecode, LINK_SIZE + 3);
1066            cb.next_item_length = GET(ecode, 3 + 2*LINK_SIZE);
1067            cb.capture_top      = offset_top/2;
1068            cb.capture_last     = md->capture_last;
1069            cb.callout_data     = md->callout_data;
1070            if ((rrc = (*pcre_callout)(&cb)) > 0) MRRETURN(MATCH_NOMATCH);
1071            if (rrc < 0) RRETURN(rrc);
1072            }
1073          ecode += _pcre_OP_lengths[OP_CALLOUT];
1074          }
1075    
1076        condcode = ecode[LINK_SIZE+1];
1077    
1078        /* Now see what the actual condition is */
1079    
1080        if (condcode == OP_RREF || condcode == OP_NRREF)    /* Recursion test */
1081          {
1082          if (md->recursive == NULL)                /* Not recursing => FALSE */
1083            {
1084            condition = FALSE;
1085            ecode += GET(ecode, 1);
1086            }
1087          else
1088            {
1089            int recno = GET2(ecode, LINK_SIZE + 2);   /* Recursion group number*/
1090            condition =  (recno == RREF_ANY || recno == md->recursive->group_num);
1091    
1092            /* If the test is for recursion into a specific subpattern, and it is
1093            false, but the test was set up by name, scan the table to see if the
1094            name refers to any other numbers, and test them. The condition is true
1095            if any one is set. */
1096    
1097            if (!condition && condcode == OP_NRREF && recno != RREF_ANY)
1098              {
1099              uschar *slotA = md->name_table;
1100              for (i = 0; i < md->name_count; i++)
1101                {
1102                if (GET2(slotA, 0) == recno) break;
1103                slotA += md->name_entry_size;
1104                }
1105    
1106              /* Found a name for the number - there can be only one; duplicate
1107              names for different numbers are allowed, but not vice versa. First
1108              scan down for duplicates. */
1109    
1110              if (i < md->name_count)
1111                {
1112                uschar *slotB = slotA;
1113                while (slotB > md->name_table)
1114                  {
1115                  slotB -= md->name_entry_size;
1116                  if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1117                    {
1118                    condition = GET2(slotB, 0) == md->recursive->group_num;
1119                    if (condition) break;
1120                    }
1121                  else break;
1122                  }
1123    
1124                /* Scan up for duplicates */
1125    
1126                if (!condition)
1127                  {
1128                  slotB = slotA;
1129                  for (i++; i < md->name_count; i++)
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                }
1141              }
1142    
1143            /* Chose branch according to the condition */
1144    
1145            ecode += condition? 3 : GET(ecode, 1);
1146            }
1147        }        }
1148    
1149      else if (ecode[LINK_SIZE+1] == OP_CREF)    /* Group used test */      else if (condcode == OP_CREF || condcode == OP_NCREF)  /* Group used test */
1150        {        {
1151        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */
1152        condition = offset < offset_top && md->offset_vector[offset] >= 0;        condition = offset < offset_top && md->offset_vector[offset] >= 0;
1153    
1154          /* If the numbered capture is unset, but the reference was by name,
1155          scan the table to see if the name refers to any other numbers, and test
1156          them. The condition is true if any one is set. This is tediously similar
1157          to the code above, but not close enough to try to amalgamate. */
1158    
1159          if (!condition && condcode == OP_NCREF)
1160            {
1161            int refno = offset >> 1;
1162            uschar *slotA = md->name_table;
1163    
1164            for (i = 0; i < md->name_count; i++)
1165              {
1166              if (GET2(slotA, 0) == refno) break;
1167              slotA += md->name_entry_size;
1168              }
1169    
1170            /* Found a name for the number - there can be only one; duplicate names
1171            for different numbers are allowed, but not vice versa. First scan down
1172            for duplicates. */
1173    
1174            if (i < md->name_count)
1175              {
1176              uschar *slotB = slotA;
1177              while (slotB > md->name_table)
1178                {
1179                slotB -= md->name_entry_size;
1180                if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1181                  {
1182                  offset = GET2(slotB, 0) << 1;
1183                  condition = offset < offset_top &&
1184                    md->offset_vector[offset] >= 0;
1185                  if (condition) break;
1186                  }
1187                else break;
1188                }
1189    
1190              /* Scan up for duplicates */
1191    
1192              if (!condition)
1193                {
1194                slotB = slotA;
1195                for (i++; i < md->name_count; i++)
1196                  {
1197                  slotB += md->name_entry_size;
1198                  if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1199                    {
1200                    offset = GET2(slotB, 0) << 1;
1201                    condition = offset < offset_top &&
1202                      md->offset_vector[offset] >= 0;
1203                    if (condition) break;
1204                    }
1205                  else break;
1206                  }
1207                }
1208              }
1209            }
1210    
1211          /* Chose branch according to the condition */
1212    
1213        ecode += condition? 3 : GET(ecode, 1);        ecode += condition? 3 : GET(ecode, 1);
1214        }        }
1215    
1216      else if (ecode[LINK_SIZE+1] == OP_DEF)     /* DEFINE - always false */      else if (condcode == OP_DEF)     /* DEFINE - always false */
1217        {        {
1218        condition = FALSE;        condition = FALSE;
1219        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1220        }        }
1221    
1222      /* The condition is an assertion. Call match() to evaluate it - setting      /* The condition is an assertion. Call match() to evaluate it - setting
1223      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
1224      assertion. */      an assertion. */
1225    
1226      else      else
1227        {        {
1228        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        md->match_function_type = MATCH_CONDASSERT;
1229            match_condassert, RM3);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM3);
1230        if (rrc == MATCH_MATCH)        if (rrc == MATCH_MATCH)
1231          {          {
1232          condition = TRUE;          condition = TRUE;
1233          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
1234          while (*ecode == OP_ALT) ecode += GET(ecode, 1);          while (*ecode == OP_ALT) ecode += GET(ecode, 1);
1235          }          }
1236        else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)        else if (rrc != MATCH_NOMATCH &&
1237                  (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1238          {          {
1239          RRETURN(rrc);         /* Need braces because of following else */          RRETURN(rrc);         /* Need braces because of following else */
1240          }          }
1241        else        else
1242          {          {
1243          condition = FALSE;          condition = FALSE;
1244          ecode += GET(ecode, 1);          ecode += codelink;
1245          }          }
1246        }        }
1247    
1248      /* 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,
1249      we can use tail recursion to avoid using another stack frame, except when      we used to use tail recursion to avoid using another stack frame, except
1250      match_cbegroup is required for an unlimited repeat of a possibly empty      when there was unlimited repeat of a possibly empty group. However, that
1251      group. If the second alternative doesn't exist, we can just plough on. */      strategy no longer works because of the possibilty of (*THEN) being
1252        encountered in the branch. A recursive call to match() is always required,
1253        unless the second alternative doesn't exist, in which case we can just
1254        plough on. */
1255    
1256      if (condition || *ecode == OP_ALT)      if (condition || *ecode == OP_ALT)
1257        {        {
1258        ecode += 1 + LINK_SIZE;        if (op == OP_SCOND) md->match_function_type = MATCH_CBEGROUP;
1259        if (op == OP_SCOND)        /* Possibly empty group */        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM49);
1260          {        if (rrc == MATCH_THEN && md->start_match_ptr == ecode)
1261          RMATCH(eptr, ecode, offset_top, md, ims, eptrb, match_cbegroup, RM49);          rrc = MATCH_NOMATCH;
1262          RRETURN(rrc);        RRETURN(rrc);
         }  
       else                       /* Group must match something */  
         {  
         flags = 0;  
         goto TAIL_RECURSE;  
         }  
1263        }        }
1264      else                         /* Condition false & no 2nd alternative */      else                         /* Condition false & no alternative */
1265        {        {
1266        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1267        }        }
1268      break;      break;
1269    
1270    
1271      /* End of the pattern, either real or forced. If we are in a top-level      /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
1272      recursion, we should restore the offsets appropriately and continue from      to close any currently open capturing brackets. */
1273      after the call. */  
1274        case OP_CLOSE:
1275        number = GET2(ecode, 1);
1276        offset = number << 1;
1277    
1278    #ifdef PCRE_DEBUG
1279          printf("end bracket %d at *ACCEPT", number);
1280          printf("\n");
1281    #endif
1282    
1283        md->capture_last = number;
1284        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1285          {
1286          md->offset_vector[offset] =
1287            md->offset_vector[md->offset_end - number];
1288          md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1289          if (offset_top <= offset) offset_top = offset + 2;
1290          }
1291        ecode += 3;
1292        break;
1293    
1294    
1295        /* End of the pattern, either real or forced. If we are in a recursion, we
1296        should restore the offsets appropriately, and if it's a top-level
1297        recursion, continue from after the call. */
1298    
1299      case OP_ACCEPT:      case OP_ACCEPT:
1300        case OP_ASSERT_ACCEPT:
1301      case OP_END:      case OP_END:
1302      if (md->recursive != NULL && md->recursive->group_num == 0)      if (md->recursive != NULL)
1303        {        {
1304        recursion_info *rec = md->recursive;        recursion_info *rec = md->recursive;
       DPRINTF(("End of pattern in a (?0) recursion\n"));  
1305        md->recursive = rec->prevrec;        md->recursive = rec->prevrec;
1306        memmove(md->offset_vector, rec->offset_save,        memmove(md->offset_vector, rec->offset_save,
1307          rec->saved_max * sizeof(int));          rec->saved_max * sizeof(int));
1308        mstart = rec->save_start;        offset_top = rec->save_offset_top;
1309        ims = original_ims;        if (rec->group_num == 0)
1310        ecode = rec->after_call;          {
1311        break;          ecode = rec->after_call;
1312            break;
1313            }
1314        }        }
1315    
1316      /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty      /* Otherwise, if we have matched an empty string, fail if not in an
1317      string - backtracking will then try other alternatives, if any. */      assertion and if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART
1318        is set and we have matched at the start of the subject. In both cases,
1319        backtracking will then try other alternatives, if any. */
1320    
1321        else if (eptr == mstart && op != OP_ASSERT_ACCEPT &&
1322            (md->notempty ||
1323              (md->notempty_atstart &&
1324                mstart == md->start_subject + md->start_offset)))
1325          MRRETURN(MATCH_NOMATCH);
1326    
1327      if (md->notempty && eptr == mstart) RRETURN(MATCH_NOMATCH);      /* Otherwise, we have a match. */
1328    
1329      md->end_match_ptr = eptr;           /* Record where we ended */      md->end_match_ptr = eptr;           /* Record where we ended */
1330      md->end_offset_top = offset_top;    /* and how many extracts were taken */      md->end_offset_top = offset_top;    /* and how many extracts were taken */
1331      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);  
1332    
1333      /* Change option settings */      /* For some reason, the macros don't work properly if an expression is
1334        given as the argument to MRRETURN when the heap is in use. */
1335    
1336      case OP_OPT:      rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
1337      ims = ecode[1];      MRRETURN(rrc);
     ecode += 2;  
     DPRINTF(("ims set to %02lx\n", ims));  
     break;  
1338    
1339      /* Assertion brackets. Check the alternative branches in turn - the      /* Assertion brackets. Check the alternative branches in turn - the
1340      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,
1341      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
1342      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
1343      this level is identical to the lookahead case. */      this level is identical to the lookahead case. When the assertion is part
1344        of a condition, we want to return immediately afterwards. The caller of
1345        this incarnation of the match() function will have set MATCH_CONDASSERT in
1346        md->match_function type, and one of these opcodes will be the first opcode
1347        that is processed. We use a local variable that is preserved over calls to
1348        match() to remember this case. */
1349    
1350      case OP_ASSERT:      case OP_ASSERT:
1351      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1352        if (md->match_function_type == MATCH_CONDASSERT)
1353          {
1354          condassert = TRUE;
1355          md->match_function_type = 0;
1356          }
1357        else condassert = FALSE;
1358    
1359      do      do
1360        {        {
1361        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
1362          RM4);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1363        if (rrc == MATCH_MATCH) break;          {
1364        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);          mstart = md->start_match_ptr;   /* In case \K reset it */
1365            break;
1366            }
1367          if (rrc != MATCH_NOMATCH &&
1368              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1369            RRETURN(rrc);
1370        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1371        }        }
1372      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1373      if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);  
1374        if (*ecode == OP_KET) MRRETURN(MATCH_NOMATCH);
1375    
1376      /* If checking an assertion for a condition, return MATCH_MATCH. */      /* If checking an assertion for a condition, return MATCH_MATCH. */
1377    
1378      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);
1379    
1380      /* Continue from after the assertion, updating the offsets high water      /* Continue from after the assertion, updating the offsets high water
1381      mark, since extracts may have been taken during the assertion. */      mark, since extracts may have been taken during the assertion. */
# Line 926  for (;;) Line 1385  for (;;)
1385      offset_top = md->end_offset_top;      offset_top = md->end_offset_top;
1386      continue;      continue;
1387    
1388      /* Negative assertion: all branches must fail to match */      /* Negative assertion: all branches must fail to match. Encountering SKIP,
1389        PRUNE, or COMMIT means we must assume failure without checking subsequent
1390        branches. */
1391    
1392      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1393      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1394        if (md->match_function_type == MATCH_CONDASSERT)
1395          {
1396          condassert = TRUE;
1397          md->match_function_type = 0;
1398          }
1399        else condassert = FALSE;
1400    
1401      do      do
1402        {        {
1403        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
1404          RM5);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) MRRETURN(MATCH_NOMATCH);
1405        if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);        if (rrc == MATCH_SKIP || rrc == MATCH_PRUNE || rrc == MATCH_COMMIT)
1406        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);          {
1407            do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1408            break;
1409            }
1410          if (rrc != MATCH_NOMATCH &&
1411              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1412            RRETURN(rrc);
1413        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1414        }        }
1415      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1416    
1417      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);  /* Condition assertion */
1418    
1419      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1420      continue;      continue;
1421    
# Line 958  for (;;) Line 1432  for (;;)
1432        while (i-- > 0)        while (i-- > 0)
1433          {          {
1434          eptr--;          eptr--;
1435          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);          if (eptr < md->start_subject) MRRETURN(MATCH_NOMATCH);
1436          BACKCHAR(eptr);          BACKCHAR(eptr);
1437          }          }
1438        }        }
# Line 969  for (;;) Line 1443  for (;;)
1443    
1444        {        {
1445        eptr -= GET(ecode, 1);        eptr -= GET(ecode, 1);
1446        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);        if (eptr < md->start_subject) MRRETURN(MATCH_NOMATCH);
1447        }        }
1448    
1449      /* Skip to next op code */      /* Save the earliest consulted character, then skip to next op code */
1450    
1451        if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
1452      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1453      break;      break;
1454    
# Line 989  for (;;) Line 1464  for (;;)
1464        cb.callout_number   = ecode[1];        cb.callout_number   = ecode[1];
1465        cb.offset_vector    = md->offset_vector;        cb.offset_vector    = md->offset_vector;
1466        cb.subject          = (PCRE_SPTR)md->start_subject;        cb.subject          = (PCRE_SPTR)md->start_subject;
1467        cb.subject_length   = md->end_subject - md->start_subject;        cb.subject_length   = (int)(md->end_subject - md->start_subject);
1468        cb.start_match      = mstart - md->start_subject;        cb.start_match      = (int)(mstart - md->start_subject);
1469        cb.current_position = eptr - md->start_subject;        cb.current_position = (int)(eptr - md->start_subject);
1470        cb.pattern_position = GET(ecode, 2);        cb.pattern_position = GET(ecode, 2);
1471        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
1472        cb.capture_top      = offset_top/2;        cb.capture_top      = offset_top/2;
1473        cb.capture_last     = md->capture_last;        cb.capture_last     = md->capture_last;
1474        cb.callout_data     = md->callout_data;        cb.callout_data     = md->callout_data;
1475        if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);        if ((rrc = (*pcre_callout)(&cb)) > 0) MRRETURN(MATCH_NOMATCH);
1476        if (rrc < 0) RRETURN(rrc);        if (rrc < 0) RRETURN(rrc);
1477        }        }
1478      ecode += 2 + 2*LINK_SIZE;      ecode += 2 + 2*LINK_SIZE;
# Line 1014  for (;;) Line 1489  for (;;)
1489      65535 such values, which is too large to put on the stack, but using malloc      65535 such values, which is too large to put on the stack, but using malloc
1490      for small numbers seems expensive. As a compromise, the stack is used when      for small numbers seems expensive. As a compromise, the stack is used when
1491      there are no more than REC_STACK_SAVE_MAX values to store; otherwise malloc      there are no more than REC_STACK_SAVE_MAX values to store; otherwise malloc
1492      is used. A problem is what to do if the malloc fails ... there is no way of      is used.
     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.  
1493    
1494      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
1495      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
# Line 1052  for (;;) Line 1525  for (;;)
1525    
1526        memcpy(new_recursive.offset_save, md->offset_vector,        memcpy(new_recursive.offset_save, md->offset_vector,
1527              new_recursive.saved_max * sizeof(int));              new_recursive.saved_max * sizeof(int));
1528        new_recursive.save_start = mstart;        new_recursive.save_offset_top = offset_top;
1529        mstart = eptr;  
   
1530        /* OK, now we can do the recursion. For each top-level alternative we        /* OK, now we can do the recursion. For each top-level alternative we
1531        restore the offset and recursion data. */        restore the offset and recursion data. */
1532    
1533        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1534        flags = (*callpat >= OP_SBRA)? match_cbegroup : 0;        cbegroup = (*callpat >= OP_SBRA);
1535        do        do
1536          {          {
1537            if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
1538          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,
1539            md, ims, eptrb, flags, RM6);            md, eptrb, RM6);
1540          if (rrc == MATCH_MATCH)          if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1541            {            {
1542            DPRINTF(("Recursion matched\n"));            DPRINTF(("Recursion matched\n"));
1543            md->recursive = new_recursive.prevrec;            md->recursive = new_recursive.prevrec;
1544            if (new_recursive.offset_save != stacksave)            if (new_recursive.offset_save != stacksave)
1545              (pcre_free)(new_recursive.offset_save);              (pcre_free)(new_recursive.offset_save);
1546            RRETURN(MATCH_MATCH);            MRRETURN(MATCH_MATCH);
1547            }            }
1548          else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)          else if (rrc != MATCH_NOMATCH &&
1549                    (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1550            {            {
1551            DPRINTF(("Recursion gave error %d\n", rrc));            DPRINTF(("Recursion gave error %d\n", rrc));
1552              if (new_recursive.offset_save != stacksave)
1553                (pcre_free)(new_recursive.offset_save);
1554            RRETURN(rrc);            RRETURN(rrc);
1555            }            }
1556    
# Line 1089  for (;;) Line 1565  for (;;)
1565        md->recursive = new_recursive.prevrec;        md->recursive = new_recursive.prevrec;
1566        if (new_recursive.offset_save != stacksave)        if (new_recursive.offset_save != stacksave)
1567          (pcre_free)(new_recursive.offset_save);          (pcre_free)(new_recursive.offset_save);
1568        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
1569        }        }
1570      /* Control never reaches here */      /* Control never reaches here */
1571    
# Line 1098  for (;;) Line 1574  for (;;)
1574      a move back into the brackets. Friedl calls these "atomic" subpatterns.      a move back into the brackets. Friedl calls these "atomic" subpatterns.
1575      Check the alternative branches in turn - the matching won't pass the KET      Check the alternative branches in turn - the matching won't pass the KET
1576      for this kind of subpattern. If any one branch matches, we carry on as at      for this kind of subpattern. If any one branch matches, we carry on as at
1577      the end of a normal bracket, leaving the subject pointer. */      the end of a normal bracket, leaving the subject pointer, but resetting
1578        the start-of-match value in case it was changed by \K. */
1579    
1580      case OP_ONCE:      case OP_ONCE:
1581      prev = ecode;      prev = ecode;
# Line 1106  for (;;) Line 1583  for (;;)
1583    
1584      do      do
1585        {        {
1586        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM7);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
1587        if (rrc == MATCH_MATCH) break;        if (rrc == MATCH_MATCH)  /* Note: _not_ MATCH_ACCEPT */
1588        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);          {
1589            mstart = md->start_match_ptr;
1590            break;
1591            }
1592          if (rrc != MATCH_NOMATCH &&
1593              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1594            RRETURN(rrc);
1595        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1596        }        }
1597      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
# Line 1117  for (;;) Line 1600  for (;;)
1600    
1601      if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);      if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
1602    
1603      /* Continue as from after the assertion, updating the offsets high water      /* Continue after the group, updating the offsets high water mark, since
1604      mark, since extracts may have been taken. */      extracts may have been taken. */
1605    
1606      do ecode += GET(ecode, 1); while (*ecode == OP_ALT);      do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
1607    
# Line 1128  for (;;) Line 1611  for (;;)
1611      /* For a non-repeating ket, just continue at this level. This also      /* For a non-repeating ket, just continue at this level. This also
1612      happens for a repeating ket if no characters were matched in the group.      happens for a repeating ket if no characters were matched in the group.
1613      This is the forcible breaking of infinite loops as implemented in Perl      This is the forcible breaking of infinite loops as implemented in Perl
1614      5.005. If there is an options reset, it will get obeyed in the normal      5.005. */
     course of events. */  
1615    
1616      if (*ecode == OP_KET || eptr == saved_eptr)      if (*ecode == OP_KET || eptr == saved_eptr)
1617        {        {
# Line 1139  for (;;) Line 1621  for (;;)
1621    
1622      /* The repeating kets try the rest of the pattern or restart from the      /* The repeating kets try the rest of the pattern or restart from the
1623      preceding bracket, in the appropriate order. The second "call" of match()      preceding bracket, in the appropriate order. The second "call" of match()
1624      uses tail recursion, to avoid using another stack frame. We need to reset      uses tail recursion, to avoid using another stack frame. */
     any options that changed within the bracket before re-running it, so  
     check the next opcode. */  
   
     if (ecode[1+LINK_SIZE] == OP_OPT)  
       {  
       ims = (ims & ~PCRE_IMS) | ecode[4];  
       DPRINTF(("ims set to %02lx at group repeat\n", ims));  
       }  
1625    
1626      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1627        {        {
1628        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM8);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM8);
1629        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1630        ecode = prev;        ecode = prev;
       flags = 0;  
       goto TAIL_RECURSE;  
1631        }        }
1632      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1633        {        {
1634        RMATCH(eptr, prev, offset_top, md, ims, eptrb, match_cbegroup, RM9);        md->match_function_type = MATCH_CBEGROUP;
1635          RMATCH(eptr, prev, offset_top, md, eptrb, RM9);
1636        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1637        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = 0;  
       goto TAIL_RECURSE;  
1638        }        }
1639        goto TAIL_RECURSE;
1640    
1641      /* Control never gets here */      /* Control never gets here */
1642    
1643      /* An alternation is the end of a branch; scan along to find the end of the      /* An alternation is the end of a branch; scan along to find the end of the
# Line 1179  for (;;) Line 1652  for (;;)
1652      at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets      at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets
1653      with fixed upper repeat limits are compiled as a number of copies, with the      with fixed upper repeat limits are compiled as a number of copies, with the
1654      optional ones preceded by BRAZERO or BRAMINZERO. */      optional ones preceded by BRAZERO or BRAMINZERO. */
1655    
1656      case OP_BRAZERO:      case OP_BRAZERO:
1657        {      next = ecode + 1;
1658        next = ecode+1;      RMATCH(eptr, next, offset_top, md, eptrb, RM10);
1659        RMATCH(eptr, next, offset_top, md, ims, eptrb, 0, RM10);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1660        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      do next += GET(next, 1); while (*next == OP_ALT);
1661        do next += GET(next,1); while (*next == OP_ALT);      ecode = next + 1 + LINK_SIZE;
       ecode = next + 1 + LINK_SIZE;  
       }  
1662      break;      break;
1663    
1664      case OP_BRAMINZERO:      case OP_BRAMINZERO:
1665        {      next = ecode + 1;
1666        next = ecode+1;      do next += GET(next, 1); while (*next == OP_ALT);
1667        do next += GET(next, 1); while (*next == OP_ALT);      RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
1668        RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0, RM11);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1669        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      ecode++;
       ecode++;  
       }  
1670      break;      break;
1671    
1672      case OP_SKIPZERO:      case OP_SKIPZERO:
1673        {      next = ecode+1;
1674        next = ecode+1;      do next += GET(next,1); while (*next == OP_ALT);
1675        do next += GET(next,1); while (*next == OP_ALT);      ecode = next + 1 + LINK_SIZE;
       ecode = next + 1 + LINK_SIZE;  
       }  
1676      break;      break;
1677    
1678        /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
1679        here; just jump to the group, with allow_zero set TRUE. */
1680    
1681        case OP_BRAPOSZERO:
1682        op = *(++ecode);
1683        allow_zero = TRUE;
1684        if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
1685          goto POSSESSIVE_NON_CAPTURE;
1686    
1687      /* End of a group, repeated or non-repeating. */      /* End of a group, repeated or non-repeating. */
1688    
1689      case OP_KET:      case OP_KET:
1690      case OP_KETRMIN:      case OP_KETRMIN:
1691      case OP_KETRMAX:      case OP_KETRMAX:
1692        case OP_KETRPOS:
1693      prev = ecode - GET(ecode, 1);      prev = ecode - GET(ecode, 1);
1694    
1695      /* If this was a group that remembered the subject start, in order to break      /* If this was a group that remembered the subject start, in order to break
# Line 1226  for (;;) Line 1703  for (;;)
1703        }        }
1704      else saved_eptr = NULL;      else saved_eptr = NULL;
1705    
1706      /* If we are at the end of an assertion group, stop matching and return      /* If we are at the end of an assertion group or an atomic group, stop
1707      MATCH_MATCH, but record the current high water mark for use by positive      matching and return MATCH_MATCH, but record the current high water mark for
1708      assertions. Do this also for the "once" (atomic) groups. */      use by positive assertions. We also need to record the match start in case
1709        it was changed by \K. */
1710    
1711      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
1712          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||
# Line 1236  for (;;) Line 1714  for (;;)
1714        {        {
1715        md->end_match_ptr = eptr;      /* For ONCE */        md->end_match_ptr = eptr;      /* For ONCE */
1716        md->end_offset_top = offset_top;        md->end_offset_top = offset_top;
1717        RRETURN(MATCH_MATCH);        md->start_match_ptr = mstart;
1718          MRRETURN(MATCH_MATCH);
1719        }        }
1720    
1721      /* 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
# Line 1245  for (;;) Line 1724  for (;;)
1724      a recurse into group 0, so it won't be picked up here. Instead, we catch it      a recurse into group 0, so it won't be picked up here. Instead, we catch it
1725      when the OP_END is reached. Other recursion is handled here. */      when the OP_END is reached. Other recursion is handled here. */
1726    
1727      if (*prev == OP_CBRA || *prev == OP_SCBRA)      if (*prev == OP_CBRA || *prev == OP_SCBRA ||
1728            *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
1729        {        {
1730        number = GET2(prev, 1+LINK_SIZE);        number = GET2(prev, 1+LINK_SIZE);
1731        offset = number << 1;        offset = number << 1;
1732    
1733  #ifdef DEBUG  #ifdef PCRE_DEBUG
1734        printf("end bracket %d", number);        printf("end bracket %d", number);
1735        printf("\n");        printf("\n");
1736  #endif  #endif
# Line 1258  for (;;) Line 1738  for (;;)
1738        md->capture_last = number;        md->capture_last = number;
1739        if (offset >= md->offset_max) md->offset_overflow = TRUE; else        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1740          {          {
1741            /* If offset is greater than offset_top, it means that we are
1742            "skipping" a capturing group, and that group's offsets must be marked
1743            unset. In earlier versions of PCRE, all the offsets were unset at the
1744            start of matching, but this doesn't work because atomic groups and
1745            assertions can cause a value to be set that should later be unset.
1746            Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as
1747            part of the atomic group, but this is not on the final matching path,
1748            so must be unset when 2 is set. (If there is no group 2, there is no
1749            problem, because offset_top will then be 2, indicating no capture.) */
1750    
1751            if (offset > offset_top)
1752              {
1753              register int *iptr = md->offset_vector + offset_top;
1754              register int *iend = md->offset_vector + offset;
1755              while (iptr < iend) *iptr++ = -1;
1756              }
1757    
1758            /* Now make the extraction */
1759    
1760          md->offset_vector[offset] =          md->offset_vector[offset] =
1761            md->offset_vector[md->offset_end - number];            md->offset_vector[md->offset_end - number];
1762          md->offset_vector[offset+1] = eptr - md->start_subject;          md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1763          if (offset_top <= offset) offset_top = offset + 2;          if (offset_top <= offset) offset_top = offset + 2;
1764          }          }
1765    
# Line 1272  for (;;) Line 1771  for (;;)
1771          recursion_info *rec = md->recursive;          recursion_info *rec = md->recursive;
1772          DPRINTF(("Recursion (%d) succeeded - continuing\n", number));          DPRINTF(("Recursion (%d) succeeded - continuing\n", number));
1773          md->recursive = rec->prevrec;          md->recursive = rec->prevrec;
         mstart = rec->save_start;  
1774          memcpy(md->offset_vector, rec->offset_save,          memcpy(md->offset_vector, rec->offset_save,
1775            rec->saved_max * sizeof(int));            rec->saved_max * sizeof(int));
1776            offset_top = rec->save_offset_top;
1777          ecode = rec->after_call;          ecode = rec->after_call;
         ims = original_ims;  
1778          break;          break;
1779          }          }
1780        }        }
1781    
     /* For both capturing and non-capturing groups, reset the value of the ims  
     flags, in case they got changed during the group. */  
   
     ims = original_ims;  
     DPRINTF(("ims reset to %02lx\n", ims));  
   
1782      /* For a non-repeating ket, just continue at this level. This also      /* For a non-repeating ket, just continue at this level. This also
1783      happens for a repeating ket if no characters were matched in the group.      happens for a repeating ket if no characters were matched in the group.
1784      This is the forcible breaking of infinite loops as implemented in Perl      This is the forcible breaking of infinite loops as implemented in Perl
# Line 1298  for (;;) Line 1790  for (;;)
1790        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1791        break;        break;
1792        }        }
1793    
1794      /* The repeating kets try the rest of the pattern or restart from the      /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
1795      preceding bracket, in the appropriate order. In the second case, we can use      and return the MATCH_KETRPOS. This makes it possible to do the repeats one
1796      tail recursion to avoid using another stack frame, unless we have an      at a time from the outer level, thus saving stack. */
1797    
1798        if (*ecode == OP_KETRPOS)
1799          {
1800          md->end_match_ptr = eptr;
1801          md->end_offset_top = offset_top;
1802          RRETURN(MATCH_KETRPOS);
1803          }
1804    
1805        /* The normal repeating kets try the rest of the pattern or restart from
1806        the preceding bracket, in the appropriate order. In the second case, we can
1807        use tail recursion to avoid using another stack frame, unless we have an
1808      unlimited repeat of a group that can match an empty string. */      unlimited repeat of a group that can match an empty string. */
1809    
     flags = (*prev >= OP_SBRA)? match_cbegroup : 0;  
   
1810      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1811        {        {
1812        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM12);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
1813        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1814        if (flags != 0)    /* Could match an empty string */        if (*prev >= OP_SBRA)    /* Could match an empty string */
1815          {          {
1816          RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM50);          md->match_function_type = MATCH_CBEGROUP;
1817            RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
1818          RRETURN(rrc);          RRETURN(rrc);
1819          }          }
1820        ecode = prev;        ecode = prev;
# Line 1320  for (;;) Line 1822  for (;;)
1822        }        }
1823      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1824        {        {
1825        RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM13);        if (*prev >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1826          RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
1827        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1828        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = 0;  
1829        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1830        }        }
1831      /* Control never gets here */      /* Control never gets here */
1832    
1833      /* Start of subject unless notbol, or after internal newline if multiline */      /* Not multiline mode: start of subject assertion, unless notbol. */
1834    
1835      case OP_CIRC:      case OP_CIRC:
1836      if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);      if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
1837      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 */  
   
1838      /* Start of subject assertion */      /* Start of subject assertion */
1839    
1840      case OP_SOD:      case OP_SOD:
1841      if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);      if (eptr != md->start_subject) MRRETURN(MATCH_NOMATCH);
1842        ecode++;
1843        break;
1844    
1845        /* Multiline mode: start of subject unless notbol, or after any newline. */
1846    
1847        case OP_CIRCM:
1848        if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
1849        if (eptr != md->start_subject &&
1850            (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
1851          MRRETURN(MATCH_NOMATCH);
1852      ecode++;      ecode++;
1853      break;      break;
1854    
1855      /* Start of match assertion */      /* Start of match assertion */
1856    
1857      case OP_SOM:      case OP_SOM:
1858      if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);      if (eptr != md->start_subject + md->start_offset) MRRETURN(MATCH_NOMATCH);
1859      ecode++;      ecode++;
1860      break;      break;
1861    
# Line 1363  for (;;) Line 1866  for (;;)
1866      ecode++;      ecode++;
1867      break;      break;
1868    
1869      /* Assert before internal newline if multiline, or before a terminating      /* Multiline mode: assert before any newline, or before end of subject
1870      newline unless endonly is set, else end of subject unless noteol is set. */      unless noteol is set. */
1871    
1872      case OP_DOLL:      case OP_DOLLM:
1873      if ((ims & PCRE_MULTILINE) != 0)      if (eptr < md->end_subject)
1874        {        { 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;  
       }  
1875      else      else
1876        {        {
1877        if (md->noteol) RRETURN(MATCH_NOMATCH);        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1878        if (!md->endonly)        SCHECK_PARTIAL();
         {  
         if (eptr != md->end_subject &&  
             (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))  
           RRETURN(MATCH_NOMATCH);  
         ecode++;  
         break;  
         }  
1879        }        }
1880        ecode++;
1881        break;
1882    
1883        /* Not multiline mode: assert before a terminating newline or before end of
1884        subject unless noteol is set. */
1885    
1886        case OP_DOLL:
1887        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1888        if (!md->endonly) goto ASSERT_NL_OR_EOS;
1889    
1890      /* ... else fall through for endonly */      /* ... else fall through for endonly */
1891    
1892      /* End of subject assertion (\z) */      /* End of subject assertion (\z) */
1893    
1894      case OP_EOD:      case OP_EOD:
1895      if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr < md->end_subject) MRRETURN(MATCH_NOMATCH);
1896        SCHECK_PARTIAL();
1897      ecode++;      ecode++;
1898      break;      break;
1899    
1900      /* End of subject or ending \n assertion (\Z) */      /* End of subject or ending \n assertion (\Z) */
1901    
1902      case OP_EODN:      case OP_EODN:
1903      if (eptr != md->end_subject &&      ASSERT_NL_OR_EOS:
1904        if (eptr < md->end_subject &&
1905          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
1906        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
1907      ecode++;  
1908        /* Either at end of string or \n before end. */
1909    
1910        SCHECK_PARTIAL();
1911        ecode++;
1912      break;      break;
1913    
1914      /* Word boundary assertions */      /* Word boundary assertions */
# Line 1414  for (;;) Line 1919  for (;;)
1919    
1920        /* Find out if the previous and current characters are "word" characters.        /* Find out if the previous and current characters are "word" characters.
1921        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
1922        be "non-word" characters. */        be "non-word" characters. Remember the earliest consulted character for
1923          partial matching. */
1924    
1925  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1926        if (utf8)        if (utf8)
1927          {          {
1928            /* Get status of previous character */
1929    
1930          if (eptr == md->start_subject) prev_is_word = FALSE; else          if (eptr == md->start_subject) prev_is_word = FALSE; else
1931            {            {
1932            const uschar *lastptr = eptr - 1;            USPTR lastptr = eptr - 1;
1933            while((*lastptr & 0xc0) == 0x80) lastptr--;            while((*lastptr & 0xc0) == 0x80) lastptr--;
1934              if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
1935            GETCHAR(c, lastptr);            GETCHAR(c, lastptr);
1936    #ifdef SUPPORT_UCP
1937              if (md->use_ucp)
1938                {
1939                if (c == '_') prev_is_word = TRUE; else
1940                  {
1941                  int cat = UCD_CATEGORY(c);
1942                  prev_is_word = (cat == ucp_L || cat == ucp_N);
1943                  }
1944                }
1945              else
1946    #endif
1947            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
1948            }            }
1949          if (eptr >= md->end_subject) cur_is_word = FALSE; else  
1950            /* Get status of next character */
1951    
1952            if (eptr >= md->end_subject)
1953              {
1954              SCHECK_PARTIAL();
1955              cur_is_word = FALSE;
1956              }
1957            else
1958            {            {
1959            GETCHAR(c, eptr);            GETCHAR(c, eptr);
1960    #ifdef SUPPORT_UCP
1961              if (md->use_ucp)
1962                {
1963                if (c == '_') cur_is_word = TRUE; else
1964                  {
1965                  int cat = UCD_CATEGORY(c);
1966                  cur_is_word = (cat == ucp_L || cat == ucp_N);
1967                  }
1968                }
1969              else
1970    #endif
1971            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
1972            }            }
1973          }          }
1974        else        else
1975  #endif  #endif
1976    
1977        /* More streamlined when not in UTF-8 mode */        /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for
1978          consistency with the behaviour of \w we do use it in this case. */
1979    
1980          {          {
1981          prev_is_word = (eptr != md->start_subject) &&          /* Get status of previous character */
1982            ((md->ctypes[eptr[-1]] & ctype_word) != 0);  
1983          cur_is_word = (eptr < md->end_subject) &&          if (eptr == md->start_subject) prev_is_word = FALSE; else
1984            ((md->ctypes[*eptr] & ctype_word) != 0);            {
1985              if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
1986    #ifdef SUPPORT_UCP
1987              if (md->use_ucp)
1988                {
1989                c = eptr[-1];
1990                if (c == '_') prev_is_word = TRUE; else
1991                  {
1992                  int cat = UCD_CATEGORY(c);
1993                  prev_is_word = (cat == ucp_L || cat == ucp_N);
1994                  }
1995                }
1996              else
1997    #endif
1998              prev_is_word = ((md->ctypes[eptr[-1]] & ctype_word) != 0);
1999              }
2000    
2001            /* Get status of next character */
2002    
2003            if (eptr >= md->end_subject)
2004              {
2005              SCHECK_PARTIAL();
2006              cur_is_word = FALSE;
2007              }
2008            else
2009    #ifdef SUPPORT_UCP
2010            if (md->use_ucp)
2011              {
2012              c = *eptr;
2013              if (c == '_') cur_is_word = TRUE; else
2014                {
2015                int cat = UCD_CATEGORY(c);
2016                cur_is_word = (cat == ucp_L || cat == ucp_N);
2017                }
2018              }
2019            else
2020    #endif
2021            cur_is_word = ((md->ctypes[*eptr] & ctype_word) != 0);
2022          }          }
2023    
2024        /* Now see if the situation is what we want */        /* Now see if the situation is what we want */
2025    
2026        if ((*ecode++ == OP_WORD_BOUNDARY)?        if ((*ecode++ == OP_WORD_BOUNDARY)?
2027             cur_is_word == prev_is_word : cur_is_word != prev_is_word)             cur_is_word == prev_is_word : cur_is_word != prev_is_word)
2028          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2029        }        }
2030      break;      break;
2031    
2032      /* Match a single character type; inline for speed */      /* Match a single character type; inline for speed */
2033    
2034      case OP_ANY:      case OP_ANY:
2035      if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);      if (IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH);
2036      /* Fall through */      /* Fall through */
2037    
2038      case OP_ALLANY:      case OP_ALLANY:
2039      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr++ >= md->end_subject)
2040          {
2041          SCHECK_PARTIAL();
2042          MRRETURN(MATCH_NOMATCH);
2043          }
2044      if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;      if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
2045      ecode++;      ecode++;
2046      break;      break;
# Line 1468  for (;;) Line 2049  for (;;)
2049      any byte, even newline, independent of the setting of PCRE_DOTALL. */      any byte, even newline, independent of the setting of PCRE_DOTALL. */
2050    
2051      case OP_ANYBYTE:      case OP_ANYBYTE:
2052      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr++ >= md->end_subject)
2053          {
2054          SCHECK_PARTIAL();
2055          MRRETURN(MATCH_NOMATCH);
2056          }
2057      ecode++;      ecode++;
2058      break;      break;
2059    
2060      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2061      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2062          {
2063          SCHECK_PARTIAL();
2064          MRRETURN(MATCH_NOMATCH);
2065          }
2066      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2067      if (      if (
2068  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1481  for (;;) Line 2070  for (;;)
2070  #endif  #endif
2071         (md->ctypes[c] & ctype_digit) != 0         (md->ctypes[c] & ctype_digit) != 0
2072         )         )
2073        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2074      ecode++;      ecode++;
2075      break;      break;
2076    
2077      case OP_DIGIT:      case OP_DIGIT:
2078      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2079          {
2080          SCHECK_PARTIAL();
2081          MRRETURN(MATCH_NOMATCH);
2082          }
2083      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2084      if (      if (
2085  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1494  for (;;) Line 2087  for (;;)
2087  #endif  #endif
2088         (md->ctypes[c] & ctype_digit) == 0         (md->ctypes[c] & ctype_digit) == 0
2089         )         )
2090        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2091      ecode++;      ecode++;
2092      break;      break;
2093    
2094      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2095      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2096          {
2097          SCHECK_PARTIAL();
2098          MRRETURN(MATCH_NOMATCH);
2099          }
2100      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2101      if (      if (
2102  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1507  for (;;) Line 2104  for (;;)
2104  #endif  #endif
2105         (md->ctypes[c] & ctype_space) != 0         (md->ctypes[c] & ctype_space) != 0
2106         )         )
2107        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2108      ecode++;      ecode++;
2109      break;      break;
2110    
2111      case OP_WHITESPACE:      case OP_WHITESPACE:
2112      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2113          {
2114          SCHECK_PARTIAL();
2115          MRRETURN(MATCH_NOMATCH);
2116          }
2117      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2118      if (      if (
2119  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1520  for (;;) Line 2121  for (;;)
2121  #endif  #endif
2122         (md->ctypes[c] & ctype_space) == 0         (md->ctypes[c] & ctype_space) == 0
2123         )         )
2124        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2125      ecode++;      ecode++;
2126      break;      break;
2127    
2128      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2129      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2130          {
2131          SCHECK_PARTIAL();
2132          MRRETURN(MATCH_NOMATCH);
2133          }
2134      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2135      if (      if (
2136  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1533  for (;;) Line 2138  for (;;)
2138  #endif  #endif
2139         (md->ctypes[c] & ctype_word) != 0         (md->ctypes[c] & ctype_word) != 0
2140         )         )
2141        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2142      ecode++;      ecode++;
2143      break;      break;
2144    
2145      case OP_WORDCHAR:      case OP_WORDCHAR:
2146      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2147          {
2148          SCHECK_PARTIAL();
2149          MRRETURN(MATCH_NOMATCH);
2150          }
2151      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2152      if (      if (
2153  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1546  for (;;) Line 2155  for (;;)
2155  #endif  #endif
2156         (md->ctypes[c] & ctype_word) == 0         (md->ctypes[c] & ctype_word) == 0
2157         )         )
2158        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2159      ecode++;      ecode++;
2160      break;      break;
2161    
2162      case OP_ANYNL:      case OP_ANYNL:
2163      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2164          {
2165          SCHECK_PARTIAL();
2166          MRRETURN(MATCH_NOMATCH);
2167          }
2168      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2169      switch(c)      switch(c)
2170        {        {
2171        default: RRETURN(MATCH_NOMATCH);        default: MRRETURN(MATCH_NOMATCH);
2172    
2173        case 0x000d:        case 0x000d:
2174        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
2175        break;        break;
# Line 1568  for (;;) Line 2182  for (;;)
2182        case 0x0085:        case 0x0085:
2183        case 0x2028:        case 0x2028:
2184        case 0x2029:        case 0x2029:
2185        if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);        if (md->bsr_anycrlf) MRRETURN(MATCH_NOMATCH);
2186        break;        break;
2187        }        }
2188      ecode++;      ecode++;
2189      break;      break;
2190    
2191      case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
2192      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2193          {
2194          SCHECK_PARTIAL();
2195          MRRETURN(MATCH_NOMATCH);
2196          }
2197      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2198      switch(c)      switch(c)
2199        {        {
# Line 1599  for (;;) Line 2217  for (;;)
2217        case 0x202f:    /* NARROW NO-BREAK SPACE */        case 0x202f:    /* NARROW NO-BREAK SPACE */
2218        case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */        case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
2219        case 0x3000:    /* IDEOGRAPHIC SPACE */        case 0x3000:    /* IDEOGRAPHIC SPACE */
2220        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2221        }        }
2222      ecode++;      ecode++;
2223      break;      break;
2224    
2225      case OP_HSPACE:      case OP_HSPACE:
2226      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2227          {
2228          SCHECK_PARTIAL();
2229          MRRETURN(MATCH_NOMATCH);
2230          }
2231      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2232      switch(c)      switch(c)
2233        {        {
2234        default: RRETURN(MATCH_NOMATCH);        default: MRRETURN(MATCH_NOMATCH);
2235        case 0x09:      /* HT */        case 0x09:      /* HT */
2236        case 0x20:      /* SPACE */        case 0x20:      /* SPACE */
2237        case 0xa0:      /* NBSP */        case 0xa0:      /* NBSP */
# Line 1635  for (;;) Line 2257  for (;;)
2257      break;      break;
2258    
2259      case OP_NOT_VSPACE:      case OP_NOT_VSPACE:
2260      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2261          {
2262          SCHECK_PARTIAL();
2263          MRRETURN(MATCH_NOMATCH);
2264          }
2265      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2266      switch(c)      switch(c)
2267        {        {
# Line 1647  for (;;) Line 2273  for (;;)
2273        case 0x85:      /* NEL */        case 0x85:      /* NEL */
2274        case 0x2028:    /* LINE SEPARATOR */        case 0x2028:    /* LINE SEPARATOR */
2275        case 0x2029:    /* PARAGRAPH SEPARATOR */        case 0x2029:    /* PARAGRAPH SEPARATOR */
2276        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2277        }        }
2278      ecode++;      ecode++;
2279      break;      break;
2280    
2281      case OP_VSPACE:      case OP_VSPACE:
2282      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2283          {
2284          SCHECK_PARTIAL();
2285          MRRETURN(MATCH_NOMATCH);
2286          }
2287      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2288      switch(c)      switch(c)
2289        {        {
2290        default: RRETURN(MATCH_NOMATCH);        default: MRRETURN(MATCH_NOMATCH);
2291        case 0x0a:      /* LF */        case 0x0a:      /* LF */
2292        case 0x0b:      /* VT */        case 0x0b:      /* VT */
2293        case 0x0c:      /* FF */        case 0x0c:      /* FF */
# Line 1676  for (;;) Line 2306  for (;;)
2306    
2307      case OP_PROP:      case OP_PROP:
2308      case OP_NOTPROP:      case OP_NOTPROP:
2309      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2310          {
2311          SCHECK_PARTIAL();
2312          MRRETURN(MATCH_NOMATCH);
2313          }
2314      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2315        {        {
2316        const ucd_record * prop = GET_UCD(c);        const ucd_record *prop = GET_UCD(c);
2317    
2318        switch(ecode[1])        switch(ecode[1])
2319          {          {
2320          case PT_ANY:          case PT_ANY:
2321          if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);          if (op == OP_NOTPROP) MRRETURN(MATCH_NOMATCH);
2322          break;          break;
2323    
2324          case PT_LAMP:          case PT_LAMP:
2325          if ((prop->chartype == ucp_Lu ||          if ((prop->chartype == ucp_Lu ||
2326               prop->chartype == ucp_Ll ||               prop->chartype == ucp_Ll ||
2327               prop->chartype == ucp_Lt) == (op == OP_NOTPROP))               prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
2328            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2329           break;          break;
2330    
2331          case PT_GC:          case PT_GC:
2332          if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))          if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))
2333            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2334          break;          break;
2335    
2336          case PT_PC:          case PT_PC:
2337          if ((ecode[2] != prop->chartype) == (op == OP_PROP))          if ((ecode[2] != prop->chartype) == (op == OP_PROP))
2338            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2339          break;          break;
2340    
2341          case PT_SC:          case PT_SC:
2342          if ((ecode[2] != prop->script) == (op == OP_PROP))          if ((ecode[2] != prop->script) == (op == OP_PROP))
2343            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2344          break;          break;
2345    
2346            /* These are specials */
2347    
2348            case PT_ALNUM:
2349            if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2350                 _pcre_ucp_gentype[prop->chartype] == ucp_N) == (op == OP_NOTPROP))
2351              MRRETURN(MATCH_NOMATCH);
2352            break;
2353    
2354            case PT_SPACE:    /* Perl space */
2355            if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2356                 c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2357                   == (op == OP_NOTPROP))
2358              MRRETURN(MATCH_NOMATCH);
2359            break;
2360    
2361            case PT_PXSPACE:  /* POSIX space */
2362            if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2363                 c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2364                 c == CHAR_FF || c == CHAR_CR)
2365                   == (op == OP_NOTPROP))
2366              MRRETURN(MATCH_NOMATCH);
2367            break;
2368    
2369            case PT_WORD:
2370            if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2371                 _pcre_ucp_gentype[prop->chartype] == ucp_N ||
2372                 c == CHAR_UNDERSCORE) == (op == OP_NOTPROP))
2373              MRRETURN(MATCH_NOMATCH);
2374            break;
2375    
2376            /* This should never occur */
2377    
2378          default:          default:
2379          RRETURN(PCRE_ERROR_INTERNAL);          RRETURN(PCRE_ERROR_INTERNAL);
2380          }          }
# Line 1721  for (;;) Line 2387  for (;;)
2387      is in the binary; otherwise a compile-time error occurs. */      is in the binary; otherwise a compile-time error occurs. */
2388    
2389      case OP_EXTUNI:      case OP_EXTUNI:
2390      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2391          {
2392          SCHECK_PARTIAL();
2393          MRRETURN(MATCH_NOMATCH);
2394          }
2395      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2396        {        {
2397        int category = UCD_CATEGORY(c);        int category = UCD_CATEGORY(c);
2398        if (category == ucp_M) RRETURN(MATCH_NOMATCH);        if (category == ucp_M) MRRETURN(MATCH_NOMATCH);
2399        while (eptr < md->end_subject)        while (eptr < md->end_subject)
2400          {          {
2401          int len = 1;          int len = 1;
# Line 1752  for (;;) Line 2422  for (;;)
2422      loops). */      loops). */
2423    
2424      case OP_REF:      case OP_REF:
2425        {      case OP_REFI:
2426        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */      caseless = op == OP_REFI;
2427        ecode += 3;      offset = GET2(ecode, 1) << 1;               /* Doubled ref number */
2428        ecode += 3;
2429    
2430        /* If the reference is unset, there are two possibilities:      /* If the reference is unset, there are two possibilities:
2431    
2432        (a) In the default, Perl-compatible state, set the length to be longer      (a) In the default, Perl-compatible state, set the length negative;
2433        than the amount of subject left; this ensures that every attempt at a      this ensures that every attempt at a match fails. We can't just fail
2434        match fails. We can't just fail here, because of the possibility of      here, because of the possibility of quantifiers with zero minima.
       quantifiers with zero minima.  
2435    
2436        (b) If the JavaScript compatibility flag is set, set the length to zero      (b) If the JavaScript compatibility flag is set, set the length to zero
2437        so that the back reference matches an empty string.      so that the back reference matches an empty string.
2438    
2439        Otherwise, set the length to the length of what was matched by the      Otherwise, set the length to the length of what was matched by the
2440        referenced subpattern. */      referenced subpattern. */
2441    
2442        if (offset >= offset_top || md->offset_vector[offset] < 0)      if (offset >= offset_top || md->offset_vector[offset] < 0)
2443          length = (md->jscript_compat)? 0 : md->end_subject - eptr + 1;        length = (md->jscript_compat)? 0 : -1;
2444        else      else
2445          length = md->offset_vector[offset+1] - md->offset_vector[offset];        length = md->offset_vector[offset+1] - md->offset_vector[offset];
2446    
2447        /* Set up for repetition, or handle the non-repeated case */      /* Set up for repetition, or handle the non-repeated case */
2448    
2449        switch (*ecode)      switch (*ecode)
2450          {        {
2451          case OP_CRSTAR:        case OP_CRSTAR:
2452          case OP_CRMINSTAR:        case OP_CRMINSTAR:
2453          case OP_CRPLUS:        case OP_CRPLUS:
2454          case OP_CRMINPLUS:        case OP_CRMINPLUS:
2455          case OP_CRQUERY:        case OP_CRQUERY:
2456          case OP_CRMINQUERY:        case OP_CRMINQUERY:
2457          c = *ecode++ - OP_CRSTAR;        c = *ecode++ - OP_CRSTAR;
2458          minimize = (c & 1) != 0;        minimize = (c & 1) != 0;
2459          min = rep_min[c];                 /* Pick up values from tables; */        min = rep_min[c];                 /* Pick up values from tables; */
2460          max = rep_max[c];                 /* zero for max => infinity */        max = rep_max[c];                 /* zero for max => infinity */
2461          if (max == 0) max = INT_MAX;        if (max == 0) max = INT_MAX;
2462          break;        break;
2463    
2464          case OP_CRRANGE:        case OP_CRRANGE:
2465          case OP_CRMINRANGE:        case OP_CRMINRANGE:
2466          minimize = (*ecode == OP_CRMINRANGE);        minimize = (*ecode == OP_CRMINRANGE);
2467          min = GET2(ecode, 1);        min = GET2(ecode, 1);
2468          max = GET2(ecode, 3);        max = GET2(ecode, 3);
2469          if (max == 0) max = INT_MAX;        if (max == 0) max = INT_MAX;
2470          ecode += 5;        ecode += 5;
2471          break;        break;
2472    
2473          default:               /* No repeat follows */        default:               /* No repeat follows */
2474          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);        if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
2475          eptr += length;          {
2476          continue;              /* With the main loop */          CHECK_PARTIAL();
2477            MRRETURN(MATCH_NOMATCH);
2478          }          }
2479          eptr += length;
2480          continue;              /* With the main loop */
2481          }
2482    
2483        /* If the length of the reference is zero, just continue with the      /* Handle repeated back references. If the length of the reference is
2484        main loop. */      zero, just continue with the main loop. */
2485    
2486        if (length == 0) continue;      if (length == 0) continue;
2487    
2488        /* First, ensure the minimum number of matches are present. We get back      /* First, ensure the minimum number of matches are present. We get back
2489        the length of the reference string explicitly rather than passing the      the length of the reference string explicitly rather than passing the
2490        address of eptr, so that eptr can be a register variable. */      address of eptr, so that eptr can be a register variable. */
2491    
2492        for (i = 1; i <= min; i++)      for (i = 1; i <= min; i++)
2493          {
2494          int slength;
2495          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2496          {          {
2497          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);          CHECK_PARTIAL();
2498          eptr += length;          MRRETURN(MATCH_NOMATCH);
2499          }          }
2500          eptr += slength;
2501          }
2502    
2503        /* If min = max, continue at the same level without recursion.      /* If min = max, continue at the same level without recursion.
2504        They are not both allowed to be zero. */      They are not both allowed to be zero. */
2505    
2506        if (min == max) continue;      if (min == max) continue;
2507    
2508        /* If minimizing, keep trying and advancing the pointer */      /* If minimizing, keep trying and advancing the pointer */
2509    
2510        if (minimize)      if (minimize)
2511          {
2512          for (fi = min;; fi++)
2513          {          {
2514          for (fi = min;; fi++)          int slength;
2515            RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
2516            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2517            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2518            if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2519            {            {
2520            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM14);            CHECK_PARTIAL();
2521            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            MRRETURN(MATCH_NOMATCH);
           if (fi >= max || !match_ref(offset, eptr, length, md, ims))  
             RRETURN(MATCH_NOMATCH);  
           eptr += length;  
2522            }            }
2523          /* Control never gets here */          eptr += slength;
2524          }          }
2525          /* Control never gets here */
2526          }
2527    
2528        /* If maximizing, find the longest string and work backwards */      /* If maximizing, find the longest string and work backwards */
2529    
2530        else      else
2531          {
2532          pp = eptr;
2533          for (i = min; i < max; i++)
2534          {          {
2535          pp = eptr;          int slength;
2536          for (i = min; i < max; i++)          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2537            {            {
2538            if (!match_ref(offset, eptr, length, md, ims)) break;            CHECK_PARTIAL();
2539            eptr += length;            break;
           }  
         while (eptr >= pp)  
           {  
           RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM15);  
           if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
           eptr -= length;  
2540            }            }
2541          RRETURN(MATCH_NOMATCH);          eptr += slength;
2542          }          }
2543          while (eptr >= pp)
2544            {
2545            RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
2546            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2547            eptr -= length;
2548            }
2549          MRRETURN(MATCH_NOMATCH);
2550        }        }
2551      /* Control never gets here */      /* Control never gets here */
2552    
   
   
2553      /* Match a bit-mapped character class, possibly repeatedly. This op code is      /* Match a bit-mapped character class, possibly repeatedly. This op code is
2554      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,
2555      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 1918  for (;;) Line 2604  for (;;)
2604          {          {
2605          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2606            {            {
2607            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2608                {
2609                SCHECK_PARTIAL();
2610                MRRETURN(MATCH_NOMATCH);
2611                }
2612            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
2613            if (c > 255)            if (c > 255)
2614              {              {
2615              if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);              if (op == OP_CLASS) MRRETURN(MATCH_NOMATCH);
2616              }              }
2617            else            else
2618              {              {
2619              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);              if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2620              }              }
2621            }            }
2622          }          }
# Line 1936  for (;;) Line 2626  for (;;)
2626          {          {
2627          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2628            {            {
2629            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2630                {
2631                SCHECK_PARTIAL();
2632                MRRETURN(MATCH_NOMATCH);
2633                }
2634            c = *eptr++;            c = *eptr++;
2635            if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);            if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2636            }            }
2637          }          }
2638    
# Line 1958  for (;;) Line 2652  for (;;)
2652            {            {
2653            for (fi = min;; fi++)            for (fi = min;; fi++)
2654              {              {
2655              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM16);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
2656              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2657              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2658                if (eptr >= md->end_subject)
2659                  {
2660                  SCHECK_PARTIAL();
2661                  MRRETURN(MATCH_NOMATCH);
2662                  }
2663              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
2664              if (c > 255)              if (c > 255)
2665                {                {
2666                if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);                if (op == OP_CLASS) MRRETURN(MATCH_NOMATCH);
2667                }                }
2668              else              else
2669                {                {
2670                if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);                if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2671                }                }
2672              }              }
2673            }            }
# Line 1978  for (;;) Line 2677  for (;;)
2677            {            {
2678            for (fi = min;; fi++)            for (fi = min;; fi++)
2679              {              {
2680              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM17);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
2681              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2682              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2683                if (eptr >= md->end_subject)
2684                  {
2685                  SCHECK_PARTIAL();
2686                  MRRETURN(MATCH_NOMATCH);
2687                  }
2688              c = *eptr++;              c = *eptr++;
2689              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);              if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2690              }              }
2691            }            }
2692          /* Control never gets here */          /* Control never gets here */
# Line 2001  for (;;) Line 2705  for (;;)
2705            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2706              {              {
2707              int len = 1;              int len = 1;
2708              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
2709                  {
2710                  SCHECK_PARTIAL();
2711                  break;
2712                  }
2713              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
2714              if (c > 255)              if (c > 255)
2715                {                {
# Line 2015  for (;;) Line 2723  for (;;)
2723              }              }
2724            for (;;)            for (;;)
2725              {              {
2726              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM18);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
2727              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2728              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2729              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2027  for (;;) Line 2735  for (;;)
2735            {            {
2736            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2737              {              {
2738              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
2739                  {
2740                  SCHECK_PARTIAL();
2741                  break;
2742                  }
2743              c = *eptr;              c = *eptr;
2744              if ((data[c/8] & (1 << (c&7))) == 0) break;              if ((data[c/8] & (1 << (c&7))) == 0) break;
2745              eptr++;              eptr++;
2746              }              }
2747            while (eptr >= pp)            while (eptr >= pp)
2748              {              {
2749              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM19);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM19);
2750              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2751              eptr--;              eptr--;
2752              }              }
2753            }            }
2754    
2755          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2756          }          }
2757        }        }
2758      /* Control never gets here */      /* Control never gets here */
2759    
2760    
2761      /* Match an extended character class. This opcode is encountered only      /* Match an extended character class. This opcode is encountered only
2762      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
2763        mode, because Unicode properties are supported in non-UTF-8 mode. */
2764    
2765  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2766      case OP_XCLASS:      case OP_XCLASS:
# Line 2088  for (;;) Line 2801  for (;;)
2801    
2802        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
2803          {          {
2804          if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);          if (eptr >= md->end_subject)
2805          GETCHARINC(c, eptr);            {
2806          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);            SCHECK_PARTIAL();
2807              MRRETURN(MATCH_NOMATCH);
2808              }
2809            GETCHARINCTEST(c, eptr);
2810            if (!_pcre_xclass(c, data)) MRRETURN(MATCH_NOMATCH);
2811          }          }
2812    
2813        /* 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 2105  for (;;) Line 2822  for (;;)
2822          {          {
2823          for (fi = min;; fi++)          for (fi = min;; fi++)
2824            {            {
2825            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM20);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM20);
2826            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2827            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2828            GETCHARINC(c, eptr);            if (eptr >= md->end_subject)
2829            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);              {
2830                SCHECK_PARTIAL();
2831                MRRETURN(MATCH_NOMATCH);
2832                }
2833              GETCHARINCTEST(c, eptr);
2834              if (!_pcre_xclass(c, data)) MRRETURN(MATCH_NOMATCH);
2835            }            }
2836          /* Control never gets here */          /* Control never gets here */
2837          }          }
# Line 2122  for (;;) Line 2844  for (;;)
2844          for (i = min; i < max; i++)          for (i = min; i < max; i++)
2845            {            {
2846            int len = 1;            int len = 1;
2847            if (eptr >= md->end_subject) break;            if (eptr >= md->end_subject)
2848            GETCHARLEN(c, eptr, len);              {
2849                SCHECK_PARTIAL();
2850                break;
2851                }
2852              GETCHARLENTEST(c, eptr, len);
2853            if (!_pcre_xclass(c, data)) break;            if (!_pcre_xclass(c, data)) break;
2854            eptr += len;            eptr += len;
2855            }            }
2856          for(;;)          for(;;)
2857            {            {
2858            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM21);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM21);
2859            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2860            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
2861            if (utf8) BACKCHAR(eptr);            if (utf8) BACKCHAR(eptr);
2862            }            }
2863          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2864          }          }
2865    
2866        /* Control never gets here */        /* Control never gets here */
# Line 2150  for (;;) Line 2876  for (;;)
2876        length = 1;        length = 1;
2877        ecode++;        ecode++;
2878        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2879        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
2880        while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);          {
2881            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2882            MRRETURN(MATCH_NOMATCH);
2883            }
2884          while (length-- > 0) if (*ecode++ != *eptr++) MRRETURN(MATCH_NOMATCH);
2885        }        }
2886      else      else
2887  #endif  #endif
2888    
2889      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2890        {        {
2891        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
2892        if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);          {
2893            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2894            MRRETURN(MATCH_NOMATCH);
2895            }
2896          if (ecode[1] != *eptr++) MRRETURN(MATCH_NOMATCH);
2897        ecode += 2;        ecode += 2;
2898        }        }
2899      break;      break;
2900    
2901      /* Match a single character, caselessly */      /* Match a single character, caselessly */
2902    
2903      case OP_CHARNC:      case OP_CHARI:
2904  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2905      if (utf8)      if (utf8)
2906        {        {
# Line 2174  for (;;) Line 2908  for (;;)
2908        ecode++;        ecode++;
2909        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2910    
2911        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
2912            {
2913            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2914            MRRETURN(MATCH_NOMATCH);
2915            }
2916    
2917        /* 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
2918        can use the fast lookup table. */        can use the fast lookup table. */
2919    
2920        if (fc < 128)        if (fc < 128)
2921          {          {
2922          if (md->lcc[*ecode++] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          if (md->lcc[*ecode++] != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
2923          }          }
2924    
2925        /* Otherwise we must pick up the subject character */        /* Otherwise we must pick up the subject character */
# Line 2200  for (;;) Line 2938  for (;;)
2938  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2939            if (dc != UCD_OTHERCASE(fc))            if (dc != UCD_OTHERCASE(fc))
2940  #endif  #endif
2941              RRETURN(MATCH_NOMATCH);              MRRETURN(MATCH_NOMATCH);
2942            }            }
2943          }          }
2944        }        }
# Line 2209  for (;;) Line 2947  for (;;)
2947    
2948      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2949        {        {
2950        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
2951        if (md->lcc[ecode[1]] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          {
2952            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2953            MRRETURN(MATCH_NOMATCH);
2954            }
2955          if (md->lcc[ecode[1]] != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
2956        ecode += 2;        ecode += 2;
2957        }        }
2958      break;      break;
# Line 2218  for (;;) Line 2960  for (;;)
2960      /* Match a single character repeatedly. */      /* Match a single character repeatedly. */
2961    
2962      case OP_EXACT:      case OP_EXACT:
2963        case OP_EXACTI:
2964      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
2965      ecode += 3;      ecode += 3;
2966      goto REPEATCHAR;      goto REPEATCHAR;
2967    
2968      case OP_POSUPTO:      case OP_POSUPTO:
2969        case OP_POSUPTOI:
2970      possessive = TRUE;      possessive = TRUE;
2971      /* Fall through */      /* Fall through */
2972    
2973      case OP_UPTO:      case OP_UPTO:
2974        case OP_UPTOI:
2975      case OP_MINUPTO:      case OP_MINUPTO:
2976        case OP_MINUPTOI:
2977      min = 0;      min = 0;
2978      max = GET2(ecode, 1);      max = GET2(ecode, 1);
2979      minimize = *ecode == OP_MINUPTO;      minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI;
2980      ecode += 3;      ecode += 3;
2981      goto REPEATCHAR;      goto REPEATCHAR;
2982    
2983      case OP_POSSTAR:      case OP_POSSTAR:
2984        case OP_POSSTARI:
2985      possessive = TRUE;      possessive = TRUE;
2986      min = 0;      min = 0;
2987      max = INT_MAX;      max = INT_MAX;
# Line 2242  for (;;) Line 2989  for (;;)
2989      goto REPEATCHAR;      goto REPEATCHAR;
2990    
2991      case OP_POSPLUS:      case OP_POSPLUS:
2992        case OP_POSPLUSI:
2993      possessive = TRUE;      possessive = TRUE;
2994      min = 1;      min = 1;
2995      max = INT_MAX;      max = INT_MAX;
# Line 2249  for (;;) Line 2997  for (;;)
2997      goto REPEATCHAR;      goto REPEATCHAR;
2998    
2999      case OP_POSQUERY:      case OP_POSQUERY:
3000        case OP_POSQUERYI:
3001      possessive = TRUE;      possessive = TRUE;
3002      min = 0;      min = 0;
3003      max = 1;      max = 1;
# Line 2256  for (;;) Line 3005  for (;;)
3005      goto REPEATCHAR;      goto REPEATCHAR;
3006    
3007      case OP_STAR:      case OP_STAR:
3008        case OP_STARI:
3009      case OP_MINSTAR:      case OP_MINSTAR:
3010        case OP_MINSTARI:
3011      case OP_PLUS:      case OP_PLUS:
3012        case OP_PLUSI:
3013      case OP_MINPLUS:      case OP_MINPLUS:
3014        case OP_MINPLUSI:
3015      case OP_QUERY:      case OP_QUERY:
3016        case OP_QUERYI:
3017      case OP_MINQUERY:      case OP_MINQUERY:
3018      c = *ecode++ - OP_STAR;      case OP_MINQUERYI:
3019        c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI);
3020      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3021      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3022      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3023      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
3024    
3025      /* 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. */  
3026    
3027      REPEATCHAR:      REPEATCHAR:
3028  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 2278  for (;;) Line 3031  for (;;)
3031        length = 1;        length = 1;
3032        charptr = ecode;        charptr = ecode;
3033        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
       if (min * length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3034        ecode += length;        ecode += length;
3035    
3036        /* Handle multibyte character matching specially here. There is        /* Handle multibyte character matching specially here. There is
# Line 2288  for (;;) Line 3040  for (;;)
3040          {          {
3041  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3042          unsigned int othercase;          unsigned int othercase;
3043          if ((ims & PCRE_CASELESS) != 0 &&          if (op >= OP_STARI &&     /* Caseless */
3044              (othercase = UCD_OTHERCASE(fc)) != fc)              (othercase = UCD_OTHERCASE(fc)) != fc)
3045            oclength = _pcre_ord2utf8(othercase, occhars);            oclength = _pcre_ord2utf8(othercase, occhars);
3046          else oclength = 0;          else oclength = 0;
# Line 2296  for (;;) Line 3048  for (;;)
3048    
3049          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3050            {            {
3051            if (memcmp(eptr, charptr, length) == 0) eptr += length;            if (eptr <= md->end_subject - length &&
3052                memcmp(eptr, charptr, length) == 0) eptr += length;
3053  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3054            /* Need braces because of following else */            else if (oclength > 0 &&
3055            else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }                     eptr <= md->end_subject - oclength &&
3056                       memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3057    #endif  /* SUPPORT_UCP */
3058            else            else
3059              {              {
3060              if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);              CHECK_PARTIAL();
3061              eptr += oclength;              MRRETURN(MATCH_NOMATCH);
3062              }              }
 #else   /* without SUPPORT_UCP */  
           else { RRETURN(MATCH_NOMATCH); }  
 #endif  /* SUPPORT_UCP */  
3063            }            }
3064    
3065          if (min == max) continue;          if (min == max) continue;
# Line 2316  for (;;) Line 3068  for (;;)
3068            {            {
3069            for (fi = min;; fi++)            for (fi = min;; fi++)
3070              {              {
3071              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM22);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM22);
3072              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3073              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3074              if (memcmp(eptr, charptr, length) == 0) eptr += length;              if (eptr <= md->end_subject - length &&
3075                  memcmp(eptr, charptr, length) == 0) eptr += length;
3076  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3077              /* Need braces because of following else */              else if (oclength > 0 &&
3078              else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }                       eptr <= md->end_subject - oclength &&
3079                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3080    #endif  /* SUPPORT_UCP */
3081              else              else
3082                {                {
3083                if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);                CHECK_PARTIAL();
3084                eptr += oclength;                MRRETURN(MATCH_NOMATCH);
3085                }                }
 #else   /* without SUPPORT_UCP */  
             else { RRETURN (MATCH_NOMATCH); }  
 #endif  /* SUPPORT_UCP */  
3086              }              }
3087            /* Control never gets here */            /* Control never gets here */
3088            }            }
# Line 2340  for (;;) Line 3092  for (;;)
3092            pp = eptr;            pp = eptr;
3093            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3094              {              {
3095              if (eptr > md->end_subject - length) break;              if (eptr <= md->end_subject - length &&
3096              if (memcmp(eptr, charptr, length) == 0) eptr += length;                  memcmp(eptr, charptr, length) == 0) eptr += length;
3097  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3098              else if (oclength == 0) break;              else if (oclength > 0 &&
3099                         eptr <= md->end_subject - oclength &&
3100                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3101    #endif  /* SUPPORT_UCP */
3102              else              else
3103                {                {
3104                if (memcmp(eptr, occhars, oclength) != 0) break;                CHECK_PARTIAL();
3105                eptr += oclength;                break;
3106                }                }
 #else   /* without SUPPORT_UCP */  
             else break;  
 #endif  /* SUPPORT_UCP */  
3107              }              }
3108    
3109            if (possessive) continue;            if (possessive) continue;
3110    
3111            for(;;)            for(;;)
3112             {              {
3113             RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM23);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM23);
3114             if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3115             if (eptr == pp) RRETURN(MATCH_NOMATCH);              if (eptr == pp) { MRRETURN(MATCH_NOMATCH); }
3116  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3117             eptr--;              eptr--;
3118             BACKCHAR(eptr);              BACKCHAR(eptr);
3119  #else   /* without SUPPORT_UCP */  #else   /* without SUPPORT_UCP */
3120             eptr -= length;              eptr -= length;
3121  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3122             }              }
3123            }            }
3124          /* Control never gets here */          /* Control never gets here */
3125          }          }
# Line 2379  for (;;) Line 3132  for (;;)
3132  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
3133    
3134      /* When not in UTF-8 mode, load a single-byte character. */      /* When not in UTF-8 mode, load a single-byte character. */
3135        {  
3136        if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);      fc = *ecode++;
       fc = *ecode++;  
       }  
3137    
3138      /* 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
3139      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 2396  for (;;) Line 3147  for (;;)
3147      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3148        max, eptr));        max, eptr));
3149    
3150      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_STARI)  /* Caseless */
3151        {        {
3152        fc = md->lcc[fc];        fc = md->lcc[fc];
3153        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
3154          if (fc != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          {
3155            if (eptr >= md->end_subject)
3156              {
3157              SCHECK_PARTIAL();
3158              MRRETURN(MATCH_NOMATCH);
3159              }
3160            if (fc != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3161            }
3162        if (min == max) continue;        if (min == max) continue;
3163        if (minimize)        if (minimize)
3164          {          {
3165          for (fi = min;; fi++)          for (fi = min;; fi++)
3166            {            {
3167            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM24);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM24);
3168            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3169            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3170                fc != md->lcc[*eptr++])            if (eptr >= md->end_subject)
3171              RRETURN(MATCH_NOMATCH);              {
3172                SCHECK_PARTIAL();
3173                MRRETURN(MATCH_NOMATCH);
3174                }
3175              if (fc != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3176            }            }
3177          /* Control never gets here */          /* Control never gets here */
3178          }          }
# Line 2419  for (;;) Line 3181  for (;;)
3181          pp = eptr;          pp = eptr;
3182          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3183            {            {
3184            if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break;            if (eptr >= md->end_subject)
3185                {
3186                SCHECK_PARTIAL();
3187                break;
3188                }
3189              if (fc != md->lcc[*eptr]) break;
3190            eptr++;            eptr++;
3191            }            }
3192    
3193          if (possessive) continue;          if (possessive) continue;
3194    
3195          while (eptr >= pp)          while (eptr >= pp)
3196            {            {
3197            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM25);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM25);
3198            eptr--;            eptr--;
3199            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3200            }            }
3201          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3202          }          }
3203        /* Control never gets here */        /* Control never gets here */
3204        }        }
# Line 2438  for (;;) Line 3207  for (;;)
3207    
3208      else      else
3209        {        {
3210        for (i = 1; i <= min; i++) if (fc != *eptr++) RRETURN(MATCH_NOMATCH);        for (i = 1; i <= min; i++)
3211            {
3212            if (eptr >= md->end_subject)
3213              {
3214              SCHECK_PARTIAL();
3215              MRRETURN(MATCH_NOMATCH);
3216              }
3217            if (fc != *eptr++) MRRETURN(MATCH_NOMATCH);
3218            }
3219    
3220        if (min == max) continue;        if (min == max) continue;
3221    
3222        if (minimize)        if (minimize)
3223          {          {
3224          for (fi = min;; fi++)          for (fi = min;; fi++)
3225            {            {
3226            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM26);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM26);
3227            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3228            if (fi >= max || eptr >= md->end_subject || fc != *eptr++)            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3229              RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
3230                {
3231                SCHECK_PARTIAL();
3232                MRRETURN(MATCH_NOMATCH);
3233                }
3234              if (fc != *eptr++) MRRETURN(MATCH_NOMATCH);
3235            }            }
3236          /* Control never gets here */          /* Control never gets here */
3237          }          }
# Line 2456  for (;;) Line 3240  for (;;)
3240          pp = eptr;          pp = eptr;
3241          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3242            {            {
3243            if (eptr >= md->end_subject || fc != *eptr) break;            if (eptr >= md->end_subject)
3244                {
3245                SCHECK_PARTIAL();
3246                break;
3247                }
3248              if (fc != *eptr) break;
3249            eptr++;            eptr++;
3250            }            }
3251          if (possessive) continue;          if (possessive) continue;
3252    
3253          while (eptr >= pp)          while (eptr >= pp)
3254            {            {
3255            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM27);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM27);
3256            eptr--;            eptr--;
3257            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3258            }            }
3259          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3260          }          }
3261        }        }
3262      /* Control never gets here */      /* Control never gets here */
# Line 2475  for (;;) Line 3265  for (;;)
3265      checking can be multibyte. */      checking can be multibyte. */
3266    
3267      case OP_NOT:      case OP_NOT:
3268      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      case OP_NOTI:
3269        if (eptr >= md->end_subject)
3270          {
3271          SCHECK_PARTIAL();
3272          MRRETURN(MATCH_NOMATCH);
3273          }
3274      ecode++;      ecode++;
3275      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
3276      if ((ims & PCRE_CASELESS) != 0)      if (op == OP_NOTI)         /* The caseless case */
3277        {        {
3278  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3279        if (c < 256)        if (c < 256)
3280  #endif  #endif
3281        c = md->lcc[c];        c = md->lcc[c];
3282        if (md->lcc[*ecode++] == c) RRETURN(MATCH_NOMATCH);        if (md->lcc[*ecode++] == c) MRRETURN(MATCH_NOMATCH);
3283        }        }
3284      else      else    /* Caseful */
3285        {        {
3286        if (*ecode++ == c) RRETURN(MATCH_NOMATCH);        if (*ecode++ == c) MRRETURN(MATCH_NOMATCH);
3287        }        }
3288      break;      break;
3289    
# Line 2500  for (;;) Line 3295  for (;;)
3295      about... */      about... */
3296    
3297      case OP_NOTEXACT:      case OP_NOTEXACT:
3298        case OP_NOTEXACTI:
3299      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
3300      ecode += 3;      ecode += 3;
3301      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3302    
3303      case OP_NOTUPTO:      case OP_NOTUPTO:
3304        case OP_NOTUPTOI:
3305      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
3306        case OP_NOTMINUPTOI:
3307      min = 0;      min = 0;
3308      max = GET2(ecode, 1);      max = GET2(ecode, 1);
3309      minimize = *ecode == OP_NOTMINUPTO;      minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI;
3310      ecode += 3;      ecode += 3;
3311      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3312    
3313      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
3314        case OP_NOTPOSSTARI:
3315      possessive = TRUE;      possessive = TRUE;
3316      min = 0;      min = 0;
3317      max = INT_MAX;      max = INT_MAX;
# Line 2520  for (;;) Line 3319  for (;;)
3319      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3320    
3321      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
3322        case OP_NOTPOSPLUSI:
3323      possessive = TRUE;      possessive = TRUE;
3324      min = 1;      min = 1;
3325      max = INT_MAX;      max = INT_MAX;
# Line 2527  for (;;) Line 3327  for (;;)
3327      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3328    
3329      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
3330        case OP_NOTPOSQUERYI:
3331      possessive = TRUE;      possessive = TRUE;
3332      min = 0;      min = 0;
3333      max = 1;      max = 1;
# Line 2534  for (;;) Line 3335  for (;;)
3335      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3336    
3337      case OP_NOTPOSUPTO:      case OP_NOTPOSUPTO:
3338        case OP_NOTPOSUPTOI:
3339      possessive = TRUE;      possessive = TRUE;
3340      min = 0;      min = 0;
3341      max = GET2(ecode, 1);      max = GET2(ecode, 1);
# Line 2541  for (;;) Line 3343  for (;;)
3343      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3344    
3345      case OP_NOTSTAR:      case OP_NOTSTAR:
3346        case OP_NOTSTARI:
3347      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
3348        case OP_NOTMINSTARI:
3349      case OP_NOTPLUS:      case OP_NOTPLUS:
3350        case OP_NOTPLUSI:
3351      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
3352        case OP_NOTMINPLUSI:
3353      case OP_NOTQUERY:      case OP_NOTQUERY:
3354        case OP_NOTQUERYI:
3355      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
3356      c = *ecode++ - OP_NOTSTAR;      case OP_NOTMINQUERYI:
3357        c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
3358      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3359      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3360      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3361      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
3362    
3363      /* 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. */  
3364    
3365      REPEATNOTCHAR:      REPEATNOTCHAR:
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3366      fc = *ecode++;      fc = *ecode++;
3367    
3368      /* 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 2571  for (;;) Line 3376  for (;;)
3376      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,
3377        max, eptr));        max, eptr));
3378    
3379      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_NOTSTARI)     /* Caseless */
3380        {        {
3381        fc = md->lcc[fc];        fc = md->lcc[fc];
3382    
# Line 2582  for (;;) Line 3387  for (;;)
3387          register unsigned int d;          register unsigned int d;
3388          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3389            {            {
3390              if (eptr >= md->end_subject)
3391                {
3392                SCHECK_PARTIAL();
3393                MRRETURN(MATCH_NOMATCH);
3394                }
3395            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3396            if (d < 256) d = md->lcc[d];            if (d < 256) d = md->lcc[d];
3397            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) MRRETURN(MATCH_NOMATCH);
3398            }            }
3399          }          }
3400        else        else
# Line 2593  for (;;) Line 3403  for (;;)
3403        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3404          {          {
3405          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3406            if (fc == md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);            {
3407              if (eptr >= md->end_subject)
3408                {
3409                SCHECK_PARTIAL();
3410                MRRETURN(MATCH_NOMATCH);
3411                }
3412              if (fc == md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3413              }
3414          }          }
3415    
3416        if (min == max) continue;        if (min == max) continue;
# Line 2607  for (;;) Line 3424  for (;;)
3424            register unsigned int d;            register unsigned int d;
3425            for (fi = min;; fi++)            for (fi = min;; fi++)
3426              {              {
3427              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM28);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM28);
3428              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3429                if (fi >= max) MRRETURN(MATCH_NOMATCH);
3430                if (eptr >= md->end_subject)
3431                  {
3432                  SCHECK_PARTIAL();
3433                  MRRETURN(MATCH_NOMATCH);
3434                  }
3435              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3436              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3437              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) MRRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
3438              }              }
3439            }            }
3440          else          else
# Line 2621  for (;;) Line 3443  for (;;)
3443            {            {
3444            for (fi = min;; fi++)            for (fi = min;; fi++)
3445              {              {
3446              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM29);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM29);
3447              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3448              if (fi >= max || eptr >= md->end_subject || fc == md->lcc[*eptr++])              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3449                RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3450                  {
3451                  SCHECK_PARTIAL();
3452                  MRRETURN(MATCH_NOMATCH);
3453                  }
3454                if (fc == md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3455              }              }
3456            }            }
3457          /* Control never gets here */          /* Control never gets here */
# Line 2644  for (;;) Line 3471  for (;;)
3471            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3472              {              {
3473              int len = 1;              int len = 1;
3474              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3475                  {
3476                  SCHECK_PARTIAL();
3477                  break;
3478                  }
3479              GETCHARLEN(d, eptr, len);              GETCHARLEN(d, eptr, len);
3480              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3481              if (fc == d) break;              if (fc == d) break;
# Line 2653  for (;;) Line 3484  for (;;)
3484          if (possessive) continue;          if (possessive) continue;
3485          for(;;)          for(;;)
3486              {              {
3487              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM30);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM30);
3488              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3489              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3490              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2665  for (;;) Line 3496  for (;;)
3496            {            {
3497            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3498              {              {
3499              if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break;              if (eptr >= md->end_subject)
3500                  {
3501                  SCHECK_PARTIAL();
3502                  break;
3503                  }
3504                if (fc == md->lcc[*eptr]) break;
3505              eptr++;              eptr++;
3506              }              }
3507            if (possessive) continue;            if (possessive) continue;
3508            while (eptr >= pp)            while (eptr >= pp)
3509              {              {
3510              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM31);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM31);
3511              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3512              eptr--;              eptr--;
3513              }              }
3514            }            }
3515    
3516          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3517          }          }
3518        /* Control never gets here */        /* Control never gets here */
3519        }        }
# Line 2693  for (;;) Line 3529  for (;;)
3529          register unsigned int d;          register unsigned int d;
3530          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3531            {            {
3532              if (eptr >= md->end_subject)
3533                {
3534                SCHECK_PARTIAL();
3535                MRRETURN(MATCH_NOMATCH);
3536                }
3537            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3538            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) MRRETURN(MATCH_NOMATCH);
3539            }            }
3540          }          }
3541        else        else
# Line 2702  for (;;) Line 3543  for (;;)
3543        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3544          {          {
3545          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3546            if (fc == *eptr++) RRETURN(MATCH_NOMATCH);            {
3547              if (eptr >= md->end_subject)
3548                {
3549                SCHECK_PARTIAL();
3550                MRRETURN(MATCH_NOMATCH);
3551                }
3552              if (fc == *eptr++) MRRETURN(MATCH_NOMATCH);
3553              }
3554          }          }
3555    
3556        if (min == max) continue;        if (min == max) continue;
# Line 2716  for (;;) Line 3564  for (;;)
3564            register unsigned int d;            register unsigned int d;
3565            for (fi = min;; fi++)            for (fi = min;; fi++)
3566              {              {
3567              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM32);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM32);
3568              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3569                if (fi >= max) MRRETURN(MATCH_NOMATCH);
3570                if (eptr >= md->end_subject)
3571                  {
3572                  SCHECK_PARTIAL();
3573                  MRRETURN(MATCH_NOMATCH);
3574                  }
3575              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3576              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) MRRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
3577              }              }
3578            }            }
3579          else          else
# Line 2729  for (;;) Line 3582  for (;;)
3582            {            {
3583            for (fi = min;; fi++)            for (fi = min;; fi++)
3584              {              {
3585              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM33);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM33);
3586              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3587              if (fi >= max || eptr >= md->end_subject || fc == *eptr++)              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3588                RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3589                  {
3590                  SCHECK_PARTIAL();
3591                  MRRETURN(MATCH_NOMATCH);
3592                  }
3593                if (fc == *eptr++) MRRETURN(MATCH_NOMATCH);
3594              }              }
3595            }            }
3596          /* Control never gets here */          /* Control never gets here */
# Line 2752  for (;;) Line 3610  for (;;)
3610            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3611              {              {
3612              int len = 1;              int len = 1;
3613              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3614                  {
3615                  SCHECK_PARTIAL();
3616                  break;
3617                  }
3618              GETCHARLEN(d, eptr, len);              GETCHARLEN(d, eptr, len);
3619              if (fc == d) break;              if (fc == d) break;
3620              eptr += len;              eptr += len;
# Line 2760  for (;;) Line 3622  for (;;)
3622            if (possessive) continue;            if (possessive) continue;
3623            for(;;)            for(;;)
3624              {              {
3625              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM34);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM34);
3626              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3627              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3628              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2772  for (;;) Line 3634  for (;;)
3634            {            {
3635            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3636              {              {
3637              if (eptr >= md->end_subject || fc == *eptr) break;              if (eptr >= md->end_subject)
3638                  {
3639                  SCHECK_PARTIAL();
3640                  break;
3641                  }
3642                if (fc == *eptr) break;
3643              eptr++;              eptr++;
3644              }              }
3645            if (possessive) continue;            if (possessive) continue;
3646            while (eptr >= pp)            while (eptr >= pp)
3647              {              {
3648              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM35);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM35);
3649              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3650              eptr--;              eptr--;
3651              }              }
3652            }            }
3653    
3654          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3655          }          }
3656        }        }
3657      /* Control never gets here */      /* Control never gets here */
# Line 2866  for (;;) Line 3733  for (;;)
3733    
3734      /* First, ensure the minimum number of matches are present. Use inline      /* First, ensure the minimum number of matches are present. Use inline
3735      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
3736      (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  
3737      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
3738      and single-bytes. */      and single-bytes. */
3739    
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3740      if (min > 0)      if (min > 0)
3741        {        {
3742  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 2881  for (;;) Line 3745  for (;;)
3745          switch(prop_type)          switch(prop_type)
3746            {            {
3747            case PT_ANY:            case PT_ANY:
3748            if (prop_fail_result) RRETURN(MATCH_NOMATCH);            if (prop_fail_result) MRRETURN(MATCH_NOMATCH);
3749            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3750              {              {
3751              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3752                  {
3753                  SCHECK_PARTIAL();
3754                  MRRETURN(MATCH_NOMATCH);
3755                  }
3756              GETCHARINCTEST(c, eptr);              GETCHARINCTEST(c, eptr);
3757              }              }
3758            break;            break;
# Line 2892  for (;;) Line 3760  for (;;)
3760            case PT_LAMP:            case PT_LAMP:
3761            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3762              {              {
3763              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3764                  {
3765                  SCHECK_PARTIAL();
3766                  MRRETURN(MATCH_NOMATCH);
3767                  }
3768              GETCHARINCTEST(c, eptr);              GETCHARINCTEST(c, eptr);
3769              prop_chartype = UCD_CHARTYPE(c);              prop_chartype = UCD_CHARTYPE(c);
3770              if ((prop_chartype == ucp_Lu ||              if ((prop_chartype == ucp_Lu ||
3771                   prop_chartype == ucp_Ll ||                   prop_chartype == ucp_Ll ||
3772                   prop_chartype == ucp_Lt) == prop_fail_result)                   prop_chartype == ucp_Lt) == prop_fail_result)
3773                RRETURN(MATCH_NOMATCH);                MRRETURN(MATCH_NOMATCH);
3774              }              }
3775            break;            break;
3776    
3777            case PT_GC:            case PT_GC:
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                  {
3782                  SCHECK_PARTIAL();
3783                  MRRETURN(MATCH_NOMATCH);
3784                  }
3785              GETCHARINCTEST(c, eptr);              GETCHARINCTEST(c, eptr);
3786              prop_category = UCD_CATEGORY(c);              prop_category = UCD_CATEGORY(c);
3787              if ((prop_category == prop_value) == prop_fail_result)              if ((prop_category == prop_value) == prop_fail_result)
3788                RRETURN(MATCH_NOMATCH);                MRRETURN(MATCH_NOMATCH);
3789              }              }
3790            break;            break;
3791    
3792            case PT_PC:            case PT_PC:
3793            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3794              {              {
3795              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3796                  {
3797                  SCHECK_PARTIAL();
3798                  MRRETURN(MATCH_NOMATCH);
3799                  }
3800              GETCHARINCTEST(c, eptr);              GETCHARINCTEST(c, eptr);
3801              prop_chartype = UCD_CHARTYPE(c);              prop_chartype = UCD_CHARTYPE(c);
3802              if ((prop_chartype == prop_value) == prop_fail_result)              if ((prop_chartype == prop_value) == prop_fail_result)
3803                RRETURN(MATCH_NOMATCH);                MRRETURN(MATCH_NOMATCH);
3804              }              }
3805            break;            break;
3806    
3807            case PT_SC:            case PT_SC:
3808            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3809              {              {
3810              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3811                  {
3812                  SCHECK_PARTIAL();
3813                  MRRETURN(MATCH_NOMATCH);
3814                  }
3815              GETCHARINCTEST(c, eptr);              GETCHARINCTEST(c, eptr);
3816              prop_script = UCD_SCRIPT(c);              prop_script = UCD_SCRIPT(c);
3817              if ((prop_script == prop_value) == prop_fail_result)              if ((prop_script == prop_value) == prop_fail_result)
3818                RRETURN(MATCH_NOMATCH);                MRRETURN(MATCH_NOMATCH);
3819                }
3820              break;
3821    
3822              case PT_ALNUM:
3823              for (i = 1; i <= min; i++)
3824                {
3825                if (eptr >= md->end_subject)
3826                  {
3827                  SCHECK_PARTIAL();
3828                  MRRETURN(MATCH_NOMATCH);
3829                  }
3830                GETCHARINCTEST(c, eptr);
3831                prop_category = UCD_CATEGORY(c);
3832                if ((prop_category == ucp_L || prop_category == ucp_N)
3833                       == prop_fail_result)
3834                  MRRETURN(MATCH_NOMATCH);
3835              }              }
3836            break;            break;
3837    
3838              case PT_SPACE:    /* Perl space */
3839              for (i = 1; i <= min; i++)
3840                {
3841                if (eptr >= md->end_subject)
3842                  {
3843                  SCHECK_PARTIAL();
3844                  MRRETURN(MATCH_NOMATCH);
3845                  }
3846                GETCHARINCTEST(c, eptr);
3847                prop_category = UCD_CATEGORY(c);
3848                if ((prop_category == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
3849                     c == CHAR_FF || c == CHAR_CR)
3850                       == prop_fail_result)
3851                  MRRETURN(MATCH_NOMATCH);
3852                }
3853              break;
3854    
3855              case PT_PXSPACE:  /* POSIX space */
3856              for (i = 1; i <= min; i++)
3857                {
3858                if (eptr >= md->end_subject)
3859                  {
3860                  SCHECK_PARTIAL();
3861                  MRRETURN(MATCH_NOMATCH);
3862                  }
3863                GETCHARINCTEST(c, eptr);
3864                prop_category = UCD_CATEGORY(c);
3865                if ((prop_category == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
3866                     c == CHAR_VT || c == CHAR_FF || c == CHAR_CR)
3867                       == prop_fail_result)
3868                  MRRETURN(MATCH_NOMATCH);
3869                }
3870              break;
3871    
3872              case PT_WORD:
3873              for (i = 1; i <= min; i++)
3874                {
3875                if (eptr >= md->end_subject)
3876                  {
3877                  SCHECK_PARTIAL();
3878                  MRRETURN(MATCH_NOMATCH);
3879                  }
3880                GETCHARINCTEST(c, eptr);
3881                prop_category = UCD_CATEGORY(c);
3882                if ((prop_category == ucp_L || prop_category == ucp_N ||
3883                     c == CHAR_UNDERSCORE)
3884                       == prop_fail_result)
3885                  MRRETURN(MATCH_NOMATCH);
3886                }
3887              break;
3888    
3889              /* This should not occur */
3890    
3891            default:            default:
3892            RRETURN(PCRE_ERROR_INTERNAL);            RRETURN(PCRE_ERROR_INTERNAL);
3893            }            }
# Line 2947  for (;;) Line 3900  for (;;)
3900          {          {
3901          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3902            {            {
3903              if (eptr >= md->end_subject)
3904                {
3905                SCHECK_PARTIAL();
3906                MRRETURN(MATCH_NOMATCH);
3907                }
3908            GETCHARINCTEST(c, eptr);            GETCHARINCTEST(c, eptr);
3909            prop_category = UCD_CATEGORY(c);            prop_category = UCD_CATEGORY(c);
3910            if (prop_category == ucp_M) RRETURN(MATCH_NOMATCH);            if (prop_category == ucp_M) MRRETURN(MATCH_NOMATCH);
3911            while (eptr < md->end_subject)            while (eptr < md->end_subject)
3912              {              {
3913              int len = 1;              int len = 1;
3914              if (!utf8) c = *eptr; else              if (!utf8) c = *eptr;
3915                {                else { GETCHARLEN(c, eptr, len); }
               GETCHARLEN(c, eptr, len);  
               }  
3916              prop_category = UCD_CATEGORY(c);              prop_category = UCD_CATEGORY(c);
3917              if (prop_category != ucp_M) break;              if (prop_category != ucp_M) break;
3918              eptr += len;              eptr += len;
# Line 2975  for (;;) Line 3931  for (;;)
3931          case OP_ANY:          case OP_ANY:
3932          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3933            {            {
3934            if (eptr >= md->end_subject || IS_NEWLINE(eptr))            if (eptr >= md->end_subject)
3935              RRETURN(MATCH_NOMATCH);              {
3936                SCHECK_PARTIAL();
3937                MRRETURN(MATCH_NOMATCH);
3938                }
3939              if (IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH);
3940            eptr++;            eptr++;
3941            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
3942            }            }
# Line 2985  for (;;) Line 3945  for (;;)
3945          case OP_ALLANY:          case OP_ALLANY:
3946          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3947            {            {
3948            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
3949                {
3950                SCHECK_PARTIAL();
3951                MRRETURN(MATCH_NOMATCH);
3952                }
3953            eptr++;            eptr++;
3954            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
3955            }            }
3956          break;          break;
3957    
3958          case OP_ANYBYTE:          case OP_ANYBYTE:
3959            if (eptr > md->end_subject - min) MRRETURN(MATCH_NOMATCH);
3960          eptr += min;          eptr += min;
3961          break;          break;
3962    
3963          case OP_ANYNL:          case OP_ANYNL:
3964          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3965            {            {
3966            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
3967                {
3968                SCHECK_PARTIAL();
3969                MRRETURN(MATCH_NOMATCH);
3970                }
3971            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
3972            switch(c)            switch(c)
3973              {              {
3974              default: RRETURN(MATCH_NOMATCH);              default: MRRETURN(MATCH_NOMATCH);
3975    
3976              case 0x000d:              case 0x000d:
3977              if (eptr < md->end_subject && *eptr == 0x0a) eptr++;              if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3978              break;              break;
# Line 3015  for (;;) Line 3985  for (;;)
3985              case 0x0085:              case 0x0085:
3986              case 0x2028:              case 0x2028:
3987              case 0x2029:              case 0x2029:
3988              if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);              if (md->bsr_anycrlf) MRRETURN(MATCH_NOMATCH);
3989              break;              break;
3990              }              }
3991            }            }
# Line 3024  for (;;) Line 3994  for (;;)
3994          case OP_NOT_HSPACE:          case OP_NOT_HSPACE:
3995          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3996            {            {
3997            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
3998                {
3999                SCHECK_PARTIAL();
4000                MRRETURN(MATCH_NOMATCH);
4001                }
4002            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
4003            switch(c)            switch(c)
4004