/[pcre]/code/trunk/pcre_compile.c
ViewVC logotype

Diff of /code/trunk/pcre_compile.c

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

revision 505 by ph10, Tue Mar 9 16:50:47 2010 UTC revision 624 by ph10, Tue Jul 19 10:43:28 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 92  is 4 there is plenty of room. */ Line 92  is 4 there is plenty of room. */
92    
93  #define COMPILE_WORK_SIZE (4096)  #define COMPILE_WORK_SIZE (4096)
94    
95  /* The overrun tests check for a slightly smaller size so that they detect the  /* The overrun tests check for a slightly smaller size so that they detect the
96  overrun before it actually does run off the end of the data block. */  overrun before it actually does run off the end of the data block. */
97    
98  #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)  #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)
# Line 124  static const short int escapes[] = { Line 124  static const short int escapes[] = {
124       -ESC_H,                  0,       -ESC_H,                  0,
125       0,                       -ESC_K,       0,                       -ESC_K,
126       0,                       0,       0,                       0,
127       0,                       0,       -ESC_N,                  0,
128       -ESC_P,                  -ESC_Q,       -ESC_P,                  -ESC_Q,
129       -ESC_R,                  -ESC_S,       -ESC_R,                  -ESC_S,
130       0,                       0,       0,                       0,
# Line 171  static const short int escapes[] = { Line 171  static const short int escapes[] = {
171  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',
172  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,
173  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,
174  /*  D0 */   '}',     0, -ESC_K,       0,      0,     0,      0, -ESC_P,  /*  D0 */   '}',     0, -ESC_K,       0,      0,-ESC_N,      0, -ESC_P,
175  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,
176  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,
177  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,
# Line 188  string is built from string macros so th Line 188  string is built from string macros so th
188  platforms. */  platforms. */
189    
190  typedef struct verbitem {  typedef struct verbitem {
191    int   len;    int   len;                 /* Length of verb name */
192    int   op;    int   op;                  /* Op when no arg, or -1 if arg mandatory */
193      int   op_arg;              /* Op when arg present, or -1 if not allowed */
194  } verbitem;  } verbitem;
195    
196  static const char verbnames[] =  static const char verbnames[] =
197      "\0"                       /* Empty name is a shorthand for MARK */
198      STRING_MARK0
199    STRING_ACCEPT0    STRING_ACCEPT0
200    STRING_COMMIT0    STRING_COMMIT0
201    STRING_F0    STRING_F0
# Line 202  static const char verbnames[] = Line 205  static const char verbnames[] =
205    STRING_THEN;    STRING_THEN;
206    
207  static const verbitem verbs[] = {  static const verbitem verbs[] = {
208    { 6, OP_ACCEPT },    { 0, -1,        OP_MARK },
209    { 6, OP_COMMIT },    { 4, -1,        OP_MARK },
210    { 1, OP_FAIL },    { 6, OP_ACCEPT, -1 },
211    { 4, OP_FAIL },    { 6, OP_COMMIT, -1 },
212    { 5, OP_PRUNE },    { 1, OP_FAIL,   -1 },
213    { 4, OP_SKIP  },    { 4, OP_FAIL,   -1 },
214    { 4, OP_THEN  }    { 5, OP_PRUNE,  OP_PRUNE_ARG },
215      { 4, OP_SKIP,   OP_SKIP_ARG  },
216      { 4, OP_THEN,   OP_THEN_ARG  }
217  };  };
218    
219  static const int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
# Line 256  static const int posix_class_maps[] = { Line 261  static const int posix_class_maps[] = {
261    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
262  };  };
263    
264    /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
265    substitutes must be in the order of the names, defined above, and there are
266    both positive and negative cases. NULL means no substitute. */
267    
268    #ifdef SUPPORT_UCP
269    static const uschar *substitutes[] = {
270      (uschar *)"\\P{Nd}",    /* \D */
271      (uschar *)"\\p{Nd}",    /* \d */
272      (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */
273      (uschar *)"\\p{Xsp}",   /* \s */
274      (uschar *)"\\P{Xwd}",   /* \W */
275      (uschar *)"\\p{Xwd}"    /* \w */
276    };
277    
278    static const uschar *posix_substitutes[] = {
279      (uschar *)"\\p{L}",     /* alpha */
280      (uschar *)"\\p{Ll}",    /* lower */
281      (uschar *)"\\p{Lu}",    /* upper */
282      (uschar *)"\\p{Xan}",   /* alnum */
283      NULL,                   /* ascii */
284      (uschar *)"\\h",        /* blank */
285      NULL,                   /* cntrl */
286      (uschar *)"\\p{Nd}",    /* digit */
287      NULL,                   /* graph */
288      NULL,                   /* print */
289      NULL,                   /* punct */
290      (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */
291      (uschar *)"\\p{Xwd}",   /* word */
292      NULL,                   /* xdigit */
293      /* Negated cases */
294      (uschar *)"\\P{L}",     /* ^alpha */
295      (uschar *)"\\P{Ll}",    /* ^lower */
296      (uschar *)"\\P{Lu}",    /* ^upper */
297      (uschar *)"\\P{Xan}",   /* ^alnum */
298      NULL,                   /* ^ascii */
299      (uschar *)"\\H",        /* ^blank */
300      NULL,                   /* ^cntrl */
301      (uschar *)"\\P{Nd}",    /* ^digit */
302      NULL,                   /* ^graph */
303      NULL,                   /* ^print */
304      NULL,                   /* ^punct */
305      (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */
306      (uschar *)"\\P{Xwd}",   /* ^word */
307      NULL                    /* ^xdigit */
308    };
309    #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))
310    #endif
311    
312  #define STRING(a)  # a  #define STRING(a)  # a
313  #define XSTRING(s) STRING(s)  #define XSTRING(s) STRING(s)
# Line 268  the number of relocations needed when a Line 320  the number of relocations needed when a
320  it is now one long string. We cannot use a table of offsets, because the  it is now one long string. We cannot use a table of offsets, because the
321  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
322  simply count through to the one we want - this isn't a performance issue  simply count through to the one we want - this isn't a performance issue
323  because these strings are used only when there is a compilation error.  because these strings are used only when there is a compilation error.
324    
325  Each substring ends with \0 to insert a null character. This includes the final  Each substring ends with \0 to insert a null character. This includes the final
326  substring, so that the whole string ends with \0\0, which can be detected when  substring, so that the whole string ends with \0\0, which can be detected when
327  counting through. */  counting through. */
328    
329  static const char error_texts[] =  static const char error_texts[] =
# Line 319  static const char error_texts[] = Line 371  static const char error_texts[] =
371    /* 35 */    /* 35 */
372    "invalid condition (?(0)\0"    "invalid condition (?(0)\0"
373    "\\C not allowed in lookbehind assertion\0"    "\\C not allowed in lookbehind assertion\0"
374    "PCRE does not support \\L, \\l, \\N, \\U, or \\u\0"    "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
375    "number after (?C is > 255\0"    "number after (?C is > 255\0"
376    "closing ) for (?C expected\0"    "closing ) for (?C expected\0"
377    /* 40 */    /* 40 */
# Line 345  static const char error_texts[] = Line 397  static const char error_texts[] =
397    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
398    "\\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"
399    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
400    "(*VERB) with an argument is not supported\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
401    /* 60 */    /* 60 */
402    "(*VERB) not recognized\0"    "(*VERB) not recognized\0"
403    "number is too big\0"    "number is too big\0"
# Line 353  static const char error_texts[] = Line 405  static const char error_texts[] =
405    "digit expected after (?+\0"    "digit expected after (?+\0"
406    "] is an invalid data character in JavaScript compatibility mode\0"    "] is an invalid data character in JavaScript compatibility mode\0"
407    /* 65 */    /* 65 */
408    "different names for subpatterns of the same number are not allowed\0";    "different names for subpatterns of the same number are not allowed\0"
409      "(*MARK) must have an argument\0"
410      "this version of PCRE is not compiled with PCRE_UCP support\0"
411      "\\c must be followed by an ASCII character\0"
412      ;
413    
414  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
415  patterns. Note that the tables in chartables are dependent on the locale, and  patterns. Note that the tables in chartables are dependent on the locale, and
# Line 489  static const unsigned char ebcdic_charta Line 545  static const unsigned char ebcdic_charta
545  /* Definition to allow mutual recursion */  /* Definition to allow mutual recursion */
546    
547  static BOOL  static BOOL
548    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,    compile_regex(int, uschar **, const uschar **, int *, BOOL, BOOL, int, int *,
549      int *, int *, branch_chain *, compile_data *, int *);      int *, branch_chain *, compile_data *, int *);
550    
551    
552    
# Line 511  static const char * Line 567  static const char *
567  find_error_text(int n)  find_error_text(int n)
568  {  {
569  const char *s = error_texts;  const char *s = error_texts;
570  for (; n > 0; n--)  for (; n > 0; n--)
571    {    {
572    while (*s++ != 0) {};    while (*s++ != 0) {};
573    if (*s == 0) return "Error text not found (please report)";    if (*s == 0) return "Error text not found (please report)";
574    }    }
575  return s;  return s;
576  }  }
577    
# Line 586  else Line 642  else
642    
643      case CHAR_l:      case CHAR_l:
644      case CHAR_L:      case CHAR_L:
     case CHAR_N:  
645      case CHAR_u:      case CHAR_u:
646      case CHAR_U:      case CHAR_U:
647      *errorcodeptr = ERR37;      *errorcodeptr = ERR37;
# Line 787  else Line 842  else
842      break;      break;
843    
844      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
845      This coding is ASCII-specific, but then the whole concept of \cx is      An error is given if the byte following \c is not an ASCII character. This
846        coding is ASCII-specific, but then the whole concept of \cx is
847      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
848    
849      case CHAR_c:      case CHAR_c:
# Line 797  else Line 853  else
853        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
854        break;        break;
855        }        }
856    #ifndef EBCDIC    /* ASCII/UTF-8 coding */
857  #ifndef EBCDIC  /* ASCII/UTF-8 coding */      if (c > 127)  /* Excludes all non-ASCII in either mode */
858          {
859          *errorcodeptr = ERR68;
860          break;
861          }
862      if (c >= CHAR_a && c <= CHAR_z) c -= 32;      if (c >= CHAR_a && c <= CHAR_z) c -= 32;
863      c ^= 0x40;      c ^= 0x40;
864  #else           /* EBCDIC coding */  #else             /* EBCDIC coding */
865      if (c >= CHAR_a && c <= CHAR_z) c += 64;      if (c >= CHAR_a && c <= CHAR_z) c += 64;
866      c ^= 0xC0;      c ^= 0xC0;
867  #endif  #endif
# Line 824  else Line 884  else
884      }      }
885    }    }
886    
887    /* Perl supports \N{name} for character names, as well as plain \N for "not
888    newline". PCRE does not support \N{name}. */
889    
890    if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET)
891      *errorcodeptr = ERR37;
892    
893    /* If PCRE_UCP is set, we change the values for \d etc. */
894    
895    if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)
896      c -= (ESC_DU - ESC_D);
897    
898    /* Set the pointer to the final character before returning. */
899    
900  *ptrptr = ptr;  *ptrptr = ptr;
901  return c;  return c;
902  }  }
# Line 1032  top-level call starts at the beginning o Line 1105  top-level call starts at the beginning o
1105  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
1106  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
1107  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
1108  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
1109  encountered, the name will be terminated by '>' because that is checked in the  track of subpatterns that reset the capturing group numbers - the (?| feature.
1110  first pass. Recursion is used to keep track of subpatterns that reset the  
1111  capturing group numbers - the (?| feature.  This function was originally called only from the second pass, in which we know
1112    that if (?< or (?' or (?P< is encountered, the name will be correctly
1113    terminated because that is checked in the first pass. There is now one call to
1114    this function in the first pass, to check for a recursive back reference by
1115    name (so that we can make the whole group atomic). In this case, we need check
1116    only up to the current position in the pattern, and that is still OK because
1117    and previous occurrences will have been checked. To make this work, the test
1118    for "end of pattern" is a check against cd->end_pattern in the main loop,
1119    instead of looking for a binary zero. This means that the special first-pass
1120    call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1121    processing items within the loop are OK, because afterwards the main loop will
1122    terminate.)
1123    
1124  Arguments:  Arguments:
1125    ptrptr       address of the current character pointer (updated)    ptrptr       address of the current character pointer (updated)
# Line 1043  Arguments: Line 1127  Arguments:
1127    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1128    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1129    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1130      utf8         TRUE if we are in UTF-8 mode
1131    count        pointer to the current capturing subpattern number (updated)    count        pointer to the current capturing subpattern number (updated)
1132    
1133  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
# Line 1050  Returns:       the number of the named s Line 1135  Returns:       the number of the named s
1135    
1136  static int  static int
1137  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,
1138    BOOL xmode, int *count)    BOOL xmode, BOOL utf8, int *count)
1139  {  {
1140  uschar *ptr = *ptrptr;  uschar *ptr = *ptrptr;
1141  int start_count = *count;  int start_count = *count;
# Line 1062  dealing with. The very first call may no Line 1147  dealing with. The very first call may no
1147    
1148  if (ptr[0] == CHAR_LEFT_PARENTHESIS)  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1149    {    {
1150    if (ptr[1] == CHAR_QUESTION_MARK &&    /* Handle specials such as (*SKIP) or (*UTF8) etc. */
1151        ptr[2] == CHAR_VERTICAL_LINE)  
1152      if (ptr[1] == CHAR_ASTERISK) ptr += 2;
1153    
1154      /* Handle a normal, unnamed capturing parenthesis. */
1155    
1156      else if (ptr[1] != CHAR_QUESTION_MARK)
1157        {
1158        *count += 1;
1159        if (name == NULL && *count == lorn) return *count;
1160        ptr++;
1161        }
1162    
1163      /* All cases now have (? at the start. Remember when we are in a group
1164      where the parenthesis numbers are duplicated. */
1165    
1166      else if (ptr[2] == CHAR_VERTICAL_LINE)
1167      {      {
1168      ptr += 3;      ptr += 3;
1169      dup_parens = TRUE;      dup_parens = TRUE;
1170      }      }
1171    
1172    /* Handle a normal, unnamed capturing parenthesis */    /* Handle comments; all characters are allowed until a ket is reached. */
1173    
1174    else if (ptr[1] != CHAR_QUESTION_MARK && ptr[1] != CHAR_ASTERISK)    else if (ptr[2] == CHAR_NUMBER_SIGN)
1175      {      {
1176      *count += 1;      for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;
1177      if (name == NULL && *count == lorn) return *count;      goto FAIL_EXIT;
     ptr++;  
1178      }      }
1179    
1180    /* Handle a condition. If it is an assertion, just carry on so that it    /* Handle a condition. If it is an assertion, just carry on so that it
1181    is processed as normal. If not, skip to the closing parenthesis of the    is processed as normal. If not, skip to the closing parenthesis of the
1182    condition (there can't be any nested parens. */    condition (there can't be any nested parens). */
1183    
1184    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
1185      {      {
# Line 1092  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1191  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1191        }        }
1192      }      }
1193    
1194    /* We have either (? or (* and not a condition */    /* Start with (? but not a condition. */
1195    
1196    else    else
1197      {      {
# Line 1121  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1220  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1220    }    }
1221    
1222  /* Past any initial parenthesis handling, scan for parentheses or vertical  /* Past any initial parenthesis handling, scan for parentheses or vertical
1223  bars. */  bars. Stop if we get to cd->end_pattern. Note that this is important for the
1224    first-pass call when this value is temporarily adjusted to stop at the current
1225    position. So DO NOT change this to a test for binary zero. */
1226    
1227  for (; *ptr != 0; ptr++)  for (; ptr < cd->end_pattern; ptr++)
1228    {    {
1229    /* Skip over backslashed characters and also entire \Q...\E */    /* Skip over backslashed characters and also entire \Q...\E */
1230    
# Line 1197  for (; *ptr != 0; ptr++) Line 1298  for (; *ptr != 0; ptr++)
1298    
1299    if (xmode && *ptr == CHAR_NUMBER_SIGN)    if (xmode && *ptr == CHAR_NUMBER_SIGN)
1300      {      {
1301      while (*(++ptr) != 0 && *ptr != CHAR_NL) {};      ptr++;
1302        while (*ptr != 0)
1303          {
1304          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1305          ptr++;
1306    #ifdef SUPPORT_UTF8
1307          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
1308    #endif
1309          }
1310      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1311      continue;      continue;
1312      }      }
# Line 1206  for (; *ptr != 0; ptr++) Line 1315  for (; *ptr != 0; ptr++)
1315    
1316    if (*ptr == CHAR_LEFT_PARENTHESIS)    if (*ptr == CHAR_LEFT_PARENTHESIS)
1317      {      {
1318      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, count);      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);
1319      if (rc > 0) return rc;      if (rc > 0) return rc;
1320      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1321      }      }
# Line 1214  for (; *ptr != 0; ptr++) Line 1323  for (; *ptr != 0; ptr++)
1323    else if (*ptr == CHAR_RIGHT_PARENTHESIS)    else if (*ptr == CHAR_RIGHT_PARENTHESIS)
1324      {      {
1325      if (dup_parens && *count < hwm_count) *count = hwm_count;      if (dup_parens && *count < hwm_count) *count = hwm_count;
1326      *ptrptr = ptr;      goto FAIL_EXIT;
     return -1;  
1327      }      }
1328    
1329    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
# Line 1253  Arguments: Line 1361  Arguments:
1361    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1362    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1363    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1364      utf8         TRUE if we are in UTF-8 mode
1365    
1366  Returns:       the number of the found subpattern, or -1 if not found  Returns:       the number of the found subpattern, or -1 if not found
1367  */  */
1368    
1369  static int  static int
1370  find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode)  find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode,
1371      BOOL utf8)
1372  {  {
1373  uschar *ptr = (uschar *)cd->start_pattern;  uschar *ptr = (uschar *)cd->start_pattern;
1374  int count = 0;  int count = 0;
# Line 1271  matching closing parens. That is why we Line 1381  matching closing parens. That is why we
1381    
1382  for (;;)  for (;;)
1383    {    {
1384    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, &count);    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);
1385    if (rc > 0 || *ptr++ == 0) break;    if (rc > 0 || *ptr++ == 0) break;
1386    }    }
1387    
# Line 1287  return rc; Line 1397  return rc;
1397    
1398  /* This is called by several functions that scan a compiled expression looking  /* This is called by several functions that scan a compiled expression looking
1399  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
1400  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
1401  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
1402  assertions, and also the \b assertion; for others it does not.  does not.
1403    
1404  Arguments:  Arguments:
1405    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  
1406    skipassert   TRUE if certain assertions are to be skipped    skipassert   TRUE if certain assertions are to be skipped
1407    
1408  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1409  */  */
1410    
1411  static const uschar*  static const uschar*
1412  first_significant_code(const uschar *code, int *options, int optbit,  first_significant_code(const uschar *code, BOOL skipassert)
   BOOL skipassert)  
1413  {  {
1414  for (;;)  for (;;)
1415    {    {
1416    switch ((int)*code)    switch ((int)*code)
1417      {      {
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
1418      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1419      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1420      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
# Line 1364  and doing the check at the end; a flag s Line 1464  and doing the check at the end; a flag s
1464    
1465  Arguments:  Arguments:
1466    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1467    options  the compiling options    utf8     TRUE in UTF-8 mode
1468    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1469    cd       the "compile data" structure    cd       the "compile data" structure
1470    
# Line 1375  Returns:   the fixed length, Line 1475  Returns:   the fixed length,
1475  */  */
1476    
1477  static int  static int
1478  find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)  find_fixedlength(uschar *code, BOOL utf8, BOOL atend, compile_data *cd)
1479  {  {
1480  int length = -1;  int length = -1;
1481    
# Line 1392  for (;;) Line 1492  for (;;)
1492    register int op = *cc;    register int op = *cc;
1493    switch (op)    switch (op)
1494      {      {
1495        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1496        OP_BRA (normal non-capturing bracket) because the other variants of these
1497        opcodes are all concerned with unlimited repeated groups, which of course
1498        are not of fixed length. They will cause a -1 response from the default
1499        case of this switch. */
1500    
1501      case OP_CBRA:      case OP_CBRA:
1502      case OP_BRA:      case OP_BRA:
1503      case OP_ONCE:      case OP_ONCE:
1504      case OP_COND:      case OP_COND:
1505      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), utf8, atend, cd);
1506      if (d < 0) return d;      if (d < 0) return d;
1507      branchlength += d;      branchlength += d;
1508      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 1405  for (;;) Line 1511  for (;;)
1511    
1512      /* 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
1513      call. If it's ALT it is an alternation in a nested call. If it is      call. If it's ALT it is an alternation in a nested call. If it is
1514      END it's the end of the outer call. All can be handled by the same code. */      END it's the end of the outer call. All can be handled by the same code.
1515        Note that we must not include the OP_KETRxxx opcodes here, because they
1516        all imply an unlimited repeat. */
1517    
1518      case OP_ALT:      case OP_ALT:
1519      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1520      case OP_END:      case OP_END:
1521      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1522        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
# Line 1428  for (;;) Line 1534  for (;;)
1534      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1535      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */
1536      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                /* Recursion */
1537      d = find_fixedlength(cs + 2, options, atend, cd);      d = find_fixedlength(cs + 2, utf8, atend, cd);
1538      if (d < 0) return d;      if (d < 0) return d;
1539      branchlength += d;      branchlength += d;
1540      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1451  for (;;) Line 1557  for (;;)
1557      case OP_RREF:      case OP_RREF:
1558      case OP_NRREF:      case OP_NRREF:
1559      case OP_DEF:      case OP_DEF:
     case OP_OPT:  
1560      case OP_CALLOUT:      case OP_CALLOUT:
1561      case OP_SOD:      case OP_SOD:
1562      case OP_SOM:      case OP_SOM:
# Line 1459  for (;;) Line 1564  for (;;)
1564      case OP_EOD:      case OP_EOD:
1565      case OP_EODN:      case OP_EODN:
1566      case OP_CIRC:      case OP_CIRC:
1567        case OP_CIRCM:
1568      case OP_DOLL:      case OP_DOLL:
1569        case OP_DOLLM:
1570      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1571      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1572      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
# Line 1468  for (;;) Line 1575  for (;;)
1575      /* Handle literal characters */      /* Handle literal characters */
1576    
1577      case OP_CHAR:      case OP_CHAR:
1578      case OP_CHARNC:      case OP_CHARI:
1579      case OP_NOT:      case OP_NOT:
1580        case OP_NOTI:
1581      branchlength++;      branchlength++;
1582      cc += 2;      cc += 2;
1583  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1584      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1585  #endif  #endif
1586      break;      break;
1587    
# Line 1485  for (;;) Line 1592  for (;;)
1592      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1593      cc += 4;      cc += 4;
1594  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1595      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1596  #endif  #endif
1597      break;      break;
1598    
# Line 1588  _pcre_find_bracket(const uschar *code, B Line 1694  _pcre_find_bracket(const uschar *code, B
1694  for (;;)  for (;;)
1695    {    {
1696    register int c = *code;    register int c = *code;
1697    
1698    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1699    
1700    /* 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 1606  for (;;) Line 1713  for (;;)
1713    
1714    /* Handle capturing bracket */    /* Handle capturing bracket */
1715    
1716    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1717               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1718      {      {
1719      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
1720      if (n == number) return (uschar *)code;      if (n == number) return (uschar *)code;
# Line 1615  for (;;) Line 1723  for (;;)
1723    
1724    /* 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
1725    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
1726    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1727      must add in its length. */
1728    
1729    else    else
1730      {      {
# Line 1639  for (;;) Line 1748  for (;;)
1748        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1749        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1750        break;        break;
1751    
1752          case OP_MARK:
1753          case OP_PRUNE_ARG:
1754          case OP_SKIP_ARG:
1755          code += code[1];
1756          break;
1757    
1758          case OP_THEN_ARG:
1759          code += code[1+LINK_SIZE];
1760          break;
1761        }        }
1762    
1763      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1653  for (;;) Line 1772  for (;;)
1772      if (utf8) switch(c)      if (utf8) switch(c)
1773        {        {
1774        case OP_CHAR:        case OP_CHAR:
1775        case OP_CHARNC:        case OP_CHARI:
1776        case OP_EXACT:        case OP_EXACT:
1777          case OP_EXACTI:
1778        case OP_UPTO:        case OP_UPTO:
1779          case OP_UPTOI:
1780        case OP_MINUPTO:        case OP_MINUPTO:
1781          case OP_MINUPTOI:
1782        case OP_POSUPTO:        case OP_POSUPTO:
1783          case OP_POSUPTOI:
1784        case OP_STAR:        case OP_STAR:
1785          case OP_STARI:
1786        case OP_MINSTAR:        case OP_MINSTAR:
1787          case OP_MINSTARI:
1788        case OP_POSSTAR:        case OP_POSSTAR:
1789          case OP_POSSTARI:
1790        case OP_PLUS:        case OP_PLUS:
1791          case OP_PLUSI:
1792        case OP_MINPLUS:        case OP_MINPLUS:
1793          case OP_MINPLUSI:
1794        case OP_POSPLUS:        case OP_POSPLUS:
1795          case OP_POSPLUSI:
1796        case OP_QUERY:        case OP_QUERY:
1797          case OP_QUERYI:
1798        case OP_MINQUERY:        case OP_MINQUERY:
1799          case OP_MINQUERYI:
1800        case OP_POSQUERY:        case OP_POSQUERY:
1801          case OP_POSQUERYI:
1802        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1803        break;        break;
1804        }        }
# Line 1710  for (;;) Line 1842  for (;;)
1842    
1843    /* 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
1844    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
1845    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1846      must add in its length. */
1847    
1848    else    else
1849      {      {
# Line 1734  for (;;) Line 1867  for (;;)
1867        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1868        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1869        break;        break;
1870    
1871          case OP_MARK:
1872          case OP_PRUNE_ARG:
1873          case OP_SKIP_ARG:
1874          code += code[1];
1875          break;
1876    
1877          case OP_THEN_ARG:
1878          code += code[1+LINK_SIZE];
1879          break;
1880        }        }
1881    
1882      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1748  for (;;) Line 1891  for (;;)
1891      if (utf8) switch(c)      if (utf8) switch(c)
1892        {        {
1893        case OP_CHAR:        case OP_CHAR:
1894        case OP_CHARNC:        case OP_CHARI:
1895        case OP_EXACT:        case OP_EXACT:
1896          case OP_EXACTI:
1897        case OP_UPTO:        case OP_UPTO:
1898          case OP_UPTOI:
1899        case OP_MINUPTO:        case OP_MINUPTO:
1900          case OP_MINUPTOI:
1901        case OP_POSUPTO:        case OP_POSUPTO:
1902          case OP_POSUPTOI:
1903        case OP_STAR:        case OP_STAR:
1904          case OP_STARI:
1905        case OP_MINSTAR:        case OP_MINSTAR:
1906          case OP_MINSTARI:
1907        case OP_POSSTAR:        case OP_POSSTAR:
1908          case OP_POSSTARI:
1909        case OP_PLUS:        case OP_PLUS:
1910          case OP_PLUSI:
1911        case OP_MINPLUS:        case OP_MINPLUS:
1912          case OP_MINPLUSI:
1913        case OP_POSPLUS:        case OP_POSPLUS:
1914          case OP_POSPLUSI:
1915        case OP_QUERY:        case OP_QUERY:
1916          case OP_QUERYI:
1917        case OP_MINQUERY:        case OP_MINQUERY:
1918          case OP_MINQUERYI:
1919        case OP_POSQUERY:        case OP_POSQUERY:
1920          case OP_POSQUERYI:
1921        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1922        break;        break;
1923        }        }
# Line 1800  could_be_empty_branch(const uschar *code Line 1956  could_be_empty_branch(const uschar *code
1956    compile_data *cd)    compile_data *cd)
1957  {  {
1958  register int c;  register int c;
1959  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE);
1960       code < endcode;       code < endcode;
1961       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + _pcre_OP_lengths[c], TRUE))
1962    {    {
1963    const uschar *ccode;    const uschar *ccode;
1964    
1965    c = *code;    c = *code;
1966    
1967    /* Skip over forward assertions; the other assertions are skipped by    /* Skip over forward assertions; the other assertions are skipped by
1968    first_significant_code() with a TRUE final argument. */    first_significant_code() with a TRUE final argument. */
1969    
# Line 1818  for (code = first_significant_code(code Line 1974  for (code = first_significant_code(code
1974      continue;      continue;
1975      }      }
1976    
   /* 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;  
     }  
   
1977    /* For a recursion/subroutine call, if its end has been reached, which    /* For a recursion/subroutine call, if its end has been reached, which
1978    implies a subroutine call, we can scan it. */    implies a backward reference subroutine call, we can scan it. If it's a
1979      forward reference subroutine call, we can't. To detect forward reference
1980      we have to scan up the list that is kept in the workspace. This function is
1981      called only when doing the real compile, not during the pre-compile that
1982      measures the size of the compiled pattern. */
1983    
1984    if (c == OP_RECURSE)    if (c == OP_RECURSE)
1985      {      {
1986      BOOL empty_branch = FALSE;      const uschar *scode;
1987      const uschar *scode = cd->start_code + GET(code, 1);      BOOL empty_branch;
1988    
1989        /* Test for forward reference */
1990    
1991        for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
1992          if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;
1993    
1994        /* Not a forward reference, test for completed backward reference */
1995    
1996        empty_branch = FALSE;
1997        scode = cd->start_code + GET(code, 1);
1998      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
1999    
2000        /* Completed backwards reference */
2001    
2002      do      do
2003        {        {
2004        if (could_be_empty_branch(scode, endcode, utf8, cd))        if (could_be_empty_branch(scode, endcode, utf8, cd))
2005          {          {
2006          empty_branch = TRUE;          empty_branch = TRUE;
2007          break;          break;
2008          }          }
2009        scode += GET(scode, 1);        scode += GET(scode, 1);
2010        }        }
2011      while (*scode == OP_ALT);      while (*scode == OP_ALT);
2012    
2013      if (!empty_branch) return FALSE;  /* All branches are non-empty */      if (!empty_branch) return FALSE;  /* All branches are non-empty */
2014      continue;      continue;
2015      }      }
2016    
2017      /* Groups with zero repeats can of course be empty; skip them. */
2018    
2019      if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2020          c == OP_BRAPOSZERO)
2021        {
2022        code += _pcre_OP_lengths[c];
2023        do code += GET(code, 1); while (*code == OP_ALT);
2024        c = *code;
2025        continue;
2026        }
2027    
2028      /* A nested group that is already marked as "could be empty" can just be
2029      skipped. */
2030    
2031      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2032          c == OP_SCBRA || c == OP_SCBRAPOS)
2033        {
2034        do code += GET(code, 1); while (*code == OP_ALT);
2035        c = *code;
2036        continue;
2037        }
2038    
2039    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
2040    
2041    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA  || c == OP_BRAPOS ||
2042          c == OP_CBRA || c == OP_CBRAPOS ||
2043          c == OP_ONCE || c == OP_COND)
2044      {      {
2045      BOOL empty_branch;      BOOL empty_branch;
2046      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 1939  for (code = first_significant_code(code Line 2127  for (code = first_significant_code(code
2127      case OP_ALLANY:      case OP_ALLANY:
2128      case OP_ANYBYTE:      case OP_ANYBYTE:
2129      case OP_CHAR:      case OP_CHAR:
2130      case OP_CHARNC:      case OP_CHARI:
2131      case OP_NOT:      case OP_NOT:
2132        case OP_NOTI:
2133      case OP_PLUS:      case OP_PLUS:
2134      case OP_MINPLUS:      case OP_MINPLUS:
2135      case OP_POSPLUS:      case OP_POSPLUS:
# Line 1980  for (code = first_significant_code(code Line 2169  for (code = first_significant_code(code
2169      case OP_KET:      case OP_KET:
2170      case OP_KETRMAX:      case OP_KETRMAX:
2171      case OP_KETRMIN:      case OP_KETRMIN:
2172        case OP_KETRPOS:
2173      case OP_ALT:      case OP_ALT:
2174      return TRUE;      return TRUE;
2175    
# Line 1988  for (code = first_significant_code(code Line 2178  for (code = first_significant_code(code
2178    
2179  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2180      case OP_STAR:      case OP_STAR:
2181        case OP_STARI:
2182      case OP_MINSTAR:      case OP_MINSTAR:
2183        case OP_MINSTARI:
2184      case OP_POSSTAR:      case OP_POSSTAR:
2185        case OP_POSSTARI:
2186      case OP_QUERY:      case OP_QUERY:
2187        case OP_QUERYI:
2188      case OP_MINQUERY:      case OP_MINQUERY:
2189        case OP_MINQUERYI:
2190      case OP_POSQUERY:      case OP_POSQUERY:
2191        case OP_POSQUERYI:
2192      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];
2193      break;      break;
2194    
2195      case OP_UPTO:      case OP_UPTO:
2196        case OP_UPTOI:
2197      case OP_MINUPTO:      case OP_MINUPTO:
2198        case OP_MINUPTOI:
2199      case OP_POSUPTO:      case OP_POSUPTO:
2200        case OP_POSUPTOI:
2201      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];
2202      break;      break;
2203  #endif  #endif
2204    
2205        /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2206        string. */
2207    
2208        case OP_MARK:
2209        case OP_PRUNE_ARG:
2210        case OP_SKIP_ARG:
2211        code += code[1];
2212        break;
2213    
2214        case OP_THEN_ARG:
2215        code += code[1+LINK_SIZE];
2216        break;
2217    
2218      /* None of the remaining opcodes are required to match a character. */      /* None of the remaining opcodes are required to match a character. */
2219    
2220      default:      default:
2221      break;      break;
2222      }      }
2223    }    }
2224    
# Line 2023  return TRUE; Line 2235  return TRUE;
2235  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
2236  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,
2237  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.
2238    This function is called only during the real compile, not during the
2239    pre-compile.
2240    
2241  Arguments:  Arguments:
2242    code        points to start of the recursion    code        points to start of the recursion
2243    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2244    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2245    utf8        TRUE if in UTF-8 mode    utf8        TRUE if in UTF-8 mode
2246    cd          pointers to tables etc    cd          pointers to tables etc
2247    
2248  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2249  */  */
# Line 2223  auto_callout(uschar *code, const uschar Line 2437  auto_callout(uschar *code, const uschar
2437  {  {
2438  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2439  *code++ = 255;  *code++ = 255;
2440  PUT(code, 0, ptr - cd->start_pattern);  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2441  PUT(code, LINK_SIZE, 0);                /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2442  return code + 2*LINK_SIZE;  return code + 2*LINK_SIZE;
2443  }  }
2444    
# Line 2249  Returns:             nothing Line 2463  Returns:             nothing
2463  static void  static void
2464  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)
2465  {  {
2466  int length = ptr - cd->start_pattern - GET(previous_callout, 2);  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2467  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
2468  }  }
2469    
# Line 2299  for (++c; c <= d; c++) Line 2513  for (++c; c <= d; c++)
2513    
2514  return TRUE;  return TRUE;
2515  }  }
2516    
2517    
2518    
2519    /*************************************************
2520    *        Check a character and a property        *
2521    *************************************************/
2522    
2523    /* This function is called by check_auto_possessive() when a property item
2524    is adjacent to a fixed character.
2525    
2526    Arguments:
2527      c            the character
2528      ptype        the property type
2529      pdata        the data for the type
2530      negated      TRUE if it's a negated property (\P or \p{^)
2531    
2532    Returns:       TRUE if auto-possessifying is OK
2533    */
2534    
2535    static BOOL
2536    check_char_prop(int c, int ptype, int pdata, BOOL negated)
2537    {
2538    const ucd_record *prop = GET_UCD(c);
2539    switch(ptype)
2540      {
2541      case PT_LAMP:
2542      return (prop->chartype == ucp_Lu ||
2543              prop->chartype == ucp_Ll ||
2544              prop->chartype == ucp_Lt) == negated;
2545    
2546      case PT_GC:
2547      return (pdata == _pcre_ucp_gentype[prop->chartype]) == negated;
2548    
2549      case PT_PC:
2550      return (pdata == prop->chartype) == negated;
2551    
2552      case PT_SC:
2553      return (pdata == prop->script) == negated;
2554    
2555      /* These are specials */
2556    
2557      case PT_ALNUM:
2558      return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2559              _pcre_ucp_gentype[prop->chartype] == ucp_N) == negated;
2560    
2561      case PT_SPACE:    /* Perl space */
2562      return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2563              c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2564              == negated;
2565    
2566      case PT_PXSPACE:  /* POSIX space */
2567      return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2568              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2569              c == CHAR_FF || c == CHAR_CR)
2570              == negated;
2571    
2572      case PT_WORD:
2573      return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2574              _pcre_ucp_gentype[prop->chartype] == ucp_N ||
2575              c == CHAR_UNDERSCORE) == negated;
2576      }
2577    return FALSE;
2578    }
2579  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2580    
2581    
# Line 2312  whether the next thing could possibly ma Line 2589  whether the next thing could possibly ma
2589  sense to automatically possessify the repeated item.  sense to automatically possessify the repeated item.
2590    
2591  Arguments:  Arguments:
2592    op_code       the repeated op code    previous      pointer to the repeated opcode
   this          data for this item, depends on the opcode  
2593    utf8          TRUE in UTF-8 mode    utf8          TRUE in UTF-8 mode
   utf8_char     used for utf8 character bytes, NULL if not relevant  
2594    ptr           next character in pattern    ptr           next character in pattern
2595    options       options bits    options       options bits
2596    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 2324  Returns:        TRUE if possessifying is Line 2599  Returns:        TRUE if possessifying is
2599  */  */
2600    
2601  static BOOL  static BOOL
2602  check_auto_possessive(int op_code, int item, BOOL utf8, uschar *utf8_char,  check_auto_possessive(const uschar *previous, BOOL utf8, const uschar *ptr,
2603    const uschar *ptr, int options, compile_data *cd)    int options, compile_data *cd)
2604  {  {
2605  int next;  int c, next;
2606    int op_code = *previous++;
2607    
2608  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
2609    
# Line 2338  if ((options & PCRE_EXTENDED) != 0) Line 2614  if ((options & PCRE_EXTENDED) != 0)
2614      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2615      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2616        {        {
2617        while (*(++ptr) != 0)        ptr++;
2618          while (*ptr != 0)
2619            {
2620          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2621            ptr++;
2622    #ifdef SUPPORT_UTF8
2623            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2624    #endif
2625            }
2626        }        }
2627      else break;      else break;
2628      }      }
# Line 2375  if ((options & PCRE_EXTENDED) != 0) Line 2658  if ((options & PCRE_EXTENDED) != 0)
2658      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2659      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2660        {        {
2661        while (*(++ptr) != 0)        ptr++;
2662          while (*ptr != 0)
2663            {
2664          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2665            ptr++;
2666    #ifdef SUPPORT_UTF8
2667            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2668    #endif
2669            }
2670        }        }
2671      else break;      else break;
2672      }      }
# Line 2388  if (*ptr == CHAR_ASTERISK || *ptr == CHA Line 2678  if (*ptr == CHAR_ASTERISK || *ptr == CHA
2678    strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)    strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
2679      return FALSE;      return FALSE;
2680    
2681  /* Now compare the next item with the previous opcode. If the previous is a  /* Now compare the next item with the previous opcode. First, handle cases when
2682  positive single character match, "item" either contains the character or, if  the next item is a character. */
 "item" is greater than 127 in utf8 mode, the character's bytes are in  
 utf8_char. */  
   
   
 /* Handle cases when the next item is a character. */  
2683    
2684  if (next >= 0) switch(op_code)  if (next >= 0) switch(op_code)
2685    {    {
2686    case OP_CHAR:    case OP_CHAR:
2687  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2688    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
2689  #else  #else
2690    (void)(utf8_char);  /* Keep compiler happy by referencing function argument */    c = *previous;
2691  #endif  #endif
2692    return item != next;    return c != next;
2693    
2694    /* 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
2695    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
2696    high-valued characters. */    high-valued characters. */
2697    
2698    case OP_CHARNC:    case OP_CHARI:
2699  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2700    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
2701    #else
2702      c = *previous;
2703  #endif  #endif
2704    if (item == next) return FALSE;    if (c == next) return FALSE;
2705  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2706    if (utf8)    if (utf8)
2707      {      {
# Line 2425  if (next >= 0) switch(op_code) Line 2712  if (next >= 0) switch(op_code)
2712  #else  #else
2713      othercase = NOTACHAR;      othercase = NOTACHAR;
2714  #endif  #endif
2715      return (unsigned int)item != othercase;      return (unsigned int)c != othercase;
2716      }      }
2717    else    else
2718  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
2719    return (item != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */
2720    
2721    /* For OP_NOT, "item" must be a single-byte character. */    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These
2722      opcodes are not used for multi-byte characters, because they are coded using
2723      an XCLASS instead. */
2724    
2725    case OP_NOT:    case OP_NOT:
2726    if (item == next) return TRUE;    return (c = *previous) == next;
2727    if ((options & PCRE_CASELESS) == 0) return FALSE;  
2728      case OP_NOTI:
2729      if ((c = *previous) == next) return TRUE;
2730  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2731    if (utf8)    if (utf8)
2732      {      {
# Line 2446  if (next >= 0) switch(op_code) Line 2737  if (next >= 0) switch(op_code)
2737  #else  #else
2738      othercase = NOTACHAR;      othercase = NOTACHAR;
2739  #endif  #endif
2740      return (unsigned int)item == othercase;      return (unsigned int)c == othercase;
2741      }      }
2742    else    else
2743  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
2744    return (item == cd->fcc[next]);  /* Non-UTF-8 mode */    return (c == cd->fcc[next]);  /* Non-UTF-8 mode */
2745    
2746      /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
2747      When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
2748    
2749    case OP_DIGIT:    case OP_DIGIT:
2750    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;
# Line 2493  if (next >= 0) switch(op_code) Line 2787  if (next >= 0) switch(op_code)
2787      case 0x202f:      case 0x202f:
2788      case 0x205f:      case 0x205f:
2789      case 0x3000:      case 0x3000:
2790      return op_code != OP_HSPACE;      return op_code == OP_NOT_HSPACE;
2791      default:      default:
2792      return op_code == OP_HSPACE;      return op_code != OP_NOT_HSPACE;
2793      }      }
2794    
2795      case OP_ANYNL:
2796    case OP_VSPACE:    case OP_VSPACE:
2797    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
2798    switch(next)    switch(next)
# Line 2509  if (next >= 0) switch(op_code) Line 2804  if (next >= 0) switch(op_code)
2804      case 0x85:      case 0x85:
2805      case 0x2028:      case 0x2028:
2806      case 0x2029:      case 0x2029:
2807      return op_code != OP_VSPACE;      return op_code == OP_NOT_VSPACE;
2808      default:      default:
2809      return op_code == OP_VSPACE;      return op_code != OP_NOT_VSPACE;
2810      }      }
2811    
2812    #ifdef SUPPORT_UCP
2813      case OP_PROP:
2814      return check_char_prop(next, previous[0], previous[1], FALSE);
2815    
2816      case OP_NOTPROP:
2817      return check_char_prop(next, previous[0], previous[1], TRUE);
2818    #endif
2819    
2820    default:    default:
2821    return FALSE;    return FALSE;
2822    }    }
2823    
2824    
2825  /* Handle the case when the next item is \d, \s, etc. */  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
2826    is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
2827    generated only when PCRE_UCP is *not* set, that is, when only ASCII
2828    characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are
2829    replaced by OP_PROP codes when PCRE_UCP is set. */
2830    
2831  switch(op_code)  switch(op_code)
2832    {    {
2833    case OP_CHAR:    case OP_CHAR:
2834    case OP_CHARNC:    case OP_CHARI:
2835  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2836    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
2837    #else
2838      c = *previous;
2839  #endif  #endif
2840    switch(-next)    switch(-next)
2841      {      {
2842      case ESC_d:      case ESC_d:
2843      return item > 127 || (cd->ctypes[item] & ctype_digit) == 0;      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;
2844    
2845      case ESC_D:      case ESC_D:
2846      return item <= 127 && (cd->ctypes[item] & ctype_digit) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;
2847    
2848      case ESC_s:      case ESC_s:
2849      return item > 127 || (cd->ctypes[item] & ctype_space) == 0;      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;
2850    
2851      case ESC_S:      case ESC_S:
2852      return item <= 127 && (cd->ctypes[item] & ctype_space) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;
2853    
2854      case ESC_w:      case ESC_w:
2855      return item > 127 || (cd->ctypes[item] & ctype_word) == 0;      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;
2856    
2857      case ESC_W:      case ESC_W:
2858      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;
2859    
2860      case ESC_h:      case ESC_h:
2861      case ESC_H:      case ESC_H:
2862      switch(item)      switch(c)
2863        {        {
2864        case 0x09:        case 0x09:
2865        case 0x20:        case 0x20:
# Line 2578  switch(op_code) Line 2887  switch(op_code)
2887    
2888      case ESC_v:      case ESC_v:
2889      case ESC_V:      case ESC_V:
2890      switch(item)      switch(c)
2891        {        {
2892        case 0x0a:        case 0x0a:
2893        case 0x0b:        case 0x0b:
# Line 2592  switch(op_code) Line 2901  switch(op_code)
2901        return -next == ESC_v;        return -next == ESC_v;
2902        }        }
2903    
2904        /* When PCRE_UCP is set, these values get generated for \d etc. Find
2905        their substitutions and process them. The result will always be either
2906        -ESC_p or -ESC_P. Then fall through to process those values. */
2907    
2908    #ifdef SUPPORT_UCP
2909        case ESC_du:
2910        case ESC_DU:
2911        case ESC_wu:
2912        case ESC_WU:
2913        case ESC_su:
2914        case ESC_SU:
2915          {
2916          int temperrorcode = 0;
2917          ptr = substitutes[-next - ESC_DU];
2918          next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);
2919          if (temperrorcode != 0) return FALSE;
2920          ptr++;    /* For compatibility */
2921          }
2922        /* Fall through */
2923    
2924        case ESC_p:
2925        case ESC_P:
2926          {
2927          int ptype, pdata, errorcodeptr;
2928          BOOL negated;
2929    
2930          ptr--;      /* Make ptr point at the p or P */
2931          ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);
2932          if (ptype < 0) return FALSE;
2933          ptr++;      /* Point past the final curly ket */
2934    
2935          /* If the property item is optional, we have to give up. (When generated
2936          from \d etc by PCRE_UCP, this test will have been applied much earlier,
2937          to the original \d etc. At this point, ptr will point to a zero byte. */
2938    
2939          if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
2940            strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
2941              return FALSE;
2942    
2943          /* Do the property check. */
2944    
2945          return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);
2946          }
2947    #endif
2948    
2949      default:      default:
2950      return FALSE;      return FALSE;
2951      }      }
2952    
2953      /* In principle, support for Unicode properties should be integrated here as
2954      well. It means re-organizing the above code so as to get hold of the property
2955      values before switching on the op-code. However, I wonder how many patterns
2956      combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,
2957      these op-codes are never generated.) */
2958    
2959    case OP_DIGIT:    case OP_DIGIT:
2960    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||
2961           next == -ESC_h || next == -ESC_v;           next == -ESC_h || next == -ESC_v || next == -ESC_R;
2962    
2963    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
2964    return next == -ESC_d;    return next == -ESC_d;
2965    
2966    case OP_WHITESPACE:    case OP_WHITESPACE:
2967    return next == -ESC_S || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;
2968    
2969    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
2970    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_s || next == -ESC_h || next == -ESC_v;
2971    
2972    case OP_HSPACE:    case OP_HSPACE:
2973    return next == -ESC_S || next == -ESC_H || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||
2974             next == -ESC_w || next == -ESC_v || next == -ESC_R;
2975    
2976    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
2977    return next == -ESC_h;    return next == -ESC_h;
2978    
2979    /* Can't have \S in here because VT matches \S (Perl anomaly) */    /* Can't have \S in here because VT matches \S (Perl anomaly) */
2980      case OP_ANYNL:
2981    case OP_VSPACE:    case OP_VSPACE:
2982    return next == -ESC_V || next == -ESC_d || next == -ESC_w;    return next == -ESC_V || next == -ESC_d || next == -ESC_w;
2983    
2984    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
2985    return next == -ESC_v;    return next == -ESC_v || next == -ESC_R;
2986    
2987    case OP_WORDCHAR:    case OP_WORDCHAR:
2988    return next == -ESC_W || next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_W || next == -ESC_s || next == -ESC_h ||
2989             next == -ESC_v || next == -ESC_R;
2990    
2991    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
2992    return next == -ESC_w || next == -ESC_d;    return next == -ESC_w || next == -ESC_d;
# Line 2687  BOOL inescq = FALSE; Line 3050  BOOL inescq = FALSE;
3050  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstbyte = FALSE;
3051  const uschar *ptr = *ptrptr;  const uschar *ptr = *ptrptr;
3052  const uschar *tempptr;  const uschar *tempptr;
3053    const uschar *nestptr = NULL;
3054  uschar *previous = NULL;  uschar *previous = NULL;
3055  uschar *previous_callout = NULL;  uschar *previous_callout = NULL;
3056  uschar *save_hwm = NULL;  uschar *save_hwm = NULL;
# Line 2757  for (;; ptr++) Line 3121  for (;; ptr++)
3121    
3122    c = *ptr;    c = *ptr;
3123    
3124      /* If we are at the end of a nested substitution, revert to the outer level
3125      string. Nesting only happens one level deep. */
3126    
3127      if (c == 0 && nestptr != NULL)
3128        {
3129        ptr = nestptr;
3130        nestptr = NULL;
3131        c = *ptr;
3132        }
3133    
3134    /* If we are in the pre-compile phase, accumulate the length used for the    /* If we are in the pre-compile phase, accumulate the length used for the
3135    previous cycle of this loop. */    previous cycle of this loop. */
3136    
# Line 2787  for (;; ptr++) Line 3161  for (;; ptr++)
3161        goto FAILED;        goto FAILED;
3162        }        }
3163    
3164      *lengthptr += code - last_code;      *lengthptr += (int)(code - last_code);
3165      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, code - last_code, c));      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, code - last_code, c));
3166    
3167      /* 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
# Line 2869  for (;; ptr++) Line 3243  for (;; ptr++)
3243      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if ((cd->ctypes[c] & ctype_space) != 0) continue;
3244      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3245        {        {
3246        while (*(++ptr) != 0)        ptr++;
3247          while (*ptr != 0)
3248          {          {
3249          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3250            ptr++;
3251    #ifdef SUPPORT_UTF8
3252            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
3253    #endif
3254          }          }
3255        if (*ptr != 0) continue;        if (*ptr != 0) continue;
3256    
# Line 2905  for (;; ptr++) Line 3284  for (;; ptr++)
3284          *errorcodeptr = ERR20;          *errorcodeptr = ERR20;
3285          goto FAILED;          goto FAILED;
3286          }          }
3287        *lengthptr += code - last_code;   /* To include callout length */        *lengthptr += (int)(code - last_code);   /* To include callout length */
3288        DPRINTF((">> end branch\n"));        DPRINTF((">> end branch\n"));
3289        }        }
3290      return TRUE;      return TRUE;
# Line 2916  for (;; ptr++) Line 3295  for (;; ptr++)
3295      the setting of any following char as a first character. */      the setting of any following char as a first character. */
3296    
3297      case CHAR_CIRCUMFLEX_ACCENT:      case CHAR_CIRCUMFLEX_ACCENT:
3298        previous = NULL;
3299      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3300        {        {
3301        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
3302          *code++ = OP_CIRCM;
3303        }        }
3304      previous = NULL;      else *code++ = OP_CIRC;
     *code++ = OP_CIRC;  
