/[pcre]/code/branches/pcre16/pcre_compile.c
ViewVC logotype

Diff of /code/branches/pcre16/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

code/trunk/pcre_compile.c revision 459 by ph10, Sun Oct 4 09:21:39 2009 UTC code/branches/pcre16/pcre_compile.c revision 757 by ph10, Mon Nov 21 11:44:55 2011 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2009 University of Cambridge             Copyright (c) 1997-2011 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 53  supporting internal functions that are n Line 53  supporting internal functions that are n
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    
56  /* When DEBUG is defined, we need the pcre_printint() function, which is also  /* When PCRE_DEBUG is defined, we need the pcre_printint() function, which is
57  used by pcretest. DEBUG is not defined when building a production library. */  also used by pcretest. PCRE_DEBUG is not defined when building a production
58    library. */
59    
60  #ifdef DEBUG  #ifdef PCRE_DEBUG
61  #include "pcre_printint.src"  #include "pcre_printint.src"
62  #endif  #endif
63    
# Line 91  is 4 there is plenty of room. */ Line 92  is 4 there is plenty of room. */
92    
93  #define COMPILE_WORK_SIZE (4096)  #define COMPILE_WORK_SIZE (4096)
94    
95    /* The overrun tests check for a slightly smaller size so that they detect the
96    overrun before it actually does run off the end of the data block. */
97    
98    #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)
99    
100    
101  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
102  are simple data values; negative values are for special things like \d and so  are simple data values; negative values are for special things like \d and so
# Line 118  static const short int escapes[] = { Line 124  static const short int escapes[] = {
124       -ESC_H,                  0,       -ESC_H,                  0,
125       0,                       -ESC_K,       0,                       -ESC_K,
126       0,                       0,       0,                       0,
127       0,                       0,       -ESC_N,                  0,
128       -ESC_P,                  -ESC_Q,       -ESC_P,                  -ESC_Q,
129       -ESC_R,                  -ESC_S,       -ESC_R,                  -ESC_S,
130       0,                       0,       0,                       0,
# Line 165  static const short int escapes[] = { Line 171  static const short int escapes[] = {
171  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',
172  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,
173  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,
174  /*  D0 */   '}',     0, -ESC_K,       0,      0,     0,      0, -ESC_P,  /*  D0 */   '}',     0, -ESC_K,       0,      0,-ESC_N,      0, -ESC_P,
175  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,
176  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,
177  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,
# Line 182  string is built from string macros so th Line 188  string is built from string macros so th
188  platforms. */  platforms. */
189    
190  typedef struct verbitem {  typedef struct verbitem {
191    int   len;    int   len;                 /* Length of verb name */
192    int   op;    int   op;                  /* Op when no arg, or -1 if arg mandatory */
193      int   op_arg;              /* Op when arg present, or -1 if not allowed */
194  } verbitem;  } verbitem;
195    
196  static const char verbnames[] =  static const char verbnames[] =
197      "\0"                       /* Empty name is a shorthand for MARK */
198      STRING_MARK0
199    STRING_ACCEPT0    STRING_ACCEPT0
200    STRING_COMMIT0    STRING_COMMIT0
201    STRING_F0    STRING_F0
# Line 196  static const char verbnames[] = Line 205  static const char verbnames[] =
205    STRING_THEN;    STRING_THEN;
206    
207  static const verbitem verbs[] = {  static const verbitem verbs[] = {
208    { 6, OP_ACCEPT },    { 0, -1,        OP_MARK },
209    { 6, OP_COMMIT },    { 4, -1,        OP_MARK },
210    { 1, OP_FAIL },    { 6, OP_ACCEPT, -1 },
211    { 4, OP_FAIL },    { 6, OP_COMMIT, -1 },
212    { 5, OP_PRUNE },    { 1, OP_FAIL,   -1 },
213    { 4, OP_SKIP  },    { 4, OP_FAIL,   -1 },
214    { 4, OP_THEN  }    { 5, OP_PRUNE,  OP_PRUNE_ARG },
215      { 4, OP_SKIP,   OP_SKIP_ARG  },
216      { 4, OP_THEN,   OP_THEN_ARG  }
217  };  };
218    
219  static const int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
# Line 220  static const char posix_names[] = Line 231  static const char posix_names[] =
231    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
232    STRING_word0  STRING_xdigit;    STRING_word0  STRING_xdigit;
233    
234  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
235    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
236    
237  /* Table of class bit maps for each POSIX class. Each class is formed from a  /* Table of class bit maps for each POSIX class. Each class is formed from a
# Line 250  static const int posix_class_maps[] = { Line 261  static const int posix_class_maps[] = {
261    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
262  };  };
263    
264    /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
265    substitutes must be in the order of the names, defined above, and there are
266    both positive and negative cases. NULL means no substitute. */
267    
268    #ifdef SUPPORT_UCP
269    static const pcre_uchar literal_PNd[]  = { '\\', 'P', '{', 'N', 'd', '}', '\0' };
270    static const pcre_uchar literal_pNd[]  = { '\\', 'p', '{', 'N', 'd', '}', '\0' };
271    static const pcre_uchar literal_PXsp[] = { '\\', 'P', '{', 'X', 's', 'p', '}', '\0' };
272    static const pcre_uchar literal_pXsp[] = { '\\', 'p', '{', 'X', 's', 'p', '}', '\0' };
273    static const pcre_uchar literal_PXwd[] = { '\\', 'P', '{', 'X', 'w', 'd', '}', '\0' };
274    static const pcre_uchar literal_pXwd[] = { '\\', 'p', '{', 'X', 'w', 'd', '}', '\0' };
275    
276    static const pcre_uchar *substitutes[] = {
277      literal_PNd,           /* \D */
278      literal_pNd,           /* \d */
279      literal_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
280      literal_pXsp,          /* \s */
281      literal_PXwd,          /* \W */
282      literal_pXwd           /* \w */
283    };
284    
285    static const pcre_uchar literal_pL[] =   { '\\', 'p', '{', 'L', '}', '\0' };
286    static const pcre_uchar literal_pLl[] =  { '\\', 'p', '{', 'L', 'l', '}', '\0' };
287    static const pcre_uchar literal_pLu[] =  { '\\', 'p', '{', 'L', 'u', '}', '\0' };
288    static const pcre_uchar literal_pXan[] = { '\\', 'p', '{', 'X', 'a', 'n', '}', '\0' };
289    static const pcre_uchar literal_h[] =    { '\\', 'h', '\0' };
290    static const pcre_uchar literal_pXps[] = { '\\', 'p', '{', 'X', 'p', 's', '}', '\0' };
291    static const pcre_uchar literal_PL[] =   { '\\', 'P', '{', 'L', '}', '\0' };
292    static const pcre_uchar literal_PLl[] =  { '\\', 'P', '{', 'L', 'l', '}', '\0' };
293    static const pcre_uchar literal_PLu[] =  { '\\', 'P', '{', 'L', 'u', '}', '\0' };
294    static const pcre_uchar literal_PXan[] = { '\\', 'P', '{', 'X', 'a', 'n', '}', '\0' };
295    static const pcre_uchar literal_H[] =    { '\\', 'H', '\0' };
296    static const pcre_uchar literal_PXps[] = { '\\', 'P', '{', 'X', 'p', 's', '}', '\0' };
297    
298    static const pcre_uchar *posix_substitutes[] = {
299      literal_pL,            /* alpha */
300      literal_pLl,           /* lower */
301      literal_pLu,           /* upper */
302      literal_pXan,          /* alnum */
303      NULL,                  /* ascii */
304      literal_h,             /* blank */
305      NULL,                  /* cntrl */
306      literal_pNd,           /* digit */
307      NULL,                  /* graph */
308      NULL,                  /* print */
309      NULL,                  /* punct */
310      literal_pXps,          /* space */    /* NOTE: Xps is POSIX space */
311      literal_pXwd,          /* word */
312      NULL,                  /* xdigit */
313      /* Negated cases */
314      literal_PL,            /* ^alpha */
315      literal_PLl,           /* ^lower */
316      literal_PLu,           /* ^upper */
317      literal_PXan,          /* ^alnum */
318      NULL,                  /* ^ascii */
319      literal_H,             /* ^blank */
320      NULL,                  /* ^cntrl */
321      literal_PNd,           /* ^digit */
322      NULL,                  /* ^graph */
323      NULL,                  /* ^print */
324      NULL,                  /* ^punct */
325      literal_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
326      literal_PXwd,          /* ^word */
327      NULL                   /* ^xdigit */
328    };
329    #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
330    #endif
331    
332  #define STRING(a)  # a  #define STRING(a)  # a
333  #define XSTRING(s) STRING(s)  #define XSTRING(s) STRING(s)
# Line 262  the number of relocations needed when a Line 340  the number of relocations needed when a
340  it is now one long string. We cannot use a table of offsets, because the  it is now one long string. We cannot use a table of offsets, because the
341  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
342  simply count through to the one we want - this isn't a performance issue  simply count through to the one we want - this isn't a performance issue
343  because these strings are used only when there is a compilation error. */  because these strings are used only when there is a compilation error.
344    
345    Each substring ends with \0 to insert a null character. This includes the final
346    substring, so that the whole string ends with \0\0, which can be detected when
347    counting through. */
348    
349  static const char error_texts[] =  static const char error_texts[] =
350    "no error\0"    "no error\0"
# Line 309  static const char error_texts[] = Line 391  static const char error_texts[] =
391    /* 35 */    /* 35 */
392    "invalid condition (?(0)\0"    "invalid condition (?(0)\0"
393    "\\C not allowed in lookbehind assertion\0"    "\\C not allowed in lookbehind assertion\0"
394    "PCRE does not support \\L, \\l, \\N, \\U, or \\u\0"    "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
395    "number after (?C is > 255\0"    "number after (?C is > 255\0"
396    "closing ) for (?C expected\0"    "closing ) for (?C expected\0"
397    /* 40 */    /* 40 */
# Line 331  static const char error_texts[] = Line 413  static const char error_texts[] =
413    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
414    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
415    /* 55 */    /* 55 */
416    "repeating a DEFINE group is not allowed\0"    "repeating a DEFINE group is not allowed\0"  /** DEAD **/
417    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
418    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
419    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
420    "(*VERB) with an argument is not supported\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
421    /* 60 */    /* 60 */
422    "(*VERB) not recognized\0"    "(*VERB) not recognized\0"
423    "number is too big\0"    "number is too big\0"
# Line 343  static const char error_texts[] = Line 425  static const char error_texts[] =
425    "digit expected after (?+\0"    "digit expected after (?+\0"
426    "] is an invalid data character in JavaScript compatibility mode\0"    "] is an invalid data character in JavaScript compatibility mode\0"
427    /* 65 */    /* 65 */
428    "different names for subpatterns of the same number are not allowed";    "different names for subpatterns of the same number are not allowed\0"
429      "(*MARK) must have an argument\0"
430      "this version of PCRE is not compiled with PCRE_UCP support\0"
431      "\\c must be followed by an ASCII character\0"
432      "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
433      /* 70 */
434      "internal error: unknown opcode in find_fixedlength()\0"
435      ;
436    
437  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
438  patterns. Note that the tables in chartables are dependent on the locale, and  patterns. Note that the tables in chartables are dependent on the locale, and
# Line 480  static const unsigned char ebcdic_charta Line 568  static const unsigned char ebcdic_charta
568  /* Definition to allow mutual recursion */  /* Definition to allow mutual recursion */
569    
570  static BOOL  static BOOL
571    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,    compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
572      int *, int *, branch_chain *, compile_data *, int *);      int *, int *, branch_chain *, compile_data *, int *);
573    
574    
# Line 502  static const char * Line 590  static const char *
590  find_error_text(int n)  find_error_text(int n)
591  {  {
592  const char *s = error_texts;  const char *s = error_texts;
593  for (; n > 0; n--) while (*s++ != 0) {};  for (; n > 0; n--)
594      {
595      while (*s++ != 0) {};
596      if (*s == 0) return "Error text not found (please report)";
597      }
598  return s;  return s;
599  }  }
600    
601    
602  /*************************************************  /*************************************************
603    *            Check for counted repeat            *
604    *************************************************/
605    
606    /* This function is called when a '{' is encountered in a place where it might
607    start a quantifier. It looks ahead to see if it really is a quantifier or not.
608    It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
609    where the ddds are digits.
610    
611    Arguments:
612      p         pointer to the first char after '{'
613    
614    Returns:    TRUE or FALSE
615    */
616    
617    static BOOL
618    is_counted_repeat(const pcre_uchar *p)
619    {
620    if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
621    while ((digitab[*p] & ctype_digit) != 0) p++;
622    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
623    
624    if (*p++ != CHAR_COMMA) return FALSE;
625    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
626    
627    if ((digitab[*p++] & ctype_digit) == 0) return FALSE;
628    while ((digitab[*p] & ctype_digit) != 0) p++;
629    
630    return (*p == CHAR_RIGHT_CURLY_BRACKET);
631    }
632    
633    
634    
635    /*************************************************
636  *            Handle escapes                      *  *            Handle escapes                      *
637  *************************************************/  *************************************************/
638    
# Line 532  Returns:         zero or positive => a d Line 657  Returns:         zero or positive => a d
657  */  */
658    
659  static int  static int
660  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,
661    int options, BOOL isclass)    int options, BOOL isclass)
662  {  {
663  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
664  const uschar *ptr = *ptrptr + 1;  const pcre_uchar *ptr = *ptrptr + 1;
665  int c, i;  int c, i;
666    
667  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
# Line 563  else if ((i = escapes[c - 0x48]) != 0) Line 688  else if ((i = escapes[c - 0x48]) != 0)
688    
689  else  else
690    {    {
691    const uschar *oldptr;    const pcre_uchar *oldptr;
692    BOOL braced, negated;    BOOL braced, negated;
693    
694    switch (c)    switch (c)
# Line 573  else Line 698  else
698    
699      case CHAR_l:      case CHAR_l:
700      case CHAR_L:      case CHAR_L:
701      case CHAR_N:      *errorcodeptr = ERR37;
702        break;
703    
704      case CHAR_u:      case CHAR_u:
705        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
706          {
707          /* In JavaScript, \u must be followed by four hexadecimal numbers.
708          Otherwise it is a lowercase u letter. */
709          if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0
710               && (digitab[ptr[3]] & ctype_xdigit) != 0 && (digitab[ptr[4]] & ctype_xdigit) != 0)
711            {
712            c = 0;
713            for (i = 0; i < 4; ++i)
714              {
715              register int cc = *(++ptr);
716    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
717              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
718              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
719    #else           /* EBCDIC coding */
720              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
721              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
722    #endif
723              }
724            }
725          }
726        else
727          *errorcodeptr = ERR37;
728        break;
729    
730      case CHAR_U:      case CHAR_U:
731      *errorcodeptr = ERR37;      /* In JavaScript, \U is an uppercase U letter. */
732        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
733      break;      break;
734    
735      /* \g must be followed by one of a number of specific things:      /* In a character class, \g is just a literal "g". Outside a character
736        class, \g must be followed by one of a number of specific things:
737    
738      (1) A number, either plain or braced. If positive, it is an absolute      (1) A number, either plain or braced. If positive, it is an absolute
739      backreference. If negative, it is a relative backreference. This is a Perl      backreference. If negative, it is a relative backreference. This is a Perl
# Line 596  else Line 750  else
750      the -ESC_g code (cf \k). */      the -ESC_g code (cf \k). */
751    
752      case CHAR_g:      case CHAR_g:
753        if (isclass) break;
754      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
755        {        {
756        c = -ESC_g;        c = -ESC_g;
# Line 606  else Line 761  else
761    
762      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
763        {        {
764        const uschar *p;        const pcre_uchar *p;
765        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
766          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;
767        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
# Line 724  else Line 879  else
879      treated as a data character. */      treated as a data character. */
880    
881      case CHAR_x:      case CHAR_x:
882        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
883          {
884          /* In JavaScript, \x must be followed by two hexadecimal numbers.
885          Otherwise it is a lowercase x letter. */
886          if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0)
887            {
888            c = 0;
889            for (i = 0; i < 2; ++i)
890              {
891              register int cc = *(++ptr);
892    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
893              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
894              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
895    #else           /* EBCDIC coding */
896              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
897              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
898    #endif
899              }
900            }
901          break;
902          }
903    
904      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
905        {        {
906        const uschar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
907        int count = 0;        int count = 0;
908    
909        c = 0;        c = 0;
# Line 774  else Line 951  else
951      break;      break;
952    
953      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
954      This coding is ASCII-specific, but then the whole concept of \cx is      An error is given if the byte following \c is not an ASCII character. This
955        coding is ASCII-specific, but then the whole concept of \cx is
956      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
957    
958      case CHAR_c:      case CHAR_c:
# Line 784  else Line 962  else
962        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
963        break;        break;
964        }        }
965    #ifndef EBCDIC    /* ASCII/UTF-8 coding */
966  #ifndef EBCDIC  /* ASCII/UTF-8 coding */      if (c > 127)  /* Excludes all non-ASCII in either mode */
967          {
968          *errorcodeptr = ERR68;
969          break;
970          }
971      if (c >= CHAR_a && c <= CHAR_z) c -= 32;      if (c >= CHAR_a && c <= CHAR_z) c -= 32;
972      c ^= 0x40;      c ^= 0x40;
973  #else           /* EBCDIC coding */  #else             /* EBCDIC coding */
974      if (c >= CHAR_a && c <= CHAR_z) c += 64;      if (c >= CHAR_a && c <= CHAR_z) c += 64;
975      c ^= 0xC0;      c ^= 0xC0;
976  #endif  #endif
# Line 811  else Line 993  else
993      }      }
994    }    }
995    
996    /* Perl supports \N{name} for character names, as well as plain \N for "not
997    newline". PCRE does not support \N{name}. However, it does support
998    quantification such as \N{2,3}. */
999    
1000    if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1001         !is_counted_repeat(ptr+2))
1002      *errorcodeptr = ERR37;
1003    
1004    /* If PCRE_UCP is set, we change the values for \d etc. */
1005    
1006    if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)
1007      c -= (ESC_DU - ESC_D);
1008    
1009    /* Set the pointer to the final character before returning. */
1010    
1011  *ptrptr = ptr;  *ptrptr = ptr;
1012  return c;  return c;
1013  }  }
# Line 837  Returns:         type value from ucp_typ Line 1034  Returns:         type value from ucp_typ
1034  */  */
1035    
1036  static int  static int
1037  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
1038  {  {
1039  int c, i, bot, top;  int c, i, bot, top;
1040  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1041  char name[32];  char name[32];
1042    
1043  c = *(++ptr);  c = *(++ptr);
# Line 911  return -1; Line 1108  return -1;
1108    
1109    
1110  /*************************************************  /*************************************************
 *            Check for counted repeat            *  
 *************************************************/  
   
 /* This function is called when a '{' is encountered in a place where it might  
 start a quantifier. It looks ahead to see if it really is a quantifier or not.  
 It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}  
 where the ddds are digits.  
   
 Arguments:  
   p         pointer to the first char after '{'  
   
 Returns:    TRUE or FALSE  
 */  
   
 static BOOL  
 is_counted_repeat(const uschar *p)  
 {  
 if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  
 while ((digitab[*p] & ctype_digit) != 0) p++;  
 if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  
   
 if (*p++ != CHAR_COMMA) return FALSE;  
 if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  
   
 if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  
 while ((digitab[*p] & ctype_digit) != 0) p++;  
   
 return (*p == CHAR_RIGHT_CURLY_BRACKET);  
 }  
   
   
   
 /*************************************************  
1111  *         Read repeat counts                     *  *         Read repeat counts                     *
1112  *************************************************/  *************************************************/
1113    
# Line 962  Returns:         pointer to '}' on succe Line 1126  Returns:         pointer to '}' on succe
1126                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1127  */  */
1128    
1129  static const uschar *  static const pcre_uchar *
1130  read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)  read_repeat_counts(const pcre_uchar *p, int *minp, int *maxp, int *errorcodeptr)
1131  {  {
1132  int min = 0;  int min = 0;
1133  int max = -1;  int max = -1;
# Line 1019  top-level call starts at the beginning o Line 1183  top-level call starts at the beginning o
1183  start at a parenthesis. It scans along a pattern's text looking for capturing  start at a parenthesis. It scans along a pattern's text looking for capturing
1184  subpatterns, and counting them. If it finds a named pattern that matches the  subpatterns, and counting them. If it finds a named pattern that matches the
1185  name it is given, it returns its number. Alternatively, if the name is NULL, it  name it is given, it returns its number. Alternatively, if the name is NULL, it
1186  returns when it reaches a given numbered subpattern. We know that if (?P< is  returns when it reaches a given numbered subpattern. Recursion is used to keep
1187  encountered, the name will be terminated by '>' because that is checked in the  track of subpatterns that reset the capturing group numbers - the (?| feature.
1188  first pass. Recursion is used to keep track of subpatterns that reset the  
1189  capturing group numbers - the (?| feature.  This function was originally called only from the second pass, in which we know
1190    that if (?< or (?' or (?P< is encountered, the name will be correctly
1191    terminated because that is checked in the first pass. There is now one call to
1192    this function in the first pass, to check for a recursive back reference by
1193    name (so that we can make the whole group atomic). In this case, we need check
1194    only up to the current position in the pattern, and that is still OK because
1195    and previous occurrences will have been checked. To make this work, the test
1196    for "end of pattern" is a check against cd->end_pattern in the main loop,
1197    instead of looking for a binary zero. This means that the special first-pass
1198    call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1199    processing items within the loop are OK, because afterwards the main loop will
1200    terminate.)
1201    
1202  Arguments:  Arguments:
1203    ptrptr       address of the current character pointer (updated)    ptrptr       address of the current character pointer (updated)
# Line 1030  Arguments: Line 1205  Arguments:
1205    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1206    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1207    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1208      utf8         TRUE if we are in UTF-8 mode
1209    count        pointer to the current capturing subpattern number (updated)    count        pointer to the current capturing subpattern number (updated)
1210    
1211  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
1212  */  */
1213    
1214  static int  static int
1215  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,
1216    BOOL xmode, int *count)    BOOL xmode, BOOL utf8, int *count)
1217  {  {
1218  uschar *ptr = *ptrptr;  pcre_uchar *ptr = *ptrptr;
1219  int start_count = *count;  int start_count = *count;
1220  int hwm_count = start_count;  int hwm_count = start_count;
1221  BOOL dup_parens = FALSE;  BOOL dup_parens = FALSE;
# Line 1049  dealing with. The very first call may no Line 1225  dealing with. The very first call may no
1225    
1226  if (ptr[0] == CHAR_LEFT_PARENTHESIS)  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1227    {    {
1228    if (ptr[1] == CHAR_QUESTION_MARK &&    /* Handle specials such as (*SKIP) or (*UTF8) etc. */
1229        ptr[2] == CHAR_VERTICAL_LINE)  
1230      if (ptr[1] == CHAR_ASTERISK) ptr += 2;
1231    
1232      /* Handle a normal, unnamed capturing parenthesis. */
1233    
1234      else if (ptr[1] != CHAR_QUESTION_MARK)
1235        {
1236        *count += 1;
1237        if (name == NULL && *count == lorn) return *count;
1238        ptr++;
1239        }
1240    
1241      /* All cases now have (? at the start. Remember when we are in a group
1242      where the parenthesis numbers are duplicated. */
1243    
1244      else if (ptr[2] == CHAR_VERTICAL_LINE)
1245      {      {
1246      ptr += 3;      ptr += 3;
1247      dup_parens = TRUE;      dup_parens = TRUE;
1248      }      }
1249    
1250    /* Handle a normal, unnamed capturing parenthesis */    /* Handle comments; all characters are allowed until a ket is reached. */
1251    
1252    else if (ptr[1] != CHAR_QUESTION_MARK && ptr[1] != CHAR_ASTERISK)    else if (ptr[2] == CHAR_NUMBER_SIGN)
1253      {      {
1254      *count += 1;      for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;
1255      if (name == NULL && *count == lorn) return *count;      goto FAIL_EXIT;
     ptr++;  
1256      }      }
1257    
1258    /* Handle a condition. If it is an assertion, just carry on so that it    /* Handle a condition. If it is an assertion, just carry on so that it
1259    is processed as normal. If not, skip to the closing parenthesis of the    is processed as normal. If not, skip to the closing parenthesis of the
1260    condition (there can't be any nested parens. */    condition (there can't be any nested parens). */
1261    
1262    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
1263      {      {
# Line 1079  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1269  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1269        }        }
1270      }      }
1271    
1272    /* We have either (? or (* and not a condition */    /* Start with (? but not a condition. */
1273    
1274    else    else
1275      {      {
# Line 1092  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1282  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1282          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1283        {        {
1284        int term;        int term;
1285        const uschar *thisname;        const pcre_uchar *thisname;
1286        *count += 1;        *count += 1;
1287        if (name == NULL && *count == lorn) return *count;        if (name == NULL && *count == lorn) return *count;
1288        term = *ptr++;        term = *ptr++;
# Line 1102  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1292  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1292        if (name != NULL && lorn == ptr - thisname &&        if (name != NULL && lorn == ptr - thisname &&
1293            strncmp((const char *)name, (const char *)thisname, lorn) == 0)            strncmp((const char *)name, (const char *)thisname, lorn) == 0)
1294          return *count;          return *count;
1295        term++;        term++;
1296        }        }
1297      }      }
1298    }    }
1299    
1300  /* Past any initial parenthesis handling, scan for parentheses or vertical  /* Past any initial parenthesis handling, scan for parentheses or vertical
1301  bars. */  bars. Stop if we get to cd->end_pattern. Note that this is important for the
1302    first-pass call when this value is temporarily adjusted to stop at the current
1303    position. So DO NOT change this to a test for binary zero. */
1304    
1305  for (; *ptr != 0; ptr++)  for (; ptr < cd->end_pattern; ptr++)
1306    {    {
1307    /* Skip over backslashed characters and also entire \Q...\E */    /* Skip over backslashed characters and also entire \Q...\E */
1308    
# Line 1148  for (; *ptr != 0; ptr++) Line 1340  for (; *ptr != 0; ptr++)
1340            break;            break;
1341          }          }
1342        else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)        else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1343          {          {
1344          negate_class = TRUE;          negate_class = TRUE;
1345          ptr++;          ptr++;
1346          }          }
1347        else break;        else break;
1348        }        }
1349    
# Line 1184  for (; *ptr != 0; ptr++) Line 1376  for (; *ptr != 0; ptr++)
1376    
1377    if (xmode && *ptr == CHAR_NUMBER_SIGN)    if (xmode && *ptr == CHAR_NUMBER_SIGN)
1378      {      {
1379      while (*(++ptr) != 0 && *ptr != CHAR_NL) {};      ptr++;
1380        while (*ptr != 0)
1381          {
1382          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1383          ptr++;
1384    #ifdef SUPPORT_UTF8
1385          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
1386    #endif
1387          }
1388      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1389      continue;      continue;
1390      }      }
# Line 1193  for (; *ptr != 0; ptr++) Line 1393  for (; *ptr != 0; ptr++)
1393    
1394    if (*ptr == CHAR_LEFT_PARENTHESIS)    if (*ptr == CHAR_LEFT_PARENTHESIS)
1395      {      {
1396      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, count);      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);
1397      if (rc > 0) return rc;      if (rc > 0) return rc;
1398      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1399      }      }
# Line 1201  for (; *ptr != 0; ptr++) Line 1401  for (; *ptr != 0; ptr++)
1401    else if (*ptr == CHAR_RIGHT_PARENTHESIS)    else if (*ptr == CHAR_RIGHT_PARENTHESIS)
1402      {      {
1403      if (dup_parens && *count < hwm_count) *count = hwm_count;      if (dup_parens && *count < hwm_count) *count = hwm_count;
1404      *ptrptr = ptr;      goto FAIL_EXIT;
     return -1;  
1405      }      }
1406    
1407    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
# Line 1240  Arguments: Line 1439  Arguments:
1439    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1440    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1441    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1442      utf8         TRUE if we are in UTF-8 mode
1443    
1444  Returns:       the number of the found subpattern, or -1 if not found  Returns:       the number of the found subpattern, or -1 if not found
1445  */  */
1446    
1447  static int  static int
1448  find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode)  find_parens(compile_data *cd, const pcre_uchar *name, int lorn, BOOL xmode,
1449      BOOL utf8)
1450  {  {
1451  uschar *ptr = (uschar *)cd->start_pattern;  pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1452  int count = 0;  int count = 0;
1453  int rc;  int rc;
1454    
# Line 1258  matching closing parens. That is why we Line 1459  matching closing parens. That is why we
1459    
1460  for (;;)  for (;;)
1461    {    {
1462    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, &count);    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);
1463    if (rc > 0 || *ptr++ == 0) break;    if (rc > 0 || *ptr++ == 0) break;
1464    }    }
1465    
# Line 1274  return rc; Line 1475  return rc;
1475    
1476  /* This is called by several functions that scan a compiled expression looking  /* This is called by several functions that scan a compiled expression looking
1477  for a fixed first character, or an anchoring op code etc. It skips over things  for a fixed first character, or an anchoring op code etc. It skips over things
1478  that do not influence this. For some calls, a change of option is important.  that do not influence this. For some calls, it makes sense to skip negative
1479  For some calls, it makes sense to skip negative forward and all backward  forward and all backward assertions, and also the \b assertion; for others it
1480  assertions, and also the \b assertion; for others it does not.  does not.
1481    
1482  Arguments:  Arguments:
1483    code         pointer to the start of the group    code         pointer to the start of the group
   options      pointer to external options  
   optbit       the option bit whose changing is significant, or  
                  zero if none are  
1484    skipassert   TRUE if certain assertions are to be skipped    skipassert   TRUE if certain assertions are to be skipped
1485    
1486  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1487  */  */
1488    
1489  static const uschar*  static const pcre_uchar*
1490  first_significant_code(const uschar *code, int *options, int optbit,  first_significant_code(const pcre_uchar *code, BOOL skipassert)
   BOOL skipassert)  
