/[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 384 by ph10, Sun Mar 8 16:27:43 2009 UTC revision 771 by ph10, Tue Nov 29 15:34:12 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-2009 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_ONCE         (-996)
80    #define MATCH_PRUNE        (-995)
81    #define MATCH_SKIP         (-994)
82    #define MATCH_SKIP_ARG     (-993)
83    #define MATCH_THEN         (-992)
84    
85  /* 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.
86  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 95  static const char rep_max[] = { 0, 0, 0,
95    
96    
97    
98  #ifdef DEBUG  #ifdef PCRE_DEBUG
99  /*************************************************  /*************************************************
100  *        Debugging function to print chars       *  *        Debugging function to print chars       *
101  *************************************************/  *************************************************/
# Line 122  while (length-- > 0) Line 128  while (length-- > 0)
128  *          Match a back-reference                *  *          Match a back-reference                *
129  *************************************************/  *************************************************/
130    
131  /* 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
132  than the number of characters left in the string, so the match fails.  negative, so the match always fails. However, in JavaScript compatibility mode,
133    the length passed is zero. Note that in caseless UTF-8 mode, the number of
134    subject bytes matched may be different to the number of reference bytes.
135    
136  Arguments:  Arguments:
137    offset      index into the offset vector    offset      index into the offset vector
138    eptr        points into the subject    eptr        pointer into the subject
139    length      length to be matched    length      length of reference to be matched (number of bytes)
140    md          points to match data block    md          points to match data block
141    ims         the ims flags    caseless    TRUE if caseless
142    
143  Returns:      TRUE if matched  Returns:      < 0 if not matched, otherwise the number of subject bytes matched
144  */  */
145    
146  static BOOL  static int
147  match_ref(int offset, register USPTR eptr, int length, match_data *md,  match_ref(int offset, register USPTR eptr, int length, match_data *md,
148    unsigned long int ims)    BOOL caseless)
149  {  {
150  USPTR p = md->start_subject + md->offset_vector[offset];  USPTR eptr_start = eptr;
151    register USPTR p = md->start_subject + md->offset_vector[offset];
152    
153  #ifdef DEBUG  #ifdef PCRE_DEBUG
154  if (eptr >= md->end_subject)  if (eptr >= md->end_subject)
155    printf("matching subject <null>");    printf("matching subject <null>");
156  else  else
# Line 154  pchars(p, length, FALSE, md); Line 163  pchars(p, length, FALSE, md);
163  printf("\n");  printf("\n");
164  #endif  #endif
165    
166  /* Always fail if not enough characters left */  /* Always fail if reference not set (and not JavaScript compatible). */
167    
168  if (length > md->end_subject - eptr) return FALSE;  if (length < 0) return -1;
169    
170  /* 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
171  properly if Unicode properties are supported. Otherwise, we can check only  properly if Unicode properties are supported. Otherwise, we can check only
172  ASCII characters. */  ASCII characters. */
173    
174  if ((ims & PCRE_CASELESS) != 0)  if (caseless)
175    {    {
176  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
177  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
178    if (md->utf8)    if (md->utf8)
179      {      {
180      USPTR endptr = eptr + length;      /* Match characters up to the end of the reference. NOTE: the number of
181      while (eptr < endptr)      bytes matched may differ, because there are some characters whose upper and
182        lower case versions code as different numbers of bytes. For example, U+023A
183        (2 bytes in UTF-8) is the upper case version of U+2C65 (3 bytes in UTF-8);
184        a sequence of 3 of the former uses 6 bytes, as does a sequence of two of
185        the latter. It is important, therefore, to check the length along the
186        reference, not along the subject (earlier code did this wrong). */
187    
188        USPTR endptr = p + length;
189        while (p < endptr)
190        {        {
191        int c, d;        int c, d;
192          if (eptr >= md->end_subject) return -1;
193        GETCHARINC(c, eptr);        GETCHARINC(c, eptr);
194        GETCHARINC(d, p);        GETCHARINC(d, p);
195        if (c != d && c != UCD_OTHERCASE(d)) return FALSE;        if (c != d && c != UCD_OTHERCASE(d)) return -1;
196        }        }
197      }      }
198    else    else
# Line 183  if ((ims & PCRE_CASELESS) != 0) Line 201  if ((ims & PCRE_CASELESS) != 0)
201    
202    /* 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
203    is no UCP support. */    is no UCP support. */
204        {
205    while (length-- > 0)      if (eptr + length > md->end_subject) return -1;
206      { if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE; }      while (length-- > 0)
207          { if (md->lcc[*p++] != md->lcc[*eptr++]) return -1; }
208        }
209    }    }
210    
211  /* 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
212  are in UTF-8 mode. */  are in UTF-8 mode. */
213    
214  else  else
215    { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }    {
216      if (eptr + length > md->end_subject) return -1;
217      while (length-- > 0) if (*p++ != *eptr++) return -1;
218      }
219    
220  return TRUE;  return eptr - eptr_start;
221  }  }
222    
223    
# Line 245  enum { RM1=1, RM2,  RM3,  RM4,  RM5,  RM Line 268  enum { RM1=1, RM2,  RM3,  RM4,  RM5,  RM
268         RM21,  RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,         RM21,  RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
269         RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,         RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
270         RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,         RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
271         RM51,  RM52, RM53, RM54 };         RM51,  RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60,
272           RM61,  RM62, RM63, RM64, RM65, RM66 };
273    
274  /* 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
275  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
276  actuall used in this definition. */  actually used in this definition. */
277    
278  #ifndef NO_RECURSE  #ifndef NO_RECURSE
279  #define REGISTER register  #define REGISTER register
280    
281  #ifdef DEBUG  #ifdef PCRE_DEBUG
282  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \  #define RMATCH(ra,rb,rc,rd,re,rw) \
283    { \    { \
284    printf("match() called in line %d\n", __LINE__); \    printf("match() called in line %d\n", __LINE__); \
285    rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1); \    rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1); \
286    printf("to line %d\n", __LINE__); \    printf("to line %d\n", __LINE__); \
287    }    }
288  #define RRETURN(ra) \  #define RRETURN(ra) \
# Line 267  actuall used in this definition. */ Line 291  actuall used in this definition. */
291    return ra; \    return ra; \
292    }    }
293  #else  #else
294  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \  #define RMATCH(ra,rb,rc,rd,re,rw) \
295    rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1)    rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1)
296  #define RRETURN(ra) return ra  #define RRETURN(ra) return ra
297  #endif  #endif
298    
# Line 281  argument of match(), which never changes Line 305  argument of match(), which never changes
305    
306  #define REGISTER  #define REGISTER
307    
308  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw)\  #define RMATCH(ra,rb,rc,rd,re,rw)\
309    {\    {\
310    heapframe *newframe = (pcre_stack_malloc)(sizeof(heapframe));\    heapframe *newframe = (heapframe *)(pcre_stack_malloc)(sizeof(heapframe));\
311      if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\
312    frame->Xwhere = rw; \    frame->Xwhere = rw; \
313    newframe->Xeptr = ra;\    newframe->Xeptr = ra;\
314    newframe->Xecode = rb;\    newframe->Xecode = rb;\
315    newframe->Xmstart = mstart;\    newframe->Xmstart = mstart;\
316    newframe->Xoffset_top = rc;\    newframe->Xoffset_top = rc;\
317    newframe->Xims = re;\    newframe->Xeptrb = re;\
   newframe->Xeptrb = rf;\  
   newframe->Xflags = rg;\  
318    newframe->Xrdepth = frame->Xrdepth + 1;\    newframe->Xrdepth = frame->Xrdepth + 1;\
319    newframe->Xprevframe = frame;\    newframe->Xprevframe = frame;\
320    frame = newframe;\    frame = newframe;\
# Line 303  argument of match(), which never changes Line 326  argument of match(), which never changes
326    
327  #define RRETURN(ra)\  #define RRETURN(ra)\
328    {\    {\
329    heapframe *newframe = frame;\    heapframe *oldframe = frame;\
330    frame = newframe->Xprevframe;\    frame = oldframe->Xprevframe;\
331    (pcre_stack_free)(newframe);\    (pcre_stack_free)(oldframe);\
332    if (frame != NULL)\    if (frame != NULL)\
333      {\      {\
334      rrc = ra;\      rrc = ra;\
# Line 322  typedef struct heapframe { Line 345  typedef struct heapframe {
345    
346    /* Function arguments that may change */    /* Function arguments that may change */
347    
348    const uschar *Xeptr;    USPTR Xeptr;
349    const uschar *Xecode;    const uschar *Xecode;
350    const uschar *Xmstart;    USPTR Xmstart;
351    int Xoffset_top;    int Xoffset_top;
   long int Xims;  
352    eptrblock *Xeptrb;    eptrblock *Xeptrb;
   int Xflags;  
353    unsigned int Xrdepth;    unsigned int Xrdepth;
354    
355    /* Function local variables */    /* Function local variables */
356    
357    const uschar *Xcallpat;    USPTR Xcallpat;
358    const uschar *Xcharptr;  #ifdef SUPPORT_UTF8
359    const uschar *Xdata;    USPTR Xcharptr;
360    const uschar *Xnext;  #endif
361    const uschar *Xpp;    USPTR Xdata;
362    const uschar *Xprev;    USPTR Xnext;
363    const uschar *Xsaved_eptr;    USPTR Xpp;
364      USPTR Xprev;
365      USPTR Xsaved_eptr;
366    
367    recursion_info Xnew_recursive;    recursion_info Xnew_recursive;
368    
# Line 347  typedef struct heapframe { Line 370  typedef struct heapframe {
370    BOOL Xcondition;    BOOL Xcondition;
371    BOOL Xprev_is_word;    BOOL Xprev_is_word;
372    
   unsigned long int Xoriginal_ims;  
   
373  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
374    int Xprop_type;    int Xprop_type;
375    int Xprop_value;    int Xprop_value;
376    int Xprop_fail_result;    int Xprop_fail_result;
   int Xprop_category;  
   int Xprop_chartype;  
   int Xprop_script;  
377    int Xoclength;    int Xoclength;
378    uschar Xocchars[8];    uschar Xocchars[8];
379  #endif  #endif
380    
381      int Xcodelink;
382    int Xctype;    int Xctype;
383    unsigned int Xfc;    unsigned int Xfc;
384    int Xfi;    int Xfi;
# Line 395  typedef struct heapframe { Line 414  typedef struct heapframe {
414    
415  /* This function is called recursively in many circumstances. Whenever it  /* This function is called recursively in many circumstances. Whenever it
416  returns a negative (error) response, the outer incarnation must also return the  returns a negative (error) response, the outer incarnation must also return the
417  same response.  same response. */
418    
419    /* These macros pack up tests that are used for partial matching, and which
420    appears several times in the code. We set the "hit end" flag if the pointer is
421    at the end of the subject and also past the start of the subject (i.e.
422    something has been matched). For hard partial matching, we then return
423    immediately. The second one is used when we already know we are past the end of
424    the subject. */
425    
426    #define CHECK_PARTIAL()\
427      if (md->partial != 0 && eptr >= md->end_subject && \
428          eptr > md->start_used_ptr) \
429        { \
430        md->hitend = TRUE; \
431        if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
432        }
433    
434  Performance note: It might be tempting to extract commonly used fields from the  #define SCHECK_PARTIAL()\
435  md structure (e.g. utf8, end_subject) into individual variables to improve    if (md->partial != 0 && eptr > md->start_used_ptr) \
436        { \
437        md->hitend = TRUE; \
438        if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
439        }
440    
441    
442    /* Performance note: It might be tempting to extract commonly used fields from
443    the md structure (e.g. utf8, end_subject) into individual variables to improve
444  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
445  made performance worse.  made performance worse.
446    
# Line 409  Arguments: Line 451  Arguments:
451                   by encountering \K)                   by encountering \K)
452     offset_top  current top pointer     offset_top  current top pointer
453     md          pointer to "static" info for the match     md          pointer to "static" info for the match
    ims         current /i, /m, and /s options  
454     eptrb       pointer to chain of blocks containing eptr at start of     eptrb       pointer to chain of blocks containing eptr at start of
455                   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  
456     rdepth      the recursion depth     rdepth      the recursion depth
457    
458  Returns:       MATCH_MATCH if matched            )  these values are >= 0  Returns:       MATCH_MATCH if matched            )  these values are >= 0
459                 MATCH_NOMATCH if failed to match  )                 MATCH_NOMATCH if failed to match  )
460                   a negative MATCH_xxx value for PRUNE, SKIP, etc
461                 a negative PCRE_ERROR_xxx value if aborted by an error condition                 a negative PCRE_ERROR_xxx value if aborted by an error condition
462                   (e.g. stopped by repeated call or recursion limit)                   (e.g. stopped by repeated call or recursion limit)
463  */  */
464    
465  static int  static int
466  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, const uschar *mstart,  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, USPTR mstart,
467    int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,    int offset_top, match_data *md, eptrblock *eptrb, unsigned int rdepth)
   int flags, unsigned int rdepth)  
468  {  {
469  /* 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,
470  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 476  register unsigned int c;   /* Character
476  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */
477    
478  BOOL minimize, possessive; /* Quantifier options */  BOOL minimize, possessive; /* Quantifier options */
479    BOOL caseless;
480    int condcode;
481    
482  /* 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
483  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 485  heap storage. Set up the top-level frame
485  heap whenever RMATCH() does a "recursion". See the macro definitions above. */  heap whenever RMATCH() does a "recursion". See the macro definitions above. */
486    
487  #ifdef NO_RECURSE  #ifdef NO_RECURSE
488  heapframe *frame = (pcre_stack_malloc)(sizeof(heapframe));  heapframe *frame = (heapframe *)(pcre_stack_malloc)(sizeof(heapframe));
489    if (frame == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
490  frame->Xprevframe = NULL;            /* Marks the top level */  frame->Xprevframe = NULL;            /* Marks the top level */
491    
492  /* Copy in the original argument variables */  /* Copy in the original argument variables */
# Line 455  frame->Xeptr = eptr; Line 495  frame->Xeptr = eptr;
495  frame->Xecode = ecode;  frame->Xecode = ecode;
496  frame->Xmstart = mstart;  frame->Xmstart = mstart;
497  frame->Xoffset_top = offset_top;  frame->Xoffset_top = offset_top;
 frame->Xims = ims;  
498  frame->Xeptrb = eptrb;  frame->Xeptrb = eptrb;
 frame->Xflags = flags;  
499  frame->Xrdepth = rdepth;  frame->Xrdepth = rdepth;
500    
501  /* This is where control jumps back to to effect "recursion" */  /* This is where control jumps back to to effect "recursion" */
# Line 470  HEAP_RECURSE: Line 508  HEAP_RECURSE:
508  #define ecode              frame->Xecode  #define ecode              frame->Xecode
509  #define mstart             frame->Xmstart  #define mstart             frame->Xmstart
510  #define offset_top         frame->Xoffset_top  #define offset_top         frame->Xoffset_top
 #define ims                frame->Xims  
511  #define eptrb              frame->Xeptrb  #define eptrb              frame->Xeptrb
 #define flags              frame->Xflags  
512  #define rdepth             frame->Xrdepth  #define rdepth             frame->Xrdepth
513    
514  /* Ditto for the local variables */  /* Ditto for the local variables */
# Line 481  HEAP_RECURSE: Line 517  HEAP_RECURSE:
517  #define charptr            frame->Xcharptr  #define charptr            frame->Xcharptr
518  #endif  #endif
519  #define callpat            frame->Xcallpat  #define callpat            frame->Xcallpat
520    #define codelink           frame->Xcodelink
521  #define data               frame->Xdata  #define data               frame->Xdata
522  #define next               frame->Xnext  #define next               frame->Xnext
523  #define pp                 frame->Xpp  #define pp                 frame->Xpp
# Line 493  HEAP_RECURSE: Line 530  HEAP_RECURSE:
530  #define condition          frame->Xcondition  #define condition          frame->Xcondition
531  #define prev_is_word       frame->Xprev_is_word  #define prev_is_word       frame->Xprev_is_word
532    
 #define original_ims       frame->Xoriginal_ims  
   
533  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
534  #define prop_type          frame->Xprop_type  #define prop_type          frame->Xprop_type
535  #define prop_value         frame->Xprop_value  #define prop_value         frame->Xprop_value
536  #define prop_fail_result   frame->Xprop_fail_result  #define prop_fail_result   frame->Xprop_fail_result
 #define prop_category      frame->Xprop_category  
 #define prop_chartype      frame->Xprop_chartype  
 #define prop_script        frame->Xprop_script  
537  #define oclength           frame->Xoclength  #define oclength           frame->Xoclength
538  #define occhars            frame->Xocchars  #define occhars            frame->Xocchars
539  #endif  #endif
# Line 531  i, and fc and c, can be the same variabl Line 563  i, and fc and c, can be the same variabl
563  #define fi i  #define fi i
564  #define fc c  #define fc c
565    
566    /* Many of the following variables are used only in small blocks of the code.
567    My normal style of coding would have declared them within each of those blocks.
568    However, in order to accommodate the version of this code that uses an external
569    "stack" implemented on the heap, it is easier to declare them all here, so the
570    declarations can be cut out in a block. The only declarations within blocks
571    below are for variables that do not have to be preserved over a recursive call
572    to RMATCH(). */
573    
574  #ifdef SUPPORT_UTF8                /* Many of these variables are used only  */  #ifdef SUPPORT_UTF8
575  const uschar *charptr;             /* in small blocks of the code. My normal */  const uschar *charptr;
576  #endif                             /* style of coding would have declared    */  #endif
577  const uschar *callpat;             /* them within each of those blocks.      */  const uschar *callpat;
578  const uschar *data;                /* However, in order to accommodate the   */  const uschar *data;
579  const uschar *next;                /* version of this code that uses an      */  const uschar *next;
580  USPTR         pp;                  /* external "stack" implemented on the    */  USPTR         pp;
581  const uschar *prev;                /* heap, it is easier to declare them all */  const uschar *prev;
582  USPTR         saved_eptr;          /* here, so the declarations can be cut   */  USPTR         saved_eptr;
583                                     /* out in a block. The only declarations  */  
584  recursion_info new_recursive;      /* within blocks below are for variables  */  recursion_info new_recursive;
585                                     /* that do not have to be preserved over  */  
586  BOOL cur_is_word;                  /* a recursive call to RMATCH().          */  BOOL cur_is_word;
587  BOOL condition;  BOOL condition;
588  BOOL prev_is_word;  BOOL prev_is_word;
589    
 unsigned long int original_ims;  
   
590  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
591  int prop_type;  int prop_type;
592  int prop_value;  int prop_value;
593  int prop_fail_result;  int prop_fail_result;
 int prop_category;  
 int prop_chartype;  
 int prop_script;  
594  int oclength;  int oclength;
595  uschar occhars[8];  uschar occhars[8];
596  #endif  #endif
597    
598    int codelink;
599  int ctype;  int ctype;
600  int length;  int length;
601  int max;  int max;
# Line 575  int stacksave[REC_STACK_SAVE_MAX]; Line 610  int stacksave[REC_STACK_SAVE_MAX];
610  eptrblock newptrb;  eptrblock newptrb;
611  #endif     /* NO_RECURSE */  #endif     /* NO_RECURSE */
612    
613    /* To save space on the stack and in the heap frame, I have doubled up on some
614    of the local variables that are used only in localised parts of the code, but
615    still need to be preserved over recursive calls of match(). These macros define
616    the alternative names that are used. */
617    
618    #define allow_zero    cur_is_word
619    #define cbegroup      condition
620    #define code_offset   codelink
621    #define condassert    condition
622    #define matched_once  prev_is_word
623    
624  /* These statements are here to stop the compiler complaining about unitialized  /* These statements are here to stop the compiler complaining about unitialized
625  variables. */  variables. */
626    
# Line 594  TAIL_RECURSE: Line 640  TAIL_RECURSE:
640  /* 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
641  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
642  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()
643  and a "return", respectively (possibly with some debugging if DEBUG is  and a "return", respectively (possibly with some debugging if PCRE_DEBUG is
644  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
645  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,
646  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 657  haven't exceeded the recursive call limi
657  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
658  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
659    
 original_ims = ims;    /* Save for resetting on ')' */  
   
660  /* 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
661  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
662  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
663  hit the closing ket, in order to break infinite loops that match no characters.  up space on the stack. See also MATCH_CONDASSERT below.
664  When match() is called in other circumstances, don't add to the chain. The  
665  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
666  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
667  match(). */  to break infinite loops that match no characters. When match() is called in
668    other circumstances, don't add to the chain. The MATCH_CBEGROUP feature must
669    NOT be used with tail recursion, because the memory block that is used is on
670    the stack, so a new one may be required for each match(). */
671    
672  if ((flags & match_cbegroup) != 0)  if (md->match_function_type == MATCH_CBEGROUP)
673    {    {
674    newptrb.epb_saved_eptr = eptr;    newptrb.epb_saved_eptr = eptr;
675    newptrb.epb_prev = eptrb;    newptrb.epb_prev = eptrb;
676    eptrb = &newptrb;    eptrb = &newptrb;
677      md->match_function_type = 0;
678    }    }
679    
680  /* Now start processing the opcodes. */  /* Now start processing the opcodes. */
# Line 636  for (;;) Line 684  for (;;)
684    minimize = possessive = FALSE;    minimize = possessive = FALSE;
685    op = *ecode;    op = *ecode;
686    
   /* 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;  
   
687    switch(op)    switch(op)
688      {      {
689        case OP_MARK:
690        md->nomatch_mark = ecode + 2;
691        md->mark = NULL;    /* In case previously set by assertion */
692        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
693          eptrb, RM55);
694        if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
695             md->mark == NULL) md->mark = ecode + 2;
696    
697        /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
698        argument, and we must check whether that argument matches this MARK's
699        argument. It is passed back in md->start_match_ptr (an overloading of that
700        variable). If it does match, we reset that variable to the current subject
701        position and return MATCH_SKIP. Otherwise, pass back the return code
702        unaltered. */
703    
704        else if (rrc == MATCH_SKIP_ARG &&
705            strcmp((char *)(ecode + 2), (char *)(md->start_match_ptr)) == 0)
706          {
707          md->start_match_ptr = eptr;
708          RRETURN(MATCH_SKIP);
709          }
710        RRETURN(rrc);
711    
712      case OP_FAIL:      case OP_FAIL:
713      RRETURN(MATCH_NOMATCH);      RRETURN(MATCH_NOMATCH);
714    
715      case OP_PRUNE:      /* COMMIT overrides PRUNE, SKIP, and THEN */
     RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,  
       ims, eptrb, flags, RM51);  
     if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
     RRETURN(MATCH_PRUNE);  
716    
717      case OP_COMMIT:      case OP_COMMIT:
718      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
719        ims, eptrb, flags, RM52);        eptrb, RM52);
720      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE &&
721            rrc != MATCH_SKIP && rrc != MATCH_SKIP_ARG &&
722            rrc != MATCH_THEN)
723          RRETURN(rrc);
724      RRETURN(MATCH_COMMIT);      RRETURN(MATCH_COMMIT);
725    
726        /* PRUNE overrides THEN */
727    
728        case OP_PRUNE:
729        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
730          eptrb, RM51);
731        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
732        RRETURN(MATCH_PRUNE);
733    
734        case OP_PRUNE_ARG:
735        md->nomatch_mark = ecode + 2;
736        md->mark = NULL;    /* In case previously set by assertion */
737        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
738          eptrb, RM56);
739        if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
740             md->mark == NULL) md->mark = ecode + 2;
741        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
742        RRETURN(MATCH_PRUNE);
743    
744        /* SKIP overrides PRUNE and THEN */
745    
746      case OP_SKIP:      case OP_SKIP:
747      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
748        ims, eptrb, flags, RM53);        eptrb, RM53);
749      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
750          RRETURN(rrc);
751      md->start_match_ptr = eptr;   /* Pass back current position */      md->start_match_ptr = eptr;   /* Pass back current position */
752      RRETURN(MATCH_SKIP);      RRETURN(MATCH_SKIP);
753    
754        /* Note that, for Perl compatibility, SKIP with an argument does NOT set
755        nomatch_mark. There is a flag that disables this opcode when re-matching a
756        pattern that ended with a SKIP for which there was not a matching MARK. */
757    
758        case OP_SKIP_ARG:
759        if (md->ignore_skip_arg)
760          {
761          ecode += _pcre_OP_lengths[*ecode] + ecode[1];
762          break;
763          }
764        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
765          eptrb, RM57);
766        if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
767          RRETURN(rrc);
768    
769        /* Pass back the current skip name by overloading md->start_match_ptr and
770        returning the special MATCH_SKIP_ARG return code. This will either be
771        caught by a matching MARK, or get to the top, where it causes a rematch
772        with the md->ignore_skip_arg flag set. */
773    
774        md->start_match_ptr = ecode + 2;
775        RRETURN(MATCH_SKIP_ARG);
776    
777        /* For THEN (and THEN_ARG) we pass back the address of the opcode, so that
778        the branch in which it occurs can be determined. Overload the start of
779        match pointer to do this. */
780    
781      case OP_THEN:      case OP_THEN:
782      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
783        ims, eptrb, flags, RM54);        eptrb, RM54);
784        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
785        md->start_match_ptr = ecode;
786        RRETURN(MATCH_THEN);
787    
788        case OP_THEN_ARG:
789        md->nomatch_mark = ecode + 2;
790        md->mark = NULL;    /* In case previously set by assertion */
791        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top,
792          md, eptrb, RM58);
793        if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
794             md->mark == NULL) md->mark = ecode + 2;
795      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
796        md->start_match_ptr = ecode;
797      RRETURN(MATCH_THEN);      RRETURN(MATCH_THEN);
798    
799      /* Handle a capturing bracket. If there is space in the offset vector, save      /* Handle an atomic group that does not contain any capturing parentheses.
800      the current subject position in the working slot at the top of the vector.      This can be handled like an assertion. Prior to 8.13, all atomic groups
801      We mustn't change the current values of the data slot, because they may be      were handled this way. In 8.13, the code was changed as below for ONCE, so
802      set from a previous iteration of this group, and be referred to by a      that backups pass through the group and thereby reset captured values.
803      reference inside the group.      However, this uses a lot more stack, so in 8.20, atomic groups that do not
804        contain any captures generate OP_ONCE_NC, which can be handled in the old,
805      If the bracket fails to match, we need to restore this value and also the      less stack intensive way.
806      values of the final offsets, in case they were set by a previous iteration  
807      of the same bracket.      Check the alternative branches in turn - the matching won't pass the KET
808        for this kind of subpattern. If any one branch matches, we carry on as at
809        the end of a normal bracket, leaving the subject pointer, but resetting
810        the start-of-match value in case it was changed by \K. */
811    
812        case OP_ONCE_NC:
813        prev = ecode;
814        saved_eptr = eptr;
815        do
816          {
817          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM64);
818          if (rrc == MATCH_MATCH)  /* Note: _not_ MATCH_ACCEPT */
819            {
820            mstart = md->start_match_ptr;
821            break;
822            }
823          if (rrc == MATCH_THEN)
824            {
825            next = ecode + GET(ecode,1);
826            if (md->start_match_ptr < next &&
827                (*ecode == OP_ALT || *next == OP_ALT))
828              rrc = MATCH_NOMATCH;
829            }
830    
831          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
832          ecode += GET(ecode,1);
833          }
834        while (*ecode == OP_ALT);
835    
836        /* If hit the end of the group (which could be repeated), fail */
837    
838        if (*ecode != OP_ONCE_NC && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
839    
840        /* Continue as from after the group, updating the offsets high water
841        mark, since extracts may have been taken. */
842    
843        do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
844    
845        offset_top = md->end_offset_top;
846        eptr = md->end_match_ptr;
847    
848        /* For a non-repeating ket, just continue at this level. This also
849        happens for a repeating ket if no characters were matched in the group.
850        This is the forcible breaking of infinite loops as implemented in Perl
851        5.005. */
852    
853        if (*ecode == OP_KET || eptr == saved_eptr)
854          {
855          ecode += 1+LINK_SIZE;
856          break;
857          }
858    
859        /* The repeating kets try the rest of the pattern or restart from the
860        preceding bracket, in the appropriate order. The second "call" of match()
861        uses tail recursion, to avoid using another stack frame. */
862    
863        if (*ecode == OP_KETRMIN)
864          {
865          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM65);
866          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
867          ecode = prev;
868          goto TAIL_RECURSE;
869          }
870        else  /* OP_KETRMAX */
871          {
872          md->match_function_type = MATCH_CBEGROUP;
873          RMATCH(eptr, prev, offset_top, md, eptrb, RM66);
874          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
875          ecode += 1 + LINK_SIZE;
876          goto TAIL_RECURSE;
877          }
878        /* Control never gets here */
879    
880        /* Handle a capturing bracket, other than those that are possessive with an
881        unlimited repeat. If there is space in the offset vector, save the current
882        subject position in the working slot at the top of the vector. We mustn't
883        change the current values of the data slot, because they may be set from a
884        previous iteration of this group, and be referred to by a reference inside
885        the group. A failure to match might occur after the group has succeeded,
886        if something later on doesn't match. For this reason, we need to restore
887        the working value and also the values of the final offsets, in case they
888        were set by a previous iteration of the same bracket.
889    
890      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
891      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 693  for (;;) Line 896  for (;;)
896      number = GET2(ecode, 1+LINK_SIZE);      number = GET2(ecode, 1+LINK_SIZE);
897      offset = number << 1;      offset = number << 1;
898    
899  #ifdef DEBUG  #ifdef PCRE_DEBUG
900      printf("start bracket %d\n", number);      printf("start bracket %d\n", number);
901      printf("subject=");      printf("subject=");
902      pchars(eptr, 16, TRUE, md);      pchars(eptr, 16, TRUE, md);
# Line 708  for (;;) Line 911  for (;;)
911        save_capture_last = md->capture_last;        save_capture_last = md->capture_last;
912    
913        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
914        md->offset_vector[md->offset_end - number] = eptr - md->start_subject;        md->offset_vector[md->offset_end - number] =
915            (int)(eptr - md->start_subject);
916    
917        flags = (op == OP_SCBRA)? match_cbegroup : 0;        for (;;)
       do  
918          {          {
919            if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
920          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
921            ims, eptrb, flags, RM1);            eptrb, RM1);
922          if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);          if (rrc == MATCH_ONCE) break;  /* Backing up through an atomic group */
923    
924            /* If we backed up to a THEN, check whether it is within the current
925            branch by comparing the address of the THEN that is passed back with
926            the end of the branch. If it is within the current branch, and the
927            branch is one of two or more alternatives (it either starts or ends
928            with OP_ALT), we have reached the limit of THEN's action, so convert
929            the return code to NOMATCH, which will cause normal backtracking to
930            happen from now on. Otherwise, THEN is passed back to an outer
931            alternative. This implements Perl's treatment of parenthesized groups,
932            where a group not containing | does not affect the current alternative,
933            that is, (X) is NOT the same as (X|(*F)). */
934    
935            if (rrc == MATCH_THEN)
936              {
937              next = ecode + GET(ecode,1);
938              if (md->start_match_ptr < next &&
939                  (*ecode == OP_ALT || *next == OP_ALT))
940                rrc = MATCH_NOMATCH;
941              }
942    
943            /* Anything other than NOMATCH is passed back. */
944    
945            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
946          md->capture_last = save_capture_last;          md->capture_last = save_capture_last;
947          ecode += GET(ecode, 1);          ecode += GET(ecode, 1);
948            if (*ecode != OP_ALT) break;
949          }          }
       while (*ecode == OP_ALT);  
