/[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 96 by nigel, Fri Mar 2 13:10:43 2007 UTC revision 227 by ph10, Tue Aug 21 15:00:15 2007 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2006 University of Cambridge             Copyright (c) 1997-2007 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 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 82  are simple data values; negative values Line 97  are simple data values; negative values
97  on. Zero means further processing is needed (for things like \x), or the escape  on. Zero means further processing is needed (for things like \x), or the escape
98  is invalid. */  is invalid. */
99    
100  #if !EBCDIC   /* This is the "normal" table for ASCII systems */  #ifndef EBCDIC  /* This is the "normal" table for ASCII systems */
101  static const short int escapes[] = {  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    
114  #else         /* This is the "abnormal" table for EBCDIC systems */  #else           /* This is the "abnormal" table for EBCDIC systems */
115  static const short int escapes[] = {  static const short int escapes[] = {
116  /*  48 */     0,     0,      0,     '.',    '<',   '(',    '+',    '|',  /*  48 */     0,     0,      0,     '.',    '<',   '(',    '+',    '|',
117  /*  50 */   '&',     0,      0,       0,      0,     0,      0,      0,  /*  50 */   '&',     0,      0,       0,      0,     0,      0,      0,
# 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 208  static const char *error_texts[] = { Line 244  static const char *error_texts[] = {
244    "malformed number or name after (?(",    "malformed number or name after (?(",
245    "conditional group contains more than two branches",    "conditional group contains more than two branches",
246    "assertion expected after (?(",    "assertion expected after (?(",
247    "(?R or (?digits must be followed by )",    "(?R or (?[+-]digits must be followed by )",
248    /* 30 */    /* 30 */
249    "unknown POSIX class name",    "unknown POSIX class name",
250    "POSIX collating elements are not supported",    "POSIX collating elements are not supported",
# 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 (?- 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 262  For convenience, we use the same bit def Line 303  For convenience, we use the same bit def
303    
304  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
305    
306  #if !EBCDIC    /* This is the "normal" case, for ASCII systems */  #ifndef EBCDIC  /* This is the "normal" case, for ASCII systems */
307  static const unsigned char digitab[] =  static const unsigned char digitab[] =
308    {    {
309    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
# Line 298  static const unsigned char digitab[] = Line 339  static const unsigned char digitab[] =
339    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
340    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
341    
342  #else          /* This is the "abnormal" case, for EBCDIC systems */  #else           /* This is the "abnormal" case, for EBCDIC systems */
343  static const unsigned char digitab[] =  static const unsigned char digitab[] =
344    {    {
345    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
# Line 312  static const unsigned char digitab[] = Line 353  static const unsigned char digitab[] =
353    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*    - 71 40 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*    - 71 40 */
354    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  72- |     */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  72- |     */
355    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  & - 87 50 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  & - 87 50 */
356    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  88-     */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  88- 95    */
357    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  - -103 60 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  - -103 60 */
358    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ?     */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ?     */
359    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
# Line 346  static const unsigned char ebcdic_charta Line 387  static const unsigned char ebcdic_charta
387    0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*    - 71 */    0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*    - 71 */
388    0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /*  72- |  */    0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /*  72- |  */
389    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  & - 87 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  & - 87 */
390    0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /*  88-  */    0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /*  88- 95 */
391    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  - -103 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*  - -103 */
392    0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ?  */    0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ?  */
393    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
# Line 373  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 399  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 421  if (c == 0) *errorcodeptr = ERR1; Line 462  if (c == 0) *errorcodeptr = ERR1;
462  a table. A non-zero result is something that can be returned immediately.  a table. A non-zero result is something that can be returned immediately.
463  Otherwise further processing may be required. */  Otherwise further processing may be required. */
464    
465  #if !EBCDIC    /* ASCII coding */  #ifndef EBCDIC  /* ASCII coding */
466  else if (c < '0' || c > 'z') {}                           /* Not alphameric */  else if (c < '0' || c > 'z') {}                           /* Not alphameric */
467  else if ((i = escapes[c - '0']) != 0) c = i;  else if ((i = escapes[c - '0']) != 0) c = i;
468    
469  #else          /* EBCDIC coding */  #else           /* EBCDIC coding */
470  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphameric */  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphameric */
471  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  c = i;
472  #endif  #endif
# Line 452  else Line 493  else
493    
494      /* \g must be followed by a number, either plain or braced. If positive, it      /* \g must be followed by a number, either plain or braced. If positive, it
495      is an absolute backreference. If negative, it is a relative backreference.      is an absolute backreference. If negative, it is a relative backreference.
496      This is a Perl 5.10 feature. */      This is a Perl 5.10 feature. Perl 5.10 also supports \g{name} as a
497        reference to a named group. This is part of Perl's movement towards a
498        unified syntax for back references. As this is synonymous with \k{name}, we
499        fudge it up by pretending it really was \k. */
500    
501      case 'g':      case 'g':
502      if (ptr[1] == '{')      if (ptr[1] == '{')
503        {        {
504          const uschar *p;
505          for (p = ptr+2; *p != 0 && *p != '}'; p++)
506            if (*p != '-' && (digitab[*p] & ctype_digit) == 0) break;
507          if (*p != 0 && *p != '}')
508            {
509            c = -ESC_k;
510            break;
511            }
512        braced = TRUE;        braced = TRUE;
513        ptr++;        ptr++;
514        }        }
# Line 473  else Line 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 484  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 513  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 562  else Line 625  else
625          if (c == 0 && cc == '0') continue;     /* Leading zeroes */          if (c == 0 && cc == '0') continue;     /* Leading zeroes */
626          count++;          count++;
627    
628  #if !EBCDIC    /* ASCII coding */  #ifndef EBCDIC  /* ASCII coding */
629          if (cc >= 'a') cc -= 32;               /* Convert to upper case */          if (cc >= 'a') cc -= 32;               /* Convert to upper case */
630          c = (c << 4) + cc - ((cc < 'A')? '0' : ('A' - 10));          c = (c << 4) + cc - ((cc < 'A')? '0' : ('A' - 10));
631  #else          /* EBCDIC coding */  #else           /* EBCDIC coding */
632          if (cc >= 'a' && cc <= 'z') cc += 64;  /* Convert to upper case */          if (cc >= 'a' && cc <= 'z') cc += 64;  /* Convert to upper case */
633          c = (c << 4) + cc - ((cc >= '0')? '0' : ('A' - 10));          c = (c << 4) + cc - ((cc >= '0')? '0' : ('A' - 10));
634  #endif  #endif
# Line 589  else Line 652  else
652        {        {
653        int cc;                               /* Some compilers don't like ++ */        int cc;                               /* Some compilers don't like ++ */
654        cc = *(++ptr);                        /* in initializers */        cc = *(++ptr);                        /* in initializers */
655  #if !EBCDIC    /* ASCII coding */  #ifndef EBCDIC  /* ASCII coding */
656        if (cc >= 'a') cc -= 32;              /* Convert to upper case */        if (cc >= 'a') cc -= 32;              /* Convert to upper case */
657        c = c * 16 + cc - ((cc < 'A')? '0' : ('A' - 10));        c = c * 16 + cc - ((cc < 'A')? '0' : ('A' - 10));
658  #else          /* EBCDIC coding */  #else           /* EBCDIC coding */
659        if (cc <= 'z') cc += 64;              /* Convert to upper case */        if (cc <= 'z') cc += 64;              /* Convert to upper case */
660        c = c * 16 + cc - ((cc >= '0')? '0' : ('A' - 10));        c = c * 16 + cc - ((cc >= '0')? '0' : ('A' - 10));
661  #endif  #endif
# Line 608  else Line 671  else
671      if (c == 0)      if (c == 0)
672        {        {
673        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
674        return 0;        break;
675        }        }
676    
677  #if !EBCDIC    /* ASCII coding */  #ifndef EBCDIC  /* ASCII coding */
678      if (c >= 'a' && c <= 'z') c -= 32;      if (c >= 'a' && c <= 'z') c -= 32;
679      c ^= 0x40;      c ^= 0x40;
680  #else          /* EBCDIC coding */  #else           /* EBCDIC coding */
681      if (c >= 'a' && c <= 'z') c += 64;      if (c >= 'a' && c <= 'z') c += 64;
682      c ^= 0xC0;      c ^= 0xC0;
683  #endif  #endif
# Line 684  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 887  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 914  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 1042  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 1131  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 1239  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
1343      if (utf8) switch(c)      if (utf8) switch(c)
1344        {        {
1345        case OP_CHAR:        case OP_CHAR:
# Line 1266  for (;;) Line 1360  for (;;)
1360        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1361        break;        break;
1362        }        }
1363    #endif
1364      }      }
1365    }    }
1366  }  }
# Line 1301  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
1436      if (utf8) switch(c)      if (utf8) switch(c)
1437        {        {
1438        case OP_CHAR:        case OP_CHAR:
# Line 1329  for (;;) Line 1453  for (;;)
1453        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1454        break;        break;
1455        }        }
1456    #endif
1457      }      }
1458    }    }
1459  }  }
# Line 1366  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 1382  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 1395  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 1461  for (code = first_significant_code(code Line 1597  for (code = first_significant_code(code
1597      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1598      return FALSE;      return FALSE;
1599    
1600        /* These are going to continue, as they may be empty, but we have to
1601        fudge the length for the \p and \P cases. */
1602    
1603        case OP_TYPESTAR:
1604        case OP_TYPEMINSTAR:
1605        case OP_TYPEPOSSTAR:
1606        case OP_TYPEQUERY:
1607        case OP_TYPEMINQUERY:
1608        case OP_TYPEPOSQUERY:
1609        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
1610        break;
1611    
1612        /* Same for these */
1613    
1614        case OP_TYPEUPTO:
1615        case OP_TYPEMINUPTO:
1616        case OP_TYPEPOSUPTO:
1617        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1618        break;
1619    
1620      /* End of branch */      /* End of branch */
1621    
1622      case OP_KET:      case OP_KET:
# Line 1623  adjust_recurse(uschar *group, int adjust Line 1779  adjust_recurse(uschar *group, int adjust
1779    uschar *save_hwm)    uschar *save_hwm)
1780  {  {
1781  uschar *ptr = group;  uschar *ptr = group;
1782    
1783  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)
1784    {    {
1785    int offset;    int offset;
# Line 1921  if (next >= 0) switch(op_code) Line 2078  if (next >= 0) switch(op_code)
2078    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
2079    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;
2080    
2081      case OP_HSPACE:
2082      case OP_NOT_HSPACE:
2083      switch(next)
2084        {
2085        case 0x09:
2086        case 0x20:
2087        case 0xa0:
2088        case 0x1680:
2089        case 0x180e:
2090        case 0x2000:
2091        case 0x2001:
2092        case 0x2002:
2093        case 0x2003:
2094        case 0x2004:
2095        case 0x2005:
2096        case 0x2006:
2097        case 0x2007:
2098        case 0x2008:
2099        case 0x2009:
2100        case 0x200A:
2101        case 0x202f:
2102        case 0x205f:
2103        case 0x3000:
2104        return op_code != OP_HSPACE;
2105        default:
2106        return op_code == OP_HSPACE;
2107        }
2108    
2109      case OP_VSPACE:
2110      case OP_NOT_VSPACE:
2111      switch(next)
2112        {
2113        case 0x0a:
2114        case 0x0b:
2115        case 0x0c:
2116        case 0x0d:
2117        case 0x85:
2118        case 0x2028:
2119        case 0x2029:
2120        return op_code != OP_VSPACE;
2121        default:
2122        return op_code == OP_VSPACE;
2123        }
2124    
2125    default:    default:
2126    return FALSE;    return FALSE;
2127    }    }
# Line 1955  switch(op_code) Line 2156  switch(op_code)
2156      case ESC_W:      case ESC_W:
2157      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;
2158    
2159        case ESC_h:
2160        case ESC_H:
2161        switch(item)
2162          {
2163          case 0x09:
2164          case 0x20:
2165          case 0xa0:
2166          case 0x1680:
2167          case 0x180e:
2168          case 0x2000:
2169          case 0x2001:
2170          case 0x2002:
2171          case 0x2003:
2172          case 0x2004:
2173          case 0x2005:
2174          case 0x2006:
2175          case 0x2007:
2176          case 0x2008:
2177          case 0x2009:
2178          case 0x200A:
2179          case 0x202f:
2180          case 0x205f:
2181          case 0x3000:
2182          return -next != ESC_h;
2183          default:
2184          return -next == ESC_h;
2185          }
2186    
2187        case ESC_v:
2188        case ESC_V:
2189        switch(item)
2190          {
2191          case 0x0a:
2192          case 0x0b:
2193          case 0x0c:
2194          case 0x0d:
2195          case 0x85:
2196          case 0x2028:
2197          case 0x2029:
2198          return -next != ESC_v;
2199          default:
2200          return -next == ESC_v;
2201          }
2202    
2203      default:      default:
2204      return FALSE;      return FALSE;
2205      }      }
2206    
2207    case OP_DIGIT:    case OP_DIGIT:
2208    return next == -ESC_D || next == -ESC_s || next == -ESC_W;    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||
2209             next == -ESC_h || next == -ESC_v;
2210    
2211    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
2212    return next == -ESC_d;    return next == -ESC_d;
# Line 1969  switch(op_code) Line 2215  switch(op_code)
2215    return next == -ESC_S || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_d || next == -ESC_w;
2216    
2217    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
2218    return next == -ESC_s;    return next == -ESC_s || next == -ESC_h || next == -ESC_v;
2219    
2220      case OP_HSPACE:
2221      return next == -ESC_S || next == -ESC_H || next == -ESC_d || next == -ESC_w;
2222    
2223      case OP_NOT_HSPACE:
2224      return next == -ESC_h;
2225    
2226      /* Can't have \S in here because VT matches \S (Perl anomaly) */
2227      case OP_VSPACE:
2228      return next == -ESC_V || next == -ESC_d || next == -ESC_w;
2229    
2230      case OP_NOT_VSPACE:
2231      return next == -ESC_v;
2232    
2233    case OP_WORDCHAR:    case OP_WORDCHAR:
2234    return next == -ESC_W || next == -ESC_s;    return next == -ESC_W || next == -ESC_s || next == -ESC_h || next == -ESC_v;
2235    
2236    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
2237    return next == -ESC_w || next == -ESC_d;    return next == -ESC_w || next == -ESC_d;
# Line 2087  for (;; ptr++) Line 2346  for (;; ptr++)
2346    BOOL possessive_quantifier;    BOOL possessive_quantifier;
2347    BOOL is_quantifier;    BOOL is_quantifier;
2348    BOOL is_recurse;    BOOL is_recurse;
2349      BOOL reset_bracount;
2350    int class_charcount;    int class_charcount;
2351    int class_lastchar;    int class_lastchar;
2352    int newoptions;    int newoptions;
2353    int recno;    int recno;
2354      int refsign;
2355    int skipbytes;    int skipbytes;
2356    int subreqbyte;    int subreqbyte;
2357    int subfirstbyte;    int subfirstbyte;
# Line 2123  for (;; ptr++) Line 2384  for (;; ptr++)
2384      */      */
2385    
2386      if (code < last_code) code = last_code;      if (code < last_code) code = last_code;
2387    
2388        /* Paranoid check for integer overflow */
2389    
2390        if (OFLOW_MAX - *lengthptr < code - last_code)
2391          {
2392          *errorcodeptr = ERR20;
2393          goto FAILED;
2394          }
2395    
2396      *lengthptr += code - last_code;      *lengthptr += code - last_code;
2397      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));
2398    
# Line 2235  for (;; ptr++) Line 2505  for (;; ptr++)
2505      *ptrptr = ptr;      *ptrptr = ptr;
2506      if (lengthptr != NULL)      if (lengthptr != NULL)
2507        {        {
2508          if (OFLOW_MAX - *lengthptr < code - last_code)
2509            {
2510            *errorcodeptr = ERR20;
2511            goto FAILED;
2512            }
2513        *lengthptr += code - last_code;   /* To include callout length */        *lengthptr += code - last_code;   /* To include callout length */
2514        DPRINTF((">> end branch\n"));        DPRINTF((">> end branch\n"));
2515        }        }
# Line 2297  for (;; ptr++) Line 2572  for (;; ptr++)
2572        goto FAILED;        goto FAILED;
2573        }        }
2574    
2575      /* 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,
2576        if the first few characters (either before or after ^) are \Q\E or \E we
2577        skip them too. This makes for compatibility with Perl. */
2578    
2579      if ((c = *(++ptr)) == '^')      negate_class = FALSE;
2580        for (;;)
2581        {        {
       negate_class = TRUE;  
2582        c = *(++ptr);        c = *(++ptr);
2583        }        if (c == '\\')
2584      else          {
2585        {          if (ptr[1] == 'E') ptr++;
2586        negate_class = FALSE;            else if (strncmp((const char *)ptr+1, "Q\\E", 3) == 0) ptr += 3;
2587                else break;
2588            }
2589          else if (!negate_class && c == '^')
2590            negate_class = TRUE;
2591          else break;
2592        }        }
2593    
2594      /* 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 2447  for (;; ptr++) Line 2729  for (;; ptr++)
2729        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
2730        case. Inside a class (and only there) it is treated as backspace.        case. Inside a class (and only there) it is treated as backspace.
2731        Elsewhere it marks a word boundary. Other escapes have preset maps ready        Elsewhere it marks a word boundary. Other escapes have preset maps ready
2732        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
2733        character in them, so set class_charcount bigger than one. */        character in them, so set class_charcount bigger than one. */
2734    
2735        if (c == '\\')        if (c == '\\')
# Line 2467  for (;; ptr++) Line 2749  for (;; ptr++)
2749            else inescq = TRUE;            else inescq = TRUE;
2750            continue;            continue;
2751            }            }
2752            else if (-c == ESC_E) continue;  /* Ignore orphan \E */
2753    
2754          if (c < 0)          if (c < 0)
2755            {            {
# Line 2515  for (;; ptr++) Line 2798  for (;; ptr++)
2798            else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||            else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||
2799                     c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;                     c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;
2800    
2801              /* We need to deal with \H, \h, \V, and \v in both phases because
2802              they use extra memory. */
2803    
2804              if (-c == ESC_h)
2805                {
2806                SETBIT(classbits, 0x09); /* VT */
2807                SETBIT(classbits, 0x20); /* SPACE */
2808                SETBIT(classbits, 0xa0); /* NSBP */
2809    #ifdef SUPPORT_UTF8
2810                if (utf8)
2811                  {
2812                  class_utf8 = TRUE;
2813                  *class_utf8data++ = XCL_SINGLE;
2814                  class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);
2815                  *class_utf8data++ = XCL_SINGLE;
2816                  class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);
2817                  *class_utf8data++ = XCL_RANGE;
2818                  class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);
2819                  class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);
2820                  *class_utf8data++ = XCL_SINGLE;
2821                  class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);
2822                  *class_utf8data++ = XCL_SINGLE;
2823                  class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);
2824                  *class_utf8data++ = XCL_SINGLE;
2825                  class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);
2826                  }
2827    #endif
2828                continue;
2829                }
2830    
2831              if (-c == ESC_H)
2832                {
2833                for (c = 0; c < 32; c++)
2834                  {
2835                  int x = 0xff;
2836                  switch (c)
2837                    {
2838                    case 0x09/8: x ^= 1 << (0x09%8); break;
2839                    case 0x20/8: x ^= 1 << (0x20%8); break;
2840                    case 0xa0/8: x ^= 1 << (0xa0%8); break;
2841                    default: break;
2842                    }
2843                  classbits[c] |= x;
2844                  }
2845    
2846    #ifdef SUPPORT_UTF8
2847                if (utf8)
2848                  {
2849                  class_utf8 = TRUE;
2850                  *class_utf8data++ = XCL_RANGE;
2851                  class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);
2852                  class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);
2853                  *class_utf8data++ = XCL_RANGE;
2854                  class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);
2855                  class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);
2856                  *class_utf8data++ = XCL_RANGE;
2857                  class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);
2858                  class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);
2859                  *class_utf8data++ = XCL_RANGE;
2860                  class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);
2861                  class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);
2862                  *class_utf8data++ = XCL_RANGE;
2863                  class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);
2864                  class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);
2865                  *class_utf8data++ = XCL_RANGE;
2866                  class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);
2867                  class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);
2868                  *class_utf8data++ = XCL_RANGE;
2869                  class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);
2870                  class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);
2871                  }
2872    #endif
2873                continue;
2874                }
2875    
2876              if (-c == ESC_v)
2877                {
2878                SETBIT(classbits, 0x0a); /* LF */
2879                SETBIT(classbits, 0x0b); /* VT */
2880                SETBIT(classbits, 0x0c); /* FF */
2881                SETBIT(classbits, 0x0d); /* CR */
2882                SETBIT(classbits, 0x85); /* NEL */
2883    #ifdef SUPPORT_UTF8
2884                if (utf8)
2885                  {
2886                  class_utf8 = TRUE;
2887                  *class_utf8data++ = XCL_RANGE;
2888                  class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);
2889                  class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);
2890                  }
2891    #endif
2892                continue;
2893                }
2894    
2895              if (-c == ESC_V)
2896                {
2897                for (c = 0; c < 32; c++)
2898                  {
2899                  int x = 0xff;
2900                  switch (c)
2901                    {
2902                    case 0x0a/8: x ^= 1 << (0x0a%8);
2903                                 x ^= 1 << (0x0b%8);
2904                                 x ^= 1 << (0x0c%8);
2905                                 x ^= 1 << (0x0d%8);
2906                                 break;
2907                    case 0x85/8: x ^= 1 << (0x85%8); break;
2908                    default: break;
2909                    }
2910                  classbits[c] |= x;
2911                  }
2912    
2913    #ifdef SUPPORT_UTF8
2914                if (utf8)
2915                  {
2916                  class_utf8 = TRUE;
2917                  *class_utf8data++ = XCL_RANGE;
2918                  class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);
2919                  class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);
2920                  *class_utf8data++ = XCL_RANGE;
2921                  class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);
2922                  class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);
2923                  }
2924    #endif
2925                continue;
2926                }
2927    
2928            /* We need to deal with \P and \p in both phases. */            /* We need to deal with \P and \p in both phases. */
2929    
2930  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 2655  for (;; ptr++) Line 3065  for (;; ptr++)
3065              unsigned int origd = d;              unsigned int origd = d;
3066              while (get_othercase_range(&cc, origd, &occ, &ocd))              while (get_othercase_range(&cc, origd, &occ, &ocd))
3067                {                {
3068                if (occ >= c && ocd <= d) continue;  /* Skip embedded ranges */                if (occ >= (unsigned int)c &&
3069                      ocd <= (unsigned int)d)
3070                    continue;                          /* Skip embedded ranges */
3071    
3072                if (occ < c  && ocd >= c - 1)        /* Extend the basic range */                if (occ < (unsigned int)c  &&
3073                      ocd >= (unsigned int)c - 1)      /* Extend the basic range */
3074                  {                                  /* if there is overlap,   */                  {                                  /* if there is overlap,   */
3075                  c = occ;                           /* noting that if occ < c */                  c = occ;                           /* noting that if occ < c */
3076                  continue;                          /* we can't have ocd > d  */                  continue;                          /* we can't have ocd > d  */
3077                  }                                  /* because a subrange is  */                  }                                  /* because a subrange is  */
3078                if (ocd > d && occ <= d + 1)         /* always shorter than    */                if (ocd > (unsigned int)d &&
3079                      occ <= (unsigned int)d + 1)      /* always shorter than    */
3080                  {                                  /* the basic range.       */                  {                                  /* the basic range.       */
3081                  d = ocd;                  d = ocd;
3082                  continue;                  continue;
# Line 2782  for (;; ptr++) Line 3196  for (;; ptr++)
3196        goto FAILED;        goto FAILED;
3197        }        }
3198    
3199        /* Remember whether \r or \n are in this class */
3200    
3201        if (negate_class)
3202          {
3203          if ((classbits[1] & 0x24) != 0x24) cd->external_options |= PCRE_HASCRORLF;
3204          }
3205        else
3206          {
3207          if ((classbits[1] & 0x24) != 0) cd->external_options |= PCRE_HASCRORLF;
3208          }
3209    
3210      /* If class_charcount is 1, we saw precisely one character whose value is      /* If class_charcount is 1, we saw precisely one character whose value is
3211      less than 256. In non-UTF-8 mode we can always optimize. In UTF-8 mode, we      less than 256. As long as there were no characters >= 128 and there was no
3212      can optimize the negative case only if there were no characters >= 128      use of \p or \P, in other words, no use of any XCLASS features, we can
3213      because OP_NOT and the related opcodes like OP_NOTSTAR operate on      optimize.
3214      single-bytes only. This is an historical hangover. Maybe one day we can  
3215      tidy these opcodes to handle multi-byte characters.      In UTF-8 mode, we can optimize the negative case only if there were no
3216        characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR
3217        operate on single-bytes only. This is an historical hangover. Maybe one day
3218        we can tidy these opcodes to handle multi-byte characters.
3219    
3220      The optimization throws away the bit map. We turn the item into a      The optimization throws away the bit map. We turn the item into a
3221      1-character OP_CHAR[NC] if it's positive, or OP_NOT if it's negative. Note      1-character OP_CHAR[NC] if it's positive, or OP_NOT if it's negative. Note
# Line 2797  for (;; ptr++) Line 3225  for (;; ptr++)
3225      reqbyte, save the previous value for reinstating. */      reqbyte, save the previous value for reinstating. */
3226    
3227  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3228      if (class_charcount == 1 &&      if (class_charcount == 1 && !class_utf8 &&
3229            (!utf8 ||        (!utf8 || !negate_class || class_lastchar < 128))
           (!class_utf8 && (!negate_class || class_lastchar < 128))))  
   
3230  #else  #else
3231      if (class_charcount == 1)      if (class_charcount == 1)
3232  #endif  #endif
# Line 3258  for (;; ptr++) Line 3684  for (;; ptr++)
3684          goto FAILED;          goto FAILED;
3685          }          }
3686    
       /* This is a paranoid check to stop integer overflow later on */  
   
       if (len > MAX_DUPLENGTH)  
         {  
         *errorcodeptr = ERR50;  
         goto FAILED;  
         }  
   
