/[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 534 by ph10, Thu Jun 3 18:26:05 2010 UTC revision 617 by ph10, Tue Jul 12 11:00:10 2011 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2010 University of Cambridge             Copyright (c) 1997-2011 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 57  possible. There are also some static sup Line 57  possible. There are also some static sup
57  #undef min  #undef min
58  #undef max  #undef max
59    
60  /* Flag bits for the match() function */  /* Values for setting in md->match_function_type to indicate two special types
61    of call to match(). We do it this way to save on using another stack variable,
62    as stack usage is to be discouraged. */
63    
64  #define match_condassert     0x01  /* Called to check a condition assertion */  #define MATCH_CONDASSERT     1  /* Called to check a condition assertion */
65  #define match_cbegroup       0x02  /* Could-be-empty unlimited repeat group */  #define MATCH_CBEGROUP       2  /* Could-be-empty unlimited repeat group */
66    
67  /* Non-error returns from the match() function. Error returns are externally  /* Non-error returns from the match() function. Error returns are externally
68  defined PCRE_ERROR_xxx codes, which are all negative. */  defined PCRE_ERROR_xxx codes, which are all negative. */
# Line 73  negative to avoid the external error cod Line 75  negative to avoid the external error cod
75    
76  #define MATCH_ACCEPT       (-999)  #define MATCH_ACCEPT       (-999)
77  #define MATCH_COMMIT       (-998)  #define MATCH_COMMIT       (-998)
78  #define MATCH_PRUNE        (-997)  #define MATCH_KETRPOS      (-997)
79  #define MATCH_SKIP         (-996)  #define MATCH_PRUNE        (-996)
80  #define MATCH_SKIP_ARG     (-995)  #define MATCH_SKIP         (-995)
81  #define MATCH_THEN         (-994)  #define MATCH_SKIP_ARG     (-994)
82    #define MATCH_THEN         (-993)
83    
84  /* This is a convenience macro for code that occurs many times. */  /* This is a convenience macro for code that occurs many times. */
85    
# Line 132  while (length-- > 0) Line 135  while (length-- > 0)
135  *          Match a back-reference                *  *          Match a back-reference                *
136  *************************************************/  *************************************************/
137    
138  /* If a back reference hasn't been set, the length that is passed is greater  /* Normally, if a back reference hasn't been set, the length that is passed is
139  than the number of characters left in the string, so the match fails.  negative, so the match always fails. However, in JavaScript compatibility mode,
140    the length passed is zero. Note that in caseless UTF-8 mode, the number of
141    subject bytes matched may be different to the number of reference bytes.
142    
143  Arguments:  Arguments:
144    offset      index into the offset vector    offset      index into the offset vector
145    eptr        points into the subject    eptr        pointer into the subject
146    length      length to be matched    length      length of reference to be matched (number of bytes)
147    md          points to match data block    md          points to match data block
148    ims         the ims flags    caseless    TRUE if caseless
149    
150  Returns:      TRUE if matched  Returns:      < 0 if not matched, otherwise the number of subject bytes matched
151  */  */
152    
153  static BOOL  static int
154  match_ref(int offset, register USPTR eptr, int length, match_data *md,  match_ref(int offset, register USPTR eptr, int length, match_data *md,
155    unsigned long int ims)    BOOL caseless)
156  {  {
157  USPTR p = md->start_subject + md->offset_vector[offset];  USPTR eptr_start = eptr;
158    register USPTR p = md->start_subject + md->offset_vector[offset];
159    
160  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
161  if (eptr >= md->end_subject)  if (eptr >= md->end_subject)
# Line 164  pchars(p, length, FALSE, md); Line 170  pchars(p, length, FALSE, md);
170  printf("\n");  printf("\n");
171  #endif  #endif
172    
173  /* Always fail if not enough characters left */  /* Always fail if reference not set (and not JavaScript compatible). */
174    
175  if (length > md->end_subject - eptr) return FALSE;  if (length < 0) return -1;
176    
177  /* Separate the caseless case for speed. In UTF-8 mode we can only do this  /* Separate the caseless case for speed. In UTF-8 mode we can only do this
178  properly if Unicode properties are supported. Otherwise, we can check only  properly if Unicode properties are supported. Otherwise, we can check only
179  ASCII characters. */  ASCII characters. */
180    
181  if ((ims & PCRE_CASELESS) != 0)  if (caseless)
182    {    {
183  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
184  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
185    if (md->utf8)    if (md->utf8)
186      {      {
187      USPTR endptr = eptr + length;      /* Match characters up to the end of the reference. NOTE: the number of
188      while (eptr < endptr)      bytes matched may differ, because there are some characters whose upper and
189        lower case versions code as different numbers of bytes. For example, U+023A
190        (2 bytes in UTF-8) is the upper case version of U+2C65 (3 bytes in UTF-8);
191        a sequence of 3 of the former uses 6 bytes, as does a sequence of two of
192        the latter. It is important, therefore, to check the length along the
193        reference, not along the subject (earlier code did this wrong). */
194    
195        USPTR endptr = p + length;
196        while (p < endptr)
197        {        {
198        int c, d;        int c, d;
199          if (eptr >= md->end_subject) return -1;
200        GETCHARINC(c, eptr);        GETCHARINC(c, eptr);
201        GETCHARINC(d, p);        GETCHARINC(d, p);
202        if (c != d && c != UCD_OTHERCASE(d)) return FALSE;        if (c != d && c != UCD_OTHERCASE(d)) return -1;
203        }        }
204      }      }
205    else    else
# Line 193  if ((ims & PCRE_CASELESS) != 0) Line 208  if ((ims & PCRE_CASELESS) != 0)
208    
209    /* The same code works when not in UTF-8 mode and in UTF-8 mode when there    /* The same code works when not in UTF-8 mode and in UTF-8 mode when there
210    is no UCP support. */    is no UCP support. */
211        {
212    while (length-- > 0)      if (eptr + length > md->end_subject) return -1;
213      { if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE; }      while (length-- > 0)
214          { if (md->lcc[*p++] != md->lcc[*eptr++]) return -1; }
215        }
216    }    }
217    
218  /* In the caseful case, we can just compare the bytes, whether or not we  /* In the caseful case, we can just compare the bytes, whether or not we
219  are in UTF-8 mode. */  are in UTF-8 mode. */
220    
221  else  else
222    { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }    {
223      if (eptr + length > md->end_subject) return -1;
224      while (length-- > 0) if (*p++ != *eptr++) return -1;
225      }
226    
227  return TRUE;  return eptr - eptr_start;
228  }  }
229    
230    
# Line 256  enum { RM1=1, RM2,  RM3,  RM4,  RM5,  RM Line 276  enum { RM1=1, RM2,  RM3,  RM4,  RM5,  RM
276         RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,         RM31,  RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
277         RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,         RM41,  RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
278         RM51,  RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60,         RM51,  RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60,
279         RM61,  RM62 };         RM61,  RM62, RM63};
280    
281  /* These versions of the macros use the stack, as normal. There are debugging  /* These versions of the macros use the stack, as normal. There are debugging
282  versions and production versions. Note that the "rw" argument of RMATCH isn't  versions and production versions. Note that the "rw" argument of RMATCH isn't
# Line 266  actually used in this definition. */ Line 286  actually used in this definition. */
286  #define REGISTER register  #define REGISTER register
287    
288  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
289  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \  #define RMATCH(ra,rb,rc,rd,re,rw) \
290    { \    { \
291    printf("match() called in line %d\n", __LINE__); \    printf("match() called in line %d\n", __LINE__); \
292    rrc = match(ra,rb,mstart,markptr,rc,rd,re,rf,rg,rdepth+1); \    rrc = match(ra,rb,mstart,markptr,rc,rd,re,rdepth+1); \
293    printf("to line %d\n", __LINE__); \    printf("to line %d\n", __LINE__); \
294    }    }
295  #define RRETURN(ra) \  #define RRETURN(ra) \
# Line 278  actually used in this definition. */ Line 298  actually used in this definition. */
298    return ra; \    return ra; \
299    }    }
300  #else  #else
301  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \  #define RMATCH(ra,rb,rc,rd,re,rw) \
302    rrc = match(ra,rb,mstart,markptr,rc,rd,re,rf,rg,rdepth+1)    rrc = match(ra,rb,mstart,markptr,rc,rd,re,rdepth+1)
303  #define RRETURN(ra) return ra  #define RRETURN(ra) return ra
304  #endif  #endif
305    
# Line 292  argument of match(), which never changes Line 312  argument of match(), which never changes
312    
313  #define REGISTER  #define REGISTER
314    
315  #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw)\  #define RMATCH(ra,rb,rc,rd,re,rw)\
316    {\    {\
317    heapframe *newframe = (pcre_stack_malloc)(sizeof(heapframe));\    heapframe *newframe = (heapframe *)(pcre_stack_malloc)(sizeof(heapframe));\
318    if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\    if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\
319    frame->Xwhere = rw; \    frame->Xwhere = rw; \
320    newframe->Xeptr = ra;\    newframe->Xeptr = ra;\
# Line 302  argument of match(), which never changes Line 322  argument of match(), which never changes
322    newframe->Xmstart = mstart;\    newframe->Xmstart = mstart;\
323    newframe->Xmarkptr = markptr;\    newframe->Xmarkptr = markptr;\
324    newframe->Xoffset_top = rc;\    newframe->Xoffset_top = rc;\
325    newframe->Xims = re;\    newframe->Xeptrb = re;\
   newframe->Xeptrb = rf;\  
   newframe->Xflags = rg;\  
326    newframe->Xrdepth = frame->Xrdepth + 1;\    newframe->Xrdepth = frame->Xrdepth + 1;\
327    newframe->Xprevframe = frame;\    newframe->Xprevframe = frame;\
328    frame = newframe;\    frame = newframe;\
# Line 340  typedef struct heapframe { Line 358  typedef struct heapframe {
358    USPTR Xmstart;    USPTR Xmstart;
359    USPTR Xmarkptr;    USPTR Xmarkptr;
360    int Xoffset_top;    int Xoffset_top;
   long int Xims;  
361    eptrblock *Xeptrb;    eptrblock *Xeptrb;
   int Xflags;  
362    unsigned int Xrdepth;    unsigned int Xrdepth;
363    
364    /* Function local variables */    /* Function local variables */
# Line 363  typedef struct heapframe { Line 379  typedef struct heapframe {
379    BOOL Xcondition;    BOOL Xcondition;
380    BOOL Xprev_is_word;    BOOL Xprev_is_word;
381    
   unsigned long int Xoriginal_ims;  
   
382  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
383    int Xprop_type;    int Xprop_type;
384    int Xprop_value;    int Xprop_value;
# Line 422  immediately. The second one is used when Line 436  immediately. The second one is used when
436  the subject. */  the subject. */
437    
438  #define CHECK_PARTIAL()\  #define CHECK_PARTIAL()\
439    if (md->partial != 0 && eptr >= md->end_subject && eptr > mstart)\    if (md->partial != 0 && eptr >= md->end_subject && \
440      {\        eptr > md->start_used_ptr) \
441      md->hitend = TRUE;\      { \
442      if (md->partial > 1) MRRETURN(PCRE_ERROR_PARTIAL);\      md->hitend = TRUE; \
443        if (md->partial > 1) MRRETURN(PCRE_ERROR_PARTIAL); \
444      }      }
445    
446  #define SCHECK_PARTIAL()\  #define SCHECK_PARTIAL()\
447    if (md->partial != 0 && eptr > mstart)\    if (md->partial != 0 && eptr > md->start_used_ptr) \
448      {\      { \
449      md->hitend = TRUE;\      md->hitend = TRUE; \
450      if (md->partial > 1) MRRETURN(PCRE_ERROR_PARTIAL);\      if (md->partial > 1) MRRETURN(PCRE_ERROR_PARTIAL); \
451      }      }
452    
453    
# Line 449  Arguments: Line 464  Arguments:
464     markptr     pointer to the most recent MARK name, or NULL     markptr     pointer to the most recent MARK name, or NULL
465     offset_top  current top pointer     offset_top  current top pointer
466     md          pointer to "static" info for the match     md          pointer to "static" info for the match
    ims         current /i, /m, and /s options  
467     eptrb       pointer to chain of blocks containing eptr at start of     eptrb       pointer to chain of blocks containing eptr at start of
468                   brackets - for testing for empty matches                   brackets - for testing for empty matches
    flags       can contain  
                  match_condassert - this is an assertion condition  
                  match_cbegroup - this is the start of an unlimited repeat  
                    group that can match an empty string  
469     rdepth      the recursion depth     rdepth      the recursion depth
470    
471  Returns:       MATCH_MATCH if matched            )  these values are >= 0  Returns:       MATCH_MATCH if matched            )  these values are >= 0
# Line 467  Returns:       MATCH_MATCH if matched Line 477  Returns:       MATCH_MATCH if matched
477    
478  static int  static int
479  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, USPTR mstart,  match(REGISTER USPTR eptr, REGISTER const uschar *ecode, USPTR mstart,
480    const uschar *markptr, int offset_top, match_data *md, unsigned long int ims,    const uschar *markptr, int offset_top, match_data *md, eptrblock *eptrb,
481    eptrblock *eptrb, int flags, unsigned int rdepth)    unsigned int rdepth)
482  {  {
483  /* These variables do not need to be preserved over recursion in this function,  /* These variables do not need to be preserved over recursion in this function,
484  so they can be ordinary variables in all cases. Mark some of them with  so they can be ordinary variables in all cases. Mark some of them with
# Line 480  register unsigned int c;   /* Character Line 490  register unsigned int c;   /* Character
490  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */  register BOOL utf8;        /* Local copy of UTF-8 flag for speed */
491    
492  BOOL minimize, possessive; /* Quantifier options */  BOOL minimize, possessive; /* Quantifier options */
493    BOOL caseless;
494  int condcode;  int condcode;
495    
496  /* When recursion is not being used, all "local" variables that have to be  /* When recursion is not being used, all "local" variables that have to be
# Line 488  heap storage. Set up the top-level frame Line 499  heap storage. Set up the top-level frame
499  heap whenever RMATCH() does a "recursion". See the macro definitions above. */  heap whenever RMATCH() does a "recursion". See the macro definitions above. */
500    
501  #ifdef NO_RECURSE  #ifdef NO_RECURSE
502  heapframe *frame = (pcre_stack_malloc)(sizeof(heapframe));  heapframe *frame = (heapframe *)(pcre_stack_malloc)(sizeof(heapframe));
503  if (frame == NULL) RRETURN(PCRE_ERROR_NOMEMORY);  if (frame == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
504  frame->Xprevframe = NULL;            /* Marks the top level */  frame->Xprevframe = NULL;            /* Marks the top level */
505    
# Line 499  frame->Xecode = ecode; Line 510  frame->Xecode = ecode;
510  frame->Xmstart = mstart;  frame->Xmstart = mstart;
511  frame->Xmarkptr = markptr;  frame->Xmarkptr = markptr;
512  frame->Xoffset_top = offset_top;  frame->Xoffset_top = offset_top;
 frame->Xims = ims;  
513  frame->Xeptrb = eptrb;  frame->Xeptrb = eptrb;
 frame->Xflags = flags;  
514  frame->Xrdepth = rdepth;  frame->Xrdepth = rdepth;
515    
516  /* This is where control jumps back to to effect "recursion" */  /* This is where control jumps back to to effect "recursion" */
# Line 515  HEAP_RECURSE: Line 524  HEAP_RECURSE:
524  #define mstart             frame->Xmstart  #define mstart             frame->Xmstart
525  #define markptr            frame->Xmarkptr  #define markptr            frame->Xmarkptr
526  #define offset_top         frame->Xoffset_top  #define offset_top         frame->Xoffset_top
 #define ims                frame->Xims  
527  #define eptrb              frame->Xeptrb  #define eptrb              frame->Xeptrb
 #define flags              frame->Xflags  
528  #define rdepth             frame->Xrdepth  #define rdepth             frame->Xrdepth
529    
530  /* Ditto for the local variables */  /* Ditto for the local variables */
# Line 539  HEAP_RECURSE: Line 546  HEAP_RECURSE:
546  #define condition          frame->Xcondition  #define condition          frame->Xcondition
547  #define prev_is_word       frame->Xprev_is_word  #define prev_is_word       frame->Xprev_is_word
548    
 #define original_ims       frame->Xoriginal_ims  
   
549  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
550  #define prop_type          frame->Xprop_type  #define prop_type          frame->Xprop_type
551  #define prop_value         frame->Xprop_value  #define prop_value         frame->Xprop_value
# Line 577  i, and fc and c, can be the same variabl Line 582  i, and fc and c, can be the same variabl
582  #define fi i  #define fi i
583  #define fc c  #define fc c
584    
585    /* Many of the following variables are used only in small blocks of the code.
586  #ifdef SUPPORT_UTF8                /* Many of these variables are used only  */  My normal style of coding would have declared them within each of those blocks.
587  const uschar *charptr;             /* in small blocks of the code. My normal */  However, in order to accommodate the version of this code that uses an external
588  #endif                             /* style of coding would have declared    */  "stack" implemented on the heap, it is easier to declare them all here, so the
589  const uschar *callpat;             /* them within each of those blocks.      */  declarations can be cut out in a block. The only declarations within blocks
590  const uschar *data;                /* However, in order to accommodate the   */  below are for variables that do not have to be preserved over a recursive call
591  const uschar *next;                /* version of this code that uses an      */  to RMATCH(). */
592  USPTR         pp;                  /* external "stack" implemented on the    */  
593  const uschar *prev;                /* heap, it is easier to declare them all */  #ifdef SUPPORT_UTF8
594  USPTR         saved_eptr;          /* here, so the declarations can be cut   */  const uschar *charptr;
595                                     /* out in a block. The only declarations  */  #endif
596  recursion_info new_recursive;      /* within blocks below are for variables  */  const uschar *callpat;
597                                     /* that do not have to be preserved over  */  const uschar *data;
598  BOOL cur_is_word;                  /* a recursive call to RMATCH().          */  const uschar *next;
599    USPTR         pp;
600    const uschar *prev;
601    USPTR         saved_eptr;
602    
603    recursion_info new_recursive;
604    
605    BOOL cur_is_word;
606  BOOL condition;  BOOL condition;
607  BOOL prev_is_word;  BOOL prev_is_word;
608    
 unsigned long int original_ims;  
   
609  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
610  int prop_type;  int prop_type;
611  int prop_value;  int prop_value;
# Line 622  int stacksave[REC_STACK_SAVE_MAX]; Line 632  int stacksave[REC_STACK_SAVE_MAX];
632  eptrblock newptrb;  eptrblock newptrb;
633  #endif     /* NO_RECURSE */  #endif     /* NO_RECURSE */
634    
635    /* To save space on the stack and in the heap frame, I have doubled up on some
636    of the local variables that are used only in localised parts of the code, but
637    still need to be preserved over recursive calls of match(). These macros define
638    the alternative names that are used. */
639    
640    #define allow_zero    cur_is_word
641    #define cbegroup      condition
642    #define code_offset   codelink
643    #define condassert    condition
644    #define matched_once  prev_is_word
645    
646  /* These statements are here to stop the compiler complaining about unitialized  /* These statements are here to stop the compiler complaining about unitialized
647  variables. */  variables. */
648    
# Line 658  haven't exceeded the recursive call limi Line 679  haven't exceeded the recursive call limi
679  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);  if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
680  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);  if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
681    
 original_ims = ims;    /* Save for resetting on ')' */  
   
682  /* At the start of a group with an unlimited repeat that may match an empty  /* At the start of a group with an unlimited repeat that may match an empty
683  string, the match_cbegroup flag is set. When this is the case, add the current  string, the variable md->match_function_type is set to MATCH_CBEGROUP. It is
684  subject pointer to the chain of such remembered pointers, to be checked when we  done this way to save having to use another function argument, which would take
685  hit the closing ket, in order to break infinite loops that match no characters.  up space on the stack. See also MATCH_CONDASSERT below.
686  When match() is called in other circumstances, don't add to the chain. The  
687  match_cbegroup flag must NOT be used with tail recursion, because the memory  When MATCH_CBEGROUP is set, add the current subject pointer to the chain of
688  block that is used is on the stack, so a new one may be required for each  such remembered pointers, to be checked when we hit the closing ket, in order
689  match(). */  to break infinite loops that match no characters. When match() is called in
690    other circumstances, don't add to the chain. The MATCH_CBEGROUP feature must
691    NOT be used with tail recursion, because the memory block that is used is on
692    the stack, so a new one may be required for each match(). */
693    
694  if ((flags & match_cbegroup) != 0)  if (md->match_function_type == MATCH_CBEGROUP)
695    {    {
696    newptrb.epb_saved_eptr = eptr;    newptrb.epb_saved_eptr = eptr;
697    newptrb.epb_prev = eptrb;    newptrb.epb_prev = eptrb;
698    eptrb = &newptrb;    eptrb = &newptrb;
699      md->match_function_type = 0;
700    }    }
701    
702  /* Now start processing the opcodes. */  /* Now start processing the opcodes. */
# Line 682  for (;;) Line 705  for (;;)
705    {    {
706    minimize = possessive = FALSE;    minimize = possessive = FALSE;
707    op = *ecode;    op = *ecode;
708    
709    switch(op)    switch(op)
710      {      {
711      case OP_MARK:      case OP_MARK:
712      markptr = ecode + 2;      markptr = ecode + 2;
713      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
714        ims, eptrb, flags, RM55);        eptrb, RM55);
715    
716      /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an      /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
717      argument, and we must check whether that argument matches this MARK's      argument, and we must check whether that argument matches this MARK's
# Line 710  for (;;) Line 733  for (;;)
733      case OP_FAIL:      case OP_FAIL:
734      MRRETURN(MATCH_NOMATCH);      MRRETURN(MATCH_NOMATCH);
735    
736        /* COMMIT overrides PRUNE, SKIP, and THEN */
737    
738      case OP_COMMIT:      case OP_COMMIT:
739      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
740        ims, eptrb, flags, RM52);        eptrb, RM52);
741      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE &&
742            rrc != MATCH_SKIP && rrc != MATCH_SKIP_ARG &&
743            rrc != MATCH_THEN)
744          RRETURN(rrc);
745      MRRETURN(MATCH_COMMIT);      MRRETURN(MATCH_COMMIT);
746    
747        /* PRUNE overrides THEN */
748    
749      case OP_PRUNE:      case OP_PRUNE:
750      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
751        ims, eptrb, flags, RM51);        eptrb, RM51);
752      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
753      MRRETURN(MATCH_PRUNE);      MRRETURN(MATCH_PRUNE);
754    
755      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
756      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
757        ims, eptrb, flags, RM56);        eptrb, RM56);
758      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
759      md->mark = ecode + 2;      md->mark = ecode + 2;
760      RRETURN(MATCH_PRUNE);      RRETURN(MATCH_PRUNE);
761    
762        /* SKIP overrides PRUNE and THEN */
763    
764      case OP_SKIP:      case OP_SKIP:
765      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
766        ims, eptrb, flags, RM53);        eptrb, RM53);
767      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
768          RRETURN(rrc);
769      md->start_match_ptr = eptr;   /* Pass back current position */      md->start_match_ptr = eptr;   /* Pass back current position */
770      MRRETURN(MATCH_SKIP);      MRRETURN(MATCH_SKIP);
771    
772      case OP_SKIP_ARG:      case OP_SKIP_ARG:
773      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,
774        ims, eptrb, flags, RM57);        eptrb, RM57);
775      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
776          RRETURN(rrc);
777    
778      /* Pass back the current skip name by overloading md->start_match_ptr and      /* Pass back the current skip name by overloading md->start_match_ptr and
779      returning the special MATCH_SKIP_ARG return code. This will either be      returning the special MATCH_SKIP_ARG return code. This will either be
# Line 749  for (;;) Line 783  for (;;)
783      md->start_match_ptr = ecode + 2;      md->start_match_ptr = ecode + 2;
784      RRETURN(MATCH_SKIP_ARG);      RRETURN(MATCH_SKIP_ARG);
785    
786        /* For THEN (and THEN_ARG) we pass back the address of the bracket or
787        the alt that is at the start of the current branch. This makes it possible
788        to skip back past alternatives that precede the THEN within the current
789        branch. */
790    
791      case OP_THEN:      case OP_THEN:
792      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
793        ims, eptrb, flags, RM54);        eptrb, RM54);
794      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
795        md->start_match_ptr = ecode - GET(ecode, 1);
796      MRRETURN(MATCH_THEN);      MRRETURN(MATCH_THEN);
797    
798      case OP_THEN_ARG:      case OP_THEN_ARG:
799      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1], offset_top, md,      RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode] + ecode[1+LINK_SIZE],
800        ims, eptrb, flags, RM58);        offset_top, md, eptrb, RM58);
801      if (rrc != MATCH_NOMATCH) RRETURN(rrc);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
802      md->mark = ecode + 2;      md->start_match_ptr = ecode - GET(ecode, 1);
803        md->mark = ecode + LINK_SIZE + 2;
804      RRETURN(MATCH_THEN);      RRETURN(MATCH_THEN);
805    
806      /* Handle a capturing bracket. If there is space in the offset vector, save      /* Handle a capturing bracket, other than those that are possessive with an
807      the current subject position in the working slot at the top of the vector.      unlimited repeat. If there is space in the offset vector, save the current
808      We mustn't change the current values of the data slot, because they may be      subject position in the working slot at the top of the vector. We mustn't
809      set from a previous iteration of this group, and be referred to by a      change the current values of the data slot, because they may be set from a
810      reference inside the group.      previous iteration of this group, and be referred to by a reference inside
811        the group. A failure to match might occur after the group has succeeded,
812      If the bracket fails to match, we need to restore this value and also the      if something later on doesn't match. For this reason, we need to restore
813      values of the final offsets, in case they were set by a previous iteration      the working value and also the values of the final offsets, in case they
814      of the same bracket.      were set by a previous iteration of the same bracket.
815    
816      If there isn't enough space in the offset vector, treat this as if it were      If there isn't enough space in the offset vector, treat this as if it were
817      a non-capturing bracket. Don't worry about setting the flag for the error      a non-capturing bracket. Don't worry about setting the flag for the error
# Line 780  for (;;) Line 821  for (;;)
821      case OP_SCBRA:      case OP_SCBRA:
822      number = GET2(ecode, 1+LINK_SIZE);      number = GET2(ecode, 1+LINK_SIZE);
823      offset = number << 1;      offset = number << 1;
824    
825  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
826      printf("start bracket %d\n", number);      printf("start bracket %d\n", number);
827      printf("subject=");      printf("subject=");
# Line 799  for (;;) Line 840  for (;;)
840        md->offset_vector[md->offset_end - number] =        md->offset_vector[md->offset_end - number] =
841          (int)(eptr - md->start_subject);          (int)(eptr - md->start_subject);
842    
843        flags = (op == OP_SCBRA)? match_cbegroup : 0;        for (;;)
       do  
844          {          {
845          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,          if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
846            ims, eptrb, flags, RM1);          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
847          if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);            eptrb, RM1);
848            if (rrc != MATCH_NOMATCH &&
849                (rrc != MATCH_THEN || md->start_match_ptr != ecode))
850              RRETURN(rrc);
851          md->capture_last = save_capture_last;          md->capture_last = save_capture_last;
852          ecode += GET(ecode, 1);          ecode += GET(ecode, 1);
853            if (*ecode != OP_ALT) break;
854          }          }
       while (*ecode == OP_ALT);  
855    
856        DPRINTF(("bracket %d failed\n", number));        DPRINTF(("bracket %d failed\n", number));
857    
# Line 816  for (;;) Line 859  for (;;)
859        md->offset_vector[offset+1] = save_offset2;        md->offset_vector[offset+1] = save_offset2;
860        md->offset_vector[md->offset_end - number] = save_offset3;        md->offset_vector[md->offset_end - number] = save_offset3;
861    
862        if (rrc != MATCH_THEN) md->mark = markptr;        if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
863        RRETURN(MATCH_NOMATCH);        RRETURN(MATCH_NOMATCH);
864        }        }
865    
# Line 831  for (;;) Line 874  for (;;)
874      /* VVVVVVVVVVVVVVVVVVVVVVVVV */      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
875      /* VVVVVVVVVVVVVVVVVVVVVVVVV */      /* VVVVVVVVVVVVVVVVVVVVVVVVV */
876    
877      /* Non-capturing bracket. Loop for all the alternatives. When we get to the      /* Non-capturing bracket, except for possessive with unlimited repeat. Loop
878      final alternative within the brackets, we would return the result of a      for all the alternatives. When we get to the final alternative within the
879      recursive call to match() whatever happened. We can reduce stack usage by      brackets, we used to return the result of a recursive call to match()
880      turning this into a tail recursion, except in the case when match_cbegroup      whatever happened so it was possible to reduce stack usage by turning this
881      is set.*/      into a tail recursion, except in the case of a possibly empty group.
882        However, now that there is the possiblity of (*THEN) occurring in the final
883        alternative, this optimization is no longer possible. */
884    
885      case OP_BRA:      case OP_BRA:
886      case OP_SBRA:      case OP_SBRA:
887      DPRINTF(("start non-capturing bracket\n"));      DPRINTF(("start non-capturing bracket\n"));
     flags = (op >= OP_SBRA)? match_cbegroup : 0;  
