/[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 221 by ph10, Fri Aug 17 09:25:08 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 474  else Line 525  else
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 888  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 915  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 1043  for (;;) Line 1106  for (;;)
1106    {    {
1107    int d;    int d;
1108    register int op = *cc;    register int op = *cc;
   
1109    switch (op)    switch (op)
1110      {      {
1111      case OP_CBRA:      case OP_CBRA:
# Line 1132  for (;;) Line 1194  for (;;)
1194    
1195      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1196      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1197        if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;
1198      cc += 4;      cc += 4;
1199      break;      break;
1200    
# Line 1240  for (;;) Line 1303  for (;;)
1303      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1304      }      }
1305    
1306    /* In UTF-8 mode, opcodes that are followed by a character may be followed by    /* Otherwise, we can get the item's length from the table, except that for
1307    a multi-byte character. The length in the table is a minimum, so we have to    repeated character types, we have to test for \p and \P, which have an extra
1308    arrange to skip the extra bytes. */    two bytes of parameters. */
1309    
1310    else    else
1311      {      {
1312        switch(c)
1313          {
1314          case OP_TYPESTAR:
1315          case OP_TYPEMINSTAR:
1316          case OP_TYPEPLUS:
1317          case OP_TYPEMINPLUS:
1318          case OP_TYPEQUERY:
1319          case OP_TYPEMINQUERY:
1320          case OP_TYPEPOSSTAR:
1321          case OP_TYPEPOSPLUS:
1322          case OP_TYPEPOSQUERY:
1323          if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
1324          break;
1325    
1326          case OP_TYPEUPTO:
1327          case OP_TYPEMINUPTO:
1328          case OP_TYPEEXACT:
1329          case OP_TYPEPOSUPTO:
1330          if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1331          break;
1332          }
1333    
1334        /* Add in the fixed length from the table */
1335    
1336      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1337    
1338      /* In UTF-8 mode, opcodes that are followed by a character may be followed by
1339      a multi-byte character. The length in the table is a minimum, so we have to
1340      arrange to skip the extra bytes. */
1341    
1342  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1343      if (utf8) switch(c)      if (utf8) switch(c)
1344        {        {
# Line 1304  for (;;) Line 1396  for (;;)
1396    
1397    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
1398    
1399    /* Otherwise, we get the item's length from the table. In UTF-8 mode, opcodes    /* Otherwise, we can get the item's length from the table, except that for
1400    that are followed by a character may be followed by a multi-byte character.    repeated character types, we have to test for \p and \P, which have an extra
1401    The length in the table is a minimum, so we have to arrange to skip the extra    two bytes of parameters. */
   bytes. */  
1402    
1403    else    else
1404      {      {
1405        switch(c)
1406          {
1407          case OP_TYPESTAR:
1408          case OP_TYPEMINSTAR:
1409          case OP_TYPEPLUS:
1410          case OP_TYPEMINPLUS:
1411          case OP_TYPEQUERY:
1412          case OP_TYPEMINQUERY:
1413          case OP_TYPEPOSSTAR:
1414          case OP_TYPEPOSPLUS:
1415          case OP_TYPEPOSQUERY:
1416          if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
1417          break;
1418    
1419          case OP_TYPEPOSUPTO:
1420          case OP_TYPEUPTO:
1421          case OP_TYPEMINUPTO:
1422          case OP_TYPEEXACT:
1423          if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1424          break;
1425          }
1426    
1427        /* Add in the fixed length from the table */
1428    
1429      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1430    
1431        /* In UTF-8 mode, opcodes that are followed by a character may be followed
1432        by a multi-byte character. The length in the table is a minimum, so we have
1433        to arrange to skip the extra bytes. */
1434    
1435  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1436      if (utf8) switch(c)      if (utf8) switch(c)
1437        {        {
# Line 1371  for (code = first_significant_code(code Line 1491  for (code = first_significant_code(code
1491    
1492    c = *code;    c = *code;
1493    
1494    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE)    /* Groups with zero repeats can of course be empty; skip them. */
1495    
1496      if (c == OP_BRAZERO || c == OP_BRAMINZERO)
1497        {
1498        code += _pcre_OP_lengths[c];
1499        do code += GET(code, 1); while (*code == OP_ALT);
1500        c = *code;
1501        continue;
1502        }
1503    
1504      /* For other groups, scan the branches. */
1505    
1506      if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)
1507      {      {
1508      BOOL empty_branch;      BOOL empty_branch;
1509      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 1519  for (code = first_significant_code(code
1519        }        }
1520      while (*code == OP_ALT);      while (*code == OP_ALT);
1521      if (!empty_branch) return FALSE;   /* All branches are non-empty */      if (!empty_branch) return FALSE;   /* All branches are non-empty */
1522        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];  
1523      continue;      continue;
1524      }      }
1525    
# Line 1400  for (code = first_significant_code(code Line 1527  for (code = first_significant_code(code
1527    
1528    switch (c)    switch (c)
1529      {      {
1530      /* Check for quantifiers after a class */      /* Check for quantifiers after a class. XCLASS is used for classes that
1531        cannot be represented just by a bit map. This includes negated single
1532        high-valued characters. The length in _pcre_OP_lengths[] is zero; the
1533        actual length is stored in the compiled code, so we must update "code"
1534        here. */
1535    
1536  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1537      case OP_XCLASS:      case OP_XCLASS:
1538      ccode = code + GET(code, 1);      ccode = code += GET(code, 1);
1539      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
1540  #endif  #endif
1541    
# Line 1926  if (next >= 0) switch(op_code) Line 2057  if (next >= 0) switch(op_code)
2057    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
2058    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;
2059    
2060      case OP_HSPACE:
2061      case OP_NOT_HSPACE:
2062      switch(next)
2063        {
2064        case 0x09:
2065        case 0x20:
2066        case 0xa0:
2067        case 0x1680:
2068        case 0x180e:
2069        case 0x2000:
2070        case 0x2001:
2071        case 0x2002:
2072        case 0x2003:
2073        case 0x2004:
2074        case 0x2005:
2075        case 0x2006:
2076        case 0x2007:
2077        case 0x2008:
2078        case 0x2009:
2079        case 0x200A:
2080        case 0x202f:
2081        case 0x205f:
2082        case 0x3000:
2083        return op_code != OP_HSPACE;
2084        default:
2085        return op_code == OP_HSPACE;
2086        }
2087    
2088      case OP_VSPACE:
2089      case OP_NOT_VSPACE:
2090      switch(next)
2091        {
2092        case 0x0a:
2093        case 0x0b:
2094        case 0x0c:
2095        case 0x0d:
2096        case 0x85:
2097        case 0x2028:
2098        case 0x2029:
2099        return op_code != OP_VSPACE;
2100        default:
2101        return op_code == OP_VSPACE;
2102        }
2103    
2104    default:    default:
2105    return FALSE;    return FALSE;
2106    }    }
# Line 1960  switch(op_code) Line 2135  switch(op_code)
2135      case ESC_W:      case ESC_W:
2136      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;
2137    
2138        case ESC_h:
2139        case ESC_H:
2140        switch(item)
2141          {
2142          case 0x09:
2143          case 0x20:
2144          case 0xa0:
2145          case 0x1680:
2146          case 0x180e:
2147          case 0x2000:
2148          case 0x2001:
2149          case 0x2002:
2150          case 0x2003:
2151          case 0x2004:
2152          case 0x2005:
2153          case 0x2006:
2154          case 0x2007:
2155          case 0x2008:
2156          case 0x2009:
2157          case 0x200A:
2158          case 0x202f:
2159          case 0x205f:
2160          case 0x3000:
2161          return -next != ESC_h;
2162          default:
2163          return -next == ESC_h;
2164          }
2165    
2166        case ESC_v:
2167        case ESC_V:
2168        switch(item)
2169          {
2170          case 0x0a:
2171          case 0x0b:
2172          case 0x0c:
2173          case 0x0d:
2174          case 0x85:
2175          case 0x2028:
2176          case 0x2029:
2177          return -next != ESC_v;
2178          default:
2179          return -next == ESC_v;
2180          }
2181    
2182      default:      default:
2183      return FALSE;      return FALSE;
2184      }      }
2185    
2186    case OP_DIGIT:    case OP_DIGIT:
2187    return next == -ESC_D || next == -ESC_s || next == -ESC_W;    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||
2188             next == -ESC_h || next == -ESC_v;
2189    
2190    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
2191    return next == -ESC_d;    return next == -ESC_d;
# Line 1974  switch(op_code) Line 2194  switch(op_code)
2194    return next == -ESC_S || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_d || next == -ESC_w;
2195    
2196    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
2197    return next == -ESC_s;    return next == -ESC_s || next == -ESC_h || next == -ESC_v;
2198    
2199      case OP_HSPACE:
2200      return next == -ESC_S || next == -ESC_H || next == -ESC_d || next == -ESC_w;
2201    
2202      case OP_NOT_HSPACE:
2203      return next == -ESC_h;
2204    
2205      /* Can't have \S in here because VT matches \S (Perl anomaly) */
2206      case OP_VSPACE:
2207      return next == -ESC_V || next == -ESC_d || next == -ESC_w;
2208    
2209      case OP_NOT_VSPACE:
2210      return next == -ESC_v;
2211    
2212    case OP_WORDCHAR:    case OP_WORDCHAR:
2213    return next == -ESC_W || next == -ESC_s;    return next == -ESC_W || next == -ESC_s || next == -ESC_h || next == -ESC_v;
2214    
2215    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
2216    return next == -ESC_w || next == -ESC_d;    return next == -ESC_w || next == -ESC_d;
# Line 2092  for (;; ptr++) Line 2325  for (;; ptr++)
2325    BOOL possessive_quantifier;    BOOL possessive_quantifier;
2326    BOOL is_quantifier;    BOOL is_quantifier;
2327    BOOL is_recurse;    BOOL is_recurse;
2328      BOOL reset_bracount;
2329    int class_charcount;    int class_charcount;
2330    int class_lastchar;    int class_lastchar;
2331    int newoptions;    int newoptions;
2332    int recno;    int recno;
2333      int refsign;
2334    int skipbytes;    int skipbytes;
2335    int subreqbyte;    int subreqbyte;
2336    int subfirstbyte;    int subfirstbyte;
# Line 2128  for (;; ptr++) Line 2363  for (;; ptr++)
2363      */      */
2364    
2365      if (code < last_code) code = last_code;      if (code < last_code) code = last_code;
2366    
2367        /* Paranoid check for integer overflow */
2368    
2369        if (OFLOW_MAX - *lengthptr < code - last_code)
2370          {
2371          *errorcodeptr = ERR20;
2372          goto FAILED;
2373          }
2374    
2375      *lengthptr += code - last_code;      *lengthptr += code - last_code;
2376      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));
2377    
# Line 2240  for (;; ptr++) Line 2484  for (;; ptr++)
2484      *ptrptr = ptr;      *ptrptr = ptr;
2485      if (lengthptr != NULL)      if (lengthptr != NULL)
2486        {        {
2487          if (OFLOW_MAX - *lengthptr < code - last_code)
2488            {
2489            *errorcodeptr = ERR20;
2490            goto FAILED;
2491            }
2492        *lengthptr += code - last_code;   /* To include callout length */        *lengthptr += code - last_code;   /* To include callout length */
2493        DPRINTF((">> end branch\n"));        DPRINTF((">> end branch\n"));
2494        }        }
# Line 2302  for (;; ptr++) Line 2551  for (;; ptr++)
2551        goto FAILED;        goto FAILED;
2552        }        }
2553    
2554      /* 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,
2555        if the first few characters (either before or after ^) are \Q\E or \E we
2556        skip them too. This makes for compatibility with Perl. */
2557    
2558      if ((c = *(++ptr)) == '^')      negate_class = FALSE;
2559        for (;;)
2560        {        {
       negate_class = TRUE;  
2561        c = *(++ptr);        c = *(++ptr);
2562        }        if (c == '\\')
2563      else          {
2564        {          if (ptr[1] == 'E') ptr++;
2565        negate_class = FALSE;            else if (strncmp((const char *)ptr+1, "Q\\E", 3) == 0) ptr += 3;
2566                else break;
2567            }
2568          else if (!negate_class && c == '^')
2569            negate_class = TRUE;
2570          else break;
2571        }        }
2572    
2573      /* 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 2708  for (;; ptr++)
2708        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
2709        case. Inside a class (and only there) it is treated as backspace.        case. Inside a class (and only there) it is treated as backspace.
2710        Elsewhere it marks a word boundary. Other escapes have preset maps ready        Elsewhere it marks a word boundary. Other escapes have preset maps ready
2711        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
2712        character in them, so set class_charcount bigger than one. */        character in them, so set class_charcount bigger than one. */
2713    
2714        if (c == '\\')        if (c == '\\')
# Line 2472  for (;; ptr++) Line 2728  for (;; ptr++)
2728            else inescq = TRUE;            else inescq = TRUE;
2729            continue;            continue;
2730            }            }
2731            else if (-c == ESC_E) continue;  /* Ignore orphan \E */
2732    
2733          if (c < 0)          if (c < 0)
2734            {            {
# Line 2520  for (;; ptr++) Line 2777  for (;; ptr++)
2777            else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||            else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||
2778                     c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;                     c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;
2779    
2780              /* We need to deal with \H, \h, \V, and \v in both phases because
2781              they use extra memory. */
2782    
2783              if (-c == ESC_h)
2784                {
2785                SETBIT(classbits, 0x09); /* VT */
2786                SETBIT(classbits, 0x20); /* SPACE */
2787                SETBIT(classbits, 0xa0); /* NSBP */
2788    #ifdef SUPPORT_UTF8
2789                if (utf8)
2790                  {
2791                  class_utf8 = TRUE;
2792                  *class_utf8data++ = XCL_SINGLE;
2793                  class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);
2794                  *class_utf8data++ = XCL_SINGLE;
2795                  class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);
2796                  *class_utf8data++ = XCL_RANGE;
2797                  class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);
2798                  class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);
2799                  *class_utf8data++ = XCL_SINGLE;
2800                  class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);
2801                  *class_utf8data++ = XCL_SINGLE;
2802                  class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);
2803                  *class_utf8data++ = XCL_SINGLE;
2804                  class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);
2805                  }
2806    #endif
2807                continue;
2808                }
2809    
2810              if (-c == ESC_H)
2811                {
2812                for (c = 0; c < 32; c++)
2813                  {
2814                  int x = 0xff;
2815                  switch (c)
2816                    {
2817                    case 0x09/8: x ^= 1 << (0x09%8); break;
2818                    case 0x20/8: x ^= 1 << (0x20%8); break;
2819                    case 0xa0/8: x ^= 1 << (0xa0%8); break;
2820                    default: break;
2821                    }
2822                  classbits[c] |= x;
2823                  }
2824    
2825    #ifdef SUPPORT_UTF8
2826                if (utf8)
2827                  {
2828                  class_utf8 = TRUE;
2829                  *class_utf8data++ = XCL_RANGE;
2830                  class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);
2831                  class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);
2832                  *class_utf8data++ = XCL_RANGE;
2833                  class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);
2834                  class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);
2835                  *class_utf8data++ = XCL_RANGE;
2836                  class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);
2837                  class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);
2838                  *class_utf8data++ = XCL_RANGE;
2839                  class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);
2840                  class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);
2841                  *class_utf8data++ = XCL_RANGE;
2842                  class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);
2843                  class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);
2844                  *class_utf8data++ = XCL_RANGE;
2845                  class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);
2846                  class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);
2847                  *class_utf8data++ = XCL_RANGE;
2848                  class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);
2849                  class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);
2850                  }
2851    #endif
2852                continue;
2853                }
2854    
2855              if (-c == ESC_v)
2856                {
2857                SETBIT(classbits, 0x0a); /* LF */
2858                SETBIT(classbits, 0x0b); /* VT */
2859                SETBIT(classbits, 0x0c); /* FF */
2860                SETBIT(classbits, 0x0d); /* CR */
2861                SETBIT(classbits, 0x85); /* NEL */
2862    #ifdef SUPPORT_UTF8
2863                if (utf8)
2864                  {
2865                  class_utf8 = TRUE;
2866                  *class_utf8data++ = XCL_RANGE;
2867                  class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);
2868                  class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);
2869                  }
2870    #endif
2871                continue;
2872                }
2873    
2874              if (-c == ESC_V)
2875                {
2876                for (c = 0; c < 32; c++)
2877                  {
2878                  int x = 0xff;
2879                  switch (c)
2880                    {
2881                    case 0x0a/8: x ^= 1 << (0x0a%8);
2882                                 x ^= 1 << (0x0b%8);
2883                                 x ^= 1 << (0x0c%8);
2884                                 x ^= 1 << (0x0d%8);
2885                                 break;
2886                    case 0x85/8: x ^= 1 << (0x85%8); break;
2887                    default: break;
2888                    }
2889                  classbits[c] |= x;
2890                  }
2891    
2892    #ifdef SUPPORT_UTF8
2893                if (utf8)
2894                  {
2895                  class_utf8 = TRUE;
2896                  *class_utf8data++ = XCL_RANGE;
2897                  class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);
2898                  class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);
2899                  *class_utf8data++ = XCL_RANGE;
2900                  class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);
2901                  class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);
2902                  }
2903    #endif
2904                continue;
2905                }
2906    
2907            /* We need to deal with \P and \p in both phases. */            /* We need to deal with \P and \p in both phases. */
2908    
2909  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 2660  for (;; ptr++) Line 3044  for (;; ptr++)
3044              unsigned int origd = d;              unsigned int origd = d;
3045              while (get_othercase_range(&cc, origd, &occ, &ocd))              while (get_othercase_range(&cc, origd, &occ, &ocd))
3046                {                {
3047                if (occ >= c && ocd <= d) continue;  /* Skip embedded ranges */                if (occ >= (unsigned int)c &&
3048                      ocd <= (unsigned int)d)
3049                    continue;                          /* Skip embedded ranges */
3050    
3051                if (occ < c  && ocd >= c - 1)        /* Extend the basic range */                if (occ < (unsigned int)c  &&
3052                      ocd >= (unsigned int)c - 1)      /* Extend the basic range */
3053                  {                                  /* if there is overlap,   */                  {                                  /* if there is overlap,   */
3054                  c = occ;                           /* noting that if occ < c */                  c = occ;                           /* noting that if occ < c */
3055                  continue;                          /* we can't have ocd > d  */                  continue;                          /* we can't have ocd > d  */
3056                  }                                  /* because a subrange is  */                  }                                  /* because a subrange is  */
3057                if (ocd > d && occ <= d + 1)         /* always shorter than    */                if (ocd > (unsigned int)d &&
3058                      occ <= (unsigned int)d + 1)      /* always shorter than    */
3059                  {                                  /* the basic range.       */                  {                                  /* the basic range.       */
3060                  d = ocd;                  d = ocd;
3061                  continue;                  continue;
# Line 3263  for (;; ptr++) Line 3651  for (;; ptr++)
3651          goto FAILED;          goto FAILED;
3652          }          }
3653    
       /* This is a paranoid check to stop integer overflow later on */  
   
       if (len > MAX_DUPLENGTH)  
         {  
         *errorcodeptr = ERR50;  
         goto FAILED;  
         }  
   
3654        /* 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
3655        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
3656        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 3739  for (;; ptr++)
3739          if (repeat_min > 1)          if (repeat_min > 1)
3740            {            {
3741            /* 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
3742            just adjust the length as if we had. */            just adjust the length as if we had. Do some paranoid checks for
3743              potential integer overflow. */
3744    
3745            if (lengthptr != NULL)            if (lengthptr != NULL)
3746              *lengthptr += (repeat_min - 1)*length_prevgroup;              {
3747                int delta = (repeat_min - 1)*length_prevgroup;
3748                if ((double)(repeat_min - 1)*(double)length_prevgroup >
3749                                                                (double)INT_MAX ||
3750                    OFLOW_MAX - *lengthptr < delta)
3751                  {
3752                  *errorcodeptr = ERR20;
3753                  goto FAILED;
3754                  }
3755                *lengthptr += delta;
3756                }
3757    
3758            /* This is compiling for real */            /* This is compiling for real */
3759    
# Line 3400  for (;; ptr++) Line 3791  for (;; ptr++)
3791          /* 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
3792          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
3793          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
3794          add 2 + 2*LINKSIZE to allow for the nesting that occurs. */          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
3795            paranoid checks to avoid integer overflow. */
3796    
3797          if (lengthptr != NULL && repeat_max > 0)          if (lengthptr != NULL && repeat_max > 0)
3798            *lengthptr += repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -            {
3799              2 - 2*LINK_SIZE;  /* Last one doesn't nest */            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
3800                          2 - 2*LINK_SIZE;   /* Last one doesn't nest */
3801              if ((double)repeat_max *
3802                    (double)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
3803                      > (double)INT_MAX ||
3804                  OFLOW_MAX - *lengthptr < delta)
3805                {
3806                *errorcodeptr = ERR20;
3807                goto FAILED;
3808                }
3809              *lengthptr += delta;
3810              }
3811    
3812          /* This is compiling for real */          /* This is compiling for real */
3813    
# Line 3556  for (;; ptr++) Line 3959  for (;; ptr++)
3959      /* ===================================================================*/      /* ===================================================================*/
3960      /* Start of nested parenthesized sub-expression, or comment or lookahead or      /* Start of nested parenthesized sub-expression, or comment or lookahead or
3961      lookbehind or option setting or condition or all the other extended      lookbehind or option setting or condition or all the other extended
3962      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. */  
3963    
3964      case '(':      case '(':
3965      newoptions = options;      newoptions = options;
3966      skipbytes = 0;      skipbytes = 0;
3967      bravalue = OP_CBRA;      bravalue = OP_CBRA;
3968      save_hwm = cd->hwm;      save_hwm = cd->hwm;
3969        reset_bracount = FALSE;
3970    
3971      if (*(++ptr) == '?')      /* First deal with various "verbs" that can be introduced by '*'. */
3972    
3973        if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)
3974          {
3975          int i, namelen;
3976          const uschar *name = ++ptr;
3977          previous = NULL;
3978          while ((cd->ctypes[*++ptr] & ctype_letter) != 0);
3979          if (*ptr == ':')
3980            {
3981            *errorcodeptr = ERR59;   /* Not supported */
3982            goto FAILED;
3983            }
3984          if (*ptr != ')')
3985            {
3986            *errorcodeptr = ERR60;
3987            goto FAILED;
3988            }
3989          namelen = ptr - name;
3990          for (i = 0; i < verbcount; i++)
3991            {
3992            if (namelen == verbs[i].len &&
3993                strncmp((char *)name, verbs[i].name, namelen) == 0)
3994              {
3995              *code = verbs[i].op;
3996              if (*code++ == OP_ACCEPT) cd->had_accept = TRUE;
3997              break;
3998              }
3999            }
4000          if (i < verbcount) continue;
4001          *errorcodeptr = ERR60;
4002          goto FAILED;
4003          }
4004    
4005        /* Deal with the extended parentheses; all are introduced by '?', and the
4006        appearance of any of them means that this is not a capturing group. */
4007    
4008        else if (*ptr == '?')
4009        {        {
4010        int i, set, unset, namelen;        int i, set, unset, namelen;
4011        int *optset;        int *optset;
# Line 3587  for (;; ptr++) Line 4026  for (;; ptr++)
4026    
4027    
4028          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4029            case '|':                 /* Reset capture count for each branch */
4030            reset_bracount = TRUE;
4031            /* Fall through */
4032    
4033            /* ------------------------------------------------------------ */
4034          case ':':                 /* Non-capturing bracket */          case ':':                 /* Non-capturing bracket */
4035          bravalue = OP_BRA;          bravalue = OP_BRA;
4036          ptr++;          ptr++;
# Line 3622  for (;; ptr++) Line 4066  for (;; ptr++)
4066    
4067          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
4068          skipbytes = 3;          skipbytes = 3;
4069            refsign = -1;
4070    
4071          /* Check for a test for recursion in a named group. */          /* Check for a test for recursion in a named group. */
4072    
# Line 3645  for (;; ptr++) Line 4090  for (;; ptr++)
4090            terminator = '\'';            terminator = '\'';
4091            ptr++;            ptr++;
4092            }            }
4093          else terminator = 0;          else
4094              {
4095              terminator = 0;
4096              if (ptr[1] == '-' || ptr[1] == '+') refsign = *(++ptr);
4097              }
4098    
4099          /* 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 */
4100    
# Line 3681  for (;; ptr++) Line 4130  for (;; ptr++)
4130          if (lengthptr != NULL) break;          if (lengthptr != NULL) break;
4131    
4132          /* 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
4133          reference. */          reference. If the string started with "+" or "-" we require the rest to
4134            be digits, in which case recno will be set. */
4135    
4136            if (refsign > 0)
4137              {
4138              if (recno <= 0)
4139                {
4140                *errorcodeptr = ERR58;
4141                goto FAILED;
4142                }
4143              if (refsign == '-')
4144                {
4145                recno = cd->bracount - recno + 1;
4146                if (recno <= 0)
4147                  {
4148                  *errorcodeptr = ERR15;
4149                  goto FAILED;
4150                  }
4151                }
4152              else recno += cd->bracount;
4153              PUT2(code, 2+LINK_SIZE, recno);
4154              break;
4155              }
4156    
4157            /* Otherwise (did not start with "+" or "-"), start by looking for the
4158            name. */
4159    
4160          slot = cd->name_table;          slot = cd->name_table;
4161          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
# Line 3773  for (;; ptr++) Line 4247  for (;; ptr++)
4247    
4248          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4249          case '!':                 /* Negative lookahead */          case '!':                 /* Negative lookahead */
         bravalue = OP_ASSERT_NOT;  
4250          ptr++;          ptr++;
4251            if (*ptr == ')')          /* Optimize (?!) */
4252              {
4253              *code++ = OP_FAIL;
4254              previous = NULL;
4255              continue;
4256              }
4257            bravalue = OP_ASSERT_NOT;
4258          break;          break;
4259    
4260    
# Line 4005  for (;; ptr++) Line 4485  for (;; ptr++)
4485          case '5': case '6': case '7': case '8': case '9':   /* subroutine */          case '5': case '6': case '7': case '8': case '9':   /* subroutine */
4486            {            {
4487            const uschar *called;            const uschar *called;
           int sign = *ptr;  
4488    
4489            if (sign == '+') ptr++;            if ((refsign = *ptr) == '+') ptr++;
4490            else if (sign == '-')            else if (refsign == '-')
4491              {              {
4492              if ((digitab[ptr[1]] & ctype_digit) == 0)              if ((digitab[ptr[1]] & ctype_digit) == 0)
4493                goto OTHER_CHAR_AFTER_QUERY;                goto OTHER_CHAR_AFTER_QUERY;
4494              ptr++;              ptr++;
4495              }              }
4496    
4497            recno = 0;            recno = 0;
4498            while((digitab[*ptr] & ctype_digit) != 0)            while((digitab[*ptr] & ctype_digit) != 0)
4499              recno = recno * 10 + *ptr++ - '0';              recno = recno * 10 + *ptr++ - '0';
# Line 4024  for (;; ptr++) Line 4503  for (;; ptr++)
4503              *errorcodeptr = ERR29;              *errorcodeptr = ERR29;
4504              goto FAILED;              goto FAILED;
4505              }              }
4506    
4507            if (sign == '-')            if (refsign == '-')
4508              {              {
4509              if (recno == 0)              if (recno == 0)
4510                {                {
4511                *errorcodeptr = ERR58;                *errorcodeptr = ERR58;
4512                goto FAILED;                goto FAILED;
4513                }                }
4514              recno = cd->bracount - recno + 1;              recno = cd->bracount - recno + 1;
4515              if (recno <= 0)              if (recno <= 0)
4516                {                {
4517                *errorcodeptr = ERR15;                *errorcodeptr = ERR15;
4518                goto FAILED;                goto FAILED;
4519                }                }
4520              }              }
4521            else if (sign == '+')            else if (refsign == '+')
4522              {              {
4523              if (recno == 0)              if (recno == 0)
4524                {                {
4525                *errorcodeptr = ERR58;                *errorcodeptr = ERR58;
4526                goto FAILED;                goto FAILED;
4527                }                }
4528              recno += cd->bracount;              recno += cd->bracount;
4529              }              }
4530    
4531            /* Come here from code above that handles a named recursion */            /* Come here from code above that handles a named recursion */
4532    
# Line 4121  for (;; ptr++) Line 4600  for (;; ptr++)
4600    
4601          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4602          default:              /* Other characters: check option setting */          default:              /* Other characters: check option setting */
4603          OTHER_CHAR_AFTER_QUERY:          OTHER_CHAR_AFTER_QUERY:
4604          set = unset = 0;          set = unset = 0;
4605          optset = &set;          optset = &set;
4606    
# Line 4256  for (;; ptr++) Line 4735  for (;; ptr++)
4735           errorcodeptr,                 /* Where to put an error message */           errorcodeptr,                 /* Where to put an error message */
4736           (bravalue == OP_ASSERTBACK ||           (bravalue == OP_ASSERTBACK ||
4737            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
4738             reset_bracount,               /* True if (?| group */
4739           skipbytes,                    /* Skip over bracket number */           skipbytes,                    /* Skip over bracket number */
4740           &subfirstbyte,                /* For possible first char */           &subfirstbyte,                /* For possible first char */
4741           &subreqbyte,                  /* For possible last char */           &subreqbyte,                  /* For possible last char */
# Line 4272  for (;; ptr++) Line 4752  for (;; ptr++)
4752      is on the bracket. */      is on the bracket. */
4753    
4754      /* 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
4755      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
4756        in the real compile phase, not in the pre-pass, where the whole group may
4757        not be available. */
4758    
4759      if (bravalue == OP_COND)      if (bravalue == OP_COND && lengthptr == NULL)
4760        {        {
4761        uschar *tc = code;        uschar *tc = code;
4762        int condcount = 0;        int condcount = 0;
# Line 4321  for (;; ptr++) Line 4803  for (;; ptr++)
4803        goto FAILED;        goto FAILED;
4804        }        }
4805    
4806      /* In the pre-compile phase, update the length by the length of the nested      /* In the pre-compile phase, update the length by the length of the group,
4807      group, less the brackets at either end. Then reduce the compiled code to      less the brackets at either end. Then reduce the compiled code to just a
4808      just the brackets so that it doesn't use much memory if it is duplicated by      set of non-capturing brackets so that it doesn't use much memory if it is
4809      a quantifier. */      duplicated by a quantifier.*/
4810    
4811      if (lengthptr != NULL)      if (lengthptr != NULL)
4812        {        {
4813          if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE)
4814            {
4815            *errorcodeptr = ERR20;
4816            goto FAILED;
4817            }
4818        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
4819        code++;        *code++ = OP_BRA;
4820        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
4821        *code++ = OP_KET;        *code++ = OP_KET;
4822        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
4823          break;    /* No need to waste time with special character handling */
4824        }        }
4825    
4826      /* Otherwise update the main code pointer to the end of the group. */      /* Otherwise update the main code pointer to the end of the group. */
4827    
4828      else code = tempcode;      code = tempcode;
4829    
4830      /* For a DEFINE group, required and first character settings are not      /* For a DEFINE group, required and first character settings are not
4831      relevant. */      relevant. */
# Line 4434  for (;; ptr++) Line 4922  for (;; ptr++)
4922        zerofirstbyte = firstbyte;        zerofirstbyte = firstbyte;
4923        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
4924    
4925        /* \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).
4926          We also support \k{name} (.NET syntax) */
4927    
4928        if (-c == ESC_k && (ptr[1] == '<' || ptr[1] == '\''))        if (-c == ESC_k && (ptr[1] == '<' || ptr[1] == '\'' || ptr[1] == '{'))
4929          {          {
4930          is_recurse = FALSE;          is_recurse = FALSE;
4931          terminator = (*(++ptr) == '<')? '>' : '\'';          terminator = (*(++ptr) == '<')? '>' : (*ptr == '\'')? '\'' : '}';
4932          goto NAMED_REF_OR_RECURSE;          goto NAMED_REF_OR_RECURSE;
4933          }          }
4934    
# Line 4605  This function is used during the pre-com Line 5094  This function is used during the pre-com
5094  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
5095  value of lengthptr distinguishes the two phases.  value of lengthptr distinguishes the two phases.
5096    
5097  Argument:  Arguments:
5098    options        option bits, including any changes for this subpattern    options        option bits, including any changes for this subpattern
5099    oldims         previous settings of ims option bits    oldims         previous settings of ims option bits
5100    codeptr        -> the address of the current code pointer    codeptr        -> the address of the current code pointer
5101    ptrptr         -> the address of the current pattern pointer    ptrptr         -> the address of the current pattern pointer
5102    errorcodeptr   -> pointer to error code variable    errorcodeptr   -> pointer to error code variable
5103    lookbehind     TRUE if this is a lookbehind assertion    lookbehind     TRUE if this is a lookbehind assertion
5104      reset_bracount TRUE to reset the count for each branch
5105    skipbytes      skip this many bytes at start (for brackets and OP_COND)    skipbytes      skip this many bytes at start (for brackets and OP_COND)
5106    firstbyteptr   place to put the first required character, or a negative number    firstbyteptr   place to put the first required character, or a negative number
5107    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 5115  Returns: TRUE on success
5115    
5116  static BOOL  static BOOL
5117  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,
5118    int *errorcodeptr, BOOL lookbehind, int skipbytes, int *firstbyteptr,    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
5119    int *reqbyteptr, branch_chain *bcptr, compile_data *cd, int *lengthptr)    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,
5120      int *lengthptr)
5121  {  {
5122  const uschar *ptr = *ptrptr;  const uschar *ptr = *ptrptr;
5123  uschar *code = *codeptr;  uschar *code = *codeptr;
# Line 4636  uschar *reverse_count = NULL; Line 5127  uschar *reverse_count = NULL;
5127  int firstbyte, reqbyte;  int firstbyte, reqbyte;
5128  int branchfirstbyte, branchreqbyte;  int branchfirstbyte, branchreqbyte;
5129  int length;  int length;
5130    int orig_bracount;
5131    int max_bracount;
5132  branch_chain bc;  branch_chain bc;
5133    
5134  bc.outer = bcptr;  bc.outer = bcptr;
# Line 4664  code += 1 + LINK_SIZE + skipbytes; Line 5157  code += 1 + LINK_SIZE + skipbytes;
5157    
5158  /* Loop for each alternative branch */  /* Loop for each alternative branch */
5159    
5160    orig_bracount = max_bracount = cd->bracount;
5161  for (;;)  for (;;)
5162    {    {
5163      /* For a (?| group, reset the capturing bracket count so that each branch
5164      uses the same numbers. */
5165    
5166      if (reset_bracount) cd->bracount = orig_bracount;
5167    
5168    /* Handle a change of ims options at the start of the branch */    /* Handle a change of ims options at the start of the branch */
5169    
5170    if ((options & PCRE_IMS) != oldims)    if ((options & PCRE_IMS) != oldims)
# Line 4695  for (;;) Line 5194  for (;;)
5194      return FALSE;      return FALSE;
5195      }      }
5196    
5197      /* Keep the highest bracket count in case (?| was used and some branch
5198      has fewer than the rest. */
5199    
5200      if (cd->bracount > max_bracount) max_bracount = cd->bracount;
5201    
5202    /* 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. */
5203    
5204    if (lengthptr == NULL)    if (lengthptr == NULL)
# Line 4758  for (;;) Line 5262  for (;;)
5262        }        }
5263      }      }
5264    
5265    /* Reached end of expression, either ')' or end of pattern. Go back through    /* Reached end of expression, either ')' or end of pattern. In the real
5266    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
5267    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
5268    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
5269    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
5270    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,
5271    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.
5272    at the terminating char. */    Return leaving the pointer at the terminating char. */
5273    
5274    if (*ptr != '|')    if (*ptr != '|')
5275      {      {
5276      int branch_length = code - last_branch;      if (lengthptr == NULL)
     do  
5277        {        {
5278        int prev_length = GET(last_branch, 1);        int branch_length = code - last_branch;
5279        PUT(last_branch, 1, branch_length);        do
5280        branch_length = prev_length;          {
5281        last_branch -= branch_length;          int prev_length = GET(last_branch, 1);
5282            PUT(last_branch, 1, branch_length);
5283            branch_length = prev_length;
5284            last_branch -= branch_length;
5285            }
5286          while (branch_length > 0);
5287        }        }
     while (branch_length > 0);  
5288    
5289      /* Fill in the ket */      /* Fill in the ket */
5290    
# Line 4794  for (;;) Line 5301  for (;;)
5301        length += 2;        length += 2;
5302        }        }
5303    
5304        /* Retain the highest bracket number, in case resetting was used. */
5305    
5306        cd->bracount = max_bracount;
5307    
5308      /* Set values to pass back */      /* Set values to pass back */
5309    
5310      *codeptr = code;      *codeptr = code;
5311      *ptrptr = ptr;      *ptrptr = ptr;
5312      *firstbyteptr = firstbyte;      *firstbyteptr = firstbyte;
5313      *reqbyteptr = reqbyte;      *reqbyteptr = reqbyte;
5314      if (lengthptr != NULL) *lengthptr += length;      if (lengthptr != NULL)
5315          {
5316          if (OFLOW_MAX - *lengthptr < length)
5317            {
5318            *errorcodeptr = ERR20;
5319            return FALSE;
5320            }
5321          *lengthptr += length;
5322          }
5323      return TRUE;      return TRUE;
5324      }      }
5325    
5326    /* 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
5327      pointer back to where it was for the start of the first branch. (That is,
5328      pretend that each branch is the only one.)
5329    
5330      In the real compile phase, insert an ALT node. Its length field points back
5331    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
5332    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
5333    zero offset until it is closed, making it possible to detect recursion. */    zero offset until it is closed, making it possible to detect recursion. */
5334    
5335    *code = OP_ALT;    if (lengthptr != NULL)
5336    PUT(code, 1, code - last_branch);      {
5337    bc.current = last_branch = code;      code = *codeptr + 1 + LINK_SIZE + skipbytes;
5338    code += 1 + LINK_SIZE;      length += 1 + LINK_SIZE;
5339        }
5340      else
5341        {
5342        *code = OP_ALT;
5343        PUT(code, 1, code - last_branch);
5344        bc.current = last_branch = code;
5345        code += 1 + LINK_SIZE;
5346        }
5347    
5348    ptr++;    ptr++;
   length += 1 + LINK_SIZE;  
