/[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 87 by nigel, Sat Feb 24 21:41:21 2007 UTC revision 610 by ph10, Tue Jun 28 15:58:34 2011 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2006 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 */
50    #define PSSTART start_subject  /* Field containing processed string start */
51    #define PSEND   end_subject    /* Field containing processed string end */
52    
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    /* Undefine some potentially clashing cpp symbols */
56    
57  /* Structure for building a chain of data that actually lives on the  #undef min
58  stack, for holding the values of the subject pointer at the start of each  #undef max
 subpattern, so as to detect when an empty string has been matched by a  
 subpattern - to break infinite loops. When NO_RECURSE is set, these blocks  
 are on the heap, not on the stack. */  
   
 typedef struct eptrblock {  
   struct eptrblock *epb_prev;  
   USPTR epb_saved_eptr;  
 } eptrblock;  
59    
60  /* Flag bits for the match() function */  /* Values for setting in md->match_function_type to indicate two special types
61    of call to match(). We do it this way to save on using another stack variable,
62    as stack usage is to be discouraged. */
63    
64  #define match_condassert   0x01    /* Called to check a condition assertion */  #define MATCH_CONDASSERT     1  /* Called to check a condition assertion */
65  #define match_isgroup      0x02    /* Set if start of bracketed group */  #define MATCH_CBEGROUP       2  /* Could-be-empty unlimited repeat group */
66    
67  /* Non-error returns from the match() function. Error returns are externally  /* Non-error returns from the match() function. Error returns are externally
68  defined PCRE_ERROR_xxx codes, which are all negative. */  defined PCRE_ERROR_xxx codes, which are all negative. */
# Line 68  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 81  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 101  Returns:     nothing Line 122  Returns:     nothing
122  static void  static void
123  pchars(const uschar *p, int length, BOOL is_subject, match_data *md)  pchars(const uschar *p, int length, BOOL is_subject, match_data *md)
124  {  {
125  int c;  unsigned int c;
126  if (is_subject && length > md->end_subject - p) length = md->end_subject - p;  if (is_subject && length > md->end_subject - p) length = md->end_subject - p;
127  while (length-- > 0)  while (length-- > 0)
128    if (isprint(c = *(p++))) printf("%c", c); else printf("\\x%02x", c);    if (isprint(c = *(p++))) printf("%c", c); else printf("\\x%02x", c);
# Line 114  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 146  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 186  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 208  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 269  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;
362    int Xflags;    unsigned int Xrdepth;
   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    
378    BOOL Xcur_is_word;    BOOL Xcur_is_word;
379    BOOL Xcondition;    BOOL Xcondition;
   BOOL Xminimize;  
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 303  typedef struct heapframe { Line 386  typedef struct heapframe {
386    int Xprop_category;    int Xprop_category;
387    int Xprop_chartype;    int Xprop_chartype;
388    int Xprop_script;    int Xprop_script;
389    int *Xprop_test_variable;    int Xoclength;
390      uschar Xocchars[8];
391  #endif  #endif
392    
393      int Xcodelink;
394    int Xctype;    int Xctype;
395    int Xfc;    unsigned int Xfc;
396    int Xfi;    int Xfi;
397    int Xlength;    int Xlength;
398    int Xmax;    int Xmax;
# Line 321  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 340  typedef struct heapframe { Line 424  typedef struct heapframe {
424  *         Match from current position            *  *         Match from current position            *
425  *************************************************/  *************************************************/
426    
427  /* On entry ecode points to the first opcode, and eptr to the first character  /* This function is called recursively in many circumstances. Whenever it
 in the subject string, while eptrb holds the value of eptr at the start of the  
 last bracketed group - used for breaking infinite loops matching zero-length  
 strings. 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 in subject     eptr        pointer to current character in subject
461     ecode       position in 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_isgroup - this is the start of a bracketed 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, 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 them with "register"  so they can be ordinary variables in all cases. Mark some of them with
485  because they are used a lot in loops. */  "register" because they are used a lot in loops. */
486    
487  register int  rrc;    /* Returns from recursive calls */  register int  rrc;         /* Returns from recursive calls */
488  register int  i;      /* Used for loops not involving calls to RMATCH() */  register int  i;           /* Used for loops not involving calls to RMATCH() */
489  register int  c;      /* Character values not kept over RMATCH() calls */  register unsigned int c;   /* Character values not kept over RMATCH() calls */
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 */
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 391  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 412  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 424  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 434  HEAP_RECURSE: Line 544  HEAP_RECURSE:
544    
545  #define cur_is_word        frame->Xcur_is_word  #define cur_is_word        frame->Xcur_is_word
546  #define condition          frame->Xcondition  #define condition          frame->Xcondition
 #define minimize           frame->Xminimize  
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 446  HEAP_RECURSE: Line 553  HEAP_RECURSE:
553  #define prop_category      frame->Xprop_category  #define prop_category      frame->Xprop_category
554  #define prop_chartype      frame->Xprop_chartype  #define prop_chartype      frame->Xprop_chartype
555  #define prop_script        frame->Xprop_script  #define prop_script        frame->Xprop_script
556  #define prop_test_variable frame->Xprop_test_variable  #define oclength           frame->Xoclength
557    #define occhars            frame->Xocchars
558  #endif  #endif
559    
560  #define ctype              frame->Xctype  #define ctype              frame->Xctype
# Line 470  HEAP_RECURSE: Line 578  HEAP_RECURSE:
578  get preserved during recursion in the normal way. In this environment, fi and  get preserved during recursion in the normal way. In this environment, fi and
579  i, and fc and c, can be the same variables. */  i, and fc and c, can be the same variables. */
580    
581  #else  #else         /* NO_RECURSE not defined */
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;
 BOOL minimize;  
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 501  int prop_fail_result; Line 613  int prop_fail_result;
613  int prop_category;  int prop_category;
614  int prop_chartype;  int prop_chartype;
615  int prop_script;  int prop_script;
616  int *prop_test_variable;  int oclength;
617    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 516  int save_offset1, save_offset2, save_off Line 630  int save_offset1, save_offset2, save_off
630  int stacksave[REC_STACK_SAVE_MAX];  int stacksave[REC_STACK_SAVE_MAX];
631    
632  eptrblock newptrb;  eptrblock newptrb;
633  #endif  #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. */
# Line 524  variables. */ Line 649  variables. */
649  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
650  prop_value = 0;  prop_value = 0;
651  prop_fail_result = 0;  prop_fail_result = 0;
 prop_test_variable = NULL;  
652  #endif  #endif
653    
654    
655    /* This label is used for tail recursion, which is used in a few cases even
656    when NO_RECURSE is not defined, in order to reduce the amount of stack that is
657    used. Thanks to Ian Taylor for noticing this possibility and sending the
658    original patch. */
659    
660    TAIL_RECURSE:
661    
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    
670    #ifdef SUPPORT_UTF8
671    utf8 = md->utf8;       /* Local copy of the flag */
672    #else
673    utf8 = FALSE;
674    #endif
675    
676  /* First check that we haven't called match() too many times, or that we  /* First check that we haven't called match() too many times, or that we
677  haven't exceeded the recursive call limit. */  haven't exceeded the recursive call limit. */
678    
679  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
680  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
681    
682  original_ims = ims;    /* Save for resetting on ')' */  /* At the start of a group with an unlimited repeat that may match an empty
683  utf8 = md->utf8;       /* Local copy of the flag */  string, the variable md->match_function_type is set to MATCH_CBEGROUP. It is
684    done this way to save having to use another function argument, which would take
685  /* At the start of a bracketed group, add the current subject pointer to the  up space on the stack. See also MATCH_CONDASSERT below.
686  stack of such pointers, to be re-instated at the end of the group when we hit  
687  the closing ket. When match() is called in other circumstances, we don't add to  When MATCH_CBEGROUP is set, add the current subject pointer to the chain of
688  this stack. */  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_isgroup) != 0)  if (md->match_function_type == MATCH_CBEGROUP)
695    {    {
   newptrb.epb_prev = eptrb;  
696    newptrb.epb_saved_eptr = eptr;    newptrb.epb_saved_eptr = eptr;
697      newptrb.epb_prev = eptrb;
698    eptrb = &newptrb;    eptrb = &newptrb;
699      md->match_function_type = 0;
700    }    }
701    
702  /* Now start processing the operations. */  /* Now start processing the opcodes. */
703    
704  for (;;)  for (;;)
705    {    {
706      minimize = possessive = FALSE;
707    op = *ecode;    op = *ecode;
708    minimize = FALSE;  
709      switch(op)
710    /* For partial matching, remember if we ever hit the end of the subject after      {
711    matching at least one subject character. */      case OP_MARK:
712        markptr = ecode + 2;
713        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
714          eptrb, RM55);
715    
716        /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
717        argument, and we must check whether that argument matches this MARK's
718        argument. It is passed back in md->start_match_ptr (an overloading of that
719        variable). If it does match, we reset that variable to the current subject
720        position and return MATCH_SKIP. Otherwise, pass back the return code
721        unaltered. */
722    
723        if (rrc == MATCH_SKIP_ARG &&
724            strcmp((char *)markptr, (char *)(md->start_match_ptr)) == 0)
725          {
726          md->start_match_ptr = eptr;
727          RRETURN(MATCH_SKIP);
728          }
729    
730        if (md->mark == NULL) md->mark = markptr;
731        RRETURN(rrc);
732    
733        case OP_FAIL:
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    if (md->partial &&      /* PRUNE overrides THEN */
       eptr >= md->end_subject &&  
       eptr > md->start_match)  
     md->hitend = TRUE;  
   
   /* Opening capturing bracket. If there is space in the offset vector, save  
   the current subject position in the working slot at the top of the vector. We  
   mustn't change the current values of the data slot, because they may be set  
   from a previous iteration of this group, and be referred to by a reference  
   inside the group.  
   
   If the bracket fails to match, we need to restore this value and also the  
   values of the final offsets, in case they were set by a previous iteration of  
   the same bracket.  
   
   If there isn't enough space in the offset vector, treat this as if it were a  
   non-capturing bracket. Don't worry about setting the flag for the error case  
   here; that is handled in the code for KET. */  
748    
749    if (op > OP_BRA)      case OP_PRUNE:
750      {      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
751      number = op - OP_BRA;        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      /* For extended extraction brackets (large number), we have to fish out the      case OP_SKIP_ARG:
773      number from a dummy opcode at the start. */      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      if (number > EXTRACT_BASIC_MAX)      /* Pass back the current skip name by overloading md->start_match_ptr and
779        number = GET2(ecode, 2+LINK_SIZE);      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
813        of the same bracket.
814    
815        If there isn't enough space in the offset vector, treat this as if it were
816        a non-capturing bracket. Don't worry about setting the flag for the error
817        case here; that is handled in the code for KET. */
818    
819        case OP_CBRA:
820        case OP_SCBRA:
821        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 subject=", number);      printf("start bracket %d\n", number);
826        printf("subject=");
827      pchars(eptr, 16, TRUE, md);      pchars(eptr, 16, TRUE, md);
828      printf("\n");      printf("\n");
829  #endif  #endif
# Line 610  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        do        for (;;)
843          {          {
844          RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,          if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
845            match_isgroup);          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 628  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 */      /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
866        as a non-capturing bracket. */
867    
868      else op = OP_BRA;      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
869      }      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
870    
871    /* Other types of node can be handled by a switch */      DPRINTF(("insufficient capture room: treat as non-capturing\n"));
872    
873    switch(op)      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
874      {      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
875      case OP_BRA:     /* Non-capturing bracket: optimized */  
876      DPRINTF(("start bracket 0\n"));      /* Non-capturing bracket, except for possessive with unlimited repeat. Loop
877      do      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:
885        case OP_SBRA:
886        DPRINTF(("start non-capturing bracket\n"));
887        for (;;)
888          {
889          if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
890          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, eptrb,
891            RM2);
892          if (rrc != MATCH_NOMATCH &&
893              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
894            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        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,        matched_once = FALSE;
928          match_isgroup);        code_offset = ecode - md->start_code;
929        if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
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          if (!matched_once)
971            {
972            md->offset_vector[offset] = save_offset1;
973            md->offset_vector[offset+1] = save_offset2;
974            md->offset_vector[md->offset_end - number] = save_offset3;
975            }
976    
977          if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
978          if (allow_zero || matched_once)
979            {
980            ecode += 1 + LINK_SIZE;
981            break;
982            }
983    
984          RRETURN(MATCH_NOMATCH);
985          }
986    
987        /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
988        as a non-capturing bracket. */
989    
990        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
991        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
992    
993        DPRINTF(("insufficient capture room: treat as non-capturing\n"));
994    
995        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
996        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
997    
998        /* 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      while (*ecode == OP_ALT);  
1031      DPRINTF(("bracket 0 failed\n"));      if (matched_once || allow_zero)
1032          {
1033          ecode += 1 + LINK_SIZE;
1034          break;
1035          }
1036      RRETURN(MATCH_NOMATCH);      RRETURN(MATCH_NOMATCH);
1037    
1038        /* 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. */      exactly what going to the ket would do. */
1044    
1045      case OP_COND:      case OP_COND:
1046      if (ecode[LINK_SIZE+1] == OP_CREF) /* Condition extract or recurse test */      case OP_SCOND:
1047        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          if (pcre_callout != NULL)
1055            {
1056            pcre_callout_block cb;
1057            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 (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 == CREF_RECURSE * 2)?        condition = offset < offset_top && md->offset_vector[offset] >= 0;
1152          (md->recursive != NULL) :  
1153          (offset < offset_top && md->offset_vector[offset] >= 0);        /* If the numbered capture is unset, but the reference was by name,
1154        RMATCH(rrc, eptr, ecode + (condition?        scan the table to see if the name refers to any other numbers, and test
1155          (LINK_SIZE + 4) : (LINK_SIZE + 1 + GET(ecode, 1))),        them. The condition is true if any one is set. This is tediously similar
1156          offset_top, md, ims, eptrb, match_isgroup);        to the code above, but not close enough to try to amalgamate. */
1157        RRETURN(rrc);  
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);
1213          }
1214    
1215        else if (condcode == OP_DEF)     /* DEFINE - always false */
1216          {
1217          condition = FALSE;
1218          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 TRUE causes it to stop at the end of an assertion. */      md->match_function_type to MATCH_CONDASSERT causes it to stop at the end of
1223        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 | match_isgroup);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM3);
1229        if (rrc == MATCH_MATCH)        if (rrc == MATCH_MATCH)
1230          {          {
1231          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE+2);          condition = TRUE;
1232            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 ecode += GET(ecode, 1);        else
1241        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,          {
1242          match_isgroup);          condition = FALSE;
1243            ecode += codelink;
1244            }
1245          }
1246    
1247        /* We are now at the branch that is to be obeyed. As there is only one,
1248        we used to use tail recursion to avoid using another stack frame, except
1249        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)
1256          {
1257          if (op == OP_SCOND) md->match_function_type = MATCH_CBEGROUP;
1258          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM49);
1259          if (rrc == MATCH_THEN && md->start_match_ptr == ecode)
1260            rrc = MATCH_NOMATCH;
1261        RRETURN(rrc);        RRETURN(rrc);
1262        }        }
1263      /* Control never reaches here */      else                         /* Condition false & no alternative */
1264          {
1265          ecode += 1 + LINK_SIZE;
1266          }
1267        break;
1268    
1269    
1270        /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
1271        to close any currently open capturing brackets. */
1272    
1273        case OP_CLOSE:
1274        number = GET2(ecode, 1);
1275        offset = number << 1;
1276    
1277      /* Skip over conditional reference or large extraction number data if  #ifdef PCRE_DEBUG
1278      encountered. */        printf("end bracket %d at *ACCEPT", number);
1279          printf("\n");
1280    #endif
1281    
1282      case OP_CREF:      md->capture_last = number;
1283      case OP_BRANUMBER:      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;      ecode += 3;
1291      break;      break;
1292    
     /* End of the pattern. If we are in a recursion, we should restore the  
     offsets appropriately and continue from after the call. */  
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_END:      case OP_END:
1300      if (md->recursive != NULL && md->recursive->group_num == 0)      if (md->recursive != NULL)
1301        {        {
1302        recursion_info *rec = md->recursive;        recursion_info *rec = md->recursive;
       DPRINTF(("End of pattern in a (?0) recursion\n"));  
1303        md->recursive = rec->prevrec;        md->recursive = rec->prevrec;
1304        memmove(md->offset_vector, rec->offset_save,        memmove(md->offset_vector, rec->offset_save,
1305          rec->saved_max * sizeof(int));          rec->saved_max * sizeof(int));
1306        md->start_match = rec->save_start;        offset_top = rec->save_offset_top;
1307        ims = original_ims;        if (rec->group_num == 0)
1308        ecode = rec->after_call;          {
1309        break;          ecode = rec->after_call;
1310            break;
1311            }
1312        }        }
1313    
1314      /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty      /* Otherwise, if we have matched an empty string, fail if PCRE_NOTEMPTY is
1315      string - backtracking will then try other alternatives, if any. */      set, or if PCRE_NOTEMPTY_ATSTART is set and we have matched at the start of
1316        the subject. In both cases, backtracking will then try other alternatives,
1317        if any. */
1318    
1319        else if (eptr == mstart &&
1320            (md->notempty ||
1321              (md->notempty_atstart &&
1322                mstart == md->start_subject + md->start_offset)))
1323          MRRETURN(MATCH_NOMATCH);
1324    
1325        /* Otherwise, we have a match. */
1326    
1327        md->end_match_ptr = eptr;           /* Record where we ended */
1328        md->end_offset_top = offset_top;    /* and how many extracts were taken */
1329        md->start_match_ptr = mstart;       /* and the start (\K can modify) */
1330    
1331      if (md->notempty && eptr == md->start_match) RRETURN(MATCH_NOMATCH);      /* For some reason, the macros don't work properly if an expression is
1332      md->end_match_ptr = eptr;          /* Record where we ended */      given as the argument to MRRETURN when the heap is in use. */
1333      md->end_offset_top = offset_top;   /* and how many extracts were taken */  
1334      RRETURN(MATCH_MATCH);      rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
1335        MRRETURN(rrc);
     /* Change option settings */  
   
     case OP_OPT:  
     ims = ecode[1];  
     ecode += 2;  
     DPRINTF(("ims set to %02lx\n", ims));  
     break;  
1336    
1337      /* Assertion brackets. Check the alternative branches in turn - the      /* Assertion brackets. Check the alternative branches in turn - the
1338      matching won't pass the KET for an assertion. If any one branch matches,      matching won't pass the KET for an assertion. If any one branch matches,
1339      the assertion is true. Lookbehind assertions have an OP_REVERSE item at the      the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
1340      start of each branch to move the current point backwards, so the code at      start of each branch to move the current point backwards, so the code at
1341      this level is identical to the lookahead case. */      this level is identical to the lookahead case. When the assertion is part
1342        of a condition, we want to return immediately afterwards. The caller of
1343        this incarnation of the match() function will have set MATCH_CONDASSERT in
1344        md->match_function type, and one of these opcodes will be the first opcode
1345        that is processed. We use a local variable that is preserved over calls to
1346        match() to remember this case. */
1347    
1348      case OP_ASSERT:      case OP_ASSERT:
1349      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1350        if (md->match_function_type == MATCH_CONDASSERT)
1351          {
1352          condassert = TRUE;
1353          md->match_function_type = 0;
1354          }
1355        else condassert = FALSE;
1356    
1357      do      do
1358        {        {
1359        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
1360          match_isgroup);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1361        if (rrc == MATCH_MATCH) break;          {
1362        if (rrc != MATCH_NOMATCH) RRETURN(rrc);          mstart = md->start_match_ptr;   /* In case \K reset it */
1363            break;
1364            }
1365          if (rrc != MATCH_NOMATCH &&
1366              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1367            RRETURN(rrc);
1368        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1369        }        }
1370      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1371      if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);  
1372        if (*ecode == OP_KET) MRRETURN(MATCH_NOMATCH);
1373    
1374      /* If checking an assertion for a condition, return MATCH_MATCH. */      /* If checking an assertion for a condition, return MATCH_MATCH. */
1375    
1376      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);
1377    
1378      /* Continue from after the assertion, updating the offsets high water      /* Continue from after the assertion, updating the offsets high water
1379      mark, since extracts may have been taken during the assertion. */      mark, since extracts may have been taken during the assertion. */
# Line 766  for (;;) Line 1383  for (;;)
1383      offset_top = md->end_offset_top;      offset_top = md->end_offset_top;
1384      continue;      continue;
1385    
1386      /* Negative assertion: all branches must fail to match */      /* Negative assertion: all branches must fail to match. Encountering SKIP,
1387        PRUNE, or COMMIT means we must assume failure without checking subsequent
1388        branches. */
1389    
1390      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1391      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1392        if (md->match_function_type == MATCH_CONDASSERT)
1393          {
1394          condassert = TRUE;
1395          md->match_function_type = 0;
1396          }
1397        else condassert = FALSE;
1398    
1399      do      do
1400        {        {
1401        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
1402          match_isgroup);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) MRRETURN(MATCH_NOMATCH);
1403        if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);        if (rrc == MATCH_SKIP || rrc == MATCH_PRUNE || rrc == MATCH_COMMIT)
1404        if (rrc != MATCH_NOMATCH) RRETURN(rrc);          {
1405            do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1406            break;
1407            }
1408          if (rrc != MATCH_NOMATCH &&
1409              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1410            RRETURN(rrc);
1411        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1412        }        }
1413      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1414    
1415      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);  /* Condition assertion */
1416    
1417      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1418      continue;      continue;
1419    
# Line 794  for (;;) Line 1426  for (;;)
1426  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1427      if (utf8)      if (utf8)
1428        {        {
1429        c = GET(ecode,1);        i = GET(ecode, 1);
1430        for (i = 0; i < c; i++)        while (i-- > 0)
1431          {          {
1432          eptr--;          eptr--;
1433          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);          if (eptr < md->start_subject) MRRETURN(MATCH_NOMATCH);
1434          BACKCHAR(eptr)          BACKCHAR(eptr);
1435          }          }
1436        }        }
1437      else      else
# Line 808  for (;;) Line 1440  for (;;)
1440      /* No UTF-8 support, or not in UTF-8 mode: count is byte count */      /* No UTF-8 support, or not in UTF-8 mode: count is byte count */
1441    
1442        {        {
1443        eptr -= GET(ecode,1);        eptr -= GET(ecode, 1);
1444        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);        if (eptr < md->start_subject) MRRETURN(MATCH_NOMATCH);
1445        }        }
1446    
1447      /* Skip to next op code */      /* Save the earliest consulted character, then skip to next op code */
1448    
1449        if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
1450      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1451      break;      break;
1452    
# Line 829  for (;;) Line 1462  for (;;)
1462        cb.callout_number   = ecode[1];        cb.callout_number   = ecode[1];
1463        cb.offset_vector    = md->offset_vector;        cb.offset_vector    = md->offset_vector;
1464        cb.subject          = (PCRE_SPTR)md->start_subject;        cb.subject          = (PCRE_SPTR)md->start_subject;
1465        cb.subject_length   = md->end_subject - md->start_subject;        cb.subject_length   = (int)(md->end_subject - md->start_subject);
1466        cb.start_match      = md->start_match - md->start_subject;        cb.start_match      = (int)(mstart - md->start_subject);
1467        cb.current_position = eptr - md->start_subject;        cb.current_position = (int)(eptr - md->start_subject);
1468        cb.pattern_position = GET(ecode, 2);        cb.pattern_position = GET(ecode, 2);
1469        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
1470        cb.capture_top      = offset_top/2;        cb.capture_top      = offset_top/2;
1471        cb.capture_last     = md->capture_last;        cb.capture_last     = md->capture_last;
1472        cb.callout_data     = md->callout_data;        cb.callout_data     = md->callout_data;
1473        if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);        if ((rrc = (*pcre_callout)(&cb)) > 0) MRRETURN(MATCH_NOMATCH);
1474        if (rrc < 0) RRETURN(rrc);        if (rrc < 0) RRETURN(rrc);
1475        }        }
1476      ecode += 2 + 2*LINK_SIZE;      ecode += 2 + 2*LINK_SIZE;
# Line 865  for (;;) Line 1498  for (;;)
1498      case OP_RECURSE:      case OP_RECURSE:
1499        {        {
1500        callpat = md->start_code + GET(ecode, 1);        callpat = md->start_code + GET(ecode, 1);
1501        new_recursive.group_num = *callpat - OP_BRA;        new_recursive.group_num = (callpat == md->start_code)? 0 :
1502            GET2(callpat, 1 + LINK_SIZE);
       /* For extended extraction brackets (large number), we have to fish out  
       the number from a dummy opcode at the start. */  
   
       if (new_recursive.group_num > EXTRACT_BASIC_MAX)  
         new_recursive.group_num = GET2(callpat, 2+LINK_SIZE);  
1503    
1504        /* Add to "recursing stack" */        /* Add to "recursing stack" */
1505    
# Line 897  for (;;) Line 1525  for (;;)
1525    
1526        memcpy(new_recursive.offset_save, md->offset_vector,        memcpy(new_recursive.offset_save, md->offset_vector,
1527              new_recursive.saved_max * sizeof(int));              new_recursive.saved_max * sizeof(int));
1528        new_recursive.save_start = md->start_match;        new_recursive.save_offset_top = offset_top;
1529        md->start_match = eptr;  
   
1530        /* OK, now we can do the recursion. For each top-level alternative we        /* OK, now we can do the recursion. For each top-level alternative we
1531        restore the offset and recursion data. */        restore the offset and recursion data. */
1532    
1533        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1534          cbegroup = (*callpat >= OP_SBRA);
1535        do        do
1536          {          {
1537          RMATCH(rrc, eptr, callpat + 1 + LINK_SIZE, offset_top, md, ims,          if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
1538              eptrb, match_isgroup);          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,
1539          if (rrc == MATCH_MATCH)            md, eptrb, RM6);
1540            if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1541            {            {
1542            DPRINTF(("Recursion matched\n"));            DPRINTF(("Recursion matched\n"));
1543            md->recursive = new_recursive.prevrec;            md->recursive = new_recursive.prevrec;
1544            if (new_recursive.offset_save != stacksave)            if (new_recursive.offset_save != stacksave)
1545              (pcre_free)(new_recursive.offset_save);              (pcre_free)(new_recursive.offset_save);
1546            RRETURN(MATCH_MATCH);            MRRETURN(MATCH_MATCH);
1547            }            }
1548          else if (rrc != MATCH_NOMATCH)          else if (rrc != MATCH_NOMATCH &&
1549                    (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1550            {            {
1551            DPRINTF(("Recursion gave error %d\n", rrc));            DPRINTF(("Recursion gave error %d\n", rrc));
1552              if (new_recursive.offset_save != stacksave)
1553                (pcre_free)(new_recursive.offset_save);
1554            RRETURN(rrc);            RRETURN(rrc);
1555            }            }
1556    
# Line 933  for (;;) Line 1565  for (;;)
1565        md->recursive = new_recursive.prevrec;        md->recursive = new_recursive.prevrec;
1566        if (new_recursive.offset_save != stacksave)        if (new_recursive.offset_save != stacksave)
1567          (pcre_free)(new_recursive.offset_save);          (pcre_free)(new_recursive.offset_save);
1568        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
1569        }        }
1570      /* Control never reaches here */      /* Control never reaches here */
1571    
# Line 942  for (;;) Line 1574  for (;;)
1574      a move back into the brackets. Friedl calls these "atomic" subpatterns.      a move back into the brackets. Friedl calls these "atomic" subpatterns.
1575      Check the alternative branches in turn - the matching won't pass the KET      Check the alternative branches in turn - the matching won't pass the KET
1576      for this kind of subpattern. If any one branch matches, we carry on as at      for this kind of subpattern. If any one branch matches, we carry on as at
1577      the end of a normal bracket, leaving the subject pointer. */      the end of a normal bracket, leaving the subject pointer, but resetting
1578        the start-of-match value in case it was changed by \K. */
1579    
1580      case OP_ONCE:      case OP_ONCE:
1581        {      prev = ecode;
1582        prev = ecode;      saved_eptr = eptr;
       saved_eptr = eptr;  
1583    
1584        do      do
1585          {
1586          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
1587          if (rrc == MATCH_MATCH)  /* Note: _not_ MATCH_ACCEPT */
1588          {          {
1589          RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,          mstart = md->start_match_ptr;
1590            eptrb, match_isgroup);          break;
         if (rrc == MATCH_MATCH) break;  
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
         ecode += GET(ecode,1);  
1591          }          }
1592        while (*ecode == OP_ALT);        if (rrc != MATCH_NOMATCH &&
1593              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1594        /* If hit the end of the group (which could be repeated), fail */          RRETURN(rrc);
1595          ecode += GET(ecode,1);
1596          }
1597        while (*ecode == OP_ALT);
1598    
1599        if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);      /* If hit the end of the group (which could be repeated), fail */
1600    
1601        /* Continue as from after the assertion, updating the offsets high water      if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
       mark, since extracts may have been taken. */  
1602    
1603        do ecode += GET(ecode,1); while (*ecode == OP_ALT);      /* Continue as from after the assertion, updating the offsets high water
1604        mark, since extracts may have been taken. */
1605    
1606        offset_top = md->end_offset_top;      do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
       eptr = md->end_match_ptr;  
1607    
1608        /* For a non-repeating ket, just continue at this level. This also      offset_top = md->end_offset_top;
1609        happens for a repeating ket if no characters were matched in the group.      eptr = md->end_match_ptr;
       This is the forcible breaking of infinite loops as implemented in Perl  
       5.005. If there is an options reset, it will get obeyed in the normal  
       course of events. */  
1610    
1611        if (*ecode == OP_KET || eptr == saved_eptr)      /* For a non-repeating ket, just continue at this level. This also
1612          {      happens for a repeating ket if no characters were matched in the group.
1613          ecode += 1+LINK_SIZE;      This is the forcible breaking of infinite loops as implemented in Perl
1614          break;      5.005. If there is an options reset, it will get obeyed in the normal
1615          }      course of events. */
1616    
1617        /* The repeating kets try the rest of the pattern or restart from the      if (*ecode == OP_KET || eptr == saved_eptr)
1618        preceding bracket, in the appropriate order. We need to reset any options        {
1619        that changed within the bracket before re-running it, so check the next        ecode += 1+LINK_SIZE;
1620        opcode. */        break;
1621          }
1622    
1623        if (ecode[1+LINK_SIZE] == OP_OPT)      /* The repeating kets try the rest of the pattern or restart from the
1624          {      preceding bracket, in the appropriate order. The second "call" of match()
1625          ims = (ims & ~PCRE_IMS) | ecode[4];      uses tail recursion, to avoid using another stack frame. */
         DPRINTF(("ims set to %02lx at group repeat\n", ims));  
         }  
1626    
1627        if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1628          {        {
1629          RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM8);
1630          if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1631          RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);        ecode = prev;
1632          if (rrc != MATCH_NOMATCH) RRETURN(rrc);        goto TAIL_RECURSE;
         }  
       else  /* OP_KETRMAX */  
         {  
         RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);  
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
         RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);  
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
         }  