888      for (;;)      for (;;)
889        {        {
890        if (ecode[GET(ecode, 1)] != OP_ALT)   /* Final alternative */        if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
891          {        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, eptrb,
892          if (flags == 0)    /* Not a possibly empty group */          RM2);
893            {        if (rrc != MATCH_NOMATCH &&
894            ecode += _pcre_OP_lengths[*ecode];            (rrc != MATCH_THEN || md->start_match_ptr != ecode))
895            DPRINTF(("bracket 0 tail recursion\n"));          RRETURN(rrc);
896            goto TAIL_RECURSE;        ecode += GET(ecode, 1);
897            }        if (*ecode != OP_ALT) break;
898          }
899    
900          /* Possibly empty group; can't use tail recursion. */      if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
901        RRETURN(MATCH_NOMATCH);
902    
903          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,      /* Handle possessive capturing brackets with an unlimited repeat. We come
904            eptrb, flags, RM48);      here from BRAZERO with allow_zero set TRUE. The offset_vector values are
905          if (rrc == MATCH_NOMATCH) md->mark = markptr;      handled similarly to the normal case above. However, the matching is
906          RRETURN(rrc);      different. The end of these brackets will always be OP_KETRPOS, which
907        returns MATCH_KETRPOS without going further in the pattern. By this means
908        we can handle the group by iteration rather than recursion, thereby
909        reducing the amount of stack needed. */
910    
911        case OP_CBRAPOS:
912        case OP_SCBRAPOS:
913        allow_zero = FALSE;
914    
915        POSSESSIVE_CAPTURE:
916        number = GET2(ecode, 1+LINK_SIZE);
917        offset = number << 1;
918    
919    #ifdef PCRE_DEBUG
920        printf("start possessive bracket %d\n", number);
921        printf("subject=");
922        pchars(eptr, 16, TRUE, md);
923        printf("\n");
924    #endif
925    
926        if (offset < md->offset_max)
927          {
928          matched_once = FALSE;
929          code_offset = ecode - md->start_code;
930    
931          save_offset1 = md->offset_vector[offset];
932          save_offset2 = md->offset_vector[offset+1];
933          save_offset3 = md->offset_vector[md->offset_end - number];
934          save_capture_last = md->capture_last;
935    
936          DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
937    
938          /* Each time round the loop, save the current subject position for use
939          when the group matches. For MATCH_MATCH, the group has matched, so we
940          restart it with a new subject starting position, remembering that we had
941          at least one match. For MATCH_NOMATCH, carry on with the alternatives, as
942          usual. If we haven't matched any alternatives in any iteration, check to
943          see if a previous iteration matched. If so, the group has matched;
944          continue from afterwards. Otherwise it has failed; restore the previous
945          capture values before returning NOMATCH. */
946    
947          for (;;)
948            {
949            md->offset_vector[md->offset_end - number] =
950              (int)(eptr - md->start_subject);
951            if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
952            RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
953              eptrb, RM63);
954            if (rrc == MATCH_KETRPOS)
955              {
956              offset_top = md->end_offset_top;
957              eptr = md->end_match_ptr;
958              ecode = md->start_code + code_offset;
959              save_capture_last = md->capture_last;
960              matched_once = TRUE;
961              continue;
962              }
963            if (rrc != MATCH_NOMATCH &&
964                (rrc != MATCH_THEN || md->start_match_ptr != ecode))
965              RRETURN(rrc);
966            md->capture_last = save_capture_last;
967            ecode += GET(ecode, 1);
968            if (*ecode != OP_ALT) break;
969          }          }
970    
971        /* For non-final alternatives, continue the loop for a NOMATCH result;        if (!matched_once)
972        otherwise return. */          {
973            md->offset_vector[offset] = save_offset1;
974            md->offset_vector[offset+1] = save_offset2;
975            md->offset_vector[md->offset_end - number] = save_offset3;
976            }
977    
978          if (rrc != MATCH_THEN && md->mark == NULL) md->mark = markptr;
979          if (allow_zero || matched_once)
980            {
981            ecode += 1 + LINK_SIZE;
982            break;
983            }
984    
985          RRETURN(MATCH_NOMATCH);
986          }
987    
988        /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
989        as a non-capturing bracket. */
990    
991        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
992        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
993    
994        DPRINTF(("insufficient capture room: treat as non-capturing\n"));
995    
996        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
997        /* VVVVVVVVVVVVVVVVVVVVVVVVV */
998    
999        /* Non-capturing possessive bracket with unlimited repeat. We come here
1000        from BRAZERO with allow_zero = TRUE. The code is similar to the above,
1001        without the capturing complication. It is written out separately for speed
1002        and cleanliness. */
1003    
1004        case OP_BRAPOS:
1005        case OP_SBRAPOS:
1006        allow_zero = FALSE;
1007    
1008        POSSESSIVE_NON_CAPTURE:
1009        matched_once = FALSE;
1010        code_offset = ecode - md->start_code;
1011    
1012        RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,      for (;;)
1013          eptrb, flags, RM2);        {
1014        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);        if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1015          RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
1016            eptrb, RM48);
1017          if (rrc == MATCH_KETRPOS)
1018            {
1019            offset_top = md->end_offset_top;
1020            eptr = md->end_match_ptr;
1021            ecode = md->start_code + code_offset;
1022            matched_once = TRUE;
1023            continue;
1024            }
1025          if (rrc != MATCH_NOMATCH &&
1026              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1027            RRETURN(rrc);
1028        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1029          if (*ecode != OP_ALT) break;
1030        }        }
1031    
1032        if (matched_once || allow_zero)
1033          {
1034          ecode += 1 + LINK_SIZE;
1035          break;
1036          }
1037        RRETURN(MATCH_NOMATCH);
1038    
1039      /* Control never reaches here. */      /* Control never reaches here. */
1040    
1041      /* Conditional group: compilation checked that there are no more than      /* Conditional group: compilation checked that there are no more than
1042      two branches. If the condition is false, skipping the first branch takes us      two branches. If the condition is false, skipping the first branch takes us
1043      past the end if there is only one branch, but that's OK because that is      past the end if there is only one branch, but that's OK because that is
1044      exactly what going to the ket would do. As there is only one branch to be      exactly what going to the ket would do. */
     obeyed, we can use tail recursion to avoid using another stack frame. */  