5349    }    }
5350  /* Control never reaches here */  /* Control never reaches here */
5351  }  }
# Line 5256  outside can help speed up starting point Line 5787  outside can help speed up starting point
5787  code = cworkspace;  code = cworkspace;
5788  *code = OP_BRA;  *code = OP_BRA;
5789  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,
5790    &code, &ptr, &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, &length);    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,
5791      &length);
5792  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
5793    
5794  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 5847  cd->start_code = codestart;
5847  cd->hwm = cworkspace;  cd->hwm = cworkspace;
5848  cd->req_varyopt = 0;  cd->req_varyopt = 0;
5849  cd->nopartial = FALSE;  cd->nopartial = FALSE;
5850    cd->had_accept = FALSE;
5851    
5852  /* Set up a starting, non-extracting bracket, then compile the expression. On  /* Set up a starting, non-extracting bracket, then compile the expression. On
5853  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 5857  ptr = (const uschar *)pattern;
5857  code = (uschar *)codestart;  code = (uschar *)codestart;
5858  *code = OP_BRA;  *code = OP_BRA;
5859  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,
5860    &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);    &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);
5861  re->top_bracket = cd->bracount;  re->top_bracket = cd->bracount;
5862  re->top_backref = cd->top_backref;  re->top_backref = cd->top_backref;
5863    
5864  if (cd->nopartial) re->options |= PCRE_NOPARTIAL;  if (cd->nopartial) re->options |= PCRE_NOPARTIAL;
5865    if (cd->had_accept) reqbyte = -1;   /* Must disable after (*ACCEPT) */
5866    
5867  /* 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. */
5868    

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12