1633        }        }
1634      RRETURN(MATCH_NOMATCH);      else  /* OP_KETRMAX */
1635          {
1636          md->match_function_type = MATCH_CBEGROUP;
1637          RMATCH(eptr, prev, offset_top, md, eptrb, RM9);
1638          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1639          ecode += 1 + LINK_SIZE;
1640          goto TAIL_RECURSE;
1641          }
1642        /* Control never gets here */
1643    
1644      /* An alternation is the end of a branch; scan along to find the end of the      /* An alternation is the end of a branch; scan along to find the end of the
1645      bracketed group and go to there. */      bracketed group and go to there. */
# Line 1018  for (;;) Line 1648  for (;;)
1648      do ecode += GET(ecode,1); while (*ecode == OP_ALT);      do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1649      break;      break;
1650    
1651      /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating      /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
1652      that it may occur zero times. It may repeat infinitely, or not at all -      indicating that it may occur zero times. It may repeat infinitely, or not
1653      i.e. it could be ()* or ()? in the pattern. Brackets with fixed upper      at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets
1654      repeat limits are compiled as a number of copies, with the optional ones      with fixed upper repeat limits are compiled as a number of copies, with the
1655      preceded by BRAZERO or BRAMINZERO. */      optional ones preceded by BRAZERO or BRAMINZERO. */
1656    
1657      case OP_BRAZERO:      case OP_BRAZERO:
1658        {      next = ecode + 1;
1659        next = ecode+1;      RMATCH(eptr, next, offset_top, md, eptrb, RM10);
1660        RMATCH(rrc, eptr, next, offset_top, md, ims, eptrb, match_isgroup);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1661        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      do next += GET(next, 1); while (*next == OP_ALT);
1662        do next += GET(next,1); while (*next == OP_ALT);      ecode = next + 1 + LINK_SIZE;
       ecode = next + 1+LINK_SIZE;  
       }  