1491  {  {
1492  for (;;)  for (;;)
1493    {    {
1494    switch ((int)*code)    switch ((int)*code)
1495      {      {
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
1496      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1497      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1498      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
# Line 1340  for (;;) Line 1531  for (;;)
1531    
1532  /* Scan a branch and compute the fixed length of subject that will match it,  /* Scan a branch and compute the fixed length of subject that will match it,
1533  if the length is fixed. This is needed for dealing with backward assertions.  if the length is fixed. This is needed for dealing with backward assertions.
1534  In UTF8 mode, the result is in characters rather than bytes. The branch is  In UTF8 mode, the result is in characters rather than bytes. The branch is
1535  temporarily terminated with OP_END when this function is called.  temporarily terminated with OP_END when this function is called.
1536    
1537  This function is called when a backward assertion is encountered, so that if it  This function is called when a backward assertion is encountered, so that if it
1538  fails, the error message can point to the correct place in the pattern.  fails, the error message can point to the correct place in the pattern.
1539  However, we cannot do this when the assertion contains subroutine calls,  However, we cannot do this when the assertion contains subroutine calls,
1540  because they can be forward references. We solve this by remembering this case  because they can be forward references. We solve this by remembering this case
1541  and doing the check at the end; a flag specifies which mode we are running in.  and doing the check at the end; a flag specifies which mode we are running in.
1542    
1543  Arguments:  Arguments:
1544    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1545    options  the compiling options    utf8     TRUE in UTF-8 mode
1546    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1547    cd       the "compile data" structure    cd       the "compile data" structure
1548    
1549  Returns:   the fixed length,  Returns:   the fixed length,
1550               or -1 if there is no fixed length,               or -1 if there is no fixed length,
1551               or -2 if \C was encountered               or -2 if \C was encountered (in UTF-8 mode only)
1552               or -3 if an OP_RECURSE item was encountered and atend is FALSE               or -3 if an OP_RECURSE item was encountered and atend is FALSE
1553                 or -4 if an unknown opcode was encountered (internal error)
1554  */  */
1555    
1556  static int  static int
1557  find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)  find_fixedlength(pcre_uchar *code, BOOL utf8, BOOL atend, compile_data *cd)
1558  {  {
1559  int length = -1;  int length = -1;
1560    
1561  register int branchlength = 0;  register int branchlength = 0;
1562  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1563    
1564  /* Scan along the opcodes for this branch. If we get to the end of the  /* Scan along the opcodes for this branch. If we get to the end of the
1565  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1375  branch, check the length against that of Line 1567  branch, check the length against that of
1567  for (;;)  for (;;)
1568    {    {
1569    int d;    int d;
1570    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1571    register int op = *cc;    register int op = *cc;
1572    switch (op)    switch (op)
1573      {      {
1574        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1575        OP_BRA (normal non-capturing bracket) because the other variants of these
1576        opcodes are all concerned with unlimited repeated groups, which of course
1577        are not of fixed length. */
1578    
1579      case OP_CBRA:      case OP_CBRA:
1580      case OP_BRA:      case OP_BRA:
1581      case OP_ONCE:      case OP_ONCE:
1582        case OP_ONCE_NC:
1583      case OP_COND:      case OP_COND:
1584      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), utf8, atend, cd);
1585      if (d < 0) return d;      if (d < 0) return d;
1586      branchlength += d;      branchlength += d;
1587      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1588      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1589      break;      break;
1590    
1591      /* Reached end of a branch; if it's a ket it is the end of a nested      /* Reached end of a branch; if it's a ket it is the end of a nested call.
1592      call. If it's ALT it is an alternation in a nested call. If it is      If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1593      END it's the end of the outer call. All can be handled by the same code. */      an ALT. If it is END it's the end of the outer call. All can be handled by
1594        the same code. Note that we must not include the OP_KETRxxx opcodes here,
1595        because they all imply an unlimited repeat. */
1596    
1597      case OP_ALT:      case OP_ALT:
1598      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1599      case OP_END:      case OP_END:
1600        case OP_ACCEPT:
1601        case OP_ASSERT_ACCEPT:
1602      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1603        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
1604      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
1605      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1606      branchlength = 0;      branchlength = 0;
1607      break;      break;
1608    
1609      /* A true recursion implies not fixed length, but a subroutine call may      /* A true recursion implies not fixed length, but a subroutine call may
1610      be OK. If the subroutine is a forward reference, we can't deal with      be OK. If the subroutine is a forward reference, we can't deal with
1611      it until the end of the pattern, so return -3. */      it until the end of the pattern, so return -3. */
1612    
1613      case OP_RECURSE:      case OP_RECURSE:
1614      if (!atend) return -3;      if (!atend) return -3;
1615      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1616      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1617      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1618      d = find_fixedlength(cs + 2, options, atend, cd);      d = find_fixedlength(cs + 2, utf8, atend, cd);
1619      if (d < 0) return d;      if (d < 0) return d;
1620      branchlength += d;      branchlength += d;
1621      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1622      break;      break;
1623    
1624      /* Skip over assertive subpatterns */      /* Skip over assertive subpatterns */
1625    
# Line 1432  for (;;) Line 1632  for (;;)
1632    
1633      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1634    
1635      case OP_REVERSE:      case OP_MARK:
1636        case OP_PRUNE_ARG:
1637        case OP_SKIP_ARG:
1638        case OP_THEN_ARG:
1639        cc += cc[1] + _pcre_OP_lengths[*cc];
1640        break;
1641    
1642        case OP_CALLOUT:
1643        case OP_CIRC:
1644        case OP_CIRCM:
1645        case OP_CLOSE:
1646        case OP_COMMIT:
1647      case OP_CREF:      case OP_CREF:
     case OP_NCREF:  
     case OP_RREF:  
     case OP_NRREF:  
1648      case OP_DEF:      case OP_DEF:
1649      case OP_OPT:      case OP_DOLL:
1650      case OP_CALLOUT:      case OP_DOLLM:
     case OP_SOD:  
     case OP_SOM:  
1651      case OP_EOD:      case OP_EOD:
1652      case OP_EODN:      case OP_EODN:
1653      case OP_CIRC:      case OP_FAIL:
1654      case OP_DOLL:      case OP_NCREF:
1655        case OP_NRREF:
1656      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1657        case OP_PRUNE:
1658        case OP_REVERSE:
1659        case OP_RREF:
1660        case OP_SET_SOM:
1661        case OP_SKIP:
1662        case OP_SOD:
1663        case OP_SOM:
1664        case OP_THEN:
1665      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1666      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
1667      break;      break;
# Line 1454  for (;;) Line 1669  for (;;)
1669      /* Handle literal characters */      /* Handle literal characters */
1670    
1671      case OP_CHAR:      case OP_CHAR:
1672      case OP_CHARNC:      case OP_CHARI:
1673      case OP_NOT:      case OP_NOT:
1674        case OP_NOTI:
1675      branchlength++;      branchlength++;
1676      cc += 2;      cc += 2;
1677  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1678      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1679  #endif  #endif
1680      break;      break;
1681    
# Line 1468  for (;;) Line 1683  for (;;)
1683      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
1684    
1685      case OP_EXACT:      case OP_EXACT:
1686        case OP_EXACTI:
1687        case OP_NOTEXACT:
1688        case OP_NOTEXACTI:
1689      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1690      cc += 4;      cc += 4;
1691  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1692      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1693  #endif  #endif
1694      break;      break;
1695    
# Line 1489  for (;;) Line 1706  for (;;)
1706      cc += 2;      cc += 2;
1707      /* Fall through */      /* Fall through */
1708    
1709        case OP_HSPACE:
1710        case OP_VSPACE:
1711        case OP_NOT_HSPACE:
1712        case OP_NOT_VSPACE:
1713      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1714      case OP_DIGIT:      case OP_DIGIT:
1715      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
# Line 1501  for (;;) Line 1722  for (;;)
1722      cc++;      cc++;
1723      break;      break;
1724    
1725      /* The single-byte matcher isn't allowed */      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1726        otherwise \C is coded as OP_ALLANY. */
1727    
1728      case OP_ANYBYTE:      case OP_ANYBYTE:
1729      return -2;      return -2;
# Line 1520  for (;;) Line 1742  for (;;)
1742    
1743      switch (*cc)      switch (*cc)
1744        {        {
1745          case OP_CRPLUS:
1746          case OP_CRMINPLUS:
1747        case OP_CRSTAR:        case OP_CRSTAR:
1748        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1749        case OP_CRQUERY:        case OP_CRQUERY:
# Line 1540  for (;;) Line 1764  for (;;)
1764    
1765      /* Anything else is variable length */      /* Anything else is variable length */
1766    
1767      default:      case OP_ANYNL:
1768        case OP_BRAMINZERO:
1769        case OP_BRAPOS:
1770        case OP_BRAPOSZERO:
1771        case OP_BRAZERO:
1772        case OP_CBRAPOS:
1773        case OP_EXTUNI:
1774        case OP_KETRMAX:
1775        case OP_KETRMIN:
1776        case OP_KETRPOS:
1777        case OP_MINPLUS:
1778        case OP_MINPLUSI:
1779        case OP_MINQUERY:
1780        case OP_MINQUERYI:
1781        case OP_MINSTAR:
1782        case OP_MINSTARI:
1783        case OP_MINUPTO:
1784        case OP_MINUPTOI:
1785        case OP_NOTMINPLUS:
1786        case OP_NOTMINPLUSI:
1787        case OP_NOTMINQUERY:
1788        case OP_NOTMINQUERYI:
1789        case OP_NOTMINSTAR:
1790        case OP_NOTMINSTARI:
1791        case OP_NOTMINUPTO:
1792        case OP_NOTMINUPTOI:
1793        case OP_NOTPLUS:
1794        case OP_NOTPLUSI:
1795        case OP_NOTPOSPLUS:
1796        case OP_NOTPOSPLUSI:
1797        case OP_NOTPOSQUERY:
1798        case OP_NOTPOSQUERYI:
1799        case OP_NOTPOSSTAR:
1800        case OP_NOTPOSSTARI:
1801        case OP_NOTPOSUPTO:
1802        case OP_NOTPOSUPTOI:
1803        case OP_NOTQUERY:
1804        case OP_NOTQUERYI:
1805        case OP_NOTSTAR:
1806        case OP_NOTSTARI:
1807        case OP_NOTUPTO:
1808        case OP_NOTUPTOI:
1809        case OP_PLUS:
1810        case OP_PLUSI:
1811        case OP_POSPLUS:
1812        case OP_POSPLUSI:
1813        case OP_POSQUERY:
1814        case OP_POSQUERYI:
1815        case OP_POSSTAR:
1816        case OP_POSSTARI:
1817        case OP_POSUPTO:
1818        case OP_POSUPTOI:
1819        case OP_QUERY:
1820        case OP_QUERYI:
1821        case OP_REF:
1822        case OP_REFI:
1823        case OP_SBRA:
1824        case OP_SBRAPOS:
1825        case OP_SCBRA:
1826        case OP_SCBRAPOS:
1827        case OP_SCOND:
1828        case OP_SKIPZERO:
1829        case OP_STAR:
1830        case OP_STARI:
1831        case OP_TYPEMINPLUS:
1832        case OP_TYPEMINQUERY:
1833        case OP_TYPEMINSTAR:
1834        case OP_TYPEMINUPTO:
1835        case OP_TYPEPLUS:
1836        case OP_TYPEPOSPLUS:
1837        case OP_TYPEPOSQUERY:
1838        case OP_TYPEPOSSTAR:
1839        case OP_TYPEPOSUPTO:
1840        case OP_TYPEQUERY:
1841        case OP_TYPESTAR:
1842        case OP_TYPEUPTO:
1843        case OP_UPTO:
1844        case OP_UPTOI:
1845      return -1;      return -1;
1846    
1847        /* Catch unrecognized opcodes so that when new ones are added they
1848        are not forgotten, as has happened in the past. */
1849    
1850        default:
1851        return -4;
1852      }      }
1853    }    }
1854  /* Control never gets here */  /* Control never gets here */
# Line 1556  for (;;) Line 1863  for (;;)
1863    
1864  /* This little function scans through a compiled pattern until it finds a  /* This little function scans through a compiled pattern until it finds a
1865  capturing bracket with the given number, or, if the number is negative, an  capturing bracket with the given number, or, if the number is negative, an
1866  instance of OP_REVERSE for a lookbehind. The function is global in the C sense  instance of OP_REVERSE for a lookbehind. The function is global in the C sense
1867  so that it can be called from pcre_study() when finding the minimum matching  so that it can be called from pcre_study() when finding the minimum matching
1868  length.  length.
1869    
1870  Arguments:  Arguments:
# Line 1568  Arguments: Line 1875  Arguments:
1875  Returns:      pointer to the opcode for the bracket, or NULL if not found  Returns:      pointer to the opcode for the bracket, or NULL if not found
1876  */  */
1877    
1878  const uschar *  const pcre_uchar *
1879  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  _pcre_find_bracket(const pcre_uchar *code, BOOL utf8, int number)
1880  {  {
1881  for (;;)  for (;;)
1882    {    {
1883    register int c = *code;    register int c = *code;
1884    
1885    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1886    
1887    /* XCLASS is used for classes that cannot be represented just by a bit    /* XCLASS is used for classes that cannot be represented just by a bit
# Line 1581  for (;;) Line 1889  for (;;)
1889    the table is zero; the actual length is stored in the compiled code. */    the table is zero; the actual length is stored in the compiled code. */
1890    
1891    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
1892    
1893    /* Handle recursion */    /* Handle recursion */
1894    
1895    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
1896      {      {
1897      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
1898      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1899      }      }
1900    
1901    /* Handle capturing bracket */    /* Handle capturing bracket */
1902    
1903    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1904               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1905      {      {
1906      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
1907      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1908      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1909      }      }
1910    
1911    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
1912    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
1913    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1914      must add in its length. */
1915    
1916    else    else
1917      {      {
# Line 1625  for (;;) Line 1935  for (;;)
1935        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1936        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1937        break;        break;
1938    
1939          case OP_MARK:
1940          case OP_PRUNE_ARG:
1941          case OP_SKIP_ARG:
1942          code += code[1];
1943          break;
1944    
1945          case OP_THEN_ARG:
1946          code += code[1];
1947          break;
1948        }        }
1949    
1950      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1639  for (;;) Line 1959  for (;;)
1959      if (utf8) switch(c)      if (utf8) switch(c)
1960        {        {
1961        case OP_CHAR:        case OP_CHAR:
1962        case OP_CHARNC:        case OP_CHARI:
1963        case OP_EXACT:        case OP_EXACT:
1964          case OP_EXACTI:
1965        case OP_UPTO:        case OP_UPTO:
1966          case OP_UPTOI:
1967        case OP_MINUPTO:        case OP_MINUPTO:
1968          case OP_MINUPTOI:
1969        case OP_POSUPTO:        case OP_POSUPTO:
1970          case OP_POSUPTOI:
1971        case OP_STAR:        case OP_STAR:
1972          case OP_STARI:
1973        case OP_MINSTAR:        case OP_MINSTAR:
1974          case OP_MINSTARI:
1975        case OP_POSSTAR:        case OP_POSSTAR:
1976          case OP_POSSTARI:
1977        case OP_PLUS:        case OP_PLUS:
1978          case OP_PLUSI:
1979        case OP_MINPLUS:        case OP_MINPLUS:
1980          case OP_MINPLUSI:
1981        case OP_POSPLUS:        case OP_POSPLUS:
1982          case OP_POSPLUSI:
1983        case OP_QUERY:        case OP_QUERY:
1984          case OP_QUERYI:
1985        case OP_MINQUERY:        case OP_MINQUERY:
1986          case OP_MINQUERYI:
1987        case OP_POSQUERY:        case OP_POSQUERY:
1988          case OP_POSQUERYI:
1989        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1990        break;        break;
1991        }        }
# Line 1679  Arguments: Line 2012  Arguments:
2012  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found
2013  */  */
2014    
2015  static const uschar *  static const pcre_uchar *
2016  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf8)
2017  {  {
2018  for (;;)  for (;;)
2019    {    {
# Line 1696  for (;;) Line 2029  for (;;)
2029    
2030    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
2031    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
2032    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2033      must add in its length. */
2034    
2035    else    else
2036      {      {
# Line 1720  for (;;) Line 2054  for (;;)
2054        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2055        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
2056        break;        break;
2057    
2058          case OP_MARK:
2059          case OP_PRUNE_ARG:
2060          case OP_SKIP_ARG:
2061          code += code[1];
2062          break;
2063    
2064          case OP_THEN_ARG:
2065          code += code[1];
2066          break;
2067        }        }
2068    
2069      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1734  for (;;) Line 2078  for (;;)
2078      if (utf8) switch(c)      if (utf8) switch(c)
2079        {        {
2080        case OP_CHAR:        case OP_CHAR:
2081        case OP_CHARNC:        case OP_CHARI:
2082        case OP_EXACT:        case OP_EXACT:
2083          case OP_EXACTI:
2084        case OP_UPTO:        case OP_UPTO:
2085          case OP_UPTOI:
2086        case OP_MINUPTO:        case OP_MINUPTO:
2087          case OP_MINUPTOI:
2088        case OP_POSUPTO:        case OP_POSUPTO:
2089          case OP_POSUPTOI:
2090        case OP_STAR:        case OP_STAR:
2091          case OP_STARI:
2092        case OP_MINSTAR:        case OP_MINSTAR:
2093          case OP_MINSTARI:
2094        case OP_POSSTAR:        case OP_POSSTAR:
2095          case OP_POSSTARI:
2096        case OP_PLUS:        case OP_PLUS:
2097          case OP_PLUSI:
2098        case OP_MINPLUS:        case OP_MINPLUS:
2099          case OP_MINPLUSI:
2100        case OP_POSPLUS:        case OP_POSPLUS:
2101          case OP_POSPLUSI:
2102        case OP_QUERY:        case OP_QUERY:
2103          case OP_QUERYI:
2104        case OP_MINQUERY:        case OP_MINQUERY:
2105          case OP_MINQUERYI:
2106        case OP_POSQUERY:        case OP_POSQUERY:
2107          case OP_POSQUERYI:
2108        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
2109        break;        break;
2110        }        }
# Line 1776  Arguments: Line 2133  Arguments:
2133    code        points to start of search    code        points to start of search
2134    endcode     points to where to stop    endcode     points to where to stop
2135    utf8        TRUE if in UTF8 mode    utf8        TRUE if in UTF8 mode
2136      cd          contains pointers to tables etc.
2137    
2138  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2139  */  */
2140    
2141  static BOOL  static BOOL
2142  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2143      BOOL utf8, compile_data *cd)
2144  {  {
2145  register int c;  register int c;
2146  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE);
2147       code < endcode;       code < endcode;
2148       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + _pcre_OP_lengths[c], TRUE))
2149    {    {
2150    const uschar *ccode;    const pcre_uchar *ccode;
2151    
2152    c = *code;    c = *code;
2153    
# Line 1802  for (code = first_significant_code(code Line 2161  for (code = first_significant_code(code
2161      continue;      continue;
2162      }      }
2163    
2164      /* For a recursion/subroutine call, if its end has been reached, which
2165      implies a backward reference subroutine call, we can scan it. If it's a
2166      forward reference subroutine call, we can't. To detect forward reference
2167      we have to scan up the list that is kept in the workspace. This function is
2168      called only when doing the real compile, not during the pre-compile that
2169      measures the size of the compiled pattern. */
2170    
2171      if (c == OP_RECURSE)
2172        {
2173        const pcre_uchar *scode;
2174        BOOL empty_branch;
2175    
2176        /* Test for forward reference */
2177    
2178        for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
2179          if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;
2180    
2181        /* Not a forward reference, test for completed backward reference */
2182    
2183        empty_branch = FALSE;
2184        scode = cd->start_code + GET(code, 1);
2185        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2186    
2187        /* Completed backwards reference */
2188    
2189        do
2190          {
2191          if (could_be_empty_branch(scode, endcode, utf8, cd))
2192            {
2193            empty_branch = TRUE;
2194            break;
2195            }
2196          scode += GET(scode, 1);
2197          }
2198        while (*scode == OP_ALT);
2199    
2200        if (!empty_branch) return FALSE;  /* All branches are non-empty */
2201        continue;
2202        }
2203    
2204    /* Groups with zero repeats can of course be empty; skip them. */    /* Groups with zero repeats can of course be empty; skip them. */
2205    
2206    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO)    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2207          c == OP_BRAPOSZERO)
2208      {      {
2209      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
2210      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
# Line 1812  for (code = first_significant_code(code Line 2212  for (code = first_significant_code(code
2212      continue;      continue;
2213      }      }
2214    
2215      /* A nested group that is already marked as "could be empty" can just be
2216      skipped. */
2217    
2218      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2219          c == OP_SCBRA || c == OP_SCBRAPOS)
2220        {
2221        do code += GET(code, 1); while (*code == OP_ALT);
2222        c = *code;
2223        continue;
2224        }
2225    
2226    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
2227    
2228    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA  || c == OP_BRAPOS ||
2229          c == OP_CBRA || c == OP_CBRAPOS ||
2230          c == OP_ONCE || c == OP_ONCE_NC ||
2231          c == OP_COND)
2232      {      {
2233      BOOL empty_branch;      BOOL empty_branch;
2234      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 1830  for (code = first_significant_code(code Line 2244  for (code = first_significant_code(code
2244        empty_branch = FALSE;        empty_branch = FALSE;
2245        do        do
2246          {          {
2247          if (!empty_branch && could_be_empty_branch(code, endcode, utf8))          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))
2248            empty_branch = TRUE;            empty_branch = TRUE;
2249          code += GET(code, 1);          code += GET(code, 1);
2250          }          }
# Line 1901  for (code = first_significant_code(code Line 2315  for (code = first_significant_code(code
2315      case OP_ALLANY:      case OP_ALLANY:
2316      case OP_ANYBYTE:      case OP_ANYBYTE:
2317      case OP_CHAR:      case OP_CHAR:
2318      case OP_CHARNC:      case OP_CHARI:
2319      case OP_NOT:      case OP_NOT:
2320        case OP_NOTI:
2321      case OP_PLUS:      case OP_PLUS:
2322      case OP_MINPLUS:      case OP_MINPLUS:
2323      case OP_POSPLUS:      case OP_POSPLUS:
# Line 1942  for (code = first_significant_code(code Line 2357  for (code = first_significant_code(code
2357      case OP_KET:      case OP_KET:
2358      case OP_KETRMAX:      case OP_KETRMAX:
2359      case OP_KETRMIN:      case OP_KETRMIN:
2360        case OP_KETRPOS:
2361      case OP_ALT:      case OP_ALT:
2362      return TRUE;      return TRUE;
2363    
# Line 1950  for (code = first_significant_code(code Line 2366  for (code = first_significant_code(code
2366    
2367  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2368      case OP_STAR:      case OP_STAR:
2369        case OP_STARI:
2370      case OP_MINSTAR:      case OP_MINSTAR:
2371        case OP_MINSTARI:
2372      case OP_POSSTAR:      case OP_POSSTAR:
2373        case OP_POSSTARI:
2374      case OP_QUERY:      case OP_QUERY:
2375        case OP_QUERYI:
2376      case OP_MINQUERY:      case OP_MINQUERY:
2377        case OP_MINQUERYI:
2378      case OP_POSQUERY:      case OP_POSQUERY:
2379        case OP_POSQUERYI:
2380      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];
2381      break;      break;
2382    
2383      case OP_UPTO:      case OP_UPTO:
2384        case OP_UPTOI:
2385      case OP_MINUPTO:      case OP_MINUPTO:
2386        case OP_MINUPTOI:
2387      case OP_POSUPTO:      case OP_POSUPTO:
2388        case OP_POSUPTOI:
2389      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];
2390      break;      break;
2391  #endif  #endif
2392    
2393        /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2394        string. */
2395    
2396        case OP_MARK:
2397        case OP_PRUNE_ARG:
2398        case OP_SKIP_ARG:
2399        code += code[1];
2400        break;
2401    
2402        case OP_THEN_ARG:
2403        code += code[1];
2404        break;
2405    
2406        /* None of the remaining opcodes are required to match a character. */
2407    
2408        default:
2409        break;
2410      }      }
2411    }    }
2412    
# Line 1980  return TRUE; Line 2423  return TRUE;
2423  the current branch of the current pattern to see if it could match the empty  the current branch of the current pattern to see if it could match the empty
2424  string. If it could, we must look outwards for branches at other levels,  string. If it could, we must look outwards for branches at other levels,
2425  stopping when we pass beyond the bracket which is the subject of the recursion.  stopping when we pass beyond the bracket which is the subject of the recursion.
2426    This function is called only during the real compile, not during the
2427    pre-compile.
2428    
2429  Arguments:  Arguments:
2430    code        points to start of the recursion    code        points to start of the recursion
2431    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2432    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2433    utf8        TRUE if in UTF-8 mode    utf8        TRUE if in UTF-8 mode
2434      cd          pointers to tables etc
2435    
2436  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2437  */  */
2438    
2439  static BOOL  static BOOL
2440  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2441    BOOL utf8)    branch_chain *bcptr, BOOL utf8, compile_data *cd)
2442  {  {
2443  while (bcptr != NULL && bcptr->current >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2444    {    {
2445    if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))
2446        return FALSE;
2447    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2448    }    }
2449  return TRUE;  return TRUE;
# Line 2028  where Perl recognizes it as the POSIX cl Line 2475  where Perl recognizes it as the POSIX cl
2475  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
2476  I think.  I think.
2477    
2478    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2479    It seems that the appearance of a nested POSIX class supersedes an apparent
2480    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2481    a digit.
2482    
2483    In Perl, unescaped square brackets may also appear as part of class names. For
2484    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2485    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2486    seem right at all. PCRE does not allow closing square brackets in POSIX class
2487    names.
2488    
2489  Arguments:  Arguments:
2490    ptr      pointer to the initial [    ptr      pointer to the initial [
2491    endptr   where to return the end pointer    endptr   where to return the end pointer
# Line 2036  Returns:   TRUE or FALSE Line 2494  Returns:   TRUE or FALSE
2494  */  */
2495    
2496  static BOOL  static BOOL
2497  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2498  {  {
2499  int terminator;          /* Don't combine these lines; the Solaris cc */  int terminator;          /* Don't combine these lines; the Solaris cc */
2500  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
2501  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != 0; ptr++)
2502    {    {
2503    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) ptr++; else    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2504        ptr++;
2505      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
2506      else
2507      {      {
     if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;  
2508      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2509        {        {
2510        *endptr = ptr;        *endptr = ptr;
2511        return TRUE;        return TRUE;
2512        }        }
2513        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
2514             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
2515              ptr[1] == CHAR_EQUALS_SIGN) &&
2516            check_posix_syntax(ptr, endptr))
2517          return FALSE;
2518      }      }
2519    }    }
2520  return FALSE;  return FALSE;
# Line 2073  Returns:     a value representing the na Line 2538  Returns:     a value representing the na
2538  */  */
2539    
2540  static int  static int
2541  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
2542  {  {
2543  const char *pn = posix_names;  const char *pn = posix_names;
2544  register int yield = 0;  register int yield = 0;
# Line 2120  Returns:     nothing Line 2585  Returns:     nothing
2585  */  */
2586    
2587  static void  static void
2588  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf8, compile_data *cd,
2589    uschar *save_hwm)    pcre_uchar *save_hwm)
2590  {  {
2591  uschar *ptr = group;  pcre_uchar *ptr = group;
2592    
2593  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf8)) != NULL)
2594    {    {
2595    int offset;    int offset;
2596    uschar *hc;    pcre_uchar *hc;
2597    
2598    /* See if this recursion is on the forward reference list. If so, adjust the    /* See if this recursion is on the forward reference list. If so, adjust the
2599    reference. */    reference. */
# Line 2173  Arguments: Line 2638  Arguments:
2638  Returns:         new code pointer  Returns:         new code pointer
2639  */  */
2640    
2641  static uschar *  static pcre_uchar *
2642  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2643  {  {
2644  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2645  *code++ = 255;  *code++ = 255;
2646  PUT(code, 0, ptr - cd->start_pattern);  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2647  PUT(code, LINK_SIZE, 0);                /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2648  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
2649  }  }
2650    
2651    
# Line 2202  Returns:             nothing Line 2667  Returns:             nothing
2667  */  */
2668    
2669  static void  static void
2670  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2671  {  {
2672  int length = ptr - cd->start_pattern - GET(previous_callout, 2);  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2673  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
2674  }  }
2675    
# Line 2254  for (++c; c <= d; c++) Line 2719  for (++c; c <= d; c++)
2719    
2720  return TRUE;  return TRUE;
2721  }  }
2722    
2723    
2724    
2725    /*************************************************
2726    *        Check a character and a property        *
2727    *************************************************/
2728    
2729    /* This function is called by check_auto_possessive() when a property item
2730    is adjacent to a fixed character.
2731    
2732    Arguments:
2733      c            the character
2734      ptype        the property type
2735      pdata        the data for the type
2736      negated      TRUE if it's a negated property (\P or \p{^)
2737    
2738    Returns:       TRUE if auto-possessifying is OK
2739    */
2740    
2741    static BOOL
2742    check_char_prop(int c, int ptype, int pdata, BOOL negated)
2743    {
2744    const ucd_record *prop = GET_UCD(c);
2745    switch(ptype)
2746      {
2747      case PT_LAMP:
2748      return (prop->chartype == ucp_Lu ||
2749              prop->chartype == ucp_Ll ||
2750              prop->chartype == ucp_Lt) == negated;
2751    
2752      case PT_GC:
2753      return (pdata == _pcre_ucp_gentype[prop->chartype]) == negated;
2754    
2755      case PT_PC:
2756      return (pdata == prop->chartype) == negated;
2757    
2758      case PT_SC:
2759      return (pdata == prop->script) == negated;
2760    
2761      /* These are specials */
2762    
2763      case PT_ALNUM:
2764      return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2765              _pcre_ucp_gentype[prop->chartype] == ucp_N) == negated;
2766    
2767      case PT_SPACE:    /* Perl space */
2768      return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2769              c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2770              == negated;
2771    
2772      case PT_PXSPACE:  /* POSIX space */
2773      return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
2774              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2775              c == CHAR_FF || c == CHAR_CR)
2776              == negated;
2777    
2778      case PT_WORD:
2779      return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||
2780              _pcre_ucp_gentype[prop->chartype] == ucp_N ||
2781              c == CHAR_UNDERSCORE) == negated;
2782      }
2783    return FALSE;
2784    }
2785  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2786    
2787    
# Line 2267  whether the next thing could possibly ma Line 2795  whether the next thing could possibly ma
2795  sense to automatically possessify the repeated item.  sense to automatically possessify the repeated item.
2796    
2797  Arguments:  Arguments:
2798    op_code       the repeated op code    previous      pointer to the repeated opcode
   this          data for this item, depends on the opcode  