950    
951        DPRINTF(("bracket %d failed\n", number));        DPRINTF(("bracket %d failed\n", number));
   
952        md->offset_vector[offset] = save_offset1;        md->offset_vector[offset] = save_offset1;
953        md->offset_vector[offset+1] = save_offset2;        md->offset_vector[offset+1] = save_offset2;
954        md->offset_vector[md->offset_end - number] = save_offset3;        md->offset_vector[md->offset_end - number] = save_offset3;
955    
956        RRETURN(MATCH_NOMATCH);        /* At this point, rrc will be one of MATCH_ONCE or MATCH_NOMATCH. */
957    
958          RRETURN(rrc);
959        }        }
960    
961      /* FALL THROUGH ... Insufficient room for saving captured contents. Treat      /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
# Line 741  for (;;) Line 969  for (;;)
969      /* VVVVVVVVVVVVVVVVVVVVVVVVV */      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
970      /* VVVVVVVVVVVVVVVVVVVVVVVVV */      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
971    
972      /* Non-capturing bracket. Loop for all the alternatives. When we get to the      /* Non-capturing or atomic group, except for possessive with unlimited
973      final alternative within the brackets, we would return the result of a      repeat and ONCE group with no captures. Loop for all the alternatives.
974      recursive call to match() whatever happened. We can reduce stack usage by  
975      turning this into a tail recursion, except in the case when match_cbegroup      When we get to the final alternative within the brackets, we used to return
976      is set.*/      the result of a recursive call to match() whatever happened so it was
977        possible to reduce stack usage by turning this into a tail recursion,
978        except in the case of a possibly empty group. However, now that there is
979        the possiblity of (*THEN) occurring in the final alternative, this
980        optimization is no longer always possible.
981    
982        We can optimize if we know there are no (*THEN)s in the pattern; at present
983        this is the best that can be done.
984    
985        MATCH_ONCE is returned when the end of an atomic group is successfully
986        reached, but subsequent matching fails. It passes back up the tree (causing
987        captured values to be reset) until the original atomic group level is
988        reached. This is tested by comparing md->once_target with the start of the
989        group. At this point, the return is converted into MATCH_NOMATCH so that
990        previous backup points can be taken. */
991    
992        case OP_ONCE:
993      case OP_BRA:      case OP_BRA:
994      case OP_SBRA:      case OP_SBRA:
995      DPRINTF(("start non-capturing bracket\n"));      DPRINTF(("start non-capturing bracket\n"));
996      flags = (op >= OP_SBRA)? match_cbegroup : 0;  
997      for (;;)      for (;;)
998        {        {
999        if (ecode[GET(ecode, 1)] != OP_ALT)   /* Final alternative */        if (op >= OP_SBRA || op == OP_ONCE) md->match_function_type = MATCH_CBEGROUP;
1000    
1001          /* If this is not a possibly empty group, and there are no (*THEN)s in
1002          the pattern, and this is the final alternative, optimize as described
1003          above. */
1004    
1005          else if (!md->hasthen && ecode[GET(ecode, 1)] != OP_ALT)
1006            {
1007            ecode += _pcre_OP_lengths[*ecode];
1008            goto TAIL_RECURSE;
1009            }
1010    
1011          /* In all other cases, we have to make another call to match(). */
1012    
1013          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, eptrb,
1014            RM2);
1015    
1016          /* See comment in the code for capturing groups above about handling
1017          THEN. */
1018    
1019          if (rrc == MATCH_THEN)
1020            {
1021            next = ecode + GET(ecode,1);
1022            if (md->start_match_ptr < next &&
1023                (*ecode == OP_ALT || *next == OP_ALT))
1024              rrc = MATCH_NOMATCH;
1025            }
1026    
1027          if (rrc != MATCH_NOMATCH)
1028          {          {
1029          if (flags == 0)    /* Not a possibly empty group */          if (rrc == MATCH_ONCE)
1030            {            {
1031            ecode += _pcre_OP_lengths[*ecode];            const uschar *scode = ecode;
1032            DPRINTF(("bracket 0 tail recursion\n"));            if (*scode != OP_ONCE)           /* If not at start, find it */
1033            goto TAIL_RECURSE;              {
1034                while (*scode == OP_ALT) scode += GET(scode, 1);
1035                scode -= GET(scode, 1);
1036                }
1037              if (md->once_target == scode) rrc = MATCH_NOMATCH;
1038            }            }
1039            RRETURN(rrc);
1040            }
1041          ecode += GET(ecode, 1);
1042          if (*ecode != OP_ALT) break;
1043          }
1044    
1045          /* Possibly empty group; can't use tail recursion. */      RRETURN(MATCH_NOMATCH);
1046    
1047          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,      /* Handle possessive capturing brackets with an unlimited repeat. We come
1048            eptrb, flags, RM48);      here from BRAZERO with allow_zero set TRUE. The offset_vector values are
1049          RRETURN(rrc);      handled similarly to the normal case above. However, the matching is
1050        different. The end of these brackets will always be OP_KETRPOS, which
1051        returns MATCH_KETRPOS without going further in the pattern. By this means
1052        we can handle the group by iteration rather than recursion, thereby
1053        reducing the amount of stack needed. */
1054    
1055        case OP_CBRAPOS:
1056        case OP_SCBRAPOS:
1057        allow_zero = FALSE;
1058    
1059        POSSESSIVE_CAPTURE:
1060        number = GET2(ecode, 1+LINK_SIZE);
1061        offset = number << 1;
1062    
1063    #ifdef PCRE_DEBUG
1064        printf("start possessive bracket %d\n", number);
1065        printf("subject=");
1066        pchars(eptr, 16, TRUE, md);
1067        printf("\n");
1068    #endif
1069    
1070        if (offset < md->offset_max)
1071          {
1072          matched_once = FALSE;
1073          code_offset = ecode - md->start_code;
1074    
1075          save_offset1 = md->offset_vector[offset];
1076          save_offset2 = md->offset_vector[offset+1];
1077          save_offset3 = md->offset_vector[md->offset_end - number];
1078          save_capture_last = md->capture_last;
1079    
1080          DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
1081    
1082          /* Each time round the loop, save the current subject position for use
1083          when the group matches. For MATCH_MATCH, the group has matched, so we
1084          restart it with a new subject starting position, remembering that we had
1085          at least one match. For MATCH_NOMATCH, carry on with the alternatives, as
1086          usual. If we haven't matched any alternatives in any iteration, check to
1087          see if a previous iteration matched. If so, the group has matched;
1088          continue from afterwards. Otherwise it has failed; restore the previous
1089          capture values before returning NOMATCH. */
1090    
1091          for (;;)
1092            {
1093            md->offset_vector[md->offset_end - number] =
1094              (int)(eptr - md->start_subject);
1095            if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1096            RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
1097              eptrb, RM63);
1098            if (rrc == MATCH_KETRPOS)
1099              {
1100              offset_top = md->end_offset_top;
1101              eptr = md->end_match_ptr;
1102              ecode = md->start_code + code_offset;
1103              save_capture_last = md->capture_last;
1104              matched_once = TRUE;
1105              continue;
1106              }
1107    
1108            /* See comment in the code for capturing groups above about handling
1109            THEN. */
1110    
1111            if (rrc == MATCH_THEN)
1112              {
1113              next = ecode + GET(ecode,1);
1114              if (md->start_match_ptr < next &&
1115                  (*ecode == OP_ALT || *next == OP_ALT))
1116                rrc = MATCH_NOMATCH;
1117              }
1118    
1119            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1120            md->capture_last = save_capture_last;
1121            ecode += GET(ecode, 1);
1122            if (*ecode != OP_ALT) break;
1123          }          }
1124    
1125        /* For non-final alternatives, continue the loop for a NOMATCH result;        if (!matched_once)
1126        otherwise return. */          {
1127            md->offset_vector[offset] = save_offset1;
1128            md->offset_vector[offset+1] = save_offset2;
1129            md->offset_vector[md->offset_end - number] = save_offset3;
1130            }
1131    
1132        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,        if (allow_zero || matched_once)
1133          eptrb, flags, RM2);          {
1134        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);          ecode += 1 + LINK_SIZE;
1135            break;
1136            }
1137    
1138          RRETURN(MATCH_NOMATCH);
1139          }
1140    
1141        /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
1142        as a non-capturing bracket. */
1143    
1144        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1145        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1146    
1147        DPRINTF(("insufficient capture room: treat as non-capturing\n"));
1148    
1149        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1150        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1151    
1152        /* Non-capturing possessive bracket with unlimited repeat. We come here
1153        from BRAZERO with allow_zero = TRUE. The code is similar to the above,
1154        without the capturing complication. It is written out separately for speed
1155        and cleanliness. */
1156    
1157        case OP_BRAPOS:
1158        case OP_SBRAPOS:
1159        allow_zero = FALSE;
1160    
1161        POSSESSIVE_NON_CAPTURE:
1162        matched_once = FALSE;
1163        code_offset = ecode - md->start_code;
1164    
1165        for (;;)
1166          {
1167          if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1168          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
1169            eptrb, RM48);
1170          if (rrc == MATCH_KETRPOS)
1171            {
1172            offset_top = md->end_offset_top;
1173            eptr = md->end_match_ptr;
1174            ecode = md->start_code + code_offset;
1175            matched_once = TRUE;
1176            continue;
1177            }
1178    
1179          /* See comment in the code for capturing groups above about handling
1180          THEN. */
1181    
1182          if (rrc == MATCH_THEN)
1183            {
1184            next = ecode + GET(ecode,1);
1185            if (md->start_match_ptr < next &&
1186                (*ecode == OP_ALT || *next == OP_ALT))
1187              rrc = MATCH_NOMATCH;
1188            }
1189    
1190          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1191        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1192          if (*ecode != OP_ALT) break;
1193        }        }
1194    
1195        if (matched_once || allow_zero)
1196          {
1197          ecode += 1 + LINK_SIZE;
1198          break;
1199          }
1200        RRETURN(MATCH_NOMATCH);
1201    
1202      /* Control never reaches here. */      /* Control never reaches here. */
1203    
1204      /* Conditional group: compilation checked that there are no more than      /* Conditional group: compilation checked that there are no more than
1205      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
1206      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
1207      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. */  
1208    
1209      case OP_COND:      case OP_COND:
1210      case OP_SCOND:      case OP_SCOND:
1211        codelink = GET(ecode, 1);
1212    
1213      /* Because of the way auto-callout works during compile, a callout item is      /* Because of the way auto-callout works during compile, a callout item is
1214      inserted between OP_COND and an assertion condition. */      inserted between OP_COND and an assertion condition. */
1215    
1216      if (ecode[LINK_SIZE+1] == OP_CALLOUT)      if (ecode[LINK_SIZE+1] == OP_CALLOUT)
1217        {        {
1218        if (pcre_callout != NULL)        if (pcre_callout != NULL)
1219          {          {
1220          pcre_callout_block cb;          pcre_callout_block cb;
1221          cb.version          = 1;   /* Version 1 of the callout block */          cb.version          = 2;   /* Version 1 of the callout block */
1222          cb.callout_number   = ecode[LINK_SIZE+2];          cb.callout_number   = ecode[LINK_SIZE+2];
1223          cb.offset_vector    = md->offset_vector;          cb.offset_vector    = md->offset_vector;
1224          cb.subject          = (PCRE_SPTR)md->start_subject;          cb.subject          = (PCRE_SPTR)md->start_subject;
1225          cb.subject_length   = md->end_subject - md->start_subject;          cb.subject_length   = (int)(md->end_subject - md->start_subject);
1226          cb.start_match      = mstart - md->start_subject;          cb.start_match      = (int)(mstart - md->start_subject);
1227          cb.current_position = eptr - md->start_subject;          cb.current_position = (int)(eptr - md->start_subject);
1228          cb.pattern_position = GET(ecode, LINK_SIZE + 3);          cb.pattern_position = GET(ecode, LINK_SIZE + 3);
1229          cb.next_item_length = GET(ecode, 3 + 2*LINK_SIZE);          cb.next_item_length = GET(ecode, 3 + 2*LINK_SIZE);
1230          cb.capture_top      = offset_top/2;          cb.capture_top      = offset_top/2;
1231          cb.capture_last     = md->capture_last;          cb.capture_last     = md->capture_last;
1232          cb.callout_data     = md->callout_data;          cb.callout_data     = md->callout_data;
1233            cb.mark             = md->nomatch_mark;
1234          if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);          if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);
1235          if (rrc < 0) RRETURN(rrc);          if (rrc < 0) RRETURN(rrc);
1236          }          }
1237        ecode += _pcre_OP_lengths[OP_CALLOUT];        ecode += _pcre_OP_lengths[OP_CALLOUT];
1238        }        }
1239    
1240        condcode = ecode[LINK_SIZE+1];
1241    
1242      /* Now see what the actual condition is */      /* Now see what the actual condition is */
1243    
1244      if (ecode[LINK_SIZE+1] == OP_RREF)         /* Recursion test */      if (condcode == OP_RREF || condcode == OP_NRREF)    /* Recursion test */
1245        {        {
1246        offset = GET2(ecode, LINK_SIZE + 2);     /* Recursion group number*/        if (md->recursive == NULL)                /* Not recursing => FALSE */
1247        condition = md->recursive != NULL &&          {
1248          (offset == RREF_ANY || offset == md->recursive->group_num);          condition = FALSE;
1249        ecode += condition? 3 : GET(ecode, 1);          ecode += GET(ecode, 1);
1250            }
1251          else
1252            {
1253            int recno = GET2(ecode, LINK_SIZE + 2);   /* Recursion group number*/
1254            condition = (recno == RREF_ANY || recno == md->recursive->group_num);
1255    
1256            /* If the test is for recursion into a specific subpattern, and it is
1257            false, but the test was set up by name, scan the table to see if the
1258            name refers to any other numbers, and test them. The condition is true
1259            if any one is set. */
1260    
1261            if (!condition && condcode == OP_NRREF)
1262              {
1263              uschar *slotA = md->name_table;
1264              for (i = 0; i < md->name_count; i++)
1265                {
1266                if (GET2(slotA, 0) == recno) break;
1267                slotA += md->name_entry_size;
1268                }
1269    
1270              /* Found a name for the number - there can be only one; duplicate
1271              names for different numbers are allowed, but not vice versa. First
1272              scan down for duplicates. */
1273    
1274              if (i < md->name_count)
1275                {
1276                uschar *slotB = slotA;
1277                while (slotB > md->name_table)
1278                  {
1279                  slotB -= md->name_entry_size;
1280                  if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1281                    {
1282                    condition = GET2(slotB, 0) == md->recursive->group_num;
1283                    if (condition) break;
1284                    }
1285                  else break;
1286                  }
1287    
1288                /* Scan up for duplicates */
1289    
1290                if (!condition)
1291                  {
1292                  slotB = slotA;
1293                  for (i++; i < md->name_count; i++)
1294                    {
1295                    slotB += md->name_entry_size;
1296                    if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1297                      {
1298                      condition = GET2(slotB, 0) == md->recursive->group_num;
1299                      if (condition) break;
1300                      }
1301                    else break;
1302                    }
1303                  }
1304                }
1305              }
1306    
1307            /* Chose branch according to the condition */
1308    
1309            ecode += condition? 3 : GET(ecode, 1);
1310            }
1311        }        }
1312    
1313      else if (ecode[LINK_SIZE+1] == OP_CREF)    /* Group used test */      else if (condcode == OP_CREF || condcode == OP_NCREF)  /* Group used test */
1314        {        {
1315        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */
1316        condition = offset < offset_top && md->offset_vector[offset] >= 0;        condition = offset < offset_top && md->offset_vector[offset] >= 0;
1317    
1318          /* If the numbered capture is unset, but the reference was by name,
1319          scan the table to see if the name refers to any other numbers, and test
1320          them. The condition is true if any one is set. This is tediously similar
1321          to the code above, but not close enough to try to amalgamate. */
1322    
1323          if (!condition && condcode == OP_NCREF)
1324            {
1325            int refno = offset >> 1;
1326            uschar *slotA = md->name_table;
1327    
1328            for (i = 0; i < md->name_count; i++)
1329              {
1330              if (GET2(slotA, 0) == refno) break;
1331              slotA += md->name_entry_size;
1332              }
1333    
1334            /* Found a name for the number - there can be only one; duplicate names
1335            for different numbers are allowed, but not vice versa. First scan down
1336            for duplicates. */
1337    
1338            if (i < md->name_count)
1339              {
1340              uschar *slotB = slotA;
1341              while (slotB > md->name_table)
1342                {
1343                slotB -= md->name_entry_size;
1344                if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1345                  {
1346                  offset = GET2(slotB, 0) << 1;
1347                  condition = offset < offset_top &&
1348                    md->offset_vector[offset] >= 0;
1349                  if (condition) break;
1350                  }
1351                else break;
1352                }
1353    
1354              /* Scan up for duplicates */
1355    
1356              if (!condition)
1357                {
1358                slotB = slotA;
1359                for (i++; i < md->name_count; i++)
1360                  {
1361                  slotB += md->name_entry_size;
1362                  if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
1363                    {
1364                    offset = GET2(slotB, 0) << 1;
1365                    condition = offset < offset_top &&
1366                      md->offset_vector[offset] >= 0;
1367                    if (condition) break;
1368                    }
1369                  else break;
1370                  }
1371                }
1372              }
1373            }
1374    
1375          /* Chose branch according to the condition */
1376    
1377        ecode += condition? 3 : GET(ecode, 1);        ecode += condition? 3 : GET(ecode, 1);
1378        }        }
1379    
1380      else if (ecode[LINK_SIZE+1] == OP_DEF)     /* DEFINE - always false */      else if (condcode == OP_DEF)     /* DEFINE - always false */
1381        {        {
1382        condition = FALSE;        condition = FALSE;
1383        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1384        }        }
1385    
1386      /* The condition is an assertion. Call match() to evaluate it - setting      /* The condition is an assertion. Call match() to evaluate it - setting
1387      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
1388      assertion. */      an assertion. */
1389    
1390      else      else
1391        {        {
1392        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        md->match_function_type = MATCH_CONDASSERT;
1393            match_condassert, RM3);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM3);
1394        if (rrc == MATCH_MATCH)        if (rrc == MATCH_MATCH)
1395          {          {
1396            if (md->end_offset_top > offset_top)
1397              offset_top = md->end_offset_top;  /* Captures may have happened */
1398          condition = TRUE;          condition = TRUE;
1399          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
1400          while (*ecode == OP_ALT) ecode += GET(ecode, 1);          while (*ecode == OP_ALT) ecode += GET(ecode, 1);
1401          }          }
1402    
1403          /* PCRE doesn't allow the effect of (*THEN) to escape beyond an
1404          assertion; it is therefore treated as NOMATCH. */
1405    
1406        else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)        else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
1407          {          {
1408          RRETURN(rrc);         /* Need braces because of following else */          RRETURN(rrc);         /* Need braces because of following else */
# Line 857  for (;;) Line 1410  for (;;)
1410        else        else
1411          {          {
1412          condition = FALSE;          condition = FALSE;
1413          ecode += GET(ecode, 1);          ecode += codelink;
1414          }          }
1415        }        }
1416    
1417      /* 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, can
1418      we can use tail recursion to avoid using another stack frame, except when      use tail recursion to avoid using another stack frame, except when there is
1419      match_cbegroup is required for an unlimited repeat of a possibly empty      unlimited repeat of a possibly empty group. In the latter case, a recursive
1420      group. If the second alternative doesn't exist, we can just plough on. */      call to match() is always required, unless the second alternative doesn't
1421        exist, in which case we can just plough on. Note that, for compatibility
1422        with Perl, the | in a conditional group is NOT treated as creating two
1423        alternatives. If a THEN is encountered in the branch, it propagates out to
1424        the enclosing alternative (unless nested in a deeper set of alternatives,
1425        of course). */
1426    
1427      if (condition || *ecode == OP_ALT)      if (condition || *ecode == OP_ALT)
1428        {        {
1429        ecode += 1 + LINK_SIZE;        if (op != OP_SCOND)
       if (op == OP_SCOND)        /* Possibly empty group */  
         {  
         RMATCH(eptr, ecode, offset_top, md, ims, eptrb, match_cbegroup, RM49);  
         RRETURN(rrc);  
         }  
       else                       /* Group must match something */  
1430          {          {
1431          flags = 0;          ecode += 1 + LINK_SIZE;
1432          goto TAIL_RECURSE;          goto TAIL_RECURSE;
1433          }          }
1434    
1435          md->match_function_type = MATCH_CBEGROUP;
1436          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM49);
1437          RRETURN(rrc);
1438        }        }
1439      else                         /* Condition false & no 2nd alternative */  
1440         /* Condition false & no alternative; continue after the group. */
1441    
1442        else
1443        {        {
1444        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1445        }        }
1446      break;      break;
1447    
1448    
1449      /* 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,
1450      recursion, we should restore the offsets appropriately and continue from      to close any currently open capturing brackets. */
     after the call. */  
