/[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 91 by nigel, Sat Feb 24 21:41:34 2007 UTC revision 409 by ph10, Sat Mar 28 17:10:56 2009 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-2009 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  #define NLBLOCK md           /* The block containing newline information */  #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 */  /* Flag bits for the match() function */
61    
62  #define match_condassert   0x01    /* Called to check a condition assertion */  #define match_condassert     0x01  /* Called to check a condition assertion */
63  #define match_isgroup      0x02    /* Set if start of bracketed group */  #define match_cbegroup       0x02  /* Could-be-empty unlimited repeat group */
64    
65  /* Non-error returns from the match() function. Error returns are externally  /* Non-error returns from the match() function. Error returns are externally
66  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 68  defined PCRE_ERROR_xxx codes, which are
68  #define MATCH_MATCH        1  #define MATCH_MATCH        1
69  #define MATCH_NOMATCH      0  #define MATCH_NOMATCH      0
70    
71    /* Special internal returns from the match() function. Make them sufficiently
72    negative to avoid the external error codes. */
73    
74    #define MATCH_COMMIT       (-999)
75    #define MATCH_PRUNE        (-998)
76    #define MATCH_SKIP         (-997)
77    #define MATCH_THEN         (-996)
78    
79  /* 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.
80  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,
81  because the offset vector is always a multiple of 3 long. */  because the offset vector is always a multiple of 3 long. */
# Line 101  Returns:     nothing Line 109  Returns:     nothing
109  static void  static void
110  pchars(const uschar *p, int length, BOOL is_subject, match_data *md)  pchars(const uschar *p, int length, BOOL is_subject, match_data *md)
111  {  {
112  int c;  unsigned int c;
113  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;
114  while (length-- > 0)  while (length-- > 0)
115    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 150  printf("\n"); Line 158  printf("\n");
158    
159  if (length > md->end_subject - eptr) return FALSE;  if (length > md->end_subject - eptr) return FALSE;
160    
161  /* Separate the caselesss case for speed */  /* Separate the caseless case for speed. In UTF-8 mode we can only do this
162    properly if Unicode properties are supported. Otherwise, we can check only
163    ASCII characters. */
164    
165  if ((ims & PCRE_CASELESS) != 0)  if ((ims & PCRE_CASELESS) != 0)
166    {    {
167    #ifdef SUPPORT_UTF8
168    #ifdef SUPPORT_UCP
169      if (md->utf8)
170        {
171        USPTR endptr = eptr + length;
172        while (eptr < endptr)
173          {
174          int c, d;
175          GETCHARINC(c, eptr);
176          GETCHARINC(d, p);
177          if (c != d && c != UCD_OTHERCASE(d)) return FALSE;
178          }
179        }
180      else
181    #endif
182    #endif
183    
184      /* The same code works when not in UTF-8 mode and in UTF-8 mode when there
185      is no UCP support. */
186    
187    while (length-- > 0)    while (length-- > 0)
188      if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE;      { if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE; }
189    }    }
190    
191    /* In the caseful case, we can just compare the bytes, whether or not we
192    are in UTF-8 mode. */
193    
194  else  else
195    { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }    { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }
196    
# Line 186  calls by keeping local variables that ne Line 220  calls by keeping local variables that ne
220  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
221  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
222  always used to.  always used to.
223    
224    The original heap-recursive code used longjmp(). However, it seems that this
225    can be very slow on some operating systems. Following a suggestion from Stan
226    Switzer, the use of longjmp() has been abolished, at the cost of having to
227    provide a unique number for each call to RMATCH. There is no way of generating
228    a sequence of numbers at compile time in C. I have given them names, to make
229    them stand out more clearly.
230    
231    Crude tests on x86 Linux show a small speedup of around 5-8%. However, on
232    FreeBSD, avoiding longjmp() more than halves the time taken to run the standard
233    tests. Furthermore, not using longjmp() means that local dynamic variables
234    don't have indeterminate values; this has meant that the frame size can be
235    reduced because the result can be "passed back" by straight setting of the
236    variable instead of being passed in the frame.
237  ****************************************************************************  ****************************************************************************
238  ***************************************************************************/  ***************************************************************************/
239    
240    /* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN
241    below must be updated in sync.  */
242    
243    enum { RM1=1, RM2,  RM3,  RM4,  RM5,  RM6,  RM7,  RM8,  RM9,  RM10,
244           RM11,  RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,
245           RM21,  RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
246           RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
247           RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
248           RM51,  RM52, RM53, RM54 };
249    
250  /* 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
251  versions and production versions. */  versions and production versions. Note that the "rw" argument of RMATCH isn't
252    actuall used in this definition. */
253    
254  #ifndef NO_RECURSE  #ifndef NO_RECURSE
255  #define REGISTER register  #define REGISTER register
256    
257  #ifdef DEBUG  #ifdef DEBUG
258  #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg) \  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \
259    { \    { \
260    printf("match() called in line %d\n", __LINE__); \    printf("match() called in line %d\n", __LINE__); \
261    rx = match(ra,rb,rc,rd,re,rf,rg,rdepth+1); \    rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1); \
262    printf("to line %d\n", __LINE__); \    printf("to line %d\n", __LINE__); \
263    }    }
264  #define RRETURN(ra) \  #define RRETURN(ra) \
# Line 208  versions and production versions. */ Line 267  versions and production versions. */
267    return ra; \    return ra; \
268    }    }
269  #else  #else
270  #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg) \  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \
271    rx = match(ra,rb,rc,rd,re,rf,rg,rdepth+1)    rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1)
272  #define RRETURN(ra) return ra  #define RRETURN(ra) return ra
273  #endif  #endif
274    
275  #else  #else
276    
277    
278  /* 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
279  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
280  match(), which never changes. */  argument of match(), which never changes. */
281    
282  #define REGISTER  #define REGISTER
283    
284  #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg)\  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw)\
285    {\    {\
286    heapframe *newframe = (pcre_stack_malloc)(sizeof(heapframe));\    heapframe *newframe = (pcre_stack_malloc)(sizeof(heapframe));\
287    if (setjmp(frame->Xwhere) == 0)\    frame->Xwhere = rw; \
288      {\    newframe->Xeptr = ra;\
289      newframe->Xeptr = ra;\    newframe->Xecode = rb;\
290      newframe->Xecode = rb;\    newframe->Xmstart = mstart;\
291      newframe->Xoffset_top = rc;\    newframe->Xoffset_top = rc;\
292      newframe->Xims = re;\    newframe->Xims = re;\
293      newframe->Xeptrb = rf;\    newframe->Xeptrb = rf;\
294      newframe->Xflags = rg;\    newframe->Xflags = rg;\
295      newframe->Xrdepth = frame->Xrdepth + 1;\    newframe->Xrdepth = frame->Xrdepth + 1;\
296      newframe->Xprevframe = frame;\    newframe->Xprevframe = frame;\
297      frame = newframe;\    frame = newframe;\
298      DPRINTF(("restarting from line %d\n", __LINE__));\    DPRINTF(("restarting from line %d\n", __LINE__));\
299      goto HEAP_RECURSE;\    goto HEAP_RECURSE;\
300      }\    L_##rw:\
301    else\    DPRINTF(("jumped back to line %d\n", __LINE__));\
     {\  
     DPRINTF(("longjumped back to line %d\n", __LINE__));\  
     frame = md->thisframe;\  
     rx = frame->Xresult;\  
     }\  
302    }    }
303    
304  #define RRETURN(ra)\  #define RRETURN(ra)\
# Line 254  match(), which never changes. */ Line 308  match(), which never changes. */
308    (pcre_stack_free)(newframe);\    (pcre_stack_free)(newframe);\
309    if (frame != NULL)\    if (frame != NULL)\
310      {\      {\
311      frame->Xresult = ra;\      rrc = ra;\
312      md->thisframe = frame;\      goto HEAP_RETURN;\
     longjmp(frame->Xwhere, 1);\  
313      }\      }\
314    return ra;\    return ra;\
315    }    }
# Line 269  typedef struct heapframe { Line 322  typedef struct heapframe {
322    
323    /* Function arguments that may change */    /* Function arguments that may change */
324    
325    const uschar *Xeptr;    USPTR Xeptr;
326    const uschar *Xecode;    const uschar *Xecode;
327      USPTR Xmstart;
328    int Xoffset_top;    int Xoffset_top;
329    long int Xims;    long int Xims;
330    eptrblock *Xeptrb;    eptrblock *Xeptrb;
# Line 279  typedef struct heapframe { Line 333  typedef struct heapframe {
333    
334    /* Function local variables */    /* Function local variables */
335    
336    const uschar *Xcallpat;    USPTR Xcallpat;
337    const uschar *Xcharptr;  #ifdef SUPPORT_UTF8
338    const uschar *Xdata;    USPTR Xcharptr;
339    const uschar *Xnext;  #endif
340    const uschar *Xpp;    USPTR Xdata;
341    const uschar *Xprev;    USPTR Xnext;
342    const uschar *Xsaved_eptr;    USPTR Xpp;
343      USPTR Xprev;
344      USPTR Xsaved_eptr;
345    
346    recursion_info Xnew_recursive;    recursion_info Xnew_recursive;
347    
348    BOOL Xcur_is_word;    BOOL Xcur_is_word;
349    BOOL Xcondition;    BOOL Xcondition;
   BOOL Xminimize;  
350    BOOL Xprev_is_word;    BOOL Xprev_is_word;
351    
352    unsigned long int Xoriginal_ims;    unsigned long int Xoriginal_ims;
# Line 303  typedef struct heapframe { Line 358  typedef struct heapframe {
358    int Xprop_category;    int Xprop_category;
359    int Xprop_chartype;    int Xprop_chartype;
360    int Xprop_script;    int Xprop_script;
361    int *Xprop_test_variable;    int Xoclength;
362      uschar Xocchars[8];
363  #endif  #endif
364    
365      int Xcodelink;
366    int Xctype;    int Xctype;
367    int Xfc;    unsigned int Xfc;
368    int Xfi;    int Xfi;
369    int Xlength;    int Xlength;
370    int Xmax;    int Xmax;
# Line 321  typedef struct heapframe { Line 378  typedef struct heapframe {
378    
379    eptrblock Xnewptrb;    eptrblock Xnewptrb;
380    
381    /* Place to pass back result, and where to jump back to */    /* Where to jump back to */
382    
383    int  Xresult;    int Xwhere;
   jmp_buf Xwhere;  
