/[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 144 by ph10, Mon Apr 2 13:32:07 2007 UTC revision 613 by ph10, Sat Jul 2 16:59:52 2011 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2007 University of Cambridge             Copyright (c) 1997-2011 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 42  POSSIBILITY OF SUCH DAMAGE. Line 42  POSSIBILITY OF SUCH DAMAGE.
42  pattern matching using an NFA algorithm, trying to mimic Perl as closely as  pattern matching using an NFA algorithm, trying to mimic Perl as closely as
43  possible. There are also some static supporting functions. */  possible. There are also some static supporting functions. */
44    
45    #ifdef HAVE_CONFIG_H
46    #include "config.h"
47    #endif
48    
49  #define NLBLOCK md             /* Block containing newline information */  #define NLBLOCK md             /* Block containing newline information */
50  #define PSSTART start_subject  /* Field containing processed string start */  #define PSSTART start_subject  /* Field containing processed string start */
51  #define PSEND   end_subject    /* Field containing processed string end */  #define PSEND   end_subject    /* Field containing processed string end */
# Line 53  possible. There are also some static sup Line 57  possible. There are also some static sup
57  #undef min  #undef min
58  #undef max  #undef max
59    
60  /* The chain of eptrblocks for tail recursions uses memory in stack workspace,  /* Values for setting in md->match_function_type to indicate two special types
61  obtained at top level, the size of which is defined by EPTR_WORK_SIZE. */  of call to match(). We do it this way to save on using another stack variable,
62    as stack usage is to be discouraged. */
 #define EPTR_WORK_SIZE (1000)  
   
 /* Flag bits for the match() function */  
63    
64  #define match_condassert     0x01  /* Called to check a condition assertion */  #define MATCH_CONDASSERT     1  /* Called to check a condition assertion */
65  #define match_cbegroup       0x02  /* Could-be-empty unlimited repeat group */  #define MATCH_CBEGROUP       2  /* Could-be-empty unlimited repeat group */
 #define match_tail_recursed  0x04  /* Tail recursive call */  
66    
67  /* Non-error returns from the match() function. Error returns are externally  /* Non-error returns from the match() function. Error returns are externally
68  defined PCRE_ERROR_xxx codes, which are all negative. */  defined PCRE_ERROR_xxx codes, which are all negative. */
# Line 70  defined PCRE_ERROR_xxx codes, which are Line 70  defined PCRE_ERROR_xxx codes, which are
70  #define MATCH_MATCH        1  #define MATCH_MATCH        1
71  #define MATCH_NOMATCH      0  #define MATCH_NOMATCH      0
72    
73    /* Special internal returns from the match() function. Make them sufficiently
74    negative to avoid the external error codes. */
75    
76    #define MATCH_ACCEPT       (-999)
77    #define MATCH_COMMIT       (-998)
78    #define MATCH_KETRPOS      (-997)
79    #define MATCH_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,
94  because the offset vector is always a multiple of 3 long. */  because the offset vector is always a multiple of 3 long. */
# Line 83  static const char rep_max[] = { 0, 0, 0, Line 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 116  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 148  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 188  calls by keeping local variables that ne Line 250  calls by keeping local variables that ne
250  obtained from malloc() instead instead of on the stack. Macros are used to  obtained from malloc() instead instead of on the stack. Macros are used to
251  achieve this so that the actual code doesn't look very different to what it  achieve this so that the actual code doesn't look very different to what it
252  always used to.  always used to.
253    
254    The original heap-recursive code used longjmp(). However, it seems that this
255    can be very slow on some operating systems. Following a suggestion from Stan
256    Switzer, the use of longjmp() has been abolished, at the cost of having to
257    provide a unique number for each call to RMATCH. There is no way of generating
258    a sequence of numbers at compile time in C. I have given them names, to make
259    them stand out more clearly.
260    
261    Crude tests on x86 Linux show a small speedup of around 5-8%. However, on
262    FreeBSD, avoiding longjmp() more than halves the time taken to run the standard
263    tests. Furthermore, not using longjmp() means that local dynamic variables
264    don't have indeterminate values; this has meant that the frame size can be
265    reduced because the result can be "passed back" by straight setting of the
266    variable instead of being passed in the frame.
267  ****************************************************************************  ****************************************************************************
268  ***************************************************************************/  ***************************************************************************/
269    
270    /* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN
271    below must be updated in sync.  */
272    
273    enum { RM1=1, RM2,  RM3,  RM4,  RM5,  RM6,  RM7,  RM8,  RM9,  RM10,
274           RM11,  RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,
275           RM21,  RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
276           RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
277           RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
278           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. */  versions and production versions. Note that the "rw" argument of RMATCH isn't
283    actually used in this definition. */
284    
285  #ifndef NO_RECURSE  #ifndef NO_RECURSE
286  #define REGISTER register  #define REGISTER register
287  #ifdef DEBUG  
288  #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg) \  #ifdef PCRE_DEBUG
289    #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    rx = match(ra,rb,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 210  versions and production versions. */ Line 298  versions and production versions. */
298    return ra; \    return ra; \
299    }    }
300  #else  #else
301  #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg) \  #define RMATCH(ra,rb,rc,rd,re,rw) \
302    rx = match(ra,rb,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    
306  #else  #else
307    
308    
309  /* These versions of the macros manage a private stack on the heap. Note  /* These versions of the macros manage a private stack on the heap. Note that
310  that the rd argument of RMATCH isn't actually used. It's the md argument of  the "rd" argument of RMATCH isn't actually used in this definition. It's the md
311  match(), which never changes. */  argument of match(), which never changes. */
312    
313  #define REGISTER  #define REGISTER
314    
315  #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg)\  #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 (setjmp(frame->Xwhere) == 0)\    if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\
319      {\    frame->Xwhere = rw; \
320      newframe->Xeptr = ra;\    newframe->Xeptr = ra;\
321      newframe->Xecode = rb;\    newframe->Xecode = rb;\
322      newframe->Xoffset_top = rc;\    newframe->Xmstart = mstart;\
323      newframe->Xims = re;\    newframe->Xmarkptr = markptr;\
324      newframe->Xeptrb = rf;\    newframe->Xoffset_top = rc;\
325      newframe->Xflags = rg;\    newframe->Xeptrb = re;\
326      newframe->Xrdepth = frame->Xrdepth + 1;\    newframe->Xrdepth = frame->Xrdepth + 1;\
327      newframe->Xprevframe = frame;\    newframe->Xprevframe = frame;\
328      frame = newframe;\    frame = newframe;\
329      DPRINTF(("restarting from line %d\n", __LINE__));\    DPRINTF(("restarting from line %d\n", __LINE__));\
330      goto HEAP_RECURSE;\    goto HEAP_RECURSE;\
331      }\    L_##rw:\
332    else\    DPRINTF(("jumped back to line %d\n", __LINE__));\
     {\  
     DPRINTF(("longjumped back to line %d\n", __LINE__));\  
     frame = md->thisframe;\  
     rx = frame->Xresult;\  
     }\  
333    }    }
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      frame->Xresult = ra;\      rrc = ra;\
343      md->thisframe = frame;\      goto HEAP_RETURN;\
     longjmp(frame->Xwhere, 1);\  
344      }\      }\
345    return ra;\    return ra;\
346    }    }
# Line 271  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      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 295  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 308  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 323  typedef struct heapframe { Line 406  typedef struct heapframe {
406    
407    eptrblock Xnewptrb;    eptrblock Xnewptrb;
408    
409    /* Place to pass back result, and where to jump back to */    /* Where to jump back to */
410    
411    int  Xresult;    int Xwhere;
   jmp_buf Xwhere;  
412    
413  } heapframe;  } heapframe;
414    
# Line 344  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  Performance note: It might be tempting to extract commonly used fields from the  #define SCHECK_PARTIAL()\
447  md structure (e.g. utf8, end_subject) into individual variables to improve    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
455    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    
459  Arguments:  Arguments:
460     eptr        pointer to current character in subject     eptr        pointer to current character in subject
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
463                     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  
                  match_tail_recursed - this is a tail_recursed group  
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,  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 387  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 394  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 */
507    
508  frame->Xeptr = eptr;  frame->Xeptr = eptr;
509  frame->Xecode = ecode;  frame->Xecode = ecode;
510    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 415  HEAP_RECURSE: Line 521  HEAP_RECURSE:
521    
522  #define eptr               frame->Xeptr  #define eptr               frame->Xeptr
523  #define ecode              frame->Xecode  #define ecode              frame->Xecode
524    #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 427  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 439  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 477  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 507  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 521  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 540  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. */
669    
 /* First check that we haven't called match() too many times, or that we  
 haven't exceeded the recursive call limit. */  
   
 if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);  
 if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);  
   
 original_ims = ims;    /* Save for resetting on ')' */  
   
670  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
671  utf8 = md->utf8;       /* Local copy of the flag */  utf8 = md->utf8;       /* Local copy of the flag */
672  #else  #else
673  utf8 = FALSE;  utf8 = FALSE;
674  #endif  #endif
675    
676    /* First check that we haven't called match() too many times, or that we
677    haven't exceeded the recursive call limit. */
678    
679    if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
680    if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
681    
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. If this  
687  is a tail recursion, use a block from the workspace, as the one on the stack is  When MATCH_CBEGROUP is set, add the current subject pointer to the chain of
688  already used. */  such remembered pointers, to be checked when we hit the closing ket, in order
689    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    eptrblock *p;    newptrb.epb_saved_eptr = eptr;
697    if ((flags & match_tail_recursed) != 0)    newptrb.epb_prev = eptrb;
698      {    eptrb = &newptrb;
699      if (md->eptrn >= EPTR_WORK_SIZE) RRETURN(PCRE_ERROR_NULLWSLIMIT);    md->match_function_type = 0;
     p = md->eptrchain + md->eptrn++;  
     }  
   else p = &newptrb;  
   p->epb_saved_eptr = eptr;  
   p->epb_prev = eptrb;  
   eptrb = p;  
700    }    }
701    
702  /* Now start processing the opcodes. */  /* Now start processing the opcodes. */
# Line 587  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 > md->start_match)  
     md->hitend = TRUE;  
   