1045    
1046      case OP_COND:      case OP_COND:
1047      case OP_SCOND:      case OP_SCOND:
1048      codelink= GET(ecode, 1);      codelink = GET(ecode, 1);
1049    
1050      /* Because of the way auto-callout works during compile, a callout item is      /* Because of the way auto-callout works during compile, a callout item is
1051      inserted between OP_COND and an assertion condition. */      inserted between OP_COND and an assertion condition. */
# Line 1053  for (;;) Line 1220  for (;;)
1220        }        }
1221    
1222      /* The condition is an assertion. Call match() to evaluate it - setting      /* The condition is an assertion. Call match() to evaluate it - setting
1223      the final argument match_condassert causes it to stop at the end of an      md->match_function_type to MATCH_CONDASSERT causes it to stop at the end of
1224      assertion. */      an assertion. */
1225    
1226      else      else
1227        {        {
1228        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,        md->match_function_type = MATCH_CONDASSERT;
1229            match_condassert, RM3);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM3);
1230        if (rrc == MATCH_MATCH)        if (rrc == MATCH_MATCH)
1231          {          {
1232          condition = TRUE;          condition = TRUE;
1233          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);          ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
1234          while (*ecode == OP_ALT) ecode += GET(ecode, 1);          while (*ecode == OP_ALT) ecode += GET(ecode, 1);
1235          }          }
1236        else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)        else if (rrc != MATCH_NOMATCH &&
1237                  (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1238          {          {
1239          RRETURN(rrc);         /* Need braces because of following else */          RRETURN(rrc);         /* Need braces because of following else */
1240          }          }
# Line 1078  for (;;) Line 1246  for (;;)
1246        }        }
1247    
1248      /* 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,
1249      we can use tail recursion to avoid using another stack frame, except when      we used to use tail recursion to avoid using another stack frame, except
1250      match_cbegroup is required for an unlimited repeat of a possibly empty      when there was unlimited repeat of a possibly empty group. However, that
1251      group. If the second alternative doesn't exist, we can just plough on. */      strategy no longer works because of the possibilty of (*THEN) being
1252        encountered in the branch. A recursive call to match() is always required,
1253        unless the second alternative doesn't exist, in which case we can just
1254        plough on. */
1255    
1256      if (condition || *ecode == OP_ALT)      if (condition || *ecode == OP_ALT)
1257        {        {
1258        ecode += 1 + LINK_SIZE;        if (op == OP_SCOND) md->match_function_type = MATCH_CBEGROUP;
1259        if (op == OP_SCOND)        /* Possibly empty group */        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM49);
1260          {        if (rrc == MATCH_THEN && md->start_match_ptr == ecode)
1261          RMATCH(eptr, ecode, offset_top, md, ims, eptrb, match_cbegroup, RM49);          rrc = MATCH_NOMATCH;
1262          RRETURN(rrc);        RRETURN(rrc);
         }  
       else                       /* Group must match something */  
         {  
         flags = 0;  
         goto TAIL_RECURSE;  
         }  
1263        }        }
1264      else                         /* Condition false & no alternative */      else                         /* Condition false & no alternative */
1265        {        {
# Line 1127  for (;;) Line 1292  for (;;)
1292      break;      break;
1293    
1294    
1295      /* End of the pattern, either real or forced. If we are in a top-level      /* End of the pattern, either real or forced. If we are in a recursion, we
1296      recursion, we should restore the offsets appropriately and continue from      should restore the offsets appropriately, and if it's a top-level
1297      after the call. */      recursion, continue from after the call. */
1298    
1299      case OP_ACCEPT:      case OP_ACCEPT:
1300        case OP_ASSERT_ACCEPT:
1301      case OP_END:      case OP_END:
1302      if (md->recursive != NULL && md->recursive->group_num == 0)      if (md->recursive != NULL)
1303        {        {
1304        recursion_info *rec = md->recursive;        recursion_info *rec = md->recursive;
       DPRINTF(("End of pattern in a (?0) recursion\n"));  
1305        md->recursive = rec->prevrec;        md->recursive = rec->prevrec;
1306        memmove(md->offset_vector, rec->offset_save,        memmove(md->offset_vector, rec->offset_save,
1307          rec->saved_max * sizeof(int));          rec->saved_max * sizeof(int));
1308        offset_top = rec->save_offset_top;        offset_top = rec->save_offset_top;
1309        ims = original_ims;        if (rec->group_num == 0)
1310        ecode = rec->after_call;          {
1311        break;          ecode = rec->after_call;
1312            break;
1313            }
1314        }        }
1315    
1316      /* Otherwise, if we have matched an empty string, fail if PCRE_NOTEMPTY is      /* Otherwise, if we have matched an empty string, fail if not in an
1317      set, or if PCRE_NOTEMPTY_ATSTART is set and we have matched at the start of      assertion and if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART
1318      the subject. In both cases, backtracking will then try other alternatives,      is set and we have matched at the start of the subject. In both cases,
1319      if any. */      backtracking will then try other alternatives, if any. */
1320    
1321      if (eptr == mstart &&      else if (eptr == mstart && op != OP_ASSERT_ACCEPT &&
1322          (md->notempty ||          (md->notempty ||
1323            (md->notempty_atstart &&            (md->notempty_atstart &&
1324              mstart == md->start_subject + md->start_offset)))              mstart == md->start_subject + md->start_offset)))
1325        MRRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
1326    
1327      /* Otherwise, we have a match. */      /* Otherwise, we have a match. */
1328    
1329      md->end_match_ptr = eptr;           /* Record where we ended */      md->end_match_ptr = eptr;           /* Record where we ended */
1330      md->end_offset_top = offset_top;    /* and how many extracts were taken */      md->end_offset_top = offset_top;    /* and how many extracts were taken */
1331      md->start_match_ptr = mstart;       /* and the start (\K can modify) */      md->start_match_ptr = mstart;       /* and the start (\K can modify) */
# Line 1169  for (;;) Line 1336  for (;;)
1336      rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;      rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
1337      MRRETURN(rrc);      MRRETURN(rrc);
1338    
     /* Change option settings */  
   
     case OP_OPT:  
     ims = ecode[1];  
     ecode += 2;  
     DPRINTF(("ims set to %02lx\n", ims));  
     break;  
   
1339      /* Assertion brackets. Check the alternative branches in turn - the      /* Assertion brackets. Check the alternative branches in turn - the
1340      matching won't pass the KET for an assertion. If any one branch matches,      matching won't pass the KET for an assertion. If any one branch matches,
1341      the assertion is true. Lookbehind assertions have an OP_REVERSE item at the      the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
1342      start of each branch to move the current point backwards, so the code at      start of each branch to move the current point backwards, so the code at
1343      this level is identical to the lookahead case. */      this level is identical to the lookahead case. When the assertion is part
1344        of a condition, we want to return immediately afterwards. The caller of
1345        this incarnation of the match() function will have set MATCH_CONDASSERT in
1346        md->match_function type, and one of these opcodes will be the first opcode
1347        that is processed. We use a local variable that is preserved over calls to
1348        match() to remember this case. */
1349    
1350      case OP_ASSERT:      case OP_ASSERT:
1351      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1352        if (md->match_function_type == MATCH_CONDASSERT)
1353          {
1354          condassert = TRUE;
1355          md->match_function_type = 0;
1356          }
1357        else condassert = FALSE;
1358    
1359      do      do
1360        {        {
1361        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
         RM4);  
1362        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1363          {          {
1364          mstart = md->start_match_ptr;   /* In case \K reset it */          mstart = md->start_match_ptr;   /* In case \K reset it */
1365          break;          break;
1366          }          }
1367        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);        if (rrc != MATCH_NOMATCH &&
1368              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1369            RRETURN(rrc);
1370        ecode += GET(ecode, 1);        ecode += GET(ecode, 1);
1371        }        }
1372      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1373    
1374      if (*ecode == OP_KET) MRRETURN(MATCH_NOMATCH);      if (*ecode == OP_KET) MRRETURN(MATCH_NOMATCH);
1375    
1376      /* If checking an assertion for a condition, return MATCH_MATCH. */      /* If checking an assertion for a condition, return MATCH_MATCH. */
1377    
1378      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);
1379    
1380      /* Continue from after the assertion, updating the offsets high water      /* Continue from after the assertion, updating the offsets high water
1381      mark, since extracts may have been taken during the assertion. */      mark, since extracts may have been taken during the assertion. */
# Line 1218  for (;;) Line 1391  for (;;)
1391    
1392      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1393      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1394        if (md->match_function_type == MATCH_CONDASSERT)
1395          {
1396          condassert = TRUE;
1397          md->match_function_type = 0;
1398          }
1399        else condassert = FALSE;
1400    
1401      do      do
1402        {        {
1403        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
         RM5);  
1404        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) MRRETURN(MATCH_NOMATCH);        if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) MRRETURN(MATCH_NOMATCH);
1405        if (rrc == MATCH_SKIP || rrc == MATCH_PRUNE || rrc == MATCH_COMMIT)        if (rrc == MATCH_SKIP || rrc == MATCH_PRUNE || rrc == MATCH_COMMIT)
1406          {          {
1407          do ecode += GET(ecode,1); while (*ecode == OP_ALT);          do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1408          break;          break;
1409          }          }
1410        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);        if (rrc != MATCH_NOMATCH &&
1411              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1412            RRETURN(rrc);
1413        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1414        }        }
1415      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
1416    
1417      if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);      if (condassert) RRETURN(MATCH_MATCH);  /* Condition assertion */
1418    
1419      ecode += 1 + LINK_SIZE;      ecode += 1 + LINK_SIZE;
1420      continue;      continue;
1421    
# Line 1308  for (;;) Line 1489  for (;;)
1489      65535 such values, which is too large to put on the stack, but using malloc      65535 such values, which is too large to put on the stack, but using malloc
1490      for small numbers seems expensive. As a compromise, the stack is used when      for small numbers seems expensive. As a compromise, the stack is used when
1491      there are no more than REC_STACK_SAVE_MAX values to store; otherwise malloc      there are no more than REC_STACK_SAVE_MAX values to store; otherwise malloc
1492      is used. A problem is what to do if the malloc fails ... there is no way of      is used.
     returning to the top level with an error. Save the top REC_STACK_SAVE_MAX  
     values on the stack, and accept that the rest may be wrong.  
