/[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 769 by zherczeg, Sun Nov 27 17:40:33 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 string_PNd[]  = {
270    (uschar *)"\\P{Nd}",    /* \D */    CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
271    (uschar *)"\\p{Nd}",    /* \d */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
272    (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */  static const pcre_uchar string_pNd[]  = {
273    (uschar *)"\\p{Xsp}",   /* \s */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
274    (uschar *)"\\P{Xwd}",   /* \W */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
275    (uschar *)"\\p{Xwd}"    /* \w */  static const pcre_uchar string_PXsp[] = {
276      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
277      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
278    static const pcre_uchar string_pXsp[] = {
279      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
280      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
281    static const pcre_uchar string_PXwd[] = {
282      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
283      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
284    static const pcre_uchar string_pXwd[] = {
285      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
286      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
287    
288    static const pcre_uchar *substitutes[] = {
289      string_PNd,           /* \D */
290      string_pNd,           /* \d */
291      string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
292      string_pXsp,          /* \s */
293      string_PXwd,          /* \W */
294      string_pXwd           /* \w */
295  };  };
296    
297  static const uschar *posix_substitutes[] = {  static const pcre_uchar string_pL[] =   {
298    (uschar *)"\\p{L}",     /* alpha */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
299    (uschar *)"\\p{Ll}",    /* lower */    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
300    (uschar *)"\\p{Lu}",    /* upper */  static const pcre_uchar string_pLl[] =  {
301    (uschar *)"\\p{Xan}",   /* alnum */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
302    NULL,                   /* ascii */    CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
303    (uschar *)"\\h",        /* blank */  static const pcre_uchar string_pLu[] =  {
304    NULL,                   /* cntrl */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
305    (uschar *)"\\p{Nd}",    /* digit */    CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
306    NULL,                   /* graph */  static const pcre_uchar string_pXan[] = {
307    NULL,                   /* print */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
308    NULL,                   /* punct */    CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
309    (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */  static const pcre_uchar string_h[] =    {
310    (uschar *)"\\p{Xwd}",   /* word */    CHAR_BACKSLASH, CHAR_h, '\0' };
311    NULL,                   /* xdigit */  static const pcre_uchar string_pXps[] = {
312      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
313      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
314    static const pcre_uchar string_PL[] =   {
315      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
316      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
317    static const pcre_uchar string_PLl[] =  {
318      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
319      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
320    static const pcre_uchar string_PLu[] =  {
321      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
322      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
323    static const pcre_uchar string_PXan[] = {
324      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
325      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
326    static const pcre_uchar string_H[] =    {
327      CHAR_BACKSLASH, CHAR_H, '\0' };
328    static const pcre_uchar string_PXps[] = {
329      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
330      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
331    
332    static const pcre_uchar *posix_substitutes[] = {
333      string_pL,            /* alpha */
334      string_pLl,           /* lower */
335      string_pLu,           /* upper */
336      string_pXan,          /* alnum */
337      NULL,                 /* ascii */
338      string_h,             /* blank */
339      NULL,                 /* cntrl */
340      string_pNd,           /* digit */
341      NULL,                 /* graph */
342      NULL,                 /* print */
343      NULL,                 /* punct */
344      string_pXps,          /* space */    /* NOTE: Xps is POSIX space */
345      string_pXwd,          /* word */
346      NULL,                 /* xdigit */
347    /* Negated cases */    /* Negated cases */
348    (uschar *)"\\P{L}",     /* ^alpha */    string_PL,            /* ^alpha */
349    (uschar *)"\\P{Ll}",    /* ^lower */    string_PLl,           /* ^lower */
350    (uschar *)"\\P{Lu}",    /* ^upper */    string_PLu,           /* ^upper */
351    (uschar *)"\\P{Xan}",   /* ^alnum */    string_PXan,          /* ^alnum */
352    NULL,                   /* ^ascii */    NULL,                 /* ^ascii */
353    (uschar *)"\\H",        /* ^blank */    string_H,             /* ^blank */
354    NULL,                   /* ^cntrl */    NULL,                 /* ^cntrl */
355    (uschar *)"\\P{Nd}",    /* ^digit */    string_PNd,           /* ^digit */
356    NULL,                   /* ^graph */    NULL,                 /* ^graph */
357    NULL,                   /* ^print */    NULL,                 /* ^print */
358    NULL,                   /* ^punct */    NULL,                 /* ^punct */
359    (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */    string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
360    (uschar *)"\\P{Xwd}",   /* ^word */    string_PXwd,          /* ^word */
361    NULL                    /* ^xdigit */    NULL                  /* ^xdigit */
362  };  };
363  #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
364  #endif  #endif
365    
366  #define STRING(a)  # a  #define STRING(a)  # a
# Line 393  static const char error_texts[] = Line 447  static const char error_texts[] =
447    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
448    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
449    /* 55 */    /* 55 */
450    "repeating a DEFINE group is not allowed\0"    "repeating a DEFINE group is not allowed\0"  /** DEAD **/
451    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
452    "\\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"
453    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
# Line 408  static const char error_texts[] = Line 462  static const char error_texts[] =
462    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
463    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
464    "this version of PCRE is not compiled with PCRE_UCP support\0"    "this version of PCRE is not compiled with PCRE_UCP support\0"
465    "\\c must be followed by an ASCII character\0"    "\\c must be followed by an ASCII character\0"
466      "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
467      /* 70 */
468      "internal error: unknown opcode in find_fixedlength()\0"
469    ;    ;
470    
471  /* 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 602  static const unsigned char ebcdic_charta
602  /* Definition to allow mutual recursion */  /* Definition to allow mutual recursion */
603    
604  static BOOL  static BOOL
605    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,    compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
606      int *, int *, branch_chain *, compile_data *, int *);      int *, int *, branch_chain *, compile_data *, int *);
607    
608    
# Line 577  return s; Line 634  return s;
634    
635    
636  /*************************************************  /*************************************************
637    *            Check for counted repeat            *
638    *************************************************/
639    
640    /* This function is called when a '{' is encountered in a place where it might
641    start a quantifier. It looks ahead to see if it really is a quantifier or not.
642    It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
643    where the ddds are digits.
644    
645    Arguments:
646      p         pointer to the first char after '{'
647    
648    Returns:    TRUE or FALSE
649    */
650    
651    static BOOL
652    is_counted_repeat(const pcre_uchar *p)
653    {
654    if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
655    while ((digitab[*p] & ctype_digit) != 0) p++;
656    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
657    
658    if (*p++ != CHAR_COMMA) return FALSE;
659    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
660    
661    if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
662    while ((digitab[*p] & ctype_digit) != 0) p++;
663    
664    return (*p == CHAR_RIGHT_CURLY_BRACKET);
665    }
666    
667    
668    
669    /*************************************************
670  *            Handle escapes                      *  *            Handle escapes                      *
671  *************************************************/  *************************************************/
672    
# Line 601  Returns:         zero or positive => a d Line 691  Returns:         zero or positive => a d
691  */  */
692    
693  static int  static int
694  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,
695    int options, BOOL isclass)    int options, BOOL isclass)
696  {  {
697  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
698  const uschar *ptr = *ptrptr + 1;  const pcre_uchar *ptr = *ptrptr + 1;
699  int c, i;  int c, i;
700    
701  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 722  else if ((i = escapes[c - 0x48]) != 0)
722    
723  else  else
724    {    {
725    const uschar *oldptr;    const pcre_uchar *oldptr;
726    BOOL braced, negated;    BOOL braced, negated;
727    
728    switch (c)    switch (c)
# Line 642  else Line 732  else
732    
733      case CHAR_l:      case CHAR_l:
734      case CHAR_L:      case CHAR_L:
735        *errorcodeptr = ERR37;
736        break;
737    
738      case CHAR_u:      case CHAR_u:
739        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
740          {
741          /* In JavaScript, \u must be followed by four hexadecimal numbers.
742          Otherwise it is a lowercase u letter. */
743          if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0
744               && (digitab[ptr[3]] & ctype_xdigit) != 0 && (digitab[ptr[4]] & ctype_xdigit) != 0)
745            {
746            c = 0;
747            for (i = 0; i < 4; ++i)
748              {
749              register int cc = *(++ptr);
750    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
751              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
752              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
753    #else           /* EBCDIC coding */
754              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
755              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
756    #endif
757              }
758            }
759          }
760        else
761          *errorcodeptr = ERR37;
762        break;
763    
764      case CHAR_U:      case CHAR_U:
765      *errorcodeptr = ERR37;      /* In JavaScript, \U is an uppercase U letter. */
766        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
767      break;      break;
768    
769      /* \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
770        class, \g must be followed by one of a number of specific things:
771    
772      (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
773      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 784  else
784      the -ESC_g code (cf \k). */      the -ESC_g code (cf \k). */
785    
786      case CHAR_g:      case CHAR_g:
787        if (isclass) break;
788      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
789        {        {
790        c = -ESC_g;        c = -ESC_g;
# Line 674  else Line 795  else
795    
796      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
797        {        {
798        const uschar *p;        const pcre_uchar *p;
799        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
800          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;
801        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
# Line 792  else Line 913  else
913      treated as a data character. */      treated as a data character. */
914    
915      case CHAR_x:      case CHAR_x:
916        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
917          {
918          /* In JavaScript, \x must be followed by two hexadecimal numbers.
919          Otherwise it is a lowercase x letter. */
920          if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0)
921            {
922            c = 0;
923            for (i = 0; i < 2; ++i)
924              {
925              register int cc = *(++ptr);
926    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
927              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
928              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
929    #else           /* EBCDIC coding */
930              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
931              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
932    #endif
933              }
934            }
935          break;
936          }
937    
938      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
939        {        {
940        const uschar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
941        int count = 0;        int count = 0;
942    
943        c = 0;        c = 0;
# Line 857  else Line 1000  else
1000      if (c > 127)  /* Excludes all non-ASCII in either mode */      if (c > 127)  /* Excludes all non-ASCII in either mode */
1001        {        {
1002        *errorcodeptr = ERR68;        *errorcodeptr = ERR68;
1003        break;        break;
1004        }        }
1005      if (c >= CHAR_a && c <= CHAR_z) c -= 32;      if (c >= CHAR_a && c <= CHAR_z) c -= 32;
1006      c ^= 0x40;      c ^= 0x40;
1007  #else             /* EBCDIC coding */  #else             /* EBCDIC coding */
# Line 885  else Line 1028  else
1028    }    }
1029    
1030  /* 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
1031  newline". PCRE does not support \N{name}. */  newline". PCRE does not support \N{name}. However, it does support
1032    quantification such as \N{2,3}. */
1033    
1034  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET)  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1035         !is_counted_repeat(ptr+2))
1036    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
1037    
1038  /* 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 1068  Returns:         type value from ucp_typ
1068  */  */
1069    
1070  static int  static int
1071  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
1072  {  {
1073  int c, i, bot, top;  int c, i, bot, top;
1074  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1075  char name[32];  pcre_uchar name[32];
1076    
1077  c = *(++ptr);  c = *(++ptr);
1078  if (c == 0) goto ERROR_RETURN;  if (c == 0) goto ERROR_RETURN;
# Line 968  else Line 1113  else
1113  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1114    
1115  bot = 0;  bot = 0;
1116  top = _pcre_utt_size;  top = PRIV(utt_size);
1117    
1118  while (bot < top)  while (bot < top)
1119    {    {
1120    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1121    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    c = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1122    if (c == 0)    if (c == 0)
1123      {      {
1124      *dptr = _pcre_utt[i].value;      *dptr = PRIV(utt)[i].value;
1125      return _pcre_utt[i].type;      return PRIV(utt)[i].type;
1126      }      }
1127    if (c > 0) bot = i + 1; else top = i;    if (c > 0) bot = i + 1; else top = i;
1128    }    }
# Line 997  return -1; Line 1142  return -1;
1142    
1143    
1144  /*************************************************  /*************************************************
 *            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);  
 }  
   
   
   
 /*************************************************  
1145  *         Read repeat counts                     *  *         Read repeat counts                     *
1146  *************************************************/  *************************************************/
1147    
# Line 1048  Returns:         pointer to '}' on succe Line 1160  Returns:         pointer to '}' on succe
1160                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1161  */  */
1162    
1163  static const uschar *  static const pcre_uchar *
1164  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)
1165  {  {
1166  int min = 0;  int min = 0;
1167  int max = -1;  int max = -1;
# Line 1105  top-level call starts at the beginning o Line 1217  top-level call starts at the beginning o
1217  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
1218  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
1219  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
1220  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
1221  encountered, the name will be terminated by '>' because that is checked in the  track of subpatterns that reset the capturing group numbers - the (?| feature.
1222  first pass. Recursion is used to keep track of subpatterns that reset the  
1223  capturing group numbers - the (?| feature.  This function was originally called only from the second pass, in which we know
1224    that if (?< or (?' or (?P< is encountered, the name will be correctly
1225    terminated because that is checked in the first pass. There is now one call to
1226    this function in the first pass, to check for a recursive back reference by
1227    name (so that we can make the whole group atomic). In this case, we need check
1228    only up to the current position in the pattern, and that is still OK because
1229    and previous occurrences will have been checked. To make this work, the test
1230    for "end of pattern" is a check against cd->end_pattern in the main loop,
1231    instead of looking for a binary zero. This means that the special first-pass
1232    call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1233    processing items within the loop are OK, because afterwards the main loop will
1234    terminate.)
1235    
1236  Arguments:  Arguments:
1237    ptrptr       address of the current character pointer (updated)    ptrptr       address of the current character pointer (updated)
# Line 1116  Arguments: Line 1239  Arguments:
1239    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1240    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1241    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1242    utf8         TRUE if we are in UTF-8 mode    utf8         TRUE if we are in UTF-8 mode
1243    count        pointer to the current capturing subpattern number (updated)    count        pointer to the current capturing subpattern number (updated)
1244    
1245  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
1246  */  */
1247    
1248  static int  static int
1249  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,
1250    BOOL xmode, BOOL utf8, int *count)    BOOL xmode, BOOL utf8, int *count)
1251  {  {
1252  uschar *ptr = *ptrptr;  pcre_uchar *ptr = *ptrptr;
1253  int start_count = *count;  int start_count = *count;
1254  int hwm_count = start_count;  int hwm_count = start_count;
1255  BOOL dup_parens = FALSE;  BOOL dup_parens = FALSE;
# Line 1193  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1316  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1316          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1317        {        {
1318        int term;        int term;
1319        const uschar *thisname;        const pcre_uchar *thisname;
1320        *count += 1;        *count += 1;
1321        if (name == NULL && *count == lorn) return *count;        if (name == NULL && *count == lorn) return *count;
1322        term = *ptr++;        term = *ptr++;
# Line 1201  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1324  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1324        thisname = ptr;        thisname = ptr;
1325        while (*ptr != term) ptr++;        while (*ptr != term) ptr++;
1326        if (name != NULL && lorn == ptr - thisname &&        if (name != NULL && lorn == ptr - thisname &&
1327            strncmp((const char *)name, (const char *)thisname, lorn) == 0)            STRNCMP_UC_UC(name, thisname, lorn) == 0)
1328          return *count;          return *count;
1329        term++;        term++;
1330        }        }
# Line 1209  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1332  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1332    }    }
1333    
1334  /* Past any initial parenthesis handling, scan for parentheses or vertical  /* Past any initial parenthesis handling, scan for parentheses or vertical
1335  bars. */  bars. Stop if we get to cd->end_pattern. Note that this is important for the
1336    first-pass call when this value is temporarily adjusted to stop at the current
1337    position. So DO NOT change this to a test for binary zero. */
1338    
1339  for (; *ptr != 0; ptr++)  for (; ptr < cd->end_pattern; ptr++)
1340    {    {
1341    /* Skip over backslashed characters and also entire \Q...\E */    /* Skip over backslashed characters and also entire \Q...\E */
1342    
# Line 1242  for (; *ptr != 0; ptr++) Line 1367  for (; *ptr != 0; ptr++)
1367          {          {
1368          if (ptr[2] == CHAR_E)          if (ptr[2] == CHAR_E)
1369            ptr+= 2;            ptr+= 2;
1370          else if (strncmp((const char *)ptr+2,          else if (STRNCMP_UC_C8(ptr + 2,
1371                   STR_Q STR_BACKSLASH STR_E, 3) == 0)                   STR_Q STR_BACKSLASH STR_E, 3) == 0)
1372            ptr += 4;            ptr += 4;
1373          else          else
# Line 1285  for (; *ptr != 0; ptr++) Line 1410  for (; *ptr != 0; ptr++)
1410    
1411    if (xmode && *ptr == CHAR_NUMBER_SIGN)    if (xmode && *ptr == CHAR_NUMBER_SIGN)
1412      {      {
1413      ptr++;      ptr++;
1414      while (*ptr != 0)      while (*ptr != 0)
1415        {        {
1416        if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }        if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1417        ptr++;        ptr++;
1418  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1419        if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;        if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
1420  #endif  #endif
1421        }        }
# Line 1348  Arguments: Line 1473  Arguments:
1473    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1474    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1475    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1476    utf8         TRUE if we are in UTF-8 mode    utf8         TRUE if we are in UTF-8 mode
1477    
1478  Returns:       the number of the found subpattern, or -1 if not found  Returns:       the number of the found subpattern, or -1 if not found
1479  */  */
1480    
1481  static int  static int
1482  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,
1483    BOOL utf8)    BOOL utf8)
1484  {  {
1485  uschar *ptr = (uschar *)cd->start_pattern;  pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1486  int count = 0;  int count = 0;
1487  int rc;  int rc;
1488    
# Line 1384  return rc; Line 1509  return rc;
1509    
1510  /* This is called by several functions that scan a compiled expression looking  /* This is called by several functions that scan a compiled expression looking
1511  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
1512  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
1513  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
1514  assertions, and also the \b assertion; for others it does not.  does not.
1515    
1516  Arguments:  Arguments:
1517    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  
1518    skipassert   TRUE if certain assertions are to be skipped    skipassert   TRUE if certain assertions are to be skipped
1519    
1520  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1521  */  */
1522    
1523  static const uschar*  static const pcre_uchar*
1524  first_significant_code(const uschar *code, int *options, int optbit,  first_significant_code(const pcre_uchar *code, BOOL skipassert)
   BOOL skipassert)  
1525  {  {
1526  for (;;)  for (;;)
1527    {    {
1528    switch ((int)*code)    switch ((int)*code)
1529      {      {
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
1530      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1531      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1532      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1533      if (!skipassert) return code;      if (!skipassert) return code;
1534      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
1535      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1536      break;      break;
1537    
1538      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
# Line 1431  for (;;) Line 1546  for (;;)
1546      case OP_RREF:      case OP_RREF:
1547      case OP_NRREF:      case OP_NRREF:
1548      case OP_DEF:      case OP_DEF:
1549      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1550      break;      break;
1551    
1552      default:      default:
# Line 1461  and doing the check at the end; a flag s Line 1576  and doing the check at the end; a flag s
1576    
1577  Arguments:  Arguments:
1578    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1579    options  the compiling options    utf8     TRUE in UTF-8 mode
1580    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1581    cd       the "compile data" structure    cd       the "compile data" structure
1582    
1583  Returns:   the fixed length,  Returns:   the fixed length,
1584               or -1 if there is no fixed length,               or -1 if there is no fixed length,
1585               or -2 if \C was encountered               or -2 if \C was encountered (in UTF-8 mode only)
1586               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
1587                 or -4 if an unknown opcode was encountered (internal error)
1588  */  */
1589    
1590  static int  static int
1591  find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)  find_fixedlength(pcre_uchar *code, BOOL utf8, BOOL atend, compile_data *cd)
1592  {  {
1593  int length = -1;  int length = -1;
1594    
1595  register int branchlength = 0;  register int branchlength = 0;
1596  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1597    
1598  /* 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
1599  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 1601  branch, check the length against that of
1601  for (;;)  for (;;)
1602    {    {
1603    int d;    int d;
1604    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1605    register int op = *cc;    register int op = *cc;
1606    switch (op)    switch (op)
1607      {      {
1608        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1609        OP_BRA (normal non-capturing bracket) because the other variants of these
1610        opcodes are all concerned with unlimited repeated groups, which of course
1611        are not of fixed length. */
1612    
1613      case OP_CBRA:      case OP_CBRA:
1614      case OP_BRA:      case OP_BRA:
1615      case OP_ONCE:      case OP_ONCE:
1616        case OP_ONCE_NC:
1617      case OP_COND:      case OP_COND:
1618      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf8, atend, cd);
1619      if (d < 0) return d;      if (d < 0) return d;
1620      branchlength += d;      branchlength += d;
1621      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1622      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1623      break;      break;
1624    
1625      /* 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.
1626      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
1627      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
1628        the same code. Note that we must not include the OP_KETRxxx opcodes here,
1629        because they all imply an unlimited repeat. */
1630    
1631      case OP_ALT:      case OP_ALT:
1632      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1633      case OP_END:      case OP_END:
1634        case OP_ACCEPT:
1635        case OP_ASSERT_ACCEPT:
1636      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1637        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
1638      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
# Line 1522  for (;;) Line 1646  for (;;)
1646    
1647      case OP_RECURSE:      case OP_RECURSE:
1648      if (!atend) return -3;      if (!atend) return -3;
1649      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1650      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1651      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1652      d = find_fixedlength(cs + 2, options, atend, cd);      d = find_fixedlength(cs + 2, utf8, atend, cd);
1653      if (d < 0) return d;      if (d < 0) return d;
1654      branchlength += d;      branchlength += d;
1655      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1542  for (;;) Line 1666  for (;;)
1666    
1667      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1668    
1669      case OP_REVERSE:      case OP_MARK:
1670        case OP_PRUNE_ARG:
1671        case OP_SKIP_ARG:
1672        case OP_THEN_ARG:
1673        cc += cc[1] + PRIV(OP_lengths)[*cc];
1674        break;
1675    
1676        case OP_CALLOUT:
1677        case OP_CIRC:
1678        case OP_CIRCM:
1679        case OP_CLOSE:
1680        case OP_COMMIT:
1681      case OP_CREF:      case OP_CREF:
     case OP_NCREF:  
     case OP_RREF:  
     case OP_NRREF:  
1682      case OP_DEF:      case OP_DEF:
1683      case OP_OPT:      case OP_DOLL:
1684      case OP_CALLOUT:      case OP_DOLLM:
     case OP_SOD:  
     case OP_SOM:  
     case OP_SET_SOM:  
1685      case OP_EOD:      case OP_EOD:
1686      case OP_EODN:      case OP_EODN:
1687      case OP_CIRC:      case OP_FAIL:
1688      case OP_DOLL:      case OP_NCREF:
1689        case OP_NRREF:
1690      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1691        case OP_PRUNE:
1692        case OP_REVERSE:
1693        case OP_RREF:
1694        case OP_SET_SOM:
1695        case OP_SKIP:
1696        case OP_SOD:
1697        case OP_SOM:
1698        case OP_THEN:
1699      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1700      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1701      break;      break;
1702    
1703      /* Handle literal characters */      /* Handle literal characters */
1704    
1705      case OP_CHAR:      case OP_CHAR:
1706      case OP_CHARNC:      case OP_CHARI:
1707      case OP_NOT:      case OP_NOT:
1708        case OP_NOTI:
1709      branchlength++;      branchlength++;
1710      cc += 2;      cc += 2;
1711  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1712      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += PRIV(utf8_table4)[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1713  #endif  #endif
1714      break;      break;
1715    
# Line 1579  for (;;) Line 1717  for (;;)
1717      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
1718    
1719      case OP_EXACT:      case OP_EXACT:
1720        case OP_EXACTI:
1721        case OP_NOTEXACT:
1722        case OP_NOTEXACTI:
1723      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1724      cc += 4;      cc += 2 + IMM2_SIZE;
1725  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1726      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += PRIV(utf8_table4)[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1727  #endif  #endif
1728      break;      break;
1729    
1730      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1731      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1732      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
1733      cc += 4;      cc += 1 + IMM2_SIZE + 1;
1734      break;      break;
1735    
1736      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1600  for (;;) Line 1740  for (;;)
1740      cc += 2;      cc += 2;
1741      /* Fall through */      /* Fall through */
1742    
1743        case OP_HSPACE:
1744        case OP_VSPACE:
1745        case OP_NOT_HSPACE:
1746        case OP_NOT_VSPACE:
1747      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1748      case OP_DIGIT:      case OP_DIGIT:
1749      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
# Line 1612  for (;;) Line 1756  for (;;)
1756      cc++;      cc++;
1757      break;      break;
1758    
1759      /* The single-byte matcher isn't allowed */      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1760        otherwise \C is coded as OP_ALLANY. */
1761    
1762      case OP_ANYBYTE:      case OP_ANYBYTE:
1763      return -2;      return -2;
# Line 1631  for (;;) Line 1776  for (;;)
1776    
1777      switch (*cc)      switch (*cc)
1778        {        {
1779          case OP_CRPLUS:
1780          case OP_CRMINPLUS:
1781        case OP_CRSTAR:        case OP_CRSTAR:
1782        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1783        case OP_CRQUERY:        case OP_CRQUERY:
# Line 1639  for (;;) Line 1786  for (;;)
1786    
1787        case OP_CRRANGE:        case OP_CRRANGE:
1788        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1789        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1790        branchlength += GET2(cc,1);        branchlength += GET2(cc,1);
1791        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1792        break;        break;
1793    
1794        default:        default:
# Line 1651  for (;;) Line 1798  for (;;)
1798    
1799      /* Anything else is variable length */      /* Anything else is variable length */
1800    
1801      default:      case OP_ANYNL:
1802        case OP_BRAMINZERO:
1803        case OP_BRAPOS:
1804        case OP_BRAPOSZERO:
1805        case OP_BRAZERO:
1806        case OP_CBRAPOS:
1807        case OP_EXTUNI:
1808        case OP_KETRMAX:
1809        case OP_KETRMIN:
1810        case OP_KETRPOS:
1811        case OP_MINPLUS:
1812        case OP_MINPLUSI:
1813        case OP_MINQUERY:
1814        case OP_MINQUERYI:
1815        case OP_MINSTAR:
1816        case OP_MINSTARI:
1817        case OP_MINUPTO:
1818        case OP_MINUPTOI:
1819        case OP_NOTMINPLUS:
1820        case OP_NOTMINPLUSI:
1821        case OP_NOTMINQUERY:
1822        case OP_NOTMINQUERYI:
1823        case OP_NOTMINSTAR:
1824        case OP_NOTMINSTARI:
1825        case OP_NOTMINUPTO:
1826        case OP_NOTMINUPTOI:
1827        case OP_NOTPLUS:
1828        case OP_NOTPLUSI:
1829        case OP_NOTPOSPLUS:
1830        case OP_NOTPOSPLUSI:
1831        case OP_NOTPOSQUERY:
1832        case OP_NOTPOSQUERYI:
1833        case OP_NOTPOSSTAR:
1834        case OP_NOTPOSSTARI:
1835        case OP_NOTPOSUPTO:
1836        case OP_NOTPOSUPTOI:
1837        case OP_NOTQUERY:
1838        case OP_NOTQUERYI:
1839        case OP_NOTSTAR:
1840        case OP_NOTSTARI:
1841        case OP_NOTUPTO:
1842        case OP_NOTUPTOI:
1843        case OP_PLUS:
1844        case OP_PLUSI:
1845        case OP_POSPLUS:
1846        case OP_POSPLUSI:
1847        case OP_POSQUERY:
1848        case OP_POSQUERYI:
1849        case OP_POSSTAR:
1850        case OP_POSSTARI:
1851        case OP_POSUPTO:
1852        case OP_POSUPTOI:
1853        case OP_QUERY:
1854        case OP_QUERYI:
1855        case OP_REF:
1856        case OP_REFI:
1857        case OP_SBRA:
1858        case OP_SBRAPOS:
1859        case OP_SCBRA:
1860        case OP_SCBRAPOS:
1861        case OP_SCOND:
1862        case OP_SKIPZERO:
1863        case OP_STAR:
1864        case OP_STARI:
1865        case OP_TYPEMINPLUS:
1866        case OP_TYPEMINQUERY:
1867        case OP_TYPEMINSTAR:
1868        case OP_TYPEMINUPTO:
1869        case OP_TYPEPLUS:
1870        case OP_TYPEPOSPLUS:
1871        case OP_TYPEPOSQUERY:
1872        case OP_TYPEPOSSTAR:
1873        case OP_TYPEPOSUPTO:
1874        case OP_TYPEQUERY:
1875        case OP_TYPESTAR:
1876        case OP_TYPEUPTO:
1877        case OP_UPTO:
1878        case OP_UPTOI:
1879      return -1;      return -1;
1880    
1881        /* Catch unrecognized opcodes so that when new ones are added they
1882        are not forgotten, as has happened in the past. */
1883    
1884        default:
1885        return -4;
1886      }      }
1887    }    }
1888  /* Control never gets here */  /* Control never gets here */
# Line 1679  Arguments: Line 1909  Arguments:
1909  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
1910  */  */
1911    
1912  const uschar *  const pcre_uchar *
1913  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf8, int number)
1914  {  {
1915  for (;;)  for (;;)
1916    {    {
1917    register int c = *code;    register int c = *code;
1918    
1919    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1920    
1921    /* 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 1928  for (;;)
1928    
1929    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
1930      {      {
1931      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
1932      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1933      }      }
1934    
1935    /* Handle capturing bracket */    /* Handle capturing bracket */
1936    
1937    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1938               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1939      {      {
1940      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
1941      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1942      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1943      }      }
1944    
1945    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
# Line 1735  for (;;) Line 1967  for (;;)
1967        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1968        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1969        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1970        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP
1971            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
1972        break;        break;
1973    
1974        case OP_MARK:        case OP_MARK:
# Line 1745  for (;;) Line 1978  for (;;)
1978        break;        break;
1979    
1980        case OP_THEN_ARG:        case OP_THEN_ARG:
1981        code += code[1+LINK_SIZE];        code += code[1];
1982        break;        break;
1983        }        }
1984    
1985      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
1986    
1987      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1988    
1989    /* In UTF-8 mode, opcodes that are followed by a character may be followed by    /* In UTF-8 mode, opcodes that are followed by a character may be followed by
1990    a multi-byte character. The length in the table is a minimum, so we have to    a multi-byte character. The length in the table is a minimum, so we have to
# Line 1761  for (;;) Line 1994  for (;;)
1994      if (utf8) switch(c)      if (utf8) switch(c)
1995        {        {
1996        case OP_CHAR:        case OP_CHAR:
1997        case OP_CHARNC:        case OP_CHARI:
1998        case OP_EXACT:        case OP_EXACT:
1999          case OP_EXACTI:
2000        case OP_UPTO:        case OP_UPTO:
2001          case OP_UPTOI:
2002        case OP_MINUPTO:        case OP_MINUPTO:
2003          case OP_MINUPTOI:
2004        case OP_POSUPTO:        case OP_POSUPTO:
2005          case OP_POSUPTOI:
2006        case OP_STAR:        case OP_STAR:
2007          case OP_STARI:
2008        case OP_MINSTAR:        case OP_MINSTAR:
2009          case OP_MINSTARI:
2010        case OP_POSSTAR:        case OP_POSSTAR:
2011          case OP_POSSTARI:
2012        case OP_PLUS:        case OP_PLUS:
2013          case OP_PLUSI:
2014        case OP_MINPLUS:        case OP_MINPLUS:
2015          case OP_MINPLUSI:
2016        case OP_POSPLUS:        case OP_POSPLUS:
2017          case OP_POSPLUSI:
2018        case OP_QUERY:        case OP_QUERY:
2019          case OP_QUERYI:
2020        case OP_MINQUERY:        case OP_MINQUERY:
2021          case OP_MINQUERYI:
2022        case OP_POSQUERY:        case OP_POSQUERY:
2023        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2024          if (code[-1] >= 0xc0) code += PRIV(utf8_table4)[code[-1] & 0x3f];
2025        break;        break;
2026        }        }
2027  #else  #else
# Line 1801  Arguments: Line 2047  Arguments:
2047  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
2048  */  */
2049    
2050  static const uschar *  static const pcre_uchar *
2051  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf8)
2052  {  {
2053  for (;;)  for (;;)
2054    {    {
# Line 1841  for (;;) Line 2087  for (;;)
2087        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2088        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2089        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2090        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP
2091            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2092        break;        break;
2093    
2094        case OP_MARK:        case OP_MARK:
# Line 1851  for (;;) Line 2098  for (;;)
2098        break;        break;
2099    
2100        case OP_THEN_ARG:        case OP_THEN_ARG:
2101        code += code[1+LINK_SIZE];        code += code[1];
2102        break;        break;
2103        }        }
2104    
2105      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2106    
2107      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2108    
2109      /* In UTF-8 mode, opcodes that are followed by a character may be followed      /* In UTF-8 mode, opcodes that are followed by a character may be followed
2110      by a multi-byte character. The length in the table is a minimum, so we have      by a multi-byte character. The length in the table is a minimum, so we have
# Line 1867  for (;;) Line 2114  for (;;)
2114      if (utf8) switch(c)      if (utf8) switch(c)
2115        {        {
2116        case OP_CHAR:        case OP_CHAR:
2117        case OP_CHARNC:        case OP_CHARI:
2118        case OP_EXACT:        case OP_EXACT:
2119          case OP_EXACTI:
2120        case OP_UPTO:        case OP_UPTO:
2121          case OP_UPTOI:
2122        case OP_MINUPTO:        case OP_MINUPTO:
2123          case OP_MINUPTOI:
2124        case OP_POSUPTO:        case OP_POSUPTO:
2125          case OP_POSUPTOI:
2126        case OP_STAR:        case OP_STAR:
2127          case OP_STARI:
2128        case OP_MINSTAR:        case OP_MINSTAR:
2129          case OP_MINSTARI:
2130        case OP_POSSTAR:        case OP_POSSTAR:
2131          case OP_POSSTARI:
2132        case OP_PLUS:        case OP_PLUS:
2133          case OP_PLUSI:
2134        case OP_MINPLUS:        case OP_MINPLUS:
2135          case OP_MINPLUSI:
2136        case OP_POSPLUS:        case OP_POSPLUS:
2137          case OP_POSPLUSI:
2138        case OP_QUERY:        case OP_QUERY:
2139          case OP_QUERYI:
2140        case OP_MINQUERY:        case OP_MINQUERY:
2141          case OP_MINQUERYI:
2142        case OP_POSQUERY:        case OP_POSQUERY:
2143        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2144          if (code[-1] >= 0xc0) code += PRIV(utf8_table4)[code[-1] & 0x3f];
2145        break;        break;
2146        }        }
2147  #else  #else
# Line 1915  Returns:      TRUE if what is matched co Line 2175  Returns:      TRUE if what is matched co
2175  */  */
2176    
2177  static BOOL  static BOOL
2178  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2179    compile_data *cd)    BOOL utf8, compile_data *cd)
2180  {  {
2181  register int c;  register int c;
2182  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2183       code < endcode;       code < endcode;
2184       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2185    {    {
2186    const uschar *ccode;    const pcre_uchar *ccode;
2187    
2188    c = *code;    c = *code;
2189    
# Line 1937  for (code = first_significant_code(code Line 2197  for (code = first_significant_code(code
2197      continue;      continue;
2198      }      }
2199    
   /* 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;  
     }  
   
2200    /* For a recursion/subroutine call, if its end has been reached, which    /* For a recursion/subroutine call, if its end has been reached, which
2201    implies a subroutine call, we can scan it. */    implies a backward reference subroutine call, we can scan it. If it's a
2202      forward reference subroutine call, we can't. To detect forward reference
2203      we have to scan up the list that is kept in the workspace. This function is
2204      called only when doing the real compile, not during the pre-compile that
2205      measures the size of the compiled pattern. */
2206    
2207    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2208      {      {
2209      BOOL empty_branch = FALSE;      const pcre_uchar *scode;
2210      const uschar *scode = cd->start_code + GET(code, 1);      BOOL empty_branch;
2211    
2212        /* Test for forward reference */
2213    
2214        for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
2215          if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;
2216    
2217        /* Not a forward reference, test for completed backward reference */
2218    
2219        empty_branch = FALSE;
2220        scode = cd->start_code + GET(code, 1);
2221      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2222    
2223        /* Completed backwards reference */
2224    
2225      do      do
2226        {        {
2227        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 2232  for (code = first_significant_code(code
2232        scode += GET(scode, 1);        scode += GET(scode, 1);
2233        }        }
2234      while (*scode == OP_ALT);      while (*scode == OP_ALT);
2235    
2236      if (!empty_branch) return FALSE;  /* All branches are non-empty */      if (!empty_branch) return FALSE;  /* All branches are non-empty */
2237      continue;      continue;
2238      }      }
2239    
2240      /* Groups with zero repeats can of course be empty; skip them. */
2241    
2242      if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2243          c == OP_BRAPOSZERO)
2244        {
2245        code += PRIV(OP_lengths)[c];
2246        do code += GET(code, 1); while (*code == OP_ALT);
2247        c = *code;
2248        continue;
2249        }
2250    
2251      /* A nested group that is already marked as "could be empty" can just be
2252      skipped. */
2253    
2254      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2255          c == OP_SCBRA || c == OP_SCBRAPOS)
2256        {
2257        do code += GET(code, 1); while (*code == OP_ALT);
2258        c = *code;
2259        continue;
2260        }
2261    
2262    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
2263    
2264    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA  || c == OP_BRAPOS ||
2265          c == OP_CBRA || c == OP_CBRAPOS ||
2266          c == OP_ONCE || c == OP_ONCE_NC ||
2267          c == OP_COND)
2268      {      {
2269      BOOL empty_branch;      BOOL empty_branch;
2270      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 2005  for (code = first_significant_code(code Line 2298  for (code = first_significant_code(code
2298      {      {
2299      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2300      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2301      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2302      actual length is stored in the compiled code, so we must update "code"      actual length is stored in the compiled code, so we must update "code"
2303      here. */      here. */
2304    
# Line 2058  for (code = first_significant_code(code Line 2351  for (code = first_significant_code(code
2351      case OP_ALLANY:      case OP_ALLANY:
2352      case OP_ANYBYTE:      case OP_ANYBYTE:
2353      case OP_CHAR:      case OP_CHAR:
2354      case OP_CHARNC:      case OP_CHARI:
2355      case OP_NOT:      case OP_NOT:
2356        case OP_NOTI:
2357      case OP_PLUS:      case OP_PLUS:
2358      case OP_MINPLUS:      case OP_MINPLUS:
2359      case OP_POSPLUS:      case OP_POSPLUS:
# Line 2091  for (code = first_significant_code(code Line 2385  for (code = first_significant_code(code
2385      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2386      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2387      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2388      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP
2389          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2390      break;      break;
2391    
2392      /* End of branch */      /* End of branch */
# Line 2099  for (code = first_significant_code(code Line 2394  for (code = first_significant_code(code
2394      case OP_KET:      case OP_KET:
2395      case OP_KETRMAX:      case OP_KETRMAX:
2396      case OP_KETRMIN:      case OP_KETRMIN:
2397        case OP_KETRPOS:
2398      case OP_ALT:      case OP_ALT:
2399      return TRUE;      return TRUE;
2400    
# Line 2107  for (code = first_significant_code(code Line 2403  for (code = first_significant_code(code
2403    
2404  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2405      case OP_STAR:      case OP_STAR:
2406        case OP_STARI:
2407      case OP_MINSTAR:      case OP_MINSTAR:
2408        case OP_MINSTARI:
2409      case OP_POSSTAR:      case OP_POSSTAR:
2410        case OP_POSSTARI:
2411      case OP_QUERY:      case OP_QUERY:
2412        case OP_QUERYI:
2413      case OP_MINQUERY:      case OP_MINQUERY:
2414        case OP_MINQUERYI:
2415      case OP_POSQUERY:      case OP_POSQUERY:
2416      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      case OP_POSQUERYI:
2417        if (utf8 && code[1] >= 0xc0) code += PRIV(utf8_table4)[code[1] & 0x3f];
2418      break;      break;
2419    
2420      case OP_UPTO:      case OP_UPTO:
2421        case OP_UPTOI:
2422      case OP_MINUPTO:      case OP_MINUPTO:
2423        case OP_MINUPTOI:
2424      case OP_POSUPTO:      case OP_POSUPTO:
2425      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      case OP_POSUPTOI:
2426        if (utf8 && code[1 + IMM2_SIZE] >= 0xc0) code += PRIV(utf8_table4)[code[1 + IMM2_SIZE] & 0x3f];
2427      break;      break;
2428  #endif  #endif
2429    
# Line 2132  for (code = first_significant_code(code Line 2437  for (code = first_significant_code(code
2437      break;      break;
2438    
2439      case OP_THEN_ARG:      case OP_THEN_ARG:
2440      code += code[1+LINK_SIZE];      code += code[1];
2441      break;      break;
2442    
2443      /* 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 2460  return TRUE;
2460  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
2461  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,
2462  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.
2463    This function is called only during the real compile, not during the
2464    pre-compile.
2465    
2466  Arguments:  Arguments:
2467    code        points to start of the recursion    code        points to start of the recursion
# Line 2167  Returns:      TRUE if what is matched co Line 2474  Returns:      TRUE if what is matched co
2474  */  */
2475    
2476  static BOOL  static BOOL
2477  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2478    BOOL utf8, compile_data *cd)    branch_chain *bcptr, BOOL utf8, compile_data *cd)
2479  {  {
2480  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2481    {    {
# Line 2205  where Perl recognizes it as the POSIX cl Line 2512  where Perl recognizes it as the POSIX cl
2512  "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,
2513  I think.  I think.
2514    
2515    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2516    It seems that the appearance of a nested POSIX class supersedes an apparent
2517    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2518    a digit.
2519    
2520    In Perl, unescaped square brackets may also appear as part of class names. For
2521    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2522    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2523    seem right at all. PCRE does not allow closing square brackets in POSIX class
2524    names.
2525    
2526  Arguments:  Arguments:
2527    ptr      pointer to the initial [    ptr      pointer to the initial [
2528    endptr   where to return the end pointer    endptr   where to return the end pointer
# Line 2213  Returns:   TRUE or FALSE Line 2531  Returns:   TRUE or FALSE
2531  */  */
2532    
2533  static BOOL  static BOOL
2534  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2535  {  {
2536  int terminator;          /* Don't combine these lines; the Solaris cc */  int terminator;          /* Don't combine these lines; the Solaris cc */
2537  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
2538  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != 0; ptr++)
2539    {    {
2540    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) ptr++; else    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2541        ptr++;
2542      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
2543      else
2544      {      {
     if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;  
2545      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2546        {        {
2547        *endptr = ptr;        *endptr = ptr;
2548        return TRUE;        return TRUE;
2549        }        }
2550        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
2551             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
2552              ptr[1] == CHAR_EQUALS_SIGN) &&
2553            check_posix_syntax(ptr, endptr))
2554          return FALSE;
2555      }      }
2556    }    }
2557  return FALSE;  return FALSE;
# Line 2250  Returns:     a value representing the na Line 2575  Returns:     a value representing the na
2575  */  */
2576    
2577  static int  static int
2578  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
2579  {  {
2580  const char *pn = posix_names;  const char *pn = posix_names;
2581  register int yield = 0;  register int yield = 0;
2582  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
2583    {    {
2584    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
2585      strncmp((const char *)ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;
2586    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
2587    yield++;    yield++;
2588    }    }
# Line 2297  Returns:     nothing Line 2622  Returns:     nothing
2622  */  */
2623    
2624  static void  static void
2625  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf8, compile_data *cd,
2626    uschar *save_hwm)    pcre_uchar *save_hwm)
2627  {  {
2628  uschar *ptr = group;  pcre_uchar *ptr = group;
2629    
2630  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf8)) != NULL)
2631    {    {
2632    int offset;    int offset;
2633    uschar *hc;    pcre_uchar *hc;
2634    
2635    /* 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
2636    reference. */    reference. */
# Line 2350  Arguments: Line 2675  Arguments:
2675  Returns:         new code pointer  Returns:         new code pointer
2676  */  */
2677    
2678  static uschar *  static pcre_uchar *
2679  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2680  {  {
2681  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2682  *code++ = 255;  *code++ = 255;
2683  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2684  PUT(code, LINK_SIZE, 0);                       /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2685  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
2686  }  }
2687    
2688    
# Line 2379  Returns:             nothing Line 2704  Returns:             nothing
2704  */  */
2705    
2706  static void  static void
2707  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2708  {  {
2709  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2710  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
# Line 2462  switch(ptype) Line 2787  switch(ptype)
2787            prop->chartype == ucp_Lt) == negated;            prop->chartype == ucp_Lt) == negated;
2788    
2789    case PT_GC:    case PT_GC:
2790    return (pdata == _pcre_ucp_gentype[prop->chartype]) == negated;    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2791    
2792    case PT_PC:    case PT_PC:
2793    return (pdata == prop->chartype) == negated;    return (pdata == prop->chartype) == negated;
# Line 2473  switch(ptype) Line 2798  switch(ptype)
2798    /* These are specials */    /* These are specials */
2799    
2800    case PT_ALNUM:    case PT_ALNUM:
2801    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2802            _pcre_ucp_gentype[prop->chartype] == ucp_N) == negated;            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2803    
2804    case PT_SPACE:    /* Perl space */    case PT_SPACE:    /* Perl space */
2805    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2806            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2807            == negated;            == negated;
2808    
2809    case PT_PXSPACE:  /* POSIX space */    case PT_PXSPACE:  /* POSIX space */
2810    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2811            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2812            c == CHAR_FF || c == CHAR_CR)            c == CHAR_FF || c == CHAR_CR)
2813            == negated;            == negated;
2814    
2815    case PT_WORD:    case PT_WORD:
2816    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2817            _pcre_ucp_gentype[prop->chartype] == ucp_N ||            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2818            c == CHAR_UNDERSCORE) == negated;            c == CHAR_UNDERSCORE) == negated;
2819    }    }
2820  return FALSE;  return FALSE;
# Line 2517  Returns:        TRUE if possessifying is Line 2842  Returns:        TRUE if possessifying is
2842  */  */
2843    
2844  static BOOL  static BOOL
2845  check_auto_possessive(const uschar *previous, BOOL utf8, const uschar *ptr,  check_auto_possessive(const pcre_uchar *previous, BOOL utf8,
2846    int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
2847  {  {
2848  int c, next;  int c, next;
2849  int op_code = *previous++;  int op_code = *previous++;
# Line 2532  if ((options & PCRE_EXTENDED) != 0) Line 2857  if ((options & PCRE_EXTENDED) != 0)
2857      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2858      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2859        {        {
2860        ptr++;        ptr++;
2861        while (*ptr != 0)        while (*ptr != 0)
2862          {          {
2863          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2864          ptr++;          ptr++;
2865  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2866          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2867  #endif  #endif
2868          }          }
# Line 2576  if ((options & PCRE_EXTENDED) != 0) Line 2901  if ((options & PCRE_EXTENDED) != 0)
2901      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2902      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2903        {        {
2904        ptr++;        ptr++;
2905        while (*ptr != 0)        while (*ptr != 0)
2906          {          {
2907          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2908          ptr++;          ptr++;
2909  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2910          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2911  #endif  #endif
2912          }          }
# Line 2593  if ((options & PCRE_EXTENDED) != 0) Line 2918  if ((options & PCRE_EXTENDED) != 0)
2918  /* If the next thing is itself optional, we have to give up. */  /* If the next thing is itself optional, we have to give up. */
2919    
2920  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
2921    strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
2922      return FALSE;      return FALSE;
2923    
2924  /* Now compare the next item with the previous opcode. First, handle cases when  /* Now compare the next item with the previous opcode. First, handle cases when
# Line 2609  if (next >= 0) switch(op_code) Line 2934  if (next >= 0) switch(op_code)
2934  #endif  #endif
2935    return c != next;    return c != next;
2936    
2937    /* 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
2938    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
2939    high-valued characters. */    high-valued characters. */
2940    
2941    case OP_CHARNC:    case OP_CHARI:
2942  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2943    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
2944  #else  #else
# Line 2636  if (next >= 0) switch(op_code) Line 2961  if (next >= 0) switch(op_code)
2961  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
2962    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */
2963    
2964    /* 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
2965      opcodes are not used for multi-byte characters, because they are coded using
2966      an XCLASS instead. */
2967    
2968    case OP_NOT:    case OP_NOT:
2969      return (c = *previous) == next;
2970    
2971      case OP_NOTI:
2972    if ((c = *previous) == next) return TRUE;    if ((c = *previous) == next) return TRUE;
   if ((options & PCRE_CASELESS) == 0) return FALSE;  
2973  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2974    if (utf8)    if (utf8)
2975      {      {
# Line 2745  replaced by OP_PROP codes when PCRE_UCP Line 3074  replaced by OP_PROP codes when PCRE_UCP
3074  switch(op_code)  switch(op_code)
3075    {    {
3076    case OP_CHAR:    case OP_CHAR:
3077    case OP_CHARNC:    case OP_CHARI:
3078  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3079    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3080  #else  #else
# Line 2851  switch(op_code) Line 3180  switch(op_code)
3180        to the original \d etc. At this point, ptr will point to a zero byte. */        to the original \d etc. At this point, ptr will point to a zero byte. */
3181    
3182        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3183          strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)          STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3184            return FALSE;            return FALSE;
3185    
3186        /* Do the property check. */        /* Do the property check. */
# Line 2932  Arguments: Line 3261  Arguments:
3261    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3262    reqbyteptr     set to the last literal character required, else < 0    reqbyteptr     set to the last literal character required, else < 0
3263    bcptr          points to current branch chain    bcptr          points to current branch chain
3264      cond_depth     conditional nesting depth
3265    cd             contains pointers to tables etc.    cd             contains pointers to tables etc.
3266    lengthptr      NULL during the real compile phase    lengthptr      NULL during the real compile phase
3267                   points to length accumulator during pre-compile phase                   points to length accumulator during pre-compile phase
# Line 2941  Returns:         TRUE on success Line 3271  Returns:         TRUE on success
3271  */  */
3272    
3273  static BOOL  static BOOL
3274  compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3275    int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,    const pcre_uchar **ptrptr, int *errorcodeptr, int *firstbyteptr,
3276    compile_data *cd, int *lengthptr)    int *reqbyteptr, branch_chain *bcptr, int cond_depth, compile_data *cd,
3277      int *lengthptr)
3278  {  {
3279  int repeat_type, op_type;  int repeat_type, op_type;
3280  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 3283  int greedy_default, greedy_non_default;
3283  int firstbyte, reqbyte;  int firstbyte, reqbyte;
3284  int zeroreqbyte, zerofirstbyte;  int zeroreqbyte, zerofirstbyte;
3285  int req_caseopt, reqvary, tempreqvary;  int req_caseopt, reqvary, tempreqvary;
3286  int options = *optionsptr;  int options = *optionsptr;               /* May change dynamically */
3287  int after_manual_callout = 0;  int after_manual_callout = 0;
3288  int length_prevgroup = 0;  int length_prevgroup = 0;
3289  register int c;  register int c;
3290  register uschar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3291  uschar *last_code = code;  pcre_uchar *last_code = code;
3292  uschar *orig_code = code;  pcre_uchar *orig_code = code;
3293  uschar *tempcode;  pcre_uchar *tempcode;
3294  BOOL inescq = FALSE;  BOOL inescq = FALSE;
3295  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstbyte = FALSE;
3296  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
3297  const uschar *tempptr;  const pcre_uchar *tempptr;
3298  const uschar *nestptr = NULL;  const pcre_uchar *nestptr = NULL;
3299  uschar *previous = NULL;  pcre_uchar *previous = NULL;
3300  uschar *previous_callout = NULL;  pcre_uchar *previous_callout = NULL;
3301  uschar *save_hwm = NULL;  pcre_uchar *save_hwm = NULL;
3302  uschar classbits[32];  pcre_uchar classbits[32];
3303    
3304    /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3305    must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3306    dynamically as we process the pattern. */
3307    
3308  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3309  BOOL class_utf8;  BOOL class_utf8;
3310  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
3311  uschar *class_utf8data;  pcre_uint8 *class_utf8data;
3312  uschar *class_utf8data_base;  pcre_uint8 *class_utf8data_base;
3313  uschar utf8_char[6];  pcre_uint8 utf8_char[6];
3314  #else  #else
3315  BOOL utf8 = FALSE;  BOOL utf8 = FALSE;
 uschar *utf8_char = NULL;  
3316  #endif  #endif
3317    
3318  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
# Line 3029  for (;; ptr++) Line 3363  for (;; ptr++)
3363    int subfirstbyte;    int subfirstbyte;
3364    int terminator;    int terminator;
3365    int mclength;    int mclength;
3366    uschar mcbuffer[8];    int tempbracount;
3367      pcre_uchar mcbuffer[8];
3368    
3369    /* Get next byte in the pattern */    /* Get next byte in the pattern */
3370    
# Line 3076  for (;; ptr++) Line 3411  for (;; ptr++)
3411        }        }
3412    
3413      *lengthptr += (int)(code - last_code);      *lengthptr += (int)(code - last_code);
3414      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),
3415          c));
3416    
3417      /* 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
3418      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 3086  for (;; ptr++) Line 3422  for (;; ptr++)
3422        {        {
3423        if (previous > orig_code)        if (previous > orig_code)
3424          {          {
3425          memmove(orig_code, previous, code - previous);          memmove(orig_code, previous, IN_UCHARS(code - previous));
3426          code -= previous - orig_code;          code -= previous - orig_code;
3427          previous = orig_code;          previous = orig_code;
3428          }          }
# Line 3150  for (;; ptr++) Line 3486  for (;; ptr++)
3486      previous_callout = NULL;      previous_callout = NULL;
3487      }      }
3488    
3489    /* In extended mode, skip white space and comments */    /* In extended mode, skip white space and comments. */
3490    
3491    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
3492      {      {
3493      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if ((cd->ctypes[c] & ctype_space) != 0) continue;
3494      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3495        {        {
3496        ptr++;        ptr++;
3497        while (*ptr != 0)        while (*ptr != 0)
3498          {          {
3499          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3500          ptr++;          ptr++;
3501  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3502          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
3503  #endif  #endif
3504          }          }
# Line 3209  for (;; ptr++) Line 3545  for (;; ptr++)
3545      the setting of any following char as a first character. */      the setting of any following char as a first character. */
3546    
3547      case CHAR_CIRCUMFLEX_ACCENT:      case CHAR_CIRCUMFLEX_ACCENT:
3548        previous = NULL;
3549      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3550        {        {
3551        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
3552          *code++ = OP_CIRCM;
3553        }        }
3554      previous = NULL;      else *code++ = OP_CIRC;
     *code++ = OP_CIRC;  
3555      break;      break;
3556    
3557      case CHAR_DOLLAR_SIGN:      case CHAR_DOLLAR_SIGN:
3558      previous = NULL;      previous = NULL;
3559      *code++ = OP_DOLL;      *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
3560      break;      break;
3561    
3562      /* 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 3283  for (;; ptr++) Line 3620  for (;; ptr++)
3620          {          {
3621          if (ptr[1] == CHAR_E)          if (ptr[1] == CHAR_E)
3622            ptr++;            ptr++;
3623          else if (strncmp((const char *)ptr+1,          else if (STRNCMP_UC_C8(ptr + 1,
3624                            STR_Q STR_BACKSLASH STR_E, 3) == 0)                            STR_Q STR_BACKSLASH STR_E, 3) == 0)
3625            ptr += 3;            ptr += 3;
3626          else          else
# Line 3326  for (;; ptr++) Line 3663  for (;; ptr++)
3663      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.
3664      */      */
3665    
3666      memset(classbits, 0, 32 * sizeof(uschar));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
3667    
3668  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3669      class_utf8 = FALSE;                       /* No chars >= 256 */      class_utf8 = FALSE;                       /* No chars >= 256 */
# Line 3340  for (;; ptr++) Line 3677  for (;; ptr++)
3677    
3678      if (c != 0) do      if (c != 0) do
3679        {        {
3680        const uschar *oldptr;        const pcre_uchar *oldptr;
3681    
3682  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3683        if (utf8 && c > 127)        if (utf8 && c > 127)
# Line 3386  for (;; ptr++) Line 3723  for (;; ptr++)
3723          {          {
3724          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
3725          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
3726          register const uschar *cbits = cd->cbits;          register const pcre_uint8 *cbits = cd->cbits;
3727          uschar pbits[32];          pcre_uint8 pbits[32];
3728    
3729          if (ptr[1] != CHAR_COLON)          if (ptr[1] != CHAR_COLON)
3730            {            {
# Line 3442  for (;; ptr++) Line 3779  for (;; ptr++)
3779          /* Copy in the first table (always present) */          /* Copy in the first table (always present) */
3780    
3781          memcpy(pbits, cbits + posix_class_maps[posix_class],          memcpy(pbits, cbits + posix_class_maps[posix_class],
3782            32 * sizeof(uschar));            32 * sizeof(pcre_uint8));
3783    
3784          /* If there is a second table, add or remove it as required. */          /* If there is a second table, add or remove it as required. */
3785    
# Line 3504  for (;; ptr++) Line 3841  for (;; ptr++)
3841    
3842          if (c < 0)          if (c < 0)
3843            {            {
3844            register const uschar *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
3845            class_charcount += 2;     /* Greater than 1 is what matters */            class_charcount += 2;     /* Greater than 1 is what matters */
3846    
3847            switch (-c)            switch (-c)
# Line 3540  for (;; ptr++) Line 3877  for (;; ptr++)
3877              continue;              continue;
3878    
3879              /* 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
3880              if it was previously set by something earlier in the character              if it was previously set by something earlier in the character
3881              class. */              class. */
3882    
3883              case ESC_s:              case ESC_s:
3884              classbits[0] |= cbits[cbit_space];              classbits[0] |= cbits[cbit_space];
3885              classbits[1] |= cbits[cbit_space+1] & ~0x08;              classbits[1] |= cbits[cbit_space+1] & ~0x08;
3886              for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];              for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
3887              continue;              continue;
3888    
# Line 3564  for (;; ptr++) Line 3901  for (;; ptr++)
3901                {                {
3902                class_utf8 = TRUE;                class_utf8 = TRUE;
3903                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
3904                class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x1680, class_utf8data);
3905                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
3906                class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x180e, class_utf8data);
3907                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
3908                class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x2000, class_utf8data);
3909                class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x200A, class_utf8data);
3910                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
3911                class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x202f, class_utf8data);
3912                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
3913                class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x205f, class_utf8data);
3914                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
3915                class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x3000, class_utf8data);
3916                }                }
3917  #endif  #endif
3918              continue;              continue;
# Line 3599  for (;; ptr++) Line 3936  for (;; ptr++)
3936                {                {
3937                class_utf8 = TRUE;                class_utf8 = TRUE;
3938                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
3939                class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x0100, class_utf8data);
3940                class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x167f, class_utf8data);
3941                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
3942                class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x1681, class_utf8data);
3943                class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x180d, class_utf8data);
3944                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
3945                class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x180f, class_utf8data);
3946                class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x1fff, class_utf8data);
3947                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
3948                class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x200B, class_utf8data);
3949                class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x202e, class_utf8data);
3950                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
3951                class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x2030, class_utf8data);
3952                class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x205e, class_utf8data);
3953                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
3954                class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x2060, class_utf8data);
3955                class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x2fff, class_utf8data);
3956                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
3957                class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x3001, class_utf8data);
3958                class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x7fffffff, class_utf8data);
3959                }                }
3960  #endif  #endif
3961              continue;              continue;
# Line 3634  for (;; ptr++) Line 3971  for (;; ptr++)
3971                {                {
3972                class_utf8 = TRUE;                class_utf8 = TRUE;
3973                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
3974                class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x2028, class_utf8data);
3975                class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x2029, class_utf8data);
3976                }                }
3977  #endif  #endif
3978              continue;              continue;
# Line 3662  for (;; ptr++) Line 3999  for (;; ptr++)
3999                {                {
4000                class_utf8 = TRUE;                class_utf8 = TRUE;
4001                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
4002                class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x0100, class_utf8data);
4003                class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x2027, class_utf8data);
4004                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
4005                class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x2029, class_utf8data);
4006                class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);                class_utf8data += PRIV(ord2utf8)(0x7fffffff, class_utf8data);
4007                }                }
4008  #endif  #endif
4009              continue;              continue;
# Line 3844  for (;; ptr++) Line 4181  for (;; ptr++)
4181                else                else
4182                  {                  {
4183                  *class_utf8data++ = XCL_RANGE;                  *class_utf8data++ = XCL_RANGE;
4184                  class_utf8data += _pcre_ord2utf8(occ, class_utf8data);                  class_utf8data += PRIV(ord2utf8)(occ, class_utf8data);
4185                  }                  }
4186                class_utf8data += _pcre_ord2utf8(ocd, class_utf8data);                class_utf8data += PRIV(ord2utf8)(ocd, class_utf8data);
4187                }                }
4188              }              }
4189  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
# Line 3855  for (;; ptr++) Line 4192  for (;; ptr++)
4192            overlapping ranges. */            overlapping ranges. */
4193    
4194            *class_utf8data++ = XCL_RANGE;            *class_utf8data++ = XCL_RANGE;
4195            class_utf8data += _pcre_ord2utf8(c, class_utf8data);            class_utf8data += PRIV(ord2utf8)(c, class_utf8data);
4196            class_utf8data += _pcre_ord2utf8(d, class_utf8data);            class_utf8data += PRIV(ord2utf8)(d, class_utf8data);
4197    
4198            /* With UCP support, we are done. Without UCP support, there is no            /* With UCP support, we are done. Without UCP support, there is no
4199            caseless matching for UTF-8 characters > 127; we can use the bit map            caseless matching for UTF-8 characters > 127; we can use the bit map
# Line 3910  for (;; ptr++) Line 4247  for (;; ptr++)
4247          {          {
4248          class_utf8 = TRUE;          class_utf8 = TRUE;
4249          *class_utf8data++ = XCL_SINGLE;          *class_utf8data++ = XCL_SINGLE;
4250          class_utf8data += _pcre_ord2utf8(c, class_utf8data);          class_utf8data += PRIV(ord2utf8)(c, class_utf8data);
4251    
4252  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4253          if ((options & PCRE_CASELESS) != 0)          if ((options & PCRE_CASELESS) != 0)
# Line 3919  for (;; ptr++) Line 4256  for (;; ptr++)
4256            if ((othercase = UCD_OTHERCASE(c)) != c)            if ((othercase = UCD_OTHERCASE(c)) != c)
4257              {              {
4258              *class_utf8data++ = XCL_SINGLE;              *class_utf8data++ = XCL_SINGLE;
4259              class_utf8data += _pcre_ord2utf8(othercase, class_utf8data);              class_utf8data += PRIV(ord2utf8)(othercase, class_utf8data);
4260              }              }
4261            }            }
4262  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
# Line 3965  for (;; ptr++) Line 4302  for (;; ptr++)
4302    
4303      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
4304      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR
4305      operate on single-bytes only. This is an historical hangover. Maybe one day      operate on single-bytes characters only. This is an historical hangover.
4306      we can tidy these opcodes to handle multi-byte characters.      Maybe one day we can tidy these opcodes to handle multi-byte characters.
4307    
4308      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
4309      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.
4310      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
4311      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
4312      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
4313      reqbyte, save the previous value for reinstating. */      of reqbyte, save the previous value for reinstating. */
4314    
4315  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4316      if (class_charcount == 1 && !class_utf8 &&      if (class_charcount == 1 && !class_utf8 &&
# Line 3984  for (;; ptr++) Line 4321  for (;; ptr++)
4321        {        {
4322        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
4323    
4324        /* The OP_NOT opcode works on one-byte characters only. */        /* The OP_NOT[I] opcodes work on one-byte characters only. */
4325    
4326        if (negate_class)        if (negate_class)
4327          {          {
4328          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
4329          zerofirstbyte = firstbyte;          zerofirstbyte = firstbyte;
4330          *code++ = OP_NOT;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4331          *code++ = class_lastchar;          *code++ = class_lastchar;
4332          break;          break;
4333          }          }
# Line 4000  for (;; ptr++) Line 4337  for (;; ptr++)
4337    
4338  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4339        if (utf8 && class_lastchar > 127)        if (utf8 && class_lastchar > 127)
4340          mclength = _pcre_ord2utf8(class_lastchar, mcbuffer);          mclength = PRIV(ord2utf8)(class_lastchar, mcbuffer);
4341        else        else
4342  #endif  #endif
4343          {          {
# Line 4118  for (;; ptr++) Line 4455  for (;; ptr++)
4455      op_type = 0;                    /* Default single-char op codes */      op_type = 0;                    /* Default single-char op codes */
4456      possessive_quantifier = FALSE;  /* Default not possessive quantifier */      possessive_quantifier = FALSE;  /* Default not possessive quantifier */
4457    
4458      /* 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
4459      for an inserted OP_ONCE for the additional '+' extension. */      insert something before it. */
4460    
4461      tempcode = previous;      tempcode = previous;
4462    
# Line 4142  for (;; ptr++) Line 4479  for (;; ptr++)
4479        }        }
4480      else repeat_type = greedy_default;      else repeat_type = greedy_default;
4481    
4482        /* If previous was a recursion call, wrap it in atomic brackets so that
4483        previous becomes the atomic group. All recursions were so wrapped in the
4484        past, but it no longer happens for non-repeated recursions. In fact, the
4485        repeated ones could be re-implemented independently so as not to need this,
4486        but for the moment we rely on the code for repeating groups. */
4487    
4488        if (*previous == OP_RECURSE)
4489          {
4490          memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
4491          *previous = OP_ONCE;
4492          PUT(previous, 1, 2 + 2*LINK_SIZE);
4493          previous[2 + 2*LINK_SIZE] = OP_KET;
4494          PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4495          code += 2 + 2 * LINK_SIZE;
4496          length_prevgroup = 3 + 3*LINK_SIZE;
4497    
4498          /* When actually compiling, we need to check whether this was a forward
4499          reference, and if so, adjust the offset. */
4500    
4501          if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4502            {
4503            int offset = GET(cd->hwm, -LINK_SIZE);
4504            if (offset == previous + 1 - cd->start_code)
4505              PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
4506            }
4507          }
4508    
4509        /* Now handle repetition for the different types of item. */
4510    
4511      /* If previous was a character match, abolish the item and generate a      /* If previous was a character match, abolish the item and generate a
4512      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
4513      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
4514      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
4515      instead.  */      instead.  */
4516    
4517      if (*previous == OP_CHAR || *previous == OP_CHARNC)      if (*previous == OP_CHAR || *previous == OP_CHARI)
4518        {        {
4519          op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;
4520    
4521        /* 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
4522        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
4523        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 4526  for (;; ptr++)
4526  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4527        if (utf8 && (code[-1] & 0x80) != 0)        if (utf8 && (code[-1] & 0x80) != 0)
4528          {          {
4529          uschar *lastchar = code - 1;          pcre_uchar *lastchar = code - 1;
4530          while((*lastchar & 0xc0) == 0x80) lastchar--;          while((*lastchar & 0xc0) == 0x80) lastchar--;
4531          c = code - lastchar;            /* Length of UTF-8 character */          c = code - lastchar;            /* Length of UTF-8 character */
4532          memcpy(utf8_char, lastchar, c); /* Save the char */          memcpy(utf8_char, lastchar, c); /* Save the char */
# Line 4194  for (;; ptr++) Line 4562  for (;; ptr++)
4562      /* If previous was a single negated character ([^a] or similar), we use      /* If previous was a single negated character ([^a] or similar), we use
4563      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-
4564      character repeats by setting opt_type to add a suitable offset into      character repeats by setting opt_type to add a suitable offset into
4565      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
4566      currently used only for single-byte chars. */      are currently used only for single-byte chars. */
4567    
4568      else if (*previous == OP_NOT)      else if (*previous == OP_NOT || *previous == OP_NOTI)
4569        {        {
4570        op_type = OP_NOTSTAR - OP_STAR;  /* Use "not" opcodes */        op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;
4571        c = previous[1];        c = previous[1];
4572        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4573            repeat_max < 0 &&            repeat_max < 0 &&
# Line 4220  for (;; ptr++) Line 4588  for (;; ptr++)
4588    
4589      else if (*previous < OP_EODN)      else if (*previous < OP_EODN)
4590        {        {
4591        uschar *oldcode;        pcre_uchar *oldcode;
4592        int prop_type, prop_value;        int prop_type, prop_value;
4593        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
4594        c = *previous;        c = *previous;
# Line 4396  for (;; ptr++) Line 4764  for (;; ptr++)
4764  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4765               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
4766  #endif  #endif
4767               *previous == OP_REF)               *previous == OP_REF ||
4768                 *previous == OP_REFI)
4769        {        {
4770        if (repeat_max == 0)        if (repeat_max == 0)
4771          {          {
# Line 4430  for (;; ptr++) Line 4799  for (;; ptr++)
4799        }        }
4800    
4801      /* 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
4802      cases. */      cases. Note that at this point we can encounter only the "basic" bracket
4803        opcodes such as BRA and CBRA, as this is the place where they get converted
4804        into the more special varieties such as BRAPOS and SBRA. A test for >=
4805        OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
4806        ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow
4807        repetition of assertions, but now it does, for Perl compatibility. */
4808    
4809      else if (*previous == OP_BRA  || *previous == OP_CBRA ||      else if (*previous >= OP_ASSERT && *previous <= OP_COND)
              *previous == OP_ONCE || *previous == OP_COND)  
4810        {        {
4811        register int i;        register int i;
       int ketoffset = 0;  
4812        int len = (int)(code - previous);        int len = (int)(code - previous);
4813        uschar *bralink = NULL;        pcre_uchar *bralink = NULL;
4814          pcre_uchar *brazeroptr = NULL;
4815    
4816        /* Repeating a DEFINE group is pointless */        /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
4817          we just ignore the repeat. */
4818    
4819        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
4820          {          goto END_REPEAT;
4821          *errorcodeptr = ERR55;  
4822          goto FAILED;        /* There is no sense in actually repeating assertions. The only potential
4823          }        use of repetition is in cases when the assertion is optional. Therefore,
4824          if the minimum is greater than zero, just ignore the repeat. If the
4825          maximum is not not zero or one, set it to 1. */
4826    
4827        /* If the maximum repeat count is unlimited, find the end of the bracket        if (*previous < OP_ONCE)    /* Assertion */
4828        by scanning through from the start, and compute the offset back to it          {
4829        from the current code pointer. There may be an OP_OPT setting following          if (repeat_min > 0) goto END_REPEAT;
4830        the final KET, so we can't find the end just by going back from the code          if (repeat_max < 0 || repeat_max > 1) repeat_max = 1;
       pointer. */  
   
       if (repeat_max == -1)  
         {  
         register uschar *ket = previous;  
         do ket += GET(ket, 1); while (*ket != OP_KET);  
         ketoffset = (int)(code - ket);  
4831          }          }
4832    
4833        /* 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 4848  for (;; ptr++)
4848          **   goto END_REPEAT;          **   goto END_REPEAT;
4849          **   }          **   }
4850    
4851          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
4852          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
4853          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
4854          groups are referenced, we cannot do this selectively.          don't have a list of which groups are referenced, we cannot do this
4855            selectively.
4856    
4857          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
4858          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 4495  for (;; ptr++) Line 4865  for (;; ptr++)
4865            {            {
4866            *code = OP_END;            *code = OP_END;
4867            adjust_recurse(previous, 1, utf8, cd, save_hwm);            adjust_recurse(previous, 1, utf8, cd, save_hwm);
4868            memmove(previous+1, previous, len);            memmove(previous + 1, previous, IN_UCHARS(len));
4869            code++;            code++;
4870            if (repeat_max == 0)            if (repeat_max == 0)
4871              {              {
4872              *previous++ = OP_SKIPZERO;              *previous++ = OP_SKIPZERO;
4873              goto END_REPEAT;              goto END_REPEAT;
4874              }              }
4875              brazeroptr = previous;    /* Save for possessive optimizing */
4876            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
4877            }            }
4878    
# Line 4518  for (;; ptr++) Line 4889  for (;; ptr++)
4889            int offset;            int offset;
4890            *code = OP_END;            *code = OP_END;
4891            adjust_recurse(previous, 2 + LINK_SIZE, utf8, cd, save_hwm);            adjust_recurse(previous, 2 + LINK_SIZE, utf8, cd, save_hwm);
4892            memmove(previous + 2 + LINK_SIZE, previous, len);            memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));
4893            code += 2 + LINK_SIZE;            code += 2 + LINK_SIZE;
4894            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
4895            *previous++ = OP_BRA;            *previous++ = OP_BRA;
# Line 4571  for (;; ptr++) Line 4942  for (;; ptr++)
4942              if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte;              if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte;
4943              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
4944                {                {
4945                uschar *hc;                pcre_uchar *hc;
4946                uschar *this_hwm = cd->hwm;                pcre_uchar *this_hwm = cd->hwm;
4947                memcpy(code, previous, len);                memcpy(code, previous, IN_UCHARS(len));
4948                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
4949                  {                  {
4950                  PUT(cd->hwm, 0, GET(hc, 0) + len);                  PUT(cd->hwm, 0, GET(hc, 0) + len);
# Line 4623  for (;; ptr++) Line 4994  for (;; ptr++)
4994    
4995          else for (i = repeat_max - 1; i >= 0; i--)          else for (i = repeat_max - 1; i >= 0; i--)
4996            {            {
4997            uschar *hc;            pcre_uchar *hc;
4998            uschar *this_hwm = cd->hwm;            pcre_uchar *this_hwm = cd->hwm;
4999    
5000            *code++ = OP_BRAZERO + repeat_type;            *code++ = OP_BRAZERO + repeat_type;
5001    
# Line 4640  for (;; ptr++) Line 5011  for (;; ptr++)
5011              PUTINC(code, 0, offset);              PUTINC(code, 0, offset);
5012              }              }
5013    
5014            memcpy(code, previous, len);            memcpy(code, previous, IN_UCHARS(len));
5015            for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)            for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
5016              {              {
5017              PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));              PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));
# Line 4657  for (;; ptr++) Line 5028  for (;; ptr++)
5028            {            {
5029            int oldlinkoffset;            int oldlinkoffset;
5030            int offset = (int)(code - bralink + 1);            int offset = (int)(code - bralink + 1);
5031            uschar *bra = code - offset;            pcre_uchar *bra = code - offset;
5032            oldlinkoffset = GET(bra, 1);            oldlinkoffset = GET(bra, 1);
5033            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
5034            *code++ = OP_KET;            *code++ = OP_KET;
# Line 4666  for (;; ptr++) Line 5037  for (;; ptr++)
5037            }            }
5038          }          }
5039    
5040        /* 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
5041        can't just offset backwards from the current code point, because we        ONCE brackets, that's all we need to do. However, possessively repeated
5042        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
5043        correct offset was computed above.        behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
5044          deal with possessive ONCEs specially.
5045        Then, when we are doing the actual compile phase, check to see whether  
5046        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
5047          whether this group is one that could match an empty string. If so,
5048        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
5049        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
5050        atomic groups at runtime, but in a different way.] */        groups at runtime, but in a different way.]
5051    
5052          Then, if the quantifier was possessive and the bracket is not a
5053          conditional, we convert the BRA code to the POS form, and the KET code to
5054          KETRPOS. (It turns out to be convenient at runtime to detect this kind of
5055          subpattern at both the start and at the end.) The use of special opcodes
5056          makes it possible to reduce greatly the stack usage in pcre_exec(). If
5057          the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
5058    
5059          Then, if the minimum number of matches is 1 or 0, cancel the possessive
5060          flag so that the default action below, of wrapping everything inside
5061          atomic brackets, does not happen. When the minimum is greater than 1,
5062          there will be earlier copies of the group, and so we still have to wrap
5063          the whole thing. */
5064    
5065        else        else
5066          {          {
5067          uschar *ketcode = code - ketoffset;          pcre_uchar *ketcode = code - 1 - LINK_SIZE;
5068          uschar *bracode = ketcode - GET(ketcode, 1);          pcre_uchar *bracode = ketcode - GET(ketcode, 1);
5069          *ketcode = OP_KETRMAX + repeat_type;  
5070          if (lengthptr == NULL && *bracode != OP_ONCE)          /* Convert possessive ONCE brackets to non-capturing */
5071    
5072            if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&
5073                possessive_quantifier) *bracode = OP_BRA;
5074    
5075            /* For non-possessive ONCE brackets, all we need to do is to
5076            set the KET. */
5077    
5078            if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)
5079              *ketcode = OP_KETRMAX + repeat_type;
5080    
5081            /* Handle non-ONCE brackets and possessive ONCEs (which have been
5082            converted to non-capturing above). */
5083    
5084            else
5085            {            {
5086            uschar *scode = bracode;            /* In the compile phase, check for empty string matching. */
5087            do  
5088              if (lengthptr == NULL)
5089              {              {
5090              if (could_be_empty_branch(scode, ketcode, utf8, cd))              pcre_uchar *scode = bracode;
5091                do
5092                {                {
5093                *bracode += OP_SBRA - OP_BRA;                if (could_be_empty_branch(scode, ketcode, utf8, cd))
5094                break;                  {
5095                    *bracode += OP_SBRA - OP_BRA;
5096                    break;
5097                    }
5098                  scode += GET(scode, 1);
5099                }                }
5100              scode += GET(scode, 1);              while (*scode == OP_ALT);
5101              }              }
5102            while (*scode == OP_ALT);  
5103              /* Handle possessive quantifiers. */
5104    
5105              if (possessive_quantifier)
5106                {
5107                /* For COND brackets, we wrap the whole thing in a possessively
5108                repeated non-capturing bracket, because we have not invented POS
5109                versions of the COND opcodes. Because we are moving code along, we
5110                must ensure that any pending recursive references are updated. */
5111    
5112                if (*bracode == OP_COND || *bracode == OP_SCOND)
5113                  {
5114                  int nlen = (int)(code - bracode);
5115                  *code = OP_END;
5116                  adjust_recurse(bracode, 1 + LINK_SIZE, utf8, cd, save_hwm);
5117                  memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));
5118                  code += 1 + LINK_SIZE;
5119                  nlen += 1 + LINK_SIZE;
5120                  *bracode = OP_BRAPOS;
5121                  *code++ = OP_KETRPOS;
5122                  PUTINC(code, 0, nlen);
5123                  PUT(bracode, 1, nlen);
5124                  }
5125    
5126                /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
5127    
5128                else
5129                  {
5130                  *bracode += 1;              /* Switch to xxxPOS opcodes */
5131                  *ketcode = OP_KETRPOS;
5132                  }
5133    
5134                /* If the minimum is zero, mark it as possessive, then unset the
5135                possessive flag when the minimum is 0 or 1. */
5136    
5137                if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
5138                if (repeat_min < 2) possessive_quantifier = FALSE;
5139                }
5140    
5141              /* Non-possessive quantifier */
5142    
5143              else *ketcode = OP_KETRMAX + repeat_type;
5144            }            }
5145          }          }
5146        }        }
# Line 4715  for (;; ptr++) Line 5161  for (;; ptr++)
5161        }        }
5162    
5163      /* If the character following a repeat is '+', or if certain optimization      /* If the character following a repeat is '+', or if certain optimization
5164      tests above succeeded, possessive_quantifier is TRUE. For some of the      tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
5165      simpler opcodes, there is an special alternative opcode for this. For      there are special alternative opcodes for this case. For anything else, we
5166      anything else, we wrap the entire repeated item inside OP_ONCE brackets.      wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
5167      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
5168      but the special opcodes can optimize it a bit. The repeated item starts at      special opcodes can optimize it.
5169      tempcode, not at previous, which might be the first part of a string whose  
5170      (former) last char we repeated.      Some (but not all) possessively repeated subpatterns have already been
5171        completely handled in the code just above. For them, possessive_quantifier
5172        is always FALSE at this stage.
5173    
5174        Note that the repeated item starts at tempcode, not at previous, which
5175        might be the first part of a string whose (former) last char we repeated.
5176    
5177      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
5178      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 4732  for (;; ptr++) Line 5183  for (;; ptr++)
5183        int len;        int len;
5184    
5185        if (*tempcode == OP_TYPEEXACT)        if (*tempcode == OP_TYPEEXACT)
5186          tempcode += _pcre_OP_lengths[*tempcode] +          tempcode += PRIV(OP_lengths)[*tempcode] +
5187            ((tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP)? 2 : 0);            ((tempcode[1 + IMM2_SIZE] == OP_PROP
5188              || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
5189    
5190        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
5191          {          {
5192          tempcode += _pcre_OP_lengths[*tempcode];          tempcode += PRIV(OP_lengths)[*tempcode];
5193  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
5194          if (utf8 && tempcode[-1] >= 0xc0)          if (utf8 && tempcode[-1] >= 0xc0)
5195            tempcode += _pcre_utf8_table4[tempcode[-1] & 0x3f];            tempcode += PRIV(utf8_table4)[tempcode[-1] & 0x3f];
5196  #endif  #endif
5197          }          }
5198    
# Line 4752  for (;; ptr++) Line 5204  for (;; ptr++)
5204          case OP_QUERY: *tempcode = OP_POSQUERY; break;          case OP_QUERY: *tempcode = OP_POSQUERY; break;
5205          case OP_UPTO:  *tempcode = OP_POSUPTO; break;          case OP_UPTO:  *tempcode = OP_POSUPTO; break;
5206    
5207          case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;          case OP_STARI:  *tempcode = OP_POSSTARI; break;
5208          case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;          case OP_PLUSI:  *tempcode = OP_POSPLUSI; break;
5209          case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;          case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
5210          case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;          case OP_UPTOI:  *tempcode = OP_POSUPTOI; break;
5211    
5212          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;
5213          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;
5214          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
5215          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;
5216    
5217            case OP_NOTSTARI:  *tempcode = OP_NOTPOSSTARI; break;
5218            case OP_NOTPLUSI:  *tempcode = OP_NOTPOSPLUSI; break;
5219            case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
5220            case OP_NOTUPTOI:  *tempcode = OP_NOTPOSUPTOI; break;
5221    
5222            case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;
5223            case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;
5224            case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
5225            case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;
5226    
5227          /* Because we are moving code along, we must ensure that any          /* Because we are moving code along, we must ensure that any
5228          pending recursive references are updated. */          pending recursive references are updated. */
5229    
5230          default:          default:
5231          *code = OP_END;          *code = OP_END;
5232          adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);          adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);
5233          memmove(tempcode + 1+LINK_SIZE, tempcode, len);          memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
5234          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
5235          len += 1 + LINK_SIZE;          len += 1 + LINK_SIZE;
5236          tempcode[0] = OP_ONCE;          tempcode[0] = OP_ONCE;
# Line 4809  for (;; ptr++) Line 5271  for (;; ptr++)
5271        int i, namelen;        int i, namelen;
5272        int arglen = 0;        int arglen = 0;
5273        const char *vn = verbnames;        const char *vn = verbnames;
5274        const uschar *name = ptr + 1;        const pcre_uchar *name = ptr + 1;
5275        const uschar *arg = NULL;        const pcre_uchar *arg = NULL;
5276        previous = NULL;        previous = NULL;
5277        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};
5278        namelen = (int)(ptr - name);        namelen = (int)(ptr - name);
5279    
5280          /* It appears that Perl allows any characters whatsoever, other than
5281          a closing parenthesis, to appear in arguments, so we no longer insist on
5282          letters, digits, and underscores. */
5283    
5284        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
5285          {          {
5286          arg = ++ptr;          arg = ++ptr;
5287          while ((cd->ctypes[*ptr] & (ctype_letter|ctype_digit)) != 0          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
           || *ptr == '_') ptr++;  
5288          arglen = (int)(ptr - arg);          arglen = (int)(ptr - arg);
5289          }          }
5290    
# Line 4834  for (;; ptr++) Line 5299  for (;; ptr++)
5299        for (i = 0; i < verbcount; i++)        for (i = 0; i < verbcount; i++)
5300          {          {
5301          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
5302              strncmp((char *)name, vn, namelen) == 0)              STRNCMP_UC_C8(name, vn, namelen) == 0)
5303            {            {
5304            /* Check for open captures before ACCEPT */            /* Check for open captures before ACCEPT and convert it to
5305              ASSERT_ACCEPT if in an assertion. */
5306    
5307            if (verbs[i].op == OP_ACCEPT)            if (verbs[i].op == OP_ACCEPT)
5308              {              {
5309              open_capitem *oc;              open_capitem *oc;
5310                if (arglen != 0)
5311                  {
5312                  *errorcodeptr = ERR59;
5313                  goto FAILED;
5314                  }
5315              cd->had_accept = TRUE;              cd->had_accept = TRUE;
5316              for (oc = cd->open_caps; oc != NULL; oc = oc->next)              for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5317                {                {
5318                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5319                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5320                }                }
5321                *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5322    
5323                /* Do not set firstbyte after *ACCEPT */
5324                if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
5325              }              }
5326    
5327            /* Handle the cases with/without an argument */            /* Handle other cases with/without an argument */
5328    
5329            if (arglen == 0)            else if (arglen == 0)
5330              {              {
5331              if (verbs[i].op < 0)   /* Argument is mandatory */              if (verbs[i].op < 0)   /* Argument is mandatory */
5332                {                {
# Line 4859  for (;; ptr++) Line 5334  for (;; ptr++)
5334                goto FAILED;                goto FAILED;
5335                }                }
5336              *code = verbs[i].op;              *code = verbs[i].op;
5337              if (*code++ == OP_THEN)              if (*code++ == OP_THEN) cd->external_flags |= PCRE_HASTHEN;
               {  
               PUT(code, 0, code - bcptr->current_branch - 1);  
               code += LINK_SIZE;  
               }  
5338              }              }
5339    
5340            else            else
# Line 4874  for (;; ptr++) Line 5345  for (;; ptr++)
5345                goto FAILED;                goto FAILED;
5346                }                }
5347              *code = verbs[i].op_arg;              *code = verbs[i].op_arg;
5348              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;  
               }  
5349              *code++ = arglen;              *code++ = arglen;
5350              memcpy(code, arg, arglen);              memcpy(code, arg, IN_UCHARS(arglen));
5351              code += arglen;              code += arglen;
5352              *code++ = 0;              *code++ = 0;
5353              }              }
# Line 4903  for (;; ptr++) Line 5370  for (;; ptr++)
5370        {        {
5371        int i, set, unset, namelen;        int i, set, unset, namelen;
5372        int *optset;        int *optset;
5373        const uschar *name;        const pcre_uchar *name;
5374        uschar *slot;        pcre_uchar *slot;
5375    
5376        switch (*(++ptr))        switch (*(++ptr))
5377          {          {
# Line 4957  for (;; ptr++) Line 5424  for (;; ptr++)
5424            break;            break;
5425    
5426          /* Most other conditions use OP_CREF (a couple change to OP_RREF          /* Most other conditions use OP_CREF (a couple change to OP_RREF
5427          below), and all need to skip 3 bytes at the start of the group. */          below), and all need to skip 1+IMM2_SIZE bytes at the start of the group. */
5428    
5429          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
5430          skipbytes = 3;          skipbytes = 1+IMM2_SIZE;
5431          refsign = -1;          refsign = -1;
5432    
5433          /* Check for a test for recursion in a named group. */          /* Check for a test for recursion in a named group. */
# Line 5056  for (;; ptr++) Line 5523  for (;; ptr++)
5523          slot = cd->name_table;          slot = cd->name_table;
5524          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
5525            {            {
5526            if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break;            if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0) break;
5527            slot += cd->name_entry_size;            slot += cd->name_entry_size;
5528            }            }
5529    
# Line 5113  for (;; ptr++) Line 5580  for (;; ptr++)
5580          /* Similarly, check for the (?(DEFINE) "condition", which is always          /* Similarly, check for the (?(DEFINE) "condition", which is always
5581          false. */          false. */
5582    
5583          else if (namelen == 6 && strncmp((char *)name, STRING_DEFINE, 6) == 0)          else if (namelen == 6 && STRNCMP_UC_C8(name, STRING_DEFINE, 6) == 0)
5584            {            {
5585            code[1+LINK_SIZE] = OP_DEF;            code[1+LINK_SIZE] = OP_DEF;
5586            skipbytes = 1;            skipbytes = 1;
# Line 5140  for (;; ptr++) Line 5607  for (;; ptr++)
5607          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5608          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */
5609          bravalue = OP_ASSERT;          bravalue = OP_ASSERT;
5610            cd->assert_depth += 1;
5611          ptr++;          ptr++;
5612          break;          break;
5613    
# Line 5154  for (;; ptr++) Line 5622  for (;; ptr++)
5622            continue;            continue;
5623            }            }
5624          bravalue = OP_ASSERT_NOT;          bravalue = OP_ASSERT_NOT;
5625            cd->assert_depth += 1;
5626          break;          break;
5627    
5628    
# Line 5163  for (;; ptr++) Line 5632  for (;; ptr++)
5632            {            {
5633            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */
5634            bravalue = OP_ASSERTBACK;            bravalue = OP_ASSERTBACK;
5635              cd->assert_depth += 1;
5636            ptr += 2;            ptr += 2;
5637            break;            break;
5638    
5639            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */
5640            bravalue = OP_ASSERTBACK_NOT;            bravalue = OP_ASSERTBACK_NOT;
5641              cd->assert_depth += 1;
5642            ptr += 2;            ptr += 2;
5643            break;            break;
5644    
# Line 5189  for (;; ptr++) Line 5660  for (;; ptr++)
5660    
5661          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5662          case CHAR_C:                 /* Callout - may be followed by digits; */          case CHAR_C:                 /* Callout - may be followed by digits; */
5663          previous_callout = code;  /* Save for later completion */          previous_callout = code;     /* Save for later completion */
5664          after_manual_callout = 1; /* Skip one item before completing */          after_manual_callout = 1;    /* Skip one item before completing */
5665          *code++ = OP_CALLOUT;          *code++ = OP_CALLOUT;
5666            {            {
5667            int n = 0;            int n = 0;
# Line 5312  for (;; ptr++) Line 5783  for (;; ptr++)
5783                if (crc < 0)                if (crc < 0)
5784                  {                  {
5785                  memmove(slot + cd->name_entry_size, slot,                  memmove(slot + cd->name_entry_size, slot,
5786                    (cd->names_found - i) * cd->name_entry_size);                    IN_UCHARS((cd->names_found - i) * cd->name_entry_size));
5787                  break;                  break;
5788                  }                  }
5789    
# Line 5326  for (;; ptr++) Line 5797  for (;; ptr++)
5797    
5798              if (!dupname)              if (!dupname)
5799                {                {
5800                uschar *cslot = cd->name_table;                pcre_uchar *cslot = cd->name_table;
5801                for (i = 0; i < cd->names_found; i++)                for (i = 0; i < cd->names_found; i++)
5802                  {                  {
5803                  if (cslot != slot)                  if (cslot != slot)
# Line 5343  for (;; ptr++) Line 5814  for (;; ptr++)
5814                }                }
5815    
5816              PUT2(slot, 0, cd->bracount + 1);              PUT2(slot, 0, cd->bracount + 1);
5817              memcpy(slot + 2, name, namelen);              memcpy(slot + 2, name, IN_UCHARS(namelen));
5818              slot[2+namelen] = 0;              slot[2 + namelen] = 0;
5819              }              }
5820            }            }
5821    
# Line 5373  for (;; ptr++) Line 5844  for (;; ptr++)
5844          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5845          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
5846    
5847          /* 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
5848          reference number. */          a dummy reference number, because it was not used in the first pass.
5849            However, with the change of recursive back references to be atomic,
5850            we have to look for the number so that this state can be identified, as
5851            otherwise the incorrect length is computed. If it's not a backwards
5852            reference, the dummy number will do. */
5853    
5854          if (lengthptr != NULL)          if (lengthptr != NULL)
5855            {            {
5856              const pcre_uchar *temp;
5857    
5858            if (namelen == 0)            if (namelen == 0)
5859              {              {
5860              *errorcodeptr = ERR62;              *errorcodeptr = ERR62;
# Line 5393  for (;; ptr++) Line 5870  for (;; ptr++)
5870              *errorcodeptr = ERR48;              *errorcodeptr = ERR48;
5871              goto FAILED;              goto FAILED;
5872              }              }
5873            recno = 0;  
5874              /* The name table does not exist in the first pass, so we cannot
5875              do a simple search as in the code below. Instead, we have to scan the
5876              pattern to find the number. It is important that we scan it only as
5877              far as we have got because the syntax of named subpatterns has not
5878              been checked for the rest of the pattern, and find_parens() assumes
5879              correct syntax. In any case, it's a waste of resources to scan
5880              further. We stop the scan at the current point by temporarily
5881              adjusting the value of cd->endpattern. */
5882    
5883              temp = cd->end_pattern;
5884              cd->end_pattern = ptr;
5885              recno = find_parens(cd, name, namelen,
5886                (options & PCRE_EXTENDED) != 0, utf8);
5887              cd->end_pattern = temp;
5888              if (recno < 0) recno = 0;    /* Forward ref; set dummy number */
5889            }            }
5890    
5891          /* 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 5406  for (;; ptr++) Line 5898  for (;; ptr++)
5898            slot = cd->name_table;            slot = cd->name_table;
5899            for (i = 0; i < cd->names_found; i++)            for (i = 0; i < cd->names_found; i++)
5900              {              {
5901              if (strncmp((char *)name, (char *)slot+2, namelen) == 0 &&              if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 &&
5902                  slot[2+namelen] == 0)                  slot[2+namelen] == 0)
5903                break;                break;
5904              slot += cd->name_entry_size;              slot += cd->name_entry_size;
# Line 5443  for (;; ptr++) Line 5935  for (;; ptr++)
5935          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:
5936          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:
5937            {            {
5938            const uschar *called;            const pcre_uchar *called;
5939            terminator = CHAR_RIGHT_PARENTHESIS;            terminator = CHAR_RIGHT_PARENTHESIS;
5940    
5941            /* Come here from the \g<...> and \g'...' code (Oniguruma            /* Come here from the \g<...> and \g'...' code (Oniguruma
# Line 5522  for (;; ptr++) Line 6014  for (;; ptr++)
6014              {              {
6015              *code = OP_END;              *code = OP_END;
6016              if (recno != 0)              if (recno != 0)
6017                called = _pcre_find_bracket(cd->start_code, utf8, recno);                called = PRIV(find_bracket)(cd->start_code, utf8, recno);
6018    
6019              /* Forward reference */              /* Forward reference */
6020    
# Line 5537  for (;; ptr++) Line 6029  for (;; ptr++)
6029    
6030                /* 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
6031                offset below, what it actually inserted is the reference number                offset below, what it actually inserted is the reference number
6032                of the group. */                of the group. Then remember the forward reference. */
6033    
6034                called = cd->start_code + recno;                called = cd->start_code + recno;
6035                PUTINC(cd->hwm, 0, (int)(code + 2 + LINK_SIZE - cd->start_code));                PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
6036                }                }
6037    
6038              /* If not a forward reference, and the subpattern is still open,              /* If not a forward reference, and the subpattern is still open,
6039              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
6040              recursion that could loop for ever, and diagnose that case. */              recursion that could loop for ever, and diagnose that case. We
6041                must not, however, do this check if we are in a conditional
6042                subpattern because the condition might be testing for recursion in
6043                a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid.
6044                Forever loops are also detected at runtime, so those that occur in
6045                conditional subpatterns will be picked up then. */
6046    
6047              else if (GET(called, 1) == 0 &&              else if (GET(called, 1) == 0 && cond_depth <= 0 &&
6048                       could_be_empty(called, code, bcptr, utf8, cd))                       could_be_empty(called, code, bcptr, utf8, cd))
6049                {                {
6050                *errorcodeptr = ERR40;                *errorcodeptr = ERR40;
# Line 5555  for (;; ptr++) Line 6052  for (;; ptr++)
6052                }                }
6053              }              }
6054    
6055            /* 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;  
6056    
6057            *code = OP_RECURSE;            *code = OP_RECURSE;
6058            PUT(code, 1, (int)(called - cd->start_code));            PUT(code, 1, (int)(called - cd->start_code));
6059            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;  
6060            }            }
6061    
6062          /* Can't determine a first byte now */          /* Can't determine a first byte now */
# Line 5632  for (;; ptr++) Line 6117  for (;; ptr++)
6117          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
6118          both phases.          both phases.
6119    
6120          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
6121          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. */  
6122    
6123          if (*ptr == CHAR_RIGHT_PARENTHESIS)          if (*ptr == CHAR_RIGHT_PARENTHESIS)
6124            {            {
# Line 5645  for (;; ptr++) Line 6129  for (;; ptr++)
6129              }              }
6130            else            else
6131              {              {
             if ((options & PCRE_IMS) != (newoptions & PCRE_IMS))  
               {  
               *code++ = OP_OPT;  
               *code++ = newoptions & PCRE_IMS;  
               }  
6132              greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);              greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
6133              greedy_non_default = greedy_default ^ 1;              greedy_non_default = greedy_default ^ 1;
6134              req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;              req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;
6135              }              }
6136    
6137            /* Change options at this level, and pass them back for use            /* Change options at this level, and pass them back for use
6138            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). */  
6139    
6140            *optionsptr = options = newoptions;            *optionsptr = options = newoptions;
6141            previous = NULL;       /* This item can't be repeated */            previous = NULL;       /* This item can't be repeated */
# Line 5691  for (;; ptr++) Line 6168  for (;; ptr++)
6168        NUMBERED_GROUP:        NUMBERED_GROUP:
6169        cd->bracount += 1;        cd->bracount += 1;
6170        PUT2(code, 1+LINK_SIZE, cd->bracount);        PUT2(code, 1+LINK_SIZE, cd->bracount);
6171        skipbytes = 2;        skipbytes = IMM2_SIZE;
6172        }        }
6173    
6174      /* Process nested bracketed regex. Assertions may not be repeated, but      /* Process nested bracketed regex. Assertions used not to be repeatable,
6175      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
6176      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
6177      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. */  
6178    
6179      previous = (bravalue >= OP_ONCE)? code : NULL;      previous = code;                      /* For handling repetition */
6180      *code = bravalue;      *code = bravalue;
6181      tempcode = code;      tempcode = code;
6182      tempreqvary = cd->req_varyopt;     /* Save value before bracket */      tempreqvary = cd->req_varyopt;        /* Save value before bracket */
6183      length_prevgroup = 0;              /* Initialize for pre-compile phase */      tempbracount = cd->bracount;          /* Save value before bracket */
6184        length_prevgroup = 0;                 /* Initialize for pre-compile phase */
6185    
6186      if (!compile_regex(      if (!compile_regex(
6187           newoptions,                   /* The complete new option state */           newoptions,                      /* The complete new option state */
6188           options & PCRE_IMS,           /* The previous ims option state */           &tempcode,                       /* Where to put code (updated) */
6189           &tempcode,                    /* Where to put code (updated) */           &ptr,                            /* Input pointer (updated) */
6190           &ptr,                         /* Input pointer (updated) */           errorcodeptr,                    /* Where to put an error message */
          errorcodeptr,                 /* Where to put an error message */  
6191           (bravalue == OP_ASSERTBACK ||           (bravalue == OP_ASSERTBACK ||
6192            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
6193           reset_bracount,               /* True if (?| group */           reset_bracount,                  /* True if (?| group */
6194           skipbytes,                    /* Skip over bracket number */           skipbytes,                       /* Skip over bracket number */
6195           &subfirstbyte,                /* For possible first char */           cond_depth +
6196           &subreqbyte,                  /* For possible last char */             ((bravalue == OP_COND)?1:0),   /* Depth of condition subpatterns */
6197           bcptr,                        /* Current branch chain */           &subfirstbyte,                   /* For possible first char */
6198           cd,                           /* Tables block */           &subreqbyte,                     /* For possible last char */
6199           (lengthptr == NULL)? NULL :   /* Actual compile phase */           bcptr,                           /* Current branch chain */
6200             &length_prevgroup           /* Pre-compile phase */           cd,                              /* Tables block */
6201             (lengthptr == NULL)? NULL :      /* Actual compile phase */
6202               &length_prevgroup              /* Pre-compile phase */
6203           ))           ))
6204        goto FAILED;        goto FAILED;
6205    
6206        /* If this was an atomic group and there are no capturing groups within it,
6207        generate OP_ONCE_NC instead of OP_ONCE. */
6208    
6209        if (bravalue == OP_ONCE && cd->bracount <= tempbracount)
6210          *code = OP_ONCE_NC;
6211    
6212        if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
6213          cd->assert_depth -= 1;
6214    
6215      /* 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
6216      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.
6217      and any option resetting that may follow it. The pattern pointer (ptr)      The pattern pointer (ptr) is on the bracket.
     is on the bracket. */  
6218    
6219      /* 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
6220      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
6221      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
6222      not be available. */      not be available. */
6223    
6224      if (bravalue == OP_COND && lengthptr == NULL)      if (bravalue == OP_COND && lengthptr == NULL)
6225        {        {
6226        uschar *tc = code;        pcre_uchar *tc = code;
6227        int condcount = 0;        int condcount = 0;
6228    
6229        do {        do {
# Line 5795  for (;; ptr++) Line 6281  for (;; ptr++)
6281          goto FAILED;          goto FAILED;
6282          }          }
6283        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
6284        *code++ = OP_BRA;        code++;   /* This already contains bravalue */
6285        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
6286        *code++ = OP_KET;        *code++ = OP_KET;
6287        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
# Line 5912  for (;; ptr++) Line 6398  for (;; ptr++)
6398    
6399        if (-c == ESC_g)        if (-c == ESC_g)
6400          {          {
6401          const uschar *p;          const pcre_uchar *p;
6402          save_hwm = cd->hwm;   /* Normally this is set when '(' is read */          save_hwm = cd->hwm;   /* Normally this is set when '(' is read */
6403          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
6404            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
# Line 5963  for (;; ptr++) Line 6449  for (;; ptr++)
6449          }          }
6450    
6451        /* \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).
6452        We also support \k{name} (.NET syntax) */        We also support \k{name} (.NET syntax).  */
6453    
6454        if (-c == ESC_k && (ptr[1] == CHAR_LESS_THAN_SIGN ||        if (-c == ESC_k)
           ptr[1] == CHAR_APOSTROPHE || ptr[1] == CHAR_LEFT_CURLY_BRACKET))  
6455          {          {
6456            if ((ptr[1] != CHAR_LESS_THAN_SIGN &&
6457              ptr[1] != CHAR_APOSTROPHE && ptr[1] != CHAR_LEFT_CURLY_BRACKET))
6458              {
6459              *errorcodeptr = ERR69;
6460              break;
6461              }
6462          is_recurse = FALSE;          is_recurse = FALSE;
6463          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
6464            CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)?            CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)?
# Line 5987  for (;; ptr++) Line 6478  for (;; ptr++)
6478          HANDLE_REFERENCE:    /* Come here from named backref handling */          HANDLE_REFERENCE:    /* Come here from named backref handling */
6479          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
6480          previous = code;          previous = code;
6481          *code++ = OP_REF;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
6482          PUT2INC(code, 0, recno);          PUT2INC(code, 0, recno);
6483          cd->backref_map |= (recno < 32)? (1 << recno) : 1;          cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6484          if (recno > cd->top_backref) cd->top_backref = recno;          if (recno > cd->top_backref) cd->top_backref = recno;
# Line 6047  for (;; ptr++) Line 6538  for (;; ptr++)
6538            }            }
6539          else          else
6540  #endif  #endif
6541            {          /* In non-UTF-8 mode, we turn \C into OP_ALLANY instead of OP_ANYBYTE
6542            so that it works in DFA mode and in lookbehinds. */
6543    
6544              {
6545            previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;            previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;
6546            *code++ = -c;            *code++ = (!utf8 && c == -ESC_C)? OP_ALLANY : -c;
6547            }            }
6548          }          }
6549        continue;        continue;
# Line 6061  for (;; ptr++) Line 6555  for (;; ptr++)
6555    
6556  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
6557      if (utf8 && c > 127)      if (utf8 && c > 127)
6558        mclength = _pcre_ord2utf8(c, mcbuffer);        mclength = PRIV(ord2utf8)(c, mcbuffer);
6559      else      else
6560  #endif  #endif
6561    
# Line 6095  for (;; ptr++) Line 6589  for (;; ptr++)
6589    
6590      ONE_CHAR:      ONE_CHAR:
6591      previous = code;      previous = code;
6592      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARNC : OP_CHAR;      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR;
6593      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
6594    
6595      /* Remember if \r or \n were seen */      /* Remember if \r or \n were seen */
# Line 6124  for (;; ptr++) Line 6618  for (;; ptr++)
6618        else firstbyte = reqbyte = REQ_NONE;        else firstbyte = reqbyte = REQ_NONE;
6619        }        }
6620    
6621      /* 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
6622      1 or the matching is caseful. */      1 or the matching is caseful. */
6623    
6624      else      else
# Line 6159  return FALSE; Line 6653  return FALSE;
6653  /* 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
6654  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
6655  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.  
   
6656  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
6657  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
6658  value of lengthptr distinguishes the two phases.  value of lengthptr distinguishes the two phases.
6659    
6660  Arguments:  Arguments:
6661    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  
6662    codeptr        -> the address of the current code pointer    codeptr        -> the address of the current code pointer
6663    ptrptr         -> the address of the current pattern pointer    ptrptr         -> the address of the current pattern pointer
6664    errorcodeptr   -> pointer to error code variable    errorcodeptr   -> pointer to error code variable
6665    lookbehind     TRUE if this is a lookbehind assertion    lookbehind     TRUE if this is a lookbehind assertion
6666    reset_bracount TRUE to reset the count for each branch    reset_bracount TRUE to reset the count for each branch
6667    skipbytes      skip this many bytes at start (for brackets and OP_COND)    skipbytes      skip this many bytes at start (for brackets and OP_COND)
6668      cond_depth     depth of nesting for conditional subpatterns
6669    firstbyteptr   place to put the first required character, or a negative number    firstbyteptr   place to put the first required character, or a negative number
6670    reqbyteptr     place to put the last required character, or a negative number    reqbyteptr     place to put the last required character, or a negative number
6671    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 6677  Returns:         TRUE on success
6677  */  */
6678    
6679  static BOOL  static BOOL
6680  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,  compile_regex(int options, pcre_uchar **codeptr, const pcre_uchar **ptrptr,
6681    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
6682    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,    int cond_depth, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,
6683    int *lengthptr)    compile_data *cd, int *lengthptr)
6684  {  {
6685  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
6686  uschar *code = *codeptr;  pcre_uchar *code = *codeptr;
6687  uschar *last_branch = code;  pcre_uchar *last_branch = code;
6688  uschar *start_bracket = code;  pcre_uchar *start_bracket = code;
6689  uschar *reverse_count = NULL;  pcre_uchar *reverse_count = NULL;
6690  open_capitem capitem;  open_capitem capitem;
6691  int capnumber = 0;  int capnumber = 0;
6692  int firstbyte, reqbyte;  int firstbyte, reqbyte;
# Line 6205  int branchfirstbyte, branchreqbyte; Line 6694  int branchfirstbyte, branchreqbyte;
6694  int length;  int length;
6695  int orig_bracount;  int orig_bracount;
6696  int max_bracount;  int max_bracount;
 int old_external_options = cd->external_options;  
6697  branch_chain bc;  branch_chain bc;
6698    
6699  bc.outer = bcptr;  bc.outer = bcptr;
# Line 6229  pre-compile phase to find out whether an Line 6717  pre-compile phase to find out whether an
6717    
6718  /* 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
6719  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
6720  detect groups that contain recursive back references to themselves. */  detect groups that contain recursive back references to themselves. Note that
6721    only OP_CBRA need be tested here; changing this opcode to one of its variants,
6722    e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
6723    
6724  if (*code == OP_CBRA)  if (*code == OP_CBRA)
6725    {    {
# Line 6255  for (;;) Line 6745  for (;;)
6745    
6746    if (reset_bracount) cd->bracount = orig_bracount;    if (reset_bracount) cd->bracount = orig_bracount;
6747    
   /* 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;  
     }  
   
6748    /* Set up dummy OP_REVERSE if lookbehind assertion */    /* Set up dummy OP_REVERSE if lookbehind assertion */
6749    
6750    if (lookbehind)    if (lookbehind)
# Line 6278  for (;;) Line 6759  for (;;)
6759    into the length. */    into the length. */
6760    
6761    if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstbyte,    if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstbyte,
6762          &branchreqbyte, &bc, cd, (lengthptr == NULL)? NULL : &length))          &branchreqbyte, &bc, cond_depth, cd,
6763            (lengthptr == NULL)? NULL : &length))
6764      {      {
6765      *ptrptr = ptr;      *ptrptr = ptr;
6766      return FALSE;      return FALSE;
6767      }      }
6768    
   /* 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;  
   
6769    /* Keep the highest bracket count in case (?| was used and some branch    /* Keep the highest bracket count in case (?| was used and some branch
6770    has fewer than the rest. */    has fewer than the rest. */
6771    
# Line 6353  for (;;) Line 6826  for (;;)
6826        {        {
6827        int fixed_length;        int fixed_length;
6828        *code = OP_END;        *code = OP_END;
6829        fixed_length = find_fixedlength(last_branch, options, FALSE, cd);        fixed_length = find_fixedlength(last_branch,  (options & PCRE_UTF8) != 0,
6830            FALSE, cd);
6831        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
6832        if (fixed_length == -3)        if (fixed_length == -3)
6833          {          {
# Line 6361  for (;;) Line 6835  for (;;)
6835          }          }
6836        else if (fixed_length < 0)        else if (fixed_length < 0)
6837          {          {
6838          *errorcodeptr = (fixed_length == -2)? ERR36 : ERR25;          *errorcodeptr = (fixed_length == -2)? ERR36 :
6839                            (fixed_length == -4)? ERR70: ERR25;
6840          *ptrptr = ptr;          *ptrptr = ptr;
6841          return FALSE;          return FALSE;
6842          }          }
# Line 6374  for (;;) Line 6849  for (;;)
6849    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
6850    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
6851    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
6852    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. */  
6853    
6854    if (*ptr != CHAR_VERTICAL_LINE)    if (*ptr != CHAR_VERTICAL_LINE)
6855      {      {
# Line 6408  for (;;) Line 6881  for (;;)
6881        if (cd->open_caps->flag)        if (cd->open_caps->flag)
6882          {          {
6883          memmove(start_bracket + 1 + LINK_SIZE, start_bracket,          memmove(start_bracket + 1 + LINK_SIZE, start_bracket,
6884            code - start_bracket);            IN_UCHARS(code - start_bracket));
6885          *start_bracket = OP_ONCE;          *start_bracket = OP_ONCE;
6886          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
6887          PUT(start_bracket, 1, (int)(code - start_bracket));          PUT(start_bracket, 1, (int)(code - start_bracket));
# Line 6420  for (;;) Line 6893  for (;;)
6893        cd->open_caps = cd->open_caps->next;        cd->open_caps = cd->open_caps->next;
6894        }        }
6895    
     /* Reset options if needed. */  
   
     if ((options & PCRE_IMS) != oldims && *ptr == CHAR_RIGHT_PARENTHESIS)  
       {  
       *code++ = OP_OPT;  
       *code++ = oldims;  
       length += 2;  
       }  
   
6896      /* Retain the highest bracket number, in case resetting was used. */      /* Retain the highest bracket number, in case resetting was used. */
6897    
6898      cd->bracount = max_bracount;      cd->bracount = max_bracount;
# Line 6488  for (;;) Line 6952  for (;;)
6952  /* 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
6953  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
6954  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
6955  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
6956  counts, since OP_CIRC can match in the middle.  be found, because ^ generates OP_CIRCM in that mode.
6957    
6958  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.
6959  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 6974  of the more common cases more precisely.
6974    
6975  Arguments:  Arguments:
6976    code           points to start of expression (the bracket)    code           points to start of expression (the bracket)
   options        points to the options setting  
6977    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
6978                    handles up to substring 31; after that we just have to take                    handles up to substring 31; after that we just have to take
6979                    the less precise approach                    the less precise approach
# Line 6520  Returns:     TRUE or FALSE Line 6983  Returns:     TRUE or FALSE
6983  */  */
6984    
6985  static BOOL  static BOOL
6986  is_anchored(register const uschar *code, int *options, unsigned int bracket_map,  is_anchored(register const pcre_uchar *code, unsigned int bracket_map,
6987    unsigned int backref_map)    unsigned int backref_map)
6988  {  {
6989  do {  do {
6990     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const pcre_uchar *scode = first_significant_code(
6991       options, PCRE_MULTILINE, FALSE);       code + PRIV(OP_lengths)[*code], FALSE);
6992     register int op = *scode;     register int op = *scode;
6993    
6994     /* Non-capturing brackets */     /* Non-capturing brackets */
6995    
6996     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
6997           op == OP_SBRA || op == OP_SBRAPOS)
6998       {       {
6999       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
7000       }       }
7001    
7002     /* Capturing brackets */     /* Capturing brackets */
7003    
7004     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
7005                op == OP_SCBRA || op == OP_SCBRAPOS)
7006       {       {
7007       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
7008       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
7009       if (!is_anchored(scode, options, new_map, backref_map)) return FALSE;       if (!is_anchored(scode, new_map, backref_map)) return FALSE;
7010       }       }
7011    
7012     /* Other brackets */     /* Other brackets */
7013    
7014     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_ONCE_NC ||
7015                op == OP_COND)
7016       {       {
7017       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
7018       }       }
7019    
7020     /* .* 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 7029  do {
7029    
7030     /* Check for explicit anchoring */     /* Check for explicit anchoring */
7031    
7032     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;  
7033     code += GET(code, 1);     code += GET(code, 1);
7034     }     }
7035  while (*code == OP_ALT);   /* Loop for each alternative */  while (*code == OP_ALT);   /* Loop for each alternative */
# Line 6596  Returns:         TRUE or FALSE Line 7060  Returns:         TRUE or FALSE
7060  */  */
7061    
7062  static BOOL  static BOOL
7063  is_startline(const uschar *code, unsigned int bracket_map,  is_startline(const pcre_uchar *code, unsigned int bracket_map,
7064    unsigned int backref_map)    unsigned int backref_map)
7065  {  {
7066  do {  do {
7067     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const pcre_uchar *scode = first_significant_code(
7068       NULL, 0, FALSE);       code + PRIV(OP_lengths)[*code], FALSE);
7069     register int op = *scode;     register int op = *scode;
7070    
7071     /* 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 6612  do { Line 7076  do {
7076     if (op == OP_COND)     if (op == OP_COND)
7077       {       {
7078       scode += 1 + LINK_SIZE;       scode += 1 + LINK_SIZE;
7079       if (*scode == OP_CALLOUT) scode += _pcre_OP_lengths[OP_CALLOUT];       if (*scode == OP_CALLOUT) scode += PRIV(OP_lengths)[OP_CALLOUT];
7080       switch (*scode)       switch (*scode)
7081         {         {
7082         case OP_CREF:         case OP_CREF:
# Line 6628  do { Line 7092  do {
7092         scode += 1 + LINK_SIZE;         scode += 1 + LINK_SIZE;
7093         break;         break;
7094         }         }
7095       scode = first_significant_code(scode, NULL, 0, FALSE);       scode = first_significant_code(scode, FALSE);
7096       op = *scode;       op = *scode;
7097       }       }
7098    
7099     /* Non-capturing brackets */     /* Non-capturing brackets */
7100    
7101     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
7102           op == OP_SBRA || op == OP_SBRAPOS)
7103       {       {
7104       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;
7105       }       }
7106    
7107     /* Capturing brackets */     /* Capturing brackets */
7108    
7109     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
7110                op == OP_SCBRA || op == OP_SCBRAPOS)
7111       {       {
7112       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
7113       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
# Line 6650  do { Line 7116  do {
7116    
7117     /* Other brackets */     /* Other brackets */
7118    
7119     else if (op == OP_ASSERT || op == OP_ONCE)     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_ONCE_NC)
7120       {       {
7121       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;
7122       }       }
# Line 6665  do { Line 7131  do {
7131    
7132     /* Check for explicit circumflex */     /* Check for explicit circumflex */
7133    
7134     else if (op != OP_CIRC) return FALSE;     else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;
7135    
7136     /* Move on to the next alternative */     /* Move on to the next alternative */
7137    
# Line 6691  we return that char, otherwise -1. Line 7157  we return that char, otherwise -1.
7157    
7158  Arguments:  Arguments:
7159    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)  
7160    inassert   TRUE if in an assertion    inassert   TRUE if in an assertion
7161    
7162  Returns:     -1 or the fixed first char  Returns:     -1 or the fixed first char
7163  */  */
7164    
7165  static int  static int
7166  find_firstassertedchar(const uschar *code, int *options, BOOL inassert)  find_firstassertedchar(const pcre_uchar *code, BOOL inassert)
7167  {  {
7168  register int c = -1;  register int c = -1;
7169  do {  do {
7170     int d;     int d;
7171     const uschar *scode =     int xl = (*code == OP_CBRA || *code == OP_SCBRA ||
7172       first_significant_code(code + 1+LINK_SIZE, options, PCRE_CASELESS, TRUE);               *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? IMM2_SIZE:0;
7173       const pcre_uchar *scode = first_significant_code(code + 1+LINK_SIZE + xl,
7174         TRUE);
7175     register int op = *scode;     register int op = *scode;
7176    
7177     switch(op)     switch(op)
# Line 6713  do { Line 7180  do {
7180       return -1;       return -1;
7181    
7182       case OP_BRA:       case OP_BRA:
7183         case OP_BRAPOS:
7184       case OP_CBRA:       case OP_CBRA:
7185         case OP_SCBRA:
7186         case OP_CBRAPOS:
7187         case OP_SCBRAPOS:
7188       case OP_ASSERT:       case OP_ASSERT:
7189       case OP_ONCE:       case OP_ONCE:
7190         case OP_ONCE_NC:
7191       case OP_COND:       case OP_COND:
7192       if ((d = find_firstassertedchar(scode, options, op == OP_ASSERT)) < 0)       if ((d = find_firstassertedchar(scode, op == OP_ASSERT)) < 0)
7193         return -1;         return -1;
7194       if (c < 0) c = d; else if (c != d) return -1;       if (c < 0) c = d; else if (c != d) return -1;
7195       break;       break;
7196    
7197       case OP_EXACT:       /* Fall through */       case OP_EXACT:
7198       scode += 2;       scode += IMM2_SIZE;
7199         /* Fall through */
7200    
7201       case OP_CHAR:       case OP_CHAR:
      case OP_CHARNC:  
7202       case OP_PLUS:       case OP_PLUS:
7203       case OP_MINPLUS:       case OP_MINPLUS:
7204       case OP_POSPLUS:       case OP_POSPLUS:
7205       if (!inassert) return -1;       if (!inassert) return -1;
7206       if (c < 0)       if (c < 0) c = scode[1];
7207         {         else if (c != scode[1]) return -1;
7208         c = scode[1];       break;
7209         if ((*options & PCRE_CASELESS) != 0) c |= REQ_CASELESS;  
7210         }       case OP_EXACTI:
7211       else if (c != scode[1]) return -1;       scode += IMM2_SIZE;
7212         /* Fall through */
7213    
7214         case OP_CHARI:
7215         case OP_PLUSI:
7216         case OP_MINPLUSI:
7217         case OP_POSPLUSI:
7218         if (!inassert) return -1;
7219         if (c < 0) c = scode[1] | REQ_CASELESS;
7220           else if (c != scode[1]) return -1;
7221       break;       break;
7222       }       }
7223    
# Line 6770  Returns:        pointer to compiled data Line 7251  Returns:        pointer to compiled data
7251                  with errorptr and erroroffset set                  with errorptr and erroroffset set
7252  */  */
7253    
7254    #ifdef COMPILE_PCRE8
7255  PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION  PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
7256  pcre_compile(const char *pattern, int options, const char **errorptr,  pcre_compile(const char *pattern, int options, const char **errorptr,
7257    int *erroroffset, const unsigned char *tables)    int *erroroffset, const unsigned char *tables)
7258    #else
7259    PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
7260    pcre16_compile(PCRE_SPTR16 pattern, int options, const char **errorptr,
7261      int *erroroffset, const unsigned char *tables)
7262    #endif
7263  {  {
7264    #ifdef COMPILE_PCRE8
7265  return pcre_compile2(pattern, options, NULL, errorptr, erroroffset, tables);  return pcre_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
7266    #else
7267    return pcre16_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
7268    #endif
7269  }  }
7270    
7271    
7272    #ifdef COMPILE_PCRE8
7273  PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION  PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
7274  pcre_compile2(const char *pattern, int options, int *errorcodeptr,  pcre_compile2(const char *pattern, int options, int *errorcodeptr,
7275    const char **errorptr, int *erroroffset, const unsigned char *tables)    const char **errorptr, int *erroroffset, const unsigned char *tables)
7276    #else
7277    PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
7278    pcre16_compile2(PCRE_SPTR16 pattern, int options, int *errorcodeptr,
7279      const char **errorptr, int *erroroffset, const unsigned char *tables)
7280    #endif
7281  {  {
7282  real_pcre *re;  real_pcre *re;
7283  int length = 1;  /* For final END opcode */  int length = 1;  /* For final END opcode */
# Line 6789  int errorcode = 0; Line 7286  int errorcode = 0;
7286  int skipatstart = 0;  int skipatstart = 0;
7287  BOOL utf8;  BOOL utf8;
7288  size_t size;  size_t size;
7289  uschar *code;  pcre_uchar *code;
7290  const uschar *codestart;  const pcre_uchar *codestart;
7291  const uschar *ptr;  const pcre_uchar *ptr;
7292  compile_data compile_block;  compile_data compile_block;
7293  compile_data *cd = &compile_block;  compile_data *cd = &compile_block;
7294    
# Line 6801  as soon as possible, so that a fairly la Line 7298  as soon as possible, so that a fairly la
7298  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
7299  to fill in forward references to subpatterns. */  to fill in forward references to subpatterns. */
7300    
7301  uschar cworkspace[COMPILE_WORK_SIZE];  pcre_uchar cworkspace[COMPILE_WORK_SIZE];
7302    
7303  /* Set this early so that early errors get offset 0. */  /* Set this early so that early errors get offset 0. */
7304    
7305  ptr = (const uschar *)pattern;  ptr = (const pcre_uchar *)pattern;
7306    
7307  /* 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
7308  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 6832  if (erroroffset == NULL) Line 7329  if (erroroffset == NULL)
7329    
7330  /* Set up pointers to the individual character tables */  /* Set up pointers to the individual character tables */
7331    
7332  if (tables == NULL) tables = _pcre_default_tables;  if (tables == NULL) tables = PRIV(default_tables);
7333  cd->lcc = tables + lcc_offset;  cd->lcc = tables + lcc_offset;
7334  cd->fcc = tables + fcc_offset;  cd->fcc = tables + fcc_offset;
7335  cd->cbits = tables + cbits_offset;  cd->cbits = tables + cbits_offset;
# Line 6855  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 7352  while (ptr[skipatstart] == CHAR_LEFT_PAR
7352    int newnl = 0;    int newnl = 0;
7353    int newbsr = 0;    int newbsr = 0;
7354    
7355    if (strncmp((char *)(ptr+skipatstart+2), STRING_UTF8_RIGHTPAR, 5) == 0)    if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF8_RIGHTPAR, 5) == 0)
7356      { skipatstart += 7; options |= PCRE_UTF8; continue; }      { skipatstart += 7; options |= PCRE_UTF8; continue; }
7357    else if (strncmp((char *)(ptr+skipatstart+2), STRING_UCP_RIGHTPAR, 4) == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UCP_RIGHTPAR, 4) == 0)
7358      { skipatstart += 6; options |= PCRE_UCP; continue; }      { skipatstart += 6; options |= PCRE_UCP; continue; }
7359      else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_START_OPT_RIGHTPAR, 13) == 0)
7360        { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }
7361    
7362    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)    if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CR_RIGHTPAR, 3) == 0)
7363      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
7364    else if (strncmp((char *)(ptr+skipatstart+2), STRING_LF_RIGHTPAR, 3)  == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LF_RIGHTPAR, 3)  == 0)
7365      { skipatstart += 5; newnl = PCRE_NEWLINE_LF; }      { skipatstart += 5; newnl = PCRE_NEWLINE_LF; }
7366    else if (strncmp((char *)(ptr+skipatstart+2), STRING_CRLF_RIGHTPAR, 5)  == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CRLF_RIGHTPAR, 5)  == 0)
7367      { skipatstart += 7; newnl = PCRE_NEWLINE_CR + PCRE_NEWLINE_LF; }      { skipatstart += 7; newnl = PCRE_NEWLINE_CR + PCRE_NEWLINE_LF; }
7368    else if (strncmp((char *)(ptr+skipatstart+2), STRING_ANY_RIGHTPAR, 4) == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANY_RIGHTPAR, 4) == 0)
7369      { skipatstart += 6; newnl = PCRE_NEWLINE_ANY; }      { skipatstart += 6; newnl = PCRE_NEWLINE_ANY; }
7370    else if (strncmp((char *)(ptr+skipatstart+2), STRING_ANYCRLF_RIGHTPAR, 8) == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANYCRLF_RIGHTPAR, 8) == 0)
7371      { skipatstart += 10; newnl = PCRE_NEWLINE_ANYCRLF; }      { skipatstart += 10; newnl = PCRE_NEWLINE_ANYCRLF; }
7372    
7373    else if (strncmp((char *)(ptr+skipatstart+2), STRING_BSR_ANYCRLF_RIGHTPAR, 12) == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_ANYCRLF_RIGHTPAR, 12) == 0)
7374      { skipatstart += 14; newbsr = PCRE_BSR_ANYCRLF; }      { skipatstart += 14; newbsr = PCRE_BSR_ANYCRLF; }
7375    else if (strncmp((char *)(ptr+skipatstart+2), STRING_BSR_UNICODE_RIGHTPAR, 12) == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_UNICODE_RIGHTPAR, 12) == 0)
7376      { skipatstart += 14; newbsr = PCRE_BSR_UNICODE; }      { skipatstart += 14; newbsr = PCRE_BSR_UNICODE; }
7377    
7378    if (newnl != 0)    if (newnl != 0)
# Line 6885  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 7384  while (ptr[skipatstart] == CHAR_LEFT_PAR
7384    
7385  utf8 = (options & PCRE_UTF8) != 0;  utf8 = (options & PCRE_UTF8) != 0;
7386    
7387  /* 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
7388    return of an error code from PRIV(valid_utf8)() is a new feature, introduced in
7389    release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
7390    not used here. */
7391    
7392  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
7393  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&
7394       (*erroroffset = _pcre_valid_utf8((USPTR)pattern, -1)) >= 0)       (errorcode = PRIV(valid_utf8)((PCRE_PUCHAR)pattern, -1, erroroffset)) != 0)
7395    {    {
7396    errorcode = ERR44;    errorcode = ERR44;
7397    goto PCRE_EARLY_ERROR_RETURN2;    goto PCRE_EARLY_ERROR_RETURN2;
# Line 6914  if ((options & PCRE_UCP) != 0) Line 7416  if ((options & PCRE_UCP) != 0)
7416    
7417  /* Check validity of \R options. */  /* Check validity of \R options. */
7418    
7419  switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))  if ((options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) ==
7420         (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
7421    {    {
7422    case 0:    errorcode = ERR56;
7423    case PCRE_BSR_ANYCRLF:    goto PCRE_EARLY_ERROR_RETURN;
   case PCRE_BSR_UNICODE:  
   break;  
   default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN;  
7424    }    }
7425    
7426  /* 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 6973  cd->backref_map = 0; Line 7473  cd->backref_map = 0;
7473  /* Reflect pattern for debugging output */  /* Reflect pattern for debugging output */
7474    
7475  DPRINTF(("------------------------------------------------------------------\n"));  DPRINTF(("------------------------------------------------------------------\n"));
7476  DPRINTF(("%s\n", pattern));  #ifdef PCRE_DEBUG
7477    print_puchar(stdout, (PCRE_PUCHAR)pattern);
7478    #endif
7479    DPRINTF(("\n"));
7480    
7481  /* Pretend to compile the pattern while actually just accumulating the length  /* Pretend to compile the pattern while actually just accumulating the length
7482  of memory required. This behaviour is triggered by passing a non-NULL final  of memory required. This behaviour is triggered by passing a non-NULL final
# Line 6989  cd->name_table = NULL; Line 7492  cd->name_table = NULL;
7492  cd->start_workspace = cworkspace;  cd->start_workspace = cworkspace;
7493  cd->start_code = cworkspace;  cd->start_code = cworkspace;
7494  cd->hwm = cworkspace;  cd->hwm = cworkspace;
7495  cd->start_pattern = (const uschar *)pattern;  cd->start_pattern = (const pcre_uchar *)pattern;
7496  cd->end_pattern = (const uschar *)(pattern + strlen(pattern));  cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));
7497  cd->req_varyopt = 0;  cd->req_varyopt = 0;
7498  cd->external_options = options;  cd->external_options = options;
7499  cd->external_flags = 0;  cd->external_flags = 0;
# Line 7005  outside can help speed up starting point Line 7508  outside can help speed up starting point
7508  ptr += skipatstart;  ptr += skipatstart;
7509  code = cworkspace;  code = cworkspace;
7510  *code = OP_BRA;  *code = OP_BRA;
7511  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,  (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,
7512    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,    FALSE, 0, 0, &firstbyte, &reqbyte, NULL, cd, &length);
   &length);  
7513  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
7514    
7515  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
7516    cd->hwm - cworkspace));    (int)(cd->hwm - cworkspace)));
7517    
7518  if (length > MAX_PATTERN_SIZE)  if (length > MAX_PATTERN_SIZE)
7519    {    {
# Line 7024  externally provided function. Integer ov Line 7526  externally provided function. Integer ov
7526  because nowadays we limit the maximum value of cd->names_found and  because nowadays we limit the maximum value of cd->names_found and
7527  cd->name_entry_size. */  cd->name_entry_size. */
7528    
7529  size = length + sizeof(real_pcre) + cd->names_found * (cd->name_entry_size + 3);  size = sizeof(real_pcre) + (length + cd->names_found * (cd->name_entry_size + 3)) * sizeof(pcre_uchar);
7530  re = (real_pcre *)(pcre_malloc)(size);  re = (real_pcre *)(pcre_malloc)(size);
7531    
7532  if (re == NULL)  if (re == NULL)
# Line 7046  re->flags = cd->external_flags; Line 7548  re->flags = cd->external_flags;
7548  re->dummy1 = 0;  re->dummy1 = 0;
7549  re->first_byte = 0;  re->first_byte = 0;
7550  re->req_byte = 0;  re->req_byte = 0;
7551  re->name_table_offset = sizeof(real_pcre);  re->name_table_offset = sizeof(real_pcre) / sizeof(pcre_uchar);
7552  re->name_entry_size = cd->name_entry_size;  re->name_entry_size = cd->name_entry_size;
7553  re->name_count = cd->names_found;  re->name_count = cd->names_found;
7554  re->ref_count = 0;  re->ref_count = 0;
7555  re->tables = (tables == _pcre_default_tables)? NULL : tables;  re->tables = (tables == PRIV(default_tables))? NULL : tables;
7556  re->nullpad = NULL;  re->nullpad = NULL;
7557    
7558  /* The starting points of the name/number translation table and of the code are  /* The starting points of the name/number translation table and of the code are
# Line 7061  field; this time it's used for rememberi Line 7563  field; this time it's used for rememberi
7563  */  */
7564    
7565  cd->final_bracount = cd->bracount;  /* Save for checking forward references */  cd->final_bracount = cd->bracount;  /* Save for checking forward references */
7566    cd->assert_depth = 0;
7567  cd->bracount = 0;  cd->bracount = 0;