709    switch(op)    switch(op)
710      {      {
711      /* Handle a capturing bracket. If there is space in the offset vector, save      case OP_MARK:
712      the current subject position in the working slot at the top of the vector.      markptr = ecode + 2;
713      We mustn't change the current values of the data slot, because they may be      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
714      set from a previous iteration of this group, and be referred to by a        eptrb, RM55);
715      reference inside the group.  
716        /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
717      If the bracket fails to match, we need to restore this value and also the      argument, and we must check whether that argument matches this MARK's
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:
734        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:
750        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
751          eptrb, RM51);
752        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);
761    
762        /* SKIP overrides PRUNE and THEN */
763    
764        case OP_SKIP:
765        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
766          eptrb, RM53);
767        if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
768          RRETURN(rrc);
769        md->start_match_ptr = eptr;   /* Pass back current position */
770        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:
792        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
793          eptrb, RM54);
794        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
795        md->start_match_ptr = ecode - GET(ecode, 1);
796        MRRETURN(MATCH_THEN);
797    
798        case OP_THEN_ARG:
799        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1+LINK_SIZE],
800          offset_top, md, eptrb, RM58);
801        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
802        md->start_match_ptr = ecode - GET(ecode, 1);
803        md->mark = ecode + LINK_SIZE + 2;
804        RRETURN(MATCH_THEN);
805    
806        /* 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 616  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 632  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(rrc, eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,          if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
845            ims, eptrb, flags);          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
846          if (rrc != MATCH_NOMATCH) 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 651  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    
865      /* Insufficient room for saving captured contents. Treat as a non-capturing      /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
866      bracket. */      as a non-capturing bracket. */
867    
868        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
869        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
870    
871      DPRINTF(("insufficient capture room: treat as non-capturing\n"));      DPRINTF(("insufficient capture room: treat as non-capturing\n"));
872    
873      /* Non-capturing bracket. Loop for all the alternatives. When we get to the      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
874      final alternative within the brackets, we would return the result of a      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
875      recursive call to match() whatever happened. We can reduce stack usage by  
876      turning this into a tail recursion. */      /* Non-capturing bracket, except for possessive with unlimited repeat. Loop
877        for all the alternatives. When we get to the final alternative within the
878        brackets, we used to return the result of a recursive call to match()
879        whatever happened so it was possible to reduce stack usage by turning this
880        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)        if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
890          {        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, eptrb,
891          ecode += _pcre_OP_lengths[*ecode];          RM2);
892          flags |= match_tail_recursed;        if (rrc != MATCH_NOMATCH &&
893          DPRINTF(("bracket 0 tail recursion\n"));            (rrc != MATCH_THEN || md->start_match_ptr != ecode))
894          goto TAIL_RECURSE;          RRETURN(rrc);
895          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        /* 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    #ifdef PCRE_DEBUG
919        printf("start possessive bracket %d\n", number);
920        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        RMATCH(rrc, eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,      DPRINTF(("insufficient capture room: treat as non-capturing\n"));
994          eptrb, flags);  
995        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
996        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
997    
998        /* Non-capturing possessive bracket with unlimited repeat. We come here
999        from BRAZERO with allow_zero = TRUE. The code is similar to the above,
1000        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(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        md->match_function_type = MATCH_CONDASSERT;
1228            match_condassert);        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)        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. If the second      we used to use tail recursion to avoid using another stack frame, except
1249      alternative doesn't exist, we can just plough on. */      when there was unlimited repeat of a possibly empty group. However, that
1250        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        flags = match_tail_recursed | ((op == OP_SCOND)? match_cbegroup : 0);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM49);
1259        goto TAIL_RECURSE;        if (rrc == MATCH_THEN && md->start_match_ptr == ecode)
1260            rrc = MATCH_NOMATCH;
1261          RRETURN(rrc);
1262        }        }
1263      else      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. If we are in a top-level recursion, we should      /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
1271      restore the offsets appropriately and continue from after the call. */      to close any currently open capturing brackets. */
1272    
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:
1299        case OP_ASSERT_ACCEPT:
1300      case OP_END:      case OP_END:
1301      if (md->recursive != NULL && md->recursive->group_num == 0)      if (md->recursive != NULL)
1302        {        {
1303        recursion_info *rec = md->recursive;        recursion_info *rec = md->recursive;
       DPRINTF(("End of pattern in a (?0) recursion\n"));  
1304        md->recursive = rec->prevrec;        md->recursive = rec->prevrec;
1305        memmove(md->offset_vector, rec->offset_save,        memmove(md->offset_vector, rec->offset_save,
1306          rec->saved_max * sizeof(int));          rec->saved_max * sizeof(int));
1307        md->start_match = rec->save_start;        offset_top = rec->save_offset_top;
1308        ims = original_ims;        if (rec->group_num == 0)
1309        ecode = rec->after_call;          {
1310        break;          ecode = rec->after_call;
1311            break;
1312            }
1313        }        }
1314    
1315      /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty      /* Otherwise, if we have matched an empty string, fail if not in an
1316      string - backtracking will then try other alternatives, if any. */      assertion and if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART
1317        is set and we have matched at the start of the subject. In both cases,
1318        backtracking will then try other alternatives, if any. */
1319    
1320        else if (eptr == mstart && op != OP_ASSERT_ACCEPT &&
1321            (md->notempty ||
1322              (md->notempty_atstart &&
1323                mstart == md->start_subject + md->start_offset)))
1324          MRRETURN(MATCH_NOMATCH);
1325    
1326        /* Otherwise, we have a match. */
1327    
1328        md->end_match_ptr = eptr;           /* Record where we ended */
1329        md->end_offset_top = offset_top;    /* and how many extracts were taken */
1330        md->start_match_ptr = mstart;       /* and the start (\K can modify) */
1331    
1332      if (md->notempty && eptr == md->start_match) RRETURN(MATCH_NOMATCH);      /* For some reason, the macros don't work properly if an expression is
1333      md->end_match_ptr = eptr;          /* Record where we ended */      given as the argument to MRRETURN when the heap is in use. */
1334      md->end_offset_top = offset_top;   /* and how many extracts were taken */  
1335      RRETURN(MATCH_MATCH);      rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
1336        MRRETURN(rrc);
     /* Change option settings */  
   
     case OP_OPT:  
     ims = ecode[1];  
     ecode += 2;  
     DPRINTF(("ims set to %02lx\n", ims));  
     break;  
1337    
1338      /* Assertion brackets. Check the alternative branches in turn - the      /* Assertion brackets. Check the alternative branches in turn - the
1339      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,
1340      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
1341      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
1342      this level is identical to the lookahead case. */      this level is identical to the lookahead case. When the assertion is part
1343        of a condition, we want to return immediately afterwards. The caller of
1344        this incarnation of the match() function will have set MATCH_CONDASSERT in
1345        md->match_function type, and one of these opcodes will be the first opcode
1346        that is processed. We use a local variable that is preserved over calls to
1347        match() to remember this case. */
1348    
1349      case OP_ASSERT:      case OP_ASSERT:
1350      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1351        if (md->match_function_type == MATCH_CONDASSERT)
1352          {
1353          condassert = TRUE;
1354          md->match_function_type = 0;
1355          }
1356        else condassert = FALSE;
1357    
1358      do      do
1359        {        {
1360        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
1361        if (rrc == MATCH_MATCH) break;        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1362        if (rrc != MATCH_NOMATCH) RRETURN(rrc);          {
1363            mstart = md->start_match_ptr;   /* In case \K reset it */
1364            break;
1365            }
1366          if (rrc != MATCH_NOMATCH &&
1367              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1368            RRETURN(rrc);
1369        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1370        }        }
1371      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1372      if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);  
1373        if (*ecode == OP_KET) MRRETURN(MATCH_NOMATCH);
1374    
1375      /* If checking an assertion for a condition, return MATCH_MATCH. */      /* If checking an assertion for a condition, return MATCH_MATCH. */
1376    
1377      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);
1378    
1379      /* Continue from after the assertion, updating the offsets high water      /* Continue from after the assertion, updating the offsets high water
1380      mark, since extracts may have been taken during the assertion. */      mark, since extracts may have been taken during the assertion. */
# Line 822  for (;;) Line 1384  for (;;)
1384      offset_top = md->end_offset_top;      offset_top = md->end_offset_top;
1385      continue;      continue;
1386    
1387      /* Negative assertion: all branches must fail to match */      /* Negative assertion: all branches must fail to match. Encountering SKIP,
1388        PRUNE, or COMMIT means we must assume failure without checking subsequent
1389        branches. */
1390    
1391      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1392      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1393        if (md->match_function_type == MATCH_CONDASSERT)
1394          {
1395          condassert = TRUE;
1396          md->match_function_type = 0;
1397          }
1398        else condassert = FALSE;
1399    
1400      do      do
1401        {        {
1402        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
1403        if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) MRRETURN(MATCH_NOMATCH);
1404        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc == MATCH_SKIP || rrc == MATCH_PRUNE || rrc == MATCH_COMMIT)
1405            {
1406            do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1407            break;
1408            }
1409          if (rrc != MATCH_NOMATCH &&
1410              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1411            RRETURN(rrc);
1412        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1413        }        }
1414      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1415    
1416      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);  /* Condition assertion */
1417    
1418      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1419      continue;      continue;
1420    
# Line 853  for (;;) Line 1431  for (;;)
1431        while (i-- > 0)        while (i-- > 0)
1432          {          {
1433          eptr--;          eptr--;
1434          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);          if (eptr < md->start_subject) MRRETURN(MATCH_NOMATCH);
1435          BACKCHAR(eptr)          BACKCHAR(eptr);
1436          }          }
1437        }        }
1438      else      else
# Line 864  for (;;) Line 1442  for (;;)
1442    
1443        {        {
1444        eptr -= GET(ecode, 1);        eptr -= GET(ecode, 1);
1445        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);        if (eptr < md->start_subject) MRRETURN(MATCH_NOMATCH);
1446        }        }
1447    
1448      /* Skip to next op code */      /* Save the earliest consulted character, then skip to next op code */
1449    
1450        if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
1451      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1452      break;      break;
1453    
# Line 884  for (;;) Line 1463  for (;;)
1463        cb.callout_number   = ecode[1];        cb.callout_number   = ecode[1];
1464        cb.offset_vector    = md->offset_vector;        cb.offset_vector    = md->offset_vector;
1465        cb.subject          = (PCRE_SPTR)md->start_subject;        cb.subject          = (PCRE_SPTR)md->start_subject;
1466        cb.subject_length   = md->end_subject - md->start_subject;        cb.subject_length   = (int)(md->end_subject - md->start_subject);
1467        cb.start_match      = md->start_match - md->start_subject;        cb.start_match      = (int)(mstart - md->start_subject);
1468        cb.current_position = eptr - md->start_subject;        cb.current_position = (int)(eptr - md->start_subject);
1469        cb.pattern_position = GET(ecode, 2);        cb.pattern_position = GET(ecode, 2);
1470        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
1471        cb.capture_top      = offset_top/2;        cb.capture_top      = offset_top/2;
1472        cb.capture_last     = md->capture_last;        cb.capture_last     = md->capture_last;
1473        cb.callout_data     = md->callout_data;        cb.callout_data     = md->callout_data;
1474        if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);        if ((rrc = (*pcre_callout)(&cb)) > 0) MRRETURN(MATCH_NOMATCH);
1475        if (rrc < 0) RRETURN(rrc);        if (rrc < 0) RRETURN(rrc);
1476        }        }
1477      ecode += 2 + 2*LINK_SIZE;      ecode += 2 + 2*LINK_SIZE;
# Line 947  for (;;) Line 1526  for (;;)
1526    
1527        memcpy(new_recursive.offset_save, md->offset_vector,        memcpy(new_recursive.offset_save, md->offset_vector,
1528              new_recursive.saved_max * sizeof(int));              new_recursive.saved_max * sizeof(int));
1529        new_recursive.save_start = md->start_match;        new_recursive.save_offset_top = offset_top;
1530        md->start_match = eptr;  
   