384    
385  } heapframe;  } heapframe;
386    
# Line 340  typedef struct heapframe { Line 396  typedef struct heapframe {
396  *         Match from current position            *  *         Match from current position            *
397  *************************************************/  *************************************************/
398    
399  /* 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  
400  returns a negative (error) response, the outer incarnation must also return the  returns a negative (error) response, the outer incarnation must also return the
401  same response.  same response.
402    
# Line 353  performance. Tests using gcc on a SPARC Line 406  performance. Tests using gcc on a SPARC
406  made performance worse.  made performance worse.
407    
408  Arguments:  Arguments:
409     eptr        pointer in subject     eptr        pointer to current character in subject
410     ecode       position in code     ecode       pointer to current position in compiled code
411       mstart      pointer to the current match start position (can be modified
412                     by encountering \K)
413     offset_top  current top pointer     offset_top  current top pointer
414     md          pointer to "static" info for the match     md          pointer to "static" info for the match
415     ims         current /i, /m, and /s options     ims         current /i, /m, and /s options
# Line 362  Arguments: Line 417  Arguments:
417                   brackets - for testing for empty matches                   brackets - for testing for empty matches
418     flags       can contain     flags       can contain
419                   match_condassert - this is an assertion condition                   match_condassert - this is an assertion condition
420                   match_isgroup - this is the start of a bracketed group                   match_cbegroup - this is the start of an unlimited repeat
421                       group that can match an empty string
422     rdepth      the recursion depth     rdepth      the recursion depth
423    
424  Returns:       MATCH_MATCH if matched            )  these values are >= 0  Returns:       MATCH_MATCH if matched            )  these values are >= 0
# Line 372  Returns:       MATCH_MATCH if matched Line 428  Returns:       MATCH_MATCH if matched
428  */  */
429    
430  static int  static int
431  match(REGISTER USPTR eptr, REGISTER const uschar *ecode,  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, USPTR mstart,
432    int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,    int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,
433    int flags, unsigned int rdepth)    int flags, unsigned int rdepth)
434  {  {
435  /* 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,
436  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
437  because they are used a lot in loops. */  "register" because they are used a lot in loops. */
438    
439  register int  rrc;         /* Returns from recursive calls */  register int  rrc;         /* Returns from recursive calls */
440  register int  i;           /* Used for loops not involving calls to RMATCH() */  register int  i;           /* Used for loops not involving calls to RMATCH() */
441  register unsigned int  c;  /* Character values not kept over RMATCH() calls */  register unsigned int c;   /* Character values not kept over RMATCH() calls */
442  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */
443    
444    BOOL minimize, possessive; /* Quantifier options */
445    int condcode;
446    
447  /* 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
448  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
449  heap storage. Set up the top-level frame here; others are obtained from the  heap storage. Set up the top-level frame here; others are obtained from the
# Line 398  frame->Xprevframe = NULL;            /* Line 457  frame->Xprevframe = NULL;            /*
457    
458  frame->Xeptr = eptr;  frame->Xeptr = eptr;
459  frame->Xecode = ecode;  frame->Xecode = ecode;
460    frame->Xmstart = mstart;
461  frame->Xoffset_top = offset_top;  frame->Xoffset_top = offset_top;
462  frame->Xims = ims;  frame->Xims = ims;
463  frame->Xeptrb = eptrb;  frame->Xeptrb = eptrb;
# Line 412  HEAP_RECURSE: Line 472  HEAP_RECURSE:
472    
473  #define eptr               frame->Xeptr  #define eptr               frame->Xeptr
474  #define ecode              frame->Xecode  #define ecode              frame->Xecode
475    #define mstart             frame->Xmstart
476  #define offset_top         frame->Xoffset_top  #define offset_top         frame->Xoffset_top
477  #define ims                frame->Xims  #define ims                frame->Xims
478  #define eptrb              frame->Xeptrb  #define eptrb              frame->Xeptrb
# Line 424  HEAP_RECURSE: Line 485  HEAP_RECURSE:
485  #define charptr            frame->Xcharptr  #define charptr            frame->Xcharptr
486  #endif  #endif
487  #define callpat            frame->Xcallpat  #define callpat            frame->Xcallpat
488    #define codelink           frame->Xcodelink
489  #define data               frame->Xdata  #define data               frame->Xdata
490  #define next               frame->Xnext  #define next               frame->Xnext
491  #define pp                 frame->Xpp  #define pp                 frame->Xpp
# Line 434  HEAP_RECURSE: Line 496  HEAP_RECURSE:
496    
497  #define cur_is_word        frame->Xcur_is_word  #define cur_is_word        frame->Xcur_is_word
498  #define condition          frame->Xcondition  #define condition          frame->Xcondition
 #define minimize           frame->Xminimize  
499  #define prev_is_word       frame->Xprev_is_word  #define prev_is_word       frame->Xprev_is_word
500    
501  #define original_ims       frame->Xoriginal_ims  #define original_ims       frame->Xoriginal_ims
# Line 446  HEAP_RECURSE: Line 507  HEAP_RECURSE:
507  #define prop_category      frame->Xprop_category  #define prop_category      frame->Xprop_category
508  #define prop_chartype      frame->Xprop_chartype  #define prop_chartype      frame->Xprop_chartype
509  #define prop_script        frame->Xprop_script  #define prop_script        frame->Xprop_script
510  #define prop_test_variable frame->Xprop_test_variable  #define oclength           frame->Xoclength
511    #define occhars            frame->Xocchars
512  #endif  #endif
513    
514  #define ctype              frame->Xctype  #define ctype              frame->Xctype
# Line 470  HEAP_RECURSE: Line 532  HEAP_RECURSE:
532  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
533  i, and fc and c, can be the same variables. */  i, and fc and c, can be the same variables. */
534    
535  #else  #else         /* NO_RECURSE not defined */
536  #define fi i  #define fi i
537  #define fc c  #define fc c
538    
# Line 489  recursion_info new_recursive;      /* wi Line 551  recursion_info new_recursive;      /* wi
551                                     /* that do not have to be preserved over  */                                     /* that do not have to be preserved over  */
552  BOOL cur_is_word;                  /* a recursive call to RMATCH().          */  BOOL cur_is_word;                  /* a recursive call to RMATCH().          */
553  BOOL condition;  BOOL condition;
 BOOL minimize;  
554  BOOL prev_is_word;  BOOL prev_is_word;
555    
556  unsigned long int original_ims;  unsigned long int original_ims;
# Line 501  int prop_fail_result; Line 562  int prop_fail_result;
562  int prop_category;  int prop_category;
563  int prop_chartype;  int prop_chartype;
564  int prop_script;  int prop_script;
565  int *prop_test_variable;  int oclength;
566    uschar occhars[8];
567  #endif  #endif
568    
569    int codelink;
570  int ctype;  int ctype;
571  int length;  int length;
572  int max;  int max;
# Line 516  int save_offset1, save_offset2, save_off Line 579  int save_offset1, save_offset2, save_off
579  int stacksave[REC_STACK_SAVE_MAX];  int stacksave[REC_STACK_SAVE_MAX];
580    
581  eptrblock newptrb;  eptrblock newptrb;
582  #endif  #endif     /* NO_RECURSE */
583    
584  /* These statements are here to stop the compiler complaining about unitialized  /* These statements are here to stop the compiler complaining about unitialized
585  variables. */  variables. */
# Line 524  variables. */ Line 587  variables. */
587  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
588  prop_value = 0;  prop_value = 0;
589  prop_fail_result = 0;  prop_fail_result = 0;
 prop_test_variable = NULL;  
590  #endif  #endif
591    
592    
593  /* This label is used for tail recursion, which is used in a few cases even  /* This label is used for tail recursion, which is used in a few cases even
594  when NO_RECURSE is not defined, in order to reduce the amount of stack that is  when NO_RECURSE is not defined, in order to reduce the amount of stack that is
595  used. Thanks to Ian Taylor for noticing this possibility and sending the  used. Thanks to Ian Taylor for noticing this possibility and sending the
# Line 542  defined). However, RMATCH isn't like a f Line 605  defined). However, RMATCH isn't like a f
605  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,
606  however, impact performance when true recursion is being used. */  however, impact performance when true recursion is being used. */
607    
608    #ifdef SUPPORT_UTF8
609    utf8 = md->utf8;       /* Local copy of the flag */
610    #else
611    utf8 = FALSE;
612    #endif
613    
614  /* 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
615  haven't exceeded the recursive call limit. */  haven't exceeded the recursive call limit. */
616    
# Line 550  if (rdepth >= md->match_limit_recursion) Line 619  if (rdepth >= md->match_limit_recursion)
619    
620  original_ims = ims;    /* Save for resetting on ')' */  original_ims = ims;    /* Save for resetting on ')' */
621    
622  #ifdef SUPPORT_UTF8  /* At the start of a group with an unlimited repeat that may match an empty
623  utf8 = md->utf8;       /* Local copy of the flag */  string, the match_cbegroup flag is set. When this is the case, add the current
624  #else  subject pointer to the chain of such remembered pointers, to be checked when we
625  utf8 = FALSE;  hit the closing ket, in order to break infinite loops that match no characters.
626  #endif  When match() is called in other circumstances, don't add to the chain. The
627    match_cbegroup flag must NOT be used with tail recursion, because the memory
628  /* At the start of a bracketed group, add the current subject pointer to the  block that is used is on the stack, so a new one may be required for each
629  stack of such pointers, to be re-instated at the end of the group when we hit  match(). */
 the closing ket. When match() is called in other circumstances, we don't add to  
 this stack. */  
630    
631  if ((flags & match_isgroup) != 0)  if ((flags & match_cbegroup) != 0)
632    {    {
   newptrb.epb_prev = eptrb;  
633    newptrb.epb_saved_eptr = eptr;    newptrb.epb_saved_eptr = eptr;
634      newptrb.epb_prev = eptrb;
635    eptrb = &newptrb;    eptrb = &newptrb;
636    }    }
637    
638  /* Now start processing the operations. */  /* Now start processing the opcodes. */
639    
640  for (;;)  for (;;)
641    {    {
642      minimize = possessive = FALSE;
643    op = *ecode;    op = *ecode;
   minimize = FALSE;  
644    
645    /* For partial matching, remember if we ever hit the end of the subject after    /* For partial matching, remember if we ever hit the end of the subject after
646    matching at least one subject character. */    matching at least one subject character. */
647    
648    if (md->partial &&    if (md->partial &&
649        eptr >= md->end_subject &&        eptr >= md->end_subject &&
650        eptr > md->start_match)        eptr > mstart)
651      md->hitend = TRUE;      md->hitend = TRUE;
652    
653    /* Opening capturing bracket. If there is space in the offset vector, save    switch(op)
   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. */  
   
   if (op > OP_BRA)  
654      {      {
655      number = op - OP_BRA;      case OP_FAIL:
656        RRETURN(MATCH_NOMATCH);
     /* For extended extraction brackets (large number), we have to fish out the  
     number from a dummy opcode at the start. */  
657    
658      if (number > EXTRACT_BASIC_MAX)      case OP_PRUNE:
659        number = GET2(ecode, 2+LINK_SIZE);      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
660          ims, eptrb, flags, RM51);
661        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
662        RRETURN(MATCH_PRUNE);
663    
664        case OP_COMMIT:
665        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
666          ims, eptrb, flags, RM52);
667        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
668        RRETURN(MATCH_COMMIT);
669    
670        case OP_SKIP:
671        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
672          ims, eptrb, flags, RM53);
673        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
674        md->start_match_ptr = eptr;   /* Pass back current position */
675        RRETURN(MATCH_SKIP);
676    
677        case OP_THEN:
678        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
679          ims, eptrb, flags, RM54);
680        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
681        RRETURN(MATCH_THEN);
682    
683        /* Handle a capturing bracket. If there is space in the offset vector, save
684        the current subject position in the working slot at the top of the vector.
685        We mustn't change the current values of the data slot, because they may be
686        set from a previous iteration of this group, and be referred to by a
687        reference inside the group.
688    
689        If the bracket fails to match, we need to restore this value and also the
690        values of the final offsets, in case they were set by a previous iteration
691        of the same bracket.
692    
693        If there isn't enough space in the offset vector, treat this as if it were
694        a non-capturing bracket. Don't worry about setting the flag for the error
695        case here; that is handled in the code for KET. */
696    
697        case OP_CBRA:
698        case OP_SCBRA:
699        number = GET2(ecode, 1+LINK_SIZE);
700      offset = number << 1;      offset = number << 1;
701    
702  #ifdef DEBUG  #ifdef DEBUG
703      printf("start bracket %d subject=", number);      printf("start bracket %d\n", number);
704        printf("subject=");
705      pchars(eptr, 16, TRUE, md);      pchars(eptr, 16, TRUE, md);
706      printf("\n");      printf("\n");
707  #endif  #endif
# Line 624  for (;;) Line 716  for (;;)
716        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
717        md->offset_vector[md->offset_end - number] = eptr - md->start_subject;        md->offset_vector[md->offset_end - number] = eptr - md->start_subject;
718    
719          flags = (op == OP_SCBRA)? match_cbegroup : 0;
720        do        do
721          {          {
722          RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
723            match_isgroup);            ims, eptrb, flags, RM1);
724          if (rrc != MATCH_NOMATCH) RRETURN(rrc);          if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
725          md->capture_last = save_capture_last;          md->capture_last = save_capture_last;
726          ecode += GET(ecode, 1);          ecode += GET(ecode, 1);
727          }          }
# Line 643  for (;;) Line 736  for (;;)
736        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
737        }        }
738    
739      /* Insufficient room for saving captured contents */      /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
740        as a non-capturing bracket. */
     else op = OP_BRA;  
     }  
   
   /* Other types of node can be handled by a switch */  
741    
742    switch(op)      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
743      {      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
     case OP_BRA:     /* Non-capturing bracket: optimized */  
     DPRINTF(("start bracket 0\n"));  
744    
745      /* Loop for all the alternatives */      DPRINTF(("insufficient capture room: treat as non-capturing\n"));
746    
747        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
748        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
749    
750        /* Non-capturing bracket. Loop for all the alternatives. When we get to the
751        final alternative within the brackets, we would return the result of a
752        recursive call to match() whatever happened. We can reduce stack usage by
753        turning this into a tail recursion, except in the case when match_cbegroup
754        is set.*/
755    
756        case OP_BRA:
757        case OP_SBRA:
758        DPRINTF(("start non-capturing bracket\n"));
759        flags = (op >= OP_SBRA)? match_cbegroup : 0;
760      for (;;)      for (;;)
761        {        {
762        /* When we get to the final alternative within the brackets, we would        if (ecode[GET(ecode, 1)] != OP_ALT)   /* Final alternative */
763        return the result of a recursive call to match() whatever happened. We          {
764        can reduce stack usage by turning this into a tail recursion. */          if (flags == 0)    /* Not a possibly empty group */
765              {
766        if (ecode[GET(ecode, 1)] != OP_ALT)            ecode += _pcre_OP_lengths[*ecode];
767         {            DPRINTF(("bracket 0 tail recursion\n"));
768         ecode += 1 + LINK_SIZE;            goto TAIL_RECURSE;
769         flags = match_isgroup;            }
770         DPRINTF(("bracket 0 tail recursion\n"));  
771         goto TAIL_RECURSE;          /* Possibly empty group; can't use tail recursion. */
772         }  
773            RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,
774              eptrb, flags, RM48);
775            RRETURN(rrc);
776            }
777    
778        /* For non-final alternatives, continue the loop for a NOMATCH result;        /* For non-final alternatives, continue the loop for a NOMATCH result;
779        otherwise return. */        otherwise return. */
780    
781        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,
782          match_isgroup);          eptrb, flags, RM2);
783        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
784        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
785        }        }
786      /* Control never reaches here. */      /* Control never reaches here. */
# Line 688  for (;;) Line 792  for (;;)
792      obeyed, we can use tail recursion to avoid using another stack frame. */      obeyed, we can use tail recursion to avoid using another stack frame. */
793    
794      case OP_COND:      case OP_COND:
795      if (ecode[LINK_SIZE+1] == OP_CREF) /* Condition extract or recurse test */      case OP_SCOND:
796        codelink= GET(ecode, 1);
797    
798        /* Because of the way auto-callout works during compile, a callout item is
799        inserted between OP_COND and an assertion condition. */
800    
801        if (ecode[LINK_SIZE+1] == OP_CALLOUT)
802          {
803          if (pcre_callout != NULL)
804            {
805            pcre_callout_block cb;
806            cb.version          = 1;   /* Version 1 of the callout block */
807            cb.callout_number   = ecode[LINK_SIZE+2];
808            cb.offset_vector    = md->offset_vector;
809            cb.subject          = (PCRE_SPTR)md->start_subject;
810            cb.subject_length   = md->end_subject - md->start_subject;
811            cb.start_match      = mstart - md->start_subject;
812            cb.current_position = eptr - md->start_subject;
813            cb.pattern_position = GET(ecode, LINK_SIZE + 3);
814            cb.next_item_length = GET(ecode, 3 + 2*LINK_SIZE);
815            cb.capture_top      = offset_top/2;
816            cb.capture_last     = md->capture_last;
817            cb.callout_data     = md->callout_data;
818            if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);
819            if (rrc < 0) RRETURN(rrc);
820            }
821          ecode += _pcre_OP_lengths[OP_CALLOUT];
822          }
823    
824        condcode = ecode[LINK_SIZE+1];
825    
826        /* Now see what the actual condition is */
827    
828        if (condcode == OP_RREF)         /* Recursion test */
829          {
830          offset = GET2(ecode, LINK_SIZE + 2);     /* Recursion group number*/
831          condition = md->recursive != NULL &&
832            (offset == RREF_ANY || offset == md->recursive->group_num);
833          ecode += condition? 3 : GET(ecode, 1);
834          }
835    
836        else if (condcode == OP_CREF)    /* Group used test */
837        {        {
838        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */
839        condition = (offset == CREF_RECURSE * 2)?        condition = offset < offset_top && md->offset_vector[offset] >= 0;
840          (md->recursive != NULL) :        ecode += condition? 3 : GET(ecode, 1);
841          (offset < offset_top && md->offset_vector[offset] >= 0);        }
842        ecode += condition? (LINK_SIZE + 4) : (LINK_SIZE + 1 + GET(ecode, 1));  
843        flags = match_isgroup;      else if (condcode == OP_DEF)     /* DEFINE - always false */
844        goto TAIL_RECURSE;        {
845          condition = FALSE;
846          ecode += GET(ecode, 1);
847        }        }
848    
849      /* The condition is an assertion. Call match() to evaluate it - setting      /* The condition is an assertion. Call match() to evaluate it - setting
850      the final argument TRUE causes it to stop at the end of an assertion. */      the final argument match_condassert causes it to stop at the end of an
851        assertion. */
852    
853      else      else
854        {        {
855        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,
856            match_condassert | match_isgroup);            match_condassert, RM3);
857        if (rrc == MATCH_MATCH)        if (rrc == MATCH_MATCH)
858          {          {
859          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE+2);          condition = TRUE;
860            ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
861          while (*ecode == OP_ALT) ecode += GET(ecode, 1);          while (*ecode == OP_ALT) ecode += GET(ecode, 1);
862          }          }
863        else if (rrc != MATCH_NOMATCH)        else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
864          {          {
865          RRETURN(rrc);         /* Need braces because of following else */          RRETURN(rrc);         /* Need braces because of following else */
866          }          }
867        else ecode += GET(ecode, 1);        else
868            {
869            condition = FALSE;
870            ecode += codelink;
871            }
872          }
873    
874        /* We are now at the branch that is to be obeyed. As there is only one,      /* We are now at the branch that is to be obeyed. As there is only one,
875        we can use tail recursion to avoid using another stack frame. */      we can use tail recursion to avoid using another stack frame, except when
876        match_cbegroup is required for an unlimited repeat of a possibly empty
877        group. If the second alternative doesn't exist, we can just plough on. */
878    
879        if (condition || *ecode == OP_ALT)
880          {
881          ecode += 1 + LINK_SIZE;
882          if (op == OP_SCOND)        /* Possibly empty group */
883            {
884            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, match_cbegroup, RM49);
885            RRETURN(rrc);
886            }
887          else                       /* Group must match something */
888            {
889            flags = 0;
890            goto TAIL_RECURSE;
891            }
892          }
893        else                         /* Condition false & no alternative */
894          {
895        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = match_isgroup;  
       goto TAIL_RECURSE;  
896        }        }
     /* Control never reaches here */  
   
     /* Skip over conditional reference or large extraction number data if  
     encountered. */  
   
     case OP_CREF:  
     case OP_BRANUMBER:  
     ecode += 3;  
897      break;      break;
898    
     /* End of the pattern. If we are in a recursion, we should restore the  
     offsets appropriately and continue from after the call. */  
