/[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 399 by ph10, Sat Mar 21 12:34:15 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 271  typedef struct heapframe { Line 324  typedef struct heapframe {
324    
325    const uschar *Xeptr;    const uschar *Xeptr;
326    const uschar *Xecode;    const uschar *Xecode;
327      const uschar *Xmstart;
328    int Xoffset_top;    int Xoffset_top;
329    long int Xims;    long int Xims;
330    eptrblock *Xeptrb;    eptrblock *Xeptrb;
# Line 291  typedef struct heapframe { Line 345  typedef struct heapframe {
345    
346    BOOL Xcur_is_word;    BOOL Xcur_is_word;
347    BOOL Xcondition;    BOOL Xcondition;
   BOOL Xminimize;  
348    BOOL Xprev_is_word;    BOOL Xprev_is_word;
349    
350    unsigned long int Xoriginal_ims;    unsigned long int Xoriginal_ims;
# Line 303  typedef struct heapframe { Line 356  typedef struct heapframe {
356    int Xprop_category;    int Xprop_category;
357    int Xprop_chartype;    int Xprop_chartype;
358    int Xprop_script;    int Xprop_script;
359    int *Xprop_test_variable;    int Xoclength;
360      uschar Xocchars[8];
361  #endif  #endif
362    
363    int Xctype;    int Xctype;
364    int Xfc;    unsigned int Xfc;
365    int Xfi;    int Xfi;
366    int Xlength;    int Xlength;
367    int Xmax;    int Xmax;
# Line 321  typedef struct heapframe { Line 375  typedef struct heapframe {
375    
376    eptrblock Xnewptrb;    eptrblock Xnewptrb;
377    
378    /* Place to pass back result, and where to jump back to */    /* Where to jump back to */
379    
380    int  Xresult;    int Xwhere;
   jmp_buf Xwhere;  
381    
382  } heapframe;  } heapframe;
383    
# Line 340  typedef struct heapframe { Line 393  typedef struct heapframe {
393  *         Match from current position            *  *         Match from current position            *
394  *************************************************/  *************************************************/
395    
396  /* 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  
397  returns a negative (error) response, the outer incarnation must also return the  returns a negative (error) response, the outer incarnation must also return the
398  same response.  same response.
399    
# Line 353  performance. Tests using gcc on a SPARC Line 403  performance. Tests using gcc on a SPARC
403  made performance worse.  made performance worse.
404    
405  Arguments:  Arguments:
406     eptr        pointer in subject     eptr        pointer to current character in subject
407     ecode       position in code     ecode       pointer to current position in compiled code
408       mstart      pointer to the current match start position (can be modified
409                     by encountering \K)
410     offset_top  current top pointer     offset_top  current top pointer
411     md          pointer to "static" info for the match     md          pointer to "static" info for the match
412     ims         current /i, /m, and /s options     ims         current /i, /m, and /s options
# Line 362  Arguments: Line 414  Arguments:
414                   brackets - for testing for empty matches                   brackets - for testing for empty matches
415     flags       can contain     flags       can contain
416                   match_condassert - this is an assertion condition                   match_condassert - this is an assertion condition
417                   match_isgroup - this is the start of a bracketed group                   match_cbegroup - this is the start of an unlimited repeat
418                       group that can match an empty string
419     rdepth      the recursion depth     rdepth      the recursion depth
420    
421  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 425  Returns:       MATCH_MATCH if matched
425  */  */
426    
427  static int  static int
428  match(REGISTER USPTR eptr, REGISTER const uschar *ecode,  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, const uschar *mstart,
429    int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,    int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,
430    int flags, unsigned int rdepth)    int flags, unsigned int rdepth)
431  {  {
432  /* 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,
433  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
434  because they are used a lot in loops. */  "register" because they are used a lot in loops. */
435    
436  register int  rrc;         /* Returns from recursive calls */  register int  rrc;         /* Returns from recursive calls */
437  register int  i;           /* Used for loops not involving calls to RMATCH() */  register int  i;           /* Used for loops not involving calls to RMATCH() */
438  register unsigned int  c;  /* Character values not kept over RMATCH() calls */  register unsigned int c;   /* Character values not kept over RMATCH() calls */
439  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */
440    
441    BOOL minimize, possessive; /* Quantifier options */
442    
443  /* 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
444  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
445  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 453  frame->Xprevframe = NULL;            /*
453    
454  frame->Xeptr = eptr;  frame->Xeptr = eptr;
455  frame->Xecode = ecode;  frame->Xecode = ecode;
456    frame->Xmstart = mstart;
457  frame->Xoffset_top = offset_top;  frame->Xoffset_top = offset_top;
458  frame->Xims = ims;  frame->Xims = ims;
459  frame->Xeptrb = eptrb;  frame->Xeptrb = eptrb;
# Line 412  HEAP_RECURSE: Line 468  HEAP_RECURSE:
468    
469  #define eptr               frame->Xeptr  #define eptr               frame->Xeptr
470  #define ecode              frame->Xecode  #define ecode              frame->Xecode
471    #define mstart             frame->Xmstart
472  #define offset_top         frame->Xoffset_top  #define offset_top         frame->Xoffset_top
473  #define ims                frame->Xims  #define ims                frame->Xims
474  #define eptrb              frame->Xeptrb  #define eptrb              frame->Xeptrb
# Line 434  HEAP_RECURSE: Line 491  HEAP_RECURSE:
491    
492  #define cur_is_word        frame->Xcur_is_word  #define cur_is_word        frame->Xcur_is_word
493  #define condition          frame->Xcondition  #define condition          frame->Xcondition
 #define minimize           frame->Xminimize  
494  #define prev_is_word       frame->Xprev_is_word  #define prev_is_word       frame->Xprev_is_word
495    
496  #define original_ims       frame->Xoriginal_ims  #define original_ims       frame->Xoriginal_ims
# Line 446  HEAP_RECURSE: Line 502  HEAP_RECURSE:
502  #define prop_category      frame->Xprop_category  #define prop_category      frame->Xprop_category
503  #define prop_chartype      frame->Xprop_chartype  #define prop_chartype      frame->Xprop_chartype
504  #define prop_script        frame->Xprop_script  #define prop_script        frame->Xprop_script
505  #define prop_test_variable frame->Xprop_test_variable  #define oclength           frame->Xoclength
506    #define occhars            frame->Xocchars
507  #endif  #endif
508    
509  #define ctype              frame->Xctype  #define ctype              frame->Xctype
# Line 470  HEAP_RECURSE: Line 527  HEAP_RECURSE:
527  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
528  i, and fc and c, can be the same variables. */  i, and fc and c, can be the same variables. */
529    
530  #else  #else         /* NO_RECURSE not defined */
531  #define fi i  #define fi i
532  #define fc c  #define fc c
533    
# Line 489  recursion_info new_recursive;      /* wi Line 546  recursion_info new_recursive;      /* wi
546                                     /* that do not have to be preserved over  */                                     /* that do not have to be preserved over  */
547  BOOL cur_is_word;                  /* a recursive call to RMATCH().          */  BOOL cur_is_word;                  /* a recursive call to RMATCH().          */
548  BOOL condition;  BOOL condition;
 BOOL minimize;  
549  BOOL prev_is_word;  BOOL prev_is_word;
550    
551  unsigned long int original_ims;  unsigned long int original_ims;
# Line 501  int prop_fail_result; Line 557  int prop_fail_result;
557  int prop_category;  int prop_category;
558  int prop_chartype;  int prop_chartype;
559  int prop_script;  int prop_script;
560  int *prop_test_variable;  int oclength;
561    uschar occhars[8];
562  #endif  #endif
563    
564    int codelink;
565    int condcode;
566  int ctype;  int ctype;
567  int length;  int length;
568  int max;  int max;
# Line 516  int save_offset1, save_offset2, save_off Line 575  int save_offset1, save_offset2, save_off
575  int stacksave[REC_STACK_SAVE_MAX];  int stacksave[REC_STACK_SAVE_MAX];
576    
577  eptrblock newptrb;  eptrblock newptrb;
578  #endif  #endif     /* NO_RECURSE */
579    
580  /* These statements are here to stop the compiler complaining about unitialized  /* These statements are here to stop the compiler complaining about unitialized
581  variables. */  variables. */
# Line 524  variables. */ Line 583  variables. */
583  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
584  prop_value = 0;  prop_value = 0;
585  prop_fail_result = 0;  prop_fail_result = 0;
 prop_test_variable = NULL;  
586  #endif  #endif
587    
588    
589  /* 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
590  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
591  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 601  defined). However, RMATCH isn't like a f
601  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,
602  however, impact performance when true recursion is being used. */  however, impact performance when true recursion is being used. */
603    
604    #ifdef SUPPORT_UTF8
605    utf8 = md->utf8;       /* Local copy of the flag */
606    #else
607    utf8 = FALSE;
608    #endif
609    
610  /* 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
611  haven't exceeded the recursive call limit. */  haven't exceeded the recursive call limit. */
612    
# Line 550  if (rdepth >= md->match_limit_recursion) Line 615  if (rdepth >= md->match_limit_recursion)
615    
616  original_ims = ims;    /* Save for resetting on ')' */  original_ims = ims;    /* Save for resetting on ')' */
617    
618  #ifdef SUPPORT_UTF8  /* At the start of a group with an unlimited repeat that may match an empty
619  utf8 = md->utf8;       /* Local copy of the flag */  string, the match_cbegroup flag is set. When this is the case, add the current
620  #else  subject pointer to the chain of such remembered pointers, to be checked when we
621  utf8 = FALSE;  hit the closing ket, in order to break infinite loops that match no characters.
622  #endif  When match() is called in other circumstances, don't add to the chain. The
623    match_cbegroup flag must NOT be used with tail recursion, because the memory
624  /* 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
625  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. */  
626    
627  if ((flags & match_isgroup) != 0)  if ((flags & match_cbegroup) != 0)
628    {    {
   newptrb.epb_prev = eptrb;  
629    newptrb.epb_saved_eptr = eptr;    newptrb.epb_saved_eptr = eptr;
630      newptrb.epb_prev = eptrb;
631    eptrb = &newptrb;    eptrb = &newptrb;
632    }    }
633    
634  /* Now start processing the operations. */  /* Now start processing the opcodes. */
635    
636  for (;;)  for (;;)
637    {    {
638      minimize = possessive = FALSE;
639    op = *ecode;    op = *ecode;
640    minimize = FALSE;  
   
641    /* 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
642    matching at least one subject character. */    matching at least one subject character. */
643    
644    if (md->partial &&    if (md->partial &&
645        eptr >= md->end_subject &&        eptr >= md->end_subject &&
646        eptr > md->start_match)        eptr > mstart)
647      md->hitend = TRUE;      md->hitend = TRUE;
648    
649    /* 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)  
650      {      {
651      number = op - OP_BRA;      case OP_FAIL:
652        RRETURN(MATCH_NOMATCH);
653    
654      /* For extended extraction brackets (large number), we have to fish out the      case OP_PRUNE:
655      number from a dummy opcode at the start. */      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
656          ims, eptrb, flags, RM51);
657      if (number > EXTRACT_BASIC_MAX)      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
658        number = GET2(ecode, 2+LINK_SIZE);      RRETURN(MATCH_PRUNE);
659    
660        case OP_COMMIT:
661        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
662          ims, eptrb, flags, RM52);
663        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
664        RRETURN(MATCH_COMMIT);
665    
666        case OP_SKIP:
667        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
668          ims, eptrb, flags, RM53);
669        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
670        md->start_match_ptr = eptr;   /* Pass back current position */
671        RRETURN(MATCH_SKIP);
672    
673        case OP_THEN:
674        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
675          ims, eptrb, flags, RM54);
676        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
677        RRETURN(MATCH_THEN);
678    
679        /* Handle a capturing bracket. If there is space in the offset vector, save
680        the current subject position in the working slot at the top of the vector.
681        We mustn't change the current values of the data slot, because they may be
682        set from a previous iteration of this group, and be referred to by a
683        reference inside the group.
684    
685        If the bracket fails to match, we need to restore this value and also the
686        values of the final offsets, in case they were set by a previous iteration
687        of the same bracket.
688    
689        If there isn't enough space in the offset vector, treat this as if it were
690        a non-capturing bracket. Don't worry about setting the flag for the error
691        case here; that is handled in the code for KET. */
692    
693        case OP_CBRA:
694        case OP_SCBRA:
695        number = GET2(ecode, 1+LINK_SIZE);
696      offset = number << 1;      offset = number << 1;
697    
698  #ifdef DEBUG  #ifdef DEBUG
699      printf("start bracket %d subject=", number);      printf("start bracket %d\n", number);
700        printf("subject=");
701      pchars(eptr, 16, TRUE, md);      pchars(eptr, 16, TRUE, md);
702      printf("\n");      printf("\n");
703  #endif  #endif
# Line 624  for (;;) Line 712  for (;;)
712        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));        DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
713        md->offset_vector[md->offset_end - number] = eptr - md->start_subject;        md->offset_vector[md->offset_end - number] = eptr - md->start_subject;
714    
715          flags = (op == OP_SCBRA)? match_cbegroup : 0;
716        do        do
717          {          {
718          RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
719            match_isgroup);            ims, eptrb, flags, RM1);
720          if (rrc != MATCH_NOMATCH) RRETURN(rrc);          if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
721          md->capture_last = save_capture_last;          md->capture_last = save_capture_last;
722          ecode += GET(ecode, 1);          ecode += GET(ecode, 1);
723          }          }
# Line 643  for (;;) Line 732  for (;;)
732        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
733        }        }
734    
735      /* Insufficient room for saving captured contents */      /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
736        as a non-capturing bracket. */
     else op = OP_BRA;  
     }  
   
   /* Other types of node can be handled by a switch */  
