/[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 406 by ph10, Mon Mar 23 12:05:43 2009 UTC revision 514 by ph10, Mon May 3 12:54:22 2010 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-2009 University of Cambridge             Copyright (c) 1997-2010 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 53  supporting internal functions that are n Line 53  supporting internal functions that are n
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    
56  /* When DEBUG is defined, we need the pcre_printint() function, which is also  /* When PCRE_DEBUG is defined, we need the pcre_printint() function, which is
57  used by pcretest. DEBUG is not defined when building a production library. */  also used by pcretest. PCRE_DEBUG is not defined when building a production
58    library. */
59    
60  #ifdef DEBUG  #ifdef PCRE_DEBUG
61  #include "pcre_printint.src"  #include "pcre_printint.src"
62  #endif  #endif
63    
# Line 91  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
96    overrun before it actually does run off the end of the data block. */
97    
98    #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)
99    
100    
101  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
102  are simple data values; negative values are for special things like \d and so  are simple data values; negative values are for special things like \d and so
# Line 118  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 165  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 182  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 196  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 262  the number of relocations needed when a Line 273  the number of relocations needed when a
273  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
274  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
275  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
276  because these strings are used only when there is a compilation error. */  because these strings are used only when there is a compilation error.
277    
278    Each substring ends with \0 to insert a null character. This includes the final
279    substring, so that the whole string ends with \0\0, which can be detected when
280    counting through. */
281    
282  static const char error_texts[] =  static const char error_texts[] =
283    "no error\0"    "no error\0"
# Line 309  static const char error_texts[] = Line 324  static const char error_texts[] =
324    /* 35 */    /* 35 */
325    "invalid condition (?(0)\0"    "invalid condition (?(0)\0"
326    "\\C not allowed in lookbehind assertion\0"    "\\C not allowed in lookbehind assertion\0"
327    "PCRE does not support \\L, \\l, \\N, \\U, or \\u\0"    "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
328    "number after (?C is > 255\0"    "number after (?C is > 255\0"
329    "closing ) for (?C expected\0"    "closing ) for (?C expected\0"
330    /* 40 */    /* 40 */
# Line 335  static const char error_texts[] = Line 350  static const char error_texts[] =
350    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
351    "\\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"
352    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
353    "(*VERB) with an argument is not supported\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
354    /* 60 */    /* 60 */
355    "(*VERB) not recognized\0"    "(*VERB) not recognized\0"
356    "number is too big\0"    "number is too big\0"
357    "subpattern name expected\0"    "subpattern name expected\0"
358    "digit expected after (?+\0"    "digit expected after (?+\0"
359    "] is an invalid data character in JavaScript compatibility mode";    "] is an invalid data character in JavaScript compatibility mode\0"
360      /* 65 */
361      "different names for subpatterns of the same number are not allowed\0"
362      "(*MARK) must have an argument\0"
363      ;
364    
365  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
366  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 500  static const char * Line 518  static const char *
518  find_error_text(int n)  find_error_text(int n)
519  {  {
520  const char *s = error_texts;  const char *s = error_texts;
521  for (; n > 0; n--) while (*s++ != 0) {};  for (; n > 0; n--)
522      {
523      while (*s++ != 0) {};
524      if (*s == 0) return "Error text not found (please report)";
525      }
526  return s;  return s;
527  }  }
528    
# Line 571  else Line 593  else
593    
594      case CHAR_l:      case CHAR_l:
595      case CHAR_L:      case CHAR_L:
     case CHAR_N:  
596      case CHAR_u:      case CHAR_u:
597      case CHAR_U:      case CHAR_U:
598      *errorcodeptr = ERR37;      *errorcodeptr = ERR37;
# Line 808  else Line 829  else
829      break;      break;
830      }      }
831    }    }
832    
833    /* Perl supports \N{name} for character names, as well as plain \N for "not
834    newline". PCRE does not support \N{name}. */
835    
836    if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET)
837      *errorcodeptr = ERR37;
838    
839  *ptrptr = ptr;  *ptrptr = ptr;
840  return c;  return c;
# Line 1009  return p; Line 1036  return p;
1036    
1037    
1038  /*************************************************  /*************************************************
1039  *       Find forward referenced subpattern       *  *  Subroutine for finding forward reference      *
1040  *************************************************/  *************************************************/
1041    
1042  /* This function scans along a pattern's text looking for capturing  /* This recursive function is called only from find_parens() below. The
1043    top-level call starts at the beginning of the pattern. All other calls must
1044    start at a parenthesis. It scans along a pattern's text looking for capturing
1045  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
1046  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
1047  returns when it reaches a given numbered subpattern. This is used for forward  returns when it reaches a given numbered subpattern. We know that if (?P< is
1048  references to subpatterns. We know that if (?P< is encountered, the name will  encountered, the name will be terminated by '>' because that is checked in the
1049  be terminated by '>' because that is checked in the first pass.  first pass. Recursion is used to keep track of subpatterns that reset the
1050    capturing group numbers - the (?| feature.
1051    
1052  Arguments:  Arguments:
1053    ptr          current position in the pattern    ptrptr       address of the current character pointer (updated)
1054    cd           compile background data    cd           compile background data
1055    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1056    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1057    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1058      count        pointer to the current capturing subpattern number (updated)
1059    
1060  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
1061  */  */
1062    
1063  static int  static int
1064  find_parens(const uschar *ptr, compile_data *cd, const uschar *name, int lorn,  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,
1065    BOOL xmode)    BOOL xmode, int *count)
1066  {  {
1067  const uschar *thisname;  uschar *ptr = *ptrptr;
1068  int count = cd->bracount;  int start_count = *count;
1069    int hwm_count = start_count;
1070    BOOL dup_parens = FALSE;
1071    
1072  for (; *ptr != 0; ptr++)  /* If the first character is a parenthesis, check on the type of group we are
1073    dealing with. The very first call may not start with a parenthesis. */
1074    
1075    if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1076    {    {
1077    int term;    if (ptr[1] == CHAR_QUESTION_MARK &&
1078          ptr[2] == CHAR_VERTICAL_LINE)
1079        {
1080        ptr += 3;
1081        dup_parens = TRUE;
1082        }
1083    
1084      /* Handle a normal, unnamed capturing parenthesis */
1085    
1086      else if (ptr[1] != CHAR_QUESTION_MARK && ptr[1] != CHAR_ASTERISK)
1087        {
1088        *count += 1;
1089        if (name == NULL && *count == lorn) return *count;
1090        ptr++;
1091        }
1092    
1093      /* Handle a condition. If it is an assertion, just carry on so that it
1094      is processed as normal. If not, skip to the closing parenthesis of the
1095      condition (there can't be any nested parens. */
1096    
1097      else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
1098        {
1099        ptr += 2;
1100        if (ptr[1] != CHAR_QUESTION_MARK)
1101          {
1102          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
1103          if (*ptr != 0) ptr++;
1104          }
1105        }
1106    
1107      /* We have either (? or (* and not a condition */
1108    
1109      else
1110        {
1111        ptr += 2;
1112        if (*ptr == CHAR_P) ptr++;                      /* Allow optional P */
1113    
1114        /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */
1115    
1116        if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&
1117            ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1118          {
1119          int term;
1120          const uschar *thisname;
1121          *count += 1;
1122          if (name == NULL && *count == lorn) return *count;
1123          term = *ptr++;
1124          if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;
1125          thisname = ptr;
1126          while (*ptr != term) ptr++;
1127          if (name != NULL && lorn == ptr - thisname &&
1128              strncmp((const char *)name, (const char *)thisname, lorn) == 0)
1129            return *count;
1130          term++;
1131          }
1132        }
1133      }
1134    
1135    /* Past any initial parenthesis handling, scan for parentheses or vertical
1136    bars. */
1137    
1138    for (; *ptr != 0; ptr++)
1139      {
1140    /* Skip over backslashed characters and also entire \Q...\E */    /* Skip over backslashed characters and also entire \Q...\E */
1141    
1142    if (*ptr == CHAR_BACKSLASH)    if (*ptr == CHAR_BACKSLASH)
1143      {      {
1144      if (*(++ptr) == 0) return -1;      if (*(++ptr) == 0) goto FAIL_EXIT;
1145      if (*ptr == CHAR_Q) for (;;)      if (*ptr == CHAR_Q) for (;;)
1146        {        {
1147        while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};        while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
1148        if (*ptr == 0) return -1;        if (*ptr == 0) goto FAIL_EXIT;
1149        if (*(++ptr) == CHAR_E) break;        if (*(++ptr) == CHAR_E) break;
1150        }        }
1151      continue;      continue;
# Line 1065  for (; *ptr != 0; ptr++) Line 1162  for (; *ptr != 0; ptr++)
1162      BOOL negate_class = FALSE;      BOOL negate_class = FALSE;
1163      for (;;)      for (;;)
1164        {        {
1165        int c = *(++ptr);        if (ptr[1] == CHAR_BACKSLASH)
       if (c == CHAR_BACKSLASH)  
1166          {          {
1167          if (ptr[1] == CHAR_E)          if (ptr[2] == CHAR_E)
1168            ptr++;            ptr+= 2;
1169          else if (strncmp((const char *)ptr+1,          else if (strncmp((const char *)ptr+2,
1170                   STR_Q STR_BACKSLASH STR_E, 3) == 0)                   STR_Q STR_BACKSLASH STR_E, 3) == 0)
1171            ptr += 3;            ptr += 4;
1172          else          else
1173            break;            break;
1174          }          }
1175        else if (!negate_class && c == CHAR_CIRCUMFLEX_ACCENT)        else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1176            {
1177          negate_class = TRUE;          negate_class = TRUE;
1178            ptr++;
1179            }
1180        else break;        else break;
1181        }        }
1182    
# Line 1093  for (; *ptr != 0; ptr++) Line 1192  for (; *ptr != 0; ptr++)
1192        if (*ptr == 0) return -1;        if (*ptr == 0) return -1;
1193        if (*ptr == CHAR_BACKSLASH)        if (*ptr == CHAR_BACKSLASH)
1194          {          {
1195          if (*(++ptr) == 0) return -1;          if (*(++ptr) == 0) goto FAIL_EXIT;
1196          if (*ptr == CHAR_Q) for (;;)          if (*ptr == CHAR_Q) for (;;)
1197            {            {
1198            while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};            while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
1199            if (*ptr == 0) return -1;            if (*ptr == 0) goto FAIL_EXIT;
1200            if (*(++ptr) == CHAR_E) break;            if (*(++ptr) == CHAR_E) break;
1201            }            }
1202          continue;          continue;
# Line 1111  for (; *ptr != 0; ptr++) Line 1210  for (; *ptr != 0; ptr++)
1210    if (xmode && *ptr == CHAR_NUMBER_SIGN)    if (xmode && *ptr == CHAR_NUMBER_SIGN)
1211      {      {
1212      while (*(++ptr) != 0 && *ptr != CHAR_NL) {};      while (*(++ptr) != 0 && *ptr != CHAR_NL) {};
1213      if (*ptr == 0) return -1;      if (*ptr == 0) goto FAIL_EXIT;
1214      continue;      continue;
1215      }      }
1216    
1217    /* An opening parens must now be a real metacharacter */    /* Check for the special metacharacters */
1218    
1219    if (*ptr != CHAR_LEFT_PARENTHESIS) continue;    if (*ptr == CHAR_LEFT_PARENTHESIS)
   if (ptr[1] != CHAR_QUESTION_MARK && ptr[1] != CHAR_ASTERISK)  
1220      {      {
1221      count++;      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, count);
1222      if (name == NULL && count == lorn) return count;      if (rc > 0) return rc;
1223      continue;      if (*ptr == 0) goto FAIL_EXIT;
1224        }
1225    
1226      else if (*ptr == CHAR_RIGHT_PARENTHESIS)
1227        {
1228        if (dup_parens && *count < hwm_count) *count = hwm_count;
1229        *ptrptr = ptr;
1230        return -1;
1231      }      }
1232    
1233    ptr += 2;    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
1234    if (*ptr == CHAR_P) ptr++;                      /* Allow optional P */      {
1235        if (*count > hwm_count) hwm_count = *count;
1236        *count = start_count;
1237        }
1238      }
1239    
1240    /* We have to disambiguate (?<! and (?<= from (?<name> */  FAIL_EXIT:
1241    *ptrptr = ptr;
1242    return -1;
1243    }
1244    
   if ((*ptr != CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_EXCLAMATION_MARK ||  
       ptr[1] == CHAR_EQUALS_SIGN) && *ptr != CHAR_APOSTROPHE)  
     continue;  
1245    
   count++;  
1246    
1247    if (name == NULL && count == lorn) return count;  
1248    term = *ptr++;  /*************************************************
1249    if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;  *       Find forward referenced subpattern       *
1250    thisname = ptr;  *************************************************/
1251    while (*ptr != term) ptr++;  
1252    if (name != NULL && lorn == ptr - thisname &&  /* This function scans along a pattern's text looking for capturing
1253        strncmp((const char *)name, (const char *)thisname, lorn) == 0)  subpatterns, and counting them. If it finds a named pattern that matches the
1254      return count;  name it is given, it returns its number. Alternatively, if the name is NULL, it
1255    returns when it reaches a given numbered subpattern. This is used for forward
1256    references to subpatterns. We used to be able to start this scan from the
1257    current compiling point, using the current count value from cd->bracount, and
1258    do it all in a single loop, but the addition of the possibility of duplicate
1259    subpattern numbers means that we have to scan from the very start, in order to
1260    take account of such duplicates, and to use a recursive function to keep track
1261    of the different types of group.
1262    
1263    Arguments:
1264      cd           compile background data
1265      name         name to seek, or NULL if seeking a numbered subpattern
1266      lorn         name length, or subpattern number if name is NULL
1267      xmode        TRUE if we are in /x mode
1268    
1269    Returns:       the number of the found subpattern, or -1 if not found
1270    */
1271    
1272    static int
1273    find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode)
1274    {
1275    uschar *ptr = (uschar *)cd->start_pattern;
1276    int count = 0;
1277    int rc;
1278    
1279    /* If the pattern does not start with an opening parenthesis, the first call
1280    to find_parens_sub() will scan right to the end (if necessary). However, if it
1281    does start with a parenthesis, find_parens_sub() will return when it hits the
1282    matching closing parens. That is why we have to have a loop. */
1283    
1284    for (;;)
1285      {
1286      rc = find_parens_sub(&ptr, cd, name, lorn, xmode, &count);
1287      if (rc > 0 || *ptr++ == 0) break;
1288    }    }
1289    
1290  return -1;  return rc;
1291  }  }
1292    
1293    
1294    
1295    
1296  /*************************************************  /*************************************************
1297  *      Find first significant op code            *  *      Find first significant op code            *
1298  *************************************************/  *************************************************/
# Line 1200  for (;;) Line 1342  for (;;)
1342    
1343      case OP_CALLOUT:      case OP_CALLOUT:
1344      case OP_CREF:      case OP_CREF:
1345        case OP_NCREF:
1346      case OP_RREF:      case OP_RREF:
1347        case OP_NRREF:
1348      case OP_DEF:      case OP_DEF:
1349      code += _pcre_OP_lengths[*code];      code += _pcre_OP_lengths[*code];
1350      break;      break;
# Line 1216  for (;;) Line 1360  for (;;)
1360    
1361    
1362  /*************************************************  /*************************************************
1363  *        Find the fixed length of a pattern      *  *        Find the fixed length of a branch       *
1364  *************************************************/  *************************************************/
1365    
1366  /* Scan a pattern and compute the fixed length of subject that will match it,  /* Scan a branch and compute the fixed length of subject that will match it,
1367  if the length is fixed. This is needed for dealing with backward assertions.  if the length is fixed. This is needed for dealing with backward assertions.
1368  In UTF8 mode, the result is in characters rather than bytes.  In UTF8 mode, the result is in characters rather than bytes. The branch is
1369    temporarily terminated with OP_END when this function is called.
1370    
1371    This function is called when a backward assertion is encountered, so that if it
1372    fails, the error message can point to the correct place in the pattern.
1373    However, we cannot do this when the assertion contains subroutine calls,
1374    because they can be forward references. We solve this by remembering this case
1375    and doing the check at the end; a flag specifies which mode we are running in.
1376    
1377  Arguments:  Arguments:
1378    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1379    options  the compiling options    options  the compiling options
1380      atend    TRUE if called when the pattern is complete
1381      cd       the "compile data" structure
1382    
1383  Returns:   the fixed length, or -1 if there is no fixed length,  Returns:   the fixed length,
1384                 or -1 if there is no fixed length,
1385               or -2 if \C was encountered               or -2 if \C was encountered
1386                 or -3 if an OP_RECURSE item was encountered and atend is FALSE
1387  */  */
1388    
1389  static int  static int
1390  find_fixedlength(uschar *code, int options)  find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)
1391  {  {
1392  int length = -1;  int length = -1;
1393    
# Line 1245  branch, check the length against that of Line 1400  branch, check the length against that of
1400  for (;;)  for (;;)
1401    {    {
1402    int d;    int d;
1403      uschar *ce, *cs;
1404    register int op = *cc;    register int op = *cc;
1405    switch (op)    switch (op)
1406      {      {
# Line 1252  for (;;) Line 1408  for (;;)
1408      case OP_BRA:      case OP_BRA:
1409      case OP_ONCE:      case OP_ONCE:
1410      case OP_COND:      case OP_COND:
1411      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options);      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd);
1412      if (d < 0) return d;      if (d < 0) return d;
1413      branchlength += d;      branchlength += d;
1414      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 1275  for (;;) Line 1431  for (;;)
1431      branchlength = 0;      branchlength = 0;
1432      break;      break;
1433    
1434        /* A true recursion implies not fixed length, but a subroutine call may
1435        be OK. If the subroutine is a forward reference, we can't deal with
1436        it until the end of the pattern, so return -3. */
1437    
1438        case OP_RECURSE:
1439        if (!atend) return -3;
1440        cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1441        do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */
1442        if (cc > cs && cc < ce) return -1;                /* Recursion */
1443        d = find_fixedlength(cs + 2, options, atend, cd);
1444        if (d < 0) return d;
1445        branchlength += d;
1446        cc += 1 + LINK_SIZE;
1447        break;
1448    
1449      /* Skip over assertive subpatterns */      /* Skip over assertive subpatterns */
1450    
1451      case OP_ASSERT:      case OP_ASSERT:
# Line 1288  for (;;) Line 1459  for (;;)
1459    
1460      case OP_REVERSE:      case OP_REVERSE:
1461      case OP_CREF:      case OP_CREF:
1462        case OP_NCREF:
1463      case OP_RREF:      case OP_RREF:
1464        case OP_NRREF:
1465      case OP_DEF:      case OP_DEF:
1466      case OP_OPT:      case OP_OPT:
1467      case OP_CALLOUT:      case OP_CALLOUT:
1468      case OP_SOD:      case OP_SOD:
1469      case OP_SOM:      case OP_SOM:
1470        case OP_SET_SOM:
1471      case OP_EOD:      case OP_EOD:
1472      case OP_EODN:      case OP_EODN:
1473      case OP_CIRC:      case OP_CIRC:
# Line 1311  for (;;) Line 1485  for (;;)
1485      branchlength++;      branchlength++;
1486      cc += 2;      cc += 2;
1487  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1488      if ((options & PCRE_UTF8) != 0)      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)
1489        {        cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       while ((*cc & 0xc0) == 0x80) cc++;  
       }  
1490  #endif  #endif
1491      break;      break;
1492    
# Line 1325  for (;;) Line 1497  for (;;)
1497      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1498      cc += 4;      cc += 4;
1499  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1500      if ((options & PCRE_UTF8) != 0)      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)
1501        {        cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       while((*cc & 0x80) == 0x80) cc++;  
       }  
1502  #endif  #endif
1503      break;      break;
1504    
# Line 1407  for (;;) Line 1577  for (;;)
1577    
1578    
1579  /*************************************************  /*************************************************
1580  *    Scan compiled regex for numbered bracket    *  *    Scan compiled regex for specific bracket    *
1581  *************************************************/  *************************************************/
1582    
1583  /* This little function scans through a compiled pattern until it finds a  /* This little function scans through a compiled pattern until it finds a
1584  capturing bracket with the given number.  capturing bracket with the given number, or, if the number is negative, an
1585    instance of OP_REVERSE for a lookbehind. The function is global in the C sense
1586    so that it can be called from pcre_study() when finding the minimum matching
1587    length.
1588    
1589  Arguments:  Arguments:
1590    code        points to start of expression    code        points to start of expression
1591    utf8        TRUE in UTF-8 mode    utf8        TRUE in UTF-8 mode
1592    number      the required bracket number    number      the required bracket number or negative to find a lookbehind
1593    
1594  Returns:      pointer to the opcode for the bracket, or NULL if not found  Returns:      pointer to the opcode for the bracket, or NULL if not found
1595  */  */
1596    
1597  static const uschar *  const uschar *
1598  find_bracket(const uschar *code, BOOL utf8, int number)  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)
1599  {  {
1600  for (;;)  for (;;)
1601    {    {
# Line 1435  for (;;) Line 1608  for (;;)
1608    
1609    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
1610    
1611      /* Handle recursion */
1612    
1613      else if (c == OP_REVERSE)
1614        {
1615        if (number < 0) return (uschar *)code;
1616        code += _pcre_OP_lengths[c];
1617        }
1618    
1619    /* Handle capturing bracket */    /* Handle capturing bracket */
1620    
1621    else if (c == OP_CBRA)    else if (c == OP_CBRA)
# Line 1446  for (;;) Line 1627  for (;;)
1627    
1628    /* 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
1629    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
1630    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1631      must add in its length. */
1632    
1633    else    else
1634      {      {
# Line 1470  for (;;) Line 1652  for (;;)
1652        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1653        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1654        break;        break;
1655    
1656          case OP_MARK:
1657          case OP_PRUNE_ARG:
1658          case OP_SKIP_ARG:
1659          case OP_THEN_ARG:
1660          code += code[1];
1661          break;
1662        }        }
1663    
1664      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1541  for (;;) Line 1730  for (;;)
1730    
1731    /* 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
1732    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
1733    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1734      must add in its length. */
1735    
1736    else    else
1737      {      {
# Line 1565  for (;;) Line 1755  for (;;)
1755        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1756        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1757        break;        break;
1758    
1759          case OP_MARK:
1760          case OP_PRUNE_ARG:
1761          case OP_SKIP_ARG:
1762          case OP_THEN_ARG:
1763          code += code[1];
1764          break;
1765        }        }
1766    
1767      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1621  Arguments: Line 1818  Arguments:
1818    code        points to start of search    code        points to start of search
1819    endcode     points to where to stop    endcode     points to where to stop
1820    utf8        TRUE if in UTF8 mode    utf8        TRUE if in UTF8 mode
1821      cd          contains pointers to tables etc.
1822    
1823  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
1824  */  */
1825    
1826  static BOOL  static BOOL
1827  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,
1828      compile_data *cd)
1829  {  {
1830  register int c;  register int c;
1831  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);
# Line 1657  for (code = first_significant_code(code Line 1856  for (code = first_significant_code(code
1856      continue;      continue;
1857      }      }
1858    
1859      /* For a recursion/subroutine call, if its end has been reached, which
1860      implies a subroutine call, we can scan it. */
1861    
1862      if (c == OP_RECURSE)
1863        {
1864        BOOL empty_branch = FALSE;
1865        const uschar *scode = cd->start_code + GET(code, 1);
1866        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
1867        do
1868          {
1869          if (could_be_empty_branch(scode, endcode, utf8, cd))
1870            {
1871            empty_branch = TRUE;
1872            break;
1873            }
1874          scode += GET(scode, 1);
1875          }
1876        while (*scode == OP_ALT);
1877        if (!empty_branch) return FALSE;  /* All branches are non-empty */
1878        continue;
1879        }
1880    
1881    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
1882    
1883    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)
# Line 1675  for (code = first_significant_code(code Line 1896  for (code = first_significant_code(code
1896        empty_branch = FALSE;        empty_branch = FALSE;
1897        do        do
1898          {          {
1899          if (!empty_branch && could_be_empty_branch(code, endcode, utf8))          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))
1900            empty_branch = TRUE;            empty_branch = TRUE;
1901          code += GET(code, 1);          code += GET(code, 1);
1902          }          }
# Line 1800  for (code = first_significant_code(code Line 2021  for (code = first_significant_code(code
2021      case OP_QUERY:      case OP_QUERY:
2022      case OP_MINQUERY:      case OP_MINQUERY:
2023      case OP_POSQUERY:      case OP_POSQUERY:
2024        if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];
2025        break;
2026    
2027      case OP_UPTO:      case OP_UPTO:
2028      case OP_MINUPTO:      case OP_MINUPTO:
2029      case OP_POSUPTO:      case OP_POSUPTO:
2030      if (utf8) while ((code[2] & 0xc0) == 0x80) code++;      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];
2031      break;      break;
2032  #endif  #endif
2033    
2034        /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2035        string. */
2036    
2037        case OP_MARK:
2038        case OP_PRUNE_ARG:
2039        case OP_SKIP_ARG:
2040        case OP_THEN_ARG:
2041        code += code[1];
2042        break;
2043    
2044        /* None of the remaining opcodes are required to match a character. */
2045    
2046        default:
2047        break;
2048      }      }
2049    }    }
2050    
# Line 1828  Arguments: Line 2067  Arguments:
2067    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2068    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2069    utf8        TRUE if in UTF-8 mode    utf8        TRUE if in UTF-8 mode
2070      cd          pointers to tables etc
2071    
2072  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2073  */  */
2074    
2075  static BOOL  static BOOL
2076  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,
2077    BOOL utf8)    BOOL utf8, compile_data *cd)
2078  {  {
2079  while (bcptr != NULL && bcptr->current >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2080    {    {
2081    if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))
2082        return FALSE;
2083    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2084    }    }
2085  return TRUE;  return TRUE;
# Line 2500  BOOL utf8 = FALSE; Line 2741  BOOL utf8 = FALSE;
2741  uschar *utf8_char = NULL;  uschar *utf8_char = NULL;
2742  #endif  #endif
2743    
2744  #ifdef DEBUG  #ifdef PCRE_DEBUG
2745  if (lengthptr != NULL) DPRINTF((">> start branch\n"));  if (lengthptr != NULL) DPRINTF((">> start branch\n"));
2746  #endif  #endif
2747    
# Line 2559  for (;; ptr++) Line 2800  for (;; ptr++)
2800    
2801    if (lengthptr != NULL)    if (lengthptr != NULL)
2802      {      {
2803  #ifdef DEBUG  #ifdef PCRE_DEBUG
2804      if (code > cd->hwm) cd->hwm = code;                 /* High water info */      if (code > cd->hwm) cd->hwm = code;                 /* High water info */
2805  #endif  #endif
2806      if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */      if (code > cd->start_workspace + WORK_SIZE_CHECK)   /* Check for overrun */
2807        {        {
2808        *errorcodeptr = ERR52;        *errorcodeptr = ERR52;
2809        goto FAILED;        goto FAILED;
# Line 2611  for (;; ptr++) Line 2852  for (;; ptr++)
2852    /* In the real compile phase, just check the workspace used by the forward    /* In the real compile phase, just check the workspace used by the forward
2853    reference list. */    reference list. */
2854    
2855    else if (cd->hwm > cd->start_workspace + COMPILE_WORK_SIZE)    else if (cd->hwm > cd->start_workspace + WORK_SIZE_CHECK)
2856      {      {
2857      *errorcodeptr = ERR52;      *errorcodeptr = ERR52;
2858      goto FAILED;      goto FAILED;
# Line 2968  for (;; ptr++) Line 3209  for (;; ptr++)
3209    
3210        /* Backslash may introduce a single character, or it may introduce one        /* Backslash may introduce a single character, or it may introduce one
3211        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
3212        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
3213        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
3214        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
3215        character in them, so set class_charcount bigger than one. */        are either treated as literal characters (by default), or are faulted if
3216          PCRE_EXTRA is set. */
3217    
3218        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
3219          {          {
3220          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3221          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
3222    
3223          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 */  
3224          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (-c == ESC_Q)            /* Handle start of quoted string */
3225            {            {
3226            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 3265  for (;; ptr++) Line 3505  for (;; ptr++)
3505            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3506            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
3507    
3508            /* \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 */  
3509    
3510            if (d < 0)            if (d < 0)
3511              {              {
3512              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  
3513                {                {
3514                ptr = oldptr;                ptr = oldptr;
3515                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto LONE_SINGLE_CHARACTER;  /* A few lines below */
# Line 3757  we set the flag only if there is a liter Line 3994  we set the flag only if there is a liter
3994    
3995        if (repeat_max == 0) goto END_REPEAT;        if (repeat_max == 0) goto END_REPEAT;
3996    
3997          /*--------------------------------------------------------------------*/
3998          /* This code is obsolete from release 8.00; the restriction was finally
3999          removed: */
4000    
4001        /* All real repeats make it impossible to handle partial matching (maybe        /* All real repeats make it impossible to handle partial matching (maybe
4002        one day we will be able to remove this restriction). */        one day we will be able to remove this restriction). */
4003    
4004        if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL;        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
4005          /*--------------------------------------------------------------------*/
4006    
4007        /* Combine the op_type with the repeat_type */        /* Combine the op_type with the repeat_type */
4008    
# Line 3907  we set the flag only if there is a liter Line 4149  we set the flag only if there is a liter
4149          goto END_REPEAT;          goto END_REPEAT;
4150          }          }
4151    
4152          /*--------------------------------------------------------------------*/
4153          /* This code is obsolete from release 8.00; the restriction was finally
4154          removed: */
4155    
4156        /* All real repeats make it impossible to handle partial matching (maybe        /* All real repeats make it impossible to handle partial matching (maybe
4157        one day we will be able to remove this restriction). */        one day we will be able to remove this restriction). */
4158    
4159        if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL;        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
4160          /*--------------------------------------------------------------------*/
4161    
4162        if (repeat_min == 0 && repeat_max == -1)        if (repeat_min == 0 && repeat_max == -1)
4163          *code++ = OP_CRSTAR + repeat_type;          *code++ = OP_CRSTAR + repeat_type;
# Line 4045  we set the flag only if there is a liter Line 4292  we set the flag only if there is a liter
4292            {            {
4293            /* In the pre-compile phase, we don't actually do the replication. We            /* In the pre-compile phase, we don't actually do the replication. We
4294            just adjust the length as if we had. Do some paranoid checks for            just adjust the length as if we had. Do some paranoid checks for
4295            potential integer overflow. */            potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
4296              integer type when available, otherwise double. */
4297    
4298            if (lengthptr != NULL)            if (lengthptr != NULL)
4299              {              {
4300              int delta = (repeat_min - 1)*length_prevgroup;              int delta = (repeat_min - 1)*length_prevgroup;
4301              if ((double)(repeat_min - 1)*(double)length_prevgroup >              if ((INT64_OR_DOUBLE)(repeat_min - 1)*
4302                                                              (double)INT_MAX ||                    (INT64_OR_DOUBLE)length_prevgroup >
4303                        (INT64_OR_DOUBLE)INT_MAX ||
4304                  OFLOW_MAX - *lengthptr < delta)                  OFLOW_MAX - *lengthptr < delta)
4305                {                {
4306                *errorcodeptr = ERR20;                *errorcodeptr = ERR20;
# Line 4097  we set the flag only if there is a liter Line 4346  we set the flag only if there is a liter
4346          just adjust the length as if we had. For each repetition we must add 1          just adjust the length as if we had. For each repetition we must add 1
4347          to the length for BRAZERO and for all but the last repetition we must          to the length for BRAZERO and for all but the last repetition we must
4348          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
4349          paranoid checks to avoid integer overflow. */          paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
4350            a 64-bit integer type when available, otherwise double. */
4351    
4352          if (lengthptr != NULL && repeat_max > 0)          if (lengthptr != NULL && repeat_max > 0)
4353            {            {
4354            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
4355                        2 - 2*LINK_SIZE;   /* Last one doesn't nest */                        2 - 2*LINK_SIZE;   /* Last one doesn't nest */
4356            if ((double)repeat_max *            if ((INT64_OR_DOUBLE)repeat_max *
4357                  (double)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)                  (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
4358                    > (double)INT_MAX ||                    > (INT64_OR_DOUBLE)INT_MAX ||
4359                OFLOW_MAX - *lengthptr < delta)                OFLOW_MAX - *lengthptr < delta)
4360              {              {
4361              *errorcodeptr = ERR20;              *errorcodeptr = ERR20;
# Line 4182  we set the flag only if there is a liter Line 4432  we set the flag only if there is a liter
4432            uschar *scode = bracode;            uschar *scode = bracode;
4433            do            do
4434              {              {
4435              if (could_be_empty_branch(scode, ketcode, utf8))              if (could_be_empty_branch(scode, ketcode, utf8, cd))
4436                {                {
4437                *bracode += OP_SBRA - OP_BRA;                *bracode += OP_SBRA - OP_BRA;
4438                break;                break;
# Line 4225  we set the flag only if there is a liter Line 4475  we set the flag only if there is a liter
4475      if (possessive_quantifier)      if (possessive_quantifier)
4476        {        {
4477        int len;        int len;
4478        if (*tempcode == OP_EXACT || *tempcode == OP_TYPEEXACT ||  
4479            *tempcode == OP_NOTEXACT)        if (*tempcode == OP_TYPEEXACT)
4480          tempcode += _pcre_OP_lengths[*tempcode] +          tempcode += _pcre_OP_lengths[*tempcode] +
4481            ((*tempcode == OP_TYPEEXACT &&            ((tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP)? 2 : 0);
4482               (tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP))? 2:0);  
4483          else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
4484            {
4485            tempcode += _pcre_OP_lengths[*tempcode];
4486    #ifdef SUPPORT_UTF8
4487            if (utf8 && tempcode[-1] >= 0xc0)
4488              tempcode += _pcre_utf8_table4[tempcode[-1] & 0x3f];
4489    #endif
4490            }
4491    
4492        len = code - tempcode;        len = code - tempcode;
4493        if (len > 0) switch (*tempcode)        if (len > 0) switch (*tempcode)
4494          {          {
# Line 4248  we set the flag only if there is a liter Line 4507  we set the flag only if there is a liter
4507          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
4508          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;
4509    
4510            /* Because we are moving code along, we must ensure that any
4511            pending recursive references are updated. */
4512    
4513          default:          default:
4514            *code = OP_END;
4515            adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);
4516          memmove(tempcode + 1+LINK_SIZE, tempcode, len);          memmove(tempcode + 1+LINK_SIZE, tempcode, len);
4517          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
4518          len += 1 + LINK_SIZE;          len += 1 + LINK_SIZE;
# Line 4284  we set the flag only if there is a liter Line 4548  we set the flag only if there is a liter
4548    
4549      /* First deal with various "verbs" that can be introduced by '*'. */      /* First deal with various "verbs" that can be introduced by '*'. */
4550    
4551      if (*(++ptr) == CHAR_ASTERISK && (cd->ctypes[ptr[1]] & ctype_letter) != 0)      if (*(++ptr) == CHAR_ASTERISK &&
4552             ((cd->ctypes[ptr[1]] & ctype_letter) != 0 || ptr[1] == ':'))
4553        {        {
4554        int i, namelen;        int i, namelen;
4555          int arglen = 0;
4556        const char *vn = verbnames;        const char *vn = verbnames;
4557        const uschar *name = ++ptr;        const uschar *name = ptr + 1;
4558          const uschar *arg = NULL;
4559        previous = NULL;        previous = NULL;
4560        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};
4561          namelen = ptr - name;
4562    
4563        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
4564          {          {
4565          *errorcodeptr = ERR59;   /* Not supported */          arg = ++ptr;
4566          goto FAILED;          while ((cd->ctypes[*ptr] & (ctype_letter|ctype_digit)) != 0
4567              || *ptr == '_') ptr++;
4568            arglen = ptr - arg;
4569          }          }
4570    
4571        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
4572          {          {
4573          *errorcodeptr = ERR60;          *errorcodeptr = ERR60;
4574          goto FAILED;          goto FAILED;
4575          }          }
4576        namelen = ptr - name;  
4577          /* Scan the table of verb names */
4578    
4579        for (i = 0; i < verbcount; i++)        for (i = 0; i < verbcount; i++)
4580          {          {
4581          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
4582              strncmp((char *)name, vn, namelen) == 0)              strncmp((char *)name, vn, namelen) == 0)
4583            {            {
4584            *code = verbs[i].op;            /* Check for open captures before ACCEPT */
4585            if (*code++ == OP_ACCEPT) cd->had_accept = TRUE;  
4586            break;            if (verbs[i].op == OP_ACCEPT)
4587                {
4588                open_capitem *oc;
4589                cd->had_accept = TRUE;
4590                for (oc = cd->open_caps; oc != NULL; oc = oc->next)
4591                  {
4592                  *code++ = OP_CLOSE;
4593                  PUT2INC(code, 0, oc->number);
4594                  }
4595                }
4596    
4597              /* Handle the cases with/without an argument */
4598    
4599              if (arglen == 0)
4600                {
4601                if (verbs[i].op < 0)   /* Argument is mandatory */
4602                  {
4603                  *errorcodeptr = ERR66;
4604                  goto FAILED;
4605                  }
4606                *code++ = verbs[i].op;
4607                }
4608    
4609              else
4610                {
4611                if (verbs[i].op_arg < 0)   /* Argument is forbidden */
4612                  {
4613                  *errorcodeptr = ERR59;
4614                  goto FAILED;
4615                  }
4616                *code++ = verbs[i].op_arg;
4617                *code++ = arglen;
4618                memcpy(code, arg, arglen);
4619                code += arglen;
4620                *code++ = 0;
4621                }
4622    
4623              break;  /* Found verb, exit loop */
4624            }            }
4625    
4626          vn += verbs[i].len + 1;          vn += verbs[i].len + 1;
4627          }          }
4628        if (i < verbcount) continue;  
4629        *errorcodeptr = ERR60;        if (i < verbcount) continue;    /* Successfully handled a verb */
4630          *errorcodeptr = ERR60;          /* Verb not recognized */
4631        goto FAILED;        goto FAILED;
4632        }        }
4633    
# Line 4470  we set the flag only if there is a liter Line 4783  we set the flag only if there is a liter
4783            }            }
4784    
4785          /* Otherwise (did not start with "+" or "-"), start by looking for the          /* Otherwise (did not start with "+" or "-"), start by looking for the
4786          name. */          name. If we find a name, add one to the opcode to change OP_CREF or
4787            OP_RREF into OP_NCREF or OP_NRREF. These behave exactly the same,
4788            except they record that the reference was originally to a name. The
4789            information is used to check duplicate names. */
4790    
4791          slot = cd->name_table;          slot = cd->name_table;
4792          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
# Line 4485  we set the flag only if there is a liter Line 4801  we set the flag only if there is a liter
4801            {            {
4802            recno = GET2(slot, 0);            recno = GET2(slot, 0);
4803            PUT2(code, 2+LINK_SIZE, recno);            PUT2(code, 2+LINK_SIZE, recno);
4804              code[1+LINK_SIZE]++;
4805            }            }
4806    
4807          /* Search the pattern for a forward reference */          /* Search the pattern for a forward reference */
4808    
4809          else if ((i = find_parens(ptr, cd, name, namelen,          else if ((i = find_parens(cd, name, namelen,
4810                          (options & PCRE_EXTENDED) != 0)) > 0)                          (options & PCRE_EXTENDED) != 0)) > 0)
4811            {            {
4812            PUT2(code, 2+LINK_SIZE, i);            PUT2(code, 2+LINK_SIZE, i);
4813              code[1+LINK_SIZE]++;
4814            }            }
4815    
4816          /* If terminator == 0 it means that the name followed directly after          /* If terminator == 0 it means that the name followed directly after
# Line 4685  we set the flag only if there is a liter Line 5003  we set the flag only if there is a liter
5003                }                }
5004              }              }
5005    
5006            /* In the real compile, create the entry in the table */            /* In the real compile, create the entry in the table, maintaining
5007              alphabetical order. Duplicate names for different numbers are
5008              permitted only if PCRE_DUPNAMES is set. Duplicate names for the same
5009              number are always OK. (An existing number can be re-used if (?|
5010              appears in the pattern.) In either event, a duplicate name results in
5011              a duplicate entry in the table, even if the number is the same. This
5012              is because the number of names, and hence the table size, is computed
5013              in the pre-compile, and it affects various numbers and pointers which
5014              would all have to be modified, and the compiled code moved down, if
5015              duplicates with the same number were omitted from the table. This
5016              doesn't seem worth the hassle. However, *different* names for the
5017              same number are not permitted. */
5018    
5019            else            else
5020              {              {
5021                BOOL dupname = FALSE;
5022              slot = cd->name_table;              slot = cd->name_table;
5023    
5024              for (i = 0; i < cd->names_found; i++)              for (i = 0; i < cd->names_found; i++)
5025                {                {
5026                int crc = memcmp(name, slot+2, namelen);                int crc = memcmp(name, slot+2, namelen);
# Line 4697  we set the flag only if there is a liter Line 5028  we set the flag only if there is a liter
5028                  {                  {
5029                  if (slot[2+namelen] == 0)                  if (slot[2+namelen] == 0)
5030                    {                    {
5031                    if ((options & PCRE_DUPNAMES) == 0)                    if (GET2(slot, 0) != cd->bracount + 1 &&
5032                          (options & PCRE_DUPNAMES) == 0)
5033                      {                      {
5034                      *errorcodeptr = ERR43;                      *errorcodeptr = ERR43;
5035                      goto FAILED;                      goto FAILED;
5036                      }                      }
5037                      else dupname = TRUE;
5038                    }                    }
5039                  else crc = -1;      /* Current name is substring */                  else crc = -1;      /* Current name is a substring */
5040                  }                  }
5041    
5042                  /* Make space in the table and break the loop for an earlier
5043                  name. For a duplicate or later name, carry on. We do this for
5044                  duplicates so that in the simple case (when ?(| is not used) they
5045                  are in order of their numbers. */
5046    
5047                if (crc < 0)                if (crc < 0)
5048                  {                  {
5049                  memmove(slot + cd->name_entry_size, slot,                  memmove(slot + cd->name_entry_size, slot,
5050                    (cd->names_found - i) * cd->name_entry_size);                    (cd->names_found - i) * cd->name_entry_size);
5051                  break;                  break;
5052                  }                  }
5053    
5054                  /* Continue the loop for a later or duplicate name */
5055    
5056                slot += cd->name_entry_size;                slot += cd->name_entry_size;
5057                }                }
5058    
5059                /* For non-duplicate names, check for a duplicate number before
5060                adding the new name. */
5061    
5062                if (!dupname)
5063                  {
5064                  uschar *cslot = cd->name_table;
5065                  for (i = 0; i < cd->names_found; i++)
5066                    {
5067                    if (cslot != slot)
5068                      {
5069                      if (GET2(cslot, 0) == cd->bracount + 1)
5070                        {
5071                        *errorcodeptr = ERR65;
5072                        goto FAILED;
5073                        }
5074                      }
5075                    else i--;
5076                    cslot += cd->name_entry_size;
5077                    }
5078                  }
5079    
5080              PUT2(slot, 0, cd->bracount + 1);              PUT2(slot, 0, cd->bracount + 1);
5081              memcpy(slot + 2, name, namelen);              memcpy(slot + 2, name, namelen);
5082              slot[2+namelen] = 0;              slot[2+namelen] = 0;
5083              }              }
5084            }            }
5085    
5086          /* In both cases, count the number of names we've encountered. */          /* In both pre-compile and compile, count the number of names we've
5087            encountered. */
5088    
         ptr++;                    /* Move past > or ' */  
5089          cd->names_found++;          cd->names_found++;
5090            ptr++;                    /* Move past > or ' */
5091          goto NUMBERED_GROUP;          goto NUMBERED_GROUP;
5092    
5093    
# Line 4788  we set the flag only if there is a liter Line 5152  we set the flag only if there is a liter
5152              recno = GET2(slot, 0);              recno = GET2(slot, 0);
5153              }              }
5154            else if ((recno =                /* Forward back reference */            else if ((recno =                /* Forward back reference */
5155                      find_parens(ptr, cd, name, namelen,                      find_parens(cd, name, namelen,
5156                        (options & PCRE_EXTENDED) != 0)) <= 0)                        (options & PCRE_EXTENDED) != 0)) <= 0)
5157              {              {
5158              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
# Line 4892  we set the flag only if there is a liter Line 5256  we set the flag only if there is a liter
5256            if (lengthptr == NULL)            if (lengthptr == NULL)
5257              {              {
5258              *code = OP_END;              *code = OP_END;
5259              if (recno != 0) called = find_bracket(cd->start_code, utf8, recno);              if (recno != 0)
5260                  called = _pcre_find_bracket(cd->start_code, utf8, recno);
5261    
5262              /* Forward reference */              /* Forward reference */
5263    
5264              if (called == NULL)              if (called == NULL)
5265                {                {
5266                if (find_parens(ptr, cd, NULL, recno,                if (find_parens(cd, NULL, recno,
5267                      (options & PCRE_EXTENDED) != 0) < 0)                      (options & PCRE_EXTENDED) != 0) < 0)
5268                  {                  {
5269                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
5270                  goto FAILED;                  goto FAILED;
5271                  }                  }
5272    
5273                  /* Fudge the value of "called" so that when it is inserted as an
5274                  offset below, what it actually inserted is the reference number
5275                  of the group. */
5276    
5277                called = cd->start_code + recno;                called = cd->start_code + recno;
5278                PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code);                PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code);
5279                }                }
# Line 4913  we set the flag only if there is a liter Line 5283  we set the flag only if there is a liter
5283              recursion that could loop for ever, and diagnose that case. */              recursion that could loop for ever, and diagnose that case. */
5284    
5285              else if (GET(called, 1) == 0 &&              else if (GET(called, 1) == 0 &&
5286                       could_be_empty(called, code, bcptr, utf8))                       could_be_empty(called, code, bcptr, utf8, cd))
5287                {                {
5288                *errorcodeptr = ERR40;                *errorcodeptr = ERR40;
5289                goto FAILED;                goto FAILED;
# Line 5008  we set the flag only if there is a liter Line 5378  we set the flag only if there is a liter
5378              {              {
5379              cd->external_options = newoptions;              cd->external_options = newoptions;
5380              }              }
5381           else            else
5382              {              {
5383              if ((options & PCRE_IMS) != (newoptions & PCRE_IMS))              if ((options & PCRE_IMS) != (newoptions & PCRE_IMS))
5384                {                {
# Line 5040  we set the flag only if there is a liter Line 5410  we set the flag only if there is a liter
5410          }     /* End of switch for character following (? */          }     /* End of switch for character following (? */
5411        }       /* End of (? handling */        }       /* End of (? handling */
5412    
5413      /* Opening parenthesis not followed by '?'. If PCRE_NO_AUTO_CAPTURE is set,      /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
5414      all unadorned brackets become non-capturing and behave like (?:...)      is set, all unadorned brackets become non-capturing and behave like (?:...)
5415      brackets. */      brackets. */
5416    
5417      else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)      else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)
# Line 5345  we set the flag only if there is a liter Line 5715  we set the flag only if there is a liter
5715    
5716        if (-c >= ESC_REF)        if (-c >= ESC_REF)
5717          {          {
5718            open_capitem *oc;
5719          recno = -c - ESC_REF;          recno = -c - ESC_REF;
5720    
5721          HANDLE_REFERENCE:    /* Come here from named backref handling */          HANDLE_REFERENCE:    /* Come here from named backref handling */
# Line 5354  we set the flag only if there is a liter Line 5725  we set the flag only if there is a liter
5725          PUT2INC(code, 0, recno);          PUT2INC(code, 0, recno);
5726          cd->backref_map |= (recno < 32)? (1 << recno) : 1;          cd->backref_map |= (recno < 32)? (1 << recno) : 1;
5727          if (recno > cd->top_backref) cd->top_backref = recno;          if (recno > cd->top_backref) cd->top_backref = recno;
5728    
5729            /* Check to see if this back reference is recursive, that it, it
5730            is inside the group that it references. A flag is set so that the
5731            group can be made atomic. */
5732    
5733            for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5734              {
5735              if (oc->number == recno)
5736                {
5737                oc->flag = TRUE;
5738                break;
5739                }
5740              }
5741          }          }
5742    
5743        /* So are Unicode property matches, if supported. */        /* So are Unicode property matches, if supported. */
# Line 5536  uschar *code = *codeptr; Line 5920  uschar *code = *codeptr;
5920  uschar *last_branch = code;  uschar *last_branch = code;
5921  uschar *start_bracket = code;  uschar *start_bracket = code;
5922  uschar *reverse_count = NULL;  uschar *reverse_count = NULL;
5923    open_capitem capitem;
5924    int capnumber = 0;
5925  int firstbyte, reqbyte;  int firstbyte, reqbyte;
5926  int branchfirstbyte, branchreqbyte;  int branchfirstbyte, branchreqbyte;
5927  int length;  int length;
5928  int orig_bracount;  int orig_bracount;
5929  int max_bracount;  int max_bracount;
5930    int old_external_options = cd->external_options;
5931  branch_chain bc;  branch_chain bc;
5932    
5933  bc.outer = bcptr;  bc.outer = bcptr;
5934  bc.current = code;  bc.current_branch = code;
5935    
5936  firstbyte = reqbyte = REQ_UNSET;  firstbyte = reqbyte = REQ_UNSET;
5937    
# Line 5562  the code that abstracts option settings Line 5949  the code that abstracts option settings
5949  them global. It tests the value of length for (2 + 2*LINK_SIZE) in the  them global. It tests the value of length for (2 + 2*LINK_SIZE) in the
5950  pre-compile phase to find out whether anything has yet been compiled or not. */  pre-compile phase to find out whether anything has yet been compiled or not. */
5951    
5952    /* If this is a capturing subpattern, add to the chain of open capturing items
5953    so that we can detect them if (*ACCEPT) is encountered. This is also used to
5954    detect groups that contain recursive back references to themselves. */
5955    
5956    if (*code == OP_CBRA)
5957      {
5958      capnumber = GET2(code, 1 + LINK_SIZE);
5959      capitem.number = capnumber;
5960      capitem.next = cd->open_caps;
5961      capitem.flag = FALSE;
5962      cd->open_caps = &capitem;
5963      }
5964    
5965  /* Offset is set zero to mark that this bracket is still open */  /* Offset is set zero to mark that this bracket is still open */
5966    
5967  PUT(code, 1, 0);  PUT(code, 1, 0);
# Line 5606  for (;;) Line 6006  for (;;)
6006      return FALSE;      return FALSE;
6007      }      }
6008    
6009      /* If the external options have changed during this branch, it means that we
6010      are at the top level, and a leading option setting has been encountered. We
6011      need to re-set the original option values to take account of this so that,
6012      during the pre-compile phase, we know to allow for a re-set at the start of
6013      subsequent branches. */
6014    
6015      if (old_external_options != cd->external_options)
6016        oldims = cd->external_options & PCRE_IMS;
6017    
6018    /* Keep the highest bracket count in case (?| was used and some branch    /* Keep the highest bracket count in case (?| was used and some branch
6019    has fewer than the rest. */    has fewer than the rest. */
6020    
# Line 5656  for (;;) Line 6065  for (;;)
6065    
6066      /* If lookbehind, check that this branch matches a fixed-length string, and      /* If lookbehind, check that this branch matches a fixed-length string, and
6067      put the length into the OP_REVERSE item. Temporarily mark the end of the      put the length into the OP_REVERSE item. Temporarily mark the end of the
6068      branch with OP_END. */      branch with OP_END. If the branch contains OP_RECURSE, the result is -3
6069        because there may be forward references that we can't check here. Set a
6070        flag to cause another lookbehind check at the end. Why not do it all at the
6071        end? Because common, erroneous checks are picked up here and the offset of
6072        the problem can be shown. */
6073    
6074      if (lookbehind)      if (lookbehind)
6075        {        {
6076        int fixed_length;        int fixed_length;
6077        *code = OP_END;        *code = OP_END;
6078        fixed_length = find_fixedlength(last_branch, options);        fixed_length = find_fixedlength(last_branch, options, FALSE, cd);
6079        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
6080        if (fixed_length < 0)        if (fixed_length == -3)
6081            {
6082            cd->check_lookbehind = TRUE;
6083            }
6084          else if (fixed_length < 0)
6085          {          {
6086          *errorcodeptr = (fixed_length == -2)? ERR36 : ERR25;          *errorcodeptr = (fixed_length == -2)? ERR36 : ERR25;
6087          *ptrptr = ptr;          *ptrptr = ptr;
6088          return FALSE;          return FALSE;
6089          }          }
6090        PUT(reverse_count, 0, fixed_length);        else { PUT(reverse_count, 0, fixed_length); }
6091        }        }
6092      }      }
6093    
# Line 5704  for (;;) Line 6121  for (;;)
6121      PUT(code, 1, code - start_bracket);      PUT(code, 1, code - start_bracket);
6122      code += 1 + LINK_SIZE;      code += 1 + LINK_SIZE;
6123    
6124      /* Resetting option if needed */      /* If it was a capturing subpattern, check to see if it contained any
6125        recursive back references. If so, we must wrap it in atomic brackets.
6126        In any event, remove the block from the chain. */
6127    
6128        if (capnumber > 0)
6129          {
6130          if (cd->open_caps->flag)
6131            {
6132            memmove(start_bracket + 1 + LINK_SIZE, start_bracket,
6133              code - start_bracket);
6134            *start_bracket = OP_ONCE;
6135            code += 1 + LINK_SIZE;
6136            PUT(start_bracket, 1, code - start_bracket);
6137            *code = OP_KET;
6138            PUT(code, 1, code - start_bracket);
6139            code += 1 + LINK_SIZE;
6140            length += 2 + 2*LINK_SIZE;
6141            }
6142          cd->open_caps = cd->open_caps->next;
6143          }
6144    
6145        /* Reset options if needed. */
6146    
6147      if ((options & PCRE_IMS) != oldims && *ptr == CHAR_RIGHT_PARENTHESIS)      if ((options & PCRE_IMS) != oldims && *ptr == CHAR_RIGHT_PARENTHESIS)
6148        {        {
# Line 5753  for (;;) Line 6191  for (;;)
6191      {      {
6192      *code = OP_ALT;      *code = OP_ALT;
6193      PUT(code, 1, code - last_branch);      PUT(code, 1, code - last_branch);
6194      bc.current = last_branch = code;      bc.current_branch = last_branch = code;
6195      code += 1 + LINK_SIZE;      code += 1 + LINK_SIZE;
6196      }      }
6197    
# Line 5900  do { Line 6338  do {
6338       switch (*scode)       switch (*scode)
6339         {         {
6340         case OP_CREF:         case OP_CREF:
6341           case OP_NCREF:
6342         case OP_RREF:         case OP_RREF:
6343           case OP_NRREF:
6344         case OP_DEF:         case OP_DEF:
6345         return FALSE;         return FALSE;
6346    
# Line 6069  int length = 1;  /* For final END opcode Line 6509  int length = 1;  /* For final END opcode
6509  int firstbyte, reqbyte, newline;  int firstbyte, reqbyte, newline;
6510  int errorcode = 0;  int errorcode = 0;
6511  int skipatstart = 0;  int skipatstart = 0;
6512  #ifdef SUPPORT_UTF8  BOOL utf8 = (options & PCRE_UTF8) != 0;
 BOOL utf8;  
 #endif  
6513  size_t size;  size_t size;
6514  uschar *code;  uschar *code;
6515  const uschar *codestart;  const uschar *codestart;
# Line 6114  if (erroroffset == NULL) Line 6552  if (erroroffset == NULL)
6552    
6553  *erroroffset = 0;  *erroroffset = 0;
6554    
 /* Can't support UTF8 unless PCRE has been compiled to include the code. */  
   
 #ifdef SUPPORT_UTF8  
 utf8 = (options & PCRE_UTF8) != 0;  
 if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&  
      (*erroroffset = _pcre_valid_utf8((uschar *)pattern, -1)) >= 0)  
   {  
   errorcode = ERR44;  
   goto PCRE_EARLY_ERROR_RETURN2;  
   }  
 #else  
 if ((options & PCRE_UTF8) != 0)  
   {  
   errorcode = ERR32;  
   goto PCRE_EARLY_ERROR_RETURN;  
   }  
 #endif  
   
 if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0)  
   {  
   errorcode = ERR17;  
   goto PCRE_EARLY_ERROR_RETURN;  
   }  
   
6555  /* Set up pointers to the individual character tables */  /* Set up pointers to the individual character tables */
6556    
6557  if (tables == NULL) tables = _pcre_default_tables;  if (tables == NULL) tables = _pcre_default_tables;
# Line 6146  cd->fcc = tables + fcc_offset; Line 6560  cd->fcc = tables + fcc_offset;
6560  cd->cbits = tables + cbits_offset;  cd->cbits = tables + cbits_offset;
6561  cd->ctypes = tables + ctypes_offset;  cd->ctypes = tables + ctypes_offset;
6562    
6563    /* Check that all undefined public option bits are zero */
6564    
6565    if ((options & ~PUBLIC_COMPILE_OPTIONS) != 0)
6566      {
6567      errorcode = ERR17;
6568      goto PCRE_EARLY_ERROR_RETURN;
6569      }
6570    
6571  /* Check for global one-time settings at the start of the pattern, and remember  /* Check for global one-time settings at the start of the pattern, and remember
6572  the offset for later. */  the offset for later. */
6573    
# Line 6155  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 6577  while (ptr[skipatstart] == CHAR_LEFT_PAR
6577    int newnl = 0;    int newnl = 0;
6578    int newbsr = 0;    int newbsr = 0;
6579    
6580      if (strncmp((char *)(ptr+skipatstart+2), STRING_UTF8_RIGHTPAR, 5) == 0)
6581        { skipatstart += 7; options |= PCRE_UTF8; continue; }
6582    
6583    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)
6584      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
6585    else if (strncmp((char *)(ptr+skipatstart+2), STRING_LF_RIGHTPAR, 3)  == 0)    else if (strncmp((char *)(ptr+skipatstart+2), STRING_LF_RIGHTPAR, 3)  == 0)
# Line 6178  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 6603  while (ptr[skipatstart] == CHAR_LEFT_PAR
6603    else break;    else break;
6604    }    }
6605    
6606    /* Can't support UTF8 unless PCRE has been compiled to include the code. */
6607    
6608    #ifdef SUPPORT_UTF8
6609    if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&
6610         (*erroroffset = _pcre_valid_utf8((USPTR)pattern, -1)) >= 0)
6611      {
6612      errorcode = ERR44;
6613      goto PCRE_EARLY_ERROR_RETURN2;
6614      }
6615    #else
6616    if (utf8)
6617      {
6618      errorcode = ERR32;
6619      goto PCRE_EARLY_ERROR_RETURN;
6620      }
6621    #endif
6622    
6623  /* Check validity of \R options. */  /* Check validity of \R options. */
6624    
6625  switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))  switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
# Line 6260  cd->end_pattern = (const uschar *)(patte Line 6702  cd->end_pattern = (const uschar *)(patte
6702  cd->req_varyopt = 0;  cd->req_varyopt = 0;
6703  cd->external_options = options;  cd->external_options = options;
6704  cd->external_flags = 0;  cd->external_flags = 0;
6705    cd->open_caps = NULL;
6706    
6707  /* Now do the pre-compile. On error, errorcode will be set non-zero, so we  /* Now do the pre-compile. On error, errorcode will be set non-zero, so we
6708  don't need to look at the result of the function here. The initial options have  don't need to look at the result of the function here. The initial options have
# Line 6334  cd->start_code = codestart; Line 6777  cd->start_code = codestart;
6777  cd->hwm = cworkspace;  cd->hwm = cworkspace;
6778  cd->req_varyopt = 0;  cd->req_varyopt = 0;
6779  cd->had_accept = FALSE;  cd->had_accept = FALSE;
6780    cd->check_lookbehind = FALSE;
6781    cd->open_caps = NULL;
6782    
6783  /* Set up a starting, non-extracting bracket, then compile the expression. On  /* Set up a starting, non-extracting bracket, then compile the expression. On
6784  error, errorcode will be set non-zero, so we don't need to look at the result  error, errorcode will be set non-zero, so we don't need to look at the result
# Line 6359  if debugging, leave the test till after Line 6804  if debugging, leave the test till after
6804    
6805  *code++ = OP_END;  *code++ = OP_END;
6806    
6807  #ifndef DEBUG  #ifndef PCRE_DEBUG
6808  if (code - codestart > length) errorcode = ERR23;  if (code - codestart > length) errorcode = ERR23;
6809  #endif  #endif
6810    
# Line 6372  while (errorcode == 0 && cd->hwm > cwork Line 6817  while (errorcode == 0 && cd->hwm > cwork
6817    cd->hwm -= LINK_SIZE;    cd->hwm -= LINK_SIZE;
6818    offset = GET(cd->hwm, 0);    offset = GET(cd->hwm, 0);
6819    recno = GET(codestart, offset);    recno = GET(codestart, offset);
6820    groupptr = find_bracket(codestart, (re->options & PCRE_UTF8) != 0, recno);    groupptr = _pcre_find_bracket(codestart, utf8, recno);
6821    if (groupptr == NULL) errorcode = ERR53;    if (groupptr == NULL) errorcode = ERR53;
6822      else PUT(((uschar *)codestart), offset, groupptr - codestart);      else PUT(((uschar *)codestart), offset, groupptr - codestart);
6823    }    }
# Line 6382  subpattern. */ Line 6827  subpattern. */
6827    
6828  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
6829    
6830    /* If there were any lookbehind assertions that contained OP_RECURSE
6831    (recursions or subroutine calls), a flag is set for them to be checked here,
6832    because they may contain forward references. Actual recursions can't be fixed
6833    length, but subroutine calls can. It is done like this so that those without
6834    OP_RECURSE that are not fixed length get a diagnosic with a useful offset. The
6835    exceptional ones forgo this. We scan the pattern to check that they are fixed
6836    length, and set their lengths. */
6837    
6838    if (cd->check_lookbehind)
6839      {
6840      uschar *cc = (uschar *)codestart;
6841    
6842      /* Loop, searching for OP_REVERSE items, and process those that do not have
6843      their length set. (Actually, it will also re-process any that have a length
6844      of zero, but that is a pathological case, and it does no harm.) When we find
6845      one, we temporarily terminate the branch it is in while we scan it. */
6846    
6847      for (cc = (uschar *)_pcre_find_bracket(codestart, utf8, -1);
6848           cc != NULL;
6849           cc = (uschar *)_pcre_find_bracket(cc, utf8, -1))
6850        {
6851        if (GET(cc, 1) == 0)
6852          {
6853          int fixed_length;
6854          uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);
6855          int end_op = *be;
6856          *be = OP_END;
6857          fixed_length = find_fixedlength(cc, re->options, TRUE, cd);
6858          *be = end_op;
6859          DPRINTF(("fixed length = %d\n", fixed_length));
6860          if (fixed_length < 0)
6861            {
6862            errorcode = (fixed_length == -2)? ERR36 : ERR25;
6863            break;
6864            }
6865          PUT(cc, 1, fixed_length);
6866          }
6867        cc += 1 + LINK_SIZE;
6868        }
6869      }
6870    
6871  /* Failed to compile, or error while post-processing */  /* Failed to compile, or error while post-processing */
6872    
6873  if (errorcode != 0)  if (errorcode != 0)
# Line 6442  if (reqbyte >= 0 && Line 6928  if (reqbyte >= 0 &&
6928  /* Print out the compiled data if debugging is enabled. This is never the  /* Print out the compiled data if debugging is enabled. This is never the
6929  case when building a production library. */  case when building a production library. */
6930    
6931  #ifdef DEBUG  #ifdef PCRE_DEBUG
   
6932  printf("Length = %d top_bracket = %d top_backref = %d\n",  printf("Length = %d top_bracket = %d top_backref = %d\n",
6933    length, re->top_bracket, re->top_backref);    length, re->top_bracket, re->top_backref);
6934    
# Line 6480  if (code - codestart > length) Line 6965  if (code - codestart > length)
6965    if (errorcodeptr != NULL) *errorcodeptr = ERR23;    if (errorcodeptr != NULL) *errorcodeptr = ERR23;
6966    return NULL;    return NULL;
6967    }    }
6968  #endif   /* DEBUG */  #endif   /* PCRE_DEBUG */
6969    
6970  return (pcre *)re;  return (pcre *)re;
6971  }  }

Legend:
Removed from v.406  
changed lines
  Added in v.514

  ViewVC Help
Powered by ViewVC 1.1.5