3305      break;      break;
3306    
3307      case CHAR_DOLLAR_SIGN:      case CHAR_DOLLAR_SIGN:
3308      previous = NULL;      previous = NULL;
3309      *code++ = OP_DOLL;      *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
3310      break;      break;
3311    
3312      /* 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 3110  for (;; ptr++) Line 3490  for (;; ptr++)
3490            ptr++;            ptr++;
3491            }            }
3492    
3493          posix_class = check_posix_name(ptr, tempptr - ptr);          posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
3494          if (posix_class < 0)          if (posix_class < 0)
3495            {            {
3496            *errorcodeptr = ERR30;            *errorcodeptr = ERR30;
# Line 3124  for (;; ptr++) Line 3504  for (;; ptr++)
3504          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
3505            posix_class = 0;            posix_class = 0;
3506    
3507          /* We build the bit map for the POSIX class in a chunk of local store          /* When PCRE_UCP is set, some of the POSIX classes are converted to
3508          because we may be adding and subtracting from it, and we don't want to          different escape sequences that use Unicode properties. */
3509          subtract bits that may be in the main map already. At the end we or the  
3510          result into the bit map that is being built. */  #ifdef SUPPORT_UCP
3511            if ((options & PCRE_UCP) != 0)
3512              {
3513              int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
3514              if (posix_substitutes[pc] != NULL)
3515                {
3516                nestptr = tempptr + 1;
3517                ptr = posix_substitutes[pc] - 1;
3518                continue;
3519                }
3520              }
3521    #endif
3522            /* In the non-UCP case, we build the bit map for the POSIX class in a
3523            chunk of local store because we may be adding and subtracting from it,
3524            and we don't want to subtract bits that may be in the main map already.
3525            At the end we or the result into the bit map that is being built. */
3526    
3527          posix_class *= 3;          posix_class *= 3;
3528    
# Line 3171  for (;; ptr++) Line 3566  for (;; ptr++)
3566    
3567        /* Backslash may introduce a single character, or it may introduce one        /* Backslash may introduce a single character, or it may introduce one
3568        of the specials, which just set a flag. The sequence \b is a special        of the specials, which just set a flag. The sequence \b is a special
3569        case. Inside a class (and only there) it is treated as backspace.        case. Inside a class (and only there) it is treated as backspace. We
3570        Elsewhere it marks a word boundary. Other escapes have preset maps ready        assume that other escapes have more than one character in them, so set
3571        to 'or' into the one we are building. We assume they have more than one        class_charcount bigger than one. Unrecognized escapes fall through and
3572        character in them, so set class_charcount bigger than one. */        are either treated as literal characters (by default), or are faulted if
3573          PCRE_EXTRA is set. */
3574    
3575        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
3576          {          {
3577          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3578          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
3579    
3580          if (-c == ESC_b) c = CHAR_BS;       /* \b is backspace in a class */          if (-c == ESC_b) c = CHAR_BS;    /* \b is backspace in a class */
         else if (-c == ESC_X) c = CHAR_X;   /* \X is literal X in a class */  
         else if (-c == ESC_R) c = CHAR_R;   /* \R is literal R in a class */  
3581          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (-c == ESC_Q)            /* Handle start of quoted string */
3582            {            {
3583            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 3200  for (;; ptr++) Line 3594  for (;; ptr++)
3594            register const uschar *cbits = cd->cbits;            register const uschar *cbits = cd->cbits;
3595            class_charcount += 2;     /* Greater than 1 is what matters */            class_charcount += 2;     /* Greater than 1 is what matters */
3596    
3597            /* Save time by not doing this in the pre-compile phase. */            switch (-c)
   
           if (lengthptr == NULL) switch (-c)  
3598              {              {
3599    #ifdef SUPPORT_UCP
3600                case ESC_du:     /* These are the values given for \d etc */
3601                case ESC_DU:     /* when PCRE_UCP is set. We replace the */
3602                case ESC_wu:     /* escape sequence with an appropriate \p */
3603                case ESC_WU:     /* or \P to test Unicode properties instead */
3604                case ESC_su:     /* of the default ASCII testing. */
3605                case ESC_SU:
3606                nestptr = ptr;
3607                ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */
3608                class_charcount -= 2;                /* Undo! */
3609                continue;
3610    #endif
3611              case ESC_d:              case ESC_d:
3612              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
3613              continue;              continue;
# Line 3222  for (;; ptr++) Line 3626  for (;; ptr++)
3626              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
3627              continue;              continue;
3628    
3629                /* Perl 5.004 onwards omits VT from \s, but we must preserve it
3630                if it was previously set by something earlier in the character
3631                class. */
3632    
3633              case ESC_s:              case ESC_s:
3634              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];              classbits[0] |= cbits[cbit_space];
3635              classbits[1] &= ~0x08;   /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= cbits[cbit_space+1] & ~0x08;
3636                for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
3637              continue;              continue;
3638    
3639              case ESC_S:              case ESC_S:
# Line 3233  for (;; ptr++) Line 3642  for (;; ptr++)
3642              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
3643              continue;              continue;
3644    
3645              default:    /* Not recognized; fall through */              case ESC_h:
             break;      /* Need "default" setting to stop compiler warning. */  
             }  
   
           /* In the pre-compile phase, just do the recognition. */  
   
           else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||  
                    c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;  
   
           /* We need to deal with \H, \h, \V, and \v in both phases because  
           they use extra memory. */  
   
           if (-c == ESC_h)  
             {  
3646              SETBIT(classbits, 0x09); /* VT */              SETBIT(classbits, 0x09); /* VT */
3647              SETBIT(classbits, 0x20); /* SPACE */              SETBIT(classbits, 0x20); /* SPACE */
3648              SETBIT(classbits, 0xa0); /* NSBP */              SETBIT(classbits, 0xa0); /* NSBP */
# Line 3270  for (;; ptr++) Line 3666  for (;; ptr++)
3666                }                }
3667  #endif  #endif
3668              continue;              continue;
             }  
3669    
3670            if (-c == ESC_H)              case ESC_H:
             {  
3671              for (c = 0; c < 32; c++)              for (c = 0; c < 32; c++)
3672                {                {
3673                int x = 0xff;                int x = 0xff;
# Line 3315  for (;; ptr++) Line 3709  for (;; ptr++)
3709                }                }
3710  #endif  #endif
3711              continue;              continue;
             }  
3712    
3713            if (-c == ESC_v)              case ESC_v:
             {  
3714              SETBIT(classbits, 0x0a); /* LF */              SETBIT(classbits, 0x0a); /* LF */
3715              SETBIT(classbits, 0x0b); /* VT */              SETBIT(classbits, 0x0b); /* VT */
3716              SETBIT(classbits, 0x0c); /* FF */              SETBIT(classbits, 0x0c); /* FF */
# Line 3334  for (;; ptr++) Line 3726  for (;; ptr++)
3726                }                }
3727  #endif  #endif
3728              continue;              continue;
             }  