737    
738    switch(op)      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
739      {      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
     case OP_BRA:     /* Non-capturing bracket: optimized */  
     DPRINTF(("start bracket 0\n"));  
740    
741      /* Loop for all the alternatives */      DPRINTF(("insufficient capture room: treat as non-capturing\n"));
742    
743        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
744        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
745    
746        /* Non-capturing bracket. Loop for all the alternatives. When we get to the
747        final alternative within the brackets, we would return the result of a
748        recursive call to match() whatever happened. We can reduce stack usage by
749        turning this into a tail recursion, except in the case when match_cbegroup
750        is set.*/
751    
752        case OP_BRA:
753        case OP_SBRA:
754        DPRINTF(("start non-capturing bracket\n"));
755        flags = (op >= OP_SBRA)? match_cbegroup : 0;
756      for (;;)      for (;;)
757        {        {
758        /* When we get to the final alternative within the brackets, we would        if (ecode[GET(ecode, 1)] != OP_ALT)   /* Final alternative */
759        return the result of a recursive call to match() whatever happened. We          {
760        can reduce stack usage by turning this into a tail recursion. */          if (flags == 0)    /* Not a possibly empty group */
761              {
762        if (ecode[GET(ecode, 1)] != OP_ALT)            ecode += _pcre_OP_lengths[*ecode];
763         {            DPRINTF(("bracket 0 tail recursion\n"));
764         ecode += 1 + LINK_SIZE;            goto TAIL_RECURSE;
765         flags = match_isgroup;            }
766         DPRINTF(("bracket 0 tail recursion\n"));  
767         goto TAIL_RECURSE;          /* Possibly empty group; can't use tail recursion. */
768         }  
769            RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,
770              eptrb, flags, RM48);
771            RRETURN(rrc);
772            }
773    
774        /* For non-final alternatives, continue the loop for a NOMATCH result;        /* For non-final alternatives, continue the loop for a NOMATCH result;
775        otherwise return. */        otherwise return. */
776    
777        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,
778          match_isgroup);          eptrb, flags, RM2);
779        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
780        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
781        }        }
782      /* Control never reaches here. */      /* Control never reaches here. */
# Line 688  for (;;) Line 788  for (;;)
788      obeyed, we can use tail recursion to avoid using another stack frame. */      obeyed, we can use tail recursion to avoid using another stack frame. */
789    
790      case OP_COND:      case OP_COND:
791      if (ecode[LINK_SIZE+1] == OP_CREF) /* Condition extract or recurse test */      case OP_SCOND:
792        codelink= GET(ecode, 1);
793    
794        /* Because of the way auto-callout works during compile, a callout item is
795        inserted between OP_COND and an assertion condition. */
796    
797        if (ecode[LINK_SIZE+1] == OP_CALLOUT)
798          {
799          if (pcre_callout != NULL)
800            {
801            pcre_callout_block cb;
802            cb.version          = 1;   /* Version 1 of the callout block */
803            cb.callout_number   = ecode[LINK_SIZE+2];
804            cb.offset_vector    = md->offset_vector;
805            cb.subject          = (PCRE_SPTR)md->start_subject;
806            cb.subject_length   = md->end_subject - md->start_subject;
807            cb.start_match      = mstart - md->start_subject;
808            cb.current_position = eptr - md->start_subject;
809            cb.pattern_position = GET(ecode, LINK_SIZE + 3);
810            cb.next_item_length = GET(ecode, 3 + 2*LINK_SIZE);
811            cb.capture_top      = offset_top/2;
812            cb.capture_last     = md->capture_last;
813            cb.callout_data     = md->callout_data;
814            if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);
815            if (rrc < 0) RRETURN(rrc);
816            }
817          ecode += _pcre_OP_lengths[OP_CALLOUT];
818          }
819    
820        condcode = ecode[LINK_SIZE+1];
821    
822        /* Now see what the actual condition is */
823    
824        if (condcode == OP_RREF)         /* Recursion test */
825          {
826          offset = GET2(ecode, LINK_SIZE + 2);     /* Recursion group number*/
827          condition = md->recursive != NULL &&
828            (offset == RREF_ANY || offset == md->recursive->group_num);
829          ecode += condition? 3 : GET(ecode, 1);
830          }
831    
832        else if (condcode == OP_CREF)    /* Group used test */
833        {        {
834        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */        offset = GET2(ecode, LINK_SIZE+2) << 1;  /* Doubled ref number */
835        condition = (offset == CREF_RECURSE * 2)?        condition = offset < offset_top && md->offset_vector[offset] >= 0;
836          (md->recursive != NULL) :        ecode += condition? 3 : GET(ecode, 1);
837          (offset < offset_top && md->offset_vector[offset] >= 0);        }
838        ecode += condition? (LINK_SIZE + 4) : (LINK_SIZE + 1 + GET(ecode, 1));  
839        flags = match_isgroup;      else if (condcode == OP_DEF)     /* DEFINE - always false */
840        goto TAIL_RECURSE;        {
841          condition = FALSE;
842          ecode += GET(ecode, 1);
843        }        }
844    
845      /* The condition is an assertion. Call match() to evaluate it - setting      /* The condition is an assertion. Call match() to evaluate it - setting
846      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
847        assertion. */
848    
849      else      else
850        {        {
851        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,
852            match_condassert | match_isgroup);            match_condassert, RM3);
853        if (rrc == MATCH_MATCH)        if (rrc == MATCH_MATCH)
854          {          {
855          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE+2);          condition = TRUE;
856            ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
857          while (*ecode == OP_ALT) ecode += GET(ecode, 1);          while (*ecode == OP_ALT) ecode += GET(ecode, 1);
858          }          }
859        else if (rrc != MATCH_NOMATCH)        else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
860          {          {
861          RRETURN(rrc);         /* Need braces because of following else */          RRETURN(rrc);         /* Need braces because of following else */
862          }          }
863        else ecode += GET(ecode, 1);        else
864            {
865            condition = FALSE;
866            ecode += codelink;
867            }
868          }
869    
870        /* 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,
871        we can use tail recursion to avoid using another stack frame. */      we can use tail recursion to avoid using another stack frame, except when
872        match_cbegroup is required for an unlimited repeat of a possibly empty
873        group. If the second alternative doesn't exist, we can just plough on. */
874    
875        if (condition || *ecode == OP_ALT)
876          {
877          ecode += 1 + LINK_SIZE;
878          if (op == OP_SCOND)        /* Possibly empty group */
879            {
880            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, match_cbegroup, RM49);
881            RRETURN(rrc);
882            }
883          else                       /* Group must match something */
884            {
885            flags = 0;
886            goto TAIL_RECURSE;
887            }
888          }
889        else                         /* Condition false & no alternative */
890          {
891        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = match_isgroup;  
       goto TAIL_RECURSE;  
892        }        }
     /* Control never reaches here */  
   
     /* Skip over conditional reference or large extraction number data if  
     encountered. */  
   
     case OP_CREF:  
     case OP_BRANUMBER:  
     ecode += 3;  
893      break;      break;
894    
     /* End of the pattern. If we are in a recursion, we should restore the  
     offsets appropriately and continue from after the call. */  
895    
896        /* End of the pattern, either real or forced. If we are in a top-level
897        recursion, we should restore the offsets appropriately and continue from
898        after the call. */
899    
900        case OP_ACCEPT:
901      case OP_END:      case OP_END:
902      if (md->recursive != NULL && md->recursive->group_num == 0)      if (md->recursive != NULL && md->recursive->group_num == 0)
903        {        {
# Line 745  for (;;) Line 906  for (;;)
906        md->recursive = rec->prevrec;        md->recursive = rec->prevrec;
907        memmove(md->offset_vector, rec->offset_save,        memmove(md->offset_vector, rec->offset_save,
908          rec->saved_max * sizeof(int));          rec->saved_max * sizeof(int));
909        md->start_match = rec->save_start;        mstart = rec->save_start;
910        ims = original_ims;        ims = original_ims;
911        ecode = rec->after_call;        ecode = rec->after_call;
912        break;        break;
# Line 754  for (;;) Line 915  for (;;)
915      /* 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
916      string - backtracking will then try other alternatives, if any. */      string - backtracking will then try other alternatives, if any. */
917    
918      if (md->notempty && eptr == md->start_match) RRETURN(MATCH_NOMATCH);      if (md->notempty && eptr == mstart) RRETURN(MATCH_NOMATCH);
919      md->end_match_ptr = eptr;          /* Record where we ended */      md->end_match_ptr = eptr;           /* Record where we ended */
920      md->end_offset_top = offset_top;   /* and how many extracts were taken */      md->end_offset_top = offset_top;    /* and how many extracts were taken */
921        md->start_match_ptr = mstart;       /* and the start (\K can modify) */
922      RRETURN(MATCH_MATCH);      RRETURN(MATCH_MATCH);
923    
924      /* Change option settings */      /* Change option settings */
# Line 777  for (;;) Line 939  for (;;)
939      case OP_ASSERTBACK:      case OP_ASSERTBACK:
940      do      do
941        {        {
942        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,
943          match_isgroup);          RM4);
944        if (rrc == MATCH_MATCH) break;        if (rrc == MATCH_MATCH) break;
945        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
946        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
947        }        }
948      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
# Line 804  for (;;) Line 966  for (;;)
966      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
967      do      do
968        {        {
969        RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,
970          match_isgroup);          RM5);
971        if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);        if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);
972        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
973        ecode += GET(ecode,1);        ecode += GET(ecode,1);
974        }        }
975      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
# Line 826  for (;;) Line 988  for (;;)
988  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
989      if (utf8)      if (utf8)
990        {        {
991        c = GET(ecode,1);        i = GET(ecode, 1);
992        for (i = 0; i < c; i++)        while (i-- > 0)
993          {          {
994          eptr--;          eptr--;
995          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);          if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
996          BACKCHAR(eptr)          BACKCHAR(eptr);
997          }          }
998        }        }
999      else      else
# Line 840  for (;;) Line 1002  for (;;)
1002      /* 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 */
1003    
1004        {        {
1005        eptr -= GET(ecode,1);        eptr -= GET(ecode, 1);
1006        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);        if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1007        }        }
1008    
# Line 862  for (;;) Line 1024  for (;;)
1024        cb.offset_vector    = md->offset_vector;        cb.offset_vector    = md->offset_vector;
1025        cb.subject          = (PCRE_SPTR)md->start_subject;        cb.subject          = (PCRE_SPTR)md->start_subject;
1026        cb.subject_length   = md->end_subject - md->start_subject;        cb.subject_length   = md->end_subject - md->start_subject;
1027        cb.start_match      = md->start_match - md->start_subject;        cb.start_match      = mstart - md->start_subject;
1028        cb.current_position = eptr - md->start_subject;        cb.current_position = eptr - md->start_subject;
1029        cb.pattern_position = GET(ecode, 2);        cb.pattern_position = GET(ecode, 2);
1030        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);        cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
# Line 897  for (;;) Line 1059  for (;;)
1059      case OP_RECURSE:      case OP_RECURSE:
1060        {        {
1061        callpat = md->start_code + GET(ecode, 1);        callpat = md->start_code + GET(ecode, 1);
1062        new_recursive.group_num = *callpat - OP_BRA;        new_recursive.group_num = (callpat == md->start_code)? 0 :
1063            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);  
1064    
1065        /* Add to "recursing stack" */        /* Add to "recursing stack" */
1066    
# Line 929  for (;;) Line 1086  for (;;)
1086    
1087        memcpy(new_recursive.offset_save, md->offset_vector,        memcpy(new_recursive.offset_save, md->offset_vector,
1088              new_recursive.saved_max * sizeof(int));              new_recursive.saved_max * sizeof(int));
1089        new_recursive.save_start = md->start_match;        new_recursive.save_start = mstart;
1090        md->start_match = eptr;        mstart = eptr;
1091    
1092        /* 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
1093        restore the offset and recursion data. */        restore the offset and recursion data. */
1094    
1095        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1096          flags = (*callpat >= OP_SBRA)? match_cbegroup : 0;
1097        do        do
1098          {          {
1099          RMATCH(rrc, eptr, callpat + 1 + LINK_SIZE, offset_top, md, ims,          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,
1100              eptrb, match_isgroup);            md, ims, eptrb, flags, RM6);
1101          if (rrc == MATCH_MATCH)          if (rrc == MATCH_MATCH)
1102            {            {
1103            DPRINTF(("Recursion matched\n"));            DPRINTF(("Recursion matched\n"));
# Line 948  for (;;) Line 1106  for (;;)
1106              (pcre_free)(new_recursive.offset_save);              (pcre_free)(new_recursive.offset_save);
1107            RRETURN(MATCH_MATCH);            RRETURN(MATCH_MATCH);
1108            }            }
1109          else if (rrc != MATCH_NOMATCH)          else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
1110            {            {
1111            DPRINTF(("Recursion gave error %d\n", rrc));            DPRINTF(("Recursion gave error %d\n", rrc));
1112            RRETURN(rrc);            RRETURN(rrc);
# Line 982  for (;;) Line 1140  for (;;)
1140    
1141      do      do
1142        {        {
1143        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);  
1144        if (rrc == MATCH_MATCH) break;        if (rrc == MATCH_MATCH) break;
1145        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
1146        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1147        }        }
1148      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
# Line 997  for (;;) Line 1154  for (;;)
1154      /* Continue as from after the assertion, updating the offsets high water      /* Continue as from after the assertion, updating the offsets high water
1155      mark, since extracts may have been taken. */      mark, since extracts may have been taken. */
1156    
1157      do ecode += GET(ecode,1); while (*ecode == OP_ALT);      do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
1158    
1159      offset_top = md->end_offset_top;      offset_top = md->end_offset_top;
1160      eptr = md->end_match_ptr;      eptr = md->end_match_ptr;
# Line 1028  for (;;) Line 1185  for (;;)
1185    
1186      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1187        {        {
1188        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);
1189        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1190        ecode = prev;        ecode = prev;
1191        flags = match_isgroup;        flags = 0;
1192        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1193        }        }
1194      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1195        {        {
1196        RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);        RMATCH(eptr, prev, offset_top, md, ims, eptrb, match_cbegroup, RM9);
1197        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1198        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1199        flags = 0;        flags = 0;
# Line 1051  for (;;) Line 1208  for (;;)
1208      do ecode += GET(ecode,1); while (*ecode == OP_ALT);      do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1209      break;      break;
1210    
1211      /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating      /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
1212      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
1213      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
1214      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
1215      preceded by BRAZERO or BRAMINZERO. */      optional ones preceded by BRAZERO or BRAMINZERO. */
1216    
1217      case OP_BRAZERO:      case OP_BRAZERO:
1218        {        {
1219        next = ecode+1;        next = ecode+1;
1220        RMATCH(rrc, eptr, next, offset_top, md, ims, eptrb, match_isgroup);        RMATCH(eptr, next, offset_top, md, ims, eptrb, 0, RM10);
1221        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1222        do next += GET(next,1); while (*next == OP_ALT);        do next += GET(next,1); while (*next == OP_ALT);
1223        ecode = next + 1+LINK_SIZE;        ecode = next + 1 + LINK_SIZE;
1224        }        }
1225      break;      break;
1226    
1227      case OP_BRAMINZERO:      case OP_BRAMINZERO:
1228        {        {
1229        next = ecode+1;        next = ecode+1;
1230        do next += GET(next,1); while (*next == OP_ALT);        do next += GET(next, 1); while (*next == OP_ALT);
1231        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);  
1232        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1233        ecode++;        ecode++;
1234        }        }
1235      break;      break;
1236    
1237      /* End of a group, repeated or non-repeating. If we are at the end of      case OP_SKIPZERO:
1238      an assertion "group", stop matching and return MATCH_MATCH, but record the        {
1239      current high water mark for use by positive assertions. Do this also        next = ecode+1;
1240      for the "once" (not-backup up) groups. */        do next += GET(next,1); while (*next == OP_ALT);
1241          ecode = next + 1 + LINK_SIZE;
1242          }
1243        break;
1244    
1245        /* End of a group, repeated or non-repeating. */
1246    
1247      case OP_KET:      case OP_KET:
1248      case OP_KETRMIN:      case OP_KETRMIN:
1249      case OP_KETRMAX:      case OP_KETRMAX:
1250      prev = ecode - GET(ecode, 1);      prev = ecode - GET(ecode, 1);
     saved_eptr = eptrb->epb_saved_eptr;  
1251    
1252      /* Back up the stack of bracket start pointers. */      /* If this was a group that remembered the subject start, in order to break
1253        infinite repeats of empty string matches, retrieve the subject start from
1254        the chain. Otherwise, set it NULL. */
1255    
1256      eptrb = eptrb->epb_prev;      if (*prev >= OP_SBRA)
1257          {
1258          saved_eptr = eptrb->epb_saved_eptr;   /* Value at start of group */
1259          eptrb = eptrb->epb_prev;              /* Backup to previous group */
1260          }
1261        else saved_eptr = NULL;
1262    
1263        /* If we are at the end of an assertion group, stop matching and return
1264        MATCH_MATCH, but record the current high water mark for use by positive
1265        assertions. Do this also for the "once" (atomic) groups. */
1266    
1267      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||      if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
1268          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||          *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||
# Line 1102  for (;;) Line 1273  for (;;)
1273        RRETURN(MATCH_MATCH);        RRETURN(MATCH_MATCH);
1274        }        }
1275    
1276      /* 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
1277      group number back at the start and if necessary complete handling an      and if necessary complete handling an extraction by setting the offsets and
1278      extraction by setting the offsets and bumping the high water mark. */      bumping the high water mark. Note that whole-pattern recursion is coded as
1279        a recurse into group 0, so it won't be picked up here. Instead, we catch it
1280        when the OP_END is reached. Other recursion is handled here. */
1281    
1282      if (*prev != OP_COND)      if (*prev == OP_CBRA || *prev == OP_SCBRA)
1283        {        {
1284        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);  
1285        offset = number << 1;        offset = number << 1;
1286    
1287  #ifdef DEBUG  #ifdef DEBUG
# Line 1121  for (;;) Line 1289  for (;;)
1289        printf("\n");        printf("\n");
1290  #endif  #endif
1291    
1292        /* Test for a numbered group. This includes groups called as a result        md->capture_last = number;
1293        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)  
1294          {          {
1295          md->capture_last = number;          md->offset_vector[offset] =
1296          if (offset >= md->offset_max) md->offset_overflow = TRUE; else            md->offset_vector[md->offset_end - number];
1297            {          md->offset_vector[offset+1] = eptr - md->start_subject;
1298            md->offset_vector[offset] =          if (offset_top <= offset) offset_top = offset + 2;
1299              md->offset_vector[md->offset_end - number];          }
1300            md->offset_vector[offset+1] = eptr - md->start_subject;  
1301            if (offset_top <= offset) offset_top = offset + 2;        /* Handle a recursively called group. Restore the offsets
1302            }        appropriately and continue from after the call. */
1303    
1304          /* Handle a recursively called group. Restore the offsets        if (md->recursive != NULL && md->recursive->group_num == number)
1305          appropriately and continue from after the call. */          {
1306            recursion_info *rec = md->recursive;
1307          if (md->recursive != NULL && md->recursive->group_num == number)          DPRINTF(("Recursion (%d) succeeded - continuing\n", number));
1308            {          md->recursive = rec->prevrec;
1309            recursion_info *rec = md->recursive;          mstart = rec->save_start;
1310            DPRINTF(("Recursion (%d) succeeded - continuing\n", number));          memcpy(md->offset_vector, rec->offset_save,
1311            md->recursive = rec->prevrec;            rec->saved_max * sizeof(int));
1312            md->start_match = rec->save_start;          ecode = rec->after_call;
1313            memcpy(md->offset_vector, rec->offset_save,          ims = original_ims;
1314              rec->saved_max * sizeof(int));          break;
           ecode = rec->after_call;  
           ims = original_ims;  
           break;  
           }  
1315          }          }
1316        }        }
1317    
1318      /* 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
1319      the group. */      flags, in case they got changed during the group. */
1320    
1321      ims = original_ims;      ims = original_ims;
1322      DPRINTF(("ims reset to %02lx\n", ims));      DPRINTF(("ims reset to %02lx\n", ims));
# Line 1175  for (;;) Line 1335  for (;;)
1335    
1336      /* 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
1337      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
1338      tail recursion to avoid using another stack frame. */      tail recursion to avoid using another stack frame, unless we have an
1339        unlimited repeat of a group that can match an empty string. */
1340    
1341        flags = (*prev >= OP_SBRA)? match_cbegroup : 0;
1342    
1343      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1344        {        {
1345        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);
1346        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1347          if (flags != 0)    /* Could match an empty string */
1348            {
1349            RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM50);
1350            RRETURN(rrc);
1351            }
1352        ecode = prev;        ecode = prev;
       flags = match_isgroup;  