3687        /* 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
3688        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
3689        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 3354  for (;; ptr++) Line 3772  for (;; ptr++)
3772          if (repeat_min > 1)          if (repeat_min > 1)
3773            {            {
3774            /* 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
3775            just adjust the length as if we had. */            just adjust the length as if we had. Do some paranoid checks for
3776              potential integer overflow. */
3777    
3778            if (lengthptr != NULL)            if (lengthptr != NULL)
3779              *lengthptr += (repeat_min - 1)*length_prevgroup;              {
3780                int delta = (repeat_min - 1)*length_prevgroup;
3781                if ((double)(repeat_min - 1)*(double)length_prevgroup >
3782                                                                (double)INT_MAX ||
3783                    OFLOW_MAX - *lengthptr < delta)
3784                  {
3785                  *errorcodeptr = ERR20;
3786                  goto FAILED;
3787                  }
3788                *lengthptr += delta;
3789                }
3790    
3791            /* This is compiling for real */            /* This is compiling for real */
3792    
# Line 3395  for (;; ptr++) Line 3824  for (;; ptr++)
3824          /* 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
3825          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
3826          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
3827          add 2 + 2*LINKSIZE to allow for the nesting that occurs. */          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
3828            paranoid checks to avoid integer overflow. */
3829    
3830          if (lengthptr != NULL && repeat_max > 0)          if (lengthptr != NULL && repeat_max > 0)
3831            *lengthptr += repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -            {
3832              2 - 2*LINK_SIZE;  /* Last one doesn't nest */            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
3833                          2 - 2*LINK_SIZE;   /* Last one doesn't nest */
3834              if ((double)repeat_max *
3835                    (double)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
3836                      > (double)INT_MAX ||
3837                  OFLOW_MAX - *lengthptr < delta)
3838                {
3839                *errorcodeptr = ERR20;
3840                goto FAILED;
3841                }
3842              *lengthptr += delta;
3843              }
3844    
3845          /* This is compiling for real */          /* This is compiling for real */
3846    
# Line 3551  for (;; ptr++) Line 3992  for (;; ptr++)
3992      /* ===================================================================*/      /* ===================================================================*/
3993      /* Start of nested parenthesized sub-expression, or comment or lookahead or      /* Start of nested parenthesized sub-expression, or comment or lookahead or
3994      lookbehind or option setting or condition or all the other extended      lookbehind or option setting or condition or all the other extended
3995      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. */  
3996    
3997      case '(':      case '(':
3998      newoptions = options;      newoptions = options;
3999      skipbytes = 0;      skipbytes = 0;
4000      bravalue = OP_CBRA;      bravalue = OP_CBRA;
4001      save_hwm = cd->hwm;      save_hwm = cd->hwm;
4002        reset_bracount = FALSE;
4003    
4004        /* First deal with various "verbs" that can be introduced by '*'. */
4005    
4006        if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)
4007          {
4008          int i, namelen;
4009          const uschar *name = ++ptr;
4010          previous = NULL;
4011          while ((cd->ctypes[*++ptr] & ctype_letter) != 0);
4012          if (*ptr == ':')
4013            {
4014            *errorcodeptr = ERR59;   /* Not supported */
4015            goto FAILED;
4016            }
4017          if (*ptr != ')')
4018            {
4019            *errorcodeptr = ERR60;
4020            goto FAILED;
4021            }
4022          namelen = ptr - name;
4023          for (i = 0; i < verbcount; i++)
4024            {
4025            if (namelen == verbs[i].len &&
4026                strncmp((char *)name, verbs[i].name, namelen) == 0)
4027              {
4028              *code = verbs[i].op;
4029              if (*code++ == OP_ACCEPT) cd->had_accept = TRUE;
4030              break;
4031              }
4032            }
4033          if (i < verbcount) continue;
4034          *errorcodeptr = ERR60;
4035          goto FAILED;
4036          }
4037    
4038      if (*(++ptr) == '?')      /* Deal with the extended parentheses; all are introduced by '?', and the
4039        appearance of any of them means that this is not a capturing group. */
4040    
4041        else if (*ptr == '?')
4042        {        {
4043        int i, set, unset, namelen;        int i, set, unset, namelen;
4044        int *optset;        int *optset;
# Line 3582  for (;; ptr++) Line 4059  for (;; ptr++)
4059    
4060    
4061          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4062            case '|':                 /* Reset capture count for each branch */
4063            reset_bracount = TRUE;
4064            /* Fall through */
4065    
4066            /* ------------------------------------------------------------ */
4067          case ':':                 /* Non-capturing bracket */          case ':':                 /* Non-capturing bracket */
4068          bravalue = OP_BRA;          bravalue = OP_BRA;
4069          ptr++;          ptr++;
# Line 3617  for (;; ptr++) Line 4099  for (;; ptr++)
4099    
4100          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
4101          skipbytes = 3;          skipbytes = 3;
4102            refsign = -1;
4103    
4104          /* Check for a test for recursion in a named group. */          /* Check for a test for recursion in a named group. */
4105    
# Line 3640  for (;; ptr++) Line 4123  for (;; ptr++)
4123            terminator = '\'';            terminator = '\'';
4124            ptr++;            ptr++;
4125            }            }
4126          else terminator = 0;          else
4127              {
4128              terminator = 0;
4129              if (ptr[1] == '-' || ptr[1] == '+') refsign = *(++ptr);
4130              }
4131    
4132          /* 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 */
4133    
# Line 3676  for (;; ptr++) Line 4163  for (;; ptr++)
4163          if (lengthptr != NULL) break;          if (lengthptr != NULL) break;
4164    
4165          /* 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
4166          reference. */          reference. If the string started with "+" or "-" we require the rest to
4167            be digits, in which case recno will be set. */
4168    
4169            if (refsign > 0)
4170              {
4171              if (recno <= 0)
4172                {
4173                *errorcodeptr = ERR58;
4174                goto FAILED;
4175                }
4176              if (refsign == '-')
4177                {
4178                recno = cd->bracount - recno + 1;
4179                if (recno <= 0)
4180                  {
4181                  *errorcodeptr = ERR15;
4182                  goto FAILED;
4183                  }
4184                }
4185              else recno += cd->bracount;
4186              PUT2(code, 2+LINK_SIZE, recno);
4187              break;
4188              }
4189    
4190            /* Otherwise (did not start with "+" or "-"), start by looking for the
4191            name. */
4192    
4193          slot = cd->name_table;          slot = cd->name_table;
4194          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
# Line 3768  for (;; ptr++) Line 4280  for (;; ptr++)
4280    
4281          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4282          case '!':                 /* Negative lookahead */          case '!':                 /* Negative lookahead */
         bravalue = OP_ASSERT_NOT;  