1451    
1452      case OP_ACCEPT:      case OP_CLOSE:
1453      case OP_END:      number = GET2(ecode, 1);
1454      if (md->recursive != NULL && md->recursive->group_num == 0)      offset = number << 1;
1455    
1456    #ifdef PCRE_DEBUG
1457          printf("end bracket %d at *ACCEPT", number);
1458          printf("\n");
1459    #endif
1460    
1461        md->capture_last = number;
1462        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1463        {        {
1464        recursion_info *rec = md->recursive;        md->offset_vector[offset] =
1465        DPRINTF(("End of pattern in a (?0) recursion\n"));          md->offset_vector[md->offset_end - number];
1466        md->recursive = rec->prevrec;        md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1467        memmove(md->offset_vector, rec->offset_save,        if (offset_top <= offset) offset_top = offset + 2;
         rec->saved_max * sizeof(int));  
       mstart = rec->save_start;  
       ims = original_ims;  
       ecode = rec->after_call;  
       break;  
1468        }        }
1469        ecode += 3;
1470        break;
1471    
     /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty  
     string - backtracking will then try other alternatives, if any. */  
1472    
1473      if (md->notempty && eptr == mstart) RRETURN(MATCH_NOMATCH);      /* End of the pattern, either real or forced. */
1474    
1475        case OP_END:
1476        case OP_ACCEPT:
1477        case OP_ASSERT_ACCEPT:
1478    
1479        /* If we have matched an empty string, fail if not in an assertion and not
1480        in a recursion if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART
1481        is set and we have matched at the start of the subject. In both cases,
1482        backtracking will then try other alternatives, if any. */
1483    
1484        if (eptr == mstart && op != OP_ASSERT_ACCEPT &&
1485             md->recursive == NULL &&
1486             (md->notempty ||
1487               (md->notempty_atstart &&
1488                 mstart == md->start_subject + md->start_offset)))
1489          RRETURN(MATCH_NOMATCH);
1490    
1491        /* Otherwise, we have a match. */
1492    
1493      md->end_match_ptr = eptr;           /* Record where we ended */      md->end_match_ptr = eptr;           /* Record where we ended */
1494      md->end_offset_top = offset_top;    /* and how many extracts were taken */      md->end_offset_top = offset_top;    /* and how many extracts were taken */
1495      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);  
1496    
1497      /* Change option settings */      /* For some reason, the macros don't work properly if an expression is
1498        given as the argument to RRETURN when the heap is in use. */
1499    
1500      case OP_OPT:      rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
1501      ims = ecode[1];      RRETURN(rrc);
     ecode += 2;  
     DPRINTF(("ims set to %02lx\n", ims));  
     break;  