899    
900        /* End of the pattern, either real or forced. If we are in a top-level
901        recursion, we should restore the offsets appropriately and continue from
902        after the call. */
903    
904        case OP_ACCEPT:
905      case OP_END:      case OP_END:
906      if (md->recursive != NULL && md->recursive->group_num == 0)      if (md->recursive != NULL && md->recursive->group_num == 0)
907        {        {
# Line 745  for (;;) Line 910  for (;;)
910        md->recursive = rec->prevrec;        md->recursive = rec->prevrec;
911        memmove(md->offset_vector, rec->offset_save,        memmove(md->offset_vector, rec->offset_save,
912          rec->saved_max * sizeof(int));          rec->saved_max * sizeof(int));
913        md->start_match = rec->save_start;        mstart = rec->save_start;
914        ims = original_ims;        ims = original_ims;
915        ecode = rec->after_call;        ecode = rec->after_call;
916        break;        break;
# Line 754  for (;;) Line 919  for (;;)
919      /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty      /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty
920      string - backtracking will then try other alternatives, if any. */      string - backtracking will then try other alternatives, if any. */
921    
922      if (md->notempty && eptr == md->start_match) RRETURN(MATCH_NOMATCH);      if (md->notempty && eptr == mstart) RRETURN(MATCH_NOMATCH);
923      md->end_match_ptr = eptr;          /* Record where we ended */      md->end_match_ptr = eptr;           /* Record where we ended */
924      md->end_offset_top = offset_top;   /* and how many extracts were taken */      md->end_offset_top = offset_top;    /* and how many extracts were taken */
925        md->start_match_ptr = mstart;       /* and the start (\K can modify) */
926      RRETURN(MATCH_MATCH);      RRETURN(MATCH_MATCH);
927    
928      /* Change option settings */      /* Change option settings */
# Line 777  for (;;) Line 943  for (;;)
943      case OP_ASSERTBACK:      case OP_ASSERTBACK:
944      do      do
945        {        {
946        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,
947          match_isgroup);          RM4);
948        if (rrc == MATCH_MATCH) break;        if (rrc == MATCH_MATCH) break;
949        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
950        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
951        }        }
952      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
# Line 804  for (;;) Line 970  for (;;)
970      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
971      do      do
972        {        {
973        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,
974          match_isgroup);          RM5);
975        if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);        if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);
976        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
977        ecode += GET(ecode,1);        ecode += GET(ecode,1);
978        }        }
979      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
# Line 826  for (;;) Line 992  for (;;)
992  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
993      if (utf8)      if (utf8)
994        {        {
995        c = GET(ecode,1);        i = GET(ecode, 1);
996        for (i = 0; i < c; i++)        while (i-- > 0)
997          {          {
998          eptr--;          eptr--;
999          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1000          BACKCHAR(eptr)          BACKCHAR(eptr);
1001          }          }
1002        }        }
1003      else      else
# Line 840  for (;;) Line 1006  for (;;)
1006      /* 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 */
1007    
1008        {        {
1009        eptr -= GET(ecode,1);        eptr -= GET(ecode, 1);
1010        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1011        }        }
1012    
# Line 862  for (;;) Line 1028  for (;;)
1028        cb.offset_vector    = md->offset_vector;        cb.offset_vector    = md->offset_vector;
1029        cb.subject          = (PCRE_SPTR)md->start_subject;        cb.subject          = (PCRE_SPTR)md->start_subject;
1030        cb.subject_length   = md->end_subject - md->start_subject;        cb.subject_length   = md->end_subject - md->start_subject;
1031        cb.start_match      = md->start_match - md->start_subject;        cb.start_match      = mstart - md->start_subject;
1032        cb.current_position = eptr - md->start_subject;        cb.current_position = eptr - md->start_subject;
1033        cb.pattern_position = GET(ecode, 2);        cb.pattern_position = GET(ecode, 2);
1034        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
# Line 897  for (;;) Line 1063  for (;;)
1063      case OP_RECURSE:      case OP_RECURSE:
1064        {        {
1065        callpat = md->start_code + GET(ecode, 1);        callpat = md->start_code + GET(ecode, 1);
1066        new_recursive.group_num = *callpat - OP_BRA;        new_recursive.group_num = (callpat == md->start_code)? 0 :
1067            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);  
1068    
1069        /* Add to "recursing stack" */        /* Add to "recursing stack" */
1070    
# Line 929  for (;;) Line 1090  for (;;)
1090    
1091        memcpy(new_recursive.offset_save, md->offset_vector,        memcpy(new_recursive.offset_save, md->offset_vector,
1092              new_recursive.saved_max * sizeof(int));              new_recursive.saved_max * sizeof(int));
1093        new_recursive.save_start = md->start_match;        new_recursive.save_start = mstart;
1094        md->start_match = eptr;        mstart = eptr;
1095    
1096        /* 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
1097        restore the offset and recursion data. */        restore the offset and recursion data. */
1098    
1099        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1100          flags = (*callpat >= OP_SBRA)? match_cbegroup : 0;
1101        do        do
1102          {          {
1103          RMATCH(rrc, eptr, callpat + 1 + LINK_SIZE, offset_top, md, ims,          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,
1104              eptrb, match_isgroup);            md, ims, eptrb, flags, RM6);
1105          if (rrc == MATCH_MATCH)          if (rrc == MATCH_MATCH)
1106            {            {
1107            DPRINTF(("Recursion matched\n"));            DPRINTF(("Recursion matched\n"));
# Line 948  for (;;) Line 1110  for (;;)
1110              (pcre_free)(new_recursive.offset_save);              (pcre_free)(new_recursive.offset_save);
1111            RRETURN(MATCH_MATCH);            RRETURN(MATCH_MATCH);
1112            }            }
1113          else if (rrc != MATCH_NOMATCH)          else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
1114            {            {
1115            DPRINTF(("Recursion gave error %d\n", rrc));            DPRINTF(("Recursion gave error %d\n", rrc));
1116              if (new_recursive.offset_save != stacksave)
1117                (pcre_free)(new_recursive.offset_save);
1118            RRETURN(rrc);            RRETURN(rrc);
1119            }            }
1120    
# Line 982  for (;;) Line 1146  for (;;)
1146    
1147      do      do
1148        {        {
1149        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM7);
         eptrb, match_isgroup);  
1150        if (rrc == MATCH_MATCH) break;        if (rrc == MATCH_MATCH) break;
1151        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
1152        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1153        }        }
1154      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
# Line 997  for (;;) Line 1160  for (;;)
1160      /* Continue as from after the assertion, updating the offsets high water      /* Continue as from after the assertion, updating the offsets high water
1161      mark, since extracts may have been taken. */      mark, since extracts may have been taken. */
1162    
1163      do ecode += GET(ecode,1); while (*ecode == OP_ALT);      do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
1164    
1165      offset_top = md->end_offset_top;      offset_top = md->end_offset_top;
1166      eptr = md->end_match_ptr;      eptr = md->end_match_ptr;
# Line 1028  for (;;) Line 1191  for (;;)
1191    
1192      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1193        {        {
1194        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM8);
1195        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1196        ecode = prev;        ecode = prev;
1197        flags = match_isgroup;        flags = 0;
1198        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1199        }        }
1200      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1201        {        {
1202        RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);        RMATCH(eptr, prev, offset_top, md, ims, eptrb, match_cbegroup, RM9);
1203        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1204        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1205        flags = 0;        flags = 0;
# Line 1051  for (;;) Line 1214  for (;;)
1214      do ecode += GET(ecode,1); while (*ecode == OP_ALT);      do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1215      break;      break;
1216    
1217      /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating      /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
1218      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
1219      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
1220      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
1221      preceded by BRAZERO or BRAMINZERO. */      optional ones preceded by BRAZERO or BRAMINZERO. */
1222    
1223      case OP_BRAZERO:      case OP_BRAZERO:
1224        {        {
1225        next = ecode+1;        next = ecode+1;
1226        RMATCH(rrc, eptr, next, offset_top, md, ims, eptrb, match_isgroup);        RMATCH(eptr, next, offset_top, md, ims, eptrb, 0, RM10);
1227        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1228        do next += GET(next,1); while (*next == OP_ALT);        do next += GET(next,1); while (*next == OP_ALT);
1229        ecode = next + 1+LINK_SIZE;        ecode = next + 1 + LINK_SIZE;
1230        }        }
1231      break;      break;
1232    
1233      case OP_BRAMINZERO:      case OP_BRAMINZERO:
1234        {        {
1235        next = ecode+1;        next = ecode+1;
1236        do next += GET(next,1); while (*next == OP_ALT);        do next += GET(next, 1); while (*next == OP_ALT);
1237        RMATCH(rrc, eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb,        RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0, RM11);
         match_isgroup);  
1238        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1239        ecode++;        ecode++;
1240        }        }
1241      break;      break;
1242    
1243      /* End of a group, repeated or non-repeating. If we are at the end of      case OP_SKIPZERO:
1244      an assertion "group", stop matching and return MATCH_MATCH, but record the        {
1245      current high water mark for use by positive assertions. Do this also        next = ecode+1;
1246      for the "once" (not-backup up) groups. */        do next += GET(next,1); while (*next == OP_ALT);
1247          ecode = next + 1 + LINK_SIZE;
1248          }
1249        break;
1250    
1251        /* End of a group, repeated or non-repeating. */
1252    
1253      case OP_KET:      case OP_KET:
1254      case OP_KETRMIN:      case OP_KETRMIN:
1255      case OP_KETRMAX:      case OP_KETRMAX:
1256      prev = ecode - GET(ecode, 1);      prev = ecode - GET(ecode, 1);
     saved_eptr = eptrb->epb_saved_eptr;  
