/[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 178 by ph10, Wed Jun 13 08:44:34 2007 UTC revision 215 by ph10, Wed Aug 15 14:20:05 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 62  used by pcretest. DEBUG is not defined w Line 65  used by pcretest. DEBUG is not defined w
65    
66  #define SETBIT(a,b) a[b/8] |= (1 << (b%8))  #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         *
# Line 120  static const short int escapes[] = { Line 130  static const short int escapes[] = {
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 */-ESC_H,     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,-ESC_V, -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,
# Line 130  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 203  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 239  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 248  static const char *error_texts[] = { Line 279  static const char *error_texts[] = {
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 a braced name or an optionally braced non-zero number",    "\\g is not followed by a braced name or an optionally braced non-zero number",
282    "(?+ or (?- or (?(+ 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 379  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, BOOL, int,    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,
418      int *, int *, branch_chain *, compile_data *, int *);      int *, int *, branch_chain *, compile_data *, int *);
419    
420    
# Line 405  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 489  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 501  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 530  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 625  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 701  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 904  for (; *ptr != 0; ptr++) Line 950  for (; *ptr != 0; ptr++)
950      {      {
951      while (*(++ptr) != ']')      while (*(++ptr) != ']')
952        {        {
953          if (*ptr == 0) return -1;
954        if (*ptr == '\\')        if (*ptr == '\\')
955          {          {
956          if (*(++ptr) == 0) return -1;          if (*(++ptr) == 0) return -1;
# Line 931  for (; *ptr != 0; ptr++) Line 978  for (; *ptr != 0; ptr++)
978    /* An opening parens must now be a real metacharacter */    /* An opening parens must now be a real metacharacter */
979    
980    if (*ptr != '(') continue;    if (*ptr != '(') continue;
981    if (ptr[1] != '?')    if (ptr[1] != '?' && ptr[1] != '*')
982      {      {
983      count++;      count++;
984      if (name == NULL && count == lorn) return count;      if (name == NULL && count == lorn) return count;
# Line 1399  for (code = first_significant_code(code Line 1446  for (code = first_significant_code(code
1446    
1447    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
1448    
1449    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE)    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)
1450      {      {
1451      BOOL empty_branch;      BOOL empty_branch;
1452      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 1949  if (next >= 0) switch(op_code) Line 1996  if (next >= 0) switch(op_code)
1996    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
1997    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;
1998    
1999      case OP_HSPACE:
2000      case OP_NOT_HSPACE:
2001      switch(next)
2002        {
2003        case 0x09:
2004        case 0x20:
2005        case 0xa0:
2006        case 0x1680:
2007        case 0x180e:
2008        case 0x2000:
2009        case 0x2001:
2010        case 0x2002:
2011        case 0x2003:
2012        case 0x2004:
2013        case 0x2005:
2014        case 0x2006:
2015        case 0x2007:
2016        case 0x2008:
2017        case 0x2009:
2018        case 0x200A:
2019        case 0x202f:
2020        case 0x205f:
2021        case 0x3000:
2022        return op_code != OP_HSPACE;
2023        default:
2024        return op_code == OP_HSPACE;
2025        }
2026    
2027      case OP_VSPACE:
2028      case OP_NOT_VSPACE:
2029      switch(next)
2030        {
2031        case 0x0a:
2032        case 0x0b:
2033        case 0x0c:
2034        case 0x0d:
2035        case 0x85:
2036        case 0x2028:
2037        case 0x2029:
2038        return op_code != OP_VSPACE;
2039        default:
2040        return op_code == OP_VSPACE;
2041        }
2042    
2043    default:    default:
2044    return FALSE;    return FALSE;
2045    }    }
# Line 1983  switch(op_code) Line 2074  switch(op_code)
2074      case ESC_W:      case ESC_W:
2075      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;
2076    
2077        case ESC_h:
2078        case ESC_H:
2079        switch(item)
2080          {
2081          case 0x09:
2082          case 0x20:
2083          case 0xa0:
2084          case 0x1680:
2085          case 0x180e:
2086          case 0x2000:
2087          case 0x2001:
2088          case 0x2002:
2089          case 0x2003:
2090          case 0x2004:
2091          case 0x2005:
2092          case 0x2006:
2093          case 0x2007:
2094          case 0x2008:
2095          case 0x2009:
2096          case 0x200A:
2097          case 0x202f:
2098          case 0x205f:
2099          case 0x3000:
2100          return -next != ESC_h;
2101          default:
2102          return -next == ESC_h;
2103          }
2104    
2105        case ESC_v:
2106        case ESC_V:
2107        switch(item)
2108          {
2109          case 0x0a:
2110          case 0x0b:
2111          case 0x0c:
2112          case 0x0d:
2113          case 0x85:
2114          case 0x2028:
2115          case 0x2029:
2116          return -next != ESC_v;
2117          default:
2118          return -next == ESC_v;
2119          }
2120    
2121      default:      default:
2122      return FALSE;      return FALSE;
2123      }      }
2124    
2125    case OP_DIGIT:    case OP_DIGIT:
2126    return next == -ESC_D || next == -ESC_s || next == -ESC_W;    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||
2127             next == -ESC_h || next == -ESC_v;
2128    
2129    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
2130    return next == -ESC_d;    return next == -ESC_d;
# Line 1997  switch(op_code) Line 2133  switch(op_code)
2133    return next == -ESC_S || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_d || next == -ESC_w;
2134    
2135    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
2136    return next == -ESC_s;    return next == -ESC_s || next == -ESC_h || next == -ESC_v;
2137    
2138      case OP_HSPACE:
2139      return next == -ESC_S || next == -ESC_H || next == -ESC_d || next == -ESC_w;
2140    
2141      case OP_NOT_HSPACE:
2142      return next == -ESC_h;
2143    
2144      /* Can't have \S in here because VT matches \S (Perl anomaly) */
2145      case OP_VSPACE:
2146      return next == -ESC_V || next == -ESC_d || next == -ESC_w;
2147    
2148      case OP_NOT_VSPACE:
2149      return next == -ESC_v;
2150    
2151    case OP_WORDCHAR:    case OP_WORDCHAR:
2152    return next == -ESC_W || next == -ESC_s;    return next == -ESC_W || next == -ESC_s || next == -ESC_h || next == -ESC_v;
2153    
2154    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
2155    return next == -ESC_w || next == -ESC_d;    return next == -ESC_w || next == -ESC_d;
# Line 2115  for (;; ptr++) Line 2264  for (;; ptr++)
2264    BOOL possessive_quantifier;    BOOL possessive_quantifier;
2265    BOOL is_quantifier;    BOOL is_quantifier;
2266    BOOL is_recurse;    BOOL is_recurse;
2267    BOOL reset_bracount;    BOOL reset_bracount;
2268    int class_charcount;    int class_charcount;
2269    int class_lastchar;    int class_lastchar;
2270    int newoptions;    int newoptions;
# Line 2153  for (;; ptr++) Line 2302  for (;; ptr++)
2302      */      */
2303    
2304      if (code < last_code) code = last_code;      if (code < last_code) code = last_code;
2305    
2306        /* Paranoid check for integer overflow */
2307    
2308        if (OFLOW_MAX - *lengthptr < code - last_code)
2309          {
2310          *errorcodeptr = ERR20;
2311          goto FAILED;
2312          }
2313    
2314      *lengthptr += code - last_code;      *lengthptr += code - last_code;
2315      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));
2316    
# Line 2265  for (;; ptr++) Line 2423  for (;; ptr++)
2423      *ptrptr = ptr;      *ptrptr = ptr;
2424      if (lengthptr != NULL)      if (lengthptr != NULL)
2425        {        {
2426          if (OFLOW_MAX - *lengthptr < code - last_code)
2427            {
2428            *errorcodeptr = ERR20;
2429            goto FAILED;
2430            }
2431        *lengthptr += code - last_code;   /* To include callout length */        *lengthptr += code - last_code;   /* To include callout length */
2432        DPRINTF((">> end branch\n"));        DPRINTF((">> end branch\n"));
2433        }        }
# Line 2327  for (;; ptr++) Line 2490  for (;; ptr++)
2490        goto FAILED;        goto FAILED;
2491        }        }
2492    
2493      /* 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,
2494        if the first few characters (either before or after ^) are \Q\E or \E we
2495        skip them too. This makes for compatibility with Perl. */
2496    
2497      if ((c = *(++ptr)) == '^')      negate_class = FALSE;
2498        for (;;)
2499        {        {
       negate_class = TRUE;  
2500        c = *(++ptr);        c = *(++ptr);
2501        }        if (c == '\\')
2502      else          {
2503        {          if (ptr[1] == 'E') ptr++;
2504        negate_class = FALSE;            else if (strncmp((const char *)ptr+1, "Q\\E", 3) == 0) ptr += 3;
2505                else break;
2506            }
2507          else if (!negate_class && c == '^')
2508            negate_class = TRUE;
2509          else break;
2510        }        }
2511    
2512      /* 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 2477  for (;; ptr++) Line 2647  for (;; ptr++)
2647        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
2648        case. Inside a class (and only there) it is treated as backspace.        case. Inside a class (and only there) it is treated as backspace.
2649        Elsewhere it marks a word boundary. Other escapes have preset maps ready        Elsewhere it marks a word boundary. Other escapes have preset maps ready
2650        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
2651        character in them, so set class_charcount bigger than one. */        character in them, so set class_charcount bigger than one. */
2652    
2653        if (c == '\\')        if (c == '\\')
# Line 2535  for (;; ptr++) Line 2705  for (;; ptr++)
2705    
2706              case ESC_E: /* Perl ignores an orphan \E */              case ESC_E: /* Perl ignores an orphan \E */
2707              continue;              continue;
2708    
2709              default:    /* Not recognized; fall through */              default:    /* Not recognized; fall through */
2710              break;      /* Need "default" setting to stop compiler warning. */              break;      /* Need "default" setting to stop compiler warning. */
2711              }              }
# Line 2544  for (;; ptr++) Line 2714  for (;; ptr++)
2714    
2715            else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||            else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||
2716                     c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;                     c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;
2717    
2718            /* We need to deal with \H, \h, \V, and \v in both phases because            /* We need to deal with \H, \h, \V, and \v in both phases because
2719            they use extra memory. */            they use extra memory. */
2720    
2721            if (-c == ESC_h)            if (-c == ESC_h)
2722              {              {
2723              SETBIT(classbits, 0x09); /* VT */              SETBIT(classbits, 0x09); /* VT */
2724              SETBIT(classbits, 0x20); /* SPACE */              SETBIT(classbits, 0x20); /* SPACE */
2725              SETBIT(classbits, 0xa0); /* NSBP */              SETBIT(classbits, 0xa0); /* NSBP */
2726  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2727              if (utf8)              if (utf8)
2728                {                {
2729                class_utf8 = TRUE;                class_utf8 = TRUE;
2730                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
2731                class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);
2732                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
2733                class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);
2734                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2735                class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);
2736                class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);
2737                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
2738                class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);
2739                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
2740                class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);
2741                *class_utf8data++ = XCL_SINGLE;                *class_utf8data++ = XCL_SINGLE;
2742                class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);
2743                }                }
2744  #endif  #endif
2745              continue;              continue;
2746              }              }
2747    
2748            if (-c == ESC_H)            if (-c == ESC_H)
2749              {              {
# Line 2581  for (;; ptr++) Line 2751  for (;; ptr++)
2751                {                {
2752                int x = 0xff;                int x = 0xff;
2753                switch (c)                switch (c)
2754                  {                  {
2755                  case 0x09/8: x ^= 1 << (0x09%8); break;                  case 0x09/8: x ^= 1 << (0x09%8); break;
2756                  case 0x20/8: x ^= 1 << (0x20%8); break;                  case 0x20/8: x ^= 1 << (0x20%8); break;
2757                  case 0xa0/8: x ^= 1 << (0xa0%8); break;                  case 0xa0/8: x ^= 1 << (0xa0%8); break;
2758                  default: break;                  default: break;
2759                  }                  }
2760                classbits[c] |= x;                classbits[c] |= x;
2761                }                }
2762    
2763  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2764              if (utf8)              if (utf8)
2765                {                {
2766                class_utf8 = TRUE;                class_utf8 = TRUE;
2767                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2768                class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);
2769                class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);
2770                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2771                class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);
2772                class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);
2773                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2774                class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);
2775                class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);
2776                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2777                class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);
2778                class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);
2779                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2780                class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);
2781                class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);
2782                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2783                class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);
2784                class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);
2785                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2786                class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);
2787                class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);
2788                }                }
2789  #endif  #endif
2790              continue;              continue;
2791              }              }
2792    
2793            if (-c == ESC_v)            if (-c == ESC_v)
2794              {              {
2795              SETBIT(classbits, 0x0a); /* LF */              SETBIT(classbits, 0x0a); /* LF */
2796              SETBIT(classbits, 0x0b); /* VT */              SETBIT(classbits, 0x0b); /* VT */
2797              SETBIT(classbits, 0x0c); /* FF */              SETBIT(classbits, 0x0c); /* FF */
2798              SETBIT(classbits, 0x0d); /* CR */              SETBIT(classbits, 0x0d); /* CR */
2799              SETBIT(classbits, 0x85); /* NEL */              SETBIT(classbits, 0x85); /* NEL */
2800  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2801              if (utf8)              if (utf8)
2802                {                {
2803                class_utf8 = TRUE;                class_utf8 = TRUE;
2804                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2805                class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);
2806                class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);
2807                }                }
2808  #endif  #endif
2809              continue;              continue;
2810              }              }
2811    
2812            if (-c == ESC_V)            if (-c == ESC_V)
2813              {              {
# Line 2645  for (;; ptr++) Line 2815  for (;; ptr++)
2815                {                {
2816                int x = 0xff;                int x = 0xff;
2817                switch (c)                switch (c)
2818                  {                  {
2819                  case 0x0a/8: x ^= 1 << (0x0a%8);                  case 0x0a/8: x ^= 1 << (0x0a%8);
2820                               x ^= 1 << (0x0b%8);                               x ^= 1 << (0x0b%8);
2821                               x ^= 1 << (0x0c%8);                               x ^= 1 << (0x0c%8);
2822                               x ^= 1 << (0x0d%8);                               x ^= 1 << (0x0d%8);
2823                               break;                               break;
2824                  case 0x85/8: x ^= 1 << (0x85%8); break;                  case 0x85/8: x ^= 1 << (0x85%8); break;
2825                  default: break;                  default: break;
2826                  }                  }
2827                classbits[c] |= x;                classbits[c] |= x;
2828                }                }
2829    
2830  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2831              if (utf8)              if (utf8)
2832                {                {
2833                class_utf8 = TRUE;                class_utf8 = TRUE;
2834                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2835                class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);
2836                class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);
2837                *class_utf8data++ = XCL_RANGE;                *class_utf8data++ = XCL_RANGE;
2838                class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);
2839                class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);                class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);
2840                }                }
2841  #endif  #endif
2842              continue;              continue;
2843              }              }
2844    
2845            /* We need to deal with \P and \p in both phases. */            /* We need to deal with \P and \p in both phases. */
2846    
# Line 2812  for (;; ptr++) Line 2982  for (;; ptr++)
2982              unsigned int origd = d;              unsigned int origd = d;
2983              while (get_othercase_range(&cc, origd, &occ, &ocd))              while (get_othercase_range(&cc, origd, &occ, &ocd))
2984                {                {
2985                if (occ >= (unsigned int)c &&                if (occ >= (unsigned int)c &&
2986                    ocd <= (unsigned int)d)                    ocd <= (unsigned int)d)
2987                  continue;                          /* Skip embedded ranges */                  continue;                          /* Skip embedded ranges */
2988    
2989                if (occ < (unsigned int)c  &&                if (occ < (unsigned int)c  &&
2990                    ocd >= (unsigned int)c - 1)      /* Extend the basic range */                    ocd >= (unsigned int)c - 1)      /* Extend the basic range */
2991                  {                                  /* if there is overlap,   */                  {                                  /* if there is overlap,   */
2992                  c = occ;                           /* noting that if occ < c */                  c = occ;                           /* noting that if occ < c */
2993                  continue;                          /* we can't have ocd > d  */                  continue;                          /* we can't have ocd > d  */
2994                  }                                  /* because a subrange is  */                  }                                  /* because a subrange is  */
2995                if (ocd > (unsigned int)d &&                if (ocd > (unsigned int)d &&
2996                    occ <= (unsigned int)d + 1)      /* always shorter than    */                    occ <= (unsigned int)d + 1)      /* always shorter than    */
2997                  {                                  /* the basic range.       */                  {                                  /* the basic range.       */
2998                  d = ocd;                  d = ocd;
# Line 3419  for (;; ptr++) Line 3589  for (;; ptr++)
3589          goto FAILED;          goto FAILED;
3590          }          }
3591    
       /* This is a paranoid check to stop integer overflow later on */  
   
       if (len > MAX_DUPLENGTH)  
         {  
         *errorcodeptr = ERR50;  
         goto FAILED;  
         }  
   
3592        /* 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
3593        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
3594        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 3515  for (;; ptr++) Line 3677  for (;; ptr++)
3677          if (repeat_min > 1)          if (repeat_min > 1)
3678            {            {
3679            /* 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
3680            just adjust the length as if we had. */            just adjust the length as if we had. Do some paranoid checks for
3681              potential integer overflow. */
3682    
3683            if (lengthptr != NULL)            if (lengthptr != NULL)
3684              *lengthptr += (repeat_min - 1)*length_prevgroup;              {
3685                int delta = (repeat_min - 1)*length_prevgroup;
3686                if ((double)(repeat_min - 1)*(double)length_prevgroup >
3687                                                                (double)INT_MAX ||
3688                    OFLOW_MAX - *lengthptr < delta)
3689                  {
3690                  *errorcodeptr = ERR20;
3691                  goto FAILED;
3692                  }
3693                *lengthptr += delta;
3694                }
3695    
3696            /* This is compiling for real */            /* This is compiling for real */
3697    
# Line 3556  for (;; ptr++) Line 3729  for (;; ptr++)
3729          /* 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
3730          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
3731          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
3732          add 2 + 2*LINKSIZE to allow for the nesting that occurs. */          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
3733            paranoid checks to avoid integer overflow. */
3734    
3735          if (lengthptr != NULL && repeat_max > 0)          if (lengthptr != NULL && repeat_max > 0)
3736            *lengthptr += repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -            {
3737              2 - 2*LINK_SIZE;  /* Last one doesn't nest */            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
3738                          2 - 2*LINK_SIZE;   /* Last one doesn't nest */
3739              if ((double)repeat_max *
3740                    (double)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
3741                      > (double)INT_MAX ||
3742                  OFLOW_MAX - *lengthptr < delta)
3743                {
3744                *errorcodeptr = ERR20;
3745                goto FAILED;
3746                }
3747              *lengthptr += delta;
3748              }
3749    
3750          /* This is compiling for real */          /* This is compiling for real */
3751    
# Line 3712  for (;; ptr++) Line 3897  for (;; ptr++)
3897      /* ===================================================================*/      /* ===================================================================*/
3898      /* Start of nested parenthesized sub-expression, or comment or lookahead or      /* Start of nested parenthesized sub-expression, or comment or lookahead or
3899      lookbehind or option setting or condition or all the other extended      lookbehind or option setting or condition or all the other extended
3900      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. */  
3901    
3902      case '(':      case '(':
3903      newoptions = options;      newoptions = options;
3904      skipbytes = 0;      skipbytes = 0;
3905      bravalue = OP_CBRA;      bravalue = OP_CBRA;
3906      save_hwm = cd->hwm;      save_hwm = cd->hwm;
3907      reset_bracount = FALSE;      reset_bracount = FALSE;
3908    
3909        /* First deal with various "verbs" that can be introduced by '*'. */
3910    
3911      if (*(++ptr) == '?')      if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)
3912          {
3913          int i, namelen;
3914          const uschar *name = ++ptr;
3915          previous = NULL;
3916          while ((cd->ctypes[*++ptr] & ctype_letter) != 0);
3917          if (*ptr == ':')
3918            {
3919            *errorcodeptr = ERR59;   /* Not supported */
3920            goto FAILED;
3921            }
3922          if (*ptr != ')')
3923            {
3924            *errorcodeptr = ERR60;
3925            goto FAILED;
3926            }
3927          namelen = ptr - name;
3928          for (i = 0; i < verbcount; i++)
3929            {
3930            if (namelen == verbs[i].len &&
3931                strncmp((char *)name, verbs[i].name, namelen) == 0)
3932              {
3933              *code = verbs[i].op;
3934              if (*code++ == OP_ACCEPT) cd->had_accept = TRUE;
3935              break;
3936              }
3937            }
3938          if (i < verbcount) continue;
3939          *errorcodeptr = ERR60;
3940          goto FAILED;
3941          }
3942    
3943        /* Deal with the extended parentheses; all are introduced by '?', and the
3944        appearance of any of them means that this is not a capturing group. */
3945    
3946        else if (*ptr == '?')
3947        {        {
3948        int i, set, unset, namelen;        int i, set, unset, namelen;
3949        int *optset;        int *optset;
# Line 3746  for (;; ptr++) Line 3966  for (;; ptr++)
3966          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
3967          case '|':                 /* Reset capture count for each branch */          case '|':                 /* Reset capture count for each branch */
3968          reset_bracount = TRUE;          reset_bracount = TRUE;
3969          /* Fall through */          /* Fall through */
3970    
3971          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
3972          case ':':                 /* Non-capturing bracket */          case ':':                 /* Non-capturing bracket */
# Line 3965  for (;; ptr++) Line 4185  for (;; ptr++)
4185    
4186          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4187          case '!':                 /* Negative lookahead */          case '!':                 /* Negative lookahead */
         bravalue = OP_ASSERT_NOT;  
4188          ptr++;          ptr++;
4189            if (*ptr == ')')          /* Optimize (?!) */
4190              {
4191              *code++ = OP_FAIL;
4192              previous = NULL;
4193              continue;
4194              }
4195            bravalue = OP_ASSERT_NOT;
4196          break;          break;
4197    
4198    
# Line 4447  for (;; ptr++) Line 4673  for (;; ptr++)
4673           errorcodeptr,                 /* Where to put an error message */           errorcodeptr,                 /* Where to put an error message */
4674           (bravalue == OP_ASSERTBACK ||           (bravalue == OP_ASSERTBACK ||
4675            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
4676           reset_bracount,               /* True if (?| group */           reset_bracount,               /* True if (?| group */
4677           skipbytes,                    /* Skip over bracket number */           skipbytes,                    /* Skip over bracket number */
4678           &subfirstbyte,                /* For possible first char */           &subfirstbyte,                /* For possible first char */
4679           &subreqbyte,                  /* For possible last char */           &subreqbyte,                  /* For possible last char */
# Line 4522  for (;; ptr++) Line 4748  for (;; ptr++)
4748    
4749      if (lengthptr != NULL)      if (lengthptr != NULL)
4750        {        {
4751          if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE)
4752            {
4753            *errorcodeptr = ERR20;
4754            goto FAILED;
4755            }
4756        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
4757        code++;        code++;
4758        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
# Line 4807  Arguments: Line 5038  Arguments:
5038    ptrptr         -> the address of the current pattern pointer    ptrptr         -> the address of the current pattern pointer
5039    errorcodeptr   -> pointer to error code variable    errorcodeptr   -> pointer to error code variable
5040    lookbehind     TRUE if this is a lookbehind assertion    lookbehind     TRUE if this is a lookbehind assertion
5041    reset_bracount TRUE to reset the count for each branch    reset_bracount TRUE to reset the count for each branch
5042    skipbytes      skip this many bytes at start (for brackets and OP_COND)    skipbytes      skip this many bytes at start (for brackets and OP_COND)
5043    firstbyteptr   place to put the first required character, or a negative number    firstbyteptr   place to put the first required character, or a negative number
5044    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 4821  Returns:         TRUE on success Line 5052  Returns:         TRUE on success
5052    
5053  static BOOL  static BOOL
5054  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,
5055    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
5056    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,
5057    int *lengthptr)    int *lengthptr)
5058  {  {
5059  const uschar *ptr = *ptrptr;  const uschar *ptr = *ptrptr;
# Line 4867  orig_bracount = max_bracount = cd->braco Line 5098  orig_bracount = max_bracount = cd->braco
5098  for (;;)  for (;;)
5099    {    {
5100    /* For a (?| group, reset the capturing bracket count so that each branch    /* For a (?| group, reset the capturing bracket count so that each branch
5101    uses the same numbers. */    uses the same numbers. */
5102    
5103    if (reset_bracount) cd->bracount = orig_bracount;    if (reset_bracount) cd->bracount = orig_bracount;
5104    
5105    /* Handle a change of ims options at the start of the branch */    /* Handle a change of ims options at the start of the branch */
5106    
5107    if ((options & PCRE_IMS) != oldims)    if ((options & PCRE_IMS) != oldims)
# Line 4899  for (;;) Line 5130  for (;;)
5130      *ptrptr = ptr;      *ptrptr = ptr;
5131      return FALSE;      return FALSE;
5132      }      }
5133    
5134    /* Keep the highest bracket count in case (?| was used and some branch    /* Keep the highest bracket count in case (?| was used and some branch
5135    has fewer than the rest. */    has fewer than the rest. */
5136    
5137    if (cd->bracount > max_bracount) max_bracount = cd->bracount;    if (cd->bracount > max_bracount) max_bracount = cd->bracount;
5138    
5139    /* 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. */
# Line 5006  for (;;) Line 5237  for (;;)
5237        *code++ = oldims;        *code++ = oldims;
5238        length += 2;        length += 2;
5239        }        }
5240    
5241      /* Retain the highest bracket number, in case resetting was used. */      /* Retain the highest bracket number, in case resetting was used. */
5242    
5243      cd->bracount = max_bracount;      cd->bracount = max_bracount;
5244    
5245      /* Set values to pass back */      /* Set values to pass back */
# Line 5017  for (;;) Line 5248  for (;;)
5248      *ptrptr = ptr;      *ptrptr = ptr;
5249      *firstbyteptr = firstbyte;      *firstbyteptr = firstbyte;
5250      *reqbyteptr = reqbyte;      *reqbyteptr = reqbyte;
5251      if (lengthptr != NULL) *lengthptr += length;      if (lengthptr != NULL)
5252          {
5253          if (OFLOW_MAX - *lengthptr < length)
5254            {
5255            *errorcodeptr = ERR20;
5256            return FALSE;
5257            }
5258          *lengthptr += length;
5259          }
5260      return TRUE;      return TRUE;
5261      }      }
5262    
# Line 5485  outside can help speed up starting point Line 5724  outside can help speed up starting point
5724  code = cworkspace;  code = cworkspace;
5725  *code = OP_BRA;  *code = OP_BRA;
5726  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,
5727    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,
5728    &length);    &length);
5729  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
5730    
# Line 5545  cd->start_code = codestart; Line 5784  cd->start_code = codestart;
5784  cd->hwm = cworkspace;  cd->hwm = cworkspace;
5785  cd->req_varyopt = 0;  cd->req_varyopt = 0;
5786  cd->nopartial = FALSE;  cd->nopartial = FALSE;
5787    cd->had_accept = FALSE;
5788    
5789  /* Set up a starting, non-extracting bracket, then compile the expression. On  /* Set up a starting, non-extracting bracket, then compile the expression. On
5790  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 5559  re->top_bracket = cd->bracount; Line 5799  re->top_bracket = cd->bracount;
5799  re->top_backref = cd->top_backref;  re->top_backref = cd->top_backref;
5800    
5801  if (cd->nopartial) re->options |= PCRE_NOPARTIAL;  if (cd->nopartial) re->options |= PCRE_NOPARTIAL;
5802    if (cd->had_accept) reqbyte = -1;   /* Must disable after (*ACCEPT) */
5803    
5804  /* 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. */
5805    

Legend:
Removed from v.178  
changed lines
  Added in v.215

  ViewVC Help
Powered by ViewVC 1.1.5