4283          ptr++;          ptr++;
4284            if (*ptr == ')')          /* Optimize (?!) */
4285              {
4286              *code++ = OP_FAIL;
4287              previous = NULL;
4288              continue;
4289              }
4290            bravalue = OP_ASSERT_NOT;
4291          break;          break;
4292    
4293    
# Line 3995  for (;; ptr++) Line 4513  for (;; ptr++)
4513    
4514    
4515          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4516            case '-': case '+':
4517          case '0': case '1': case '2': case '3': case '4':   /* Recursion or */          case '0': case '1': case '2': case '3': case '4':   /* Recursion or */
4518          case '5': case '6': case '7': case '8': case '9':   /* subroutine */          case '5': case '6': case '7': case '8': case '9':   /* subroutine */
4519            {            {
4520            const uschar *called;            const uschar *called;
4521    
4522              if ((refsign = *ptr) == '+') ptr++;
4523              else if (refsign == '-')
4524                {
4525                if ((digitab[ptr[1]] & ctype_digit) == 0)
4526                  goto OTHER_CHAR_AFTER_QUERY;
4527                ptr++;
4528                }
4529    
4530            recno = 0;            recno = 0;
4531            while((digitab[*ptr] & ctype_digit) != 0)            while((digitab[*ptr] & ctype_digit) != 0)
4532              recno = recno * 10 + *ptr++ - '0';              recno = recno * 10 + *ptr++ - '0';
4533    
4534            if (*ptr != ')')            if (*ptr != ')')
4535              {              {
4536              *errorcodeptr = ERR29;              *errorcodeptr = ERR29;
4537              goto FAILED;              goto FAILED;
4538              }              }
4539    
4540              if (refsign == '-')
4541                {
4542                if (recno == 0)
4543                  {
4544                  *errorcodeptr = ERR58;
4545                  goto FAILED;
4546                  }
4547                recno = cd->bracount - recno + 1;
4548                if (recno <= 0)
4549                  {
4550                  *errorcodeptr = ERR15;
4551                  goto FAILED;
4552                  }
4553                }
4554              else if (refsign == '+')
4555                {
4556                if (recno == 0)
4557                  {
4558                  *errorcodeptr = ERR58;
4559                  goto FAILED;
4560                  }
4561                recno += cd->bracount;
4562                }
4563    
4564            /* Come here from code above that handles a named recursion */            /* Come here from code above that handles a named recursion */
4565    
4566            HANDLE_RECURSION:            HANDLE_RECURSION:
# Line 4080  for (;; ptr++) Line 4633  for (;; ptr++)
4633    
4634          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
4635          default:              /* Other characters: check option setting */          default:              /* Other characters: check option setting */
4636            OTHER_CHAR_AFTER_QUERY:
4637          set = unset = 0;          set = unset = 0;
4638          optset = &set;          optset = &set;
4639    
# Line 4214  for (;; ptr++) Line 4768  for (;; ptr++)
4768           errorcodeptr,                 /* Where to put an error message */           errorcodeptr,                 /* Where to put an error message */
4769           (bravalue == OP_ASSERTBACK ||           (bravalue == OP_ASSERTBACK ||
4770            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */            bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
4771             reset_bracount,               /* True if (?| group */
4772           skipbytes,                    /* Skip over bracket number */           skipbytes,                    /* Skip over bracket number */
4773           &subfirstbyte,                /* For possible first char */           &subfirstbyte,                /* For possible first char */
4774           &subreqbyte,                  /* For possible last char */           &subreqbyte,                  /* For possible last char */
# Line 4230  for (;; ptr++) Line 4785  for (;; ptr++)
4785      is on the bracket. */      is on the bracket. */
4786    
4787      /* 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
4788      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
4789        in the real compile phase, not in the pre-pass, where the whole group may
4790        not be available. */
4791    
4792      if (bravalue == OP_COND)      if (bravalue == OP_COND && lengthptr == NULL)
4793        {        {
4794        uschar *tc = code;        uschar *tc = code;
4795        int condcount = 0;        int condcount = 0;
# Line 4279  for (;; ptr++) Line 4836  for (;; ptr++)
4836        goto FAILED;        goto FAILED;
4837        }        }
4838    
4839      /* 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,
4840      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
4841      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
4842      a quantifier. */      duplicated by a quantifier.*/
4843    
4844      if (lengthptr != NULL)      if (lengthptr != NULL)
4845        {        {
4846          if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE)
4847            {
4848            *errorcodeptr = ERR20;
4849            goto FAILED;
4850            }
4851        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
4852        code++;        *code++ = OP_BRA;
4853        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
4854        *code++ = OP_KET;        *code++ = OP_KET;
4855        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
4856          break;    /* No need to waste time with special character handling */
4857        }        }
4858    
4859      /* Otherwise update the main code pointer to the end of the group. */      /* Otherwise update the main code pointer to the end of the group. */
4860    
4861      else code = tempcode;      code = tempcode;
4862    
4863      /* For a DEFINE group, required and first character settings are not      /* For a DEFINE group, required and first character settings are not
4864      relevant. */      relevant. */
# Line 4392  for (;; ptr++) Line 4955  for (;; ptr++)
4955        zerofirstbyte = firstbyte;        zerofirstbyte = firstbyte;
4956        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
4957    
4958        /* \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).
4959          We also support \k{name} (.NET syntax) */
4960    
4961        if (-c == ESC_k && (ptr[1] == '<' || ptr[1] == '\''))        if (-c == ESC_k && (ptr[1] == '<' || ptr[1] == '\'' || ptr[1] == '{'))
4962          {          {
4963          is_recurse = FALSE;          is_recurse = FALSE;
4964          terminator = (*(++ptr) == '<')? '>' : '\'';          terminator = (*(++ptr) == '<')? '>' : (*ptr == '\'')? '\'' : '}';
4965          goto NAMED_REF_OR_RECURSE;          goto NAMED_REF_OR_RECURSE;
4966          }          }
4967    
# Line 4498  for (;; ptr++) Line 5062  for (;; ptr++)
5062      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARNC : OP_CHAR;      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARNC : OP_CHAR;
5063      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
5064    
5065        /* Remember if \r or \n were seen */
5066    
5067        if (mcbuffer[0] == '\r' || mcbuffer[0] == '\n')
5068          cd->external_options |= PCRE_HASCRORLF;
5069    
5070      /* Set the first and required bytes appropriately. If no previous first      /* Set the first and required bytes appropriately. If no previous first
5071      byte, set it from this character, but revert to none on a zero repeat.      byte, set it from this character, but revert to none on a zero repeat.
5072      Otherwise, leave the firstbyte value alone, and don't change it on a zero      Otherwise, leave the firstbyte value alone, and don't change it on a zero
# Line 4563  This function is used during the pre-com Line 5132  This function is used during the pre-com
5132  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
5133  value of lengthptr distinguishes the two phases.  value of lengthptr distinguishes the two phases.
5134    
5135  Argument:  Arguments:
5136    options        option bits, including any changes for this subpattern    options        option bits, including any changes for this subpattern
5137    oldims         previous settings of ims option bits    oldims         previous settings of ims option bits
5138    codeptr        -> the address of the current code pointer    codeptr        -> the address of the current code pointer
5139    ptrptr         -> the address of the current pattern pointer    ptrptr         -> the address of the current pattern pointer
5140    errorcodeptr   -> pointer to error code variable    errorcodeptr   -> pointer to error code variable
5141    lookbehind     TRUE if this is a lookbehind assertion    lookbehind     TRUE if this is a lookbehind assertion
5142      reset_bracount TRUE to reset the count for each branch
5143    skipbytes      skip this many bytes at start (for brackets and OP_COND)    skipbytes      skip this many bytes at start (for brackets and OP_COND)
5144    firstbyteptr   place to put the first required character, or a negative number    firstbyteptr   place to put the first required character, or a negative number
5145    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 4583  Returns: TRUE on success Line 5153  Returns: TRUE on success
5153    
5154  static BOOL  static BOOL
5155  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,
5156    int *errorcodeptr, BOOL lookbehind, int skipbytes, int *firstbyteptr,    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
5157    int *reqbyteptr, branch_chain *bcptr, compile_data *cd, int *lengthptr)    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,
5158      int *lengthptr)
5159  {  {
5160  const uschar *ptr = *ptrptr;  const uschar *ptr = *ptrptr;
5161  uschar *code = *codeptr;  uschar *code = *codeptr;
# Line 4594  uschar *reverse_count = NULL; Line 5165  uschar *reverse_count = NULL;
5165  int firstbyte, reqbyte;  int firstbyte, reqbyte;
5166  int branchfirstbyte, branchreqbyte;  int branchfirstbyte, branchreqbyte;
5167  int length;  int length;
5168    int orig_bracount;
5169    int max_bracount;
5170  branch_chain bc;  branch_chain bc;
5171    
5172  bc.outer = bcptr;  bc.outer = bcptr;
# Line 4622  code += 1 + LINK_SIZE + skipbytes; Line 5195  code += 1 + LINK_SIZE + skipbytes;
5195    
5196  /* Loop for each alternative branch */  /* Loop for each alternative branch */
5197    
5198    orig_bracount = max_bracount = cd->bracount;
5199  for (;;)  for (;;)
5200    {    {
5201      /* For a (?| group, reset the capturing bracket count so that each branch
5202      uses the same numbers. */
5203    
5204      if (reset_bracount) cd->bracount = orig_bracount;
5205    
5206    /* Handle a change of ims options at the start of the branch */    /* Handle a change of ims options at the start of the branch */
5207    
5208    if ((options & PCRE_IMS) != oldims)    if ((options & PCRE_IMS) != oldims)
# Line 4653  for (;;) Line 5232  for (;;)
5232      return FALSE;      return FALSE;
5233      }      }
5234    
5235      /* Keep the highest bracket count in case (?| was used and some branch
5236      has fewer than the rest. */
5237    
5238      if (cd->bracount > max_bracount) max_bracount = cd->bracount;
5239    
5240    /* 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. */
5241    
5242    if (lengthptr == NULL)    if (lengthptr == NULL)
# Line 4716  for (;;) Line 5300  for (;;)
5300        }        }
5301      }      }
5302    
5303    /* Reached end of expression, either ')' or end of pattern. Go back through    /* Reached end of expression, either ')' or end of pattern. In the real
5304    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
5305    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
5306    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
5307    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
5308    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,
5309    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.
5310    at the terminating char. */    Return leaving the pointer at the terminating char. */
5311    
5312    if (*ptr != '|')    if (*ptr != '|')
5313      {      {
5314      int branch_length = code - last_branch;      if (lengthptr == NULL)
     do  
5315        {        {
5316        int prev_length = GET(last_branch, 1);        int branch_length = code - last_branch;
5317        PUT(last_branch, 1, branch_length);        do
5318        branch_length = prev_length;          {
5319        last_branch -= branch_length;          int prev_length = GET(last_branch, 1);
5320            PUT(last_branch, 1, branch_length);
5321            branch_length = prev_length;
5322            last_branch -= branch_length;
5323            }
5324          while (branch_length > 0);
5325        }        }
     while (branch_length > 0);  
5326    
5327      /* Fill in the ket */      /* Fill in the ket */
5328    
# Line 4752  for (;;) Line 5339  for (;;)
5339        length += 2;        length += 2;
5340        }        }
5341    
5342        /* Retain the highest bracket number, in case resetting was used. */
5343    
5344        cd->bracount = max_bracount;
5345    
5346      /* Set values to pass back */      /* Set values to pass back */
5347    
5348      *codeptr = code;      *codeptr = code;
5349      *ptrptr = ptr;      *ptrptr = ptr;
5350      *firstbyteptr = firstbyte;      *firstbyteptr = firstbyte;
5351      *reqbyteptr = reqbyte;      *reqbyteptr = reqbyte;
5352      if (lengthptr != NULL) *lengthptr += length;      if (lengthptr != NULL)
5353          {
5354          if (OFLOW_MAX - *lengthptr < length)
5355            {
5356            *errorcodeptr = ERR20;
5357            return FALSE;
5358            }
5359          *lengthptr += length;
5360          }
5361      return TRUE;      return TRUE;
5362      }      }
5363    
5364    /* 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
5365      pointer back to where it was for the start of the first branch. (That is,
5366      pretend that each branch is the only one.)
5367    
5368      In the real compile phase, insert an ALT node. Its length field points back
5369    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
5370    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
5371    zero offset until it is closed, making it possible to detect recursion. */    zero offset until it is closed, making it possible to detect recursion. */
5372    
5373    *code = OP_ALT;    if (lengthptr != NULL)
5374    PUT(code, 1, code - last_branch);      {
5375    bc.current = last_branch = code;      code = *codeptr + 1 + LINK_SIZE + skipbytes;
5376    code += 1 + LINK_SIZE;      length += 1 + LINK_SIZE;
5377        }
5378      else
5379        {
5380        *code = OP_ALT;
5381        PUT(code, 1, code - last_branch);
5382        bc.current = last_branch = code;
5383        code += 1 + LINK_SIZE;
5384        }
5385    
5386    ptr++;    ptr++;
   length += 1 + LINK_SIZE;  