3729    
3730            if (-c == ESC_V)              case ESC_V:
             {  
3731              for (c = 0; c < 32; c++)              for (c = 0; c < 32; c++)
3732                {                {
3733                int x = 0xff;                int x = 0xff;
# Line 3367  for (;; ptr++) Line 3757  for (;; ptr++)
3757                }                }
3758  #endif  #endif
3759              continue;              continue;
             }  
   
           /* We need to deal with \P and \p in both phases. */  
3760    
3761  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3762            if (-c == ESC_p || -c == ESC_P)              case ESC_p:
3763              {              case ESC_P:
3764              BOOL negated;                {
3765              int pdata;                BOOL negated;
3766              int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                int pdata;
3767              if (ptype < 0) goto FAILED;                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
3768              class_utf8 = TRUE;                if (ptype < 0) goto FAILED;
3769              *class_utf8data++ = ((-c == ESC_p) != negated)?                class_utf8 = TRUE;
3770                XCL_PROP : XCL_NOTPROP;                *class_utf8data++ = ((-c == ESC_p) != negated)?
3771              *class_utf8data++ = ptype;                  XCL_PROP : XCL_NOTPROP;
3772              *class_utf8data++ = pdata;                *class_utf8data++ = ptype;
3773              class_charcount -= 2;   /* Not a < 256 character */                *class_utf8data++ = pdata;
3774              continue;                class_charcount -= 2;   /* Not a < 256 character */
3775              }                continue;
3776                  }
3777  #endif  #endif
3778            /* Unrecognized escapes are faulted if PCRE is running in its              /* Unrecognized escapes are faulted if PCRE is running in its
3779            strict mode. By default, for compatibility with Perl, they are              strict mode. By default, for compatibility with Perl, they are
3780            treated as literals. */              treated as literals. */
3781    
3782            if ((options & PCRE_EXTRA) != 0)              default:
3783              {              if ((options & PCRE_EXTRA) != 0)
3784              *errorcodeptr = ERR7;                {
3785              goto FAILED;                *errorcodeptr = ERR7;
3786                  goto FAILED;
3787                  }
3788                class_charcount -= 2;  /* Undo the default count from above */
3789                c = *ptr;              /* Get the final character and fall through */
3790                break;
3791              }              }
   
           class_charcount -= 2;  /* Undo the default count from above */  
           c = *ptr;              /* Get the final character and fall through */  