2799    utf8          TRUE in UTF-8 mode    utf8          TRUE in UTF-8 mode
   utf8_char     used for utf8 character bytes, NULL if not relevant  
2800    ptr           next character in pattern    ptr           next character in pattern
2801    options       options bits    options       options bits
2802    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 2279  Returns:        TRUE if possessifying is Line 2805  Returns:        TRUE if possessifying is
2805  */  */
2806    
2807  static BOOL  static BOOL
2808  check_auto_possessive(int op_code, int item, BOOL utf8, uschar *utf8_char,  check_auto_possessive(const pcre_uchar *previous, BOOL utf8,
2809    const uschar *ptr, int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
2810  {  {
2811  int next;  int c, next;
2812    int op_code = *previous++;
2813    
2814  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
2815    
# Line 2293  if ((options & PCRE_EXTENDED) != 0) Line 2820  if ((options & PCRE_EXTENDED) != 0)
2820      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2821      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2822        {        {
2823        while (*(++ptr) != 0)        ptr++;
2824          while (*ptr != 0)
2825            {
2826          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2827            ptr++;
2828    #ifdef SUPPORT_UTF8
2829            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2830    #endif
2831            }
2832        }        }
2833      else break;      else break;
2834      }      }
# Line 2330  if ((options & PCRE_EXTENDED) != 0) Line 2864  if ((options & PCRE_EXTENDED) != 0)
2864      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2865      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2866        {        {
2867        while (*(++ptr) != 0)        ptr++;
2868          while (*ptr != 0)
2869            {
2870          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2871            ptr++;
2872    #ifdef SUPPORT_UTF8
2873            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2874    #endif
2875            }
2876        }        }
2877      else break;      else break;
2878      }      }
# Line 2343  if (*ptr == CHAR_ASTERISK || *ptr == CHA Line 2884  if (*ptr == CHAR_ASTERISK || *ptr == CHA
2884    strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)    strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
2885      return FALSE;      return FALSE;
2886    
2887  /* Now compare the next item with the previous opcode. If the previous is a  /* Now compare the next item with the previous opcode. First, handle cases when
2888  positive single character match, "item" either contains the character or, if  the next item is a character. */
 "item" is greater than 127 in utf8 mode, the character's bytes are in  
 utf8_char. */  
   
   
 /* Handle cases when the next item is a character. */  