5387    }    }
5388  /* Control never reaches here */  /* Control never reaches here */
5389  }  }
# Line 5039  Returns: pointer to compiled data Line 5650  Returns: pointer to compiled data
5650                  with errorptr and erroroffset set                  with errorptr and erroroffset set
5651  */  */
5652    
5653  PCRE_DATA_SCOPE pcre *  PCRE_EXP_DEFN pcre *
5654  pcre_compile(const char *pattern, int options, const char **errorptr,  pcre_compile(const char *pattern, int options, const char **errorptr,
5655    int *erroroffset, const unsigned char *tables)    int *erroroffset, const unsigned char *tables)
5656  {  {
# Line 5047  return pcre_compile2(pattern, options, N Line 5658  return pcre_compile2(pattern, options, N
5658  }  }
5659    
5660    
5661  PCRE_DATA_SCOPE pcre *  PCRE_EXP_DEFN pcre *
5662  pcre_compile2(const char *pattern, int options, int *errorcodeptr,  pcre_compile2(const char *pattern, int options, int *errorcodeptr,
5663    const char **errorptr, int *erroroffset, const unsigned char *tables)    const char **errorptr, int *erroroffset, const unsigned char *tables)
5664  {  {
# Line 5055  real_pcre *re; Line 5666  real_pcre *re;
5666  int length = 1;  /* For final END opcode */  int length = 1;  /* For final END opcode */
5667  int firstbyte, reqbyte, newline;  int firstbyte, reqbyte, newline;
5668  int errorcode = 0;  int errorcode = 0;
5669    int skipatstart = 0;
5670  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
5671  BOOL utf8;  BOOL utf8;
5672  #endif  #endif
# Line 5096  if (errorcodeptr != NULL) *errorcodeptr Line 5708  if (errorcodeptr != NULL) *errorcodeptr
5708  if (erroroffset == NULL)  if (erroroffset == NULL)
5709    {    {
5710    errorcode = ERR16;    errorcode = ERR16;
5711    goto PCRE_EARLY_ERROR_RETURN;    goto PCRE_EARLY_ERROR_RETURN2;
5712    }    }
5713    
5714  *erroroffset = 0;  *erroroffset = 0;
# Line 5109  if (utf8 && (options & PCRE_NO_UTF8_CHEC Line 5721  if (utf8 && (options & PCRE_NO_UTF8_CHEC
5721       (*erroroffset = _pcre_valid_utf8((uschar *)pattern, -1)) >= 0)       (*erroroffset = _pcre_valid_utf8((uschar *)pattern, -1)) >= 0)
5722    {    {
5723    errorcode = ERR44;    errorcode = ERR44;
5724    goto PCRE_UTF8_ERROR_RETURN;    goto PCRE_EARLY_ERROR_RETURN2;
5725    }    }
5726  #else  #else
5727  if ((options & PCRE_UTF8) != 0)  if ((options & PCRE_UTF8) != 0)
# Line 5133  cd->fcc = tables + fcc_offset; Line 5745  cd->fcc = tables + fcc_offset;
5745  cd->cbits = tables + cbits_offset;  cd->cbits = tables + cbits_offset;
5746  cd->ctypes = tables + ctypes_offset;  cd->ctypes = tables + ctypes_offset;
5747    
5748    /* Check for newline settings at the start of the pattern, and remember the
5749    offset for later. */
5750    
5751    if (ptr[0] == '(' && ptr[1] == '*')
5752      {
5753      int newnl = 0;
5754      if (strncmp((char *)(ptr+2), "CR)", 3) == 0)
5755        { skipatstart = 5; newnl = PCRE_NEWLINE_CR; }
5756      else if (strncmp((char *)(ptr+2), "LF)", 3)  == 0)
5757        { skipatstart = 5; newnl = PCRE_NEWLINE_LF; }
5758      else if (strncmp((char *)(ptr+2), "CRLF)", 5)  == 0)
5759        { skipatstart = 7; newnl = PCRE_NEWLINE_CR + PCRE_NEWLINE_LF; }
5760      else if (strncmp((char *)(ptr+2), "ANY)", 4) == 0)
5761        { skipatstart = 6; newnl = PCRE_NEWLINE_ANY; }
5762      else if (strncmp((char *)(ptr+2), "ANYCRLF)", 8)  == 0)
5763        { skipatstart = 10; newnl = PCRE_NEWLINE_ANYCRLF; }
5764      if (skipatstart > 0)
5765        options = (options & ~PCRE_NEWLINE_BITS) | newnl;
5766      }
5767    
5768  /* Handle different types of newline. The three bits give seven cases. The  /* Handle different types of newline. The three bits give seven cases. The
5769  current code allows for fixed one- or two-byte sequences, plus "any". */  current code allows for fixed one- or two-byte sequences, plus "any" and
5770    "anycrlf". */
5771    
5772  switch (options & (PCRE_NEWLINE_CRLF | PCRE_NEWLINE_ANY))  switch (options & PCRE_NEWLINE_BITS)
5773    {    {
5774    case 0: newline = NEWLINE; break;   /* Compile-time default */    case 0: newline = NEWLINE; break;   /* Build-time default */
5775    case PCRE_NEWLINE_CR: newline = '\r'; break;    case PCRE_NEWLINE_CR: newline = '\r'; break;
5776    case PCRE_NEWLINE_LF: newline = '\n'; break;    case PCRE_NEWLINE_LF: newline = '\n'; break;
5777    case PCRE_NEWLINE_CR+    case PCRE_NEWLINE_CR+
5778         PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break;         PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break;
5779    case PCRE_NEWLINE_ANY: newline = -1; break;    case PCRE_NEWLINE_ANY: newline = -1; break;
5780      case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
5781    default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN;    default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN;
5782    }    }
5783    
5784  if (newline < 0)  if (newline == -2)
5785      {
5786      cd->nltype = NLTYPE_ANYCRLF;
5787      }
5788    else if (newline < 0)
5789    {    {
5790    cd->nltype = NLTYPE_ANY;    cd->nltype = NLTYPE_ANY;
5791    }    }
# Line 5205  been put into the cd block so that they Line 5843  been put into the cd block so that they
5843  found within the regex right at the beginning. Bringing initial option settings  found within the regex right at the beginning. Bringing initial option settings
5844  outside can help speed up starting point checks. */  outside can help speed up starting point checks. */
5845    
5846    ptr += skipatstart;
5847  code = cworkspace;  code = cworkspace;
5848  *code = OP_BRA;  *code = OP_BRA;
5849  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,
5850    &code, &ptr, &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, &length);    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,
5851      &length);
5852  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
5853    
5854  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
# Line 5267  cd->start_code = codestart; Line 5907  cd->start_code = codestart;
5907  cd->hwm = cworkspace;  cd->hwm = cworkspace;
5908  cd->req_varyopt = 0;  cd->req_varyopt = 0;
5909  cd->nopartial = FALSE;  cd->nopartial = FALSE;
5910    cd->had_accept = FALSE;
5911    
5912  /* Set up a starting, non-extracting bracket, then compile the expression. On  /* Set up a starting, non-extracting bracket, then compile the expression. On
5913  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
5914  of the function here. */  of the function here. */
5915    
5916  ptr = (const uschar *)pattern;  ptr = (const uschar *)pattern + skipatstart;
5917  code = (uschar *)codestart;  code = (uschar *)codestart;
5918  *code = OP_BRA;  *code = OP_BRA;
5919  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,
5920    &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);    &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);
5921  re->top_bracket = cd->bracount;  re->top_bracket = cd->bracount;
5922  re->top_backref = cd->top_backref;  re->top_backref = cd->top_backref;
5923    
5924  if (cd->nopartial) re->options |= PCRE_NOPARTIAL;  if (cd->nopartial) re->options |= PCRE_NOPARTIAL;
5925    if (cd->had_accept) reqbyte = -1;   /* Must disable after (*ACCEPT) */
5926    
5927  /* 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. */
5928    
# Line 5321  if (errorcode != 0) Line 5963  if (errorcode != 0)
5963    (pcre_free)(re);    (pcre_free)(re);
5964    PCRE_EARLY_ERROR_RETURN:    PCRE_EARLY_ERROR_RETURN:
5965    *erroroffset = ptr - (const uschar *)pattern;    *erroroffset = ptr - (const uschar *)pattern;
5966  #ifdef SUPPORT_UTF8    PCRE_EARLY_ERROR_RETURN2:
   PCRE_UTF8_ERROR_RETURN:  
 #endif  