1531        /* 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
1532        restore the offset and recursion data. */        restore the offset and recursion data. */
1533    
1534        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1535        flags = (*callpat >= OP_SBRA)? match_cbegroup : 0;        cbegroup = (*callpat >= OP_SBRA);
1536        do        do
1537          {          {
1538          RMATCH(rrc, eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,          if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
1539            md, ims, eptrb, flags);          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,
1540          if (rrc == MATCH_MATCH)            md, eptrb, RM6);
1541            if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1542            {            {
1543            DPRINTF(("Recursion matched\n"));            DPRINTF(("Recursion matched\n"));
1544            md->recursive = new_recursive.prevrec;            md->recursive = new_recursive.prevrec;
1545            if (new_recursive.offset_save != stacksave)            if (new_recursive.offset_save != stacksave)
1546              (pcre_free)(new_recursive.offset_save);              (pcre_free)(new_recursive.offset_save);
1547            RRETURN(MATCH_MATCH);            MRRETURN(MATCH_MATCH);
1548            }            }
1549          else if (rrc != MATCH_NOMATCH)          else if (rrc != MATCH_NOMATCH &&
1550                    (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1551            {            {
1552            DPRINTF(("Recursion gave error %d\n", rrc));            DPRINTF(("Recursion gave error %d\n", rrc));
1553              if (new_recursive.offset_save != stacksave)
1554                (pcre_free)(new_recursive.offset_save);
1555            RRETURN(rrc);            RRETURN(rrc);
1556            }            }
1557    
# Line 984  for (;;) Line 1566  for (;;)
1566        md->recursive = new_recursive.prevrec;        md->recursive = new_recursive.prevrec;
1567        if (new_recursive.offset_save != stacksave)        if (new_recursive.offset_save != stacksave)
1568          (pcre_free)(new_recursive.offset_save);          (pcre_free)(new_recursive.offset_save);
1569        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
1570        }        }
1571      /* Control never reaches here */      /* Control never reaches here */
1572    
# Line 993  for (;;) Line 1575  for (;;)
1575      a move back into the brackets. Friedl calls these "atomic" subpatterns.      a move back into the brackets. Friedl calls these "atomic" subpatterns.
1576      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
1577      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
1578      the end of a normal bracket, leaving the subject pointer. */      the end of a normal bracket, leaving the subject pointer, but resetting
1579        the start-of-match value in case it was changed by \K. */
1580    
1581      case OP_ONCE:      case OP_ONCE:
1582      prev = ecode;      prev = ecode;
# Line 1001  for (;;) Line 1584  for (;;)
1584    
1585      do      do
1586        {        {
1587        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
1588          eptrb, 0);        if (rrc == MATCH_MATCH)  /* Note: _not_ MATCH_ACCEPT */
1589        if (rrc == MATCH_MATCH) break;          {
1590        if (rrc != MATCH_NOMATCH) RRETURN(rrc);          mstart = md->start_match_ptr;
1591            break;
1592            }
1593          if (rrc != MATCH_NOMATCH &&
1594              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1595            RRETURN(rrc);
1596        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1597        }        }
1598      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
# Line 1035  for (;;) Line 1623  for (;;)
1623    
1624      /* 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
1625      preceding bracket, in the appropriate order. The second "call" of match()      preceding bracket, in the appropriate order. The second "call" of match()
1626      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));  
       }  
1627    
1628      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1629        {        {
1630        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM8);
1631        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1632        ecode = prev;        ecode = prev;
       flags = match_tail_recursed;  
1633        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1634        }        }
1635      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1636        {        {
1637        RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_cbegroup);        md->match_function_type = MATCH_CBEGROUP;
1638          RMATCH(eptr, prev, offset_top, md, eptrb, RM9);
1639        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1640        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = match_tail_recursed;  
1641        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1642        }        }
1643      /* Control never gets here */      /* Control never gets here */
# Line 1070  for (;;) Line 1649  for (;;)
1649      do ecode += GET(ecode,1); while (*ecode == OP_ALT);      do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1650      break;      break;
1651    
1652      /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating      /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
1653      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
1654      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
1655      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
1656      preceded by BRAZERO or BRAMINZERO. */      optional ones preceded by BRAZERO or BRAMINZERO. */
1657    
1658      case OP_BRAZERO:      case OP_BRAZERO:
1659        {      next = ecode + 1;
1660        next = ecode+1;      RMATCH(eptr, next, offset_top, md, eptrb, RM10);
1661        RMATCH(rrc, eptr, next, offset_top, md, ims, eptrb, 0);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1662        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      do next += GET(next, 1); while (*next == OP_ALT);
1663        do next += GET(next,1); while (*next == OP_ALT);      ecode = next + 1 + LINK_SIZE;
       ecode = next + 1 + LINK_SIZE;  
       }  
1664      break;      break;
1665    
1666      case OP_BRAMINZERO:      case OP_BRAMINZERO:
1667        {      next = ecode + 1;
1668        next = ecode+1;      do next += GET(next, 1); while (*next == OP_ALT);
1669        do next += GET(next, 1); while (*next == OP_ALT);      RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
1670        RMATCH(rrc, eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1671        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      ecode++;
1672        ecode++;      break;
1673        }  
1674        case OP_SKIPZERO:
1675        next = ecode+1;
1676        do next += GET(next,1); while (*next == OP_ALT);
1677        ecode = next + 1 + LINK_SIZE;
1678      break;      break;
1679    
1680        /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
1681        here; just jump to the group, with allow_zero set TRUE. */
1682    
1683        case OP_BRAPOSZERO:
1684        op = *(++ecode);
1685        allow_zero = TRUE;
1686        if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
1687          goto POSSESSIVE_NON_CAPTURE;
1688    
1689      /* End of a group, repeated or non-repeating. */      /* End of a group, repeated or non-repeating. */
1690    
1691      case OP_KET:      case OP_KET:
1692      case OP_KETRMIN:      case OP_KETRMIN:
1693      case OP_KETRMAX:      case OP_KETRMAX:
1694        case OP_KETRPOS:
1695      prev = ecode - GET(ecode, 1);      prev = ecode - GET(ecode, 1);
1696    
1697      /* 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 1114  for (;;) Line 1705  for (;;)
1705        }        }
1706      else saved_eptr = NULL;      else saved_eptr = NULL;
1707    
1708      /* 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
1709      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
1710      assertions. Do this also for the "once" (atomic) groups. */      use by positive assertions. We also need to record the match start in case
1711        it was changed by \K. */
1712    
1713      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
1714          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||
# Line 1124  for (;;) Line 1716  for (;;)
1716        {        {
1717        md->end_match_ptr = eptr;      /* For ONCE */        md->end_match_ptr = eptr;      /* For ONCE */
1718        md->end_offset_top = offset_top;        md->end_offset_top = offset_top;
1719        RRETURN(MATCH_MATCH);        md->start_match_ptr = mstart;
1720          MRRETURN(MATCH_MATCH);
1721        }        }
1722    
1723      /* 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 1133  for (;;) Line 1726  for (;;)
1726      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
1727      when the OP_END is reached. Other recursion is handled here. */      when the OP_END is reached. Other recursion is handled here. */
1728    
1729      if (*prev == OP_CBRA || *prev == OP_SCBRA)      if (*prev == OP_CBRA || *prev == OP_SCBRA ||
1730            *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
1731        {        {
1732        number = GET2(prev, 1+LINK_SIZE);        number = GET2(prev, 1+LINK_SIZE);
1733        offset = number << 1;        offset = number << 1;
1734    
1735  #ifdef DEBUG  #ifdef PCRE_DEBUG
1736        printf("end bracket %d", number);        printf("end bracket %d", number);
1737        printf("\n");        printf("\n");
1738  #endif  #endif
# Line 1148  for (;;) Line 1742  for (;;)
1742          {          {
1743          md->offset_vector[offset] =          md->offset_vector[offset] =
1744            md->offset_vector[md->offset_end - number];            md->offset_vector[md->offset_end - number];
1745          md->offset_vector[offset+1] = eptr - md->start_subject;          md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1746          if (offset_top <= offset) offset_top = offset + 2;          if (offset_top <= offset) offset_top = offset + 2;
1747          }          }
1748    
# Line 1160  for (;;) Line 1754  for (;;)
1754          recursion_info *rec = md->recursive;          recursion_info *rec = md->recursive;
1755          DPRINTF(("Recursion (%d) succeeded - continuing\n", number));          DPRINTF(("Recursion (%d) succeeded - continuing\n", number));
1756          md->recursive = rec->prevrec;          md->recursive = rec->prevrec;
         md->start_match = rec->save_start;  
1757          memcpy(md->offset_vector, rec->offset_save,          memcpy(md->offset_vector, rec->offset_save,
1758            rec->saved_max * sizeof(int));            rec->saved_max * sizeof(int));
1759            offset_top = rec->save_offset_top;
1760          ecode = rec->after_call;          ecode = rec->after_call;
         ims = original_ims;  
1761          break;          break;
1762          }          }
1763        }        }
1764    
     /* 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));  
   
1765      /* For a non-repeating ket, just continue at this level. This also      /* For a non-repeating ket, just continue at this level. This also
1766      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.
1767      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 1186  for (;;) Line 1773  for (;;)
1773        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1774        break;        break;
1775        }        }
1776    
1777      /* The repeating kets try the rest of the pattern or restart from the      /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
1778      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
1779      tail recursion to avoid using another stack frame. */      at a time from the outer level, thus saving stack. */
1780    
1781      flags = (*prev >= OP_SBRA)? match_cbegroup : 0;      if (*ecode == OP_KETRPOS)
1782          {
1783          md->end_match_ptr = eptr;
1784          md->end_offset_top = offset_top;
1785          RRETURN(MATCH_KETRPOS);
1786          }
1787    
1788        /* The normal repeating kets try the rest of the pattern or restart from
1789        the preceding bracket, in the appropriate order. In the second case, we can
1790        use tail recursion to avoid using another stack frame, unless we have an
1791        unlimited repeat of a group that can match an empty string. */
1792    
1793      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1794        {        {
1795        RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
1796        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1797          if (*prev >= OP_SBRA)    /* Could match an empty string */
1798            {
1799            md->match_function_type = MATCH_CBEGROUP;
1800            RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
1801            RRETURN(rrc);
1802            }
1803        ecode = prev;        ecode = prev;
       flags |= match_tail_recursed;  
1804        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1805        }        }
1806      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1807        {        {
1808        RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, flags);        if (*prev >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1809          RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
1810        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1811        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = match_tail_recursed;  
1812        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1813        }        }
1814      /* Control never gets here */      /* Control never gets here */
1815    
1816      /* Start of subject unless notbol, or after internal newline if multiline */      /* Not multiline mode: start of subject assertion, unless notbol. */
1817    
1818      case OP_CIRC:      case OP_CIRC:
1819      if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);      if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
1820      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 */  
   
