/[pcre]/code/trunk/pcre_compile.c
ViewVC logotype

Diff of /code/trunk/pcre_compile.c

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

revision 535 by ph10, Thu Jun 3 19:18:24 2010 UTC revision 1045 by ph10, Sun Sep 23 16:50:00 2012 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-2010 University of Cambridge             Copyright (c) 1997-2012 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 PCRE_DEBUG is defined, we need the pcre_printint() function, which is  /* When PCRE_DEBUG is defined, we need the pcre(16)_printint() function, which
57  also used by pcretest. PCRE_DEBUG is not defined when building a production  is also used by pcretest. PCRE_DEBUG is not defined when building a production
58  library. */  library. We do not need to select pcre16_printint.c specially, because the
59    COMPILE_PCREx macro will already be appropriately set. */
60    
61  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
62  #include "pcre_printint.src"  /* pcre_printint.c should not include any headers */
63    #define PCRE_INCLUDED
64    #include "pcre_printint.c"
65    #undef PCRE_INCLUDED
66  #endif  #endif
67    
68    
69  /* Macro for setting individual bits in class bitmaps. */  /* Macro for setting individual bits in class bitmaps. */
70    
71  #define SETBIT(a,b) a[b/8] |= (1 << (b%8))  #define SETBIT(a,b) a[(b)/8] |= (1 << ((b)&7))
72    
73  /* Maximum length value to check against when making sure that the integer that  /* Maximum length value to check against when making sure that the integer that
74  holds the compiled pattern length does not overflow. We make it a bit less than  holds the compiled pattern length does not overflow. We make it a bit less than
# Line 73  to check them every time. */ Line 77  to check them every time. */
77    
78  #define OFLOW_MAX (INT_MAX - 20)  #define OFLOW_MAX (INT_MAX - 20)
79    
80    /* Definitions to allow mutual recursion */
81    
82    static int
83      add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
84        const pcre_uint32 *, unsigned int);
85    
86    static BOOL
87      compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL,
88        int, int, int *, int *, branch_chain *, compile_data *, int *);
89    
90    
91    
92  /*************************************************  /*************************************************
93  *      Code parameters and static tables         *  *      Code parameters and static tables         *
# Line 88  so this number is very generous. Line 103  so this number is very generous.
103  The same workspace is used during the second, actual compile phase for  The same workspace is used during the second, actual compile phase for
104  remembering forward references to groups so that they can be filled in at the  remembering forward references to groups so that they can be filled in at the
105  end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE  end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
106  is 4 there is plenty of room. */  is 4 there is plenty of room for most patterns. However, the memory can get
107    filled up by repetitions of forward references, for example patterns like
108    /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
109    that the workspace is expanded using malloc() in this situation. The value
110    below is therefore a minimum, and we put a maximum on it for safety. The
111    minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
112    kicks in at the same number of forward references in all cases. */
113    
114  #define COMPILE_WORK_SIZE (4096)  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
115    #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
116    
117  /* The overrun tests check for a slightly smaller size so that they detect the  /* The overrun tests check for a slightly smaller size so that they detect the
118  overrun before it actually does run off the end of the data block. */  overrun before it actually does run off the end of the data block. */
119    
120  #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)  #define WORK_SIZE_SAFETY_MARGIN (100)
121    
122    /* Private flags added to firstchar and reqchar. */
123    
124    #define REQ_CASELESS   0x10000000l      /* Indicates caselessness */
125    #define REQ_VARY       0x20000000l      /* Reqchar followed non-literal item */
126    
127    /* Repeated character flags. */
128    
129    #define UTF_LENGTH     0x10000000l      /* The char contains its length. */
130    
131  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
132  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 231  static const char posix_names[] = Line 261  static const char posix_names[] =
261    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
262    STRING_word0  STRING_xdigit;    STRING_word0  STRING_xdigit;
263    
264  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
265    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 };
266    
267  /* 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 266  substitutes must be in the order of the Line 296  substitutes must be in the order of the
296  both positive and negative cases. NULL means no substitute. */  both positive and negative cases. NULL means no substitute. */
297    
298  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
299  static const uschar *substitutes[] = {  static const pcre_uchar string_PNd[]  = {
300    (uschar *)"\\P{Nd}",    /* \D */    CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
301    (uschar *)"\\p{Nd}",    /* \d */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
302    (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */  static const pcre_uchar string_pNd[]  = {
303    (uschar *)"\\p{Xsp}",   /* \s */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
304    (uschar *)"\\P{Xwd}",   /* \W */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
305    (uschar *)"\\p{Xwd}"    /* \w */  static const pcre_uchar string_PXsp[] = {
306      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
307      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
308    static const pcre_uchar string_pXsp[] = {
309      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
310      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
311    static const pcre_uchar string_PXwd[] = {
312      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
313      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
314    static const pcre_uchar string_pXwd[] = {
315      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
316      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
317    
318    static const pcre_uchar *substitutes[] = {
319      string_PNd,           /* \D */
320      string_pNd,           /* \d */
321      string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
322      string_pXsp,          /* \s */
323      string_PXwd,          /* \W */
324      string_pXwd           /* \w */
325  };  };
326    
327  static const uschar *posix_substitutes[] = {  static const pcre_uchar string_pL[] =   {
328    (uschar *)"\\p{L}",     /* alpha */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
329    (uschar *)"\\p{Ll}",    /* lower */    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
330    (uschar *)"\\p{Lu}",    /* upper */  static const pcre_uchar string_pLl[] =  {
331    (uschar *)"\\p{Xan}",   /* alnum */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
332    NULL,                   /* ascii */    CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
333    (uschar *)"\\h",        /* blank */  static const pcre_uchar string_pLu[] =  {
334    NULL,                   /* cntrl */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
335    (uschar *)"\\p{Nd}",    /* digit */    CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
336    NULL,                   /* graph */  static const pcre_uchar string_pXan[] = {
337    NULL,                   /* print */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
338    NULL,                   /* punct */    CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
339    (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */  static const pcre_uchar string_h[] =    {
340    (uschar *)"\\p{Xwd}",   /* word */    CHAR_BACKSLASH, CHAR_h, '\0' };
341    NULL,                   /* xdigit */  static const pcre_uchar string_pXps[] = {
342      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
343      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
344    static const pcre_uchar string_PL[] =   {
345      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
346      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
347    static const pcre_uchar string_PLl[] =  {
348      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
349      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
350    static const pcre_uchar string_PLu[] =  {
351      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
352      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
353    static const pcre_uchar string_PXan[] = {
354      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
355      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
356    static const pcre_uchar string_H[] =    {
357      CHAR_BACKSLASH, CHAR_H, '\0' };
358    static const pcre_uchar string_PXps[] = {
359      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
360      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
361    
362    static const pcre_uchar *posix_substitutes[] = {
363      string_pL,            /* alpha */
364      string_pLl,           /* lower */
365      string_pLu,           /* upper */
366      string_pXan,          /* alnum */
367      NULL,                 /* ascii */
368      string_h,             /* blank */
369      NULL,                 /* cntrl */
370      string_pNd,           /* digit */
371      NULL,                 /* graph */
372      NULL,                 /* print */
373      NULL,                 /* punct */
374      string_pXps,          /* space */    /* NOTE: Xps is POSIX space */
375      string_pXwd,          /* word */
376      NULL,                 /* xdigit */
377    /* Negated cases */    /* Negated cases */
378    (uschar *)"\\P{L}",     /* ^alpha */    string_PL,            /* ^alpha */
379    (uschar *)"\\P{Ll}",    /* ^lower */    string_PLl,           /* ^lower */
380    (uschar *)"\\P{Lu}",    /* ^upper */    string_PLu,           /* ^upper */
381    (uschar *)"\\P{Xan}",   /* ^alnum */    string_PXan,          /* ^alnum */
382    NULL,                   /* ^ascii */    NULL,                 /* ^ascii */
383    (uschar *)"\\H",        /* ^blank */    string_H,             /* ^blank */
384    NULL,                   /* ^cntrl */    NULL,                 /* ^cntrl */
385    (uschar *)"\\P{Nd}",    /* ^digit */    string_PNd,           /* ^digit */
386    NULL,                   /* ^graph */    NULL,                 /* ^graph */
387    NULL,                   /* ^print */    NULL,                 /* ^print */
388    NULL,                   /* ^punct */    NULL,                 /* ^punct */
389    (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */    string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
390    (uschar *)"\\P{Xwd}",   /* ^word */    string_PXwd,          /* ^word */
391    NULL                    /* ^xdigit */    NULL                  /* ^xdigit */
392  };  };
393  #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
394  #endif  #endif
395    
396  #define STRING(a)  # a  #define STRING(a)  # a
# Line 365  static const char error_texts[] = Line 449  static const char error_texts[] =
449    /* 30 */    /* 30 */
450    "unknown POSIX class name\0"    "unknown POSIX class name\0"
451    "POSIX collating elements are not supported\0"    "POSIX collating elements are not supported\0"
452    "this version of PCRE is not compiled with PCRE_UTF8 support\0"    "this version of PCRE is compiled without UTF support\0"
453    "spare error\0"  /** DEAD **/    "spare error\0"  /** DEAD **/
454    "character value in \\x{...} sequence is too large\0"    "character value in \\x{...} sequence is too large\0"
455    /* 35 */    /* 35 */
# Line 388  static const char error_texts[] = Line 472  static const char error_texts[] =
472    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
473    /* 50 */    /* 50 */
474    "repeated subpattern is too long\0"    /** DEAD **/    "repeated subpattern is too long\0"    /** DEAD **/
475    "octal value is greater than \\377 (not in UTF-8 mode)\0"    "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
476    "internal error: overran compiling workspace\0"    "internal error: overran compiling workspace\0"
477    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
478    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
479    /* 55 */    /* 55 */
480    "repeating a DEFINE group is not allowed\0"    "repeating a DEFINE group is not allowed\0"  /** DEAD **/
481    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
482    "\\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"
483    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
# Line 407  static const char error_texts[] = Line 491  static const char error_texts[] =
491    /* 65 */    /* 65 */
492    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
493    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
494    "this version of PCRE is not compiled with PCRE_UCP support\0"    "this version of PCRE is not compiled with Unicode property support\0"
495      "\\c must be followed by an ASCII character\0"
496      "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
497      /* 70 */
498      "internal error: unknown opcode in find_fixedlength()\0"
499      "\\N is not supported in a class\0"
500      "too many forward references\0"
501      "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
502      "invalid UTF-16 string\0"
503      /* 75 */
504      "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
505      "character value in \\u.... sequence is too large\0"
506    ;    ;
507    
508  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 426  For convenience, we use the same bit def Line 521  For convenience, we use the same bit def
521    
522  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
523    
524    /* Using a simple comparison for decimal numbers rather than a memory read
525    is much faster, and the resulting code is simpler (the compiler turns it
526    into a subtraction and unsigned comparison). */
527    
528    #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
529    
530  #ifndef EBCDIC  #ifndef EBCDIC
531    
532  /* 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
533  UTF-8 mode. */  UTF-8 mode. */
534    
535  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
536    {    {
537    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
538    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */
# Line 470  static const unsigned char digitab[] = Line 571  static const unsigned char digitab[] =
571    
572  /* 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. */
573    
574  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
575    {    {
576    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
577    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */
# Line 505  static const unsigned char digitab[] = Line 606  static const unsigned char digitab[] =
606    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */
607    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */
608    
609  static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */  static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
610    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */
611    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */
612    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */
# Line 541  static const unsigned char ebcdic_charta Line 642  static const unsigned char ebcdic_charta
642  #endif  #endif
643    
644    
 /* Definition to allow mutual recursion */  
   
 static BOOL  
   compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,  
     int *, int *, branch_chain *, compile_data *, int *);  
   
645    
646    
647  /*************************************************  /*************************************************
# Line 576  return s; Line 671  return s;
671    
672    
673  /*************************************************  /*************************************************
674    *           Expand the workspace                 *
675    *************************************************/
676    
677    /* This function is called during the second compiling phase, if the number of
678    forward references fills the existing workspace, which is originally a block on
679    the stack. A larger block is obtained from malloc() unless the ultimate limit
680    has been reached or the increase will be rather small.
681    
682    Argument: pointer to the compile data block
683    Returns:  0 if all went well, else an error number
684    */
685    
686    static int
687    expand_workspace(compile_data *cd)
688    {
689    pcre_uchar *newspace;
690    int newsize = cd->workspace_size * 2;
691    
692    if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
693    if (cd->workspace_size >= COMPILE_WORK_SIZE_MAX ||
694        newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
695     return ERR72;
696    
697    newspace = (PUBL(malloc))(IN_UCHARS(newsize));
698    if (newspace == NULL) return ERR21;
699    memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
700    cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
701    if (cd->workspace_size > COMPILE_WORK_SIZE)
702      (PUBL(free))((void *)cd->start_workspace);
703    cd->start_workspace = newspace;
704    cd->workspace_size = newsize;
705    return 0;
706    }
707    
708    
709    
710    /*************************************************
711    *            Check for counted repeat            *
712    *************************************************/
713    
714    /* This function is called when a '{' is encountered in a place where it might
715    start a quantifier. It looks ahead to see if it really is a quantifier or not.
716    It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
717    where the ddds are digits.
718    
719    Arguments:
720      p         pointer to the first char after '{'
721    
722    Returns:    TRUE or FALSE
723    */
724    
725    static BOOL
726    is_counted_repeat(const pcre_uchar *p)
727    {
728    if (!IS_DIGIT(*p)) return FALSE;
729    p++;
730    while (IS_DIGIT(*p)) p++;
731    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
732    
733    if (*p++ != CHAR_COMMA) return FALSE;
734    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
735    
736    if (!IS_DIGIT(*p)) return FALSE;
737    p++;
738    while (IS_DIGIT(*p)) p++;
739    
740    return (*p == CHAR_RIGHT_CURLY_BRACKET);
741    }
742    
743    
744    
745    /*************************************************
746  *            Handle escapes                      *  *            Handle escapes                      *
747  *************************************************/  *************************************************/
748    
# Line 600  Returns:         zero or positive => a d Line 767  Returns:         zero or positive => a d
767  */  */
768    
769  static int  static int
770  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,
771    int options, BOOL isclass)    int options, BOOL isclass)
772  {  {
773  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
774  const uschar *ptr = *ptrptr + 1;  BOOL utf = (options & PCRE_UTF8) != 0;
775  int c, i;  const pcre_uchar *ptr = *ptrptr + 1;
776    pcre_int32 c;
777    int i;
778    
779  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
780  ptr--;                            /* Set pointer back to the last byte */  ptr--;                            /* Set pointer back to the last byte */
# Line 619  in a table. A non-zero result is somethi Line 788  in a table. A non-zero result is somethi
788  Otherwise further processing may be required. */  Otherwise further processing may be required. */
789    
790  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
791  else if (c < CHAR_0 || c > CHAR_z) {}                     /* Not alphanumeric */  /* Not alphanumeric */
792    else if (c < CHAR_0 || c > CHAR_z) {}
793  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if ((i = escapes[c - CHAR_0]) != 0) c = i;
794    
795  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
796  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */  /* Not alphanumeric */
797    else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
798  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  c = i;
799  #endif  #endif
800    
# Line 631  else if ((i = escapes[c - 0x48]) != 0) Line 802  else if ((i = escapes[c - 0x48]) != 0)
802    
803  else  else
804    {    {
805    const uschar *oldptr;    const pcre_uchar *oldptr;
806    BOOL braced, negated;    BOOL braced, negated;
807    
808    switch (c)    switch (c)
# Line 641  else Line 812  else
812    
813      case CHAR_l:      case CHAR_l:
814      case CHAR_L:      case CHAR_L:
815        *errorcodeptr = ERR37;
816        break;
817    
818      case CHAR_u:      case CHAR_u:
819        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
820          {
821          /* In JavaScript, \u must be followed by four hexadecimal numbers.
822          Otherwise it is a lowercase u letter. */
823          if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
824            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
825            && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
826            && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
827            {
828            c = 0;
829            for (i = 0; i < 4; ++i)
830              {
831              register int cc = *(++ptr);
832    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
833              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
834              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
835    #else           /* EBCDIC coding */
836              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
837              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
838    #endif
839              }
840    
841    #ifdef COMPILE_PCRE8
842            if (c > (utf ? 0x10ffff : 0xff))
843    #else
844    #ifdef COMPILE_PCRE16
845            if (c > (utf ? 0x10ffff : 0xffff))
846    #endif
847    #endif
848              {
849              *errorcodeptr = ERR76;
850              }
851            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
852            }
853          }
854        else
855          *errorcodeptr = ERR37;
856        break;
857    
858      case CHAR_U:      case CHAR_U:
859      *errorcodeptr = ERR37;      /* In JavaScript, \U is an uppercase U letter. */
860        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
861      break;      break;
862    
863      /* \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
864        class, \g must be followed by one of a number of specific things:
865    
866      (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
867      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 663  else Line 878  else
878      the -ESC_g code (cf \k). */      the -ESC_g code (cf \k). */
879    
880      case CHAR_g:      case CHAR_g:
881        if (isclass) break;
882      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
883        {        {
884        c = -ESC_g;        c = -ESC_g;
# Line 673  else Line 889  else
889    
890      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
891        {        {
892        const uschar *p;        const pcre_uchar *p;
893        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
894          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
895        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
896          {          {
897          c = -ESC_k;          c = -ESC_k;
# Line 693  else Line 909  else
909        }        }
910      else negated = FALSE;      else negated = FALSE;
911    
912        /* The integer range is limited by the machine's int representation. */
913      c = 0;      c = 0;
914      while ((digitab[ptr[1]] & ctype_digit) != 0)      while (IS_DIGIT(ptr[1]))
915          {
916          if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
917            {
918            c = -1;
919            break;
920            }
921        c = c * 10 + *(++ptr) - CHAR_0;        c = c * 10 + *(++ptr) - CHAR_0;
922          }
923      if (c < 0)   /* Integer overflow */      if (((unsigned int)c) > INT_MAX) /* Integer overflow */
924        {        {
925          while (IS_DIGIT(ptr[1]))
926            ptr++;
927        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
928        break;        break;
929        }        }
# Line 746  else Line 971  else
971      if (!isclass)      if (!isclass)
972        {        {
973        oldptr = ptr;        oldptr = ptr;
974          /* The integer range is limited by the machine's int representation. */
975        c -= CHAR_0;        c -= CHAR_0;
976        while ((digitab[ptr[1]] & ctype_digit) != 0)        while (IS_DIGIT(ptr[1]))
977            {
978            if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
979              {
980              c = -1;
981              break;
982              }
983          c = c * 10 + *(++ptr) - CHAR_0;          c = c * 10 + *(++ptr) - CHAR_0;
984        if (c < 0)    /* Integer overflow */          }
985          if (((unsigned int)c) > INT_MAX) /* Integer overflow */
986          {          {
987            while (IS_DIGIT(ptr[1]))
988              ptr++;
989          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
990          break;          break;
991          }          }
# Line 776  else Line 1011  else
1011      /* \0 always starts an octal number, but we may drop through to here with a      /* \0 always starts an octal number, but we may drop through to here with a
1012      larger first octal digit. The original code used just to take the least      larger first octal digit. The original code used just to take the least
1013      significant 8 bits of octal numbers (I think this is what early Perls used      significant 8 bits of octal numbers (I think this is what early Perls used
1014      to do). Nowadays we allow for larger numbers in UTF-8 mode, but no more      to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode,
1015      than 3 octal digits. */      but no more than 3 octal digits. */
1016    
1017      case CHAR_0:      case CHAR_0:
1018      c -= CHAR_0;      c -= CHAR_0;
1019      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1020          c = c * 8 + *(++ptr) - CHAR_0;          c = c * 8 + *(++ptr) - CHAR_0;
1021      if (!utf8 && c > 255) *errorcodeptr = ERR51;  #ifdef COMPILE_PCRE8
1022        if (!utf && c > 0xff) *errorcodeptr = ERR51;
1023    #endif
1024      break;      break;
1025    
1026      /* \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
1027      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.
1028      treated as a data character. */      If not, { is treated as a data character. */
1029    
1030      case CHAR_x:      case CHAR_x:
1031        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1032          {
1033          /* In JavaScript, \x must be followed by two hexadecimal numbers.
1034          Otherwise it is a lowercase x letter. */
1035          if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1036            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1037            {
1038            c = 0;
1039            for (i = 0; i < 2; ++i)
1040              {
1041              register int cc = *(++ptr);
1042    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1043              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1044              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1045    #else           /* EBCDIC coding */
1046              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
1047              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1048    #endif
1049              }
1050            }
1051          break;
1052          }
1053    
1054      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1055        {        {
1056        const uschar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
       int count = 0;  
1057    
1058        c = 0;        c = 0;
1059        while ((digitab[*pt] & ctype_xdigit) != 0)        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
1060          {          {
1061          register int cc = *pt++;          register int cc = *pt++;
1062          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
         count++;  
1063    
1064  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1065          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
# Line 810  else Line 1068  else
1068          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 */
1069          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1070  #endif  #endif
1071    
1072    #ifdef COMPILE_PCRE8
1073            if (c > (utf ? 0x10ffff : 0xff)) { c = -1; break; }
1074    #else
1075    #ifdef COMPILE_PCRE16
1076            if (c > (utf ? 0x10ffff : 0xffff)) { c = -1; break; }
1077    #endif
1078    #endif
1079            }
1080    
1081          if (c < 0)
1082            {
1083            while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1084            *errorcodeptr = ERR34;
1085          }          }
1086    
1087        if (*pt == CHAR_RIGHT_CURLY_BRACKET)        if (*pt == CHAR_RIGHT_CURLY_BRACKET)
1088          {          {
1089          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1090          ptr = pt;          ptr = pt;
1091          break;          break;
1092          }          }
# Line 826  else Line 1098  else
1098      /* Read just a single-byte hex-defined char */      /* Read just a single-byte hex-defined char */
1099    
1100      c = 0;      c = 0;
1101      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1102        {        {
1103        int cc;                                  /* Some compilers don't like */        int cc;                                  /* Some compilers don't like */
1104        cc = *(++ptr);                           /* ++ in initializers */        cc = *(++ptr);                           /* ++ in initializers */
# Line 841  else Line 1113  else
1113      break;      break;
1114    
1115      /* 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.
1116      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
1117        coding is ASCII-specific, but then the whole concept of \cx is
1118      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1119    
1120      case CHAR_c:      case CHAR_c:
# Line 851  else Line 1124  else
1124        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1125        break;        break;
1126        }        }
1127    #ifndef EBCDIC    /* ASCII/UTF-8 coding */
1128  #ifndef EBCDIC  /* ASCII/UTF-8 coding */      if (c > 127)  /* Excludes all non-ASCII in either mode */
1129          {
1130          *errorcodeptr = ERR68;
1131          break;
1132          }
1133      if (c >= CHAR_a && c <= CHAR_z) c -= 32;      if (c >= CHAR_a && c <= CHAR_z) c -= 32;
1134      c ^= 0x40;      c ^= 0x40;
1135  #else           /* EBCDIC coding */  #else             /* EBCDIC coding */
1136      if (c >= CHAR_a && c <= CHAR_z) c += 64;      if (c >= CHAR_a && c <= CHAR_z) c += 64;
1137      c ^= 0xC0;      c ^= 0xC0;
1138  #endif  #endif
# Line 879  else Line 1156  else
1156    }    }
1157    
1158  /* Perl supports \N{name} for character names, as well as plain \N for "not  /* Perl supports \N{name} for character names, as well as plain \N for "not
1159  newline". PCRE does not support \N{name}. */  newline". PCRE does not support \N{name}. However, it does support
1160    quantification such as \N{2,3}. */
1161    
1162  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET)  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1163         !is_counted_repeat(ptr+2))
1164    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
1165    
1166  /* If PCRE_UCP is set, we change the values for \d etc. */  /* If PCRE_UCP is set, we change the values for \d etc. */
# Line 917  Returns:         type value from ucp_typ Line 1196  Returns:         type value from ucp_typ
1196  */  */
1197    
1198  static int  static int
1199  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
1200  {  {
1201  int c, i, bot, top;  int c, i, bot, top;
1202  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1203  char name[32];  pcre_uchar name[32];
1204    
1205  c = *(++ptr);  c = *(++ptr);
1206  if (c == 0) goto ERROR_RETURN;  if (c == 0) goto ERROR_RETURN;
# Line 938  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1217  if (c == CHAR_LEFT_CURLY_BRACKET)
1217      *negptr = TRUE;      *negptr = TRUE;
1218      ptr++;      ptr++;
1219      }      }
1220    for (i = 0; i < (int)sizeof(name) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1221      {      {
1222      c = *(++ptr);      c = *(++ptr);
1223      if (c == 0) goto ERROR_RETURN;      if (c == 0) goto ERROR_RETURN;
# Line 962  else Line 1241  else
1241  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1242    
1243  bot = 0;  bot = 0;
1244  top = _pcre_utt_size;  top = PRIV(utt_size);
1245    
1246  while (bot < top)  while (bot < top)
1247    {    {
1248    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1249    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    c = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1250    if (c == 0)    if (c == 0)
1251      {      {
1252      *dptr = _pcre_utt[i].value;      *dptr = PRIV(utt)[i].value;
1253      return _pcre_utt[i].type;      return PRIV(utt)[i].type;
1254      }      }
1255    if (c > 0) bot = i + 1; else top = i;    if (c > 0) bot = i + 1; else top = i;
1256    }    }
# Line 991  return -1; Line 1270  return -1;
1270    
1271    
1272  /*************************************************  /*************************************************
 *            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);  
 }  
   
   
   
 /*************************************************  
1273  *         Read repeat counts                     *  *         Read repeat counts                     *
1274  *************************************************/  *************************************************/
1275    
# Line 1042  Returns:         pointer to '}' on succe Line 1288  Returns:         pointer to '}' on succe
1288                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1289  */  */
1290    
1291  static const uschar *  static const pcre_uchar *
1292  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)
1293  {  {
1294  int min = 0;  int min = 0;
1295  int max = -1;  int max = -1;
# Line 1051  int max = -1; Line 1297  int max = -1;
1297  /* 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
1298  an integer overflow. */  an integer overflow. */
1299    
1300  while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + *p++ - CHAR_0;
1301  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1302    {    {
1303    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 1066  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1312  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1312    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1313      {      {
1314      max = 0;      max = 0;
1315      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + *p++ - CHAR_0;
1316      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1317        {        {
1318        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1099  top-level call starts at the beginning o Line 1345  top-level call starts at the beginning o
1345  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
1346  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
1347  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
1348  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
1349  encountered, the name will be terminated by '>' because that is checked in the  track of subpatterns that reset the capturing group numbers - the (?| feature.
1350  first pass. Recursion is used to keep track of subpatterns that reset the  
1351  capturing group numbers - the (?| feature.  This function was originally called only from the second pass, in which we know
1352    that if (?< or (?' or (?P< is encountered, the name will be correctly
1353    terminated because that is checked in the first pass. There is now one call to
1354    this function in the first pass, to check for a recursive back reference by
1355    name (so that we can make the whole group atomic). In this case, we need check
1356    only up to the current position in the pattern, and that is still OK because
1357    and previous occurrences will have been checked. To make this work, the test
1358    for "end of pattern" is a check against cd->end_pattern in the main loop,
1359    instead of looking for a binary zero. This means that the special first-pass
1360    call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1361    processing items within the loop are OK, because afterwards the main loop will
1362    terminate.)
1363    
1364  Arguments:  Arguments:
1365    ptrptr       address of the current character pointer (updated)    ptrptr       address of the current character pointer (updated)
# Line 1110  Arguments: Line 1367  Arguments:
1367    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1368    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1369    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1370      utf          TRUE if we are in UTF-8 / UTF-16 mode
1371    count        pointer to the current capturing subpattern number (updated)    count        pointer to the current capturing subpattern number (updated)
1372    
1373  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
1374  */  */
1375    
1376  static int  static int
1377  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,
1378    BOOL xmode, int *count)    BOOL xmode, BOOL utf, int *count)
1379  {  {
1380  uschar *ptr = *ptrptr;  pcre_uchar *ptr = *ptrptr;
1381  int start_count = *count;  int start_count = *count;
1382  int hwm_count = start_count;  int hwm_count = start_count;
1383  BOOL dup_parens = FALSE;  BOOL dup_parens = FALSE;
# Line 1129  dealing with. The very first call may no Line 1387  dealing with. The very first call may no
1387    
1388  if (ptr[0] == CHAR_LEFT_PARENTHESIS)  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1389    {    {
1390    if (ptr[1] == CHAR_QUESTION_MARK &&    /* Handle specials such as (*SKIP) or (*UTF8) etc. */
1391        ptr[2] == CHAR_VERTICAL_LINE)  
1392      if (ptr[1] == CHAR_ASTERISK) ptr += 2;
1393    
1394      /* Handle a normal, unnamed capturing parenthesis. */
1395    
1396      else if (ptr[1] != CHAR_QUESTION_MARK)
1397        {
1398        *count += 1;
1399        if (name == NULL && *count == lorn) return *count;
1400        ptr++;
1401        }
1402    
1403      /* All cases now have (? at the start. Remember when we are in a group
1404      where the parenthesis numbers are duplicated. */
1405    
1406      else if (ptr[2] == CHAR_VERTICAL_LINE)
1407      {      {
1408      ptr += 3;      ptr += 3;
1409      dup_parens = TRUE;      dup_parens = TRUE;
1410      }      }
1411    
1412    /* Handle a normal, unnamed capturing parenthesis */    /* Handle comments; all characters are allowed until a ket is reached. */
1413    
1414    else if (ptr[1] != CHAR_QUESTION_MARK && ptr[1] != CHAR_ASTERISK)    else if (ptr[2] == CHAR_NUMBER_SIGN)
1415      {      {
1416      *count += 1;      for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;
1417      if (name == NULL && *count == lorn) return *count;      goto FAIL_EXIT;
     ptr++;  
1418      }      }
1419    
1420    /* 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
1421    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
1422    condition (there can't be any nested parens. */    condition (there can't be any nested parens). */
1423    
1424    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
1425      {      {
# Line 1159  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1431  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1431        }        }
1432      }      }
1433    
1434    /* We have either (? or (* and not a condition */    /* Start with (? but not a condition. */
1435    
1436    else    else
1437      {      {
# Line 1172  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1444  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1444          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1445        {        {
1446        int term;        int term;
1447        const uschar *thisname;        const pcre_uchar *thisname;
1448        *count += 1;        *count += 1;
1449        if (name == NULL && *count == lorn) return *count;        if (name == NULL && *count == lorn) return *count;
1450        term = *ptr++;        term = *ptr++;
# Line 1180  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1452  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1452        thisname = ptr;        thisname = ptr;
1453        while (*ptr != term) ptr++;        while (*ptr != term) ptr++;
1454        if (name != NULL && lorn == ptr - thisname &&        if (name != NULL && lorn == ptr - thisname &&
1455            strncmp((const char *)name, (const char *)thisname, lorn) == 0)            STRNCMP_UC_UC(name, thisname, lorn) == 0)
1456          return *count;          return *count;
1457        term++;        term++;
1458        }        }
# Line 1188  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1460  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1460    }    }
1461    
1462  /* Past any initial parenthesis handling, scan for parentheses or vertical  /* Past any initial parenthesis handling, scan for parentheses or vertical
1463  bars. */  bars. Stop if we get to cd->end_pattern. Note that this is important for the
1464    first-pass call when this value is temporarily adjusted to stop at the current
1465    position. So DO NOT change this to a test for binary zero. */
1466    
1467  for (; *ptr != 0; ptr++)  for (; ptr < cd->end_pattern; ptr++)
1468    {    {
1469    /* Skip over backslashed characters and also entire \Q...\E */    /* Skip over backslashed characters and also entire \Q...\E */
1470    
# Line 1221  for (; *ptr != 0; ptr++) Line 1495  for (; *ptr != 0; ptr++)
1495          {          {
1496          if (ptr[2] == CHAR_E)          if (ptr[2] == CHAR_E)
1497            ptr+= 2;            ptr+= 2;
1498          else if (strncmp((const char *)ptr+2,          else if (STRNCMP_UC_C8(ptr + 2,
1499                   STR_Q STR_BACKSLASH STR_E, 3) == 0)                   STR_Q STR_BACKSLASH STR_E, 3) == 0)
1500            ptr += 4;            ptr += 4;
1501          else          else
# Line 1264  for (; *ptr != 0; ptr++) Line 1538  for (; *ptr != 0; ptr++)
1538    
1539    if (xmode && *ptr == CHAR_NUMBER_SIGN)    if (xmode && *ptr == CHAR_NUMBER_SIGN)
1540      {      {
1541      while (*(++ptr) != 0 && *ptr != CHAR_NL) {};      ptr++;
1542        while (*ptr != 0)
1543          {
1544          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1545          ptr++;
1546    #ifdef SUPPORT_UTF
1547          if (utf) FORWARDCHAR(ptr);
1548    #endif
1549          }
1550      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1551      continue;      continue;
1552      }      }
# Line 1273  for (; *ptr != 0; ptr++) Line 1555  for (; *ptr != 0; ptr++)
1555    
1556    if (*ptr == CHAR_LEFT_PARENTHESIS)    if (*ptr == CHAR_LEFT_PARENTHESIS)
1557      {      {
1558      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, count);      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, count);
1559      if (rc > 0) return rc;      if (rc > 0) return rc;
1560      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1561      }      }
# Line 1281  for (; *ptr != 0; ptr++) Line 1563  for (; *ptr != 0; ptr++)
1563    else if (*ptr == CHAR_RIGHT_PARENTHESIS)    else if (*ptr == CHAR_RIGHT_PARENTHESIS)
1564      {      {
1565      if (dup_parens && *count < hwm_count) *count = hwm_count;      if (dup_parens && *count < hwm_count) *count = hwm_count;
1566      *ptrptr = ptr;      goto FAIL_EXIT;
     return -1;  
1567      }      }
1568    
1569    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
# Line 1320  Arguments: Line 1601  Arguments:
1601    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1602    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1603    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1604      utf          TRUE if we are in UTF-8 / UTF-16 mode
1605    
1606  Returns:       the number of the found subpattern, or -1 if not found  Returns:       the number of the found subpattern, or -1 if not found
1607  */  */
1608    
1609  static int  static int
1610  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,
1611      BOOL utf)
1612  {  {
1613  uschar *ptr = (uschar *)cd->start_pattern;  pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1614  int count = 0;  int count = 0;
1615  int rc;  int rc;
1616    
# Line 1338  matching closing parens. That is why we Line 1621  matching closing parens. That is why we
1621    
1622  for (;;)  for (;;)
1623    {    {
1624    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, &count);    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, &count);
1625    if (rc > 0 || *ptr++ == 0) break;    if (rc > 0 || *ptr++ == 0) break;
1626    }    }
1627    
# Line 1354  return rc; Line 1637  return rc;
1637    
1638  /* This is called by several functions that scan a compiled expression looking  /* This is called by several functions that scan a compiled expression looking
1639  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
1640  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
1641  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
1642  assertions, and also the \b assertion; for others it does not.  does not.
1643    
1644  Arguments:  Arguments:
1645    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  
1646    skipassert   TRUE if certain assertions are to be skipped    skipassert   TRUE if certain assertions are to be skipped
1647    
1648  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1649  */  */
1650    
1651  static const uschar*  static const pcre_uchar*
1652  first_significant_code(const uschar *code, int *options, int optbit,  first_significant_code(const pcre_uchar *code, BOOL skipassert)
   BOOL skipassert)  
1653  {  {
1654  for (;;)  for (;;)
1655    {    {
1656    switch ((int)*code)    switch ((int)*code)
1657      {      {
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
1658      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1659      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1660      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1661      if (!skipassert) return code;      if (!skipassert) return code;
1662      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
1663      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1664      break;      break;
1665    
1666      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
# Line 1401  for (;;) Line 1674  for (;;)
1674      case OP_RREF:      case OP_RREF:
1675      case OP_NRREF:      case OP_NRREF:
1676      case OP_DEF:      case OP_DEF:
1677      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1678      break;      break;
1679    
1680      default:      default:
# Line 1431  and doing the check at the end; a flag s Line 1704  and doing the check at the end; a flag s
1704    
1705  Arguments:  Arguments:
1706    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1707    options  the compiling options    utf      TRUE in UTF-8 / UTF-16 mode
1708    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1709    cd       the "compile data" structure    cd       the "compile data" structure
1710    
1711  Returns:   the fixed length,  Returns:   the fixed length,
1712               or -1 if there is no fixed length,               or -1 if there is no fixed length,
1713               or -2 if \C was encountered               or -2 if \C was encountered (in UTF-8 mode only)
1714               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
1715                 or -4 if an unknown opcode was encountered (internal error)
1716  */  */
1717    
1718  static int  static int
1719  find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)  find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1720  {  {
1721  int length = -1;  int length = -1;
1722    
1723  register int branchlength = 0;  register int branchlength = 0;
1724  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1725    
1726  /* 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
1727  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1455  branch, check the length against that of Line 1729  branch, check the length against that of
1729  for (;;)  for (;;)
1730    {    {
1731    int d;    int d;
1732    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1733    register int op = *cc;    register int op = *cc;
1734    
1735    switch (op)    switch (op)
1736      {      {
1737        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1738        OP_BRA (normal non-capturing bracket) because the other variants of these
1739        opcodes are all concerned with unlimited repeated groups, which of course
1740        are not of fixed length. */
1741    
1742      case OP_CBRA:      case OP_CBRA:
1743      case OP_BRA:      case OP_BRA:
1744      case OP_ONCE:      case OP_ONCE:
1745        case OP_ONCE_NC:
1746      case OP_COND:      case OP_COND:
1747      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);
1748      if (d < 0) return d;      if (d < 0) return d;
1749      branchlength += d;      branchlength += d;
1750      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1751      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1752      break;      break;
1753    
1754      /* 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.
1755      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
1756      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
1757        the same code. Note that we must not include the OP_KETRxxx opcodes here,
1758        because they all imply an unlimited repeat. */
1759    
1760      case OP_ALT:      case OP_ALT:
1761      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1762      case OP_END:      case OP_END:
1763        case OP_ACCEPT:
1764        case OP_ASSERT_ACCEPT:
1765      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1766        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
1767      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
# Line 1492  for (;;) Line 1775  for (;;)
1775    
1776      case OP_RECURSE:      case OP_RECURSE:
1777      if (!atend) return -3;      if (!atend) return -3;
1778      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1779      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1780      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1781      d = find_fixedlength(cs + 2, options, atend, cd);      d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd);
1782      if (d < 0) return d;      if (d < 0) return d;
1783      branchlength += d;      branchlength += d;
1784      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1508  for (;;) Line 1791  for (;;)
1791      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1792      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1793      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1794      /* Fall through */      cc += PRIV(OP_lengths)[*cc];
1795        break;
1796    
1797      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1798    
1799      case OP_REVERSE:      case OP_MARK:
1800        case OP_PRUNE_ARG:
1801        case OP_SKIP_ARG:
1802        case OP_THEN_ARG:
1803        cc += cc[1] + PRIV(OP_lengths)[*cc];
1804        break;
1805    
1806        case OP_CALLOUT:
1807        case OP_CIRC:
1808        case OP_CIRCM:
1809        case OP_CLOSE:
1810        case OP_COMMIT:
1811      case OP_CREF:      case OP_CREF:
     case OP_NCREF:  
     case OP_RREF:  
     case OP_NRREF:  
1812      case OP_DEF:      case OP_DEF:
1813      case OP_OPT:      case OP_DOLL:
1814      case OP_CALLOUT:      case OP_DOLLM:
     case OP_SOD:  
     case OP_SOM:  
     case OP_SET_SOM:  
1815      case OP_EOD:      case OP_EOD:
1816      case OP_EODN:      case OP_EODN:
1817      case OP_CIRC:      case OP_FAIL:
1818      case OP_DOLL:      case OP_NCREF:
1819        case OP_NRREF:
1820      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1821        case OP_PRUNE:
1822        case OP_REVERSE:
1823        case OP_RREF:
1824        case OP_SET_SOM:
1825        case OP_SKIP:
1826        case OP_SOD:
1827        case OP_SOM:
1828        case OP_THEN:
1829      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1830      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1831      break;      break;
1832    
1833      /* Handle literal characters */      /* Handle literal characters */
1834    
1835      case OP_CHAR:      case OP_CHAR:
1836      case OP_CHARNC:      case OP_CHARI:
1837      case OP_NOT:      case OP_NOT:
1838        case OP_NOTI:
1839      branchlength++;      branchlength++;
1840      cc += 2;      cc += 2;
1841  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1842      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1843  #endif  #endif
1844      break;      break;
1845    
# Line 1549  for (;;) Line 1847  for (;;)
1847      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
1848    
1849      case OP_EXACT:      case OP_EXACT:
1850        case OP_EXACTI:
1851        case OP_NOTEXACT:
1852        case OP_NOTEXACTI:
1853      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1854      cc += 4;      cc += 2 + IMM2_SIZE;
1855  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1856      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1857  #endif  #endif
1858      break;      break;
1859    
1860      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1861      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1862      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;
1863      cc += 4;      cc += 1 + IMM2_SIZE + 1;
1864      break;      break;
1865    
1866      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1570  for (;;) Line 1870  for (;;)
1870      cc += 2;      cc += 2;
1871      /* Fall through */      /* Fall through */
1872    
1873        case OP_HSPACE:
1874        case OP_VSPACE:
1875        case OP_NOT_HSPACE:
1876        case OP_NOT_VSPACE:
1877      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1878      case OP_DIGIT:      case OP_DIGIT:
1879      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
# Line 1582  for (;;) Line 1886  for (;;)
1886      cc++;      cc++;
1887      break;      break;
1888    
1889      /* The single-byte matcher isn't allowed */      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1890        otherwise \C is coded as OP_ALLANY. */
1891    
1892      case OP_ANYBYTE:      case OP_ANYBYTE:
1893      return -2;      return -2;
1894    
1895      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1896    
1897  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || defined COMPILE_PCRE16
1898      case OP_XCLASS:      case OP_XCLASS:
1899      cc += GET(cc, 1) - 33;      cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];
1900      /* Fall through */      /* Fall through */
1901  #endif  #endif
1902    
1903      case OP_CLASS:      case OP_CLASS:
1904      case OP_NCLASS:      case OP_NCLASS:
1905      cc += 33;      cc += PRIV(OP_lengths)[OP_CLASS];
1906    
1907      switch (*cc)      switch (*cc)
1908        {        {
1909          case OP_CRPLUS:
1910          case OP_CRMINPLUS:
1911        case OP_CRSTAR:        case OP_CRSTAR:
1912        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1913        case OP_CRQUERY:        case OP_CRQUERY:
# Line 1609  for (;;) Line 1916  for (;;)
1916    
1917        case OP_CRRANGE:        case OP_CRRANGE:
1918        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1919        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1920        branchlength += GET2(cc,1);        branchlength += GET2(cc,1);
1921        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1922        break;        break;
1923    
1924        default:        default:
# Line 1621  for (;;) Line 1928  for (;;)
1928    
1929      /* Anything else is variable length */      /* Anything else is variable length */
1930    
1931      default:      case OP_ANYNL:
1932        case OP_BRAMINZERO:
1933        case OP_BRAPOS:
1934        case OP_BRAPOSZERO:
1935        case OP_BRAZERO:
1936        case OP_CBRAPOS:
1937        case OP_EXTUNI:
1938        case OP_KETRMAX:
1939        case OP_KETRMIN:
1940        case OP_KETRPOS:
1941        case OP_MINPLUS:
1942        case OP_MINPLUSI:
1943        case OP_MINQUERY:
1944        case OP_MINQUERYI:
1945        case OP_MINSTAR:
1946        case OP_MINSTARI:
1947        case OP_MINUPTO:
1948        case OP_MINUPTOI:
1949        case OP_NOTMINPLUS:
1950        case OP_NOTMINPLUSI:
1951        case OP_NOTMINQUERY:
1952        case OP_NOTMINQUERYI:
1953        case OP_NOTMINSTAR:
1954        case OP_NOTMINSTARI:
1955        case OP_NOTMINUPTO:
1956        case OP_NOTMINUPTOI:
1957        case OP_NOTPLUS:
1958        case OP_NOTPLUSI:
1959        case OP_NOTPOSPLUS:
1960        case OP_NOTPOSPLUSI:
1961        case OP_NOTPOSQUERY:
1962        case OP_NOTPOSQUERYI:
1963        case OP_NOTPOSSTAR:
1964        case OP_NOTPOSSTARI:
1965        case OP_NOTPOSUPTO:
1966        case OP_NOTPOSUPTOI:
1967        case OP_NOTQUERY:
1968        case OP_NOTQUERYI:
1969        case OP_NOTSTAR:
1970        case OP_NOTSTARI:
1971        case OP_NOTUPTO:
1972        case OP_NOTUPTOI:
1973        case OP_PLUS:
1974        case OP_PLUSI:
1975        case OP_POSPLUS:
1976        case OP_POSPLUSI:
1977        case OP_POSQUERY:
1978        case OP_POSQUERYI:
1979        case OP_POSSTAR:
1980        case OP_POSSTARI:
1981        case OP_POSUPTO:
1982        case OP_POSUPTOI:
1983        case OP_QUERY:
1984        case OP_QUERYI:
1985        case OP_REF:
1986        case OP_REFI:
1987        case OP_SBRA:
1988        case OP_SBRAPOS:
1989        case OP_SCBRA:
1990        case OP_SCBRAPOS:
1991        case OP_SCOND:
1992        case OP_SKIPZERO:
1993        case OP_STAR:
1994        case OP_STARI:
1995        case OP_TYPEMINPLUS:
1996        case OP_TYPEMINQUERY:
1997        case OP_TYPEMINSTAR:
1998        case OP_TYPEMINUPTO:
1999        case OP_TYPEPLUS:
2000        case OP_TYPEPOSPLUS:
2001        case OP_TYPEPOSQUERY:
2002        case OP_TYPEPOSSTAR:
2003        case OP_TYPEPOSUPTO:
2004        case OP_TYPEQUERY:
2005        case OP_TYPESTAR:
2006        case OP_TYPEUPTO:
2007        case OP_UPTO:
2008        case OP_UPTOI:
2009      return -1;      return -1;
2010    
2011        /* Catch unrecognized opcodes so that when new ones are added they
2012        are not forgotten, as has happened in the past. */
2013    
2014        default:
2015        return -4;
2016      }      }
2017    }    }
2018  /* Control never gets here */  /* Control never gets here */
# Line 1643  length. Line 2033  length.
2033    
2034  Arguments:  Arguments:
2035    code        points to start of expression    code        points to start of expression
2036    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 mode
2037    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
2038    
2039  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
2040  */  */
2041    
2042  const uschar *  const pcre_uchar *
2043  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
2044  {  {
2045  for (;;)  for (;;)
2046    {    {
2047    register int c = *code;    register int c = *code;
2048    
2049    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2050    
2051    /* 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 1667  for (;;) Line 2058  for (;;)
2058    
2059    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
2060      {      {
2061      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
2062      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2063      }      }
2064    
2065    /* Handle capturing bracket */    /* Handle capturing bracket */
2066    
2067    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
2068               c == OP_CBRAPOS || c == OP_SCBRAPOS)
2069      {      {
2070      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
2071      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
2072      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2073      }      }
2074    
2075    /* 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
# Line 1705  for (;;) Line 2097  for (;;)
2097        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2098        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2099        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
2100        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP
2101            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2102        break;        break;
2103    
2104        case OP_MARK:        case OP_MARK:
2105        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2106        case OP_SKIP_ARG:        case OP_SKIP_ARG:
2107          code += code[1];
2108          break;
2109    
2110        case OP_THEN_ARG:        case OP_THEN_ARG:
2111        code += code[1];        code += code[1];
2112        break;        break;
# Line 1718  for (;;) Line 2114  for (;;)
2114    
2115      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2116    
2117      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2118    
2119    /* 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
2120    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
2121    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
2122    
2123  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2124      if (utf8) switch(c)      if (utf) switch(c)
2125        {        {
2126        case OP_CHAR:        case OP_CHAR:
2127        case OP_CHARNC:        case OP_CHARI:
2128        case OP_EXACT:        case OP_EXACT:
2129          case OP_EXACTI:
2130        case OP_UPTO:        case OP_UPTO:
2131          case OP_UPTOI:
2132        case OP_MINUPTO:        case OP_MINUPTO:
2133          case OP_MINUPTOI:
2134        case OP_POSUPTO:        case OP_POSUPTO:
2135          case OP_POSUPTOI:
2136        case OP_STAR:        case OP_STAR:
2137          case OP_STARI:
2138        case OP_MINSTAR:        case OP_MINSTAR:
2139          case OP_MINSTARI:
2140        case OP_POSSTAR:        case OP_POSSTAR:
2141          case OP_POSSTARI:
2142        case OP_PLUS:        case OP_PLUS:
2143          case OP_PLUSI:
2144        case OP_MINPLUS:        case OP_MINPLUS:
2145          case OP_MINPLUSI:
2146        case OP_POSPLUS:        case OP_POSPLUS:
2147          case OP_POSPLUSI:
2148        case OP_QUERY:        case OP_QUERY:
2149          case OP_QUERYI:
2150        case OP_MINQUERY:        case OP_MINQUERY:
2151          case OP_MINQUERYI:
2152        case OP_POSQUERY:        case OP_POSQUERY:
2153        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2154          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2155        break;        break;
2156        }        }
2157  #else  #else
2158      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2159  #endif  #endif
2160      }      }
2161    }    }
# Line 1763  instance of OP_RECURSE. Line 2172  instance of OP_RECURSE.
2172    
2173  Arguments:  Arguments:
2174    code        points to start of expression    code        points to start of expression
2175    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 mode
2176    
2177  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
2178  */  */
2179    
2180  static const uschar *  static const pcre_uchar *
2181  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
2182  {  {
2183  for (;;)  for (;;)
2184    {    {
# Line 1808  for (;;) Line 2217  for (;;)
2217        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2218        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2219        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2220        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP
2221            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2222        break;        break;
2223    
2224        case OP_MARK:        case OP_MARK:
2225        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2226        case OP_SKIP_ARG:        case OP_SKIP_ARG:
2227          code += code[1];
2228          break;
2229    
2230        case OP_THEN_ARG:        case OP_THEN_ARG:
2231        code += code[1];        code += code[1];
2232        break;        break;
# Line 1821  for (;;) Line 2234  for (;;)
2234    
2235      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2236    
2237      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2238    
2239      /* 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
2240      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
2241      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
2242    
2243  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2244      if (utf8) switch(c)      if (utf) switch(c)
2245        {        {
2246        case OP_CHAR:        case OP_CHAR:
2247        case OP_CHARNC:        case OP_CHARI:
2248          case OP_NOT:
2249          case OP_NOTI:
2250        case OP_EXACT:        case OP_EXACT:
2251          case OP_EXACTI:
2252          case OP_NOTEXACT:
2253          case OP_NOTEXACTI:
2254        case OP_UPTO:        case OP_UPTO:
2255          case OP_UPTOI:
2256          case OP_NOTUPTO:
2257          case OP_NOTUPTOI:
2258        case OP_MINUPTO:        case OP_MINUPTO:
2259          case OP_MINUPTOI:
2260          case OP_NOTMINUPTO:
2261          case OP_NOTMINUPTOI:
2262        case OP_POSUPTO:        case OP_POSUPTO:
2263          case OP_POSUPTOI:
2264          case OP_NOTPOSUPTO:
2265          case OP_NOTPOSUPTOI:
2266        case OP_STAR:        case OP_STAR:
2267          case OP_STARI:
2268          case OP_NOTSTAR:
2269          case OP_NOTSTARI:
2270        case OP_MINSTAR:        case OP_MINSTAR:
2271          case OP_MINSTARI:
2272          case OP_NOTMINSTAR:
2273          case OP_NOTMINSTARI:
2274        case OP_POSSTAR:        case OP_POSSTAR:
2275          case OP_POSSTARI:
2276          case OP_NOTPOSSTAR:
2277          case OP_NOTPOSSTARI:
2278        case OP_PLUS:        case OP_PLUS:
2279          case OP_PLUSI:
2280          case OP_NOTPLUS:
2281          case OP_NOTPLUSI:
2282        case OP_MINPLUS:        case OP_MINPLUS:
2283          case OP_MINPLUSI:
2284          case OP_NOTMINPLUS:
2285          case OP_NOTMINPLUSI:
2286        case OP_POSPLUS:        case OP_POSPLUS:
2287          case OP_POSPLUSI:
2288          case OP_NOTPOSPLUS:
2289          case OP_NOTPOSPLUSI:
2290        case OP_QUERY:        case OP_QUERY:
2291          case OP_QUERYI:
2292          case OP_NOTQUERY:
2293          case OP_NOTQUERYI:
2294        case OP_MINQUERY:        case OP_MINQUERY:
2295          case OP_MINQUERYI:
2296          case OP_NOTMINQUERY:
2297          case OP_NOTMINQUERYI:
2298        case OP_POSQUERY:        case OP_POSQUERY:
2299        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2300          case OP_NOTPOSQUERY:
2301          case OP_NOTPOSQUERYI:
2302          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2303        break;        break;
2304        }        }
2305  #else  #else
2306      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2307  #endif  #endif
2308      }      }
2309    }    }
# Line 1872  bracket whose current branch will alread Line 2326  bracket whose current branch will alread
2326  Arguments:  Arguments:
2327    code        points to start of search    code        points to start of search
2328    endcode     points to where to stop    endcode     points to where to stop
2329    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 mode
2330    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2331    
2332  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2333  */  */
2334    
2335  static BOOL  static BOOL
2336  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2337    compile_data *cd)    BOOL utf, compile_data *cd)
2338  {  {
2339  register int c;  register int c;
2340  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2341       code < endcode;       code < endcode;
2342       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2343    {    {
2344    const uschar *ccode;    const pcre_uchar *ccode;
2345    
2346    c = *code;    c = *code;
2347    
# Line 1901  for (code = first_significant_code(code Line 2355  for (code = first_significant_code(code
2355      continue;      continue;
2356      }      }
2357    
   /* Groups with zero repeats can of course be empty; skip them. */  
   
   if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO)  
     {  
     code += _pcre_OP_lengths[c];  
     do code += GET(code, 1); while (*code == OP_ALT);  
     c = *code;  
     continue;  
     }  
   
2358    /* For a recursion/subroutine call, if its end has been reached, which    /* For a recursion/subroutine call, if its end has been reached, which
2359    implies a subroutine call, we can scan it. */    implies a backward reference subroutine call, we can scan it. If it's a
2360      forward reference subroutine call, we can't. To detect forward reference
2361      we have to scan up the list that is kept in the workspace. This function is
2362      called only when doing the real compile, not during the pre-compile that
2363      measures the size of the compiled pattern. */
2364    
2365    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2366      {      {
2367      BOOL empty_branch = FALSE;      const pcre_uchar *scode;
2368      const uschar *scode = cd->start_code + GET(code, 1);      BOOL empty_branch;
2369    
2370        /* Test for forward reference */
2371    
2372        for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
2373          if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;
2374    
2375        /* Not a forward reference, test for completed backward reference */
2376    
2377        empty_branch = FALSE;
2378        scode = cd->start_code + GET(code, 1);
2379      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2380    
2381        /* Completed backwards reference */
2382    
2383      do      do
2384        {        {
2385        if (could_be_empty_branch(scode, endcode, utf8, cd))        if (could_be_empty_branch(scode, endcode, utf, cd))
2386          {          {
2387          empty_branch = TRUE;          empty_branch = TRUE;
2388          break;          break;
# Line 1929  for (code = first_significant_code(code Line 2390  for (code = first_significant_code(code
2390        scode += GET(scode, 1);        scode += GET(scode, 1);
2391        }        }
2392      while (*scode == OP_ALT);      while (*scode == OP_ALT);
2393    
2394      if (!empty_branch) return FALSE;  /* All branches are non-empty */      if (!empty_branch) return FALSE;  /* All branches are non-empty */
2395      continue;      continue;
2396      }      }
2397    
2398      /* Groups with zero repeats can of course be empty; skip them. */
2399    
2400      if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2401          c == OP_BRAPOSZERO)
2402        {
2403        code += PRIV(OP_lengths)[c];
2404        do code += GET(code, 1); while (*code == OP_ALT);
2405        c = *code;
2406        continue;
2407        }
2408    
2409      /* A nested group that is already marked as "could be empty" can just be
2410      skipped. */
2411    
2412      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2413          c == OP_SCBRA || c == OP_SCBRAPOS)
2414        {
2415        do code += GET(code, 1); while (*code == OP_ALT);
2416        c = *code;
2417        continue;
2418        }
2419    
2420    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
2421    
2422    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA  || c == OP_BRAPOS ||
2423          c == OP_CBRA || c == OP_CBRAPOS ||
2424          c == OP_ONCE || c == OP_ONCE_NC ||
2425          c == OP_COND)
2426      {      {
2427      BOOL empty_branch;      BOOL empty_branch;
2428      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 1951  for (code = first_significant_code(code Line 2438  for (code = first_significant_code(code
2438        empty_branch = FALSE;        empty_branch = FALSE;
2439        do        do
2440          {          {
2441          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))
2442            empty_branch = TRUE;            empty_branch = TRUE;
2443          code += GET(code, 1);          code += GET(code, 1);
2444          }          }
# Line 1969  for (code = first_significant_code(code Line 2456  for (code = first_significant_code(code
2456      {      {
2457      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2458      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2459      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2460      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"
2461      here. */      here. */
2462    
2463  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2464      case OP_XCLASS:      case OP_XCLASS:
2465      ccode = code += GET(code, 1);      ccode = code += GET(code, 1);
2466      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
# Line 1981  for (code = first_significant_code(code Line 2468  for (code = first_significant_code(code
2468    
2469      case OP_CLASS:      case OP_CLASS:
2470      case OP_NCLASS:      case OP_NCLASS:
2471      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2472    
2473  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2474      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
2475  #endif  #endif
2476    
# Line 2022  for (code = first_significant_code(code Line 2509  for (code = first_significant_code(code
2509      case OP_ALLANY:      case OP_ALLANY:
2510      case OP_ANYBYTE:      case OP_ANYBYTE:
2511      case OP_CHAR:      case OP_CHAR:
2512      case OP_CHARNC:      case OP_CHARI:
2513      case OP_NOT:      case OP_NOT:
2514        case OP_NOTI:
2515      case OP_PLUS:      case OP_PLUS:
2516      case OP_MINPLUS:      case OP_MINPLUS:
2517      case OP_POSPLUS:      case OP_POSPLUS:
# Line 2055  for (code = first_significant_code(code Line 2543  for (code = first_significant_code(code
2543      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2544      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2545      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2546      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP
2547          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2548      break;      break;
2549    
2550      /* End of branch */      /* End of branch */
# Line 2063  for (code = first_significant_code(code Line 2552  for (code = first_significant_code(code
2552      case OP_KET:      case OP_KET:
2553      case OP_KETRMAX:      case OP_KETRMAX:
2554      case OP_KETRMIN:      case OP_KETRMIN:
2555        case OP_KETRPOS:
2556      case OP_ALT:      case OP_ALT:
2557      return TRUE;      return TRUE;
2558    
2559      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2560      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO may be followed by a multibyte character */
2561    
2562  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2563      case OP_STAR:      case OP_STAR:
2564        case OP_STARI:
2565      case OP_MINSTAR:      case OP_MINSTAR:
2566        case OP_MINSTARI:
2567      case OP_POSSTAR:      case OP_POSSTAR:
2568        case OP_POSSTARI:
2569      case OP_QUERY:      case OP_QUERY:
2570        case OP_QUERYI:
2571      case OP_MINQUERY:      case OP_MINQUERY:
2572        case OP_MINQUERYI:
2573      case OP_POSQUERY:      case OP_POSQUERY:
2574      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      case OP_POSQUERYI:
2575        if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2576      break;      break;
2577    
2578      case OP_UPTO:      case OP_UPTO:
2579        case OP_UPTOI:
2580      case OP_MINUPTO:      case OP_MINUPTO:
2581        case OP_MINUPTOI:
2582      case OP_POSUPTO:      case OP_POSUPTO:
2583      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      case OP_POSUPTOI:
2584        if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2585      break;      break;
2586  #endif  #endif
2587    
# Line 2092  for (code = first_significant_code(code Line 2591  for (code = first_significant_code(code
2591      case OP_MARK:      case OP_MARK:
2592      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2593      case OP_SKIP_ARG:      case OP_SKIP_ARG:
2594        code += code[1];
2595        break;
2596    
2597      case OP_THEN_ARG:      case OP_THEN_ARG:
2598      code += code[1];      code += code[1];
2599      break;      break;
# Line 2116  return TRUE; Line 2618  return TRUE;
2618  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
2619  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,
2620  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.
2621    This function is called only during the real compile, not during the
2622    pre-compile.
2623    
2624  Arguments:  Arguments:
2625    code        points to start of the recursion    code        points to start of the recursion
2626    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2627    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2628    utf8        TRUE if in UTF-8 mode    utf         TRUE if in UTF-8 / UTF-16 mode
2629    cd          pointers to tables etc    cd          pointers to tables etc
2630    
2631  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2632  */  */
2633    
2634  static BOOL  static BOOL
2635  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2636    BOOL utf8, compile_data *cd)    branch_chain *bcptr, BOOL utf, compile_data *cd)
2637  {  {
2638  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2639    {    {
2640    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))
2641      return FALSE;      return FALSE;
2642    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2643    }    }
# Line 2166  where Perl recognizes it as the POSIX cl Line 2670  where Perl recognizes it as the POSIX cl
2670  "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,
2671  I think.  I think.
2672    
2673    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2674    It seems that the appearance of a nested POSIX class supersedes an apparent
2675    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2676    a digit.
2677    
2678    In Perl, unescaped square brackets may also appear as part of class names. For
2679    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2680    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2681    seem right at all. PCRE does not allow closing square brackets in POSIX class
2682    names.
2683    
2684  Arguments:  Arguments:
2685    ptr      pointer to the initial [    ptr      pointer to the initial [
2686    endptr   where to return the end pointer    endptr   where to return the end pointer
# Line 2174  Returns:   TRUE or FALSE Line 2689  Returns:   TRUE or FALSE
2689  */  */
2690    
2691  static BOOL  static BOOL
2692  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2693  {  {
2694  int terminator;          /* Don't combine these lines; the Solaris cc */  int terminator;          /* Don't combine these lines; the Solaris cc */
2695  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
2696  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != 0; ptr++)
2697    {    {
2698    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) ptr++; else    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2699        ptr++;
2700      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
2701      else
2702      {      {
     if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;  
2703      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2704        {        {
2705        *endptr = ptr;        *endptr = ptr;
2706        return TRUE;        return TRUE;
2707        }        }
2708        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
2709             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
2710              ptr[1] == CHAR_EQUALS_SIGN) &&
2711            check_posix_syntax(ptr, endptr))
2712          return FALSE;
2713      }      }
2714    }    }
2715  return FALSE;  return FALSE;
# Line 2211  Returns:     a value representing the na Line 2733  Returns:     a value representing the na
2733  */  */
2734    
2735  static int  static int
2736  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
2737  {  {
2738  const char *pn = posix_names;  const char *pn = posix_names;
2739  register int yield = 0;  register int yield = 0;
2740  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
2741    {    {
2742    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
2743      strncmp((const char *)ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;
2744    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
2745    yield++;    yield++;
2746    }    }
# Line 2250  value in the reference (which is a group Line 2772  value in the reference (which is a group
2772  Arguments:  Arguments:
2773    group      points to the start of the group    group      points to the start of the group
2774    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
2775    utf8       TRUE in UTF-8 mode    utf        TRUE in UTF-8 / UTF-16 mode
2776    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
2777    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
2778    
# Line 2258  Returns:     nothing Line 2780  Returns:     nothing
2780  */  */
2781    
2782  static void  static void
2783  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
2784    uschar *save_hwm)    pcre_uchar *save_hwm)
2785  {  {
2786  uschar *ptr = group;  pcre_uchar *ptr = group;
2787    
2788  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
2789    {    {
2790    int offset;    int offset;
2791    uschar *hc;    pcre_uchar *hc;
2792    
2793    /* 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
2794    reference. */    reference. */
# Line 2311  Arguments: Line 2833  Arguments:
2833  Returns:         new code pointer  Returns:         new code pointer
2834  */  */
2835    
2836  static uschar *  static pcre_uchar *
2837  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2838  {  {
2839  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2840  *code++ = 255;  *code++ = 255;
2841  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2842  PUT(code, LINK_SIZE, 0);                       /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2843  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
2844  }  }
2845    
2846    
# Line 2340  Returns:             nothing Line 2862  Returns:             nothing
2862  */  */
2863    
2864  static void  static void
2865  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2866  {  {
2867  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2868  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
# Line 2354  PUT(previous_callout, 2 + LINK_SIZE, len Line 2876  PUT(previous_callout, 2 + LINK_SIZE, len
2876  *************************************************/  *************************************************/
2877    
2878  /* This function is passed the start and end of a class range, in UTF-8 mode  /* This function is passed the start and end of a class range, in UTF-8 mode
2879  with UCP support. It searches up the characters, looking for internal ranges of  with UCP support. It searches up the characters, looking for ranges of
2880  characters in the "other" case. Each call returns the next one, updating the  characters in the "other" case. Each call returns the next one, updating the
2881  start address.  start address. A character with multiple other cases is returned on its own
2882    with a special return value.
2883    
2884  Arguments:  Arguments:
2885    cptr        points to starting character value; updated    cptr        points to starting character value; updated
# Line 2364  Arguments: Line 2887  Arguments:
2887    ocptr       where to put start of othercase range    ocptr       where to put start of othercase range
2888    odptr       where to put end of othercase range    odptr       where to put end of othercase range
2889    
2890  Yield:        TRUE when range returned; FALSE when no more  Yield:        -1 when no more
2891                   0 when a range is returned
2892                  >0 the CASESET offset for char with multiple other cases
2893                    in this case, ocptr contains the original
2894  */  */
2895    
2896  static BOOL  static int
2897  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,
2898    unsigned int *odptr)    unsigned int *odptr)
2899  {  {
2900  unsigned int c, othercase, next;  unsigned int c, othercase, next;
2901    int co;
2902    
2903    /* Find the first character that has an other case. If it has multiple other
2904    cases, return its case offset value. */
2905    
2906  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
2907    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
2908      if ((co = UCD_CASESET(c)) != 0)
2909        {
2910        *ocptr = c++;   /* Character that has the set */
2911        *cptr = c;      /* Rest of input range */
2912        return co;
2913        }
2914      if ((othercase = UCD_OTHERCASE(c)) != c) break;
2915      }
2916    
2917  if (c > d) return FALSE;  if (c > d) return -1;  /* Reached end of range */
2918    
2919  *ocptr = othercase;  *ocptr = othercase;
2920  next = othercase + 1;  next = othercase + 1;
# Line 2387  for (++c; c <= d; c++) Line 2925  for (++c; c <= d; c++)
2925    next++;    next++;
2926    }    }
2927    
2928  *odptr = next - 1;  *odptr = next - 1;     /* End of othercase range */
2929  *cptr = c;  *cptr = c;             /* Rest of input range */
2930    return 0;
 return TRUE;  
2931  }  }
2932    
2933    
# Line 2423  switch(ptype) Line 2960  switch(ptype)
2960            prop->chartype == ucp_Lt) == negated;            prop->chartype == ucp_Lt) == negated;
2961    
2962    case PT_GC:    case PT_GC:
2963    return (pdata == _pcre_ucp_gentype[prop->chartype]) == negated;    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2964    
2965    case PT_PC:    case PT_PC:
2966    return (pdata == prop->chartype) == negated;    return (pdata == prop->chartype) == negated;
# Line 2434  switch(ptype) Line 2971  switch(ptype)
2971    /* These are specials */    /* These are specials */
2972    
2973    case PT_ALNUM:    case PT_ALNUM:
2974    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2975            _pcre_ucp_gentype[prop->chartype] == ucp_N) == negated;            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2976    
2977    case PT_SPACE:    /* Perl space */    case PT_SPACE:    /* Perl space */
2978    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2979            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2980            == negated;            == negated;
2981    
2982    case PT_PXSPACE:  /* POSIX space */    case PT_PXSPACE:  /* POSIX space */
2983    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2984            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2985            c == CHAR_FF || c == CHAR_CR)            c == CHAR_FF || c == CHAR_CR)
2986            == negated;            == negated;
2987    
2988    case PT_WORD:    case PT_WORD:
2989    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2990            _pcre_ucp_gentype[prop->chartype] == ucp_N ||            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2991            c == CHAR_UNDERSCORE) == negated;            c == CHAR_UNDERSCORE) == negated;
2992    }    }
2993  return FALSE;  return FALSE;
# Line 2469  sense to automatically possessify the re Line 3006  sense to automatically possessify the re
3006    
3007  Arguments:  Arguments:
3008    previous      pointer to the repeated opcode    previous      pointer to the repeated opcode
3009    utf8          TRUE in UTF-8 mode    utf           TRUE in UTF-8 / UTF-16 mode
3010    ptr           next character in pattern    ptr           next character in pattern
3011    options       options bits    options       options bits
3012    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 2478  Returns:        TRUE if possessifying is Line 3015  Returns:        TRUE if possessifying is
3015  */  */
3016    
3017  static BOOL  static BOOL
3018  check_auto_possessive(const uschar *previous, BOOL utf8, const uschar *ptr,  check_auto_possessive(const pcre_uchar *previous, BOOL utf,
3019    int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
3020  {  {
3021  int c, next;  pcre_int32 c, next;
3022  int op_code = *previous++;  int op_code = *previous++;
3023    
3024  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
# Line 2490  if ((options & PCRE_EXTENDED) != 0) Line 3027  if ((options & PCRE_EXTENDED) != 0)
3027    {    {
3028    for (;;)    for (;;)
3029      {      {
3030      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
3031      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
3032        {        {
3033        while (*(++ptr) != 0)        ptr++;
3034          while (*ptr != 0)
3035            {
3036          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3037            ptr++;
3038    #ifdef SUPPORT_UTF
3039            if (utf) FORWARDCHAR(ptr);
3040    #endif
3041            }
3042        }        }
3043      else break;      else break;
3044      }      }
# Line 2510  if (*ptr == CHAR_BACKSLASH) Line 3054  if (*ptr == CHAR_BACKSLASH)
3054    if (temperrorcode != 0) return FALSE;    if (temperrorcode != 0) return FALSE;
3055    ptr++;    /* Point after the escape sequence */    ptr++;    /* Point after the escape sequence */
3056    }    }
3057    else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)
 else if ((cd->ctypes[*ptr] & ctype_meta) == 0)  
3058    {    {
3059  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3060    if (utf8) { GETCHARINC(next, ptr); } else    if (utf) { GETCHARINC(next, ptr); } else
3061  #endif  #endif
3062    next = *ptr++;    next = *ptr++;
3063    }    }
   
3064  else return FALSE;  else return FALSE;
3065    
3066  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
# Line 2527  if ((options & PCRE_EXTENDED) != 0) Line 3069  if ((options & PCRE_EXTENDED) != 0)
3069    {    {
3070    for (;;)    for (;;)
3071      {      {
3072      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
3073      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
3074        {        {
3075        while (*(++ptr) != 0)        ptr++;
3076          while (*ptr != 0)
3077            {
3078          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3079            ptr++;
3080    #ifdef SUPPORT_UTF
3081            if (utf) FORWARDCHAR(ptr);
3082    #endif
3083            }
3084        }        }
3085      else break;      else break;
3086      }      }
# Line 2540  if ((options & PCRE_EXTENDED) != 0) Line 3089  if ((options & PCRE_EXTENDED) != 0)
3089  /* If the next thing is itself optional, we have to give up. */  /* If the next thing is itself optional, we have to give up. */
3090    
3091  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3092    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)
3093      return FALSE;      return FALSE;
3094    
3095  /* Now compare the next item with the previous opcode. First, handle cases when  /* Now compare the next item with the previous opcode. First, handle cases when
# Line 2549  the next item is a character. */ Line 3098  the next item is a character. */
3098  if (next >= 0) switch(op_code)  if (next >= 0) switch(op_code)
3099    {    {
3100    case OP_CHAR:    case OP_CHAR:
3101  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3102    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3103  #else  #else
3104    c = *previous;    c = *previous;
3105  #endif  #endif
3106    return c != next;    return c != next;
3107    
3108    /* 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
3109    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
3110    high-valued characters. */    high-valued characters. */
3111    
3112    case OP_CHARNC:    case OP_CHARI:
3113  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3114    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3115  #else  #else
3116    c = *previous;    c = *previous;
3117  #endif  #endif
3118    if (c == next) return FALSE;    if (c == next) return FALSE;
3119  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3120    if (utf8)    if (utf)
3121      {      {
3122      unsigned int othercase;      unsigned int othercase;
3123      if (next < 128) othercase = cd->fcc[next]; else      if (next < 128) othercase = cd->fcc[next]; else
# Line 2580  if (next >= 0) switch(op_code) Line 3129  if (next >= 0) switch(op_code)
3129      return (unsigned int)c != othercase;      return (unsigned int)c != othercase;
3130      }      }
3131    else    else
3132  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF */
3133    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */
   
   /* For OP_NOT, its data is always a single-byte character. */  
3134    
3135    case OP_NOT:    case OP_NOT:
3136    if ((c = *previous) == next) return TRUE;  #ifdef SUPPORT_UTF
3137    if ((options & PCRE_CASELESS) == 0) return FALSE;    GETCHARTEST(c, previous);
3138  #ifdef SUPPORT_UTF8  #else
3139    if (utf8)    c = *previous;
3140    #endif
3141      return c == next;
3142    
3143      case OP_NOTI:
3144    #ifdef SUPPORT_UTF
3145      GETCHARTEST(c, previous);
3146    #else
3147      c = *previous;
3148    #endif
3149      if (c == next) return TRUE;
3150    #ifdef SUPPORT_UTF
3151      if (utf)
3152      {      {
3153      unsigned int othercase;      unsigned int othercase;
3154      if (next < 128) othercase = cd->fcc[next]; else      if (next < 128) othercase = cd->fcc[next]; else
3155  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3156      othercase = UCD_OTHERCASE(next);      othercase = UCD_OTHERCASE((unsigned int)next);
3157  #else  #else
3158      othercase = NOTACHAR;      othercase = NOTACHAR;
3159  #endif  #endif
3160      return (unsigned int)c == othercase;      return (unsigned int)c == othercase;
3161      }      }
3162    else    else
3163  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF */
3164    return (c == cd->fcc[next]);  /* Non-UTF-8 mode */    return (c == TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */
3165    
3166    /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.    /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3167    When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */    When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3168    
3169    case OP_DIGIT:    case OP_DIGIT:
3170    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;    return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
3171    
3172    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
3173    return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;    return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
3174    
3175    case OP_WHITESPACE:    case OP_WHITESPACE:
3176    return next > 127 || (cd->ctypes[next] & ctype_space) == 0;    return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
3177    
3178    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3179    return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;    return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
3180    
3181    case OP_WORDCHAR:    case OP_WORDCHAR:
3182    return next > 127 || (cd->ctypes[next] & ctype_word) == 0;    return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
3183    
3184    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
3185    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;    return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
3186    
3187    case OP_HSPACE:    case OP_HSPACE:
3188    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
3189    switch(next)    switch(next)
3190      {      {
3191      case 0x09:      HSPACE_CASES:
     case 0x20:  
     case 0xa0:  
     case 0x1680:  
     case 0x180e:  
     case 0x2000:  
     case 0x2001:  
     case 0x2002:  
     case 0x2003:  
     case 0x2004:  
     case 0x2005:  
     case 0x2006:  
     case 0x2007:  
     case 0x2008:  
     case 0x2009:  
     case 0x200A:  
     case 0x202f:  
     case 0x205f:  
     case 0x3000:  
3192      return op_code == OP_NOT_HSPACE;      return op_code == OP_NOT_HSPACE;
3193    
3194      default:      default:
3195      return op_code != OP_NOT_HSPACE;      return op_code != OP_NOT_HSPACE;
3196      }      }
# Line 2658  if (next >= 0) switch(op_code) Line 3200  if (next >= 0) switch(op_code)
3200    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3201    switch(next)    switch(next)
3202      {      {
3203      case 0x0a:      VSPACE_CASES:
     case 0x0b:  
     case 0x0c:  
     case 0x0d:  
     case 0x85:  
     case 0x2028:  
     case 0x2029:  
3204      return op_code == OP_NOT_VSPACE;      return op_code == OP_NOT_VSPACE;
3205    
3206      default:      default:
3207      return op_code != OP_NOT_VSPACE;      return op_code != OP_NOT_VSPACE;
3208      }      }
# Line 2692  replaced by OP_PROP codes when PCRE_UCP Line 3229  replaced by OP_PROP codes when PCRE_UCP
3229  switch(op_code)  switch(op_code)
3230    {    {
3231    case OP_CHAR:    case OP_CHAR:
3232    case OP_CHARNC:    case OP_CHARI:
3233  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3234    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3235  #else  #else
3236    c = *previous;    c = *previous;
# Line 2701  switch(op_code) Line 3238  switch(op_code)
3238    switch(-next)    switch(-next)
3239      {      {
3240      case ESC_d:      case ESC_d:
3241      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;
3242    
3243      case ESC_D:      case ESC_D:
3244      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;
3245    
3246      case ESC_s:      case ESC_s:
3247      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;
3248    
3249      case ESC_S:      case ESC_S:
3250      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;
3251    
3252      case ESC_w:      case ESC_w:
3253      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;
3254    
3255      case ESC_W:      case ESC_W:
3256      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;
3257    
3258      case ESC_h:      case ESC_h:
3259      case ESC_H:      case ESC_H:
3260      switch(c)      switch(c)
3261        {        {
3262        case 0x09:        HSPACE_CASES:
       case 0x20:  
       case 0xa0:  
       case 0x1680:  
       case 0x180e:  
       case 0x2000:  
       case 0x2001:  
       case 0x2002:  
       case 0x2003:  
       case 0x2004:  
       case 0x2005:  
       case 0x2006:  
       case 0x2007:  
       case 0x2008:  
       case 0x2009:  
       case 0x200A:  
       case 0x202f:  
       case 0x205f:  
       case 0x3000:  
3263        return -next != ESC_h;        return -next != ESC_h;
3264    
3265        default:        default:
3266        return -next == ESC_h;        return -next == ESC_h;
3267        }        }
# Line 2750  switch(op_code) Line 3270  switch(op_code)
3270      case ESC_V:      case ESC_V:
3271      switch(c)      switch(c)
3272        {        {
3273        case 0x0a:        VSPACE_CASES:
       case 0x0b:  
       case 0x0c:  
       case 0x0d:  
       case 0x85:  
       case 0x2028:  
       case 0x2029:  
3274        return -next != ESC_v;        return -next != ESC_v;
3275    
3276        default:        default:
3277        return -next == ESC_v;        return -next == ESC_v;
3278        }        }
# Line 2798  switch(op_code) Line 3313  switch(op_code)
3313        to the original \d etc. At this point, ptr will point to a zero byte. */        to the original \d etc. At this point, ptr will point to a zero byte. */
3314    
3315        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3316          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)
3317            return FALSE;            return FALSE;
3318    
3319        /* Do the property check. */        /* Do the property check. */
# Line 2825  switch(op_code) Line 3340  switch(op_code)
3340    return next == -ESC_d;    return next == -ESC_d;
3341    
3342    case OP_WHITESPACE:    case OP_WHITESPACE:
3343    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;    return next == -ESC_S || next == -ESC_d || next == -ESC_w;
3344    
3345    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3346    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_s || next == -ESC_h || next == -ESC_v || next == -ESC_R;
3347    
3348    case OP_HSPACE:    case OP_HSPACE:
3349    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||
# Line 2862  switch(op_code) Line 3377  switch(op_code)
3377    
3378    
3379  /*************************************************  /*************************************************
3380    *        Add a character or range to a class     *
3381    *************************************************/
3382    
3383    /* This function packages up the logic of adding a character or range of
3384    characters to a class. The character values in the arguments will be within the
3385    valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3386    mutually recursive with the function immediately below.
3387    
3388    Arguments:
3389      classbits     the bit map for characters < 256
3390      uchardptr     points to the pointer for extra data
3391      options       the options word
3392      cd            contains pointers to tables etc.
3393      start         start of range character
3394      end           end of range character
3395    
3396    Returns:        the number of < 256 characters added
3397                    the pointer to extra data is updated
3398    */
3399    
3400    static int
3401    add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3402      compile_data *cd, unsigned int start, unsigned int end)
3403    {
3404    unsigned int c;
3405    int n8 = 0;
3406    
3407    /* If caseless matching is required, scan the range and process alternate
3408    cases. In Unicode, there are 8-bit characters that have alternate cases that
3409    are greater than 255 and vice-versa. Sometimes we can just extend the original
3410    range. */
3411    
3412    if ((options & PCRE_CASELESS) != 0)
3413      {
3414    #ifdef SUPPORT_UCP
3415      if ((options & PCRE_UTF8) != 0)
3416        {
3417        int rc;
3418        unsigned int oc, od;
3419    
3420        options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3421        c = start;
3422    
3423        while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3424          {
3425          /* Handle a single character that has more than one other case. */
3426    
3427          if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3428            PRIV(ucd_caseless_sets) + rc, oc);
3429    
3430          /* Do nothing if the other case range is within the original range. */
3431    
3432          else if (oc >= start && od <= end) continue;
3433    
3434          /* Extend the original range if there is overlap, noting that if oc < c, we
3435          can't have od > end because a subrange is always shorter than the basic
3436          range. Otherwise, use a recursive call to add the additional range. */
3437    
3438          else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3439          else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3440          else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3441          }
3442        }
3443      else
3444    #endif  /* SUPPORT_UCP */
3445    
3446      /* Not UTF-mode, or no UCP */
3447    
3448      for (c = start; c <= end && c < 256; c++)
3449        {
3450        SETBIT(classbits, cd->fcc[c]);
3451        n8++;
3452        }
3453      }
3454    
3455    /* Now handle the original range. Adjust the final value according to the bit
3456    length - this means that the same lists of (e.g.) horizontal spaces can be used
3457    in all cases. */
3458    
3459    #ifdef COMPILE_PCRE8
3460    #ifdef SUPPORT_UTF
3461      if ((options & PCRE_UTF8) == 0)
3462    #endif
3463      if (end > 0xff) end = 0xff;
3464    #endif
3465    
3466    #ifdef COMPILE_PCRE16
3467    #ifdef SUPPORT_UTF
3468      if ((options & PCRE_UTF16) == 0)
3469    #endif
3470      if (end > 0xffff) end = 0xffff;
3471    #endif
3472    
3473    /* If all characters are less than 256, use the bit map. Otherwise use extra
3474    data. */
3475    
3476    if (end < 0x100)
3477      {
3478      for (c = start; c <= end; c++)
3479        {
3480        n8++;
3481        SETBIT(classbits, c);
3482        }
3483      }
3484    
3485    else
3486      {
3487      pcre_uchar *uchardata = *uchardptr;
3488    
3489    #ifdef SUPPORT_UTF
3490      if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3491        {
3492        if (start < end)
3493          {
3494          *uchardata++ = XCL_RANGE;
3495          uchardata += PRIV(ord2utf)(start, uchardata);
3496          uchardata += PRIV(ord2utf)(end, uchardata);
3497          }
3498        else if (start == end)
3499          {
3500          *uchardata++ = XCL_SINGLE;
3501          uchardata += PRIV(ord2utf)(start, uchardata);
3502          }
3503        }
3504      else
3505    #endif  /* SUPPORT_UTF */
3506    
3507      /* Without UTF support, character values are constrained by the bit length,
3508      and can only be > 256 for 16-bit and 32-bit libraries. */
3509    
3510    #ifdef COMPILE_PCRE8
3511        {}
3512    #else
3513      if (start < end)
3514        {
3515        *uchardata++ = XCL_RANGE;
3516        *uchardata++ = start;
3517        *uchardata++ = end;
3518        }
3519      else if (start == end)
3520        {
3521        *uchardata++ = XCL_SINGLE;
3522        *uchardata++ = start;
3523        }
3524    #endif
3525    
3526      *uchardptr = uchardata;   /* Updata extra data pointer */
3527      }
3528    
3529    return n8;    /* Number of 8-bit characters */
3530    }
3531    
3532    
3533    
3534    
3535    /*************************************************
3536    *        Add a list of characters to a class     *
3537    *************************************************/
3538    
3539    /* This function is used for adding a list of case-equivalent characters to a
3540    class, and also for adding a list of horizontal or vertical whitespace. If the
3541    list is in order (which it should be), ranges of characters are detected and
3542    handled appropriately. This function is mutually recursive with the function
3543    above.
3544    
3545    Arguments:
3546      classbits     the bit map for characters < 256
3547      uchardptr     points to the pointer for extra data
3548      options       the options word
3549      cd            contains pointers to tables etc.
3550      p             points to row of 32-bit values, terminated by NOTACHAR
3551      except        character to omit; this is used when adding lists of
3552                      case-equivalent characters to avoid including the one we
3553                      already know about
3554    
3555    Returns:        the number of < 256 characters added
3556                    the pointer to extra data is updated
3557    */
3558    
3559    static int
3560    add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3561      compile_data *cd, const pcre_uint32 *p, unsigned int except)
3562    {
3563    int n8 = 0;
3564    while (p[0] < NOTACHAR)
3565      {
3566      int n = 0;
3567      if (p[0] != except)
3568        {
3569        while(p[n+1] == p[0] + n + 1) n++;
3570        n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3571        }
3572      p += n + 1;
3573      }
3574    return n8;
3575    }
3576    
3577    
3578    
3579    /*************************************************
3580    *    Add characters not in a list to a class     *
3581    *************************************************/
3582    
3583    /* This function is used for adding the complement of a list of horizontal or
3584    vertical whitespace to a class. The list must be in order.
3585    
3586    Arguments:
3587      classbits     the bit map for characters < 256
3588      uchardptr     points to the pointer for extra data
3589      options       the options word
3590      cd            contains pointers to tables etc.
3591      p             points to row of 32-bit values, terminated by NOTACHAR
3592    
3593    Returns:        the number of < 256 characters added
3594                    the pointer to extra data is updated
3595    */
3596    
3597    static int
3598    add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3599      int options, compile_data *cd, const pcre_uint32 *p)
3600    {
3601    int n8 = 0;
3602    if (p[0] > 0)
3603      n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
3604    while (p[0] < NOTACHAR)
3605      {
3606      while (p[1] == p[0] + 1) p++;
3607      n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3608        (p[1] == NOTACHAR)? 0x10ffff : p[1] - 1);
3609      p++;
3610      }
3611    return n8;
3612    }
3613    
3614    
3615    
3616    /*************************************************
3617  *           Compile one branch                   *  *           Compile one branch                   *
3618  *************************************************/  *************************************************/
3619    
# Line 2876  Arguments: Line 3628  Arguments:
3628    codeptr        points to the pointer to the current code point    codeptr        points to the pointer to the current code point
3629    ptrptr         points to the current pattern pointer    ptrptr         points to the current pattern pointer
3630    errorcodeptr   points to error code variable    errorcodeptr   points to error code variable
3631    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3632    reqbyteptr     set to the last literal character required, else < 0    reqcharptr     set to the last literal character required, else < 0
3633    bcptr          points to current branch chain    bcptr          points to current branch chain
3634      cond_depth     conditional nesting depth
3635    cd             contains pointers to tables etc.    cd             contains pointers to tables etc.
3636    lengthptr      NULL during the real compile phase    lengthptr      NULL during the real compile phase
3637                   points to length accumulator during pre-compile phase                   points to length accumulator during pre-compile phase
# Line 2888  Returns:         TRUE on success Line 3641  Returns:         TRUE on success
3641  */  */
3642    
3643  static BOOL  static BOOL
3644  compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3645    int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,
3646      pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,
3647    compile_data *cd, int *lengthptr)    compile_data *cd, int *lengthptr)
3648  {  {
3649  int repeat_type, op_type;  int repeat_type, op_type;
3650  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3651  int bravalue = 0;  int bravalue = 0;
3652  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3653  int firstbyte, reqbyte;  pcre_int32 firstchar, reqchar;
3654  int zeroreqbyte, zerofirstbyte;  pcre_int32 zeroreqchar, zerofirstchar;
3655  int req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
3656  int options = *optionsptr;  int options = *optionsptr;               /* May change dynamically */
3657  int after_manual_callout = 0;  int after_manual_callout = 0;
3658  int length_prevgroup = 0;  int length_prevgroup = 0;
3659  register int c;  register int c;
3660  register uschar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3661  uschar *last_code = code;  pcre_uchar *last_code = code;
3662  uschar *orig_code = code;  pcre_uchar *orig_code = code;
3663  uschar *tempcode;  pcre_uchar *tempcode;
3664  BOOL inescq = FALSE;  BOOL inescq = FALSE;
3665  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstchar = FALSE;
3666  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
3667  const uschar *tempptr;  const pcre_uchar *tempptr;
3668  const uschar *nestptr = NULL;  const pcre_uchar *nestptr = NULL;
3669  uschar *previous = NULL;  pcre_uchar *previous = NULL;
3670  uschar *previous_callout = NULL;  pcre_uchar *previous_callout = NULL;
3671  uschar *save_hwm = NULL;  pcre_uchar *save_hwm = NULL;
3672  uschar classbits[32];  pcre_uint8 classbits[32];
3673    
3674  #ifdef SUPPORT_UTF8  /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3675  BOOL class_utf8;  must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3676  BOOL utf8 = (options & PCRE_UTF8) != 0;  dynamically as we process the pattern. */
3677  uschar *class_utf8data;  
3678  uschar *class_utf8data_base;  #ifdef SUPPORT_UTF
3679  uschar utf8_char[6];  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
3680    BOOL utf = (options & PCRE_UTF8) != 0;
3681    pcre_uchar utf_chars[6];
3682  #else  #else
3683  BOOL utf8 = FALSE;  BOOL utf = FALSE;
3684  uschar *utf8_char = NULL;  #endif
3685    
3686    /* Helper variables for OP_XCLASS opcode (for characters > 255). */
3687    
3688    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3689    BOOL xclass;
3690    pcre_uchar *class_uchardata;
3691    pcre_uchar *class_uchardata_base;
3692  #endif  #endif
3693    
3694  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
# Line 2939  greedy_non_default = greedy_default ^ 1; Line 3702  greedy_non_default = greedy_default ^ 1;
3702    
3703  /* Initialize no first byte, no required byte. REQ_UNSET means "no char  /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3704  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
3705  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
3706  find one.  find one.
3707    
3708  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
3709  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
3710  zerofirstbyte and zeroreqbyte when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3711  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
3712    
3713  firstbyte = reqbyte = zerofirstbyte = zeroreqbyte = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;
3714    
3715  /* The variable req_caseopt contains either the REQ_CASELESS value or zero,  /* The variable req_caseopt contains either the REQ_CASELESS value
3716  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
3717  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
3718  case status of the value. This is used only for ASCII characters. */  firstchar or reqchar variables to record the case status of the
3719    value. This is used only for ASCII characters. */
3720    
3721  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
3722    
3723  /* Switch on next character until the end of the branch */  /* Switch on next character until the end of the branch */
3724    
# Line 2966  for (;; ptr++) Line 3730  for (;; ptr++)
3730    BOOL is_quantifier;    BOOL is_quantifier;
3731    BOOL is_recurse;    BOOL is_recurse;
3732    BOOL reset_bracount;    BOOL reset_bracount;
3733    int class_charcount;    int class_has_8bitchar;
3734    int class_lastchar;    int class_one_char;
3735    int newoptions;    int newoptions;
3736    int recno;    int recno;
3737    int refsign;    int refsign;
3738    int skipbytes;    int skipbytes;
3739    int subreqbyte;    int subreqchar;
3740    int subfirstbyte;    int subfirstchar;
3741    int terminator;    int terminator;
3742    int mclength;    int mclength;
3743    uschar mcbuffer[8];    int tempbracount;
3744      pcre_uchar mcbuffer[8];
3745    
3746    /* Get next byte in the pattern */    /* Get next character in the pattern */
3747    
3748    c = *ptr;    c = *ptr;
3749    
# Line 3000  for (;; ptr++) Line 3765  for (;; ptr++)
3765  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
3766      if (code > cd->hwm) cd->hwm = code;                 /* High water info */      if (code > cd->hwm) cd->hwm = code;                 /* High water info */
3767  #endif  #endif
3768      if (code > cd->start_workspace + WORK_SIZE_CHECK)   /* Check for overrun */      if (code > cd->start_workspace + cd->workspace_size -
3769            WORK_SIZE_SAFETY_MARGIN)                       /* Check for overrun */
3770        {        {
3771        *errorcodeptr = ERR52;        *errorcodeptr = ERR52;
3772        goto FAILED;        goto FAILED;
# Line 3023  for (;; ptr++) Line 3789  for (;; ptr++)
3789        }        }
3790    
3791      *lengthptr += (int)(code - last_code);      *lengthptr += (int)(code - last_code);
3792      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, code - last_code, c));      DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
3793          (int)(code - last_code), c, c));
3794    
3795      /* 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
3796      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 3033  for (;; ptr++) Line 3800  for (;; ptr++)
3800        {        {
3801        if (previous > orig_code)        if (previous > orig_code)
3802          {          {
3803          memmove(orig_code, previous, code - previous);          memmove(orig_code, previous, IN_UCHARS(code - previous));
3804          code -= previous - orig_code;          code -= previous - orig_code;
3805          previous = orig_code;          previous = orig_code;
3806          }          }
# Line 3049  for (;; ptr++) Line 3816  for (;; ptr++)
3816    /* 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
3817    reference list. */    reference list. */
3818    
3819    else if (cd->hwm > cd->start_workspace + WORK_SIZE_CHECK)    else if (cd->hwm > cd->start_workspace + cd->workspace_size -
3820               WORK_SIZE_SAFETY_MARGIN)
3821      {      {
3822      *errorcodeptr = ERR52;      *errorcodeptr = ERR52;
3823      goto FAILED;      goto FAILED;
# Line 3097  for (;; ptr++) Line 3865  for (;; ptr++)
3865      previous_callout = NULL;      previous_callout = NULL;
3866      }      }
3867    
3868    /* In extended mode, skip white space and comments */    /* In extended mode, skip white space and comments. */
3869    
3870    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
3871      {      {
3872      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if (MAX_255(*ptr) && (cd->ctypes[c] & ctype_space) != 0) continue;
3873      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3874        {        {
3875        while (*(++ptr) != 0)        ptr++;
3876          while (*ptr != 0)
3877          {          {
3878          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3879            ptr++;
3880    #ifdef SUPPORT_UTF
3881            if (utf) FORWARDCHAR(ptr);
3882    #endif
3883          }          }
3884        if (*ptr != 0) continue;        if (*ptr != 0) continue;
3885    
# Line 3129  for (;; ptr++) Line 3902  for (;; ptr++)
3902      case 0:                        /* The branch terminates at string end */      case 0:                        /* The branch terminates at string end */
3903      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
3904      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
3905      *firstbyteptr = firstbyte;      *firstcharptr = firstchar;
3906      *reqbyteptr = reqbyte;      *reqcharptr = reqchar;
3907      *codeptr = code;      *codeptr = code;
3908      *ptrptr = ptr;      *ptrptr = ptr;
3909      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 3151  for (;; ptr++) Line 3924  for (;; ptr++)
3924      the setting of any following char as a first character. */      the setting of any following char as a first character. */
3925    
3926      case CHAR_CIRCUMFLEX_ACCENT:      case CHAR_CIRCUMFLEX_ACCENT:
3927        previous = NULL;
3928      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3929        {        {
3930        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3931          *code++ = OP_CIRCM;
3932        }        }
3933      previous = NULL;      else *code++ = OP_CIRC;
     *code++ = OP_CIRC;  
3934      break;      break;
3935    
3936      case CHAR_DOLLAR_SIGN:      case CHAR_DOLLAR_SIGN:
3937      previous = NULL;      previous = NULL;
3938      *code++ = OP_DOLL;      *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
3939      break;      break;
3940    
3941      /* 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
3942      repeats. The value of reqbyte doesn't change either. */      repeats. The value of reqchar doesn't change either. */
3943    
3944      case CHAR_DOT:      case CHAR_DOT:
3945      if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3946      zerofirstbyte = firstbyte;      zerofirstchar = firstchar;
3947      zeroreqbyte = reqbyte;      zeroreqchar = reqchar;
3948      previous = code;      previous = code;
3949      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
3950      break;      break;
# Line 3225  for (;; ptr++) Line 3999  for (;; ptr++)
3999          {          {
4000          if (ptr[1] == CHAR_E)          if (ptr[1] == CHAR_E)
4001            ptr++;            ptr++;
4002          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)  
4003            ptr += 3;            ptr += 3;
4004          else          else
4005            break;            break;
# Line 3245  for (;; ptr++) Line 4018  for (;; ptr++)
4018          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4019        {        {
4020        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
4021        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4022        zerofirstbyte = firstbyte;        zerofirstchar = firstchar;
4023        break;        break;
4024        }        }
4025    
# Line 3256  for (;; ptr++) Line 4029  for (;; ptr++)
4029    
4030      should_flip_negation = FALSE;      should_flip_negation = FALSE;
4031    
4032      /* Keep a count of chars with values < 256 so that we can optimize the case      /* For optimization purposes, we track some properties of the class:
4033      of just a single character (as long as it's < 256). However, For higher      class_has_8bitchar will be non-zero if the class contains at least one <
4034      valued UTF-8 characters, we don't yet do any optimization. */      256 character; class_one_char will be 1 if the class contains just one
4035        character. */
4036    
4037      class_charcount = 0;      class_has_8bitchar = 0;
4038      class_lastchar = -1;      class_one_char = 0;
4039    
4040      /* Initialize the 32-char bit map to all zeros. We build the map in a      /* Initialize the 32-char bit map to all zeros. We build the map in a
4041      temporary bit of memory, in case the class contains only 1 character (less      temporary bit of memory, in case the class contains fewer than two
4042      than 256), because in that case the compiled code doesn't use the bit map.      8-bit characters because in that case the compiled code doesn't use the bit
4043      */      map. */
4044    
4045      memset(classbits, 0, 32 * sizeof(uschar));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
4046    
4047  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4048      class_utf8 = FALSE;                       /* No chars >= 256 */      xclass = FALSE;
4049      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
4050      class_utf8data_base = class_utf8data;     /* For resetting in pass 1 */      class_uchardata_base = class_uchardata;   /* Save the start */
4051  #endif  #endif
4052    
4053      /* 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 3282  for (;; ptr++) Line 4056  for (;; ptr++)
4056    
4057      if (c != 0) do      if (c != 0) do
4058        {        {
4059        const uschar *oldptr;        const pcre_uchar *oldptr;
4060    
4061  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4062        if (utf8 && c > 127)        if (utf && HAS_EXTRALEN(c))
4063          {                           /* Braces are required because the */          {                           /* Braces are required because the */
4064          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
4065          }          }
4066    #endif
4067    
4068        /* In the pre-compile phase, accumulate the length of any UTF-8 extra  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4069          /* In the pre-compile phase, accumulate the length of any extra
4070        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
4071        contain a zillion UTF-8 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
4072        (which is on the stack). */        (which is on the stack). We have to remember that there was XCLASS data,
4073          however. */
4074        if (lengthptr != NULL)  
4075          {        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
4076          *lengthptr += class_utf8data - class_utf8data_base;          {
4077          class_utf8data = class_utf8data_base;          xclass = TRUE;
4078            *lengthptr += class_uchardata - class_uchardata_base;
4079            class_uchardata = class_uchardata_base;
4080          }          }
   
4081  #endif  #endif
4082    
4083        /* Inside \Q...\E everything is literal except \E */        /* Inside \Q...\E everything is literal except \E */
# Line 3328  for (;; ptr++) Line 4105  for (;; ptr++)
4105          {          {
4106          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
4107          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
4108          register const uschar *cbits = cd->cbits;          register const pcre_uint8 *cbits = cd->cbits;
4109          uschar pbits[32];          pcre_uint8 pbits[32];
4110    
4111          if (ptr[1] != CHAR_COLON)          if (ptr[1] != CHAR_COLON)
4112            {            {
# Line 3384  for (;; ptr++) Line 4161  for (;; ptr++)
4161          /* Copy in the first table (always present) */          /* Copy in the first table (always present) */
4162    
4163          memcpy(pbits, cbits + posix_class_maps[posix_class],          memcpy(pbits, cbits + posix_class_maps[posix_class],
4164            32 * sizeof(uschar));            32 * sizeof(pcre_uint8));
4165    
4166          /* If there is a second table, add or remove it as required. */          /* If there is a second table, add or remove it as required. */
4167    
# Line 3399  for (;; ptr++) Line 4176  for (;; ptr++)
4176              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4177            }            }
4178    
4179          /* Not see if we need to remove any special characters. An option          /* Now see if we need to remove any special characters. An option
4180          value of 1 removes vertical space and 2 removes underscore. */          value of 1 removes vertical space and 2 removes underscore. */
4181    
4182          if (tabopt < 0) tabopt = -tabopt;          if (tabopt < 0) tabopt = -tabopt;
# Line 3415  for (;; ptr++) Line 4192  for (;; ptr++)
4192            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4193    
4194          ptr = tempptr + 1;          ptr = tempptr + 1;
4195          class_charcount = 10;  /* Set > 1; assumes more than 1 per class */          /* Every class contains at least one < 256 character. */
4196            class_has_8bitchar = 1;
4197            /* Every class contains at least two characters. */
4198            class_one_char = 2;
4199          continue;    /* End of POSIX syntax handling */          continue;    /* End of POSIX syntax handling */
4200          }          }
4201    
4202        /* Backslash may introduce a single character, or it may introduce one        /* Backslash may introduce a single character, or it may introduce one
4203        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
4204        case. Inside a class (and only there) it is treated as backspace. We        case. Inside a class (and only there) it is treated as backspace. We
4205        assume that other escapes have more than one character in them, so set        assume that other escapes have more than one character in them, so
4206        class_charcount bigger than one. Unrecognized escapes fall through and        speculatively set both class_has_8bitchar and class_one_char bigger
4207        are either treated as literal characters (by default), or are faulted if        than one. Unrecognized escapes fall through and are either treated
4208          as literal characters (by default), or are faulted if
4209        PCRE_EXTRA is set. */        PCRE_EXTRA is set. */
4210    
4211        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
# Line 3433  for (;; ptr++) Line 4214  for (;; ptr++)
4214          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
4215    
4216          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 */
4217            else if (-c == ESC_N)            /* \N is not supported in a class */
4218              {
4219              *errorcodeptr = ERR71;
4220              goto FAILED;
4221              }
4222          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (-c == ESC_Q)            /* Handle start of quoted string */
4223            {            {
4224            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 3446  for (;; ptr++) Line 4232  for (;; ptr++)
4232    
4233          if (c < 0)          if (c < 0)
4234            {            {
4235            register const uschar *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
4236            class_charcount += 2;     /* Greater than 1 is what matters */            /* Every class contains at least two < 256 characters. */
4237              class_has_8bitchar++;
4238              /* Every class contains at least two characters. */
4239              class_one_char += 2;
4240    
4241            switch (-c)            switch (-c)
4242              {              {
# Line 3460  for (;; ptr++) Line 4249  for (;; ptr++)
4249              case ESC_SU:              case ESC_SU:
4250              nestptr = ptr;              nestptr = ptr;
4251              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */
4252              class_charcount -= 2;                /* Undo! */              class_has_8bitchar--;                /* Undo! */
4253              continue;              continue;
4254  #endif  #endif
4255              case ESC_d:              case ESC_d:
# Line 3481  for (;; ptr++) Line 4270  for (;; ptr++)
4270              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
4271              continue;              continue;
4272    
4273                /* Perl 5.004 onwards omits VT from \s, but we must preserve it
4274                if it was previously set by something earlier in the character
4275                class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and
4276                EBCDIC, so we lazily just adjust the appropriate bit. */
4277    
4278              case ESC_s:              case ESC_s:
4279              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];              classbits[0] |= cbits[cbit_space];
4280              classbits[1] &= ~0x08;   /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= cbits[cbit_space+1] & ~0x08;
4281                for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
4282              continue;              continue;
4283    
4284              case ESC_S:              case ESC_S:
# Line 3491  for (;; ptr++) Line 4286  for (;; ptr++)
4286              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
4287              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
4288              continue;              continue;
4289    
4290                /* The rest apply in both UCP and non-UCP cases. */
4291    
4292              case ESC_h:              case ESC_h:
4293              SETBIT(classbits, 0x09); /* VT */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4294              SETBIT(classbits, 0x20); /* SPACE */                PRIV(hspace_list), NOTACHAR);
             SETBIT(classbits, 0xa0); /* NSBP */  
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);  
               }  
 #endif  
4295              continue;              continue;
4296    
4297              case ESC_H:              case ESC_H:
4298              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4299                {                cd, PRIV(hspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x09/8: x ^= 1 << (0x09%8); break;  
                 case 0x20/8: x ^= 1 << (0x20%8); break;  
                 case 0xa0/8: x ^= 1 << (0xa0%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
   
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);  
               }  
 #endif  
4300              continue;              continue;
4301    
4302              case ESC_v:              case ESC_v:
4303              SETBIT(classbits, 0x0a); /* LF */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4304              SETBIT(classbits, 0x0b); /* VT */                PRIV(vspace_list), NOTACHAR);
             SETBIT(classbits, 0x0c); /* FF */  
             SETBIT(classbits, 0x0d); /* CR */  
             SETBIT(classbits, 0x85); /* NEL */  
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);  
               }  
 #endif  
4305              continue;              continue;
4306    
4307              case ESC_V:              case ESC_V:
4308              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4309                {                cd, PRIV(vspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x0a/8: x ^= 1 << (0x0a%8);  
                              x ^= 1 << (0x0b%8);  
                              x ^= 1 << (0x0c%8);  
                              x ^= 1 << (0x0d%8);  
                              break;  
                 case 0x85/8: x ^= 1 << (0x85%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
   
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);  
               }  
 #endif  
4310              continue;              continue;
4311    
4312  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 3616  for (;; ptr++) Line 4317  for (;; ptr++)
4317                int pdata;                int pdata;
4318                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
4319                if (ptype < 0) goto FAILED;                if (ptype < 0) goto FAILED;
4320                class_utf8 = TRUE;                *class_uchardata++ = ((-c == ESC_p) != negated)?
               *class_utf8data++ = ((-c == ESC_p) != negated)?  
4321                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
4322                *class_utf8data++ = ptype;                *class_uchardata++ = ptype;
4323                *class_utf8data++ = pdata;                *class_uchardata++ = pdata;
4324                class_charcount -= 2;   /* Not a < 256 character */                class_has_8bitchar--;                /* Undo! */
4325                continue;                continue;
4326                }                }
4327  #endif  #endif
# Line 3635  for (;; ptr++) Line 4335  for (;; ptr++)
4335                *errorcodeptr = ERR7;                *errorcodeptr = ERR7;
4336                goto FAILED;                goto FAILED;
4337                }                }
4338              class_charcount -= 2;  /* Undo the default count from above */              class_has_8bitchar--;    /* Undo the speculative increase. */
4339              c = *ptr;              /* Get the final character and fall through */              class_one_char -= 2;     /* Undo the speculative increase. */
4340                c = *ptr;                /* Get the final character and fall through */
4341              break;              break;
4342              }              }
4343            }            }
4344    
4345          /* Fall through if we have a single character (c >= 0). This may be          /* Fall through if the escape just defined a single character (c >= 0).
4346          greater than 256 in UTF-8 mode. */          This may be greater than 256. */
4347    
4348          }   /* End of backslash handling */          }   /* End of backslash handling */
4349    
4350        /* A single character may be followed by '-' to form a range. However,        /* A character may be followed by '-' to form a range. However, Perl does
4351        Perl does not permit ']' to be the end of the range. A '-' character        not permit ']' to be the end of the range. A '-' character at the end is
4352        at the end is treated as a literal. Perl ignores orphaned \E sequences        treated as a literal. Perl ignores orphaned \E sequences entirely. The
4353        entirely. The code for handling \Q and \E is messy. */        code for handling \Q and \E is messy. */
4354    
4355        CHECK_RANGE:        CHECK_RANGE:
4356        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 3657  for (;; ptr++) Line 4358  for (;; ptr++)
4358          inescq = FALSE;          inescq = FALSE;
4359          ptr += 2;          ptr += 2;
4360          }          }
   
4361        oldptr = ptr;        oldptr = ptr;
4362    
4363        /* Remember \r or \n */        /* Remember if \r or \n were explicitly used */
4364    
4365        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4366    
# Line 3683  for (;; ptr++) Line 4383  for (;; ptr++)
4383            inescq = TRUE;            inescq = TRUE;
4384            break;            break;
4385            }            }
4386    
4387            /* Minus (hyphen) at the end of a class is treated as a literal, so put
4388            back the pointer and jump to handle the character that preceded it. */
4389    
4390          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4391            {            {
4392            ptr = oldptr;            ptr = oldptr;
4393            goto LONE_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4394            }            }
4395    
4396            /* Otherwise, we have a potential range; pick up the next character */
4397    
4398  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4399          if (utf8)          if (utf)
4400            {                           /* Braces are required because the */            {                           /* Braces are required because the */
4401            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */
4402            }            }
# Line 3708  for (;; ptr++) Line 4413  for (;; ptr++)
4413            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
4414            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4415    
4416            /* \b is backspace; any other special means the '-' was literal */            /* \b is backspace; any other special means the '-' was literal. */
4417    
4418            if (d < 0)            if (d < 0)
4419              {              {
4420              if (d == -ESC_b) d = CHAR_BS; else              if (d == -ESC_b) d = CHAR_BS; else
4421                {                {
4422                ptr = oldptr;                ptr = oldptr;
4423                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4424                }                }
4425              }              }
4426            }            }
4427    
4428          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
4429          one-character ranges */          one-character ranges. */
   
         if (d < c)  
           {  
           *errorcodeptr = ERR8;  
           goto FAILED;  
           }  
   
         if (d == c) goto LONE_SINGLE_CHARACTER;  /* A few lines below */  
   
         /* Remember \r or \n */  
   
         if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;  
   
         /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless  
         matching, we have to use an XCLASS with extra data items. Caseless  
         matching for characters > 127 is available only if UCP support is  
         available. */  
   
 #ifdef SUPPORT_UTF8  
         if (utf8 && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))  
           {  
           class_utf8 = TRUE;  
   
           /* With UCP support, we can find the other case equivalents of  
           the relevant characters. There may be several ranges. Optimize how  
           they fit with the basic range. */  
   
 #ifdef SUPPORT_UCP  
           if ((options & PCRE_CASELESS) != 0)  
             {  
             unsigned int occ, ocd;  
             unsigned int cc = c;  
             unsigned int origd = d;  
             while (get_othercase_range(&cc, origd, &occ, &ocd))  
               {  
               if (occ >= (unsigned int)c &&  
                   ocd <= (unsigned int)d)  
                 continue;                          /* Skip embedded ranges */  
   
               if (occ < (unsigned int)c  &&  
                   ocd >= (unsigned int)c - 1)      /* Extend the basic range */  
                 {                                  /* if there is overlap,   */  
                 c = occ;                           /* noting that if occ < c */  
                 continue;                          /* we can't have ocd > d  */  
                 }                                  /* because a subrange is  */  
               if (ocd > (unsigned int)d &&  
                   occ <= (unsigned int)d + 1)      /* always shorter than    */  
                 {                                  /* the basic range.       */  
                 d = ocd;  
                 continue;  
                 }  
   
               if (occ == ocd)  
                 {  
                 *class_utf8data++ = XCL_SINGLE;  
                 }  
               else  
                 {  
                 *class_utf8data++ = XCL_RANGE;  
                 class_utf8data += _pcre_ord2utf8(occ, class_utf8data);  
                 }  
               class_utf8data += _pcre_ord2utf8(ocd, class_utf8data);  
               }  
             }  
 #endif  /* SUPPORT_UCP */  
   
           /* Now record the original range, possibly modified for UCP caseless  
           overlapping ranges. */  
   
           *class_utf8data++ = XCL_RANGE;  
           class_utf8data += _pcre_ord2utf8(c, class_utf8data);  
           class_utf8data += _pcre_ord2utf8(d, class_utf8data);  
   
           /* With UCP support, we are done. Without UCP support, there is no  
           caseless matching for UTF-8 characters > 127; we can use the bit map  
           for the smaller ones. */  
   
 #ifdef SUPPORT_UCP  
           continue;    /* With next character in the class */  
 #else  
           if ((options & PCRE_CASELESS) == 0 || c > 127) continue;  
   
           /* Adjust upper limit and fall through to set up the map */  
   
           d = 127;  
   
 #endif  /* SUPPORT_UCP */  
           }  
 #endif  /* SUPPORT_UTF8 */  
   
         /* We use the bit map for all cases when not in UTF-8 mode; else  
         ranges that lie entirely within 0-127 when there is UCP support; else  
         for partial ranges without UCP support. */  
   
         class_charcount += d - c + 1;  
         class_lastchar = d;  
   
         /* We can save a bit of time by skipping this in the pre-compile. */  
4430    
4431          if (lengthptr == NULL) for (; c <= d; c++)          if (d < c)
4432            {            {
4433            classbits[c/8] |= (1 << (c&7));            *errorcodeptr = ERR8;
4434            if ((options & PCRE_CASELESS) != 0)            goto FAILED;
             {  
             int uc = cd->fcc[c];           /* flip case */  
             classbits[uc/8] |= (1 << (uc&7));  
             }  
4435            }            }
4436            if (d == c) goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4437    
4438          continue;   /* Go get the next char in the class */          /* We have found a character range, so single character optimizations
4439          }          cannot be done anymore. Any value greater than 1 indicates that there
4440            is more than one character. */
4441    
4442            class_one_char = 2;
4443    
4444        /* Handle a lone single character - we can get here for a normal          /* Remember an explicit \r or \n, and add the range to the class. */
       non-escape char, or after \ that introduces a single character or for an  
       apparent range that isn't. */  
4445    
4446        LONE_SINGLE_CHARACTER:          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4447    
4448            class_has_8bitchar +=
4449              add_to_class(classbits, &class_uchardata, options, cd, c, d);
4450    
4451            continue;   /* Go get the next char in the class */
4452            }
4453    
4454        /* Handle a character that cannot go in the bit map */        /* Handle a single character - we can get here for a normal non-escape
4455          char, or after \ that introduces a single character or for an apparent
4456          range that isn't. Only the value 1 matters for class_one_char, so don't
4457          increase it if it is already 2 or more ... just in case there's a class
4458          with a zillion characters in it. */
4459    
4460          CLASS_SINGLE_CHARACTER:
4461          if (class_one_char < 2) class_one_char++;
4462    
4463          /* If class_one_char is 1, we have the first single character in the
4464          class, and there have been no prior ranges, or XCLASS items generated by
4465          escapes. If this is the final character in the class, we can optimize by
4466          turning the item into a 1-character OP_CHAR[I] if it's positive, or
4467          OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
4468          to be set. Otherwise, there can be no first char if this item is first,
4469          whatever repeat count may follow. In the case of reqchar, save the
4470          previous value for reinstating. */
4471    
4472  #ifdef SUPPORT_UTF8        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
       if (utf8 && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127)))  
4473          {          {
4474          class_utf8 = TRUE;          ptr++;
4475          *class_utf8data++ = XCL_SINGLE;          zeroreqchar = reqchar;
         class_utf8data += _pcre_ord2utf8(c, class_utf8data);  
4476    
4477  #ifdef SUPPORT_UCP          if (negate_class)
         if ((options & PCRE_CASELESS) != 0)  
4478            {            {
4479            unsigned int othercase;            if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4480            if ((othercase = UCD_OTHERCASE(c)) != c)            zerofirstchar = firstchar;
4481              {            *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4482              *class_utf8data++ = XCL_SINGLE;  #ifdef SUPPORT_UTF
4483              class_utf8data += _pcre_ord2utf8(othercase, class_utf8data);            if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4484              }              code += PRIV(ord2utf)(c, code);
4485              else
4486    #endif
4487                *code++ = c;
4488              goto NOT_CHAR;
4489            }            }
 #endif  /* SUPPORT_UCP */  
4490    
4491          }          /* For a single, positive character, get the value into mcbuffer, and
4492        else          then we can handle this with the normal one-character code. */
 #endif  /* SUPPORT_UTF8 */  
4493    
4494        /* Handle a single-byte character */  #ifdef SUPPORT_UTF
4495          {          if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4496          classbits[c/8] |= (1 << (c&7));            mclength = PRIV(ord2utf)(c, mcbuffer);
4497          if ((options & PCRE_CASELESS) != 0)          else
4498    #endif
4499            {            {
4500            c = cd->fcc[c];   /* flip case */            mcbuffer[0] = c;
4501            classbits[c/8] |= (1 << (c&7));            mclength = 1;
4502            }            }
4503          class_charcount++;          goto ONE_CHAR;
4504          class_lastchar = c;          }       /* End of 1-char optimization */
4505          }  
4506          /* There is more than one character in the class, or an XCLASS item
4507          has been generated. Add this character to the class. */
4508    
4509          class_has_8bitchar +=
4510            add_to_class(classbits, &class_uchardata, options, cd, c, c);
4511        }        }
4512    
4513      /* Loop until ']' reached. This "while" is the end of the "do" far above.      /* Loop until ']' reached. This "while" is the end of the "do" far above.
# Line 3895  for (;; ptr++) Line 4527  for (;; ptr++)
4527        goto FAILED;        goto FAILED;
4528        }        }
4529    
4530      /* If class_charcount is 1, we saw precisely one character whose value is      /* We will need an XCLASS if data has been placed in class_uchardata. In
4531      less than 256. As long as there were no characters >= 128 and there was no      the second phase this is a sufficient test. However, in the pre-compile
4532      use of \p or \P, in other words, no use of any XCLASS features, we can      phase, class_uchardata gets emptied to prevent workspace overflow, so it
4533      optimize.      only if the very last character in the class needs XCLASS will it contain
4534        anything at this point. For this reason, xclass gets set TRUE above when
4535      In UTF-8 mode, we can optimize the negative case only if there were no      uchar_classdata is emptied, and that's why this code is the way it is here
4536      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR      instead of just doing a test on class_uchardata below. */
4537      operate on single-bytes only. This is an historical hangover. Maybe one day  
4538      we can tidy these opcodes to handle multi-byte characters.  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4539        if (class_uchardata > class_uchardata_base) xclass = TRUE;
4540      The optimization throws away the bit map. We turn the item into a  #endif
4541      1-character OP_CHAR[NC] if it's positive, or OP_NOT if it's negative. Note  
4542      that OP_NOT does not support multibyte characters. In the positive case, it      /* If this is the first thing in the branch, there can be no first char
4543      can cause firstbyte to be set. Otherwise, there can be no first char if      setting, whatever the repeat count. Any reqchar setting must remain
4544      this item is first, whatever repeat count may follow. In the case of      unchanged after any kind of repeat. */
4545      reqbyte, save the previous value for reinstating. */  
4546        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4547  #ifdef SUPPORT_UTF8      zerofirstchar = firstchar;
4548      if (class_charcount == 1 && !class_utf8 &&      zeroreqchar = reqchar;
       (!utf8 || !negate_class || class_lastchar < 128))  
 #else  
     if (class_charcount == 1)  
 #endif  
       {  
       zeroreqbyte = reqbyte;  
   
       /* The OP_NOT opcode works on one-byte characters only. */  
   
       if (negate_class)  
         {  
         if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;  
         zerofirstbyte = firstbyte;  
         *code++ = OP_NOT;  
         *code++ = class_lastchar;  
         break;  
         }  
   
       /* For a single, positive character, get the value into mcbuffer, and  
       then we can handle this with the normal one-character code. */  
   
 #ifdef SUPPORT_UTF8  
       if (utf8 && class_lastchar > 127)  
         mclength = _pcre_ord2utf8(class_lastchar, mcbuffer);  
       else  
 #endif  
         {  
         mcbuffer[0] = class_lastchar;  
         mclength = 1;  
         }  
       goto ONE_CHAR;  
       }       /* End of 1-char optimization */  
   
     /* The general case - not the one-char optimization. If this is the first  
     thing in the branch, there can be no first char setting, whatever the  
     repeat count. Any reqbyte setting must remain unchanged after any kind of  
     repeat. */  
   
     if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;  
     zerofirstbyte = firstbyte;  
     zeroreqbyte = reqbyte;  
4549    
4550      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
4551      extended class, with its own opcode, unless there was a negated special      extended class, with its own opcode, unless there was a negated special
# Line 3964  for (;; ptr++) Line 4555  for (;; ptr++)
4555      be listed) there are no characters < 256, we can omit the bitmap in the      be listed) there are no characters < 256, we can omit the bitmap in the
4556      actual compiled code. */      actual compiled code. */
4557    
4558  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4559      if (class_utf8 && (!should_flip_negation || (options & PCRE_UCP) != 0))      if (xclass && (!should_flip_negation || (options & PCRE_UCP) != 0))
4560    #elif !defined COMPILE_PCRE8
4561        if (xclass && !should_flip_negation)
4562    #endif
4563    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4564        {        {
4565        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */        *class_uchardata++ = XCL_END;    /* Marks the end of extra data */
4566        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
4567        code += LINK_SIZE;        code += LINK_SIZE;
4568        *code = negate_class? XCL_NOT : 0;        *code = negate_class? XCL_NOT:0;
4569    
4570        /* 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;
4571        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. */
4572    
4573        if (class_charcount > 0)        if (class_has_8bitchar > 0)
4574          {          {
4575          *code++ |= XCL_MAP;          *code++ |= XCL_MAP;
4576          memmove(code + 32, code, class_utf8data - code);          memmove(code + (32 / sizeof(pcre_uchar)), code,
4577              IN_UCHARS(class_uchardata - code));
4578          memcpy(code, classbits, 32);          memcpy(code, classbits, 32);
4579          code = class_utf8data + 32;          code = class_uchardata + (32 / sizeof(pcre_uchar));
4580          }          }
4581        else code = class_utf8data;        else code = class_uchardata;
4582    
4583        /* Now fill in the complete length of the item */        /* Now fill in the complete length of the item */
4584    
4585        PUT(previous, 1, code - previous);        PUT(previous, 1, (int)(code - previous));
4586        break;   /* End of class handling */        break;   /* End of class handling */
4587        }        }
4588  #endif  #endif
# Line 3998  for (;; ptr++) Line 4594  for (;; ptr++)
4594      negating it if necessary. */      negating it if necessary. */
4595    
4596      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
4597      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  
4598        {        {
4599          if (negate_class)
4600            for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
4601        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
4602        }        }
4603      code += 32;      code += 32 / sizeof(pcre_uchar);
4604        NOT_CHAR:
4605      break;      break;
4606    
4607    
# Line 4044  for (;; ptr++) Line 4638  for (;; ptr++)
4638    
4639      if (repeat_min == 0)      if (repeat_min == 0)
4640        {        {
4641        firstbyte = zerofirstbyte;    /* Adjust for zero repeat */        firstchar = zerofirstchar;    /* Adjust for zero repeat */
4642        reqbyte = zeroreqbyte;        /* Ditto */        reqchar = zeroreqchar;        /* Ditto */
4643        }        }
4644    
4645      /* Remember whether this is a variable length repeat */      /* Remember whether this is a variable length repeat */
# Line 4055  for (;; ptr++) Line 4649  for (;; ptr++)
4649      op_type = 0;                    /* Default single-char op codes */      op_type = 0;                    /* Default single-char op codes */
4650      possessive_quantifier = FALSE;  /* Default not possessive quantifier */      possessive_quantifier = FALSE;  /* Default not possessive quantifier */
4651    
4652      /* 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
4653      for an inserted OP_ONCE for the additional '+' extension. */      insert something before it. */
4654    
4655      tempcode = previous;      tempcode = previous;
4656    
# Line 4079  for (;; ptr++) Line 4673  for (;; ptr++)
4673        }        }
4674      else repeat_type = greedy_default;      else repeat_type = greedy_default;
4675    
4676      /* If previous was a character match, abolish the item and generate a      /* If previous was a recursion call, wrap it in atomic brackets so that
4677      repeat item instead. If a char item has a minumum of more than one, ensure      previous becomes the atomic group. All recursions were so wrapped in the
4678      that it is set in reqbyte - it might not be if a sequence such as x{3} is      past, but it no longer happens for non-repeated recursions. In fact, the
4679      the first thing in a branch because the x will have gone into firstbyte      repeated ones could be re-implemented independently so as not to need this,
4680      instead.  */      but for the moment we rely on the code for repeating groups. */
4681    
4682        if (*previous == OP_RECURSE)
4683          {
4684          memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
4685          *previous = OP_ONCE;
4686          PUT(previous, 1, 2 + 2*LINK_SIZE);
4687          previous[2 + 2*LINK_SIZE] = OP_KET;
4688          PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4689          code += 2 + 2 * LINK_SIZE;
4690          length_prevgroup = 3 + 3*LINK_SIZE;
4691    
4692          /* When actually compiling, we need to check whether this was a forward
4693          reference, and if so, adjust the offset. */
4694    
4695          if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4696            {
4697         &