2889    
2890  if (next >= 0) switch(op_code)  if (next >= 0) switch(op_code)
2891    {    {
2892    case OP_CHAR:    case OP_CHAR:
2893  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2894    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
2895  #else  #else
2896    (void)(utf8_char);  /* Keep compiler happy by referencing function argument */    c = *previous;
2897  #endif  #endif
2898    return item != next;    return c != next;
2899    
2900    /* For CHARNC (caseless character) we must check the other case. If we have    /* For CHARI (caseless character) we must check the other case. If we have
2901    Unicode property support, we can use it to test the other case of    Unicode property support, we can use it to test the other case of
2902    high-valued characters. */    high-valued characters. */
2903    
2904    case OP_CHARNC:    case OP_CHARI:
2905  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2906    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
2907    #else
2908      c = *previous;
2909  #endif  #endif
2910    if (item == next) return FALSE;    if (c == next) return FALSE;
2911  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2912    if (utf8)    if (utf8)
2913      {      {
# Line 2380  if (next >= 0) switch(op_code) Line 2918  if (next >= 0) switch(op_code)
2918  #else  #else
2919      othercase = NOTACHAR;      othercase = NOTACHAR;
2920  #endif  #endif
2921      return (unsigned int)item != othercase;      return (unsigned int)c != othercase;
2922      }      }
2923    else    else
2924  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
2925    return (item != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */
2926    
2927    /* For OP_NOT, "item" must be a single-byte character. */    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These
2928      opcodes are not used for multi-byte characters, because they are coded using
2929      an XCLASS instead. */
2930    
2931    case OP_NOT:    case OP_NOT:
2932    if (item == next) return TRUE;    return (c = *previous) == next;
2933    if ((options & PCRE_CASELESS) == 0) return FALSE;  
2934      case OP_NOTI:
2935      if ((c = *previous) == next) return TRUE;
2936  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2937    if (utf8)    if (utf8)
2938      {      {
# Line 2401  if (next >= 0) switch(op_code) Line 2943  if (next >= 0) switch(op_code)
2943  #else  #else
2944      othercase = NOTACHAR;      othercase = NOTACHAR;
2945  #endif  #endif
2946      return (unsigned int)item == othercase;      return (unsigned int)c == othercase;
2947      }      }
2948    else    else
2949  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
2950    return (item == cd->fcc[next]);  /* Non-UTF-8 mode */    return (c == cd->fcc[next]);  /* Non-UTF-8 mode */
2951    
2952      /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
2953      When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
2954    
2955    case OP_DIGIT:    case OP_DIGIT:
2956    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;
# Line 2448  if (next >= 0) switch(op_code) Line 2993  if (next >= 0) switch(op_code)
2993      case 0x202f:      case 0x202f:
2994      case 0x205f:      case 0x205f:
2995      case 0x3000:      case 0x3000:
2996      return op_code != OP_HSPACE;      return op_code == OP_NOT_HSPACE;
2997      default:      default:
2998      return op_code == OP_HSPACE;      return op_code != OP_NOT_HSPACE;
2999      }      }
3000    
3001      case OP_ANYNL:
3002    case OP_VSPACE:    case OP_VSPACE:
3003    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3004    switch(next)    switch(next)
# Line 2464  if (next >= 0) switch(op_code) Line 3010  if (next >= 0) switch(op_code)
3010      case 0x85:      case 0x85:
3011      case 0x2028:      case 0x2028:
3012      case 0x2029:      case 0x2029:
3013      return op_code != OP_VSPACE;      return op_code == OP_NOT_VSPACE;
3014      default:      default:
3015      return op_code == OP_VSPACE;      return op_code != OP_NOT_VSPACE;
3016      }      }
3017    
3018    #ifdef SUPPORT_UCP
3019      case OP_PROP:
3020      return check_char_prop(next, previous[0], previous[1], FALSE);
3021    
3022      case OP_NOTPROP:
3023      return check_char_prop(next, previous[0], previous[1], TRUE);
3024    #endif
3025    
3026    default:    default:
3027    return FALSE;    return FALSE;
3028    }    }
3029    
3030    
3031  /* Handle the case when the next item is \d, \s, etc. */  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3032    is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3033    generated only when PCRE_UCP is *not* set, that is, when only ASCII
3034    characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are
3035    replaced by OP_PROP codes when PCRE_UCP is set. */
3036    
3037  switch(op_code)  switch(op_code)
3038    {    {
3039    case OP_CHAR:    case OP_CHAR:
3040    case OP_CHARNC:    case OP_CHARI:
3041  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3042    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
3043    #else
3044      c = *previous;
3045  #endif  #endif
3046    switch(-next)    switch(-next)
3047      {      {
3048      case ESC_d:      case ESC_d:
3049      return item > 127 || (cd->ctypes[item] & ctype_digit) == 0;      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;
3050    
3051      case ESC_D:      case ESC_D:
3052      return item <= 127 && (cd->ctypes[item] & ctype_digit) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;
3053    
3054      case ESC_s:      case ESC_s:
3055      return item > 127 || (cd->ctypes[item] & ctype_space) == 0;      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;
3056    
3057      case ESC_S:      case ESC_S:
3058      return item <= 127 && (cd->ctypes[item] & ctype_space) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;
3059    
3060      case ESC_w:      case ESC_w:
3061      return item > 127 || (cd->ctypes[item] & ctype_word) == 0;      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;
3062    
3063      case ESC_W:      case ESC_W:
3064      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;
3065    
3066      case ESC_h:      case ESC_h:
3067      case ESC_H:      case ESC_H:
3068      switch(item)      switch(c)
3069        {        {
3070        case 0x09:        case 0x09:
3071        case 0x20:        case 0x20:
# Line 2533  switch(op_code) Line 3093  switch(op_code)
3093    
3094      case ESC_v:      case ESC_v:
3095      case ESC_V:      case ESC_V:
3096      switch(item)      switch(c)
3097        {        {
3098        case 0x0a:        case 0x0a:
3099        case 0x0b:        case 0x0b:
# Line 2547  switch(op_code) Line 3107  switch(op_code)
3107        return -next == ESC_v;        return -next == ESC_v;
3108        }        }
3109    
3110        /* When PCRE_UCP is set, these values get generated for \d etc. Find
3111        their substitutions and process them. The result will always be either
3112        -ESC_p or -ESC_P. Then fall through to process those values. */
3113    
3114    #ifdef SUPPORT_UCP
3115        case ESC_du:
3116        case ESC_DU:
3117        case ESC_wu:
3118        case ESC_WU:
3119        case ESC_su:
3120        case ESC_SU:
3121          {
3122          int temperrorcode = 0;
3123          ptr = substitutes[-next - ESC_DU];
3124          next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);
3125          if (temperrorcode != 0) return FALSE;
3126          ptr++;    /* For compatibility */
3127          }
3128        /* Fall through */
3129    
3130        case ESC_p:
3131        case ESC_P:
3132          {
3133          int ptype, pdata, errorcodeptr;
3134          BOOL negated;
3135    
3136          ptr--;      /* Make ptr point at the p or P */
3137          ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);
3138          if (ptype < 0) return FALSE;
3139          ptr++;      /* Point past the final curly ket */
3140    
3141          /* If the property item is optional, we have to give up. (When generated
3142          from \d etc by PCRE_UCP, this test will have been applied much earlier,
3143          to the original \d etc. At this point, ptr will point to a zero byte. */
3144    
3145          if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3146            strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3147              return FALSE;
3148    
3149          /* Do the property check. */
3150    
3151          return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);
3152          }
3153    #endif
3154    
3155      default:      default:
3156      return FALSE;      return FALSE;
3157      }      }
3158    
3159      /* In principle, support for Unicode properties should be integrated here as
3160      well. It means re-organizing the above code so as to get hold of the property
3161      values before switching on the op-code. However, I wonder how many patterns
3162      combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,
3163      these op-codes are never generated.) */
3164    
3165    case OP_DIGIT:    case OP_DIGIT:
3166    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||
3167           next == -ESC_h || next == -ESC_v;           next == -ESC_h || next == -ESC_v || next == -ESC_R;
3168    
3169    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
3170    return next == -ESC_d;    return next == -ESC_d;
3171    
3172    case OP_WHITESPACE:    case OP_WHITESPACE:
3173    return next == -ESC_S || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;
3174    
3175    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3176    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_s || next == -ESC_h || next == -ESC_v;
3177    
3178    case OP_HSPACE:    case OP_HSPACE:
3179    return next == -ESC_S || next == -ESC_H || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||
3180             next == -ESC_w || next == -ESC_v || next == -ESC_R;
3181    
3182    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
3183    return next == -ESC_h;    return next == -ESC_h;
3184    
3185    /* Can't have \S in here because VT matches \S (Perl anomaly) */    /* Can't have \S in here because VT matches \S (Perl anomaly) */
3186      case OP_ANYNL:
3187    case OP_VSPACE:    case OP_VSPACE:
3188    return next == -ESC_V || next == -ESC_d || next == -ESC_w;    return next == -ESC_V || next == -ESC_d || next == -ESC_w;
3189    
3190    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3191    return next == -ESC_v;    return next == -ESC_v || next == -ESC_R;
3192    
3193    case OP_WORDCHAR:    case OP_WORDCHAR:
3194    return next == -ESC_W || next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_W || next == -ESC_s || next == -ESC_h ||
3195             next == -ESC_v || next == -ESC_R;
3196    
3197    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
3198    return next == -ESC_w || next == -ESC_d;    return next == -ESC_w || next == -ESC_d;
# Line 2610  Arguments: Line 3224  Arguments:
3224    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3225    reqbyteptr     set to the last literal character required, else < 0    reqbyteptr     set to the last literal character required, else < 0
3226    bcptr          points to current branch chain    bcptr          points to current branch chain
3227      cond_depth     conditional nesting depth
3228    cd             contains pointers to tables etc.    cd             contains pointers to tables etc.
3229    lengthptr      NULL during the real compile phase    lengthptr      NULL during the real compile phase
3230                   points to length accumulator during pre-compile phase                   points to length accumulator during pre-compile phase
# Line 2619  Returns:         TRUE on success Line 3234  Returns:         TRUE on success
3234  */  */
3235    
3236  static BOOL  static BOOL
3237  compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3238    int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,    const pcre_uchar **ptrptr, int *errorcodeptr, int *firstbyteptr,
3239    compile_data *cd, int *lengthptr)    int *reqbyteptr, branch_chain *bcptr, int cond_depth, compile_data *cd,
3240      int *lengthptr)
3241  {  {
3242  int repeat_type, op_type;  int repeat_type, op_type;
3243  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
# Line 2630  int greedy_default, greedy_non_default; Line 3246  int greedy_default, greedy_non_default;
3246  int firstbyte, reqbyte;  int firstbyte, reqbyte;
3247  int zeroreqbyte, zerofirstbyte;  int zeroreqbyte, zerofirstbyte;
3248  int req_caseopt, reqvary, tempreqvary;  int req_caseopt, reqvary, tempreqvary;
3249  int options = *optionsptr;  int options = *optionsptr;               /* May change dynamically */
3250  int after_manual_callout = 0;  int after_manual_callout = 0;
3251  int length_prevgroup = 0;  int length_prevgroup = 0;
3252  register int c;  register int c;
3253  register uschar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3254  uschar *last_code = code;  pcre_uchar *last_code = code;
3255  uschar *orig_code = code;  pcre_uchar *orig_code = code;
3256  uschar *tempcode;  pcre_uchar *tempcode;
3257  BOOL inescq = FALSE;  BOOL inescq = FALSE;
3258  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstbyte = FALSE;
3259  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
3260  const uschar *tempptr;  const pcre_uchar *tempptr;
3261  uschar *previous = NULL;  const pcre_uchar *nestptr = NULL;
3262  uschar *previous_callout = NULL;  pcre_uchar *previous = NULL;
3263  uschar *save_hwm = NULL;  pcre_uchar *previous_callout = NULL;
3264  uschar classbits[32];  pcre_uchar *save_hwm = NULL;
3265    pcre_uchar classbits[32];
3266    
3267    /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3268    must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3269    dynamically as we process the pattern. */
3270    
3271  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3272  BOOL class_utf8;  BOOL class_utf8;
3273  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
3274  uschar *class_utf8data;  pcre_uint8 *class_utf8data;
3275  uschar *class_utf8data_base;  pcre_uint8 *class_utf8data_base;
3276  uschar utf8_char[6];  pcre_uint8 utf8_char[6];
3277  #else  #else
3278  BOOL utf8 = FALSE;  BOOL utf8 = FALSE;
 uschar *utf8_char = NULL;  
3279  #endif  #endif
3280    
3281  #ifdef DEBUG  #ifdef PCRE_DEBUG
3282  if (lengthptr != NULL) DPRINTF((">> start branch\n"));  if (lengthptr != NULL) DPRINTF((">> start branch\n"));
3283  #endif  #endif
3284    
# Line 2706  for (;; ptr++) Line 3326  for (;; ptr++)
3326    int subfirstbyte;    int subfirstbyte;
3327    int terminator;    int terminator;
3328    int mclength;    int mclength;
3329    uschar mcbuffer[8];    int tempbracount;
3330      pcre_uchar mcbuffer[8];
3331    
3332    /* Get next byte in the pattern */    /* Get next byte in the pattern */
3333    
3334    c = *ptr;    c = *ptr;
3335    
3336      /* If we are at the end of a nested substitution, revert to the outer level
3337      string. Nesting only happens one level deep. */
3338    
3339      if (c == 0 && nestptr != NULL)
3340        {
3341        ptr = nestptr;
3342        nestptr = NULL;
3343        c = *ptr;
3344        }
3345    
3346    /* If we are in the pre-compile phase, accumulate the length used for the    /* If we are in the pre-compile phase, accumulate the length used for the
3347    previous cycle of this loop. */    previous cycle of this loop. */
3348    
3349    if (lengthptr != NULL)    if (lengthptr != NULL)
3350      {      {
3351  #ifdef DEBUG  #ifdef PCRE_DEBUG
3352      if (code > cd->hwm) cd->hwm = code;                 /* High water info */      if (code > cd->hwm) cd->hwm = code;                 /* High water info */
3353  #endif  #endif
3354      if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */      if (code > cd->start_workspace + WORK_SIZE_CHECK)   /* Check for overrun */
3355        {        {
3356        *errorcodeptr = ERR52;        *errorcodeptr = ERR52;
3357        goto FAILED;        goto FAILED;
# Line 2742  for (;; ptr++) Line 3373  for (;; ptr++)
3373        goto FAILED;        goto FAILED;
3374        }        }
3375    
3376      *lengthptr += code - last_code;      *lengthptr += (int)(code - last_code);
3377      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, code - last_code, c));      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, (int)(code - last_code),
3378          c));
3379    
3380      /* If "previous" is set and it is not at the start of the work space, move      /* If "previous" is set and it is not at the start of the work space, move
3381      it back to there, in order to avoid filling up the work space. Otherwise,      it back to there, in order to avoid filling up the work space. Otherwise,
# Line 2769  for (;; ptr++) Line 3401  for (;; ptr++)
3401    /* In the real compile phase, just check the workspace used by the forward    /* In the real compile phase, just check the workspace used by the forward
3402    reference list. */    reference list. */
3403    
3404    else if (cd->hwm > cd->start_workspace + COMPILE_WORK_SIZE)    else if (cd->hwm > cd->start_workspace + WORK_SIZE_CHECK)
3405      {      {
3406      *errorcodeptr = ERR52;      *errorcodeptr = ERR52;
3407      goto FAILED;      goto FAILED;
# Line 2817  for (;; ptr++) Line 3449  for (;; ptr++)
3449      previous_callout = NULL;      previous_callout = NULL;
3450      }      }
3451    
3452    /* In extended mode, skip white space and comments */    /* In extended mode, skip white space and comments. */
3453    
3454    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
3455      {      {
3456      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if ((cd->ctypes[c] & ctype_space) != 0) continue;
3457      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3458        {        {
3459        while (*(++ptr) != 0)        ptr++;
3460          while (*ptr != 0)
3461          {          {
3462          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3463            ptr++;
3464    #ifdef SUPPORT_UTF8
3465            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
3466    #endif
3467          }          }
3468        if (*ptr != 0) continue;        if (*ptr != 0) continue;
3469    
# Line 2860  for (;; ptr++) Line 3497  for (;; ptr++)
3497          *errorcodeptr = ERR20;          *errorcodeptr = ERR20;
3498          goto FAILED;          goto FAILED;
3499          }          }
3500        *lengthptr += code - last_code;   /* To include callout length */        *lengthptr += (int)(code - last_code);   /* To include callout length */
3501        DPRINTF((">> end branch\n"));        DPRINTF((">> end branch\n"));
3502        }        }
3503      return TRUE;      return TRUE;
# Line 2871  for (;; ptr++) Line 3508  for (;; ptr++)
3508      the setting of any following char as a first character. */      the setting of any following char as a first character. */
3509    
3510      case CHAR_CIRCUMFLEX_ACCENT:      case CHAR_CIRCUMFLEX_ACCENT:
3511        previous = NULL;
3512      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3513        {        {
3514        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
3515          *code++ = OP_CIRCM;
3516        }        }
3517      previous = NULL;      else *code++ = OP_CIRC;
     *code++ = OP_CIRC;  