1502    
1503      /* Assertion brackets. Check the alternative branches in turn - the      /* Assertion brackets. Check the alternative branches in turn - the
1504      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,
1505      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
1506      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
1507      this level is identical to the lookahead case. */      this level is identical to the lookahead case. When the assertion is part
1508        of a condition, we want to return immediately afterwards. The caller of
1509        this incarnation of the match() function will have set MATCH_CONDASSERT in
1510        md->match_function type, and one of these opcodes will be the first opcode
1511        that is processed. We use a local variable that is preserved over calls to
1512        match() to remember this case. */
1513    
1514      case OP_ASSERT:      case OP_ASSERT:
1515      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1516        if (md->match_function_type == MATCH_CONDASSERT)
1517          {
1518          condassert = TRUE;
1519          md->match_function_type = 0;
1520          }
1521        else condassert = FALSE;
1522    
1523      do      do
1524        {        {
1525        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
1526          RM4);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1527        if (rrc == MATCH_MATCH) break;          {
1528            mstart = md->start_match_ptr;   /* In case \K reset it */
1529            break;
1530            }
1531    
1532          /* PCRE does not allow THEN to escape beyond an assertion; it is treated
1533          as NOMATCH. */
1534    
1535        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
1536        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1537        }        }
1538      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1539    
1540      if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);      if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);
1541    
1542      /* If checking an assertion for a condition, return MATCH_MATCH. */      /* If checking an assertion for a condition, return MATCH_MATCH. */
1543    
1544      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);
1545    
1546      /* Continue from after the assertion, updating the offsets high water      /* Continue from after the assertion, updating the offsets high water
1547      mark, since extracts may have been taken during the assertion. */      mark, since extracts may have been taken during the assertion. */
# Line 954  for (;;) Line 1551  for (;;)
1551      offset_top = md->end_offset_top;      offset_top = md->end_offset_top;
1552      continue;      continue;
1553    
1554      /* Negative assertion: all branches must fail to match */      /* Negative assertion: all branches must fail to match. Encountering SKIP,
1555        PRUNE, or COMMIT means we must assume failure without checking subsequent
1556        branches. */
1557    
1558      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1559      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1560        if (md->match_function_type == MATCH_CONDASSERT)
1561          {
1562          condassert = TRUE;
1563          md->match_function_type = 0;
1564          }
1565        else condassert = FALSE;
1566    
1567      do      do
1568        {        {
1569        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
1570          RM5);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) RRETURN(MATCH_NOMATCH);
1571        if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);        if (rrc == MATCH_SKIP || rrc == MATCH_PRUNE || rrc == MATCH_COMMIT)
1572            {
1573            do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1574            break;
1575            }
1576    
1577          /* PCRE does not allow THEN to escape beyond an assertion; it is treated
1578          as NOMATCH. */
1579    
1580        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
1581        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1582        }        }
1583      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1584    
1585      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);  /* Condition assertion */
1586    
1587      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1588      continue;      continue;
# Line 1000  for (;;) Line 1614  for (;;)
1614        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1615        }        }
1616    
1617      /* Skip to next op code */      /* Save the earliest consulted character, then skip to next op code */
1618    
1619        if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
1620      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1621      break;      break;
1622    
# Line 1013  for (;;) Line 1628  for (;;)
1628      if (pcre_callout != NULL)      if (pcre_callout != NULL)
1629        {        {
1630        pcre_callout_block cb;        pcre_callout_block cb;
1631        cb.version          = 1;   /* Version 1 of the callout block */        cb.version          = 2;   /* Version 1 of the callout block */
1632        cb.callout_number   = ecode[1];        cb.callout_number   = ecode[1];
1633        cb.offset_vector    = md->offset_vector;        cb.offset_vector    = md->offset_vector;
1634        cb.subject          = (PCRE_SPTR)md->start_subject;        cb.subject          = (PCRE_SPTR)md->start_subject;
1635        cb.subject_length   = md->end_subject - md->start_subject;        cb.subject_length   = (int)(md->end_subject - md->start_subject);
1636        cb.start_match      = mstart - md->start_subject;        cb.start_match      = (int)(mstart - md->start_subject);
1637        cb.current_position = eptr - md->start_subject;        cb.current_position = (int)(eptr - md->start_subject);
1638        cb.pattern_position = GET(ecode, 2);        cb.pattern_position = GET(ecode, 2);
1639        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
1640        cb.capture_top      = offset_top/2;        cb.capture_top      = offset_top/2;
1641        cb.capture_last     = md->capture_last;        cb.capture_last     = md->capture_last;
1642        cb.callout_data     = md->callout_data;        cb.callout_data     = md->callout_data;
1643          cb.mark             = md->nomatch_mark;
1644        if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);        if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);
1645        if (rrc < 0) RRETURN(rrc);        if (rrc < 0) RRETURN(rrc);
1646        }        }
# Line 1035  for (;;) Line 1651  for (;;)
1651      offset data is the offset to the starting bracket from the start of the      offset data is the offset to the starting bracket from the start of the
1652      whole pattern. (This is so that it works from duplicated subpatterns.)      whole pattern. (This is so that it works from duplicated subpatterns.)
1653    
1654      If there are any capturing brackets started but not finished, we have to      The state of the capturing groups is preserved over recursion, and
1655      save their starting points and reinstate them after the recursion. However,      re-instated afterwards. We don't know how many are started and not yet
1656      we don't know how many such there are (offset_top records the completed      finished (offset_top records the completed total) so we just have to save
1657      total) so we just have to save all the potential data. There may be up to      all the potential data. There may be up to 65535 such values, which is too
1658      65535 such values, which is too large to put on the stack, but using malloc      large to put on the stack, but using malloc for small numbers seems
1659      for small numbers seems expensive. As a compromise, the stack is used when      expensive. As a compromise, the stack is used when there are no more than
1660      there are no more than REC_STACK_SAVE_MAX values to store; otherwise malloc      REC_STACK_SAVE_MAX values to store; otherwise malloc is used.
     is used. A problem is what to do if the malloc fails ... there is no way of  
     returning to the top level with an error. Save the top REC_STACK_SAVE_MAX  
     values on the stack, and accept that the rest may be wrong.  
1661    
1662      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
1663      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
1664      for the original version of this logic. */      for the original version of this logic. It has, however, been hacked around
1665        a lot, so he is not to blame for the current way it works. */
1666    
1667      case OP_RECURSE:      case OP_RECURSE:
1668        {        {
1669          recursion_info *ri;
1670          int recno;
1671    
1672        callpat = md->start_code + GET(ecode, 1);        callpat = md->start_code + GET(ecode, 1);
1673        new_recursive.group_num = (callpat == md->start_code)? 0 :        recno = (callpat == md->start_code)? 0 :
1674          GET2(callpat, 1 + LINK_SIZE);          GET2(callpat, 1 + LINK_SIZE);
1675    
1676          /* Check for repeating a recursion without advancing the subject pointer.
1677          This should catch convoluted mutual recursions. (Some simple cases are
1678          caught at compile time.) */
1679    
1680          for (ri = md->recursive; ri != NULL; ri = ri->prevrec)
1681            if (recno == ri->group_num && eptr == ri->subject_position)
1682              RRETURN(PCRE_ERROR_RECURSELOOP);
1683    
1684        /* Add to "recursing stack" */        /* Add to "recursing stack" */
1685    
1686          new_recursive.group_num = recno;
1687          new_recursive.subject_position = eptr;
1688        new_recursive.prevrec = md->recursive;        new_recursive.prevrec = md->recursive;
1689        md->recursive = &new_recursive;        md->recursive = &new_recursive;
1690    
1691        /* Find where to continue from afterwards */        /* Where to continue from afterwards */
1692    
1693        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       new_recursive.after_call = ecode;  
1694    
1695        /* Now save the offset data. */        /* Now save the offset data */
1696    
1697        new_recursive.saved_max = md->offset_end;        new_recursive.saved_max = md->offset_end;
1698        if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)        if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)
# Line 1077  for (;;) Line 1703  for (;;)
1703            (int *)(pcre_malloc)(new_recursive.saved_max * sizeof(int));            (int *)(pcre_malloc)(new_recursive.saved_max * sizeof(int));
1704          if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);          if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
1705          }          }
   
1706        memcpy(new_recursive.offset_save, md->offset_vector,        memcpy(new_recursive.offset_save, md->offset_vector,
1707              new_recursive.saved_max * sizeof(int));              new_recursive.saved_max * sizeof(int));
       new_recursive.save_start = mstart;  
       mstart = eptr;  
1708    
1709        /* OK, now we can do the recursion. For each top-level alternative we        /* OK, now we can do the recursion. After processing each alternative,
1710        restore the offset and recursion data. */        restore the offset data. If there were nested recursions, md->recursive
1711          might be changed, so reset it before looping. */
1712    
1713        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1714        flags = (*callpat >= OP_SBRA)? match_cbegroup : 0;        cbegroup = (*callpat >= OP_SBRA);
1715        do        do
1716          {          {
1717            if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
1718          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,
1719            md, ims, eptrb, flags, RM6);            md, eptrb, RM6);
1720          if (rrc == MATCH_MATCH)          memcpy(md->offset_vector, new_recursive.offset_save,
1721                new_recursive.saved_max * sizeof(int));
1722            md->recursive = new_recursive.prevrec;
1723            if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1724            {            {
1725            DPRINTF(("Recursion matched\n"));            DPRINTF(("Recursion matched\n"));
           md->recursive = new_recursive.prevrec;  
1726            if (new_recursive.offset_save != stacksave)            if (new_recursive.offset_save != stacksave)
1727              (pcre_free)(new_recursive.offset_save);              (pcre_free)(new_recursive.offset_save);
1728            RRETURN(MATCH_MATCH);  
1729              /* Set where we got to in the subject, and reset the start in case
1730              it was changed by \K. This *is* propagated back out of a recursion,
1731              for Perl compatibility. */
1732    
1733              eptr = md->end_match_ptr;
1734              mstart = md->start_match_ptr;
1735              goto RECURSION_MATCHED;        /* Exit loop; end processing */
1736            }            }
1737    
1738            /* PCRE does not allow THEN to escape beyond a recursion; it is treated
1739            as NOMATCH. */
1740    
1741          else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)          else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
1742            {            {
1743            DPRINTF(("Recursion gave error %d\n", rrc));            DPRINTF(("Recursion gave error %d\n", rrc));
1744              if (new_recursive.offset_save != stacksave)
1745                (pcre_free)(new_recursive.offset_save);
1746            RRETURN(rrc);            RRETURN(rrc);
1747            }            }
1748    
1749          md->recursive = &new_recursive;          md->recursive = &new_recursive;
         memcpy(md->offset_vector, new_recursive.offset_save,  
             new_recursive.saved_max * sizeof(int));  