1257    
1258      /* Back up the stack of bracket start pointers. */      /* If this was a group that remembered the subject start, in order to break
1259        infinite repeats of empty string matches, retrieve the subject start from
1260        the chain. Otherwise, set it NULL. */
1261    
1262        if (*prev >= OP_SBRA)
1263          {
1264          saved_eptr = eptrb->epb_saved_eptr;   /* Value at start of group */
1265          eptrb = eptrb->epb_prev;              /* Backup to previous group */
1266          }
1267        else saved_eptr = NULL;
1268    
1269      eptrb = eptrb->epb_prev;      /* If we are at the end of an assertion group, stop matching and return
1270        MATCH_MATCH, but record the current high water mark for use by positive
1271        assertions. Do this also for the "once" (atomic) groups. */
1272    
1273      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
1274          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||
# Line 1102  for (;;) Line 1279  for (;;)
1279        RRETURN(MATCH_MATCH);        RRETURN(MATCH_MATCH);
1280        }        }
1281    
1282      /* In all other cases except a conditional group we have to check the      /* For capturing groups we have to check the group number back at the start
1283      group number back at the start and if necessary complete handling an      and if necessary complete handling an extraction by setting the offsets and
1284      extraction by setting the offsets and bumping the high water mark. */      bumping the high water mark. Note that whole-pattern recursion is coded as
1285        a recurse into group 0, so it won't be picked up here. Instead, we catch it
1286        when the OP_END is reached. Other recursion is handled here. */
1287    
1288      if (*prev != OP_COND)      if (*prev == OP_CBRA || *prev == OP_SCBRA)
1289        {        {
1290        number = *prev - OP_BRA;        number = GET2(prev, 1+LINK_SIZE);
   
       /* For extended extraction brackets (large number), we have to fish out  
       the number from a dummy opcode at the start. */  
   
       if (number > EXTRACT_BASIC_MAX) number = GET2(prev, 2+LINK_SIZE);  
1291        offset = number << 1;        offset = number << 1;
1292    
1293  #ifdef DEBUG  #ifdef DEBUG
# Line 1121  for (;;) Line 1295  for (;;)
1295        printf("\n");        printf("\n");
1296  #endif  #endif
1297    
1298        /* Test for a numbered group. This includes groups called as a result        md->capture_last = number;
1299        of recursion. Note that whole-pattern recursion is coded as a recurse        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
       into group 0, so it won't be picked up here. Instead, we catch it when  
       the OP_END is reached. */  
   
       if (number > 0)  
1300          {          {
1301          md->capture_last = number;          md->offset_vector[offset] =
1302          if (offset >= md->offset_max) md->offset_overflow = TRUE; else            md->offset_vector[md->offset_end - number];
1303            {          md->offset_vector[offset+1] = eptr - md->start_subject;
1304            md->offset_vector[offset] =          if (offset_top <= offset) offset_top = offset + 2;
1305              md->offset_vector[md->offset_end - number];          }
1306            md->offset_vector[offset+1] = eptr - md->start_subject;  
1307            if (offset_top <= offset) offset_top = offset + 2;        /* Handle a recursively called group. Restore the offsets
1308            }        appropriately and continue from after the call. */
1309    
1310          /* Handle a recursively called group. Restore the offsets        if (md->recursive != NULL && md->recursive->group_num == number)
1311          appropriately and continue from after the call. */          {
1312            recursion_info *rec = md->recursive;
1313          if (md->recursive != NULL && md->recursive->group_num == number)          DPRINTF(("Recursion (%d) succeeded - continuing\n", number));
1314            {          md->recursive = rec->prevrec;
1315            recursion_info *rec = md->recursive;          mstart = rec->save_start;
1316            DPRINTF(("Recursion (%d) succeeded - continuing\n", number));          memcpy(md->offset_vector, rec->offset_save,
1317            md->recursive = rec->prevrec;            rec->saved_max * sizeof(int));
1318            md->start_match = rec->save_start;          ecode = rec->after_call;
1319            memcpy(md->offset_vector, rec->offset_save,          ims = original_ims;
1320              rec->saved_max * sizeof(int));          break;
           ecode = rec->after_call;  
           ims = original_ims;  
           break;  
           }  
1321          }          }
1322        }        }
1323    
1324      /* Reset the value of the ims flags, in case they got changed during      /* For both capturing and non-capturing groups, reset the value of the ims
1325      the group. */      flags, in case they got changed during the group. */
1326    
1327      ims = original_ims;      ims = original_ims;
1328      DPRINTF(("ims reset to %02lx\n", ims));      DPRINTF(("ims reset to %02lx\n", ims));
# Line 1175  for (;;) Line 1341  for (;;)
1341    
1342      /* The repeating kets try the rest of the pattern or restart from the      /* The repeating kets try the rest of the pattern or restart from the
1343      preceding bracket, in the appropriate order. In the second case, we can use      preceding bracket, in the appropriate order. In the second case, we can use
1344      tail recursion to avoid using another stack frame. */      tail recursion to avoid using another stack frame, unless we have an
1345        unlimited repeat of a group that can match an empty string. */
1346    
1347        flags = (*prev >= OP_SBRA)? match_cbegroup : 0;
1348    
1349      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1350        {        {
1351        RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM12);
1352        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1353          if (flags != 0)    /* Could match an empty string */
1354            {
1355            RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM50);
1356            RRETURN(rrc);
1357            }
1358        ecode = prev;        ecode = prev;
       flags = match_isgroup;  
1359        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1360        }        }
1361      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1362        {        {
1363        RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);        RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM13);
1364        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1365        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1366        flags = 0;        flags = 0;
# Line 1202  for (;;) Line 1375  for (;;)
1375      if ((ims & PCRE_MULTILINE) != 0)      if ((ims & PCRE_MULTILINE) != 0)
1376        {        {
1377        if (eptr != md->start_subject &&        if (eptr != md->start_subject &&
1378            (eptr == md->end_subject ||            (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
            eptr < md->start_subject + md->nllen ||  
            !IS_NEWLINE(eptr - md->nllen)))  
1379          RRETURN(MATCH_NOMATCH);          RRETURN(MATCH_NOMATCH);
1380        ecode++;        ecode++;
1381        break;        break;
# Line 1225  for (;;) Line 1396  for (;;)
1396      ecode++;      ecode++;
1397      break;      break;
1398    
1399        /* Reset the start of match point */
1400    
1401        case OP_SET_SOM:
1402        mstart = eptr;
1403        ecode++;
1404        break;
1405    
1406      /* Assert before internal newline if multiline, or before a terminating      /* Assert before internal newline if multiline, or before a terminating
1407      newline unless endonly is set, else end of subject unless noteol is set. */      newline unless endonly is set, else end of subject unless noteol is set. */
1408    
# Line 1244  for (;;) Line 1422  for (;;)
1422        if (!md->endonly)        if (!md->endonly)
1423          {          {
1424          if (eptr != md->end_subject &&          if (eptr != md->end_subject &&
1425              (eptr != md->end_subject - md->nllen || !IS_NEWLINE(eptr)))              (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
1426            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1427          ecode++;          ecode++;
1428          break;          break;
# Line 1263  for (;;) Line 1441  for (;;)
1441    
1442      case OP_EODN:      case OP_EODN:
1443      if (eptr != md->end_subject &&      if (eptr != md->end_subject &&
1444          (eptr != md->end_subject - md->nllen || !IS_NEWLINE(eptr)))          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
1445        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1446      ecode++;      ecode++;
1447      break;      break;
# Line 1283  for (;;) Line 1461  for (;;)
1461          {          {
1462          if (eptr == md->start_subject) prev_is_word = FALSE; else          if (eptr == md->start_subject) prev_is_word = FALSE; else
1463            {            {
1464            const uschar *lastptr = eptr - 1;            USPTR lastptr = eptr - 1;
1465            while((*lastptr & 0xc0) == 0x80) lastptr--;            while((*lastptr & 0xc0) == 0x80) lastptr--;
1466            GETCHAR(c, lastptr);            GETCHAR(c, lastptr);
1467            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;            prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
# Line 1317  for (;;) Line 1495  for (;;)
1495      /* Match a single character type; inline for speed */      /* Match a single character type; inline for speed */
1496    
1497      case OP_ANY:      case OP_ANY:
1498      if ((ims & PCRE_DOTALL) == 0)      if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
1499        {      /* Fall through */
1500        if (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr))  
1501          RRETURN(MATCH_NOMATCH);      case OP_ALLANY:
       }  
1502      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);
1503      if (utf8)      if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
       while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;  
1504      ecode++;      ecode++;
1505      break;      break;
1506    
# Line 1414  for (;;) Line 1590  for (;;)
1590      ecode++;      ecode++;
1591      break;      break;
1592    
1593        case OP_ANYNL:
1594        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1595        GETCHARINCTEST(c, eptr);
1596        switch(c)
1597          {
1598          default: RRETURN(MATCH_NOMATCH);
1599          case 0x000d:
1600          if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
1601          break;
1602    
1603          case 0x000a:
1604          break;
1605    
1606          case 0x000b:
1607          case 0x000c:
1608          case 0x0085:
1609          case 0x2028:
1610          case 0x2029:
1611          if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
1612          break;
1613          }
1614        ecode++;
1615        break;
1616    
1617        case OP_NOT_HSPACE:
1618        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1619        GETCHARINCTEST(c, eptr);
1620        switch(c)
1621          {
1622          default: break;
1623          case 0x09:      /* HT */
1624          case 0x20:      /* SPACE */
1625          case 0xa0:      /* NBSP */
1626          case 0x1680:    /* OGHAM SPACE MARK */
1627          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
1628          case 0x2000:    /* EN QUAD */
1629          case 0x2001:    /* EM QUAD */
1630          case 0x2002:    /* EN SPACE */
1631          case 0x2003:    /* EM SPACE */
1632          case 0x2004:    /* THREE-PER-EM SPACE */
1633          case 0x2005:    /* FOUR-PER-EM SPACE */
1634          case 0x2006:    /* SIX-PER-EM SPACE */
1635          case 0x2007:    /* FIGURE SPACE */
1636          case 0x2008:    /* PUNCTUATION SPACE */
1637          case 0x2009:    /* THIN SPACE */
1638          case 0x200A:    /* HAIR SPACE */
1639          case 0x202f:    /* NARROW NO-BREAK SPACE */
1640          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
1641          case 0x3000:    /* IDEOGRAPHIC SPACE */
1642          RRETURN(MATCH_NOMATCH);
1643          }
1644        ecode++;
1645        break;
1646    
1647        case OP_HSPACE:
1648        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1649        GETCHARINCTEST(c, eptr);
1650        switch(c)
1651          {
1652          default: RRETURN(MATCH_NOMATCH);
1653          case 0x09:      /* HT */
1654          case 0x20:      /* SPACE */
1655          case 0xa0:      /* NBSP */
1656          case 0x1680:    /* OGHAM SPACE MARK */
1657          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
1658          case 0x2000:    /* EN QUAD */
1659          case 0x2001:    /* EM QUAD */
1660          case 0x2002:    /* EN SPACE */
1661          case 0x2003:    /* EM SPACE */
1662          case 0x2004:    /* THREE-PER-EM SPACE */
1663          case 0x2005:    /* FOUR-PER-EM SPACE */
1664          case 0x2006:    /* SIX-PER-EM SPACE */
1665          case 0x2007:    /* FIGURE SPACE */
1666          case 0x2008:    /* PUNCTUATION SPACE */
1667          case 0x2009:    /* THIN SPACE */
1668          case 0x200A:    /* HAIR SPACE */
1669          case 0x202f:    /* NARROW NO-BREAK SPACE */
1670          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
1671          case 0x3000:    /* IDEOGRAPHIC SPACE */
1672          break;
1673          }
1674        ecode++;
1675        break;
1676    
1677        case OP_NOT_VSPACE:
1678        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1679        GETCHARINCTEST(c, eptr);
1680        switch(c)
1681          {
1682          default: break;
1683          case 0x0a:      /* LF */
1684          case 0x0b:      /* VT */
1685          case 0x0c:      /* FF */
1686          case 0x0d:      /* CR */
1687          case 0x85:      /* NEL */
1688          case 0x2028:    /* LINE SEPARATOR */
1689          case 0x2029:    /* PARAGRAPH SEPARATOR */
1690          RRETURN(MATCH_NOMATCH);
1691          }
1692        ecode++;
1693        break;
1694    
1695        case OP_VSPACE:
1696        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1697        GETCHARINCTEST(c, eptr);
1698        switch(c)
1699          {
1700          default: RRETURN(MATCH_NOMATCH);
1701          case 0x0a:      /* LF */
1702          case 0x0b:      /* VT */
1703          case 0x0c:      /* FF */
1704          case 0x0d:      /* CR */
1705          case 0x85:      /* NEL */
1706          case 0x2028:    /* LINE SEPARATOR */
1707          case 0x2029:    /* PARAGRAPH SEPARATOR */
1708          break;
1709          }
1710        ecode++;
1711        break;
1712    
1713  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
1714      /* Check the next character by Unicode property. We will get here only      /* Check the next character by Unicode property. We will get here only
1715      if the support is in the binary; otherwise a compile-time error occurs. */      if the support is in the binary; otherwise a compile-time error occurs. */
# Line 1423  for (;;) Line 1719  for (;;)
1719      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1720      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1721        {        {
1722        int chartype, script;        const ucd_record *prop = GET_UCD(c);
       int category = _pcre_ucp_findprop(c, &chartype, &script);  
1723    
1724        switch(ecode[1])        switch(ecode[1])
1725          {          {
# Line 1433  for (;;) Line 1728  for (;;)
1728          break;          break;
1729    
1730          case PT_LAMP:          case PT_LAMP:
1731          if ((chartype == ucp_Lu ||          if ((prop->chartype == ucp_Lu ||
1732               chartype == ucp_Ll ||               prop->chartype == ucp_Ll ||
1733               chartype == ucp_Lt) == (op == OP_NOTPROP))               prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
1734            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1735           break;           break;
1736    
1737          case PT_GC:          case PT_GC:
1738          if ((ecode[2] != category) == (op == OP_PROP))          if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))
1739            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1740          break;          break;
1741    
1742          case PT_PC:          case PT_PC:
1743          if ((ecode[2] != chartype) == (op == OP_PROP))          if ((ecode[2] != prop->chartype) == (op == OP_PROP))
1744            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1745          break;          break;
1746    
1747          case PT_SC:          case PT_SC:
1748          if ((ecode[2] != script) == (op == OP_PROP))          if ((ecode[2] != prop->script) == (op == OP_PROP))
1749            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1750          break;          break;
1751    
1752          default:          default:
1753          RRETURN(PCRE_ERROR_INTERNAL);          RRETURN(PCRE_ERROR_INTERNAL);
         break;  
1754          }          }
1755    
1756        ecode += 3;        ecode += 3;
# Line 1470  for (;;) Line 1764  for (;;)
1764      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1765      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1766        {        {
1767        int chartype, script;        int category = UCD_CATEGORY(c);
       int category = _pcre_ucp_findprop(c, &chartype, &script);  
1768        if (category == ucp_M) RRETURN(MATCH_NOMATCH);        if (category == ucp_M) RRETURN(MATCH_NOMATCH);
1769        while (eptr < md->end_subject)        while (eptr < md->end_subject)
1770          {          {
# Line 1480  for (;;) Line 1773  for (;;)
1773            {            {
1774            GETCHARLEN(c, eptr, len);            GETCHARLEN(c, eptr, len);
1775            }            }
1776          category = _pcre_ucp_findprop(c, &chartype, &script);          category = UCD_CATEGORY(c);
1777          if (category != ucp_M) break;          if (category != ucp_M) break;
1778          eptr += len;          eptr += len;
1779          }          }
# Line 1501  for (;;) Line 1794  for (;;)
1794      case OP_REF:      case OP_REF:
1795        {        {
1796        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */
1797        ecode += 3;                                 /* Advance past item */        ecode += 3;
1798    
1799          /* If the reference is unset, there are two possibilities:
1800    
1801          (a) In the default, Perl-compatible state, set the length to be longer
1802          than the amount of subject left; this ensures that every attempt at a
1803          match fails. We can't just fail here, because of the possibility of
1804          quantifiers with zero minima.
1805    
1806          (b) If the JavaScript compatibility flag is set, set the length to zero
1807          so that the back reference matches an empty string.
1808    
1809        /* If the reference is unset, set the length to be longer than the amount        Otherwise, set the length to the length of what was matched by the
1810        of subject left; this ensures that every attempt at a match fails. We        referenced subpattern. */
1811        can't just fail here, because of the possibility of quantifiers with zero  
1812        minima. */        if (offset >= offset_top || md->offset_vector[offset] < 0)
1813            length = (md->jscript_compat)? 0 : md->end_subject - eptr + 1;
1814        length = (offset >= offset_top || md->offset_vector[offset] < 0)?        else
1815          md->end_subject - eptr + 1 :          length = md->offset_vector[offset+1] - md->offset_vector[offset];
         md->offset_vector[offset+1] - md->offset_vector[offset];  
1816    
1817        /* Set up for repetition, or handle the non-repeated case */        /* Set up for repetition, or handle the non-repeated case */
1818    
# Line 1570  for (;;) Line 1872  for (;;)
1872          {          {
1873          for (fi = min;; fi++)          for (fi = min;; fi++)
1874            {            {
1875            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM14);
1876            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1877            if (fi >= max || !match_ref(offset, eptr, length, md, ims))            if (fi >= max || !match_ref(offset, eptr, length, md, ims))
1878              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
# Line 1591  for (;;) Line 1893  for (;;)
1893            }            }
1894          while (eptr >= pp)          while (eptr >= pp)
1895            {            {
1896            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM15);
1897            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1898            eptr -= length;            eptr -= length;
1899            }            }
# Line 1696  for (;;) Line 1998  for (;;)
1998            {            {
1999            for (fi = min;; fi++)            for (fi = min;; fi++)
2000              {              {
2001              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM16);
2002              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2003              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2004              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
# Line 1716  for (;;) Line 2018  for (;;)
2018            {            {
2019            for (fi = min;; fi++)            for (fi = min;; fi++)
2020              {              {
2021              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM17);
2022              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2023              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2024              c = *eptr++;              c = *eptr++;
# Line 1753  for (;;) Line 2055  for (;;)
2055              }              }
2056            for (;;)            for (;;)
2057              {              {
2058              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM18);
2059              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2060              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2061              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 1772  for (;;) Line 2074  for (;;)
2074              }              }
2075            while (eptr >= pp)            while (eptr >= pp)
2076              {              {
2077              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM19);
2078              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2079              eptr--;              eptr--;
2080              }              }
# Line 1785  for (;;) Line 2087  for (;;)
2087    
2088    
2089      /* Match an extended character class. This opcode is encountered only      /* Match an extended character class. This opcode is encountered only
2090      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
2091        mode, because Unicode properties are supported in non-UTF-8 mode. */
2092    
2093  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2094      case OP_XCLASS:      case OP_XCLASS:
# Line 1827  for (;;) Line 2130  for (;;)
2130        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
2131          {          {
2132          if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);          if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2133          GETCHARINC(c, eptr);          GETCHARINCTEST(c, eptr);
2134          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);
2135          }          }
2136    
# Line 1843  for (;;) Line 2146  for (;;)
2146          {          {
2147          for (fi = min;; fi++)          for (fi = min;; fi++)
2148            {            {
2149            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM20);
2150            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2151            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2152            GETCHARINC(c, eptr);            GETCHARINCTEST(c, eptr);
2153            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);
2154            }            }
2155          /* Control never gets here */          /* Control never gets here */
# Line 1861  for (;;) Line 2164  for (;;)
2164            {            {
2165            int len = 1;            int len = 1;
2166            if (eptr >= md->end_subject) break;            if (eptr >= md->end_subject) break;
2167            GETCHARLEN(c, eptr, len);            GETCHARLENTEST(c, eptr, len);
2168            if (!_pcre_xclass(c, data)) break;            if (!_pcre_xclass(c, data)) break;
2169            eptr += len;            eptr += len;
2170            }            }
2171          for(;;)          for(;;)
2172            {            {
2173            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM21);
2174            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2175            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
2176            BACKCHAR(eptr)            if (utf8) BACKCHAR(eptr);
2177            }            }
2178          RRETURN(MATCH_NOMATCH);          RRETURN(MATCH_NOMATCH);
2179          }          }
# Line 1926  for (;;) Line 2229  for (;;)
2229    
2230        else        else
2231          {          {
2232          int dc;          unsigned int dc;
2233          GETCHARINC(dc, eptr);          GETCHARINC(dc, eptr);
2234          ecode += length;          ecode += length;
2235    
# Line 1936  for (;;) Line 2239  for (;;)
2239          if (fc != dc)          if (fc != dc)
2240            {            {
2241  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2242            if (dc != _pcre_ucp_othercase(fc))            if (dc != UCD_OTHERCASE(fc))
2243  #endif  #endif
2244              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
2245            }            }
# Line 1953  for (;;) Line 2256  for (;;)
2256        }        }
2257      break;      break;
2258    
2259      /* Match a single character repeatedly; different opcodes share code. */      /* Match a single character repeatedly. */
2260    
2261      case OP_EXACT:      case OP_EXACT:
2262      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
2263      ecode += 3;      ecode += 3;
2264      goto REPEATCHAR;      goto REPEATCHAR;
2265    
2266        case OP_POSUPTO:
2267        possessive = TRUE;
2268        /* Fall through */
2269    
2270      case OP_UPTO:      case OP_UPTO:
2271      case OP_MINUPTO:      case OP_MINUPTO:
2272      min = 0;      min = 0;
# Line 1968  for (;;) Line 2275  for (;;)
2275      ecode += 3;      ecode += 3;
2276      goto REPEATCHAR;      goto REPEATCHAR;
2277    
2278        case OP_POSSTAR:
2279        possessive = TRUE;
2280        min = 0;
2281        max = INT_MAX;
2282        ecode++;
2283        goto REPEATCHAR;
2284    
2285        case OP_POSPLUS:
2286        possessive = TRUE;
2287        min = 1;
2288        max = INT_MAX;
2289        ecode++;
2290        goto REPEATCHAR;
2291    
2292        case OP_POSQUERY:
2293        possessive = TRUE;
2294        min = 0;
2295        max = 1;
2296        ecode++;
2297        goto REPEATCHAR;
2298    
2299      case OP_STAR:      case OP_STAR:
2300      case OP_MINSTAR:      case OP_MINSTAR:
2301      case OP_PLUS:      case OP_PLUS:
# Line 1999  for (;;) Line 2327  for (;;)
2327    
2328        if (length > 1)        if (length > 1)
2329          {          {
         int oclength = 0;  
         uschar occhars[8];  
   
2330  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2331          int othercase;          unsigned int othercase;
2332          if ((ims & PCRE_CASELESS) != 0 &&          if ((ims & PCRE_CASELESS) != 0 &&
2333              (othercase = _pcre_ucp_othercase(fc)) >= 0 &&              (othercase = UCD_OTHERCASE(fc)) != fc)
              othercase >= 0)  
2334            oclength = _pcre_ord2utf8(othercase, occhars);            oclength = _pcre_ord2utf8(othercase, occhars);
2335            else oclength = 0;
2336  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2337    
2338          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2339            {            {
2340            if (memcmp(eptr, charptr, length) == 0) eptr += length;            if (memcmp(eptr, charptr, length) == 0) eptr += length;
2341    #ifdef SUPPORT_UCP
2342            /* Need braces because of following else */            /* Need braces because of following else */
2343            else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }            else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }
2344            else            else
# Line 2020  for (;;) Line 2346  for (;;)
2346              if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);              if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);
2347              eptr += oclength;              eptr += oclength;
2348              }              }
2349    #else   /* without SUPPORT_UCP */
2350              else { RRETURN(MATCH_NOMATCH); }
2351    #endif  /* SUPPORT_UCP */
2352            }            }
2353    
2354          if (min == max) continue;          if (min == max) continue;
# Line 2028  for (;;) Line 2357  for (;;)
2357            {            {
2358            for (fi = min;; fi++)            for (fi = min;; fi++)
2359              {              {
2360              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM22);
2361              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2362              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2363              if (memcmp(eptr, charptr, length) == 0) eptr += length;              if (memcmp(eptr, charptr, length) == 0) eptr += length;
2364    #ifdef SUPPORT_UCP
2365              /* Need braces because of following else */              /* Need braces because of following else */
2366              else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }              else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }
2367              else              else
# Line 2039  for (;;) Line 2369  for (;;)
2369                if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);                if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);
2370                eptr += oclength;                eptr += oclength;
2371                }                }
2372    #else   /* without SUPPORT_UCP */
2373                else { RRETURN (MATCH_NOMATCH); }
2374    #endif  /* SUPPORT_UCP */
2375              }              }
2376            /* Control never gets here */            /* Control never gets here */
2377            }            }
2378          else  
2379            else  /* Maximize */
2380            {            {
2381            pp = eptr;            pp = eptr;
2382            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2383              {              {
2384              if (eptr > md->end_subject - length) break;              if (eptr > md->end_subject - length) break;
2385              if (memcmp(eptr, charptr, length) == 0) eptr += length;              if (memcmp(eptr, charptr, length) == 0) eptr += length;
2386    #ifdef SUPPORT_UCP
2387              else if (oclength == 0) break;              else if (oclength == 0) break;
2388              else              else
2389                {                {
2390                if (memcmp(eptr, occhars, oclength) != 0) break;                if (memcmp(eptr, occhars, oclength) != 0) break;
2391                eptr += oclength;                eptr += oclength;
2392                }                }
2393    #else   /* without SUPPORT_UCP */
2394                else break;
2395    #endif  /* SUPPORT_UCP */
2396              }              }
2397            while (eptr >= pp)  
2398              if (possessive) continue;
2399              for(;;)
2400             {             {
2401             RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);             RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM23);
2402             if (rrc != MATCH_NOMATCH) RRETURN(rrc);             if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2403               if (eptr == pp) RRETURN(MATCH_NOMATCH);
2404    #ifdef SUPPORT_UCP
2405               eptr--;
2406               BACKCHAR(eptr);
2407    #else   /* without SUPPORT_UCP */
2408             eptr -= length;             eptr -= length;
2409    #endif  /* SUPPORT_UCP */
2410             }             }
           RRETURN(MATCH_NOMATCH);  
2411            }            }
2412          /* Control never gets here */          /* Control never gets here */
2413          }          }
# Line 2102  for (;;) Line 2447  for (;;)
2447          {          {
2448          for (fi = min;; fi++)          for (fi = min;; fi++)
2449            {            {
2450            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM24);
2451            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2452            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max || eptr >= md->end_subject ||
2453                fc != md->lcc[*eptr++])                fc != md->lcc[*eptr++])
# Line 2110  for (;;) Line 2455  for (;;)
2455            }            }
2456          /* Control never gets here */          /* Control never gets here */
2457          }          }
2458        else        else  /* Maximize */
2459          {          {
2460          pp = eptr;          pp = eptr;
2461          for (i = min; i < max; i++)          for (i = min; i < max; i++)
# Line 2118  for (;;) Line 2463  for (;;)
2463            if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break;            if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break;
2464            eptr++;            eptr++;
2465            }            }
2466            if (possessive) continue;
2467          while (eptr >= pp)          while (eptr >= pp)
2468            {            {
2469            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM25);
2470            eptr--;            eptr--;
2471            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2472            }            }
# Line 2139  for (;;) Line 2485  for (;;)
2485          {          {
2486          for (fi = min;; fi++)          for (fi = min;; fi++)
2487            {            {
2488            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM26);
2489            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2490            if (fi >= max || eptr >= md->end_subject || fc != *eptr++)            if (fi >= max || eptr >= md->end_subject || fc != *eptr++)
2491              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
2492            }            }
2493          /* Control never gets here */          /* Control never gets here */
2494          }          }
2495        else        else  /* Maximize */
2496          {          {
2497          pp = eptr;          pp = eptr;
2498          for (i = min; i < max; i++)          for (i = min; i < max; i++)
# Line 2154  for (;;) Line 2500  for (;;)
2500            if (eptr >= md->end_subject || fc != *eptr) break;            if (eptr >= md->end_subject || fc != *eptr) break;
2501            eptr++;            eptr++;
2502            }            }
2503            if (possessive) continue;
2504          while (eptr >= pp)          while (eptr >= pp)
2505            {            {
2506            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM27);
2507            eptr--;            eptr--;
2508            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2509            }            }
# Line 2206  for (;;) Line 2553  for (;;)
2553      ecode += 3;      ecode += 3;
2554      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
2555    
2556        case OP_NOTPOSSTAR:
2557        possessive = TRUE;
2558        min = 0;
2559        max = INT_MAX;
2560        ecode++;
2561        goto REPEATNOTCHAR;
2562    
2563        case OP_NOTPOSPLUS:
2564        possessive = TRUE;
2565        min = 1;
2566        max = INT_MAX;
2567        ecode++;
2568        goto REPEATNOTCHAR;
2569    
2570        case OP_NOTPOSQUERY:
2571        possessive = TRUE;
2572        min = 0;
2573        max = 1;
2574        ecode++;
2575        goto REPEATNOTCHAR;
2576    
2577        case OP_NOTPOSUPTO:
2578        possessive = TRUE;
2579        min = 0;
2580        max = GET2(ecode, 1);
2581        ecode += 3;
2582        goto REPEATNOTCHAR;
2583    
2584      case OP_NOTSTAR:      case OP_NOTSTAR:
2585      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
2586      case OP_NOTPLUS:      case OP_NOTPLUS:
# Line 2245  for (;;) Line 2620  for (;;)
2620        /* UTF-8 mode */        /* UTF-8 mode */
2621        if (utf8)        if (utf8)
2622          {          {
2623          register int d;          register unsigned int d;
2624          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2625            {            {
2626            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
# Line 2270  for (;;) Line 2645  for (;;)
2645          /* UTF-8 mode */          /* UTF-8 mode */
2646          if (utf8)          if (utf8)
2647            {            {
2648            register int d;            register unsigned int d;
2649            for (fi = min;; fi++)            for (fi = min;; fi++)
2650              {              {
2651              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM28);
2652              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2653                if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2654              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
2655              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
2656              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) RRETURN(MATCH_NOMATCH);
2657                RRETURN(MATCH_NOMATCH);  
2658              }              }
2659            }            }
2660          else          else
# Line 2287  for (;;) Line 2663  for (;;)
2663            {            {
2664            for (fi = min;; fi++)            for (fi = min;; fi++)
2665              {              {
2666              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM29);
2667              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2668              if (fi >= max || eptr >= md->end_subject || fc == md->lcc[*eptr++])              if (fi >= max || eptr >= md->end_subject || fc == md->lcc[*eptr++])
2669                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
# Line 2306  for (;;) Line 2682  for (;;)
2682          /* UTF-8 mode */          /* UTF-8 mode */
2683          if (utf8)          if (utf8)
2684            {            {
2685            register int d;            register unsigned int d;
2686            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2687              {              {
2688              int len = 1;              int len = 1;
# Line 2316  for (;;) Line 2692  for (;;)
2692              if (fc == d) break;              if (fc == d) break;
2693              eptr += len;              eptr += len;
2694              }              }
2695            for(;;)          if (possessive) continue;
2696            for(;;)
2697              {              {
2698              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM30);
2699              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2700              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2701              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2333  for (;;) Line 2710  for (;;)
2710              if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break;              if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break;
2711              eptr++;              eptr++;
2712              }              }
2713              if (possessive) continue;
2714            while (eptr >= pp)            while (eptr >= pp)
2715              {              {
2716              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM31);
2717              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2718              eptr--;              eptr--;
2719              }              }
# Line 2354  for (;;) Line 2732  for (;;)
2732        /* UTF-8 mode */        /* UTF-8 mode */
2733        if (utf8)        if (utf8)
2734          {          {
2735          register int d;          register unsigned int d;
2736          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2737            {            {
2738            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
# Line 2377  for (;;) Line 2755  for (;;)
2755          /* UTF-8 mode */          /* UTF-8 mode */
2756          if (utf8)          if (utf8)
2757            {            {
2758            register int d;            register unsigned int d;
2759            for (fi = min;; fi++)            for (fi = min;; fi++)
2760              {              {
2761              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM32);
2762              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2763                if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2764              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
2765              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) RRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
2766              }              }
2767            }            }
2768          else          else
# Line 2393  for (;;) Line 2771  for (;;)
2771            {            {
2772            for (fi = min;; fi++)            for (fi = min;; fi++)
2773              {              {
2774              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM33);
2775              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2776              if (fi >= max || eptr >= md->end_subject || fc == *eptr++)              if (fi >= max || eptr >= md->end_subject || fc == *eptr++)
2777                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
# Line 2412  for (;;) Line 2790  for (;;)
2790          /* UTF-8 mode */          /* UTF-8 mode */
2791          if (utf8)          if (utf8)
2792            {            {
2793            register int d;            register unsigned int d;
2794            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2795              {              {
2796              int len = 1;              int len = 1;
# Line 2421  for (;;) Line 2799  for (;;)
2799              if (fc == d) break;              if (fc == d) break;
2800              eptr += len;              eptr += len;
2801              }              }
2802              if (possessive) continue;
2803            for(;;)            for(;;)
2804              {              {
2805              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM34);
2806              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2807              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2808              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2438  for (;;) Line 2817  for (;;)
2817              if (eptr >= md->end_subject || fc == *eptr) break;              if (eptr >= md->end_subject || fc == *eptr) break;
2818              eptr++;              eptr++;
2819              }              }
2820              if (possessive) continue;
2821            while (eptr >= pp)            while (eptr >= pp)
2822              {              {
2823              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM35);
2824              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2825              eptr--;              eptr--;
2826              }              }
# Line 2469  for (;;) Line 2849  for (;;)
2849      ecode += 3;      ecode += 3;
2850      goto REPEATTYPE;      goto REPEATTYPE;
2851    
2852      case OP_TYPESTAR:      case OP_TYPEPOSSTAR:
2853      case OP_TYPEMINSTAR:      possessive = TRUE;
2854      case OP_TYPEPLUS:      min = 0;
2855      case OP_TYPEMINPLUS:      max = INT_MAX;
2856      case OP_TYPEQUERY:      ecode++;
2857      case OP_TYPEMINQUERY:      goto REPEATTYPE;
2858      c = *ecode++ - OP_TYPESTAR;  
2859      minimize = (c & 1) != 0;      case OP_TYPEPOSPLUS:
2860      min = rep_min[c];                 /* Pick up values from tables; */      possessive = TRUE;
2861      max = rep_max[c];                 /* zero for max => infinity */      min = 1;
2862      if (max == 0) max = INT_MAX;      max = INT_MAX;
2863        ecode++;
2864        goto REPEATTYPE;
2865    
2866        case OP_TYPEPOSQUERY:
2867        possessive = TRUE;
2868        min = 0;
2869        max = 1;
2870        ecode++;
2871        goto REPEATTYPE;
2872    
2873        case OP_TYPEPOSUPTO:
2874        possessive = TRUE;
2875        min = 0;
2876        max = GET2(ecode, 1);
2877        ecode += 3;
2878        goto REPEATTYPE;
2879    
2880        case OP_TYPESTAR:
2881        case OP_TYPEMINSTAR:
2882        case OP_TYPEPLUS:
2883        case OP_TYPEMINPLUS:
2884        case OP_TYPEQUERY:
2885        case OP_TYPEMINQUERY:
2886        c = *ecode++ - OP_TYPESTAR;
2887        minimize = (c & 1) != 0;
2888        min = rep_min[c];                 /* Pick up values from tables; */
2889        max = rep_max[c];                 /* zero for max => infinity */
2890        if (max == 0) max = INT_MAX;
2891    
2892      /* Common code for all repeated single character type matches. Note that      /* Common code for all repeated single character type matches. Note that
2893      in UTF-8 mode, '.' matches a character of any length, but for the other      in UTF-8 mode, '.' matches a character of any length, but for the other
# Line 2519  for (;;) Line 2927  for (;;)
2927            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2928              {              {
2929              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2930              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2931              }              }
2932            break;            break;
2933    
# Line 2527  for (;;) Line 2935  for (;;)
2935            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2936              {              {
2937              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2938              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2939              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
2940              if ((prop_chartype == ucp_Lu ||              if ((prop_chartype == ucp_Lu ||
2941                   prop_chartype == ucp_Ll ||                   prop_chartype == ucp_Ll ||
2942                   prop_chartype == ucp_Lt) == prop_fail_result)                   prop_chartype == ucp_Lt) == prop_fail_result)
# Line 2540  for (;;) Line 2948  for (;;)
2948            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2949              {              {
2950              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2951              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2952              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
2953              if ((prop_category == prop_value) == prop_fail_result)              if ((prop_category == prop_value) == prop_fail_result)
2954                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
2955              }              }
# Line 2551  for (;;) Line 2959  for (;;)
2959            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2960              {              {
2961              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2962              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2963              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
2964              if ((prop_chartype == prop_value) == prop_fail_result)              if ((prop_chartype == prop_value) == prop_fail_result)
2965                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
2966              }              }
# Line 2562  for (;;) Line 2970  for (;;)
2970            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2971              {              {
2972              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2973              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2974              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_script = UCD_SCRIPT(c);
2975              if ((prop_script == prop_value) == prop_fail_result)              if ((prop_script == prop_value) == prop_fail_result)
2976                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
2977              }              }
# Line 2571  for (;;) Line 2979  for (;;)
2979    
2980            default:            default:
2981            RRETURN(PCRE_ERROR_INTERNAL);            RRETURN(PCRE_ERROR_INTERNAL);
           break;  
2982            }            }
2983          }          }
2984    
# Line 2583  for (;;) Line 2990  for (;;)
2990          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2991            {            {
2992            GETCHARINCTEST(c, eptr);            GETCHARINCTEST(c, eptr);
2993            prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);            prop_category = UCD_CATEGORY(c);
2994            if (prop_category == ucp_M) RRETURN(MATCH_NOMATCH);            if (prop_category == ucp_M) RRETURN(MATCH_NOMATCH);
2995            while (eptr < md->end_subject)            while (eptr < md->end_subject)
2996              {              {
# Line 2592  for (;;) Line 2999  for (;;)
2999                {                {
3000                GETCHARLEN(c, eptr, len);                GETCHARLEN(c, eptr, len);
3001                }                }
3002              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3003              if (prop_category != ucp_M) break;              if (prop_category != ucp_M) break;
3004              eptr += len;              eptr += len;
3005              }              }
# Line 2610  for (;;) Line 3017  for (;;)
3017          case OP_ANY:          case OP_ANY:
3018          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3019            {            {
3020            if (eptr >= md->end_subject ||            if (eptr >= md->end_subject || IS_NEWLINE(eptr))
                ((ims & PCRE_DOTALL) == 0 &&  
                  eptr <= md->end_subject - md->nllen &&  
                  IS_NEWLINE(eptr)))  
3021              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3022            eptr++;            eptr++;
3023            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
3024            }            }
3025          break;          break;
3026    
3027            case OP_ALLANY:
3028            for (i = 1; i <= min; i++)
3029              {
3030              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3031              eptr++;
3032              while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
3033              }
3034            break;
3035    
3036          case OP_ANYBYTE:          case OP_ANYBYTE:
3037          eptr += min;          eptr += min;
3038          break;          break;
3039    
3040            case OP_ANYNL:
3041            for (i = 1; i <= min; i++)
3042              {
3043              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3044              GETCHARINC(c, eptr);
3045              switch(c)
3046                {
3047                default: RRETURN(MATCH_NOMATCH);
3048                case 0x000d:
3049                if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3050                break;
3051    
3052                case 0x000a:
3053                break;
3054    
3055                case 0x000b:
3056                case 0x000c:
3057                case 0x0085:
3058                case 0x2028:
3059                case 0x2029:
3060                if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
3061                break;
3062                }
3063              }
3064            break;
3065    
3066            case OP_NOT_HSPACE:
3067            for (i = 1; i <= min; i++)
3068              {
3069              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3070              GETCHARINC(c, eptr);
3071              switch(c)
3072                {
3073                default: break;
3074                case 0x09:      /* HT */
3075                case 0x20:      /* SPACE */
3076                case 0xa0:      /* NBSP */
3077                case 0x1680:    /* OGHAM SPACE MARK */
3078                case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
3079                case 0x2000:    /* EN QUAD */
3080                case 0x2001:    /* EM QUAD */
3081                case 0x2002:    /* EN SPACE */
3082                case 0x2003:    /* EM SPACE */
3083                case 0x2004:    /* THREE-PER-EM SPACE */
3084                case 0x2005:    /* FOUR-PER-EM SPACE */
3085                case 0x2006:    /* SIX-PER-EM SPACE */
3086                case 0x2007:    /* FIGURE SPACE */
3087                case 0x2008:    /* PUNCTUATION SPACE */
3088                case 0x2009:    /* THIN SPACE */
3089                case 0x200A:    /* HAIR SPACE */
3090                case 0x202f:    /* NARROW NO-BREAK SPACE */
3091                case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
3092                case 0x3000:    /* IDEOGRAPHIC SPACE */
3093                RRETURN(MATCH_NOMATCH);
3094                }
3095              }
3096            break;
3097    
3098            case OP_HSPACE:
3099            for (i = 1; i <= min; i++)
3100              {
3101              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3102              GETCHARINC(c, eptr);
3103              switch(c)
3104                {
3105                default: RRETURN(MATCH_NOMATCH);
3106                case 0x09:      /* HT */
3107                case 0x20:      /* SPACE */
3108                case 0xa0:      /* NBSP */
3109                case 0x1680:    /* OGHAM SPACE MARK */
3110                case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
3111                case 0x2000:    /* EN QUAD */
3112                case 0x2001:    /* EM QUAD */
3113                case 0x2002:    /* EN SPACE */
3114                case 0x2003:    /* EM SPACE */
3115                case 0x2004:    /* THREE-PER-EM SPACE */
3116                case 0x2005:    /* FOUR-PER-EM SPACE */
3117                case 0x2006:    /* SIX-PER-EM SPACE */
3118                case 0x2007:    /* FIGURE SPACE */
3119                case 0x2008:    /* PUNCTUATION SPACE */
3120                case 0x2009:    /* THIN SPACE */
3121                case 0x200A:    /* HAIR SPACE */
3122                case 0x202f:    /* NARROW NO-BREAK SPACE */
3123                case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
3124                case 0x3000:    /* IDEOGRAPHIC SPACE */
3125                break;
3126                }
3127              }
3128            break;
3129    
3130            case OP_NOT_VSPACE:
3131            for (i = 1; i <= min; i++)
3132              {
3133              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3134              GETCHARINC(c, eptr);
3135              switch(c)
3136                {
3137                default: break;
3138                case 0x0a:      /* LF */
3139                case 0x0b:      /* VT */
3140                case 0x0c:      /* FF */
3141                case 0x0d:      /* CR */
3142                case 0x85:      /* NEL */
3143                case 0x2028:    /* LINE SEPARATOR */
3144                case 0x2029:    /* PARAGRAPH SEPARATOR */
3145                RRETURN(MATCH_NOMATCH);
3146                }
3147              }
3148            break;
3149    
3150            case OP_VSPACE:
3151            for (i = 1; i <= min; i++)
3152              {
3153              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3154              GETCHARINC(c, eptr);
3155              switch(c)
3156                {
3157                default: RRETURN(MATCH_NOMATCH);
3158                case 0x0a:      /* LF */
3159                case 0x0b:      /* VT */
3160                case 0x0c:      /* FF */
3161                case 0x0d:      /* CR */
3162                case 0x85:      /* NEL */
3163                case 0x2028:    /* LINE SEPARATOR */
3164                case 0x2029:    /* PARAGRAPH SEPARATOR */
3165                break;
3166                }
3167              }
3168            break;
3169    
3170          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
3171          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3172            {            {
# Line 2648  for (;;) Line 3191  for (;;)
3191          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3192            {            {
3193            if (eptr >= md->end_subject ||            if (eptr >= md->end_subject ||
3194               (*eptr < 128 && (md->ctypes[*eptr++] & ctype_space) != 0))               (*eptr < 128 && (md->ctypes[*eptr] & ctype_space) != 0))
3195              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3196            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;            while (++eptr < md->end_subject && (*eptr & 0xc0) == 0x80);
3197            }            }
3198          break;          break;
3199    
# Line 2668  for (;;) Line 3211  for (;;)
3211          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3212            {            {
3213            if (eptr >= md->end_subject ||            if (eptr >= md->end_subject ||
3214               (*eptr < 128 && (md->ctypes[*eptr++] & ctype_word) != 0))               (*eptr < 128 && (md->ctypes[*eptr] & ctype_word) != 0))
3215              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3216            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;            while (++eptr < md->end_subject && (*eptr & 0xc0) == 0x80);
3217            }            }
3218          break;          break;
3219    
# Line 2692  for (;;) Line 3235  for (;;)
3235  #endif     /* SUPPORT_UTF8 */  #endif     /* SUPPORT_UTF8 */
3236    
3237        /* Code for the non-UTF-8 case for minimum matching of operators other        /* Code for the non-UTF-8 case for minimum matching of operators other
3238        than OP_PROP and OP_NOTPROP. */        than OP_PROP and OP_NOTPROP. We can assume that there are the minimum
3239          number of bytes present, as this was tested above. */
3240    
3241        switch(ctype)        switch(ctype)
3242          {          {
3243          case OP_ANY:          case OP_ANY:
3244          if ((ims & PCRE_DOTALL) == 0)          for (i = 1; i <= min; i++)
3245            {            {
3246            for (i = 1; i <= min; i++)            if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
3247              {            eptr++;
             if (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr))  
               RRETURN(MATCH_NOMATCH);  
             eptr++;  
             }  
3248            }            }
3249          else eptr += min;          break;
3250    
3251            case OP_ALLANY:
3252            eptr += min;
3253          break;          break;
3254    
3255          case OP_ANYBYTE:          case OP_ANYBYTE:
3256          eptr += min;          eptr += min;
3257          break;          break;
3258    
3259            /* Because of the CRLF case, we can't assume the minimum number of
3260            bytes are present in this case. */
3261    
3262            case OP_ANYNL:
3263            for (i = 1; i <= min; i++)
3264              {
3265              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3266              switch(*eptr++)
3267                {
3268                default: RRETURN(MATCH_NOMATCH);
3269                case 0x000d:
3270                if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3271                break;
3272                case 0x000a:
3273                break;
3274    
3275                case 0x000b:
3276                case 0x000c:
3277                case 0x0085:
3278                if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
3279                break;
3280                }
3281              }
3282            break;
3283    
3284            case OP_NOT_HSPACE:
3285            for (i = 1; i <= min; i++)
3286              {
3287              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3288              switch(*eptr++)
3289                {
3290                default: break;
3291                case 0x09:      /* HT */
3292                case 0x20:      /* SPACE */
3293                case 0xa0:      /* NBSP */
3294                RRETURN(MATCH_NOMATCH);
3295                }
3296              }
3297            break;
3298    
3299            case OP_HSPACE:
3300            for (i = 1; i <= min; i++)
3301              {
3302              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3303              switch(*eptr++)
3304                {
3305                default: RRETURN(MATCH_NOMATCH);
3306                case 0x09:      /* HT */
3307                case 0x20:      /* SPACE */
3308                case 0xa0:      /* NBSP */
3309                break;
3310                }
3311              }
3312            break;
3313    
3314            case OP_NOT_VSPACE:
3315            for (i = 1; i <= min; i++)
3316              {
3317              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3318              switch(*eptr++)
3319                {
3320                default: break;
3321                case 0x0a:      /* LF */
3322                case 0x0b:      /* VT */
3323                case 0x0c:      /* FF */
3324                case 0x0d:      /* CR */
3325                case 0x85:      /* NEL */
3326                RRETURN(MATCH_NOMATCH);
3327                }
3328              }
3329            break;
3330    
3331            case OP_VSPACE:
3332            for (i = 1; i <= min; i++)
3333              {
3334              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3335              switch(*eptr++)
3336                {
3337                default: RRETURN(MATCH_NOMATCH);
3338                case 0x0a:      /* LF */
3339                case 0x0b:      /* VT */
3340                case 0x0c:      /* FF */
3341                case 0x0d:      /* CR */
3342                case 0x85:      /* NEL */
3343                break;
3344                }
3345              }
3346            break;
3347    
3348          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
3349          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3350            if ((md->ctypes[*eptr++] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH);            if ((md->ctypes[*eptr++] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH);
# Line 2768  for (;;) Line 3400  for (;;)
3400            case PT_ANY:            case PT_ANY:
3401            for (fi = min;; fi++)            for (fi = min;; fi++)
3402              {              {
3403              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM36);
3404              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3405              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3406              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3407              if (prop_fail_result) RRETURN(MATCH_NOMATCH);              if (prop_fail_result) RRETURN(MATCH_NOMATCH);
3408              }              }
3409            break;            /* Control never gets here */
3410    
3411            case PT_LAMP:            case PT_LAMP:
3412            for (fi = min;; fi++)            for (fi = min;; fi++)
3413              {              {
3414              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM37);
3415              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3416              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3417              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3418              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
3419              if ((prop_chartype == ucp_Lu ||              if ((prop_chartype == ucp_Lu ||
3420                   prop_chartype == ucp_Ll ||                   prop_chartype == ucp_Ll ||
3421                   prop_chartype == ucp_Lt) == prop_fail_result)                   prop_chartype == ucp_Lt) == prop_fail_result)
3422                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3423              }              }
3424            break;            /* Control never gets here */
3425    
3426            case PT_GC:            case PT_GC:
3427            for (fi = min;; fi++)            for (fi = min;; fi++)
3428              {              {
3429              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM38);
3430              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3431              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3432              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3433              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3434              if ((prop_category == prop_value) == prop_fail_result)              if ((prop_category == prop_value) == prop_fail_result)
3435                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3436              }              }
3437            break;            /* Control never gets here */
3438    
3439            case PT_PC:            case PT_PC:
3440            for (fi = min;; fi++)            for (fi = min;; fi++)
3441              {              {
3442              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM39);
3443              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3444              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3445              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3446              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
3447              if ((prop_chartype == prop_value) == prop_fail_result)              if ((prop_chartype == prop_value) == prop_fail_result)
3448                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3449              }              }
3450            break;            /* Control never gets here */
3451    
3452            case PT_SC:            case PT_SC:
3453            for (fi = min;; fi++)            for (fi = min;; fi++)
3454              {              {
3455              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM40);
3456              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3457              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3458              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3459              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_script = UCD_SCRIPT(c);
3460              if ((prop_script == prop_value) == prop_fail_result)              if ((prop_script == prop_value) == prop_fail_result)
3461                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3462              }              }
3463            break;            /* Control never gets here */
3464    
3465            default:            default:
3466            RRETURN(PCRE_ERROR_INTERNAL);            RRETURN(PCRE_ERROR_INTERNAL);
           break;  
3467            }            }
3468          }          }
3469    
# Line 2843  for (;;) Line 3474  for (;;)
3474          {          {
3475          for (fi = min;; fi++)          for (fi = min;; fi++)
3476            {            {
3477            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM41);
3478            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3479            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3480            GETCHARINCTEST(c, eptr);            GETCHARINCTEST(c, eptr);
3481            prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);            prop_category = UCD_CATEGORY(c);
3482            if (prop_category == ucp_M) RRETURN(MATCH_NOMATCH);            if (prop_category == ucp_M) RRETURN(MATCH_NOMATCH);
3483            while (eptr < md->end_subject)            while (eptr < md->end_subject)
3484              {              {
# Line 2856  for (;;) Line 3487  for (;;)
3487                {                {
3488                GETCHARLEN(c, eptr, len);                GETCHARLEN(c, eptr, len);
3489                }                }
3490              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3491              if (prop_category != ucp_M) break;              if (prop_category != ucp_M) break;
3492              eptr += len;              eptr += len;
3493              }              }
# Line 2872  for (;;) Line 3503  for (;;)
3503          {          {
3504          for (fi = min;; fi++)          for (fi = min;; fi++)
3505            {            {
3506            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM42);
3507            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3508            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max || eptr >= md->end_subject ||
3509                 (ctype == OP_ANY && (ims & PCRE_DOTALL) == 0 &&                 (ctype == OP_ANY && IS_NEWLINE(eptr)))
                 eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)))  
3510              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3511    
3512            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
3513            switch(ctype)            switch(ctype)
3514              {              {
3515              case OP_ANY:        /* This is the DOTALL case */              case OP_ANY:        /* This is the non-NL case */
3516                case OP_ALLANY:
3517                case OP_ANYBYTE:
3518              break;              break;
3519    
3520              case OP_ANYBYTE:              case OP_ANYNL:
3521                switch(c)
3522                  {
3523                  default: RRETURN(MATCH_NOMATCH);
3524                  case 0x000d:
3525                  if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3526                  break;
3527                  case 0x000a:
3528                  break;
3529    
3530                  case 0x000b:
3531                  case 0x000c:
3532                  case 0x0085:
3533                  case 0x2028:
3534                  case 0x2029:
3535                  if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
3536                  break;
3537                  }
3538                break;
3539    
3540                case OP_NOT_HSPACE:
3541                switch(c)
3542                  {
3543                  default: break;
3544                  case 0x09:      /* HT */
3545                  case 0x20:      /* SPACE */
3546                  case 0xa0:      /* NBSP */
3547                  case 0x1680:    /* OGHAM SPACE MARK */
3548                  case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
3549                  case 0x2000:    /* EN QUAD */
3550                  case 0x2001:    /* EM QUAD */
3551                  case 0x2002:    /* EN SPACE */
3552                  case 0x2003:    /* EM SPACE */
3553                  case 0x2004:    /* THREE-PER-EM SPACE */
3554                  case 0x2005:    /* FOUR-PER-EM SPACE */
3555                  case 0x2006:    /* SIX-PER-EM SPACE */
3556                  case 0x2007:    /* FIGURE SPACE */
3557                  case 0x2008:    /* PUNCTUATION SPACE */
3558                  case 0x2009:    /* THIN SPACE */
3559                  case 0x200A:    /* HAIR SPACE */
3560                  case 0x202f:    /* NARROW NO-BREAK SPACE */
3561                  case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
3562                  case 0x3000:    /* IDEOGRAPHIC SPACE */
3563                  RRETURN(MATCH_NOMATCH);
3564                  }
3565                break;
3566    
3567                case OP_HSPACE:
3568                switch(c)
3569                  {
3570                  default: RRETURN(MATCH_NOMATCH);
3571                  case 0x09:      /* HT */
3572                  case 0x20:      /* SPACE */
3573                  case 0xa0:      /* NBSP */
3574                  case 0x1680:    /* OGHAM SPACE MARK */
3575                  case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
3576                  case 0x2000:    /* EN QUAD */
3577                  case 0x2001:    /* EM QUAD */
3578                  case 0x2002:    /* EN SPACE */
3579                  case 0x2003:    /* EM SPACE */
3580                  case 0x2004:    /* THREE-PER-EM SPACE */
3581                  case 0x2005:    /* FOUR-PER-EM SPACE */
3582                  case 0x2006:    /* SIX-PER-EM SPACE */
3583                  case 0x2007:    /* FIGURE SPACE */
3584                  case 0x2008:    /* PUNCTUATION SPACE */
3585                  case 0x2009:    /* THIN SPACE */
3586                  case 0x200A:    /* HAIR SPACE */
3587                  case 0x202f:    /* NARROW NO-BREAK SPACE */
3588                  case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
3589                  case 0x3000:    /* IDEOGRAPHIC SPACE */
3590                  break;
3591                  }
3592                break;
3593    
3594                case OP_NOT_VSPACE:
3595                switch(c)
3596                  {
3597                  default: break;
3598                  case 0x0a:      /* LF */
3599                  case 0x0b:      /* VT */
3600                  case 0x0c:      /* FF */
3601                  case 0x0d:      /* CR */
3602                  case 0x85:      /* NEL */
3603                  case 0x2028:    /* LINE SEPARATOR */
3604                  case 0x2029:    /* PARAGRAPH SEPARATOR */
3605                  RRETURN(MATCH_NOMATCH);
3606                  }
3607                break;
3608    
3609                case OP_VSPACE:
3610                switch(c)
3611                  {
3612                  default: RRETURN(MATCH_NOMATCH);
3613                  case 0x0a:      /* LF */
3614                  case 0x0b:      /* VT */
3615                  case 0x0c:      /* FF */
3616                  case 0x0d:      /* CR */
3617                  case 0x85:      /* NEL */
3618                  case 0x2028:    /* LINE SEPARATOR */
3619                  case 0x2029:    /* PARAGRAPH SEPARATOR */
3620                  break;
3621                  }
3622              break;              break;
3623    
3624              case OP_NOT_DIGIT:              case OP_NOT_DIGIT:
# Line 2929  for (;;) Line 3662  for (;;)
3662          {          {
3663          for (fi = min;; fi++)          for (fi = min;; fi++)
3664            {            {
3665            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM43);
3666            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3667            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max || eptr >= md->end_subject ||
3668                 ((ims & PCRE_DOTALL) == 0 &&                 (ctype == OP_ANY && IS_NEWLINE(eptr)))
                  eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)))  
3669              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3670    
3671            c = *eptr++;            c = *eptr++;
3672            switch(ctype)            switch(ctype)
3673              {              {
3674              case OP_ANY:   /* This is the DOTALL case */              case OP_ANY:     /* This is the non-NL case */
3675                case OP_ALLANY:
3676                case OP_ANYBYTE:
3677              break;              break;
3678    
3679              case OP_ANYBYTE:              case OP_ANYNL:
3680                switch(c)
3681                  {
3682                  default: RRETURN(MATCH_NOMATCH);
3683                  case 0x000d:
3684                  if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3685                  break;
3686    
3687                  case 0x000a:
3688                  break;
3689    
3690                  case 0x000b:
3691                  case 0x000c:
3692                  case 0x0085:
3693                  if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
3694                  break;
3695                  }
3696                break;
3697    
3698                case OP_NOT_HSPACE:
3699                switch(c)
3700                  {
3701                  default: break;
3702                  case 0x09:      /* HT */
3703                  case 0x20:      /* SPACE */
3704                  case 0xa0:      /* NBSP */
3705                  RRETURN(MATCH_NOMATCH);
3706                  }
3707                break;
3708    
3709                case OP_HSPACE:
3710                switch(c)
3711                  {
3712                  default: RRETURN(MATCH_NOMATCH);
3713                  case 0x09:      /* HT */
3714                  case 0x20:      /* SPACE */
3715                  case 0xa0:      /* NBSP */
3716                  break;
3717                  }
3718                break;
3719    
3720                case OP_NOT_VSPACE:
3721                switch(c)
3722                  {
3723                  default: break;
3724                  case 0x0a:      /* LF */
3725                  case 0x0b:      /* VT */
3726                  case 0x0c:      /* FF */
3727                  case 0x0d:      /* CR */
3728                  case 0x85:      /* NEL */
3729                  RRETURN(MATCH_NOMATCH);
3730                  }
3731                break;
3732    
3733                case OP_VSPACE:
3734                switch(c)
3735                  {
3736                  default: RRETURN(MATCH_NOMATCH);
3737                  case 0x0a:      /* LF */
3738                  case 0x0b:      /* VT */
3739                  case 0x0c:      /* FF */
3740                  case 0x0d:      /* CR */
3741                  case 0x85:      /* NEL */
3742                  break;
3743                  }
3744              break;              break;
3745    
3746              case OP_NOT_DIGIT:              case OP_NOT_DIGIT:
# Line 2977  for (;;) Line 3775  for (;;)
3775        /* Control never gets here */        /* Control never gets here */
3776        }        }
3777    
3778      /* If maximizing it is worth using inline code for speed, doing the type      /* If maximizing, it is worth using inline code for speed, doing the type
3779      test once at the start (i.e. keep it out of the loop). Again, keep the      test once at the start (i.e. keep it out of the loop). Again, keep the
3780      UTF-8 and UCP stuff separate. */      UTF-8 and UCP stuff separate. */
3781    
# Line 3007  for (;;) Line 3805  for (;;)
3805              int len = 1;              int len = 1;
3806              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject) break;
3807              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
3808              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
3809              if ((prop_chartype == ucp_Lu ||              if ((prop_chartype == ucp_Lu ||
3810                   prop_chartype == ucp_Ll ||                   prop_chartype == ucp_Ll ||
3811                   prop_chartype == ucp_Lt) == prop_fail_result)                   prop_chartype == ucp_Lt) == prop_fail_result)
# Line 3022  for (;;) Line 3820  for (;;)
3820              int len = 1;              int len = 1;
3821              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject) break;
3822              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
3823              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3824              if ((prop_category == prop_value) == prop_fail_result)              if ((prop_category == prop_value) == prop_fail_result)
3825                break;                break;
3826              eptr+= len;              eptr+= len;
# Line 3035  for (;;) Line 3833  for (;;)
3833              int len = 1;              int len = 1;
3834              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject) break;
3835              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
3836              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
3837              if ((prop_chartype == prop_value) == prop_fail_result)              if ((prop_chartype == prop_value) == prop_fail_result)
3838                break;                break;
3839              eptr+= len;              eptr+= len;
# Line 3048  for (;;) Line 3846  for (;;)
3846              int len = 1;              int len = 1;
3847              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject) break;
3848              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
3849              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_script = UCD_SCRIPT(c);
3850              if ((prop_script == prop_value) == prop_fail_result)              if ((prop_script == prop_value) == prop_fail_result)
3851                break;                break;
3852              eptr+= len;              eptr+= len;
# Line 3058  for (;;) Line 3856  for (;;)
3856    
3857          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run */
3858    
3859            if (possessive) continue;
3860          for(;;)          for(;;)
3861            {            {
3862            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM44);
3863            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3864            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
3865            BACKCHAR(eptr);            if (utf8) BACKCHAR(eptr);
3866            }            }
3867          }          }
3868    
# Line 3076  for (;;) Line 3875  for (;;)
3875            {            {
3876            if (eptr >= md->end_subject) break;            if (eptr >= md->end_subject) break;
3877            GETCHARINCTEST(c, eptr);            GETCHARINCTEST(c, eptr);
3878            prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);            prop_category = UCD_CATEGORY(c);
3879            if (prop_category == ucp_M) break;            if (prop_category == ucp_M) break;
3880            while (eptr < md->end_subject)            while (eptr < md->end_subject)
3881              {              {
# Line 3085  for (;;) Line 3884  for (;;)
3884                {                {
3885                GETCHARLEN(c, eptr, len);                GETCHARLEN(c, eptr, len);
3886                }                }
3887              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3888              if (prop_category != ucp_M) break;              if (prop_category != ucp_M) break;
3889              eptr += len;              eptr += len;
3890              }              }
# Line 3093  for (;;) Line 3892  for (;;)
3892    
3893          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run */
3894    
3895            if (possessive) continue;
3896          for(;;)          for(;;)
3897            {            {
3898            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM45);
3899            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3900            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
3901            for (;;)                        /* Move back over one extended */            for (;;)                        /* Move back over one extended */
3902              {              {
3903              int len = 1;              int len = 1;
             BACKCHAR(eptr);  
3904              if (!utf8) c = *eptr; else              if (!utf8) c = *eptr; else
3905                {                {
3906                  BACKCHAR(eptr);
3907                GETCHARLEN(c, eptr, len);                GETCHARLEN(c, eptr, len);
3908                }                }
3909              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3910              if (prop_category != ucp_M) break;              if (prop_category != ucp_M) break;
3911              eptr--;              eptr--;
3912              }              }
# Line 3124  for (;;) Line 3924  for (;;)
3924          switch(ctype)          switch(ctype)
3925            {            {
3926            case OP_ANY:            case OP_ANY:
   
           /* Special code is required for UTF8, but when the maximum is  
           unlimited we don't need it, so we repeat the non-UTF8 code. This is  
           probably worth it, because .* is quite a common idiom. */  
   
3927            if (max < INT_MAX)            if (max < INT_MAX)
3928              {              {
3929              if ((ims & PCRE_DOTALL) == 0)              for (i = min; i < max; i++)
               {  
               for (i = min; i < max; i++)  
                 {  
                 if (eptr >= md->end_subject ||  
                     (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)))  
                   break;  
                 eptr++;  
                 while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;  
                 }  
               }  
             else  
3930                {                {
3931                for (i = min; i < max; i++)                if (eptr >= md->end_subject || IS_NEWLINE(eptr)) break;
3932                  {                eptr++;
3933                  if (eptr >= md->end_subject) break;                while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
                 eptr++;  
                 while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;  
                 }  
3934                }                }
3935              }              }
3936    
# Line 3157  for (;;) Line 3938  for (;;)
3938    
3939            else            else
3940              {              {
3941              if ((ims & PCRE_DOTALL) == 0)              for (i = min; i < max; i++)
3942                {                {
3943                for (i = min; i < max; i++)                if (eptr >= md->end_subject || IS_NEWLINE(eptr)) break;
3944                  {                eptr++;
3945                  if (eptr >= md->end_subject ||                while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
                     (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)))  
                   break;  
                 eptr++;  
                 }  
               break;  
3946                }                }
3947              else              }
3948              break;
3949    
3950              case OP_ALLANY:
3951              if (max < INT_MAX)
3952                {
3953                for (i = min; i < max; i++)
3954                {                {
3955                c = max - min;                if (eptr >= md->end_subject) break;
3956                if (c > md->end_subject - eptr) c = md->end_subject - eptr;                eptr++;
3957                eptr += c;                while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
3958                }                }
3959              }              }
3960              else eptr = md->end_subject;   /* Unlimited UTF-8 repeat */
3961            break;            break;
3962    
3963            /* The byte case is the same as non-UTF8 */            /* The byte case is the same as non-UTF8 */
3964    
3965            case OP_ANYBYTE:            case OP_ANYBYTE:
3966            c = max - min;            c = max - min;
3967            if (c > md->end_subject - eptr) c = md->end_subject - eptr;            if (c > (unsigned int)(md->end_subject - eptr))
3968                c = md->end_subject - eptr;
3969            eptr += c;            eptr += c;
3970            break;            break;
3971    
3972              case OP_ANYNL:
3973              for (i = min; i < max; i++)
3974                {
3975                int len = 1;
3976                if (eptr >= md->end_subject) break;
3977                GETCHARLEN(c, eptr, len);
3978                if (c == 0x000d)
3979                  {
3980                  if (++eptr >= md->end_subject) break;
3981                  if (*eptr == 0x000a) eptr++;
3982                  }
3983                else
3984                  {
3985                  if (c != 0x000a &&
3986                      (md->bsr_anycrlf ||
3987                       (c != 0x000b && c != 0x000c &&
3988                        c != 0x0085 && c != 0x2028 && c != 0x2029)))
3989                    break;
3990                  eptr += len;
3991                  }
3992                }
3993              break;
3994    
3995              case OP_NOT_HSPACE:
3996              case OP_HSPACE:
3997              for (i = min; i < max; i++)
3998                {
3999                BOOL gotspace;
4000                int len = 1;
4001                if (eptr >= md->end_subject) break;
4002                GETCHARLEN(c, eptr, len);
4003                switch(c)
4004                  {
4005                  default: gotspace = FALSE; break;
4006                  case 0x09:      /* HT */
4007                  case 0x20:      /* SPACE */
4008                  case 0xa0:      /* NBSP */
4009                  case 0x1680:    /* OGHAM SPACE MARK */
4010                  case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
4011                  case 0x2000:    /* EN QUAD */
4012                  case 0x2001:    /* EM QUAD */
4013                  case 0x2002:    /* EN SPACE */
4014                  case 0x2003:    /* EM SPACE */
4015                  case 0x2004:    /* THREE-PER-EM SPACE */
4016                  case 0x2005:    /* FOUR-PER-EM SPACE */
4017                  case 0x2006:    /* SIX-PER-EM SPACE */
4018                  case 0x2007:    /* FIGURE SPACE */
4019                  case 0x2008:    /* PUNCTUATION SPACE */
4020                  case 0x2009:    /* THIN SPACE */
4021                  case 0x200A:    /* HAIR SPACE */
4022                  case 0x202f:    /* NARROW NO-BREAK SPACE */
4023                  case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
4024                  case 0x3000:    /* IDEOGRAPHIC SPACE */
4025                  gotspace = TRUE;
4026                  break;
4027                  }
4028                if (gotspace == (ctype == OP_NOT_HSPACE)) break;
4029                eptr += len;
4030                }
4031              break;
4032    
4033              case OP_NOT_VSPACE:
4034              case OP_VSPACE:
4035              for (i = min; i < max; i++)
4036                {
4037                BOOL gotspace;
4038                int len = 1;
4039                if (eptr >= md->end_subject) break;
4040                GETCHARLEN(c, eptr, len);
4041                switch(c)
4042                  {
4043                  default: gotspace = FALSE; break;
4044                  case 0x0a:      /* LF */
4045                  case 0x0b:      /* VT */
4046                  case 0x0c:      /* FF */
4047                  case 0x0d:      /* CR */
4048                  case 0x85:      /* NEL */
4049                  case 0x2028:    /* LINE SEPARATOR */
4050                  case 0x2029:    /* PARAGRAPH SEPARATOR */
4051                  gotspace = TRUE;
4052                  break;
4053                  }
4054                if (gotspace == (ctype == OP_NOT_VSPACE)) break;
4055                eptr += len;
4056                }
4057              break;
4058    
4059            case OP_NOT_DIGIT:            case OP_NOT_DIGIT:
4060            for (i = min; i < max; i++)            for (i = min; i < max; i++)
4061              {              {
# Line 3257  for (;;) Line 4128  for (;;)
4128    
4129          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run */
4130    
4131            if (possessive) continue;
4132          for(;;)          for(;;)
4133            {            {
4134            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM46);
4135            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4136            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
4137            BACKCHAR(eptr);            BACKCHAR(eptr);
4138            }            }
4139          }          }
4140        else        else
4141  #endif  #endif  /* SUPPORT_UTF8 */
4142    
4143        /* Not UTF-8 mode */        /* Not UTF-8 mode */
4144          {          {
4145          switch(ctype)          switch(ctype)
4146            {            {
4147            case OP_ANY:            case OP_ANY:
4148            if ((ims & PCRE_DOTALL) == 0)            for (i = min; i < max; i++)
4149              {              {
4150              for (i = min; i < max; i++)              if (eptr >= md->end_subject || IS_NEWLINE(eptr)) break;
4151                eptr++;
4152                }
4153              break;
4154    
4155              case OP_ALLANY:
4156              case OP_ANYBYTE:
4157              c = max - min;
4158              if (c > (unsigned int)(md->end_subject - eptr))
4159                c = md->end_subject - eptr;
4160              eptr += c;
4161              break;
4162    
4163              case OP_ANYNL:
4164              for (i = min; i < max; i++)
4165                {
4166                if (eptr >= md->end_subject) break;
4167                c = *eptr;
4168                if (c == 0x000d)
4169                  {
4170                  if (++eptr >= md->end_subject) break;
4171                  if (*eptr == 0x000a) eptr++;
4172                  }
4173                else
4174                {                {
4175                if (eptr >= md->end_subject ||                if (c != 0x000a &&
4176                    (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)))                    (md->bsr_anycrlf ||
4177                        (c != 0x000b && c != 0x000c && c != 0x0085)))
4178                  break;                  break;
4179                eptr++;                eptr++;
4180                }                }
             break;  
4181              }              }
4182            /* For DOTALL case, fall through and treat as \C */            break;
4183    
4184            case OP_ANYBYTE:            case OP_NOT_HSPACE:
4185            c = max - min;            for (i = min; i < max; i++)
4186            if (c > md->end_subject - eptr) c = md->end_subject - eptr;              {
4187            eptr += c;              if (eptr >= md->end_subject) break;
4188                c = *eptr;
4189                if (c == 0x09 || c == 0x20 || c == 0xa0) break;
4190                eptr++;
4191                }
4192              break;
4193    
4194              case OP_HSPACE:
4195              for (i = min; i < max; i++)
4196                {
4197                if (eptr >= md->end_subject) break;
4198                c = *eptr;
4199                if (c != 0x09 && c != 0x20 && c != 0xa0) break;
4200                eptr++;
4201                }
4202              break;
4203    
4204              case OP_NOT_VSPACE:
4205              for (i = min; i < max; i++)
4206                {
4207                if (eptr >= md->end_subject) break;
4208                c = *eptr;
4209                if (c == 0x0a || c == 0x0b || c == 0x0c || c == 0x0d || c == 0x85)
4210                  break;
4211                eptr++;
4212                }
4213              break;
4214    
4215              case OP_VSPACE:
4216              for (i = min; i < max; i++)
4217                {
4218                if (eptr >= md->end_subject) break;
4219                c = *eptr;
4220                if (c != 0x0a && c != 0x0b && c != 0x0c && c != 0x0d && c != 0x85)
4221                  break;
4222                eptr++;
4223                }
4224            break;            break;
4225    
4226            case OP_NOT_DIGIT:            case OP_NOT_DIGIT:
# Line 3352  for (;;) Line 4283  for (;;)
4283    
4284          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run */
4285    
4286            if (possessive) continue;
4287          while (eptr >= pp)          while (eptr >= pp)
4288            {            {
4289            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM47);
4290            eptr--;            eptr--;
4291            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4292            }            }
# Line 3366  for (;;) Line 4298  for (;;)
4298        }        }
4299      /* Control never gets here */      /* Control never gets here */
4300    
4301      /* There's been some horrible disaster. Since all codes > OP_BRA are      /* There's been some horrible disaster. Arrival here can only mean there is
4302      for capturing brackets, and there shouldn't be any gaps between 0 and      something seriously wrong in the code above or the OP_xxx definitions. */
     OP_BRA, arrival here can only mean there is something seriously wrong  
     in the code above or the OP_xxx definitions. */  
4303    
4304      default:      default:
4305      DPRINTF(("Unknown opcode %d\n", *ecode));      DPRINTF(("Unknown opcode %d\n", *ecode));
4306      RRETURN(PCRE_ERROR_UNKNOWN_NODE);      RRETURN(PCRE_ERROR_UNKNOWN_OPCODE);
4307      }      }
4308    
4309    /* Do not stick any code in here without much thought; it is assumed    /* Do not stick any code in here without much thought; it is assumed
# Line 3382  for (;;) Line 4312  for (;;)
4312    
4313    }             /* End of main loop */    }             /* End of main loop */
4314  /* Control never reaches here */  /* Control never reaches here */
4315    
4316    
4317    /* When compiling to use the heap rather than the stack for recursive calls to
4318    match(), the RRETURN() macro jumps here. The number that is saved in
4319    frame->Xwhere indicates which label we actually want to return to. */
4320    
4321    #ifdef NO_RECURSE
4322    #define LBL(val) case val: goto L_RM##val;
4323    HEAP_RETURN:
4324    switch (frame->Xwhere)
4325      {
4326      LBL( 1) LBL( 2) LBL( 3) LBL( 4) LBL( 5) LBL( 6) LBL( 7) LBL( 8)
4327      LBL( 9) LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(17)
4328      LBL(19) LBL(24) LBL(25) LBL(26) LBL(27) LBL(29) LBL(31) LBL(33)
4329      LBL(35) LBL(43) LBL(47) LBL(48) LBL(49) LBL(50) LBL(51) LBL(52)
4330      LBL(53) LBL(54)
4331    #ifdef SUPPORT_UTF8
4332      LBL(16) LBL(18) LBL(20) LBL(21) LBL(22) LBL(23) LBL(28) LBL(30)
4333      LBL(32) LBL(34) LBL(42) LBL(46)
4334    #ifdef SUPPORT_UCP
4335      LBL(36) LBL(37) LBL(38) LBL(39) LBL(40) LBL(41) LBL(44) LBL(45)
4336    #endif  /* SUPPORT_UCP */
4337    #endif  /* SUPPORT_UTF8 */
4338      default:
4339      DPRINTF(("jump error in pcre match: label %d non-existent\n", frame->Xwhere));
4340      return PCRE_ERROR_INTERNAL;
4341      }
4342    #undef LBL
4343    #endif  /* NO_RECURSE */
4344  }  }
4345    
4346    
# Line 3394  Undefine all the macros that were define Line 4353  Undefine all the macros that were define
4353  #ifdef NO_RECURSE  #ifdef NO_RECURSE
4354  #undef eptr  #undef eptr
4355  #undef ecode  #undef ecode
4356    #undef mstart
4357  #undef offset_top  #undef offset_top
4358  #undef ims  #undef ims
4359  #undef eptrb  #undef eptrb
# Line 3411  Undefine all the macros that were define Line 4371  Undefine all the macros that were define
4371    
4372  #undef cur_is_word  #undef cur_is_word
4373  #undef condition  #undef condition
 #undef minimize  
