/[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 166 by ph10, Wed May 9 14:48:28 2007 UTC revision 213 by ph10, Wed Aug 15 11:34:14 2007 UTC
# Line 42  POSSIBILITY OF SUCH DAMAGE. Line 42  POSSIBILITY OF SUCH DAMAGE.
42  supporting internal functions that are not used by other modules. */  supporting internal functions that are not used by other modules. */
43    
44    
45    #ifdef HAVE_CONFIG_H
46    #include <config.h>
47    #endif
48    
49  #define NLBLOCK cd             /* Block containing newline information */  #define NLBLOCK cd             /* Block containing newline information */
50  #define PSSTART start_pattern  /* Field containing processed string start */  #define PSSTART start_pattern  /* Field containing processed string start */
51  #define PSEND   end_pattern    /* Field containing processed string end */  #define PSEND   end_pattern    /* Field containing processed string end */
52    
   
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    
# Line 58  used by pcretest. DEBUG is not defined w Line 61  used by pcretest. DEBUG is not defined w
61  #endif  #endif
62    
63    
64    /* Macro for setting individual bits in class bitmaps. */
65    
66    #define SETBIT(a,b) a[b/8] |= (1 << (b%8))
67    
68    /* Maximum length value to check against when making sure that the integer that
69    holds the compiled pattern length does not overflow. We make it a bit less than
70    INT_MAX to allow for adding in group terminating bytes, so that we don't have
71    to check them every time. */
72    
73    #define OFLOW_MAX (INT_MAX - 20)
74    
75    
76  /*************************************************  /*************************************************
77  *      Code parameters and static tables         *  *      Code parameters and static tables         *
78  *************************************************/  *************************************************/
# Line 87  static const short int escapes[] = { Line 102  static const short int escapes[] = {
102       0,      0,      0,      0,      0,      0,      0,      0,   /* 0 - 7 */       0,      0,      0,      0,      0,      0,      0,      0,   /* 0 - 7 */
103       0,      0,    ':',    ';',    '<',    '=',    '>',    '?',   /* 8 - ? */       0,      0,    ':',    ';',    '<',    '=',    '>',    '?',   /* 8 - ? */
104     '@', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E,      0, -ESC_G,   /* @ - G */     '@', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E,      0, -ESC_G,   /* @ - G */
105       0,      0,      0,      0,      0,      0,      0,      0,   /* H - O */  -ESC_H,      0,      0, -ESC_K,      0,      0,      0,      0,   /* H - O */
106  -ESC_P, -ESC_Q, -ESC_R, -ESC_S,      0,      0,      0, -ESC_W,   /* P - W */  -ESC_P, -ESC_Q, -ESC_R, -ESC_S,      0,      0, -ESC_V, -ESC_W,   /* P - W */
107  -ESC_X,      0, -ESC_Z,    '[',   '\\',    ']',    '^',    '_',   /* X - _ */  -ESC_X,      0, -ESC_Z,    '[',   '\\',    ']',    '^',    '_',   /* X - _ */
108     '`',      7, -ESC_b,      0, -ESC_d,  ESC_e,  ESC_f,      0,   /* ` - g */     '`',      7, -ESC_b,      0, -ESC_d,  ESC_e,  ESC_f,      0,   /* ` - g */
109       0,      0,      0, -ESC_k,      0,      0,  ESC_n,      0,   /* h - o */  -ESC_h,      0,      0, -ESC_k,      0,      0,  ESC_n,      0,   /* h - o */
110  -ESC_p,      0,  ESC_r, -ESC_s,  ESC_tee,    0,      0, -ESC_w,   /* p - w */  -ESC_p,      0,  ESC_r, -ESC_s,  ESC_tee,    0, -ESC_v, -ESC_w,   /* p - w */
111       0,      0, -ESC_z                                            /* x - z */       0,      0, -ESC_z                                            /* x - z */
112  };  };
113    
# Line 106  static const short int escapes[] = { Line 121  static const short int escapes[] = {
121  /*  70 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  70 */     0,     0,      0,       0,      0,     0,      0,      0,
122  /*  78 */     0,   '`',    ':',     '#',    '@',  '\'',    '=',    '"',  /*  78 */     0,   '`',    ':',     '#',    '@',  '\'',    '=',    '"',
123  /*  80 */     0,     7, -ESC_b,       0, -ESC_d, ESC_e,  ESC_f,      0,  /*  80 */     0,     7, -ESC_b,       0, -ESC_d, ESC_e,  ESC_f,      0,
124  /*  88 */     0,     0,      0,     '{',      0,     0,      0,      0,  /*  88 */-ESC_h,     0,      0,     '{',      0,     0,      0,      0,
125  /*  90 */     0,     0, -ESC_k,     'l',      0, ESC_n,      0, -ESC_p,  /*  90 */     0,     0, -ESC_k,     'l',      0, ESC_n,      0, -ESC_p,
126  /*  98 */     0, ESC_r,      0,     '}',      0,     0,      0,      0,  /*  98 */     0, ESC_r,      0,     '}',      0,     0,      0,      0,
127  /*  A0 */     0,   '~', -ESC_s, ESC_tee,      0,     0, -ESC_w,      0,  /*  A0 */     0,   '~', -ESC_s, ESC_tee,      0,-ESC_v, -ESC_w,      0,
128  /*  A8 */     0,-ESC_z,      0,       0,      0,   '[',      0,      0,  /*  A8 */     0,-ESC_z,      0,       0,      0,   '[',      0,      0,
129  /*  B0 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  B0 */     0,     0,      0,       0,      0,     0,      0,      0,
130  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',
131  /*  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,
132  /*  C8 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,
133  /*  D0 */   '}',     0,      0,       0,      0,     0,      0, -ESC_P,  /*  D0 */   '}',     0, -ESC_K,       0,      0,     0,      0, -ESC_P,
134  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,
135  /*  E0 */  '\\',     0, -ESC_S,       0,      0,     0, -ESC_W, -ESC_X,  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,
136  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,
137  /*  F0 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  F0 */     0,     0,      0,       0,      0,     0,      0,      0,
138  /*  F8 */     0,     0,      0,       0,      0,     0,      0,      0  /*  F8 */     0,     0,      0,       0,      0,     0,      0,      0
# Line 125  static const short int escapes[] = { Line 140  static const short int escapes[] = {
140  #endif  #endif
141    
142    
143    /* Table of special "verbs" like (*PRUNE) */
144    
145    typedef struct verbitem {
146      const char *name;
147      int   len;
148      int   op;
149    } verbitem;
150    
151    static verbitem verbs[] = {
152      { "ACCEPT", 6, OP_ACCEPT },
153      { "COMMIT", 6, OP_COMMIT },
154      { "F",      1, OP_FAIL },
155      { "FAIL",   4, OP_FAIL },
156      { "PRUNE",  5, OP_PRUNE },
157      { "SKIP",   4, OP_SKIP  },
158      { "THEN",   4, OP_THEN  }
159    };
160    
161    static int verbcount = sizeof(verbs)/sizeof(verbitem);
162    
163    
164  /* Tables of names of POSIX character classes and their lengths. The list is  /* Tables of names of POSIX character classes and their lengths. The list is
165  terminated by a zero length entry. The first three must be alpha, lower, upper,  terminated by a zero length entry. The first three must be alpha, lower, upper,
166  as this is assumed for handling case independence. */  as this is assumed for handling case independence. */
# Line 198  static const char *error_texts[] = { Line 234  static const char *error_texts[] = {
234    "missing ) after comment",    "missing ) after comment",
235    "parentheses nested too deeply",  /** DEAD **/    "parentheses nested too deeply",  /** DEAD **/
236    /* 20 */    /* 20 */
237    "regular expression too large",    "regular expression is too large",
238    "failed to get memory",    "failed to get memory",
239    "unmatched parentheses",    "unmatched parentheses",
240    "internal error: code overflow",    "internal error: code overflow",
# Line 234  static const char *error_texts[] = { Line 270  static const char *error_texts[] = {
270    "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)",    "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)",
271    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")",    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")",
272    /* 50 */    /* 50 */
273    "repeated subpattern is too long",    "repeated subpattern is too long",    /** DEAD **/
274    "octal value is greater than \\377 (not in UTF-8 mode)",    "octal value is greater than \\377 (not in UTF-8 mode)",
275    "internal error: overran compiling workspace",    "internal error: overran compiling workspace",
276    "internal error: previously-checked referenced subpattern not found",    "internal error: previously-checked referenced subpattern not found",
# Line 242  static const char *error_texts[] = { Line 278  static const char *error_texts[] = {
278    /* 55 */    /* 55 */
279    "repeating a DEFINE group is not allowed",    "repeating a DEFINE group is not allowed",
280    "inconsistent NEWLINE options",    "inconsistent NEWLINE options",
281    "\\g is not followed by an (optionally braced) non-zero number",    "\\g is not followed by a braced name or an optionally braced non-zero number",
282    "(?+ or (?- must be followed by a non-zero number"    "(?+ or (?- or (?(+ or (?(- must be followed by a non-zero number",
283      "(*VERB) with an argument is not supported",
284      /* 60 */
285      "(*VERB) not recognized",
286      "number is too big"
287  };  };
288    
289    
# Line 374  static const unsigned char ebcdic_charta Line 414  static const unsigned char ebcdic_charta
414  /* Definition to allow mutual recursion */  /* Definition to allow mutual recursion */
415    
416  static BOOL  static BOOL
417    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, int, int *,    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,
418      int *, branch_chain *, compile_data *, int *);      int *, int *, branch_chain *, compile_data *, int *);
419    
420    
421    
# Line 400  Arguments: Line 440  Arguments:
440    
441  Returns:         zero or positive => a data character  Returns:         zero or positive => a data character
442                   negative => a special escape sequence                   negative => a special escape sequence
443                   on error, errorptr is set                   on error, errorcodeptr is set
444  */  */
445    
446  static int  static int
# Line 453  else Line 493  else
493    
494      /* \g must be followed by a number, either plain or braced. If positive, it      /* \g must be followed by a number, either plain or braced. If positive, it
495      is an absolute backreference. If negative, it is a relative backreference.      is an absolute backreference. If negative, it is a relative backreference.
496      This is a Perl 5.10 feature. */      This is a Perl 5.10 feature. Perl 5.10 also supports \g{name} as a
497        reference to a named group. This is part of Perl's movement towards a
498        unified syntax for back references. As this is synonymous with \k{name}, we
499        fudge it up by pretending it really was \k. */
500    
501      case 'g':      case 'g':
502      if (ptr[1] == '{')      if (ptr[1] == '{')
503        {        {
504          const uschar *p;
505          for (p = ptr+2; *p != 0 && *p != '}'; p++)
506            if (*p != '-' && (digitab[*p] & ctype_digit) == 0) break;
507          if (*p != 0 && *p != '}')
508            {
509            c = -ESC_k;
510            break;
511            }
512        braced = TRUE;        braced = TRUE;
513        ptr++;        ptr++;
514        }        }
# Line 473  else Line 524  else
524      c = 0;      c = 0;
525      while ((digitab[ptr[1]] & ctype_digit) != 0)      while ((digitab[ptr[1]] & ctype_digit) != 0)
526        c = c * 10 + *(++ptr) - '0';        c = c * 10 + *(++ptr) - '0';
527    
528        if (c < 0)
529          {
530          *errorcodeptr = ERR61;
531          break;
532          }
533    
534      if (c == 0 || (braced && *(++ptr) != '}'))      if (c == 0 || (braced && *(++ptr) != '}'))
535        {        {
536        *errorcodeptr = ERR57;        *errorcodeptr = ERR57;
537        return 0;        break;
538        }        }
539    
540      if (negated)      if (negated)
# Line 485  else Line 542  else
542        if (c > bracount)        if (c > bracount)
543          {          {
544          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
545          return 0;          break;
546          }          }
547        c = bracount - (c - 1);        c = bracount - (c - 1);
548        }        }
# Line 514  else Line 571  else
571        c -= '0';        c -= '0';
572        while ((digitab[ptr[1]] & ctype_digit) != 0)        while ((digitab[ptr[1]] & ctype_digit) != 0)
573          c = c * 10 + *(++ptr) - '0';          c = c * 10 + *(++ptr) - '0';
574          if (c < 0)
575            {
576            *errorcodeptr = ERR61;
577            break;
578            }
579        if (c < 10 || c <= bracount)        if (c < 10 || c <= bracount)
580          {          {
581          c = -(ESC_REF + c);          c = -(ESC_REF + c);
# Line 609  else Line 671  else
671      if (c == 0)      if (c == 0)
672        {        {
673        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
674        return 0;        break;
675        }        }
676    
677  #ifndef EBCDIC  /* ASCII coding */  #ifndef EBCDIC  /* ASCII coding */
# Line 685  if (c == '{') Line 747  if (c == '{')
747      *negptr = TRUE;      *negptr = TRUE;
748      ptr++;      ptr++;
749      }      }
750    for (i = 0; i < sizeof(name) - 1; i++)    for (i = 0; i < (int)sizeof(name) - 1; i++)
751      {      {
752      c = *(++ptr);      c = *(++ptr);
753      if (c == 0) goto ERROR_RETURN;      if (c == 0) goto ERROR_RETURN;
# Line 915  for (; *ptr != 0; ptr++) Line 977  for (; *ptr != 0; ptr++)
977    /* An opening parens must now be a real metacharacter */    /* An opening parens must now be a real metacharacter */
978    
979    if (*ptr != '(') continue;    if (*ptr != '(') continue;
980    if (ptr[1] != '?')    if (ptr[1] != '?' && ptr[1] != '*')
981      {      {
982      count++;      count++;
983      if (name == NULL && count == lorn) return count;      if (name == NULL && count == lorn) return count;
# Line 1371  for (code = first_significant_code(code Line 1433  for (code = first_significant_code(code
1433    
1434    c = *code;    c = *code;
1435    
1436    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE)    /* Groups with zero repeats can of course be empty; skip them. */
1437    
1438      if (c == OP_BRAZERO || c == OP_BRAMINZERO)
1439        {
1440        code += _pcre_OP_lengths[c];
1441        do code += GET(code, 1); while (*code == OP_ALT);
1442        c = *code;
1443        continue;
1444        }
1445    
1446      /* For other groups, scan the branches. */
1447    
1448      if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)
1449      {      {
1450      BOOL empty_branch;      BOOL empty_branch;
1451      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 1387  for (code = first_significant_code(code Line 1461  for (code = first_significant_code(code
1461        }        }
1462      while (*code == OP_ALT);      while (*code == OP_ALT);
1463      if (!empty_branch) return FALSE;   /* All branches are non-empty */      if (!empty_branch) return FALSE;   /* All branches are non-empty */
1464        c = *code;
     /* Move past the KET and fudge things so that the increment in the "for"  
     above has no effect. */  
   
     c = OP_END;  
     code += 1 + LINK_SIZE - _pcre_OP_lengths[c];  
1465      continue;      continue;
1466      }      }
1467    
# Line 1926  if (next >= 0) switch(op_code) Line 1995  if (next >= 0) switch(op_code)
1995    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
1996    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;
1997    
1998      case OP_HSPACE:
1999      case OP_NOT_HSPACE:
2000      switch(next)
2001        {
2002        case 0x09:
2003        case 0x20:
2004        case 0xa0:
2005        case 0x1680:
2006        case 0x180e:
2007        case 0x2000:
2008        case 0x2001:
2009        case 0x2002:
2010        case 0x2003:
2011        case 0x2004:
2012        case 0x2005:
2013        case 0x2006:
2014        case 0x2007:
2015        case 0x2008:
2016        case 0x2009:
2017        case 0x200A:
2018        case 0x202f:
2019        case 0x205f:
2020        case 0x3000:
2021        return op_code != OP_HSPACE;
2022        default:
2023        return op_code == OP_HSPACE;
2024        }
2025    
2026      case OP_VSPACE:
2027      case OP_NOT_VSPACE:
2028      switch(next)
2029        {
2030        case 0x0a:
2031        case 0x0b:
2032        case 0x0c:
2033        case 0x0d:
2034        case 0x85:
2035        case 0x2028:
2036        case 0x2029:
2037        return op_code != OP_VSPACE;
2038        default:
2039        return op_code == OP_VSPACE;
2040        }
2041    
2042    default:    default:
2043    return FALSE;    return FALSE;
2044    }    }
# Line 1960  switch(op_code) Line 2073  switch(op_code)
2073      case ESC_W:      case ESC_W:
2074      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;
2075    
2076        case ESC_h:
2077        case ESC_H:
2078        switch(item)
2079          {
2080          case 0x09:
2081          case 0x20:
2082          case 0xa0:
2083          case 0x1680:
2084          case 0x180e:
2085          case 0x2000:
2086          case 0x2001:
2087          case 0x2002:
2088          case 0x2003:
2089          case 0x2004:
2090          case 0x2005:
2091          case 0x2006:
2092          case 0x2007:
2093          case 0x2008:
2094          case 0x2009:
2095          case 0x200A:
2096          case 0x202f:
2097          case 0x205f:
2098          case 0x3000:
2099          return -next != ESC_h;
2100          default:
2101          return -next == ESC_h;
2102          }
2103    
2104        case ESC_v:
2105        case ESC_V:
2106        switch(item)
2107          {
2108          case 0x0a:
2109          case 0x0b:
2110          case 0x0c:
2111          case 0x0d:
2112          case 0x85:
2113          case 0x2028:
2114          case 0x2029:
2115          return -next != ESC_v;
2116          default:
2117          return -next == ESC_v;
2118          }
2119    
2120      default:      default:
2121      return FALSE;      return FALSE;
2122      }      }
2123    
2124    case OP_DIGIT:    case OP_DIGIT:
2125    return next == -ESC_D || next == -ESC_s || next == -ESC_W;    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||
2126             next == -ESC_h || next == -ESC_v;
2127    
2128    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
2129    return next == -ESC_d;    return next == -ESC_d;
# Line 1974  switch(op_code) Line 2132  switch(op_code)
2132    return next == -ESC_S || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_d || next == -ESC_w;
2133    
2134    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
2135    return next == -ESC_s;    return next == -ESC_s || next == -ESC_h || next == -ESC_v;
2136    
2137      case OP_HSPACE:
2138      return next == -ESC_S || next == -ESC_H || next == -ESC_d || next == -ESC_w;
2139    
2140      case OP_NOT_HSPACE:
2141      return next == -ESC_h;
2142    
2143      /* Can't have \S in here because VT matches \S (Perl anomaly) */
2144      case OP_VSPACE:
2145      return next == -ESC_V || next == -ESC_d || next == -ESC_w;
2146    
2147      case OP_NOT_VSPACE:
2148      return next == -ESC_v;
2149    
2150    case OP_WORDCHAR:    case OP_WORDCHAR:
2151    return next == -ESC_W || next == -ESC_s;    return next == -ESC_W || next == -ESC_s || next == -ESC_h || next == -ESC_v;
2152    
2153    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
2154    return next == -ESC_w || next == -ESC_d;    return next == -ESC_w || next == -ESC_d;
# Line 2092  for (;; ptr++) Line 2263  for (;; ptr++)
2263    BOOL possessive_quantifier;    BOOL possessive_quantifier;
2264    BOOL is_quantifier;    BOOL is_quantifier;
2265    BOOL is_recurse;    BOOL is_recurse;
2266      BOOL reset_bracount;
2267    int class_charcount;    int class_charcount;
2268    int class_lastchar;    int class_lastchar;
2269    int newoptions;    int newoptions;
2270    int recno;    int recno;
2271      int refsign;
2272    int skipbytes;    int skipbytes;
2273    int subreqbyte;    int subreqbyte;
2274    int subfirstbyte;    int subfirstbyte;
# Line 2128  for (;; ptr++) Line 2301  for (;; ptr++)
2301      */      */
2302    
2303      if (code < last_code) code = last_code;      if (code < last_code) code = last_code;
2304    
2305        /* Paranoid check for integer overflow */
2306    
2307        if (OFLOW_MAX - *lengthptr < code - last_code)
2308          {
2309          *errorcodeptr = ERR20;
2310          goto FAILED;
2311          }
2312    
2313      *lengthptr += code - last_code;      *lengthptr += code - last_code;
2314      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));
2315    
# Line 2240  for (;; ptr++) Line 2422  for (;; ptr++)
2422      *ptrptr = ptr;      *ptrptr = ptr;
2423      if (lengthptr != NULL)      if (lengthptr != NULL)
2424        {        {
2425          if (OFLOW_MAX - *lengthptr < code - last_code)
2426            {
2427            *errorcodeptr = ERR20;
2428            goto FAILED;
2429            }
2430        *lengthptr += code - last_code;   /* To include callout length */        *lengthptr += code - last_code;   /* To include callout length */
2431        DPRINTF((">> end branch\n"));        DPRINTF((">> end branch\n"));
2432        }        }
# Line 2302  for (;; ptr++) Line 2489  for (;; ptr++)
2489        goto FAILED;        goto FAILED;
2490        }        }
2491    
2492      /* If the first character is '^', set the negation flag and skip it. */      /* If the first character is '^', set the negation flag and skip it. Also,
2493        if the first few characters (either before or after ^) are \Q\E or \E we
2494        skip them too. This makes for compatibility with Perl. */
2495    
2496      if ((c = *(++ptr)) == '^')      negate_class = FALSE;
2497        for (;;)
2498        {        {
       negate_class = TRUE;  
2499        c = *(++ptr);        c = *(++ptr);
2500        }        if (c == '\\')
2501      else          {
2502        {          if (ptr[1] == 'E') ptr++;
2503        negate_class = FALSE;            else if (strncmp((const char *)ptr+1, "Q\\E", 3) == 0) ptr += 3;
2504                else break;
2505            }
2506          else if (!negate_class && c == '^')
2507            negate_class = TRUE;
2508          else break;
2509        }        }
2510    
2511      /* Keep a count of chars with values < 256 so that we can optimize the case      /* Keep a count of chars with values < 256 so that we can optimize the case
# Line 2452  for (;; ptr++) Line 2646  for (;; ptr++)
2646        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
2647        case. Inside a class (and only there) it is treated as backspace.        case. Inside a class (and only there) it is treated as backspace.
2648        Elsewhere it marks a word boundary. Other escapes have preset maps ready        Elsewhere it marks a word boundary. Other escapes have preset maps ready
2649        to or into the one we are building. We assume they have more than one        to 'or' into the one we are building. We assume they have more than one
2650        character in them, so set class_charcount bigger than one. */        character in them, so set class_charcount bigger than one. */
2651    
2652        if (c == '\\')        if (c == '\\')
# Line 2520  for (;; ptr++) Line 2714  for (;; ptr++)
2714            else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||            else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||
2715                     c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;                     c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;
2716    
2717              /* We need to deal with \H, \h, \V, and \v in both phases because
2718              they use extra memory. */
2719    
2720              if (-c == ESC_h)
2721                {
2722                SETBIT(classbits, 0x09); /* VT */
2723                SETBIT(classbits, 0x20); /* SPACE */
2724                SETBIT(classbits, 0xa0); /* NSBP */
2725    #ifdef SUPPORT_UTF8
2726                if (utf8)
2727                  {
2728                  class_utf8 = TRUE;
2729                  *class_utf8data++ = XCL_SINGLE;
2730                  class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);
2731                  *class_utf8data++ = XCL_SINGLE;
2732                  class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);
2733                  *class_utf8data++ = XCL_RANGE;
2734                  class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);
2735                  class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);
2736                  *class_utf8data++ = XCL_SINGLE;
2737                  class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);
2738                  *class_utf8data++ = XCL_SINGLE;
2739                  class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);
2740                  *class_utf8data++ = XCL_SINGLE;
2741                  class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);
2742                  }
2743    #endif
2744                continue;
2745                }
2746    
2747              if (-c == ESC_H)
2748                {
2749                for (c = 0; c < 32; c++)
2750                  {
2751                  int x = 0xff;
2752                  switch (c)
2753                    {
2754                    case 0x09/8: x ^= 1 << (0x09%8); break;
2755                    case 0x20/8: x ^= 1 << (0x20%8); break;
2756                    case 0xa0/8: x ^= 1 << (0xa0%8); break;
2757                    default: break;
2758                    }
2759                  classbits[c] |= x;
2760                  }
2761    
2762    #ifdef SUPPORT_UTF8
2763                if (utf8)
2764                  {
2765                  class_utf8 = TRUE;
2766                  *class_utf8data++ = XCL_RANGE;
2767                  class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);
2768                  class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);
2769                  *class_utf8data++ = XCL_RANGE;
2770                  class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);
2771                  class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);
2772                  *class_utf8data++ = XCL_RANGE;
2773                  class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);
2774                  class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);
2775                  *class_utf8data++ = XCL_RANGE;
2776                  class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);
2777                  class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);
2778                  *class_utf8data++ = XCL_RANGE;
2779                  class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);
2780                  class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);
2781                  *class_utf8data++ = XCL_RANGE;
2782                  class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);
2783                  class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);
2784                  *class_utf8data++ = XCL_RANGE;
2785                  class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);
2786                  class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);
2787                  }
2788    #endif
2789                continue;
2790                }
2791    
2792              if (-c == ESC_v)
2793                {
2794                SETBIT(classbits, 0x0a); /* LF */
2795                SETBIT(classbits, 0x0b); /* VT */
2796                SETBIT(classbits, 0x0c); /* FF */
2797                SETBIT(classbits, 0x0d); /* CR */
2798                SETBIT(classbits, 0x85); /* NEL */
2799    #ifdef SUPPORT_UTF8
2800                if (utf8)
2801                  {
2802                  class_utf8 = TRUE;
2803                  *class_utf8data++ = XCL_RANGE;
2804                  class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);
2805                  class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);
2806                  }
2807    #endif
2808                continue;
2809                }
2810    
2811              if (-c == ESC_V)
2812                {
2813                for (c = 0; c < 32; c++)
2814                  {
2815                  int x = 0xff;
2816                  switch (c)
2817                    {
2818                    case 0x0a/8: x ^= 1 << (0x0a%8);
2819                                 x ^= 1 << (0x0b%8);
2820                                 x ^= 1 << (0x0c%8);
2821                                 x ^= 1 << (0x0d%8);
2822                                 break;
2823                    case 0x85/8: x ^= 1 << (0x85%8); break;
2824                    default: break;
2825                    }
2826                  classbits[c] |= x;
2827                  }
2828    
2829    #ifdef SUPPORT_UTF8
2830                if (utf8)
2831                  {
2832                  class_utf8 = TRUE;
2833                  *class_utf8data++ = XCL_RANGE;
2834                  class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);
2835                  class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);
2836                  *class_utf8data++ = XCL_RANGE;
2837                  class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);
2838                  class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);
2839                  }
2840    #endif
2841                continue;
2842                }
2843    
2844            /* We need to deal with \P and \p in both phases. */            /* We need to deal with \P and \p in both phases. */
2845    
2846  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 2660  for (;; ptr++) Line 2981  for (;; ptr++)
2981              unsigned int origd = d;              unsigned int origd = d;
2982              while (get_othercase_range(&cc, origd, &occ, &ocd))              while (get_othercase_range(&cc, origd, &occ, &ocd))
2983                {                {
2984                if (occ >= c && ocd <= d) continue;  /* Skip embedded ranges */                if (occ >= (unsigned int)c &&
2985                      ocd <= (unsigned int)d)
2986                    continue;                          /* Skip embedded ranges */
2987    
2988                if (occ < c  && ocd >= c - 1)        /* Extend the basic range */                if (occ < (unsigned int)c  &&
2989                      ocd >= (unsigned int)c - 1)      /* Extend the basic range */
2990                  {                                  /* if there is overlap,   */                  {                                  /* if there is overlap,   */
2991                  c = occ;                           /* noting that if occ < c */                  c = occ;                           /* noting that if occ < c */
2992                  continue;                          /* we can't have ocd > d  */                  continue;                          /* we can't have ocd > d  */
2993                  }                                  /* because a subrange is  */                  }                                  /* because a subrange is  */
2994                if (ocd > d && occ <= d + 1)         /* always shorter than    */                if (ocd > (unsigned int)d &&
2995                      occ <= (unsigned int)d + 1)      /* always shorter than    */
2996                  {                                  /* the basic range.       */                  {                                  /* the basic range.       */
2997                  d = ocd;                  d = ocd;
2998                  continue;                  continue;
# Line 3263  for (;; ptr++) Line 3588  for (;; ptr++)
3588          goto FAILED;          goto FAILED;
3589          }          }
3590    
       /* This is a paranoid check to stop integer overflow later on */  
   
       if (len > MAX_DUPLENGTH)  
         {  
         *errorcodeptr = ERR50;  
         goto FAILED;  
         }  
   
3591        /* If the maximum repeat count is unlimited, find the end of the bracket        /* If the maximum repeat count is unlimited, find the end of the bracket
3592        by scanning through from the start, and compute the offset back to it        by scanning through from the start, and compute the offset back to it
3593        from the current code pointer. There may be an OP_OPT setting following        from the current code pointer. There may be an OP_OPT setting following
# Line 3359  for (;; ptr++) Line 3676  for (;; ptr++)
3676          if (repeat_min > 1)          if (repeat_min > 1)
3677            {            {
3678            /* 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
3679            just adjust the length as if we had. */            just adjust the length as if we had. Do some paranoid checks for
3680              potential integer overflow. */
3681    
3682            if (lengthptr != NULL)            if (lengthptr != NULL)
3683              *lengthptr += (repeat_min - 1)*length_prevgroup;              {
3684                int delta = (repeat_min - 1)*length_prevgroup;
3685                if ((double)(repeat_min - 1)*(double)length_prevgroup >
3686                                                                (double)INT_MAX ||
3687                    OFLOW_MAX - *lengthptr < delta)
3688                  {
3689                  *errorcodeptr = ERR20;
3690                  goto FAILED;
3691                  }
3692                *lengthptr += delta;
3693                }
3694    
3695            /* This is compiling for real */            /* This is compiling for real */
3696    
# Line 3400  for (;; ptr++) Line 3728  for (;; ptr++)
3728          /* 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
3729          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
3730          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
3731          add 2 + 2*LINKSIZE to allow for the nesting that occurs. */          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
3732            paranoid checks to avoid integer overflow. */
3733    
3734          if (lengthptr != NULL && repeat_max > 0)          if (lengthptr != NULL && repeat_max > 0)
3735            *lengthptr += repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -            {
3736              2 - 2*LINK_SIZE;  /* Last one doesn't nest */            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
3737                          2 - 2*LINK_SIZE;   /* Last one doesn't nest */
3738              if ((double)repeat_max *
3739                    (double)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
3740                      > (double)INT_MAX ||
3741                  OFLOW_MAX - *lengthptr < delta)
3742                {
3743                *errorcodeptr = ERR20;
3744                goto FAILED;
3745                }
3746              *lengthptr += delta;
3747              }
3748    
3749          /* This is compiling for real */          /* This is compiling for real */
3750    
# Line 3556  for (;; ptr++) Line 3896  for (;; ptr++)
3896      /* ===================================================================*/      /* ===================================================================*/
3897      /* Start of nested parenthesized sub-expression, or comment or lookahead or      /* Start of nested parenthesized sub-expression, or comment or lookahead or
3898      lookbehind or option setting or condition or all the other extended      lookbehind or option setting or condition or all the other extended
3899      parenthesis forms. First deal with the specials; all are introduced by ?,      parenthesis forms.  */
     and the appearance of any of them means that this is not a capturing  
     group. */  
3900    
3901      case '(':      case '(':
3902      newoptions = options;      newoptions = options;
3903      skipbytes = 0;      skipbytes = 0;
3904      bravalue = OP_CBRA;      bravalue = OP_CBRA;
3905      save_hwm = cd->hwm;      save_hwm = cd->hwm;
3906        reset_bracount = FALSE;
3907    
3908        /* First deal with various "verbs" that can be introduced by '*'. */
3909    
3910      if (*(++ptr) == '?')      if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)
3911          {
3912          int i, namelen;
3913          const uschar *name = ++ptr;
3914          previous = NULL;
3915          while ((cd->ctypes[*++ptr] & ctype_letter) != 0);
3916          if (*ptr == ':')
3917            {
3918            *errorcodeptr = ERR59;   /* Not supported */
3919            goto FAILED;
3920            }
3921          if (*ptr != ')')
3922            {
3923            *errorcodeptr = ERR60;
3924            goto FAILED;
3925            }
3926          namelen = ptr - name;
3927          for (i = 0; i < verbcount; i++)
3928            {
3929            if (namelen == verbs[i].len &&
3930                strncmp((char *)name, verbs[i].name, namelen) == 0)
3931              {
3932              *code = verbs[i].op;
3933              if (*code++ == OP_ACCEPT) cd->had_accept = TRUE;
3934              break;
3935              }
3936            }
3937          if (i < verbcount) continue;
3938          *errorcodeptr = ERR60;
3939          goto FAILED;
3940          }
3941    
3942        /* Deal with the extended parentheses; all are introduced by '?', and the
3943        appearance of any of them means that this is not a capturing group. */
3944    
3945        else if (*ptr == '?')
3946        {        {
3947        int i, set, unset, namelen;        int i, set, unset, namelen;
3948        int *optset;        int *optset;
# Line 3587  for (;; ptr++) Line 3963  for (;; ptr++)
3963    
3964    
3965          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
3966            case '|':                 /* Reset capture count for each branch */
3967            reset_bracount = TRUE;
3968            /* Fall through */
3969    
3970            /* ------------------------------------------------------------ */
3971          case ':':                 /* Non-capturing bracket */          case ':':                 /* Non-capturing bracket */
3972          bravalue = OP_BRA;          bravalue = OP_BRA;
3973          ptr++;          ptr++;
# Line 3622  for (;; ptr++) Line 4003  for (;; ptr++)
4003    
4004          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
4005          skipbytes = 3;          skipbytes = 3;
4006            refsign = -1;
4007    
4008          /* Check for a test for recursion in a named group. */          /* Check for a test for recursion in a named group. */
4009    
# Line 3645  for (;; ptr++) Line 4027  for (;; ptr++)
4027            terminator = '\'';            terminator = '\'';
4028            ptr++;            ptr++;
4029            }            }
4030          else terminator = 0;          else
4031              {
4032              terminator = 0;
4033              if (ptr[1] == '-' || ptr[1] == '+') refsign = *(++ptr);
4034              }
4035    
4036          /* We now expect to read a name; any thing else is an error */          /* We now expect to read a name; any thing else is an error */
4037    
# Line 3681  for (;; ptr++) Line 4067  for (;; ptr++)
4067          if (lengthptr != NULL) break;          if (lengthptr != NULL) break;
4068    
4069          /* In the real compile we do the work of looking for the actual          /* In the real compile we do the work of looking for the actual
4070          reference. */          reference. If the string started with "+" or "-" we require the rest to
4071            be digits, in which case recno will be set. */
4072    
4073            if (refsign > 0)
4074              {
4075              if (recno <= 0)
4076                {
4077                *errorcodeptr = ERR58;
4078                goto FAILED;
4079                }
4080              if (refsign == '-')
4081                {
4082                recno = cd->bracount - recno + 1;
4083                if (recno <= 0)
4084                  {
4085                  *errorcodeptr = ERR15;
4086                  goto FAILED;
4087                  }
4088                }
4089              else recno += cd->bracount;
4090              PUT2(code, 2+LINK_SIZE, recno);
4091              break;
4092              }
4093    
4094            /* Otherwise (did not start with "+" or "-"), start by looking for the
4095            name. */
4096    
4097          slot = cd->name_table;          slot = cd->name_table;
4098          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
# Line 3773  for (;; ptr++) Line 4184  for (;; ptr++)
4184    
4185          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4186          case '!':                 /* Negative lookahead */          case '!':                 /* Negative lookahead */
         bravalue = OP_ASSERT_NOT;  
4187          ptr++;          ptr++;
4188            if (*ptr == ')')          /* Optimize (?!) */
4189              {
4190              *code++ = OP_FAIL;
4191              previous = NULL;
4192              continue;
4193              }
4194            bravalue = OP_ASSERT_NOT;
4195          break;          break;
4196    
4197    
# Line 4005  for (;; ptr++) Line 4422  for (;; ptr++)
4422          case '5': case '6': case '7': case '8': case '9':   /* subroutine */          case '5': case '6': case '7': case '8': case '9':   /* subroutine */
4423            {            {
4424            const uschar *called;            const uschar *called;
           int sign = *ptr;  
4425    
4426            if (sign == '+') ptr++;            if ((refsign = *ptr) == '+') ptr++;
4427            else if (sign == '-')            else if (refsign == '-')
4428              {              {
4429              if ((digitab[ptr[1]] & ctype_digit) == 0)              if ((digitab[ptr[1]] & ctype_digit) == 0)
4430                goto OTHER_CHAR_AFTER_QUERY;                goto OTHER_CHAR_AFTER_QUERY;
4431              ptr++;              ptr++;
4432              }              }
4433    
4434            recno = 0;            recno = 0;
4435            while((digitab[*ptr] & ctype_digit) != 0)            while((digitab[*ptr] & ctype_digit) != 0)
4436              recno = recno * 10 + *ptr++ - '0';              recno = recno * 10 + *ptr++ - '0';
# Line 4024  for (;; ptr++) Line 4440  for (;; ptr++)
4440              *errorcodeptr = ERR29;              *errorcodeptr = ERR29;
4441              goto FAILED;              goto FAILED;
4442              }              }
4443    
4444            if (sign == '-')            if (refsign == '-')
4445              {              {
4446              if (recno == 0)              if (recno == 0)
4447                {                {
4448                *errorcodeptr = ERR58;                *errorcodeptr = ERR58;
4449                goto FAILED;                goto FAILED;
4450                }                }
4451              recno = cd->bracount - recno + 1;              recno = cd->bracount - recno + 1;
4452              if (recno <= 0)              if (recno <= 0)
4453                {                {
4454                *errorcodeptr = ERR15;                *errorcodeptr = ERR15;
4455                goto FAILED;                goto FAILED;
4456                }                }
4457              }              }
4458            else if (sign == '+')            else if (refsign == '+')
4459              {              {
4460              if (recno == 0)              if (recno == 0)
4461                {                {
4462                *errorcodeptr = ERR58;                *errorcodeptr = ERR58;
4463                goto FAILED;                goto FAILED;
4464                }                }
4465              recno += cd->bracount;              recno += cd->bracount;
4466              }              }
4467    
4468            /* Come here from code above that handles a named recursion */            /* Come here from code above that handles a named recursion */
4469    
# Line 4121  for (;; ptr++) Line 4537  for (;; ptr++)
4537    
4538          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4539          default:              /* Other characters: check option setting */          default:              /* Other characters: check option setting */
4540          OTHER_CHAR_AFTER_QUERY:          OTHER_CHAR_AFTER_QUERY:
4541          set = unset = 0;          set = unset = 0;
4542          optset = &set;          optset = &set;
4543    
# Line 4256  for (;; ptr++) Line 4672  for (;; ptr++)
4672           errorcodeptr,                 /* Where to put an error message */           errorcodeptr,                 /* Where to put an error message */
4673           (bravalue == OP_ASSERTBACK ||           (bravalue == OP_ASSERTBACK ||
4674            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
4675             reset_bracount,               /* True if (?| group */
4676           skipbytes,                    /* Skip over bracket number */           skipbytes,                    /* Skip over bracket number */
4677           &subfirstbyte,                /* For possible first char */           &subfirstbyte,                /* For possible first char */
4678           &subreqbyte,                  /* For possible last char */           &subreqbyte,                  /* For possible last char */
# Line 4272  for (;; ptr++) Line 4689  for (;; ptr++)
4689      is on the bracket. */      is on the bracket. */
4690    
4691      /* If this is a conditional bracket, check that there are no more than      /* If this is a conditional bracket, check that there are no more than
4692      two branches in the group, or just one if it's a DEFINE group. */      two branches in the group, or just one if it's a DEFINE group. We do this
4693        in the real compile phase, not in the pre-pass, where the whole group may
4694        not be available. */
4695    
4696      if (bravalue == OP_COND)      if (bravalue == OP_COND && lengthptr == NULL)
4697        {        {
4698        uschar *tc = code;        uschar *tc = code;
4699        int condcount = 0;        int condcount = 0;
# Line 4328  for (;; ptr++) Line 4747  for (;; ptr++)
4747    
4748      if (lengthptr != NULL)      if (lengthptr != NULL)
4749        {        {
4750          if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE)
4751            {
4752            *errorcodeptr = ERR20;
4753            goto FAILED;
4754            }
4755        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
4756        code++;        code++;
4757        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
# Line 4434  for (;; ptr++) Line 4858  for (;; ptr++)
4858        zerofirstbyte = firstbyte;        zerofirstbyte = firstbyte;
4859        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
4860    
4861        /* \k<name> or \k'name' is a back reference by name (Perl syntax) */        /* \k<name> or \k'name' is a back reference by name (Perl syntax).
4862          We also support \k{name} (.NET syntax) */
4863    
4864        if (-c == ESC_k && (ptr[1] == '<' || ptr[1] == '\''))        if (-c == ESC_k && (ptr[1] == '<' || ptr[1] == '\'' || ptr[1] == '{'))
4865          {          {
4866          is_recurse = FALSE;          is_recurse = FALSE;
4867          terminator = (*(++ptr) == '<')? '>' : '\'';          terminator = (*(++ptr) == '<')? '>' : (*ptr == '\'')? '\'' : '}';
4868          goto NAMED_REF_OR_RECURSE;          goto NAMED_REF_OR_RECURSE;
4869          }          }
4870    
# Line 4605  This function is used during the pre-com Line 5030  This function is used during the pre-com
5030  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
5031  value of lengthptr distinguishes the two phases.  value of lengthptr distinguishes the two phases.
5032    
5033  Argument:  Arguments:
5034    options        option bits, including any changes for this subpattern    options        option bits, including any changes for this subpattern
5035    oldims         previous settings of ims option bits    oldims         previous settings of ims option bits
5036    codeptr        -> the address of the current code pointer    codeptr        -> the address of the current code pointer
5037    ptrptr         -> the address of the current pattern pointer    ptrptr         -> the address of the current pattern pointer
5038    errorcodeptr   -> pointer to error code variable    errorcodeptr   -> pointer to error code variable
5039    lookbehind     TRUE if this is a lookbehind assertion    lookbehind     TRUE if this is a lookbehind assertion
5040      reset_bracount TRUE to reset the count for each branch
5041    skipbytes      skip this many bytes at start (for brackets and OP_COND)    skipbytes      skip this many bytes at start (for brackets and OP_COND)
5042    firstbyteptr   place to put the first required character, or a negative number    firstbyteptr   place to put the first required character, or a negative number
5043    reqbyteptr     place to put the last required character, or a negative number    reqbyteptr     place to put the last required character, or a negative number
# Line 4625  Returns:         TRUE on success Line 5051  Returns:         TRUE on success
5051    
5052  static BOOL  static BOOL
5053  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,
5054    int *errorcodeptr, BOOL lookbehind, int skipbytes, int *firstbyteptr,    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
5055    int *reqbyteptr, branch_chain *bcptr, compile_data *cd, int *lengthptr)    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,
5056      int *lengthptr)
5057  {  {
5058  const uschar *ptr = *ptrptr;  const uschar *ptr = *ptrptr;
5059  uschar *code = *codeptr;  uschar *code = *codeptr;
# Line 4636  uschar *reverse_count = NULL; Line 5063  uschar *reverse_count = NULL;
5063  int firstbyte, reqbyte;  int firstbyte, reqbyte;
5064  int branchfirstbyte, branchreqbyte;  int branchfirstbyte, branchreqbyte;
5065  int length;  int length;
5066    int orig_bracount;
5067    int max_bracount;
5068  branch_chain bc;  branch_chain bc;
5069    
5070  bc.outer = bcptr;  bc.outer = bcptr;
# Line 4664  code += 1 + LINK_SIZE + skipbytes; Line 5093  code += 1 + LINK_SIZE + skipbytes;
5093    
5094  /* Loop for each alternative branch */  /* Loop for each alternative branch */
5095    
5096    orig_bracount = max_bracount = cd->bracount;
5097  for (;;)  for (;;)
5098    {    {
5099      /* For a (?| group, reset the capturing bracket count so that each branch
5100      uses the same numbers. */
5101    
5102      if (reset_bracount) cd->bracount = orig_bracount;
5103    
5104    /* Handle a change of ims options at the start of the branch */    /* Handle a change of ims options at the start of the branch */
5105    
5106    if ((options & PCRE_IMS) != oldims)    if ((options & PCRE_IMS) != oldims)
# Line 4695  for (;;) Line 5130  for (;;)
5130      return FALSE;      return FALSE;
5131      }      }
5132    
5133      /* Keep the highest bracket count in case (?| was used and some branch
5134      has fewer than the rest. */
5135    
5136      if (cd->bracount > max_bracount) max_bracount = cd->bracount;
5137    
5138    /* In the real compile phase, there is some post-processing to be done. */    /* In the real compile phase, there is some post-processing to be done. */
5139    
5140    if (lengthptr == NULL)    if (lengthptr == NULL)
# Line 4758  for (;;) Line 5198  for (;;)
5198        }        }
5199      }      }
5200    
5201    /* Reached end of expression, either ')' or end of pattern. Go back through    /* Reached end of expression, either ')' or end of pattern. In the real
5202    the alternative branches and reverse the chain of offsets, with the field in    compile phase, go back through the alternative branches and reverse the chain
5203    the BRA item now becoming an offset to the first alternative. If there are    of offsets, with the field in the BRA item now becoming an offset to the
5204    no alternatives, it points to the end of the group. The length in the    first alternative. If there are no alternatives, it points to the end of the
5205    terminating ket is always the length of the whole bracketed item. If any of    group. The length in the terminating ket is always the length of the whole
5206    the ims options were changed inside the group, compile a resetting op-code    bracketed item. If any of the ims options were changed inside the group,
5207    following, except at the very end of the pattern. Return leaving the pointer    compile a resetting op-code following, except at the very end of the pattern.
5208    at the terminating char. */    Return leaving the pointer at the terminating char. */
5209    
5210    if (*ptr != '|')    if (*ptr != '|')
5211      {      {
5212      int branch_length = code - last_branch;      if (lengthptr == NULL)
     do  
5213        {        {
5214        int prev_length = GET(last_branch, 1);        int branch_length = code - last_branch;
5215        PUT(last_branch, 1, branch_length);        do
5216        branch_length = prev_length;          {
5217        last_branch -= branch_length;          int prev_length = GET(last_branch, 1);
5218            PUT(last_branch, 1, branch_length);
5219            branch_length = prev_length;
5220            last_branch -= branch_length;
5221            }
5222          while (branch_length > 0);
5223        }        }
     while (branch_length > 0);  
5224    
5225      /* Fill in the ket */      /* Fill in the ket */
5226    
# Line 4794  for (;;) Line 5237  for (;;)
5237        length += 2;        length += 2;
5238        }        }
5239    
5240        /* Retain the highest bracket number, in case resetting was used. */
5241    
5242        cd->bracount = max_bracount;
5243    
5244      /* Set values to pass back */      /* Set values to pass back */
5245    
5246      *codeptr = code;      *codeptr = code;
5247      *ptrptr = ptr;      *ptrptr = ptr;
5248      *firstbyteptr = firstbyte;      *firstbyteptr = firstbyte;
5249      *reqbyteptr = reqbyte;      *reqbyteptr = reqbyte;
5250      if (lengthptr != NULL) *lengthptr += length;      if (lengthptr != NULL)
5251          {
5252          if (OFLOW_MAX - *lengthptr < length)
5253            {
5254            *errorcodeptr = ERR20;
5255            return FALSE;
5256            }
5257          *lengthptr += length;
5258          }
5259      return TRUE;      return TRUE;
5260      }      }
5261    
5262    /* Another branch follows; insert an "or" node. Its length field points back    /* Another branch follows. In the pre-compile phase, we can move the code
5263      pointer back to where it was for the start of the first branch. (That is,
5264      pretend that each branch is the only one.)
5265    
5266      In the real compile phase, insert an ALT node. Its length field points back
5267    to the previous branch while the bracket remains open. At the end the chain    to the previous branch while the bracket remains open. At the end the chain
5268    is reversed. It's done like this so that the start of the bracket has a    is reversed. It's done like this so that the start of the bracket has a
5269    zero offset until it is closed, making it possible to detect recursion. */    zero offset until it is closed, making it possible to detect recursion. */
5270    
5271    *code = OP_ALT;    if (lengthptr != NULL)
5272    PUT(code, 1, code - last_branch);      {
5273    bc.current = last_branch = code;      code = *codeptr + 1 + LINK_SIZE + skipbytes;
5274    code += 1 + LINK_SIZE;      length += 1 + LINK_SIZE;
5275        }
5276      else
5277        {
5278        *code = OP_ALT;
5279        PUT(code, 1, code - last_branch);
5280        bc.current = last_branch = code;
5281        code += 1 + LINK_SIZE;
5282        }
5283    
5284    ptr++;    ptr++;
   length += 1 + LINK_SIZE;  