1750          callpat += GET(callpat, 1);          callpat += GET(callpat, 1);
1751          }          }
1752        while (*callpat == OP_ALT);        while (*callpat == OP_ALT);
# Line 1119  for (;;) Line 1757  for (;;)
1757          (pcre_free)(new_recursive.offset_save);          (pcre_free)(new_recursive.offset_save);
1758        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1759        }        }
     /* Control never reaches here */  
   
     /* "Once" brackets are like assertion brackets except that after a match,  
     the point in the subject string is not moved back. Thus there can never be  
     a move back into the brackets. Friedl calls these "atomic" subpatterns.  
     Check the alternative branches in turn - the matching won't pass the KET  
     for this kind of subpattern. If any one branch matches, we carry on as at  
     the end of a normal bracket, leaving the subject pointer. */  
   
     case OP_ONCE:  
     prev = ecode;  
     saved_eptr = eptr;  
   
     do  
       {  
       RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM7);  
       if (rrc == MATCH_MATCH) break;  
       if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);  
       ecode += GET(ecode,1);  
       }  
     while (*ecode == OP_ALT);  
   
     /* If hit the end of the group (which could be repeated), fail */  
   
     if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);  
   
     /* Continue as from after the assertion, updating the offsets high water  
     mark, since extracts may have been taken. */  
   
     do ecode += GET(ecode, 1); while (*ecode == OP_ALT);  
   
     offset_top = md->end_offset_top;  
     eptr = md->end_match_ptr;  
   
     /* For a non-repeating ket, just continue at this level. This also  
     happens for a repeating ket if no characters were matched in the group.  
     This is the forcible breaking of infinite loops as implemented in Perl  
     5.005. If there is an options reset, it will get obeyed in the normal  
     course of events. */  
   
     if (*ecode == OP_KET || eptr == saved_eptr)  
       {  
       ecode += 1+LINK_SIZE;  
       break;  
       }  
1760    
1761      /* The repeating kets try the rest of the pattern or restart from the      RECURSION_MATCHED:
1762      preceding bracket, in the appropriate order. The second "call" of match()      break;
     uses tail recursion, to avoid using another stack frame. We need to reset  
     any options that changed within the bracket before re-running it, so  
     check the next opcode. */  
   
     if (ecode[1+LINK_SIZE] == OP_OPT)  
       {  
       ims = (ims & ~PCRE_IMS) | ecode[4];  
       DPRINTF(("ims set to %02lx at group repeat\n", ims));  
       }  
   
     if (*ecode == OP_KETRMIN)  
       {  
       RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM8);  
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       ecode = prev;  
       flags = 0;  
       goto TAIL_RECURSE;  
       }  
     else  /* OP_KETRMAX */  
       {  
       RMATCH(eptr, prev, offset_top, md, ims, eptrb, match_cbegroup, RM9);  
       if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
       ecode += 1 + LINK_SIZE;  
       flags = 0;  
       goto TAIL_RECURSE;  
       }  
     /* Control never gets here */  
1763    
1764      /* 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
1765      bracketed group and go to there. */      bracketed group and go to there. */
# Line 1209  for (;;) Line 1775  for (;;)
1775      optional ones preceded by BRAZERO or BRAMINZERO. */      optional ones preceded by BRAZERO or BRAMINZERO. */
1776    
1777      case OP_BRAZERO:      case OP_BRAZERO:
1778        {      next = ecode + 1;
1779        next = ecode+1;      RMATCH(eptr, next, offset_top, md, eptrb, RM10);
1780        RMATCH(eptr, next, offset_top, md, ims, eptrb, 0, RM10);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1781        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      do next += GET(next, 1); while (*next == OP_ALT);
1782        do next += GET(next,1); while (*next == OP_ALT);      ecode = next + 1 + LINK_SIZE;
       ecode = next + 1 + LINK_SIZE;  
       }  
1783      break;      break;
1784    
1785      case OP_BRAMINZERO:      case OP_BRAMINZERO:
1786        {      next = ecode + 1;
1787        next = ecode+1;      do next += GET(next, 1); while (*next == OP_ALT);
1788        do next += GET(next, 1); while (*next == OP_ALT);      RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
1789        RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0, RM11);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1790        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      ecode++;
       ecode++;  
       }  
1791      break;      break;
1792    
1793      case OP_SKIPZERO:      case OP_SKIPZERO:
1794        {      next = ecode+1;
1795        next = ecode+1;      do next += GET(next,1); while (*next == OP_ALT);
1796        do next += GET(next,1); while (*next == OP_ALT);      ecode = next + 1 + LINK_SIZE;
       ecode = next + 1 + LINK_SIZE;  
       }  
1797      break;      break;
1798    
1799        /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
1800        here; just jump to the group, with allow_zero set TRUE. */
1801    
1802        case OP_BRAPOSZERO:
1803        op = *(++ecode);
1804        allow_zero = TRUE;
1805        if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
1806          goto POSSESSIVE_NON_CAPTURE;
1807    
1808      /* End of a group, repeated or non-repeating. */      /* End of a group, repeated or non-repeating. */
1809    
1810      case OP_KET:      case OP_KET:
1811      case OP_KETRMIN:      case OP_KETRMIN:
1812      case OP_KETRMAX:      case OP_KETRMAX:
1813        case OP_KETRPOS:
1814      prev = ecode - GET(ecode, 1);      prev = ecode - GET(ecode, 1);
1815    
1816      /* 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
1817      infinite repeats of empty string matches, retrieve the subject start from      infinite repeats of empty string matches, retrieve the subject start from
1818      the chain. Otherwise, set it NULL. */      the chain. Otherwise, set it NULL. */
1819    
1820      if (*prev >= OP_SBRA)      if (*prev >= OP_SBRA || *prev == OP_ONCE)
1821        {        {
1822        saved_eptr = eptrb->epb_saved_eptr;   /* Value at start of group */        saved_eptr = eptrb->epb_saved_eptr;   /* Value at start of group */
1823        eptrb = eptrb->epb_prev;              /* Backup to previous group */        eptrb = eptrb->epb_prev;              /* Backup to previous group */
1824        }        }
1825      else saved_eptr = NULL;      else saved_eptr = NULL;
1826    
1827      /* 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 a non-capturing atomic
1828      MATCH_MATCH, but record the current high water mark for use by positive      group, stop matching and return MATCH_MATCH, but record the current high
1829      assertions. Do this also for the "once" (atomic) groups. */      water mark for use by positive assertions. We also need to record the match
1830        start in case it was changed by \K. */
1831      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||  
1832          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||      if ((*prev >= OP_ASSERT && *prev <= OP_ASSERTBACK_NOT) ||
1833          *prev == OP_ONCE)           *prev == OP_ONCE_NC)
1834        {        {
1835        md->end_match_ptr = eptr;      /* For ONCE */        md->end_match_ptr = eptr;      /* For ONCE_NC */
1836        md->end_offset_top = offset_top;        md->end_offset_top = offset_top;
1837        RRETURN(MATCH_MATCH);        md->start_match_ptr = mstart;
1838          RRETURN(MATCH_MATCH);         /* Sets md->mark */
1839        }        }
1840    
1841      /* 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
1842      and if necessary complete handling an extraction by setting the offsets and      and if necessary complete handling an extraction by setting the offsets and
1843      bumping the high water mark. Note that whole-pattern recursion is coded as      bumping the high water mark. Whole-pattern recursion is coded as a recurse
1844      a recurse into group 0, so it won't be picked up here. Instead, we catch it      into group 0, so it won't be picked up here. Instead, we catch it when the
1845      when the OP_END is reached. Other recursion is handled here. */      OP_END is reached. Other recursion is handled here. We just have to record
1846        the current subject position and start match pointer and give a MATCH
1847        return. */
1848    
1849      if (*prev == OP_CBRA || *prev == OP_SCBRA)      if (*prev == OP_CBRA || *prev == OP_SCBRA ||
1850            *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
1851        {        {
1852        number = GET2(prev, 1+LINK_SIZE);        number = GET2(prev, 1+LINK_SIZE);
1853        offset = number << 1;        offset = number << 1;
1854    
1855  #ifdef DEBUG  #ifdef PCRE_DEBUG
1856        printf("end bracket %d", number);        printf("end bracket %d", number);
1857        printf("\n");        printf("\n");
1858  #endif  #endif
1859    
1860          /* Handle a recursively called group. */
1861    
1862          if (md->recursive != NULL && md->recursive->group_num == number)
1863            {
1864            md->end_match_ptr = eptr;
1865            md->start_match_ptr = mstart;
1866            RRETURN(MATCH_MATCH);
1867            }
1868    
1869          /* Deal with capturing */
1870    
1871        md->capture_last = number;        md->capture_last = number;
1872        if (offset >= md->offset_max) md->offset_overflow = TRUE; else        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1873          {          {
1874            /* If offset is greater than offset_top, it means that we are
1875            "skipping" a capturing group, and that group's offsets must be marked
1876            unset. In earlier versions of PCRE, all the offsets were unset at the
1877            start of matching, but this doesn't work because atomic groups and
1878            assertions can cause a value to be set that should later be unset.
1879            Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as
1880            part of the atomic group, but this is not on the final matching path,
1881            so must be unset when 2 is set. (If there is no group 2, there is no
1882            problem, because offset_top will then be 2, indicating no capture.) */
1883    
1884            if (offset > offset_top)
1885              {
1886              register int *iptr = md->offset_vector + offset_top;
1887              register int *iend = md->offset_vector + offset;
1888              while (iptr < iend) *iptr++ = -1;
1889              }
1890    
1891            /* Now make the extraction */
1892    
1893          md->offset_vector[offset] =          md->offset_vector[offset] =
1894            md->offset_vector[md->offset_end - number];            md->offset_vector[md->offset_end - number];
1895          md->offset_vector[offset+1] = eptr - md->start_subject;          md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1896          if (offset_top <= offset) offset_top = offset + 2;          if (offset_top <= offset) offset_top = offset + 2;
1897          }          }
1898          }
1899    
1900        /* Handle a recursively called group. Restore the offsets      /* For an ordinary non-repeating ket, just continue at this level. This
1901        appropriately and continue from after the call. */      also happens for a repeating ket if no characters were matched in the
1902        group. This is the forcible breaking of infinite loops as implemented in
1903        Perl 5.005. For a non-repeating atomic group that includes captures,
1904        establish a backup point by processing the rest of the pattern at a lower
1905        level. If this results in a NOMATCH return, pass MATCH_ONCE back to the
1906        original OP_ONCE level, thereby bypassing intermediate backup points, but
1907        resetting any captures that happened along the way. */
1908    
1909        if (md->recursive != NULL && md->recursive->group_num == number)      if (*ecode == OP_KET || eptr == saved_eptr)
1910          {
1911          if (*prev == OP_ONCE)
1912          {          {
1913          recursion_info *rec = md->recursive;          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
1914          DPRINTF(("Recursion (%d) succeeded - continuing\n", number));          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1915          md->recursive = rec->prevrec;          md->once_target = prev;  /* Level at which to change to MATCH_NOMATCH */
1916          mstart = rec->save_start;          RRETURN(MATCH_ONCE);
         memcpy(md->offset_vector, rec->offset_save,  
           rec->saved_max * sizeof(int));  
         ecode = rec->after_call;  
         ims = original_ims;  
         break;  
1917          }          }
1918          ecode += 1 + LINK_SIZE;    /* Carry on at this level */
1919          break;
1920        }        }
1921    
1922      /* For both capturing and non-capturing groups, reset the value of the ims      /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
1923      flags, in case they got changed during the group. */      and return the MATCH_KETRPOS. This makes it possible to do the repeats one
1924        at a time from the outer level, thus saving stack. */
     ims = original_ims;  
     DPRINTF(("ims reset to %02lx\n", ims));  
   
     /* For a non-repeating ket, just continue at this level. This also  
     happens for a repeating ket if no characters were matched in the group.  
     This is the forcible breaking of infinite loops as implemented in Perl  
     5.005. If there is an options reset, it will get obeyed in the normal  
     course of events. */  
1925    
1926      if (*ecode == OP_KET || eptr == saved_eptr)      if (*ecode == OP_KETRPOS)
1927        {        {
1928        ecode += 1 + LINK_SIZE;        md->end_match_ptr = eptr;
1929        break;        md->end_offset_top = offset_top;
1930          RRETURN(MATCH_KETRPOS);
1931        }        }
1932    
1933      /* The repeating kets try the rest of the pattern or restart from the      /* The normal repeating kets try the rest of the pattern or restart from
1934      preceding bracket, in the appropriate order. In the second case, we can use      the preceding bracket, in the appropriate order. In the second case, we can
1935      tail recursion to avoid using another stack frame, unless we have an      use tail recursion to avoid using another stack frame, unless we have an
1936      unlimited repeat of a group that can match an empty string. */      an atomic group or an unlimited repeat of a group that can match an empty
1937        string. */
     flags = (*prev >= OP_SBRA)? match_cbegroup : 0;  
1938    
1939      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1940        {        {
1941        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM12);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
1942        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1943        if (flags != 0)    /* Could match an empty string */        if (*prev == OP_ONCE)
1944            {
1945            RMATCH(eptr, prev, offset_top, md, eptrb, RM8);
1946            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1947            md->once_target = prev;  /* Level at which to change to MATCH_NOMATCH */
1948            RRETURN(MATCH_ONCE);
1949            }
1950          if (*prev >= OP_SBRA)    /* Could match an empty string */
1951          {          {
1952          RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM50);          md->match_function_type = MATCH_CBEGROUP;
1953            RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
1954          RRETURN(rrc);          RRETURN(rrc);
1955          }          }
1956        ecode = prev;        ecode = prev;
# Line 1348  for (;;) Line 1958  for (;;)
1958        }        }
1959      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1960        {        {
1961        RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM13);        if (*prev >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1962          RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
1963          if (rrc == MATCH_ONCE && md->once_target == prev) rrc = MATCH_NOMATCH;
1964        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1965          if (*prev == OP_ONCE)
1966            {
1967            RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM9);
1968            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1969            md->once_target = prev;
1970            RRETURN(MATCH_ONCE);
1971            }
1972        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = 0;  
1973        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1974        }        }
1975      /* Control never gets here */      /* Control never gets here */
1976    
1977      /* Start of subject unless notbol, or after internal newline if multiline */      /* Not multiline mode: start of subject assertion, unless notbol. */
1978    
1979      case OP_CIRC:      case OP_CIRC:
1980      if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);      if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
     if ((ims & PCRE_MULTILINE) != 0)  
       {  
       if (eptr != md->start_subject &&  
           (eptr == md->end_subject || !WAS_NEWLINE(eptr)))  
         RRETURN(MATCH_NOMATCH);  
       ecode++;  
       break;  
       }  
     /* ... else fall through */  