4374  #undef prev_is_word  #undef prev_is_word
4375    
4376  #undef original_ims  #undef original_ims
# Line 3467  Returns:          > 0 => success; value Line 4426  Returns:          > 0 => success; value
4426                   < -1 => some kind of unexpected problem                   < -1 => some kind of unexpected problem
4427  */  */
4428    
4429  PCRE_DATA_SCOPE int  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
4430  pcre_exec(const pcre *argument_re, const pcre_extra *extra_data,  pcre_exec(const pcre *argument_re, const pcre_extra *extra_data,
4431    PCRE_SPTR subject, int length, int start_offset, int options, int *offsets,    PCRE_SPTR subject, int length, int start_offset, int options, int *offsets,
4432    int offsetcount)    int offsetcount)
# Line 3484  BOOL startline; Line 4443  BOOL startline;
4443  BOOL firstline;  BOOL firstline;
4444  BOOL first_byte_caseless = FALSE;  BOOL first_byte_caseless = FALSE;
4445  BOOL req_byte_caseless = FALSE;  BOOL req_byte_caseless = FALSE;
4446    BOOL utf8;
4447  match_data match_block;  match_data match_block;
4448  match_data *md = &match_block;  match_data *md = &match_block;
4449  const uschar *tables;  const uschar *tables;
# Line 3553  if (re->magic_number != MAGIC_NUMBER) Line 4513  if (re->magic_number != MAGIC_NUMBER)
4513  /* Set up other data */  /* Set up other data */
4514    
4515  anchored = ((re->options | options) & PCRE_ANCHORED) != 0;  anchored = ((re->options | options) & PCRE_ANCHORED) != 0;
4516  startline = (re->options & PCRE_STARTLINE) != 0;  startline = (re->flags & PCRE_STARTLINE) != 0;
4517  firstline = (re->options & PCRE_FIRSTLINE) != 0;  firstline = (re->options & PCRE_FIRSTLINE) != 0;
4518    
4519  /* The code starts after the real_pcre block and the capture name table. */  /* The code starts after the real_pcre block and the capture name table. */
# Line 3567  md->end_subject = md->start_subject + le Line 4527  md->end_subject = md->start_subject + le
4527  end_subject = md->end_subject;  end_subject = md->end_subject;
4528    
4529  md->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;  md->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;
4530  md->utf8 = (re->options & PCRE_UTF8) != 0;  utf8 = md->utf8 = (re->options & PCRE_UTF8) != 0;
4531    md->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;
4532    
4533  md->notbol = (options & PCRE_NOTBOL) != 0;  md->notbol = (options & PCRE_NOTBOL) != 0;
4534  md->noteol = (options & PCRE_NOTEOL) != 0;  md->noteol = (options & PCRE_NOTEOL) != 0;
# Line 3580  md->recursive = NULL; Line 4541  md->recursive = NULL;
4541  md->lcc = tables + lcc_offset;  md->lcc = tables + lcc_offset;
4542  md->ctypes = tables + ctypes_offset;  md->ctypes = tables + ctypes_offset;
4543    
4544  /* Handle different types of newline. The two bits give four cases. If nothing  /* Handle different \R options. */
 is set at run time, whatever was used at compile time applies. */  