3518      break;      break;
3519    
3520      case CHAR_DOLLAR_SIGN:      case CHAR_DOLLAR_SIGN:
3521      previous = NULL;      previous = NULL;
3522      *code++ = OP_DOLL;      *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
3523      break;      break;
3524    
3525      /* There can never be a first char if '.' is first, whatever happens about      /* There can never be a first char if '.' is first, whatever happens about
# Line 2988  for (;; ptr++) Line 3626  for (;; ptr++)
3626      than 256), because in that case the compiled code doesn't use the bit map.      than 256), because in that case the compiled code doesn't use the bit map.
3627      */      */
3628    
3629      memset(classbits, 0, 32 * sizeof(uschar));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
3630    
3631  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3632      class_utf8 = FALSE;                       /* No chars >= 256 */      class_utf8 = FALSE;                       /* No chars >= 256 */
# Line 3002  for (;; ptr++) Line 3640  for (;; ptr++)
3640    
3641      if (c != 0) do      if (c != 0) do
3642        {        {
3643        const uschar *oldptr;        const pcre_uchar *oldptr;
3644    
3645  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3646        if (utf8 && c > 127)        if (utf8 && c > 127)
# Line 3048  for (;; ptr++) Line 3686  for (;; ptr++)
3686          {          {
3687          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
3688          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
3689          register const uschar *cbits = cd->cbits;          register const pcre_uint8 *cbits = cd->cbits;
3690          uschar pbits[32];          pcre_uint8 pbits[32];
3691    
3692          if (ptr[1] != CHAR_COLON)          if (ptr[1] != CHAR_COLON)
3693            {            {
# Line 3065  for (;; ptr++) Line 3703  for (;; ptr++)
3703            ptr++;            ptr++;
3704            }            }
3705    
3706          posix_class = check_posix_name(ptr, tempptr - ptr);          posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
3707          if (posix_class < 0)          if (posix_class < 0)
3708            {            {
3709            *errorcodeptr = ERR30;            *errorcodeptr = ERR30;
# Line 3079  for (;; ptr++) Line 3717  for (;; ptr++)
3717          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
3718            posix_class = 0;            posix_class = 0;
3719    
3720          /* We build the bit map for the POSIX class in a chunk of local store          /* When PCRE_UCP is set, some of the POSIX classes are converted to
3721          because we may be adding and subtracting from it, and we don't want to          different escape sequences that use Unicode properties. */
3722          subtract bits that may be in the main map already. At the end we or the  
3723          result into the bit map that is being built. */  #ifdef SUPPORT_UCP
3724            if ((options & PCRE_UCP) != 0)
3725              {
3726              int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
3727              if (posix_substitutes[pc] != NULL)
3728                {
3729                nestptr = tempptr + 1;
3730                ptr = posix_substitutes[pc] - 1;
3731                continue;
3732                }
3733              }
3734    #endif
3735            /* In the non-UCP case, we build the bit map for the POSIX class in a
3736            chunk of local store because we may be adding and subtracting from it,
3737            and we don't want to subtract bits that may be in the main map already.
3738            At the end we or the result into the bit map that is being built. */
3739    
3740          posix_class *= 3;          posix_class *= 3;
3741    
3742          /* Copy in the first table (always present) */          /* Copy in the first table (always present) */
3743    
3744          memcpy(pbits, cbits + posix_class_maps[posix_class],          memcpy(pbits, cbits + posix_class_maps[posix_class],
3745            32 * sizeof(uschar));            32 * sizeof(pcre_uint8));
3746    
3747          /* If there is a second table, add or remove it as required. */          /* If there is a second table, add or remove it as required. */
3748    
# Line 3126  for (;; ptr++) Line 3779  for (;; ptr++)
3779    
3780        /* Backslash may introduce a single character, or it may introduce one        /* Backslash may introduce a single character, or it may introduce one
3781        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
3782        case. Inside a class (and only there) it is treated as backspace.        case. Inside a class (and only there) it is treated as backspace. We
3783        Elsewhere it marks a word boundary. Other escapes have preset maps ready        assume that other escapes have more than one character in them, so set
3784        to 'or' into the one we are building. We assume they have more than one        class_charcount bigger than one. Unrecognized escapes fall through and
3785        character in them, so set class_charcount bigger than one. */        are either treated as literal characters (by default), or are faulted if
3786          PCRE_EXTRA is set. */
3787    
3788        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
3789          {          {
3790          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3791          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
3792    
3793          if (-c == ESC_b) c = CHAR_BS;       /* \b is backspace in a class */          if (-c == ESC_b) c = CHAR_BS;    /* \b is backspace in a class */
         else if (-c == ESC_X) c = CHAR_X;   /* \X is literal X in a class */  
         else if (-c == ESC_R) c = CHAR_R;   /* \R is literal R in a class */  
3794          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (-c == ESC_Q)            /* Handle start of quoted string */
3795            {            {
3796            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 3152  for (;; ptr++) Line 3804  for (;; ptr++)
3804    
3805          if (c < 0)          if (c < 0)
3806            {            {
3807            register const uschar *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
3808            class_charcount += 2;     /* Greater than 1 is what matters */            class_charcount += 2;     /* Greater than 1 is what matters */
3809    
3810            /* Save time by not doing this in the pre-compile phase. */            switch (-c)
   
           if (lengthptr == NULL) switch (-c)  
3811              {              {
3812    #ifdef SUPPORT_UCP
3813                case ESC_du:     /* These are the values given for \d etc */
3814                case ESC_DU:     /* when PCRE_UCP is set. We replace the */
3815                case ESC_wu:     /* escape sequence with an appropriate \p */
3816                case ESC_WU:     /* or \P to test Unicode properties instead */
3817                case ESC_su:     /* of the default ASCII testing. */
3818                case ESC_SU:
3819                nestptr = ptr;
3820                ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */
3821                class_charcount -= 2;                /* Undo! */
3822                continue;
3823    #endif
3824              case ESC_d:              case ESC_d:
3825              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
3826              continue;              continue;
# Line 3177  for (;; ptr++) Line 3839  for (;; ptr++)
3839              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
3840              continue;              continue;
3841    
3842                /* Perl 5.004 onwards omits VT from \s, but we must preserve it
3843                if it was previously set by something earlier in the character
3844                class. */
3845    
3846              case ESC_s:              case ESC_s:
3847              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];              classbits[0] |= cbits[cbit_space];
3848              classbits[1] &= ~0x08;   /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= cbits[cbit_space+1] & ~0x08;
3849                for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
3850              continue;              continue;
3851    
3852              case ESC_S:              case ESC_S:
# Line 3188  for (;; ptr++) Line 3855  for (;; ptr++)
3855              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
3856              continue;              continue;
3857    
3858              default:    /* Not recognized; fall through */              case ESC_h:
             break;      /* Need "default" setting to stop compiler warning. */  
             }  
   
           /* In the pre-compile phase, just do the recognition. */  
   
           else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||  
                    c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;  
   
           /* We need to deal with \H, \h, \V, and \v in both phases because  
           they use extra memory. */  
   
           if (-c == ESC_h)  
             {  
3859              SETBIT(classbits, 0x09); /* VT */              SETBIT(classbits, 0x09); /* VT */
3860              SETBIT(classbits, 0x20); /* SPACE */              SETBIT(classbits, 0x20); /* SPACE */
3861              SETBIT(classbits, 0xa0); /* NSBP */              SETBIT(classbits, 0xa0); /* NSBP */
# Line 3225  for (;; ptr++) Line 3879  for (;; ptr++)
3879                }                }
3880  #endif  #endif
3881              continue;              continue;
             }  
3882    
3883            if (-c == ESC_H)              case ESC_H:
             {  
3884              for (c = 0; c < 32; c++)              for (c = 0; c < 32; c++)
3885                {                {
3886                int x = 0xff;                int x = 0xff;
# Line 3270  for (;; ptr++) Line 3922  for (;; ptr++)
3922                }                }
3923  #endif  #endif
3924              continue;              continue;
             }  
3925    
3926            if (-c == ESC_v)              case ESC_v:
             {  
3927              SETBIT(classbits, 0x0a); /* LF */              SETBIT(classbits, 0x0a); /* LF */
3928              SETBIT(classbits, 0x0b); /* VT */              SETBIT(classbits, 0x0b); /* VT */
3929              SETBIT(classbits, 0x0c); /* FF */              SETBIT(classbits, 0x0c); /* FF */
# Line 3289  for (;; ptr++) Line 3939  for (;; ptr++)
3939                }                }
3940  #endif  #endif
3941              continue;              continue;
             }  
3942    
3943            if (-c == ESC_V)              case ESC_V:
             {  
3944              for (c = 0; c < 32; c++)              for (c = 0; c < 32; c++)
3945                {                {
3946                int x = 0xff;                int x = 0xff;
# Line 3322  for (;; ptr++) Line 3970  for (;; ptr++)
3970                }                }
3971  #endif  #endif
3972              continue;              continue;
             }  
   
           /* We need to deal with \P and \p in both phases. */  
3973    
3974  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3975            if (-c == ESC_p || -c == ESC_P)              case ESC_p:
3976              {              case ESC_P:
3977              BOOL negated;                {
3978              int pdata;                BOOL negated;
3979              int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                int pdata;
3980              if (ptype < 0) goto FAILED;                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
3981              class_utf8 = TRUE;                if (ptype < 0) goto FAILED;
3982              *class_utf8data++ = ((-c == ESC_p) != negated)?                class_utf8 = TRUE;
3983                XCL_PROP : XCL_NOTPROP;                *class_utf8data++ = ((-c == ESC_p) != negated)?
3984              *class_utf8data++ = ptype;                  XCL_PROP : XCL_NOTPROP;
3985              *class_utf8data++ = pdata;                *class_utf8data++ = ptype;
3986              class_charcount -= 2;   /* Not a < 256 character */                *class_utf8data++ = pdata;
3987              continue;                class_charcount -= 2;   /* Not a < 256 character */
3988              }                continue;
3989                  }
3990  #endif  #endif
3991            /* Unrecognized escapes are faulted if PCRE is running in its              /* Unrecognized escapes are faulted if PCRE is running in its
3992            strict mode. By default, for compatibility with Perl, they are              strict mode. By default, for compatibility with Perl, they are
3993            treated as literals. */              treated as literals. */
3994    
3995            if ((options & PCRE_EXTRA) != 0)              default:
3996              {              if ((options & PCRE_EXTRA) != 0)
3997              *errorcodeptr = ERR7;                {
3998              goto FAILED;                *errorcodeptr = ERR7;
3999                  goto FAILED;
4000                  }
4001                class_charcount -= 2;  /* Undo the default count from above */
4002                c = *ptr;              /* Get the final character and fall through */
4003                break;
4004              }              }
   
           class_charcount -= 2;  /* Undo the default count from above */  
           c = *ptr;              /* Get the final character and fall through */  
4005            }            }
4006    
4007          /* Fall through if we have a single character (c >= 0). This may be          /* Fall through if we have a single character (c >= 0). This may be
# Line 3423  for (;; ptr++) Line 4071  for (;; ptr++)
4071            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
4072            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4073    
4074            /* \b is backspace; \X is literal X; \R is literal R; any other            /* \b is backspace; any other special means the '-' was literal */
           special means the '-' was literal */  
4075    
4076            if (d < 0)            if (d < 0)
4077              {              {
4078              if (d == -ESC_b) d = CHAR_BS;              if (d == -ESC_b) d = CHAR_BS; else
             else if (d == -ESC_X) d = CHAR_X;  
             else if (d == -ESC_R) d = CHAR_R; else  
4079                {                {
4080                ptr = oldptr;                ptr = oldptr;
4081                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto LONE_SINGLE_CHARACTER;  /* A few lines below */
# Line 3596  for (;; ptr++) Line 4241  for (;; ptr++)
4241          }          }
4242        }        }
4243    
4244      /* Loop until ']' reached. This "while" is the end of the "do" above. */      /* Loop until ']' reached. This "while" is the end of the "do" far above.
4245        If we are at the end of an internal nested string, revert to the outer
4246        string. */
4247    
4248        while (((c = *(++ptr)) != 0 ||
4249               (nestptr != NULL &&
4250                 (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != 0)) &&
4251               (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
4252    
4253      while ((c = *(++ptr)) != 0 && (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));      /* Check for missing terminating ']' */
4254    
4255      if (c == 0)                          /* Missing terminating ']' */      if (c == 0)
4256        {        {
4257        *errorcodeptr = ERR6;        *errorcodeptr = ERR6;
4258        goto FAILED;        goto FAILED;
4259        }        }
4260    
   
 /* This code has been disabled because it would mean that \s counts as  
 an explicit \r or \n reference, and that's not really what is wanted. Now  
 we set the flag only if there is a literal "\r" or "\n" in the class. */  
   
 #if 0  
     /* Remember whether \r or \n are in this class */  
   
     if (negate_class)  
       {  
       if ((classbits[1] & 0x24) != 0x24) cd->external_flags |= PCRE_HASCRORLF;  
       }  
     else  
       {  
       if ((classbits[1] & 0x24) != 0) cd->external_flags |= PCRE_HASCRORLF;  
       }  
 #endif  
   
   
4261      /* 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
4262      less than 256. As long as there were no characters >= 128 and there was no      less than 256. As long as there were no characters >= 128 and there was no
4263      use of \p or \P, in other words, no use of any XCLASS features, we can      use of \p or \P, in other words, no use of any XCLASS features, we can
# Line 3632  we set the flag only if there is a liter Line 4265  we set the flag only if there is a liter
4265    
4266      In UTF-8 mode, we can optimize the negative case only if there were no      In UTF-8 mode, we can optimize the negative case only if there were no
4267      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR
4268      operate on single-bytes only. This is an historical hangover. Maybe one day      operate on single-bytes characters only. This is an historical hangover.
4269      we can tidy these opcodes to handle multi-byte characters.      Maybe one day we can tidy these opcodes to handle multi-byte characters.
4270    
4271      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
4272      1-character OP_CHAR[NC] if it's positive, or OP_NOT if it's negative. Note      1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.
4273      that OP_NOT does not support multibyte characters. In the positive case, it      Note that OP_NOT[I] does not support multibyte characters. In the positive
4274      can cause firstbyte to be set. Otherwise, there can be no first char if      case, it can cause firstbyte to be set. Otherwise, there can be no first
4275      this item is first, whatever repeat count may follow. In the case of      char if this item is first, whatever repeat count may follow. In the case
4276      reqbyte, save the previous value for reinstating. */      of reqbyte, save the previous value for reinstating. */
4277    
4278  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4279      if (class_charcount == 1 && !class_utf8 &&      if (class_charcount == 1 && !class_utf8 &&
# Line 3651  we set the flag only if there is a liter Line 4284  we set the flag only if there is a liter
4284        {        {
4285        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
4286    
4287        /* The OP_NOT opcode works on one-byte characters only. */        /* The OP_NOT[I] opcodes work on one-byte characters only. */
4288    
4289        if (negate_class)        if (negate_class)
4290          {          {
4291          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
4292          zerofirstbyte = firstbyte;          zerofirstbyte = firstbyte;
4293          *code++ = OP_NOT;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4294          *code++ = class_lastchar;          *code++ = class_lastchar;
4295          break;          break;
4296          }          }
# Line 3688  we set the flag only if there is a liter Line 4321  we set the flag only if there is a liter
4321    
4322      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
4323      extended class, with its own opcode, unless there was a negated special      extended class, with its own opcode, unless there was a negated special
4324      such as \S in the class, because in that case all characters > 255 are in      such as \S in the class, and PCRE_UCP is not set, because in that case all
4325      the class, so any that were explicitly given as well can be ignored. If      characters > 255 are in the class, so any that were explicitly given as
4326      (when there are explicit characters > 255 that must be listed) there are no      well can be ignored. If (when there are explicit characters > 255 that must
4327      characters < 256, we can omit the bitmap in the actual compiled code. */      be listed) there are no characters < 256, we can omit the bitmap in the
4328        actual compiled code. */
4329    
4330  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4331      if (class_utf8 && !should_flip_negation)      if (class_utf8 && (!should_flip_negation || (options & PCRE_UCP) != 0))
4332        {        {
4333        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */
4334        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
# Line 3720  we set the flag only if there is a liter Line 4354  we set the flag only if there is a liter
4354        }        }
4355  #endif  #endif
4356    
4357      /* If there are no characters > 255, set the opcode to OP_CLASS or      /* If there are no characters > 255, or they are all to be included or
4358      OP_NCLASS, depending on whether the whole class was negated and whether      excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
4359      there were negative specials such as \S in the class. Then copy the 32-byte      whole class was negated and whether there were negative specials such as \S
4360      map into the code vector, negating it if necessary. */      (non-UCP) in the class. Then copy the 32-byte map into the code vector,
4361        negating it if necessary. */
4362    
4363      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
4364      if (negate_class)      if (negate_class)
# Line 3783  we set the flag only if there is a liter Line 4418  we set the flag only if there is a liter
4418      op_type = 0;                    /* Default single-char op codes */      op_type = 0;                    /* Default single-char op codes */
4419      possessive_quantifier = FALSE;  /* Default not possessive quantifier */      possessive_quantifier = FALSE;  /* Default not possessive quantifier */
4420    
4421      /* Save start of previous item, in case we have to move it up to make space      /* Save start of previous item, in case we have to move it up in order to
4422      for an inserted OP_ONCE for the additional '+' extension. */      insert something before it. */
4423    
4424      tempcode = previous;      tempcode = previous;
4425    
# Line 3807  we set the flag only if there is a liter Line 4442  we set the flag only if there is a liter
4442        }        }
4443      else repeat_type = greedy_default;      else repeat_type = greedy_default;
4444    
4445        /* If previous was a recursion call, wrap it in atomic brackets so that
4446        previous becomes the atomic group. All recursions were so wrapped in the
4447        past, but it no longer happens for non-repeated recursions. In fact, the
4448        repeated ones could be re-implemented independently so as not to need this,
4449        but for the moment we rely on the code for repeating groups. */
4450    
4451        if (*previous == OP_RECURSE)
4452          {
4453          memmove(previous + 1 + LINK_SIZE, previous, 1 + LINK_SIZE);
4454          *previous = OP_ONCE;
4455          PUT(previous, 1, 2 + 2*LINK_SIZE);
4456          previous[2 + 2*LINK_SIZE] = OP_KET;
4457          PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4458          code += 2 + 2 * LINK_SIZE;
4459          length_prevgroup = 3 + 3*LINK_SIZE;
4460    
4461          /* When actually compiling, we need to check whether this was a forward
4462          reference, and if so, adjust the offset. */
4463    
4464          if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4465            {
4466            int offset = GET(cd->hwm, -LINK_SIZE);
4467            if (offset == previous + 1 - cd->start_code)
4468              PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
4469            }
4470          }
4471    
4472        /* Now handle repetition for the different types of item. */
4473    
4474      /* If previous was a character match, abolish the item and generate a      /* If previous was a character match, abolish the item and generate a
4475      repeat item instead. If a char item has a minumum of more than one, ensure      repeat item instead. If a char item has a minumum of more than one, ensure
4476      that it is set in reqbyte - it might not be if a sequence such as x{3} is      that it is set in reqbyte - it might not be if a sequence such as x{3} is
4477      the first thing in a branch because the x will have gone into firstbyte      the first thing in a branch because the x will have gone into firstbyte
4478      instead.  */      instead.  */
4479    
4480      if (*previous == OP_CHAR || *previous == OP_CHARNC)      if (*previous == OP_CHAR || *previous == OP_CHARI)
4481        {        {
4482          op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;
4483    
4484        /* Deal with UTF-8 characters that take up more than one byte. It's        /* Deal with UTF-8 characters that take up more than one byte. It's
4485        easier to write this out separately than try to macrify it. Use c to        easier to write this out separately than try to macrify it. Use c to
4486        hold the length of the character in bytes, plus 0x80 to flag that it's a        hold the length of the character in bytes, plus 0x80 to flag that it's a
# Line 3823  we set the flag only if there is a liter Line 4489  we set the flag only if there is a liter
4489  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4490        if (utf8 && (code[-1] & 0x80) != 0)        if (utf8 && (code[-1] & 0x80) != 0)
4491          {          {
4492          uschar *lastchar = code - 1;          pcre_uchar *lastchar = code - 1;
4493          while((*lastchar & 0xc0) == 0x80) lastchar--;          while((*lastchar & 0xc0) == 0x80) lastchar--;
4494          c = code - lastchar;            /* Length of UTF-8 character */          c = code - lastchar;            /* Length of UTF-8 character */
4495          memcpy(utf8_char, lastchar, c); /* Save the char */          memcpy(utf8_char, lastchar, c); /* Save the char */
# Line 3847  we set the flag only if there is a liter Line 4513  we set the flag only if there is a liter
4513    
4514        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4515            repeat_max < 0 &&            repeat_max < 0 &&
4516            check_auto_possessive(*previous, c, utf8, utf8_char, ptr + 1,            check_auto_possessive(previous, utf8, ptr + 1, options, cd))
             options, cd))  
4517          {          {
4518          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4519          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 3860  we set the flag only if there is a liter Line 4525  we set the flag only if there is a liter
4525      /* If previous was a single negated character ([^a] or similar), we use      /* If previous was a single negated character ([^a] or similar), we use
4526      one of the special opcodes, replacing it. The code is shared with single-      one of the special opcodes, replacing it. The code is shared with single-
4527      character repeats by setting opt_type to add a suitable offset into      character repeats by setting opt_type to add a suitable offset into
4528      repeat_type. We can also test for auto-possessification. OP_NOT is      repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI
4529      currently used only for single-byte chars. */      are currently used only for single-byte chars. */
4530    
4531      else if (*previous == OP_NOT)      else if (*previous == OP_NOT || *previous == OP_NOTI)
4532        {        {
4533        op_type = OP_NOTSTAR - OP_STAR;  /* Use "not" opcodes */        op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;
4534        c = previous[1];        c = previous[1];
4535        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4536            repeat_max < 0 &&            repeat_max < 0 &&
4537            check_auto_possessive(OP_NOT, c, utf8, NULL, ptr + 1, options, cd))            check_auto_possessive(previous, utf8, ptr + 1, options, cd))
4538          {          {
4539          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4540          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 3886  we set the flag only if there is a liter Line 4551  we set the flag only if there is a liter
4551    
4552      else if (*previous < OP_EODN)      else if (*previous < OP_EODN)
4553        {        {
4554        uschar *oldcode;        pcre_uchar *oldcode;
4555        int prop_type, prop_value;        int prop_type, prop_value;
4556        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
4557        c = *previous;        c = *previous;
4558    
4559        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4560            repeat_max < 0 &&            repeat_max < 0 &&
4561            check_auto_possessive(c, 0, utf8, NULL, ptr + 1, options, cd))            check_auto_possessive(previous, utf8, ptr + 1, options, cd))
4562          {          {
4563          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4564          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 3915  we set the flag only if there is a liter Line 4580  we set the flag only if there is a liter
4580    
4581        if (repeat_max == 0) goto END_REPEAT;        if (repeat_max == 0) goto END_REPEAT;
4582    
4583        /*--------------------------------------------------------------------*/        /*--------------------------------------------------------------------*/
4584        /* This code is obsolete from release 8.00; the restriction was finally        /* This code is obsolete from release 8.00; the restriction was finally
4585        removed: */        removed: */
4586    
4587        /* All real repeats make it impossible to handle partial matching (maybe        /* All real repeats make it impossible to handle partial matching (maybe
4588        one day we will be able to remove this restriction). */        one day we will be able to remove this restriction). */
4589    
4590        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
4591        /*--------------------------------------------------------------------*/        /*--------------------------------------------------------------------*/
4592    
4593        /* Combine the op_type with the repeat_type */        /* Combine the op_type with the repeat_type */
4594    
# Line 4062  we set the flag only if there is a liter Line 4727  we set the flag only if there is a liter
4727  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4728               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
4729  #endif  #endif
4730               *previous == OP_REF)               *previous == OP_REF ||
4731                 *previous == OP_REFI)
4732        {        {
4733        if (repeat_max == 0)        if (repeat_max == 0)
4734          {          {
# Line 4070  we set the flag only if there is a liter Line 4736  we set the flag only if there is a liter
4736          goto END_REPEAT;          goto END_REPEAT;
4737          }          }
4738    
4739        /*--------------------------------------------------------------------*/        /*--------------------------------------------------------------------*/
4740        /* This code is obsolete from release 8.00; the restriction was finally        /* This code is obsolete from release 8.00; the restriction was finally
4741        removed: */        removed: */
4742    
# Line 4078  we set the flag only if there is a liter Line 4744  we set the flag only if there is a liter
4744        one day we will be able to remove this restriction). */        one day we will be able to remove this restriction). */
4745    
4746        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
4747        /*--------------------------------------------------------------------*/        /*--------------------------------------------------------------------*/
4748    
4749        if (repeat_min == 0 && repeat_max == -1)        if (repeat_min == 0 && repeat_max == -1)
4750          *code++ = OP_CRSTAR + repeat_type;          *code++ = OP_CRSTAR + repeat_type;
# Line 4096  we set the flag only if there is a liter Line 4762  we set the flag only if there is a liter
4762        }        }
4763    
4764      /* If previous was a bracket group, we may have to replicate it in certain      /* If previous was a bracket group, we may have to replicate it in certain
4765      cases. */      cases. Note that at this point we can encounter only the "basic" bracket
4766        opcodes such as BRA and CBRA, as this is the place where they get converted
4767        into the more special varieties such as BRAPOS and SBRA. A test for >=
4768        OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
4769        ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow
4770        repetition of assertions, but now it does, for Perl compatibility. */
4771    
4772      else if (*previous == OP_BRA  || *previous == OP_CBRA ||      else if (*previous >= OP_ASSERT && *previous <= OP_COND)
              *previous == OP_ONCE || *previous == OP_COND)  
4773        {        {
4774        register int i;        register int i;
4775        int ketoffset = 0;        int len = (int)(code - previous);
4776        int len = code - previous;        pcre_uchar *bralink = NULL;
4777        uschar *bralink = NULL;        pcre_uchar *brazeroptr = NULL;
4778    
4779        /* Repeating a DEFINE group is pointless */        /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
4780          we just ignore the repeat. */
4781    
4782        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
4783          {          goto END_REPEAT;
4784          *errorcodeptr = ERR55;  
4785          goto FAILED;        /* There is no sense in actually repeating assertions. The only potential
4786          }        use of repetition is in cases when the assertion is optional. Therefore,
4787          if the minimum is greater than zero, just ignore the repeat. If the
4788          maximum is not not zero or one, set it to 1. */
4789    
4790        /* If the maximum repeat count is unlimited, find the end of the bracket        if (*previous < OP_ONCE)    /* Assertion */
4791        by scanning through from the start, and compute the offset back to it          {
4792        from the current code pointer. There may be an OP_OPT setting following          if (repeat_min > 0) goto END_REPEAT;
4793        the final KET, so we can't find the end just by going back from the code          if (repeat_max < 0 || repeat_max > 1) repeat_max = 1;
       pointer. */  
   
       if (repeat_max == -1)  
         {  
         register uschar *ket = previous;  
         do ket += GET(ket, 1); while (*ket != OP_KET);  
         ketoffset = code - ket;  
4794          }          }
4795    
4796        /* The case of a zero minimum is special because of the need to stick        /* The case of a zero minimum is special because of the need to stick
# Line 4145  we set the flag only if there is a liter Line 4811  we set the flag only if there is a liter
4811          **   goto END_REPEAT;          **   goto END_REPEAT;
4812          **   }          **   }
4813    
4814          However, that fails when a group is referenced as a subroutine from          However, that fails when a group or a subgroup within it is referenced
4815          elsewhere in the pattern, so now we stick in OP_SKIPZERO in front of it          as a subroutine from elsewhere in the pattern, so now we stick in
4816          so that it is skipped on execution. As we don't have a list of which          OP_SKIPZERO in front of it so that it is skipped on execution. As we
4817          groups are referenced, we cannot do this selectively.          don't have a list of which groups are referenced, we cannot do this
4818            selectively.
4819    
4820          If the maximum is 1 or unlimited, we just have to stick in the BRAZERO          If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
4821          and do no more at this point. However, we do need to adjust any          and do no more at this point. However, we do need to adjust any
# Line 4168  we set the flag only if there is a liter Line 4835  we set the flag only if there is a liter
4835              *previous++ = OP_SKIPZERO;              *previous++ = OP_SKIPZERO;
4836              goto END_REPEAT;              goto END_REPEAT;
4837              }              }
4838              brazeroptr = previous;    /* Save for possessive optimizing */
4839            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
4840            }            }
4841    
# Line 4192  we set the flag only if there is a liter Line 4860  we set the flag only if there is a liter
4860            /* We chain together the bracket offset fields that have to be            /* We chain together the bracket offset fields that have to be
4861            filled in later when the ends of the brackets are reached. */            filled in later when the ends of the brackets are reached. */
4862    
4863            offset = (bralink == NULL)? 0 : previous - bralink;            offset = (bralink == NULL)? 0 : (int)(previous - bralink);
4864            bralink = previous;            bralink = previous;
4865            PUTINC(previous, 0, offset);            PUTINC(previous, 0, offset);
4866            }            }
# Line 4213  we set the flag only if there is a liter Line 4881  we set the flag only if there is a liter
4881            {            {
4882            /* 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
4883            just adjust the length as if we had. Do some paranoid checks for            just adjust the length as if we had. Do some paranoid checks for
4884            potential integer overflow. */            potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
4885              integer type when available, otherwise double. */
4886    
4887            if (lengthptr != NULL)            if (lengthptr != NULL)
4888              {              {
4889              int delta = (repeat_min - 1)*length_prevgroup;              int delta = (repeat_min - 1)*length_prevgroup;
4890              if ((double)(repeat_min - 1)*(double)length_prevgroup >              if ((INT64_OR_DOUBLE)(repeat_min - 1)*
4891                                                              (double)INT_MAX ||                    (INT64_OR_DOUBLE)length_prevgroup >
4892                        (INT64_OR_DOUBLE)INT_MAX ||
4893                  OFLOW_MAX - *lengthptr < delta)                  OFLOW_MAX - *lengthptr < delta)
4894                {                {
4895                *errorcodeptr = ERR20;                *errorcodeptr = ERR20;
# Line 4235  we set the flag only if there is a liter Line 4905  we set the flag only if there is a liter
4905              if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte;              if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte;
4906              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
4907                {                {
4908                uschar *hc;                pcre_uchar *hc;
4909                uschar *this_hwm = cd->hwm;                pcre_uchar *this_hwm = cd->hwm;
4910                memcpy(code, previous, len);                memcpy(code, previous, len);
4911                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
4912                  {                  {
# Line 4265  we set the flag only if there is a liter Line 4935  we set the flag only if there is a liter
4935          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
4936          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
4937          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
4938          paranoid checks to avoid integer overflow. */          paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
4939            a 64-bit integer type when available, otherwise double. */
4940    
4941          if (lengthptr != NULL && repeat_max > 0)          if (lengthptr != NULL && repeat_max > 0)
4942            {            {
4943            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
4944                        2 - 2*LINK_SIZE;   /* Last one doesn't nest */                        2 - 2*LINK_SIZE;   /* Last one doesn't nest */
4945            if ((double)repeat_max *            if ((INT64_OR_DOUBLE)repeat_max *
4946                  (double)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)                  (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
4947                    > (double)INT_MAX ||                    > (INT64_OR_DOUBLE)INT_MAX ||
4948                OFLOW_MAX - *lengthptr < delta)                OFLOW_MAX - *lengthptr < delta)
4949              {              {
4950              *errorcodeptr = ERR20;              *errorcodeptr = ERR20;
# Line 4286  we set the flag only if there is a liter Line 4957  we set the flag only if there is a liter
4957    
4958          else for (i = repeat_max - 1; i >= 0; i--)          else for (i = repeat_max - 1; i >= 0; i--)
4959            {            {
4960            uschar *hc;            pcre_uchar *hc;
4961            uschar *this_hwm = cd->hwm;            pcre_uchar *this_hwm = cd->hwm;
4962    
4963            *code++ = OP_BRAZERO + repeat_type;            *code++ = OP_BRAZERO + repeat_type;
4964    
# Line 4298  we set the flag only if there is a liter Line 4969  we set the flag only if there is a liter
4969              {              {
4970              int offset;              int offset;
4971              *code++ = OP_BRA;              *code++ = OP_BRA;
4972              offset = (bralink == NULL)? 0 : code - bralink;              offset = (bralink == NULL)? 0 : (int)(code - bralink);
4973              bralink = code;              bralink = code;
4974              PUTINC(code, 0, offset);              PUTINC(code, 0, offset);
4975              }              }
# Line 4319  we set the flag only if there is a liter Line 4990  we set the flag only if there is a liter
4990          while (bralink != NULL)          while (bralink != NULL)
4991            {            {
4992            int oldlinkoffset;            int oldlinkoffset;
4993            int offset = code - bralink + 1;            int offset = (int)(code - bralink + 1);
4994            uschar *bra = code - offset;            pcre_uchar *bra = code - offset;
4995            oldlinkoffset = GET(bra, 1);            oldlinkoffset = GET(bra, 1);
4996            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
4997            *code++ = OP_KET;            *code++ = OP_KET;
# Line 4329  we set the flag only if there is a liter Line 5000  we set the flag only if there is a liter
5000            }            }
5001          }          }
5002    
5003        /* If the maximum is unlimited, set a repeater in the final copy. We        /* If the maximum is unlimited, set a repeater in the final copy. For
5004        can't just offset backwards from the current code point, because we        ONCE brackets, that's all we need to do. However, possessively repeated
5005        don't know if there's been an options resetting after the ket. The        ONCE brackets can be converted into non-capturing brackets, as the
5006        correct offset was computed above.        behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
5007          deal with possessive ONCEs specially.
5008        Then, when we are doing the actual compile phase, check to see whether  
5009        this group is a non-atomic one that could match an empty string. If so,        Otherwise, when we are doing the actual compile phase, check to see
5010          whether this group is one that could match an empty string. If so,
5011        convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so        convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
5012        that runtime checking can be done. [This check is also applied to        that runtime checking can be done. [This check is also applied to ONCE
5013        atomic groups at runtime, but in a different way.] */        groups at runtime, but in a different way.]
5014    
5015          Then, if the quantifier was possessive and the bracket is not a
5016          conditional, we convert the BRA code to the POS form, and the KET code to
5017          KETRPOS. (It turns out to be convenient at runtime to detect this kind of
5018          subpattern at both the start and at the end.) The use of special opcodes
5019          makes it possible to reduce greatly the stack usage in pcre_exec(). If
5020          the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
5021    
5022          Then, if the minimum number of matches is 1 or 0, cancel the possessive
5023          flag so that the default action below, of wrapping everything inside
5024          atomic brackets, does not happen. When the minimum is greater than 1,
5025          there will be earlier copies of the group, and so we still have to wrap
5026          the whole thing. */
5027    
5028        else        else
5029          {          {
5030          uschar *ketcode = code - ketoffset;          pcre_uchar *ketcode = code - 1 - LINK_SIZE;
5031          uschar *bracode = ketcode - GET(ketcode, 1);          pcre_uchar *bracode = ketcode - GET(ketcode, 1);
5032          *ketcode = OP_KETRMAX + repeat_type;  
5033          if (lengthptr == NULL && *bracode != OP_ONCE)          /* Convert possessive ONCE brackets to non-capturing */
5034    
5035            if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&
5036                possessive_quantifier) *bracode = OP_BRA;
5037    
5038            /* For non-possessive ONCE brackets, all we need to do is to
5039            set the KET. */
5040    
5041            if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)
5042              *ketcode = OP_KETRMAX + repeat_type;
5043    
5044            /* Handle non-ONCE brackets and possessive ONCEs (which have been
5045            converted to non-capturing above). */
5046    
5047            else
5048            {            {
5049            uschar *scode = bracode;            /* In the compile phase, check for empty string matching. */
5050            do  
5051              if (lengthptr == NULL)
5052              {              {
5053              if (could_be_empty_branch(scode, ketcode, utf8))              pcre_uchar *scode = bracode;
5054                do
5055                {                {
5056                *bracode += OP_SBRA - OP_BRA;                if (could_be_empty_branch(scode, ketcode, utf8, cd))
5057                break;                  {
5058                    *bracode += OP_SBRA - OP_BRA;
5059                    break;
5060                    }
5061                  scode += GET(scode, 1);
5062                  }
5063                while (*scode == OP_ALT);
5064                }
5065    
5066              /* Handle possessive quantifiers. */
5067    
5068              if (possessive_quantifier)
5069                {
5070                /* For COND brackets, we wrap the whole thing in a possessively
5071                repeated non-capturing bracket, because we have not invented POS
5072                versions of the COND opcodes. Because we are moving code along, we
5073                must ensure that any pending recursive references are updated. */
5074    
5075                if (*bracode == OP_COND || *bracode == OP_SCOND)
5076                  {
5077                  int nlen = (int)(code - bracode);
5078                  *code = OP_END;
5079                  adjust_recurse(bracode, 1 + LINK_SIZE, utf8, cd, save_hwm);
5080                  memmove(bracode + 1+LINK_SIZE, bracode, nlen);
5081                  code += 1 + LINK_SIZE;
5082                  nlen += 1 + LINK_SIZE;
5083                  *bracode = OP_BRAPOS;
5084                  *code++ = OP_KETRPOS;
5085                  PUTINC(code, 0, nlen);
5086                  PUT(bracode, 1, nlen);
5087                  }
5088    
5089                /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
5090    
5091                else
5092                  {
5093                  *bracode += 1;              /* Switch to xxxPOS opcodes */
5094                  *ketcode = OP_KETRPOS;
5095                }                }
5096              scode += GET(scode, 1);  
5097                /* If the minimum is zero, mark it as possessive, then unset the
5098                possessive flag when the minimum is 0 or 1. */
5099    
5100                if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
5101                if (repeat_min < 2) possessive_quantifier = FALSE;
5102              }              }
5103            while (*scode == OP_ALT);  
5104              /* Non-possessive quantifier */
5105    
5106              else *ketcode = OP_KETRMAX + repeat_type;
5107            }            }
5108          }          }
5109        }        }
# Line 4378  we set the flag only if there is a liter Line 5124  we set the flag only if there is a liter
5124        }        }
5125    
5126      /* If the character following a repeat is '+', or if certain optimization      /* If the character following a repeat is '+', or if certain optimization
5127      tests above succeeded, possessive_quantifier is TRUE. For some of the      tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
5128      simpler opcodes, there is an special alternative opcode for this. For      there are special alternative opcodes for this case. For anything else, we
5129      anything else, we wrap the entire repeated item inside OP_ONCE brackets.      wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
5130      The '+' notation is just syntactic sugar, taken from Sun's Java package,      notation is just syntactic sugar, taken from Sun's Java package, but the
5131      but the special opcodes can optimize it a bit. The repeated item starts at      special opcodes can optimize it.
5132      tempcode, not at previous, which might be the first part of a string whose  
5133      (former) last char we repeated.      Some (but not all) possessively repeated subpatterns have already been
5134        completely handled in the code just above. For them, possessive_quantifier
5135        is always FALSE at this stage.
5136    
5137        Note that the repeated item starts at tempcode, not at previous, which
5138        might be the first part of a string whose (former) last char we repeated.
5139    
5140      Possessifying an 'exact' quantifier has no effect, so we can ignore it. But      Possessifying an 'exact' quantifier has no effect, so we can ignore it. But
5141      an 'upto' may follow. We skip over an 'exact' item, and then test the      an 'upto' may follow. We skip over an 'exact' item, and then test the
# Line 4393  we set the flag only if there is a liter Line 5144  we set the flag only if there is a liter
5144      if (possessive_quantifier)      if (possessive_quantifier)
5145        {        {
5146        int len;        int len;
5147    
5148        if (*tempcode == OP_TYPEEXACT)        if (*tempcode == OP_TYPEEXACT)
5149          tempcode += _pcre_OP_lengths[*tempcode] +          tempcode += _pcre_OP_lengths[*tempcode] +
5150            ((tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP)? 2 : 0);            ((tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP)? 2 : 0);
5151    
5152        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
5153          {          {
5154          tempcode += _pcre_OP_lengths[*tempcode];          tempcode += _pcre_OP_lengths[*tempcode];
# Line 4405  we set the flag only if there is a liter Line 5156  we set the flag only if there is a liter
5156          if (utf8 && tempcode[-1] >= 0xc0)          if (utf8 && tempcode[-1] >= 0xc0)
5157            tempcode += _pcre_utf8_table4[tempcode[-1] & 0x3f];            tempcode += _pcre_utf8_table4[tempcode[-1] & 0x3f];
5158  #endif  #endif
5159          }          }
5160    
5161        len = code - tempcode;        len = (int)(code - tempcode);
5162        if (len > 0) switch (*tempcode)        if (len > 0) switch (*tempcode)
5163          {          {
5164          case OP_STAR:  *tempcode = OP_POSSTAR; break;          case OP_STAR:  *tempcode = OP_POSSTAR; break;
# Line 4415  we set the flag only if there is a liter Line 5166  we set the flag only if there is a liter
5166          case OP_QUERY: *tempcode = OP_POSQUERY; break;          case OP_QUERY: *tempcode = OP_POSQUERY; break;
5167          case OP_UPTO:  *tempcode = OP_POSUPTO; break;          case OP_UPTO:  *tempcode = OP_POSUPTO; break;
5168    
5169          case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;          case OP_STARI:  *tempcode = OP_POSSTARI; break;
5170          case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;          case OP_PLUSI:  *tempcode = OP_POSPLUSI; break;
5171          case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;          case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
5172          case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;          case OP_UPTOI:  *tempcode = OP_POSUPTOI; break;
5173    
5174          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;
5175          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;
5176          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
5177          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;
5178    
5179            case OP_NOTSTARI:  *tempcode = OP_NOTPOSSTARI; break;
5180            case OP_NOTPLUSI:  *tempcode = OP_NOTPOSPLUSI; break;
5181            case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
5182            case OP_NOTUPTOI:  *tempcode = OP_NOTPOSUPTOI; break;
5183    
5184            case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;
5185            case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;
5186            case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
5187            case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;
5188    
5189            /* Because we are moving code along, we must ensure that any
5190            pending recursive references are updated. */
5191    
5192          default:          default:
5193            *code = OP_END;
5194            adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);
5195          memmove(tempcode + 1+LINK_SIZE, tempcode, len);          memmove(tempcode + 1+LINK_SIZE, tempcode, len);
5196          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
5197          len += 1 + LINK_SIZE;          len += 1 + LINK_SIZE;
# Line 4461  we set the flag only if there is a liter Line 5227  we set the flag only if there is a liter
5227    
5228      /* First deal with various "verbs" that can be introduced by '*'. */      /* First deal with various "verbs" that can be introduced by '*'. */
5229    
5230      if (*(++ptr) == CHAR_ASTERISK && (cd->ctypes[ptr[1]] & ctype_letter) != 0)      if (*(++ptr) == CHAR_ASTERISK &&
5231             ((cd->ctypes[ptr[1]] & ctype_letter) != 0 || ptr[1] == ':'))
5232        {        {
5233        int i, namelen;        int i, namelen;
5234          int arglen = 0;
5235        const char *vn = verbnames;        const char *vn = verbnames;
5236        const uschar *name = ++ptr;        const pcre_uchar *name = ptr + 1;
5237          const pcre_uchar *arg = NULL;
5238        previous = NULL;        previous = NULL;
5239        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};
5240          namelen = (int)(ptr - name);
5241    
5242          /* It appears that Perl allows any characters whatsoever, other than
5243          a closing parenthesis, to appear in arguments, so we no longer insist on
5244          letters, digits, and underscores. */
5245    
5246        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
5247          {          {
5248          *errorcodeptr = ERR59;   /* Not supported */          arg = ++ptr;
5249          goto FAILED;          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5250            arglen = (int)(ptr - arg);
5251          }          }
5252    
5253        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
5254          {          {
5255          *errorcodeptr = ERR60;          *errorcodeptr = ERR60;
5256          goto FAILED;          goto FAILED;
5257          }          }
5258        namelen = ptr - name;  
5259          /* Scan the table of verb names */
5260    
5261        for (i = 0; i < verbcount; i++)        for (i = 0; i < verbcount; i++)
5262          {          {
5263          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
5264              strncmp((char *)name, vn, namelen) == 0)              strncmp((char *)name, vn, namelen) == 0)
5265            {            {
5266            /* Check for open captures before ACCEPT */            /* Check for open captures before ACCEPT and convert it to
5267              ASSERT_ACCEPT if in an assertion. */
5268    
5269            if (verbs[i].op == OP_ACCEPT)            if (verbs[i].op == OP_ACCEPT)
5270              {              {
5271              open_capitem *oc;              open_capitem *oc;
5272              cd->had_accept = TRUE;              if (arglen != 0)
5273                  {
5274                  *errorcodeptr = ERR59;
5275                  goto FAILED;
5276                  }
5277                cd->had_accept = TRUE;
5278              for (oc = cd->open_caps; oc != NULL; oc = oc->next)              for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5279                {                {
5280                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5281                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5282                }                }
5283              }              *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5284            *code++ = verbs[i].op;  
5285            break;              /* Do not set firstbyte after *ACCEPT */
5286                if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
5287                }
5288    
5289              /* Handle other cases with/without an argument */
5290    
5291              else if (arglen == 0)
5292                {
5293                if (verbs[i].op < 0)   /* Argument is mandatory */
5294                  {
5295                  *errorcodeptr = ERR66;
5296                  goto FAILED;
5297                  }
5298                *code = verbs[i].op;
5299                if (*code++ == OP_THEN) cd->external_flags |= PCRE_HASTHEN;
5300                }
5301    
5302              else
5303                {
5304                if (verbs[i].op_arg < 0)   /* Argument is forbidden */
5305                  {
5306                  *errorcodeptr = ERR59;
5307                  goto FAILED;
5308                  }
5309                *code = verbs[i].op_arg;
5310                if (*code++ == OP_THEN_ARG) cd->external_flags |= PCRE_HASTHEN;
5311                *code++ = arglen;
5312                memcpy(code, arg, arglen);
5313                code += arglen;
5314                *code++ = 0;
5315                }
5316    
5317              break;  /* Found verb, exit loop */
5318            }            }
5319    
5320          vn += verbs[i].len + 1;          vn += verbs[i].len + 1;
5321          }          }
5322        if (i < verbcount) continue;  
5323        *errorcodeptr = ERR60;        if (i < verbcount) continue;    /* Successfully handled a verb */
5324          *errorcodeptr = ERR60;          /* Verb not recognized */
5325        goto FAILED;        goto FAILED;
5326        }        }
5327    
# Line 4513  we set the flag only if there is a liter Line 5332  we set the flag only if there is a liter
5332        {        {
5333        int i, set, unset, namelen;        int i, set, unset, namelen;
5334        int *optset;        int *optset;
5335        const uschar *name;        const pcre_uchar *name;
5336        uschar *slot;        pcre_uchar *slot;
5337    
5338        switch (*(++ptr))        switch (*(++ptr))
5339          {          {
# Line 4621  we set the flag only if there is a liter Line 5440  we set the flag only if there is a liter
5440                recno * 10 + *ptr - CHAR_0 : -1;                recno * 10 + *ptr - CHAR_0 : -1;
5441            ptr++;            ptr++;
5442            }            }
5443          namelen = ptr - name;          namelen = (int)(ptr - name);
5444    
5445          if ((terminator > 0 && *ptr++ != terminator) ||          if ((terminator > 0 && *ptr++ != terminator) ||
5446              *ptr++ != CHAR_RIGHT_PARENTHESIS)              *ptr++ != CHAR_RIGHT_PARENTHESIS)
# Line 4658  we set the flag only if there is a liter Line 5477  we set the flag only if there is a liter
5477            }            }
5478    
5479          /* Otherwise (did not start with "+" or "-"), start by looking for the          /* Otherwise (did not start with "+" or "-"), start by looking for the
5480          name. If we find a name, add one to the opcode to change OP_CREF or          name. If we find a name, add one to the opcode to change OP_CREF or
5481          OP_RREF into OP_NCREF or OP_NRREF. These behave exactly the same,          OP_RREF into OP_NCREF or OP_NRREF. These behave exactly the same,
5482          except they record that the reference was originally to a name. The          except they record that the reference was originally to a name. The
5483          information is used to check duplicate names. */          information is used to check duplicate names. */
5484    
5485          slot = cd->name_table;          slot = cd->name_table;
# Line 4682  we set the flag only if there is a liter Line 5501  we set the flag only if there is a liter
5501          /* Search the pattern for a forward reference */          /* Search the pattern for a forward reference */
5502    
5503          else if ((i = find_parens(cd, name, namelen,          else if ((i = find_parens(cd, name, namelen,
5504                          (options & PCRE_EXTENDED) != 0)) > 0)                          (options & PCRE_EXTENDED) != 0, utf8)) > 0)
5505            {            {
5506            PUT2(code, 2+LINK_SIZE, i);            PUT2(code, 2+LINK_SIZE, i);
5507            code[1+LINK_SIZE]++;            code[1+LINK_SIZE]++;
# Line 4750  we set the flag only if there is a liter Line 5569  we set the flag only if there is a liter
5569          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5570          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */
5571          bravalue = OP_ASSERT;          bravalue = OP_ASSERT;
5572            cd->assert_depth += 1;
5573          ptr++;          ptr++;
5574          break;          break;
5575    
# Line 4764  we set the flag only if there is a liter Line 5584  we set the flag only if there is a liter
5584            continue;            continue;
5585            }            }
5586          bravalue = OP_ASSERT_NOT;          bravalue = OP_ASSERT_NOT;
5587            cd->assert_depth += 1;
5588          break;          break;
5589    
5590    
# Line 4773  we set the flag only if there is a liter Line 5594  we set the flag only if there is a liter
5594            {            {
5595            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */
5596            bravalue = OP_ASSERTBACK;            bravalue = OP_ASSERTBACK;
5597              cd->assert_depth += 1;
5598            ptr += 2;            ptr += 2;
5599            break;            break;
5600    
5601            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */
5602            bravalue = OP_ASSERTBACK_NOT;            bravalue = OP_ASSERTBACK_NOT;
5603              cd->assert_depth += 1;
5604            ptr += 2;            ptr += 2;
5605            break;            break;
5606    
# Line 4799  we set the flag only if there is a liter Line 5622  we set the flag only if there is a liter
5622    
5623          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5624          case CHAR_C:                 /* Callout - may be followed by digits; */          case CHAR_C:                 /* Callout - may be followed by digits; */
5625          previous_callout = code;  /* Save for later completion */          previous_callout = code;     /* Save for later completion */
5626          after_manual_callout = 1; /* Skip one item before completing */          after_manual_callout = 1;    /* Skip one item before completing */
5627          *code++ = OP_CALLOUT;          *code++ = OP_CALLOUT;
5628            {            {
5629            int n = 0;            int n = 0;
# Line 4817  we set the flag only if there is a liter Line 5640  we set the flag only if there is a liter
5640              goto FAILED;              goto FAILED;
5641              }              }
5642            *code++ = n;            *code++ = n;
5643            PUT(code, 0, ptr - cd->start_pattern + 1);  /* Pattern offset */            PUT(code, 0, (int)(ptr - cd->start_pattern + 1)); /* Pattern offset */
5644            PUT(code, LINK_SIZE, 0);                    /* Default length */            PUT(code, LINK_SIZE, 0);                          /* Default length */
5645            code += 2 * LINK_SIZE;            code += 2 * LINK_SIZE;
5646            }            }
5647          previous = NULL;          previous = NULL;
# Line 4851  we set the flag only if there is a liter Line 5674  we set the flag only if there is a liter
5674            name = ++ptr;            name = ++ptr;
5675    
5676            while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;            while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5677            namelen = ptr - name;            namelen = (int)(ptr - name);
5678    
5679            /* In the pre-compile phase, just do a syntax check. */            /* In the pre-compile phase, just do a syntax check. */
5680    
# Line 4887  we set the flag only if there is a liter Line 5710  we set the flag only if there is a liter
5710            is because the number of names, and hence the table size, is computed            is because the number of names, and hence the table size, is computed
5711            in the pre-compile, and it affects various numbers and pointers which            in the pre-compile, and it affects various numbers and pointers which
5712            would all have to be modified, and the compiled code moved down, if            would all have to be modified, and the compiled code moved down, if
5713            duplicates with the same number were omitted from the table. This            duplicates with the same number were omitted from the table. This
5714            doesn't seem worth the hassle. However, *different* names for the            doesn't seem worth the hassle. However, *different* names for the
5715            same number are not permitted. */            same number are not permitted. */
5716    
# Line 4895  we set the flag only if there is a liter Line 5718  we set the flag only if there is a liter
5718              {              {
5719              BOOL dupname = FALSE;              BOOL dupname = FALSE;
5720              slot = cd->name_table;              slot = cd->name_table;
5721    
5722              for (i = 0; i < cd->names_found; i++)              for (i = 0; i < cd->names_found; i++)
5723                {                {
5724                int crc = memcmp(name, slot+2, namelen);                int crc = memcmp(name, slot+2, namelen);
# Line 4909  we set the flag only if there is a liter Line 5732  we set the flag only if there is a liter
5732                      *errorcodeptr = ERR43;                      *errorcodeptr = ERR43;
5733                      goto FAILED;                      goto FAILED;
5734                      }                      }
5735                    else dupname = TRUE;                    else dupname = TRUE;
5736                    }                    }
5737                  else crc = -1;      /* Current name is a substring */                  else crc = -1;      /* Current name is a substring */
5738                  }                  }
5739    
5740                /* Make space in the table and break the loop for an earlier                /* Make space in the table and break the loop for an earlier
5741                name. For a duplicate or later name, carry on. We do this for                name. For a duplicate or later name, carry on. We do this for
5742                duplicates so that in the simple case (when ?(| is not used) they                duplicates so that in the simple case (when ?(| is not used) they
5743                are in order of their numbers. */                are in order of their numbers. */
5744    
5745                if (crc < 0)                if (crc < 0)
5746                  {                  {
5747                  memmove(slot + cd->name_entry_size, slot,                  memmove(slot + cd->name_entry_size, slot,
5748                    (cd->names_found - i) * cd->name_entry_size);                    (cd->names_found - i) * cd->name_entry_size);
5749                  break;                  break;
5750                  }                  }
5751    
5752                /* Continue the loop for a later or duplicate name */                /* Continue the loop for a later or duplicate name */
5753    
5754                slot += cd->name_entry_size;                slot += cd->name_entry_size;
5755                }                }
5756    
5757              /* For non-duplicate names, check for a duplicate number before              /* For non-duplicate names, check for a duplicate number before
5758              adding the new name. */              adding the new name. */
5759    
5760              if (!dupname)              if (!dupname)
5761                {                {
5762                uschar *cslot = cd->name_table;                pcre_uchar *cslot = cd->name_table;
5763                for (i = 0; i < cd->names_found; i++)                for (i = 0; i < cd->names_found; i++)
5764                  {                  {
5765                  if (cslot != slot)                  if (cslot != slot)
# Line 4945  we set the flag only if there is a liter Line 5768  we set the flag only if there is a liter
5768                      {                      {
5769                      *errorcodeptr = ERR65;                      *errorcodeptr = ERR65;
5770                      goto FAILED;                      goto FAILED;
5771                      }                      }
5772                    }                    }
5773                  else i--;                  else i--;
5774                  cslot += cd->name_entry_size;                  cslot += cd->name_entry_size;
5775                  }                  }
5776                }                }
5777    
5778              PUT2(slot, 0, cd->bracount + 1);              PUT2(slot, 0, cd->bracount + 1);
5779              memcpy(slot + 2, name, namelen);              memcpy(slot + 2, name, namelen);
# Line 4981  we set the flag only if there is a liter Line 5804  we set the flag only if there is a liter
5804          NAMED_REF_OR_RECURSE:          NAMED_REF_OR_RECURSE:
5805          name = ++ptr;          name = ++ptr;
5806          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5807          namelen = ptr - name;          namelen = (int)(ptr - name);
5808    
5809          /* In the pre-compile phase, do a syntax check and set a dummy          /* In the pre-compile phase, do a syntax check. We used to just set
5810          reference number. */          a dummy reference number, because it was not used in the first pass.
5811            However, with the change of recursive back references to be atomic,
5812            we have to look for the number so that this state can be identified, as
5813            otherwise the incorrect length is computed. If it's not a backwards
5814            reference, the dummy number will do. */
5815    
5816          if (lengthptr != NULL)          if (lengthptr != NULL)
5817            {            {
5818              const pcre_uchar *temp;
5819    
5820            if (namelen == 0)            if (namelen == 0)
5821              {              {
5822              *errorcodeptr = ERR62;              *errorcodeptr = ERR62;
# Line 5003  we set the flag only if there is a liter Line 5832  we set the flag only if there is a liter
5832              *errorcodeptr = ERR48;              *errorcodeptr = ERR48;
5833              goto FAILED;              goto FAILED;
5834              }              }
5835            recno = 0;  
5836              /* The name table does not exist in the first pass, so we cannot
5837              do a simple search as in the code below. Instead, we have to scan the
5838              pattern to find the number. It is important that we scan it only as
5839              far as we have got because the syntax of named subpatterns has not
5840              been checked for the rest of the pattern, and find_parens() assumes
5841              correct syntax. In any case, it's a waste of resources to scan
5842              further. We stop the scan at the current point by temporarily
5843              adjusting the value of cd->endpattern. */
5844    
5845              temp = cd->end_pattern;
5846              cd->end_pattern = ptr;
5847              recno = find_parens(cd, name, namelen,
5848                (options & PCRE_EXTENDED) != 0, utf8);
5849              cd->end_pattern = temp;
5850              if (recno < 0) recno = 0;    /* Forward ref; set dummy number */
5851            }            }
5852    
5853          /* In the real compile, seek the name in the table. We check the name          /* In the real compile, seek the name in the table. We check the name
# Line 5028  we set the flag only if there is a liter Line 5872  we set the flag only if there is a liter
5872              }              }
5873            else if ((recno =                /* Forward back reference */            else if ((recno =                /* Forward back reference */
5874                      find_parens(cd, name, namelen,                      find_parens(cd, name, namelen,
5875                        (options & PCRE_EXTENDED) != 0)) <= 0)                        (options & PCRE_EXTENDED) != 0, utf8)) <= 0)
5876              {              {
5877              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
5878              goto FAILED;              goto FAILED;
# Line 5053  we set the flag only if there is a liter Line 5897  we set the flag only if there is a liter
5897          case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:          case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:
5898          case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:          case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
5899            {            {
5900            const uschar *called;            const pcre_uchar *called;
5901            terminator = CHAR_RIGHT_PARENTHESIS;            terminator = CHAR_RIGHT_PARENTHESIS;
5902    
5903            /* Come here from the \g<...> and \g'...' code (Oniguruma            /* Come here from the \g<...> and \g'...' code (Oniguruma
# Line 5131  we set the flag only if there is a liter Line 5975  we set the flag only if there is a liter
5975            if (lengthptr == NULL)            if (lengthptr == NULL)
5976              {              {
5977              *code = OP_END;              *code = OP_END;
5978              if (recno != 0)              if (recno != 0)
5979                called = _pcre_find_bracket(cd->start_code, utf8, recno);                called = _pcre_find_bracket(cd->start_code, utf8, recno);
5980    
5981              /* Forward reference */              /* Forward reference */
# Line 5139  we set the flag only if there is a liter Line 5983  we set the flag only if there is a liter
5983              if (called == NULL)              if (called == NULL)
5984                {                {
5985                if (find_parens(cd, NULL, recno,                if (find_parens(cd, NULL, recno,
5986                      (options & PCRE_EXTENDED) != 0) < 0)                      (options & PCRE_EXTENDED) != 0, utf8) < 0)
5987                  {                  {
5988                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
5989                  goto FAILED;                  goto FAILED;
5990                  }                  }
5991    
5992                  /* Fudge the value of "called" so that when it is inserted as an
5993                  offset below, what it actually inserted is the reference number
5994                  of the group. Then remember the forward reference. */
5995    
5996                called = cd->start_code + recno;                called = cd->start_code + recno;
5997                PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code);                PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
5998                }                }