1981    
1982      /* Start of subject assertion */      /* Start of subject assertion */
1983    
# Line 1377  for (;;) Line 1986  for (;;)
1986      ecode++;      ecode++;
1987      break;      break;
1988    
1989        /* Multiline mode: start of subject unless notbol, or after any newline. */
1990    
1991        case OP_CIRCM:
1992        if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
1993        if (eptr != md->start_subject &&
1994            (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
1995          RRETURN(MATCH_NOMATCH);
1996        ecode++;
1997        break;
1998    
1999      /* Start of match assertion */      /* Start of match assertion */
2000    
2001      case OP_SOM:      case OP_SOM:
# Line 1391  for (;;) Line 2010  for (;;)
2010      ecode++;      ecode++;
2011      break;      break;
2012    
2013      /* Assert before internal newline if multiline, or before a terminating      /* Multiline mode: assert before any newline, or before end of subject
2014      newline unless endonly is set, else end of subject unless noteol is set. */      unless noteol is set. */
2015    
2016      case OP_DOLL:      case OP_DOLLM:
2017      if ((ims & PCRE_MULTILINE) != 0)      if (eptr < md->end_subject)
2018        {        { if (!IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); }
       if (eptr < md->end_subject)  
         { if (!IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); }  
       else  
         { if (md->noteol) RRETURN(MATCH_NOMATCH); }  
       ecode++;  
       break;  
       }  
2019      else      else
2020        {        {
2021        if (md->noteol) RRETURN(MATCH_NOMATCH);        if (md->noteol) RRETURN(MATCH_NOMATCH);
2022        if (!md->endonly)        SCHECK_PARTIAL();
         {  
         if (eptr != md->end_subject &&  
             (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))  
           RRETURN(MATCH_NOMATCH);  
         ecode++;  
         break;  
         }  
2023        }        }
2024        ecode++;
2025        break;
2026    
2027        /* Not multiline mode: assert before a terminating newline or before end of
2028        subject unless noteol is set. */
2029    
2030        case OP_DOLL:
2031        if (md->noteol) RRETURN(MATCH_NOMATCH);
2032        if (!md->endonly) goto ASSERT_NL_OR_EOS;
2033    
2034      /* ... else fall through for endonly */      /* ... else fall through for endonly */
2035    
2036      /* End of subject assertion (\z) */      /* End of subject assertion (\z) */
2037    
2038      case OP_EOD:      case OP_EOD:
2039      if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);
2040        SCHECK_PARTIAL();
2041      ecode++;      ecode++;
2042      break;      break;
2043    
2044      /* End of subject or ending \n assertion (\Z) */      /* End of subject or ending \n assertion (\Z) */
2045    
2046      case OP_EODN:      case OP_EODN:
2047      if (eptr != md->end_subject &&      ASSERT_NL_OR_EOS:
2048        if (eptr < md->end_subject &&
2049          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
2050        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
2051    
2052        /* Either at end of string or \n before end. */
2053    
2054        SCHECK_PARTIAL();
2055      ecode++;      ecode++;
2056      break;      break;
2057    
# Line 1442  for (;;) Line 2063  for (;;)
2063    
2064        /* Find out if the previous and current characters are "word" characters.        /* Find out if the previous and current characters are "word" characters.
2065        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
2066        be "non-word" characters. */        be "non-word" characters. Remember the earliest consulted character for
2067          partial matching. */
2068    
2069  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2070        if (utf8)        if (utf8)
2071          {          {
2072            /* Get status of previous character */
2073    
2074          if (eptr == md->start_subject) prev_is_word = FALSE; else          if (eptr == md->start_subject) prev_is_word = FALSE; else
2075            {            {
2076            const uschar *lastptr = eptr - 1;            USPTR lastptr = eptr - 1;
2077            while((*lastptr & 0xc0) == 0x80) lastptr--;            while((*lastptr & 0xc0) == 0x80) lastptr--;
2078              if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
2079            GETCHAR(c, lastptr);            GETCHAR(c, lastptr);
2080    #ifdef SUPPORT_UCP
2081              if (md->use_ucp)
2082                {
2083                if (c == '_') prev_is_word = TRUE; else
2084                  {
2085                  int cat = UCD_CATEGORY(c);
2086                  prev_is_word = (cat == ucp_L || cat == ucp_N);
2087                  }
2088                }
2089              else
2090    #endif
2091            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
2092            }            }
2093          if (eptr >= md->end_subject) cur_is_word = FALSE; else  
2094            /* Get status of next character */
2095    
2096            if (eptr >= md->end_subject)
2097              {
2098              SCHECK_PARTIAL();
2099              cur_is_word = FALSE;
2100              }
2101            else
2102            {            {
2103            GETCHAR(c, eptr);            GETCHAR(c, eptr);
2104    #ifdef SUPPORT_UCP
2105              if (md->use_ucp)
2106                {
2107                if (c == '_') cur_is_word = TRUE; else
2108                  {
2109                  int cat = UCD_CATEGORY(c);
2110                  cur_is_word = (cat == ucp_L || cat == ucp_N);
2111                  }
2112                }
2113              else
2114    #endif
2115            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
2116            }            }
2117          }          }
2118        else        else
2119  #endif  #endif
2120    
2121        /* More streamlined when not in UTF-8 mode */        /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for
2122          consistency with the behaviour of \w we do use it in this case. */
2123    
2124          {          {
2125          prev_is_word = (eptr != md->start_subject) &&          /* Get status of previous character */
2126            ((md->ctypes[eptr[-1]] & ctype_word) != 0);  
2127          cur_is_word = (eptr < md->end_subject) &&          if (eptr == md->start_subject) prev_is_word = FALSE; else
2128            ((md->ctypes[*eptr] & ctype_word) != 0);            {
2129              if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
2130    #ifdef SUPPORT_UCP
2131              if (md->use_ucp)
2132                {
2133                c = eptr[-1];
2134                if (c == '_') prev_is_word = TRUE; else
2135                  {
2136                  int cat = UCD_CATEGORY(c);
2137                  prev_is_word = (cat == ucp_L || cat == ucp_N);
2138                  }
2139                }
2140              else
2141    #endif
2142              prev_is_word = ((md->ctypes[eptr[-1]] & ctype_word) != 0);
2143              }
2144    
2145            /* Get status of next character */
2146    
2147            if (eptr >= md->end_subject)
2148              {
2149              SCHECK_PARTIAL();
2150              cur_is_word = FALSE;
2151              }
2152            else
2153    #ifdef SUPPORT_UCP
2154            if (md->use_ucp)
2155              {
2156              c = *eptr;
2157              if (c == '_') cur_is_word = TRUE; else
2158                {
2159                int cat = UCD_CATEGORY(c);
2160                cur_is_word = (cat == ucp_L || cat == ucp_N);
2161                }
2162              }
2163            else
2164    #endif
2165            cur_is_word = ((md->ctypes[*eptr] & ctype_word) != 0);
2166          }          }
2167    
2168        /* Now see if the situation is what we want */        /* Now see if the situation is what we want */
# Line 1487  for (;;) Line 2180  for (;;)
2180      /* Fall through */      /* Fall through */
2181    
2182      case OP_ALLANY:      case OP_ALLANY:
2183      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)   /* DO NOT merge the eptr++ here; it must */
2184          {                            /* not be updated before SCHECK_PARTIAL. */
2185          SCHECK_PARTIAL();
2186          RRETURN(MATCH_NOMATCH);
2187          }
2188        eptr++;
2189      if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;      if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
2190      ecode++;      ecode++;
2191      break;      break;
# Line 1496  for (;;) Line 2194  for (;;)
2194      any byte, even newline, independent of the setting of PCRE_DOTALL. */      any byte, even newline, independent of the setting of PCRE_DOTALL. */
2195    
2196      case OP_ANYBYTE:      case OP_ANYBYTE:
2197      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)   /* DO NOT merge the eptr++ here; it must */
2198          {                            /* not be updated before SCHECK_PARTIAL. */
2199          SCHECK_PARTIAL();
2200          RRETURN(MATCH_NOMATCH);
2201          }
2202        eptr++;
2203      ecode++;      ecode++;
2204      break;      break;
2205    
2206      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2207      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2208          {
2209          SCHECK_PARTIAL();
2210          RRETURN(MATCH_NOMATCH);
2211          }
2212      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2213      if (      if (
2214  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1514  for (;;) Line 2221  for (;;)
2221      break;      break;
2222    
2223      case OP_DIGIT:      case OP_DIGIT:
2224      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2225          {
2226          SCHECK_PARTIAL();
2227          RRETURN(MATCH_NOMATCH);
2228          }
2229      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2230      if (      if (
2231  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1527  for (;;) Line 2238  for (;;)
2238      break;      break;
2239    
2240      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2241      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2242          {
2243          SCHECK_PARTIAL();
2244          RRETURN(MATCH_NOMATCH);
2245          }
2246      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2247      if (      if (
2248  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1540  for (;;) Line 2255  for (;;)
2255      break;      break;
2256    
2257      case OP_WHITESPACE:      case OP_WHITESPACE:
2258      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2259          {
2260          SCHECK_PARTIAL();
2261          RRETURN(MATCH_NOMATCH);
2262          }
2263      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2264      if (      if (
2265  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1553  for (;;) Line 2272  for (;;)
2272      break;      break;
2273    
2274      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2275      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2276          {
2277          SCHECK_PARTIAL();
2278          RRETURN(MATCH_NOMATCH);
2279          }
2280      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2281      if (      if (
2282  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1566  for (;;) Line 2289  for (;;)
2289      break;      break;
2290    
2291      case OP_WORDCHAR:      case OP_WORDCHAR:
2292      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2293          {
2294          SCHECK_PARTIAL();
2295          RRETURN(MATCH_NOMATCH);
2296          }
2297      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2298      if (      if (
2299  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1579  for (;;) Line 2306  for (;;)
2306      break;      break;
2307    
2308      case OP_ANYNL:      case OP_ANYNL:
2309      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2310          {
2311          SCHECK_PARTIAL();
2312          RRETURN(MATCH_NOMATCH);
2313          }
2314      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2315      switch(c)      switch(c)
2316        {        {
2317        default: RRETURN(MATCH_NOMATCH);        default: RRETURN(MATCH_NOMATCH);
2318    
2319        case 0x000d:        case 0x000d:
2320        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
2321        break;        break;
# Line 1603  for (;;) Line 2335  for (;;)
2335      break;      break;
2336    
2337      case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
2338      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2339          {
2340          SCHECK_PARTIAL();
2341          RRETURN(MATCH_NOMATCH);
2342          }
2343      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2344      switch(c)      switch(c)
2345        {        {
# Line 1633  for (;;) Line 2369  for (;;)
2369      break;      break;
2370    
2371      case OP_HSPACE:      case OP_HSPACE:
2372      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2373          {
2374          SCHECK_PARTIAL();
2375          RRETURN(MATCH_NOMATCH);
2376          }
2377      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2378      switch(c)      switch(c)
2379        {        {
# Line 1663  for (;;) Line 2403  for (;;)
2403      break;      break;
2404    
2405      case OP_NOT_VSPACE:      case OP_NOT_VSPACE:
2406      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2407          {
2408          SCHECK_PARTIAL();
2409          RRETURN(MATCH_NOMATCH);
2410          }
2411      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2412      switch(c)      switch(c)
2413        {        {
# Line 1681  for (;;) Line 2425  for (;;)
2425      break;      break;
2426    
2427      case OP_VSPACE:      case OP_VSPACE:
2428      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2429          {
2430          SCHECK_PARTIAL();
2431          RRETURN(MATCH_NOMATCH);
2432          }
2433      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2434      switch(c)      switch(c)
2435        {        {
# Line 1704  for (;;) Line 2452  for (;;)
2452    
2453      case OP_PROP:      case OP_PROP:
2454      case OP_NOTPROP:      case OP_NOTPROP:
2455      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2456          {
2457          SCHECK_PARTIAL();
2458          RRETURN(MATCH_NOMATCH);
2459          }
2460      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2461        {        {
2462        const ucd_record *prop = GET_UCD(c);        const ucd_record *prop = GET_UCD(c);
# Line 1715  for (;;) Line 2467  for (;;)
2467          if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);          if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
2468          break;          break;
2469    
2470          case PT_LAMP:          case PT_LAMP:
2471          if ((prop->chartype == ucp_Lu ||          if ((prop->chartype == ucp_Lu ||
2472               prop->chartype == ucp_Ll ||               prop->chartype == ucp_Ll ||
2473               prop->chartype == ucp_Lt) == (op == OP_NOTPROP))               prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
2474              RRETURN(MATCH_NOMATCH);
2475            break;
2476    
2477            case PT_GC:
2478            if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))
2479              RRETURN(MATCH_NOMATCH);
2480            break;
2481    
2482            case PT_PC:
2483            if ((ecode[2] != prop->chartype) == (op == OP_PROP))
2484              RRETURN(MATCH_NOMATCH);
2485            break;
2486    
2487            case PT_SC:
2488            if ((ecode[2] != prop->script) == (op == OP_PROP))
2489              RRETURN(MATCH_NOMATCH);
2490            break;
2491    
2492            /* These are specials */
2493    
2494            case PT_ALNUM:
2495            if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2496                 _pcre_ucp_gentype[prop->chartype] == ucp_N) == (op == OP_NOTPROP))
2497            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
2498           break;          break;
2499    
2500          case PT_GC:          case PT_SPACE:    /* Perl space */
2501          if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))          if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2502                 c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2503                   == (op == OP_NOTPROP))
2504            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
2505          break;          break;
2506    
2507          case PT_PC:          case PT_PXSPACE:  /* POSIX space */
2508          if ((ecode[2] != prop->chartype) == (op == OP_PROP))          if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2509                 c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2510                 c == CHAR_FF || c == CHAR_CR)
2511                   == (op == OP_NOTPROP))
2512            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
2513          break;          break;
2514    
2515          case PT_SC:          case PT_WORD:
2516          if ((ecode[2] != prop->script) == (op == OP_PROP))          if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2517                 _pcre_ucp_gentype[prop->chartype] == ucp_N ||
2518                 c == CHAR_UNDERSCORE) == (op == OP_NOTPROP))
2519            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
2520          break;          break;
2521    
2522            /* This should never occur */
2523    
2524          default:          default:
2525          RRETURN(PCRE_ERROR_INTERNAL);          RRETURN(PCRE_ERROR_INTERNAL);
2526          }          }
# Line 1749  for (;;) Line 2533  for (;;)
2533      is in the binary; otherwise a compile-time error occurs. */      is in the binary; otherwise a compile-time error occurs. */
2534    
2535      case OP_EXTUNI:      case OP_EXTUNI:
2536      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2537          {
2538          SCHECK_PARTIAL();
2539          RRETURN(MATCH_NOMATCH);
2540          }
2541      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2542        if (UCD_CATEGORY(c) == ucp_M) RRETURN(MATCH_NOMATCH);
2543        while (eptr < md->end_subject)
2544        {        {
2545        int category = UCD_CATEGORY(c);        int len = 1;
2546        if (category == ucp_M) RRETURN(MATCH_NOMATCH);        if (!utf8) c = *eptr; else { GETCHARLEN(c, eptr, len); }
2547        while (eptr < md->end_subject)        if (UCD_CATEGORY(c) != ucp_M) break;
2548          {        eptr += len;
         int len = 1;  
         if (!utf8) c = *eptr; else  
           {  
           GETCHARLEN(c, eptr, len);  
           }  
         category = UCD_CATEGORY(c);  
         if (category != ucp_M) break;  
         eptr += len;  
         }  
2549        }        }
2550      ecode++;      ecode++;
2551      break;      break;
# Line 1780  for (;;) Line 2561  for (;;)
2561      loops). */      loops). */
2562    
2563      case OP_REF:      case OP_REF:
2564        {      case OP_REFI:
2565        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */      caseless = op == OP_REFI;
2566        ecode += 3;      offset = GET2(ecode, 1) << 1;               /* Doubled ref number */
2567        ecode += 3;
2568    
2569        /* If the reference is unset, there are two possibilities:      /* If the reference is unset, there are two possibilities:
2570    
2571        (a) In the default, Perl-compatible state, set the length to be longer      (a) In the default, Perl-compatible state, set the length negative;
2572        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
2573        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.  
2574    
2575        (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
2576        so that the back reference matches an empty string.      so that the back reference matches an empty string.
2577    
2578        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
2579        referenced subpattern. */      referenced subpattern. */
2580    
2581        if (offset >= offset_top || md->offset_vector[offset] < 0)      if (offset >= offset_top || md->offset_vector[offset] < 0)
2582          length = (md->jscript_compat)? 0 : md->end_subject - eptr + 1;        length = (md->jscript_compat)? 0 : -1;
2583        else      else
2584          length = md->offset_vector[offset+1] - md->offset_vector[offset];        length = md->offset_vector[offset+1] - md->offset_vector[offset];
2585    
2586        /* Set up for repetition, or handle the non-repeated case */      /* Set up for repetition, or handle the non-repeated case */
2587    
2588        switch (*ecode)      switch (*ecode)
2589          {        {
2590          case OP_CRSTAR:        case OP_CRSTAR:
2591          case OP_CRMINSTAR:        case OP_CRMINSTAR:
2592          case OP_CRPLUS:        case OP_CRPLUS:
2593          case OP_CRMINPLUS:        case OP_CRMINPLUS:
2594          case OP_CRQUERY:        case OP_CRQUERY:
2595          case OP_CRMINQUERY:        case OP_CRMINQUERY:
2596          c = *ecode++ - OP_CRSTAR;        c = *ecode++ - OP_CRSTAR;
2597          minimize = (c & 1) != 0;        minimize = (c & 1) != 0;
2598          min = rep_min[c];                 /* Pick up values from tables; */        min = rep_min[c];                 /* Pick up values from tables; */
2599          max = rep_max[c];                 /* zero for max => infinity */        max = rep_max[c];                 /* zero for max => infinity */
2600          if (max == 0) max = INT_MAX;        if (max == 0) max = INT_MAX;
2601          break;        break;
2602    
2603          case OP_CRRANGE:        case OP_CRRANGE:
2604          case OP_CRMINRANGE:        case OP_CRMINRANGE:
2605          minimize = (*ecode == OP_CRMINRANGE);        minimize = (*ecode == OP_CRMINRANGE);
2606          min = GET2(ecode, 1);        min = GET2(ecode, 1);
2607          max = GET2(ecode, 3);        max = GET2(ecode, 3);
2608          if (max == 0) max = INT_MAX;        if (max == 0) max = INT_MAX;
2609          ecode += 5;        ecode += 5;
2610          break;        break;
2611    
2612          default:               /* No repeat follows */        default:               /* No repeat follows */
2613          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);        if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
2614          eptr += length;          {
2615          continue;              /* With the main loop */          CHECK_PARTIAL();
2616            RRETURN(MATCH_NOMATCH);
2617          }          }
2618          eptr += length;
2619          continue;              /* With the main loop */
2620          }
2621    
2622        /* If the length of the reference is zero, just continue with the      /* Handle repeated back references. If the length of the reference is
2623        main loop. */      zero, just continue with the main loop. */
2624    
2625        if (length == 0) continue;      if (length == 0) continue;
2626    
2627        /* First, ensure the minimum number of matches are present. We get back      /* First, ensure the minimum number of matches are present. We get back
2628        the length of the reference string explicitly rather than passing the      the length of the reference string explicitly rather than passing the
2629        address of eptr, so that eptr can be a register variable. */      address of eptr, so that eptr can be a register variable. */
2630    
2631        for (i = 1; i <= min; i++)      for (i = 1; i <= min; i++)
2632          {
2633          int slength;
2634          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2635          {          {
2636          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);          CHECK_PARTIAL();
2637          eptr += length;          RRETURN(MATCH_NOMATCH);
2638          }          }
2639          eptr += slength;
2640          }
2641    
2642        /* If min = max, continue at the same level without recursion.      /* If min = max, continue at the same level without recursion.
2643        They are not both allowed to be zero. */      They are not both allowed to be zero. */
2644    
2645        if (min == max) continue;      if (min == max) continue;
2646    
2647        /* If minimizing, keep trying and advancing the pointer */      /* If minimizing, keep trying and advancing the pointer */
2648    
2649        if (minimize)      if (minimize)
2650          {
2651          for (fi = min;; fi++)
2652          {          {
2653          for (fi = min;; fi++)          int slength;
2654            RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
2655            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2656            if (fi >= max) RRETURN(MATCH_NOMATCH);
2657            if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2658            {            {
2659            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM14);            CHECK_PARTIAL();
2660            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            RRETURN(MATCH_NOMATCH);
           if (fi >= max || !match_ref(offset, eptr, length, md, ims))  
             RRETURN(MATCH_NOMATCH);  
           eptr += length;  
2661            }            }
2662          /* Control never gets here */          eptr += slength;
2663          }          }
2664          /* Control never gets here */
2665          }
2666    
2667        /* If maximizing, find the longest string and work backwards */      /* If maximizing, find the longest string and work backwards */
2668    
2669        else      else
2670          {
2671          pp = eptr;
2672          for (i = min; i < max; i++)
2673          {          {
2674          pp = eptr;          int slength;
2675          for (i = min; i < max; i++)          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
           {  
           if (!match_ref(offset, eptr, length, md, ims)) break;  
           eptr += length;  
           }  
         while (eptr >= pp)  
2676            {            {
2677            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM15);            CHECK_PARTIAL();
2678            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            break;
           eptr -= length;  
2679            }            }
2680          RRETURN(MATCH_NOMATCH);          eptr += slength;
2681            }
2682          while (eptr >= pp)
2683            {
2684            RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
2685            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2686            eptr -= length;
2687          }          }
2688          RRETURN(MATCH_NOMATCH);
2689        }        }
2690      /* Control never gets here */      /* Control never gets here */
2691    
   
   
2692      /* Match a bit-mapped character class, possibly repeatedly. This op code is      /* Match a bit-mapped character class, possibly repeatedly. This op code is
2693      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,
2694      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 1946  for (;;) Line 2743  for (;;)
2743          {          {
2744          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2745            {            {
2746            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2747                {
2748                SCHECK_PARTIAL();
2749                RRETURN(MATCH_NOMATCH);
2750                }
2751            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
2752            if (c > 255)            if (c > 255)
2753              {              {
# Line 1964  for (;;) Line 2765  for (;;)
2765          {          {
2766          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2767            {            {
2768            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2769                {
2770                SCHECK_PARTIAL();
2771                RRETURN(MATCH_NOMATCH);
2772                }
2773            c = *eptr++;            c = *eptr++;
2774            if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);            if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2775            }            }
# Line 1986  for (;;) Line 2791  for (;;)
2791            {            {
2792            for (fi = min;; fi++)            for (fi = min;; fi++)
2793              {              {
2794              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM16);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
2795              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2796              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) RRETURN(MATCH_NOMATCH);
2797                if (eptr >= md->end_subject)
2798                  {
2799                  SCHECK_PARTIAL();
2800                  RRETURN(MATCH_NOMATCH);
2801                  }
2802              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
2803              if (c > 255)              if (c > 255)
2804                {                {
# Line 2006  for (;;) Line 2816  for (;;)
2816            {            {
2817            for (fi = min;; fi++)            for (fi = min;; fi++)
2818              {              {
2819              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM17);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
2820              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2821              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) RRETURN(MATCH_NOMATCH);
2822                if (eptr >= md->end_subject)
2823                  {
2824                  SCHECK_PARTIAL();
2825                  RRETURN(MATCH_NOMATCH);
2826                  }
2827              c = *eptr++;              c = *eptr++;
2828              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2829              }              }
# Line 2029  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                  {
2849                  SCHECK_PARTIAL();
2850                  break;
2851                  }
2852              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
2853              if (c > 255)              if (c > 255)
2854                {                {
# Line 2043  for (;;) Line 2862  for (;;)
2862              }              }
2863            for (;;)            for (;;)
2864              {              {
2865              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM18);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
2866              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2867              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2868              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2055  for (;;) Line 2874  for (;;)
2874            {            {
2875            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2876              {              {
2877              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
2878                  {
2879                  SCHECK_PARTIAL();
2880                  break;
2881                  }
2882              c = *eptr;              c = *eptr;
2883              if ((data[c/8] & (1 << (c&7))) == 0) break;              if ((data[c/8] & (1 << (c&7))) == 0) break;
2884              eptr++;              eptr++;
2885              }              }
2886            while (eptr >= pp)            while (eptr >= pp)
2887              {              {
2888              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM19);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM19);
2889              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2890              eptr--;              eptr--;
2891              }              }
# Line 2117  for (;;) Line 2940  for (;;)
2940    
2941        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
2942          {          {
2943          if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);          if (eptr >= md->end_subject)
2944              {
2945              SCHECK_PARTIAL();
2946              RRETURN(MATCH_NOMATCH);
2947              }
2948          GETCHARINCTEST(c, eptr);          GETCHARINCTEST(c, eptr);
2949          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);
2950          }          }
# Line 2134  for (;;) Line 2961  for (;;)
2961          {          {
2962          for (fi = min;; fi++)          for (fi = min;; fi++)
2963            {            {
2964            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM20);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM20);
2965            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2966            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (fi >= max) RRETURN(MATCH_NOMATCH);
2967              if (eptr >= md->end_subject)
2968                {
2969                SCHECK_PARTIAL();
2970                RRETURN(MATCH_NOMATCH);
2971                }
2972            GETCHARINCTEST(c, eptr);            GETCHARINCTEST(c, eptr);
2973            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);
2974            }            }
# Line 2151  for (;;) Line 2983  for (;;)
2983          for (i = min; i < max; i++)          for (i = min; i < max; i++)
2984            {            {
2985            int len = 1;            int len = 1;
2986            if (eptr >= md->end_subject) break;            if (eptr >= md->end_subject)
2987                {
2988                SCHECK_PARTIAL();
2989                break;
2990                }
2991            GETCHARLENTEST(c, eptr, len);            GETCHARLENTEST(c, eptr, len);
2992            if (!_pcre_xclass(c, data)) break;            if (!_pcre_xclass(c, data)) break;
2993            eptr += len;            eptr += len;
2994            }            }
2995          for(;;)          for(;;)
2996            {            {
2997            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM21);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM21);
2998            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2999            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
3000            if (utf8) BACKCHAR(eptr);            if (utf8) BACKCHAR(eptr);
# Line 2179  for (;;) Line 3015  for (;;)
3015        length = 1;        length = 1;
3016        ecode++;        ecode++;
3017        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
3018        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
3019            {
3020            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
3021            RRETURN(MATCH_NOMATCH);
3022            }
3023        while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);        while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);
3024        }        }
3025      else      else
# Line 2187  for (;;) Line 3027  for (;;)
3027    
3028      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
3029        {        {
3030        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
3031            {
3032            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
3033            RRETURN(MATCH_NOMATCH);
3034            }
3035        if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);        if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);
3036        ecode += 2;        ecode += 2;
3037        }        }
# Line 2195  for (;;) Line 3039  for (;;)
3039    
3040      /* Match a single character, caselessly */      /* Match a single character, caselessly */
3041    
3042      case OP_CHARNC:      case OP_CHARI:
3043  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3044      if (utf8)      if (utf8)
3045        {        {
# Line 2203  for (;;) Line 3047  for (;;)
3047        ecode++;        ecode++;
3048        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
3049    
3050        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
3051            {
3052            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
3053            RRETURN(MATCH_NOMATCH);
3054            }
3055    
3056        /* 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
3057        can use the fast lookup table. */        can use the fast lookup table. */
# Line 2238  for (;;) Line 3086  for (;;)
3086    
3087      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
3088        {        {
3089        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
3090            {
3091            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
3092            RRETURN(MATCH_NOMATCH);
3093            }
3094        if (md->lcc[ecode[1]] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);        if (md->lcc[ecode[1]] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);
3095        ecode += 2;        ecode += 2;
3096        }        }
# Line 2247  for (;;) Line 3099  for (;;)
3099      /* Match a single character repeatedly. */      /* Match a single character repeatedly. */
3100    
3101      case OP_EXACT:      case OP_EXACT:
3102        case OP_EXACTI:
3103      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
3104      ecode += 3;      ecode += 3;
3105      goto REPEATCHAR;      goto REPEATCHAR;
3106    
3107      case OP_POSUPTO:      case OP_POSUPTO:
3108        case OP_POSUPTOI:
3109      possessive = TRUE;      possessive = TRUE;
3110      /* Fall through */      /* Fall through */
3111    
3112      case OP_UPTO:      case OP_UPTO:
3113        case OP_UPTOI:
3114      case OP_MINUPTO:      case OP_MINUPTO:
3115        case OP_MINUPTOI:
3116      min = 0;      min = 0;
3117      max = GET2(ecode, 1);      max = GET2(ecode, 1);
3118      minimize = *ecode == OP_MINUPTO;      minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI;
3119      ecode += 3;      ecode += 3;
3120      goto REPEATCHAR;      goto REPEATCHAR;
3121    
3122      case OP_POSSTAR:      case OP_POSSTAR:
3123        case OP_POSSTARI:
3124      possessive = TRUE;      possessive = TRUE;
3125      min = 0;      min = 0;
3126      max = INT_MAX;      max = INT_MAX;
# Line 2271  for (;;) Line 3128  for (;;)
3128      goto REPEATCHAR;      goto REPEATCHAR;
3129    
3130      case OP_POSPLUS:      case OP_POSPLUS:
3131        case OP_POSPLUSI:
3132      possessive = TRUE;      possessive = TRUE;
3133      min = 1;      min = 1;
3134      max = INT_MAX;      max = INT_MAX;
# Line 2278  for (;;) Line 3136  for (;;)
3136      goto REPEATCHAR;      goto REPEATCHAR;
3137    
3138      case OP_POSQUERY:      case OP_POSQUERY:
3139        case OP_POSQUERYI:
3140      possessive = TRUE;      possessive = TRUE;
3141      min = 0;      min = 0;
3142      max = 1;      max = 1;
# Line 2285  for (;;) Line 3144  for (;;)
3144      goto REPEATCHAR;      goto REPEATCHAR;
3145    
3146      case OP_STAR:      case OP_STAR:
3147        case OP_STARI:
3148      case OP_MINSTAR:      case OP_MINSTAR:
3149        case OP_MINSTARI:
3150      case OP_PLUS:      case OP_PLUS:
3151        case OP_PLUSI:
3152      case OP_MINPLUS:      case OP_MINPLUS:
3153        case OP_MINPLUSI:
3154      case OP_QUERY:      case OP_QUERY:
3155        case OP_QUERYI:
3156      case OP_MINQUERY:      case OP_MINQUERY:
3157      c = *ecode++ - OP_STAR;      case OP_MINQUERYI:
3158        c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI);
3159      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3160      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3161      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3162      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
3163    
3164      /* 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. */  
3165    
3166      REPEATCHAR:      REPEATCHAR:
3167  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 2307  for (;;) Line 3170  for (;;)
3170        length = 1;        length = 1;
3171        charptr = ecode;        charptr = ecode;
3172        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
       if (min * length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3173        ecode += length;        ecode += length;
3174    
3175        /* Handle multibyte character matching specially here. There is        /* Handle multibyte character matching specially here. There is
# Line 2317  for (;;) Line 3179  for (;;)
3179          {          {
3180  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3181          unsigned int othercase;          unsigned int othercase;
3182          if ((ims & PCRE_CASELESS) != 0 &&          if (op >= OP_STARI &&     /* Caseless */
3183              (othercase = UCD_OTHERCASE(fc)) != fc)              (othercase = UCD_OTHERCASE(fc)) != fc)
3184            oclength = _pcre_ord2utf8(othercase, occhars);            oclength = _pcre_ord2utf8(othercase, occhars);
3185          else oclength = 0;          else oclength = 0;
# Line 2325  for (;;) Line 3187  for (;;)
3187    
3188          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3189            {            {
3190            if (memcmp(eptr, charptr, length) == 0) eptr += length;            if (eptr <= md->end_subject - length &&
3191                memcmp(eptr, charptr, length) == 0) eptr += length;
3192  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3193            /* Need braces because of following else */            else if (oclength > 0 &&
3194            else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }                     eptr <= md->end_subject - oclength &&
3195                       memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3196    #endif  /* SUPPORT_UCP */
3197            else            else
3198              {              {
3199              if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);              CHECK_PARTIAL();
3200              eptr += oclength;              RRETURN(MATCH_NOMATCH);
3201              }              }
 #else   /* without SUPPORT_UCP */  
           else { RRETURN(MATCH_NOMATCH); }  
 #endif  /* SUPPORT_UCP */  
3202            }            }
3203    
3204          if (min == max) continue;          if (min == max) continue;
# Line 2345  for (;;) Line 3207  for (;;)
3207            {            {
3208            for (fi = min;; fi++)            for (fi = min;; fi++)
3209              {              {
3210              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM22);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM22);
3211              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3212              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) RRETURN(MATCH_NOMATCH);
3213              if (memcmp(eptr, charptr, length) == 0) eptr += length;              if (eptr <= md->end_subject - length &&
3214                  memcmp(eptr, charptr, length) == 0) eptr += length;
3215  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3216              /* Need braces because of following else */              else if (oclength > 0 &&
3217              else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }                       eptr <= md->end_subject - oclength &&
3218                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3219    #endif  /* SUPPORT_UCP */
3220              else              else
3221                {                {
3222                if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);                CHECK_PARTIAL();
3223                eptr += oclength;                RRETURN(MATCH_NOMATCH);
3224                }                }
 #else   /* without SUPPORT_UCP */  
             else { RRETURN (MATCH_NOMATCH); }  
 #endif  /* SUPPORT_UCP */  
3225              }              }
3226            /* Control never gets here */            /* Control never gets here */
3227            }            }
# Line 2369  for (;;) Line 3231  for (;;)
3231            pp = eptr;            pp = eptr;
3232            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3233              {              {
3234              if (eptr > md->end_subject - length) break;              if (eptr <= md->end_subject - length &&
3235              if (memcmp(eptr, charptr, length) == 0) eptr += length;                  memcmp(eptr, charptr, length) == 0) eptr += length;
3236  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3237              else if (oclength == 0) break;              else if (oclength > 0 &&
3238                         eptr <= md->end_subject - oclength &&
3239                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3240    #endif  /* SUPPORT_UCP */
3241              else              else
3242                {                {
3243                if (memcmp(eptr, occhars, oclength) != 0) break;                CHECK_PARTIAL();
3244                eptr += oclength;                break;
3245                }                }
 #else   /* without SUPPORT_UCP */  
             else break;  
 #endif  /* SUPPORT_UCP */  
3246              }              }
3247    
3248            if (possessive) continue;            if (possessive) continue;
3249    
3250            for(;;)            for(;;)
3251             {              {
3252             RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM23);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM23);
3253             if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3254             if (eptr == pp) RRETURN(MATCH_NOMATCH);              if (eptr == pp) { RRETURN(MATCH_NOMATCH); }
3255  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3256             eptr--;              eptr--;
3257             BACKCHAR(eptr);              BACKCHAR(eptr);
3258  #else   /* without SUPPORT_UCP */  #else   /* without SUPPORT_UCP */
3259             eptr -= length;              eptr -= length;
3260  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3261             }              }
3262            }            }
3263          /* Control never gets here */          /* Control never gets here */
3264          }          }
# Line 2408  for (;;) Line 3271  for (;;)
3271  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
3272    
3273      /* When not in UTF-8 mode, load a single-byte character. */      /* When not in UTF-8 mode, load a single-byte character. */
3274        {  
3275        if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);      fc = *ecode++;
       fc = *ecode++;  
       }  