1663      break;      break;
1664    
1665      case OP_BRAMINZERO:      case OP_BRAMINZERO:
1666        {      next = ecode + 1;
1667        next = ecode+1;      do next += GET(next, 1); while (*next == OP_ALT);
1668        do next += GET(next,1); while (*next == OP_ALT);      RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
1669        RMATCH(rrc, eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb,      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1670          match_isgroup);      ecode++;
1671        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      break;
1672        ecode++;  
1673        }      case OP_SKIPZERO:
1674        next = ecode+1;
1675        do next += GET(next,1); while (*next == OP_ALT);
1676        ecode = next + 1 + LINK_SIZE;
1677      break;      break;
1678    
1679        /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
1680        here; just jump to the group, with allow_zero set TRUE. */
1681    
1682        case OP_BRAPOSZERO:
1683        op = *(++ecode);
1684        allow_zero = TRUE;
1685        if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
1686          goto POSSESSIVE_NON_CAPTURE;
1687    
1688      /* End of a group, repeated or non-repeating. If we are at the end of      /* End of a group, repeated or non-repeating. */
     an assertion "group", stop matching and return MATCH_MATCH, but record the  
     current high water mark for use by positive assertions. Do this also  
     for the "once" (not-backup up) groups. */  
1689    
1690      case OP_KET:      case OP_KET:
1691      case OP_KETRMIN:      case OP_KETRMIN:
1692      case OP_KETRMAX:      case OP_KETRMAX:
1693        {      case OP_KETRPOS:
1694        prev = ecode - GET(ecode, 1);      prev = ecode - GET(ecode, 1);
       saved_eptr = eptrb->epb_saved_eptr;  
1695    
1696        /* Back up the stack of bracket start pointers. */      /* If this was a group that remembered the subject start, in order to break
1697        infinite repeats of empty string matches, retrieve the subject start from
1698        the chain. Otherwise, set it NULL. */
1699    
1700        eptrb = eptrb->epb_prev;      if (*prev >= OP_SBRA)
1701          {
1702        if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||        saved_eptr = eptrb->epb_saved_eptr;   /* Value at start of group */
1703            *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||        eptrb = eptrb->epb_prev;              /* Backup to previous group */
1704            *prev == OP_ONCE)        }
1705          {      else saved_eptr = NULL;
         md->end_match_ptr = eptr;      /* For ONCE */  
         md->end_offset_top = offset_top;  
         RRETURN(MATCH_MATCH);  
         }  
1706    
1707        /* In all other cases except a conditional group we have to check the      /* If we are at the end of an assertion group or an atomic group, stop
1708        group number back at the start and if necessary complete handling an      matching and return MATCH_MATCH, but record the current high water mark for
1709        extraction by setting the offsets and bumping the high water mark. */      use by positive assertions. We also need to record the match start in case
1710        it was changed by \K. */
1711    
1712        if (*prev != OP_COND)      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
1713          {          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||
1714          number = *prev - OP_BRA;          *prev == OP_ONCE)
1715          {
1716          md->end_match_ptr = eptr;      /* For ONCE */
1717          md->end_offset_top = offset_top;
1718          md->start_match_ptr = mstart;
1719          MRRETURN(MATCH_MATCH);
1720          }
1721    
1722          /* For extended extraction brackets (large number), we have to fish out      /* For capturing groups we have to check the group number back at the start
1723          the number from a dummy opcode at the start. */      and if necessary complete handling an extraction by setting the offsets and
1724        bumping the high water mark. Note that whole-pattern recursion is coded as
1725        a recurse into group 0, so it won't be picked up here. Instead, we catch it
1726        when the OP_END is reached. Other recursion is handled here. */
1727    
1728          if (number > EXTRACT_BASIC_MAX) number = GET2(prev, 2+LINK_SIZE);      if (*prev == OP_CBRA || *prev == OP_SCBRA ||
1729          offset = number << 1;          *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
1730          {
1731          number = GET2(prev, 1+LINK_SIZE);
1732          offset = number << 1;
1733    
1734  #ifdef DEBUG  #ifdef PCRE_DEBUG
1735          printf("end bracket %d", number);        printf("end bracket %d", number);
1736          printf("\n");        printf("\n");
1737  #endif  #endif
1738    
1739          /* Test for a numbered group. This includes groups called as a result        md->capture_last = number;
1740          of recursion. Note that whole-pattern recursion is coded as a recurse        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1741          into group 0, so it won't be picked up here. Instead, we catch it when          {
1742          the OP_END is reached. */          md->offset_vector[offset] =
1743              md->offset_vector[md->offset_end - number];
1744          if (number > 0)          md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1745            {          if (offset_top <= offset) offset_top = offset + 2;
           md->capture_last = number;  
           if (offset >= md->offset_max) md->offset_overflow = TRUE; else  
             {  
             md->offset_vector[offset] =  
               md->offset_vector[md->offset_end - number];  
             md->offset_vector[offset+1] = eptr - md->start_subject;  
             if (offset_top <= offset) offset_top = offset + 2;  
             }  
   
           /* Handle a recursively called group. Restore the offsets  
           appropriately and continue from after the call. */  
   
           if (md->recursive != NULL && md->recursive->group_num == number)  
             {  
             recursion_info *rec = md->recursive;  
             DPRINTF(("Recursion (%d) succeeded - continuing\n", number));  
             md->recursive = rec->prevrec;  
             md->start_match = rec->save_start;  
             memcpy(md->offset_vector, rec->offset_save,  
               rec->saved_max * sizeof(int));  
             ecode = rec->after_call;  
             ims = original_ims;  
             break;  
             }  
           }  
1746          }          }
1747    
1748        /* Reset the value of the ims flags, in case they got changed during        /* Handle a recursively called group. Restore the offsets
1749        the group. */        appropriately and continue from after the call. */
1750    
1751        ims = original_ims;        if (md->recursive != NULL && md->recursive->group_num == number)
       DPRINTF(("ims reset to %02lx\n", ims));  
   
       /* For a non-repeating ket, just continue at this level. This also  
       happens for a repeating ket if no characters were matched in the group.  
       This is the forcible breaking of infinite loops as implemented in Perl  
       5.005. If there is an options reset, it will get obeyed in the normal  
       course of events. */  
   
       if (*ecode == OP_KET || eptr == saved_eptr)  