5967    *errorptr = error_texts[errorcode];    *errorptr = error_texts[errorcode];
5968    if (errorcodeptr != NULL) *errorcodeptr = errorcode;    if (errorcodeptr != NULL) *errorcodeptr = errorcode;
5969    return NULL;    return NULL;
# Line 5381  case when building a production library. Line 6021  case when building a production library.
6021  printf("Length = %d top_bracket = %d top_backref = %d\n",  printf("Length = %d top_bracket = %d top_backref = %d\n",
6022    length, re->top_bracket, re->top_backref);    length, re->top_bracket, re->top_backref);
6023    
6024  if (re->options != 0)  printf("Options=%08x\n", re->options);
   {  
   printf("%s%s%s%s%s%s%s%s%s\n",  
     ((re->options & PCRE_NOPARTIAL) != 0)? "nopartial " : "",  
     ((re->options & PCRE_ANCHORED) != 0)? "anchored " : "",  
     ((re->options & PCRE_CASELESS) != 0)? "caseless " : "",  
     ((re->options & PCRE_EXTENDED) != 0)? "extended " : "",  
     ((re->options & PCRE_MULTILINE) != 0)? "multiline " : "",  
     ((re->options & PCRE_DOTALL) != 0)? "dotall " : "",  
     ((re->options & PCRE_DOLLAR_ENDONLY) != 0)? "endonly " : "",  
     ((re->options & PCRE_EXTRA) != 0)? "extra " : "",  
     ((re->options & PCRE_UNGREEDY) != 0)? "ungreedy " : "");  
   }  
6025    
6026  if ((re->options & PCRE_FIRSTSET) != 0)  if ((re->options & PCRE_FIRSTSET) != 0)
6027    {    {
# Line 5413  if ((re->options & PCRE_REQCHSET) != 0) Line 6041  if ((re->options & PCRE_REQCHSET) != 0)
6041      else printf("Req char = \\x%02x%s\n", ch, caseless);      else printf("Req char = \\x%02x%s\n", ch, caseless);
6042    }    }
6043    
6044  pcre_printint(re, stdout);  pcre_printint(re, stdout, TRUE);
6045    
6046  /* This check is done here in the debugging case so that the code that  /* This check is done here in the debugging case so that the code that
6047  was compiled can be seen. */  was compiled can be seen. */

Legend:
Removed from v.96  
changed lines
  Added in v.227

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12