3792            }            }
3793    
3794          /* Fall through if we have a single character (c >= 0). This may be          /* Fall through if we have a single character (c >= 0). This may be
# Line 3468  for (;; ptr++) Line 3858  for (;; ptr++)
3858            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3859            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
3860    
3861            /* \b is backspace; \X is literal X; \R is literal R; any other            /* \b is backspace; any other special means the '-' was literal */
           special means the '-' was literal */  
3862    
3863            if (d < 0)            if (d < 0)
3864              {              {
3865              if (d == -ESC_b) d = CHAR_BS;              if (d == -ESC_b) d = CHAR_BS; else
             else if (d == -ESC_X) d = CHAR_X;  
             else if (d == -ESC_R) d = CHAR_R; else  
3866                {                {
3867                ptr = oldptr;                ptr = oldptr;
3868                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto LONE_SINGLE_CHARACTER;  /* A few lines below */
# Line 3641  for (;; ptr++) Line 4028  for (;; ptr++)
4028          }          }
4029        }        }
4030    
4031      /* Loop until ']' reached. This "while" is the end of the "do" above. */      /* Loop until ']' reached. This "while" is the end of the "do" far above.
4032        If we are at the end of an internal nested string, revert to the outer
4033        string. */
4034    
4035        while (((c = *(++ptr)) != 0 ||
4036               (nestptr != NULL &&
4037                 (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != 0)) &&
4038               (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
4039    
4040      while ((c = *(++ptr)) != 0 && (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));      /* Check for missing terminating ']' */
4041    
4042      if (c == 0)                          /* Missing terminating ']' */      if (c == 0)
4043        {        {
4044        *errorcodeptr = ERR6;        *errorcodeptr = ERR6;
4045        goto FAILED;        goto FAILED;
4046        }        }
4047    
   
 /* This code has been disabled because it would mean that \s counts as  
 an explicit \r or \n reference, and that's not really what is wanted. Now  
 we set the flag only if there is a literal "\r" or "\n" in the class. */  
   
 #if 0  
     /* Remember whether \r or \n are in this class */  
   
     if (negate_class)  
       {  
       if ((classbits[1] & 0x24) != 0x24) cd->external_flags |= PCRE_HASCRORLF;  
       }  
     else  
       {  
       if ((classbits[1] & 0x24) != 0) cd->external_flags |= PCRE_HASCRORLF;  
       }  
 #endif  
   
   
4048      /* If class_charcount is 1, we saw precisely one character whose value is      /* If class_charcount is 1, we saw precisely one character whose value is
4049      less than 256. As long as there were no characters >= 128 and there was no      less than 256. As long as there were no characters >= 128 and there was no
4050      use of \p or \P, in other words, no use of any XCLASS features, we can      use of \p or \P, in other words, no use of any XCLASS features, we can
# Line 3677  we set the flag only if there is a liter Line 4052  we set the flag only if there is a liter
4052    
4053      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
4054      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR
4055      operate on single-bytes only. This is an historical hangover. Maybe one day      operate on single-bytes characters only. This is an historical hangover.
4056      we can tidy these opcodes to handle multi-byte characters.      Maybe one day we can tidy these opcodes to handle multi-byte characters.
4057    
4058      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
4059      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.
4060      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
4061      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
4062      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
4063      reqbyte, save the previous value for reinstating. */      of reqbyte, save the previous value for reinstating. */
4064    
4065  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4066      if (class_charcount == 1 && !class_utf8 &&      if (class_charcount == 1 && !class_utf8 &&
# Line 3696  we set the flag only if there is a liter Line 4071  we set the flag only if there is a liter
4071        {        {
4072        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
4073    
4074        /* The OP_NOT opcode works on one-byte characters only. */        /* The OP_NOT[I] opcodes work on one-byte characters only. */
4075    
4076        if (negate_class)        if (negate_class)
4077          {          {
4078          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
4079          zerofirstbyte = firstbyte;          zerofirstbyte = firstbyte;
4080          *code++ = OP_NOT;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4081          *code++ = class_lastchar;          *code++ = class_lastchar;
4082          break;          break;
4083          }          }
# Line 3733  we set the flag only if there is a liter Line 4108  we set the flag only if there is a liter
4108    
4109      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
4110      extended class, with its own opcode, unless there was a negated special      extended class, with its own opcode, unless there was a negated special
4111      such as \S in the class, because in that case all characters > 255 are in      such as \S in the class, and PCRE_UCP is not set, because in that case all
4112      the class, so any that were explicitly given as well can be ignored. If      characters > 255 are in the class, so any that were explicitly given as
4113      (when there are explicit characters > 255 that must be listed) there are no      well can be ignored. If (when there are explicit characters > 255 that must
4114      characters < 256, we can omit the bitmap in the actual compiled code. */      be listed) there are no characters < 256, we can omit the bitmap in the
4115        actual compiled code. */
4116    
4117  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4118      if (class_utf8 && !should_flip_negation)      if (class_utf8 && (!should_flip_negation || (options & PCRE_UCP) != 0))
4119        {        {
4120        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */
4121        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
# Line 3765  we set the flag only if there is a liter Line 4141  we set the flag only if there is a liter
4141        }        }
4142  #endif  #endif
4143    
4144      /* If there are no characters > 255, set the opcode to OP_CLASS or      /* If there are no characters > 255, or they are all to be included or
4145      OP_NCLASS, depending on whether the whole class was negated and whether      excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
4146      there were negative specials such as \S in the class. Then copy the 32-byte      whole class was negated and whether there were negative specials such as \S
4147      map into the code vector, negating it if necessary. */      (non-UCP) in the class. Then copy the 32-byte map into the code vector,
4148        negating it if necessary. */
4149    
4150      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
4151      if (negate_class)      if (negate_class)
# Line 3851  we set the flag only if there is a liter Line 4228  we set the flag only if there is a liter
4228        ptr++;        ptr++;
4229        }        }
4230      else repeat_type = greedy_default;      else repeat_type = greedy_default;
4231    
4232        /* If previous was a recursion call, wrap it in atomic brackets so that
4233        previous becomes the atomic group. All recursions were so wrapped in the
4234        past, but it no longer happens for non-repeated recursions. In fact, the
4235        repeated ones could be re-implemented independently so as not to need this,
4236        but for the moment we rely on the code for repeating groups. */
4237    
4238        if (*previous == OP_RECURSE)
4239          {
4240          memmove(previous + 1 + LINK_SIZE, previous, 1 + LINK_SIZE);
4241          *previous = OP_ONCE;
4242          PUT(previous, 1, 2 + 2*LINK_SIZE);
4243          previous[2 + 2*LINK_SIZE] = OP_KET;
4244          PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4245          code += 2 + 2 * LINK_SIZE;
4246          length_prevgroup = 3 + 3*LINK_SIZE;
4247    
4248          /* When actually compiling, we need to check whether this was a forward
4249          reference, and if so, adjust the offset. */
4250    
4251          if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4252            {
4253            int offset = GET(cd->hwm, -LINK_SIZE);
4254            if (offset == previous + 1 - cd->start_code)
4255              PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
4256            }
4257          }
4258    
4259        /* Now handle repetition for the different types of item. */
4260    
4261      /* If previous was a character match, abolish the item and generate a      /* If previous was a character match, abolish the item and generate a
4262      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
# Line 3858  we set the flag only if there is a liter Line 4264  we set the flag only if there is a liter
4264      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
4265      instead.  */      instead.  */
4266    
4267      if (*previous == OP_CHAR || *previous == OP_CHARNC)      if (*previous == OP_CHAR || *previous == OP_CHARI)
4268        {        {
4269          op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;
4270    
4271        /* 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
4272        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
4273        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 3892  we set the flag only if there is a liter Line 4300  we set the flag only if there is a liter
4300    
4301        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4302            repeat_max < 0 &&            repeat_max < 0 &&
4303            check_auto_possessive(*previous, c, utf8, utf8_char, ptr + 1,            check_auto_possessive(previous, utf8, ptr + 1, options, cd))
             options, cd))  
4304          {          {
4305          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4306          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 3905  we set the flag only if there is a liter Line 4312  we set the flag only if there is a liter
4312      /* If previous was a single negated character ([^a] or similar), we use      /* If previous was a single negated character ([^a] or similar), we use
4313      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-
4314      character repeats by setting opt_type to add a suitable offset into      character repeats by setting opt_type to add a suitable offset into
4315      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
4316      currently used only for single-byte chars. */      are currently used only for single-byte chars. */
4317    
4318      else if (*previous == OP_NOT)      else if (*previous == OP_NOT || *previous == OP_NOTI)
4319        {        {
4320        op_type = OP_NOTSTAR - OP_STAR;  /* Use "not" opcodes */        op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;
4321        c = previous[1];        c = previous[1];
4322        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4323            repeat_max < 0 &&            repeat_max < 0 &&
4324            check_auto_possessive(OP_NOT, c, utf8, NULL, ptr + 1, options, cd))            check_auto_possessive(previous, utf8, ptr + 1, options, cd))
4325          {          {
4326          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4327          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 3938  we set the flag only if there is a liter Line 4345  we set the flag only if there is a liter
4345    
4346        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4347            repeat_max < 0 &&            repeat_max < 0 &&
4348            check_auto_possessive(c, 0, utf8, NULL, ptr + 1, options, cd))            check_auto_possessive(previous, utf8, ptr + 1, options, cd))
4349          {          {
4350          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4351          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 4107  we set the flag only if there is a liter Line 4514  we set the flag only if there is a liter
4514  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4515               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
4516  #endif  #endif
4517               *previous == OP_REF)               *previous == OP_REF ||
4518                 *previous == OP_REFI)
4519        {        {
4520        if (repeat_max == 0)        if (repeat_max == 0)
4521          {          {
# Line 4141  we set the flag only if there is a liter Line 4549  we set the flag only if there is a liter
4549        }        }
4550    
4551      /* 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
4552      cases. */      cases. Note that at this point we can encounter only the "basic" BRA and
4553        KET opcodes, as this is the place where they get converted into the more
4554        special varieties. */
4555    
4556      else if (*previous == OP_BRA  || *previous == OP_CBRA ||      else if (*previous == OP_BRA  || *previous == OP_CBRA ||
4557               *previous == OP_ONCE || *previous == OP_COND)               *previous == OP_ONCE || *previous == OP_COND)
4558        {        {
4559        register int i;        register int i;
4560        int ketoffset = 0;        int len = (int)(code - previous);
       int len = code - previous;  
4561        uschar *bralink = NULL;        uschar *bralink = NULL;
4562          uschar *brazeroptr = NULL;
4563    
4564        /* Repeating a DEFINE group is pointless */        /* Repeating a DEFINE group is pointless */
4565    
4566        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
# Line 4159  we set the flag only if there is a liter Line 4569  we set the flag only if there is a liter
4569          goto FAILED;          goto FAILED;
4570          }          }
4571    
       /* If the maximum repeat count is unlimited, find the end of the bracket  
       by scanning through from the start, and compute the offset back to it  
       from the current code pointer. There may be an OP_OPT setting following  
       the final KET, so we can't find the end just by going back from the code  
       pointer. */  
   
       if (repeat_max == -1)  
         {  
         register uschar *ket = previous;  
         do ket += GET(ket, 1); while (*ket != OP_KET);  
         ketoffset = code - ket;  
         }  
   
4572        /* 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
4573        OP_BRAZERO in front of it, and because the group appears once in the        OP_BRAZERO in front of it, and because the group appears once in the
4574        data, whereas in other cases it appears the minimum number of times. For        data, whereas in other cases it appears the minimum number of times. For
# Line 4213  we set the flag only if there is a liter Line 4610  we set the flag only if there is a liter
4610              *previous++ = OP_SKIPZERO;              *previous++ = OP_SKIPZERO;
4611              goto END_REPEAT;              goto END_REPEAT;
4612              }              }
4613              brazeroptr = previous;    /* Save for possessive optimizing */
4614            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
4615            }            }
4616    
# Line 4237  we set the flag only if there is a liter Line 4635  we set the flag only if there is a liter
4635            /* We chain together the bracket offset fields that have to be            /* We chain together the bracket offset fields that have to be
4636            filled in later when the ends of the brackets are reached. */            filled in later when the ends of the brackets are reached. */
4637    
4638            offset = (bralink == NULL)? 0 : previous - bralink;            offset = (bralink == NULL)? 0 : (int)(previous - bralink);
4639            bralink = previous;            bralink = previous;
4640            PUTINC(previous, 0, offset);            PUTINC(previous, 0, offset);
4641            }            }
# Line 4346  we set the flag only if there is a liter Line 4744  we set the flag only if there is a liter
4744              {              {
4745              int offset;              int offset;
4746              *code++ = OP_BRA;              *code++ = OP_BRA;
4747              offset = (bralink == NULL)? 0 : code - bralink;              offset = (bralink == NULL)? 0 : (int)(code - bralink);
4748              bralink = code;              bralink = code;
4749              PUTINC(code, 0, offset);              PUTINC(code, 0, offset);
4750              }              }
# Line 4367  we set the flag only if there is a liter Line 4765  we set the flag only if there is a liter
4765          while (bralink != NULL)          while (bralink != NULL)
4766            {            {
4767            int oldlinkoffset;            int oldlinkoffset;
4768            int offset = code - bralink + 1;            int offset = (int)(code - bralink + 1);
4769            uschar *bra = code - offset;            uschar *bra = code - offset;
4770            oldlinkoffset = GET(bra, 1);            oldlinkoffset = GET(bra, 1);
4771            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
# Line 4377  we set the flag only if there is a liter Line 4775  we set the flag only if there is a liter
4775            }            }
4776          }          }
4777    
4778        /* 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
4779        can't just offset backwards from the current code point, because we        ONCE brackets, that's all we need to do. However, possessively repeated
4780        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
4781        correct offset was computed above.        behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
4782          deal with possessive ONCEs specially.
4783    
4784          Otherwise, if the quantifier was possessive, we convert the BRA code to
4785          the POS form, and the KET code to KETRPOS. (It turns out to be convenient
4786          at runtime to detect this kind of subpattern at both the start and at the
4787          end.) The use of special opcodes makes it possible to reduce greatly the
4788          stack usage in pcre_exec(). If the group is preceded by OP_BRAZERO,
4789          convert this to OP_BRAPOSZERO. Then cancel the possessive flag so that
4790          the default action below, of wrapping everything inside atomic brackets,
4791          does not happen.
4792    
4793        Then, when we are doing the actual compile phase, check to see whether        Then, when we are doing the actual compile phase, check to see whether
4794        this group is a non-atomic one that could match an empty string. If so,        this group is one that could match an empty string. If so, convert the
4795        convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so        initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so that runtime
4796        that runtime checking can be done. [This check is also applied to        checking can be done. [This check is also applied to ONCE groups at
4797        atomic groups at runtime, but in a different way.] */        runtime, but in a different way.] */
4798    
4799        else        else
4800          {          {
4801          uschar *ketcode = code - ketoffset;          uschar *ketcode = code - 1 - LINK_SIZE;
4802          uschar *bracode = ketcode - GET(ketcode, 1);          uschar *bracode = ketcode - GET(ketcode, 1);
4803          *ketcode = OP_KETRMAX + repeat_type;  
4804          if (lengthptr == NULL && *bracode != OP_ONCE)          if (*bracode == OP_ONCE && possessive_quantifier) *bracode = OP_BRA;
4805            if (*bracode == OP_ONCE)
4806              *ketcode = OP_KETRMAX + repeat_type;
4807            else
4808            {            {
4809            uschar *scode = bracode;            if (possessive_quantifier)
4810            do              {
4811                *bracode += 1;                   /* Switch to xxxPOS opcodes */
4812                *ketcode = OP_KETRPOS;
4813                if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
4814                possessive_quantifier = FALSE;
4815                }
4816              else *ketcode = OP_KETRMAX + repeat_type;
4817    
4818              if (lengthptr == NULL)
4819              {              {
4820              if (could_be_empty_branch(scode, ketcode, utf8, cd))              uschar *scode = bracode;
4821                do
4822                {                {
4823                *bracode += OP_SBRA - OP_BRA;                if (could_be_empty_branch(scode, ketcode, utf8, cd))
4824                break;                  {
4825                    *bracode += OP_SBRA - OP_BRA;
4826                    break;
4827                    }
4828                  scode += GET(scode, 1);
4829                }                }
4830              scode += GET(scode, 1);              while (*scode == OP_ALT);
4831              }              }
           while (*scode == OP_ALT);  
4832            }            }
4833          }          }
4834        }        }
# Line 4426  we set the flag only if there is a liter Line 4849  we set the flag only if there is a liter
4849        }        }
4850    
4851      /* If the character following a repeat is '+', or if certain optimization      /* If the character following a repeat is '+', or if certain optimization
4852      tests above succeeded, possessive_quantifier is TRUE. For some of the      tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
4853      simpler opcodes, there is an special alternative opcode for this. For      there are special alternative opcodes for this case. For anything else, we
4854      anything else, we wrap the entire repeated item inside OP_ONCE brackets.      wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
4855      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
4856      but the special opcodes can optimize it a bit. The repeated item starts at      special opcodes can optimize it.
4857      tempcode, not at previous, which might be the first part of a string whose  
4858      (former) last char we repeated.      Possessively repeated subpatterns have already been handled in the code
4859        just above, so possessive_quantifier is always FALSE for them at this
4860        stage.
4861    
4862        Note that the repeated item starts at tempcode, not at previous, which
4863        might be the first part of a string whose (former) last char we repeated.
4864    
4865      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
4866      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 4455  we set the flag only if there is a liter Line 4883  we set the flag only if there is a liter
4883  #endif  #endif
4884          }          }
4885    
4886        len = code - tempcode;        len = (int)(code - tempcode);
4887        if (len > 0) switch (*tempcode)        if (len > 0) switch (*tempcode)
4888          {          {
4889          case OP_STAR:  *tempcode = OP_POSSTAR; break;          case OP_STAR:  *tempcode = OP_POSSTAR; break;
# Line 4463  we set the flag only if there is a liter Line 4891  we set the flag only if there is a liter
4891          case OP_QUERY: *tempcode = OP_POSQUERY; break;          case OP_QUERY: *tempcode = OP_POSQUERY; break;
4892          case OP_UPTO:  *tempcode = OP_POSUPTO; break;          case OP_UPTO:  *tempcode = OP_POSUPTO; break;
4893    
4894          case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;          case OP_STARI:  *tempcode = OP_POSSTARI; break;
4895          case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;          case OP_PLUSI:  *tempcode = OP_POSPLUSI; break;
4896          case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;          case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
4897          case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;          case OP_UPTOI:  *tempcode = OP_POSUPTOI; break;
4898    
4899          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;
4900          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;
4901          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
4902          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;
4903    
4904            case OP_NOTSTARI:  *tempcode = OP_NOTPOSSTARI; break;
4905            case OP_NOTPLUSI:  *tempcode = OP_NOTPOSPLUSI; break;
4906            case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
4907            case OP_NOTUPTOI:  *tempcode = OP_NOTPOSUPTOI; break;
4908    
4909            case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;
4910            case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;
4911            case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
4912            case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;
4913    
4914          /* Because we are moving code along, we must ensure that any          /* Because we are moving code along, we must ensure that any
4915          pending recursive references are updated. */          pending recursive references are updated. */
4916    
4917          default:          default:
4918          *code = OP_END;          *code = OP_END;
4919          adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);          adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);
# Line 4514  we set the flag only if there is a liter Line 4952  we set the flag only if there is a liter
4952    
4953      /* First deal with various "verbs" that can be introduced by '*'. */      /* First deal with various "verbs" that can be introduced by '*'. */
4954    
4955      if (*(++ptr) == CHAR_ASTERISK && (cd->ctypes[ptr[1]] & ctype_letter) != 0)      if (*(++ptr) == CHAR_ASTERISK &&
4956             ((cd->ctypes[ptr[1]] & ctype_letter) != 0 || ptr[1] == ':'))
4957        {        {
4958        int i, namelen;        int i, namelen;
4959          int arglen = 0;
4960        const char *vn = verbnames;        const char *vn = verbnames;
4961        const uschar *name = ++ptr;        const uschar *name = ptr + 1;
4962          const uschar *arg = NULL;
4963        previous = NULL;        previous = NULL;
4964        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};
4965          namelen = (int)(ptr - name);
4966    
4967        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
4968          {          {
4969          *errorcodeptr = ERR59;   /* Not supported */          arg = ++ptr;
4970          goto FAILED;          while ((cd->ctypes[*ptr] & (ctype_letter|ctype_digit)) != 0
4971              || *ptr == '_') ptr++;
4972            arglen = (int)(ptr - arg);
4973          }          }
4974    
4975        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
4976          {          {
4977          *errorcodeptr = ERR60;          *errorcodeptr = ERR60;
4978          goto FAILED;          goto FAILED;
4979          }          }
4980        namelen = ptr - name;  
4981          /* Scan the table of verb names */
4982    
4983        for (i = 0; i < verbcount; i++)        for (i = 0; i < verbcount; i++)
4984          {          {
4985          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
4986              strncmp((char *)name, vn, namelen) == 0)              strncmp((char *)name, vn, namelen) == 0)
4987            {            {
4988            /* Check for open captures before ACCEPT */            /* Check for open captures before ACCEPT and convert it to
4989              ASSERT_ACCEPT if in an assertion. */
4990    
4991            if (verbs[i].op == OP_ACCEPT)            if (verbs[i].op == OP_ACCEPT)
4992              {              {
4993              open_capitem *oc;              open_capitem *oc;
4994                if (arglen != 0)
4995                  {
4996                  *errorcodeptr = ERR59;
4997                  goto FAILED;
4998                  }
4999              cd->had_accept = TRUE;              cd->had_accept = TRUE;
5000              for (oc = cd->open_caps; oc != NULL; oc = oc->next)              for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5001                {                {
5002                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5003                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5004                }                }
5005                *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5006              }              }
5007            *code++ = verbs[i].op;  
5008            break;            /* Handle other cases with/without an argument */
5009    
5010              else if (arglen == 0)
5011                {
5012                if (verbs[i].op < 0)   /* Argument is mandatory */
5013                  {
5014                  *errorcodeptr = ERR66;
5015                  goto FAILED;
5016                  }
5017                *code = verbs[i].op;
5018                if (*code++ == OP_THEN)
5019                  {
5020                  PUT(code, 0, code - bcptr->current_branch - 1);
5021                  code += LINK_SIZE;
5022                  }
5023                }
5024    
5025              else
5026                {
5027                if (verbs[i].op_arg < 0)   /* Argument is forbidden */
5028                  {
5029                  *errorcodeptr = ERR59;
5030                  goto FAILED;
5031                  }
5032                *code = verbs[i].op_arg;
5033                if (*code++ == OP_THEN_ARG)
5034                  {
5035                  PUT(code, 0, code - bcptr->current_branch - 1);
5036                  code += LINK_SIZE;
5037                  }
5038                *code++ = arglen;
5039                memcpy(code, arg, arglen);
5040                code += arglen;
5041                *code++ = 0;
5042                }
5043    
5044              break;  /* Found verb, exit loop */
5045            }            }
5046    
5047          vn += verbs[i].len + 1;          vn += verbs[i].len + 1;
5048          }          }
5049        if (i < verbcount) continue;  
5050        *errorcodeptr = ERR60;        if (i < verbcount) continue;    /* Successfully handled a verb */
5051          *errorcodeptr = ERR60;          /* Verb not recognized */
5052        goto FAILED;        goto FAILED;
5053        }        }
5054    
# Line 4674  we set the flag only if there is a liter Line 5167  we set the flag only if there is a liter
5167                recno * 10 + *ptr - CHAR_0 : -1;                recno * 10 + *ptr - CHAR_0 : -1;
5168            ptr++;            ptr++;
5169            }            }
5170          namelen = ptr - name;          namelen = (int)(ptr - name);
5171    
5172          if ((terminator > 0 && *ptr++ != terminator) ||          if ((terminator > 0 && *ptr++ != terminator) ||
5173              *ptr++ != CHAR_RIGHT_PARENTHESIS)              *ptr++ != CHAR_RIGHT_PARENTHESIS)
# Line 4735  we set the flag only if there is a liter Line 5228  we set the flag only if there is a liter
5228          /* Search the pattern for a forward reference */          /* Search the pattern for a forward reference */
5229    
5230          else if ((i = find_parens(cd, name, namelen,          else if ((i = find_parens(cd, name, namelen,
5231                          (options & PCRE_EXTENDED) != 0)) > 0)                          (options & PCRE_EXTENDED) != 0, utf8)) > 0)
5232            {            {
5233            PUT2(code, 2+LINK_SIZE, i);            PUT2(code, 2+LINK_SIZE, i);
5234            code[1+LINK_SIZE]++;            code[1+LINK_SIZE]++;
# Line 4803  we set the flag only if there is a liter Line 5296  we set the flag only if there is a liter
5296          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5297          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */
5298          bravalue = OP_ASSERT;          bravalue = OP_ASSERT;
5299            cd->assert_depth += 1;
5300          ptr++;          ptr++;
5301          break;          break;
5302    
# Line 4817  we set the flag only if there is a liter Line 5311  we set the flag only if there is a liter
5311            continue;            continue;
5312            }            }
5313          bravalue = OP_ASSERT_NOT;          bravalue = OP_ASSERT_NOT;
5314            cd->assert_depth += 1;
5315          break;          break;
5316    
5317    
# Line 4826  we set the flag only if there is a liter Line 5321  we set the flag only if there is a liter
5321            {            {
5322            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */
5323            bravalue = OP_ASSERTBACK;            bravalue = OP_ASSERTBACK;
5324              cd->assert_depth += 1;
5325            ptr += 2;            ptr += 2;
5326            break;            break;
5327    
5328            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */
5329            bravalue = OP_ASSERTBACK_NOT;            bravalue = OP_ASSERTBACK_NOT;
5330              cd->assert_depth += 1;
5331            ptr += 2;            ptr += 2;
5332            break;            break;
5333    
# Line 4870  we set the flag only if there is a liter Line 5367  we set the flag only if there is a liter
5367              goto FAILED;              goto FAILED;
5368              }              }
5369            *code++ = n;            *code++ = n;
5370            PUT(code, 0, ptr - cd->start_pattern + 1);  /* Pattern offset */            PUT(code, 0, (int)(ptr - cd->start_pattern + 1)); /* Pattern offset */
5371            PUT(code, LINK_SIZE, 0);                    /* Default length */            PUT(code, LINK_SIZE, 0);                          /* Default length */
5372            code += 2 * LINK_SIZE;            code += 2 * LINK_SIZE;
5373            }            }
5374          previous = NULL;          previous = NULL;
# Line 4904  we set the flag only if there is a liter Line 5401  we set the flag only if there is a liter
5401            name = ++ptr;            name = ++ptr;
5402    
5403            while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;            while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5404            namelen = ptr - name;            namelen = (int)(ptr - name);
5405    
5406            /* In the pre-compile phase, just do a syntax check. */            /* In the pre-compile phase, just do a syntax check. */
5407    
# Line 5034  we set the flag only if there is a liter Line 5531  we set the flag only if there is a liter
5531          NAMED_REF_OR_RECURSE:          NAMED_REF_OR_RECURSE:
5532          name = ++ptr;          name = ++ptr;
5533          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5534          namelen = ptr - name;          namelen = (int)(ptr - name);
5535    
5536          /* 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
5537          reference number. */          a dummy reference number, because it was not used in the first pass.
5538            However, with the change of recursive back references to be atomic,
5539            we have to look for the number so that this state can be identified, as
5540            otherwise the incorrect length is computed. If it's not a backwards
5541            reference, the dummy number will do. */
5542    
5543          if (lengthptr != NULL)          if (lengthptr != NULL)
5544            {            {
5545              const uschar *temp;
5546    
5547            if (namelen == 0)            if (namelen == 0)
5548              {              {
5549              *errorcodeptr = ERR62;              *errorcodeptr = ERR62;
# Line 5056  we set the flag only if there is a liter Line 5559  we set the flag only if there is a liter
5559              *errorcodeptr = ERR48;              *errorcodeptr = ERR48;
5560              goto FAILED;              goto FAILED;
5561              }              }
5562            recno = 0;  
5563              /* The name table does not exist in the first pass, so we cannot
5564              do a simple search as in the code below. Instead, we have to scan the
5565              pattern to find the number. It is important that we scan it only as
5566              far as we have got because the syntax of named subpatterns has not
5567              been checked for the rest of the pattern, and find_parens() assumes
5568              correct syntax. In any case, it's a waste of resources to scan
5569              further. We stop the scan at the current point by temporarily
5570              adjusting the value of cd->endpattern. */
5571    
5572              temp = cd->end_pattern;
5573              cd->end_pattern = ptr;
5574              recno = find_parens(cd, name, namelen,
5575                (options & PCRE_EXTENDED) != 0, utf8);
5576              cd->end_pattern = temp;
5577              if (recno < 0) recno = 0;    /* Forward ref; set dummy number */
5578            }            }
5579    
5580          /* 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 5081  we set the flag only if there is a liter Line 5599  we set the flag only if there is a liter
5599              }              }
5600            else if ((recno =                /* Forward back reference */            else if ((recno =                /* Forward back reference */
5601                      find_parens(cd, name, namelen,                      find_parens(cd, name, namelen,
5602                        (options & PCRE_EXTENDED) != 0)) <= 0)                        (options & PCRE_EXTENDED) != 0, utf8)) <= 0)
5603              {              {
5604              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
5605              goto FAILED;              goto FAILED;
# Line 5192  we set the flag only if there is a liter Line 5710  we set the flag only if there is a liter
5710              if (called == NULL)              if (called == NULL)
5711                {                {
5712                if (find_parens(cd, NULL, recno,                if (find_parens(cd, NULL, recno,
5713                      (options & PCRE_EXTENDED) != 0) < 0)                      (options & PCRE_EXTENDED) != 0, utf8) < 0)
5714                  {                  {
5715                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
5716                  goto FAILED;                  goto FAILED;
5717                  }                  }
5718    
5719                /* 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
5720                offset below, what it actually inserted is the reference number                offset below, what it actually inserted is the reference number
5721                of the group. */                of the group. Then remember the forward reference. */
5722    
5723                called = cd->start_code + recno;                called = cd->start_code + recno;
5724                PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code);                PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
5725                }                }
5726    
5727              /* If not a forward reference, and the subpattern is still open,              /* If not a forward reference, and the subpattern is still open,
# Line 5218  we set the flag only if there is a liter Line 5736  we set the flag only if there is a liter
5736                }                }
5737              }              }
5738    
5739            /* Insert the recursion/subroutine item, automatically wrapped inside            /* Insert the recursion/subroutine item. */
5740            "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;  
   
5741            *code = OP_RECURSE;            *code = OP_RECURSE;
5742            PUT(code, 1, called - cd->start_code);            PUT(code, 1, (int)(called - cd->start_code));
5743            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;  
5744            }            }
5745    
5746          /* Can't determine a first byte now */          /* Can't determine a first byte now */
# Line 5295  we set the flag only if there is a liter Line 5801  we set the flag only if there is a liter
5801          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
5802          both phases.          both phases.
5803    
5804          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
5805          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. */  
5806    
5807          if (*ptr == CHAR_RIGHT_PARENTHESIS)          if (*ptr == CHAR_RIGHT_PARENTHESIS)
5808            {            {
# Line 5308  we set the flag only if there is a liter Line 5813  we set the flag only if there is a liter
5813              }              }
5814            else            else
5815              {              {
             if ((options & PCRE_IMS) != (newoptions & PCRE_IMS))  
               {  
               *code++ = OP_OPT;  
               *code++ = newoptions & PCRE_IMS;  
               }  
5816              greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);              greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
5817              greedy_non_default = greedy_default ^ 1;              greedy_non_default = greedy_default ^ 1;
5818              req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;              req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;
5819              }              }
5820    
5821            /* Change options at this level, and pass them back for use            /* Change options at this level, and pass them back for use
5822            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). */  
5823    
5824            *optionsptr = options = newoptions;            *optionsptr = options = newoptions;
5825            previous = NULL;       /* This item can't be repeated */            previous = NULL;       /* This item can't be repeated */
# Line 5338  we set the flag only if there is a liter Line 5836  we set the flag only if there is a liter
5836          }     /* End of switch for character following (? */          }     /* End of switch for character following (? */
5837        }       /* End of (? handling */        }       /* End of (? handling */
5838    
5839      /* Opening parenthesis not followed by '?'. If PCRE_NO_AUTO_CAPTURE is set,      /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
5840      all unadorned brackets become non-capturing and behave like (?:...)      is set, all unadorned brackets become non-capturing and behave like (?:...)
5841      brackets. */      brackets. */
5842    
5843      else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)      else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)
# Line 5359  we set the flag only if there is a liter Line 5857  we set the flag only if there is a liter
5857    
5858      /* Process nested bracketed regex. Assertions may not be repeated, but      /* Process nested bracketed regex. Assertions may not be repeated, but
5859      other kinds can be. All their opcodes are >= OP_ONCE. We copy code into a      other kinds can be. All their opcodes are >= OP_ONCE. We copy code into a
5860      non-register variable in order to be able to pass its address because some      non-register variable (tempcode) in order to be able to pass its address
5861      compilers complain otherwise. Pass in a new setting for the ims options if      because some compilers complain otherwise. */
     they have changed. */  