1752          {          {
1753          ecode += 1 + LINK_SIZE;          recursion_info *rec = md->recursive;
1754            DPRINTF(("Recursion (%d) succeeded - continuing\n", number));
1755            md->recursive = rec->prevrec;
1756            memcpy(md->offset_vector, rec->offset_save,
1757              rec->saved_max * sizeof(int));
1758            offset_top = rec->save_offset_top;
1759            ecode = rec->after_call;
1760          break;          break;
1761          }          }
1762          }
1763    
1764        /* The repeating kets try the rest of the pattern or restart from the      /* For a non-repeating ket, just continue at this level. This also
1765        preceding bracket, in the appropriate order. */      happens for a repeating ket if no characters were matched in the group.
1766        This is the forcible breaking of infinite loops as implemented in Perl
1767        5.005. If there is an options reset, it will get obeyed in the normal
1768        course of events. */
1769    
1770        if (*ecode == OP_KETRMIN)      if (*ecode == OP_KET || eptr == saved_eptr)
1771          {        {
1772          RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);        ecode += 1 + LINK_SIZE;
1773          if (rrc != MATCH_NOMATCH) RRETURN(rrc);        break;
1774          RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);        }
1775          if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
1776          }      /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
1777        else  /* OP_KETRMAX */      and return the MATCH_KETRPOS. This makes it possible to do the repeats one
1778        at a time from the outer level, thus saving stack. */
1779    
1780        if (*ecode == OP_KETRPOS)
1781          {
1782          md->end_match_ptr = eptr;
1783          md->end_offset_top = offset_top;
1784          RRETURN(MATCH_KETRPOS);
1785          }
1786    
1787        /* The normal repeating kets try the rest of the pattern or restart from
1788        the preceding bracket, in the appropriate order. In the second case, we can
1789        use tail recursion to avoid using another stack frame, unless we have an
1790        unlimited repeat of a group that can match an empty string. */
1791    
1792        if (*ecode == OP_KETRMIN)
1793          {
1794          RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
1795          if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1796          if (*prev >= OP_SBRA)    /* Could match an empty string */
1797          {          {
1798          RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);          md->match_function_type = MATCH_CBEGROUP;
1799          if (rrc != MATCH_NOMATCH) RRETURN(rrc);          RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
1800          RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);          RRETURN(rrc);
         if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
1801          }          }
1802          ecode = prev;
1803          goto TAIL_RECURSE;
1804        }        }
1805        else  /* OP_KETRMAX */
     RRETURN(MATCH_NOMATCH);  
   
     /* Start of subject unless notbol, or after internal newline if multiline */  
   
     case OP_CIRC:  
     if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);  
     if ((ims & PCRE_MULTILINE) != 0)  
1806        {        {
1807        if (eptr != md->start_subject && eptr[-1] != NEWLINE)        if (*prev >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1808          RRETURN(MATCH_NOMATCH);        RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
1809        ecode++;        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1810        break;        ecode += 1 + LINK_SIZE;
1811          goto TAIL_RECURSE;
1812        }        }
1813      /* ... else fall through */      /* Control never gets here */
1814    
1815        /* Not multiline mode: start of subject assertion, unless notbol. */
1816    
1817        case OP_CIRC:
1818        if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
1819    
1820      /* Start of subject assertion */      /* Start of subject assertion */
1821    
1822      case OP_SOD:      case OP_SOD:
1823      if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);      if (eptr != md->start_subject) MRRETURN(MATCH_NOMATCH);
1824        ecode++;
1825        break;
1826    
1827        /* Multiline mode: start of subject unless notbol, or after any newline. */
1828    
1829        case OP_CIRCM:
1830        if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
1831        if (eptr != md->start_subject &&
1832            (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
1833          MRRETURN(MATCH_NOMATCH);
1834      ecode++;      ecode++;
1835      break;      break;
1836    
1837      /* Start of match assertion */      /* Start of match assertion */
1838    
1839      case OP_SOM:      case OP_SOM:
1840      if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);      if (eptr != md->start_subject + md->start_offset) MRRETURN(MATCH_NOMATCH);
1841      ecode++;      ecode++;
1842      break;      break;
1843    
1844      /* 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. */  
1845    
1846      case OP_DOLL:      case OP_SET_SOM:
1847      if ((ims & PCRE_MULTILINE) != 0)      mstart = eptr;
1848        {      ecode++;
1849        if (eptr < md->end_subject)      break;
1850          { if (*eptr != NEWLINE) RRETURN(MATCH_NOMATCH); }  
1851        else      /* Multiline mode: assert before any newline, or before end of subject
1852          { if (md->noteol) RRETURN(MATCH_NOMATCH); }      unless noteol is set. */
1853        ecode++;  
1854        break;      case OP_DOLLM:
1855        }      if (eptr < md->end_subject)
1856          { if (!IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH); }
1857      else      else
1858        {        {
1859        if (md->noteol) RRETURN(MATCH_NOMATCH);        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1860        if (!md->endonly)        SCHECK_PARTIAL();
         {  
         if (eptr < md->end_subject - 1 ||  
            (eptr == md->end_subject - 1 && *eptr != NEWLINE))  
           RRETURN(MATCH_NOMATCH);  
         ecode++;  
         break;  
         }  
1861        }        }
1862      /* ... else fall through */      ecode++;
1863        break;
1864    
1865        /* Not multiline mode: assert before a terminating newline or before end of
1866        subject unless noteol is set. */
1867    
1868        case OP_DOLL:
1869        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1870        if (!md->endonly) goto ASSERT_NL_OR_EOS;
1871    
1872        /* ... else fall through for endonly */
1873    
1874      /* End of subject assertion (\z) */      /* End of subject assertion (\z) */
1875    
1876      case OP_EOD:      case OP_EOD:
1877      if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr < md->end_subject) MRRETURN(MATCH_NOMATCH);
1878        SCHECK_PARTIAL();
1879      ecode++;      ecode++;
1880      break;      break;
1881    
1882      /* End of subject or ending \n assertion (\Z) */      /* End of subject or ending \n assertion (\Z) */
1883    
1884      case OP_EODN:      case OP_EODN:
1885      if (eptr < md->end_subject - 1 ||      ASSERT_NL_OR_EOS:
1886         (eptr == md->end_subject - 1 && *eptr != NEWLINE)) RRETURN(MATCH_NOMATCH);      if (eptr < md->end_subject &&
1887            (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
1888          MRRETURN(MATCH_NOMATCH);
1889    
1890        /* Either at end of string or \n before end. */
1891    
1892        SCHECK_PARTIAL();
1893      ecode++;      ecode++;
1894      break;      break;
1895    
# Line 1239  for (;;) Line 1901  for (;;)
1901    
1902        /* Find out if the previous and current characters are "word" characters.        /* Find out if the previous and current characters are "word" characters.
1903        It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to        It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to
1904        be "non-word" characters. */        be "non-word" characters. Remember the earliest consulted character for
1905          partial matching. */
1906    
1907  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1908        if (utf8)        if (utf8)
1909          {          {
1910            /* Get status of previous character */
1911    
1912          if (eptr == md->start_subject) prev_is_word = FALSE; else          if (eptr == md->start_subject) prev_is_word = FALSE; else
1913            {            {
1914            const uschar *lastptr = eptr - 1;            USPTR lastptr = eptr - 1;
1915            while((*lastptr & 0xc0) == 0x80) lastptr--;            while((*lastptr & 0xc0) == 0x80) lastptr--;
1916              if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
1917            GETCHAR(c, lastptr);            GETCHAR(c, lastptr);
1918    #ifdef SUPPORT_UCP
1919              if (md->use_ucp)
1920                {
1921                if (c == '_') prev_is_word = TRUE; else
1922                  {
1923                  int cat = UCD_CATEGORY(c);
1924                  prev_is_word = (cat == ucp_L || cat == ucp_N);
1925                  }
1926                }
1927              else
1928    #endif
1929            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
1930            }            }
1931          if (eptr >= md->end_subject) cur_is_word = FALSE; else  
1932            /* Get status of next character */
1933    
1934            if (eptr >= md->end_subject)
1935              {
1936              SCHECK_PARTIAL();
1937              cur_is_word = FALSE;
1938              }
1939            else
1940            {            {
1941            GETCHAR(c, eptr);            GETCHAR(c, eptr);
1942    #ifdef SUPPORT_UCP
1943              if (md->use_ucp)
1944                {
1945                if (c == '_') cur_is_word = TRUE; else
1946                  {
1947                  int cat = UCD_CATEGORY(c);
1948                  cur_is_word = (cat == ucp_L || cat == ucp_N);
1949                  }
1950                }
1951              else
1952    #endif
1953            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
1954            }            }
1955          }          }
1956        else        else
1957  #endif  #endif
1958    
1959        /* More streamlined when not in UTF-8 mode */        /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for
1960          consistency with the behaviour of \w we do use it in this case. */
1961    
1962          {          {
1963          prev_is_word = (eptr != md->start_subject) &&          /* Get status of previous character */
1964            ((md->ctypes[eptr[-1]] & ctype_word) != 0);  
1965          cur_is_word = (eptr < md->end_subject) &&          if (eptr == md->start_subject) prev_is_word = FALSE; else
1966            ((md->ctypes[*eptr] & ctype_word) != 0);            {
1967              if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
1968    #ifdef SUPPORT_UCP
1969              if (md->use_ucp)
1970                {
1971                c = eptr[-1];
1972                if (c == '_') prev_is_word = TRUE; else
1973                  {
1974                  int cat = UCD_CATEGORY(c);
1975                  prev_is_word = (cat == ucp_L || cat == ucp_N);
1976                  }
1977                }
1978              else
1979    #endif
1980              prev_is_word = ((md->ctypes[eptr[-1]] & ctype_word) != 0);
1981              }
1982    
1983            /* Get status of next character */
1984    
1985            if (eptr >= md->end_subject)
1986              {
1987              SCHECK_PARTIAL();
1988              cur_is_word = FALSE;
1989              }
1990            else
1991    #ifdef SUPPORT_UCP
1992            if (md->use_ucp)
1993              {
1994              c = *eptr;
1995              if (c == '_') cur_is_word = TRUE; else
1996                {
1997                int cat = UCD_CATEGORY(c);
1998                cur_is_word = (cat == ucp_L || cat == ucp_N);
1999                }
2000              }
2001            else
2002    #endif
2003            cur_is_word = ((md->ctypes[*eptr] & ctype_word) != 0);
2004          }          }
2005    
2006        /* Now see if the situation is what we want */        /* Now see if the situation is what we want */
2007    
2008        if ((*ecode++ == OP_WORD_BOUNDARY)?        if ((*ecode++ == OP_WORD_BOUNDARY)?
2009             cur_is_word == prev_is_word : cur_is_word != prev_is_word)             cur_is_word == prev_is_word : cur_is_word != prev_is_word)
2010          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2011        }        }
2012      break;      break;
2013    
2014      /* Match a single character type; inline for speed */      /* Match a single character type; inline for speed */
2015    
2016      case OP_ANY:      case OP_ANY:
2017      if ((ims & PCRE_DOTALL) == 0 && eptr < md->end_subject && *eptr == NEWLINE)      if (IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH);
2018        RRETURN(MATCH_NOMATCH);      /* Fall through */
2019      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);  
2020  #ifdef SUPPORT_UTF8      case OP_ALLANY:
2021      if (utf8)      if (eptr++ >= md->end_subject)
2022        while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;        {
2023  #endif        SCHECK_PARTIAL();
2024          MRRETURN(MATCH_NOMATCH);
2025          }
2026        if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
2027      ecode++;      ecode++;
2028      break;      break;
2029    
# Line 1294  for (;;) Line 2031  for (;;)
2031      any byte, even newline, independent of the setting of PCRE_DOTALL. */      any byte, even newline, independent of the setting of PCRE_DOTALL. */
2032    
2033      case OP_ANYBYTE:      case OP_ANYBYTE:
2034      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr++ >= md->end_subject)
2035          {
2036          SCHECK_PARTIAL();
2037          MRRETURN(MATCH_NOMATCH);
2038          }
2039      ecode++;      ecode++;
2040      break;      break;
2041    
2042      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2043      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2044          {
2045          SCHECK_PARTIAL();
2046          MRRETURN(MATCH_NOMATCH);
2047          }
2048      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2049      if (      if (
2050  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1307  for (;;) Line 2052  for (;;)
2052  #endif  #endif
2053         (md->ctypes[c] & ctype_digit) != 0         (md->ctypes[c] & ctype_digit) != 0
2054         )         )
2055        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2056      ecode++;      ecode++;
2057      break;      break;
2058    
2059      case OP_DIGIT:      case OP_DIGIT:
2060      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2061          {
2062          SCHECK_PARTIAL();
2063          MRRETURN(MATCH_NOMATCH);
2064          }
2065      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2066      if (      if (
2067  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1320  for (;;) Line 2069  for (;;)
2069  #endif  #endif
2070         (md->ctypes[c] & ctype_digit) == 0         (md->ctypes[c] & ctype_digit) == 0
2071         )         )
2072        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2073      ecode++;      ecode++;
2074      break;      break;
2075    
2076      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2077      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2078          {
2079          SCHECK_PARTIAL();
2080          MRRETURN(MATCH_NOMATCH);
2081          }
2082      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2083      if (      if (
2084  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1333  for (;;) Line 2086  for (;;)
2086  #endif  #endif
2087         (md->ctypes[c] & ctype_space) != 0         (md->ctypes[c] & ctype_space) != 0
2088         )         )
2089        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2090      ecode++;      ecode++;
2091      break;      break;
2092    
2093      case OP_WHITESPACE:      case OP_WHITESPACE:
2094      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2095          {
2096          SCHECK_PARTIAL();
2097          MRRETURN(MATCH_NOMATCH);
2098          }
2099      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2100      if (      if (
2101  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1346  for (;;) Line 2103  for (;;)
2103  #endif  #endif
2104         (md->ctypes[c] & ctype_space) == 0         (md->ctypes[c] & ctype_space) == 0
2105         )         )
2106        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2107      ecode++;      ecode++;
2108      break;      break;
2109    
2110      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2111      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2112          {
2113          SCHECK_PARTIAL();
2114          MRRETURN(MATCH_NOMATCH);
2115          }
2116      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2117      if (      if (
2118  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1359  for (;;) Line 2120  for (;;)
2120  #endif  #endif
2121         (md->ctypes[c] & ctype_word) != 0         (md->ctypes[c] & ctype_word) != 0
2122         )         )
2123        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2124      ecode++;      ecode++;
2125      break;      break;
2126    
2127      case OP_WORDCHAR:      case OP_WORDCHAR:
2128      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2129          {
2130          SCHECK_PARTIAL();
2131          MRRETURN(MATCH_NOMATCH);
2132          }
2133      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2134      if (      if (
2135  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1372  for (;;) Line 2137  for (;;)
2137  #endif  #endif
2138         (md->ctypes[c] & ctype_word) == 0         (md->ctypes[c] & ctype_word) == 0
2139         )         )
2140        RRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
2141        ecode++;
2142        break;
2143    
2144        case OP_ANYNL:
2145        if (eptr >= md->end_subject)
2146          {
2147          SCHECK_PARTIAL();
2148          MRRETURN(MATCH_NOMATCH);
2149          }
2150        GETCHARINCTEST(c, eptr);
2151        switch(c)
2152          {
2153          default: MRRETURN(MATCH_NOMATCH);
2154    
2155          case 0x000d:
2156          if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
2157          break;
2158    
2159          case 0x000a:
2160          break;
2161    
2162          case 0x000b:
2163          case 0x000c:
2164          case 0x0085:
2165          case 0x2028:
2166          case 0x2029:
2167          if (md->bsr_anycrlf) MRRETURN(MATCH_NOMATCH);
2168          break;
2169          }
2170        ecode++;
2171        break;
2172    
2173        case OP_NOT_HSPACE:
2174        if (eptr >= md->end_subject)
2175          {
2176          SCHECK_PARTIAL();
2177          MRRETURN(MATCH_NOMATCH);
2178          }
2179        GETCHARINCTEST(c, eptr);
2180        switch(c)
2181          {
2182          default: break;
2183          case 0x09:      /* HT */
2184          case 0x20:      /* SPACE */
2185          case 0xa0:      /* NBSP */
2186          case 0x1680:    /* OGHAM SPACE MARK */
2187          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
2188          case 0x2000:    /* EN QUAD */
2189          case 0x2001:    /* EM QUAD */
2190          case 0x2002:    /* EN SPACE */
2191          case 0x2003:    /* EM SPACE */
2192          case 0x2004:    /* THREE-PER-EM SPACE */
2193          case 0x2005:    /* FOUR-PER-EM SPACE */
2194          case 0x2006:    /* SIX-PER-EM SPACE */
2195          case 0x2007:    /* FIGURE SPACE */
2196          case 0x2008:    /* PUNCTUATION SPACE */
2197          case 0x2009:    /* THIN SPACE */
2198          case 0x200A:    /* HAIR SPACE */
2199          case 0x202f:    /* NARROW NO-BREAK SPACE */
2200          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
2201          case 0x3000:    /* IDEOGRAPHIC SPACE */
2202          MRRETURN(MATCH_NOMATCH);
2203          }
2204        ecode++;
2205        break;
2206    
2207        case OP_HSPACE:
2208        if (eptr >= md->end_subject)
2209          {
2210          SCHECK_PARTIAL();
2211          MRRETURN(MATCH_NOMATCH);
2212          }
2213        GETCHARINCTEST(c, eptr);
2214        switch(c)
2215          {
2216          default: MRRETURN(MATCH_NOMATCH);
2217          case 0x09:      /* HT */
2218          case 0x20:      /* SPACE */
2219          case 0xa0:      /* NBSP */
2220          case 0x1680:    /* OGHAM SPACE MARK */
2221          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
2222          case 0x2000:    /* EN QUAD */
2223          case 0x2001:    /* EM QUAD */
2224          case 0x2002:    /* EN SPACE */
2225          case 0x2003:    /* EM SPACE */
2226          case 0x2004:    /* THREE-PER-EM SPACE */
2227          case 0x2005:    /* FOUR-PER-EM SPACE */
2228          case 0x2006:    /* SIX-PER-EM SPACE */
2229          case 0x2007:    /* FIGURE SPACE */
2230          case 0x2008:    /* PUNCTUATION SPACE */
2231          case 0x2009:    /* THIN SPACE */
2232          case 0x200A:    /* HAIR SPACE */
2233          case 0x202f:    /* NARROW NO-BREAK SPACE */
2234          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
2235          case 0x3000:    /* IDEOGRAPHIC SPACE */
2236          break;
2237          }
2238        ecode++;
2239        break;
2240    
2241        case OP_NOT_VSPACE:
2242        if (eptr >= md->end_subject)
2243          {
2244          SCHECK_PARTIAL();
2245          MRRETURN(MATCH_NOMATCH);
2246          }
2247        GETCHARINCTEST(c, eptr);
2248        switch(c)
2249          {
2250          default: break;
2251          case 0x0a:      /* LF */
2252          case 0x0b:      /* VT */
2253          case 0x0c:      /* FF */
2254          case 0x0d:      /* CR */
2255          case 0x85:      /* NEL */
2256          case 0x2028:    /* LINE SEPARATOR */
2257          case 0x2029:    /* PARAGRAPH SEPARATOR */
2258          MRRETURN(MATCH_NOMATCH);
2259          }
2260        ecode++;
2261        break;
2262    
2263        case OP_VSPACE:
2264        if (eptr >= md->end_subject)
2265          {
2266          SCHECK_PARTIAL();
2267          MRRETURN(MATCH_NOMATCH);
2268          }
2269        GETCHARINCTEST(c, eptr);
2270        switch(c)
2271          {
2272          default: MRRETURN(MATCH_NOMATCH);
2273          case 0x0a:      /* LF */
2274          case 0x0b:      /* VT */
2275          case 0x0c:      /* FF */
2276          case 0x0d:      /* CR */
2277          case 0x85:      /* NEL */
2278          case 0x2028:    /* LINE SEPARATOR */
2279          case 0x2029:    /* PARAGRAPH SEPARATOR */
2280          break;
2281          }
2282      ecode++;      ecode++;
2283      break;      break;
2284    
# Line 1382  for (;;) Line 2288  for (;;)
2288    
2289      case OP_PROP:      case OP_PROP:
2290      case OP_NOTPROP:      case OP_NOTPROP:
2291      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2292          {
2293          SCHECK_PARTIAL();
2294          MRRETURN(MATCH_NOMATCH);
2295          }
2296      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2297        {        {
2298        int chartype, script;        const ucd_record *prop = GET_UCD(c);
       int category = _pcre_ucp_findprop(c, &chartype, &script);  
2299    
2300        switch(ecode[1])        switch(ecode[1])
2301          {          {
2302          case PT_ANY:          case PT_ANY:
2303          if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);          if (op == OP_NOTPROP) MRRETURN(MATCH_NOMATCH);
2304          break;          break;
2305    
2306          case PT_LAMP:          case PT_LAMP:
2307          if ((chartype == ucp_Lu ||          if ((prop->chartype == ucp_Lu ||
2308               chartype == ucp_Ll ||               prop->chartype == ucp_Ll ||
2309               chartype == ucp_Lt) == (op == OP_NOTPROP))               prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
2310            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2311           break;          break;
2312    
2313          case PT_GC:          case PT_GC:
2314          if ((ecode[2] != category) == (op == OP_PROP))          if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))
2315            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2316          break;          break;
2317    
2318          case PT_PC:          case PT_PC:
2319          if ((ecode[2] != chartype) == (op == OP_PROP))          if ((ecode[2] != prop->chartype) == (op == OP_PROP))
2320            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2321          break;          break;
2322    
2323          case PT_SC:          case PT_SC:
2324          if ((ecode[2] != script) == (op == OP_PROP))          if ((ecode[2] != prop->script) == (op == OP_PROP))
2325            RRETURN(MATCH_NOMATCH);            MRRETURN(MATCH_NOMATCH);
2326            break;
2327    
2328            /* These are specials */
2329    
2330            case PT_ALNUM:
2331            if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2332                 _pcre_ucp_gentype[prop->chartype] == ucp_N) == (op == OP_NOTPROP))
2333              MRRETURN(MATCH_NOMATCH);
2334          break;          break;
2335    
2336            case PT_SPACE:    /* Perl space */
2337            if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2338                 c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2339                   == (op == OP_NOTPROP))
2340              MRRETURN(MATCH_NOMATCH);
2341            break;
2342    
2343            case PT_PXSPACE:  /* POSIX space */
2344            if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2345                 c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2346                 c == CHAR_FF || c == CHAR_CR)
2347                   == (op == OP_NOTPROP))
2348              MRRETURN(MATCH_NOMATCH);
2349            break;
2350    
2351            case PT_WORD:
2352            if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2353                 _pcre_ucp_gentype[prop->chartype] == ucp_N ||
2354                 c == CHAR_UNDERSCORE) == (op == OP_NOTPROP))
2355              MRRETURN(MATCH_NOMATCH);
2356            break;
2357    
2358            /* This should never occur */
2359    
2360          default:          default:
2361          RRETURN(PCRE_ERROR_INTERNAL);          RRETURN(PCRE_ERROR_INTERNAL);
         break;  