1353        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1354        }        }
1355      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1356        {        {
1357        RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);        RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM13);
1358        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1359        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1360        flags = 0;        flags = 0;
# Line 1202  for (;;) Line 1369  for (;;)
1369      if ((ims & PCRE_MULTILINE) != 0)      if ((ims & PCRE_MULTILINE) != 0)
1370        {        {
1371        if (eptr != md->start_subject &&        if (eptr != md->start_subject &&
1372            (eptr == md->end_subject ||            (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
            eptr < md->start_subject + md->nllen ||  
            !IS_NEWLINE(eptr - md->nllen)))  
1373          RRETURN(MATCH_NOMATCH);          RRETURN(MATCH_NOMATCH);
1374        ecode++;        ecode++;
1375        break;        break;
# Line 1225  for (;;) Line 1390  for (;;)
1390      ecode++;      ecode++;
1391      break;      break;
1392    
1393        /* Reset the start of match point */
1394    
1395        case OP_SET_SOM:
1396        mstart = eptr;
1397        ecode++;
1398        break;
1399    
1400      /* Assert before internal newline if multiline, or before a terminating      /* Assert before internal newline if multiline, or before a terminating
1401      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. */
1402    
# Line 1244  for (;;) Line 1416  for (;;)
1416        if (!md->endonly)        if (!md->endonly)
1417          {          {
1418          if (eptr != md->end_subject &&          if (eptr != md->end_subject &&
1419              (eptr != md->end_subject - md->nllen || !IS_NEWLINE(eptr)))              (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
1420            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1421          ecode++;          ecode++;
1422          break;          break;
# Line 1263  for (;;) Line 1435  for (;;)
1435    
1436      case OP_EODN:      case OP_EODN:
1437      if (eptr != md->end_subject &&      if (eptr != md->end_subject &&
1438          (eptr != md->end_subject - md->nllen || !IS_NEWLINE(eptr)))          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
1439        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
1440      ecode++;      ecode++;
1441      break;      break;
# Line 1317  for (;;) Line 1489  for (;;)
1489      /* Match a single character type; inline for speed */      /* Match a single character type; inline for speed */
1490    
1491      case OP_ANY:      case OP_ANY:
1492      if ((ims & PCRE_DOTALL) == 0)      if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
1493        {      /* Fall through */
1494        if (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr))  
1495          RRETURN(MATCH_NOMATCH);      case OP_ALLANY:
       }  
1496      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);
1497      if (utf8)      if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
       while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;  
1498      ecode++;      ecode++;
1499      break;      break;
1500    
# Line 1414  for (;;) Line 1584  for (;;)
1584      ecode++;      ecode++;
1585      break;      break;
1586    
1587        case OP_ANYNL:
1588        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1589        GETCHARINCTEST(c, eptr);
1590        switch(c)
1591          {
1592          default: RRETURN(MATCH_NOMATCH);
1593          case 0x000d:
1594          if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
1595          break;
1596    
1597          case 0x000a:
1598          break;
1599    
1600          case 0x000b:
1601          case 0x000c:
1602          case 0x0085:
1603          case 0x2028:
1604          case 0x2029:
1605          if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
1606          break;
1607          }
1608        ecode++;
1609        break;
1610    
1611        case OP_NOT_HSPACE:
1612        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1613        GETCHARINCTEST(c, eptr);
1614        switch(c)
1615          {
1616          default: break;
1617          case 0x09:      /* HT */
1618          case 0x20:      /* SPACE */
1619          case 0xa0:      /* NBSP */
1620          case 0x1680:    /* OGHAM SPACE MARK */
1621          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
1622          case 0x2000:    /* EN QUAD */
1623          case 0x2001:    /* EM QUAD */
1624          case 0x2002:    /* EN SPACE */
1625          case 0x2003:    /* EM SPACE */
1626          case 0x2004:    /* THREE-PER-EM SPACE */
1627          case 0x2005:    /* FOUR-PER-EM SPACE */
1628          case 0x2006:    /* SIX-PER-EM SPACE */
1629          case 0x2007:    /* FIGURE SPACE */
1630          case 0x2008:    /* PUNCTUATION SPACE */
1631          case 0x2009:    /* THIN SPACE */
1632          case 0x200A:    /* HAIR SPACE */
1633          case 0x202f:    /* NARROW NO-BREAK SPACE */
1634          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
1635          case 0x3000:    /* IDEOGRAPHIC SPACE */
1636          RRETURN(MATCH_NOMATCH);
1637          }
1638        ecode++;
1639        break;
1640    
1641        case OP_HSPACE:
1642        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1643        GETCHARINCTEST(c, eptr);
1644        switch(c)
1645          {
1646          default: RRETURN(MATCH_NOMATCH);
1647          case 0x09:      /* HT */
1648          case 0x20:      /* SPACE */
1649          case 0xa0:      /* NBSP */
1650          case 0x1680:    /* OGHAM SPACE MARK */
1651          case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
1652          case 0x2000:    /* EN QUAD */
1653          case 0x2001:    /* EM QUAD */
1654          case 0x2002:    /* EN SPACE */
1655          case 0x2003:    /* EM SPACE */
1656          case 0x2004:    /* THREE-PER-EM SPACE */
1657          case 0x2005:    /* FOUR-PER-EM SPACE */
1658          case 0x2006:    /* SIX-PER-EM SPACE */
1659          case 0x2007:    /* FIGURE SPACE */
1660          case 0x2008:    /* PUNCTUATION SPACE */
1661          case 0x2009:    /* THIN SPACE */
1662          case 0x200A:    /* HAIR SPACE */
1663          case 0x202f:    /* NARROW NO-BREAK SPACE */
1664          case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
1665          case 0x3000:    /* IDEOGRAPHIC SPACE */
1666          break;
1667          }
1668        ecode++;
1669        break;
1670    
1671        case OP_NOT_VSPACE:
1672        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1673        GETCHARINCTEST(c, eptr);
1674        switch(c)
1675          {
1676          default: break;
1677          case 0x0a:      /* LF */
1678          case 0x0b:      /* VT */
1679          case 0x0c:      /* FF */
1680          case 0x0d:      /* CR */
1681          case 0x85:      /* NEL */
1682          case 0x2028:    /* LINE SEPARATOR */
1683          case 0x2029:    /* PARAGRAPH SEPARATOR */
1684          RRETURN(MATCH_NOMATCH);
1685          }
1686        ecode++;
1687        break;
1688    
1689        case OP_VSPACE:
1690        if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1691        GETCHARINCTEST(c, eptr);
1692        switch(c)
1693          {
1694          default: RRETURN(MATCH_NOMATCH);
1695          case 0x0a:      /* LF */
1696          case 0x0b:      /* VT */
1697          case 0x0c:      /* FF */
1698          case 0x0d:      /* CR */
1699          case 0x85:      /* NEL */
1700          case 0x2028:    /* LINE SEPARATOR */
1701          case 0x2029:    /* PARAGRAPH SEPARATOR */
1702          break;
1703          }
1704        ecode++;
1705        break;
1706    
1707  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
1708      /* Check the next character by Unicode property. We will get here only      /* Check the next character by Unicode property. We will get here only
1709      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 1713  for (;;)
1713      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1714      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1715        {        {
1716        int chartype, script;        const ucd_record *prop = GET_UCD(c);
       int category = _pcre_ucp_findprop(c, &chartype, &script);  
1717    
1718        switch(ecode[1])        switch(ecode[1])
1719          {          {
# Line 1433  for (;;) Line 1722  for (;;)
1722          break;          break;
1723    
1724          case PT_LAMP:          case PT_LAMP:
1725          if ((chartype == ucp_Lu ||          if ((prop->chartype == ucp_Lu ||
1726               chartype == ucp_Ll ||               prop->chartype == ucp_Ll ||
1727               chartype == ucp_Lt) == (op == OP_NOTPROP))               prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
1728            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1729           break;           break;
1730    
1731          case PT_GC:          case PT_GC:
1732          if ((ecode[2] != category) == (op == OP_PROP))          if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))
1733            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1734          break;          break;
1735    
1736          case PT_PC:          case PT_PC:
1737          if ((ecode[2] != chartype) == (op == OP_PROP))          if ((ecode[2] != prop->chartype) == (op == OP_PROP))
1738            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1739          break;          break;
1740    
1741          case PT_SC:          case PT_SC:
1742          if ((ecode[2] != script) == (op == OP_PROP))          if ((ecode[2] != prop->script) == (op == OP_PROP))
1743            RRETURN(MATCH_NOMATCH);            RRETURN(MATCH_NOMATCH);
1744          break;          break;
1745    
1746          default:          default:
1747          RRETURN(PCRE_ERROR_INTERNAL);          RRETURN(PCRE_ERROR_INTERNAL);
         break;  
1748          }          }
1749    
1750        ecode += 3;        ecode += 3;
# Line 1470  for (;;) Line 1758  for (;;)
1758      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);      if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1759      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
1760        {        {
1761        int chartype, script;        int category = UCD_CATEGORY(c);
       int category = _pcre_ucp_findprop(c, &chartype, &script);  
1762        if (category == ucp_M) RRETURN(MATCH_NOMATCH);        if (category == ucp_M) RRETURN(MATCH_NOMATCH);
1763        while (eptr < md->end_subject)        while (eptr < md->end_subject)
1764          {          {
# Line 1480  for (;;) Line 1767  for (;;)
1767            {            {
1768            GETCHARLEN(c, eptr, len);            GETCHARLEN(c, eptr, len);
1769            }            }
1770          category = _pcre_ucp_findprop(c, &chartype, &script);          category = UCD_CATEGORY(c);
1771          if (category != ucp_M) break;          if (category != ucp_M) break;
1772          eptr += len;          eptr += len;
1773          }          }
# Line 1501  for (;;) Line 1788  for (;;)
1788      case OP_REF:      case OP_REF:
1789        {        {
1790        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */
1791        ecode += 3;                                 /* Advance past item */        ecode += 3;
1792    
1793          /* If the reference is unset, there are two possibilities:
1794    
1795          (a) In the default, Perl-compatible state, set the length to be longer
1796          than the amount of subject left; this ensures that every attempt at a
1797          match fails. We can't just fail here, because of the possibility of
1798          quantifiers with zero minima.
1799    
1800          (b) If the JavaScript compatibility flag is set, set the length to zero
1801          so that the back reference matches an empty string.
1802    
1803          Otherwise, set the length to the length of what was matched by the
1804          referenced subpattern. */
1805    
1806        /* If the reference is unset, set the length to be longer than the amount        if (offset >= offset_top || md->offset_vector[offset] < 0)
1807        of subject left; this ensures that every attempt at a match fails. We          length = (md->jscript_compat)? 0 : md->end_subject - eptr + 1;
1808        can't just fail here, because of the possibility of quantifiers with zero        else
1809        minima. */          length = md->offset_vector[offset+1] - md->offset_vector[offset];
   
       length = (offset >= offset_top || md->offset_vector[offset] < 0)?  
         md->end_subject - eptr + 1 :  
         md->offset_vector[offset+1] - md->offset_vector[offset];  
1810    
1811        /* Set up for repetition, or handle the non-repeated case */        /* Set up for repetition, or handle the non-repeated case */
1812    
# Line 1570  for (;;) Line 1866  for (;;)
1866          {          {
1867          for (fi = min;; fi++)          for (fi = min;; fi++)
1868            {            {
1869            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM14);
1870            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1871            if (fi >= max || !match_ref(offset, eptr, length, md, ims))            if (fi >= max || !match_ref(offset, eptr, length, md, ims))
1872              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
# Line 1591  for (;;) Line 1887  for (;;)
1887            }            }
1888          while (eptr >= pp)          while (eptr >= pp)
1889            {            {
1890            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM15);
1891            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1892            eptr -= length;            eptr -= length;
1893            }            }
# Line 1696  for (;;) Line 1992  for (;;)
1992            {            {
1993            for (fi = min;; fi++)            for (fi = min;; fi++)
1994              {              {
1995              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM16);
1996              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1997              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
1998              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
# Line 1716  for (;;) Line 2012  for (;;)
2012            {            {
2013            for (fi = min;; fi++)            for (fi = min;; fi++)
2014              {              {
2015              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM17);
2016              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2017              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2018              c = *eptr++;              c = *eptr++;
# Line 1753  for (;;) Line 2049  for (;;)
2049              }              }
2050            for (;;)            for (;;)
2051              {              {
2052              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM18);
2053              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2054              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2055              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 1772  for (;;) Line 2068  for (;;)
2068              }              }
2069            while (eptr >= pp)            while (eptr >= pp)
2070              {              {
2071              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM19);
2072              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2073              eptr--;              eptr--;
2074              }              }
# Line 1785  for (;;) Line 2081  for (;;)
2081    
2082    
2083      /* Match an extended character class. This opcode is encountered only      /* Match an extended character class. This opcode is encountered only
2084      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
2085        mode, because Unicode properties are supported in non-UTF-8 mode. */
2086    
2087  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2088      case OP_XCLASS:      case OP_XCLASS:
# Line 1827  for (;;) Line 2124  for (;;)
2124        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
2125          {          {
2126          if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);          if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2127          GETCHARINC(c, eptr);          GETCHARINCTEST(c, eptr);
2128          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);          if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);
2129          }          }
2130    
# Line 1843  for (;;) Line 2140  for (;;)
2140          {          {
2141          for (fi = min;; fi++)          for (fi = min;; fi++)
2142            {            {
2143            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM20);
2144            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2145            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2146            GETCHARINC(c, eptr);            GETCHARINCTEST(c, eptr);
2147            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);            if (!_pcre_xclass(c, data)) RRETURN(MATCH_NOMATCH);
2148            }            }
2149          /* Control never gets here */          /* Control never gets here */
# Line 1861  for (;;) Line 2158  for (;;)
2158            {            {
2159            int len = 1;            int len = 1;
2160            if (eptr >= md->end_subject) break;            if (eptr >= md->end_subject) break;
2161            GETCHARLEN(c, eptr, len);            GETCHARLENTEST(c, eptr, len);
2162            if (!_pcre_xclass(c, data)) break;            if (!_pcre_xclass(c, data)) break;
2163            eptr += len;            eptr += len;
2164            }            }
2165          for(;;)          for(;;)
2166            {            {
2167            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM21);
2168            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2169            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
2170            BACKCHAR(eptr)            if (utf8) BACKCHAR(eptr);
2171            }            }
2172          RRETURN(MATCH_NOMATCH);          RRETURN(MATCH_NOMATCH);
2173          }          }
# Line 1926  for (;;) Line 2223  for (;;)
2223    
2224        else        else
2225          {          {
2226          int dc;          unsigned int dc;
2227          GETCHARINC(dc, eptr);          GETCHARINC(dc, eptr);
2228          ecode += length;          ecode += length;
2229    
# Line 1936  for (;;) Line 2233  for (;;)
2233          if (fc != dc)          if (fc != dc)
2234            {            {
2235  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2236            if (dc != _pcre_ucp_othercase(fc))            if (dc != UCD_OTHERCASE(fc))
2237  #endif  #endif
2238              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
2239            }            }
# Line 1953  for (;;) Line 2250  for (;;)
2250        }        }
2251      break;      break;
2252    
2253      /* Match a single character repeatedly; different opcodes share code. */      /* Match a single character repeatedly. */
2254    
2255      case OP_EXACT:      case OP_EXACT:
2256      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
2257      ecode += 3;      ecode += 3;
2258      goto REPEATCHAR;      goto REPEATCHAR;
2259    
2260        case OP_POSUPTO:
2261        possessive = TRUE;
2262        /* Fall through */
2263    
2264      case OP_UPTO:      case OP_UPTO:
2265      case OP_MINUPTO:      case OP_MINUPTO:
2266      min = 0;      min = 0;
# Line 1968  for (;;) Line 2269  for (;;)
2269      ecode += 3;      ecode += 3;
2270      goto REPEATCHAR;      goto REPEATCHAR;
2271    
2272        case OP_POSSTAR:
2273        possessive = TRUE;
2274        min = 0;
2275        max = INT_MAX;
2276        ecode++;
2277        goto REPEATCHAR;
2278    
2279        case OP_POSPLUS:
2280        possessive = TRUE;
2281        min = 1;
2282        max = INT_MAX;
2283        ecode++;
2284        goto REPEATCHAR;
2285    
2286        case OP_POSQUERY:
2287        possessive = TRUE;
2288        min = 0;
2289        max = 1;
2290        ecode++;
2291        goto REPEATCHAR;
2292    
2293      case OP_STAR:      case OP_STAR:
2294      case OP_MINSTAR:      case OP_MINSTAR:
2295      case OP_PLUS:      case OP_PLUS:
# Line 1999  for (;;) Line 2321  for (;;)
2321    
2322        if (length > 1)        if (length > 1)
2323          {          {
         int oclength = 0;  
         uschar occhars[8];  
   
2324  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2325          int othercase;          unsigned int othercase;
2326          if ((ims & PCRE_CASELESS) != 0 &&          if ((ims & PCRE_CASELESS) != 0 &&
2327              (othercase = _pcre_ucp_othercase(fc)) >= 0 &&              (othercase = UCD_OTHERCASE(fc)) != fc)
              othercase >= 0)  
2328            oclength = _pcre_ord2utf8(othercase, occhars);            oclength = _pcre_ord2utf8(othercase, occhars);
2329            else oclength = 0;
2330  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2331    
2332          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2333            {            {
2334            if (memcmp(eptr, charptr, length) == 0) eptr += length;            if (memcmp(eptr, charptr, length) == 0) eptr += length;
2335    #ifdef SUPPORT_UCP
2336            /* Need braces because of following else */            /* Need braces because of following else */
2337            else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }            else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }
2338            else            else
# Line 2020  for (;;) Line 2340  for (;;)
2340              if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);              if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);
2341              eptr += oclength;              eptr += oclength;
2342              }              }
2343    #else   /* without SUPPORT_UCP */
2344              else { RRETURN(MATCH_NOMATCH); }
2345    #endif  /* SUPPORT_UCP */
2346            }            }
2347    
2348          if (min == max) continue;          if (min == max) continue;
# Line 2028  for (;;) Line 2351  for (;;)
2351            {            {
2352            for (fi = min;; fi++)            for (fi = min;; fi++)
2353              {              {
2354              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM22);
2355              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2356              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2357              if (memcmp(eptr, charptr, length) == 0) eptr += length;              if (memcmp(eptr, charptr, length) == 0) eptr += length;
2358    #ifdef SUPPORT_UCP
2359              /* Need braces because of following else */              /* Need braces because of following else */
2360              else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }              else if (oclength == 0) { RRETURN(MATCH_NOMATCH); }
2361              else              else
# Line 2039  for (;;) Line 2363  for (;;)
2363                if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);                if (memcmp(eptr, occhars, oclength) != 0) RRETURN(MATCH_NOMATCH);
2364                eptr += oclength;                eptr += oclength;
2365                }                }
2366    #else   /* without SUPPORT_UCP */
2367                else { RRETURN (MATCH_NOMATCH); }
2368    #endif  /* SUPPORT_UCP */
2369              }              }
2370            /* Control never gets here */            /* Control never gets here */
2371            }            }
2372          else  
2373            else  /* Maximize */
2374            {            {
2375            pp = eptr;            pp = eptr;
2376            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2377              {              {
2378              if (eptr > md->end_subject - length) break;              if (eptr > md->end_subject - length) break;
2379              if (memcmp(eptr, charptr, length) == 0) eptr += length;              if (memcmp(eptr, charptr, length) == 0) eptr += length;
2380    #ifdef SUPPORT_UCP
2381              else if (oclength == 0) break;              else if (oclength == 0) break;
2382              else              else
2383                {                {
2384                if (memcmp(eptr, occhars, oclength) != 0) break;                if (memcmp(eptr, occhars, oclength) != 0) break;
2385                eptr += oclength;                eptr += oclength;
2386                }                }
2387    #else   /* without SUPPORT_UCP */
2388                else break;
2389    #endif  /* SUPPORT_UCP */
2390              }              }
2391            while (eptr >= pp)  
2392              if (possessive) continue;
2393              for(;;)
2394             {             {
2395             RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);             RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM23);
2396             if (rrc != MATCH_NOMATCH) RRETURN(rrc);             if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2397               if (eptr == pp) RRETURN(MATCH_NOMATCH);
2398    #ifdef SUPPORT_UCP
2399               eptr--;
2400               BACKCHAR(eptr);
2401    #else   /* without SUPPORT_UCP */
2402             eptr -= length;             eptr -= length;
2403    #endif  /* SUPPORT_UCP */
2404             }             }
           RRETURN(MATCH_NOMATCH);  