1821      /* Start of subject assertion */      /* Start of subject assertion */
1822    
1823      case OP_SOD:      case OP_SOD:
1824      if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);      if (eptr != md->start_subject) MRRETURN(MATCH_NOMATCH);
1825        ecode++;
1826        break;
1827    
1828        /* Multiline mode: start of subject unless notbol, or after any newline. */
1829    
1830        case OP_CIRCM:
1831        if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
1832        if (eptr != md->start_subject &&
1833            (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
1834          MRRETURN(MATCH_NOMATCH);
1835      ecode++;      ecode++;
1836      break;      break;
1837    
1838      /* Start of match assertion */      /* Start of match assertion */
1839    
1840      case OP_SOM:      case OP_SOM:
1841      if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);      if (eptr != md->start_subject + md->start_offset) MRRETURN(MATCH_NOMATCH);
1842      ecode++;      ecode++;
1843      break;      break;
1844    
1845      /* Assert before internal newline if multiline, or before a terminating      /* Reset the start of match point */
     newline unless endonly is set, else end of subject unless noteol is set. */  
1846    
1847      case OP_DOLL:      case OP_SET_SOM:
1848      if ((ims & PCRE_MULTILINE) != 0)      mstart = eptr;
1849        ecode++;
1850        break;
1851    
1852        /* Multiline mode: assert before any newline, or before end of subject
1853        unless noteol is set. */
1854    
1855        case OP_DOLLM:
1856        if (eptr < md->end_subject)
1857          { if (!IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH); }
1858        else
1859        {        {
1860        if (eptr < md->end_subject)        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1861          { if (!IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); }        SCHECK_PARTIAL();
       else  
         { if (md->noteol) RRETURN(MATCH_NOMATCH); }  
       ecode++;  
       break;  
       }  
     else  
       {  
       if (md->noteol) RRETURN(MATCH_NOMATCH);  
       if (!md->endonly)  
         {  
         if (eptr != md->end_subject &&  
             (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))  
           RRETURN(MATCH_NOMATCH);  
         ecode++;  
         break;  
         }  
1862        }        }
1863        ecode++;
1864        break;
1865    
1866        /* Not multiline mode: assert before a terminating newline or before end of
1867        subject unless noteol is set. */
1868    
1869        case OP_DOLL:
1870        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1871        if (!md->endonly) goto ASSERT_NL_OR_EOS;
1872    
1873      /* ... else fall through for endonly */      /* ... else fall through for endonly */
1874    
1875      /* End of subject assertion (\z) */      /* End of subject assertion (\z) */
1876    
1877      case OP_EOD:      case OP_EOD:
1878      if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr < md->end_subject) MRRETURN(MATCH_NOMATCH);
1879        SCHECK_PARTIAL();
1880      ecode++;      ecode++;
1881      break;      break;
1882    
1883      /* End of subject or ending \n assertion (\Z) */      /* End of subject or ending \n assertion (\Z) */
1884    
1885      case OP_EODN:      case OP_EODN:
1886      if (eptr != md->end_subject &&      ASSERT_NL_OR_EOS:
1887        if (eptr < md->end_subject &&
1888          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
1889        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
1890    
1891        /* Either at end of string or \n before end. */
1892    
1893        SCHECK_PARTIAL();
1894      ecode++;      ecode++;
1895      break;      break;
1896    
# Line 1290  for (;;) Line 1902  for (;;)
1902    
1903        /* Find out if the previous and current characters are "word" characters.        /* Find out if the previous and current characters are "word" characters.
1904        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
1905        be "non-word" characters. */        be "non-word" characters. Remember the earliest consulted character for
1906          partial matching. */
1907    
1908  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1909        if (utf8)        if (utf8)
1910          {          {
1911            /* Get status of previous character */
1912    
1913          if (eptr == md->start_subject) prev_is_word = FALSE; else          if (eptr == md->start_subject) prev_is_word = FALSE; else
1914            {            {
1915            const uschar *lastptr = eptr - 1;            USPTR lastptr = eptr - 1;
1916            while((*lastptr & 0xc0) == 0x80) lastptr--;            while((*lastptr & 0xc0) == 0x80) lastptr--;
1917              if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
1918            GETCHAR(c, lastptr);            GETCHAR(c, lastptr);
1919    #ifdef SUPPORT_UCP
1920              if (md->use_ucp)
1921                {
1922                if (c == '_') prev_is_word = TRUE; else
1923                  {
1924                  int cat = UCD_CATEGORY(c);
1925                  prev_is_word = (cat == ucp_L || cat == ucp_N);
1926                  }
1927                }
1928              else
1929    #endif
1930            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
1931            }            }
1932          if (eptr >= md->end_subject) cur_is_word = FALSE; else  
1933            /* Get status of next character */
1934    
1935            if (eptr >= md->end_subject)
1936              {
1937              SCHECK_PARTIAL();
1938              cur_is_word = FALSE;
1939              }
1940            else
1941            {            {
1942            GETCHAR(c, eptr);            GETCHAR(c, eptr);
1943    #ifdef SUPPORT_UCP
1944              if (md->use_ucp)
1945                {
1946                if (c == '_') cur_is_word = TRUE; else
1947                  {
1948                  int cat = UCD_CATEGORY(c);
1949                  cur_is_word = (cat == ucp_L || cat == ucp_N);
1950                  }
1951                }
1952              else
1953    #endif
1954            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
1955            }            }
1956          }          }
1957        else        else
1958  #endif  #endif
1959    
1960        /* More streamlined when not in UTF-8 mode */        /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for
1961          consistency with the behaviour of \w we do use it in this case. */
1962    
1963          {          {
1964          prev_is_word = (eptr != md->start_subject) &&          /* Get status of previous character */
1965            ((md->ctypes[eptr[-1]] & ctype_word) != 0);  
1966          cur_is_word = (eptr < md->end_subject) &&          if (eptr == md->start_subject) prev_is_word = FALSE; else
1967            ((md->ctypes[*eptr] & ctype_word) != 0);            {
1968              if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
1969    #ifdef SUPPORT_UCP
1970              if (md->use_ucp)
1971                {
1972                c = eptr[-1];
1973                if (c == '_') prev_is_word = TRUE; else
1974                  {
1975                  int cat = UCD_CATEGORY(c);
1976                  prev_is_word = (cat == ucp_L || cat == ucp_N);
1977                  }
1978                }
1979              else
1980    #endif
1981              prev_is_word = ((md->ctypes[eptr[-1]] & ctype_word) != 0);
1982              }
1983    
1984            /* Get status of next character */
1985    
1986            if (eptr >= md->end_subject)
1987              {
1988              SCHECK_PARTIAL();
1989              cur_is_word = FALSE;
1990              }
1991            else
1992    #ifdef SUPPORT_UCP
1993            if (md->use_ucp)
1994              {
1995              c = *eptr;
1996              if (c == '_') cur_is_word = TRUE; else
1997                {
1998                int cat = UCD_CATEGORY(c);
1999                cur_is_word = (cat == ucp_L || cat == ucp_N);
2000                }
2001              }
2002            else
2003    #endif
2004            cur_is_word = ((md->ctypes[*eptr] & ctype_word) != 0);
2005          }          }
2006    
2007        /* Now see if the situation is what we want */        /* Now see if the situation is what we want */
2008    
2009        if ((*ecode++ == OP_WORD_BOUNDARY)?        if ((*ecode++ == OP_WORD_BOUNDARY)?
2010             cur_is_word == prev_is_word : cur_is_word != prev_is_word)             cur_is_word == prev_is_word : cur_is_word != prev_is_word)
2011          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2012        }        }
2013      break;      break;
2014    
2015      /* Match a single character type; inline for speed */      /* Match a single character type; inline for speed */
2016    
2017      case OP_ANY:      case OP_ANY:
2018      if ((ims & PCRE_DOTALL) == 0)      if (IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH);
2019        /* Fall through */
2020    
2021        case OP_ALLANY:
2022        if (eptr++ >= md->end_subject)
2023        {        {
2024        if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);        SCHECK_PARTIAL();
2025          MRRETURN(MATCH_NOMATCH);
2026        }        }
2027      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++;  
2028      ecode++;      ecode++;
2029      break;      break;
2030    
# Line 1345  for (;;) Line 2032  for (;;)
2032      any byte, even newline, independent of the setting of PCRE_DOTALL. */      any byte, even newline, independent of the setting of PCRE_DOTALL. */
2033    
2034      case OP_ANYBYTE:      case OP_ANYBYTE:
2035      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr++ >= md->end_subject)
2036          {
2037          SCHECK_PARTIAL();
2038          MRRETURN(MATCH_NOMATCH);
2039          }
2040      ecode++;      ecode++;
2041      break;      break;
2042    
2043      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2044      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2045          {
2046          SCHECK_PARTIAL();
2047          MRRETURN(MATCH_NOMATCH);
2048          }
2049      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2050      if (      if (
2051  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1358  for (;;) Line 2053  for (;;)
2053  #endif  #endif
2054         (md->ctypes[c] & ctype_digit) != 0         (md->ctypes[c] & ctype_digit) != 0
2055         )         )
2056        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2057      ecode++;      ecode++;
2058      break;      break;
2059    
2060      case OP_DIGIT:      case OP_DIGIT:
2061      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2062          {
2063          SCHECK_PARTIAL();
2064          MRRETURN(MATCH_NOMATCH);
2065          }
2066      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2067      if (      if (
2068  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1371  for (;;) Line 2070  for (;;)
2070  #endif  #endif
2071         (md->ctypes[c] & ctype_digit) == 0         (md->ctypes[c] & ctype_digit) == 0
2072         )         )
2073        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2074      ecode++;      ecode++;
2075      break;      break;
2076    
2077      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2078      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2079          {
2080          SCHECK_PARTIAL();
2081          MRRETURN(MATCH_NOMATCH);
2082          }
2083      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2084      if (      if (
2085  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1384  for (;;) Line 2087  for (;;)
2087  #endif  #endif
2088         (md->ctypes[c] & ctype_space) != 0         (md->ctypes[c] & ctype_space) != 0
2089         )         )
2090        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2091      ecode++;      ecode++;
2092      break;      break;
2093    
2094      case OP_WHITESPACE:      case OP_WHITESPACE:
2095      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2096          {
2097          SCHECK_PARTIAL();
2098          MRRETURN(MATCH_NOMATCH);
2099          }
2100      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2101      if (      if (
2102  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1397  for (;;) Line 2104  for (;;)
2104  #endif  #endif
2105         (md->ctypes[c] & ctype_space) == 0         (md->ctypes[c] & ctype_space) == 0
2106         )         )
2107        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2108      ecode++;      ecode++;
2109      break;      break;
2110    
2111      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2112      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2113          {
2114          SCHECK_PARTIAL();
2115          MRRETURN(MATCH_NOMATCH);
2116          }
2117      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2118      if (      if (
2119  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1410  for (;;) Line 2121  for (;;)
2121  #endif  #endif
2122         (md->ctypes[c] & ctype_word) != 0         (md->ctypes[c] & ctype_word) != 0
2123         )         )
2124        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2125      ecode++;      ecode++;
2126      break;      break;
2127    
2128      case OP_WORDCHAR:      case OP_WORDCHAR:
2129      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2130          {
2131          SCHECK_PARTIAL();
2132          MRRETURN(MATCH_NOMATCH);
2133          }
2134      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2135      if (      if (
2136  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1423  for (;;) Line 2138  for (;;)
2138  #endif  #endif
2139         (md->ctypes[c] & ctype_word) == 0         (md->ctypes[c] & ctype_word) == 0
2140         )         )
2141        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2142      ecode++;      ecode++;
2143      break;      break;
2144    
2145      case OP_ANYNL:      case OP_ANYNL:
2146      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2147          {
2148          SCHECK_PARTIAL();
2149          MRRETURN(MATCH_NOMATCH);
2150          }
2151      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2152      switch(c)      switch(c)
2153        {        {
2154        default: RRETURN(MATCH_NOMATCH);        default: MRRETURN(MATCH_NOMATCH);
2155    
2156        case 0x000d:        case 0x000d:
2157        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
2158        break;        break;
2159    
2160        case 0x000a:        case 0x000a:
2161          break;
2162    
2163        case 0x000b:        case 0x000b:
2164        case 0x000c:        case 0x000c:
2165        case 0x0085:        case 0x0085:
2166        case 0x2028:        case 0x2028:
2167        case 0x2029:        case 0x2029:
2168          if (md->bsr_anycrlf) MRRETURN(MATCH_NOMATCH);
2169          break;
2170          }
2171        ecode++;
2172        break;
2173    
2174        case OP_NOT_HSPACE:
2175        if (eptr >= md->end_subject)
2176          {
2177          SCHECK_PARTIAL();
2178          MRRETURN(MATCH_NOMATCH);
2179          }
2180        GETCHARINCTEST(c, eptr);
2181        switch(c)
2182          {
2183          default: break;
2184          case 0x09:      /* HT */
2185          case 0x20:      /* SPACE */
2186          case 0xa0:      /* NBSP */
2187          case 0x1680:    /* OGHAM SPACE MARK */
2188          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
2189          case 0x2000:    /* EN QUAD */
2190          case 0x2001:    /* EM QUAD */
2191          case 0x2002:    /* EN SPACE */
2192          case 0x2003:    /* EM SPACE */
2193          case 0x2004:    /* THREE-PER-EM SPACE */
2194          case 0x2005:    /* FOUR-PER-EM SPACE */
2195          case 0x2006:    /* SIX-PER-EM SPACE */
2196          case 0x2007:    /* FIGURE SPACE */
2197          case 0x2008:    /* PUNCTUATION SPACE */
2198          case 0x2009:    /* THIN SPACE */
2199          case 0x200A:    /* HAIR SPACE */
2200          case 0x202f:    /* NARROW NO-BREAK SPACE */
2201          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
2202          case 0x3000:    /* IDEOGRAPHIC SPACE */
2203          MRRETURN(MATCH_NOMATCH);
2204          }
2205        ecode++;
2206        break;
2207    
2208        case OP_HSPACE:
2209        if (eptr >= md->end_subject)
2210          {
2211          SCHECK_PARTIAL();
2212          MRRETURN(MATCH_NOMATCH);
2213          }
2214        GETCHARINCTEST(c, eptr);
2215        switch(c)
2216          {
2217          default: MRRETURN(MATCH_NOMATCH);
2218          case 0x09:      /* HT */
2219          case 0x20:      /* SPACE */
2220          case 0xa0:      /* NBSP */
2221          case 0x1680:    /* OGHAM SPACE MARK */
2222          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
2223          case 0x2000:    /* EN QUAD */
2224          case 0x2001:    /* EM QUAD */
2225          case 0x2002:    /* EN SPACE */
2226          case 0x2003:    /* EM SPACE */
2227          case 0x2004:    /* THREE-PER-EM SPACE */
2228          case 0x2005:    /* FOUR-PER-EM SPACE */
2229          case 0x2006:    /* SIX-PER-EM SPACE */
2230          case 0x2007:    /* FIGURE SPACE */
2231          case 0x2008:    /* PUNCTUATION SPACE */
2232          case 0x2009:    /* THIN SPACE */
2233          case 0x200A:    /* HAIR SPACE */
2234          case 0x202f:    /* NARROW NO-BREAK SPACE */
2235          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
2236          case 0x3000:    /* IDEOGRAPHIC SPACE */
2237          break;
2238          }
2239        ecode++;
2240        break;
2241    
2242        case OP_NOT_VSPACE:
2243        if (eptr >= md->end_subject)
2244          {
2245          SCHECK_PARTIAL();
2246          MRRETURN(MATCH_NOMATCH);
2247          }
2248        GETCHARINCTEST(c, eptr);
2249        switch(c)
2250          {
2251          default: break;
2252          case 0x0a:      /* LF */
2253          case 0x0b:      /* VT */
2254          case 0x0c:      /* FF */
2255          case 0x0d:      /* CR */
2256          case 0x85:      /* NEL */
2257          case 0x2028:    /* LINE SEPARATOR */
2258          case 0x2029:    /* PARAGRAPH SEPARATOR */
2259          MRRETURN(MATCH_NOMATCH);
2260          }
2261        ecode++;
2262        break;
2263    
2264        case OP_VSPACE:
2265        if (eptr >= md->end_subject)
2266          {
2267          SCHECK_PARTIAL();
2268          MRRETURN(MATCH_NOMATCH);
2269          }
2270        GETCHARINCTEST(c, eptr);
2271        switch(c)
2272          {
2273          default: MRRETURN(MATCH_NOMATCH);
2274          case 0x0a:      /* LF */
2275          case 0x0b:      /* VT */
2276          case 0x0c:      /* FF */
2277          case 0x0d:      /* CR */
2278          case 0x85:      /* NEL */
2279          case 0x2028:    /* LINE SEPARATOR */
2280          case 0x2029:    /* PARAGRAPH SEPARATOR */
2281        break;        break;
2282        }        }
2283      ecode++;      ecode++;
# Line 1453  for (;;) Line 2289  for (;;)
2289    
2290      case OP_PROP:      case OP_PROP:
2291      case OP_NOTPROP:      case OP_NOTPROP:
2292      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2293          {
2294          SCHECK_PARTIAL();
2295          MRRETURN(MATCH_NOMATCH);
2296          }
2297      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2298        {        {
2299        int chartype, script;        const ucd_record *prop = GET_UCD(c);
       int category = _pcre_ucp_findprop(c, &chartype, &script);  
2300    
2301        switch(ecode[1])        switch(ecode[1])
2302          {          {
2303          case PT_ANY:          case PT_ANY:
2304          if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);          if (op == OP_NOTPROP) MRRETURN(MATCH_NOMATCH);
2305          break;          break;
2306    
2307          case PT_LAMP:          case PT_LAMP:
2308          if ((chartype == ucp_Lu ||          if ((prop->chartype == ucp_Lu ||
2309               chartype == ucp_Ll ||               prop->chartype == ucp_Ll ||
2310               chartype == ucp_Lt) == (op == OP_NOTPROP))               prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
2311            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2312           break;          break;
2313    
2314          case PT_GC:          case PT_GC:
2315          if ((ecode[2] != category) == (op == OP_PROP))          if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))
2316            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2317          break;          break;
2318    
2319          case PT_PC:          case PT_PC:
2320          if ((ecode[2] != chartype) == (op == OP_PROP))          if ((ecode[2] != prop->chartype) == (op == OP_PROP))
2321            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2322          break;          break;
2323    
2324          case PT_SC:          case PT_SC:
2325          if ((ecode[2] != script) == (op == OP_PROP))          if ((ecode[2] != prop->script) == (op == OP_PROP))
2326            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2327            break;
2328    
2329            /* These are specials */
2330    
2331            case PT_ALNUM:
2332            if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2333                 _pcre_ucp_gentype[prop->chartype] == ucp_N) == (op == OP_NOTPROP))
2334              MRRETURN(MATCH_NOMATCH);
2335            break;
2336    
2337            case PT_SPACE:    /* Perl space */
2338            if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2339                 c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2340                   == (op == OP_NOTPROP))
2341              MRRETURN(MATCH_NOMATCH);
2342            break;
2343    
2344            case PT_PXSPACE:  /* POSIX space */
2345            if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2346                 c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2347                 c == CHAR_FF || c == CHAR_CR)
2348                   == (op == OP_NOTPROP))
2349              MRRETURN(MATCH_NOMATCH);
2350            break;
2351    
2352            case PT_WORD:
2353            if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2354                 _pcre_ucp_gentype[prop->chartype] == ucp_N ||
2355                 c == CHAR_UNDERSCORE) == (op == OP_NOTPROP))
2356              MRRETURN(MATCH_NOMATCH);
2357          break;          break;
2358    
2359            /* This should never occur */
2360    
2361          default:          default:
2362          RRETURN(PCRE_ERROR_INTERNAL);          RRETURN(PCRE_ERROR_INTERNAL);
2363          }          }
# Line 1499  for (;;) Line 2370  for (;;)
2370      is in the binary; otherwise a compile-time error occurs. */      is in the binary; otherwise a compile-time error occurs. */
2371    
2372      case OP_EXTUNI:      case OP_EXTUNI:
2373      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2374          {
2375          SCHECK_PARTIAL();
2376          MRRETURN(MATCH_NOMATCH);
2377          }
2378      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2379        {        {
2380        int chartype, script;        int category = UCD_CATEGORY(c);
2381        int category = _pcre_ucp_findprop(c, &chartype, &script);        if (category == ucp_M) MRRETURN(MATCH_NOMATCH);
       if (category == ucp_M) RRETURN(MATCH_NOMATCH);  
2382        while (eptr < md->end_subject)        while (eptr < md->end_subject)
2383          {          {
2384          int len = 1;          int len = 1;
# Line 1512  for (;;) Line 2386  for (;;)
2386            {            {
2387            GETCHARLEN(c, eptr, len);            GETCHARLEN(c, eptr, len);
2388            }            }
2389          category = _pcre_ucp_findprop(c, &chartype, &script);          category = UCD_CATEGORY(c);
2390          if (category != ucp_M) break;          if (category != ucp_M) break;
2391          eptr += len;          eptr += len;
2392          }          }
# Line 1531  for (;;) Line 2405  for (;;)
2405      loops). */      loops). */
2406    
2407      case OP_REF:      case OP_REF:
2408        {      case OP_REFI:
2409        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */      caseless = op == OP_REFI;
2410        ecode += 3;                                 /* Advance past item */      offset = GET2(ecode, 1) << 1;               /* Doubled ref number */
2411        ecode += 3;
2412    
2413        /* 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];  
2414    
2415        /* Set up for repetition, or handle the non-repeated case */      (a) In the default, Perl-compatible state, set the length negative;
2416        this ensures that every attempt at a match fails. We can't just fail
2417        here, because of the possibility of quantifiers with zero minima.
2418    
2419        switch (*ecode)      (b) If the JavaScript compatibility flag is set, set the length to zero
2420          {      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;  
2421    
2422          case OP_CRRANGE:      Otherwise, set the length to the length of what was matched by the
2423          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;  
2424    
2425          default:               /* No repeat follows */      if (offset >= offset_top || md->offset_vector[offset] < 0)
2426          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);        length = (md->jscript_compat)? 0 : -1;
2427          eptr += length;      else
2428          continue;              /* With the main loop */        length = md->offset_vector[offset+1] - md->offset_vector[offset];
2429    
2430        /* Set up for repetition, or handle the non-repeated case */
2431    
2432        switch (*ecode)
2433          {
2434          case OP_CRSTAR:
2435          case OP_CRMINSTAR:
2436          case OP_CRPLUS:
2437          case OP_CRMINPLUS:
2438          case OP_CRQUERY:
2439          case OP_CRMINQUERY:
2440          c = *ecode++ - OP_CRSTAR;
2441          minimize = (c & 1) != 0;
2442          min = rep_min[c];                 /* Pick up values from tables; */
2443          max = rep_max[c];                 /* zero for max => infinity */
2444          if (max == 0) max = INT_MAX;
2445          break;
2446    
2447          case OP_CRRANGE:
2448          case OP_CRMINRANGE:
2449          minimize = (*ecode == OP_CRMINRANGE);
2450          min = GET2(ecode, 1);
2451          max = GET2(ecode, 3);
2452          if (max == 0) max = INT_MAX;
2453          ecode += 5;
2454          break;
2455    
2456          default:               /* No repeat follows */
2457          if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
2458            {
2459            CHECK_PARTIAL();
2460            MRRETURN(MATCH_NOMATCH);
2461          }          }
2462          eptr += length;
2463          continue;              /* With the main loop */
2464          }
2465    
2466        /* If the length of the reference is zero, just continue with the      /* Handle repeated back references. If the length of the reference is
2467        main loop. */      zero, just continue with the main loop. */
2468    
2469        if (length == 0) continue;      if (length == 0) continue;
2470    
2471        /* First, ensure the minimum number of matches are present. We get back      /* First, ensure the minimum number of matches are present. We get back
2472        the length of the reference string explicitly rather than passing the      the length of the reference string explicitly rather than passing the
2473        address of eptr, so that eptr can be a register variable. */      address of eptr, so that eptr can be a register variable. */
2474    
2475        for (i = 1; i <= min; i++)      for (i = 1; i <= min; i++)
2476          {
2477          int slength;
2478          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2479          {          {
2480          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);          CHECK_PARTIAL();
2481          eptr += length;          MRRETURN(MATCH_NOMATCH);
2482          }          }
2483          eptr += slength;
2484          }
2485    
2486        /* If min = max, continue at the same level without recursion.      /* If min = max, continue at the same level without recursion.
2487        They are not both allowed to be zero. */      They are not both allowed to be zero. */
2488    
2489        if (min == max) continue;      if (min == max) continue;
2490    
2491        /* If minimizing, keep trying and advancing the pointer */      /* If minimizing, keep trying and advancing the pointer */
2492    
2493        if (minimize)      if (minimize)
2494          {
2495          for (fi = min;; fi++)
2496          {          {
2497          for (fi = min;; fi++)          int slength;
2498            RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
2499            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2500            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2501            if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2502            {            {
2503            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            CHECK_PARTIAL();
2504            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            MRRETURN(MATCH_NOMATCH);
           if (fi >= max || !match_ref(offset, eptr, length, md, ims))  
             RRETURN(MATCH_NOMATCH);  
           eptr += length;  
2505            }            }
2506          /* Control never gets here */          eptr += slength;
2507          }          }
2508          /* Control never gets here */
2509          }
2510    
2511        /* If maximizing, find the longest string and work backwards */      /* If maximizing, find the longest string and work backwards */
2512    
2513        else      else
2514          {
2515          pp = eptr;
2516          for (i = min; i < max; i++)
2517          {          {
2518          pp = eptr;          int slength;
2519          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)  
2520            {            {
2521            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            CHECK_PARTIAL();
2522            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            break;
           eptr -= length;  
2523            }            }
2524          RRETURN(MATCH_NOMATCH);          eptr += slength;
2525            }
2526          while (eptr >= pp)
2527            {
2528            RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
2529            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2530            eptr -= length;
2531          }          }
2532          MRRETURN(MATCH_NOMATCH);
2533        }        }
2534      /* Control never gets here */      /* Control never gets here */
2535    
   
   
2536      /* Match a bit-mapped character class, possibly repeatedly. This op code is      /* Match a bit-mapped character class, possibly repeatedly. This op code is
2537      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,
2538      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 1688  for (;;) Line 2587  for (;;)
2587          {          {
2588          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2589            {            {
2590            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2591                {
2592                SCHECK_PARTIAL();
2593                MRRETURN(MATCH_NOMATCH);
2594                }
2595            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
2596            if (c > 255)            if (c > 255)
2597              {              {
2598              if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);              if (op == OP_CLASS) MRRETURN(MATCH_NOMATCH);
2599              }              }
2600            else            else
2601              {              {
2602              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);              if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2603              }              }
2604            }            }
2605          }          }
# Line 1706  for (;;) Line 2609  for (;;)
2609          {          {
2610          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2611            {            {
2612            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2613                {
2614                SCHECK_PARTIAL();
2615                MRRETURN(MATCH_NOMATCH);
2616                }
2617            c = *eptr++;            c = *eptr++;
2618            if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);            if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2619            }            }
2620          }          }
2621    
# Line 1728  for (;;) Line 2635  for (;;)
2635            {            {
2636            for (fi = min;; fi++)            for (fi = min;; fi++)
2637              {              {
2638              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
2639              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2640              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2641                if (eptr >= md->end_subject)
2642                  {
2643                  SCHECK_PARTIAL();
2644                  MRRETURN(MATCH_NOMATCH);
2645                  }
2646              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
2647              if (c > 255)              if (c > 255)
2648                {                {
2649                if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);                if (op == OP_CLASS) MRRETURN(MATCH_NOMATCH);
2650                }                }
2651              else              else
2652                {                {
2653                if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);                if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2654                }                }
2655              }              }
2656            }            }
# Line 1748  for (;;) Line 2660  for (;;)
2660            {            {
2661            for (fi = min;; fi++)            for (fi = min;; fi++)
2662              {              {
2663              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
2664              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2665              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2666                if (eptr >= md->end_subject)
2667                  {
2668                  SCHECK_PARTIAL();
2669                  MRRETURN(MATCH_NOMATCH);
2670                  }
2671              c = *eptr++;              c = *eptr++;
2672              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);              if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2673              }              }
2674            }            }
2675          /* Control never gets here */          /* Control never gets here */
# Line 1771  for (;;) Line 2688  for (;;)
2688            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2689              {              {
2690              int len = 1;              int len = 1;
2691              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
2692                  {
2693                  SCHECK_PARTIAL();
2694                  break;
2695                  }
2696              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
2697              if (c > 255)              if (c > 255)
2698                {                {
# Line 1785  for (;;) Line 2706  for (;;)
2706              }              }
2707            for (;;)            for (;;)
2708              {              {
2709              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
2710              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2711              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2712              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 1797  for (;;) Line 2718  for (;;)
2718            {            {
2719            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2720              {              {
2721              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
2722                  {
2723                  SCHECK_PARTIAL();
2724                  break;
2725                  }
2726              c = *eptr;              c = *eptr;
2727              if ((data[c/8] & (1 << (c&7))) == 0) break;              if ((data[c/8] & (1 << (c&7))) == 0) break;
2728              eptr++;              eptr++;
2729              }              }
2730            while (eptr >= pp)            while (eptr >= pp)
2731              {              {
2732              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM19);
2733              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2734              eptr--;              eptr--;
2735              }              }
2736            }            }
2737    
2738          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2739          }          }
2740        }        }
2741      /* Control never gets here */      /* Control never gets here */
2742    
2743    
2744      /* Match an extended character class. This opcode is encountered only      /* Match an extended character class. This opcode is encountered only
2745      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
2746        mode, because Unicode properties are supported in non-UTF-8 mode. */
2747    
2748  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2749      case OP_XCLASS:      case OP_XCLASS:
# Line 1858  for (;;) Line 2784  for (;;)
2784    
2785        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
2786          {          {
2787          if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);          if (eptr >= md->end_subject)
2788          GETCHARINC(c, eptr);            {
2789          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);            SCHECK_PARTIAL();
2790              MRRETURN(MATCH_NOMATCH);
2791              }
2792            GETCHARINCTEST(c, eptr);
2793            if (!_pcre_xclass(c, data)) MRRETURN(MATCH_NOMATCH);
2794          }          }
2795    
2796        /* 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 1875  for (;;) Line 2805  for (;;)
2805          {          {
2806          for (fi = min;; fi++)          for (fi = min;; fi++)
2807            {            {
2808            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM20);
2809            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2810            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2811            GETCHARINC(c, eptr);            if (eptr >= md->end_subject)
2812            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);              {
2813                SCHECK_PARTIAL();
2814                MRRETURN(MATCH_NOMATCH);
2815                }
2816              GETCHARINCTEST(c, eptr);
2817              if (!_pcre_xclass(c, data)) MRRETURN(MATCH_NOMATCH);
2818            }            }
2819          /* Control never gets here */          /* Control never gets here */
2820          }          }
# Line 1892  for (;;) Line 2827  for (;;)
2827          for (i = min; i < max; i++)          for (i = min; i < max; i++)
2828            {            {
2829            int len = 1;            int len = 1;
2830            if (eptr >= md->end_subject) break;            if (eptr >= md->end_subject)
2831            GETCHARLEN(c, eptr, len);              {
2832                SCHECK_PARTIAL();
2833                break;
2834                }
2835              GETCHARLENTEST(c, eptr, len);
2836            if (!_pcre_xclass(c, data)) break;            if (!_pcre_xclass(c, data)) break;
2837            eptr += len;            eptr += len;
2838            }            }
2839          for(;;)          for(;;)
2840            {            {
2841            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM21);
2842            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2843            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
2844            BACKCHAR(eptr)            if (utf8) BACKCHAR(eptr);
2845            }            }
2846          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2847          }          }
2848    
2849        /* Control never gets here */        /* Control never gets here */
# Line 1920  for (;;) Line 2859  for (;;)
2859        length = 1;        length = 1;
2860        ecode++;        ecode++;
2861        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2862        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
2863        while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);          {
2864            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2865            MRRETURN(MATCH_NOMATCH);
2866            }
2867          while (length-- > 0) if (*ecode++ != *eptr++) MRRETURN(MATCH_NOMATCH);
2868        }        }
2869      else      else
2870  #endif  #endif
2871    
2872      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2873        {        {
2874        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
2875        if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);          {
2876            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2877            MRRETURN(MATCH_NOMATCH);
2878            }
2879          if (ecode[1] != *eptr++) MRRETURN(MATCH_NOMATCH);
2880        ecode += 2;        ecode += 2;
2881        }        }
2882      break;      break;
2883    
2884      /* Match a single character, caselessly */      /* Match a single character, caselessly */
2885    
2886      case OP_CHARNC:      case OP_CHARI:
2887  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2888      if (utf8)      if (utf8)
2889        {        {
# Line 1944  for (;;) Line 2891  for (;;)
2891        ecode++;        ecode++;
2892        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2893    
2894        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
2895            {
2896            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2897            MRRETURN(MATCH_NOMATCH);
2898            }
2899    
2900        /* 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
2901        can use the fast lookup table. */        can use the fast lookup table. */
2902    
2903        if (fc < 128)        if (fc < 128)
2904          {          {
2905          if (md->lcc[*ecode++] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          if (md->lcc[*ecode++] != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
2906          }          }
2907    
2908        /* Otherwise we must pick up the subject character */        /* Otherwise we must pick up the subject character */
# Line 1968  for (;;) Line 2919  for (;;)
2919          if (fc != dc)          if (fc != dc)
2920            {            {
2921  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2922            if (dc != _pcre_ucp_othercase(fc))            if (dc != UCD_OTHERCASE(fc))
2923  #endif  #endif
2924              RRETURN(MATCH_NOMATCH);              MRRETURN(MATCH_NOMATCH);
2925            }            }
2926          }          }
2927        }        }
# Line 1979  for (;;) Line 2930  for (;;)
2930    
2931      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2932        {        {
2933        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
2934        if (md->lcc[ecode[1]] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          {
2935            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2936            MRRETURN(MATCH_NOMATCH);
2937            }
2938          if (md->lcc[ecode[1]] != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
2939        ecode += 2;        ecode += 2;
2940        }        }
2941      break;      break;
# Line 1988  for (;;) Line 2943  for (;;)
2943      /* Match a single character repeatedly. */      /* Match a single character repeatedly. */
2944    
2945      case OP_EXACT:      case OP_EXACT:
2946        case OP_EXACTI:
2947      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
2948      ecode += 3;      ecode += 3;
2949      goto REPEATCHAR;      goto REPEATCHAR;
2950    
2951      case OP_POSUPTO:      case OP_POSUPTO:
2952        case OP_POSUPTOI:
2953      possessive = TRUE;      possessive = TRUE;
2954      /* Fall through */      /* Fall through */
2955    
2956      case OP_UPTO:      case OP_UPTO:
2957        case OP_UPTOI:
2958      case OP_MINUPTO:      case OP_MINUPTO:
2959        case OP_MINUPTOI:
2960      min = 0;      min = 0;
2961      max = GET2(ecode, 1);      max = GET2(ecode, 1);
2962      minimize = *ecode == OP_MINUPTO;      minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI;
2963      ecode += 3;      ecode += 3;
2964      goto REPEATCHAR;      goto REPEATCHAR;
2965    
2966      case OP_POSSTAR:      case OP_POSSTAR:
2967        case OP_POSSTARI:
2968      possessive = TRUE;      possessive = TRUE;
2969      min = 0;      min = 0;
2970      max = INT_MAX;      max = INT_MAX;
# Line 2012  for (;;) Line 2972  for (;;)
2972      goto REPEATCHAR;      goto REPEATCHAR;
2973    
2974      case OP_POSPLUS:      case OP_POSPLUS:
2975        case OP_POSPLUSI:
2976      possessive = TRUE;      possessive = TRUE;
2977      min = 1;      min = 1;
2978      max = INT_MAX;      max = INT_MAX;
# Line 2019  for (;;) Line 2980  for (;;)
2980      goto REPEATCHAR;      goto REPEATCHAR;
2981    
2982      case OP_POSQUERY:      case OP_POSQUERY:
2983        case OP_POSQUERYI:
2984      possessive = TRUE;      possessive = TRUE;
2985      min = 0;      min = 0;
2986      max = 1;      max = 1;
# Line 2026  for (;;) Line 2988  for (;;)
2988      goto REPEATCHAR;      goto REPEATCHAR;
2989    
2990      case OP_STAR:      case OP_STAR:
2991        case OP_STARI:
2992      case OP_MINSTAR:      case OP_MINSTAR:
2993        case OP_MINSTARI:
2994      case OP_PLUS:      case OP_PLUS:
2995        case OP_PLUSI:
2996      case OP_MINPLUS:      case OP_MINPLUS:
2997        case OP_MINPLUSI:
2998      case OP_QUERY:      case OP_QUERY:
2999        case OP_QUERYI:
3000      case OP_MINQUERY:      case OP_MINQUERY:
3001      c = *ecode++ - OP_STAR;      case OP_MINQUERYI:
3002        c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI);
3003      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3004      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3005      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3006      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
3007    
3008      /* 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. */  
3009    
3010      REPEATCHAR:      REPEATCHAR:
3011  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 2048  for (;;) Line 3014  for (;;)
3014        length = 1;        length = 1;
3015        charptr = ecode;        charptr = ecode;
3016        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
       if (min * length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3017        ecode += length;        ecode += length;
3018    
3019        /* Handle multibyte character matching specially here. There is        /* Handle multibyte character matching specially here. There is
# Line 2058  for (;;) Line 3023  for (;;)
3023          {          {
3024  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3025          unsigned int othercase;          unsigned int othercase;
3026          if ((ims & PCRE_CASELESS) != 0 &&          if (op >= OP_STARI &&     /* Caseless */
3027              (othercase = _pcre_ucp_othercase(fc)) != NOTACHAR)              (othercase = UCD_OTHERCASE(fc)) != fc)
3028            oclength = _pcre_ord2utf8(othercase, occhars);            oclength = _pcre_ord2utf8(othercase, occhars);
3029          else oclength = 0;          else oclength = 0;
3030  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3031    
3032          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3033            {            {
3034            if (memcmp(eptr, charptr, length) == 0) eptr += length;            if (eptr <= md->end_subject - length &&
3035                memcmp(eptr, charptr, length) == 0) eptr += length;
3036  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3037            /* Need braces because of following else */            else if (oclength > 0 &&
3038            else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }                     eptr <= md->end_subject - oclength &&
3039                       memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3040    #endif  /* SUPPORT_UCP */
3041            else            else
3042              {              {
3043              if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);              CHECK_PARTIAL();
3044              eptr += oclength;              MRRETURN(MATCH_NOMATCH);
3045              }              }
 #else   /* without SUPPORT_UCP */  
           else { RRETURN(MATCH_NOMATCH); }  
 #endif  /* SUPPORT_UCP */  
3046            }            }
3047    
3048          if (min == max) continue;          if (min == max) continue;
# Line 2086  for (;;) Line 3051  for (;;)
3051            {            {
3052            for (fi = min;; fi++)            for (fi = min;; fi++)
3053              {              {
3054              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM22);
3055              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3056              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3057              if (memcmp(eptr, charptr, length) == 0) eptr += length;              if (eptr <= md->end_subject - length &&
3058                  memcmp(eptr, charptr, length) == 0) eptr += length;
3059  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3060              /* Need braces because of following else */              else if (oclength > 0 &&
3061              else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }                       eptr <= md->end_subject - oclength &&
3062                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3063    #endif  /* SUPPORT_UCP */
3064              else              else
3065                {                {
3066                if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);                CHECK_PARTIAL();
3067                eptr += oclength;                MRRETURN(MATCH_NOMATCH);
3068                }                }
 #else   /* without SUPPORT_UCP */  
             else { RRETURN (MATCH_NOMATCH); }  
 #endif  /* SUPPORT_UCP */  
3069              }              }
3070            /* Control never gets here */            /* Control never gets here */
3071            }            }
# Line 2110  for (;;) Line 3075  for (;;)
3075            pp = eptr;            pp = eptr;
3076            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3077              {              {
3078              if (eptr > md->end_subject - length) break;              if (eptr <= md->end_subject - length &&
3079              if (memcmp(eptr, charptr, length) == 0) eptr += length;                  memcmp(eptr, charptr, length) == 0) eptr += length;
3080  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3081              else if (oclength == 0) break;              else if (oclength > 0 &&
3082                         eptr <= md->end_subject - oclength &&
3083                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3084    #endif  /* SUPPORT_UCP */
3085              else              else
3086                {                {
3087                if (memcmp(eptr, occhars, oclength) != 0) break;                CHECK_PARTIAL();
3088                eptr += oclength;                break;
3089                }                }
 #else   /* without SUPPORT_UCP */  
             else break;  
 #endif  /* SUPPORT_UCP */  
3090              }              }
3091    
3092            if (possessive) continue;            if (possessive) continue;
3093    
3094            for(;;)            for(;;)
3095             {              {
3096             RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM23);
3097             if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3098             if (eptr == pp) RRETURN(MATCH_NOMATCH);              if (eptr == pp) { MRRETURN(MATCH_NOMATCH); }
3099  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3100             eptr--;              eptr--;
3101             BACKCHAR(eptr);              BACKCHAR(eptr);
3102  #else   /* without SUPPORT_UCP */  #else   /* without SUPPORT_UCP */
3103             eptr -= length;              eptr -= length;
3104  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3105             }              }
3106            }            }
3107          /* Control never gets here */          /* Control never gets here */
3108          }          }
# Line 2149  for (;;) Line 3115  for (;;)
3115  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
3116    
3117      /* When not in UTF-8 mode, load a single-byte character. */      /* When not in UTF-8 mode, load a single-byte character. */
3118        {  
3119        if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);      fc = *ecode++;
       fc = *ecode++;  
       }  