2362          }          }
2363    
2364        ecode += 3;        ecode += 3;
# Line 1429  for (;;) Line 2369  for (;;)
2369      is in the binary; otherwise a compile-time error occurs. */      is in the binary; otherwise a compile-time error occurs. */
2370    
2371      case OP_EXTUNI:      case OP_EXTUNI:
2372      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject)
2373          {
2374          SCHECK_PARTIAL();
2375          MRRETURN(MATCH_NOMATCH);
2376          }
2377      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
2378        {        {
2379        int chartype, script;        int category = UCD_CATEGORY(c);
2380        int category = _pcre_ucp_findprop(c, &chartype, &script);        if (category == ucp_M) MRRETURN(MATCH_NOMATCH);
       if (category == ucp_M) RRETURN(MATCH_NOMATCH);  
2381        while (eptr < md->end_subject)        while (eptr < md->end_subject)
2382          {          {
2383          int len = 1;          int len = 1;
# Line 1442  for (;;) Line 2385  for (;;)
2385            {            {
2386            GETCHARLEN(c, eptr, len);            GETCHARLEN(c, eptr, len);
2387            }            }
2388          category = _pcre_ucp_findprop(c, &chartype, &script);          category = UCD_CATEGORY(c);
2389          if (category != ucp_M) break;          if (category != ucp_M) break;
2390          eptr += len;          eptr += len;
2391          }          }
# Line 1461  for (;;) Line 2404  for (;;)
2404      loops). */      loops). */
2405    
2406      case OP_REF:      case OP_REF:
2407        {      case OP_REFI:
2408        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */      caseless = op == OP_REFI;
2409        ecode += 3;                                 /* Advance past item */      offset = GET2(ecode, 1) << 1;               /* Doubled ref number */
2410        ecode += 3;
2411    
2412        /* If the reference is unset, set the length to be longer than the amount      /* If the reference is unset, there are two possibilities:
       of subject left; this ensures that every attempt at a match fails. We  
       can't just fail here, because of the possibility of quantifiers with zero  
       minima. */  
   
       length = (offset >= offset_top || md->offset_vector[offset] < 0)?  
         md->end_subject - eptr + 1 :  
         md->offset_vector[offset+1] - md->offset_vector[offset];  
2413    
2414        /* Set up for repetition, or handle the non-repeated case */      (a) In the default, Perl-compatible state, set the length negative;
2415        this ensures that every attempt at a match fails. We can't just fail
2416        here, because of the possibility of quantifiers with zero minima.
2417    
2418        switch (*ecode)      (b) If the JavaScript compatibility flag is set, set the length to zero
2419          {      so that the back reference matches an empty string.
         case OP_CRSTAR:  
         case OP_CRMINSTAR:  
         case OP_CRPLUS:  
         case OP_CRMINPLUS:  
         case OP_CRQUERY:  
         case OP_CRMINQUERY:  
         c = *ecode++ - OP_CRSTAR;  
         minimize = (c & 1) != 0;  
         min = rep_min[c];                 /* Pick up values from tables; */  
         max = rep_max[c];                 /* zero for max => infinity */  
         if (max == 0) max = INT_MAX;  
         break;  
2420    
2421          case OP_CRRANGE:      Otherwise, set the length to the length of what was matched by the
2422          case OP_CRMINRANGE:      referenced subpattern. */
         minimize = (*ecode == OP_CRMINRANGE);  
         min = GET2(ecode, 1);  
         max = GET2(ecode, 3);  
         if (max == 0) max = INT_MAX;  
         ecode += 5;  
         break;  
2423    
2424          default:               /* No repeat follows */      if (offset >= offset_top || md->offset_vector[offset] < 0)
2425          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);        length = (md->jscript_compat)? 0 : -1;
2426          eptr += length;      else
2427          continue;              /* With the main loop */        length = md->offset_vector[offset+1] - md->offset_vector[offset];
         }  
2428    
2429        /* If the length of the reference is zero, just continue with the      /* Set up for repetition, or handle the non-repeated case */
       main loop. */  