4545    
4546  switch ((((options & PCRE_NEWLINE_CRLF) == 0)? re->options : options) &  switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
          PCRE_NEWLINE_CRLF)  
4547    {    {
4548    default:              newline = NEWLINE; break;   /* Compile-time default */    case 0:
4549    case PCRE_NEWLINE_CR: newline = '\r'; break;    if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0)
4550    case PCRE_NEWLINE_LF: newline = '\n'; break;      md->bsr_anycrlf = (re->options & PCRE_BSR_ANYCRLF) != 0;
4551      else
4552    #ifdef BSR_ANYCRLF
4553      md->bsr_anycrlf = TRUE;
4554    #else
4555      md->bsr_anycrlf = FALSE;
4556    #endif
4557      break;
4558    
4559      case PCRE_BSR_ANYCRLF:
4560      md->bsr_anycrlf = TRUE;
4561      break;
4562    
4563      case PCRE_BSR_UNICODE:
4564      md->bsr_anycrlf = FALSE;
4565      break;
4566    
4567      default: return PCRE_ERROR_BADNEWLINE;
4568      }
4569    
4570    /* Handle different types of newline. The three bits give eight cases. If
4571    nothing is set at run time, whatever was used at compile time applies. */
4572    
4573    switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options :
4574            (pcre_uint32)options) & PCRE_NEWLINE_BITS)
4575      {
4576      case 0: newline = NEWLINE; break;   /* Compile-time default */
4577      case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
4578      case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
4579    case PCRE_NEWLINE_CR+    case PCRE_NEWLINE_CR+
4580         PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break;         PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
4581      case PCRE_NEWLINE_ANY: newline = -1; break;
4582      case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
4583      default: return PCRE_ERROR_BADNEWLINE;
4584    }    }
4585    
4586  if (newline > 255)  if (newline == -2)
4587    {    {
4588    md->nllen = 2;    md->nltype = NLTYPE_ANYCRLF;
4589    md->nl[0] = (newline >> 8) & 255;    }
4590    md->nl[1] = newline & 255;  else if (newline < 0)
4591      {
4592      md->nltype = NLTYPE_ANY;
4593    }    }
4594  else  else
4595    {    {
4596    md->nllen = 1;    md->nltype = NLTYPE_FIXED;
4597    md->nl[0] = newline;    if (newline > 255)
4598        {
4599        md->nllen = 2;
4600        md->nl[0] = (newline >> 8) & 255;
4601        md->nl[1] = newline & 255;
4602        }
4603      else
4604        {
4605        md->nllen = 1;
4606        md->nl[0] = newline;
4607        }
4608    }    }
4609    
4610  /* Partial matching is supported only for a restricted set of regexes at the  /* Partial matching is supported only for a restricted set of regexes at the
4611  moment. */  moment. */
4612    
4613  if (md->partial && (re->options & PCRE_NOPARTIAL) != 0)  if (md->partial && (re->flags & PCRE_NOPARTIAL) != 0)
4614    return PCRE_ERROR_BADPARTIAL;    return PCRE_ERROR_BADPARTIAL;
4615    
4616  /* Check a UTF-8 string if required. Unfortunately there's no way of passing  /* Check a UTF-8 string if required. Unfortunately there's no way of passing
4617  back the character offset. */  back the character offset. */
4618    
4619  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4620  if (md->utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)
4621    {    {
4622    if (_pcre_valid_utf8((uschar *)subject, length) >= 0)    if (_pcre_valid_utf8((USPTR)subject, length) >= 0)
4623      return PCRE_ERROR_BADUTF8;      return PCRE_ERROR_BADUTF8;
4624    if (start_offset > 0 && start_offset < length)    if (start_offset > 0 && start_offset < length)
4625      {      {
4626      int tb = ((uschar *)subject)[start_offset];      int tb = ((USPTR)subject)[start_offset];
4627      if (tb > 127)      if (tb > 127)
4628        {        {
4629        tb &= 0xc0;        tb &= 0xc0;
# Line 3685  studied, there may be a bitmap of possib Line 4687  studied, there may be a bitmap of possib
4687    
4688  if (!anchored)  if (!anchored)
4689    {    {
4690    if ((re->options & PCRE_FIRSTSET) != 0)    if ((re->flags & PCRE_FIRSTSET) != 0)
4691      {      {
4692      first_byte = re->first_byte & 255;      first_byte = re->first_byte & 255;
4693      if ((first_byte_caseless = ((re->first_byte & REQ_CASELESS) != 0)) == TRUE)      if ((first_byte_caseless = ((re->first_byte & REQ_CASELESS) != 0)) == TRUE)
# Line 3700  if (!anchored) Line 4702  if (!anchored)
4702  /* For anchored or unanchored matches, there may be a "last known required  /* For anchored or unanchored matches, there may be a "last known required
4703  character" set. */  character" set. */
4704    
4705  if ((re->options & PCRE_REQCHSET) != 0)  if ((re->flags & PCRE_REQCHSET) != 0)
4706    {    {
4707    req_byte = re->req_byte & 255;    req_byte = re->req_byte & 255;
4708    req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0;    req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0;
4709    req_byte2 = (tables + fcc_offset)[req_byte];  /* case flipped */    req_byte2 = (tables + fcc_offset)[req_byte];  /* case flipped */
4710    }    }
4711    
4712    
4713    /* ==========================================================================*/
4714    
4715  /* Loop for handling unanchored repeated matching attempts; for anchored regexs  /* Loop for handling unanchored repeated matching attempts; for anchored regexs
4716  the loop runs just once. */  the loop runs just once. */
4717    
4718  do  for(;;)
4719    {    {
4720    USPTR save_end_subject = end_subject;    USPTR save_end_subject = end_subject;
4721      USPTR new_start_match;
4722    
4723    /* Reset the maximum number of extractions we might see. */    /* Reset the maximum number of extractions we might see. */
4724    
# Line 3723  do Line 4729  do
4729      while (iptr < iend) *iptr++ = -1;      while (iptr < iend) *iptr++ = -1;
4730      }      }
4731    
4732    /* Advance to a unique first char if possible. If firstline is TRUE, the    /* If firstline is TRUE, the start of the match is constrained to the first
4733    start of the match is constrained to the first line of a multiline string.    line of a multiline string. That is, the match must be before or at the first
4734    Implement this by temporarily adjusting end_subject so that we stop scanning    newline. Implement this by temporarily adjusting end_subject so that we stop
4735    at a newline. If the match fails at the newline, later code breaks this loop.    scanning at a newline. If the match fails at the newline, later code breaks
4736    */    this loop. */
4737    
4738    if (firstline)    if (firstline)
4739      {      {
4740      USPTR t = start_match;      USPTR t = start_match;
4741      while (t <= save_end_subject - md->nllen && !IS_NEWLINE(t)) t++;  #ifdef SUPPORT_UTF8
4742        if (utf8)
4743          {
4744          while (t < md->end_subject && !IS_NEWLINE(t))
4745            {
4746            t++;
4747            while (t < end_subject && (*t & 0xc0) == 0x80) t++;
4748            }
4749          }
4750        else
4751    #endif
4752        while (t < md->end_subject && !IS_NEWLINE(t)) t++;
4753      end_subject = t;      end_subject = t;
4754      }      }
4755    
4756    /* Now test for a unique first byte */    /* There are some optimizations that avoid running the match if a known
4757      starting point is not found, or if a known later character is not present.
4758      However, there is an option that disables these, for testing and for ensuring
4759      that all callouts do actually occur. */
4760    
4761    if (first_byte >= 0)    if ((options & PCRE_NO_START_OPTIMIZE) == 0)
4762      {      {
4763      if (first_byte_caseless)      /* Advance to a unique first byte if there is one. */
       while (start_match < end_subject &&  
              md->lcc[*start_match] != first_byte)  
         start_match++;  
     else  
       while (start_match < end_subject && *start_match != first_byte)  
         start_match++;  
     }  
4764    
4765    /* Or to just after a linebreak for a multiline match if possible */      if (first_byte >= 0)
4766          {
4767          if (first_byte_caseless)
4768            while (start_match < end_subject && md->lcc[*start_match] != first_byte)
4769              start_match++;
4770          else
4771            while (start_match < end_subject && *start_match != first_byte)
4772              start_match++;
4773          }
4774    
4775    else if (startline)      /* Or to just after a linebreak for a multiline match */
4776      {  
4777      if (start_match >= md->start_subject + md->nllen +      else if (startline)
           start_offset)  
4778        {        {
4779        while (start_match <= end_subject &&        if (start_match > md->start_subject + start_offset)
4780               !IS_NEWLINE(start_match - md->nllen))          {
4781          start_match++;  #ifdef SUPPORT_UTF8
4782            if (utf8)
4783              {
4784              while (start_match < end_subject && !WAS_NEWLINE(start_match))
4785                {
4786                start_match++;
4787                while(start_match < end_subject && (*start_match & 0xc0) == 0x80)
4788                  start_match++;
4789                }
4790              }
4791            else
4792    #endif
4793            while (start_match < end_subject && !WAS_NEWLINE(start_match))
4794              start_match++;
4795    
4796            /* If we have just passed a CR and the newline option is ANY or ANYCRLF,
4797            and we are now at a LF, advance the match position by one more character.
4798            */
4799    
4800            if (start_match[-1] == CHAR_CR &&
4801                 (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&
4802                 start_match < end_subject &&
4803                 *start_match == CHAR_NL)
4804              start_match++;
4805            }
4806        }        }
     }  
4807    
4808    /* Or to a non-unique first char after study */      /* Or to a non-unique first byte after study */
4809    
4810    else if (start_bits != NULL)      else if (start_bits != NULL)
     {  
     while (start_match < end_subject)  
4811        {        {
4812        register unsigned int c = *start_match;        while (start_match < end_subject)
4813        if ((start_bits[c/8] & (1 << (c&7))) == 0) start_match++; else break;          {
4814            register unsigned int c = *start_match;
4815            if ((start_bits[c/8] & (1 << (c&7))) == 0) start_match++;
4816              else break;
4817            }
4818        }        }
4819      }      }   /* Starting optimizations */
4820    
4821    /* Restore fudged end_subject */    /* Restore fudged end_subject */
4822    
# Line 3783  do Line 4828  do
4828    printf("\n");    printf("\n");
4829  #endif  #endif
4830    
4831    /* If req_byte is set, we know that that character must appear in the subject    /* If req_byte is set, we know that that character must appear in the
4832    for the match to succeed. If the first character is set, req_byte must be    subject for the match to succeed. If the first character is set, req_byte
4833    later in the subject; otherwise the test starts at the match point. This    must be later in the subject; otherwise the test starts at the match point.
4834    optimization can save a huge amount of backtracking in patterns with nested    This optimization can save a huge amount of backtracking in patterns with
4835    unlimited repeats that aren't going to match. Writing separate code for    nested unlimited repeats that aren't going to match. Writing separate code
4836    cased/caseless versions makes it go faster, as does using an autoincrement    for cased/caseless versions makes it go faster, as does using an
4837    and backing off on a match.    autoincrement and backing off on a match.
4838    
4839    HOWEVER: when the subject string is very, very long, searching to its end can    HOWEVER: when the subject string is very, very long, searching to its end
4840    take a long time, and give bad performance on quite ordinary patterns. This    can take a long time, and give bad performance on quite ordinary patterns.
4841    showed up when somebody was matching /^C/ on a 32-megabyte string... so we    This showed up when somebody was matching something like /^\d+C/ on a
4842    don't do this when the string is sufficiently long.    32-megabyte string... so we don't do this when the string is sufficiently
4843      long.
4844    
4845    ALSO: this processing is disabled when partial matching is requested.    ALSO: this processing is disabled when partial matching is requested, or if
4846    */    disabling is explicitly requested. */
4847    
4848    if (req_byte >= 0 &&    if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&
4849          req_byte >= 0 &&
4850        end_subject - start_match < REQ_BYTE_MAX &&        end_subject - start_match < REQ_BYTE_MAX &&
4851        !md->partial)        !md->partial)
4852      {      {
# Line 3826  do Line 4873  do
4873            }            }
4874          }          }
4875    
4876        /* If we can't find the required character, break the matching loop */        /* If we can't find the required character, break the matching loop,
4877          forcing a match failure. */
4878    
4879        if (p >= end_subject) break;        if (p >= end_subject)
4880            {
4881            rc = MATCH_NOMATCH;
4882            break;
4883            }
4884    
4885        /* If we have found the required character, save the point where we        /* If we have found the required character, save the point where we
4886        found it, so that we don't search again next time round the loop if        found it, so that we don't search again next time round the loop if
# Line 3838  do Line 4890  do
4890        }        }
4891      }      }
4892    
4893    /* When a match occurs, substrings will be set for all internal extractions;    /* OK, we can now run the match. */
   we just need to set up the whole thing as substring 0 before returning. If  
   there were too many extractions, set the return code to zero. In the case  
   where we had to get some local store to hold offsets for backreferences, copy  
   those back references that we can. In this case there need not be overflow  
   if certain parts of the pattern were not used. */  
4894    
4895    md->start_match = start_match;    md->start_match_ptr = start_match;
4896    md->match_call_count = 0;    md->match_call_count = 0;
4897      rc = match(start_match, md->start_code, start_match, 2, md, ims, NULL, 0, 0);
4898    
4899    rc = match(start_match, md->start_code, 2, md, ims, NULL, match_isgroup, 0);    switch(rc)
   
   /* When the result is no match, if the subject's first character was a  
   newline and the PCRE_FIRSTLINE option is set, break (which will return  
   PCRE_ERROR_NOMATCH). The option requests that a match occur before the first  
   newline in the subject. Otherwise, advance the pointer to the next character  
   and continue - but the continuation will actually happen only when the  
   pattern is not anchored. */  
   
   if (rc == MATCH_NOMATCH)  
4900      {      {
4901      if (firstline &&      /* NOMATCH and PRUNE advance by one character. THEN at this level acts