2405            }            }
2406          /* Control never gets here */          /* Control never gets here */
2407          }          }
# Line 2102  for (;;) Line 2441  for (;;)
2441          {          {
2442          for (fi = min;; fi++)          for (fi = min;; fi++)
2443            {            {
2444            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM24);
2445            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2446            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max || eptr >= md->end_subject ||
2447                fc != md->lcc[*eptr++])                fc != md->lcc[*eptr++])
# Line 2110  for (;;) Line 2449  for (;;)
2449            }            }
2450          /* Control never gets here */          /* Control never gets here */
2451          }          }
2452        else        else  /* Maximize */
2453          {          {
2454          pp = eptr;          pp = eptr;
2455          for (i = min; i < max; i++)          for (i = min; i < max; i++)
# Line 2118  for (;;) Line 2457  for (;;)
2457            if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break;            if (eptr >= md->end_subject || fc != md->lcc[*eptr]) break;
2458            eptr++;            eptr++;
2459            }            }
2460            if (possessive) continue;
2461          while (eptr >= pp)          while (eptr >= pp)
2462            {            {
2463            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM25);
2464            eptr--;            eptr--;
2465            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2466            }            }
# Line 2139  for (;;) Line 2479  for (;;)
2479          {          {
2480          for (fi = min;; fi++)          for (fi = min;; fi++)
2481            {            {
2482            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM26);
2483            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2484            if (fi >= max || eptr >= md->end_subject || fc != *eptr++)            if (fi >= max || eptr >= md->end_subject || fc != *eptr++)
2485              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
2486            }            }
2487          /* Control never gets here */          /* Control never gets here */
2488          }          }
2489        else        else  /* Maximize */
2490          {          {
2491          pp = eptr;          pp = eptr;
2492          for (i = min; i < max; i++)          for (i = min; i < max; i++)
# Line 2154  for (;;) Line 2494  for (;;)
2494            if (eptr >= md->end_subject || fc != *eptr) break;            if (eptr >= md->end_subject || fc != *eptr) break;
2495            eptr++;            eptr++;
2496            }            }
2497            if (possessive) continue;
2498          while (eptr >= pp)          while (eptr >= pp)
2499            {            {
2500            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM27);
2501            eptr--;            eptr--;
2502            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2503            }            }
# Line 2206  for (;;) Line 2547  for (;;)
2547      ecode += 3;      ecode += 3;
2548      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
2549    
2550        case OP_NOTPOSSTAR:
2551        possessive = TRUE;
2552        min = 0;
2553        max = INT_MAX;
2554        ecode++;
2555        goto REPEATNOTCHAR;
2556    
2557        case OP_NOTPOSPLUS:
2558        possessive = TRUE;
2559        min = 1;
2560        max = INT_MAX;
2561        ecode++;
2562        goto REPEATNOTCHAR;
2563    
2564        case OP_NOTPOSQUERY:
2565        possessive = TRUE;
2566        min = 0;
2567        max = 1;
2568        ecode++;
2569        goto REPEATNOTCHAR;
2570    
2571        case OP_NOTPOSUPTO:
2572        possessive = TRUE;
2573        min = 0;
2574        max = GET2(ecode, 1);
2575        ecode += 3;
2576        goto REPEATNOTCHAR;
2577    
2578      case OP_NOTSTAR:      case OP_NOTSTAR:
2579      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
2580      case OP_NOTPLUS:      case OP_NOTPLUS:
# Line 2245  for (;;) Line 2614  for (;;)
2614        /* UTF-8 mode */        /* UTF-8 mode */
2615        if (utf8)        if (utf8)
2616          {          {
2617          register int d;          register unsigned int d;
2618          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2619            {            {
2620            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
# Line 2270  for (;;) Line 2639  for (;;)
2639          /* UTF-8 mode */          /* UTF-8 mode */
2640          if (utf8)          if (utf8)
2641            {            {
2642            register int d;            register unsigned int d;
2643            for (fi = min;; fi++)            for (fi = min;; fi++)
2644              {              {
2645              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM28);
2646              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2647                if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2648              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
2649              if (d < 256) d = md->lcc[d];              if (d < 256) d = md->lcc[d];
2650              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) RRETURN(MATCH_NOMATCH);
2651                RRETURN(MATCH_NOMATCH);  
2652              }              }
2653            }            }
2654          else          else
# Line 2287  for (;;) Line 2657  for (;;)
2657            {            {
2658            for (fi = min;; fi++)            for (fi = min;; fi++)
2659              {              {
2660              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM29);
2661              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2662              if (fi >= max || eptr >= md->end_subject || fc == md->lcc[*eptr++])              if (fi >= max || eptr >= md->end_subject || fc == md->lcc[*eptr++])
2663                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
# Line 2306  for (;;) Line 2676  for (;;)
2676          /* UTF-8 mode */          /* UTF-8 mode */
2677          if (utf8)          if (utf8)
2678            {            {
2679            register int d;            register unsigned int d;
2680            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2681              {              {
2682              int len = 1;              int len = 1;
# Line 2316  for (;;) Line 2686  for (;;)
2686              if (fc == d) break;              if (fc == d) break;
2687              eptr += len;              eptr += len;
2688              }              }
2689            for(;;)          if (possessive) continue;
2690            for(;;)
2691              {              {
2692              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM30);
2693              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2694              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2695              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2333  for (;;) Line 2704  for (;;)
2704              if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break;              if (eptr >= md->end_subject || fc == md->lcc[*eptr]) break;
2705              eptr++;              eptr++;
2706              }              }
2707              if (possessive) continue;
2708            while (eptr >= pp)            while (eptr >= pp)
2709              {              {
2710              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM31);
2711              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2712              eptr--;              eptr--;
2713              }              }
# Line 2354  for (;;) Line 2726  for (;;)
2726        /* UTF-8 mode */        /* UTF-8 mode */
2727        if (utf8)        if (utf8)
2728          {          {
2729          register int d;          register unsigned int d;
2730          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2731            {            {
2732            GETCHARINC(d, eptr);            GETCHARINC(d, eptr);
# Line 2377  for (;;) Line 2749  for (;;)
2749          /* UTF-8 mode */          /* UTF-8 mode */
2750          if (utf8)          if (utf8)
2751            {            {
2752            register int d;            register unsigned int d;
2753            for (fi = min;; fi++)            for (fi = min;; fi++)
2754              {              {
2755              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM32);
2756              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2757                if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2758              GETCHARINC(d, eptr);              GETCHARINC(d, eptr);
2759              if (fi >= max || eptr >= md->end_subject || fc == d)              if (fc == d) RRETURN(MATCH_NOMATCH);
               RRETURN(MATCH_NOMATCH);  
2760              }              }
2761            }            }
2762          else          else
# Line 2393  for (;;) Line 2765  for (;;)
2765            {            {
2766            for (fi = min;; fi++)            for (fi = min;; fi++)
2767              {              {
2768              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM33);
2769              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2770              if (fi >= max || eptr >= md->end_subject || fc == *eptr++)              if (fi >= max || eptr >= md->end_subject || fc == *eptr++)
2771                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
# Line 2412  for (;;) Line 2784  for (;;)
2784          /* UTF-8 mode */          /* UTF-8 mode */
2785          if (utf8)          if (utf8)
2786            {            {
2787            register int d;            register unsigned int d;
2788            for (i = min; i < max; i++)            for (i = min; i < max; i++)
2789              {              {
2790              int len = 1;              int len = 1;
# Line 2421  for (;;) Line 2793  for (;;)
2793              if (fc == d) break;              if (fc == d) break;
2794              eptr += len;              eptr += len;
2795              }              }
2796              if (possessive) continue;
2797            for(;;)            for(;;)
2798              {              {
2799              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM34);
2800              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2801              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2802              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2438  for (;;) Line 2811  for (;;)
2811              if (eptr >= md->end_subject || fc == *eptr) break;              if (eptr >= md->end_subject || fc == *eptr) break;
2812              eptr++;              eptr++;
2813              }              }
2814              if (possessive) continue;
2815            while (eptr >= pp)            while (eptr >= pp)
2816              {              {
2817              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM35);
2818              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2819              eptr--;              eptr--;
2820              }              }
# Line 2469  for (;;) Line 2843  for (;;)
2843      ecode += 3;      ecode += 3;
2844      goto REPEATTYPE;      goto REPEATTYPE;
2845    
2846        case OP_TYPEPOSSTAR:
2847        possessive = TRUE;
2848        min = 0;
2849        max = INT_MAX;
2850        ecode++;
2851        goto REPEATTYPE;
2852    
2853        case OP_TYPEPOSPLUS:
2854        possessive = TRUE;
2855        min = 1;
2856        max = INT_MAX;
2857        ecode++;
2858        goto REPEATTYPE;
2859    
2860        case OP_TYPEPOSQUERY:
2861        possessive = TRUE;
2862        min = 0;
2863        max = 1;
2864        ecode++;
2865        goto REPEATTYPE;
2866    
2867        case OP_TYPEPOSUPTO:
2868        possessive = TRUE;
2869        min = 0;
2870        max = GET2(ecode, 1);
2871        ecode += 3;
2872        goto REPEATTYPE;
2873    
2874      case OP_TYPESTAR:      case OP_TYPESTAR:
2875      case OP_TYPEMINSTAR:      case OP_TYPEMINSTAR:
2876      case OP_TYPEPLUS:      case OP_TYPEPLUS:
# Line 2519  for (;;) Line 2921  for (;;)
2921            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2922              {              {
2923              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2924              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2925              }              }
2926            break;            break;
2927    
# Line 2527  for (;;) Line 2929  for (;;)
2929            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2930              {              {
2931              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2932              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2933              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
2934              if ((prop_chartype == ucp_Lu ||              if ((prop_chartype == ucp_Lu ||
2935                   prop_chartype == ucp_Ll ||                   prop_chartype == ucp_Ll ||
2936                   prop_chartype == ucp_Lt) == prop_fail_result)                   prop_chartype == ucp_Lt) == prop_fail_result)
# Line 2540  for (;;) Line 2942  for (;;)
2942            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2943              {              {
2944              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2945              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2946              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
2947              if ((prop_category == prop_value) == prop_fail_result)              if ((prop_category == prop_value) == prop_fail_result)
2948                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
2949              }              }
# Line 2551  for (;;) Line 2953  for (;;)
2953            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2954              {              {
2955              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2956              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2957              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
2958              if ((prop_chartype == prop_value) == prop_fail_result)              if ((prop_chartype == prop_value) == prop_fail_result)
2959                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
2960              }              }
# Line 2562  for (;;) Line 2964  for (;;)
2964            for (i = 1; i <= min; i++)            for (i = 1; i <= min; i++)
2965              {              {
2966              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
2967              GETCHARINC(c, eptr);              GETCHARINCTEST(c, eptr);
2968              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_script = UCD_SCRIPT(c);
2969              if ((prop_script == prop_value) == prop_fail_result)              if ((prop_script == prop_value) == prop_fail_result)
2970                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
2971              }              }
# Line 2571  for (;;) Line 2973  for (;;)
2973    
2974            default:            default:
2975            RRETURN(PCRE_ERROR_INTERNAL);            RRETURN(PCRE_ERROR_INTERNAL);
           break;  
2976            }            }
2977          }          }
2978    
# Line 2583  for (;;) Line 2984  for (;;)
2984          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
2985            {            {
2986            GETCHARINCTEST(c, eptr);            GETCHARINCTEST(c, eptr);
2987            prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);            prop_category = UCD_CATEGORY(c);
2988            if (prop_category == ucp_M) RRETURN(MATCH_NOMATCH);            if (prop_category == ucp_M) RRETURN(MATCH_NOMATCH);
2989            while (eptr < md->end_subject)            while (eptr < md->end_subject)
2990              {              {
# Line 2592  for (;;) Line 2993  for (;;)
2993                {                {
2994                GETCHARLEN(c, eptr, len);                GETCHARLEN(c, eptr, len);
2995                }                }
2996              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
2997              if (prop_category != ucp_M) break;              if (prop_category != ucp_M) break;
2998              eptr += len;              eptr += len;
2999              }              }
# Line 2610  for (;;) Line 3011  for (;;)
3011          case OP_ANY:          case OP_ANY:
3012          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3013            {            {
3014            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)))  
3015              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3016            eptr++;            eptr++;
3017            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
3018            }            }
3019          break;          break;
3020    
3021          case OP_ANYBYTE:          case OP_ALLANY:
3022            for (i = 1; i <= min; i++)
3023              {
3024              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3025              eptr++;
3026              while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
3027              }
3028            break;
3029    
3030            case OP_ANYBYTE:
3031          eptr += min;          eptr += min;
3032          break;          break;
3033    
3034            case OP_ANYNL:
3035            for (i = 1; i <= min; i++)
3036              {
3037              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3038              GETCHARINC(c, eptr);
3039              switch(c)
3040                {
3041                default: RRETURN(MATCH_NOMATCH);
3042                case 0x000d:
3043                if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3044                break;
3045    
3046                case 0x000a:
3047                break;
3048    
3049                case 0x000b:
3050                case 0x000c:
3051                case 0x0085:
3052                case 0x2028:
3053                case 0x2029:
3054                if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
3055                break;
3056                }
3057              }
3058            break;
3059    
3060            case OP_NOT_HSPACE:
3061            for (i = 1; i <= min; i++)
3062              {
3063              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3064              GETCHARINC(c, eptr);
3065              switch(c)
3066                {
3067                default: break;
3068                case 0x09:      /* HT */
3069                case 0x20:      /* SPACE */
3070                case 0xa0:      /* NBSP */
3071                case 0x1680:    /* OGHAM SPACE MARK */
3072                case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
3073                case 0x2000:    /* EN QUAD */
3074                case 0x2001:    /* EM QUAD */
3075                case 0x2002:    /* EN SPACE */
3076                case 0x2003:    /* EM SPACE */
3077                case 0x2004:    /* THREE-PER-EM SPACE */
3078                case 0x2005:    /* FOUR-PER-EM SPACE */
3079                case 0x2006:    /* SIX-PER-EM SPACE */
3080                case 0x2007:    /* FIGURE SPACE */
3081                case 0x2008:    /* PUNCTUATION SPACE */
3082                case 0x2009:    /* THIN SPACE */
3083                case 0x200A:    /* HAIR SPACE */
3084                case 0x202f:    /* NARROW NO-BREAK SPACE */
3085                case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
3086                case 0x3000:    /* IDEOGRAPHIC SPACE */
3087                RRETURN(MATCH_NOMATCH);
3088                }
3089              }
3090            break;
3091    
3092            case OP_HSPACE:
3093            for (i = 1; i <= min; i++)
3094              {
3095              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3096              GETCHARINC(c, eptr);
3097              switch(c)
3098                {
3099                default: RRETURN(MATCH_NOMATCH);
3100                case 0x09:      /* HT */
3101                case 0x20:      /* SPACE */
3102                case 0xa0:      /* NBSP */
3103                case 0x1680:    /* OGHAM SPACE MARK */
3104                case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
3105                case 0x2000:    /* EN QUAD */
3106                case 0x2001:    /* EM QUAD */
3107                case 0x2002:    /* EN SPACE */
3108                case 0x2003:    /* EM SPACE */
3109                case 0x2004:    /* THREE-PER-EM SPACE */
3110                case 0x2005:    /* FOUR-PER-EM SPACE */
3111                case 0x2006:    /* SIX-PER-EM SPACE */
3112                case 0x2007:    /* FIGURE SPACE */
3113                case 0x2008:    /* PUNCTUATION SPACE */
3114                case 0x2009:    /* THIN SPACE */
3115                case 0x200A:    /* HAIR SPACE */
3116                case 0x202f:    /* NARROW NO-BREAK SPACE */
3117                case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
3118                case 0x3000:    /* IDEOGRAPHIC SPACE */
3119                break;
3120                }
3121              }
3122            break;
3123    
3124            case OP_NOT_VSPACE:
3125            for (i = 1; i <= min; i++)
3126              {
3127              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3128              GETCHARINC(c, eptr);
3129              switch(c)
3130                {
3131                default: break;
3132                case 0x0a:      /* LF */
3133                case 0x0b:      /* VT */
3134                case 0x0c:      /* FF */
3135                case 0x0d:      /* CR */
3136                case 0x85:      /* NEL */
3137                case 0x2028:    /* LINE SEPARATOR */
3138                case 0x2029:    /* PARAGRAPH SEPARATOR */
3139                RRETURN(MATCH_NOMATCH);
3140                }
3141              }
3142            break;
3143    
3144            case OP_VSPACE:
3145            for (i = 1; i <= min; i++)
3146              {
3147              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3148              GETCHARINC(c, eptr);
3149              switch(c)
3150                {
3151                default: RRETURN(MATCH_NOMATCH);
3152                case 0x0a:      /* LF */
3153                case 0x0b:      /* VT */
3154                case 0x0c:      /* FF */
3155                case 0x0d:      /* CR */
3156                case 0x85:      /* NEL */
3157                case 0x2028:    /* LINE SEPARATOR */
3158                case 0x2029:    /* PARAGRAPH SEPARATOR */
3159                break;
3160                }
3161              }
3162            break;
3163    
3164          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
3165          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3166            {            {
# Line 2648  for (;;) Line 3185  for (;;)
3185          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3186            {            {
3187            if (eptr >= md->end_subject ||            if (eptr >= md->end_subject ||
3188               (*eptr < 128 && (md->ctypes[*eptr++] & ctype_space) != 0))               (*eptr < 128 && (md->ctypes[*eptr] & ctype_space) != 0))
3189              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3190            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;            while (++eptr < md->end_subject && (*eptr & 0xc0) == 0x80);
3191            }            }
3192          break;          break;
3193    
# Line 2668  for (;;) Line 3205  for (;;)
3205          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3206            {            {
3207            if (eptr >= md->end_subject ||            if (eptr >= md->end_subject ||
3208               (*eptr < 128 && (md->ctypes[*eptr++] & ctype_word) != 0))               (*eptr < 128 && (md->ctypes[*eptr] & ctype_word) != 0))
3209              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3210            while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;            while (++eptr < md->end_subject && (*eptr & 0xc0) == 0x80);
3211            }            }
3212          break;          break;
3213    
# Line 2692  for (;;) Line 3229  for (;;)
3229  #endif     /* SUPPORT_UTF8 */  #endif     /* SUPPORT_UTF8 */
3230    
3231        /* 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
3232        than OP_PROP and OP_NOTPROP. */        than OP_PROP and OP_NOTPROP. We can assume that there are the minimum
3233          number of bytes present, as this was tested above. */
3234    
3235        switch(ctype)        switch(ctype)
3236          {          {
3237          case OP_ANY:          case OP_ANY:
3238          if ((ims & PCRE_DOTALL) == 0)          for (i = 1; i <= min; i++)
3239            {            {
3240            for (i = 1; i <= min; i++)            if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
3241              {            eptr++;
             if (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr))  
               RRETURN(MATCH_NOMATCH);  
             eptr++;  
             }  
3242            }            }
3243          else eptr += min;          break;
3244    
3245            case OP_ALLANY:
3246            eptr += min;
3247          break;          break;
3248    
3249          case OP_ANYBYTE:          case OP_ANYBYTE:
3250          eptr += min;          eptr += min;
3251          break;          break;
3252    
3253            /* Because of the CRLF case, we can't assume the minimum number of
3254            bytes are present in this case. */
3255    
3256            case OP_ANYNL:
3257            for (i = 1; i <= min; i++)
3258              {
3259              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3260              switch(*eptr++)
3261                {
3262                default: RRETURN(MATCH_NOMATCH);
3263                case 0x000d:
3264                if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3265                break;
3266                case 0x000a:
3267                break;
3268    
3269                case 0x000b:
3270                case 0x000c:
3271                case 0x0085:
3272                if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
3273                break;
3274                }
3275              }
3276            break;
3277    
3278            case OP_NOT_HSPACE:
3279            for (i = 1; i <= min; i++)
3280              {
3281              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3282              switch(*eptr++)
3283                {
3284                default: break;
3285                case 0x09:      /* HT */
3286                case 0x20:      /* SPACE */
3287                case 0xa0:      /* NBSP */
3288                RRETURN(MATCH_NOMATCH);
3289                }
3290              }
3291            break;
3292    
3293            case OP_HSPACE:
3294            for (i = 1; i <= min; i++)
3295              {
3296              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3297              switch(*eptr++)
3298                {
3299                default: RRETURN(MATCH_NOMATCH);
3300                case 0x09:      /* HT */
3301                case 0x20:      /* SPACE */
3302                case 0xa0:      /* NBSP */
3303                break;
3304                }
3305              }
3306            break;
3307    
3308            case OP_NOT_VSPACE:
3309            for (i = 1; i <= min; i++)
3310              {
3311              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3312              switch(*eptr++)
3313                {
3314                default: break;
3315                case 0x0a:      /* LF */
3316                case 0x0b:      /* VT */
3317                case 0x0c:      /* FF */
3318                case 0x0d:      /* CR */
3319                case 0x85:      /* NEL */
3320                RRETURN(MATCH_NOMATCH);
3321                }
3322              }
3323            break;
3324    
3325            case OP_VSPACE:
3326            for (i = 1; i <= min; i++)
3327              {
3328              if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3329              switch(*eptr++)
3330                {
3331                default: RRETURN(MATCH_NOMATCH);
3332                case 0x0a:      /* LF */
3333                case 0x0b:      /* VT */
3334                case 0x0c:      /* FF */
3335                case 0x0d:      /* CR */
3336                case 0x85:      /* NEL */
3337                break;
3338                }
3339              }
3340            break;
3341    
3342          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
3343          for (i = 1; i <= min; i++)          for (i = 1; i <= min; i++)
3344            if ((md->ctypes[*eptr++] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH);            if ((md->ctypes[*eptr++] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH);
# Line 2768  for (;;) Line 3394  for (;;)
3394            case PT_ANY:            case PT_ANY:
3395            for (fi = min;; fi++)            for (fi = min;; fi++)
3396              {              {
3397              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM36);
3398              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3399              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3400              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3401              if (prop_fail_result) RRETURN(MATCH_NOMATCH);              if (prop_fail_result) RRETURN(MATCH_NOMATCH);
3402              }              }
3403            break;            /* Control never gets here */
3404    
3405            case PT_LAMP:            case PT_LAMP:
3406            for (fi = min;; fi++)            for (fi = min;; fi++)
3407              {              {
3408              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM37);
3409              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3410              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3411              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3412              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
3413              if ((prop_chartype == ucp_Lu ||              if ((prop_chartype == ucp_Lu ||
3414                   prop_chartype == ucp_Ll ||                   prop_chartype == ucp_Ll ||
3415                   prop_chartype == ucp_Lt) == prop_fail_result)                   prop_chartype == ucp_Lt) == prop_fail_result)
3416                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3417              }              }
3418            break;            /* Control never gets here */
3419    
3420            case PT_GC:            case PT_GC:
3421            for (fi = min;; fi++)            for (fi = min;; fi++)
3422              {              {
3423              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM38);
3424              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3425              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3426              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3427              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3428              if ((prop_category == prop_value) == prop_fail_result)              if ((prop_category == prop_value) == prop_fail_result)
3429                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3430              }              }
3431            break;            /* Control never gets here */
3432    
3433            case PT_PC:            case PT_PC:
3434            for (fi = min;; fi++)            for (fi = min;; fi++)
3435              {              {
3436              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM39);
3437              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3438              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3439              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3440              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
3441              if ((prop_chartype == prop_value) == prop_fail_result)              if ((prop_chartype == prop_value) == prop_fail_result)
3442                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3443              }              }
3444            break;            /* Control never gets here */
3445    
3446            case PT_SC:            case PT_SC:
3447            for (fi = min;; fi++)            for (fi = min;; fi++)
3448              {              {
3449              RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM40);
3450              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3451              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);              if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3452              GETCHARINC(c, eptr);              GETCHARINC(c, eptr);
3453              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_script = UCD_SCRIPT(c);
3454              if ((prop_script == prop_value) == prop_fail_result)              if ((prop_script == prop_value) == prop_fail_result)
3455                RRETURN(MATCH_NOMATCH);                RRETURN(MATCH_NOMATCH);
3456              }              }
3457            break;            /* Control never gets here */
3458    
3459            default:            default:
3460            RRETURN(PCRE_ERROR_INTERNAL);            RRETURN(PCRE_ERROR_INTERNAL);
           break;  
3461            }            }
3462          }          }
3463    
# Line 2843  for (;;) Line 3468  for (;;)
3468          {          {
3469          for (fi = min;; fi++)          for (fi = min;; fi++)
3470            {            {
3471            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM41);
3472            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3473            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);            if (fi >= max || eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
3474            GETCHARINCTEST(c, eptr);            GETCHARINCTEST(c, eptr);
3475            prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);            prop_category = UCD_CATEGORY(c);
3476            if (prop_category == ucp_M) RRETURN(MATCH_NOMATCH);            if (prop_category == ucp_M) RRETURN(MATCH_NOMATCH);
3477            while (eptr < md->end_subject)            while (eptr < md->end_subject)
3478              {              {
# Line 2856  for (;;) Line 3481  for (;;)
3481                {                {
3482                GETCHARLEN(c, eptr, len);                GETCHARLEN(c, eptr, len);
3483                }                }
3484              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3485              if (prop_category != ucp_M) break;              if (prop_category != ucp_M) break;
3486              eptr += len;              eptr += len;
3487              }              }
# Line 2872  for (;;) Line 3497  for (;;)
3497          {          {
3498          for (fi = min;; fi++)          for (fi = min;; fi++)
3499            {            {
3500            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM42);
3501            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3502            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max || eptr >= md->end_subject ||
3503                 (ctype == OP_ANY && (ims & PCRE_DOTALL) == 0 &&                 (ctype == OP_ANY && IS_NEWLINE(eptr)))
                 eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)))  
3504              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3505    
3506            GETCHARINC(c, eptr);            GETCHARINC(c, eptr);
3507            switch(ctype)            switch(ctype)
3508              {              {
3509              case OP_ANY:        /* This is the DOTALL case */              case OP_ANY:        /* This is the non-NL case */
3510                case OP_ALLANY:
3511                case OP_ANYBYTE:
3512              break;              break;
3513    
3514              case OP_ANYBYTE:              case OP_ANYNL:
3515                switch(c)
3516                  {
3517                  default: RRETURN(MATCH_NOMATCH);
3518                  case 0x000d:
3519                  if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3520                  break;
3521                  case 0x000a:
3522                  break;
3523    
3524                  case 0x000b:
3525                  case 0x000c:
3526                  case 0x0085:
3527                  case 0x2028:
3528                  case 0x2029:
3529                  if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
3530                  break;
3531                  }
3532                break;
3533    
3534                case OP_NOT_HSPACE:
3535                switch(c)
3536                  {
3537                  default: break;
3538                  case 0x09:      /* HT */
3539                  case 0x20:      /* SPACE */
3540                  case 0xa0:      /* NBSP */
3541                  case 0x1680:    /* OGHAM SPACE MARK */
3542                  case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
3543                  case 0x2000:    /* EN QUAD */
3544                  case 0x2001:    /* EM QUAD */
3545                  case 0x2002:    /* EN SPACE */
3546                  case 0x2003:    /* EM SPACE */
3547                  case 0x2004:    /* THREE-PER-EM SPACE */
3548                  case 0x2005:    /* FOUR-PER-EM SPACE */
3549                  case 0x2006:    /* SIX-PER-EM SPACE */
3550                  case 0x2007:    /* FIGURE SPACE */
3551                  case 0x2008:    /* PUNCTUATION SPACE */
3552                  case 0x2009:    /* THIN SPACE */
3553                  case 0x200A:    /* HAIR SPACE */
3554                  case 0x202f:    /* NARROW NO-BREAK SPACE */
3555                  case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
3556                  case 0x3000:    /* IDEOGRAPHIC SPACE */
3557                  RRETURN(MATCH_NOMATCH);
3558                  }
3559                break;
3560    
3561                case OP_HSPACE:
3562                switch(c)
3563                  {
3564                  default: RRETURN(MATCH_NOMATCH);
3565                  case 0x09:      /* HT */
3566                  case 0x20:      /* SPACE */
3567                  case 0xa0:      /* NBSP */
3568                  case 0x1680:    /* OGHAM SPACE MARK */
3569                  case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
3570                  case 0x2000:    /* EN QUAD */
3571                  case 0x2001:    /* EM QUAD */
3572                  case 0x2002:    /* EN SPACE */
3573                  case 0x2003:    /* EM SPACE */
3574                  case 0x2004:    /* THREE-PER-EM SPACE */
3575                  case 0x2005:    /* FOUR-PER-EM SPACE */
3576                  case 0x2006:    /* SIX-PER-EM SPACE */
3577                  case 0x2007:    /* FIGURE SPACE */
3578                  case 0x2008:    /* PUNCTUATION SPACE */
3579                  case 0x2009:    /* THIN SPACE */
3580                  case 0x200A:    /* HAIR SPACE */
3581                  case 0x202f:    /* NARROW NO-BREAK SPACE */
3582                  case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
3583                  case 0x3000:    /* IDEOGRAPHIC SPACE */
3584                  break;
3585                  }
3586                break;
3587    
3588                case OP_NOT_VSPACE:
3589                switch(c)
3590                  {
3591                  default: break;
3592                  case 0x0a:      /* LF */
3593                  case 0x0b:      /* VT */
3594                  case 0x0c:      /* FF */
3595                  case 0x0d:      /* CR */
3596                  case 0x85:      /* NEL */
3597                  case 0x2028:    /* LINE SEPARATOR */
3598                  case 0x2029:    /* PARAGRAPH SEPARATOR */
3599                  RRETURN(MATCH_NOMATCH);
3600                  }
3601                break;
3602    
3603                case OP_VSPACE:
3604                switch(c)
3605                  {
3606                  default: RRETURN(MATCH_NOMATCH);
3607                  case 0x0a:      /* LF */
3608                  case 0x0b:      /* VT */
3609                  case 0x0c:      /* FF */
3610                  case 0x0d:      /* CR */
3611                  case 0x85:      /* NEL */
3612                  case 0x2028:    /* LINE SEPARATOR */
3613                  case 0x2029:    /* PARAGRAPH SEPARATOR */
3614                  break;
3615                  }
3616              break;              break;
3617    
3618              case OP_NOT_DIGIT:              case OP_NOT_DIGIT:
# Line 2929  for (;;) Line 3656  for (;;)
3656          {          {
3657          for (fi = min;; fi++)          for (fi = min;; fi++)
3658            {            {
3659            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM43);
3660            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3661            if (fi >= max || eptr >= md->end_subject ||            if (fi >= max || eptr >= md->end_subject ||
3662                 ((ims & PCRE_DOTALL) == 0 &&                 (ctype == OP_ANY && IS_NEWLINE(eptr)))
                  eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)))  
3663              RRETURN(MATCH_NOMATCH);              RRETURN(MATCH_NOMATCH);
3664    
3665            c = *eptr++;            c = *eptr++;
3666            switch(ctype)            switch(ctype)
3667              {              {
3668              case OP_ANY:   /* This is the DOTALL case */              case OP_ANY:     /* This is the non-NL case */
3669                case OP_ALLANY:
3670                case OP_ANYBYTE:
3671              break;              break;
3672    
3673              case OP_ANYBYTE:              case OP_ANYNL:
3674                switch(c)
3675                  {
3676                  default: RRETURN(MATCH_NOMATCH);
3677                  case 0x000d:
3678                  if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3679                  break;
3680    
3681                  case 0x000a:
3682                  break;
3683    
3684                  case 0x000b:
3685                  case 0x000c:
3686                  case 0x0085:
3687                  if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
3688                  break;
3689                  }
3690                break;
3691    
3692                case OP_NOT_HSPACE:
3693                switch(c)
3694                  {
3695                  default: break;
3696                  case 0x09:      /* HT */
3697                  case 0x20:      /* SPACE */
3698                  case 0xa0:      /* NBSP */
3699                  RRETURN(MATCH_NOMATCH);
3700                  }
3701                break;
3702    
3703                case OP_HSPACE:
3704                switch(c)
3705                  {
3706                  default: RRETURN(MATCH_NOMATCH);
3707                  case 0x09:      /* HT */
3708                  case 0x20:      /* SPACE */
3709                  case 0xa0:      /* NBSP */
3710                  break;
3711                  }
3712                break;
3713    
3714                case OP_NOT_VSPACE:
3715                switch(c)
3716                  {
3717                  default: break;
3718                  case 0x0a:      /* LF */
3719                  case 0x0b:      /* VT */
3720                  case 0x0c:      /* FF */
3721                  case 0x0d:      /* CR */
3722                  case 0x85:      /* NEL */
3723                  RRETURN(MATCH_NOMATCH);
3724                  }
3725                break;
3726    
3727                case OP_VSPACE:
3728                switch(c)
3729                  {
3730                  default: RRETURN(MATCH_NOMATCH);
3731                  case 0x0a:      /* LF */
3732                  case 0x0b:      /* VT */
3733                  case 0x0c:      /* FF */
3734                  case 0x0d:      /* CR */
3735                  case 0x85:      /* NEL */
3736                  break;
3737                  }
3738              break;              break;
3739    
3740              case OP_NOT_DIGIT:              case OP_NOT_DIGIT:
# Line 2977  for (;;) Line 3769  for (;;)
3769        /* Control never gets here */        /* Control never gets here */
3770        }        }
3771    
3772      /* 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
3773      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
3774      UTF-8 and UCP stuff separate. */      UTF-8 and UCP stuff separate. */
3775    
# Line 3007  for (;;) Line 3799  for (;;)
3799              int len = 1;              int len = 1;
3800              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject) break;
3801              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
3802              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
3803              if ((prop_chartype == ucp_Lu ||              if ((prop_chartype == ucp_Lu ||
3804                   prop_chartype == ucp_Ll ||                   prop_chartype == ucp_Ll ||
3805                   prop_chartype == ucp_Lt) == prop_fail_result)                   prop_chartype == ucp_Lt) == prop_fail_result)
# Line 3022  for (;;) Line 3814  for (;;)
3814              int len = 1;              int len = 1;
3815              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject) break;
3816              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
3817              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3818              if ((prop_category == prop_value) == prop_fail_result)              if ((prop_category == prop_value) == prop_fail_result)
3819                break;                break;
3820              eptr+= len;              eptr+= len;
# Line 3035  for (;;) Line 3827  for (;;)
3827              int len = 1;              int len = 1;
3828              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject) break;
3829              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
3830              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_chartype = UCD_CHARTYPE(c);
3831              if ((prop_chartype == prop_value) == prop_fail_result)              if ((prop_chartype == prop_value) == prop_fail_result)
3832                break;                break;
3833              eptr+= len;              eptr+= len;
# Line 3048  for (;;) Line 3840  for (;;)
3840              int len = 1;              int len = 1;
3841              if (eptr >= md->end_subject) break;              if (eptr >= md->end_subject) break;
3842              GETCHARLEN(c, eptr, len);              GETCHARLEN(c, eptr, len);
3843              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_script = UCD_SCRIPT(c);
3844              if ((prop_script == prop_value) == prop_fail_result)              if ((prop_script == prop_value) == prop_fail_result)
3845                break;                break;
3846              eptr+= len;              eptr+= len;
# Line 3058  for (;;) Line 3850  for (;;)
3850    
3851          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run */
3852    
3853            if (possessive) continue;
3854          for(;;)          for(;;)
3855            {            {
3856            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM44);
3857            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3858            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
3859            BACKCHAR(eptr);            if (utf8) BACKCHAR(eptr);
3860            }            }
3861          }          }
3862    
# Line 3076  for (;;) Line 3869  for (;;)
3869            {            {
3870            if (eptr >= md->end_subject) break;            if (eptr >= md->end_subject) break;
3871            GETCHARINCTEST(c, eptr);            GETCHARINCTEST(c, eptr);
3872            prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);            prop_category = UCD_CATEGORY(c);
3873            if (prop_category == ucp_M) break;            if (prop_category == ucp_M) break;
3874            while (eptr < md->end_subject)            while (eptr < md->end_subject)
3875              {              {
# Line 3085  for (;;) Line 3878  for (;;)
3878                {                {
3879                GETCHARLEN(c, eptr, len);                GETCHARLEN(c, eptr, len);
3880                }                }
3881              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3882              if (prop_category != ucp_M) break;              if (prop_category != ucp_M) break;
3883              eptr += len;              eptr += len;
3884              }              }
# Line 3093  for (;;) Line 3886  for (;;)
3886    
3887          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run */
3888    
3889            if (possessive) continue;
3890          for(;;)          for(;;)
3891            {            {
3892            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM45);
3893            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3894            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
3895            for (;;)                        /* Move back over one extended */            for (;;)                        /* Move back over one extended */
3896              {              {
3897              int len = 1;              int len = 1;
             BACKCHAR(eptr);  
3898              if (!utf8) c = *eptr; else              if (!utf8) c = *eptr; else
3899                {                {
3900                  BACKCHAR(eptr);
3901                GETCHARLEN(c, eptr, len);                GETCHARLEN(c, eptr, len);
3902                }                }
3903              prop_category = _pcre_ucp_findprop(c, &prop_chartype, &prop_script);              prop_category = UCD_CATEGORY(c);
3904              if (prop_category != ucp_M) break;              if (prop_category != ucp_M) break;
3905              eptr--;              eptr--;
3906              }              }
# Line 3124  for (;;) Line 3918  for (;;)
3918          switch(ctype)          switch(ctype)
3919            {            {
3920            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. */  
   
3921            if (max < INT_MAX)            if (max < INT_MAX)
3922              {              {
3923              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  
3924                {                {
3925                for (i = min; i < max; i++)                if (eptr >= md->end_subject || IS_NEWLINE(eptr)) break;
3926                  {                eptr++;
3927                  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++;  
                 }  
3928                }                }
3929              }              }
3930    
# Line 3157  for (;;) Line 3932  for (;;)
3932    
3933            else            else
3934              {              {
3935              if ((ims & PCRE_DOTALL) == 0)              for (i = min; i < max; i++)
3936                {                {
3937                for (i = min; i < max; i++)                if (eptr >= md->end_subject || IS_NEWLINE(eptr)) break;
3938                  {                eptr++;
3939                  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;  
3940                }                }
3941              else              }
3942              break;
3943    
3944              case OP_ALLANY:
3945              if (max < INT_MAX)
3946                {
3947                for (i = min; i < max; i++)
3948                {                {
3949                c = max - min;                if (eptr >= md->end_subject) break;
3950                if (c > md->end_subject - eptr) c = md->end_subject - eptr;                eptr++;
3951                eptr += c;                while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
3952                }                }
3953              }              }
3954              else eptr = md->end_subject;   /* Unlimited UTF-8 repeat */
3955            break;            break;
3956    
3957            /* The byte case is the same as non-UTF8 */            /* The byte case is the same as non-UTF8 */
3958    
3959            case OP_ANYBYTE:            case OP_ANYBYTE:
3960            c = max - min;            c = max - min;
3961            if (c > md->end_subject - eptr) c = md->end_subject - eptr;            if (c > (unsigned int)(md->end_subject - eptr))
3962                c = md->end_subject - eptr;
3963            eptr += c;            eptr += c;
3964            break;            break;
3965    
3966              case OP_ANYNL:
3967              for (i = min; i < max; i++)
3968                {
3969                int len = 1;
3970                if (eptr >= md->end_subject) break;
3971                GETCHARLEN(c, eptr, len);
3972                if (c == 0x000d)
3973                  {
3974                  if (++eptr >= md->end_subject) break;
3975                  if (*eptr == 0x000a) eptr++;
3976                  }
3977                else
3978                  {
3979                  if (c != 0x000a &&
3980                      (md->bsr_anycrlf ||
3981                       (c != 0x000b && c != 0x000c &&
3982                        c != 0x0085 && c != 0x2028 && c != 0x2029)))
3983                    break;
3984                  eptr += len;
3985                  }
3986                }
3987              break;
3988    
3989              case OP_NOT_HSPACE:
3990              case OP_HSPACE:
3991              for (i = min; i < max; i++)
3992                {
3993                BOOL gotspace;
3994                int len = 1;
3995                if (eptr >= md->end_subject) break;
3996                GETCHARLEN(c, eptr, len);
3997                switch(c)
3998                  {
3999                  default: gotspace = FALSE; break;
4000                  case 0x09:      /* HT */
4001                  case 0x20:      /* SPACE */
4002                  case 0xa0:      /* NBSP */
4003                  case 0x1680:    /* OGHAM SPACE MARK */
4004                  case 0x180e:    /* MONGOLIAN VOWEL SEPARATOR */
4005                  case 0x2000:    /* EN QUAD */
4006                  case 0x2001:    /* EM QUAD */
4007                  case 0x2002:    /* EN SPACE */
4008                  case 0x2003:    /* EM SPACE */
4009                  case 0x2004:    /* THREE-PER-EM SPACE */
4010                  case 0x2005:    /* FOUR-PER-EM SPACE */
4011                  case 0x2006:    /* SIX-PER-EM SPACE */
4012                  case 0x2007:    /* FIGURE SPACE */
4013                  case 0x2008:    /* PUNCTUATION SPACE */
4014                  case 0x2009:    /* THIN SPACE */
4015                  case 0x200A:    /* HAIR SPACE */
4016                  case 0x202f:    /* NARROW NO-BREAK SPACE */
4017                  case 0x205f:    /* MEDIUM MATHEMATICAL SPACE */
4018                  case 0x3000:    /* IDEOGRAPHIC SPACE */
4019                  gotspace = TRUE;
4020                  break;
4021                  }
4022                if (gotspace == (ctype == OP_NOT_HSPACE)) break;
4023                eptr += len;
4024                }
4025              break;
4026    
4027              case OP_NOT_VSPACE:
4028              case OP_VSPACE:
4029              for (i = min; i < max; i++)
4030                {
4031                BOOL gotspace;
4032                int len = 1;
4033                if (eptr >= md->end_subject) break;
4034                GETCHARLEN(c, eptr, len);
4035                switch(c)
4036                  {
4037                  default: gotspace = FALSE; break;
4038                  case 0x0a:      /* LF */
4039                  case 0x0b:      /* VT */
4040                  case 0x0c:      /* FF */
4041                  case 0x0d:      /* CR */
4042                  case 0x85:      /* NEL */
4043                  case 0x2028:    /* LINE SEPARATOR */
4044                  case 0x2029:    /* PARAGRAPH SEPARATOR */
4045                  gotspace = TRUE;
4046                  break;
4047                  }
4048                if (gotspace == (ctype == OP_NOT_VSPACE)) break;
4049                eptr += len;
4050                }
4051              break;
4052    
4053            case OP_NOT_DIGIT:            case OP_NOT_DIGIT:
4054            for (i = min; i < max; i++)            for (i = min; i < max; i++)
4055              {              {
# Line 3257  for (;;) Line 4122  for (;;)
4122    
4123          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run */
4124    
4125            if (possessive) continue;
4126          for(;;)          for(;;)
4127            {            {
4128            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM46);
4129            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4130            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
4131            BACKCHAR(eptr);            BACKCHAR(eptr);
4132            }            }
4133          }          }
4134        else        else
4135  #endif  #endif  /* SUPPORT_UTF8 */
4136    
4137        /* Not UTF-8 mode */        /* Not UTF-8 mode */
4138          {          {
4139          switch(ctype)          switch(ctype)
4140            {            {
4141            case OP_ANY:            case OP_ANY:
4142            if ((ims & PCRE_DOTALL) == 0)            for (i = min; i < max; i++)
4143              {              {
4144              for (i = min; i < max; i++)              if (eptr >= md->end_subject || IS_NEWLINE(eptr)) break;
4145                eptr++;
4146                }
4147              break;
4148    
4149              case OP_ALLANY:
4150              case OP_ANYBYTE:
4151              c = max - min;
4152              if (c > (unsigned int)(md->end_subject - eptr))
4153                c = md->end_subject - eptr;
4154              eptr += c;
4155              break;
4156    
4157              case OP_ANYNL:
4158              for (i = min; i < max; i++)
4159                {
4160                if (eptr >= md->end_subject) break;
4161                c = *eptr;
4162                if (c == 0x000d)
4163                  {
4164                  if (++eptr >= md->end_subject) break;
4165                  if (*eptr == 0x000a) eptr++;
4166                  }
4167                else
4168                {                {
4169                if (eptr >= md->end_subject ||                if (c != 0x000a &&
4170                    (eptr <= md->end_subject - md->nllen && IS_NEWLINE(eptr)))                    (md->bsr_anycrlf ||
4171                        (c != 0x000b && c != 0x000c && c != 0x0085)))
4172                  break;                  break;
4173                eptr++;                eptr++;
4174                }                }
             break;  
4175              }              }
4176            /* For DOTALL case, fall through and treat as \C */            break;
4177    
4178              case OP_NOT_HSPACE:
4179              for (i = min; i < max; i++)
4180                {
4181                if (eptr >= md->end_subject) break;
4182                c = *eptr;
4183                if (c == 0x09 || c == 0x20 || c == 0xa0) break;
4184                eptr++;
4185                }
4186              break;
4187    
4188            case OP_ANYBYTE:            case OP_HSPACE:
4189            c = max - min;            for (i = min; i < max; i++)
4190            if (c > md->end_subject - eptr) c = md->end_subject - eptr;              {
4191            eptr += c;              if (eptr >= md->end_subject) break;
4192                c = *eptr;
4193                if (c != 0x09 && c != 0x20 && c != 0xa0) break;
4194                eptr++;
4195                }
4196              break;
4197    
4198              case OP_NOT_VSPACE:
4199              for (i = min; i < max; i++)
4200                {
4201                if (eptr >= md->end_subject) break;
4202                c = *eptr;
4203                if (c == 0x0a || c == 0x0b || c == 0x0c || c == 0x0d || c == 0x85)
4204                  break;
4205                eptr++;
4206                }
4207              break;
4208    
4209              case OP_VSPACE:
4210              for (i = min; i < max; i++)
4211                {
4212                if (eptr >= md->end_subject) break;
4213                c = *eptr;
4214                if (c != 0x0a && c != 0x0b && c != 0x0c && c != 0x0d && c != 0x85)
4215                  break;
4216                eptr++;
4217                }
4218            break;            break;
4219    
4220            case OP_NOT_DIGIT:            case OP_NOT_DIGIT:
# Line 3352  for (;;) Line 4277  for (;;)
4277    
4278          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run */
4279    
4280            if (possessive) continue;
4281          while (eptr >= pp)          while (eptr >= pp)
4282            {            {
4283            RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM47);
4284            eptr--;            eptr--;
4285            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4286            }            }
# Line 3366  for (;;) Line 4292  for (;;)
4292        }        }
4293      /* Control never gets here */      /* Control never gets here */
4294    
4295      /* 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
4296      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. */  
4297    
4298      default:      default:
4299      DPRINTF(("Unknown opcode %d\n", *ecode));      DPRINTF(("Unknown opcode %d\n", *ecode));
4300      RRETURN(PCRE_ERROR_UNKNOWN_NODE);      RRETURN(PCRE_ERROR_UNKNOWN_OPCODE);
4301      }      }
4302    
4303    /* 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 4306  for (;;)
4306    
4307    }             /* End of main loop */    }             /* End of main loop */
4308  /* Control never reaches here */  /* Control never reaches here */
4309    
4310    
4311    /* When compiling to use the heap rather than the stack for recursive calls to
4312    match(), the RRETURN() macro jumps here. The number that is saved in
4313    frame->Xwhere indicates which label we actually want to return to. */
4314    
4315    #ifdef NO_RECURSE
4316    #define LBL(val) case val: goto L_RM##val;
4317    HEAP_RETURN:
4318    switch (frame->Xwhere)
4319      {
4320      LBL( 1) LBL( 2) LBL( 3) LBL( 4) LBL( 5) LBL( 6) LBL( 7) LBL( 8)
4321      LBL( 9) LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(17)
4322      LBL(19) LBL(24) LBL(25) LBL(26) LBL(27) LBL(29) LBL(31) LBL(33)
4323      LBL(35) LBL(43) LBL(47) LBL(48) LBL(49) LBL(50) LBL(51) LBL(52)
4324      LBL(53) LBL(54)
4325    #ifdef SUPPORT_UTF8
4326      LBL(16) LBL(18) LBL(20) LBL(21) LBL(22) LBL(23) LBL(28) LBL(30)
4327      LBL(32) LBL(34) LBL(42) LBL(46)
4328    #ifdef SUPPORT_UCP
4329      LBL(36) LBL(37) LBL(38) LBL(39) LBL(40) LBL(41) LBL(44) LBL(45)
4330    #endif  /* SUPPORT_UCP */
4331    #endif  /* SUPPORT_UTF8 */
4332      default:
4333      DPRINTF(("jump error in pcre match: label %d non-existent\n", frame->Xwhere));
4334      return PCRE_ERROR_INTERNAL;
4335      }
4336    #undef LBL
4337    #endif  /* NO_RECURSE */
4338  }  }
4339    
4340    
# Line 3394  Undefine all the macros that were define Line 4347  Undefine all the macros that were define
4347  #ifdef NO_RECURSE  #ifdef NO_RECURSE
4348  #undef eptr  #undef eptr
4349  #undef ecode  #undef ecode
4350    #undef mstart
4351  #undef offset_top  #undef offset_top
4352  #undef ims  #undef ims
4353  #undef eptrb  #undef eptrb
# Line 3411  Undefine all the macros that were define Line 4365  Undefine all the macros that were define
4365    
4366  #undef cur_is_word  #undef cur_is_word
4367  #undef condition  #undef condition
 #undef minimize  
4368  #undef prev_is_word  #undef prev_is_word
4369    
4370  #undef original_ims  #undef original_ims
# Line 3467  Returns:          > 0 => success; value Line 4420  Returns:          > 0 => success; value
4420                   < -1 => some kind of unexpected problem                   < -1 => some kind of unexpected problem
4421  */  */
4422    
4423  PCRE_DATA_SCOPE int  PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
4424  pcre_exec(const pcre *argument_re, const pcre_extra *extra_data,  pcre_exec(const pcre *argument_re, const pcre_extra *extra_data,
4425    PCRE_SPTR subject, int length, int start_offset, int options, int *offsets,    PCRE_SPTR subject, int length, int start_offset, int options, int *offsets,
4426    int offsetcount)    int offsetcount)
# Line 3484  BOOL startline; Line 4437  BOOL startline;
4437  BOOL firstline;  BOOL firstline;
4438  BOOL first_byte_caseless = FALSE;  BOOL first_byte_caseless = FALSE;
4439  BOOL req_byte_caseless = FALSE;  BOOL req_byte_caseless = FALSE;
4440    BOOL utf8;
4441  match_data match_block;  match_data match_block;
4442  match_data *md = &match_block;  match_data *md = &match_block;
4443  const uschar *tables;  const uschar *tables;
# Line 3553  if (re->magic_number != MAGIC_NUMBER) Line 4507  if (re->magic_number != MAGIC_NUMBER)
4507  /* Set up other data */  /* Set up other data */
4508    
4509  anchored = ((re->options | options) & PCRE_ANCHORED) != 0;  anchored = ((re->options | options) & PCRE_ANCHORED) != 0;
4510  startline = (re->options & PCRE_STARTLINE) != 0;  startline = (re->flags & PCRE_STARTLINE) != 0;
4511  firstline = (re->options & PCRE_FIRSTLINE) != 0;  firstline = (re->options & PCRE_FIRSTLINE) != 0;
4512    
4513  /* 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 4521  md->end_subject = md->start_subject + le
4521  end_subject = md->end_subject;  end_subject = md->end_subject;
4522    
4523  md->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;  md->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;
4524  md->utf8 = (re->options & PCRE_UTF8) != 0;  utf8 = md->utf8 = (re->options & PCRE_UTF8) != 0;
4525    md->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;
4526    
4527  md->notbol = (options & PCRE_NOTBOL) != 0;  md->notbol = (options & PCRE_NOTBOL) != 0;
4528  md->noteol = (options & PCRE_NOTEOL) != 0;  md->noteol = (options & PCRE_NOTEOL) != 0;
# Line 3580  md->recursive = NULL; Line 4535  md->recursive = NULL;
4535  md->lcc = tables + lcc_offset;  md->lcc = tables + lcc_offset;
4536  md->ctypes = tables + ctypes_offset;  md->ctypes = tables + ctypes_offset;
4537    
4538  /* Handle different types of newline. The two bits give four cases. If nothing  /* Handle different \R options. */
4539  is set at run time, whatever was used at compile time applies. */  
4540    switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
4541      {
4542      case 0:
4543      if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0)
4544        md->bsr_anycrlf = (re->options & PCRE_BSR_ANYCRLF) != 0;
4545      else
4546    #ifdef BSR_ANYCRLF
4547      md->bsr_anycrlf = TRUE;
4548    #else
4549      md->bsr_anycrlf = FALSE;
4550    #endif
4551      break;
4552    
4553      case PCRE_BSR_ANYCRLF:
4554      md->bsr_anycrlf = TRUE;
4555      break;
4556    
4557      case PCRE_BSR_UNICODE:
4558      md->bsr_anycrlf = FALSE;
4559      break;
4560    
4561  switch ((((options & PCRE_NEWLINE_CRLF) == 0)? re->options : options) &    default: return PCRE_ERROR_BADNEWLINE;
4562           PCRE_NEWLINE_CRLF)    }
4563    
4564    /* Handle different types of newline. The three bits give eight cases. If
4565    nothing is set at run time, whatever was used at compile time applies. */
4566    
4567    switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options :
4568            (pcre_uint32)options) & PCRE_NEWLINE_BITS)
4569    {    {
4570    default:              newline = NEWLINE; break;   /* Compile-time default */    case 0: newline = NEWLINE; break;   /* Compile-time default */
4571    case PCRE_NEWLINE_CR: newline = '\r'; break;    case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
4572    case PCRE_NEWLINE_LF: newline = '\n'; break;    case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
4573    case PCRE_NEWLINE_CR+    case PCRE_NEWLINE_CR+
4574         PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break;         PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
4575      case PCRE_NEWLINE_ANY: newline = -1; break;
4576      case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
4577      default: return PCRE_ERROR_BADNEWLINE;
4578    }    }
4579    
4580  if (newline > 255)  if (newline == -2)
4581    {    {
4582    md->nllen = 2;    md->nltype = NLTYPE_ANYCRLF;
4583    md->nl[0] = (newline >> 8) & 255;    }
4584    md->nl[1] = newline & 255;  else if (newline < 0)
4585      {
4586      md->nltype = NLTYPE_ANY;
4587    }    }
4588  else  else
4589    {    {
4590    md->nllen = 1;    md->nltype = NLTYPE_FIXED;
4591    md->nl[0] = newline;    if (newline > 255)
4592        {
4593        md->nllen = 2;
4594        md->nl[0] = (newline >> 8) & 255;
4595        md->nl[1] = newline & 255;
4596        }
4597      else
4598        {
4599        md->nllen = 1;
4600        md->nl[0] = newline;
4601        }
4602    }    }
4603    
4604  /* 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
4605  moment. */  moment. */
4606    
4607  if (md->partial && (re->options & PCRE_NOPARTIAL) != 0)  if (md->partial && (re->flags & PCRE_NOPARTIAL) != 0)
4608    return PCRE_ERROR_BADPARTIAL;    return PCRE_ERROR_BADPARTIAL;
4609    
4610  /* 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
4611  back the character offset. */  back the character offset. */
4612    
4613  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4614  if (md->utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)
4615    {    {
4616    if (_pcre_valid_utf8((uschar *)subject, length) >= 0)    if (_pcre_valid_utf8((uschar *)subject, length) >= 0)
4617      return PCRE_ERROR_BADUTF8;      return PCRE_ERROR_BADUTF8;
# Line 3685  studied, there may be a bitmap of possib Line 4681  studied, there may be a bitmap of possib
4681    
4682  if (!anchored)  if (!anchored)
4683    {    {
4684    if ((re->options & PCRE_FIRSTSET) != 0)    if ((re->flags & PCRE_FIRSTSET) != 0)
4685      {      {
4686      first_byte = re->first_byte & 255;      first_byte = re->first_byte & 255;
4687      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 4696  if (!anchored)
4696  /* For anchored or unanchored matches, there may be a "last known required  /* For anchored or unanchored matches, there may be a "last known required
4697  character" set. */  character" set. */
4698    
4699  if ((re->options & PCRE_REQCHSET) != 0)  if ((re->flags & PCRE_REQCHSET) != 0)
4700    {    {
4701    req_byte = re->req_byte & 255;    req_byte = re->req_byte & 255;
4702    req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0;    req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0;
4703    req_byte2 = (tables + fcc_offset)[req_byte];  /* case flipped */    req_byte2 = (tables + fcc_offset)[req_byte];  /* case flipped */
4704    }    }
4705    
4706    
4707    /* ==========================================================================*/
4708    
4709  /* Loop for handling unanchored repeated matching attempts; for anchored regexs  /* Loop for handling unanchored repeated matching attempts; for anchored regexs
4710  the loop runs just once. */  the loop runs just once. */
4711    
4712  do  for(;;)
4713    {    {
4714    USPTR save_end_subject = end_subject;    USPTR save_end_subject = end_subject;
4715      USPTR new_start_match;
4716    
4717    /* Reset the maximum number of extractions we might see. */    /* Reset the maximum number of extractions we might see. */
4718    
# Line 3723  do Line 4723  do
4723      while (iptr < iend) *iptr++ = -1;      while (iptr < iend) *iptr++ = -1;
4724      }      }
4725    
4726    /* 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
4727    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
4728    Implement this by temporarily adjusting end_subject so that we stop scanning    newline. Implement this by temporarily adjusting end_subject so that we stop
4729    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
4730    */    this loop. */
4731    
4732    if (firstline)    if (firstline)
4733      {      {
4734      USPTR t = start_match;      USPTR t = start_match;
4735      while (t <= save_end_subject - md->nllen && !IS_NEWLINE(t)) t++;  #ifdef SUPPORT_UTF8
4736        if (utf8)
4737          {
4738          while (t < md->end_subject && !IS_NEWLINE(t))
4739            {
4740            t++;
4741            while (t < end_subject && (*t & 0xc0) == 0x80) t++;
4742            }
4743          }
4744        else
4745    #endif
4746        while (t < md->end_subject && !IS_NEWLINE(t)) t++;
4747      end_subject = t;      end_subject = t;
4748      }      }
4749    
4750    /* Now test for a unique first byte */    /* There are some optimizations that avoid running the match if a known
4751      starting point is not found, or if a known later character is not present.
4752      However, there is an option that disables these, for testing and for ensuring
4753      that all callouts do actually occur. */
4754    
4755    if (first_byte >= 0)    if ((options & PCRE_NO_START_OPTIMIZE) == 0)
4756      {      {
4757      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++;  
     }  
4758    
4759    /* Or to just after a linebreak for a multiline match if possible */      if (first_byte >= 0)
4760          {
4761          if (first_byte_caseless)
4762            while (start_match < end_subject && md->lcc[*start_match] != first_byte)
4763              start_match++;
4764          else
4765            while (start_match < end_subject && *start_match != first_byte)
4766              start_match++;
4767          }
4768    
4769    else if (startline)      /* Or to just after a linebreak for a multiline match */
4770      {  
4771      if (start_match >= md->start_subject + md->nllen +      else if (startline)
           start_offset)  
4772        {        {
4773        while (start_match <= end_subject &&        if (start_match > md->start_subject + start_offset)
4774               !IS_NEWLINE(start_match - md->nllen))          {
4775          start_match++;  #ifdef SUPPORT_UTF8
4776            if (utf8)
4777              {
4778              while (start_match < end_subject && !WAS_NEWLINE(start_match))
4779                {
4780                start_match++;
4781                while(start_match < end_subject && (*start_match & 0xc0) == 0x80)
4782                  start_match++;
4783                }
4784              }
4785            else
4786    #endif
4787            while (start_match < end_subject && !WAS_NEWLINE(start_match))
4788              start_match++;
4789    
4790            /* If we have just passed a CR and the newline option is ANY or ANYCRLF,
4791            and we are now at a LF, advance the match position by one more character.
4792            */
4793    
4794            if (start_match[-1] == CHAR_CR &&
4795                 (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&
4796                 start_match < end_subject &&
4797                 *start_match == CHAR_NL)
4798              start_match++;
4799            }
4800        }        }
     }  
4801    
4802    /* Or to a non-unique first char after study */      /* Or to a non-unique first byte after study */
4803    
4804    else if (start_bits != NULL)      else if (start_bits != NULL)
     {  
     while (start_match < end_subject)  
4805        {        {
4806        register unsigned int c = *start_match;        while (start_match < end_subject)
4807        if ((start_bits[c/8] & (1 << (c&7))) == 0) start_match++; else break;          {
4808            register unsigned int c = *start_match;
4809            if ((start_bits[c/8] & (1 << (c&7))) == 0) start_match++;
4810              else break;
4811            }
4812        }        }
4813      }      }   /* Starting optimizations */
4814    
4815    /* Restore fudged end_subject */    /* Restore fudged end_subject */
4816    
# Line 3783  do Line 4822  do
4822    printf("\n");    printf("\n");
4823  #endif  #endif
4824    
4825    /* 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
4826    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
4827    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.
4828    optimization can save a huge amount of backtracking in patterns with nested    This optimization can save a huge amount of backtracking in patterns with
4829    unlimited repeats that aren't going to match. Writing separate code for    nested unlimited repeats that aren't going to match. Writing separate code
4830    cased/caseless versions makes it go faster, as does using an autoincrement    for cased/caseless versions makes it go faster, as does using an
4831    and backing off on a match.    autoincrement and backing off on a match.
4832    
4833    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
4834    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.
4835    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
4836    don't do this when the string is sufficiently long.    32-megabyte string... so we don't do this when the string is sufficiently
4837      long.
4838    
4839    ALSO: this processing is disabled when partial matching is requested.    ALSO: this processing is disabled when partial matching is requested, or if
4840    */    disabling is explicitly requested. */
4841    
4842    if (req_byte >= 0 &&    if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&
4843          req_byte >= 0 &&
4844        end_subject - start_match < REQ_BYTE_MAX &&        end_subject - start_match < REQ_BYTE_MAX &&
4845        !md->partial)        !md->partial)
4846      {      {
# Line 3826  do Line 4867  do
4867            }            }
4868          }          }
4869    
4870        /* If we can't find the required character, break the matching loop */        /* If we can't find the required character, break the matching loop,
4871          forcing a match failure. */
4872    
4873        if (p >= end_subject) break;        if (p >= end_subject)
4874            {
4875            rc = MATCH_NOMATCH;
4876            break;
4877            }
4878    
4879        /* If we have found the required character, save the point where we        /* If we have found the required character, save the point where we
4880        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 4884  do
4884        }        }
4885      }      }
4886    
4887    /* 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. */  
4888    
4889    md->start_match = start_match;    md->start_match_ptr = start_match;
4890    md->match_call_count = 0;    md->match_call_count = 0;
4891      rc = match(start_match, md->start_code, start_match, 2, md, ims, NULL, 0, 0);
4892    
4893    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)  
4894      {      {
4895      if (firstline &&      /* NOMATCH and PRUNE advance by one character. THEN at this level acts
4896          start_match <= md->end_subject - md->nllen &&      exactly like PRUNE. */
4897          IS_NEWLINE(start_match))  
4898        break;      case MATCH_NOMATCH:
4899      start_match++;      case MATCH_PRUNE:
4900        case MATCH_THEN:
4901        new_start_match = start_match + 1;
4902  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4903      if (md->utf8)      if (utf8)
4904        while(start_match < end_subject && (*start_match & 0xc0) == 0x80)        while(new_start_match < end_subject && (*new_start_match & 0xc0) == 0x80)
4905          start_match++;          new_start_match++;
4906  #endif  #endif
4907      continue;      break;
     }  
4908    
4909    if (rc != MATCH_MATCH)      /* SKIP passes back the next starting point explicitly. */
4910      {  
4911      DPRINTF((">>>> error: returning %d\n", rc));      case MATCH_SKIP:
4912      return rc;      new_start_match = md->start_match_ptr;
4913        break;
4914    
4915        /* COMMIT disables the bumpalong, but otherwise behaves as NOMATCH. */
4916    
4917        case MATCH_COMMIT:
4918        rc = MATCH_NOMATCH;
4919        goto ENDLOOP;
4920    
4921        /* Any other return is some kind of error. */
4922    
4923        default:
4924        goto ENDLOOP;
4925      }      }
4926    
4927    /* We have a match! Copy the offset information from temporary store if    /* Control reaches here for the various types of "no match at this point"
4928    necessary */    result. Reset the code to MATCH_NOMATCH for subsequent checking. */
4929    
4930      rc = MATCH_NOMATCH;
4931    
4932      /* If PCRE_FIRSTLINE is set, the match must happen before or at the first
4933      newline in the subject (though it may continue over the newline). Therefore,
4934      if we have just failed to match, starting at a newline, do not continue. */
4935    
4936      if (firstline && IS_NEWLINE(start_match)) break;
4937    
4938      /* Advance to new matching position */
4939    
4940      start_match = new_start_match;
4941    
4942      /* Break the loop if the pattern is anchored or if we have passed the end of
4943      the subject. */
4944    
4945      if (anchored || start_match > end_subject) break;
4946    
4947      /* If we have just passed a CR and we are now at a LF, and the pattern does
4948      not contain any explicit matches for \r or \n, and the newline option is CRLF
4949      or ANY or ANYCRLF, advance the match position by one more character. */
4950    
4951      if (start_match[-1] == CHAR_CR &&
4952          start_match < end_subject &&
4953          *start_match == CHAR_NL &&
4954          (re->flags & PCRE_HASCRORLF) == 0 &&
4955            (md->nltype == NLTYPE_ANY ||
4956             md->nltype == NLTYPE_ANYCRLF ||
4957             md->nllen == 2))
4958        start_match++;
4959    
4960      }   /* End of for(;;) "bumpalong" loop */
4961    
4962    /* ==========================================================================*/
4963    
4964    /* We reach here when rc is not MATCH_NOMATCH, or if one of the stopping
4965    conditions is true:
4966    
4967    (1) The pattern is anchored or the match was failed by (*COMMIT);
4968    
4969    (2) We are past the end of the subject;
4970    
4971    (3) PCRE_FIRSTLINE is set and we have failed to match at a newline, because
4972        this option requests that a match occur at or before the first newline in
4973        the subject.
4974    
4975    When we have a match and the offset vector is big enough to deal with any
4976    backreferences, captured substring offsets will already be set up. In the case
4977    where we had to get some local store to hold offsets for backreference
4978    processing, copy those that we can. In this case there need not be overflow if
4979    certain parts of the pattern were not used, even though there are more
4980    capturing parentheses than vector slots. */
4981    
4982    ENDLOOP:
4983    
4984    if (rc == MATCH_MATCH)
4985      {
4986    if (using_temporary_offsets)    if (using_temporary_offsets)
4987      {      {
4988      if (offsetcount >= 4)      if (offsetcount >= 4)
# Line 3889  do Line 4991  do