2430    
2431        if (length == 0) continue;      switch (*ecode)
2432          {
2433          case OP_CRSTAR:
2434          case OP_CRMINSTAR:
2435          case OP_CRPLUS:
2436          case OP_CRMINPLUS:
2437          case OP_CRQUERY:
2438          case OP_CRMINQUERY:
2439          c = *ecode++ - OP_CRSTAR;
2440          minimize = (c & 1) != 0;
2441          min = rep_min[c];                 /* Pick up values from tables; */
2442          max = rep_max[c];                 /* zero for max => infinity */
2443          if (max == 0) max = INT_MAX;
2444          break;
2445    
2446        /* First, ensure the minimum number of matches are present. We get back        case OP_CRRANGE:
2447        the length of the reference string explicitly rather than passing the        case OP_CRMINRANGE:
2448        address of eptr, so that eptr can be a register variable. */        minimize = (*ecode == OP_CRMINRANGE);
2449          min = GET2(ecode, 1);
2450          max = GET2(ecode, 3);
2451          if (max == 0) max = INT_MAX;
2452          ecode += 5;
2453          break;
2454    
2455        for (i = 1; i <= min; i++)        default:               /* No repeat follows */
2456          if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
2457          {          {
2458          if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);          CHECK_PARTIAL();
2459          eptr += length;          MRRETURN(MATCH_NOMATCH);
2460          }          }
2461          eptr += length;
2462          continue;              /* With the main loop */
2463          }
2464    
2465        /* If min = max, continue at the same level without recursion.      /* Handle repeated back references. If the length of the reference is
2466        They are not both allowed to be zero. */      zero, just continue with the main loop. */
2467    
2468        if (min == max) continue;      if (length == 0) continue;
2469    
2470        /* If minimizing, keep trying and advancing the pointer */      /* First, ensure the minimum number of matches are present. We get back
2471        the length of the reference string explicitly rather than passing the
2472        address of eptr, so that eptr can be a register variable. */
2473    
2474        if (minimize)      for (i = 1; i <= min; i++)
2475          {
2476          int slength;
2477          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2478          {          {
2479          for (fi = min;; fi++)          CHECK_PARTIAL();
2480            {          MRRETURN(MATCH_NOMATCH);
           RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);  
           if (rrc != MATCH_NOMATCH) RRETURN(rrc);  
           if (fi >= max || !match_ref(offset, eptr, length, md, ims))  
             RRETURN(MATCH_NOMATCH);  
           eptr += length;  
           }  
         /* Control never gets here */  
2481          }          }
2482          eptr += slength;
2483          }
2484    
2485        /* If maximizing, find the longest string and work backwards */      /* If min = max, continue at the same level without recursion.
2486        They are not both allowed to be zero. */
2487    
2488        else      if (min == max) continue;
2489    
2490        /* If minimizing, keep trying and advancing the pointer */
2491    
2492        if (minimize)
2493          {
2494          for (fi = min;; fi++)
2495          {          {
2496          pp = eptr;          int slength;
2497          for (i = min; i < max; i++)          RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
2498            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2499            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2500            if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2501            {            {
2502            if (!match_ref(offset, eptr, length, md, ims)) break;            CHECK_PARTIAL();
2503            eptr += length;            MRRETURN(MATCH_NOMATCH);
2504            }            }
2505          while (eptr >= pp)          eptr += slength;
2506            }
2507          /* Control never gets here */
2508          }
2509    
2510        /* If maximizing, find the longest string and work backwards */
2511    
2512        else
2513          {
2514          pp = eptr;
2515          for (i = min; i < max; i++)
2516            {
2517            int slength;
2518            if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2519            {            {
2520            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            CHECK_PARTIAL();
2521            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            break;
           eptr -= length;  
2522            }            }
2523          RRETURN(MATCH_NOMATCH);          eptr += slength;
2524          }          }
2525          while (eptr >= pp)
2526            {
2527            RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
2528            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2529            eptr -= length;
2530            }
2531          MRRETURN(MATCH_NOMATCH);
2532        }        }
2533      /* Control never gets here */      /* Control never gets here */
2534    
   
   
2535      /* Match a bit-mapped character class, possibly repeatedly. This op code is      /* Match a bit-mapped character class, possibly repeatedly. This op code is
2536      used when all the characters in the class have values in the range 0-255,      used when all the characters in the class have values in the range 0-255,
2537      and either the matching is caseful, or the characters are in the range      and either the matching is caseful, or the characters are in the range
# Line 1618  for (;;) Line 2586  for (;;)
2586          {          {
2587          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2588            {            {
2589            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2590                {
2591                SCHECK_PARTIAL();
2592                MRRETURN(MATCH_NOMATCH);
2593                }
2594            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
2595            if (c > 255)            if (c > 255)
2596              {              {
2597              if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);              if (op == OP_CLASS) MRRETURN(MATCH_NOMATCH);
2598              }              }
2599            else            else
2600              {              {
2601              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);              if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2602              }              }
2603            }            }
2604          }          }
# Line 1636  for (;;) Line 2608  for (;;)
2608          {          {
2609          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2610            {            {
2611            if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
2612                {
2613                SCHECK_PARTIAL();
2614                MRRETURN(MATCH_NOMATCH);
2615                }
2616            c = *eptr++;            c = *eptr++;
2617            if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);            if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2618            }            }
2619          }          }
2620    
# Line 1658  for (;;) Line 2634  for (;;)
2634            {            {
2635            for (fi = min;; fi++)            for (fi = min;; fi++)
2636              {              {
2637              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
2638              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2639              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2640                if (eptr >= md->end_subject)
2641                  {
2642                  SCHECK_PARTIAL();
2643                  MRRETURN(MATCH_NOMATCH);
2644                  }
2645              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
2646              if (c > 255)              if (c > 255)
2647                {                {
2648                if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);                if (op == OP_CLASS) MRRETURN(MATCH_NOMATCH);
2649                }                }
2650              else              else
2651                {                {
2652                if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);                if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2653                }                }
2654              }              }
2655            }            }
# Line 1678  for (;;) Line 2659  for (;;)
2659            {            {
2660            for (fi = min;; fi++)            for (fi = min;; fi++)
2661              {              {
2662              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
2663              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2664              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2665                if (eptr >= md->end_subject)
2666                  {
2667                  SCHECK_PARTIAL();
2668                  MRRETURN(MATCH_NOMATCH);
2669                  }
2670              c = *eptr++;              c = *eptr++;
2671              if ((data[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);              if ((data[c/8] & (1 << (c&7))) == 0) MRRETURN(MATCH_NOMATCH);
2672              }              }
2673            }            }
2674          /* Control never gets here */          /* Control never gets here */
# Line 1701  for (;;) Line 2687  for (;;)
2687            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2688              {              {
2689              int len = 1;              int len = 1;
2690              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
2691                  {
2692                  SCHECK_PARTIAL();
2693                  break;
2694                  }
2695              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
2696              if (c > 255)              if (c > 255)
2697                {                {
# Line 1715  for (;;) Line 2705  for (;;)
2705              }              }
2706            for (;;)            for (;;)
2707              {              {
2708              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
2709              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2710              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2711              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 1727  for (;;) Line 2717  for (;;)
2717            {            {
2718            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2719              {              {
2720              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
2721                  {
2722                  SCHECK_PARTIAL();
2723                  break;
2724                  }
2725              c = *eptr;              c = *eptr;
2726              if ((data[c/8] & (1 << (c&7))) == 0) break;              if ((data[c/8] & (1 << (c&7))) == 0) break;
2727              eptr++;              eptr++;
2728              }              }
2729            while (eptr >= pp)            while (eptr >= pp)
2730              {              {
2731              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM19);
2732              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2733              eptr--;              eptr--;
2734              }              }
2735            }            }
2736    
2737          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2738          }          }
2739        }        }
2740      /* Control never gets here */      /* Control never gets here */
2741    
2742    
2743      /* Match an extended character class. This opcode is encountered only      /* Match an extended character class. This opcode is encountered only
2744      in UTF-8 mode, because that's the only time it is compiled. */      when UTF-8 mode mode is supported. Nevertheless, we may not be in UTF-8
2745        mode, because Unicode properties are supported in non-UTF-8 mode. */
2746    
2747  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2748      case OP_XCLASS:      case OP_XCLASS:
# Line 1788  for (;;) Line 2783  for (;;)
2783    
2784        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
2785          {          {
2786          if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);          if (eptr >= md->end_subject)
2787          GETCHARINC(c, eptr);            {
2788          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);            SCHECK_PARTIAL();
2789              MRRETURN(MATCH_NOMATCH);
2790              }
2791            GETCHARINCTEST(c, eptr);
2792            if (!_pcre_xclass(c, data)) MRRETURN(MATCH_NOMATCH);
2793          }          }
2794    
2795        /* If max == min we can continue with the main loop without the        /* If max == min we can continue with the main loop without the
# Line 1805  for (;;) Line 2804  for (;;)
2804          {          {
2805          for (fi = min;; fi++)          for (fi = min;; fi++)
2806            {            {
2807            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM20);
2808            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2809            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2810            GETCHARINC(c, eptr);            if (eptr >= md->end_subject)
2811            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);              {
2812                SCHECK_PARTIAL();
2813                MRRETURN(MATCH_NOMATCH);
2814                }
2815              GETCHARINCTEST(c, eptr);
2816              if (!_pcre_xclass(c, data)) MRRETURN(MATCH_NOMATCH);
2817            }            }
2818          /* Control never gets here */          /* Control never gets here */
2819          }          }
# Line 1822  for (;;) Line 2826  for (;;)
2826          for (i = min; i < max; i++)          for (i = min; i < max; i++)
2827            {            {
2828            int len = 1;            int len = 1;
2829            if (eptr >= md->end_subject) break;            if (eptr >= md->end_subject)
2830            GETCHARLEN(c, eptr, len);              {
2831                SCHECK_PARTIAL();
2832                break;
2833                }
2834              GETCHARLENTEST(c, eptr, len);
2835            if (!_pcre_xclass(c, data)) break;            if (!_pcre_xclass(c, data)) break;
2836            eptr += len;            eptr += len;
2837            }            }
2838          for(;;)          for(;;)
2839            {            {
2840            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM21);
2841            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2842            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
2843            BACKCHAR(eptr)            if (utf8) BACKCHAR(eptr);
2844            }            }
2845          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
2846          }          }
2847    
2848        /* Control never gets here */        /* Control never gets here */
# Line 1850  for (;;) Line 2858  for (;;)
2858        length = 1;        length = 1;
2859        ecode++;        ecode++;
2860        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2861        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
2862        while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);          {
2863            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2864            MRRETURN(MATCH_NOMATCH);
2865            }
2866          while (length-- > 0) if (*ecode++ != *eptr++) MRRETURN(MATCH_NOMATCH);
2867        }        }
2868      else      else
2869  #endif  #endif
2870    
2871      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2872        {        {
2873        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
2874        if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);          {
2875            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2876            MRRETURN(MATCH_NOMATCH);
2877            }
2878          if (ecode[1] != *eptr++) MRRETURN(MATCH_NOMATCH);
2879        ecode += 2;        ecode += 2;
2880        }        }
2881      break;      break;
2882    
2883      /* Match a single character, caselessly */      /* Match a single character, caselessly */
2884    
2885      case OP_CHARNC:      case OP_CHARI:
2886  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2887      if (utf8)      if (utf8)
2888        {        {
# Line 1874  for (;;) Line 2890  for (;;)
2890        ecode++;        ecode++;
2891        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
2892    
2893        if (length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);        if (length > md->end_subject - eptr)
2894            {
2895            CHECK_PARTIAL();             /* Not SCHECK_PARTIAL() */
2896            MRRETURN(MATCH_NOMATCH);
2897            }
2898    
2899        /* If the pattern character's value is < 128, we have only one byte, and        /* If the pattern character's value is < 128, we have only one byte, and
2900        can use the fast lookup table. */        can use the fast lookup table. */
2901    
2902        if (fc < 128)        if (fc < 128)
2903          {          {
2904          if (md->lcc[*ecode++] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          if (md->lcc[*ecode++] != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
2905          }          }
2906    
2907        /* Otherwise we must pick up the subject character */        /* Otherwise we must pick up the subject character */
2908    
2909        else        else
2910          {          {
2911          int dc;          unsigned int dc;
2912          GETCHARINC(dc, eptr);          GETCHARINC(dc, eptr);
2913          ecode += length;          ecode += length;
2914    
# Line 1898  for (;;) Line 2918  for (;;)
2918          if (fc != dc)          if (fc != dc)
2919            {            {
2920  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2921            if (dc != _pcre_ucp_othercase(fc))            if (dc != UCD_OTHERCASE(fc))
2922  #endif  #endif
2923              RRETURN(MATCH_NOMATCH);              MRRETURN(MATCH_NOMATCH);
2924            }            }
2925          }          }
2926        }        }
# Line 1909  for (;;) Line 2929  for (;;)
2929    
2930      /* Non-UTF-8 mode */      /* Non-UTF-8 mode */
2931        {        {
2932        if (md->end_subject - eptr < 1) RRETURN(MATCH_NOMATCH);        if (md->end_subject - eptr < 1)
2933        if (md->lcc[ecode[1]] != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          {
2934            SCHECK_PARTIAL();            /* This one can use SCHECK_PARTIAL() */
2935            MRRETURN(MATCH_NOMATCH);
2936            }
2937          if (md->lcc[ecode[1]] != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
2938        ecode += 2;        ecode += 2;
2939        }        }
2940      break;      break;
2941    
2942      /* Match a single character repeatedly; different opcodes share code. */      /* Match a single character repeatedly. */
2943    
2944      case OP_EXACT:      case OP_EXACT:
2945        case OP_EXACTI:
2946      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
2947      ecode += 3;      ecode += 3;
2948      goto REPEATCHAR;      goto REPEATCHAR;
2949    
2950        case OP_POSUPTO:
2951        case OP_POSUPTOI:
2952        possessive = TRUE;
2953        /* Fall through */
2954    
2955      case OP_UPTO:      case OP_UPTO:
2956        case OP_UPTOI:
2957      case OP_MINUPTO:      case OP_MINUPTO:
2958        case OP_MINUPTOI:
2959      min = 0;      min = 0;
2960      max = GET2(ecode, 1);      max = GET2(ecode, 1);
2961      minimize = *ecode == OP_MINUPTO;      minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI;
2962      ecode += 3;      ecode += 3;
2963      goto REPEATCHAR;      goto REPEATCHAR;
2964    
2965        case OP_POSSTAR:
2966        case OP_POSSTARI:
2967        possessive = TRUE;
2968        min = 0;
2969        max = INT_MAX;
2970        ecode++;
2971        goto REPEATCHAR;
2972    
2973        case OP_POSPLUS:
2974        case OP_POSPLUSI:
2975        possessive = TRUE;
2976        min = 1;
2977        max = INT_MAX;
2978        ecode++;
2979        goto REPEATCHAR;
2980    
2981        case OP_POSQUERY:
2982        case OP_POSQUERYI:
2983        possessive = TRUE;
2984        min = 0;
2985        max = 1;
2986        ecode++;
2987        goto REPEATCHAR;
2988    
2989      case OP_STAR:      case OP_STAR:
2990        case OP_STARI:
2991      case OP_MINSTAR:      case OP_MINSTAR:
2992        case OP_MINSTARI:
2993      case OP_PLUS:      case OP_PLUS:
2994        case OP_PLUSI:
2995      case OP_MINPLUS:      case OP_MINPLUS:
2996        case OP_MINPLUSI:
2997      case OP_QUERY:      case OP_QUERY:
2998        case OP_QUERYI:
2999      case OP_MINQUERY:      case OP_MINQUERY:
3000      c = *ecode++ - OP_STAR;      case OP_MINQUERYI:
3001        c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI);
3002      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3003      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3004      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3005      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
3006    
3007      /* Common code for all repeated single-character matches. We can give      /* Common code for all repeated single-character matches. */
     up quickly if there are fewer than the minimum number of characters left in  
     the subject. */  
3008    
3009      REPEATCHAR:      REPEATCHAR:
3010  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 1953  for (;;) Line 3013  for (;;)
3013        length = 1;        length = 1;
3014        charptr = ecode;        charptr = ecode;
3015        GETCHARLEN(fc, ecode, length);        GETCHARLEN(fc, ecode, length);
       if (min * length > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3016        ecode += length;        ecode += length;
3017    
3018        /* Handle multibyte character matching specially here. There is        /* Handle multibyte character matching specially here. There is
# Line 1961  for (;;) Line 3020  for (;;)
3020    
3021        if (length > 1)        if (length > 1)
3022          {          {
         int oclength = 0;  
         uschar occhars[8];  
   
3023  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3024          int othercase;          unsigned int othercase;
3025          if ((ims & PCRE_CASELESS) != 0 &&          if (op >= OP_STARI &&     /* Caseless */
3026              (othercase = _pcre_ucp_othercase(fc)) >= 0 &&              (othercase = UCD_OTHERCASE(fc)) != fc)
              othercase >= 0)  
3027            oclength = _pcre_ord2utf8(othercase, occhars);            oclength = _pcre_ord2utf8(othercase, occhars);
3028            else oclength = 0;
3029  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3030    
3031          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3032            {            {
3033            if (memcmp(eptr, charptr, length) == 0) eptr += length;            if (eptr <= md->end_subject - length &&
3034            /* Need braces because of following else */              memcmp(eptr, charptr, length) == 0) eptr += length;
3035            else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }  #ifdef SUPPORT_UCP
3036              else if (oclength > 0 &&
3037                       eptr <= md->end_subject - oclength &&
3038                       memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3039    #endif  /* SUPPORT_UCP */
3040            else            else
3041              {              {
3042              if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);              CHECK_PARTIAL();
3043              eptr += oclength;              MRRETURN(MATCH_NOMATCH);
3044              }              }
3045            }            }
3046    
# Line 1990  for (;;) Line 3050  for (;;)
3050            {            {
3051            for (fi = min;; fi++)            for (fi = min;; fi++)
3052              {              {
3053              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM22);
3054              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3055              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3056              if (memcmp(eptr, charptr, length) == 0) eptr += length;              if (eptr <= md->end_subject - length &&
3057              /* Need braces because of following else */                memcmp(eptr, charptr, length) == 0) eptr += length;
3058              else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }  #ifdef SUPPORT_UCP
3059                else if (oclength > 0 &&
3060                         eptr <= md->end_subject - oclength &&
3061                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3062    #endif  /* SUPPORT_UCP */
3063              else              else
3064                {                {
3065                if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);                CHECK_PARTIAL();
3066                eptr += oclength;                MRRETURN(MATCH_NOMATCH);
3067                }                }
3068              }              }
3069            /* Control never gets here */            /* Control never gets here */
3070            }            }
3071          else  
3072            else  /* Maximize */
3073            {            {
3074            pp = eptr;            pp = eptr;
3075            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3076              {              {
3077              if (eptr > md->end_subject - length) break;              if (eptr <= md->end_subject - length &&
3078              if (memcmp(eptr, charptr, length) == 0) eptr += length;                  memcmp(eptr, charptr, length) == 0) eptr += length;
3079              else if (oclength == 0) break;  #ifdef SUPPORT_UCP
3080                else if (oclength > 0 &&
3081                         eptr <= md->end_subject - oclength &&
3082                         memcmp(eptr, occhars, oclength) == 0) eptr += oclength;
3083    #endif  /* SUPPORT_UCP */
3084              else              else
3085                {                {
3086                if (memcmp(eptr, occhars, oclength) != 0) break;                CHECK_PARTIAL();
3087                eptr += oclength;                break;
3088                }                }
3089              }              }
3090            while (eptr >= pp)  
3091             {            if (possessive) continue;
3092             RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);  
3093             if (rrc != MATCH_NOMATCH) RRETURN(rrc);            for(;;)
3094             eptr -= length;              {
3095             }              RMATCH(eptr, ecode, offset_top, md, eptrb, RM23);
3096            RRETURN(MATCH_NOMATCH);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3097                if (eptr == pp) { MRRETURN(MATCH_NOMATCH); }
3098    #ifdef SUPPORT_UCP
3099                eptr--;
3100                BACKCHAR(eptr);
3101    #else   /* without SUPPORT_UCP */
3102                eptr -= length;
3103    #endif  /* SUPPORT_UCP */
3104                }
3105            }            }
3106          /* Control never gets here */          /* Control never gets here */
3107          }          }
# Line 2037  for (;;) Line 3114  for (;;)
3114  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
3115    
3116      /* When not in UTF-8 mode, load a single-byte character. */      /* When not in UTF-8 mode, load a single-byte character. */
3117        {  
3118        if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);      fc = *ecode++;
       fc = *ecode++;  
       }  
