/[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 781 by zherczeg, Sat Dec 3 07:58:30 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    /* Private flags added to firstchar and reqchar. */
101    
102    #define REQ_CASELESS   0x10000000l      /* Indicates caselessness */
103    #define REQ_VARY       0x20000000l      /* Reqchar followed non-literal item */
104    
105  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
106  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 128  static const short int escapes[] = {
128       -ESC_H,                  0,       -ESC_H,                  0,
129       0,                       -ESC_K,       0,                       -ESC_K,
130       0,                       0,       0,                       0,
131       0,                       0,       -ESC_N,                  0,
132       -ESC_P,                  -ESC_Q,       -ESC_P,                  -ESC_Q,
133       -ESC_R,                  -ESC_S,       -ESC_R,                  -ESC_S,
134       0,                       0,       0,                       0,
# Line 165  static const short int escapes[] = { Line 175  static const short int escapes[] = {
175  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',
176  /*  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,
177  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,
178  /*  D0 */   '}',     0, -ESC_K,       0,      0,     0,      0, -ESC_P,  /*  D0 */   '}',     0, -ESC_K,       0,      0,-ESC_N,      0, -ESC_P,
179  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,
180  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,
181  /*  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 192  string is built from string macros so th
192  platforms. */  platforms. */
193    
194  typedef struct verbitem {  typedef struct verbitem {
195    int   len;    int   len;                 /* Length of verb name */
196    int   op;    int   op;                  /* Op when no arg, or -1 if arg mandatory */
197      int   op_arg;              /* Op when arg present, or -1 if not allowed */
198  } verbitem;  } verbitem;
199    
200  static const char verbnames[] =  static const char verbnames[] =
201      "\0"                       /* Empty name is a shorthand for MARK */
202      STRING_MARK0
203    STRING_ACCEPT0    STRING_ACCEPT0
204    STRING_COMMIT0    STRING_COMMIT0
205    STRING_F0    STRING_F0
# Line 196  static const char verbnames[] = Line 209  static const char verbnames[] =
209    STRING_THEN;    STRING_THEN;
210    
211  static const verbitem verbs[] = {  static const verbitem verbs[] = {
212    { 6, OP_ACCEPT },    { 0, -1,        OP_MARK },
213    { 6, OP_COMMIT },    { 4, -1,        OP_MARK },
214    { 1, OP_FAIL },    { 6, OP_ACCEPT, -1 },
215    { 4, OP_FAIL },    { 6, OP_COMMIT, -1 },
216    { 5, OP_PRUNE },    { 1, OP_FAIL,   -1 },
217    { 4, OP_SKIP  },    { 4, OP_FAIL,   -1 },
218    { 4, OP_THEN  }    { 5, OP_PRUNE,  OP_PRUNE_ARG },
219      { 4, OP_SKIP,   OP_SKIP_ARG  },
220      { 4, OP_THEN,   OP_THEN_ARG  }
221  };  };
222    
223  static const int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
# Line 220  static const char posix_names[] = Line 235  static const char posix_names[] =
235    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
236    STRING_word0  STRING_xdigit;    STRING_word0  STRING_xdigit;
237    
238  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
239    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 };
240    
241  /* 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 265  static const int posix_class_maps[] = {
265    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
266  };  };
267    
268    /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
269    substitutes must be in the order of the names, defined above, and there are
270    both positive and negative cases. NULL means no substitute. */
271    
272    #ifdef SUPPORT_UCP
273    static const pcre_uchar string_PNd[]  = {
274      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
275      CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
276    static const pcre_uchar string_pNd[]  = {
277      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
278      CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
279    static const pcre_uchar string_PXsp[] = {
280      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
281      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
282    static const pcre_uchar string_pXsp[] = {
283      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
284      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
285    static const pcre_uchar string_PXwd[] = {
286      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
287      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
288    static const pcre_uchar string_pXwd[] = {
289      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
290      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
291    
292    static const pcre_uchar *substitutes[] = {
293      string_PNd,           /* \D */
294      string_pNd,           /* \d */
295      string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
296      string_pXsp,          /* \s */
297      string_PXwd,          /* \W */
298      string_pXwd           /* \w */
299    };
300    
301    static const pcre_uchar string_pL[] =   {
302      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
303      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
304    static const pcre_uchar string_pLl[] =  {
305      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
306      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
307    static const pcre_uchar string_pLu[] =  {
308      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
309      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
310    static const pcre_uchar string_pXan[] = {
311      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
312      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
313    static const pcre_uchar string_h[] =    {
314      CHAR_BACKSLASH, CHAR_h, '\0' };
315    static const pcre_uchar string_pXps[] = {
316      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
317      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
318    static const pcre_uchar string_PL[] =   {
319      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
320      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
321    static const pcre_uchar string_PLl[] =  {
322      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
323      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
324    static const pcre_uchar string_PLu[] =  {
325      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
326      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
327    static const pcre_uchar string_PXan[] = {
328      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
329      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
330    static const pcre_uchar string_H[] =    {
331      CHAR_BACKSLASH, CHAR_H, '\0' };
332    static const pcre_uchar string_PXps[] = {
333      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
334      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
335    
336    static const pcre_uchar *posix_substitutes[] = {
337      string_pL,            /* alpha */
338      string_pLl,           /* lower */
339      string_pLu,           /* upper */
340      string_pXan,          /* alnum */
341      NULL,                 /* ascii */
342      string_h,             /* blank */
343      NULL,                 /* cntrl */
344      string_pNd,           /* digit */
345      NULL,                 /* graph */
346      NULL,                 /* print */
347      NULL,                 /* punct */
348      string_pXps,          /* space */    /* NOTE: Xps is POSIX space */
349      string_pXwd,          /* word */
350      NULL,                 /* xdigit */
351      /* Negated cases */
352      string_PL,            /* ^alpha */
353      string_PLl,           /* ^lower */
354      string_PLu,           /* ^upper */
355      string_PXan,          /* ^alnum */
356      NULL,                 /* ^ascii */
357      string_H,             /* ^blank */
358      NULL,                 /* ^cntrl */
359      string_PNd,           /* ^digit */
360      NULL,                 /* ^graph */
361      NULL,                 /* ^print */
362      NULL,                 /* ^punct */
363      string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
364      string_PXwd,          /* ^word */
365      NULL                  /* ^xdigit */
366    };
367    #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
368    #endif
369    
370  #define STRING(a)  # a  #define STRING(a)  # a
371  #define XSTRING(s) STRING(s)  #define XSTRING(s) STRING(s)
# Line 262  the number of relocations needed when a Line 378  the number of relocations needed when a
378  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
379  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
380  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
381  because these strings are used only when there is a compilation error. */  because these strings are used only when there is a compilation error.
382    
383    Each substring ends with \0 to insert a null character. This includes the final
384    substring, so that the whole string ends with \0\0, which can be detected when
385    counting through. */
386    
387  static const char error_texts[] =  static const char error_texts[] =
388    "no error\0"    "no error\0"
# Line 309  static const char error_texts[] = Line 429  static const char error_texts[] =
429    /* 35 */    /* 35 */
430    "invalid condition (?(0)\0"    "invalid condition (?(0)\0"
431    "\\C not allowed in lookbehind assertion\0"    "\\C not allowed in lookbehind assertion\0"
432    "PCRE does not support \\L, \\l, \\N, \\U, or \\u\0"    "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
433    "number after (?C is > 255\0"    "number after (?C is > 255\0"
434    "closing ) for (?C expected\0"    "closing ) for (?C expected\0"
435    /* 40 */    /* 40 */
# Line 331  static const char error_texts[] = Line 451  static const char error_texts[] =
451    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
452    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
453    /* 55 */    /* 55 */
454    "repeating a DEFINE group is not allowed\0"    "repeating a DEFINE group is not allowed\0"  /** DEAD **/
455    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
456    "\\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"
457    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
458    "(*VERB) with an argument is not supported\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
459    /* 60 */    /* 60 */
460    "(*VERB) not recognized\0"    "(*VERB) not recognized\0"
461    "number is too big\0"    "number is too big\0"
# Line 343  static const char error_texts[] = Line 463  static const char error_texts[] =
463    "digit expected after (?+\0"    "digit expected after (?+\0"
464    "] is an invalid data character in JavaScript compatibility mode\0"    "] is an invalid data character in JavaScript compatibility mode\0"
465    /* 65 */    /* 65 */
466    "different names for subpatterns of the same number are not allowed";    "different names for subpatterns of the same number are not allowed\0"
467      "(*MARK) must have an argument\0"
468      "this version of PCRE is not compiled with PCRE_UCP support\0"
469      "\\c must be followed by an ASCII character\0"
470      "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
471      /* 70 */
472      "internal error: unknown opcode in find_fixedlength()\0"
473      "Not allowed UTF-8 / UTF-16 code point (>= 0xd800 && <= 0xdfff)\0"
474      ;
475    
476  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
477  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 362  For convenience, we use the same bit def Line 489  For convenience, we use the same bit def
489    
490  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
491    
492    /* Using a simple comparison for decimal numbers rather than a memory read
493    is much faster, and the resulting code is simpler (the compiler turns it
494    into a subtraction and unsigned comparison). */
495    
496    #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
497    
498  #ifndef EBCDIC  #ifndef EBCDIC
499    
500  /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in  /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
501  UTF-8 mode. */  UTF-8 mode. */
502    
503  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
504    {    {
505    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
506    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */
# Line 406  static const unsigned char digitab[] = Line 539  static const unsigned char digitab[] =
539    
540  /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */  /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
541    
542  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
543    {    {
544    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
545    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */
# Line 441  static const unsigned char digitab[] = Line 574  static const unsigned char digitab[] =
574    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */
575    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */
576    
577  static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */  static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
578    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */
579    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */
580    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */
# Line 480  static const unsigned char ebcdic_charta Line 613  static const unsigned char ebcdic_charta
613  /* Definition to allow mutual recursion */  /* Definition to allow mutual recursion */
614    
615  static BOOL  static BOOL
616    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,    compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
617      int *, int *, branch_chain *, compile_data *, int *);      int *, int *, branch_chain *, compile_data *, int *);
618    
619    
# Line 502  static const char * Line 635  static const char *
635  find_error_text(int n)  find_error_text(int n)
636  {  {
637  const char *s = error_texts;  const char *s = error_texts;
638  for (; n > 0; n--) while (*s++ != 0) {};  for (; n > 0; n--)
639      {
640      while (*s++ != 0) {};
641      if (*s == 0) return "Error text not found (please report)";
642      }
643  return s;  return s;
644  }  }
645    
646    
647  /*************************************************  /*************************************************
648    *            Check for counted repeat            *
649    *************************************************/
650    
651    /* This function is called when a '{' is encountered in a place where it might
652    start a quantifier. It looks ahead to see if it really is a quantifier or not.
653    It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
654    where the ddds are digits.
655    
656    Arguments:
657      p         pointer to the first char after '{'
658    
659    Returns:    TRUE or FALSE
660    */
661    
662    static BOOL
663    is_counted_repeat(const pcre_uchar *p)
664    {
665    if (!IS_DIGIT(*p)) return FALSE;
666    p++;
667    while (IS_DIGIT(*p)) p++;
668    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
669    
670    if (*p++ != CHAR_COMMA) return FALSE;
671    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
672    
673    if (!IS_DIGIT(*p)) return FALSE;
674    p++;
675    while (IS_DIGIT(*p)) p++;
676    
677    return (*p == CHAR_RIGHT_CURLY_BRACKET);
678    }
679    
680    
681    
682    /*************************************************
683  *            Handle escapes                      *  *            Handle escapes                      *
684  *************************************************/  *************************************************/
685    
# Line 532  Returns:         zero or positive => a d Line 704  Returns:         zero or positive => a d
704  */  */
705    
706  static int  static int
707  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,
708    int options, BOOL isclass)    int options, BOOL isclass)
709  {  {
710  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
711  const uschar *ptr = *ptrptr + 1;  BOOL utf = (options & PCRE_UTF8) != 0;
712  int c, i;  const pcre_uchar *ptr = *ptrptr + 1;
713    pcre_int32 c;
714    int i;
715    
716  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
717  ptr--;                            /* Set pointer back to the last byte */  ptr--;                            /* Set pointer back to the last byte */
# Line 551  in a table. A non-zero result is somethi Line 725  in a table. A non-zero result is somethi
725  Otherwise further processing may be required. */  Otherwise further processing may be required. */
726    
727  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
728  else if (c < CHAR_0 || c > CHAR_z) {}                     /* Not alphanumeric */  /* Not alphanumeric */
729    else if (c < CHAR_0 || c > CHAR_z) {}
730  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if ((i = escapes[c - CHAR_0]) != 0) c = i;
731    
732  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
733  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */  /* Not alphanumeric */
734    else if (c < 'a' || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
735  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  c = i;
736  #endif  #endif
737    
# Line 563  else if ((i = escapes[c - 0x48]) != 0) Line 739  else if ((i = escapes[c - 0x48]) != 0)
739    
740  else  else
741    {    {
742    const uschar *oldptr;    const pcre_uchar *oldptr;
743    BOOL braced, negated;    BOOL braced, negated;
744    
745    switch (c)    switch (c)
# Line 573  else Line 749  else
749    
750      case CHAR_l:      case CHAR_l:
751      case CHAR_L:      case CHAR_L:
752      case CHAR_N:      *errorcodeptr = ERR37;
753        break;
754    
755      case CHAR_u:      case CHAR_u:
756        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
757          {
758          /* In JavaScript, \u must be followed by four hexadecimal numbers.
759          Otherwise it is a lowercase u letter. */
760          if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
761            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
762            && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
763            && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
764            {
765            c = 0;
766            for (i = 0; i < 4; ++i)
767              {
768              register int cc = *(++ptr);
769    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
770              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
771              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
772    #else           /* EBCDIC coding */
773              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
774              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
775    #endif
776              }
777            }
778          }
779        else
780          *errorcodeptr = ERR37;
781        break;
782    
783      case CHAR_U:      case CHAR_U:
784      *errorcodeptr = ERR37;      /* In JavaScript, \U is an uppercase U letter. */
785        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
786      break;      break;
787    
788      /* \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
789        class, \g must be followed by one of a number of specific things:
790    
791      (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
792      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 803  else
803      the -ESC_g code (cf \k). */      the -ESC_g code (cf \k). */
804    
805      case CHAR_g:      case CHAR_g:
806        if (isclass) break;
807      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
808        {        {
809        c = -ESC_g;        c = -ESC_g;
# Line 606  else Line 814  else
814    
815      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
816        {        {
817        const uschar *p;        const pcre_uchar *p;
818        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
819          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
820        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
821          {          {
822          c = -ESC_k;          c = -ESC_k;
# Line 626  else Line 834  else
834        }        }
835      else negated = FALSE;      else negated = FALSE;
836    
837        /* The integer range is limited by the machine's int representation. */
838      c = 0;      c = 0;
839      while ((digitab[ptr[1]] & ctype_digit) != 0)      while (IS_DIGIT(ptr[1]))
840          {
841          if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
842            {
843            c = -1;
844            break;
845            }
846        c = c * 10 + *(++ptr) - CHAR_0;        c = c * 10 + *(++ptr) - CHAR_0;
847          }
848      if (c < 0)   /* Integer overflow */      if (((unsigned int)c) > INT_MAX) /* Integer overflow */
849        {        {
850          while (IS_DIGIT(ptr[1]))
851            ptr++;
852        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
853        break;        break;
854        }        }
# Line 679  else Line 896  else
896      if (!isclass)      if (!isclass)
897        {        {
898        oldptr = ptr;        oldptr = ptr;
899          /* The integer range is limited by the machine's int representation. */
900        c -= CHAR_0;        c -= CHAR_0;
901        while ((digitab[ptr[1]] & ctype_digit) != 0)        while (IS_DIGIT(ptr[1]))
902            {
903            if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
904              {
905              c = -1;
906              break;
907              }
908          c = c * 10 + *(++ptr) - CHAR_0;          c = c * 10 + *(++ptr) - CHAR_0;
909        if (c < 0)    /* Integer overflow */          }
910          if (((unsigned int)c) > INT_MAX) /* Integer overflow */
911          {          {
912            while (IS_DIGIT(ptr[1]))
913              ptr++;
914          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
915          break;          break;
916          }          }
# Line 716  else Line 943  else
943      c -= CHAR_0;      c -= CHAR_0;
944      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
945          c = c * 8 + *(++ptr) - CHAR_0;          c = c * 8 + *(++ptr) - CHAR_0;
946      if (!utf8 && c > 255) *errorcodeptr = ERR51;      if (!utf && c > 0xff) *errorcodeptr = ERR51;
947      break;      break;
948    
949      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \x is complicated. \x{ddd} is a character number which can be greater
950      than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is      than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.
951      treated as a data character. */      If not, { is treated as a data character. */
952    
953      case CHAR_x:      case CHAR_x:
954        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
955          {
956          /* In JavaScript, \x must be followed by two hexadecimal numbers.
957          Otherwise it is a lowercase x letter. */
958          if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
959            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
960            {
961            c = 0;
962            for (i = 0; i < 2; ++i)
963              {
964              register int cc = *(++ptr);
965    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
966              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
967              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
968    #else           /* EBCDIC coding */
969              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
970              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
971    #endif
972              }
973            }
974          break;
975          }
976    
977      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
978        {        {
979        const uschar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
       int count = 0;  
980    
981        c = 0;        c = 0;
982        while ((digitab[*pt] & ctype_xdigit) != 0)        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
983          {          {
984          register int cc = *pt++;          register int cc = *pt++;
985          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
         count++;  
986    
987  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
988          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
# Line 743  else Line 991  else
991          if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */          if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
992          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
993  #endif  #endif
994    
995    #ifdef COMPILE_PCRE8
996            if (c > (utf ? 0x10ffff : 0xff)) { c = -1; break; }
997    #else
998    #ifdef COMPILE_PCRE16
999            if (c > (utf ? 0x10ffff : 0xffff)) { c = -1; break; }
1000    #endif
1001    #endif
1002            }
1003    
1004          if (c < 0)
1005            {
1006            while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1007            *errorcodeptr = ERR34;
1008          }          }
1009    
1010        if (*pt == CHAR_RIGHT_CURLY_BRACKET)        if (*pt == CHAR_RIGHT_CURLY_BRACKET)
1011          {          {
1012          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR71;
1013          ptr = pt;          ptr = pt;
1014          break;          break;
1015          }          }
# Line 759  else Line 1021  else
1021      /* Read just a single-byte hex-defined char */      /* Read just a single-byte hex-defined char */
1022    
1023      c = 0;      c = 0;
1024      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1025        {        {
1026        int cc;                                  /* Some compilers don't like */        int cc;                                  /* Some compilers don't like */
1027        cc = *(++ptr);                           /* ++ in initializers */        cc = *(++ptr);                           /* ++ in initializers */
# Line 774  else Line 1036  else
1036      break;      break;
1037    
1038      /* 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.
1039      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
1040        coding is ASCII-specific, but then the whole concept of \cx is
1041      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1042    
1043      case CHAR_c:      case CHAR_c:
# Line 784  else Line 1047  else
1047        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1048        break;        break;
1049        }        }
1050    #ifndef EBCDIC    /* ASCII/UTF-8 coding */
1051  #ifndef EBCDIC  /* ASCII/UTF-8 coding */      if (c > 127)  /* Excludes all non-ASCII in either mode */
1052          {
1053          *errorcodeptr = ERR68;
1054          break;
1055          }
1056      if (c >= CHAR_a && c <= CHAR_z) c -= 32;      if (c >= CHAR_a && c <= CHAR_z) c -= 32;
1057      c ^= 0x40;      c ^= 0x40;
1058  #else           /* EBCDIC coding */  #else             /* EBCDIC coding */
1059      if (c >= CHAR_a && c <= CHAR_z) c += 64;      if (c >= CHAR_a && c <= CHAR_z) c += 64;
1060      c ^= 0xC0;      c ^= 0xC0;
1061  #endif  #endif
# Line 811  else Line 1078  else
1078      }      }
1079    }    }
1080    
1081    /* Perl supports \N{name} for character names, as well as plain \N for "not
1082    newline". PCRE does not support \N{name}. However, it does support
1083    quantification such as \N{2,3}. */
1084    
1085    if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1086         !is_counted_repeat(ptr+2))
1087      *errorcodeptr = ERR37;
1088    
1089    /* If PCRE_UCP is set, we change the values for \d etc. */
1090    
1091    if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)
1092      c -= (ESC_DU - ESC_D);
1093    
1094    /* Set the pointer to the final character before returning. */
1095    
1096  *ptrptr = ptr;  *ptrptr = ptr;
1097  return c;  return c;
1098  }  }
# Line 837  Returns:         type value from ucp_typ Line 1119  Returns:         type value from ucp_typ
1119  */  */
1120    
1121  static int  static int
1122  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
1123  {  {
1124  int c, i, bot, top;  int c, i, bot, top;
1125  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1126  char name[32];  pcre_uchar name[32];
1127    
1128  c = *(++ptr);  c = *(++ptr);
1129  if (c == 0) goto ERROR_RETURN;  if (c == 0) goto ERROR_RETURN;
# Line 882  else Line 1164  else
1164  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1165    
1166  bot = 0;  bot = 0;
1167  top = _pcre_utt_size;  top = PRIV(utt_size);
1168    
1169  while (bot < top)  while (bot < top)
1170    {    {
1171    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1172    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    c = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1173    if (c == 0)    if (c == 0)
1174      {      {
1175      *dptr = _pcre_utt[i].value;      *dptr = PRIV(utt)[i].value;
1176      return _pcre_utt[i].type;      return PRIV(utt)[i].type;
1177      }      }
1178    if (c > 0) bot = i + 1; else top = i;    if (c > 0) bot = i + 1; else top = i;
1179    }    }
# Line 911  return -1; Line 1193  return -1;
1193    
1194    
1195  /*************************************************  /*************************************************
 *            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);  
 }  
   
   
   
 /*************************************************  
1196  *         Read repeat counts                     *  *         Read repeat counts                     *
1197  *************************************************/  *************************************************/
1198    
# Line 962  Returns:         pointer to '}' on succe Line 1211  Returns:         pointer to '}' on succe
1211                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1212  */  */
1213    
1214  static const uschar *  static const pcre_uchar *
1215  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)
1216  {  {
1217  int min = 0;  int min = 0;
1218  int max = -1;  int max = -1;
# Line 971  int max = -1; Line 1220  int max = -1;
1220  /* Read the minimum value and do a paranoid check: a negative value indicates  /* Read the minimum value and do a paranoid check: a negative value indicates
1221  an integer overflow. */  an integer overflow. */
1222    
1223  while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + *p++ - CHAR_0;
1224  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1225    {    {
1226    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 986  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1235  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1235    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1236      {      {
1237      max = 0;      max = 0;
1238      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + *p++ - CHAR_0;
1239      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1240        {        {
1241        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1019  top-level call starts at the beginning o Line 1268  top-level call starts at the beginning o
1268  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
1269  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
1270  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
1271  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
1272  encountered, the name will be terminated by '>' because that is checked in the  track of subpatterns that reset the capturing group numbers - the (?| feature.
1273  first pass. Recursion is used to keep track of subpatterns that reset the  
1274  capturing group numbers - the (?| feature.  This function was originally called only from the second pass, in which we know
1275    that if (?< or (?' or (?P< is encountered, the name will be correctly
1276    terminated because that is checked in the first pass. There is now one call to
1277    this function in the first pass, to check for a recursive back reference by
1278    name (so that we can make the whole group atomic). In this case, we need check
1279    only up to the current position in the pattern, and that is still OK because
1280    and previous occurrences will have been checked. To make this work, the test
1281    for "end of pattern" is a check against cd->end_pattern in the main loop,
1282    instead of looking for a binary zero. This means that the special first-pass
1283    call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1284    processing items within the loop are OK, because afterwards the main loop will
1285    terminate.)
1286    
1287  Arguments:  Arguments:
1288    ptrptr       address of the current character pointer (updated)    ptrptr       address of the current character pointer (updated)
# Line 1030  Arguments: Line 1290  Arguments:
1290    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1291    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1292    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1293      utf          TRUE if we are in UTF-8 / UTF-16 mode
1294    count        pointer to the current capturing subpattern number (updated)    count        pointer to the current capturing subpattern number (updated)
1295    
1296  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
1297  */  */
1298    
1299  static int  static int
1300  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,
1301    BOOL xmode, int *count)    BOOL xmode, BOOL utf, int *count)
1302  {  {
1303  uschar *ptr = *ptrptr;  pcre_uchar *ptr = *ptrptr;
1304  int start_count = *count;  int start_count = *count;
1305  int hwm_count = start_count;  int hwm_count = start_count;
1306  BOOL dup_parens = FALSE;  BOOL dup_parens = FALSE;
# Line 1049  dealing with. The very first call may no Line 1310  dealing with. The very first call may no
1310    
1311  if (ptr[0] == CHAR_LEFT_PARENTHESIS)  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1312    {    {
1313    if (ptr[1] == CHAR_QUESTION_MARK &&    /* Handle specials such as (*SKIP) or (*UTF8) etc. */
1314        ptr[2] == CHAR_VERTICAL_LINE)  
1315      if (ptr[1] == CHAR_ASTERISK) ptr += 2;
1316    
1317      /* Handle a normal, unnamed capturing parenthesis. */
1318    
1319      else if (ptr[1] != CHAR_QUESTION_MARK)
1320        {
1321        *count += 1;
1322        if (name == NULL && *count == lorn) return *count;
1323        ptr++;
1324        }
1325    
1326      /* All cases now have (? at the start. Remember when we are in a group
1327      where the parenthesis numbers are duplicated. */
1328    
1329      else if (ptr[2] == CHAR_VERTICAL_LINE)
1330      {      {
1331      ptr += 3;      ptr += 3;
1332      dup_parens = TRUE;      dup_parens = TRUE;
1333      }      }
1334    
1335    /* Handle a normal, unnamed capturing parenthesis */    /* Handle comments; all characters are allowed until a ket is reached. */
1336    
1337    else if (ptr[1] != CHAR_QUESTION_MARK && ptr[1] != CHAR_ASTERISK)    else if (ptr[2] == CHAR_NUMBER_SIGN)
1338      {      {
1339      *count += 1;      for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;
1340      if (name == NULL && *count == lorn) return *count;      goto FAIL_EXIT;
     ptr++;  
1341      }      }
1342    
1343    /* 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
1344    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
1345    condition (there can't be any nested parens. */    condition (there can't be any nested parens). */
1346    
1347    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
1348      {      {
# Line 1079  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1354  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1354        }        }
1355      }      }
1356    
1357    /* We have either (? or (* and not a condition */    /* Start with (? but not a condition. */
1358    
1359    else    else
1360      {      {
# Line 1092  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1367  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1367          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1368        {        {
1369        int term;        int term;
1370        const uschar *thisname;        const pcre_uchar *thisname;
1371        *count += 1;        *count += 1;
1372        if (name == NULL && *count == lorn) return *count;        if (name == NULL && *count == lorn) return *count;
1373        term = *ptr++;        term = *ptr++;
# Line 1100  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1375  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1375        thisname = ptr;        thisname = ptr;
1376        while (*ptr != term) ptr++;        while (*ptr != term) ptr++;
1377        if (name != NULL && lorn == ptr - thisname &&        if (name != NULL && lorn == ptr - thisname &&
1378            strncmp((const char *)name, (const char *)thisname, lorn) == 0)            STRNCMP_UC_UC(name, thisname, lorn) == 0)
1379          return *count;          return *count;
1380        term++;        term++;
1381        }        }
1382      }      }
1383    }    }
1384    
1385  /* Past any initial parenthesis handling, scan for parentheses or vertical  /* Past any initial parenthesis handling, scan for parentheses or vertical
1386  bars. */  bars. Stop if we get to cd->end_pattern. Note that this is important for the
1387    first-pass call when this value is temporarily adjusted to stop at the current
1388    position. So DO NOT change this to a test for binary zero. */
1389    
1390  for (; *ptr != 0; ptr++)  for (; ptr < cd->end_pattern; ptr++)
1391    {    {
1392    /* Skip over backslashed characters and also entire \Q...\E */    /* Skip over backslashed characters and also entire \Q...\E */
1393    
# Line 1141  for (; *ptr != 0; ptr++) Line 1418  for (; *ptr != 0; ptr++)
1418          {          {
1419          if (ptr[2] == CHAR_E)          if (ptr[2] == CHAR_E)
1420            ptr+= 2;            ptr+= 2;
1421          else if (strncmp((const char *)ptr+2,          else if (STRNCMP_UC_C8(ptr + 2,
1422                   STR_Q STR_BACKSLASH STR_E, 3) == 0)                   STR_Q STR_BACKSLASH STR_E, 3) == 0)
1423            ptr += 4;            ptr += 4;
1424          else          else
1425            break;            break;
1426          }          }
1427        else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)        else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1428          {          {
1429          negate_class = TRUE;          negate_class = TRUE;
1430          ptr++;          ptr++;
1431          }          }
1432        else break;        else break;
1433        }        }
1434    
# Line 1184  for (; *ptr != 0; ptr++) Line 1461  for (; *ptr != 0; ptr++)
1461    
1462    if (xmode && *ptr == CHAR_NUMBER_SIGN)    if (xmode && *ptr == CHAR_NUMBER_SIGN)
1463      {      {
1464      while (*(++ptr) != 0 && *ptr != CHAR_NL) {};      ptr++;
1465        while (*ptr != 0)
1466          {
1467          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1468          ptr++;
1469    #ifdef SUPPORT_UTF8
1470          if (utf) while ((*ptr & 0xc0) == 0x80) ptr++;
1471    #endif
1472          }
1473      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1474      continue;      continue;
1475      }      }
# Line 1193  for (; *ptr != 0; ptr++) Line 1478  for (; *ptr != 0; ptr++)
1478    
1479    if (*ptr == CHAR_LEFT_PARENTHESIS)    if (*ptr == CHAR_LEFT_PARENTHESIS)
1480      {      {
1481      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, count);      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, count);
1482      if (rc > 0) return rc;      if (rc > 0) return rc;
1483      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1484      }      }
# Line 1201  for (; *ptr != 0; ptr++) Line 1486  for (; *ptr != 0; ptr++)
1486    else if (*ptr == CHAR_RIGHT_PARENTHESIS)    else if (*ptr == CHAR_RIGHT_PARENTHESIS)
1487      {      {
1488      if (dup_parens && *count < hwm_count) *count = hwm_count;      if (dup_parens && *count < hwm_count) *count = hwm_count;
1489      *ptrptr = ptr;      goto FAIL_EXIT;
     return -1;  
1490      }      }
1491    
1492    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
# Line 1240  Arguments: Line 1524  Arguments:
1524    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1525    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1526    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1527      utf          TRUE if we are in UTF-8 / UTF-16 mode
1528    
1529  Returns:       the number of the found subpattern, or -1 if not found  Returns:       the number of the found subpattern, or -1 if not found
1530  */  */
1531    
1532  static int  static int
1533  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,
1534      BOOL utf)
1535  {  {
1536  uschar *ptr = (uschar *)cd->start_pattern;  pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1537  int count = 0;  int count = 0;
1538  int rc;  int rc;
1539    
# Line 1258  matching closing parens. That is why we Line 1544  matching closing parens. That is why we
1544    
1545  for (;;)  for (;;)
1546    {    {
1547    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, &count);    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, &count);
1548    if (rc > 0 || *ptr++ == 0) break;    if (rc > 0 || *ptr++ == 0) break;
1549    }    }
1550    
# Line 1274  return rc; Line 1560  return rc;
1560    
1561  /* This is called by several functions that scan a compiled expression looking  /* This is called by several functions that scan a compiled expression looking
1562  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
1563  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
1564  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
1565  assertions, and also the \b assertion; for others it does not.  does not.
1566    
1567  Arguments:  Arguments:
1568    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  
1569    skipassert   TRUE if certain assertions are to be skipped    skipassert   TRUE if certain assertions are to be skipped
1570    
1571  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1572  */  */
1573    
1574  static const uschar*  static const pcre_uchar*
1575  first_significant_code(const uschar *code, int *options, int optbit,  first_significant_code(const pcre_uchar *code, BOOL skipassert)
   BOOL skipassert)  
1576  {  {
1577  for (;;)  for (;;)
1578    {    {
1579    switch ((int)*code)    switch ((int)*code)
1580      {      {
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
1581      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1582      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1583      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1584      if (!skipassert) return code;      if (!skipassert) return code;
1585      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
1586      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1587      break;      break;
1588    
1589      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
# Line 1321  for (;;) Line 1597  for (;;)
1597      case OP_RREF:      case OP_RREF:
1598      case OP_NRREF:      case OP_NRREF:
1599      case OP_DEF:      case OP_DEF:
1600      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1601      break;      break;
1602    
1603      default:      default:
# Line 1340  for (;;) Line 1616  for (;;)
1616    
1617  /* 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,
1618  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.
1619  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
1620  temporarily terminated with OP_END when this function is called.  temporarily terminated with OP_END when this function is called.
1621    
1622  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
1623  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.
1624  However, we cannot do this when the assertion contains subroutine calls,  However, we cannot do this when the assertion contains subroutine calls,
1625  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
1626  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.
1627    
1628  Arguments:  Arguments:
1629    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1630    options  the compiling options    utf      TRUE in UTF-8 / UTF-16 mode
1631    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1632    cd       the "compile data" structure    cd       the "compile data" structure
1633    
1634  Returns:   the fixed length,  Returns:   the fixed length,
1635               or -1 if there is no fixed length,               or -1 if there is no fixed length,
1636               or -2 if \C was encountered               or -2 if \C was encountered (in UTF-8 mode only)
1637               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
1638                 or -4 if an unknown opcode was encountered (internal error)
1639  */  */
1640    
1641  static int  static int
1642  find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)  find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1643  {  {
1644  int length = -1;  int length = -1;
1645    
1646  register int branchlength = 0;  register int branchlength = 0;
1647  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1648    
1649  /* 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
1650  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 1652  branch, check the length against that of
1652  for (;;)  for (;;)
1653    {    {
1654    int d;    int d;
1655    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1656    register int op = *cc;    register int op = *cc;
1657    switch (op)    switch (op)
1658      {      {
1659        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1660        OP_BRA (normal non-capturing bracket) because the other variants of these
1661        opcodes are all concerned with unlimited repeated groups, which of course
1662        are not of fixed length. */
1663    
1664      case OP_CBRA:      case OP_CBRA:
1665      case OP_BRA:      case OP_BRA:
1666      case OP_ONCE:      case OP_ONCE:
1667        case OP_ONCE_NC:
1668      case OP_COND:      case OP_COND:
1669      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd);
1670      if (d < 0) return d;      if (d < 0) return d;
1671      branchlength += d;      branchlength += d;
1672      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1673      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1674      break;      break;
1675    
1676      /* 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.
1677      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
1678      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
1679        the same code. Note that we must not include the OP_KETRxxx opcodes here,
1680        because they all imply an unlimited repeat. */
1681    
1682      case OP_ALT:      case OP_ALT:
1683      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1684      case OP_END:      case OP_END:
1685        case OP_ACCEPT:
1686        case OP_ASSERT_ACCEPT:
1687      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1688        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
1689      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
1690      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1691      branchlength = 0;      branchlength = 0;
1692      break;      break;
1693    
1694      /* A true recursion implies not fixed length, but a subroutine call may      /* A true recursion implies not fixed length, but a subroutine call may
1695      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
1696      it until the end of the pattern, so return -3. */      it until the end of the pattern, so return -3. */
1697    
1698      case OP_RECURSE:      case OP_RECURSE:
1699      if (!atend) return -3;      if (!atend) return -3;
1700      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1701      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1702      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1703      d = find_fixedlength(cs + 2, options, atend, cd);      d = find_fixedlength(cs + 2, utf, atend, cd);
1704      if (d < 0) return d;      if (d < 0) return d;
1705      branchlength += d;      branchlength += d;
1706      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1707      break;      break;
1708    
1709      /* Skip over assertive subpatterns */      /* Skip over assertive subpatterns */
1710    
# Line 1432  for (;;) Line 1717  for (;;)
1717    
1718      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1719    
1720      case OP_REVERSE:      case OP_MARK:
1721        case OP_PRUNE_ARG:
1722        case OP_SKIP_ARG:
1723        case OP_THEN_ARG:
1724        cc += cc[1] + PRIV(OP_lengths)[*cc];
1725        break;
1726    
1727        case OP_CALLOUT:
1728        case OP_CIRC:
1729        case OP_CIRCM:
1730        case OP_CLOSE:
1731        case OP_COMMIT:
1732      case OP_CREF:      case OP_CREF:
     case OP_NCREF:  
     case OP_RREF:  
     case OP_NRREF:  
1733      case OP_DEF:      case OP_DEF:
1734      case OP_OPT:      case OP_DOLL:
1735      case OP_CALLOUT:      case OP_DOLLM:
     case OP_SOD:  
     case OP_SOM:  
1736      case OP_EOD:      case OP_EOD:
1737      case OP_EODN:      case OP_EODN:
1738      case OP_CIRC:      case OP_FAIL:
1739      case OP_DOLL:      case OP_NCREF:
1740        case OP_NRREF:
1741      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1742        case OP_PRUNE:
1743        case OP_REVERSE:
1744        case OP_RREF:
1745        case OP_SET_SOM:
1746        case OP_SKIP:
1747        case OP_SOD:
1748        case OP_SOM:
1749        case OP_THEN:
1750      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1751      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1752      break;      break;
1753    
1754      /* Handle literal characters */      /* Handle literal characters */
1755    
1756      case OP_CHAR:      case OP_CHAR:
1757      case OP_CHARNC:      case OP_CHARI:
1758      case OP_NOT:      case OP_NOT:
1759        case OP_NOTI:
1760      branchlength++;      branchlength++;
1761      cc += 2;      cc += 2;
1762  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1763      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf && cc[-1] >= 0xc0) cc += PRIV(utf8_table4)[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1764  #endif  #endif
1765      break;      break;
1766    
# Line 1468  for (;;) Line 1768  for (;;)
1768      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
1769    
1770      case OP_EXACT:      case OP_EXACT:
1771        case OP_EXACTI:
1772        case OP_NOTEXACT:
1773        case OP_NOTEXACTI:
1774      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1775      cc += 4;      cc += 2 + IMM2_SIZE;
1776  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1777      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf && cc[-1] >= 0xc0) cc += PRIV(utf8_table4)[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1778  #endif  #endif
1779      break;      break;
1780    
1781      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1782      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1783      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
1784      cc += 4;      cc += 1 + IMM2_SIZE + 1;
1785      break;      break;
1786    
1787      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1489  for (;;) Line 1791  for (;;)
1791      cc += 2;      cc += 2;
1792      /* Fall through */      /* Fall through */
1793    
1794        case OP_HSPACE:
1795        case OP_VSPACE:
1796        case OP_NOT_HSPACE:
1797        case OP_NOT_VSPACE:
1798      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1799      case OP_DIGIT:      case OP_DIGIT:
1800      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
# Line 1501  for (;;) Line 1807  for (;;)
1807      cc++;      cc++;
1808      break;      break;
1809    
1810      /* The single-byte matcher isn't allowed */      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1811        otherwise \C is coded as OP_ALLANY. */
1812    
1813      case OP_ANYBYTE:      case OP_ANYBYTE:
1814      return -2;      return -2;
1815    
1816      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1817    
1818  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || defined COMPILE_PCRE16
1819      case OP_XCLASS:      case OP_XCLASS:
1820      cc += GET(cc, 1) - 33;      cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];
1821      /* Fall through */      /* Fall through */
1822  #endif  #endif
1823    
1824      case OP_CLASS:      case OP_CLASS:
1825      case OP_NCLASS:      case OP_NCLASS:
1826      cc += 33;      cc += PRIV(OP_lengths)[OP_CLASS];
1827    
1828      switch (*cc)      switch (*cc)
1829        {        {
1830          case OP_CRPLUS:
1831          case OP_CRMINPLUS:
1832        case OP_CRSTAR:        case OP_CRSTAR:
1833        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1834        case OP_CRQUERY:        case OP_CRQUERY:
# Line 1528  for (;;) Line 1837  for (;;)
1837    
1838        case OP_CRRANGE:        case OP_CRRANGE:
1839        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1840        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1841        branchlength += GET2(cc,1);        branchlength += GET2(cc,1);
1842        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1843        break;        break;
1844    
1845        default:        default:
# Line 1540  for (;;) Line 1849  for (;;)
1849    
1850      /* Anything else is variable length */      /* Anything else is variable length */
1851    
1852      default:      case OP_ANYNL:
1853        case OP_BRAMINZERO:
1854        case OP_BRAPOS:
1855        case OP_BRAPOSZERO:
1856        case OP_BRAZERO:
1857        case OP_CBRAPOS:
1858        case OP_EXTUNI:
1859        case OP_KETRMAX:
1860        case OP_KETRMIN:
1861        case OP_KETRPOS:
1862        case OP_MINPLUS:
1863        case OP_MINPLUSI:
1864        case OP_MINQUERY:
1865        case OP_MINQUERYI:
1866        case OP_MINSTAR:
1867        case OP_MINSTARI:
1868        case OP_MINUPTO:
1869        case OP_MINUPTOI:
1870        case OP_NOTMINPLUS:
1871        case OP_NOTMINPLUSI:
1872        case OP_NOTMINQUERY:
1873        case OP_NOTMINQUERYI:
1874        case OP_NOTMINSTAR:
1875        case OP_NOTMINSTARI:
1876        case OP_NOTMINUPTO:
1877        case OP_NOTMINUPTOI:
1878        case OP_NOTPLUS:
1879        case OP_NOTPLUSI:
1880        case OP_NOTPOSPLUS:
1881        case OP_NOTPOSPLUSI:
1882        case OP_NOTPOSQUERY:
1883        case OP_NOTPOSQUERYI:
1884        case OP_NOTPOSSTAR:
1885        case OP_NOTPOSSTARI:
1886        case OP_NOTPOSUPTO:
1887        case OP_NOTPOSUPTOI:
1888        case OP_NOTQUERY:
1889        case OP_NOTQUERYI:
1890        case OP_NOTSTAR:
1891        case OP_NOTSTARI:
1892        case OP_NOTUPTO:
1893        case OP_NOTUPTOI:
1894        case OP_PLUS:
1895        case OP_PLUSI:
1896        case OP_POSPLUS:
1897        case OP_POSPLUSI:
1898        case OP_POSQUERY:
1899        case OP_POSQUERYI:
1900        case OP_POSSTAR:
1901        case OP_POSSTARI:
1902        case OP_POSUPTO:
1903        case OP_POSUPTOI:
1904        case OP_QUERY:
1905        case OP_QUERYI:
1906        case OP_REF:
1907        case OP_REFI:
1908        case OP_SBRA:
1909        case OP_SBRAPOS:
1910        case OP_SCBRA:
1911        case OP_SCBRAPOS:
1912        case OP_SCOND:
1913        case OP_SKIPZERO:
1914        case OP_STAR:
1915        case OP_STARI:
1916        case OP_TYPEMINPLUS:
1917        case OP_TYPEMINQUERY:
1918        case OP_TYPEMINSTAR:
1919        case OP_TYPEMINUPTO:
1920        case OP_TYPEPLUS:
1921        case OP_TYPEPOSPLUS:
1922        case OP_TYPEPOSQUERY:
1923        case OP_TYPEPOSSTAR:
1924        case OP_TYPEPOSUPTO:
1925        case OP_TYPEQUERY:
1926        case OP_TYPESTAR:
1927        case OP_TYPEUPTO:
1928        case OP_UPTO:
1929        case OP_UPTOI:
1930      return -1;      return -1;
1931    
1932        /* Catch unrecognized opcodes so that when new ones are added they
1933        are not forgotten, as has happened in the past. */
1934    
1935        default:
1936        return -4;
1937      }      }
1938    }    }
1939  /* Control never gets here */  /* Control never gets here */
# Line 1556  for (;;) Line 1948  for (;;)
1948    
1949  /* This little function scans through a compiled pattern until it finds a  /* This little function scans through a compiled pattern until it finds a
1950  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
1951  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
1952  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
1953  length.  length.
1954    
1955  Arguments:  Arguments:
1956    code        points to start of expression    code        points to start of expression
1957    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 mode
1958    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
1959    
1960  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
1961  */  */
1962    
1963  const uschar *  const pcre_uchar *
1964  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
1965  {  {
1966  for (;;)  for (;;)
1967    {    {
1968    register int c = *code;    register int c = *code;
1969    
1970    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1971    
1972    /* 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 1974  for (;;)
1974    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. */
1975    
1976    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
1977    
1978    /* Handle recursion */    /* Handle recursion */
1979    
1980    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
1981      {      {
1982      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
1983      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1984      }      }
1985    
1986    /* Handle capturing bracket */    /* Handle capturing bracket */
1987    
1988    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1989               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1990      {      {
1991      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
1992      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1993      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1994      }      }
1995    
1996    /* 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
1997    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
1998    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1999      must add in its length. */
2000    
2001    else    else
2002      {      {
# Line 1623  for (;;) Line 2018  for (;;)
2018        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2019        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2020        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
2021        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP
2022            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2023          break;
2024    
2025          case OP_MARK:
2026          case OP_PRUNE_ARG:
2027          case OP_SKIP_ARG:
2028          code += code[1];
2029          break;
2030    
2031          case OP_THEN_ARG:
2032          code += code[1];
2033        break;        break;
2034        }        }
2035    
2036      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2037    
2038      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2039    
2040    /* In UTF-8 mode, opcodes that are followed by a character may be followed by    /* In UTF-8 mode, opcodes that are followed by a character may be followed by
2041    a multi-byte character. The length in the table is a minimum, so we have to    a multi-byte character. The length in the table is a minimum, so we have to
2042    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
2043    
2044  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2045      if (utf8) switch(c)      if (utf) switch(c)
2046        {        {
2047        case OP_CHAR:        case OP_CHAR:
2048        case OP_CHARNC:        case OP_CHARI:
2049        case OP_EXACT:        case OP_EXACT:
2050          case OP_EXACTI:
2051        case OP_UPTO:        case OP_UPTO:
2052          case OP_UPTOI:
2053        case OP_MINUPTO:        case OP_MINUPTO:
2054          case OP_MINUPTOI:
2055        case OP_POSUPTO:        case OP_POSUPTO:
2056          case OP_POSUPTOI:
2057        case OP_STAR:        case OP_STAR:
2058          case OP_STARI:
2059        case OP_MINSTAR:        case OP_MINSTAR:
2060          case OP_MINSTARI:
2061        case OP_POSSTAR:        case OP_POSSTAR:
2062          case OP_POSSTARI:
2063        case OP_PLUS:        case OP_PLUS:
2064          case OP_PLUSI:
2065        case OP_MINPLUS:        case OP_MINPLUS:
2066          case OP_MINPLUSI:
2067        case OP_POSPLUS:        case OP_POSPLUS:
2068          case OP_POSPLUSI:
2069        case OP_QUERY:        case OP_QUERY:
2070          case OP_QUERYI:
2071        case OP_MINQUERY:        case OP_MINQUERY:
2072          case OP_MINQUERYI:
2073        case OP_POSQUERY:        case OP_POSQUERY:
2074        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2075          if (code[-1] >= 0xc0) code += PRIV(utf8_table4)[code[-1] & 0x3f];
2076        break;        break;
2077        }        }
2078  #else  #else
2079      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2080  #endif  #endif
2081      }      }
2082    }    }
# Line 1674  instance of OP_RECURSE. Line 2093  instance of OP_RECURSE.
2093    
2094  Arguments:  Arguments:
2095    code        points to start of expression    code        points to start of expression
2096    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 mode
2097    
2098  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
2099  */  */
2100    
2101  static const uschar *  static const pcre_uchar *
2102  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
2103  {  {
2104  for (;;)  for (;;)
2105    {    {
# Line 1696  for (;;) Line 2115  for (;;)
2115    
2116    /* 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
2117    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
2118    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2119      must add in its length. */
2120    
2121    else    else
2122      {      {
# Line 1718  for (;;) Line 2138  for (;;)
2138        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2139        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2140        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2141        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP
2142            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2143          break;
2144    
2145          case OP_MARK:
2146          case OP_PRUNE_ARG:
2147          case OP_SKIP_ARG:
2148          code += code[1];
2149          break;
2150    
2151          case OP_THEN_ARG:
2152          code += code[1];
2153        break;        break;
2154        }        }
2155    
2156      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2157    
2158      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2159    
2160      /* In UTF-8 mode, opcodes that are followed by a character may be followed      /* In UTF-8 mode, opcodes that are followed by a character may be followed
2161      by a multi-byte character. The length in the table is a minimum, so we have      by a multi-byte character. The length in the table is a minimum, so we have
2162      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
2163    
2164  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2165      if (utf8) switch(c)      if (utf) switch(c)
2166        {        {
2167        case OP_CHAR:        case OP_CHAR:
2168        case OP_CHARNC:        case OP_CHARI:
2169        case OP_EXACT:        case OP_EXACT:
2170          case OP_EXACTI:
2171        case OP_UPTO:        case OP_UPTO:
2172          case OP_UPTOI:
2173        case OP_MINUPTO:        case OP_MINUPTO:
2174          case OP_MINUPTOI:
2175        case OP_POSUPTO:        case OP_POSUPTO:
2176          case OP_POSUPTOI:
2177        case OP_STAR:        case OP_STAR:
2178          case OP_STARI:
2179        case OP_MINSTAR:        case OP_MINSTAR:
2180          case OP_MINSTARI:
2181        case OP_POSSTAR:        case OP_POSSTAR:
2182          case OP_POSSTARI:
2183        case OP_PLUS:        case OP_PLUS:
2184          case OP_PLUSI:
2185        case OP_MINPLUS:        case OP_MINPLUS:
2186          case OP_MINPLUSI:
2187        case OP_POSPLUS:        case OP_POSPLUS:
2188          case OP_POSPLUSI:
2189        case OP_QUERY:        case OP_QUERY:
2190          case OP_QUERYI:
2191        case OP_MINQUERY:        case OP_MINQUERY:
2192          case OP_MINQUERYI:
2193        case OP_POSQUERY:        case OP_POSQUERY:
2194        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2195          if (code[-1] >= 0xc0) code += PRIV(utf8_table4)[code[-1] & 0x3f];
2196        break;        break;
2197        }        }
2198  #else  #else
2199      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2200  #endif  #endif
2201      }      }
2202    }    }
# Line 1775  bracket whose current branch will alread Line 2219  bracket whose current branch will alread
2219  Arguments:  Arguments:
2220    code        points to start of search    code        points to start of search
2221    endcode     points to where to stop    endcode     points to where to stop
2222    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 mode
2223      cd          contains pointers to tables etc.
2224    
2225  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2226  */  */
2227    
2228  static BOOL  static BOOL
2229  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2230      BOOL utf, compile_data *cd)
2231  {  {
2232  register int c;  register int c;
2233  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2234       code < endcode;       code < endcode;
2235       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2236    {    {
2237    const uschar *ccode;    const pcre_uchar *ccode;
2238    
2239    c = *code;    c = *code;
2240    
# Line 1802  for (code = first_significant_code(code Line 2248  for (code = first_significant_code(code
2248      continue;      continue;
2249      }      }
2250    
2251      /* For a recursion/subroutine call, if its end has been reached, which
2252      implies a backward reference subroutine call, we can scan it. If it's a
2253      forward reference subroutine call, we can't. To detect forward reference
2254      we have to scan up the list that is kept in the workspace. This function is
2255      called only when doing the real compile, not during the pre-compile that
2256      measures the size of the compiled pattern. */
2257    
2258      if (c == OP_RECURSE)
2259        {
2260        const pcre_uchar *scode;
2261        BOOL empty_branch;
2262    
2263        /* Test for forward reference */
2264    
2265        for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
2266          if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;
2267    
2268        /* Not a forward reference, test for completed backward reference */
2269    
2270        empty_branch = FALSE;
2271        scode = cd->start_code + GET(code, 1);
2272        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2273    
2274        /* Completed backwards reference */
2275    
2276        do
2277          {
2278          if (could_be_empty_branch(scode, endcode, utf, cd))
2279            {
2280            empty_branch = TRUE;
2281            break;
2282            }
2283          scode += GET(scode, 1);
2284          }
2285        while (*scode == OP_ALT);
2286    
2287        if (!empty_branch) return FALSE;  /* All branches are non-empty */
2288        continue;
2289        }
2290    
2291    /* Groups with zero repeats can of course be empty; skip them. */    /* Groups with zero repeats can of course be empty; skip them. */
2292    
2293    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO)    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2294          c == OP_BRAPOSZERO)
2295        {
2296        code += PRIV(OP_lengths)[c];
2297        do code += GET(code, 1); while (*code == OP_ALT);
2298        c = *code;
2299        continue;
2300        }
2301    
2302      /* A nested group that is already marked as "could be empty" can just be
2303      skipped. */
2304    
2305      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2306          c == OP_SCBRA || c == OP_SCBRAPOS)
2307      {      {
     code += _pcre_OP_lengths[c];  
2308      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
2309      c = *code;      c = *code;
2310      continue;      continue;
# Line 1814  for (code = first_significant_code(code Line 2312  for (code = first_significant_code(code
2312    
2313    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
2314    
2315    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA  || c == OP_BRAPOS ||
2316          c == OP_CBRA || c == OP_CBRAPOS ||
2317          c == OP_ONCE || c == OP_ONCE_NC ||
2318          c == OP_COND)
2319      {      {
2320      BOOL empty_branch;      BOOL empty_branch;
2321      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 2331  for (code = first_significant_code(code
2331        empty_branch = FALSE;        empty_branch = FALSE;
2332        do        do
2333          {          {
2334          if (!empty_branch && could_be_empty_branch(code, endcode, utf8))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))
2335            empty_branch = TRUE;            empty_branch = TRUE;
2336          code += GET(code, 1);          code += GET(code, 1);
2337          }          }
# Line 1848  for (code = first_significant_code(code Line 2349  for (code = first_significant_code(code
2349      {      {
2350      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2351      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2352      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2353      actual length is stored in the compiled code, so we must update "code"      actual length is stored in the compiled code, so we must update "code"
2354      here. */      here. */
2355    
# Line 1860  for (code = first_significant_code(code Line 2361  for (code = first_significant_code(code
2361    
2362      case OP_CLASS:      case OP_CLASS:
2363      case OP_NCLASS:      case OP_NCLASS:
2364      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2365    
2366  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2367      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
# Line 1901  for (code = first_significant_code(code Line 2402  for (code = first_significant_code(code
2402      case OP_ALLANY:      case OP_ALLANY:
2403      case OP_ANYBYTE:      case OP_ANYBYTE:
2404      case OP_CHAR:      case OP_CHAR:
2405      case OP_CHARNC:      case OP_CHARI:
2406      case OP_NOT:      case OP_NOT:
2407        case OP_NOTI:
2408      case OP_PLUS:      case OP_PLUS:
2409      case OP_MINPLUS:      case OP_MINPLUS:
2410      case OP_POSPLUS:      case OP_POSPLUS:
# Line 1934  for (code = first_significant_code(code Line 2436  for (code = first_significant_code(code
2436      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2437      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2438      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2439      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP
2440          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2441      break;      break;
2442    
2443      /* End of branch */      /* End of branch */
# Line 1942  for (code = first_significant_code(code Line 2445  for (code = first_significant_code(code
2445      case OP_KET:      case OP_KET:
2446      case OP_KETRMAX:      case OP_KETRMAX:
2447      case OP_KETRMIN:      case OP_KETRMIN:
2448        case OP_KETRPOS:
2449      case OP_ALT:      case OP_ALT:
2450      return TRUE;      return TRUE;
2451    
# Line 1950  for (code = first_significant_code(code Line 2454  for (code = first_significant_code(code
2454    
2455  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2456      case OP_STAR:      case OP_STAR:
2457        case OP_STARI:
2458      case OP_MINSTAR:      case OP_MINSTAR:
2459        case OP_MINSTARI:
2460      case OP_POSSTAR:      case OP_POSSTAR:
2461        case OP_POSSTARI:
2462      case OP_QUERY:      case OP_QUERY:
2463        case OP_QUERYI:
2464      case OP_MINQUERY:      case OP_MINQUERY:
2465        case OP_MINQUERYI:
2466      case OP_POSQUERY:      case OP_POSQUERY:
2467      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      case OP_POSQUERYI:
2468        if (utf && code[1] >= 0xc0) code += PRIV(utf8_table4)[code[1] & 0x3f];
2469      break;      break;
2470    
2471      case OP_UPTO:      case OP_UPTO:
2472        case OP_UPTOI:
2473      case OP_MINUPTO:      case OP_MINUPTO:
2474        case OP_MINUPTOI:
2475      case OP_POSUPTO:      case OP_POSUPTO:
2476      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      case OP_POSUPTOI:
2477        if (utf && code[1 + IMM2_SIZE] >= 0xc0) code += PRIV(utf8_table4)[code[1 + IMM2_SIZE] & 0x3f];
2478      break;      break;
2479  #endif  #endif
2480    
2481        /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2482        string. */
2483    
2484        case OP_MARK:
2485        case OP_PRUNE_ARG:
2486        case OP_SKIP_ARG:
2487        code += code[1];
2488        break;
2489    
2490        case OP_THEN_ARG:
2491        code += code[1];
2492        break;
2493    
2494        /* None of the remaining opcodes are required to match a character. */
2495    
2496        default:
2497        break;
2498      }      }
2499    }    }
2500    
# Line 1980  return TRUE; Line 2511  return TRUE;
2511  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
2512  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,
2513  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.
2514    This function is called only during the real compile, not during the
2515    pre-compile.
2516    
2517  Arguments:  Arguments:
2518    code        points to start of the recursion    code        points to start of the recursion
2519    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2520    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2521    utf8        TRUE if in UTF-8 mode    utf         TRUE if in UTF-8 / UTF-16 mode
2522      cd          pointers to tables etc
2523    
2524  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2525  */  */
2526    
2527  static BOOL  static BOOL
2528  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2529    BOOL utf8)    branch_chain *bcptr, BOOL utf, compile_data *cd)
2530  {  {
2531  while (bcptr != NULL && bcptr->current >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2532    {    {
2533    if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))
2534        return FALSE;
2535    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2536    }    }
2537  return TRUE;  return TRUE;
# Line 2028  where Perl recognizes it as the POSIX cl Line 2563  where Perl recognizes it as the POSIX cl
2563  "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,
2564  I think.  I think.
2565    
2566    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2567    It seems that the appearance of a nested POSIX class supersedes an apparent
2568    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2569    a digit.
2570    
2571    In Perl, unescaped square brackets may also appear as part of class names. For
2572    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2573    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2574    seem right at all. PCRE does not allow closing square brackets in POSIX class
2575    names.
2576    
2577  Arguments:  Arguments:
2578    ptr      pointer to the initial [    ptr      pointer to the initial [
2579    endptr   where to return the end pointer    endptr   where to return the end pointer
# Line 2036  Returns:   TRUE or FALSE Line 2582  Returns:   TRUE or FALSE
2582  */  */
2583    
2584  static BOOL  static BOOL
2585  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2586  {  {
2587  int terminator;          /* Don't combine these lines; the Solaris cc */  int terminator;          /* Don't combine these lines; the Solaris cc */
2588  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
2589  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != 0; ptr++)
2590    {    {
2591    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) ptr++; else    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2592        ptr++;
2593      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
2594      else
2595      {      {
     if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;  
2596      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2597        {        {
2598        *endptr = ptr;        *endptr = ptr;
2599        return TRUE;        return TRUE;
2600        }        }
2601        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
2602             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
2603              ptr[1] == CHAR_EQUALS_SIGN) &&
2604            check_posix_syntax(ptr, endptr))
2605          return FALSE;
2606      }      }
2607    }    }
2608  return FALSE;  return FALSE;
# Line 2073  Returns:     a value representing the na Line 2626  Returns:     a value representing the na
2626  */  */
2627    
2628  static int  static int
2629  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
2630  {  {
2631  const char *pn = posix_names;  const char *pn = posix_names;
2632  register int yield = 0;  register int yield = 0;
2633  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
2634    {    {
2635    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
2636      strncmp((const char *)ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;
2637    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
2638    yield++;    yield++;
2639    }    }
# Line 2112  value in the reference (which is a group Line 2665  value in the reference (which is a group
2665  Arguments:  Arguments:
2666    group      points to the start of the group    group      points to the start of the group
2667    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
2668    utf8       TRUE in UTF-8 mode    utf        TRUE in UTF-8 / UTF-16 mode
2669    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
2670    save_hwm   the hwm forward reference pointer at the start of the group    save_hwm   the hwm forward reference pointer at the start of the group
2671    
# Line 2120  Returns:     nothing Line 2673  Returns:     nothing
2673  */  */
2674    
2675  static void  static void
2676  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
2677    uschar *save_hwm)    pcre_uchar *save_hwm)
2678  {  {
2679  uschar *ptr = group;  pcre_uchar *ptr = group;
2680    
2681  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
2682    {    {
2683    int offset;    int offset;
2684    uschar *hc;    pcre_uchar *hc;
2685    
2686    /* 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
2687    reference. */    reference. */
# Line 2173  Arguments: Line 2726  Arguments:
2726  Returns:         new code pointer  Returns:         new code pointer
2727  */  */
2728    
2729  static uschar *  static pcre_uchar *
2730  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2731  {  {
2732  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2733  *code++ = 255;  *code++ = 255;
2734  PUT(code, 0, ptr - cd->start_pattern);  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2735  PUT(code, LINK_SIZE, 0);                /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2736  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
2737  }  }
2738    
2739    
# Line 2202  Returns:             nothing Line 2755  Returns:             nothing
2755  */  */
2756    
2757  static void  static void
2758  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2759  {  {
2760  int length = ptr - cd->start_pattern - GET(previous_callout, 2);  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2761  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
2762  }  }
2763    
# Line 2254  for (++c; c <= d; c++) Line 2807  for (++c; c <= d; c++)
2807    
2808  return TRUE;  return TRUE;
2809  }  }
2810    
2811    
2812    
2813    /*************************************************
2814    *        Check a character and a property        *
2815    *************************************************/
2816    
2817    /* This function is called by check_auto_possessive() when a property item
2818    is adjacent to a fixed character.
2819    
2820    Arguments:
2821      c            the character
2822      ptype        the property type
2823      pdata        the data for the type
2824      negated      TRUE if it's a negated property (\P or \p{^)
2825    
2826    Returns:       TRUE if auto-possessifying is OK
2827    */
2828    
2829    static BOOL
2830    check_char_prop(int c, int ptype, int pdata, BOOL negated)
2831    {
2832    const ucd_record *prop = GET_UCD(c);
2833    switch(ptype)
2834      {
2835      case PT_LAMP:
2836      return (prop->chartype == ucp_Lu ||
2837              prop->chartype == ucp_Ll ||
2838              prop->chartype == ucp_Lt) == negated;
2839    
2840      case PT_GC:
2841      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2842    
2843      case PT_PC:
2844      return (pdata == prop->chartype) == negated;
2845    
2846      case PT_SC:
2847      return (pdata == prop->script) == negated;
2848    
2849      /* These are specials */
2850    
2851      case PT_ALNUM:
2852      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2853              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2854    
2855      case PT_SPACE:    /* Perl space */
2856      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2857              c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2858              == negated;
2859    
2860      case PT_PXSPACE:  /* POSIX space */
2861      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2862              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2863              c == CHAR_FF || c == CHAR_CR)
2864              == negated;
2865    
2866      case PT_WORD:
2867      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2868              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2869              c == CHAR_UNDERSCORE) == negated;
2870      }
2871    return FALSE;
2872    }
2873  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2874    
2875    
# Line 2267  whether the next thing could possibly ma Line 2883  whether the next thing could possibly ma
2883  sense to automatically possessify the repeated item.  sense to automatically possessify the repeated item.
2884    
2885  Arguments:  Arguments:
2886    op_code       the repeated op code    previous      pointer to the repeated opcode
2887    this          data for this item, depends on the opcode    utf           TRUE in UTF-8 / UTF-16 mode
   utf8          TRUE in UTF-8 mode  
   utf8_char     used for utf8 character bytes, NULL if not relevant  
2888    ptr           next character in pattern    ptr           next character in pattern
2889    options       options bits    options       options bits
2890    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 2279  Returns:        TRUE if possessifying is Line 2893  Returns:        TRUE if possessifying is
2893  */  */
2894    
2895  static BOOL  static BOOL
2896  check_auto_possessive(int op_code, int item, BOOL utf8, uschar *utf8_char,  check_auto_possessive(const pcre_uchar *previous, BOOL utf,
2897    const uschar *ptr, int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
2898  {  {
2899  int next;  int c, next;
2900    int op_code = *previous++;
2901    
2902  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
2903    
# Line 2293  if ((options & PCRE_EXTENDED) != 0) Line 2908  if ((options & PCRE_EXTENDED) != 0)
2908      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2909      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2910        {        {
2911        while (*(++ptr) != 0)        ptr++;
2912          while (*ptr != 0)
2913            {
2914          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2915            ptr++;
2916    #ifdef SUPPORT_UTF8
2917            if (utf) while ((*ptr & 0xc0) == 0x80) ptr++;
2918    #endif
2919            }
2920        }        }
2921      else break;      else break;
2922      }      }
# Line 2314  if (*ptr == CHAR_BACKSLASH) Line 2936  if (*ptr == CHAR_BACKSLASH)
2936  else if ((cd->ctypes[*ptr] & ctype_meta) == 0)  else if ((cd->ctypes[*ptr] & ctype_meta) == 0)
2937    {    {
2938  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2939    if (utf8) { GETCHARINC(next, ptr); } else    if (utf) { GETCHARINC(next, ptr); } else
2940  #endif  #endif
2941    next = *ptr++;    next = *ptr++;
2942    }    }
# Line 2330  if ((options & PCRE_EXTENDED) != 0) Line 2952  if ((options & PCRE_EXTENDED) != 0)
2952      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2953      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2954        {        {
2955        while (*(++ptr) != 0)        ptr++;
2956          while (*ptr != 0)
2957            {
2958          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2959            ptr++;
2960    #ifdef SUPPORT_UTF8
2961            if (utf) while ((*ptr & 0xc0) == 0x80) ptr++;
2962    #endif
2963            }
2964        }        }
2965      else break;      else break;
2966      }      }
# Line 2340  if ((options & PCRE_EXTENDED) != 0) Line 2969  if ((options & PCRE_EXTENDED) != 0)
2969  /* If the next thing is itself optional, we have to give up. */  /* If the next thing is itself optional, we have to give up. */
2970    
2971  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
2972    strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
2973      return FALSE;      return FALSE;
2974    
2975  /* 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
2976  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. */  
2977    
2978  if (next >= 0) switch(op_code)  if (next >= 0) switch(op_code)
2979    {    {
2980    case OP_CHAR:    case OP_CHAR:
2981  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2982    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
2983  #else  #else
2984    (void)(utf8_char);  /* Keep compiler happy by referencing function argument */    c = *previous;
2985  #endif  #endif
2986    return item != next;    return c != next;
2987    
2988    /* 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
2989    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
2990    high-valued characters. */    high-valued characters. */
2991    
2992    case OP_CHARNC:    case OP_CHARI:
2993  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2994    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
2995    #else
2996      c = *previous;
2997  #endif  #endif
2998    if (item == next) return FALSE;    if (c == next) return FALSE;
2999  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3000    if (utf8)    if (utf)
3001      {      {
3002      unsigned int othercase;      unsigned int othercase;
3003      if (next < 128) othercase = cd->fcc[next]; else      if (next < 128) othercase = cd->fcc[next]; else
# Line 2380  if (next >= 0) switch(op_code) Line 3006  if (next >= 0) switch(op_code)
3006  #else  #else
3007      othercase = NOTACHAR;      othercase = NOTACHAR;
3008  #endif  #endif
3009      return (unsigned int)item != othercase;      return (unsigned int)c != othercase;
3010      }      }
3011    else    else
3012  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
3013    return (item != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */
3014    
3015    /* 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
3016      opcodes are not used for multi-byte characters, because they are coded using
3017      an XCLASS instead. */
3018    
3019    case OP_NOT:    case OP_NOT:
3020    if (item == next) return TRUE;    return (c = *previous) == next;
3021    if ((options & PCRE_CASELESS) == 0) return FALSE;  
3022      case OP_NOTI:
3023      if ((c = *previous) == next) return TRUE;
3024  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3025    if (utf8)    if (utf)
3026      {      {
3027      unsigned int othercase;      unsigned int othercase;
3028      if (next < 128) othercase = cd->fcc[next]; else      if (next < 128) othercase = cd->fcc[next]; else
# Line 2401  if (next >= 0) switch(op_code) Line 3031  if (next >= 0) switch(op_code)
3031  #else  #else
3032      othercase = NOTACHAR;      othercase = NOTACHAR;
3033  #endif  #endif
3034      return (unsigned int)item == othercase;      return (unsigned int)c == othercase;
3035      }      }
3036    else    else
3037  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
3038    return (item == cd->fcc[next]);  /* Non-UTF-8 mode */    return (c == cd->fcc[next]);  /* Non-UTF-8 mode */
3039    
3040      /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3041      When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3042    
3043    case OP_DIGIT:    case OP_DIGIT:
3044    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 3081  if (next >= 0) switch(op_code)
3081      case 0x202f:      case 0x202f:
3082      case 0x205f:      case 0x205f:
3083      case 0x3000:      case 0x3000:
3084      return op_code != OP_HSPACE;      return op_code == OP_NOT_HSPACE;
3085      default:      default:
3086      return op_code == OP_HSPACE;      return op_code != OP_NOT_HSPACE;
3087      }      }
3088    
3089      case OP_ANYNL:
3090    case OP_VSPACE:    case OP_VSPACE:
3091    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3092    switch(next)    switch(next)
# Line 2464  if (next >= 0) switch(op_code) Line 3098  if (next >= 0) switch(op_code)
3098      case 0x85:      case 0x85:
3099      case 0x2028:      case 0x2028:
3100      case 0x2029:      case 0x2029:
3101      return op_code != OP_VSPACE;      return op_code == OP_NOT_VSPACE;
3102      default:      default:
3103      return op_code == OP_VSPACE;      return op_code != OP_NOT_VSPACE;
3104      }      }
3105    
3106    #ifdef SUPPORT_UCP
3107      case OP_PROP:
3108      return check_char_prop(next, previous[0], previous[1], FALSE);
3109    
3110      case OP_NOTPROP:
3111      return check_char_prop(next, previous[0], previous[1], TRUE);
3112    #endif
3113    
3114    default:    default:
3115    return FALSE;    return FALSE;
3116    }    }
3117    
3118    
3119  /* 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
3120    is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3121    generated only when PCRE_UCP is *not* set, that is, when only ASCII
3122    characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are
3123    replaced by OP_PROP codes when PCRE_UCP is set. */
3124    
3125  switch(op_code)  switch(op_code)
3126    {    {
3127    case OP_CHAR:    case OP_CHAR:
3128    case OP_CHARNC:    case OP_CHARI:
3129  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3130    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
3131    #else
3132      c = *previous;
3133  #endif  #endif
3134    switch(-next)    switch(-next)
3135      {      {
3136      case ESC_d:      case ESC_d:
3137      return item > 127 || (cd->ctypes[item] & ctype_digit) == 0;      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;
3138    
3139      case ESC_D:      case ESC_D:
3140      return item <= 127 && (cd->ctypes[item] & ctype_digit) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;
3141    
3142      case ESC_s:      case ESC_s:
3143      return item > 127 || (cd->ctypes[item] & ctype_space) == 0;      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;
3144    
3145      case ESC_S:      case ESC_S:
3146      return item <= 127 && (cd->ctypes[item] & ctype_space) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;
3147    
3148      case ESC_w:      case ESC_w:
3149      return item > 127 || (cd->ctypes[item] & ctype_word) == 0;      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;
3150    
3151      case ESC_W:      case ESC_W:
3152      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;
3153    
3154      case ESC_h:      case ESC_h:
3155      case ESC_H:      case ESC_H:
3156      switch(item)      switch(c)
3157        {        {
3158        case 0x09:        case 0x09:
3159        case 0x20:        case 0x20:
# Line 2533  switch(op_code) Line 3181  switch(op_code)
3181    
3182      case ESC_v:      case ESC_v:
3183      case ESC_V:      case ESC_V:
3184      switch(item)      switch(c)
3185        {        {
3186        case 0x0a:        case 0x0a:
3187        case 0x0b:        case 0x0b:
# Line 2547  switch(op_code) Line 3195  switch(op_code)
3195        return -next == ESC_v;        return -next == ESC_v;
3196        }        }
3197    
3198        /* When PCRE_UCP is set, these values get generated for \d etc. Find
3199        their substitutions and process them. The result will always be either
3200        -ESC_p or -ESC_P. Then fall through to process those values. */
3201    
3202    #ifdef SUPPORT_UCP
3203        case ESC_du:
3204        case ESC_DU:
3205        case ESC_wu:
3206        case ESC_WU:
3207        case ESC_su:
3208        case ESC_SU:
3209          {
3210          int temperrorcode = 0;
3211          ptr = substitutes[-next - ESC_DU];
3212          next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);
3213          if (temperrorcode != 0) return FALSE;
3214          ptr++;    /* For compatibility */
3215          }
3216        /* Fall through */
3217    
3218        case ESC_p:
3219        case ESC_P:
3220          {
3221          int ptype, pdata, errorcodeptr;
3222          BOOL negated;
3223    
3224          ptr--;      /* Make ptr point at the p or P */
3225          ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);
3226          if (ptype < 0) return FALSE;
3227          ptr++;      /* Point past the final curly ket */
3228    
3229          /* If the property item is optional, we have to give up. (When generated
3230          from \d etc by PCRE_UCP, this test will have been applied much earlier,
3231          to the original \d etc. At this point, ptr will point to a zero byte. */
3232    
3233          if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3234            STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3235              return FALSE;
3236    
3237          /* Do the property check. */
3238    
3239          return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);
3240          }
3241    #endif
3242    
3243      default:      default:
3244      return FALSE;      return FALSE;
3245      }      }
3246    
3247      /* In principle, support for Unicode properties should be integrated here as
3248      well. It means re-organizing the above code so as to get hold of the property
3249      values before switching on the op-code. However, I wonder how many patterns
3250      combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,
3251      these op-codes are never generated.) */
3252    
3253    case OP_DIGIT:    case OP_DIGIT:
3254    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||
3255           next == -ESC_h || next == -ESC_v;           next == -ESC_h || next == -ESC_v || next == -ESC_R;
3256    
3257    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
3258    return next == -ESC_d;    return next == -ESC_d;
3259    
3260    case OP_WHITESPACE:    case OP_WHITESPACE:
3261    return next == -ESC_S || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;
3262    
3263    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3264    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_s || next == -ESC_h || next == -ESC_v;
3265    
3266    case OP_HSPACE:    case OP_HSPACE:
3267    return next == -ESC_S || next == -ESC_H || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||
3268             next == -ESC_w || next == -ESC_v || next == -ESC_R;
3269    
3270    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
3271    return next == -ESC_h;    return next == -ESC_h;
3272    
3273    /* Can't have \S in here because VT matches \S (Perl anomaly) */    /* Can't have \S in here because VT matches \S (Perl anomaly) */
3274      case OP_ANYNL:
3275    case OP_VSPACE:    case OP_VSPACE:
3276    return next == -ESC_V || next == -ESC_d || next == -ESC_w;    return next == -ESC_V || next == -ESC_d || next == -ESC_w;
3277    
3278    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3279    return next == -ESC_v;    return next == -ESC_v || next == -ESC_R;
3280    
3281    case OP_WORDCHAR:    case OP_WORDCHAR:
3282    return next == -ESC_W || next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_W || next == -ESC_s || next == -ESC_h ||
3283             next == -ESC_v || next == -ESC_R;
3284    
3285    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
3286    return next == -ESC_w || next == -ESC_d;    return next == -ESC_w || next == -ESC_d;
# Line 2607  Arguments: Line 3309  Arguments:
3309    codeptr        points to the pointer to the current code point    codeptr        points to the pointer to the current code point
3310    ptrptr         points to the current pattern pointer    ptrptr         points to the current pattern pointer
3311    errorcodeptr   points to error code variable    errorcodeptr   points to error code variable
3312    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3313    reqbyteptr     set to the last literal character required, else < 0    reqcharptr     set to the last literal character required, else < 0
3314    bcptr          points to current branch chain    bcptr          points to current branch chain
3315      cond_depth     conditional nesting depth
3316    cd             contains pointers to tables etc.    cd             contains pointers to tables etc.
3317    lengthptr      NULL during the real compile phase    lengthptr      NULL during the real compile phase
3318                   points to length accumulator during pre-compile phase                   points to length accumulator during pre-compile phase
# Line 2619  Returns:         TRUE on success Line 3322  Returns:         TRUE on success
3322  */  */
3323    
3324  static BOOL  static BOOL
3325  compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3326    int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,
3327      pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,
3328    compile_data *cd, int *lengthptr)    compile_data *cd, int *lengthptr)
3329  {  {
3330  int repeat_type, op_type;  int repeat_type, op_type;
3331  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3332  int bravalue = 0;  int bravalue = 0;
3333  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3334  int firstbyte, reqbyte;  pcre_int32 firstchar, reqchar;
3335  int zeroreqbyte, zerofirstbyte;  pcre_int32 zeroreqchar, zerofirstchar;
3336  int req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
3337  int options = *optionsptr;  int options = *optionsptr;               /* May change dynamically */
3338  int after_manual_callout = 0;  int after_manual_callout = 0;
3339  int length_prevgroup = 0;  int length_prevgroup = 0;
3340  register int c;  register int c;
3341  register uschar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3342  uschar *last_code = code;  pcre_uchar *last_code = code;
3343  uschar *orig_code = code;  pcre_uchar *orig_code = code;
3344  uschar *tempcode;  pcre_uchar *tempcode;
3345  BOOL inescq = FALSE;  BOOL inescq = FALSE;
3346  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstchar = FALSE;
3347  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
3348  const uschar *tempptr;  const pcre_uchar *tempptr;
3349  uschar *previous = NULL;  const pcre_uchar *nestptr = NULL;
3350  uschar *previous_callout = NULL;  pcre_uchar *previous = NULL;
3351  uschar *save_hwm = NULL;  pcre_uchar *previous_callout = NULL;
3352  uschar classbits[32];  pcre_uchar *save_hwm = NULL;
3353    pcre_uint8 classbits[32];
3354    
3355    /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3356    must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3357    dynamically as we process the pattern. */
3358    
3359  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3360  BOOL class_utf8;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
3361  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
3362  uschar *class_utf8data;  pcre_uchar utf_chars[6];
 uschar *class_utf8data_base;  
 uschar utf8_char[6];  
3363  #else  #else
3364  BOOL utf8 = FALSE;  BOOL utf = FALSE;
 uschar *utf8_char = NULL;  
3365  #endif  #endif
3366    
3367  #ifdef DEBUG  /* Helper variables for OP_XCLASS opcode (for characters > 255). */
3368    
3369    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3370    BOOL xclass;
3371    pcre_uchar *class_uchardata;
3372    pcre_uchar *class_uchardata_base;
3373    #endif
3374    
3375    #ifdef PCRE_DEBUG
3376  if (lengthptr != NULL) DPRINTF((">> start branch\n"));  if (lengthptr != NULL) DPRINTF((">> start branch\n"));
3377  #endif  #endif
3378    
# Line 2669  greedy_non_default = greedy_default ^ 1; Line 3383  greedy_non_default = greedy_default ^ 1;
3383    
3384  /* Initialize no first byte, no required byte. REQ_UNSET means "no char  /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3385  matching encountered yet". It gets changed to REQ_NONE if we hit something that  matching encountered yet". It gets changed to REQ_NONE if we hit something that
3386  matches a non-fixed char first char; reqbyte just remains unset if we never  matches a non-fixed char first char; reqchar just remains unset if we never
3387  find one.  find one.
3388    
3389  When we hit a repeat whose minimum is zero, we may have to adjust these values  When we hit a repeat whose minimum is zero, we may have to adjust these values
3390  to take the zero repeat into account. This is implemented by setting them to  to take the zero repeat into account. This is implemented by setting them to
3391  zerofirstbyte and zeroreqbyte when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3392  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
3393    
3394  firstbyte = reqbyte = zerofirstbyte = zeroreqbyte = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;
3395    
3396  /* The variable req_caseopt contains either the REQ_CASELESS value or zero,  /* The variable req_caseopt contains either the REQ_CASELESS value
3397  according to the current setting of the caseless flag. REQ_CASELESS is a bit  or zero, according to the current setting of the caseless flag. The
3398  value > 255. It is added into the firstbyte or reqbyte variables to record the  REQ_CASELESS leaves the lower 28 bit empty. It is added into the
3399  case status of the value. This is used only for ASCII characters. */  firstchar or reqchar variables to record the case status of the
3400    value. This is used only for ASCII characters. */
3401    
3402  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
3403    
3404  /* Switch on next character until the end of the branch */  /* Switch on next character until the end of the branch */
3405    
# Line 2702  for (;; ptr++) Line 3417  for (;; ptr++)
3417    int recno;    int recno;
3418    int refsign;    int refsign;
3419    int skipbytes;    int skipbytes;
3420    int subreqbyte;    int subreqchar;
3421    int subfirstbyte;    int subfirstchar;
3422    int terminator;    int terminator;
3423    int mclength;    int mclength;
3424    uschar mcbuffer[8];    int tempbracount;
3425      pcre_uchar mcbuffer[8];
3426    
3427    /* Get next byte in the pattern */    /* Get next byte in the pattern */
3428    
3429    c = *ptr;    c = *ptr;
3430    
3431      /* If we are at the end of a nested substitution, revert to the outer level
3432      string. Nesting only happens one level deep. */
3433    
3434      if (c == 0 && nestptr != NULL)
3435        {
3436        ptr = nestptr;
3437        nestptr = NULL;
3438        c = *ptr;
3439        }
3440    
3441    /* 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
3442    previous cycle of this loop. */    previous cycle of this loop. */
3443    
3444    if (lengthptr != NULL)    if (lengthptr != NULL)
3445      {      {
3446  #ifdef DEBUG  #ifdef PCRE_DEBUG
3447      if (code > cd->hwm) cd->hwm = code;                 /* High water info */      if (code > cd->hwm) cd->hwm = code;                 /* High water info */
3448  #endif  #endif
3449      if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */      if (code > cd->start_workspace + WORK_SIZE_CHECK)   /* Check for overrun */
3450        {        {
3451        *errorcodeptr = ERR52;        *errorcodeptr = ERR52;
3452        goto FAILED;        goto FAILED;
# Line 2742  for (;; ptr++) Line 3468  for (;; ptr++)
3468        goto FAILED;        goto FAILED;
3469        }        }
3470    
3471      *lengthptr += code - last_code;      *lengthptr += (int)(code - last_code);
3472      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, code - last_code, c));      DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
3473          (int)(code - last_code), c, c));
3474    
3475      /* 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
3476      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 2753  for (;; ptr++) Line 3480  for (;; ptr++)
3480        {        {
3481        if (previous > orig_code)        if (previous > orig_code)
3482          {          {
3483          memmove(orig_code, previous, code - previous);          memmove(orig_code, previous, IN_UCHARS(code - previous));
3484          code -= previous - orig_code;          code -= previous - orig_code;
3485          previous = orig_code;          previous = orig_code;
3486          }          }
# Line 2769  for (;; ptr++) Line 3496  for (;; ptr++)
3496    /* 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
3497    reference list. */    reference list. */
3498    
3499    else if (cd->hwm > cd->start_workspace + COMPILE_WORK_SIZE)    else if (cd->hwm > cd->start_workspace + WORK_SIZE_CHECK)
3500      {      {
3501      *errorcodeptr = ERR52;      *errorcodeptr = ERR52;
3502      goto FAILED;      goto FAILED;
# Line 2817  for (;; ptr++) Line 3544  for (;; ptr++)
3544      previous_callout = NULL;      previous_callout = NULL;
3545      }      }
3546    
3547    /* In extended mode, skip white space and comments */    /* In extended mode, skip white space and comments. */
3548    
3549    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
3550      {      {
3551      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if ((cd->ctypes[c] & ctype_space) != 0) continue;
3552      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3553        {        {
3554        while (*(++ptr) != 0)        ptr++;
3555          while (*ptr != 0)
3556          {          {
3557          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3558            ptr++;
3559    #ifdef SUPPORT_UTF8
3560            if (utf) while ((*ptr & 0xc0) == 0x80) ptr++;
3561    #endif
3562          }          }
3563        if (*ptr != 0) continue;        if (*ptr != 0) continue;
3564    
# Line 2849  for (;; ptr++) Line 3581  for (;; ptr++)
3581      case 0:                        /* The branch terminates at string end */      case 0:                        /* The branch terminates at string end */
3582      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
3583      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
3584      *firstbyteptr = firstbyte;      *firstcharptr = firstchar;
3585      *reqbyteptr = reqbyte;      *reqcharptr = reqchar;
3586      *codeptr = code;      *codeptr = code;
3587      *ptrptr = ptr;      *ptrptr = ptr;
3588      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 2860  for (;; ptr++) Line 3592  for (;; ptr++)
3592          *errorcodeptr = ERR20;          *errorcodeptr = ERR20;
3593          goto FAILED;          goto FAILED;
3594          }          }
3595        *lengthptr += code - last_code;   /* To include callout length */        *lengthptr += (int)(code - last_code);   /* To include callout length */
3596        DPRINTF((">> end branch\n"));        DPRINTF((">> end branch\n"));
3597        }        }
3598      return TRUE;      return TRUE;
# Line 2871  for (;; ptr++) Line 3603  for (;; ptr++)
3603      the setting of any following char as a first character. */      the setting of any following char as a first character. */
3604    
3605      case CHAR_CIRCUMFLEX_ACCENT:      case CHAR_CIRCUMFLEX_ACCENT:
3606        previous = NULL;
3607      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3608        {        {
3609        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3610          *code++ = OP_CIRCM;
3611        }        }
3612      previous = NULL;      else *code++ = OP_CIRC;
     *code++ = OP_CIRC;  
3613      break;      break;
3614    
3615      case CHAR_DOLLAR_SIGN:      case CHAR_DOLLAR_SIGN:
3616      previous = NULL;      previous = NULL;
3617      *code++ = OP_DOLL;      *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
3618      break;      break;
3619    
3620      /* 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
3621      repeats. The value of reqbyte doesn't change either. */      repeats. The value of reqchar doesn't change either. */
3622    
3623      case CHAR_DOT:      case CHAR_DOT:
3624      if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3625      zerofirstbyte = firstbyte;      zerofirstchar = firstchar;
3626      zeroreqbyte = reqbyte;      zeroreqchar = reqchar;
3627      previous = code;      previous = code;
3628      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
3629      break;      break;
# Line 2945  for (;; ptr++) Line 3678  for (;; ptr++)
3678          {          {
3679          if (ptr[1] == CHAR_E)          if (ptr[1] == CHAR_E)
3680            ptr++;            ptr++;
3681          else if (strncmp((const char *)ptr+1,          else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0)
                           STR_Q STR_BACKSLASH STR_E, 3) == 0)  
3682            ptr += 3;            ptr += 3;
3683          else          else
3684            break;            break;
# Line 2965  for (;; ptr++) Line 3697  for (;; ptr++)
3697          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
3698        {        {
3699        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
3700        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3701        zerofirstbyte = firstbyte;        zerofirstchar = firstchar;
3702        break;        break;
3703        }        }
3704    
# Line 2988  for (;; ptr++) Line 3720  for (;; ptr++)
3720      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.
3721      */      */
3722    
3723      memset(classbits, 0, 32 * sizeof(uschar));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
3724    
3725  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3726      class_utf8 = FALSE;                       /* No chars >= 256 */      xclass = FALSE;                           /* No chars >= 256 */
3727      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For UTF-8 items */
3728      class_utf8data_base = class_utf8data;     /* For resetting in pass 1 */      class_uchardata_base = class_uchardata;   /* For resetting in pass 1 */
3729  #endif  #endif
3730    
3731      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
# Line 3002  for (;; ptr++) Line 3734  for (;; ptr++)
3734    
3735      if (c != 0) do      if (c != 0) do
3736        {        {
3737        const uschar *oldptr;        const pcre_uchar *oldptr;
3738    
3739  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3740        if (utf8 && c > 127)        if (utf && c > 127)
3741          {                           /* Braces are required because the */          {                           /* Braces are required because the */
3742          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
3743          }          }
3744    #endif
3745    
3746        /* In the pre-compile phase, accumulate the length of any UTF-8 extra  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3747          /* In the pre-compile phase, accumulate the length of any extra
3748        data and reset the pointer. This is so that very large classes that        data and reset the pointer. This is so that very large classes that
3749        contain a zillion UTF-8 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
3750        (which is on the stack). */        (which is on the stack). */
3751    
3752        if (lengthptr != NULL)        if (lengthptr != NULL)
3753          {          {
3754          *lengthptr += class_utf8data - class_utf8data_base;          *lengthptr += class_uchardata - class_uchardata_base;
3755          class_utf8data = class_utf8data_base;          class_uchardata = class_uchardata_base;
3756          }          }
   
3757  #endif  #endif
3758    
3759        /* Inside \Q...\E everything is literal except \E */        /* Inside \Q...\E everything is literal except \E */
# Line 3048  for (;; ptr++) Line 3781  for (;; ptr++)
3781          {          {
3782          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
3783          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
3784          register const uschar *cbits = cd->cbits;          register const pcre_uint8 *cbits = cd->cbits;
3785          uschar pbits[32];          pcre_uint8 pbits[32];
3786    
3787          if (ptr[1] != CHAR_COLON)          if (ptr[1] != CHAR_COLON)
3788            {            {
# Line 3065  for (;; ptr++) Line 3798  for (;; ptr++)
3798            ptr++;            ptr++;
3799            }            }
3800    
3801          posix_class = check_posix_name(ptr, tempptr - ptr);          posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
3802          if (posix_class < 0)          if (posix_class < 0)
3803            {            {
3804            *errorcodeptr = ERR30;            *errorcodeptr = ERR30;
# Line 3079  for (;; ptr++) Line 3812  for (;; ptr++)
3812          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
3813            posix_class = 0;            posix_class = 0;
3814    
3815          /* 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
3816          because we may be adding and subtracting from it, and we don't want to          different escape sequences that use Unicode properties. */
3817          subtract bits that may be in the main map already. At the end we or the  
3818          result into the bit map that is being built. */  #ifdef SUPPORT_UCP
3819            if ((options & PCRE_UCP) != 0)
3820              {
3821              int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
3822              if (posix_substitutes[pc] != NULL)
3823                {
3824                nestptr = tempptr + 1;
3825                ptr = posix_substitutes[pc] - 1;
3826                continue;
3827                }
3828              }
3829    #endif
3830            /* In the non-UCP case, we build the bit map for the POSIX class in a
3831            chunk of local store because we may be adding and subtracting from it,
3832            and we don't want to subtract bits that may be in the main map already.
3833            At the end we or the result into the bit map that is being built. */
3834    
3835          posix_class *= 3;          posix_class *= 3;
3836    
3837          /* Copy in the first table (always present) */          /* Copy in the first table (always present) */
3838    
3839          memcpy(pbits, cbits + posix_class_maps[posix_class],          memcpy(pbits, cbits + posix_class_maps[posix_class],
3840            32 * sizeof(uschar));            32 * sizeof(pcre_uint8));
3841    
3842          /* If there is a second table, add or remove it as required. */          /* If there is a second table, add or remove it as required. */
3843    
# Line 3126  for (;; ptr++) Line 3874  for (;; ptr++)
3874    
3875        /* Backslash may introduce a single character, or it may introduce one        /* Backslash may introduce a single character, or it may introduce one
3876        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
3877        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
3878        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
3879        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
3880        character in them, so set class_charcount bigger than one. */        are either treated as literal characters (by default), or are faulted if
3881          PCRE_EXTRA is set. */
3882    
3883        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
3884          {          {
3885          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3886          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
3887    
3888          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 */  
3889          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (-c == ESC_Q)            /* Handle start of quoted string */
3890            {            {
3891            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 3152  for (;; ptr++) Line 3899  for (;; ptr++)
3899    
3900          if (c < 0)          if (c < 0)
3901            {            {
3902            register const uschar *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
3903            class_charcount += 2;     /* Greater than 1 is what matters */            class_charcount += 2;     /* Greater than 1 is what matters */
3904    
3905            /* Save time by not doing this in the pre-compile phase. */            switch (-c)
   
           if (lengthptr == NULL) switch (-c)  
3906              {              {
3907    #ifdef SUPPORT_UCP
3908                case ESC_du:     /* These are the values given for \d etc */
3909                case ESC_DU:     /* when PCRE_UCP is set. We replace the */
3910                case ESC_wu:     /* escape sequence with an appropriate \p */
3911                case ESC_WU:     /* or \P to test Unicode properties instead */
3912                case ESC_su:     /* of the default ASCII testing. */
3913                case ESC_SU:
3914                nestptr = ptr;
3915                ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */
3916                class_charcount -= 2;                /* Undo! */
3917                continue;
3918    #endif
3919              case ESC_d:              case ESC_d:
3920              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
3921              continue;              continue;
# Line 3177  for (;; ptr++) Line 3934  for (;; ptr++)
3934              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
3935              continue;              continue;
3936    
3937                /* Perl 5.004 onwards omits VT from \s, but we must preserve it
3938                if it was previously set by something earlier in the character
3939                class. */
3940    
3941              case ESC_s:              case ESC_s:
3942              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];              classbits[0] |= cbits[cbit_space];
3943              classbits[1] &= ~0x08;   /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= cbits[cbit_space+1] & ~0x08;
3944                for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
3945              continue;              continue;
3946    
3947              case ESC_S:              case ESC_S:
# Line 3188  for (;; ptr++) Line 3950  for (;; ptr++)
3950              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
3951              continue;              continue;
3952    
3953              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)  
             {  
3954              SETBIT(classbits, 0x09); /* VT */              SETBIT(classbits, 0x09); /* VT */
3955              SETBIT(classbits, 0x20); /* SPACE */              SETBIT(classbits, 0x20); /* SPACE */
3956              SETBIT(classbits, 0xa0); /* NSBP */              SETBIT(classbits, 0xa0); /* NSBP */
3957  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3958              if (utf8)              if (utf)
3959                {                {
3960                class_utf8 = TRUE;                xclass = TRUE;
3961                *class_utf8data++ = XCL_SINGLE;                *class_uchardata++ = XCL_SINGLE;
3962                class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x1680, class_uchardata);
3963                *class_utf8data++ = XCL_SINGLE;                *class_uchardata++ = XCL_SINGLE;
3964                class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x180e, class_uchardata);
3965                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
3966                class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x2000, class_uchardata);
3967                class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x200A, class_uchardata);
3968                *class_utf8data++ = XCL_SINGLE;                *class_uchardata++ = XCL_SINGLE;
3969                class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x202f, class_uchardata);
3970                *class_utf8data++ = XCL_SINGLE;                *class_uchardata++ = XCL_SINGLE;
3971                class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x205f, class_uchardata);
3972                *class_utf8data++ = XCL_SINGLE;                *class_uchardata++ = XCL_SINGLE;
3973                class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x3000, class_uchardata);
3974                }                }
3975  #endif  #endif
3976              continue;              continue;
             }  
3977    
3978            if (-c == ESC_H)              case ESC_H:
             {  
3979              for (c = 0; c < 32; c++)              for (c = 0; c < 32; c++)
3980                {                {
3981                int x = 0xff;                int x = 0xff;
# Line 3242  for (;; ptr++) Line 3989  for (;; ptr++)
3989                classbits[c] |= x;                classbits[c] |= x;
3990                }                }
3991    
3992  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3993              if (utf8)              if (utf)
3994                {                {
3995                class_utf8 = TRUE;                xclass = TRUE;
3996                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
3997                class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x0100, class_uchardata);
3998                class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x167f, class_uchardata);
3999                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
4000                class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x1681, class_uchardata);
4001                class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x180d, class_uchardata);
4002                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
4003                class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x180f, class_uchardata);
4004                class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x1fff, class_uchardata);
4005                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
4006                class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x200B, class_uchardata);
4007                class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x202e, class_uchardata);
4008                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
4009                class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x2030, class_uchardata);
4010                class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x205e, class_uchardata);
4011                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
4012                class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x2060, class_uchardata);
4013                class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x2fff, class_uchardata);
4014                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
4015                class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x3001, class_uchardata);
4016                class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);
4017                }                }
4018  #endif  #endif
4019              continue;              continue;
             }  
4020    
4021            if (-c == ESC_v)              case ESC_v:
             {  
4022              SETBIT(classbits, 0x0a); /* LF */              SETBIT(classbits, 0x0a); /* LF */
4023              SETBIT(classbits, 0x0b); /* VT */              SETBIT(classbits, 0x0b); /* VT */
4024              SETBIT(classbits, 0x0c); /* FF */              SETBIT(classbits, 0x0c); /* FF */
4025              SETBIT(classbits, 0x0d); /* CR */              SETBIT(classbits, 0x0d); /* CR */
4026              SETBIT(classbits, 0x85); /* NEL */              SETBIT(classbits, 0x85); /* NEL */
4027  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4028              if (utf8)              if (utf)
4029                {                {
4030                class_utf8 = TRUE;                xclass = TRUE;
4031                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
4032                class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x2028, class_uchardata);
4033                class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x2029, class_uchardata);
4034                }                }
4035  #endif  #endif
4036              continue;              continue;
             }  
4037    
4038            if (-c == ESC_V)              case ESC_V:
             {  
4039              for (c = 0; c < 32; c++)              for (c = 0; c < 32; c++)
4040                {                {
4041                int x = 0xff;                int x = 0xff;
# Line 3309  for (;; ptr++) Line 4052  for (;; ptr++)
4052                classbits[c] |= x;                classbits[c] |= x;
4053                }                }
4054    
4055  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4056              if (utf8)              if (utf)
4057                {                {
4058                class_utf8 = TRUE;                xclass = TRUE;
4059                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
4060                class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x0100, class_uchardata);
4061                class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x2027, class_uchardata);
4062                *class_utf8data++ = XCL_RANGE;                *class_uchardata++ = XCL_RANGE;
4063                class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x2029, class_uchardata);
4064                class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);                class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);
4065                }                }
4066  #endif  #endif
4067              continue;              continue;
             }  
   
           /* We need to deal with \P and \p in both phases. */  
4068    
4069  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4070            if (-c == ESC_p || -c == ESC_P)              case ESC_p:
4071              {              case ESC_P:
4072              BOOL negated;                {
4073              int pdata;                BOOL negated;
4074              int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                int pdata;
4075              if (ptype < 0) goto FAILED;                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
4076              class_utf8 = TRUE;                if (ptype < 0) goto FAILED;
4077              *class_utf8data++ = ((-c == ESC_p) != negated)?                xclass = TRUE;
4078                XCL_PROP : XCL_NOTPROP;                *class_uchardata++ = ((-c == ESC_p) != negated)?
4079              *class_utf8data++ = ptype;                  XCL_PROP : XCL_NOTPROP;
4080              *class_utf8data++ = pdata;                *class_uchardata++ = ptype;
4081              class_charcount -= 2;   /* Not a < 256 character */                *class_uchardata++ = pdata;
4082              continue;                class_charcount -= 2;   /* Not a < 256 character */
4083              }                continue;
4084                  }
4085  #endif  #endif
4086            /* Unrecognized escapes are faulted if PCRE is running in its              /* Unrecognized escapes are faulted if PCRE is running in its
4087            strict mode. By default, for compatibility with Perl, they are              strict mode. By default, for compatibility with Perl, they are
4088            treated as literals. */              treated as literals. */
4089    
4090            if ((options & PCRE_EXTRA) != 0)              default:
4091              {              if ((options & PCRE_EXTRA) != 0)
4092              *errorcodeptr = ERR7;                {
4093              goto FAILED;                *errorcodeptr = ERR7;
4094                  goto FAILED;
4095                  }
4096                class_charcount -= 2;  /* Undo the default count from above */
4097                c = *ptr;              /* Get the final character and fall through */
4098                break;
4099              }              }
   
           class_charcount -= 2;  /* Undo the default count from above */  
           c = *ptr;              /* Get the final character and fall through */  
4100            }            }
4101    
4102          /* 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
4103          greater than 256 in UTF-8 mode. */          greater than 256 mode. */
4104    
4105          }   /* End of backslash handling */          }   /* End of backslash handling */
4106    
# Line 3406  for (;; ptr++) Line 4149  for (;; ptr++)
4149            }            }
4150    
4151  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4152          if (utf8)          if (utf)
4153            {                           /* Braces are required because the */            {                           /* Braces are required because the */
4154            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */
4155            }            }
# Line 3423  for (;; ptr++) Line 4166  for (;; ptr++)
4166            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
4167            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4168    
4169            /* \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 */  
4170    
4171            if (d < 0)            if (d < 0)
4172              {              {
4173              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  
4174                {                {
4175                ptr = oldptr;                ptr = oldptr;
4176                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto LONE_SINGLE_CHARACTER;  /* A few lines below */
# Line 3458  for (;; ptr++) Line 4198  for (;; ptr++)
4198          matching for characters > 127 is available only if UCP support is          matching for characters > 127 is available only if UCP support is
4199          available. */          available. */
4200    
4201  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4202          if (utf8 && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))          if (utf && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))
4203    #endif
4204    #ifndef COMPILE_PCRE8
4205            if (d > 255)
4206    #endif
4207    #if defined SUPPORT_UTF || defined COMPILE_PCRE16
4208            {            {
4209            class_utf8 = TRUE;            xclass = TRUE;
4210    
4211            /* With UCP support, we can find the other case equivalents of            /* With UCP support, we can find the other case equivalents of
4212            the relevant characters. There may be several ranges. Optimize how            the relevant characters. There may be several ranges. Optimize how
# Line 3494  for (;; ptr++) Line 4239  for (;; ptr++)
4239    
4240                if (occ == ocd)                if (occ == ocd)
4241                  {                  {
4242                  *class_utf8data++ = XCL_SINGLE;                  *class_uchardata++ = XCL_SINGLE;
4243                  }                  }
4244                else                else
4245                  {                  {
4246                  *class_utf8data++ = XCL_RANGE;                  *class_uchardata++ = XCL_RANGE;
4247                  class_utf8data += _pcre_ord2utf8(occ, class_utf8data);                  class_uchardata += PRIV(ord2utf)(occ, class_uchardata);
4248                  }                  }
4249                class_utf8data += _pcre_ord2utf8(ocd, class_utf8data);                class_uchardata += PRIV(ord2utf)(ocd, class_uchardata);
4250                }                }
4251              }              }
4252  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
# Line 3509  for (;; ptr++) Line 4254  for (;; ptr++)
4254            /* Now record the original range, possibly modified for UCP caseless            /* Now record the original range, possibly modified for UCP caseless
4255            overlapping ranges. */            overlapping ranges. */
4256    
4257            *class_utf8data++ = XCL_RANGE;            *class_uchardata++ = XCL_RANGE;
4258            class_utf8data += _pcre_ord2utf8(c, class_utf8data);  #ifdef SUPPORT_UTF
4259            class_utf8data += _pcre_ord2utf8(d, class_utf8data);            class_uchardata += PRIV(ord2utf)(c, class_uchardata);
4260              class_uchardata += PRIV(ord2utf)(d, class_uchardata);
4261    #else
4262              *class_uchardata++ = c;
4263              *class_uchardata++ = d;
4264    #endif
4265    
4266            /* With UCP support, we are done. Without UCP support, there is no            /* With UCP support, we are done. Without UCP support, there is no
4267            caseless matching for UTF-8 characters > 127; we can use the bit map            caseless matching for UTF characters > 127; we can use the bit map
4268            for the smaller ones. */            for the smaller ones. As for 16 bit characters without UTF, we
4269              can still use  */
4270    
4271  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4272            continue;    /* With next character in the class */            continue;    /* With next character in the class */
4273  #else  #else
4274    #ifdef SUPPORT_UTF
4275            if ((options & PCRE_CASELESS) == 0 || c > 127) continue;            if ((options & PCRE_CASELESS) == 0 || c > 127) continue;
   
4276            /* Adjust upper limit and fall through to set up the map */            /* Adjust upper limit and fall through to set up the map */
   
4277            d = 127;            d = 127;
4278    #else
4279              if (c > 255) continue;
4280              /* Adjust upper limit and fall through to set up the map */
4281              d = 255;
4282    #endif  /* SUPPORT_UTF */
4283  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
4284            }            }
4285  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 || COMPILE_PCRE16 */
4286    
4287          /* We use the bit map for all cases when not in UTF-8 mode; else          /* We use the bit map for 8 bit mode, or when the characters fall
4288          ranges that lie entirely within 0-127 when there is UCP support; else          partially or entirely to [0-255] ([0-127] for UCP) ranges. */
         for partial ranges without UCP support. */  
4289    
4290          class_charcount += d - c + 1;          class_charcount += d - c + 1;
4291          class_lastchar = d;          class_lastchar = d;
# Line 3560  for (;; ptr++) Line 4313  for (;; ptr++)
4313    
4314        /* Handle a character that cannot go in the bit map */        /* Handle a character that cannot go in the bit map */
4315    
4316  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4317        if (utf8 && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127)))        if (utf && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127)))
4318    #endif
4319    #ifndef COMPILE_PCRE8
4320          if (c > 255)
4321    #endif
4322    #if defined SUPPORT_UTF || defined COMPILE_PCRE16
4323          {          {
4324          class_utf8 = TRUE;          xclass = TRUE;
4325          *class_utf8data++ = XCL_SINGLE;          *class_uchardata++ = XCL_SINGLE;
4326          class_utf8data += _pcre_ord2utf8(c, class_utf8data);  #ifdef SUPPORT_UTF
4327            class_uchardata += PRIV(ord2utf)(c, class_uchardata);
4328    #else
4329            *class_uchardata++ = c;
4330    #endif
4331    
4332  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4333          if ((options & PCRE_CASELESS) != 0)          if ((options & PCRE_CASELESS) != 0)
# Line 3573  for (;; ptr++) Line 4335  for (;; ptr++)
4335            unsigned int othercase;            unsigned int othercase;
4336            if ((othercase = UCD_OTHERCASE(c)) != c)            if ((othercase = UCD_OTHERCASE(c)) != c)
4337              {              {
4338              *class_utf8data++ = XCL_SINGLE;              *class_uchardata++ = XCL_SINGLE;
4339              class_utf8data += _pcre_ord2utf8(othercase, class_utf8data);              class_uchardata += PRIV(ord2utf)(othercase, class_uchardata);
4340              }              }
4341            }            }
4342  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
# Line 3596  for (;; ptr++) Line 4358  for (;; ptr++)
4358          }          }
4359        }        }
4360    
4361      /* 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.
4362        If we are at the end of an internal nested string, revert to the outer
4363        string. */
4364    
4365        while (((c = *(++ptr)) != 0 ||
4366               (nestptr != NULL &&
4367                 (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != 0)) &&
4368               (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
4369    
4370      while ((c = *(++ptr)) != 0 && (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));      /* Check for missing terminating ']' */
4371    
4372      if (c == 0)                          /* Missing terminating ']' */      if (c == 0)
4373        {        {
4374        *errorcodeptr = ERR6;        *errorcodeptr = ERR6;
4375        goto FAILED;        goto FAILED;
4376        }        }
4377    
   
 /* 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  
   
   
4378      /* 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
4379      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
4380      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 4382  we set the flag only if there is a liter
4382    
4383      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
4384      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR
4385      operate on single-bytes only. This is an historical hangover. Maybe one day      operate on single-bytes characters only. This is an historical hangover.
4386      we can tidy these opcodes to handle multi-byte characters.      Maybe one day we can tidy these opcodes to handle multi-byte characters.
4387    
4388      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
4389      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.
4390      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
4391      can cause firstbyte to be set. Otherwise, there can be no first char if      case, it can cause firstchar to be set. Otherwise, there can be no first
4392      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
4393      reqbyte, save the previous value for reinstating. */      of reqchar, save the previous value for reinstating. */
4394    
4395  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4396      if (class_charcount == 1 && !class_utf8 &&      if (class_charcount == 1 && !xclass &&
4397        (!utf8 || !negate_class || class_lastchar < 128))        (!utf || !negate_class || class_lastchar < 128))
4398  #else  #else
4399      if (class_charcount == 1)      if (class_charcount == 1)
4400  #endif  #endif
4401        {        {
4402        zeroreqbyte = reqbyte;        zeroreqchar = reqchar;
4403    
4404        /* The OP_NOT opcode works on one-byte characters only. */        /* The OP_NOT[I] opcodes work on one-byte characters only. */
4405    
4406        if (negate_class)        if (negate_class)
4407          {          {
4408          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4409          zerofirstbyte = firstbyte;          zerofirstchar = firstchar;
4410          *code++ = OP_NOT;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4411          *code++ = class_lastchar;          *code++ = class_lastchar;
4412          break;          break;
4413          }          }
# Line 3666  we set the flag only if there is a liter Line 4416  we set the flag only if there is a liter
4416        then we can handle this with the normal one-character code. */        then we can handle this with the normal one-character code. */
4417    
4418  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4419        if (utf8 && class_lastchar > 127)        if (utf && class_lastchar > 127)
4420          mclength = _pcre_ord2utf8(class_lastchar, mcbuffer);          mclength = PRIV(ord2utf)(class_lastchar, mcbuffer);
4421        else        else
4422  #endif  #endif
4423          {          {
# Line 3679  we set the flag only if there is a liter Line 4429  we set the flag only if there is a liter
4429    
4430      /* The general case - not the one-char optimization. If this is the first      /* The general case - not the one-char optimization. If this is the first
4431      thing in the branch, there can be no first char setting, whatever the      thing in the branch, there can be no first char setting, whatever the
4432      repeat count. Any reqbyte setting must remain unchanged after any kind of      repeat count. Any reqchar setting must remain unchanged after any kind of
4433      repeat. */      repeat. */
4434    
4435      if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4436      zerofirstbyte = firstbyte;      zerofirstchar = firstchar;
4437      zeroreqbyte = reqbyte;      zeroreqchar = reqchar;
4438    
4439      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
4440      extended class, with its own opcode, unless there was a negated special      extended class, with its own opcode, unless there was a negated special
4441      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
4442      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
4443      (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
4444      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
4445        actual compiled code. */
4446    
4447  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4448      if (class_utf8 && !should_flip_negation)      if (xclass && (!should_flip_negation || (options & PCRE_UCP) != 0))
4449    #endif
4450    #ifndef COMPILE_PCRE8
4451        if (xclass && !should_flip_negation)
4452    #endif
4453    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4454        {        {
4455        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */        *class_uchardata++ = XCL_END;    /* Marks the end of extra data */
4456        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
4457        code += LINK_SIZE;        code += LINK_SIZE;
4458        *code = negate_class? XCL_NOT : 0;        *code = negate_class? XCL_NOT:0;
4459    
4460        /* If the map is required, move up the extra data to make room for it;        /* If the map is required, move up the extra data to make room for it;
4461        otherwise just move the code pointer to the end of the extra data. */        otherwise just move the code pointer to the end of the extra data. */
# Line 3707  we set the flag only if there is a liter Line 4463  we set the flag only if there is a liter
4463        if (class_charcount > 0)        if (class_charcount > 0)
4464          {          {
4465          *code++ |= XCL_MAP;          *code++ |= XCL_MAP;
4466          memmove(code + 32, code, class_utf8data - code);          memmove(code + (32 / sizeof(pcre_uchar)), code,
4467              IN_UCHARS(class_uchardata - code));
4468          memcpy(code, classbits, 32);          memcpy(code, classbits, 32);
4469          code = class_utf8data + 32;          code = class_uchardata + (32 / sizeof(pcre_uchar));
4470          }          }
4471        else code = class_utf8data;        else code = class_uchardata;
4472    
4473        /* Now fill in the complete length of the item */        /* Now fill in the complete length of the item */
4474    
# Line 3720  we set the flag only if there is a liter Line 4477  we set the flag only if there is a liter
4477        }        }
4478  #endif  #endif
4479    
4480      /* 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
4481      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
4482      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
4483      map into the code vector, negating it if necessary. */      (non-UCP) in the class. Then copy the 32-byte map into the code vector,
4484        negating it if necessary. */
4485    
4486      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
4487      if (negate_class)      if (lengthptr == NULL)    /* Save time in the pre-compile phase */
       {  
       if (lengthptr == NULL)    /* Save time in the pre-compile phase */  
         for (c = 0; c < 32; c++) code[c] = ~classbits[c];  
       }  
     else  
4488        {        {
4489          if (negate_class)
4490            for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
4491        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
4492        }        }
4493      code += 32;      code += 32 / sizeof(pcre_uchar);
4494      break;      break;
4495    
4496    
# Line 3772  we set the flag only if there is a liter Line 4527  we set the flag only if there is a liter
4527    
4528      if (repeat_min == 0)      if (repeat_min == 0)
4529        {        {
4530        firstbyte = zerofirstbyte;    /* Adjust for zero repeat */        firstchar = zerofirstchar;    /* Adjust for zero repeat */
4531        reqbyte = zeroreqbyte;        /* Ditto */        reqchar = zeroreqchar;        /* Ditto */
4532        }        }
4533    
4534      /* Remember whether this is a variable length repeat */      /* Remember whether this is a variable length repeat */
# Line 3783  we set the flag only if there is a liter Line 4538  we set the flag only if there is a liter
4538      op_type = 0;                    /* Default single-char op codes */      op_type = 0;                    /* Default single-char op codes */
4539      possessive_quantifier = FALSE;  /* Default not possessive quantifier */      possessive_quantifier = FALSE;  /* Default not possessive quantifier */
4540    
4541      /* 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
4542      for an inserted OP_ONCE for the additional '+' extension. */      insert something before it. */
4543    
4544      tempcode = previous;      tempcode = previous;
4545    
# Line 3807  we set the flag only if there is a liter Line 4562  we set the flag only if there is a liter
4562        }        }
4563      else repeat_type = greedy_default;      else repeat_type = greedy_default;
4564    
4565        /* If previous was a recursion call, wrap it in atomic brackets so that
4566        previous becomes the atomic group. All recursions were so wrapped in the
4567        past, but it no longer happens for non-repeated recursions. In fact, the
4568        repeated ones could be re-implemented independently so as not to need this,
4569        but for the moment we rely on the code for repeating groups. */
4570    
4571        if (*previous == OP_RECURSE)
4572          {
4573          memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
4574          *previous = OP_ONCE;
4575          PUT(previous, 1, 2 + 2*LINK_SIZE);
4576          previous[2 + 2*LINK_SIZE] = OP_KET;
4577          PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4578          code += 2 + 2 * LINK_SIZE;
4579          length_prevgroup = 3 + 3*LINK_SIZE;
4580    
4581          /* When actually compiling, we need to check whether this was a forward
4582          reference, and if so, adjust the offset. */
4583    
4584          if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4585            {
4586            int offset = GET(cd->hwm, -LINK_SIZE);
4587            if (offset == previous + 1 - cd->start_code)
4588              PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
4589            }
4590          }
4591    
4592        /* Now handle repetition for the different types of item. */
4593    
4594      /* If previous was a character match, abolish the item and generate a      /* If previous was a character match, abolish the item and generate a
4595      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
4596      that it is set in reqbyte - it might not be if a sequence such as x{3} is      that it is set in reqchar - it might not be if a sequence such as x{3} is
4597      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 firstchar
4598      instead.  */      instead.  */
4599    
4600      if (*previous == OP_CHAR || *previous == OP_CHARNC)      if (*previous == OP_CHAR || *previous == OP_CHARI)
4601        {        {
4602          op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;
4603    
4604        /* 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
4605        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
4606        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
4607        length rather than a small character. */        length rather than a small character. */
4608    
4609  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4610        if (utf8 && (code[-1] & 0x80) != 0)        if (utf && (code[-1] & 0x80) != 0)
4611          {          {
4612          uschar *lastchar = code - 1;          pcre_uchar *lastchar = code - 1;
4613          while((*lastchar & 0xc0) == 0x80) lastchar--;          while((*lastchar & 0xc0) == 0x80) lastchar--;
4614          c = code - lastchar;            /* Length of UTF-8 character */          c = code - lastchar;            /* Length of UTF-8 character */
4615          memcpy(utf8_char, lastchar, c); /* Save the char */          memcpy(utf_chars, lastchar, c); /* Save the char */
4616          c |= 0x80;                      /* Flag c as a length */          c |= 0x80;                      /* Flag c as a length */
4617          }          }
4618        else        else
# Line 3837  we set the flag only if there is a liter Line 4623  we set the flag only if there is a liter
4623    
4624          {          {
4625          c = code[-1];          c = code[-1];
4626          if (repeat_min > 1) reqbyte = c | req_caseopt | cd->req_varyopt;          if (repeat_min > 1) reqchar = c | req_caseopt | cd->req_varyopt;
4627          }          }
4628    
4629        /* If the repetition is unlimited, it pays to see if the next thing on        /* If the repetition is unlimited, it pays to see if the next thing on
# Line 3847  we set the flag only if there is a liter Line 4633  we set the flag only if there is a liter
4633    
4634        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4635            repeat_max < 0 &&            repeat_max < 0 &&
4636            check_auto_possessive(*previous, c, utf8, utf8_char, ptr + 1,            check_auto_possessive(previous, utf, ptr + 1, options, cd))
             options, cd))  
4637          {          {
4638          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4639          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 3860  we set the flag only if there is a liter Line 4645  we set the flag only if there is a liter
4645      /* If previous was a single negated character ([^a] or similar), we use      /* If previous was a single negated character ([^a] or similar), we use
4646      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-
4647      character repeats by setting opt_type to add a suitable offset into      character repeats by setting opt_type to add a suitable offset into
4648      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
4649      currently used only for single-byte chars. */      are currently used only for single-byte chars. */
4650    
4651      else if (*previous == OP_NOT)      else if (*previous == OP_NOT || *previous == OP_NOTI)
4652        {        {
4653        op_type = OP_NOTSTAR - OP_STAR;  /* Use "not" opcodes */        op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;
4654        c = previous[1];        c = previous[1];
4655        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4656            repeat_max < 0 &&            repeat_max < 0 &&
4657            check_auto_possessive(OP_NOT, c, utf8, NULL, ptr + 1, options, cd))            check_auto_possessive(previous, utf, ptr + 1, options, cd))
4658          {          {
4659          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4660          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 3886  we set the flag only if there is a liter Line 4671  we set the flag only if there is a liter
4671    
4672      else if (*previous < OP_EODN)      else if (*previous < OP_EODN)
4673        {        {
4674        uschar *oldcode;        pcre_uchar *oldcode;
4675        int prop_type, prop_value;        int prop_type, prop_value;
4676        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
4677        c = *previous;        c = *previous;
4678    
4679        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4680            repeat_max < 0 &&            repeat_max < 0 &&
4681            check_auto_possessive(c, 0, utf8, NULL, ptr + 1, options, cd))            check_auto_possessive(previous, utf, ptr + 1, options, cd))
4682          {          {
4683          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4684          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 3915  we set the flag only if there is a liter Line 4700  we set the flag only if there is a liter
4700    
4701        if (repeat_max == 0) goto END_REPEAT;        if (repeat_max == 0) goto END_REPEAT;
4702