5862    
5863      previous = (bravalue >= OP_ONCE)? code : NULL;      previous = (bravalue >= OP_ONCE)? code : NULL;
5864      *code = bravalue;      *code = bravalue;
# Line 5371  we set the flag only if there is a liter Line 5868  we set the flag only if there is a liter
5868    
5869      if (!compile_regex(      if (!compile_regex(
5870           newoptions,                   /* The complete new option state */           newoptions,                   /* The complete new option state */
          options & PCRE_IMS,           /* The previous ims option state */  
5871           &tempcode,                    /* Where to put code (updated) */           &tempcode,                    /* Where to put code (updated) */
5872           &ptr,                         /* Input pointer (updated) */           &ptr,                         /* Input pointer (updated) */
5873           errorcodeptr,                 /* Where to put an error message */           errorcodeptr,                 /* Where to put an error message */
# Line 5387  we set the flag only if there is a liter Line 5883  we set the flag only if there is a liter
5883             &length_prevgroup           /* Pre-compile phase */             &length_prevgroup           /* Pre-compile phase */
5884           ))           ))
5885        goto FAILED;        goto FAILED;
5886    
5887        if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
5888          cd->assert_depth -= 1;
5889    
5890      /* 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
5891      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
# Line 5458  we set the flag only if there is a liter Line 5957  we set the flag only if there is a liter
5957          goto FAILED;          goto FAILED;
5958          }          }
5959        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
5960        *code++ = OP_BRA;        code++;   /* This already contains bravalue */
5961        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
5962        *code++ = OP_KET;        *code++ = OP_KET;
5963        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
# Line 5531  we set the flag only if there is a liter Line 6030  we set the flag only if there is a liter
6030    
6031      /* ===================================================================*/      /* ===================================================================*/
6032      /* Handle metasequences introduced by \. For ones like \d, the ESC_ values      /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
6033      are arranged to be the negation of the corresponding OP_values. For the      are arranged to be the negation of the corresponding OP_values in the
6034      back references, the values are ESC_REF plus the reference number. Only      default case when PCRE_UCP is not set. For the back references, the values
6035      back references and those types that consume a character may be repeated.      are ESC_REF plus the reference number. Only back references and those types
6036      We can test for values between ESC_b and ESC_Z for the latter; this may      that consume a character may be repeated. We can test for values between
6037      have to change if any new ones are ever created. */      ESC_b and ESC_Z for the latter; this may have to change if any new ones are
6038        ever created. */
6039    
6040      case CHAR_BACKSLASH:      case CHAR_BACKSLASH:
6041      tempptr = ptr;      tempptr = ptr;
# Line 5649  we set the flag only if there is a liter Line 6149  we set the flag only if there is a liter
6149          HANDLE_REFERENCE:    /* Come here from named backref handling */          HANDLE_REFERENCE:    /* Come here from named backref handling */
6150          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
6151          previous = code;          previous = code;
6152          *code++ = OP_REF;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
6153          PUT2INC(code, 0, recno);          PUT2INC(code, 0, recno);
6154          cd->backref_map |= (recno < 32)? (1 << recno) : 1;          cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6155          if (recno > cd->top_backref) cd->top_backref = recno;          if (recno > cd->top_backref) cd->top_backref = recno;
# Line 5695  we set the flag only if there is a liter Line 6195  we set the flag only if there is a liter
6195  #endif  #endif
6196    
6197        /* For the rest (including \X when Unicode properties are supported), we        /* For the rest (including \X when Unicode properties are supported), we
6198        can obtain the OP value by negating the escape value. */        can obtain the OP value by negating the escape value in the default
6199          situation when PCRE_UCP is not set. When it *is* set, we substitute
6200          Unicode property tests. */
6201    
6202        else        else
6203          {          {
6204          previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;  #ifdef SUPPORT_UCP
6205          *code++ = -c;          if (-c >= ESC_DU && -c <= ESC_wu)
6206              {
6207              nestptr = ptr + 1;                   /* Where to resume */
6208              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */
6209              }
6210            else
6211    #endif
6212              {
6213              previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;
6214              *code++ = -c;
6215              }
6216          }          }
6217        continue;        continue;
6218        }        }
# Line 5745  we set the flag only if there is a liter Line 6257  we set the flag only if there is a liter
6257    
6258      ONE_CHAR:      ONE_CHAR:
6259      previous = code;      previous = code;
6260      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARNC : OP_CHAR;      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR;
6261      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
6262    
6263      /* Remember if \r or \n were seen */      /* Remember if \r or \n were seen */
# Line 5809  return FALSE; Line 6321  return FALSE;
6321  /* 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
6322  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
6323  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.  
   
6324  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
6325  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
6326  value of lengthptr distinguishes the two phases.  value of lengthptr distinguishes the two phases.
6327    
6328  Arguments:  Arguments:
6329    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  
6330    codeptr        -> the address of the current code pointer    codeptr        -> the address of the current code pointer
6331    ptrptr         -> the address of the current pattern pointer    ptrptr         -> the address of the current pattern pointer
6332    errorcodeptr   -> pointer to error code variable    errorcodeptr   -> pointer to error code variable
# Line 5838  Returns:         TRUE on success Line 6344  Returns:         TRUE on success
6344  */  */
6345    
6346  static BOOL  static BOOL
6347  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,  compile_regex(int options, uschar **codeptr, const uschar **ptrptr,
6348    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
6349    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,
6350    int *lengthptr)    int *lengthptr)
# Line 5855  int branchfirstbyte, branchreqbyte; Line 6361  int branchfirstbyte, branchreqbyte;
6361  int length;  int length;
6362  int orig_bracount;  int orig_bracount;
6363  int max_bracount;  int max_bracount;
 int old_external_options = cd->external_options;  