3119    
3120      /* The value of fc at this point is always less than 256, though we may or      /* The value of fc at this point is always less than 256, though we may or
3121      may not be in UTF-8 mode. The code is duplicated for the caseless and      may not be in UTF-8 mode. The code is duplicated for the caseless and
# Line 2054  for (;;) Line 3129  for (;;)
3129      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3130        max, eptr));        max, eptr));
3131    
3132      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_STARI)  /* Caseless */
3133        {        {
3134        fc = md->lcc[fc];        fc = md->lcc[fc];
3135        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
3136          if (fc != md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);          {
3137            if (eptr >= md->end_subject)
3138              {
3139              SCHECK_PARTIAL();
3140              MRRETURN(MATCH_NOMATCH);
3141              }
3142            if (fc != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3143            }
3144        if (min == max) continue;        if (min == max) continue;
3145        if (minimize)        if (minimize)
3146          {          {
3147          for (fi = min;; fi++)          for (fi = min;; fi++)
3148            {            {
3149            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM24);
3150            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3151            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3152                fc != md->lcc[*eptr++])            if (eptr >= md->end_subject)
3153              RRETURN(MATCH_NOMATCH);              {
3154                SCHECK_PARTIAL();
3155                MRRETURN(MATCH_NOMATCH);
3156                }
3157              if (fc != md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3158            }            }
3159          /* Control never gets here */          /* Control never gets here */
3160          }          }
3161        else        else  /* Maximize */
3162          {          {
3163          pp = eptr;          pp = eptr;
3164          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3165            {            {
3166            if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break;            if (eptr >= md->end_subject)
3167                {
3168                SCHECK_PARTIAL();
3169                break;
3170                }
3171              if (fc != md->lcc[*eptr]) break;
3172            eptr++;            eptr++;
3173            }            }
3174    
3175            if (possessive) continue;
3176    
3177          while (eptr >= pp)          while (eptr >= pp)
3178            {            {
3179            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM25);
3180            eptr--;            eptr--;
3181            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3182            }            }
3183          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3184          }          }
3185        /* Control never gets here */        /* Control never gets here */
3186        }        }
# Line 2095  for (;;) Line 3189  for (;;)
3189    
3190      else      else
3191        {        {
3192        for (i = 1; i <= min; i++) if (fc != *eptr++) RRETURN(MATCH_NOMATCH);        for (i = 1; i <= min; i++)
3193            {
3194            if (eptr >= md->end_subject)
3195              {
3196              SCHECK_PARTIAL();
3197              MRRETURN(MATCH_NOMATCH);
3198              }
3199            if (fc != *eptr++) MRRETURN(MATCH_NOMATCH);
3200            }
3201    
3202        if (min == max) continue;        if (min == max) continue;
3203    
3204        if (minimize)        if (minimize)
3205          {          {
3206          for (fi = min;; fi++)          for (fi = min;; fi++)
3207            {            {
3208            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM26);
3209            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3210            if (fi >= max || eptr >= md->end_subject || fc != *eptr++)            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3211              RRETURN(MATCH_NOMATCH);            if (eptr >= md->end_subject)
3212                {
3213                SCHECK_PARTIAL();
3214                MRRETURN(MATCH_NOMATCH);
3215                }
3216              if (fc != *eptr++) MRRETURN(MATCH_NOMATCH);
3217            }            }
3218          /* Control never gets here */          /* Control never gets here */
3219          }          }
3220        else        else  /* Maximize */
3221          {          {
3222          pp = eptr;          pp = eptr;
3223          for (i = min; i < max; i++)          for (i = min; i < max; i++)
3224            {            {
3225            if (eptr >= md->end_subject || fc != *eptr) break;            if (eptr >= md->end_subject)
3226                {
3227                SCHECK_PARTIAL();
3228                break;
3229                }
3230              if (fc != *eptr) break;
3231            eptr++;            eptr++;
3232            }            }
3233            if (possessive) continue;
3234    
3235          while (eptr >= pp)          while (eptr >= pp)
3236            {            {
3237            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM27);
3238            eptr--;            eptr--;
3239            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3240            }            }
3241          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3242          }          }
3243        }        }
3244      /* Control never gets here */      /* Control never gets here */
# Line 2131  for (;;) Line 3247  for (;;)
3247      checking can be multibyte. */      checking can be multibyte. */
3248    
3249      case OP_NOT:      case OP_NOT:
3250      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      case OP_NOTI:
3251        if (eptr >= md->end_subject)
3252          {
3253          SCHECK_PARTIAL();
3254          MRRETURN(MATCH_NOMATCH);
3255          }
3256      ecode++;      ecode++;
3257      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
3258      if ((ims & PCRE_CASELESS) != 0)      if (op == OP_NOTI)         /* The caseless case */
3259        {        {
3260  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3261        if (c < 256)        if (c < 256)
3262  #endif  #endif
3263        c = md->lcc[c];        c = md->lcc[c];
3264        if (md->lcc[*ecode++] == c) RRETURN(MATCH_NOMATCH);        if (md->lcc[*ecode++] == c) MRRETURN(MATCH_NOMATCH);
3265        }        }
3266      else      else    /* Caseful */
3267        {        {
3268        if (*ecode++ == c) RRETURN(MATCH_NOMATCH);        if (*ecode++ == c) MRRETURN(MATCH_NOMATCH);
3269        }        }
3270      break;      break;
3271    
# Line 2156  for (;;) Line 3277  for (;;)
3277      about... */      about... */
3278    
3279      case OP_NOTEXACT:      case OP_NOTEXACT:
3280        case OP_NOTEXACTI:
3281      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
3282      ecode += 3;      ecode += 3;
3283      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3284    
3285      case OP_NOTUPTO:      case OP_NOTUPTO:
3286        case OP_NOTUPTOI:
3287      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
3288        case OP_NOTMINUPTOI:
3289        min = 0;
3290        max = GET2(ecode, 1);
3291        minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI;
3292        ecode += 3;
3293        goto REPEATNOTCHAR;
3294    
3295        case OP_NOTPOSSTAR:
3296        case OP_NOTPOSSTARI:
3297        possessive = TRUE;
3298        min = 0;
3299        max = INT_MAX;
3300        ecode++;
3301        goto REPEATNOTCHAR;
3302    
3303        case OP_NOTPOSPLUS:
3304        case OP_NOTPOSPLUSI:
3305        possessive = TRUE;
3306        min = 1;
3307        max = INT_MAX;
3308        ecode++;
3309        goto REPEATNOTCHAR;
3310    
3311        case OP_NOTPOSQUERY:
3312        case OP_NOTPOSQUERYI:
3313        possessive = TRUE;
3314        min = 0;
3315        max = 1;
3316        ecode++;
3317        goto REPEATNOTCHAR;
3318    
3319        case OP_NOTPOSUPTO:
3320        case OP_NOTPOSUPTOI:
3321        possessive = TRUE;
3322      min = 0;      min = 0;
3323      max = GET2(ecode, 1);      max = GET2(ecode, 1);
     minimize = *ecode == OP_NOTMINUPTO;  
