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