3276    
3277      /* 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
3278      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 2425  for (;;) Line 3286  for (;;)
3286      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3287        max, eptr));        max, eptr));
3288    
3289      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_STARI)  /* Caseless */
3290        {        {
3291        fc = md->lcc[fc];        fc = md->lcc[fc];
3292        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
3293            {
3294            if (eptr >= md->end_subject)
3295              {
3296              SCHECK_PARTIAL();
3297              RRETURN(MATCH_NOMATCH);
3298              }
3299          if (fc != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          if (fc != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);
3300            }
3301        if (min == max) continue;        if (min == max) continue;
3302        if (minimize)        if (minimize)
3303          {          {
3304          for (fi = min;; fi++)          for (fi = min;; fi++)
3305            {            {
3306            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM24);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM24);
3307            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3308            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max) RRETURN(MATCH_NOMATCH);
3309                fc != md->lcc[*eptr++])            if (eptr >= md->end_subject)
3310                {
3311                SCHECK_PARTIAL();
3312              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3313                }
3314              if (fc != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);
3315            }            }
3316          /* Control never gets here */          /* Control never gets here */
3317          }          }
# Line 2448  for (;;) Line 3320  for (;;)
3320          pp = eptr;          pp = eptr;
3321          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3322            {            {
3323            if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break;            if (eptr >= md->end_subject)
3324                {
3325                SCHECK_PARTIAL();
3326                break;
3327                }
3328              if (fc != md->lcc[*eptr]) break;
3329            eptr++;            eptr++;
3330            }            }
3331    
3332          if (possessive) continue;          if (possessive) continue;
3333    
3334          while (eptr >= pp)          while (eptr >= pp)
3335            {            {
3336            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM25);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM25);
3337            eptr--;            eptr--;
3338            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3339            }            }
# Line 2467  for (;;) Line 3346  for (;;)
3346    
3347      else      else
3348        {        {
3349        for (i = 1; i <= min; i++) if (fc != *eptr++) RRETURN(MATCH_NOMATCH);        for (i = 1; i <= min; i++)
3350            {
3351            if (eptr >= md->end_subject)
3352              {
3353              SCHECK_PARTIAL();
3354              RRETURN(MATCH_NOMATCH);
3355              }
3356            if (fc != *eptr++) RRETURN(MATCH_NOMATCH);
3357            }
3358    
3359        if (min == max) continue;        if (min == max) continue;
3360    
3361        if (minimize)        if (minimize)
3362          {          {
3363          for (fi = min;; fi++)          for (fi = min;; fi++)
3364            {            {
3365            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM26);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM26);
3366            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3367            if (fi >= max || eptr >= md->end_subject || fc != *eptr++)            if (fi >= max) RRETURN(MATCH_NOMATCH);
3368              if (eptr >= md->end_subject)
3369                {
3370                SCHECK_PARTIAL();
3371              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3372                }
3373              if (fc != *eptr++) RRETURN(MATCH_NOMATCH);
3374            }            }
3375          /* Control never gets here */          /* Control never gets here */
3376          }          }
# Line 2485  for (;;) Line 3379  for (;;)
3379          pp = eptr;          pp = eptr;
3380          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3381            {            {
3382            if (eptr >= md->end_subject || fc != *eptr) break;            if (eptr >= md->end_subject)
3383                {
3384                SCHECK_PARTIAL();
3385                break;
3386                }
3387              if (fc != *eptr) break;
3388            eptr++;            eptr++;
3389            }            }
3390          if (possessive) continue;          if (possessive) continue;
3391    
3392          while (eptr >= pp)          while (eptr >= pp)
3393            {            {
3394            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM27);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM27);
3395            eptr--;            eptr--;
3396            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3397            }            }
# Line 2504  for (;;) Line 3404  for (;;)
3404      checking can be multibyte. */      checking can be multibyte. */
3405    
3406      case OP_NOT:      case OP_NOT:
3407      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      case OP_NOTI:
3408        if (eptr >= md->end_subject)
3409          {
3410          SCHECK_PARTIAL();
3411          RRETURN(MATCH_NOMATCH);
3412          }
3413      ecode++;      ecode++;
3414      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
3415      if ((ims & PCRE_CASELESS) != 0)      if (op == OP_NOTI)         /* The caseless case */
3416        {        {
3417  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3418        if (c < 256)        if (c < 256)
# Line 2515  for (;;) Line 3420  for (;;)
3420        c = md->lcc[c];        c = md->lcc[c];
3421        if (md->lcc[*ecode++] == c) RRETURN(MATCH_NOMATCH);        if (md->lcc[*ecode++] == c) RRETURN(MATCH_NOMATCH);
3422        }        }
3423      else      else    /* Caseful */
3424        {        {
3425        if (*ecode++ == c) RRETURN(MATCH_NOMATCH);        if (*ecode++ == c) RRETURN(MATCH_NOMATCH);
3426        }        }
# Line 2529  for (;;) Line 3434  for (;;)
3434      about... */      about... */
3435    
3436      case OP_NOTEXACT:      case OP_NOTEXACT:
3437        case OP_NOTEXACTI:
3438      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
3439      ecode += 3;      ecode += 3;
3440      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3441    
3442      case OP_NOTUPTO:      case OP_NOTUPTO:
3443        case OP_NOTUPTOI:
3444      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
3445        case OP_NOTMINUPTOI:
3446      min = 0;      min = 0;
3447      max = GET2(ecode, 1);      max = GET2(ecode, 1);
3448      minimize = *ecode == OP_NOTMINUPTO;      minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI;
3449      ecode += 3;      ecode += 3;
3450      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3451    
3452      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
3453        case OP_NOTPOSSTARI:
3454      possessive = TRUE;      possessive = TRUE;
3455      min = 0;      min = 0;
3456      max = INT_MAX;      max = INT_MAX;
# Line 2549  for (;;) Line 3458  for (;;)
3458      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3459    
3460      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
3461        case OP_NOTPOSPLUSI:
3462      possessive = TRUE;      possessive = TRUE;
3463      min = 1;      min = 1;
3464      max = INT_MAX;      max = INT_MAX;
# Line 2556  for (;;) Line 3466  for (;;)
3466      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3467    
3468      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
3469        case OP_NOTPOSQUERYI:
3470      possessive = TRUE;      possessive = TRUE;
3471      min = 0;      min = 0;
3472      max = 1;      max = 1;
# Line 2563  for (;;) Line 3474  for (;;)
3474      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3475    
3476      case OP_NOTPOSUPTO:      case OP_NOTPOSUPTO:
3477        case OP_NOTPOSUPTOI:
3478      possessive = TRUE;      possessive = TRUE;
3479      min = 0;      min = 0;
3480      max = GET2(ecode, 1);      max = GET2(ecode, 1);
# Line 2570  for (;;) Line 3482  for (;;)
3482      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3483    
3484      case OP_NOTSTAR:      case OP_NOTSTAR:
3485        case OP_NOTSTARI:
3486      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
3487        case OP_NOTMINSTARI:
3488      case OP_NOTPLUS:      case OP_NOTPLUS:
3489        case OP_NOTPLUSI:
3490      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
3491        case OP_NOTMINPLUSI:
3492      case OP_NOTQUERY:      case OP_NOTQUERY:
3493        case OP_NOTQUERYI:
3494      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
3495      c = *ecode++ - OP_NOTSTAR;      case OP_NOTMINQUERYI:
3496        c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
3497      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3498      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3499      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3500      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
3501    
3502      /* 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. */  
3503    
3504      REPEATNOTCHAR:      REPEATNOTCHAR:
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3505      fc = *ecode++;      fc = *ecode++;
3506    
3507      /* 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 2600  for (;;) Line 3515  for (;;)
3515      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,
3516        max, eptr));        max, eptr));
3517    
3518      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_NOTSTARI)     /* Caseless */
3519        {        {
3520        fc = md->lcc[fc];        fc = md->lcc[fc];
3521    
# Line 2611  for (;;) Line 3526  for (;;)
3526          register unsigned int d;          register unsigned int d;
3527          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3528            {            {
3529              if (eptr >= md->end_subject)
3530                {
3531                SCHECK_PARTIAL();
3532                RRETURN(MATCH_NOMATCH);
3533                }
3534            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3535            if (d < 256) d = md->lcc[d];            if (d < 256) d = md->lcc[d];
3536            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) RRETURN(MATCH_NOMATCH);
# Line 2622  for (;;) Line 3542  for (;;)
3542        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3543          {          {
3544          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3545              {
3546              if (eptr >= md->end_subject)
3547                {
3548                SCHECK_PARTIAL();
3549                RRETURN(MATCH_NOMATCH);
3550                }
3551            if (fc == md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);            if (fc == md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);
3552              }
3553          }          }
3554    
3555        if (min == max) continue;        if (min == max) continue;
# Line 2636  for (;;) Line 3563  for (;;)
3563            register unsigned int d;            register unsigned int d;
3564            for (fi = min;; fi++)            for (fi = min;; fi++)
3565              {              {
3566              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM28);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM28);
3567              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3568              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) RRETURN(MATCH_NOMATCH);
3569                if (eptr >= md->end_subject)
3570                  {
3571                  SCHECK_PARTIAL();
3572                  RRETURN(MATCH_NOMATCH);
3573                  }
3574              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3575              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3576              if (fc == d) RRETURN(MATCH_NOMATCH);              if (fc == d) RRETURN(MATCH_NOMATCH);
   
3577              }              }
3578            }            }
3579          else          else
# Line 2651  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, RM29);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM29);
3586              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3587              if (fi >= max || eptr >= md->end_subject || fc == md->lcc[*eptr++])              if (fi >= max) RRETURN(MATCH_NOMATCH);
3588                if (eptr >= md->end_subject)
3589                  {
3590                  SCHECK_PARTIAL();
3591                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3592                  }
3593                if (fc == md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);
3594              }              }
3595            }            }
3596          /* Control never gets here */          /* Control never gets here */
# Line 2674  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 (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3620              if (fc == d) break;              if (fc == d) break;
# Line 2683  for (;;) Line 3623  for (;;)
3623          if (possessive) continue;          if (possessive) continue;
3624          for(;;)          for(;;)
3625              {              {
3626              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM30);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM30);
3627              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3628              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3629              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2695  for (;;) Line 3635  for (;;)
3635            {            {
3636            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3637              {              {
3638              if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break;              if (eptr >= md->end_subject)
3639                  {
3640                  SCHECK_PARTIAL();
3641                  break;
3642                  }
3643                if (fc == md->lcc[*eptr]) break;
3644              eptr++;              eptr++;
3645              }              }
3646            if (possessive) continue;            if (possessive) continue;
3647            while (eptr >= pp)            while (eptr >= pp)
3648              {              {
3649              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM31);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM31);
3650              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3651              eptr--;              eptr--;
3652              }              }
# Line 2723  for (;;) Line 3668  for (;;)
3668          register unsigned int d;          register unsigned int d;
3669          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3670            {            {
3671              if (eptr >= md->end_subject)
3672                {
3673                SCHECK_PARTIAL();
3674                RRETURN(MATCH_NOMATCH);
3675                }
3676            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3677            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) RRETURN(MATCH_NOMATCH);
3678            }            }
# Line 2732  for (;;) Line 3682  for (;;)
3682        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3683          {          {
3684          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3685              {
3686              if (eptr >= md->end_subject)
3687                {
3688                SCHECK_PARTIAL();
3689                RRETURN(MATCH_NOMATCH);
3690                }
3691            if (fc == *eptr++) RRETURN(MATCH_NOMATCH);            if (fc == *eptr++) RRETURN(MATCH_NOMATCH);
3692              }
3693          }          }
3694    
3695        if (min == max) continue;        if (min == max) continue;
# Line 2746  for (;;) Line 3703  for (;;)
3703            register unsigned int d;            register unsigned int d;
3704            for (fi = min;; fi++)            for (fi = min;; fi++)
3705              {              {
3706              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM32);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM32);
3707              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3708              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) RRETURN(MATCH_NOMATCH);
3709                if (eptr >= md->end_subject)
3710                  {
3711                  SCHECK_PARTIAL();
3712                  RRETURN(MATCH_NOMATCH);
3713                  }
3714              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3715              if (fc == d) RRETURN(MATCH_NOMATCH);              if (fc == d) RRETURN(MATCH_NOMATCH);
3716              }              }
# Line 2759  for (;;) Line 3721  for (;;)
3721            {            {
3722            for (fi = min;; fi++)            for (fi = min;; fi++)
3723              {              {
3724              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM33);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM33);
3725              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3726              if (fi >= max || eptr >= md->end_subject || fc == *eptr++)              if (fi >= max) RRETURN(MATCH_NOMATCH);
3727                if (eptr >= md->end_subject)
3728                  {
3729                  SCHECK_PARTIAL();
3730                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3731                  }
3732                if (fc == *eptr++) RRETURN(MATCH_NOMATCH);
3733              }              }
3734            }            }
3735          /* Control never gets here */          /* Control never gets here */
# Line 2782  for (;;) Line 3749  for (;;)
3749            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3750              {              {
3751              int len = 1;              int len = 1;
3752              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3753                  {
3754                  SCHECK_PARTIAL();
3755                  break;
3756                  }
3757              GETCHARLEN(d, eptr, len);              GETCHARLEN(d, eptr, len);
3758              if (fc == d) break;              if (fc == d) break;
3759              eptr += len;              eptr += len;
# Line 2790  for (;;) Line 3761  for (;;)
3761            if (possessive) continue;            if (possessive) continue;
3762            for(;;)            for(;;)
3763              {              {
3764              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM34);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM34);
3765              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3766              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3767              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2802  for (;;) Line 3773  for (;;)
3773            {            {
3774            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3775              {              {
3776              if (eptr >= md->end_subject || fc == *eptr) break;              if (eptr >= md->end_subject)
3777                  {
3778                  SCHECK_PARTIAL();
3779                  break;
3780                  }
3781                if (fc == *eptr) break;
3782              eptr++;              eptr++;
3783              }              }
3784            if (possessive) continue;            if (possessive) continue;
3785            while (eptr >= pp)            while (eptr >= pp)
3786              {              {
3787              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM35);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM35);
3788              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3789              eptr--;              eptr--;
3790              }              }
# Line 2896  for (;;) Line 3872  for (;;)
3872    
3873      /* First, ensure the minimum number of matches are present. Use inline      /* First, ensure the minimum number of matches are present. Use inline
3874      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
3875      (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  
3876      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
3877      and single-bytes. */      and single-bytes. */
3878    
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3879      if (min > 0)      if (min > 0)
3880        {        {
3881  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 2914  for (;;) Line 3887  for (;;)
3887            if (prop_fail_result) RRETURN(MATCH_NOMATCH);            if (prop_fail_result) RRETURN(MATCH_NOMATCH);
3888            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3889              {              {
3890              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3891                  {
3892                  SCHECK_PARTIAL();
3893                  RRETURN(MATCH_NOMATCH);
3894                  }
3895                GETCHARINCTEST(c, eptr);
3896                }
3897              break;
3898    
3899              case PT_LAMP:
3900              for (i = 1; i <= min; i++)
3901                {
3902                int chartype;
3903                if (eptr >= md->end_subject)
3904                  {
3905                  SCHECK_PARTIAL();
3906                  RRETURN(MATCH_NOMATCH);
3907                  }
3908                GETCHARINCTEST(c, eptr);
3909                chartype = UCD_CHARTYPE(c);
3910                if ((chartype == ucp_Lu ||
3911                     chartype == ucp_Ll ||
3912                     chartype == ucp_Lt) == prop_fail_result)
3913                  RRETURN(MATCH_NOMATCH);
3914                }
3915              break;
3916    
3917              case PT_GC:
3918              for (i = 1; i <= min; i++)
3919                {
3920                if (eptr >= md->end_subject)
3921                  {
3922                  SCHECK_PARTIAL();
3923                  RRETURN(MATCH_NOMATCH);
3924                  }
3925                GETCHARINCTEST(c, eptr);
3926                if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result)
3927                  RRETURN(MATCH_NOMATCH);
3928                }
3929              break;
3930    
3931              case PT_PC:
3932              for (i = 1; i <= min; i++)
3933                {
3934                if (eptr >= md->end_subject)
3935                  {
3936                  SCHECK_PARTIAL();
3937                  RRETURN(MATCH_NOMATCH);
3938                  }
3939                GETCHARINCTEST(c, eptr);
3940                if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result)
3941                  RRETURN(MATCH_NOMATCH);
3942                }
3943              break;
3944    
3945              case PT_SC:
3946              for (i = 1; i <= min; i++)