1493    
1494      There are also other values that have to be saved. We use a chained      There are also other values that have to be saved. We use a chained
1495      sequence of blocks that actually live on the stack. Thanks to Robin Houston      sequence of blocks that actually live on the stack. Thanks to Robin Houston
# Line 1347  for (;;) Line 1526  for (;;)
1526        memcpy(new_recursive.offset_save, md->offset_vector,        memcpy(new_recursive.offset_save, md->offset_vector,
1527              new_recursive.saved_max * sizeof(int));              new_recursive.saved_max * sizeof(int));
1528        new_recursive.save_offset_top = offset_top;        new_recursive.save_offset_top = offset_top;
1529    
1530        /* OK, now we can do the recursion. For each top-level alternative we        /* OK, now we can do the recursion. For each top-level alternative we
1531        restore the offset and recursion data. */        restore the offset and recursion data. */
1532    
1533        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));        DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1534        flags = (*callpat >= OP_SBRA)? match_cbegroup : 0;        cbegroup = (*callpat >= OP_SBRA);
1535        do        do
1536          {          {
1537            if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
1538          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,          RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,
1539            md, ims, eptrb, flags, RM6);            md, eptrb, RM6);
1540          if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)          if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1541            {            {
1542            DPRINTF(("Recursion matched\n"));            DPRINTF(("Recursion matched\n"));
# Line 1365  for (;;) Line 1545  for (;;)
1545              (pcre_free)(new_recursive.offset_save);              (pcre_free)(new_recursive.offset_save);
1546            MRRETURN(MATCH_MATCH);            MRRETURN(MATCH_MATCH);
1547            }            }
1548          else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)          else if (rrc != MATCH_NOMATCH &&
1549                    (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1550            {            {
1551            DPRINTF(("Recursion gave error %d\n", rrc));            DPRINTF(("Recursion gave error %d\n", rrc));
1552            if (new_recursive.offset_save != stacksave)            if (new_recursive.offset_save != stacksave)
# Line 1402  for (;;) Line 1583  for (;;)
1583    
1584      do      do
1585        {        {
1586        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM7);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
1587        if (rrc == MATCH_MATCH)  /* Note: _not_ MATCH_ACCEPT */        if (rrc == MATCH_MATCH)  /* Note: _not_ MATCH_ACCEPT */
1588          {          {
1589          mstart = md->start_match_ptr;          mstart = md->start_match_ptr;
1590          break;          break;
1591          }          }
1592        if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);        if (rrc != MATCH_NOMATCH &&
1593              (rrc != MATCH_THEN || md->start_match_ptr != ecode))
1594            RRETURN(rrc);
1595        ecode += GET(ecode,1);        ecode += GET(ecode,1);
1596        }        }
1597      while (*ecode == OP_ALT);      while (*ecode == OP_ALT);
# Line 1417  for (;;) Line 1600  for (;;)
1600    
1601      if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);      if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
1602    
1603      /* Continue as from after the assertion, updating the offsets high water      /* Continue after the group, updating the offsets high water mark, since
1604      mark, since extracts may have been taken. */      extracts may have been taken. */
1605    
1606      do ecode += GET(ecode, 1); while (*ecode == OP_ALT);      do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
1607    
# Line 1428  for (;;) Line 1611  for (;;)
1611      /* For a non-repeating ket, just continue at this level. This also      /* For a non-repeating ket, just continue at this level. This also
1612      happens for a repeating ket if no characters were matched in the group.      happens for a repeating ket if no characters were matched in the group.
1613      This is the forcible breaking of infinite loops as implemented in Perl      This is the forcible breaking of infinite loops as implemented in Perl
1614      5.005. If there is an options reset, it will get obeyed in the normal      5.005. */
     course of events. */  
1615    
1616      if (*ecode == OP_KET || eptr == saved_eptr)      if (*ecode == OP_KET || eptr == saved_eptr)
1617        {        {
# Line 1439  for (;;) Line 1621  for (;;)
1621    
1622      /* 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
1623      preceding bracket, in the appropriate order. The second "call" of match()      preceding bracket, in the appropriate order. The second "call" of match()
1624      uses tail recursion, to avoid using another stack frame. We need to reset      uses tail recursion, to avoid using another stack frame. */
     any options that changed within the bracket before re-running it, so  
     check the next opcode. */  
   
     if (ecode[1+LINK_SIZE] == OP_OPT)  
       {  
       ims = (ims & ~PCRE_IMS) | ecode[4];  
       DPRINTF(("ims set to %02lx at group repeat\n", ims));  
       }  
1625    
1626      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1627        {        {
1628        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM8);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM8);
1629        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1630        ecode = prev;        ecode = prev;
       flags = 0;  
       goto TAIL_RECURSE;  
1631        }        }
1632      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1633        {        {
1634        RMATCH(eptr, prev, offset_top, md, ims, eptrb, match_cbegroup, RM9);        md->match_function_type = MATCH_CBEGROUP;
1635          RMATCH(eptr, prev, offset_top, md, eptrb, RM9);
1636        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1637        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = 0;  
       goto TAIL_RECURSE;  
1638        }        }
1639        goto TAIL_RECURSE;
1640    
1641      /* Control never gets here */      /* Control never gets here */
1642    
1643      /* An alternation is the end of a branch; scan along to find the end of the      /* An alternation is the end of a branch; scan along to find the end of the
# Line 1479  for (;;) Line 1652  for (;;)
1652      at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets      at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets
1653      with fixed upper repeat limits are compiled as a number of copies, with the      with fixed upper repeat limits are compiled as a number of copies, with the
1654      optional ones preceded by BRAZERO or BRAMINZERO. */      optional ones preceded by BRAZERO or BRAMINZERO. */
1655    
1656      case OP_BRAZERO:      case OP_BRAZERO:
1657        {      next = ecode + 1;
1658        next = ecode+1;      RMATCH(eptr, next, offset_top, md, eptrb, RM10);
1659        RMATCH(eptr, next, offset_top, md, ims, eptrb, 0, RM10);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1660        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      do next += GET(next, 1); while (*next == OP_ALT);
1661        do next += GET(next,1); while (*next == OP_ALT);      ecode = next + 1 + LINK_SIZE;
       ecode = next + 1 + LINK_SIZE;  
       }  
1662      break;      break;
1663    
1664      case OP_BRAMINZERO:      case OP_BRAMINZERO:
1665        {      next = ecode + 1;
1666        next = ecode+1;      do next += GET(next, 1); while (*next == OP_ALT);
1667        do next += GET(next, 1); while (*next == OP_ALT);      RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
1668        RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0, RM11);      if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1669        if (rrc != MATCH_NOMATCH) RRETURN(rrc);      ecode++;
       ecode++;  
       }  
1670      break;      break;
1671    
1672      case OP_SKIPZERO:      case OP_SKIPZERO:
1673        {      next = ecode+1;
1674        next = ecode+1;      do next += GET(next,1); while (*next == OP_ALT);
1675        do next += GET(next,1); while (*next == OP_ALT);      ecode = next + 1 + LINK_SIZE;
       ecode = next + 1 + LINK_SIZE;  
       }  
1676      break;      break;
1677    
1678        /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
1679        here; just jump to the group, with allow_zero set TRUE. */
1680    
1681        case OP_BRAPOSZERO:
1682        op = *(++ecode);
1683        allow_zero = TRUE;
1684        if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
1685          goto POSSESSIVE_NON_CAPTURE;
1686    
1687      /* End of a group, repeated or non-repeating. */      /* End of a group, repeated or non-repeating. */
1688    
1689      case OP_KET:      case OP_KET:
1690      case OP_KETRMIN:      case OP_KETRMIN:
1691      case OP_KETRMAX:      case OP_KETRMAX:
1692        case OP_KETRPOS:
1693      prev = ecode - GET(ecode, 1);      prev = ecode - GET(ecode, 1);
1694    
1695      /* If this was a group that remembered the subject start, in order to break      /* If this was a group that remembered the subject start, in order to break
# Line 1547  for (;;) Line 1724  for (;;)
1724      a recurse into group 0, so it won't be picked up here. Instead, we catch it      a recurse into group 0, so it won't be picked up here. Instead, we catch it
1725      when the OP_END is reached. Other recursion is handled here. */      when the OP_END is reached. Other recursion is handled here. */
1726    
1727      if (*prev == OP_CBRA || *prev == OP_SCBRA)      if (*prev == OP_CBRA || *prev == OP_SCBRA ||
1728            *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
1729        {        {
1730        number = GET2(prev, 1+LINK_SIZE);        number = GET2(prev, 1+LINK_SIZE);
1731        offset = number << 1;        offset = number << 1;
# Line 1560  for (;;) Line 1738  for (;;)
1738        md->capture_last = number;        md->capture_last = number;
1739        if (offset >= md->offset_max) md->offset_overflow = TRUE; else        if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1740          {          {
1741            /* If offset is greater than offset_top, it means that we are
1742            "skipping" a capturing group, and that group's offsets must be marked
1743            unset. In earlier versions of PCRE, all the offsets were unset at the
1744            start of matching, but this doesn't work because atomic groups and
1745            assertions can cause a value to be set that should later be unset.
1746            Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as
1747            part of the atomic group, but this is not on the final matching path,
1748            so must be unset when 2 is set. (If there is no group 2, there is no
1749            problem, because offset_top will then be 2, indicating no capture.) */
1750    
1751            if (offset > offset_top)
1752              {
1753              register int *iptr = md->offset_vector + offset_top;
1754              register int *iend = md->offset_vector + offset;
1755              while (iptr < iend) *iptr++ = -1;
1756              }
1757    
1758            /* Now make the extraction */
1759    
1760          md->offset_vector[offset] =          md->offset_vector[offset] =
1761            md->offset_vector[md->offset_end - number];            md->offset_vector[md->offset_end - number];
1762          md->offset_vector[offset+1] = (int)(eptr - md->start_subject);          md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
# Line 1578  for (;;) Line 1775  for (;;)
1775            rec->saved_max * sizeof(int));            rec->saved_max * sizeof(int));
1776          offset_top = rec->save_offset_top;          offset_top = rec->save_offset_top;
1777          ecode = rec->after_call;          ecode = rec->after_call;
         ims = original_ims;  
1778          break;          break;
1779          }          }
1780        }        }
1781    
     /* For both capturing and non-capturing groups, reset the value of the ims  
     flags, in case they got changed during the group. */  
   
     ims = original_ims;  
     DPRINTF(("ims reset to %02lx\n", ims));  
   
1782      /* For a non-repeating ket, just continue at this level. This also      /* For a non-repeating ket, just continue at this level. This also
1783      happens for a repeating ket if no characters were matched in the group.      happens for a repeating ket if no characters were matched in the group.
1784      This is the forcible breaking of infinite loops as implemented in Perl      This is the forcible breaking of infinite loops as implemented in Perl
# Line 1600  for (;;) Line 1790  for (;;)
1790        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
1791        break;        break;
1792        }        }
1793    
1794      /* The repeating kets try the rest of the pattern or restart from the      /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
1795      preceding bracket, in the appropriate order. In the second case, we can use      and return the MATCH_KETRPOS. This makes it possible to do the repeats one
1796      tail recursion to avoid using another stack frame, unless we have an      at a time from the outer level, thus saving stack. */
1797    
1798        if (*ecode == OP_KETRPOS)
1799          {
1800          md->end_match_ptr = eptr;
1801          md->end_offset_top = offset_top;
1802          RRETURN(MATCH_KETRPOS);
1803          }
1804    
1805        /* The normal repeating kets try the rest of the pattern or restart from
1806        the preceding bracket, in the appropriate order. In the second case, we can
1807        use tail recursion to avoid using another stack frame, unless we have an
1808      unlimited repeat of a group that can match an empty string. */      unlimited repeat of a group that can match an empty string. */
1809    
     flags = (*prev >= OP_SBRA)? match_cbegroup : 0;  
   
1810      if (*ecode == OP_KETRMIN)      if (*ecode == OP_KETRMIN)
1811        {        {
1812        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM12);        RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
1813        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1814        if (flags != 0)    /* Could match an empty string */        if (*prev >= OP_SBRA)    /* Could match an empty string */
1815          {          {
1816          RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM50);          md->match_function_type = MATCH_CBEGROUP;
1817            RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
1818          RRETURN(rrc);          RRETURN(rrc);
1819          }          }
1820        ecode = prev;        ecode = prev;
# Line 1622  for (;;) Line 1822  for (;;)
1822        }        }
1823      else  /* OP_KETRMAX */      else  /* OP_KETRMAX */
1824        {        {
1825        RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM13);        if (*prev >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1826          RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
1827        if (rrc != MATCH_NOMATCH) RRETURN(rrc);        if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1828        ecode += 1 + LINK_SIZE;        ecode += 1 + LINK_SIZE;
       flags = 0;  
1829        goto TAIL_RECURSE;        goto TAIL_RECURSE;
1830        }        }
1831      /* Control never gets here */      /* Control never gets here */
1832    
1833      /* Start of subject unless notbol, or after internal newline if multiline */      /* Not multiline mode: start of subject assertion, unless notbol. */
1834    
1835      case OP_CIRC:      case OP_CIRC:
1836      if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);      if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
1837      if ((ims & PCRE_MULTILINE) != 0)  
       {  
       if (eptr != md->start_subject &&  
           (eptr == md->end_subject || !WAS_NEWLINE(eptr)))  
         MRRETURN(MATCH_NOMATCH);  
       ecode++;  
       break;  
       }  
     /* ... else fall through */  
   