5285    }    }
5286  /* Control never reaches here */  /* Control never reaches here */
5287  }  }
# Line 5256  outside can help speed up starting point Line 5723  outside can help speed up starting point
5723  code = cworkspace;  code = cworkspace;
5724  *code = OP_BRA;  *code = OP_BRA;
5725  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,
5726    &code, &ptr, &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, &length);    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,
5727      &length);
5728  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
5729    
5730  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
# Line 5315  cd->start_code = codestart; Line 5783  cd->start_code = codestart;
5783  cd->hwm = cworkspace;  cd->hwm = cworkspace;
5784  cd->req_varyopt = 0;  cd->req_varyopt = 0;
5785  cd->nopartial = FALSE;  cd->nopartial = FALSE;
5786    cd->had_accept = FALSE;
5787    
5788  /* Set up a starting, non-extracting bracket, then compile the expression. On  /* Set up a starting, non-extracting bracket, then compile the expression. On
5789  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 5324  ptr = (const uschar *)pattern; Line 5793  ptr = (const uschar *)pattern;
5793  code = (uschar *)codestart;  code = (uschar *)codestart;
5794  *code = OP_BRA;  *code = OP_BRA;
5795  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,
5796    &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);    &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);
5797  re->top_bracket = cd->bracount;  re->top_bracket = cd->bracount;
5798  re->top_backref = cd->top_backref;  re->top_backref = cd->top_backref;
5799    
5800  if (cd->nopartial) re->options |= PCRE_NOPARTIAL;  if (cd->nopartial) re->options |= PCRE_NOPARTIAL;
5801    if (cd->had_accept) reqbyte = -1;   /* Must disable after (*ACCEPT) */
5802    
5803  /* If not reached end of pattern on success, there's an excess bracket. */  /* If not reached end of pattern on success, there's an excess bracket. */
5804    

Legend:
Removed from v.166  
changed lines
  Added in v.213

  ViewVC Help
Powered by ViewVC 1.1.5