6364  branch_chain bc;  branch_chain bc;
6365    
6366  bc.outer = bcptr;  bc.outer = bcptr;
# Line 5879  pre-compile phase to find out whether an Line 6384  pre-compile phase to find out whether an
6384    
6385  /* 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
6386  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
6387  detect groups that contain recursive back references to themselves. */  detect groups that contain recursive back references to themselves. Note that
6388    only OP_CBRA need be tested here; changing this opcode to one of its variants,
6389    e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
6390    
6391  if (*code == OP_CBRA)  if (*code == OP_CBRA)
6392    {    {
# Line 5905  for (;;) Line 6412  for (;;)
6412    
6413    if (reset_bracount) cd->bracount = orig_bracount;    if (reset_bracount) cd->bracount = orig_bracount;
6414    
   /* 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;  
     }  
   
6415    /* Set up dummy OP_REVERSE if lookbehind assertion */    /* Set up dummy OP_REVERSE if lookbehind assertion */
6416    
6417    if (lookbehind)    if (lookbehind)
# Line 5934  for (;;) Line 6432  for (;;)
6432      return FALSE;      return FALSE;
6433      }      }
6434    
   /* 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;  
   
6435    /* Keep the highest bracket count in case (?| was used and some branch    /* Keep the highest bracket count in case (?| was used and some branch
6436    has fewer than the rest. */    has fewer than the rest. */
6437    
# Line 6003  for (;;) Line 6492  for (;;)
6492        {        {
6493        int fixed_length;        int fixed_length;
6494        *code = OP_END;        *code = OP_END;
6495        fixed_length = find_fixedlength(last_branch, options, FALSE, cd);        fixed_length = find_fixedlength(last_branch,  (options & PCRE_UTF8) != 0,
6496            FALSE, cd);
6497        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
6498        if (fixed_length == -3)        if (fixed_length == -3)
6499          {          {
# Line 6024  for (;;) Line 6514  for (;;)
6514    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
6515    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
6516    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
6517    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. */  
6518    
6519    if (*ptr != CHAR_VERTICAL_LINE)    if (*ptr != CHAR_VERTICAL_LINE)
6520      {      {
6521      if (lengthptr == NULL)      if (lengthptr == NULL)
6522        {        {
6523        int branch_length = code - last_branch;        int branch_length = (int)(code - last_branch);
6524        do        do
6525          {          {
6526          int prev_length = GET(last_branch, 1);          int prev_length = GET(last_branch, 1);
# Line 6046  for (;;) Line 6534  for (;;)
6534      /* Fill in the ket */      /* Fill in the ket */
6535    
6536      *code = OP_KET;      *code = OP_KET;
6537      PUT(code, 1, code - start_bracket);      PUT(code, 1, (int)(code - start_bracket));
6538      code += 1 + LINK_SIZE;      code += 1 + LINK_SIZE;
6539    
6540      /* If it was a capturing subpattern, check to see if it contained any      /* If it was a capturing subpattern, check to see if it contained any
# Line 6061  for (;;) Line 6549  for (;;)
6549            code - start_bracket);            code - start_bracket);
6550          *start_bracket = OP_ONCE;          *start_bracket = OP_ONCE;
6551          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
6552          PUT(start_bracket, 1, code - start_bracket);          PUT(start_bracket, 1, (int)(code - start_bracket));
6553          *code = OP_KET;          *code = OP_KET;
6554          PUT(code, 1, code - start_bracket);          PUT(code, 1, (int)(code - start_bracket));
6555          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
6556          length += 2 + 2*LINK_SIZE;          length += 2 + 2*LINK_SIZE;
6557          }          }
6558        cd->open_caps = cd->open_caps->next;        cd->open_caps = cd->open_caps->next;
6559        }        }
6560    
     /* Reset options if needed. */  
   
     if ((options & PCRE_IMS) != oldims && *ptr == CHAR_RIGHT_PARENTHESIS)  
       {  
       *code++ = OP_OPT;  
       *code++ = oldims;  
       length += 2;  
       }  
   
6561      /* Retain the highest bracket number, in case resetting was used. */      /* Retain the highest bracket number, in case resetting was used. */
6562    
6563      cd->bracount = max_bracount;      cd->bracount = max_bracount;
# Line 6118  for (;;) Line 6597  for (;;)
6597    else    else
6598      {      {
6599      *code = OP_ALT;      *code = OP_ALT;
6600      PUT(code, 1, code - last_branch);      PUT(code, 1, (int)(code - last_branch));
6601      bc.current_branch = last_branch = code;      bc.current_branch = last_branch = code;
6602      code += 1 + LINK_SIZE;      code += 1 + LINK_SIZE;
6603      }      }
# Line 6138  for (;;) Line 6617  for (;;)
6617  /* 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
6618  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
6619  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
6620  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
6621  counts, since OP_CIRC can match in the middle.  be found, because ^ generates OP_CIRCM in that mode.
6622    
6623  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.
6624  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 6160  of the more common cases more precisely. Line 6639  of the more common cases more precisely.
6639    
6640  Arguments:  Arguments:
6641    code           points to start of expression (the bracket)    code           points to start of expression (the bracket)
   options        points to the options setting  
6642    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
6643                    handles up to substring 31; after that we just have to take                    handles up to substring 31; after that we just have to take
6644                    the less precise approach                    the less precise approach
# Line 6170  Returns:     TRUE or FALSE Line 6648  Returns:     TRUE or FALSE
6648  */  */
6649    
6650  static BOOL  static BOOL
6651  is_anchored(register const uschar *code, int *options, unsigned int bracket_map,  is_anchored(register const uschar *code, unsigned int bracket_map,
6652    unsigned int backref_map)    unsigned int backref_map)
6653  {  {
6654  do {  do {
6655     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],
6656       options, PCRE_MULTILINE, FALSE);       FALSE);
6657     register int op = *scode;     register int op = *scode;
6658    
6659     /* Non-capturing brackets */     /* Non-capturing brackets */
6660    
6661     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
6662           op == OP_SBRA || op == OP_SBRAPOS)
6663       {       {
6664       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
6665       }       }
6666    
6667     /* Capturing brackets */     /* Capturing brackets */
6668    
6669     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
6670                op == OP_SCBRA || op == OP_SCBRAPOS)
6671       {       {
6672       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
6673       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
6674       if (!is_anchored(scode, options, new_map, backref_map)) return FALSE;       if (!is_anchored(scode, new_map, backref_map)) return FALSE;
6675       }       }
6676    
6677     /* Other brackets */     /* Other brackets */
6678    
6679     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)
6680       {       {
6681       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
6682       }       }
6683    
6684     /* .* 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 6213  do { Line 6693  do {
6693    
6694     /* Check for explicit anchoring */     /* Check for explicit anchoring */
6695    
6696     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;  
6697     code += GET(code, 1);     code += GET(code, 1);
6698     }     }
6699  while (*code == OP_ALT);   /* Loop for each alternative */  while (*code == OP_ALT);   /* Loop for each alternative */
# Line 6251  is_startline(const uschar *code, unsigne Line 6729  is_startline(const uschar *code, unsigne
6729  {  {
6730  do {  do {
6731     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],
6732       NULL, 0, FALSE);       FALSE);
6733     register int op = *scode;     register int op = *scode;
6734    
6735     /* 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 6278  do { Line 6756  do {
6756         scode += 1 + LINK_SIZE;         scode += 1 + LINK_SIZE;
6757         break;         break;
6758         }         }
6759       scode = first_significant_code(scode, NULL, 0, FALSE);       scode = first_significant_code(scode, FALSE);
6760       op = *scode;       op = *scode;
6761       }       }
6762    
6763     /* Non-capturing brackets */     /* Non-capturing brackets */
6764    
6765     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
6766           op == OP_SBRA || op == OP_SBRAPOS)
6767       {       {
6768       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;
6769       }       }
6770    
6771     /* Capturing brackets */     /* Capturing brackets */
6772    
6773     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
6774                op == OP_SCBRA || op == OP_SCBRAPOS)
6775       {       {
6776       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
6777       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
# Line 6315  do { Line 6795  do {
6795    
6796     /* Check for explicit circumflex */     /* Check for explicit circumflex */
6797    
6798     else if (op != OP_CIRC) return FALSE;     else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;
6799    
6800     /* Move on to the next alternative */     /* Move on to the next alternative */
6801    
# Line 6341  we return that char, otherwise -1. Line 6821  we return that char, otherwise -1.
6821    
6822  Arguments:  Arguments:
6823    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)  
6824    inassert   TRUE if in an assertion    inassert   TRUE if in an assertion
6825    
6826  Returns:     -1 or the fixed first char  Returns:     -1 or the fixed first char
6827  */  */
6828    
6829  static int  static int
6830  find_firstassertedchar(const uschar *code, int *options, BOOL inassert)  find_firstassertedchar(const uschar *code, BOOL inassert)
6831  {  {
6832  register int c = -1;  register int c = -1;
6833  do {  do {
6834     int d;     int d;
6835     const uschar *scode =     int xl = (*code == OP_CBRA || *code == OP_SCBRA ||
6836       first_significant_code(code + 1+LINK_SIZE, options, PCRE_CASELESS, TRUE);               *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? 2:0;
6837       const uschar *scode = first_significant_code(code + 1+LINK_SIZE + xl, TRUE);
6838     register int op = *scode;     register int op = *scode;
6839    
6840     switch(op)     switch(op)
# Line 6363  do { Line 6843  do {
6843       return -1;       return -1;
6844    
6845       case OP_BRA:       case OP_BRA:
6846         case OP_BRAPOS:
6847       case OP_CBRA:       case OP_CBRA:
6848         case OP_SCBRA:
6849         case OP_CBRAPOS:
6850         case OP_SCBRAPOS:
6851       case OP_ASSERT:       case OP_ASSERT:
6852       case OP_ONCE:       case OP_ONCE:
6853       case OP_COND:       case OP_COND:
6854       if ((d = find_firstassertedchar(scode, options, op == OP_ASSERT)) < 0)       if ((d = find_firstassertedchar(scode, op == OP_ASSERT)) < 0)
6855         return -1;         return -1;
6856       if (c < 0) c = d; else if (c != d) return -1;       if (c < 0) c = d; else if (c != d) return -1;
6857       break;       break;
6858    
6859       case OP_EXACT:       /* Fall through */       case OP_EXACT:
6860       scode += 2;       scode += 2;
6861         /* Fall through */
6862    
6863       case OP_CHAR:       case OP_CHAR:
      case OP_CHARNC:  
6864       case OP_PLUS:       case OP_PLUS:
6865       case OP_MINPLUS:       case OP_MINPLUS:
6866       case OP_POSPLUS:       case OP_POSPLUS:
6867       if (!inassert) return -1;       if (!inassert) return -1;
6868       if (c < 0)       if (c < 0) c = scode[1];
6869         {         else if (c != scode[1]) return -1;
6870         c = scode[1];       break;
6871         if ((*options & PCRE_CASELESS) != 0) c |= REQ_CASELESS;  
6872         }       case OP_EXACTI:
6873       else if (c != scode[1]) return -1;       scode += 2;
6874         /* Fall through */
6875    
6876         case OP_CHARI:
6877         case OP_PLUSI:
6878         case OP_MINPLUSI:
6879         case OP_POSPLUSI:
6880         if (!inassert) return -1;
6881         if (c < 0) c = scode[1] | REQ_CASELESS;
6882           else if (c != scode[1]) return -1;
6883       break;       break;
6884       }       }
6885    
# Line 6437  int length = 1;  /* For final END opcode Line 6930  int length = 1;  /* For final END opcode
6930  int firstbyte, reqbyte, newline;  int firstbyte, reqbyte, newline;
6931  int errorcode = 0;  int errorcode = 0;
6932  int skipatstart = 0;  int skipatstart = 0;
6933  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8;
6934  size_t size;  size_t size;
6935  uschar *code;  uschar *code;
6936  const uschar *codestart;  const uschar *codestart;
# Line 6507  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 7000  while (ptr[skipatstart] == CHAR_LEFT_PAR
7000    
7001    if (strncmp((char *)(ptr+skipatstart+2), STRING_UTF8_RIGHTPAR, 5) == 0)    if (strncmp((char *)(ptr+skipatstart+2), STRING_UTF8_RIGHTPAR, 5) == 0)
7002      { skipatstart += 7; options |= PCRE_UTF8; continue; }      { skipatstart += 7; options |= PCRE_UTF8; continue; }
7003      else if (strncmp((char *)(ptr+skipatstart+2), STRING_UCP_RIGHTPAR, 4) == 0)
7004        { skipatstart += 6; options |= PCRE_UCP; continue; }
7005      else if (strncmp((char *)(ptr+skipatstart+2), STRING_NO_START_OPT_RIGHTPAR, 13) == 0)
7006        { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }
7007    
7008    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)
7009      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
# Line 6531  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 7028  while (ptr[skipatstart] == CHAR_LEFT_PAR
7028    else break;    else break;
7029    }    }
7030    
7031  /* Can't support UTF8 unless PCRE has been compiled to include the code. */  utf8 = (options & PCRE_UTF8) != 0;
7032    
7033    /* Can't support UTF8 unless PCRE has been compiled to include the code. The
7034    return of an error code from _pcre_valid_utf8() is a new feature, introduced in
7035    release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
7036    not used here. */
7037    
7038  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
7039  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&
7040       (*erroroffset = _pcre_valid_utf8((USPTR)pattern, -1)) >= 0)       (errorcode = _pcre_valid_utf8((USPTR)pattern, -1, erroroffset)) != 0)
7041    {    {
7042    errorcode = ERR44;    errorcode = ERR44;
7043    goto PCRE_EARLY_ERROR_RETURN2;    goto PCRE_EARLY_ERROR_RETURN2;
# Line 6548  if (utf8) Line 7050  if (utf8)
7050    }    }
7051  #endif  #endif
7052    
7053    /* Can't support UCP unless PCRE has been compiled to include the code. */
7054    
7055    #ifndef SUPPORT_UCP
7056    if ((options & PCRE_UCP) != 0)
7057      {
7058      errorcode = ERR67;
7059      goto PCRE_EARLY_ERROR_RETURN;
7060      }
7061    #endif
7062    
7063  /* Check validity of \R options. */  /* Check validity of \R options. */
7064    
7065  switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))  switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
# Line 6641  outside can help speed up starting point Line 7153  outside can help speed up starting point
7153  ptr += skipatstart;  ptr += skipatstart;
7154  code = cworkspace;  code = cworkspace;
7155  *code = OP_BRA;  *code = OP_BRA;
7156  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,  (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,
7157    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,    FALSE, 0, &firstbyte, &reqbyte, NULL, cd, &length);
   &length);  
7158  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
7159    
7160  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
# Line 6676  regex compiled on a system with 4-byte p Line 7187  regex compiled on a system with 4-byte p
7187  pointers. */  pointers. */
7188    
7189  re->magic_number = MAGIC_NUMBER;  re->magic_number = MAGIC_NUMBER;
7190  re->size = size;  re->size = (int)size;
7191  re->options = cd->external_options;  re->options = cd->external_options;
7192  re->flags = cd->external_flags;  re->flags = cd->external_flags;
7193  re->dummy1 = 0;  re->dummy1 = 0;
# Line 6697  field; this time it's used for rememberi Line 7208  field; this time it's used for rememberi
7208  */  */
7209    
7210  cd->final_bracount = cd->bracount;  /* Save for checking forward references */  cd->final_bracount = cd->bracount;  /* Save for checking forward references */
7211    cd->assert_depth = 0;
7212  cd->bracount = 0;  cd->bracount = 0;
7213  cd->names_found = 0;  cd->names_found = 0;
7214  cd->name_table = (uschar *)re + re->name_table_offset;  cd->name_table = (uschar *)re + re->name_table_offset;
# Line 6715  of the function here. */ Line 7227  of the function here. */
7227  ptr = (const uschar *)pattern + skipatstart;  ptr = (const uschar *)pattern + skipatstart;
7228  code = (uschar *)codestart;  code = (uschar *)codestart;
7229  *code = OP_BRA;  *code = OP_BRA;
7230  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,  (void)compile_regex(re->options, &code, &ptr, &errorcode, FALSE, FALSE, 0,
7231    &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);    &firstbyte, &reqbyte, NULL, cd, NULL);
7232  re->top_bracket = cd->bracount;  re->top_bracket = cd->bracount;
7233  re->top_backref = cd->top_backref;  re->top_backref = cd->top_backref;
7234  re->flags = cd->external_flags;  re->flags = cd->external_flags;
# Line 6747  while (errorcode == 0 && cd->hwm > cwork Line 7259  while (errorcode == 0 && cd->hwm > cwork
7259    recno = GET(codestart, offset);    recno = GET(codestart, offset);
7260    groupptr = _pcre_find_bracket(codestart, utf8, recno);    groupptr = _pcre_find_bracket(codestart, utf8, recno);
7261    if (groupptr == NULL) errorcode = ERR53;    if (groupptr == NULL) errorcode = ERR53;
7262      else PUT(((uschar *)codestart), offset, groupptr - codestart);      else PUT(((uschar *)codestart), offset, (int)(groupptr - codestart));
7263    }    }
7264    
7265  /* Give an error if there's back reference to a non-existent capturing  /* Give an error if there's back reference to a non-existent capturing
# Line 6782  if (cd->check_lookbehind) Line 7294  if (cd->check_lookbehind)
7294        uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);        uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);
7295        int end_op = *be;        int end_op = *be;
7296        *be = OP_END;        *be = OP_END;
7297        fixed_length = find_fixedlength(cc, re->options, TRUE, cd);        fixed_length = find_fixedlength(cc, (re->options & PCRE_UTF8) != 0, TRUE,
7298            cd);
7299        *be = end_op;        *be = end_op;
7300        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
7301        if (fixed_length < 0)        if (fixed_length < 0)
# Line 6802  if (errorcode != 0) Line 7315  if (errorcode != 0)
7315    {    {
7316    (pcre_free)(re);    (pcre_free)(re);
7317    PCRE_EARLY_ERROR_RETURN:    PCRE_EARLY_ERROR_RETURN:
7318    *erroroffset = ptr - (const uschar *)pattern;    *erroroffset = (int)(ptr - (const uschar *)pattern);
7319    PCRE_EARLY_ERROR_RETURN2:    PCRE_EARLY_ERROR_RETURN2:
7320    *errorptr = find_error_text(errorcode);    *errorptr = find_error_text(errorcode);
7321    if (errorcodeptr != NULL) *errorcodeptr = errorcode;    if (errorcodeptr != NULL) *errorcodeptr = errorcode;
# Line 6821  start with ^. and also when all branches Line 7334  start with ^. and also when all branches
7334    
7335  if ((re->options & PCRE_ANCHORED) == 0)  if ((re->options & PCRE_ANCHORED) == 0)
7336    {    {
7337    int temp_options = re->options;   /* May get changed during these scans */    if (is_anchored(codestart, 0, cd->backref_map))
   if (is_anchored(codestart, &temp_options, 0, cd->backref_map))  
7338      re->options |= PCRE_ANCHORED;      re->options |= PCRE_ANCHORED;
7339    else    else
7340      {      {
7341      if (firstbyte < 0)      if (firstbyte < 0)
7342        firstbyte = find_firstassertedchar(codestart, &temp_options, FALSE);        firstbyte = find_firstassertedchar(codestart, FALSE);
7343      if (firstbyte >= 0)   /* Remove caseless flag for non-caseable chars */      if (firstbyte >= 0)   /* Remove caseless flag for non-caseable chars */
7344        {        {
7345        int ch = firstbyte & 255;        int ch = firstbyte & 255;

Legend:
Removed from v.505  
changed lines
  Added in v.624

  ViewVC Help
Powered by ViewVC 1.1.5