3324      ecode += 3;      ecode += 3;
3325      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3326    
3327      case OP_NOTSTAR:      case OP_NOTSTAR:
3328        case OP_NOTSTARI:
3329      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
3330        case OP_NOTMINSTARI:
3331      case OP_NOTPLUS:      case OP_NOTPLUS:
3332        case OP_NOTPLUSI:
3333      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
3334        case OP_NOTMINPLUSI:
3335      case OP_NOTQUERY:      case OP_NOTQUERY:
3336        case OP_NOTQUERYI:
3337      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
3338      c = *ecode++ - OP_NOTSTAR;      case OP_NOTMINQUERYI:
3339        c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
3340      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3341      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3342      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3343      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
3344    
3345      /* Common code for all repeated single-byte matches. We can give up quickly      /* Common code for all repeated single-byte matches. */
     if there are fewer than the minimum number of bytes left in the  
     subject. */  
3346    
3347      REPEATNOTCHAR:      REPEATNOTCHAR:
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3348      fc = *ecode++;      fc = *ecode++;
3349    
3350      /* The code is duplicated for the caseless and caseful cases, for speed,      /* The code is duplicated for the caseless and caseful cases, for speed,
# Line 2199  for (;;) Line 3358  for (;;)
3358      DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max,      DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3359        max, eptr));        max, eptr));
3360    
3361      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_NOTSTARI)     /* Caseless */
3362        {        {
3363        fc = md->lcc[fc];        fc = md->lcc[fc];
3364    
# Line 2207  for (;;) Line 3366  for (;;)
3366        /* UTF-8 mode */        /* UTF-8 mode */
3367        if (utf8)        if (utf8)
3368          {          {
3369          register int d;          register unsigned int d;
3370          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3371            {            {
3372              if (eptr >= md->end_subject)
3373                {
3374                SCHECK_PARTIAL();
3375                MRRETURN(MATCH_NOMATCH);
3376                }
3377            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3378            if (d < 256) d = md->lcc[d];            if (d < 256) d = md->lcc[d];
3379            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) MRRETURN(MATCH_NOMATCH);
3380            }            }
3381          }          }
3382        else        else
# Line 2221  for (;;) Line 3385  for (;;)
3385        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3386          {          {
3387          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3388            if (fc == md->lcc[*eptr++]) RRETURN(MATCH_NOMATCH);            {
3389              if (eptr >= md->end_subject)
3390                {
3391                SCHECK_PARTIAL();
3392                MRRETURN(MATCH_NOMATCH);
3393                }
3394              if (fc == md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3395              }
3396          }          }
3397    
3398        if (min == max) continue;        if (min == max) continue;
# Line 2232  for (;;) Line 3403  for (;;)
3403          /* UTF-8 mode */          /* UTF-8 mode */
3404          if (utf8)          if (utf8)
3405            {            {
3406            register int d;            register unsigned int d;
3407            for (fi = min;; fi++)            for (fi = min;; fi++)
3408              {              {
3409              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM28);
3410              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3411                if (fi >= max) MRRETURN(MATCH_NOMATCH);
3412                if (eptr >= md->end_subject)
3413                  {
3414                  SCHECK_PARTIAL();
3415                  MRRETURN(MATCH_NOMATCH);
3416                  }
3417              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3418              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3419              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) MRRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
3420              }              }
3421            }            }
3422          else          else
# Line 2249  for (;;) Line 3425  for (;;)
3425            {            {
3426            for (fi = min;; fi++)            for (fi = min;; fi++)
3427              {              {
3428              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM29);
3429              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3430              if (fi >= max || eptr >= md->end_subject || fc == md->lcc[*eptr++])              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3431                RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3432                  {
3433                  SCHECK_PARTIAL();
3434                  MRRETURN(MATCH_NOMATCH);
3435                  }
3436                if (fc == md->lcc[*eptr++]) MRRETURN(MATCH_NOMATCH);
3437              }              }
3438            }            }
3439          /* Control never gets here */          /* Control never gets here */
# Line 2268  for (;;) Line 3449  for (;;)
3449          /* UTF-8 mode */          /* UTF-8 mode */
3450          if (utf8)          if (utf8)
3451            {            {
3452            register int d;            register unsigned int d;
3453            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3454              {              {
3455              int len = 1;              int len = 1;
3456              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3457                  {
3458                  SCHECK_PARTIAL();
3459                  break;
3460                  }
3461              GETCHARLEN(d, eptr, len);              GETCHARLEN(d, eptr, len);
3462              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
3463              if (fc == d) break;              if (fc == d) break;
3464              eptr += len;              eptr += len;
3465              }              }
3466            for(;;)          if (possessive) continue;
3467            for(;;)
3468              {              {
3469              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM30);
3470              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3471              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3472              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2292  for (;;) Line 3478  for (;;)
3478            {            {
3479            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3480              {              {
3481              if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break;              if (eptr >= md->end_subject)
3482                  {
3483                  SCHECK_PARTIAL();
3484                  break;
3485                  }
3486                if (fc == md->lcc[*eptr]) break;
3487              eptr++;              eptr++;
3488              }              }
3489              if (possessive) continue;
3490            while (eptr >= pp)            while (eptr >= pp)
3491              {              {
3492              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM31);
3493              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3494              eptr--;              eptr--;
3495              }              }
3496            }            }
3497    
3498          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3499          }          }
3500        /* Control never gets here */        /* Control never gets here */
3501        }        }
# Line 2316  for (;;) Line 3508  for (;;)
3508        /* UTF-8 mode */        /* UTF-8 mode */
3509        if (utf8)        if (utf8)
3510          {          {
3511          register int d;          register unsigned int d;
3512          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3513            {            {
3514              if (eptr >= md->end_subject)
3515                {
3516                SCHECK_PARTIAL();
3517                MRRETURN(MATCH_NOMATCH);
3518                }
3519            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
3520            if (fc == d) RRETURN(MATCH_NOMATCH);            if (fc == d) MRRETURN(MATCH_NOMATCH);
3521            }            }
3522          }          }
3523        else        else
# Line 2328  for (;;) Line 3525  for (;;)
3525        /* Not UTF-8 mode */        /* Not UTF-8 mode */
3526          {          {
3527          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3528            if (fc == *eptr++) RRETURN(MATCH_NOMATCH);            {
3529              if (eptr >= md->end_subject)
3530                {
3531                SCHECK_PARTIAL();
3532                MRRETURN(MATCH_NOMATCH);
3533                }
3534              if (fc == *eptr++) MRRETURN(MATCH_NOMATCH);
3535              }
3536          }          }
3537    
3538        if (min == max) continue;        if (min == max) continue;
# Line 2339  for (;;) Line 3543  for (;;)
3543          /* UTF-8 mode */          /* UTF-8 mode */
3544          if (utf8)          if (utf8)
3545            {            {
3546            register int d;            register unsigned int d;
3547            for (fi = min;; fi++)            for (fi = min;; fi++)
3548              {              {
3549              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM32);
3550              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3551                if (fi >= max) MRRETURN(MATCH_NOMATCH);
3552                if (eptr >= md->end_subject)
3553                  {
3554                  SCHECK_PARTIAL();
3555                  MRRETURN(MATCH_NOMATCH);
3556                  }
3557              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
3558              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) MRRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
3559              }              }
3560            }            }
3561          else          else
# Line 2355  for (;;) Line 3564  for (;;)
3564            {            {
3565            for (fi = min;; fi++)            for (fi = min;; fi++)
3566              {              {
3567              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM33);
3568              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3569              if (fi >= max || eptr >= md->end_subject || fc == *eptr++)              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3570                RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3571                  {
3572                  SCHECK_PARTIAL();
3573                  MRRETURN(MATCH_NOMATCH);
3574                  }
3575                if (fc == *eptr++) MRRETURN(MATCH_NOMATCH);
3576              }              }
3577            }            }
3578          /* Control never gets here */          /* Control never gets here */
# Line 2374  for (;;) Line 3588  for (;;)
3588          /* UTF-8 mode */          /* UTF-8 mode */
3589          if (utf8)          if (utf8)
3590            {            {
3591            register int d;            register unsigned int d;
3592            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3593              {              {
3594              int len = 1;              int len = 1;
3595              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject)
3596                  {
3597                  SCHECK_PARTIAL();
3598                  break;
3599                  }
3600              GETCHARLEN(d, eptr, len);              GETCHARLEN(d, eptr, len);
3601              if (fc == d) break;              if (fc == d) break;
3602              eptr += len;              eptr += len;
3603              }              }
3604              if (possessive) continue;
3605            for(;;)            for(;;)
3606              {              {
3607              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM34);
3608              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3609              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3610              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2397  for (;;) Line 3616  for (;;)
3616            {            {
3617            for (i = min; i < max; i++)            for (i = min; i < max; i++)
3618              {              {
3619              if (eptr >= md->end_subject || fc == *eptr) break;              if (eptr >= md->end_subject)
3620                  {
3621                  SCHECK_PARTIAL();
3622                  break;
3623                  }
3624                if (fc == *eptr) break;
3625              eptr++;              eptr++;
3626              }              }
3627              if (possessive) continue;
3628            while (eptr >= pp)            while (eptr >= pp)
3629              {              {
3630              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM35);
3631              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3632              eptr--;              eptr--;
3633              }              }
3634            }            }
3635    
3636          RRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
3637          }          }
3638        }        }
3639      /* Control never gets here */      /* Control never gets here */
# Line 2431  for (;;) Line 3656  for (;;)
3656      ecode += 3;      ecode += 3;
3657      goto REPEATTYPE;      goto REPEATTYPE;
3658    
3659        case OP_TYPEPOSSTAR:
3660        possessive = TRUE;
3661        min = 0;
3662        max = INT_MAX;
3663        ecode++;
3664        goto REPEATTYPE;
3665    
3666        case OP_TYPEPOSPLUS:
3667        possessive = TRUE;
3668        min = 1;
3669        max = INT_MAX;
3670        ecode++;
3671        goto REPEATTYPE;
3672    
3673        case OP_TYPEPOSQUERY:
3674        possessive = TRUE;
3675        min = 0;
3676        max = 1;
3677        ecode++;
3678        goto REPEATTYPE;
3679    
3680        case OP_TYPEPOSUPTO:
3681        possessive = TRUE;
3682        min = 0;
3683        max = GET2(ecode, 1);
3684        ecode += 3;
3685        goto REPEATTYPE;
3686    
3687      case OP_TYPESTAR:      case OP_TYPESTAR:
3688      case OP_TYPEMINSTAR:      case OP_TYPEMINSTAR:
3689      case OP_TYPEPLUS:      case OP_TYPEPLUS:
# Line 2462  for (;;) Line 3715  for (;;)
3715    
3716      /* First, ensure the minimum number of matches are present. Use inline      /* First, ensure the minimum number of matches are present. Use inline
3717      code for maximizing the speed, and do the type test once at the start      code for maximizing the speed, and do the type test once at the start
3718      (i.e. keep it out of the loop). Also we can test that there are at least      (i.e. keep it out of the loop). Separate the UTF-8 code completely as that
     the minimum number of bytes before we start. This isn't as effective in  
     UTF-8 mode, but it does no harm. Separate the UTF-8 code completely as that  
3719      is tidier. Also separate the UCP code, which can be the same for both UTF-8      is tidier. Also separate the UCP code, which can be the same for both UTF-8
3720      and single-bytes. */      and single-bytes. */
3721    
     if (min > md->end_subject - eptr) RRETURN(MATCH_NOMATCH);  
3722      if (min > 0)      if (min > 0)
3723        {        {
3724  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 2477  for (;;) Line 3727  for (;;)
3727          switch(prop_type)          switch(prop_type)
3728            {            {
3729            case PT_ANY:            case PT_ANY:
3730            if (prop_fail_result) RRETURN(MATCH_NOMATCH);            if (prop_fail_result) MRRETURN(MATCH_NOMATCH);
3731            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3732              {              {
3733              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3734              GETCHARINC(c, eptr);                {
3735                  SCHECK_PARTIAL();
3736                  MRRETURN(MATCH_NOMATCH);
3737                  }
3738                GETCHARINCTEST(c, eptr);
3739              }              }
3740            break;            break;
3741    
3742            case PT_LAMP:            case PT_LAMP:
3743            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
3744              {              {
3745              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject)
3746              GETCHARINC(c, eptr);                {
3747              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);                SCHECK_PARTIAL();
3748                  MRRETURN(MATCH_NOMATCH);
3749                  }
3750                GETCHARINCTEST(c, eptr);
3751                prop_chartype = UCD_CHARTYPE(c);
3752              if ((prop_chartype == ucp_Lu ||              if ((prop_chartype == ucp_Lu ||
3753                   prop_chartype == ucp_Ll ||                   prop_chartype == ucp_Ll ||
3754                   prop_chartype == ucp_Lt) == prop_fail_result)                   prop_chartype == ucp_Lt) == prop_fail_result)
3755                RRETURN(MATCH_NOMATCH);                MRRETURN(MATCH_NOMATCH);
3756              }              }
3757            break;            break;
3758    
3759            case PT_GC: