/[pcre]/code/branches/pcre16/pcre_compile.c
ViewVC logotype

Diff of /code/branches/pcre16/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

code/trunk/pcre_compile.c revision 574 by ph10, Sat Nov 20 17:47:27 2010 UTC code/branches/pcre16/pcre_compile.c revision 756 by ph10, Mon Nov 21 10:48:42 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 231  static const char posix_names[] = Line 231  static const char posix_names[] =
231    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
232    STRING_word0  STRING_xdigit;    STRING_word0  STRING_xdigit;
233    
234  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
235    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
236    
237  /* Table of class bit maps for each POSIX class. Each class is formed from a  /* Table of class bit maps for each POSIX class. Each class is formed from a
# Line 266  substitutes must be in the order of the Line 266  substitutes must be in the order of the
266  both positive and negative cases. NULL means no substitute. */  both positive and negative cases. NULL means no substitute. */
267    
268  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
269  static const uschar *substitutes[] = {  static const pcre_uchar literal_PNd[]  = { '\\', 'P', '{', 'N', 'd', '}', '\0' };
270    (uschar *)"\\P{Nd}",    /* \D */  static const pcre_uchar literal_pNd[]  = { '\\', 'p', '{', 'N', 'd', '}', '\0' };
271    (uschar *)"\\p{Nd}",    /* \d */  static const pcre_uchar literal_PXsp[] = { '\\', 'P', '{', 'X', 's', 'p', '}', '\0' };
272    (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */  static const pcre_uchar literal_pXsp[] = { '\\', 'p', '{', 'X', 's', 'p', '}', '\0' };
273    (uschar *)"\\p{Xsp}",   /* \s */  static const pcre_uchar literal_PXwd[] = { '\\', 'P', '{', 'X', 'w', 'd', '}', '\0' };
274    (uschar *)"\\P{Xwd}",   /* \W */  static const pcre_uchar literal_pXwd[] = { '\\', 'p', '{', 'X', 'w', 'd', '}', '\0' };
275    (uschar *)"\\p{Xwd}"    /* \w */  
276    static const pcre_uchar *substitutes[] = {
277      literal_PNd,           /* \D */
278      literal_pNd,           /* \d */
279      literal_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
280      literal_pXsp,          /* \s */
281      literal_PXwd,          /* \W */
282      literal_pXwd           /* \w */
283  };  };
284    
285  static const uschar *posix_substitutes[] = {  static const pcre_uchar literal_pL[] =   { '\\', 'p', '{', 'L', '}', '\0' };
286    (uschar *)"\\p{L}",     /* alpha */  static const pcre_uchar literal_pLl[] =  { '\\', 'p', '{', 'L', 'l', '}', '\0' };
287    (uschar *)"\\p{Ll}",    /* lower */  static const pcre_uchar literal_pLu[] =  { '\\', 'p', '{', 'L', 'u', '}', '\0' };
288    (uschar *)"\\p{Lu}",    /* upper */  static const pcre_uchar literal_pXan[] = { '\\', 'p', '{', 'X', 'a', 'n', '}', '\0' };
289    (uschar *)"\\p{Xan}",   /* alnum */  static const pcre_uchar literal_h[] =    { '\\', 'h', '\0' };
290    NULL,                   /* ascii */  static const pcre_uchar literal_pXps[] = { '\\', 'p', '{', 'X', 'p', 's', '}', '\0' };
291    (uschar *)"\\h",        /* blank */  static const pcre_uchar literal_PL[] =   { '\\', 'P', '{', 'L', '}', '\0' };
292    NULL,                   /* cntrl */  static const pcre_uchar literal_PLl[] =  { '\\', 'P', '{', 'L', 'l', '}', '\0' };
293    (uschar *)"\\p{Nd}",    /* digit */  static const pcre_uchar literal_PLu[] =  { '\\', 'P', '{', 'L', 'u', '}', '\0' };
294    NULL,                   /* graph */  static const pcre_uchar literal_PXan[] = { '\\', 'P', '{', 'X', 'a', 'n', '}', '\0' };
295    NULL,                   /* print */  static const pcre_uchar literal_H[] =    { '\\', 'H', '\0' };
296    NULL,                   /* punct */  static const pcre_uchar literal_PXps[] = { '\\', 'P', '{', 'X', 'p', 's', '}', '\0' };
297    (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */  
298    (uschar *)"\\p{Xwd}",   /* word */  static const pcre_uchar *posix_substitutes[] = {
299    NULL,                   /* xdigit */    literal_pL,            /* alpha */
300      literal_pLl,           /* lower */
301      literal_pLu,           /* upper */
302      literal_pXan,          /* alnum */
303      NULL,                  /* ascii */
304      literal_h,             /* blank */
305      NULL,                  /* cntrl */
306      literal_pNd,           /* digit */
307      NULL,                  /* graph */
308      NULL,                  /* print */
309      NULL,                  /* punct */
310      literal_pXps,          /* space */    /* NOTE: Xps is POSIX space */
311      literal_pXwd,          /* word */
312      NULL,                  /* xdigit */
313    /* Negated cases */    /* Negated cases */
314    (uschar *)"\\P{L}",     /* ^alpha */    literal_PL,            /* ^alpha */
315    (uschar *)"\\P{Ll}",    /* ^lower */    literal_PLl,           /* ^lower */
316    (uschar *)"\\P{Lu}",    /* ^upper */    literal_PLu,           /* ^upper */
317    (uschar *)"\\P{Xan}",   /* ^alnum */    literal_PXan,          /* ^alnum */
318    NULL,                   /* ^ascii */    NULL,                  /* ^ascii */
319    (uschar *)"\\H",        /* ^blank */    literal_H,             /* ^blank */
320    NULL,                   /* ^cntrl */    NULL,                  /* ^cntrl */
321    (uschar *)"\\P{Nd}",    /* ^digit */    literal_PNd,           /* ^digit */
322    NULL,                   /* ^graph */    NULL,                  /* ^graph */
323    NULL,                   /* ^print */    NULL,                  /* ^print */
324    NULL,                   /* ^punct */    NULL,                  /* ^punct */
325    (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */    literal_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
326    (uschar *)"\\P{Xwd}",   /* ^word */    literal_PXwd,          /* ^word */
327    NULL                    /* ^xdigit */    NULL                   /* ^xdigit */
328  };  };
329  #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
330  #endif  #endif
331    
332  #define STRING(a)  # a  #define STRING(a)  # a
# Line 393  static const char error_texts[] = Line 413  static const char error_texts[] =
413    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
414    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
415    /* 55 */    /* 55 */
416    "repeating a DEFINE group is not allowed\0"    "repeating a DEFINE group is not allowed\0"  /** DEAD **/
417    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
418    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
419    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
# Line 408  static const char error_texts[] = Line 428  static const char error_texts[] =
428    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
429    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
430    "this version of PCRE is not compiled with PCRE_UCP support\0"    "this version of PCRE is not compiled with PCRE_UCP support\0"
431    "\\c must be followed by an ASCII character\0"    "\\c must be followed by an ASCII character\0"
432      "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
433      /* 70 */
434      "internal error: unknown opcode in find_fixedlength()\0"
435    ;    ;
436    
437  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 545  static const unsigned char ebcdic_charta Line 568  static const unsigned char ebcdic_charta
568  /* Definition to allow mutual recursion */  /* Definition to allow mutual recursion */
569    
570  static BOOL  static BOOL
571    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,    compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
572      int *, int *, branch_chain *, compile_data *, int *);      int *, int *, branch_chain *, compile_data *, int *);
573    
574    
# Line 577  return s; Line 600  return s;
600    
601    
602  /*************************************************  /*************************************************
603    *            Check for counted repeat            *
604    *************************************************/
605    
606    /* This function is called when a '{' is encountered in a place where it might
607    start a quantifier. It looks ahead to see if it really is a quantifier or not.
608    It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
609    where the ddds are digits.
610    
611    Arguments:
612      p         pointer to the first char after '{'
613    
614    Returns:    TRUE or FALSE
615    */
616    
617    static BOOL
618    is_counted_repeat(const pcre_uchar *p)
619    {
620    if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
621    while ((digitab[*p] & ctype_digit) != 0) p++;
622    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
623    
624    if (*p++ != CHAR_COMMA) return FALSE;
625    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
626    
627    if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
628    while ((digitab[*p] & ctype_digit) != 0) p++;
629    
630    return (*p == CHAR_RIGHT_CURLY_BRACKET);
631    }
632    
633    
634    
635    /*************************************************
636  *            Handle escapes                      *  *            Handle escapes                      *
637  *************************************************/  *************************************************/
638    
# Line 601  Returns:         zero or positive => a d Line 657  Returns:         zero or positive => a d
657  */  */
658    
659  static int  static int
660  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,
661    int options, BOOL isclass)    int options, BOOL isclass)
662  {  {
663  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
664  const uschar *ptr = *ptrptr + 1;  const pcre_uchar *ptr = *ptrptr + 1;
665  int c, i;  int c, i;
666    
667  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
# Line 632  else if ((i = escapes[c - 0x48]) != 0) Line 688  else if ((i = escapes[c - 0x48]) != 0)
688    
689  else  else
690    {    {
691    const uschar *oldptr;    const pcre_uchar *oldptr;
692    BOOL braced, negated;    BOOL braced, negated;
693    
694    switch (c)    switch (c)
# Line 642  else Line 698  else
698    
699      case CHAR_l:      case CHAR_l:
700      case CHAR_L:      case CHAR_L:
701        *errorcodeptr = ERR37;
702        break;
703    
704      case CHAR_u:      case CHAR_u:
705        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
706          {
707          /* In JavaScript, \u must be followed by four hexadecimal numbers.
708          Otherwise it is a lowercase u letter. */
709          if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0
710               && (digitab[ptr[3]] & ctype_xdigit) != 0 && (digitab[ptr[4]] & ctype_xdigit) != 0)
711            {
712            c = 0;
713            for (i = 0; i < 4; ++i)
714              {
715              register int cc = *(++ptr);
716    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
717              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
718              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
719    #else           /* EBCDIC coding */
720              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
721              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
722    #endif
723              }
724            }
725          }
726        else
727          *errorcodeptr = ERR37;
728        break;
729    
730      case CHAR_U:      case CHAR_U:
731      *errorcodeptr = ERR37;      /* In JavaScript, \U is an uppercase U letter. */
732        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
733      break;      break;
734    
735      /* \g must be followed by one of a number of specific things:      /* In a character class, \g is just a literal "g". Outside a character
736        class, \g must be followed by one of a number of specific things:
737    
738      (1) A number, either plain or braced. If positive, it is an absolute      (1) A number, either plain or braced. If positive, it is an absolute
739      backreference. If negative, it is a relative backreference. This is a Perl      backreference. If negative, it is a relative backreference. This is a Perl
# Line 664  else Line 750  else
750      the -ESC_g code (cf \k). */      the -ESC_g code (cf \k). */
751    
752      case CHAR_g:      case CHAR_g:
753        if (isclass) break;
754      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
755        {        {
756        c = -ESC_g;        c = -ESC_g;
# Line 674  else Line 761  else
761    
762      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
763        {        {
764        const uschar *p;        const pcre_uchar *p;
765        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
766          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;
767        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
# Line 792  else Line 879  else
879      treated as a data character. */      treated as a data character. */
880    
881      case CHAR_x:      case CHAR_x:
882        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
883          {
884          /* In JavaScript, \x must be followed by two hexadecimal numbers.
885          Otherwise it is a lowercase x letter. */
886          if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0)
887            {
888            c = 0;
889            for (i = 0; i < 2; ++i)
890              {
891              register int cc = *(++ptr);
892    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
893              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
894              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
895    #else           /* EBCDIC coding */
896              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
897              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
898    #endif
899              }
900            }
901          break;
902          }
903    
904      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
905        {        {
906        const uschar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
907        int count = 0;        int count = 0;
908    
909        c = 0;        c = 0;
# Line 857  else Line 966  else
966      if (c > 127)  /* Excludes all non-ASCII in either mode */      if (c > 127)  /* Excludes all non-ASCII in either mode */
967        {        {
968        *errorcodeptr = ERR68;        *errorcodeptr = ERR68;
969        break;        break;
970        }        }
971      if (c >= CHAR_a && c <= CHAR_z) c -= 32;      if (c >= CHAR_a && c <= CHAR_z) c -= 32;
972      c ^= 0x40;      c ^= 0x40;
973  #else             /* EBCDIC coding */  #else             /* EBCDIC coding */
# Line 885  else Line 994  else
994    }    }
995    
996  /* Perl supports \N{name} for character names, as well as plain \N for "not  /* Perl supports \N{name} for character names, as well as plain \N for "not
997  newline". PCRE does not support \N{name}. */  newline". PCRE does not support \N{name}. However, it does support
998    quantification such as \N{2,3}. */
999    
1000  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET)  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1001         !is_counted_repeat(ptr+2))
1002    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
1003    
1004  /* If PCRE_UCP is set, we change the values for \d etc. */  /* If PCRE_UCP is set, we change the values for \d etc. */
# Line 923  Returns:         type value from ucp_typ Line 1034  Returns:         type value from ucp_typ
1034  */  */
1035    
1036  static int  static int
1037  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
1038  {  {
1039  int c, i, bot, top;  int c, i, bot, top;
1040  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1041  char name[32];  char name[32];
1042    
1043  c = *(++ptr);  c = *(++ptr);
# Line 997  return -1; Line 1108  return -1;
1108    
1109    
1110  /*************************************************  /*************************************************
 *            Check for counted repeat            *  
 *************************************************/  
   
 /* This function is called when a '{' is encountered in a place where it might  
 start a quantifier. It looks ahead to see if it really is a quantifier or not.  
 It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}  
 where the ddds are digits.  
   
 Arguments:  
   p         pointer to the first char after '{'  
   
 Returns:    TRUE or FALSE  
 */  
   
 static BOOL  
 is_counted_repeat(const uschar *p)  
 {  
 if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  
 while ((digitab[*p] & ctype_digit) != 0) p++;  
 if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  
   
 if (*p++ != CHAR_COMMA) return FALSE;  
 if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  
   
 if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  
 while ((digitab[*p] & ctype_digit) != 0) p++;  
   
 return (*p == CHAR_RIGHT_CURLY_BRACKET);  
 }  
   
   
   
 /*************************************************  
1111  *         Read repeat counts                     *  *         Read repeat counts                     *
1112  *************************************************/  *************************************************/
1113    
# Line 1048  Returns:         pointer to '}' on succe Line 1126  Returns:         pointer to '}' on succe
1126                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1127  */  */
1128    
1129  static const uschar *  static const pcre_uchar *
1130  read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)  read_repeat_counts(const pcre_uchar *p, int *minp, int *maxp, int *errorcodeptr)
1131  {  {
1132  int min = 0;  int min = 0;
1133  int max = -1;  int max = -1;
# Line 1105  top-level call starts at the beginning o Line 1183  top-level call starts at the beginning o
1183  start at a parenthesis. It scans along a pattern's text looking for capturing  start at a parenthesis. It scans along a pattern's text looking for capturing
1184  subpatterns, and counting them. If it finds a named pattern that matches the  subpatterns, and counting them. If it finds a named pattern that matches the
1185  name it is given, it returns its number. Alternatively, if the name is NULL, it  name it is given, it returns its number. Alternatively, if the name is NULL, it
1186  returns when it reaches a given numbered subpattern. We know that if (?P< is  returns when it reaches a given numbered subpattern. Recursion is used to keep
1187  encountered, the name will be terminated by '>' because that is checked in the  track of subpatterns that reset the capturing group numbers - the (?| feature.
1188  first pass. Recursion is used to keep track of subpatterns that reset the  
1189  capturing group numbers - the (?| feature.  This function was originally called only from the second pass, in which we know
1190    that if (?< or (?' or (?P< is encountered, the name will be correctly
1191    terminated because that is checked in the first pass. There is now one call to
1192    this function in the first pass, to check for a recursive back reference by
1193    name (so that we can make the whole group atomic). In this case, we need check
1194    only up to the current position in the pattern, and that is still OK because
1195    and previous occurrences will have been checked. To make this work, the test
1196    for "end of pattern" is a check against cd->end_pattern in the main loop,
1197    instead of looking for a binary zero. This means that the special first-pass
1198    call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1199    processing items within the loop are OK, because afterwards the main loop will
1200    terminate.)
1201    
1202  Arguments:  Arguments:
1203    ptrptr       address of the current character pointer (updated)    ptrptr       address of the current character pointer (updated)
# Line 1116  Arguments: Line 1205  Arguments:
1205    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1206    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1207    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1208    utf8         TRUE if we are in UTF-8 mode    utf8         TRUE if we are in UTF-8 mode
1209    count        pointer to the current capturing subpattern number (updated)    count        pointer to the current capturing subpattern number (updated)
1210    
1211  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
1212  */  */
1213    
1214  static int  static int
1215  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,
1216    BOOL xmode, BOOL utf8, int *count)    BOOL xmode, BOOL utf8, int *count)
1217  {  {
1218  uschar *ptr = *ptrptr;  pcre_uchar *ptr = *ptrptr;
1219  int start_count = *count;  int start_count = *count;
1220  int hwm_count = start_count;  int hwm_count = start_count;
1221  BOOL dup_parens = FALSE;  BOOL dup_parens = FALSE;
# Line 1193  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1282  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1282          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1283        {        {
1284        int term;        int term;
1285        const uschar *thisname;        const pcre_uchar *thisname;
1286        *count += 1;        *count += 1;
1287        if (name == NULL && *count == lorn) return *count;        if (name == NULL && *count == lorn) return *count;
1288        term = *ptr++;        term = *ptr++;
# Line 1209  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1298  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1298    }    }
1299    
1300  /* Past any initial parenthesis handling, scan for parentheses or vertical  /* Past any initial parenthesis handling, scan for parentheses or vertical
1301  bars. */  bars. Stop if we get to cd->end_pattern. Note that this is important for the
1302    first-pass call when this value is temporarily adjusted to stop at the current
1303    position. So DO NOT change this to a test for binary zero. */
1304    
1305  for (; *ptr != 0; ptr++)  for (; ptr < cd->end_pattern; ptr++)
1306    {    {
1307    /* Skip over backslashed characters and also entire \Q...\E */    /* Skip over backslashed characters and also entire \Q...\E */
1308    
# Line 1285  for (; *ptr != 0; ptr++) Line 1376  for (; *ptr != 0; ptr++)
1376    
1377    if (xmode && *ptr == CHAR_NUMBER_SIGN)    if (xmode && *ptr == CHAR_NUMBER_SIGN)
1378      {      {
1379      ptr++;      ptr++;
1380      while (*ptr != 0)      while (*ptr != 0)
1381        {        {
1382        if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }        if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1383        ptr++;        ptr++;
1384  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1385        if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;        if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
1386  #endif  #endif
1387        }        }
# Line 1348  Arguments: Line 1439  Arguments:
1439    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1440    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1441    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1442    utf8         TRUE if we are in UTF-8 mode    utf8         TRUE if we are in UTF-8 mode
1443    
1444  Returns:       the number of the found subpattern, or -1 if not found  Returns:       the number of the found subpattern, or -1 if not found
1445  */  */
1446    
1447  static int  static int
1448  find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode,  find_parens(compile_data *cd, const pcre_uchar *name, int lorn, BOOL xmode,
1449    BOOL utf8)    BOOL utf8)
1450  {  {
1451  uschar *ptr = (uschar *)cd->start_pattern;  pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1452  int count = 0;  int count = 0;
1453  int rc;  int rc;
1454    
# Line 1384  return rc; Line 1475  return rc;
1475    
1476  /* This is called by several functions that scan a compiled expression looking  /* This is called by several functions that scan a compiled expression looking
1477  for a fixed first character, or an anchoring op code etc. It skips over things  for a fixed first character, or an anchoring op code etc. It skips over things
1478  that do not influence this. For some calls, a change of option is important.  that do not influence this. For some calls, it makes sense to skip negative
1479  For some calls, it makes sense to skip negative forward and all backward  forward and all backward assertions, and also the \b assertion; for others it
1480  assertions, and also the \b assertion; for others it does not.  does not.
1481    
1482  Arguments:  Arguments:
1483    code         pointer to the start of the group    code         pointer to the start of the group
   options      pointer to external options  
   optbit       the option bit whose changing is significant, or  
                  zero if none are  
1484    skipassert   TRUE if certain assertions are to be skipped    skipassert   TRUE if certain assertions are to be skipped
1485    
1486  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1487  */  */
1488    
1489  static const uschar*  static const pcre_uchar*
1490  first_significant_code(const uschar *code, int *options, int optbit,  first_significant_code(const pcre_uchar *code, BOOL skipassert)
   BOOL skipassert)  
1491  {  {
1492  for (;;)  for (;;)
1493    {    {
1494    switch ((int)*code)    switch ((int)*code)
1495      {      {
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
1496      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1497      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1498      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
# Line 1461  and doing the check at the end; a flag s Line 1542  and doing the check at the end; a flag s
1542    
1543  Arguments:  Arguments:
1544    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1545    options  the compiling options    utf8     TRUE in UTF-8 mode
1546    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1547    cd       the "compile data" structure    cd       the "compile data" structure
1548    
1549  Returns:   the fixed length,  Returns:   the fixed length,
1550               or -1 if there is no fixed length,               or -1 if there is no fixed length,
1551               or -2 if \C was encountered               or -2 if \C was encountered (in UTF-8 mode only)
1552               or -3 if an OP_RECURSE item was encountered and atend is FALSE               or -3 if an OP_RECURSE item was encountered and atend is FALSE
1553                 or -4 if an unknown opcode was encountered (internal error)
1554  */  */
1555    
1556  static int  static int
1557  find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)  find_fixedlength(pcre_uchar *code, BOOL utf8, BOOL atend, compile_data *cd)
1558  {  {
1559  int length = -1;  int length = -1;
1560    
1561  register int branchlength = 0;  register int branchlength = 0;
1562  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1563    
1564  /* Scan along the opcodes for this branch. If we get to the end of the  /* Scan along the opcodes for this branch. If we get to the end of the
1565  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1485  branch, check the length against that of Line 1567  branch, check the length against that of
1567  for (;;)  for (;;)
1568    {    {
1569    int d;    int d;
1570    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1571    register int op = *cc;    register int op = *cc;
1572    switch (op)    switch (op)
1573      {      {
1574        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1575        OP_BRA (normal non-capturing bracket) because the other variants of these
1576        opcodes are all concerned with unlimited repeated groups, which of course
1577        are not of fixed length. */
1578    
1579      case OP_CBRA:      case OP_CBRA:
1580      case OP_BRA:      case OP_BRA:
1581      case OP_ONCE:      case OP_ONCE:
1582        case OP_ONCE_NC:
1583      case OP_COND:      case OP_COND:
1584      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), utf8, atend, cd);
1585      if (d < 0) return d;      if (d < 0) return d;
1586      branchlength += d;      branchlength += d;
1587      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1588      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1589      break;      break;
1590    
1591      /* Reached end of a branch; if it's a ket it is the end of a nested      /* Reached end of a branch; if it's a ket it is the end of a nested call.
1592      call. If it's ALT it is an alternation in a nested call. If it is      If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1593      END it's the end of the outer call. All can be handled by the same code. */      an ALT. If it is END it's the end of the outer call. All can be handled by
1594        the same code. Note that we must not include the OP_KETRxxx opcodes here,
1595        because they all imply an unlimited repeat. */
1596    
1597      case OP_ALT:      case OP_ALT:
1598      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1599      case OP_END:      case OP_END:
1600        case OP_ACCEPT:
1601        case OP_ASSERT_ACCEPT:
1602      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1603        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
1604      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
# Line 1522  for (;;) Line 1612  for (;;)
1612    
1613      case OP_RECURSE:      case OP_RECURSE:
1614      if (!atend) return -3;      if (!atend) return -3;
1615      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1616      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1617      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1618      d = find_fixedlength(cs + 2, options, atend, cd);      d = find_fixedlength(cs + 2, utf8, atend, cd);
1619      if (d < 0) return d;      if (d < 0) return d;
1620      branchlength += d;      branchlength += d;
1621      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1542  for (;;) Line 1632  for (;;)
1632    
1633      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1634    
1635      case OP_REVERSE:      case OP_MARK:
1636        case OP_PRUNE_ARG:
1637        case OP_SKIP_ARG:
1638        case OP_THEN_ARG:
1639        cc += cc[1] + _pcre_OP_lengths[*cc];
1640        break;
1641    
1642        case OP_CALLOUT:
1643        case OP_CIRC:
1644        case OP_CIRCM:
1645        case OP_CLOSE:
1646        case OP_COMMIT:
1647      case OP_CREF:      case OP_CREF:
     case OP_NCREF:  
     case OP_RREF:  
     case OP_NRREF:  
1648      case OP_DEF:      case OP_DEF:
1649      case OP_OPT:      case OP_DOLL:
1650      case OP_CALLOUT:      case OP_DOLLM:
     case OP_SOD:  
     case OP_SOM:  
     case OP_SET_SOM:  
1651      case OP_EOD:      case OP_EOD:
1652      case OP_EODN:      case OP_EODN:
1653      case OP_CIRC:      case OP_FAIL:
1654      case OP_DOLL:      case OP_NCREF:
1655        case OP_NRREF:
1656      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1657        case OP_PRUNE:
1658        case OP_REVERSE:
1659        case OP_RREF:
1660        case OP_SET_SOM:
1661        case OP_SKIP:
1662        case OP_SOD:
1663        case OP_SOM:
1664        case OP_THEN:
1665      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1666      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
1667      break;      break;
# Line 1565  for (;;) Line 1669  for (;;)
1669      /* Handle literal characters */      /* Handle literal characters */
1670    
1671      case OP_CHAR:      case OP_CHAR:
1672      case OP_CHARNC:      case OP_CHARI:
1673      case OP_NOT:      case OP_NOT:
1674        case OP_NOTI:
1675      branchlength++;      branchlength++;
1676      cc += 2;      cc += 2;
1677  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1678      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1679  #endif  #endif
1680      break;      break;
1681    
# Line 1579  for (;;) Line 1683  for (;;)
1683      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
1684    
1685      case OP_EXACT:      case OP_EXACT:
1686        case OP_EXACTI:
1687        case OP_NOTEXACT:
1688        case OP_NOTEXACTI:
1689      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1690      cc += 4;      cc += 4;
1691  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1692      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1693  #endif  #endif
1694      break;      break;
1695    
# Line 1600  for (;;) Line 1706  for (;;)
1706      cc += 2;      cc += 2;
1707      /* Fall through */      /* Fall through */
1708    
1709        case OP_HSPACE:
1710        case OP_VSPACE:
1711        case OP_NOT_HSPACE:
1712        case OP_NOT_VSPACE:
1713      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1714      case OP_DIGIT:      case OP_DIGIT:
1715      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
# Line 1612  for (;;) Line 1722  for (;;)
1722      cc++;      cc++;
1723      break;      break;
1724    
1725      /* The single-byte matcher isn't allowed */      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1726        otherwise \C is coded as OP_ALLANY. */
1727    
1728      case OP_ANYBYTE:      case OP_ANYBYTE:
1729      return -2;      return -2;
# Line 1631  for (;;) Line 1742  for (;;)
1742    
1743      switch (*cc)      switch (*cc)
1744        {        {
1745          case OP_CRPLUS:
1746          case OP_CRMINPLUS:
1747        case OP_CRSTAR:        case OP_CRSTAR:
1748        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1749        case OP_CRQUERY:        case OP_CRQUERY:
# Line 1651  for (;;) Line 1764  for (;;)
1764    
1765      /* Anything else is variable length */      /* Anything else is variable length */
1766    
1767      default:      case OP_ANYNL:
1768        case OP_BRAMINZERO:
1769        case OP_BRAPOS:
1770        case OP_BRAPOSZERO:
1771        case OP_BRAZERO:
1772        case OP_CBRAPOS:
1773        case OP_EXTUNI:
1774        case OP_KETRMAX:
1775        case OP_KETRMIN:
1776        case OP_KETRPOS:
1777        case OP_MINPLUS:
1778        case OP_MINPLUSI:
1779        case OP_MINQUERY:
1780        case OP_MINQUERYI:
1781        case OP_MINSTAR:
1782        case OP_MINSTARI:
1783        case OP_MINUPTO:
1784        case OP_MINUPTOI:
1785        case OP_NOTMINPLUS:
1786        case OP_NOTMINPLUSI:
1787        case OP_NOTMINQUERY:
1788        case OP_NOTMINQUERYI:
1789        case OP_NOTMINSTAR:
1790        case OP_NOTMINSTARI:
1791        case OP_NOTMINUPTO:
1792        case OP_NOTMINUPTOI:
1793        case OP_NOTPLUS:
1794        case OP_NOTPLUSI:
1795        case OP_NOTPOSPLUS:
1796        case OP_NOTPOSPLUSI:
1797        case OP_NOTPOSQUERY:
1798        case OP_NOTPOSQUERYI:
1799        case OP_NOTPOSSTAR:
1800        case OP_NOTPOSSTARI:
1801        case OP_NOTPOSUPTO:
1802        case OP_NOTPOSUPTOI:
1803        case OP_NOTQUERY:
1804        case OP_NOTQUERYI:
1805        case OP_NOTSTAR:
1806        case OP_NOTSTARI:
1807        case OP_NOTUPTO:
1808        case OP_NOTUPTOI:
1809        case OP_PLUS:
1810        case OP_PLUSI:
1811        case OP_POSPLUS:
1812        case OP_POSPLUSI:
1813        case OP_POSQUERY:
1814        case OP_POSQUERYI:
1815        case OP_POSSTAR:
1816        case OP_POSSTARI:
1817        case OP_POSUPTO:
1818        case OP_POSUPTOI:
1819        case OP_QUERY:
1820        case OP_QUERYI:
1821        case OP_REF:
1822        case OP_REFI:
1823        case OP_SBRA:
1824        case OP_SBRAPOS:
1825        case OP_SCBRA:
1826        case OP_SCBRAPOS:
1827        case OP_SCOND:
1828        case OP_SKIPZERO:
1829        case OP_STAR:
1830        case OP_STARI:
1831        case OP_TYPEMINPLUS:
1832        case OP_TYPEMINQUERY:
1833        case OP_TYPEMINSTAR:
1834        case OP_TYPEMINUPTO:
1835        case OP_TYPEPLUS:
1836        case OP_TYPEPOSPLUS:
1837        case OP_TYPEPOSQUERY:
1838        case OP_TYPEPOSSTAR:
1839        case OP_TYPEPOSUPTO:
1840        case OP_TYPEQUERY:
1841        case OP_TYPESTAR:
1842        case OP_TYPEUPTO:
1843        case OP_UPTO:
1844        case OP_UPTOI:
1845      return -1;      return -1;
1846    
1847        /* Catch unrecognized opcodes so that when new ones are added they
1848        are not forgotten, as has happened in the past. */
1849    
1850        default:
1851        return -4;
1852      }      }
1853    }    }
1854  /* Control never gets here */  /* Control never gets here */
# Line 1679  Arguments: Line 1875  Arguments:
1875  Returns:      pointer to the opcode for the bracket, or NULL if not found  Returns:      pointer to the opcode for the bracket, or NULL if not found
1876  */  */
1877    
1878  const uschar *  const pcre_uchar *
1879  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  _pcre_find_bracket(const pcre_uchar *code, BOOL utf8, int number)
1880  {  {
1881  for (;;)  for (;;)
1882    {    {
1883    register int c = *code;    register int c = *code;
1884    
1885    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1886    
1887    /* XCLASS is used for classes that cannot be represented just by a bit    /* XCLASS is used for classes that cannot be represented just by a bit
# Line 1697  for (;;) Line 1894  for (;;)
1894    
1895    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
1896      {      {
1897      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
1898      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1899      }      }
1900    
1901    /* Handle capturing bracket */    /* Handle capturing bracket */
1902    
1903    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1904               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1905      {      {
1906      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
1907      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1908      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1909      }      }
1910    
# Line 1745  for (;;) Line 1943  for (;;)
1943        break;        break;
1944    
1945        case OP_THEN_ARG:        case OP_THEN_ARG:
1946        code += code[1+LINK_SIZE];        code += code[1];
1947        break;        break;
1948        }        }
1949    
# Line 1761  for (;;) Line 1959  for (;;)
1959      if (utf8) switch(c)      if (utf8) switch(c)
1960        {        {
1961        case OP_CHAR:        case OP_CHAR:
1962        case OP_CHARNC:        case OP_CHARI:
1963        case OP_EXACT:        case OP_EXACT:
1964          case OP_EXACTI:
1965        case OP_UPTO:        case OP_UPTO:
1966          case OP_UPTOI:
1967        case OP_MINUPTO:        case OP_MINUPTO:
1968          case OP_MINUPTOI:
1969        case OP_POSUPTO:        case OP_POSUPTO:
1970          case OP_POSUPTOI:
1971        case OP_STAR:        case OP_STAR:
1972          case OP_STARI:
1973        case OP_MINSTAR:        case OP_MINSTAR:
1974          case OP_MINSTARI:
1975        case OP_POSSTAR:        case OP_POSSTAR:
1976          case OP_POSSTARI:
1977        case OP_PLUS:        case OP_PLUS:
1978          case OP_PLUSI:
1979        case OP_MINPLUS:        case OP_MINPLUS:
1980          case OP_MINPLUSI:
1981        case OP_POSPLUS:        case OP_POSPLUS:
1982          case OP_POSPLUSI:
1983        case OP_QUERY:        case OP_QUERY:
1984          case OP_QUERYI:
1985        case OP_MINQUERY:        case OP_MINQUERY:
1986          case OP_MINQUERYI:
1987        case OP_POSQUERY:        case OP_POSQUERY:
1988          case OP_POSQUERYI:
1989        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1990        break;        break;
1991        }        }
# Line 1801  Arguments: Line 2012  Arguments:
2012  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found
2013  */  */
2014    
2015  static const uschar *  static const pcre_uchar *
2016  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf8)
2017  {  {
2018  for (;;)  for (;;)
2019    {    {
# Line 1851  for (;;) Line 2062  for (;;)
2062        break;        break;
2063    
2064        case OP_THEN_ARG:        case OP_THEN_ARG:
2065        code += code[1+LINK_SIZE];        code += code[1];
2066        break;        break;
2067        }        }
2068    
# Line 1867  for (;;) Line 2078  for (;;)
2078      if (utf8) switch(c)      if (utf8) switch(c)
2079        {        {
2080        case OP_CHAR:        case OP_CHAR:
2081        case OP_CHARNC:        case OP_CHARI:
2082        case OP_EXACT:        case OP_EXACT:
2083          case OP_EXACTI:
2084        case OP_UPTO:        case OP_UPTO:
2085          case OP_UPTOI:
2086        case OP_MINUPTO:        case OP_MINUPTO:
2087          case OP_MINUPTOI:
2088        case OP_POSUPTO:        case OP_POSUPTO:
2089          case OP_POSUPTOI:
2090        case OP_STAR:        case OP_STAR:
2091          case OP_STARI:
2092        case OP_MINSTAR:        case OP_MINSTAR:
2093          case OP_MINSTARI:
2094        case OP_POSSTAR:        case OP_POSSTAR:
2095          case OP_POSSTARI:
2096        case OP_PLUS:        case OP_PLUS:
2097          case OP_PLUSI:
2098        case OP_MINPLUS:        case OP_MINPLUS:
2099          case OP_MINPLUSI:
2100        case OP_POSPLUS:        case OP_POSPLUS:
2101          case OP_POSPLUSI:
2102        case OP_QUERY:        case OP_QUERY:
2103          case OP_QUERYI:
2104        case OP_MINQUERY:        case OP_MINQUERY:
2105          case OP_MINQUERYI:
2106        case OP_POSQUERY:        case OP_POSQUERY:
2107          case OP_POSQUERYI:
2108        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
2109        break;        break;
2110        }        }
# Line 1915  Returns:      TRUE if what is matched co Line 2139  Returns:      TRUE if what is matched co
2139  */  */
2140    
2141  static BOOL  static BOOL
2142  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2143    compile_data *cd)    BOOL utf8, compile_data *cd)
2144  {  {
2145  register int c;  register int c;
2146  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE);
2147       code < endcode;       code < endcode;
2148       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + _pcre_OP_lengths[c], TRUE))
2149    {    {
2150    const uschar *ccode;    const pcre_uchar *ccode;
2151    
2152    c = *code;    c = *code;
2153    
# Line 1937  for (code = first_significant_code(code Line 2161  for (code = first_significant_code(code
2161      continue;      continue;
2162      }      }
2163    
   /* Groups with zero repeats can of course be empty; skip them. */  
   
   if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO)  
     {  
     code += _pcre_OP_lengths[c];  
     do code += GET(code, 1); while (*code == OP_ALT);  
     c = *code;  
     continue;  
     }  
   
2164    /* For a recursion/subroutine call, if its end has been reached, which    /* For a recursion/subroutine call, if its end has been reached, which
2165    implies a subroutine call, we can scan it. */    implies a backward reference subroutine call, we can scan it. If it's a
2166      forward reference subroutine call, we can't. To detect forward reference
2167      we have to scan up the list that is kept in the workspace. This function is
2168      called only when doing the real compile, not during the pre-compile that
2169      measures the size of the compiled pattern. */
2170    
2171    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2172      {      {
2173      BOOL empty_branch = FALSE;      const pcre_uchar *scode;
2174      const uschar *scode = cd->start_code + GET(code, 1);      BOOL empty_branch;
2175    
2176        /* Test for forward reference */
2177    
2178        for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
2179          if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;
2180    
2181        /* Not a forward reference, test for completed backward reference */
2182    
2183        empty_branch = FALSE;
2184        scode = cd->start_code + GET(code, 1);
2185      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2186    
2187        /* Completed backwards reference */
2188    
2189      do      do
2190        {        {
2191        if (could_be_empty_branch(scode, endcode, utf8, cd))        if (could_be_empty_branch(scode, endcode, utf8, cd))
# Line 1965  for (code = first_significant_code(code Line 2196  for (code = first_significant_code(code
2196        scode += GET(scode, 1);        scode += GET(scode, 1);
2197        }        }
2198      while (*scode == OP_ALT);      while (*scode == OP_ALT);
2199    
2200      if (!empty_branch) return FALSE;  /* All branches are non-empty */      if (!empty_branch) return FALSE;  /* All branches are non-empty */
2201      continue;      continue;
2202      }      }
2203    
2204      /* Groups with zero repeats can of course be empty; skip them. */
2205    
2206      if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2207          c == OP_BRAPOSZERO)
2208        {
2209        code += _pcre_OP_lengths[c];
2210        do code += GET(code, 1); while (*code == OP_ALT);
2211        c = *code;
2212        continue;
2213        }
2214    
2215      /* A nested group that is already marked as "could be empty" can just be
2216      skipped. */
2217    
2218      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2219          c == OP_SCBRA || c == OP_SCBRAPOS)
2220        {
2221        do code += GET(code, 1); while (*code == OP_ALT);
2222        c = *code;
2223        continue;
2224        }
2225    
2226    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
2227    
2228    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA  || c == OP_BRAPOS ||
2229          c == OP_CBRA || c == OP_CBRAPOS ||
2230          c == OP_ONCE || c == OP_ONCE_NC ||
2231          c == OP_COND)
2232      {      {
2233      BOOL empty_branch;      BOOL empty_branch;
2234      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 2058  for (code = first_significant_code(code Line 2315  for (code = first_significant_code(code
2315      case OP_ALLANY:      case OP_ALLANY:
2316      case OP_ANYBYTE:      case OP_ANYBYTE:
2317      case OP_CHAR:      case OP_CHAR:
2318      case OP_CHARNC:      case OP_CHARI:
2319      case OP_NOT:      case OP_NOT:
2320        case OP_NOTI:
2321      case OP_PLUS:      case OP_PLUS:
2322      case OP_MINPLUS:      case OP_MINPLUS:
2323      case OP_POSPLUS:      case OP_POSPLUS:
# Line 2099  for (code = first_significant_code(code Line 2357  for (code = first_significant_code(code
2357      case OP_KET:      case OP_KET:
2358      case OP_KETRMAX:      case OP_KETRMAX:
2359      case OP_KETRMIN:      case OP_KETRMIN:
2360        case OP_KETRPOS:
2361      case OP_ALT:      case OP_ALT:
2362      return TRUE;      return TRUE;
2363    
# Line 2107  for (code = first_significant_code(code Line 2366  for (code = first_significant_code(code
2366    
2367  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2368      case OP_STAR:      case OP_STAR:
2369        case OP_STARI:
2370      case OP_MINSTAR:      case OP_MINSTAR:
2371        case OP_MINSTARI:
2372      case OP_POSSTAR:      case OP_POSSTAR:
2373        case OP_POSSTARI:
2374      case OP_QUERY:      case OP_QUERY:
2375        case OP_QUERYI:
2376      case OP_MINQUERY:      case OP_MINQUERY:
2377        case OP_MINQUERYI:
2378      case OP_POSQUERY:      case OP_POSQUERY:
2379        case OP_POSQUERYI:
2380      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];
2381      break;      break;
2382    
2383      case OP_UPTO:      case OP_UPTO:
2384        case OP_UPTOI:
2385      case OP_MINUPTO:      case OP_MINUPTO:
2386        case OP_MINUPTOI:
2387      case OP_POSUPTO:      case OP_POSUPTO:
2388        case OP_POSUPTOI:
2389      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];
2390      break;      break;
2391  #endif  #endif
# Line 2132  for (code = first_significant_code(code Line 2400  for (code = first_significant_code(code
2400      break;      break;
2401    
2402      case OP_THEN_ARG:      case OP_THEN_ARG:
2403      code += code[1+LINK_SIZE];      code += code[1];
2404      break;      break;
2405    
2406      /* None of the remaining opcodes are required to match a character. */      /* None of the remaining opcodes are required to match a character. */
# Line 2155  return TRUE; Line 2423  return TRUE;
2423  the current branch of the current pattern to see if it could match the empty  the current branch of the current pattern to see if it could match the empty
2424  string. If it could, we must look outwards for branches at other levels,  string. If it could, we must look outwards for branches at other levels,
2425  stopping when we pass beyond the bracket which is the subject of the recursion.  stopping when we pass beyond the bracket which is the subject of the recursion.
2426    This function is called only during the real compile, not during the
2427    pre-compile.
2428    
2429  Arguments:  Arguments:
2430    code        points to start of the recursion    code        points to start of the recursion
# Line 2167  Returns:      TRUE if what is matched co Line 2437  Returns:      TRUE if what is matched co
2437  */  */
2438    
2439  static BOOL  static BOOL
2440  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2441    BOOL utf8, compile_data *cd)    branch_chain *bcptr, BOOL utf8, compile_data *cd)
2442  {  {
2443  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2444    {    {
# Line 2205  where Perl recognizes it as the POSIX cl Line 2475  where Perl recognizes it as the POSIX cl
2475  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
2476  I think.  I think.
2477    
2478    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2479    It seems that the appearance of a nested POSIX class supersedes an apparent
2480    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2481    a digit.
2482    
2483    In Perl, unescaped square brackets may also appear as part of class names. For
2484    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2485    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2486    seem right at all. PCRE does not allow closing square brackets in POSIX class
2487    names.
2488    
2489  Arguments:  Arguments:
2490    ptr      pointer to the initial [    ptr      pointer to the initial [
2491    endptr   where to return the end pointer    endptr   where to return the end pointer
# Line 2213  Returns:   TRUE or FALSE Line 2494  Returns:   TRUE or FALSE
2494  */  */
2495    
2496  static BOOL  static BOOL
2497  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2498  {  {
2499  int terminator;          /* Don't combine these lines; the Solaris cc */  int terminator;          /* Don't combine these lines; the Solaris cc */
2500  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
2501  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != 0; ptr++)
2502    {    {
2503    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) ptr++; else    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2504        ptr++;
2505      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
2506      else
2507      {      {
     if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;  
2508      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2509        {        {
2510        *endptr = ptr;        *endptr = ptr;
2511        return TRUE;        return TRUE;
2512        }        }
2513        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
2514             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
2515              ptr[1] == CHAR_EQUALS_SIGN) &&
2516            check_posix_syntax(ptr, endptr))
2517          return FALSE;
2518      }      }
2519    }    }
2520  return FALSE;  return FALSE;
# Line 2250  Returns:     a value representing the na Line 2538  Returns:     a value representing the na
2538  */  */
2539    
2540  static int  static int
2541  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
2542  {  {
2543  const char *pn = posix_names;  const char *pn = posix_names;
2544  register int yield = 0;  register int yield = 0;
# Line 2297  Returns:     nothing Line 2585  Returns:     nothing
2585  */  */
2586    
2587  static void  static void
2588  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf8, compile_data *cd,
2589    uschar *save_hwm)    pcre_uchar *save_hwm)
2590  {  {
2591  uschar *ptr = group;  pcre_uchar *ptr = group;
2592    
2593  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf8)) != NULL)
2594    {    {
2595    int offset;    int offset;
2596    uschar *hc;    pcre_uchar *hc;
2597    
2598    /* See if this recursion is on the forward reference list. If so, adjust the    /* See if this recursion is on the forward reference list. If so, adjust the
2599    reference. */    reference. */
# Line 2350  Arguments: Line 2638  Arguments:
2638  Returns:         new code pointer  Returns:         new code pointer
2639  */  */
2640    
2641  static uschar *  static pcre_uchar *
2642  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2643  {  {
2644  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2645  *code++ = 255;  *code++ = 255;
2646  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2647  PUT(code, LINK_SIZE, 0);                       /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2648  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
2649  }  }
2650    
2651    
# Line 2379  Returns:             nothing Line 2667  Returns:             nothing
2667  */  */
2668    
2669  static void  static void
2670  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2671  {  {
2672  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2673  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
# Line 2517  Returns:        TRUE if possessifying is Line 2805  Returns:        TRUE if possessifying is
2805  */  */
2806    
2807  static BOOL  static BOOL
2808  check_auto_possessive(const uschar *previous, BOOL utf8, const uschar *ptr,  check_auto_possessive(const pcre_uchar *previous, BOOL utf8,
2809    int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
2810  {  {
2811  int c, next;  int c, next;
2812  int op_code = *previous++;  int op_code = *previous++;
# Line 2532  if ((options & PCRE_EXTENDED) != 0) Line 2820  if ((options & PCRE_EXTENDED) != 0)
2820      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2821      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2822        {        {
2823        ptr++;        ptr++;
2824        while (*ptr != 0)        while (*ptr != 0)
2825          {          {
2826          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2827          ptr++;          ptr++;
2828  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2829          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2830  #endif  #endif
2831          }          }
# Line 2576  if ((options & PCRE_EXTENDED) != 0) Line 2864  if ((options & PCRE_EXTENDED) != 0)
2864      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2865      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2866        {        {
2867        ptr++;        ptr++;
2868        while (*ptr != 0)        while (*ptr != 0)
2869          {          {
2870          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2871          ptr++;          ptr++;
2872  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2873          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2874  #endif  #endif
2875          }          }
# Line 2609  if (next >= 0) switch(op_code) Line 2897  if (next >= 0) switch(op_code)
2897  #endif  #endif
2898    return c != next;    return c != next;
2899    
2900    /* For CHARNC (caseless character) we must check the other case. If we have    /* For CHARI (caseless character) we must check the other case. If we have
2901    Unicode property support, we can use it to test the other case of    Unicode property support, we can use it to test the other case of
2902    high-valued characters. */    high-valued characters. */
2903    
2904    case OP_CHARNC:    case OP_CHARI:
2905  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2906    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
2907  #else  #else
# Line 2636  if (next >= 0) switch(op_code) Line 2924  if (next >= 0) switch(op_code)
2924  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
2925    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */
2926    
2927    /* For OP_NOT, its data is always a single-byte character. */    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These
2928      opcodes are not used for multi-byte characters, because they are coded using
2929      an XCLASS instead. */
2930    
2931    case OP_NOT:    case OP_NOT:
2932      return (c = *previous) == next;
2933    
2934      case OP_NOTI:
2935    if ((c = *previous) == next) return TRUE;    if ((c = *previous) == next) return TRUE;
   if ((options & PCRE_CASELESS) == 0) return FALSE;  
2936  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2937    if (utf8)    if (utf8)
2938      {      {
# Line 2745  replaced by OP_PROP codes when PCRE_UCP Line 3037  replaced by OP_PROP codes when PCRE_UCP
3037  switch(op_code)  switch(op_code)
3038    {    {
3039    case OP_CHAR:    case OP_CHAR:
3040    case OP_CHARNC:    case OP_CHARI:
3041  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3042    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3043  #else  #else
# Line 2932  Arguments: Line 3224  Arguments:
3224    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3225    reqbyteptr     set to the last literal character required, else < 0    reqbyteptr     set to the last literal character required, else < 0
3226    bcptr          points to current branch chain    bcptr          points to current branch chain
3227      cond_depth     conditional nesting depth
3228    cd             contains pointers to tables etc.    cd             contains pointers to tables etc.
3229    lengthptr      NULL during the real compile phase    lengthptr      NULL during the real compile phase
3230                   points to length accumulator during pre-compile phase                   points to length accumulator during pre-compile phase
# Line 2941  Returns:         TRUE on success Line 3234  Returns:         TRUE on success
3234  */  */
3235    
3236  static BOOL  static BOOL
3237  compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3238    int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,    const pcre_uchar **ptrptr, int *errorcodeptr, int *firstbyteptr,
3239    compile_data *cd, int *lengthptr)    int *reqbyteptr, branch_chain *bcptr, int cond_depth, compile_data *cd,
3240      int *lengthptr)
3241  {  {
3242  int repeat_type, op_type;  int repeat_type, op_type;
3243  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
# Line 2952  int greedy_default, greedy_non_default; Line 3246  int greedy_default, greedy_non_default;
3246  int firstbyte, reqbyte;  int firstbyte, reqbyte;
3247  int zeroreqbyte, zerofirstbyte;  int zeroreqbyte, zerofirstbyte;
3248  int req_caseopt, reqvary, tempreqvary;  int req_caseopt, reqvary, tempreqvary;
3249  int options = *optionsptr;  int options = *optionsptr;               /* May change dynamically */
3250  int after_manual_callout = 0;  int after_manual_callout = 0;
3251  int length_prevgroup = 0;  int length_prevgroup = 0;
3252  register int c;  register int c;
3253  register uschar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3254  uschar *last_code = code;  pcre_uchar *last_code = code;
3255  uschar *orig_code = code;  pcre_uchar *orig_code = code;
3256  uschar *tempcode;  pcre_uchar *tempcode;
3257  BOOL inescq = FALSE;  BOOL inescq = FALSE;
3258  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstbyte = FALSE;
3259  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
3260  const uschar *tempptr;  const pcre_uchar *tempptr;
3261  const uschar *nestptr = NULL;  const pcre_uchar *nestptr = NULL;
3262  uschar *previous = NULL;  pcre_uchar *previous = NULL;
3263  uschar *previous_callout = NULL;  pcre_uchar *previous_callout = NULL;
3264  uschar *save_hwm = NULL;  pcre_uchar *save_hwm = NULL;
3265  uschar classbits[32];  pcre_uchar classbits[32];
3266    
3267    /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3268    must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3269    dynamically as we process the pattern. */
3270    
3271  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3272  BOOL class_utf8;  BOOL class_utf8;
3273  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
3274  uschar *class_utf8data;  pcre_uint8 *class_utf8data;
3275  uschar *class_utf8data_base;  pcre_uint8 *class_utf8data_base;
3276  uschar utf8_char[6];  pcre_uint8 utf8_char[6];
3277  #else  #else
3278  BOOL utf8 = FALSE;  BOOL utf8 = FALSE;
 uschar *utf8_char = NULL;  
3279  #endif  #endif
3280    
3281  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
# Line 3029  for (;; ptr++) Line 3326  for (;; ptr++)
3326    int subfirstbyte;    int subfirstbyte;
3327    int terminator;    int terminator;
3328    int mclength;    int mclength;
3329    uschar mcbuffer[8];    int tempbracount;
3330      pcre_uchar mcbuffer[8];
3331    
3332    /* Get next byte in the pattern */    /* Get next byte in the pattern */
3333    
# Line 3076  for (;; ptr++) Line 3374  for (;; ptr++)
3374        }        }
3375    
3376      *lengthptr += (int)(code - last_code);      *lengthptr += (int)(code - last_code);
3377      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, code - last_code, c));      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, (int)(code - last_code),
3378          c));
3379    
3380      /* If "previous" is set and it is not at the start of the work space, move      /* If "previous" is set and it is not at the start of the work space, move
3381      it back to there, in order to avoid filling up the work space. Otherwise,      it back to there, in order to avoid filling up the work space. Otherwise,
# Line 3150  for (;; ptr++) Line 3449  for (;; ptr++)
3449      previous_callout = NULL;      previous_callout = NULL;
3450      }      }
3451    
3452    /* In extended mode, skip white space and comments */    /* In extended mode, skip white space and comments. */
3453    
3454    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
3455      {      {
3456      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if ((cd->ctypes[c] & ctype_space) != 0) continue;
3457      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3458        {        {
3459        ptr++;        ptr++;
3460        while (*ptr != 0)        while (*ptr != 0)
3461          {          {
3462          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3463          ptr++;          ptr++;
3464  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3465          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
3466  #endif  #endif
3467          }          }
# Line 3209  for (;; ptr++) Line 3508  for (;; ptr++)
3508      the setting of any following char as a first character. */      the setting of any following char as a first character. */
3509    
3510      case CHAR_CIRCUMFLEX_ACCENT:      case CHAR_CIRCUMFLEX_ACCENT:
3511        previous = NULL;
3512      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3513        {        {
3514        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
3515          *code++ = OP_CIRCM;
3516        }        }
3517      previous = NULL;      else *code++ = OP_CIRC;
     *code++ = OP_CIRC;  
3518      break;      break;
3519    
3520      case CHAR_DOLLAR_SIGN:      case CHAR_DOLLAR_SIGN:
3521      previous = NULL;      previous = NULL;
3522      *code++ = OP_DOLL;      *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
3523      break;      break;
3524    
3525      /* There can never be a first char if '.' is first, whatever happens about      /* There can never be a first char if '.' is first, whatever happens about
# Line 3326  for (;; ptr++) Line 3626  for (;; ptr++)
3626      than 256), because in that case the compiled code doesn't use the bit map.      than 256), because in that case the compiled code doesn't use the bit map.
3627      */      */
3628    
3629      memset(classbits, 0, 32 * sizeof(uschar));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
3630    
3631  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3632      class_utf8 = FALSE;                       /* No chars >= 256 */      class_utf8 = FALSE;                       /* No chars >= 256 */
# Line 3340  for (;; ptr++) Line 3640  for (;; ptr++)
3640    
3641      if (c != 0) do      if (c != 0) do
3642        {        {
3643        const uschar *oldptr;        const pcre_uchar *oldptr;
3644    
3645  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3646        if (utf8 && c > 127)        if (utf8 && c > 127)
# Line 3386  for (;; ptr++) Line 3686  for (;; ptr++)
3686          {          {
3687          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
3688          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
3689          register const uschar *cbits = cd->cbits;          register const pcre_uint8 *cbits = cd->cbits;
3690          uschar pbits[32];          pcre_uint8 pbits[32];
3691    
3692          if (ptr[1] != CHAR_COLON)          if (ptr[1] != CHAR_COLON)
3693            {            {
# Line 3442  for (;; ptr++) Line 3742  for (;; ptr++)
3742          /* Copy in the first table (always present) */          /* Copy in the first table (always present) */
3743    
3744          memcpy(pbits, cbits + posix_class_maps[posix_class],          memcpy(pbits, cbits + posix_class_maps[posix_class],
3745            32 * sizeof(uschar));            32 * sizeof(pcre_uint8));
3746    
3747          /* If there is a second table, add or remove it as required. */          /* If there is a second table, add or remove it as required. */
3748    
# Line 3504  for (;; ptr++) Line 3804  for (;; ptr++)
3804    
3805          if (c < 0)          if (c < 0)
3806            {            {
3807            register const uschar *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
3808            class_charcount += 2;     /* Greater than 1 is what matters */            class_charcount += 2;     /* Greater than 1 is what matters */
3809    
3810            switch (-c)            switch (-c)
# Line 3540  for (;; ptr++) Line 3840  for (;; ptr++)
3840              continue;              continue;
3841    
3842              /* Perl 5.004 onwards omits VT from \s, but we must preserve it              /* Perl 5.004 onwards omits VT from \s, but we must preserve it
3843              if it was previously set by something earlier in the character              if it was previously set by something earlier in the character
3844              class. */              class. */
3845    
3846              case ESC_s:              case ESC_s:
3847              classbits[0] |= cbits[cbit_space];              classbits[0] |= cbits[cbit_space];
3848              classbits[1] |= cbits[cbit_space+1] & ~0x08;              classbits[1] |= cbits[cbit_space+1] & ~0x08;
3849              for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];              for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
3850              continue;              continue;
3851    
# Line 3965  for (;; ptr++) Line 4265  for (;; ptr++)
4265    
4266      In UTF-8 mode, we can optimize the negative case only if there were no      In UTF-8 mode, we can optimize the negative case only if there were no
4267      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR
4268      operate on single-bytes only. This is an historical hangover. Maybe one day      operate on single-bytes characters only. This is an historical hangover.
4269      we can tidy these opcodes to handle multi-byte characters.      Maybe one day we can tidy these opcodes to handle multi-byte characters.
4270    
4271      The optimization throws away the bit map. We turn the item into a      The optimization throws away the bit map. We turn the item into a
4272      1-character OP_CHAR[NC] if it's positive, or OP_NOT if it's negative. Note      1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.
4273      that OP_NOT does not support multibyte characters. In the positive case, it      Note that OP_NOT[I] does not support multibyte characters. In the positive
4274      can cause firstbyte to be set. Otherwise, there can be no first char if      case, it can cause firstbyte to be set. Otherwise, there can be no first
4275      this item is first, whatever repeat count may follow. In the case of      char if this item is first, whatever repeat count may follow. In the case
4276      reqbyte, save the previous value for reinstating. */      of reqbyte, save the previous value for reinstating. */
4277    
4278  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4279      if (class_charcount == 1 && !class_utf8 &&      if (class_charcount == 1 && !class_utf8 &&
# Line 3984  for (;; ptr++) Line 4284  for (;; ptr++)
4284        {        {
4285        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
4286    
4287        /* The OP_NOT opcode works on one-byte characters only. */        /* The OP_NOT[I] opcodes work on one-byte characters only. */
4288    
4289        if (negate_class)        if (negate_class)
4290          {          {
4291          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
4292          zerofirstbyte = firstbyte;          zerofirstbyte = firstbyte;
4293          *code++ = OP_NOT;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4294          *code++ = class_lastchar;          *code++ = class_lastchar;
4295          break;          break;
4296          }          }
# Line 4118  for (;; ptr++) Line 4418  for (;; ptr++)
4418      op_type = 0;                    /* Default single-char op codes */      op_type = 0;                    /* Default single-char op codes */
4419      possessive_quantifier = FALSE;  /* Default not possessive quantifier */      possessive_quantifier = FALSE;  /* Default not possessive quantifier */
4420    
4421      /* Save start of previous item, in case we have to move it up to make space      /* Save start of previous item, in case we have to move it up in order to
4422      for an inserted OP_ONCE for the additional '+' extension. */      insert something before it. */
4423    
4424      tempcode = previous;      tempcode = previous;
4425    
# Line 4142  for (;; ptr++) Line 4442  for (;; ptr++)
4442        }        }
4443      else repeat_type = greedy_default;      else repeat_type = greedy_default;
4444    
4445        /* If previous was a recursion call, wrap it in atomic brackets so that
4446        previous becomes the atomic group. All recursions were so wrapped in the
4447        past, but it no longer happens for non-repeated recursions. In fact, the
4448        repeated ones could be re-implemented independently so as not to need this,
4449        but for the moment we rely on the code for repeating groups. */
4450    
4451        if (*previous == OP_RECURSE)
4452          {
4453          memmove(previous + 1 + LINK_SIZE, previous, 1 + LINK_SIZE);
4454          *previous = OP_ONCE;
4455          PUT(previous, 1, 2 + 2*LINK_SIZE);
4456          previous[2 + 2*LINK_SIZE] = OP_KET;
4457          PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4458          code += 2 + 2 * LINK_SIZE;
4459          length_prevgroup = 3 + 3*LINK_SIZE;
4460    
4461          /* When actually compiling, we need to check whether this was a forward
4462          reference, and if so, adjust the offset. */
4463    
4464          if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4465            {
4466            int offset = GET(cd->hwm, -LINK_SIZE);
4467            if (offset == previous + 1 - cd->start_code)
4468              PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
4469            }
4470          }
4471    
4472        /* Now handle repetition for the different types of item. */
4473    
4474      /* If previous was a character match, abolish the item and generate a      /* If previous was a character match, abolish the item and generate a
4475      repeat item instead. If a char item has a minumum of more than one, ensure      repeat item instead. If a char item has a minumum of more than one, ensure
4476      that it is set in reqbyte - it might not be if a sequence such as x{3} is      that it is set in reqbyte - it might not be if a sequence such as x{3} is
4477      the first thing in a branch because the x will have gone into firstbyte      the first thing in a branch because the x will have gone into firstbyte
4478      instead.  */      instead.  */
4479    
4480      if (*previous == OP_CHAR || *previous == OP_CHARNC)      if (*previous == OP_CHAR || *previous == OP_CHARI)
4481        {        {
4482          op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;
4483    
4484        /* Deal with UTF-8 characters that take up more than one byte. It's        /* Deal with UTF-8 characters that take up more than one byte. It's
4485        easier to write this out separately than try to macrify it. Use c to        easier to write this out separately than try to macrify it. Use c to
4486        hold the length of the character in bytes, plus 0x80 to flag that it's a        hold the length of the character in bytes, plus 0x80 to flag that it's a
# Line 4158  for (;; ptr++) Line 4489  for (;; ptr++)
4489  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4490        if (utf8 && (code[-1] & 0x80) != 0)        if (utf8 && (code[-1] & 0x80) != 0)
4491          {          {
4492          uschar *lastchar = code - 1;          pcre_uchar *lastchar = code - 1;
4493          while((*lastchar & 0xc0) == 0x80) lastchar--;          while((*lastchar & 0xc0) == 0x80) lastchar--;
4494          c = code - lastchar;            /* Length of UTF-8 character */          c = code - lastchar;            /* Length of UTF-8 character */
4495          memcpy(utf8_char, lastchar, c); /* Save the char */          memcpy(utf8_char, lastchar, c); /* Save the char */
# Line 4194  for (;; ptr++) Line 4525  for (;; ptr++)
4525      /* If previous was a single negated character ([^a] or similar), we use      /* If previous was a single negated character ([^a] or similar), we use
4526      one of the special opcodes, replacing it. The code is shared with single-      one of the special opcodes, replacing it. The code is shared with single-
4527      character repeats by setting opt_type to add a suitable offset into      character repeats by setting opt_type to add a suitable offset into
4528      repeat_type. We can also test for auto-possessification. OP_NOT is      repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI
4529      currently used only for single-byte chars. */      are currently used only for single-byte chars. */
4530    
4531      else if (*previous == OP_NOT)      else if (*previous == OP_NOT || *previous == OP_NOTI)
4532        {        {
4533        op_type = OP_NOTSTAR - OP_STAR;  /* Use "not" opcodes */        op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;
4534        c = previous[1];        c = previous[1];
4535        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4536            repeat_max < 0 &&            repeat_max < 0 &&
# Line 4220  for (;; ptr++) Line 4551  for (;; ptr++)
4551    
4552      else if (*previous < OP_EODN)      else if (*previous < OP_EODN)
4553        {        {
4554        uschar *oldcode;        pcre_uchar *oldcode;
4555        int prop_type, prop_value;        int prop_type, prop_value;
4556        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
4557        c = *previous;        c = *previous;
# Line 4396  for (;; ptr++) Line 4727  for (;; ptr++)
4727  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4728               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
4729  #endif  #endif
4730               *previous == OP_REF)               *previous == OP_REF ||
4731                 *previous == OP_REFI)
4732        {        {
4733        if (repeat_max == 0)        if (repeat_max == 0)
4734          {          {
# Line 4430  for (;; ptr++) Line 4762  for (;; ptr++)
4762        }        }
4763    
4764      /* If previous was a bracket group, we may have to replicate it in certain      /* If previous was a bracket group, we may have to replicate it in certain
4765      cases. */      cases. Note that at this point we can encounter only the "basic" bracket
4766        opcodes such as BRA and CBRA, as this is the place where they get converted
4767        into the more special varieties such as BRAPOS and SBRA. A test for >=
4768        OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
4769        ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow
4770        repetition of assertions, but now it does, for Perl compatibility. */
4771    
4772      else if (*previous == OP_BRA  || *previous == OP_CBRA ||      else if (*previous >= OP_ASSERT && *previous <= OP_COND)
              *previous == OP_ONCE || *previous == OP_COND)  
4773        {        {
4774        register int i;        register int i;
       int ketoffset = 0;  
4775        int len = (int)(code - previous);        int len = (int)(code - previous);
4776        uschar *bralink = NULL;        pcre_uchar *bralink = NULL;
4777          pcre_uchar *brazeroptr = NULL;
4778    
4779        /* Repeating a DEFINE group is pointless */        /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
4780          we just ignore the repeat. */
4781    
4782        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
4783          {          goto END_REPEAT;
         *errorcodeptr = ERR55;  
         goto FAILED;  
         }  
4784    
4785        /* If the maximum repeat count is unlimited, find the end of the bracket        /* There is no sense in actually repeating assertions. The only potential
4786        by scanning through from the start, and compute the offset back to it        use of repetition is in cases when the assertion is optional. Therefore,
4787        from the current code pointer. There may be an OP_OPT setting following        if the minimum is greater than zero, just ignore the repeat. If the
4788        the final KET, so we can't find the end just by going back from the code        maximum is not not zero or one, set it to 1. */
4789        pointer. */  
4790          if (*previous < OP_ONCE)    /* Assertion */
4791        if (repeat_max == -1)          {
4792          {          if (repeat_min > 0) goto END_REPEAT;
4793          register uschar *ket = previous;          if (repeat_max < 0 || repeat_max > 1) repeat_max = 1;
         do ket += GET(ket, 1); while (*ket != OP_KET);  
         ketoffset = (int)(code - ket);  
4794          }          }
4795    
4796        /* The case of a zero minimum is special because of the need to stick        /* The case of a zero minimum is special because of the need to stick
# Line 4479  for (;; ptr++) Line 4811  for (;; ptr++)
4811          **   goto END_REPEAT;          **   goto END_REPEAT;
4812          **   }          **   }
4813    
4814          However, that fails when a group is referenced as a subroutine from          However, that fails when a group or a subgroup within it is referenced
4815          elsewhere in the pattern, so now we stick in OP_SKIPZERO in front of it          as a subroutine from elsewhere in the pattern, so now we stick in
4816          so that it is skipped on execution. As we don't have a list of which          OP_SKIPZERO in front of it so that it is skipped on execution. As we
4817          groups are referenced, we cannot do this selectively.          don't have a list of which groups are referenced, we cannot do this
4818            selectively.
4819    
4820          If the maximum is 1 or unlimited, we just have to stick in the BRAZERO          If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
4821          and do no more at this point. However, we do need to adjust any          and do no more at this point. However, we do need to adjust any
# Line 4502  for (;; ptr++) Line 4835  for (;; ptr++)
4835              *previous++ = OP_SKIPZERO;              *previous++ = OP_SKIPZERO;
4836              goto END_REPEAT;              goto END_REPEAT;
4837              }              }
4838              brazeroptr = previous;    /* Save for possessive optimizing */
4839            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
4840            }            }
4841    
# Line 4571  for (;; ptr++) Line 4905  for (;; ptr++)
4905              if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte;              if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte;
4906              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
4907                {                {
4908                uschar *hc;                pcre_uchar *hc;
4909                uschar *this_hwm = cd->hwm;                pcre_uchar *this_hwm = cd->hwm;
4910                memcpy(code, previous, len);                memcpy(code, previous, len);
4911                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
4912                  {                  {
# Line 4623  for (;; ptr++) Line 4957  for (;; ptr++)
4957    
4958          else for (i = repeat_max - 1; i >= 0; i--)          else for (i = repeat_max - 1; i >= 0; i--)
4959            {            {
4960            uschar *hc;            pcre_uchar *hc;
4961            uschar *this_hwm = cd->hwm;            pcre_uchar *this_hwm = cd->hwm;
4962    
4963            *code++ = OP_BRAZERO + repeat_type;            *code++ = OP_BRAZERO + repeat_type;
4964    
# Line 4657  for (;; ptr++) Line 4991  for (;; ptr++)
4991            {            {
4992            int oldlinkoffset;            int oldlinkoffset;
4993            int offset = (int)(code - bralink + 1);            int offset = (int)(code - bralink + 1);
4994            uschar *bra = code - offset;            pcre_uchar *bra = code - offset;
4995            oldlinkoffset = GET(bra, 1);            oldlinkoffset = GET(bra, 1);
4996            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
4997            *code++ = OP_KET;            *code++ = OP_KET;
# Line 4666  for (;; ptr++) Line 5000  for (;; ptr++)
5000            }            }
5001          }          }
5002    
5003        /* If the maximum is unlimited, set a repeater in the final copy. We        /* If the maximum is unlimited, set a repeater in the final copy. For
5004        can't just offset backwards from the current code point, because we        ONCE brackets, that's all we need to do. However, possessively repeated
5005        don't know if there's been an options resetting after the ket. The        ONCE brackets can be converted into non-capturing brackets, as the
5006        correct offset was computed above.        behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
5007          deal with possessive ONCEs specially.
5008        Then, when we are doing the actual compile phase, check to see whether  
5009        this group is a non-atomic one that could match an empty string. If so,        Otherwise, when we are doing the actual compile phase, check to see
5010          whether this group is one that could match an empty string. If so,
5011        convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so        convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
5012        that runtime checking can be done. [This check is also applied to        that runtime checking can be done. [This check is also applied to ONCE
5013        atomic groups at runtime, but in a different way.] */        groups at runtime, but in a different way.]
5014    
5015          Then, if the quantifier was possessive and the bracket is not a
5016          conditional, we convert the BRA code to the POS form, and the KET code to
5017          KETRPOS. (It turns out to be convenient at runtime to detect this kind of
5018          subpattern at both the start and at the end.) The use of special opcodes
5019          makes it possible to reduce greatly the stack usage in pcre_exec(). If
5020          the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
5021    
5022          Then, if the minimum number of matches is 1 or 0, cancel the possessive
5023          flag so that the default action below, of wrapping everything inside
5024          atomic brackets, does not happen. When the minimum is greater than 1,
5025          there will be earlier copies of the group, and so we still have to wrap
5026          the whole thing. */
5027    
5028        else        else
5029          {          {
5030          uschar *ketcode = code - ketoffset;          pcre_uchar *ketcode = code - 1 - LINK_SIZE;
5031          uschar *bracode = ketcode - GET(ketcode, 1);          pcre_uchar *bracode = ketcode - GET(ketcode, 1);
5032          *ketcode = OP_KETRMAX + repeat_type;  
5033          if (lengthptr == NULL && *bracode != OP_ONCE)          /* Convert possessive ONCE brackets to non-capturing */
5034    
5035            if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&
5036                possessive_quantifier) *bracode = OP_BRA;
5037    
5038            /* For non-possessive ONCE brackets, all we need to do is to
5039            set the KET. */
5040    
5041            if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)
5042              *ketcode = OP_KETRMAX + repeat_type;
5043    
5044            /* Handle non-ONCE brackets and possessive ONCEs (which have been
5045            converted to non-capturing above). */
5046    
5047            else
5048            {            {
5049            uschar *scode = bracode;            /* In the compile phase, check for empty string matching. */
5050            do  
5051              if (lengthptr == NULL)
5052              {              {
5053              if (could_be_empty_branch(scode, ketcode, utf8, cd))              pcre_uchar *scode = bracode;
5054                do
5055                {                {
5056                *bracode += OP_SBRA - OP_BRA;                if (could_be_empty_branch(scode, ketcode, utf8, cd))
5057                break;                  {
5058                    *bracode += OP_SBRA - OP_BRA;
5059                    break;
5060                    }
5061                  scode += GET(scode, 1);
5062                }                }
5063              scode += GET(scode, 1);              while (*scode == OP_ALT);
5064              }              }
5065            while (*scode == OP_ALT);  
5066              /* Handle possessive quantifiers. */
5067    
5068              if (possessive_quantifier)
5069                {
5070                /* For COND brackets, we wrap the whole thing in a possessively
5071                repeated non-capturing bracket, because we have not invented POS
5072                versions of the COND opcodes. Because we are moving code along, we
5073                must ensure that any pending recursive references are updated. */
5074    
5075                if (*bracode == OP_COND || *bracode == OP_SCOND)
5076                  {
5077                  int nlen = (int)(code - bracode);
5078                  *code = OP_END;
5079                  adjust_recurse(bracode, 1 + LINK_SIZE, utf8, cd, save_hwm);
5080                  memmove(bracode + 1+LINK_SIZE, bracode, nlen);
5081                  code += 1 + LINK_SIZE;
5082                  nlen += 1 + LINK_SIZE;
5083                  *bracode = OP_BRAPOS;
5084                  *code++ = OP_KETRPOS;
5085                  PUTINC(code, 0, nlen);
5086                  PUT(bracode, 1, nlen);
5087                  }
5088    
5089                /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
5090    
5091                else
5092                  {
5093                  *bracode += 1;              /* Switch to xxxPOS opcodes */
5094                  *ketcode = OP_KETRPOS;
5095                  }
5096    
5097                /* If the minimum is zero, mark it as possessive, then unset the
5098                possessive flag when the minimum is 0 or 1. */
5099    
5100                if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
5101                if (repeat_min < 2) possessive_quantifier = FALSE;
5102                }
5103    
5104              /* Non-possessive quantifier */
5105    
5106              else *ketcode = OP_KETRMAX + repeat_type;
5107            }            }
5108          }          }
5109        }        }
# Line 4715  for (;; ptr++) Line 5124  for (;; ptr++)
5124        }        }
5125    
5126      /* If the character following a repeat is '+', or if certain optimization      /* If the character following a repeat is '+', or if certain optimization
5127      tests above succeeded, possessive_quantifier is TRUE. For some of the      tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
5128      simpler opcodes, there is an special alternative opcode for this. For      there are special alternative opcodes for this case. For anything else, we
5129      anything else, we wrap the entire repeated item inside OP_ONCE brackets.      wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
5130      The '+' notation is just syntactic sugar, taken from Sun's Java package,      notation is just syntactic sugar, taken from Sun's Java package, but the
5131      but the special opcodes can optimize it a bit. The repeated item starts at      special opcodes can optimize it.
5132      tempcode, not at previous, which might be the first part of a string whose  
5133      (former) last char we repeated.      Some (but not all) possessively repeated subpatterns have already been
5134        completely handled in the code just above. For them, possessive_quantifier
5135        is always FALSE at this stage.
5136    
5137        Note that the repeated item starts at tempcode, not at previous, which
5138        might be the first part of a string whose (former) last char we repeated.
5139    
5140      Possessifying an 'exact' quantifier has no effect, so we can ignore it. But      Possessifying an 'exact' quantifier has no effect, so we can ignore it. But
5141      an 'upto' may follow. We skip over an 'exact' item, and then test the      an 'upto' may follow. We skip over an 'exact' item, and then test the
# Line 4752  for (;; ptr++) Line 5166  for (;; ptr++)
5166          case OP_QUERY: *tempcode = OP_POSQUERY; break;          case OP_QUERY: *tempcode = OP_POSQUERY; break;
5167          case OP_UPTO:  *tempcode = OP_POSUPTO; break;          case OP_UPTO:  *tempcode = OP_POSUPTO; break;
5168    
5169          case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;          case OP_STARI:  *tempcode = OP_POSSTARI; break;
5170          case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;          case OP_PLUSI:  *tempcode = OP_POSPLUSI; break;
5171          case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;          case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
5172          case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;          case OP_UPTOI:  *tempcode = OP_POSUPTOI; break;
5173    
5174          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;
5175          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;
5176          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
5177          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;
5178    
5179            case OP_NOTSTARI:  *tempcode = OP_NOTPOSSTARI; break;
5180            case OP_NOTPLUSI:  *tempcode = OP_NOTPOSPLUSI; break;
5181            case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
5182            case OP_NOTUPTOI:  *tempcode = OP_NOTPOSUPTOI; break;
5183    
5184            case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;
5185            case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;
5186            case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
5187            case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;
5188    
5189          /* Because we are moving code along, we must ensure that any          /* Because we are moving code along, we must ensure that any
5190          pending recursive references are updated. */          pending recursive references are updated. */
5191    
# Line 4809  for (;; ptr++) Line 5233  for (;; ptr++)
5233        int i, namelen;        int i, namelen;
5234        int arglen = 0;        int arglen = 0;
5235        const char *vn = verbnames;        const char *vn = verbnames;
5236        const uschar *name = ptr + 1;        const pcre_uchar *name = ptr + 1;
5237        const uschar *arg = NULL;        const pcre_uchar *arg = NULL;
5238        previous = NULL;        previous = NULL;
5239        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};
5240        namelen = (int)(ptr - name);        namelen = (int)(ptr - name);
5241    
5242          /* It appears that Perl allows any characters whatsoever, other than
5243          a closing parenthesis, to appear in arguments, so we no longer insist on
5244          letters, digits, and underscores. */
5245    
5246        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
5247          {          {
5248          arg = ++ptr;          arg = ++ptr;
5249          while ((cd->ctypes[*ptr] & (ctype_letter|ctype_digit)) != 0          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
           || *ptr == '_') ptr++;  
5250          arglen = (int)(ptr - arg);          arglen = (int)(ptr - arg);
5251          }          }
5252    
# Line 4836  for (;; ptr++) Line 5263  for (;; ptr++)
5263          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
5264              strncmp((char *)name, vn, namelen) == 0)              strncmp((char *)name, vn, namelen) == 0)
5265            {            {
5266            /* Check for open captures before ACCEPT */            /* Check for open captures before ACCEPT and convert it to
5267              ASSERT_ACCEPT if in an assertion. */
5268    
5269            if (verbs[i].op == OP_ACCEPT)            if (verbs[i].op == OP_ACCEPT)
5270              {              {
5271              open_capitem *oc;              open_capitem *oc;
5272                if (arglen != 0)
5273                  {
5274                  *errorcodeptr = ERR59;
5275                  goto FAILED;
5276                  }
5277              cd->had_accept = TRUE;              cd->had_accept = TRUE;
5278              for (oc = cd->open_caps; oc != NULL; oc = oc->next)              for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5279                {                {
5280                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5281                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5282                }                }
5283                *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5284    
5285                /* Do not set firstbyte after *ACCEPT */
5286                if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
5287              }              }
5288    
5289            /* Handle the cases with/without an argument */            /* Handle other cases with/without an argument */
5290    
5291            if (arglen == 0)            else if (arglen == 0)
5292              {              {
5293              if (verbs[i].op < 0)   /* Argument is mandatory */              if (verbs[i].op < 0)   /* Argument is mandatory */
5294                {                {
# Line 4859  for (;; ptr++) Line 5296  for (;; ptr++)
5296                goto FAILED;                goto FAILED;
5297                }                }
5298              *code = verbs[i].op;              *code = verbs[i].op;
5299              if (*code++ == OP_THEN)              if (*code++ == OP_THEN) cd->external_flags |= PCRE_HASTHEN;
               {  
               PUT(code, 0, code - bcptr->current_branch - 1);  
               code += LINK_SIZE;  
               }  
5300              }              }
5301    
5302            else            else
# Line 4874  for (;; ptr++) Line 5307  for (;; ptr++)
5307                goto FAILED;                goto FAILED;
5308                }                }
5309              *code = verbs[i].op_arg;              *code = verbs[i].op_arg;
5310              if (*code++ == OP_THEN_ARG)              if (*code++ == OP_THEN_ARG) cd->external_flags |= PCRE_HASTHEN;
               {  
               PUT(code, 0, code - bcptr->current_branch - 1);  
               code += LINK_SIZE;  
               }  
5311              *code++ = arglen;              *code++ = arglen;
5312              memcpy(code, arg, arglen);              memcpy(code, arg, arglen);
5313              code += arglen;              code += arglen;
# Line 4903  for (;; ptr++) Line 5332  for (;; ptr++)
5332        {        {
5333        int i, set, unset, namelen;        int i, set, unset, namelen;
5334        int *optset;        int *optset;
5335        const uschar *name;        const pcre_uchar *name;
5336        uschar *slot;        pcre_uchar *slot;
5337    
5338        switch (*(++ptr))        switch (*(++ptr))
5339          {          {
# Line 5140  for (;; ptr++) Line 5569  for (;; ptr++)
5569          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5570          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */
5571          bravalue = OP_ASSERT;          bravalue = OP_ASSERT;
5572            cd->assert_depth += 1;
5573          ptr++;          ptr++;
5574          break;          break;
5575    
# Line 5154  for (;; ptr++) Line 5584  for (;; ptr++)
5584            continue;            continue;
5585            }            }
5586          bravalue = OP_ASSERT_NOT;          bravalue = OP_ASSERT_NOT;
5587            cd->assert_depth += 1;
5588          break;          break;
5589    
5590    
# Line 5163  for (;; ptr++) Line 5594  for (;; ptr++)
5594            {            {
5595            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */
5596            bravalue = OP_ASSERTBACK;            bravalue = OP_ASSERTBACK;
5597              cd->assert_depth += 1;
5598            ptr += 2;            ptr += 2;
5599            break;            break;
5600    
5601            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */
5602            bravalue = OP_ASSERTBACK_NOT;            bravalue = OP_ASSERTBACK_NOT;
5603              cd->assert_depth += 1;
5604            ptr += 2;            ptr += 2;
5605            break;            break;
5606    
# Line 5189  for (;; ptr++) Line 5622  for (;; ptr++)
5622    
5623          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5624          case CHAR_C:                 /* Callout - may be followed by digits; */          case CHAR_C:                 /* Callout - may be followed by digits; */
5625          previous_callout = code;  /* Save for later completion */          previous_callout = code;     /* Save for later completion */
5626          after_manual_callout = 1; /* Skip one item before completing */          after_manual_callout = 1;    /* Skip one item before completing */
5627          *code++ = OP_CALLOUT;          *code++ = OP_CALLOUT;
5628            {            {
5629            int n = 0;            int n = 0;
# Line 5326  for (;; ptr++) Line 5759  for (;; ptr++)
5759    
5760              if (!dupname)              if (!dupname)
5761                {                {
5762                uschar *cslot = cd->name_table;                pcre_uchar *cslot = cd->name_table;
5763                for (i = 0; i < cd->names_found; i++)                for (i = 0; i < cd->names_found; i++)
5764                  {                  {
5765                  if (cslot != slot)                  if (cslot != slot)
# Line 5373  for (;; ptr++) Line 5806  for (;; ptr++)
5806          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5807          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
5808    
5809          /* In the pre-compile phase, do a syntax check and set a dummy          /* In the pre-compile phase, do a syntax check. We used to just set
5810          reference number. */          a dummy reference number, because it was not used in the first pass.
5811            However, with the change of recursive back references to be atomic,
5812            we have to look for the number so that this state can be identified, as
5813            otherwise the incorrect length is computed. If it's not a backwards
5814            reference, the dummy number will do. */
5815    
5816          if (lengthptr != NULL)          if (lengthptr != NULL)
5817            {            {
5818              const pcre_uchar *temp;
5819    
5820            if (namelen == 0)            if (namelen == 0)
5821              {              {
5822              *errorcodeptr = ERR62;              *errorcodeptr = ERR62;
# Line 5393  for (;; ptr++) Line 5832  for (;; ptr++)
5832              *errorcodeptr = ERR48;              *errorcodeptr = ERR48;
5833              goto FAILED;              goto FAILED;
5834              }              }
5835            recno = 0;  
5836              /* The name table does not exist in the first pass, so we cannot
5837              do a simple search as in the code below. Instead, we have to scan the
5838              pattern to find the number. It is important that we scan it only as
5839              far as we have got because the syntax of named subpatterns has not
5840              been checked for the rest of the pattern, and find_parens() assumes
5841              correct syntax. In any case, it's a waste of resources to scan
5842              further. We stop the scan at the current point by temporarily
5843              adjusting the value of cd->endpattern. */
5844    
5845              temp = cd->end_pattern;
5846              cd->end_pattern = ptr;
5847              recno = find_parens(cd, name, namelen,
5848                (options & PCRE_EXTENDED) != 0, utf8);
5849              cd->end_pattern = temp;
5850              if (recno < 0) recno = 0;    /* Forward ref; set dummy number */
5851            }            }
5852    
5853          /* In the real compile, seek the name in the table. We check the name          /* In the real compile, seek the name in the table. We check the name
# Line 5443  for (;; ptr++) Line 5897  for (;; ptr++)
5897          case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:          case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:
5898          case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:          case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
5899            {            {
5900            const uschar *called;            const pcre_uchar *called;
5901            terminator = CHAR_RIGHT_PARENTHESIS;            terminator = CHAR_RIGHT_PARENTHESIS;
5902    
5903            /* Come here from the \g<...> and \g'...' code (Oniguruma            /* Come here from the \g<...> and \g'...' code (Oniguruma
# Line 5537  for (;; ptr++) Line 5991  for (;; ptr++)
5991    
5992                /* Fudge the value of "called" so that when it is inserted as an                /* Fudge the value of "called" so that when it is inserted as an
5993                offset below, what it actually inserted is the reference number                offset below, what it actually inserted is the reference number
5994                of the group. */                of the group. Then remember the forward reference. */
5995    
5996                called = cd->start_code + recno;                called = cd->start_code + recno;
5997                PUTINC(cd->hwm, 0, (int)(code + 2 + LINK_SIZE - cd->start_code));                PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
5998                }                }
5999    
6000              /* If not a forward reference, and the subpattern is still open,              /* If not a forward reference, and the subpattern is still open,
6001              this is a recursive call. We check to see if this is a left              this is a recursive call. We check to see if this is a left
6002              recursion that could loop for ever, and diagnose that case. */              recursion that could loop for ever, and diagnose that case. We
6003                must not, however, do this check if we are in a conditional
6004                subpattern because the condition might be testing for recursion in
6005                a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid.
6006                Forever loops are also detected at runtime, so those that occur in
6007                conditional subpatterns will be picked up then. */
6008    
6009              else if (GET(called, 1) == 0 &&              else if (GET(called, 1) == 0 && cond_depth <= 0 &&
6010                       could_be_empty(called, code, bcptr, utf8, cd))                       could_be_empty(called, code, bcptr, utf8, cd))
6011                {                {
6012                *errorcodeptr = ERR40;                *errorcodeptr = ERR40;
# Line 5555  for (;; ptr++) Line 6014  for (;; ptr++)
6014                }                }
6015              }              }
6016    
6017            /* Insert the recursion/subroutine item, automatically wrapped inside            /* Insert the recursion/subroutine item. */
           "once" brackets. Set up a "previous group" length so that a  
           subsequent quantifier will work. */  
   
           *code = OP_ONCE;  
           PUT(code, 1, 2 + 2*LINK_SIZE);  
           code += 1 + LINK_SIZE;  
6018    
6019            *code = OP_RECURSE;            *code = OP_RECURSE;
6020            PUT(code, 1, (int)(called - cd->start_code));            PUT(code, 1, (int)(called - cd->start_code));
6021            code += 1 + LINK_SIZE;            code += 1 + LINK_SIZE;
   
           *code = OP_KET;  
           PUT(code, 1, 2 + 2*LINK_SIZE);  
           code += 1 + LINK_SIZE;  
   
           length_prevgroup = 3 + 3*LINK_SIZE;  
6022            }            }
6023    
6024          /* Can't determine a first byte now */          /* Can't determine a first byte now */
# Line 5632  for (;; ptr++) Line 6079  for (;; ptr++)
6079          is necessary to ensure we correctly detect the start of the pattern in          is necessary to ensure we correctly detect the start of the pattern in
6080          both phases.          both phases.
6081    
6082          If we are not at the pattern start, compile code to change the ims          If we are not at the pattern start, reset the greedy defaults and the
6083          options if this setting actually changes any of them, and reset the          case value for firstbyte and reqbyte. */
         greedy defaults and the case value for firstbyte and reqbyte. */  
6084    
6085          if (*ptr == CHAR_RIGHT_PARENTHESIS)          if (*ptr == CHAR_RIGHT_PARENTHESIS)
6086            {            {
# Line 5645  for (;; ptr++) Line 6091  for (;; ptr++)
6091              }              }
6092            else            else
6093              {              {
             if ((options & PCRE_IMS) != (newoptions & PCRE_IMS))  
               {  
               *code++ = OP_OPT;  
               *code++ = newoptions & PCRE_IMS;  
               }  
6094              greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);              greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
6095              greedy_non_default = greedy_default ^ 1;              greedy_non_default = greedy_default ^ 1;
6096              req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;              req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;
6097              }              }
6098    
6099            /* Change options at this level, and pass them back for use            /* Change options at this level, and pass them back for use
6100            in subsequent branches. When not at the start of the pattern, this            in subsequent branches. */
           information is also necessary so that a resetting item can be  
           compiled at the end of a group (if we are in a group). */  
6101    
6102            *optionsptr = options = newoptions;            *optionsptr = options = newoptions;
6103            previous = NULL;       /* This item can't be repeated */            previous = NULL;       /* This item can't be repeated */
# Line 5694  for (;; ptr++) Line 6133  for (;; ptr++)
6133        skipbytes = 2;        skipbytes = 2;
6134        }        }
6135    
6136      /* Process nested bracketed regex. Assertions may not be repeated, but      /* Process nested bracketed regex. Assertions used not to be repeatable,
6137      other kinds can be. All their opcodes are >= OP_ONCE. We copy code into a      but this was changed for Perl compatibility, so all kinds can now be
6138      non-register variable in order to be able to pass its address because some      repeated. We copy code into a non-register variable (tempcode) in order to
6139      compilers complain otherwise. Pass in a new setting for the ims options if      be able to pass its address because some compilers complain otherwise. */
     they have changed. */  
6140    
6141      previous = (bravalue >= OP_ONCE)? code : NULL;      previous = code;                      /* For handling repetition */
6142      *code = bravalue;      *code = bravalue;
6143      tempcode = code;      tempcode = code;
6144      tempreqvary = cd->req_varyopt;     /* Save value before bracket */      tempreqvary = cd->req_varyopt;        /* Save value before bracket */
6145      length_prevgroup = 0;              /* Initialize for pre-compile phase */      tempbracount = cd->bracount;          /* Save value before bracket */
6146        length_prevgroup = 0;                 /* Initialize for pre-compile phase */
6147    
6148      if (!compile_regex(      if (!compile_regex(
6149           newoptions,                   /* The complete new option state */           newoptions,                      /* The complete new option state */
6150           options & PCRE_IMS,           /* The previous ims option state */           &tempcode,                       /* Where to put code (updated) */
6151           &tempcode,                    /* Where to put code (updated) */           &ptr,                            /* Input pointer (updated) */
6152           &ptr,                         /* Input pointer (updated) */           errorcodeptr,                    /* Where to put an error message */
          errorcodeptr,                 /* Where to put an error message */  
6153           (bravalue == OP_ASSERTBACK ||           (bravalue == OP_ASSERTBACK ||
6154            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
6155           reset_bracount,               /* True if (?| group */           reset_bracount,                  /* True if (?| group */
6156           skipbytes,                    /* Skip over bracket number */           skipbytes,                       /* Skip over bracket number */
6157           &subfirstbyte,                /* For possible first char */           cond_depth +
6158           &subreqbyte,                  /* For possible last char */             ((bravalue == OP_COND)?1:0),   /* Depth of condition subpatterns */
6159           bcptr,                        /* Current branch chain */           &subfirstbyte,                   /* For possible first char */
6160           cd,                           /* Tables block */           &subreqbyte,                     /* For possible last char */
6161           (lengthptr == NULL)? NULL :   /* Actual compile phase */           bcptr,                           /* Current branch chain */
6162             &length_prevgroup           /* Pre-compile phase */           cd,                              /* Tables block */
6163             (lengthptr == NULL)? NULL :      /* Actual compile phase */
6164               &length_prevgroup              /* Pre-compile phase */
6165           ))           ))
6166        goto FAILED;        goto FAILED;
6167    
6168        /* If this was an atomic group and there are no capturing groups within it,
6169        generate OP_ONCE_NC instead of OP_ONCE. */
6170    
6171        if (bravalue == OP_ONCE && cd->bracount <= tempbracount)
6172          *code = OP_ONCE_NC;
6173    
6174        if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
6175          cd->assert_depth -= 1;
6176    
6177      /* At the end of compiling, code is still pointing to the start of the      /* At the end of compiling, code is still pointing to the start of the
6178      group, while tempcode has been updated to point past the end of the group      group, while tempcode has been updated to point past the end of the group.
6179      and any option resetting that may follow it. The pattern pointer (ptr)      The pattern pointer (ptr) is on the bracket.
     is on the bracket. */  
6180    
6181      /* If this is a conditional bracket, check that there are no more than      If this is a conditional bracket, check that there are no more than
6182      two branches in the group, or just one if it's a DEFINE group. We do this      two branches in the group, or just one if it's a DEFINE group. We do this
6183      in the real compile phase, not in the pre-pass, where the whole group may      in the real compile phase, not in the pre-pass, where the whole group may
6184      not be available. */      not be available. */
6185    
6186      if (bravalue == OP_COND && lengthptr == NULL)      if (bravalue == OP_COND && lengthptr == NULL)
6187        {        {
6188        uschar *tc = code;        pcre_uchar *tc = code;
6189        int condcount = 0;        int condcount = 0;
6190    
6191        do {        do {
# Line 5795  for (;; ptr++) Line 6243  for (;; ptr++)
6243          goto FAILED;          goto FAILED;
6244          }          }
6245        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
6246        *code++ = OP_BRA;        code++;   /* This already contains bravalue */
6247        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
6248        *code++ = OP_KET;        *code++ = OP_KET;
6249        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
# Line 5912  for (;; ptr++) Line 6360  for (;; ptr++)
6360    
6361        if (-c == ESC_g)        if (-c == ESC_g)
6362          {          {
6363          const uschar *p;          const pcre_uchar *p;
6364          save_hwm = cd->hwm;   /* Normally this is set when '(' is read */          save_hwm = cd->hwm;   /* Normally this is set when '(' is read */
6365          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
6366            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
# Line 5963  for (;; ptr++) Line 6411  for (;; ptr++)
6411          }          }
6412    
6413        /* \k<name> or \k'name' is a back reference by name (Perl syntax).        /* \k<name> or \k'name' is a back reference by name (Perl syntax).
6414        We also support \k{name} (.NET syntax) */        We also support \k{name} (.NET syntax).  */
6415    
6416        if (-c == ESC_k && (ptr[1] == CHAR_LESS_THAN_SIGN ||        if (-c == ESC_k)
           ptr[1] == CHAR_APOSTROPHE || ptr[1] == CHAR_LEFT_CURLY_BRACKET))  
6417          {          {
6418            if ((ptr[1] != CHAR_LESS_THAN_SIGN &&
6419              ptr[1] != CHAR_APOSTROPHE && ptr[1] != CHAR_LEFT_CURLY_BRACKET))
6420              {
6421              *errorcodeptr = ERR69;
6422              break;
6423              }
6424          is_recurse = FALSE;          is_recurse = FALSE;
6425          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
6426            CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)?            CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)?
# Line 5987  for (;; ptr++) Line 6440  for (;; ptr++)
6440          HANDLE_REFERENCE:    /* Come here from named backref handling */          HANDLE_REFERENCE:    /* Come here from named backref handling */
6441          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
6442          previous = code;          previous = code;
6443          *code++ = OP_REF;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
6444          PUT2INC(code, 0, recno);          PUT2INC(code, 0, recno);
6445          cd->backref_map |= (recno < 32)? (1 << recno) : 1;          cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6446          if (recno > cd->top_backref) cd->top_backref = recno;          if (recno > cd->top_backref) cd->top_backref = recno;
# Line 6047  for (;; ptr++) Line 6500  for (;; ptr++)
6500            }            }
6501          else          else
6502  #endif  #endif
6503            {          /* In non-UTF-8 mode, we turn \C into OP_ALLANY instead of OP_ANYBYTE
6504            so that it works in DFA mode and in lookbehinds. */
6505    
6506              {
6507            previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;            previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;
6508            *code++ = -c;            *code++ = (!utf8 && c == -ESC_C)? OP_ALLANY : -c;
6509            }            }
6510          }          }
6511        continue;        continue;
# Line 6095  for (;; ptr++) Line 6551  for (;; ptr++)
6551    
6552      ONE_CHAR:      ONE_CHAR:
6553      previous = code;      previous = code;
6554      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARNC : OP_CHAR;      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR;
6555      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
6556    
6557      /* Remember if \r or \n were seen */      /* Remember if \r or \n were seen */
# Line 6124  for (;; ptr++) Line 6580  for (;; ptr++)
6580        else firstbyte = reqbyte = REQ_NONE;        else firstbyte = reqbyte = REQ_NONE;
6581        }        }
6582    
6583      /* firstbyte was previously set; we can set reqbyte only the length is      /* firstbyte was previously set; we can set reqbyte only if the length is
6584      1 or the matching is caseful. */      1 or the matching is caseful. */
6585    
6586      else      else
# Line 6159  return FALSE; Line 6615  return FALSE;
6615  /* On entry, ptr is pointing past the bracket character, but on return it  /* On entry, ptr is pointing past the bracket character, but on return it
6616  points to the closing bracket, or vertical bar, or end of string. The code  points to the closing bracket, or vertical bar, or end of string. The code
6617  variable is pointing at the byte into which the BRA operator has been stored.  variable is pointing at the byte into which the BRA operator has been stored.
 If the ims options are changed at the start (for a (?ims: group) or during any  
 branch, we need to insert an OP_OPT item at the start of every following branch  
 to ensure they get set correctly at run time, and also pass the new options  
 into every subsequent branch compile.  
   
6618  This function is used during the pre-compile phase when we are trying to find  This function is used during the pre-compile phase when we are trying to find
6619  out the amount of memory needed, as well as during the real compile phase. The  out the amount of memory needed, as well as during the real compile phase. The
6620  value of lengthptr distinguishes the two phases.  value of lengthptr distinguishes the two phases.
6621    
6622  Arguments:  Arguments:
6623    options        option bits, including any changes for this subpattern    options        option bits, including any changes for this subpattern
   oldims         previous settings of ims option bits  
6624    codeptr        -> the address of the current code pointer    codeptr        -> the address of the current code pointer
6625    ptrptr         -> the address of the current pattern pointer    ptrptr         -> the address of the current pattern pointer
6626    errorcodeptr   -> pointer to error code variable    errorcodeptr   -> pointer to error code variable
6627    lookbehind     TRUE if this is a lookbehind assertion    lookbehind     TRUE if this is a lookbehind assertion
6628    reset_bracount TRUE to reset the count for each branch    reset_bracount TRUE to reset the count for each branch
6629    skipbytes      skip this many bytes at start (for brackets and OP_COND)    skipbytes      skip this many bytes at start (for brackets and OP_COND)
6630      cond_depth     depth of nesting for conditional subpatterns
6631    firstbyteptr   place to put the first required character, or a negative number    firstbyteptr   place to put the first required character, or a negative number
6632    reqbyteptr     place to put the last required character, or a negative number    reqbyteptr     place to put the last required character, or a negative number
6633    bcptr          pointer to the chain of currently open branches    bcptr          pointer to the chain of currently open branches
# Line 6188  Returns:         TRUE on success Line 6639  Returns:         TRUE on success
6639  */  */
6640    
6641  static BOOL  static BOOL
6642  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,  compile_regex(int options, pcre_uchar **codeptr, const pcre_uchar **ptrptr,
6643    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
6644    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,    int cond_depth, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,
6645    int *lengthptr)    compile_data *cd, int *lengthptr)
6646  {  {
6647  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
6648  uschar *code = *codeptr;  pcre_uchar *code = *codeptr;
6649  uschar *last_branch = code;  pcre_uchar *last_branch = code;
6650  uschar *start_bracket = code;  pcre_uchar *start_bracket = code;
6651  uschar *reverse_count = NULL;  pcre_uchar *reverse_count = NULL;
6652  open_capitem capitem;  open_capitem capitem;
6653  int capnumber = 0;  int capnumber = 0;
6654  int firstbyte, reqbyte;  int firstbyte, reqbyte;
# Line 6205  int branchfirstbyte, branchreqbyte; Line 6656  int branchfirstbyte, branchreqbyte;
6656  int length;  int length;
6657  int orig_bracount;  int orig_bracount;
6658  int max_bracount;  int max_bracount;
 int old_external_options = cd->external_options;  
6659  branch_chain bc;  branch_chain bc;
6660    
6661  bc.outer = bcptr;  bc.outer = bcptr;
# Line 6229  pre-compile phase to find out whether an Line 6679  pre-compile phase to find out whether an
6679    
6680  /* If this is a capturing subpattern, add to the chain of open capturing items  /* If this is a capturing subpattern, add to the chain of open capturing items
6681  so that we can detect them if (*ACCEPT) is encountered. This is also used to  so that we can detect them if (*ACCEPT) is encountered. This is also used to
6682  detect groups that contain recursive back references to themselves. */  detect groups that contain recursive back references to themselves. Note that
6683    only OP_CBRA need be tested here; changing this opcode to one of its variants,
6684    e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
6685    
6686  if (*code == OP_CBRA)  if (*code == OP_CBRA)
6687    {    {
# Line 6255  for (;;) Line 6707  for (;;)
6707    
6708    if (reset_bracount) cd->bracount = orig_bracount;    if (reset_bracount) cd->bracount = orig_bracount;
6709    
   /* Handle a change of ims options at the start of the branch */  
   
   if ((options & PCRE_IMS) != oldims)  
     {  
     *code++ = OP_OPT;  
     *code++ = options & PCRE_IMS;  
     length += 2;  
     }  
   
6710    /* Set up dummy OP_REVERSE if lookbehind assertion */    /* Set up dummy OP_REVERSE if lookbehind assertion */
6711    
6712    if (lookbehind)    if (lookbehind)
# Line 6278  for (;;) Line 6721  for (;;)
6721    into the length. */    into the length. */
6722    
6723    if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstbyte,    if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstbyte,
6724          &branchreqbyte, &bc, cd, (lengthptr == NULL)? NULL : &length))          &branchreqbyte, &bc, cond_depth, cd,
6725            (lengthptr == NULL)? NULL : &length))
6726      {      {
6727      *ptrptr = ptr;      *ptrptr = ptr;
6728      return FALSE;      return FALSE;
6729      }      }
6730    
   /* If the external options have changed during this branch, it means that we  
   are at the top level, and a leading option setting has been encountered. We  
   need to re-set the original option values to take account of this so that,  
   during the pre-compile phase, we know to allow for a re-set at the start of  
   subsequent branches. */  
   
   if (old_external_options != cd->external_options)  
     oldims = cd->external_options & PCRE_IMS;  
   
6731    /* Keep the highest bracket count in case (?| was used and some branch    /* Keep the highest bracket count in case (?| was used and some branch
6732    has fewer than the rest. */    has fewer than the rest. */
6733    
# Line 6353  for (;;) Line 6788  for (;;)
6788        {        {
6789        int fixed_length;        int fixed_length;
6790        *code = OP_END;        *code = OP_END;
6791        fixed_length = find_fixedlength(last_branch, options, FALSE, cd);        fixed_length = find_fixedlength(last_branch,  (options & PCRE_UTF8) != 0,
6792            FALSE, cd);
6793        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
6794        if (fixed_length == -3)        if (fixed_length == -3)
6795          {          {
# Line 6361  for (;;) Line 6797  for (;;)
6797          }          }
6798        else if (fixed_length < 0)        else if (fixed_length < 0)
6799          {          {
6800          *errorcodeptr = (fixed_length == -2)? ERR36 : ERR25;          *errorcodeptr = (fixed_length == -2)? ERR36 :
6801                            (fixed_length == -4)? ERR70: ERR25;
6802          *ptrptr = ptr;          *ptrptr = ptr;
6803          return FALSE;          return FALSE;
6804          }          }
# Line 6374  for (;;) Line 6811  for (;;)
6811    of offsets, with the field in the BRA item now becoming an offset to the    of offsets, with the field in the BRA item now becoming an offset to the
6812    first alternative. If there are no alternatives, it points to the end of the    first alternative. If there are no alternatives, it points to the end of the
6813    group. The length in the terminating ket is always the length of the whole    group. The length in the terminating ket is always the length of the whole
6814    bracketed item. If any of the ims options were changed inside the group,    bracketed item. Return leaving the pointer at the terminating char. */
   compile a resetting op-code following, except at the very end of the pattern.  
   Return leaving the pointer at the terminating char. */  
6815    
6816    if (*ptr != CHAR_VERTICAL_LINE)    if (*ptr != CHAR_VERTICAL_LINE)
6817      {      {
# Line 6420  for (;;) Line 6855  for (;;)
6855        cd->open_caps = cd->open_caps->next;        cd->open_caps = cd->open_caps->next;
6856        }        }
6857    
     /* Reset options if needed. */  
   
     if ((options & PCRE_IMS) != oldims && *ptr == CHAR_RIGHT_PARENTHESIS)  
       {  
       *code++ = OP_OPT;  
       *code++ = oldims;  
       length += 2;  
       }  
   
6858      /* Retain the highest bracket number, in case resetting was used. */      /* Retain the highest bracket number, in case resetting was used. */
6859    
6860      cd->bracount = max_bracount;      cd->bracount = max_bracount;
# Line 6488  for (;;) Line 6914  for (;;)
6914  /* Try to find out if this is an anchored regular expression. Consider each  /* Try to find out if this is an anchored regular expression. Consider each
6915  alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket  alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket
6916  all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then  all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then
6917  it's anchored. However, if this is a multiline pattern, then only OP_SOD  it's anchored. However, if this is a multiline pattern, then only OP_SOD will
6918  counts, since OP_CIRC can match in the middle.  be found, because ^ generates OP_CIRCM in that mode.
6919    
6920  We can also consider a regex to be anchored if OP_SOM starts all its branches.  We can also consider a regex to be anchored if OP_SOM starts all its branches.
6921  This is the code for \G, which means "match at start of match position, taking  This is the code for \G, which means "match at start of match position, taking
# Line 6510  of the more common cases more precisely. Line 6936  of the more common cases more precisely.
6936    
6937  Arguments:  Arguments:
6938    code           points to start of expression (the bracket)    code           points to start of expression (the bracket)
   options        points to the options setting  
6939    bracket_map    a bitmap of which brackets we are inside while testing; this    bracket_map    a bitmap of which brackets we are inside while testing; this
6940                    handles up to substring 31; after that we just have to take                    handles up to substring 31; after that we just have to take
6941                    the less precise approach                    the less precise approach
# Line 6520  Returns:     TRUE or FALSE Line 6945  Returns:     TRUE or FALSE
6945  */  */
6946    
6947  static BOOL  static BOOL
6948  is_anchored(register const uschar *code, int *options, unsigned int bracket_map,  is_anchored(register const pcre_uchar *code, unsigned int bracket_map,
6949    unsigned int backref_map)    unsigned int backref_map)
6950  {  {
6951  do {  do {
6952     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const pcre_uchar *scode = first_significant_code(
6953       options, PCRE_MULTILINE, FALSE);       code + _pcre_OP_lengths[*code], FALSE);
6954     register int op = *scode;     register int op = *scode;
6955    
6956     /* Non-capturing brackets */     /* Non-capturing brackets */
6957    
6958     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
6959           op == OP_SBRA || op == OP_SBRAPOS)
6960       {       {
6961       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
6962       }       }
6963    
6964     /* Capturing brackets */     /* Capturing brackets */
6965    
6966     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
6967                op == OP_SCBRA || op == OP_SCBRAPOS)
6968       {       {
6969       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
6970       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
6971       if (!is_anchored(scode, options, new_map, backref_map)) return FALSE;       if (!is_anchored(scode, new_map, backref_map)) return FALSE;
6972       }       }
6973    
6974     /* Other brackets */     /* Other brackets */
6975    
6976     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_ONCE_NC ||
6977                op == OP_COND)
6978       {       {
6979       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
6980       }       }
6981    
6982     /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and     /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and
# Line 6563  do { Line 6991  do {
6991    
6992     /* Check for explicit anchoring */     /* Check for explicit anchoring */
6993    
6994     else if (op != OP_SOD && op != OP_SOM &&     else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE;
            ((*options & PCRE_MULTILINE) != 0 || op != OP_CIRC))  
      return FALSE;  
6995     code += GET(code, 1);     code += GET(code, 1);
6996     }     }
6997  while (*code == OP_ALT);   /* Loop for each alternative */  while (*code == OP_ALT);   /* Loop for each alternative */
# Line 6596  Returns:         TRUE or FALSE Line 7022  Returns:         TRUE or FALSE
7022  */  */
7023    
7024  static BOOL  static BOOL
7025  is_startline(const uschar *code, unsigned int bracket_map,  is_startline(const pcre_uchar *code, unsigned int bracket_map,
7026    unsigned int backref_map)    unsigned int backref_map)
7027  {  {
7028  do {  do {
7029     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const pcre_uchar *scode = first_significant_code(
7030       NULL, 0, FALSE);       code + _pcre_OP_lengths[*code], FALSE);
7031     register int op = *scode;     register int op = *scode;
7032    
7033     /* If we are at the start of a conditional assertion group, *both* the     /* If we are at the start of a conditional assertion group, *both* the
# Line 6628  do { Line 7054  do {
7054         scode += 1 + LINK_SIZE;         scode += 1 + LINK_SIZE;
7055         break;         break;
7056         }         }
7057       scode = first_significant_code(scode, NULL, 0, FALSE);       scode = first_significant_code(scode, FALSE);
7058       op = *scode;       op = *scode;
7059       }       }
7060    
7061     /* Non-capturing brackets */     /* Non-capturing brackets */
7062    
7063     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
7064           op == OP_SBRA || op == OP_SBRAPOS)
7065       {       {
7066       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;
7067       }       }
7068    
7069     /* Capturing brackets */     /* Capturing brackets */
7070    
7071     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
7072                op == OP_SCBRA || op == OP_SCBRAPOS)
7073       {       {
7074       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
7075       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
# Line 6650  do { Line 7078  do {
7078    
7079     /* Other brackets */     /* Other brackets */
7080    
7081     else if (op == OP_ASSERT || op == OP_ONCE)     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_ONCE_NC)
7082       {       {
7083       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;
7084       }       }
# Line 6665  do { Line 7093  do {
7093    
7094     /* Check for explicit circumflex */     /* Check for explicit circumflex */
7095    
7096     else if (op != OP_CIRC) return FALSE;     else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;
7097    
7098     /* Move on to the next alternative */     /* Move on to the next alternative */
7099    
# Line 6691  we return that char, otherwise -1. Line 7119  we return that char, otherwise -1.
7119    
7120  Arguments:  Arguments:
7121    code       points to start of expression (the bracket)    code       points to start of expression (the bracket)
   options    pointer to the options (used to check casing changes)  
7122    inassert   TRUE if in an assertion    inassert   TRUE if in an assertion
7123    
7124  Returns:     -1 or the fixed first char  Returns:     -1 or the fixed first char
7125  */  */
7126    
7127  static int  static int
7128  find_firstassertedchar(const uschar *code, int *options, BOOL inassert)  find_firstassertedchar(const pcre_uchar *code, BOOL inassert)
7129  {  {
7130  register int c = -1;  register int c = -1;
7131  do {  do {
7132     int d;     int d;
7133     const uschar *scode =     int xl = (*code == OP_CBRA || *code == OP_SCBRA ||
7134       first_significant_code(code + 1+LINK_SIZE, options, PCRE_CASELESS, TRUE);               *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? 2:0;
7135       const pcre_uchar *scode = first_significant_code(code + 1+LINK_SIZE + xl,
7136         TRUE);
7137     register int op = *scode;     register int op = *scode;
7138    
7139     switch(op)     switch(op)
# Line 6713  do { Line 7142  do {
7142       return -1;       return -1;
7143    
7144       case OP_BRA:       case OP_BRA:
7145         case OP_BRAPOS:
7146       case OP_CBRA:       case OP_CBRA:
7147         case OP_SCBRA:
7148         case OP_CBRAPOS:
7149         case OP_SCBRAPOS:
7150       case OP_ASSERT:       case OP_ASSERT:
7151       case OP_ONCE:       case OP_ONCE:
7152         case OP_ONCE_NC:
7153       case OP_COND:       case OP_COND:
7154       if ((d = find_firstassertedchar(scode, options, op == OP_ASSERT)) < 0)       if ((d = find_firstassertedchar(scode, op == OP_ASSERT)) < 0)
7155         return -1;         return -1;
7156       if (c < 0) c = d; else if (c != d) return -1;       if (c < 0) c = d; else if (c != d) return -1;
7157       break;       break;
7158    
7159       case OP_EXACT:       /* Fall through */       case OP_EXACT:
7160       scode += 2;       scode += 2;
7161         /* Fall through */
7162    
7163       case OP_CHAR:       case OP_CHAR:
      case OP_CHARNC:  
7164       case OP_PLUS:       case OP_PLUS:
7165       case OP_MINPLUS:       case OP_MINPLUS:
7166       case OP_POSPLUS:       case OP_POSPLUS:
7167       if (!inassert) return -1;       if (!inassert) return -1;
7168       if (c < 0)       if (c < 0) c = scode[1];
7169         {         else if (c != scode[1]) return -1;
7170         c = scode[1];       break;
7171         if ((*options & PCRE_CASELESS) != 0) c |= REQ_CASELESS;  
7172         }       case OP_EXACTI:
7173       else if (c != scode[1]) return -1;       scode += 2;
7174         /* Fall through */
7175    
7176         case OP_CHARI:
7177         case OP_PLUSI:
7178         case OP_MINPLUSI:
7179         case OP_POSPLUSI:
7180         if (!inassert) return -1;
7181         if (c < 0) c = scode[1] | REQ_CASELESS;
7182           else if (c != scode[1]) return -1;
7183       break;       break;
7184       }       }
7185    
# Line 6789  int errorcode = 0; Line 7232  int errorcode = 0;
7232  int skipatstart = 0;  int skipatstart = 0;
7233  BOOL utf8;  BOOL utf8;
7234  size_t size;  size_t size;
7235  uschar *code;  pcre_uchar *code;
7236  const uschar *codestart;  const pcre_uchar *codestart;
7237  const uschar *ptr;  const pcre_uchar *ptr;
7238  compile_data compile_block;  compile_data compile_block;
7239  compile_data *cd = &compile_block;  compile_data *cd = &compile_block;
7240    
# Line 6801  as soon as possible, so that a fairly la Line 7244  as soon as possible, so that a fairly la
7244  this purpose. The same space is used in the second phase for remembering where  this purpose. The same space is used in the second phase for remembering where
7245  to fill in forward references to subpatterns. */  to fill in forward references to subpatterns. */
7246    
7247  uschar cworkspace[COMPILE_WORK_SIZE];  pcre_uchar cworkspace[COMPILE_WORK_SIZE];
7248    
7249  /* Set this early so that early errors get offset 0. */  /* Set this early so that early errors get offset 0. */
7250    
7251  ptr = (const uschar *)pattern;  ptr = (const pcre_uchar *)pattern;
7252    
7253  /* We can't pass back an error message if errorptr is NULL; I guess the best we  /* We can't pass back an error message if errorptr is NULL; I guess the best we
7254  can do is just return NULL, but we can set a code value if there is a code  can do is just return NULL, but we can set a code value if there is a code
# Line 6859  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 7302  while (ptr[skipatstart] == CHAR_LEFT_PAR
7302      { skipatstart += 7; options |= PCRE_UTF8; continue; }      { skipatstart += 7; options |= PCRE_UTF8; continue; }
7303    else if (strncmp((char *)(ptr+skipatstart+2), STRING_UCP_RIGHTPAR, 4) == 0)    else if (strncmp((char *)(ptr+skipatstart+2), STRING_UCP_RIGHTPAR, 4) == 0)
7304      { skipatstart += 6; options |= PCRE_UCP; continue; }      { skipatstart += 6; options |= PCRE_UCP; continue; }
7305      else if (strncmp((char *)(ptr+skipatstart+2), STRING_NO_START_OPT_RIGHTPAR, 13) == 0)
7306        { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }
7307    
7308    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)
7309      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
# Line 6885  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 7330  while (ptr[skipatstart] == CHAR_LEFT_PAR
7330    
7331  utf8 = (options & PCRE_UTF8) != 0;  utf8 = (options & PCRE_UTF8) != 0;
7332    
7333  /* Can't support UTF8 unless PCRE has been compiled to include the code. */  /* Can't support UTF8 unless PCRE has been compiled to include the code. The
7334    return of an error code from _pcre_valid_utf8() is a new feature, introduced in
7335    release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
7336    not used here. */
7337    
7338  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
7339  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&
7340       (*erroroffset = _pcre_valid_utf8((USPTR)pattern, -1)) >= 0)       (errorcode = _pcre_valid_utf8((PCRE_PUCHAR)pattern, -1, erroroffset)) != 0)
7341    {    {
7342    errorcode = ERR44;    errorcode = ERR44;
7343    goto PCRE_EARLY_ERROR_RETURN2;    goto PCRE_EARLY_ERROR_RETURN2;
# Line 6914  if ((options & PCRE_UCP) != 0) Line 7362  if ((options & PCRE_UCP) != 0)
7362    
7363  /* Check validity of \R options. */  /* Check validity of \R options. */
7364    
7365  switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))  if ((options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) ==
7366         (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
7367    {    {
7368    case 0:    errorcode = ERR56;
7369    case PCRE_BSR_ANYCRLF:    goto PCRE_EARLY_ERROR_RETURN;
   case PCRE_BSR_UNICODE:  
   break;  
   default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN;  
7370    }    }
7371    
7372  /* Handle different types of newline. The three bits give seven cases. The  /* Handle different types of newline. The three bits give seven cases. The
# Line 6989  cd->name_table = NULL; Line 7435  cd->name_table = NULL;
7435  cd->start_workspace = cworkspace;  cd->start_workspace = cworkspace;
7436  cd->start_code = cworkspace;  cd->start_code = cworkspace;
7437  cd->hwm = cworkspace;  cd->hwm = cworkspace;
7438  cd->start_pattern = (const uschar *)pattern;  cd->start_pattern = (const pcre_uchar *)pattern;
7439  cd->end_pattern = (const uschar *)(pattern + strlen(pattern));  cd->end_pattern = (const pcre_uchar *)(pattern + strlen(pattern));
7440  cd->req_varyopt = 0;  cd->req_varyopt = 0;
7441  cd->external_options = options;  cd->external_options = options;
7442  cd->external_flags = 0;  cd->external_flags = 0;
# Line 7005  outside can help speed up starting point Line 7451  outside can help speed up starting point
7451  ptr += skipatstart;  ptr += skipatstart;
7452  code = cworkspace;  code = cworkspace;
7453  *code = OP_BRA;  *code = OP_BRA;
7454  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,  (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,
7455    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,    FALSE, 0, 0, &firstbyte, &reqbyte, NULL, cd, &length);
   &length);  
7456  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
7457    
7458  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
# Line 7061  field; this time it's used for rememberi Line 7506  field; this time it's used for rememberi
7506  */  */
7507    
7508  cd->final_bracount = cd->bracount;  /* Save for checking forward references */  cd->final_bracount = cd->bracount;  /* Save for checking forward references */
7509    cd->assert_depth = 0;
7510  cd->bracount = 0;  cd->bracount = 0;
7511  cd->names_found = 0;  cd->names_found = 0;
7512  cd->name_table = (uschar *)re + re->name_table_offset;  cd->name_table = (pcre_uchar *)re + re->name_table_offset;
7513  codestart = cd->name_table + re->name_entry_size * re->name_count;  codestart = cd->name_table + re->name_entry_size * re->name_count;
7514  cd->start_code = codestart;  cd->start_code = codestart;
7515  cd->hwm = cworkspace;  cd->hwm = cworkspace;
# Line 7076  cd->open_caps = NULL; Line 7522  cd->open_caps = NULL;
7522  error, errorcode will be set non-zero, so we don't need to look at the result  error, errorcode will be set non-zero, so we don't need to look at the result
7523  of the function here. */  of the function here. */
7524    
7525  ptr = (const uschar *)pattern + skipatstart;  ptr = (const pcre_uchar *)pattern + skipatstart;
7526  code = (uschar *)codestart;  code = (pcre_uchar *)codestart;
7527  *code = OP_BRA;  *code = OP_BRA;
7528  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,  (void)compile_regex(re->options, &code, &ptr, &errorcode, FALSE, FALSE, 0, 0,
7529    &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);    &firstbyte, &reqbyte, NULL, cd, NULL);
7530  re->top_bracket = cd->bracount;  re->top_bracket = cd->bracount;
7531  re->top_backref = cd->top_backref;  re->top_backref = cd->top_backref;
7532  re->flags = cd->external_flags;  re->flags = cd->external_flags;
7533    
7534  if (cd->had_accept) reqbyte = -1;   /* Must disable after (*ACCEPT) */  if (cd->had_accept) reqbyte = REQ_NONE;   /* Must disable after (*ACCEPT) */
7535    
7536  /* If not reached end of pattern on success, there's an excess bracket. */  /* If not reached end of pattern on success, there's an excess bracket. */
7537    
# Line 7105  if (code - codestart > length) errorcode Line 7551  if (code - codestart > length) errorcode
7551  while (errorcode == 0 && cd->hwm > cworkspace)  while (errorcode == 0 && cd->hwm > cworkspace)
7552    {    {
7553    int offset, recno;    int offset, recno;
7554    const uschar *groupptr;    const pcre_uchar *groupptr;
7555    cd->hwm -= LINK_SIZE;    cd->hwm -= LINK_SIZE;
7556    offset = GET(cd->hwm, 0);    offset = GET(cd->hwm, 0);
7557    recno = GET(codestart, offset);    recno = GET(codestart, offset);
7558    groupptr = _pcre_find_bracket(codestart, utf8, recno);    groupptr = _pcre_find_bracket(codestart, utf8, recno);
7559    if (groupptr == NULL) errorcode = ERR53;    if (groupptr == NULL) errorcode = ERR53;
7560      else PUT(((uschar *)codestart), offset, (int)(groupptr - codestart));      else PUT(((pcre_uchar *)codestart), offset, (int)(groupptr - codestart));
7561    }    }
7562    
7563  /* Give an error if there's back reference to a non-existent capturing  /* Give an error if there's back reference to a non-existent capturing
# Line 7129  length, and set their lengths. */ Line 7575  length, and set their lengths. */
7575    
7576  if (cd->check_lookbehind)  if (cd->check_lookbehind)
7577    {    {
7578    uschar *cc = (uschar *)codestart;    pcre_uchar *cc = (pcre_uchar *)codestart;
7579    
7580    /* Loop, searching for OP_REVERSE items, and process those that do not have    /* Loop, searching for OP_REVERSE items, and process those that do not have
7581    their length set. (Actually, it will also re-process any that have a length    their length set. (Actually, it will also re-process any that have a length
7582    of zero, but that is a pathological case, and it does no harm.) When we find    of zero, but that is a pathological case, and it does no harm.) When we find
7583    one, we temporarily terminate the branch it is in while we scan it. */    one, we temporarily terminate the branch it is in while we scan it. */
7584    
7585    for (cc = (uschar *)_pcre_find_bracket(codestart, utf8, -1);    for (cc = (pcre_uchar *)_pcre_find_bracket(codestart, utf8, -1);
7586         cc != NULL;         cc != NULL;
7587         cc = (uschar *)_pcre_find_bracket(cc, utf8, -1))         cc = (pcre_uchar *)_pcre_find_bracket(cc, utf8, -1))
7588      {      {
7589      if (GET(cc, 1) == 0)      if (GET(cc, 1) == 0)
7590        {        {
7591        int fixed_length;        int fixed_length;
7592        uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);        pcre_uchar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);
7593        int end_op = *be;        int end_op = *be;
7594        *be = OP_END;        *be = OP_END;
7595        fixed_length = find_fixedlength(cc, re->options, TRUE, cd);        fixed_length = find_fixedlength(cc, (re->options & PCRE_UTF8) != 0, TRUE,
7596            cd);
7597        *be = end_op;        *be = end_op;
7598        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
7599        if (fixed_length < 0)        if (fixed_length < 0)
7600          {          {
7601          errorcode = (fixed_length == -2)? ERR36 : ERR25;          errorcode = (fixed_length == -2)? ERR36 :
7602                        (fixed_length == -4)? ERR70 : ERR25;
7603          break;          break;
7604          }          }
7605        PUT(cc, 1, fixed_length);        PUT(cc, 1, fixed_length);
# Line 7166  if (errorcode != 0) Line 7614  if (errorcode != 0)
7614    {    {
7615    (pcre_free)(re);    (pcre_free)(re);
7616    PCRE_EARLY_ERROR_RETURN:    PCRE_EARLY_ERROR_RETURN:
7617    *erroroffset = (int)(ptr - (const uschar *)pattern);    *erroroffset = (int)(ptr - (const pcre_uchar *)pattern);
7618    PCRE_EARLY_ERROR_RETURN2:    PCRE_EARLY_ERROR_RETURN2:
7619    *errorptr = find_error_text(errorcode);    *errorptr = find_error_text(errorcode);
7620    if (errorcodeptr != NULL) *errorcodeptr = errorcode;    if (errorcodeptr != NULL) *errorcodeptr = errorcode;
# Line 7185  start with ^. and also when all branches Line 7633  start with ^. and also when all branches
7633    
7634  if ((re->options & PCRE_ANCHORED) == 0)  if ((re->options & PCRE_ANCHORED) == 0)
7635    {    {
7636    int temp_options = re->options;   /* May get changed during these scans */    if (is_anchored(codestart, 0, cd->backref_map))
   if (is_anchored(codestart, &temp_options, 0, cd->backref_map))  
7637      re->options |= PCRE_ANCHORED;      re->options |= PCRE_ANCHORED;
7638    else    else
7639      {      {
7640      if (firstbyte < 0)      if (firstbyte < 0)
7641        firstbyte = find_firstassertedchar(codestart, &temp_options, FALSE);        firstbyte = find_firstassertedchar(codestart, FALSE);
7642      if (firstbyte >= 0)   /* Remove caseless flag for non-caseable chars */      if (firstbyte >= 0)   /* Remove caseless flag for non-caseable chars */
7643        {        {
7644        int ch = firstbyte & 255;        int ch = firstbyte & 255;
# Line 7253  if (code - codestart > length) Line 7700  if (code - codestart > length)
7700    {    {
7701    (pcre_free)(re);    (pcre_free)(re);
7702    *errorptr = find_error_text(ERR23);    *errorptr = find_error_text(ERR23);
7703    *erroroffset = ptr - (uschar *)pattern;    *erroroffset = ptr - (pcre_uchar *)pattern;
7704    if (errorcodeptr != NULL) *errorcodeptr = ERR23;    if (errorcodeptr != NULL) *errorcodeptr = ERR23;
7705    return NULL;    return NULL;
7706    }    }

Legend:
Removed from v.574  
changed lines
  Added in v.756

  ViewVC Help
Powered by ViewVC 1.1.5