3120    
3121      /* 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
3122      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 2166  for (;;) Line 3130  for (;;)
3130      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3131        max, eptr));        max, eptr));
3132    
3133      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_STARI)  /* Caseless */
3134        {        {
3135        fc = md->lcc[fc];        fc = md->lcc[fc];
3136        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
3137          if (fc != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          {
3138            if (eptr >= md->end_subject)
3139              {
3140              SCHECK_PARTIAL();
3141              MRRETURN(MATCH_NOMATCH);
3142              }
3143            if (fc != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3144            }
3145        if (min == max) continue;        if (min == max) continue;
3146        if (minimize)        if (minimize)
3147          {          {
3148          for (fi = min;; fi++)          for (fi = min;; fi++)
3149            {            {
3150            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM24);
3151            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3152            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3153                fc != md->lcc[*eptr++])            if (eptr >= md->end_subject)
3154              RRETURN(MATCH_NOMATCH);              {
3155                SCHECK_PARTIAL();
3156                MRRETURN(MATCH_NOMATCH);
3157                }
3158              if (fc != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3159            }            }
3160          /* Control never gets here */          /* Control never gets here */
3161          }          }
# Line 2189  for (;;) Line 3164  for (;;)
3164          pp = eptr;          pp = eptr;
3165          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3166            {            {
3167            if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break;            if (eptr >= md->end_subject)
3168                {
3169                SCHECK_PARTIAL();
3170                break;
3171                }
3172              if (fc != md->lcc[*eptr]) break;
3173            eptr++;            eptr++;
3174            }            }
3175    
3176          if (possessive) continue;          if (possessive) continue;
3177    
3178          while (eptr >= pp)          while (eptr >= pp)
3179            {            {
3180            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM25);
3181            eptr--;            eptr--;
3182            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3183            }            }
3184          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3185          }          }
3186        /* Control never gets here */        /* Control never gets here */
3187        }        }
# Line 2208  for (;;) Line 3190  for (;;)
3190    
3191      else      else
3192        {        {
3193        for (i = 1; i <= min; i++) if (fc != *eptr++) RRETURN(MATCH_NOMATCH);        for (i = 1; i <= min; i++)
3194            {
3195            if (eptr >= md->end_subject)
3196              {
3197              SCHECK_PARTIAL();
3198              MRRETURN(MATCH_NOMATCH);
3199              }
3200            if (fc != *eptr++) MRRETURN(MATCH_NOMATCH);
3201            }
3202    
3203        if (min == max) continue;        if (min == max) continue;
3204    
3205        if (minimize)        if (minimize)
3206          {          {
3207          for (fi = min;; fi++)          for (fi = min;; fi++)
3208            {            {
3209            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM26);
3210            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3211            if (fi >= max || eptr >= md->end_subject || fc != *eptr++)            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3212              RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
3213                {
3214                SCHECK_PARTIAL();
3215                MRRETURN(MATCH_NOMATCH);
3216                }
3217              if (fc != *eptr++) MRRETURN(MATCH_NOMATCH);
3218            }            }
3219          /* Control never gets here */          /* Control never gets here */
3220          }          }
# Line 2226  for (;;) Line 3223  for (;;)
3223          pp = eptr;          pp = eptr;
3224          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3225            {            {
3226            if (eptr >= md->end_subject || fc != *eptr) break;            if (eptr >= md->end_subject)
3227                {
3228                SCHECK_PARTIAL();
3229                break;
3230                }
3231              if (fc != *eptr) break;
3232            eptr++;            eptr++;
3233            }            }
3234          if (possessive) continue;          if (possessive) continue;
3235    
3236          while (eptr >= pp)          while (eptr >= pp)
3237            {            {
3238            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM27);
3239            eptr--;            eptr--;
3240            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3241            }            }
3242          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3243          }          }
3244        }        }
3245      /* Control never gets here */      /* Control never gets here */
# Line 2245  for (;;) Line 3248  for (;;)
3248      checking can be multibyte. */      checking can be multibyte. */
3249    
3250      case OP_NOT:      case OP_NOT:
3251      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      case OP_NOTI:
3252        if (eptr >= md->end_subject)
3253          {
3254          SCHECK_PARTIAL();
3255          MRRETURN(MATCH_NOMATCH);
3256          }
3257      ecode++;      ecode++;
3258      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
3259      if ((ims & PCRE_CASELESS) != 0)      if (op == OP_NOTI)         /* The caseless case */
3260        {        {
3261  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3262        if (c < 256)        if (c < 256)
3263  #endif  #endif
3264        c = md->lcc[c];        c = md->lcc[c];
3265        if (md->lcc[*ecode++] == c) RRETURN(MATCH_NOMATCH);        if (md->lcc[*ecode++] == c) MRRETURN(MATCH_NOMATCH);
3266        }        }
3267      else      else    /* Caseful */
3268        {        {
3269        if (*ecode++ == c) RRETURN(MATCH_NOMATCH);        if (*ecode++ == c) MRRETURN(MATCH_NOMATCH);
3270        }        }
3271      break;      break;
3272    
# Line 2270  for (;;) Line 3278  for (;;)
3278      about... */      about... */
3279    
3280      case OP_NOTEXACT:      case OP_NOTEXACT:
3281        case OP_NOTEXACTI:
3282      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
3283      ecode += 3;      ecode += 3;
3284      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3285    
3286      case OP_NOTUPTO:      case OP_NOTUPTO:
3287        case OP_NOTUPTOI:
3288      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
3289        case OP_NOTMINUPTOI:
3290      min = 0;      min = 0;
3291      max = GET2(ecode, 1);      max = GET2(ecode, 1);
3292      minimize = *ecode == OP_NOTMINUPTO;      minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI;
3293      ecode += 3;      ecode += 3;
3294      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3295    
3296      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
3297        case OP_NOTPOSSTARI:
3298      possessive = TRUE;      possessive = TRUE;
3299      min = 0;      min = 0;
3300      max = INT_MAX;      max = INT_MAX;
# Line 2290  for (;;) Line 3302  for (;;)
3302      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3303    
3304      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
3305        case OP_NOTPOSPLUSI:
3306      possessive = TRUE;      possessive = TRUE;
3307      min = 1;      min = 1;
3308      max = INT_MAX;      max = INT_MAX;
# Line 2297  for (;;) Line 3310  for (;;)
3310      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3311    
3312      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
3313        case OP_NOTPOSQUERYI:
3314      possessive = TRUE;      possessive = TRUE;
3315      min = 0;      min = 0;
3316      max = 1;      max = 1;
# Line 2304  for (;;) Line 3318  for (;;)
3318      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3319    
3320      case OP_NOTPOSUPTO:      case OP_NOTPOSUPTO:
3321        case OP_NOTPOSUPTOI:
3322      possessive = TRUE;      possessive = TRUE;
3323      min = 0;      min = 0;
3324      max = GET2(ecode, 1);      max = GET2(ecode, 1);
# Line 2311  for (;;) Line 3326  for (;;)
3326      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3327    
3328      case OP_NOTSTAR:      case OP_NOTSTAR:
3329        case OP_NOTSTARI:
3330      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
3331        case OP_NOTMINSTARI:
3332      case OP_NOTPLUS:      case OP_NOTPLUS:
3333        case OP_NOTPLUSI:
3334      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
3335        case OP_NOTMINPLUSI:
3336      case OP_NOTQUERY:      case OP_NOTQUERY:
3337        case OP_NOTQUERYI:
3338      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
3339      c = *ecode++ - OP_NOTSTAR;      case OP_NOTMINQUERYI:
3340        c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
3341      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3342      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3343      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3344      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
3345    
3346      /* 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. */  
3347    
3348      REPEATNOTCHAR:      REPEATNOTCHAR:
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3349      fc = *ecode++;      fc = *ecode++;
3350    
3351      /* 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 2341  for (;;) Line 3359  for (;;)
3359      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,
3360        max, eptr));        max, eptr));
3361    
3362      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_NOTSTARI)     /* Caseless */
3363        {        {
3364        fc = md->lcc[fc];        fc = md->lcc[fc];
3365    
# Line 2352  for (;;) Line 3370  for (;;)
3370          register unsigned int d;          register unsigned int d;
3371          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3372            {            {
3373              if (eptr >= md->end_subject)
3374                {
3375                SCHECK_PARTIAL();
3376                MRRETURN(MATCH_NOMATCH);
3377                }
3378            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3379            if (d < 256) d = md->lcc[d];            if (d < 256) d = md->lcc[d];
3380            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) MRRETURN(MATCH_NOMATCH);
3381            }            }
3382          }          }
3383        else        else
# Line 2363  for (;;) Line 3386  for (;;)
3386        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3387          {          {
3388          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3389            if (fc == md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);            {
3390              if (eptr >= md->end_subject)
3391                {
3392                SCHECK_PARTIAL();
3393                MRRETURN(MATCH_NOMATCH);
3394                }
3395              if (fc == md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3396              }
3397          }          }
3398    
3399        if (min == max) continue;        if (min == max) continue;
# Line 2377  for (;;) Line 3407  for (;;)
3407            register unsigned int d;            register unsigned int d;
3408            for (fi = min;; fi++)            for (fi = min;; fi++)
3409              {              {
3410              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM28);
3411              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3412                if (fi >= max) MRRETURN(MATCH_NOMATCH);
3413                if (eptr >= md->end_subject)
3414                  {
3415                  SCHECK_PARTIAL();
3416                  MRRETURN(MATCH_NOMATCH);
3417                  }
3418              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3419              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3420              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) MRRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
3421              }              }
3422            }            }
3423          else          else
# Line 2391  for (;;) Line 3426  for (;;)
3426            {            {
3427            for (fi = min;; fi++)            for (fi = min;; fi++)
3428              {              {
3429              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM29);
3430              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3431              if (fi >= max || eptr >= md->end_subject || fc == md->lcc[*eptr++])              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3432                RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3433                  {
3434                  SCHECK_PARTIAL();
3435                  MRRETURN(MATCH_NOMATCH);
3436                  }
3437                if (fc == md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3438              }              }
3439            }            }
3440          /* Control never gets here */          /* Control never gets here */
# Line 2414  for (;;) Line 3454  for (;;)
3454            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3455              {              {
3456              int len = 1;              int len = 1;
3457              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3458                  {
3459                  SCHECK_PARTIAL();
3460                  break;
3461                  }
3462              GETCHARLEN(d, eptr, len);              GETCHARLEN(d, eptr, len);
3463              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3464              if (fc == d) break;              if (fc == d) break;
# Line 2423  for (;;) Line 3467  for (;;)
3467          if (possessive) continue;          if (possessive) continue;
3468          for(;;)          for(;;)
3469              {              {
3470              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM30);
3471              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3472              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3473              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2435  for (;;) Line 3479  for (;;)
3479            {            {
3480            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3481              {              {
3482              if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break;              if (eptr >= md->end_subject)
3483                  {
3484                  SCHECK_PARTIAL();
3485                  break;
3486                  }
3487                if (fc == md->lcc[*eptr]) break;
3488              eptr++;              eptr++;
3489              }              }
3490            if (possessive) continue;            if (possessive) continue;
3491            while (eptr >= pp)            while (eptr >= pp)
3492              {              {
3493              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM31);
3494              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3495              eptr--;              eptr--;
3496              }              }
3497            }            }
3498    
3499          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3500          }          }
3501        /* Control never gets here */        /* Control never gets here */
3502        }        }
# Line 2463  for (;;) Line 3512  for (;;)
3512          register unsigned int d;          register unsigned int d;
3513          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3514            {            {
3515              if (eptr >= md->end_subject)
3516                {
3517                SCHECK_PARTIAL();
3518                MRRETURN(MATCH_NOMATCH);
3519                }
3520            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3521            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) MRRETURN(MATCH_NOMATCH);
3522            }            }
3523          }          }
3524        else        else
# Line 2472  for (;;) Line 3526  for (;;)
3526        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3527          {          {
3528          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3529            if (fc == *eptr++) RRETURN(MATCH_NOMATCH);            {
3530              if (eptr >= md->end_subject)
3531                {
3532                SCHECK_PARTIAL();
3533                MRRETURN(MATCH_NOMATCH);
3534                }
3535              if (fc == *eptr++) MRRETURN(MATCH_NOMATCH);
3536              }
3537          }          }
3538    
3539        if (min == max) continue;        if (min == max) continue;
# Line 2486  for (;;) Line 3547  for (;;)
3547            register unsigned int d;            register unsigned int d;
3548            for (fi = min;; fi++)            for (fi = min;; fi++)
3549              {              {
3550              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM32);
3551              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3552                if (fi >= max) MRRETURN(MATCH_NOMATCH);
3553                if (eptr >= md->end_subject)
3554                  {
3555                  SCHECK_PARTIAL();
3556                  MRRETURN(MATCH_NOMATCH);
3557                  }
3558              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3559              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) MRRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
3560              }              }
3561            }            }
3562          else          else
# Line 2499  for (;;) Line 3565  for (;;)
3565            {            {
3566            for (fi = min;; fi++)            for (fi = min;; fi++)
3567              {              {
3568              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM33);
3569              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3570              if (fi >= max || eptr >= md->end_subject || fc == *eptr++)              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3571                RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3572                  {
3573                  SCHECK_PARTIAL();
3574                  MRRETURN(MATCH_NOMATCH);
3575                  }
3576                if (fc == *eptr++) MRRETURN(MATCH_NOMATCH);
3577              }              }
3578            }            }
3579          /* Control never gets here */          /* Control never gets here */
# Line 2522  for (;;) Line 3593  for (;;)
3593            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3594              {              {
3595              int len = 1;              int len = 1;
3596              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3597                  {
3598                  SCHECK_PARTIAL();
3599                  break;
3600                  }
3601              GETCHARLEN(d, eptr, len);              GETCHARLEN(d, eptr, len);
3602              if (fc == d) break;              if (fc == d) break;
3603              eptr += len;              eptr += len;
# Line 2530  for (;;) Line 3605  for (;;)
3605            if (possessive) continue;            if (possessive) continue;
3606            for(;;)            for(;;)
3607              {              {
3608              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM34);
3609              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3610              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3611              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2542  for (;;) Line 3617  for (;;)
3617            {            {
3618            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3619              {              {
3620              if (eptr >= md->end_subject || fc == *eptr) break;              if (eptr >= md->end_subject)
3621                  {
3622                  SCHECK_PARTIAL();
3623                  break;
3624                  }
3625                if (fc == *eptr) break;
3626              eptr++;              eptr++;
3627              }              }
3628            if (possessive) continue;            if (possessive) continue;
3629            while (eptr >= pp)            while (eptr >= pp)
3630              {              {
3631              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM35);
3632              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3633              eptr--;              eptr--;
3634              }              }
3635            }            }
3636    
3637          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3638          }          }
3639        }        }
3640      /* Control never gets here */      /* Control never gets here */
# Line 2636  for (;;) Line 3716  for (;;)
3716    
3717      /* First, ensure the minimum number of matches are present. Use inline      /* First, ensure the minimum number of matches are present. Use inline
3718      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
3719      (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  
3720      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
3721      and single-bytes. */      and single-bytes. */
3722    
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3723      if (min > 0)      if (min > 0)
3724        {        {
3725  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 2651  for (;;) Line 3728  for (;;)
3728          switch(prop_type)          switch(prop_type)
3729            {            {
3730            case PT_ANY:            case PT_ANY:
3731            if (prop_fail_result) RRETURN(MATCH_NOMATCH);            if (prop_fail_result) MRRETURN(MATCH_NOMATCH);
3732            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3733              {              {
3734              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3735              GETCHARINC(c, eptr);                {
3736                  SCHECK_PARTIAL();
3737                  MRRETURN(MATCH_NOMATCH);
3738                  }
3739                GETCHARINCTEST(c, eptr);
3740              }              }
3741            break;            break;
3742    
3743            case PT_LAMP:            case PT_LAMP:
3744            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3745              {              {
3746              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3747              GETCHARINC(c, eptr);                {
3748              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);                SCHECK_PARTIAL();
3749                  MRRETURN(MATCH_NOMATCH);
3750                  }
3751                GETCHARINCTEST(c, eptr);
3752                prop_chartype = UCD_CHARTYPE(c);
3753              if ((prop_chartype == ucp_Lu ||              if ((prop_chartype == ucp_Lu ||
3754                   prop_chartype == ucp_Ll ||                   prop_chartype == ucp_Ll ||
3755                   prop_chartype == ucp_Lt) == prop_fail_result)                   prop_chartype == ucp_Lt) == prop_fail_result)
3756                RRETURN(MATCH_NOMATCH);                MRRETURN(MATCH_NOMATCH);
3757              }              }
3758            break;            break;
3759    
3760            case PT_GC:            case PT_GC:
3761            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3762              {              {
3763              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3764              GETCHARINC(c, eptr);                {
3765              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);                SCHECK_PARTIAL();
3766                  MRRETURN(MATCH_NOMATCH);
3767                  }
3768                GETCHARINCTEST(c, eptr);
3769                prop_category = UCD_CATEGORY(c);
3770              if ((prop_category == prop_value) == prop_fail_result)              if ((prop_category == prop_value) == prop_fail_result)
3771                RRETURN(MATCH_NOMATCH);                MRRETURN(MATCH_NOMATCH);
3772              }              }
3773            break;            break;
3774    
3775            case PT_PC:            case PT_PC:
3776            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)