1838      /* Start of subject assertion */      /* Start of subject assertion */
1839    
1840      case OP_SOD:      case OP_SOD:
1841      if (eptr != md->start_subject) MRRETURN(MATCH_NOMATCH);      if (eptr != md->start_subject) MRRETURN(MATCH_NOMATCH);
1842      ecode++;      ecode++;
1843      break;      break;
1844    
1845        /* Multiline mode: start of subject unless notbol, or after any newline. */
1846    
1847        case OP_CIRCM:
1848        if (md->notbol && eptr == md->start_subject) MRRETURN(MATCH_NOMATCH);
1849        if (eptr != md->start_subject &&
1850            (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
1851          MRRETURN(MATCH_NOMATCH);
1852        ecode++;
1853        break;
1854    
1855      /* Start of match assertion */      /* Start of match assertion */
1856    
# Line 1665  for (;;) Line 1866  for (;;)
1866      ecode++;      ecode++;
1867      break;      break;
1868    
1869      /* Assert before internal newline if multiline, or before a terminating      /* Multiline mode: assert before any newline, or before end of subject
1870      newline unless endonly is set, else end of subject unless noteol is set. */      unless noteol is set. */
1871    
1872      case OP_DOLL:      case OP_DOLLM:
1873      if ((ims & PCRE_MULTILINE) != 0)      if (eptr < md->end_subject)
1874        {        { if (!IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH); }
       if (eptr < md->end_subject)  
         { if (!IS_NEWLINE(eptr)) MRRETURN(MATCH_NOMATCH); }  
       else  
         { if (md->noteol) MRRETURN(MATCH_NOMATCH); }  
       ecode++;  
       break;  
       }  
1875      else      else
1876        {        {
1877        if (md->noteol) MRRETURN(MATCH_NOMATCH);        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1878        if (!md->endonly)        SCHECK_PARTIAL();
         {  
         if (eptr != md->end_subject &&  
             (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))  
           MRRETURN(MATCH_NOMATCH);  
         ecode++;  
         break;  
         }  
1879        }        }
1880        ecode++;
1881        break;
1882    
1883        /* Not multiline mode: assert before a terminating newline or before end of
1884        subject unless noteol is set. */
1885    
1886        case OP_DOLL:
1887        if (md->noteol) MRRETURN(MATCH_NOMATCH);
1888        if (!md->endonly) goto ASSERT_NL_OR_EOS;
1889    
1890      /* ... else fall through for endonly */      /* ... else fall through for endonly */
1891    
1892      /* End of subject assertion (\z) */      /* End of subject assertion (\z) */
1893    
1894      case OP_EOD:      case OP_EOD:
1895      if (eptr < md->end_subject) MRRETURN(MATCH_NOMATCH);      if (eptr < md->end_subject) MRRETURN(MATCH_NOMATCH);
1896        SCHECK_PARTIAL();
1897      ecode++;      ecode++;
1898      break;      break;
1899    
1900      /* End of subject or ending \n assertion (\Z) */      /* End of subject or ending \n assertion (\Z) */
1901    
1902      case OP_EODN:      case OP_EODN:
1903      if (eptr != md->end_subject &&      ASSERT_NL_OR_EOS:
1904        if (eptr < md->end_subject &&
1905          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))          (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
1906        MRRETURN(MATCH_NOMATCH);        MRRETURN(MATCH_NOMATCH);
1907    
1908        /* Either at end of string or \n before end. */
1909    
1910        SCHECK_PARTIAL();
1911      ecode++;      ecode++;
1912      break;      break;
1913    
# Line 1966  for (;;) Line 2169  for (;;)
2169      switch(c)      switch(c)
2170        {        {
2171        default: MRRETURN(MATCH_NOMATCH);        default: MRRETURN(MATCH_NOMATCH);
2172    
2173        case 0x000d:        case 0x000d:
2174        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;        if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
2175        break;        break;
# Line 2218  for (;;) Line 2422  for (;;)
2422      loops). */      loops). */
2423    
2424      case OP_REF:      case OP_REF:
2425        {      case OP_REFI:
2426        offset = GET2(ecode, 1) << 1;               /* Doubled ref number */      caseless = op == OP_REFI;
2427        ecode += 3;      offset = GET2(ecode, 1) << 1;               /* Doubled ref number */
2428        ecode += 3;
2429    
2430        /* If the reference is unset, there are two possibilities:      /* If the reference is unset, there are two possibilities:
2431    
2432        (a) In the default, Perl-compatible state, set the length to be longer      (a) In the default, Perl-compatible state, set the length negative;
2433        than the amount of subject left; this ensures that every attempt at a      this ensures that every attempt at a match fails. We can't just fail
2434        match fails. We can't just fail here, because of the possibility of      here, because of the possibility of quantifiers with zero minima.
       quantifiers with zero minima.  
2435    
2436        (b) If the JavaScript compatibility flag is set, set the length to zero      (b) If the JavaScript compatibility flag is set, set the length to zero
2437        so that the back reference matches an empty string.      so that the back reference matches an empty string.
2438    
2439        Otherwise, set the length to the length of what was matched by the      Otherwise, set the length to the length of what was matched by the
2440        referenced subpattern. */      referenced subpattern. */
2441    
2442        if (offset >= offset_top || md->offset_vector[offset] < 0)      if (offset >= offset_top || md->offset_vector[offset] < 0)
2443          length = (md->jscript_compat)? 0 : (int)(md->end_subject - eptr + 1);        length = (md->jscript_compat)? 0 : -1;
2444        else      else
2445          length = md->offset_vector[offset+1] - md->offset_vector[offset];        length = md->offset_vector[offset+1] - md->offset_vector[offset];
2446    
2447        /* Set up for repetition, or handle the non-repeated case */      /* Set up for repetition, or handle the non-repeated case */
2448    
2449        switch (*ecode)      switch (*ecode)
2450          {        {
2451          case OP_CRSTAR:        case OP_CRSTAR:
2452          case OP_CRMINSTAR:        case OP_CRMINSTAR:
2453          case OP_CRPLUS:        case OP_CRPLUS:
2454          case OP_CRMINPLUS:        case OP_CRMINPLUS:
2455          case OP_CRQUERY:        case OP_CRQUERY:
2456          case OP_CRMINQUERY:        case OP_CRMINQUERY:
2457          c = *ecode++ - OP_CRSTAR;        c = *ecode++ - OP_CRSTAR;
2458          minimize = (c & 1) != 0;        minimize = (c & 1) != 0;
2459          min = rep_min[c];                 /* Pick up values from tables; */        min = rep_min[c];                 /* Pick up values from tables; */
2460          max = rep_max[c];                 /* zero for max => infinity */        max = rep_max[c];                 /* zero for max => infinity */
2461          if (max == 0) max = INT_MAX;        if (max == 0) max = INT_MAX;
2462          break;        break;
2463    
2464          case OP_CRRANGE:        case OP_CRRANGE:
2465          case OP_CRMINRANGE:        case OP_CRMINRANGE:
2466          minimize = (*ecode == OP_CRMINRANGE);        minimize = (*ecode == OP_CRMINRANGE);
2467          min = GET2(ecode, 1);        min = GET2(ecode, 1);
2468          max = GET2(ecode, 3);        max = GET2(ecode, 3);
2469          if (max == 0) max = INT_MAX;        if (max == 0) max = INT_MAX;
2470          ecode += 5;        ecode += 5;
2471          break;        break;
2472    
2473          default:               /* No repeat follows */        default:               /* No repeat follows */
2474          if (!match_ref(offset, eptr, length, md, ims))        if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
2475            {          {
2476            CHECK_PARTIAL();          CHECK_PARTIAL();
2477            MRRETURN(MATCH_NOMATCH);          MRRETURN(MATCH_NOMATCH);
           }  
         eptr += length;  
         continue;              /* With the main loop */  
2478          }          }
2479          eptr += length;
2480          continue;              /* With the main loop */
2481          }
2482    
2483        /* If the length of the reference is zero, just continue with the      /* Handle repeated back references. If the length of the reference is
2484        main loop. */      zero, just continue with the main loop. */
2485    
2486        if (length == 0) continue;      if (length == 0) continue;
2487    
2488        /* First, ensure the minimum number of matches are present. We get back      /* First, ensure the minimum number of matches are present. We get back
2489        the length of the reference string explicitly rather than passing the      the length of the reference string explicitly rather than passing the
2490        address of eptr, so that eptr can be a register variable. */      address of eptr, so that eptr can be a register variable. */
2491    
2492        for (i = 1; i <= min; i++)      for (i = 1; i <= min; i++)
2493          {
2494          int slength;
2495          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2496          {          {
2497          if (!match_ref(offset, eptr, length, md, ims))          CHECK_PARTIAL();
2498            {          MRRETURN(MATCH_NOMATCH);
           CHECK_PARTIAL();  
           MRRETURN(MATCH_NOMATCH);  
           }  
         eptr += length;  
2499          }          }
2500          eptr += slength;
2501          }
2502    
2503        /* If min = max, continue at the same level without recursion.      /* If min = max, continue at the same level without recursion.
2504        They are not both allowed to be zero. */      They are not both allowed to be zero. */
2505    
2506        if (min == max) continue;      if (min == max) continue;
2507    
2508        /* If minimizing, keep trying and advancing the pointer */      /* If minimizing, keep trying and advancing the pointer */
2509    
2510        if (minimize)      if (minimize)
2511          {
2512          for (fi = min;; fi++)
2513          {          {
2514          for (fi = min;; fi++)          int slength;
2515            RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
2516            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2517            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2518            if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2519            {            {
2520            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM14);            CHECK_PARTIAL();
2521            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            MRRETURN(MATCH_NOMATCH);
           if (fi >= max) MRRETURN(MATCH_NOMATCH);  
           if (!match_ref(offset, eptr, length, md, ims))  
             {  
             CHECK_PARTIAL();  
             MRRETURN(MATCH_NOMATCH);  
             }  
           eptr += length;  
2522            }            }
2523          /* Control never gets here */          eptr += slength;
2524          }          }
2525          /* Control never gets here */
2526          }
2527    
2528        /* If maximizing, find the longest string and work backwards */      /* If maximizing, find the longest string and work backwards */
2529    
2530        else      else
2531          {
2532          pp = eptr;
2533          for (i = min; i < max; i++)
2534          {          {
2535          pp = eptr;          int slength;
2536          for (i = min; i < max; i++)          if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
           {  
           if (!match_ref(offset, eptr, length, md, ims))  
             {  
             CHECK_PARTIAL();  
             break;  
             }  
           eptr += length;  
           }  
         while (eptr >= pp)  
2537            {            {
2538            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM15);            CHECK_PARTIAL();
2539            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            break;
           eptr -= length;  
2540            }            }
2541          MRRETURN(MATCH_NOMATCH);          eptr += slength;
2542            }
2543          while (eptr >= pp)
2544            {
2545            RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
2546            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2547            eptr -= length;
2548          }          }
2549          MRRETURN(MATCH_NOMATCH);
2550        }        }
2551      /* Control never gets here */      /* Control never gets here */
2552    
# Line 2446  for (;;) Line 2652  for (;;)
2652            {            {
2653            for (fi = min;; fi++)            for (fi = min;; fi++)
2654              {              {
2655              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM16);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
2656              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2657              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2658              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 2471  for (;;) Line 2677  for (;;)
2677            {            {
2678            for (fi = min;; fi++)            for (fi = min;; fi++)
2679              {              {
2680              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM17);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
2681              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2682              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
2683              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 2517  for (;;) Line 2723  for (;;)
2723              }              }
2724            for (;;)            for (;;)
2725              {              {
2726              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM18);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
2727              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2728              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
2729              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 2540  for (;;) Line 2746  for (;;)
2746              }              }
2747            while (eptr >= pp)            while (eptr >= pp)
2748              {              {
2749              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM19);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM19);
2750              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2751              eptr--;              eptr--;
2752              }              }
# Line 2616  for (;;) Line 2822  for (;;)
2822          {          {
2823          for (fi = min;; fi++)          for (fi = min;; fi++)
2824            {            {
2825            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM20);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM20);
2826            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2827            if (fi >= max) MRRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
2828            if (eptr >= md->end_subject)            if (eptr >= md->end_subject)
# Line 2649  for (;;) Line 2855  for (;;)
2855            }            }
2856          for(;;)          for(;;)
2857            {            {
2858            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM21);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM21);
2859            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2860            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
2861            if (utf8) BACKCHAR(eptr);            if (utf8) BACKCHAR(eptr);
# Line 2694  for (;;) Line 2900  for (;;)
2900    
2901      /* Match a single character, caselessly */      /* Match a single character, caselessly */
2902    
2903      case OP_CHARNC:      case OP_CHARI:
2904  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2905      if (utf8)      if (utf8)
2906        {        {
# Line 2754  for (;;) Line 2960  for (;;)
2960      /* Match a single character repeatedly. */      /* Match a single character repeatedly. */
2961    
2962      case OP_EXACT:      case OP_EXACT:
2963        case OP_EXACTI:
2964      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
2965      ecode += 3;      ecode += 3;
2966      goto REPEATCHAR;      goto REPEATCHAR;
2967    
2968      case OP_POSUPTO:      case OP_POSUPTO:
2969        case OP_POSUPTOI:
2970      possessive = TRUE;      possessive = TRUE;
2971      /* Fall through */      /* Fall through */
2972    
2973      case OP_UPTO:      case OP_UPTO:
2974        case OP_UPTOI:
2975      case OP_MINUPTO:      case OP_MINUPTO:
2976        case OP_MINUPTOI:
2977      min = 0;      min = 0;
2978      max = GET2(ecode, 1);      max = GET2(ecode, 1);
2979      minimize = *ecode == OP_MINUPTO;      minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI;
2980      ecode += 3;      ecode += 3;
2981      goto REPEATCHAR;      goto REPEATCHAR;
2982    
2983      case OP_POSSTAR:      case OP_POSSTAR:
2984        case OP_POSSTARI:
2985      possessive = TRUE;      possessive = TRUE;
2986      min = 0;      min = 0;
2987      max = INT_MAX;      max = INT_MAX;
# Line 2778  for (;;) Line 2989  for (;;)
2989      goto REPEATCHAR;      goto REPEATCHAR;
2990    
2991      case OP_POSPLUS:      case OP_POSPLUS:
2992        case OP_POSPLUSI:
2993      possessive = TRUE;      possessive = TRUE;
2994      min = 1;      min = 1;
2995      max = INT_MAX;      max = INT_MAX;
# Line 2785  for (;;) Line 2997  for (;;)
2997      goto REPEATCHAR;      goto REPEATCHAR;
2998    
2999      case OP_POSQUERY:      case OP_POSQUERY:
3000        case OP_POSQUERYI:
3001      possessive = TRUE;      possessive = TRUE;
3002      min = 0;      min = 0;
3003      max = 1;      max = 1;
# Line 2792  for (;;) Line 3005  for (;;)
3005      goto REPEATCHAR;      goto REPEATCHAR;
3006    
3007      case OP_STAR:      case OP_STAR:
3008        case OP_STARI:
3009      case OP_MINSTAR:      case OP_MINSTAR:
3010        case OP_MINSTARI:
3011      case OP_PLUS:      case OP_PLUS:
3012        case OP_PLUSI:
3013      case OP_MINPLUS:      case OP_MINPLUS:
3014        case OP_MINPLUSI:
3015      case OP_QUERY:      case OP_QUERY:
3016        case OP_QUERYI:
3017      case OP_MINQUERY:      case OP_MINQUERY:
3018      c = *ecode++ - OP_STAR;      case OP_MINQUERYI:
3019        c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI);
3020      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
   
3021      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3022      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
3023      if (max == 0) max = INT_MAX;      if (max == 0) max = INT_MAX;
# Line 2822  for (;;) Line 3040  for (;;)
3040          {          {
3041  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3042          unsigned int othercase;          unsigned int othercase;
3043          if ((ims & PCRE_CASELESS) != 0 &&          if (op >= OP_STARI &&     /* Caseless */
3044              (othercase = UCD_OTHERCASE(fc)) != fc)              (othercase = UCD_OTHERCASE(fc)) != fc)
3045            oclength = _pcre_ord2utf8(othercase, occhars);            oclength = _pcre_ord2utf8(othercase, occhars);
3046          else oclength = 0;          else oclength = 0;
# Line 2850  for (;;) Line 3068  for (;;)
3068            {            {
3069            for (fi = min;; fi++)            for (fi = min;; fi++)
3070              {              {
3071              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM22);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM22);
3072              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3073              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3074              if (eptr <= md->end_subject - length &&              if (eptr <= md->end_subject - length &&
# Line 2892  for (;;) Line 3110  for (;;)
3110    
3111            for(;;)            for(;;)
3112              {              {
3113              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM23);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM23);
3114              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3115              if (eptr == pp) { MRRETURN(MATCH_NOMATCH); }              if (eptr == pp) { MRRETURN(MATCH_NOMATCH); }
3116  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 2929  for (;;) Line 3147  for (;;)
3147      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,      DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3148        max, eptr));        max, eptr));
3149    
3150      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_STARI)  /* Caseless */
3151        {        {
3152        fc = md->lcc[fc];        fc = md->lcc[fc];
3153        for (i = 1; i <= min; i++)        for (i = 1; i <= min; i++)
# Line 2946  for (;;) Line 3164  for (;;)
3164          {          {
3165          for (fi = min;; fi++)          for (fi = min;; fi++)
3166            {            {
3167            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM24);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM24);
3168            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3169            if (fi >= max) MRRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3170            if (eptr >= md->end_subject)            if (eptr >= md->end_subject)
# Line 2976  for (;;) Line 3194  for (;;)
3194    
3195          while (eptr >= pp)          while (eptr >= pp)
3196            {            {
3197            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM25);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM25);
3198            eptr--;            eptr--;
3199            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3200            }            }
# Line 3005  for (;;) Line 3223  for (;;)
3223          {          {
3224          for (fi = min;; fi++)          for (fi = min;; fi++)
3225            {            {
3226            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM26);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM26);
3227            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3228            if (fi >= max) MRRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
3229            if (eptr >= md->end_subject)            if (eptr >= md->end_subject)
# Line 3034  for (;;) Line 3252  for (;;)
3252    
3253          while (eptr >= pp)          while (eptr >= pp)
3254            {            {
3255            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM27);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM27);
3256            eptr--;            eptr--;
3257            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3258            }            }
# Line 3047  for (;;) Line 3265  for (;;)
3265      checking can be multibyte. */      checking can be multibyte. */
3266    
3267      case OP_NOT:      case OP_NOT:
3268        case OP_NOTI:
3269      if (eptr >= md->end_subject)      if (eptr >= md->end_subject)
3270        {        {
3271        SCHECK_PARTIAL();        SCHECK_PARTIAL();
# Line 3054  for (;;) Line 3273  for (;;)
3273        }        }
3274      ecode++;      ecode++;
3275      GETCHARINCTEST(c, eptr);      GETCHARINCTEST(c, eptr);
3276      if ((ims & PCRE_CASELESS) != 0)      if (op == OP_NOTI)         /* The caseless case */
3277        {        {
3278  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3279        if (c < 256)        if (c < 256)
# Line 3062  for (;;) Line 3281  for (;;)
3281        c = md->lcc[c];        c = md->lcc[c];
3282        if (md->lcc[*ecode++] == c) MRRETURN(MATCH_NOMATCH);        if (md->lcc[*ecode++] == c) MRRETURN(MATCH_NOMATCH);
3283        }        }
3284      else      else    /* Caseful */
3285        {        {
3286        if (*ecode++ == c) MRRETURN(MATCH_NOMATCH);        if (*ecode++ == c) MRRETURN(MATCH_NOMATCH);
3287        }        }
# Line 3076  for (;;) Line 3295  for (;;)
3295      about... */      about... */
3296    
3297      case OP_NOTEXACT:      case OP_NOTEXACT:
3298        case OP_NOTEXACTI:
3299      min = max = GET2(ecode, 1);      min = max = GET2(ecode, 1);
3300      ecode += 3;      ecode += 3;
3301      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3302    
3303      case OP_NOTUPTO:      case OP_NOTUPTO:
3304        case OP_NOTUPTOI:
3305      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
3306        case OP_NOTMINUPTOI:
3307      min = 0;      min = 0;
3308      max = GET2(ecode, 1);      max = GET2(ecode, 1);
3309      minimize = *ecode == OP_NOTMINUPTO;      minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI;
3310      ecode += 3;      ecode += 3;
3311      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3312    
3313      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
3314        case OP_NOTPOSSTARI:
3315      possessive = TRUE;      possessive = TRUE;
3316      min = 0;      min = 0;
3317      max = INT_MAX;      max = INT_MAX;
# Line 3096  for (;;) Line 3319  for (;;)
3319      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3320    
3321      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
3322        case OP_NOTPOSPLUSI:
3323      possessive = TRUE;      possessive = TRUE;
3324      min = 1;      min = 1;
3325      max = INT_MAX;      max = INT_MAX;
# Line 3103  for (;;) Line 3327  for (;;)
3327      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3328    
3329      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
3330        case OP_NOTPOSQUERYI:
3331      possessive = TRUE;      possessive = TRUE;
3332      min = 0;      min = 0;
3333      max = 1;      max = 1;
# Line 3110  for (;;) Line 3335  for (;;)
3335      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3336    
3337      case OP_NOTPOSUPTO:      case OP_NOTPOSUPTO:
3338        case OP_NOTPOSUPTOI:
3339      possessive = TRUE;      possessive = TRUE;
3340      min = 0;      min = 0;
3341      max = GET2(ecode, 1);      max = GET2(ecode, 1);
# Line 3117  for (;;) Line 3343  for (;;)
3343      goto REPEATNOTCHAR;      goto REPEATNOTCHAR;
3344    
3345      case OP_NOTSTAR:      case OP_NOTSTAR:
3346        case OP_NOTSTARI:
3347      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
3348        case OP_NOTMINSTARI:
3349      case OP_NOTPLUS:      case OP_NOTPLUS:
3350        case OP_NOTPLUSI:
3351      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
3352        case OP_NOTMINPLUSI:
3353      case OP_NOTQUERY:      case OP_NOTQUERY:
3354        case OP_NOTQUERYI:
3355      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
3356      c = *ecode++ - OP_NOTSTAR;      case OP_NOTMINQUERYI:
3357        c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
3358      minimize = (c & 1) != 0;      minimize = (c & 1) != 0;
3359      min = rep_min[c];                 /* Pick up values from tables; */      min = rep_min[c];                 /* Pick up values from tables; */
3360      max = rep_max[c];                 /* zero for max => infinity */      max = rep_max[c];                 /* zero for max => infinity */
# Line 3144  for (;;) Line 3376  for (;;)
3376      DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max,      DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3377        max, eptr));        max, eptr));
3378    
3379      if ((ims & PCRE_CASELESS) != 0)      if (op >= OP_NOTSTARI)     /* Caseless */
3380        {        {
3381        fc = md->lcc[fc];        fc = md->lcc[fc];
3382    
# Line 3192  for (;;) Line 3424  for (;;)
3424            register unsigned int d;            register unsigned int d;
3425            for (fi = min;; fi++)            for (fi = min;; fi++)
3426              {              {
3427              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM28);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM28);
3428              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3429              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3430              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 3211  for (;;) Line 3443  for (;;)
3443            {            {
3444            for (fi = min;; fi++)            for (fi = min;; fi++)
3445              {              {
3446              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM29);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM29);
3447              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3448              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3449              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 3252  for (;;) Line 3484  for (;;)
3484          if (possessive) continue;          if (possessive) continue;
3485          for(;;)          for(;;)
3486              {              {
3487              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM30);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM30);
3488              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3489              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3490              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 3275  for (;;) Line 3507  for (;;)
3507            if (possessive) continue;            if (possessive) continue;
3508            while (eptr >= pp)            while (eptr >= pp)
3509              {              {
3510              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM31);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM31);
3511              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3512              eptr--;              eptr--;
3513              }              }
# Line 3332  for (;;) Line 3564  for (;;)
3564            register unsigned int d;            register unsigned int d;
3565            for (fi = min;; fi++)            for (fi = min;; fi++)
3566              {              {
3567              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM32);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM32);
3568              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3569              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3570              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 3350  for (;;) Line 3582  for (;;)
3582            {            {
3583            for (fi = min;; fi++)            for (fi = min;; fi++)
3584              {              {
3585              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM33);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM33);
3586              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3587              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
3588              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 3390  for (;;) Line 3622  for (;;)
3622            if (possessive) continue;            if (possessive) continue;
3623            for(;;)            for(;;)
3624              {              {
3625              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM34);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM34);
3626              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3627              if (eptr-- == pp) break;        /* Stop if tried at original pos */              if (eptr-- == pp) break;        /* Stop if tried at original pos */
3628              BACKCHAR(eptr);              BACKCHAR(eptr);
# Line 3413  for (;;) Line 3645  for (;;)
3645            if (possessive) continue;            if (possessive) continue;
3646            while (eptr >= pp)            while (eptr >= pp)
3647              {              {
3648              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM35);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM35);
3649              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3650              eptr--;              eptr--;
3651              }              }
# Line 3740  for (;;) Line 3972  for (;;)
3972            switch(c)            switch(c)
3973              {              {
3974              default: MRRETURN(MATCH_NOMATCH);              default: MRRETURN(MATCH_NOMATCH);
3975    
3976              case 0x000d:              case 0x000d:
3977              if (eptr < md->end_subject && *eptr == 0x0a) eptr++;              if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
3978              break;              break;
# Line 4016  for (;;) Line 4249  for (;;)
4249            switch(*eptr++)            switch(*eptr++)
4250              {              {
4251              default: MRRETURN(MATCH_NOMATCH);              default: MRRETURN(MATCH_NOMATCH);
4252    
4253              case 0x000d:              case 0x000d:
4254              if (eptr < md->end_subject && *eptr == 0x0a) eptr++;              if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
4255              break;              break;
4256    
4257              case 0x000a:              case 0x000a:
4258              break;              break;
4259    
# Line 4208  for (;;) Line 4443  for (;;)
4443            case PT_ANY:            case PT_ANY:
4444            for (fi = min;; fi++)            for (fi = min;; fi++)
4445              {              {
4446              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM36);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM36);
4447              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4448              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
4449              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 4224  for (;;) Line 4459  for (;;)
4459            case PT_LAMP:            case PT_LAMP:
4460            for (fi = min;; fi++)            for (fi = min;; fi++)
4461              {              {
4462              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM37);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM37);
4463              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4464              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
4465              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 4244  for (;;) Line 4479  for (;;)
4479            case PT_GC:            case PT_GC:
4480            for (fi = min;; fi++)            for (fi = min;; fi++)
4481              {              {
4482              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM38);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM38);
4483              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4484              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
4485              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 4262  for (;;) Line 4497  for (;;)
4497            case PT_PC:            case PT_PC:
4498            for (fi = min;; fi++)            for (fi = min;; fi++)
4499              {              {
4500              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM39);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM39);
4501              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4502              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
4503              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 4280  for (;;) Line 4515  for (;;)
4515            case PT_SC:            case PT_SC:
4516            for (fi = min;; fi++)            for (fi = min;; fi++)
4517              {              {
4518              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM40);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM40);
4519              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4520              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
4521              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 4298  for (;;) Line 4533  for (;;)
4533            case PT_ALNUM:            case PT_ALNUM:
4534            for (fi = min;; fi++)            for (fi = min;; fi++)
4535              {              {
4536              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM59);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM59);
4537              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4538              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
4539              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 4317  for (;;) Line 4552  for (;;)
4552            case PT_SPACE:    /* Perl space */            case PT_SPACE:    /* Perl space */
4553            for (fi = min;; fi++)            for (fi = min;; fi++)
4554              {              {
4555              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM60);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM60);
4556              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4557              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
4558              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 4337  for (;;) Line 4572  for (;;)
4572            case PT_PXSPACE:  /* POSIX space */            case PT_PXSPACE:  /* POSIX space */
4573            for (fi = min;; fi++)            for (fi = min;; fi++)
4574              {              {
4575              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM61);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM61);
4576              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4577              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
4578              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 4357  for (;;) Line 4592  for (;;)
4592            case PT_WORD:            case PT_WORD:
4593            for (fi = min;; fi++)            for (fi = min;; fi++)
4594              {              {
4595              RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM62);              RMATCH(eptr, ecode, offset_top, md, eptrb, RM62);
4596              if (rrc != MATCH_NOMATCH) RRETURN(rrc);              if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4597              if (fi >= max) MRRETURN(MATCH_NOMATCH);              if (fi >= max) MRRETURN(MATCH_NOMATCH);
4598              if (eptr >= md->end_subject)              if (eptr >= md->end_subject)
# Line 4389  for (;;) Line 4624  for (;;)
4624          {          {
4625          for (fi = min;; fi++)          for (fi = min;; fi++)
4626            {            {
4627            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM41);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM41);
4628            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4629            if (fi >= max) MRRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
4630            if (eptr >= md->end_subject)            if (eptr >= md->end_subject)
# Line 4421  for (;;) Line 4656  for (;;)
4656          {          {
4657          for (fi = min;; fi++)          for (fi = min;; fi++)
4658            {            {
4659            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM42);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM42);
4660            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4661            if (fi >= max) MRRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
4662            if (eptr >= md->end_subject)            if (eptr >= md->end_subject)
# Line 4584  for (;;) Line 4819  for (;;)
4819          {          {
4820          for (fi = min;; fi++)          for (fi = min;; fi++)
4821            {            {
4822            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM43);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM43);
4823            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4824            if (fi >= max) MRRETURN(MATCH_NOMATCH);            if (fi >= max) MRRETURN(MATCH_NOMATCH);
4825            if (eptr >= md->end_subject)            if (eptr >= md->end_subject)
# Line 4882  for (;;) Line 5117  for (;;)
5117          if (possessive) continue;          if (possessive) continue;
5118          for(;;)          for(;;)
5119            {            {
5120            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM44);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM44);
5121            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
5122            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
5123            if (utf8) BACKCHAR(eptr);            if (utf8) BACKCHAR(eptr);
# Line 4923  for (;;) Line 5158  for (;;)
5158    
5159          for(;;)          for(;;)
5160            {            {
5161            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM45);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM45);
5162            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
5163            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
5164            for (;;)                        /* Move back over one extended */            for (;;)                        /* Move back over one extended */
# Line 5207  for (;;) Line 5442  for (;;)
5442            RRETURN(PCRE_ERROR_INTERNAL);            RRETURN(PCRE_ERROR_INTERNAL);
5443            }            }
5444    
5445          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run. If possessive, we are
5446            done (no backing up). Otherwise, match at this position; anything other
5447            than no match is immediately returned. For nomatch, back up one
5448            character, unless we are matching \R and the last thing matched was
5449            \r\n, in which case, back up two bytes. */
5450    
5451          if (possessive) continue;          if (possessive) continue;
5452          for(;;)          for(;;)
5453            {            {
5454            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM46);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM46);
5455            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
5456            if (eptr-- == pp) break;        /* Stop if tried at original pos */            if (eptr-- == pp) break;        /* Stop if tried at original pos */
5457            BACKCHAR(eptr);            BACKCHAR(eptr);
5458              if (ctype == OP_ANYNL && eptr > pp  && *eptr == '\n' &&
5459                  eptr[-1] == '\r') eptr--;
5460            }            }
5461          }          }
5462        else        else
# Line 5414  for (;;) Line 5655  for (;;)
5655            RRETURN(PCRE_ERROR_INTERNAL);            RRETURN(PCRE_ERROR_INTERNAL);
5656            }            }
5657    
5658          /* eptr is now past the end of the maximum run */          /* eptr is now past the end of the maximum run. If possessive, we are
5659            done (no backing up). Otherwise, match at this position; anything other
5660            than no match is immediately returned. For nomatch, back up one
5661            character (byte), unless we are matching \R and the last thing matched
5662            was \r\n, in which case, back up two bytes. */
5663    
5664          if (possessive) continue;          if (possessive) continue;
5665          while (eptr >= pp)          while (eptr >= pp)
5666            {            {
5667            RMATCH(eptr, ecode, offset_top, md, ims, eptrb, 0, RM47);            RMATCH(eptr, ecode, offset_top, md, eptrb, RM47);
           eptr--;  
5668            if (rrc != MATCH_NOMATCH) RRETURN(rrc);            if (rrc != MATCH_NOMATCH) RRETURN(rrc);
5669              eptr--;
5670              if (ctype == OP_ANYNL && eptr > pp  && *eptr == '\n' &&
5671                  eptr[-1] == '\r') eptr--;
5672            }            }
5673          }          }
5674    
# Line 5460  switch (frame->Xwhere) Line 5707  switch (frame->Xwhere)
5707    LBL( 9) LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(17)    LBL( 9) LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(17)
5708    LBL(19) LBL(24) LBL(25) LBL(26) LBL(27) LBL(29) LBL(31) LBL(33)    LBL(19) LBL(24) LBL(25) LBL(26) LBL(27) LBL(29) LBL(31) LBL(33)
5709    LBL(35) LBL(43) LBL(47) LBL(48) LBL(49) LBL(50) LBL(51) LBL(52)    LBL(35) LBL(43) LBL(47) LBL(48) LBL(49) LBL(50) LBL(51) LBL(52)
5710    LBL(53) LBL(54) LBL(55) LBL(56) LBL(57) LBL(58)    LBL(53) LBL(54) LBL(55) LBL(56) LBL(57) LBL(58) LBL(63)
5711  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
5712    LBL(16) LBL(18) LBL(20) LBL(21) LBL(22) LBL(23) LBL(28) LBL(30)    LBL(16) LBL(18) LBL(20) LBL(21) LBL(22) LBL(23) LBL(28) LBL(30)
5713    LBL(32) LBL(34) LBL(42) LBL(46)    LBL(32) LBL(34) LBL(42) LBL(46)
# Line 5489  Undefine all the macros that were define Line 5736  Undefine all the macros that were define
5736  #undef ecode  #undef ecode
5737  #undef mstart  #undef mstart
5738  #undef offset_top  #undef offset_top
 #undef ims  
5739  #undef eptrb  #undef eptrb
5740  #undef flags  #undef flags
5741    
# Line 5507  Undefine all the macros that were define Line 5753  Undefine all the macros that were define
5753  #undef condition  #undef condition
5754  #undef prev_is_word  #undef prev_is_word
5755    
 #undef original_ims  
   
5756  #undef ctype  #undef ctype
5757  #undef length  #undef length
5758  #undef max  #undef max
# Line 5565  pcre_exec(const pcre *argument_re, const Line 5809  pcre_exec(const pcre *argument_re, const
5809    PCRE_SPTR subject, int length, int start_offset, int options, int *offsets,    PCRE_SPTR subject, int length, int start_offset, int options, int *offsets,
5810    int offsetcount)    int offsetcount)
5811  {  {
5812  int rc, resetcount, ocount;  int rc, ocount;
5813  int first_byte = -1;  int first_byte = -1;
5814  int req_byte = -1;  int req_byte = -1;
5815  int req_byte2 = -1;  int req_byte2 = -1;
5816  int newline;  int newline;
 unsigned long int ims;  
5817  BOOL using_temporary_offsets = FALSE;  BOOL using_temporary_offsets = FALSE;
5818  BOOL anchored;  BOOL anchored;
5819  BOOL startline;  BOOL startline;
# Line 5600  if ((options & ~PUBLIC_EXEC_OPTIONS) != Line 5843  if ((options & ~PUBLIC_EXEC_OPTIONS) !=
5843  if (re == NULL || subject == NULL ||  if (re == NULL || subject == NULL ||
5844     (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;     (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;
5845  if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;  if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
5846    if (start_offset < 0 || start_offset > length) return PCRE_ERROR_BADOFFSET;
5847    
5848  /* This information is for finding all the numbers associated with a given  /* This information is for finding all the numbers associated with a given
5849  name, for condition testing. */  name, for condition testing. */
# Line 5673  utf8 = md->utf8 = (re->options & PCRE_UT Line 5917  utf8 = md->utf8 = (re->options & PCRE_UT
5917  md->use_ucp = (re->options & PCRE_UCP) != 0;  md->use_ucp = (re->options & PCRE_UCP) != 0;
5918  md->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;  md->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;
5919    
5920    /* Some options are unpacked into BOOL variables in the hope that testing
5921    them will be faster than individual option bits. */
5922    
5923  md->notbol = (options & PCRE_NOTBOL) != 0;  md->notbol = (options & PCRE_NOTBOL) != 0;
5924  md->noteol = (options & PCRE_NOTEOL) != 0;  md->noteol = (options & PCRE_NOTEOL) != 0;
5925  md->notempty = (options & PCRE_NOTEMPTY) != 0;  md->notempty = (options & PCRE_NOTEMPTY) != 0;
5926  md->notempty_atstart = (options & PCRE_NOTEMPTY_ATSTART) != 0;  md->notempty_atstart = (options & PCRE_NOTEMPTY_ATSTART) != 0;
5927  md->partial = ((options & PCRE_PARTIAL_HARD) != 0)? 2 :  md->partial = ((options & PCRE_PARTIAL_HARD) != 0)? 2 :
5928                ((options & PCRE_PARTIAL_SOFT) != 0)? 1 : 0;                ((options & PCRE_PARTIAL_SOFT) != 0)? 1 : 0;
5929    
5930    
5931  md->hitend = FALSE;  md->hitend = FALSE;
5932  md->mark = NULL;                        /* In case never set */  md->mark = NULL;                        /* In case never set */
5933    
# Line 5760  defined (though never set). So there's n Line 6009  defined (though never set). So there's n
6009  if (md->partial && (re->flags & PCRE_NOPARTIAL) != 0)  if (md->partial && (re->flags & PCRE_NOPARTIAL) != 0)
6010    return PCRE_ERROR_BADPARTIAL;    return PCRE_ERROR_BADPARTIAL;
6011    
6012  /* Check a UTF-8 string if required. Unfortunately there's no way of passing  /* Check a UTF-8 string if required. Pass back the character offset and error
6013  back the character offset. */  code for an invalid string if a results vector is available. */
6014    
6015  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
6016  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)
6017    {    {
6018    if (_pcre_valid_utf8((USPTR)subject, length) >= 0)    int erroroffset;
6019      return PCRE_ERROR_BADUTF8;    int errorcode = _pcre_valid_utf8((USPTR)subject, length, &erroroffset);
6020    if (start_offset > 0 && start_offset < length)    if (errorcode != 0)
6021      {      {
6022      int tb = ((USPTR)subject)[start_offset];      if (offsetcount >= 2)
     if (tb > 127)  
6023        {        {
6024        tb &= 0xc0;        offsets[0] = erroroffset;
6025        if (tb != 0 && tb != 0xc0) return PCRE_ERROR_BADUTF8_OFFSET;        offsets[1] = errorcode;
6026        }        }
6027      }      return (errorcode <= PCRE_UTF8_ERR5 && md->partial > 1)?
6028          PCRE_ERROR_SHORTUTF8 : PCRE_ERROR_BADUTF8;
6029        }
6030    
6031      /* Check that a start_offset points to the start of a UTF-8 character. */
6032    
6033      if (start_offset > 0 && start_offset < length &&
6034          (((USPTR)subject)[start_offset] & 0xc0) == 0x80)
6035        return PCRE_ERROR_BADUTF8_OFFSET;
6036    }    }
6037  #endif  #endif
6038    
 /* The ims options can vary during the matching as a result of the presence  
 of (?ims) items in the pattern. They are kept in a local variable so that  
 restoring at the exit of a group is easy. */  
   
 ims = re->options & (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL);  
   
6039  /* If the expression has got more back references than the offsets supplied can  /* If the expression has got more back references than the offsets supplied can
6040  hold, we get a temporary chunk of working store to use during the matching.  hold, we get a temporary chunk of working store to use during the matching.
6041  Otherwise, we can use the vector supplied, rounding down its size to a multiple  Otherwise, we can use the vector supplied, rounding down its size to a multiple
# Line 5808  md->offset_max = (2*ocount)/3; Line 6058  md->offset_max = (2*ocount)/3;
6058  md->offset_overflow = FALSE;  md->offset_overflow = FALSE;
6059  md->capture_last = -1;  md->capture_last = -1;
6060    
 /* Compute the minimum number of offsets that we need to reset each time. Doing  
 this makes a huge difference to execution time when there aren't many brackets  
 in the pattern. */  
   
 resetcount = 2 + re->top_bracket * 2;  
 if (resetcount > offsetcount) resetcount = ocount;  
   
6061  /* Reset the working variable associated with each extraction. These should  /* Reset the working variable associated with each extraction. These should
6062  never be used unless previously set, but they get saved and restored, and so we  never be used unless previously set, but they get saved and restored, and so we
6063  initialize them to avoid reading uninitialized locations. */  initialize them to avoid reading uninitialized locations. Also, unset the
6064    offsets for the matched string. This is really just for tidiness with callouts,
6065    in case they inspect these fields. */
6066    
6067  if (md->offset_vector != NULL)  if (md->offset_vector != NULL)
6068    {    {
6069    register int *iptr = md->offset_vector + ocount;    register int *iptr = md->offset_vector + ocount;
6070    register int *iend = iptr - resetcount/2 + 1;    register int *iend = iptr - re->top_bracket;
6071      if (iend < md->offset_vector + 2) iend = md->offset_vector + 2;
6072    while (--iptr >= iend) *iptr = -1;    while (--iptr >= iend) *iptr = -1;
6073      md->offset_vector[0] = md->offset_vector[1] = -1;
6074    }    }
6075    
6076  /* Set up the first character to match, if available. The first_byte value is  /* Set up the first character to match, if available. The first_byte value is
# Line 5857  if ((re->flags & PCRE_REQCHSET) != 0) Line 6104  if ((re->flags & PCRE_REQCHSET) != 0)
6104    }    }
6105    
6106    
6107    
6108    
6109  /* ==========================================================================*/  /* ==========================================================================*/
6110    
6111  /* Loop for handling unanchored repeated matching attempts; for anchored regexs  /* Loop for handling unanchored repeated matching attempts; for anchored regexs
# Line 5867  for(;;) Line 6116  for(;;)
6116    USPTR save_end_subject = end_subject;    USPTR save_end_subject = end_subject;
6117    USPTR new_start_match;    USPTR new_start_match;
6118    
   /* Reset the maximum number of extractions we might see. */  
   
   if (md->offset_vector != NULL)  
     {  
     register int *iptr = md->offset_vector;  
     register int *iend = iptr + resetcount;  
     while (iptr < iend) *iptr++ = -1;  
     }  
   
6119    /* If firstline is TRUE, the start of the match is constrained to the first    /* If firstline is TRUE, the start of the match is constrained to the first
6120    line of a multiline string. That is, the match must be before or at the first    line of a multiline string. That is, the match must be before or at the first
6121    newline. Implement this by temporarily adjusting end_subject so that we stop    newline. Implement this by temporarily adjusting end_subject so that we stop
# Line 5903  for(;;) Line 6143  for(;;)
6143    /* There are some optimizations that avoid running the match if a known    /* There are some optimizations that avoid running the match if a known
6144    starting point is not found, or if a known later character is not present.    starting point is not found, or if a known later character is not present.
6145    However, there is an option that disables these, for testing and for ensuring    However, there is an option that disables these, for testing and for ensuring
6146    that all callouts do actually occur. */    that all callouts do actually occur. The option can be set in the regex by
6147      (*NO_START_OPT) or passed in match-time options. */
6148    
6149    if ((options & PCRE_NO_START_OPTIMIZE) == 0)    if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0)
6150      {      {
6151      /* Advance to a unique first byte if there is one. */      /* Advance to a unique first byte if there is one. */
6152    
# Line 5959  for(;;) Line 6200  for(;;)
6200        while (start_match < end_subject)        while (start_match < end_subject)
6201          {          {
6202          register unsigned int c = *start_match;          register unsigned int c = *start_match;
6203          if ((start_bits[c/8] & (1 << (c&7))) == 0) start_match++;          if ((start_bits[c/8] & (1 << (c&7))) == 0)
6204            else break;            {
6205              start_match++;
6206    #ifdef SUPPORT_UTF8
6207              if (utf8)
6208                while(start_match < end_subject && (*start_match & 0xc0) == 0x80)
6209                  start_match++;
6210    #endif
6211              }
6212            else break;
6213          }          }
6214        }        }
6215      }   /* Starting optimizations */      }   /* Starting optimizations */
# Line 6055  for(;;) Line 6304  for(;;)
6304    md->start_match_ptr = start_match;    md->start_match_ptr = start_match;
6305    md->start_used_ptr = start_match;    md->start_used_ptr = start_match;
6306    md->match_call_count = 0;    md->match_call_count = 0;
6307    rc = match(start_match, md->start_code, start_match, NULL, 2, md, ims, NULL,    md->match_function_type = 0;
6308      0, 0);    md->end_offset_top = 0;
6309      rc = match(start_match, md->start_code, start_match, NULL, 2, md, NULL, 0);
6310    if (md->hitend && start_partial == NULL) start_partial = md->start_used_ptr;    if (md->hitend && start_partial == NULL) start_partial = md->start_used_ptr;
6311    
6312    switch(rc)    switch(rc)
6313      {      {
6314      /* NOMATCH and PRUNE advance by one character. If MATCH_SKIP_ARG reaches      /* SKIP passes back the next starting point explicitly, but if it is the
6315      this level it means that a MARK that matched the SKIP's arg was not found.      same as the match we have just done, treat it as NOMATCH. */
6316      We treat this as NOMATCH. THEN at this level acts exactly like PRUNE. */  
6317        case MATCH_SKIP:
6318        if (md->start_match_ptr != start_match)
6319          {
6320          new_start_match = md->start_match_ptr;
6321          break;
6322          }
6323        /* Fall through */
6324    
6325        /* If MATCH_SKIP_ARG reaches this level it means that a MARK that matched
6326        the SKIP's arg was not found. We also treat this as NOMATCH. */
6327    
6328        case MATCH_SKIP_ARG:
6329        /* Fall through */
6330    
6331        /* NOMATCH and PRUNE advance by one character. THEN at this level acts
6332        exactly like PRUNE. */
6333    
6334      case MATCH_NOMATCH:      case MATCH_NOMATCH:
6335      case MATCH_PRUNE:      case MATCH_PRUNE:
     case MATCH_SKIP_ARG:  
6336      case MATCH_THEN:      case MATCH_THEN:
6337      new_start_match = start_match + 1;      new_start_match = start_match + 1;
6338  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 6077  for(;;) Line 6342  for(;;)
6342  #endif  #endif
6343      break;      break;
6344    
     /* SKIP passes back the next starting point explicitly. */  
   
     case MATCH_SKIP:  
     new_start_match = md->start_match_ptr;  
     break;  
   
6345      /* COMMIT disables the bumpalong, but otherwise behaves as NOMATCH. */      /* COMMIT disables the bumpalong, but otherwise behaves as NOMATCH. */
6346    
6347      case MATCH_COMMIT:      case MATCH_COMMIT:
# Line 6170  if (rc == MATCH_MATCH || rc == MATCH_ACC Line 6429  if (rc == MATCH_MATCH || rc == MATCH_ACC
6429    
6430    /* Set the return code to the number of captured strings, or 0 if there are    /* Set the return code to the number of captured strings, or 0 if there are
6431    too many to fit into the vector. */    too many to fit into the vector. */
6432    
6433    rc = md->offset_overflow? 0 : md->end_offset_top/2;    rc = md->offset_overflow? 0 : md->end_offset_top/2;
6434    
6435    /* If there is space, set up the whole thing as substring 0. The value of    /* If there is space, set up the whole thing as substring 0. The value of

Legend:
Removed from v.534  
changed lines
  Added in v.617

  ViewVC Help
Powered by ViewVC 1.1.5