/[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 773 by ph10, Wed Nov 30 18:10:27 2011 UTC revision 1376 by ph10, Sat Oct 12 18:02:11 2013 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-2011 University of Cambridge             Copyright (c) 1997-2013 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|32)_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, int, int,
88        pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,
89        compile_data *, int *);
90    
91    
92    
93  /*************************************************  /*************************************************
94  *      Code parameters and static tables         *  *      Code parameters and static tables         *
# Line 88  so this number is very generous. Line 104  so this number is very generous.
104  The same workspace is used during the second, actual compile phase for  The same workspace is used during the second, actual compile phase for
105  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
106  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
107  is 4 there is plenty of room for most patterns. However, the memory can get  is 4 there is plenty of room for most patterns. However, the memory can get
108  filled up by repetitions of forward references, for example patterns like  filled up by repetitions of forward references, for example patterns like
109  /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so  /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
110  that the workspace is expanded using malloc() in this situation. The value  that the workspace is expanded using malloc() in this situation. The value
111  below is therefore a minimum, and we put a maximum on it for safety. The  below is therefore a minimum, and we put a maximum on it for safety. The
112  minimum is now also defined in terms of LINK_SIZE so that the use of malloc()  minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
113  kicks in at the same number of forward references in all cases. */  kicks in at the same number of forward references in all cases. */
114    
115  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
117    
118    /* This value determines the size of the initial vector that is used for
119    remembering named groups during the pre-compile. It is allocated on the stack,
120    but if it is too small, it is expanded using malloc(), in a similar way to the
121    workspace. The value is the number of slots in the list. */
122    
123    #define NAMED_GROUP_LIST_SIZE  20
124    
125  /* 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
126  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. */
127    
128  #define WORK_SIZE_SAFETY_MARGIN (100)  #define WORK_SIZE_SAFETY_MARGIN (100)
129    
130    /* Private flags added to firstchar and reqchar. */
131    
132    #define REQ_CASELESS    (1 << 0)        /* Indicates caselessness */
133    #define REQ_VARY        (1 << 1)        /* Reqchar followed non-literal item */
134    /* Negative values for the firstchar and reqchar flags */
135    #define REQ_UNSET       (-2)
136    #define REQ_NONE        (-1)
137    
138    /* Repeated character flags. */
139    
140    #define UTF_LENGTH     0x10000000l      /* The char contains its length. */
141    
142  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
143  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 238  static const char posix_names[] = Line 272  static const char posix_names[] =
272    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
273    STRING_word0  STRING_xdigit;    STRING_word0  STRING_xdigit;
274    
275  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
276    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 };
277    
278  /* 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 273  substitutes must be in the order of the Line 307  substitutes must be in the order of the
307  both positive and negative cases. NULL means no substitute. */  both positive and negative cases. NULL means no substitute. */
308    
309  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
310  static const uschar *substitutes[] = {  static const pcre_uchar string_PNd[]  = {
311    (uschar *)"\\P{Nd}",    /* \D */    CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
312    (uschar *)"\\p{Nd}",    /* \d */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
313    (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */  static const pcre_uchar string_pNd[]  = {
314    (uschar *)"\\p{Xsp}",   /* \s */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
315    (uschar *)"\\P{Xwd}",   /* \W */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
316    (uschar *)"\\p{Xwd}"    /* \w */  static const pcre_uchar string_PXsp[] = {
317      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
318      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
319    static const pcre_uchar string_pXsp[] = {
320      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
321      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
322    static const pcre_uchar string_PXwd[] = {
323      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
324      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
325    static const pcre_uchar string_pXwd[] = {
326      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
327      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
328    
329    static const pcre_uchar *substitutes[] = {
330      string_PNd,           /* \D */
331      string_pNd,           /* \d */
332      string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
333      string_pXsp,          /* \s */
334      string_PXwd,          /* \W */
335      string_pXwd           /* \w */
336  };  };
337    
338  static const uschar *posix_substitutes[] = {  static const pcre_uchar string_pL[] =   {
339    (uschar *)"\\p{L}",     /* alpha */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
340    (uschar *)"\\p{Ll}",    /* lower */    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
341    (uschar *)"\\p{Lu}",    /* upper */  static const pcre_uchar string_pLl[] =  {
342    (uschar *)"\\p{Xan}",   /* alnum */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
343    NULL,                   /* ascii */    CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
344    (uschar *)"\\h",        /* blank */  static const pcre_uchar string_pLu[] =  {
345    NULL,                   /* cntrl */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
346    (uschar *)"\\p{Nd}",    /* digit */    CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
347    NULL,                   /* graph */  static const pcre_uchar string_pXan[] = {
348    NULL,                   /* print */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
349    NULL,                   /* punct */    CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
350    (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */  static const pcre_uchar string_h[] =    {
351    (uschar *)"\\p{Xwd}",   /* word */    CHAR_BACKSLASH, CHAR_h, '\0' };
352    NULL,                   /* xdigit */  static const pcre_uchar string_pXps[] = {
353      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
354      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
355    static const pcre_uchar string_PL[] =   {
356      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
357      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
358    static const pcre_uchar string_PLl[] =  {
359      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
360      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
361    static const pcre_uchar string_PLu[] =  {
362      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
363      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
364    static const pcre_uchar string_PXan[] = {
365      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
366      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
367    static const pcre_uchar string_H[] =    {
368      CHAR_BACKSLASH, CHAR_H, '\0' };
369    static const pcre_uchar string_PXps[] = {
370      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
371      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
372    
373    static const pcre_uchar *posix_substitutes[] = {
374      string_pL,            /* alpha */
375      string_pLl,           /* lower */
376      string_pLu,           /* upper */
377      string_pXan,          /* alnum */
378      NULL,                 /* ascii */
379      string_h,             /* blank */
380      NULL,                 /* cntrl */
381      string_pNd,           /* digit */
382      NULL,                 /* graph */
383      NULL,                 /* print */
384      NULL,                 /* punct */
385      string_pXps,          /* space */    /* NOTE: Xps is POSIX space */
386      string_pXwd,          /* word */
387      NULL,                 /* xdigit */
388    /* Negated cases */    /* Negated cases */
389    (uschar *)"\\P{L}",     /* ^alpha */    string_PL,            /* ^alpha */
390    (uschar *)"\\P{Ll}",    /* ^lower */    string_PLl,           /* ^lower */
391    (uschar *)"\\P{Lu}",    /* ^upper */    string_PLu,           /* ^upper */
392    (uschar *)"\\P{Xan}",   /* ^alnum */    string_PXan,          /* ^alnum */
393    NULL,                   /* ^ascii */    NULL,                 /* ^ascii */
394    (uschar *)"\\H",        /* ^blank */    string_H,             /* ^blank */
395    NULL,                   /* ^cntrl */    NULL,                 /* ^cntrl */
396    (uschar *)"\\P{Nd}",    /* ^digit */    string_PNd,           /* ^digit */
397    NULL,                   /* ^graph */    NULL,                 /* ^graph */
398    NULL,                   /* ^print */    NULL,                 /* ^print */
399    NULL,                   /* ^punct */    NULL,                 /* ^punct */
400    (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */    string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
401    (uschar *)"\\P{Xwd}",   /* ^word */    string_PXwd,          /* ^word */
402    NULL                    /* ^xdigit */    NULL                  /* ^xdigit */
403  };  };
404  #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
405  #endif  #endif
406    
407  #define STRING(a)  # a  #define STRING(a)  # a
# Line 372  static const char error_texts[] = Line 460  static const char error_texts[] =
460    /* 30 */    /* 30 */
461    "unknown POSIX class name\0"    "unknown POSIX class name\0"
462    "POSIX collating elements are not supported\0"    "POSIX collating elements are not supported\0"
463    "this version of PCRE is not compiled with PCRE_UTF8 support\0"    "this version of PCRE is compiled without UTF support\0"
464    "spare error\0"  /** DEAD **/    "spare error\0"  /** DEAD **/
465    "character value in \\x{...} sequence is too large\0"    "character value in \\x{} or \\o{} is too large\0"
466    /* 35 */    /* 35 */
467    "invalid condition (?(0)\0"    "invalid condition (?(0)\0"
468    "\\C not allowed in lookbehind assertion\0"    "\\C not allowed in lookbehind assertion\0"
# Line 395  static const char error_texts[] = Line 483  static const char error_texts[] =
483    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
484    /* 50 */    /* 50 */
485    "repeated subpattern is too long\0"    /** DEAD **/    "repeated subpattern is too long\0"    /** DEAD **/
486    "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"
487    "internal error: overran compiling workspace\0"    "internal error: overran compiling workspace\0"
488    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
489    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
# Line 406  static const char error_texts[] = Line 494  static const char error_texts[] =
494    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
495    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
496    /* 60 */    /* 60 */
497    "(*VERB) not recognized\0"    "(*VERB) not recognized or malformed\0"
498    "number is too big\0"    "number is too big\0"
499    "subpattern name expected\0"    "subpattern name expected\0"
500    "digit expected after (?+\0"    "digit expected after (?+\0"
# Line 414  static const char error_texts[] = Line 502  static const char error_texts[] =
502    /* 65 */    /* 65 */
503    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
504    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
505    "this version of PCRE is not compiled with PCRE_UCP support\0"    "this version of PCRE is not compiled with Unicode property support\0"
506    "\\c must be followed by an ASCII character\0"    "\\c must be followed by an ASCII character\0"
507    "\\k is not followed by a braced, angle-bracketed, or quoted name\0"    "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
508    /* 70 */    /* 70 */
509    "internal error: unknown opcode in find_fixedlength()\0"    "internal error: unknown opcode in find_fixedlength()\0"
510    "\\N is not supported in a class\0"    "\\N is not supported in a class\0"
511    "too many forward references\0"    "too many forward references\0"
512      "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
513      "invalid UTF-16 string\0"
514      /* 75 */
515      "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
516      "character value in \\u.... sequence is too large\0"
517      "invalid UTF-32 string\0"
518      "setting UTF is disabled by the application\0"
519      "non-hex character in \\x{} (closing brace missing?)\0"
520      /* 80 */
521      "non-octal character in \\o{} (closing brace missing?)\0"
522      "missing opening brace after \\o\0"
523    ;    ;
524    
525  /* 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 439  For convenience, we use the same bit def Line 538  For convenience, we use the same bit def
538    
539  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
540    
541    /* Using a simple comparison for decimal numbers rather than a memory read
542    is much faster, and the resulting code is simpler (the compiler turns it
543    into a subtraction and unsigned comparison). */
544    
545    #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
546    
547  #ifndef EBCDIC  #ifndef EBCDIC
548    
549  /* 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
550  UTF-8 mode. */  UTF-8 mode. */
551    
552  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
553    {    {
554    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
555    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */
# Line 483  static const unsigned char digitab[] = Line 588  static const unsigned char digitab[] =
588    
589  /* 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. */
590    
591  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
592    {    {
593    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
594    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */
# Line 518  static const unsigned char digitab[] = Line 623  static const unsigned char digitab[] =
623    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */
624    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */
625    
626  static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */  static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
627    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */
628    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */
629    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */
# Line 554  static const unsigned char ebcdic_charta Line 659  static const unsigned char ebcdic_charta
659  #endif  #endif
660    
661    
662  /* Definition to allow mutual recursion */  /* This table is used to check whether auto-possessification is possible
663    between adjacent character-type opcodes. The left-hand (repeated) opcode is
664    used to select the row, and the right-hand opcode is use to select the column.
665    A value of 1 means that auto-possessification is OK. For example, the second
666    value in the first row means that \D+\d can be turned into \D++\d.
667    
668    The Unicode property types (\P and \p) have to be present to fill out the table
669    because of what their opcode values are, but the table values should always be
670    zero because property types are handled separately in the code. The last four
671    columns apply to items that cannot be repeated, so there is no need to have
672    rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
673    *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
674    
675    #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
676    #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
677    
678    static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
679    /* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
680      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
681      { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
682      { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
683      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
684      { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
685      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
686      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
687      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
688      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
689      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
690      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
691      { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
692      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
693      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
694      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
695      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
696      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
697    };
698    
699    
700  static BOOL  /* This table is used to check whether auto-possessification is possible
701    compile_regex(int, uschar **, const uschar **, int *, BOOL, BOOL, int, int,  between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
702      int *, int *, branch_chain *, compile_data *, int *);  left-hand (repeated) opcode is used to select the row, and the right-hand
703    opcode is used to select the column. The values are as follows:
704    
705      0   Always return FALSE (never auto-possessify)
706      1   Character groups are distinct (possessify if both are OP_PROP)
707      2   Check character categories in the same group (general or particular)
708      3   TRUE if the two opcodes are not the same (PROP vs NOTPROP)
709    
710      4   Check left general category vs right particular category
711      5   Check right general category vs left particular category
712    
713      6   Left alphanum vs right general category
714      7   Left space vs right general category
715      8   Left word vs right general category
716    
717      9   Right alphanum vs left general category
718     10   Right space vs left general category
719     11   Right word vs left general category
720    
721     12   Left alphanum vs right particular category
722     13   Left space vs right particular category
723     14   Left word vs right particular category
724    
725     15   Right alphanum vs left particular category
726     16   Right space vs left particular category
727     17   Right word vs left particular category
728    */
729    
730    static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
731    /* ANY LAMP GC  PC  SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
732      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_ANY */
733      { 0,  3,  0,  0,  0,    3,    1,      1,   0,    0,   0 },  /* PT_LAMP */
734      { 0,  0,  2,  4,  0,    9,   10,     10,  11,    0,   0 },  /* PT_GC */
735      { 0,  0,  5,  2,  0,   15,   16,     16,  17,    0,   0 },  /* PT_PC */
736      { 0,  0,  0,  0,  2,    0,    0,      0,   0,    0,   0 },  /* PT_SC */
737      { 0,  3,  6, 12,  0,    3,    1,      1,   0,    0,   0 },  /* PT_ALNUM */
738      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_SPACE */
739      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_PXSPACE */
740      { 0,  0,  8, 14,  0,    0,    1,      1,   3,    0,   0 },  /* PT_WORD */
741      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_CLIST */
742      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   3 }   /* PT_UCNC */
743    };
744    
745    /* This table is used to check whether auto-possessification is possible
746    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
747    specifies a general category and the other specifies a particular category. The
748    row is selected by the general category and the column by the particular
749    category. The value is 1 if the particular category is not part of the general
750    category. */
751    
752    static const pcre_uint8 catposstab[7][30] = {
753    /* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
754      { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* C */
755      { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* L */
756      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* M */
757      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* N */
758      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },  /* P */
759      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 },  /* S */
760      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 }   /* Z */
761    };
762    
763    /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
764    a general or particular category. The properties in each row are those
765    that apply to the character set in question. Duplication means that a little
766    unnecessary work is done when checking, but this keeps things much simpler
767    because they can all use the same code. For more details see the comment where
768    this table is used.
769    
770    Note: SPACE and PXSPACE used to be different because Perl excluded VT from
771    "space", but from Perl 5.18 it's included, so both categories are treated the
772    same here. */
773    
774    static const pcre_uint8 posspropstab[3][4] = {
775      { ucp_L, ucp_N, ucp_N, ucp_Nl },  /* ALNUM, 3rd and 4th values redundant */
776      { ucp_Z, ucp_Z, ucp_C, ucp_Cc },  /* SPACE and PXSPACE, 2nd value redundant */
777      { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
778    };
779    
780    
781    
# Line 581  find_error_text(int n) Line 798  find_error_text(int n)
798  const char *s = error_texts;  const char *s = error_texts;
799  for (; n > 0; n--)  for (; n > 0; n--)
800    {    {
801    while (*s++ != 0) {};    while (*s++ != CHAR_NULL) {};
802    if (*s == 0) return "Error text not found (please report)";    if (*s == CHAR_NULL) return "Error text not found (please report)";
803    }    }
804  return s;  return s;
805  }  }
806    
807    
808    
809  /*************************************************  /*************************************************
810  *           Expand the workspace                 *  *           Expand the workspace                 *
811  *************************************************/  *************************************************/
812    
813  /* This function is called during the second compiling phase, if the number of  /* This function is called during the second compiling phase, if the number of
814  forward references fills the existing workspace, which is originally a block on  forward references fills the existing workspace, which is originally a block on
815  the stack. A larger block is obtained from malloc() unless the ultimate limit  the stack. A larger block is obtained from malloc() unless the ultimate limit
816  has been reached or the increase will be rather small.  has been reached or the increase will be rather small.
817    
818  Argument: pointer to the compile data block  Argument: pointer to the compile data block
# Line 604  Returns:  0 if all went well, else an er Line 822  Returns:  0 if all went well, else an er
822  static int  static int
823  expand_workspace(compile_data *cd)  expand_workspace(compile_data *cd)
824  {  {
825  uschar *newspace;  pcre_uchar *newspace;
826  int newsize = cd->workspace_size * 2;  int newsize = cd->workspace_size * 2;
827    
828  if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;  if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
# Line 612  if (cd->workspace_size >= COMPILE_WORK_S Line 830  if (cd->workspace_size >= COMPILE_WORK_S
830      newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)      newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
831   return ERR72;   return ERR72;
832    
833  newspace = (pcre_malloc)(newsize);  newspace = (PUBL(malloc))(IN_UCHARS(newsize));
834  if (newspace == NULL) return ERR21;  if (newspace == NULL) return ERR21;
835    memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
836  memcpy(newspace, cd->start_workspace, cd->workspace_size);  cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
837  cd->hwm = (uschar *)newspace + (cd->hwm - cd->start_workspace);  if (cd->workspace_size > COMPILE_WORK_SIZE)
838  if (cd->workspace_size > COMPILE_WORK_SIZE)    (PUBL(free))((void *)cd->start_workspace);
   (pcre_free)((void *)cd->start_workspace);  
839  cd->start_workspace = newspace;  cd->start_workspace = newspace;
840  cd->workspace_size = newsize;  cd->workspace_size = newsize;
841  return 0;  return 0;
# Line 642  Returns:    TRUE or FALSE Line 859  Returns:    TRUE or FALSE
859  */  */
860    
861  static BOOL  static BOOL
862  is_counted_repeat(const uschar *p)  is_counted_repeat(const pcre_uchar *p)
863  {  {
864  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
865  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
866    while (IS_DIGIT(*p)) p++;
867  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
868    
869  if (*p++ != CHAR_COMMA) return FALSE;  if (*p++ != CHAR_COMMA) return FALSE;
870  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
871    
872  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
873  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
874    while (IS_DIGIT(*p)) p++;
875    
876  return (*p == CHAR_RIGHT_CURLY_BRACKET);  return (*p == CHAR_RIGHT_CURLY_BRACKET);
877  }  }
# Line 664  return (*p == CHAR_RIGHT_CURLY_BRACKET); Line 883  return (*p == CHAR_RIGHT_CURLY_BRACKET);
883  *************************************************/  *************************************************/
884    
885  /* This function is called when a \ has been encountered. It either returns a  /* This function is called when a \ has been encountered. It either returns a
886  positive value for a simple escape such as \n, or a negative value which  positive value for a simple escape such as \n, or 0 for a data character which
887  encodes one of the more complicated things such as \d. A backreference to group  will be placed in chptr. A backreference to group n is returned as negative n.
888  n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When  When UTF-8 is enabled, a positive value greater than 255 may be returned in
889  UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,  chptr. On entry, ptr is pointing at the \. On exit, it is on the final
890  ptr is pointing at the \. On exit, it is on the final character of the escape  character of the escape sequence.
 sequence.  
891    
892  Arguments:  Arguments:
893    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
894      chptr          points to a returned data character
895    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
896    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
897    options        the options bits    options        the options bits
898    isclass        TRUE if inside a character class    isclass        TRUE if inside a character class
899    
900  Returns:         zero or positive => a data character  Returns:         zero => a data character
901                   negative => a special escape sequence                   positive => a special escape sequence
902                     negative => a back reference
903                   on error, errorcodeptr is set                   on error, errorcodeptr is set
904  */  */
905    
906  static int  static int
907  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
908    int options, BOOL isclass)    int bracount, int options, BOOL isclass)
909  {  {
910  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
911  const uschar *ptr = *ptrptr + 1;  BOOL utf = (options & PCRE_UTF8) != 0;
912  int c, i;  const pcre_uchar *ptr = *ptrptr + 1;
913    pcre_uint32 c;
914    int escape = 0;
915    int i;
916    
917  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
918  ptr--;                            /* Set pointer back to the last byte */  ptr--;                            /* Set pointer back to the last byte */
919    
920  /* If backslash is at the end of the pattern, it's an error. */  /* If backslash is at the end of the pattern, it's an error. */
921    
922  if (c == 0) *errorcodeptr = ERR1;  if (c == CHAR_NULL) *errorcodeptr = ERR1;
923    
924  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
925  in a table. A non-zero result is something that can be returned immediately.  in a table. A non-zero result is something that can be returned immediately.
926  Otherwise further processing may be required. */  Otherwise further processing may be required. */
927    
928  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
929  else if (c < CHAR_0 || c > CHAR_z) {}                     /* Not alphanumeric */  /* Not alphanumeric */
930  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if (c < CHAR_0 || c > CHAR_z) {}
931    else if ((i = escapes[c - CHAR_0]) != 0)
932      { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
933    
934  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
935  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */  /* Not alphanumeric */
936  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
937    else if ((i = escapes[c - 0x48]) != 0)  { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
938  #endif  #endif
939    
940  /* Escapes that need further processing, or are illegal. */  /* Escapes that need further processing, or are illegal. */
941    
942  else  else
943    {    {
944    const uschar *oldptr;    const pcre_uchar *oldptr;
945    BOOL braced, negated;    BOOL braced, negated, overflow;
946      int s;
947    
948    switch (c)    switch (c)
949      {      {
# Line 733  else Line 960  else
960        {        {
961        /* In JavaScript, \u must be followed by four hexadecimal numbers.        /* In JavaScript, \u must be followed by four hexadecimal numbers.
962        Otherwise it is a lowercase u letter. */        Otherwise it is a lowercase u letter. */
963        if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
964             && (digitab[ptr[3]] & ctype_xdigit) != 0 && (digitab[ptr[4]] & ctype_xdigit) != 0)          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
965            && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
966            && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
967          {          {
968          c = 0;          c = 0;
969          for (i = 0; i < 4; ++i)          for (i = 0; i < 4; ++i)
970            {            {
971            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
972  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
973            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
974            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
# Line 748  else Line 977  else
977            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
978  #endif  #endif
979            }            }
980    
981    #if defined COMPILE_PCRE8
982            if (c > (utf ? 0x10ffffU : 0xffU))
983    #elif defined COMPILE_PCRE16
984            if (c > (utf ? 0x10ffffU : 0xffffU))
985    #elif defined COMPILE_PCRE32
986            if (utf && c > 0x10ffffU)
987    #endif
988              {
989              *errorcodeptr = ERR76;
990              }
991            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
992          }          }
993        }        }
994      else      else
# Line 774  else Line 1015  else
1015      (3) For Oniguruma compatibility we also support \g followed by a name or a      (3) For Oniguruma compatibility we also support \g followed by a name or a
1016      number either in angle brackets or in single quotes. However, these are      number either in angle brackets or in single quotes. However, these are
1017      (possibly recursive) subroutine calls, _not_ backreferences. Just return      (possibly recursive) subroutine calls, _not_ backreferences. Just return
1018      the -ESC_g code (cf \k). */      the ESC_g code (cf \k). */
1019    
1020      case CHAR_g:      case CHAR_g:
1021      if (isclass) break;      if (isclass) break;
1022      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
1023        {        {
1024        c = -ESC_g;        escape = ESC_g;
1025        break;        break;
1026        }        }
1027    
# Line 788  else Line 1029  else
1029    
1030      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1031        {        {
1032        const uschar *p;        const pcre_uchar *p;
1033        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
1034          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1035        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1036          {          {
1037          c = -ESC_k;          escape = ESC_k;
1038          break;          break;
1039          }          }
1040        braced = TRUE;        braced = TRUE;
# Line 808  else Line 1049  else
1049        }        }
1050      else negated = FALSE;      else negated = FALSE;
1051    
1052      c = 0;      /* The integer range is limited by the machine's int representation. */
1053      while ((digitab[ptr[1]] & ctype_digit) != 0)      s = 0;
1054        c = c * 10 + *(++ptr) - CHAR_0;      overflow = FALSE;
1055        while (IS_DIGIT(ptr[1]))
1056      if (c < 0)   /* Integer overflow */        {
1057          if (s > INT_MAX / 10 - 1) /* Integer overflow */
1058            {
1059            overflow = TRUE;
1060            break;
1061            }
1062          s = s * 10 + (int)(*(++ptr) - CHAR_0);
1063          }
1064        if (overflow) /* Integer overflow */
1065        {        {
1066          while (IS_DIGIT(ptr[1]))
1067            ptr++;
1068        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
1069        break;        break;
1070        }        }
# Line 824  else Line 1075  else
1075        break;        break;
1076        }        }
1077    
1078      if (c == 0)      if (s == 0)
1079        {        {
1080        *errorcodeptr = ERR58;        *errorcodeptr = ERR58;
1081        break;        break;
# Line 832  else Line 1083  else
1083    
1084      if (negated)      if (negated)
1085        {        {
1086        if (c > bracount)        if (s > bracount)
1087          {          {
1088          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
1089          break;          break;
1090          }          }
1091        c = bracount - (c - 1);        s = bracount - (s - 1);
1092        }        }
1093    
1094      c = -(ESC_REF + c);      escape = -s;
1095      break;      break;
1096    
1097      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
1098      starting with one that is not zero is not straightforward. By experiment,      starting with one that is not zero is not straightforward. Perl has changed
1099      the way Perl works seems to be as follows:      over the years. Nowadays \g{} for backreferences and \o{} for octal are
1100        recommended to avoid the ambiguities in the old syntax.
1101    
1102      Outside a character class, the digits are read as a decimal number. If the      Outside a character class, the digits are read as a decimal number. If the
1103      number is less than 10, or if there are that many previous extracting      number is less than 8 (used to be 10), or if there are that many previous
1104      left brackets, then it is a back reference. Otherwise, up to three octal      extracting left brackets, then it is a back reference. Otherwise, up to
1105      digits are read to form an escaped byte. Thus \123 is likely to be octal      three octal digits are read to form an escaped byte. Thus \123 is likely to
1106      123 (cf \0123, which is octal 012 followed by the literal 3). If the octal      be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If
1107      value is greater than 377, the least significant 8 bits are taken. Inside a      the octal value is greater than 377, the least significant 8 bits are
1108      character class, \ followed by a digit is always an octal number. */      taken. \8 and \9 are treated as the literal characters 8 and 9.
1109    
1110        Inside a character class, \ followed by a digit is always either a literal
1111        8 or 9 or an octal number. */
1112    
1113      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
1114      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
# Line 861  else Line 1116  else
1116      if (!isclass)      if (!isclass)
1117        {        {
1118        oldptr = ptr;        oldptr = ptr;
1119        c -= CHAR_0;        /* The integer range is limited by the machine's int representation. */
1120        while ((digitab[ptr[1]] & ctype_digit) != 0)        s = (int)(c -CHAR_0);
1121          c = c * 10 + *(++ptr) - CHAR_0;        overflow = FALSE;
1122        if (c < 0)    /* Integer overflow */        while (IS_DIGIT(ptr[1]))
1123            {
1124            if (s > INT_MAX / 10 - 1) /* Integer overflow */
1125              {
1126              overflow = TRUE;
1127              break;
1128              }
1129            s = s * 10 + (int)(*(++ptr) - CHAR_0);
1130            }
1131          if (overflow) /* Integer overflow */
1132          {          {
1133            while (IS_DIGIT(ptr[1]))
1134              ptr++;
1135          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1136          break;          break;
1137          }          }
1138        if (c < 10 || c <= bracount)        if (s < 8 || s <= bracount)  /* Check for back reference */
1139          {          {
1140          c = -(ESC_REF + c);          escape = -s;
1141          break;          break;
1142          }          }
1143        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
1144        }        }
1145    
1146      /* Handle an octal number following \. If the first digit is 8 or 9, Perl      /* Handle a digit following \ when the number is not a back reference. If
1147      generates a binary zero byte and treats the digit as a following literal.      the first digit is 8 or 9, Perl used to generate a binary zero byte and
1148      Thus we have to pull back the pointer by one. */      then treat the digit as a following literal. At least by Perl 5.18 this
1149        changed so as not to insert the binary zero. */
1150    
1151      if ((c = *ptr) >= CHAR_8)      if ((c = *ptr) >= CHAR_8) break;
1152        {  
1153        ptr--;      /* Fall through with a digit less than 8 */
       c = 0;  
       break;  
       }  
1154    
1155      /* \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
1156      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
1157      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
1158      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,
1159      than 3 octal digits. */      but no more than 3 octal digits. */
1160    
1161      case CHAR_0:      case CHAR_0:
1162      c -= CHAR_0;      c -= CHAR_0;
1163      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1164          c = c * 8 + *(++ptr) - CHAR_0;          c = c * 8 + *(++ptr) - CHAR_0;
1165      if (!utf8 && c > 255) *errorcodeptr = ERR51;  #ifdef COMPILE_PCRE8
1166        if (!utf && c > 0xff) *errorcodeptr = ERR51;
1167    #endif
1168        break;
1169    
1170        /* \o is a relatively new Perl feature, supporting a more general way of
1171        specifying character codes in octal. The only supported form is \o{ddd}. */
1172    
1173        case CHAR_o:
1174        if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
1175          {
1176          ptr += 2;
1177          c = 0;
1178          overflow = FALSE;
1179          while (*ptr >= CHAR_0 && *ptr <= CHAR_7)
1180            {
1181            register pcre_uint32 cc = *ptr++;
1182            if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1183    #ifdef COMPILE_PCRE32
1184            if (c >= 0x20000000l) { overflow = TRUE; break; }
1185    #endif
1186            c = (c << 3) + cc - CHAR_0 ;
1187    #if defined COMPILE_PCRE8
1188            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1189    #elif defined COMPILE_PCRE16
1190            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1191    #elif defined COMPILE_PCRE32
1192            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1193    #endif
1194            }
1195          if (overflow)
1196            {
1197            while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++;
1198            *errorcodeptr = ERR34;
1199            }
1200          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1201            {
1202            if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1203            }
1204          else *errorcodeptr = ERR80;
1205          }
1206      break;      break;
1207    
1208      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
1209      than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is      numbers. Otherwise it is a lowercase x letter. */
     treated as a data character. */  
1210    
1211      case CHAR_x:      case CHAR_x:
1212      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1213        {        {
1214        /* In JavaScript, \x must be followed by two hexadecimal numbers.        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1215        Otherwise it is a lowercase x letter. */          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
       if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0)  
1216          {          {
1217          c = 0;          c = 0;
1218          for (i = 0; i < 2; ++i)          for (i = 0; i < 2; ++i)
1219            {            {
1220            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
1221  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1222            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1223            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
# Line 925  else Line 1227  else
1227  #endif  #endif
1228            }            }
1229          }          }
1230        break;        }    /* End JavaScript handling */
       }  
1231    
1232      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      /* Handle \x in Perl's style. \x{ddd} is a character number which can be
1233        {      greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
1234        const uschar *pt = ptr + 2;      digits. If not, { used to be treated as a data character. However, Perl
1235        int count = 0;      seems to read hex digits up to the first non-such, and ignore the rest, so
1236        that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
1237        now gives an error. */
1238    
1239        c = 0;      else
1240        while ((digitab[*pt] & ctype_xdigit) != 0)        {
1241          if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1242          {          {
1243          register int cc = *pt++;          ptr += 2;
1244          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          c = 0;
1245          count++;          overflow = FALSE;
1246            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)
1247              {
1248              register pcre_uint32 cc = *ptr++;
1249              if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1250    
1251    #ifdef COMPILE_PCRE32
1252              if (c >= 0x10000000l) { overflow = TRUE; break; }
1253    #endif
1254    
1255  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1256          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1257          c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1258  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1259          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 */
1260          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1261  #endif  #endif
         }  
1262    
1263        if (*pt == CHAR_RIGHT_CURLY_BRACKET)  #if defined COMPILE_PCRE8
1264          {            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1265          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;  #elif defined COMPILE_PCRE16
1266          ptr = pt;            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1267          break;  #elif defined COMPILE_PCRE32
1268          }            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1269    #endif
1270              }
1271    
1272        /* If the sequence of hex digits does not end with '}', then we don't          if (overflow)
1273        recognize this construct; fall through to the normal \x handling. */            {
1274        }            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++;
1275              *errorcodeptr = ERR34;
1276              }
1277    
1278            else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1279              {
1280              if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1281              }
1282    
1283      /* Read just a single-byte hex-defined char */          /* If the sequence of hex digits does not end with '}', give an error.
1284            We used just to recognize this construct and fall through to the normal
1285            \x handling, but nowadays Perl gives an error, which seems much more
1286            sensible, so we do too. */
1287    
1288      c = 0;          else *errorcodeptr = ERR79;
1289      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)          }   /* End of \x{} processing */
1290        {  
1291        int cc;                                  /* Some compilers don't like */        /* Read a single-byte hex-defined char (up to two hex digits after \x) */
1292        cc = *(++ptr);                           /* ++ in initializers */  
1293          else
1294            {
1295            c = 0;
1296            while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1297              {
1298              pcre_uint32 cc;                          /* Some compilers don't like */
1299              cc = *(++ptr);                           /* ++ in initializers */
1300  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1301        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
1302        c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1303  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1304        if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */            if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */
1305        c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1306  #endif  #endif
1307        }            }
1308            }     /* End of \xdd handling */
1309          }       /* End of Perl-style \x handling */
1310      break;      break;
1311    
1312      /* 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.
# Line 984  else Line 1316  else
1316    
1317      case CHAR_c:      case CHAR_c:
1318      c = *(++ptr);      c = *(++ptr);
1319      if (c == 0)      if (c == CHAR_NULL)
1320        {        {
1321        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1322        break;        break;
# Line 1024  else Line 1356  else
1356  newline". PCRE does not support \N{name}. However, it does support  newline". PCRE does not support \N{name}. However, it does support
1357  quantification such as \N{2,3}. */  quantification such as \N{2,3}. */
1358    
1359  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&  if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1360       !is_counted_repeat(ptr+2))       !is_counted_repeat(ptr+2))
1361    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
1362    
1363  /* If PCRE_UCP is set, we change the values for \d etc. */  /* If PCRE_UCP is set, we change the values for \d etc. */
1364    
1365  if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)  if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
1366    c -= (ESC_DU - ESC_D);    escape += (ESC_DU - ESC_D);
1367    
1368  /* Set the pointer to the final character before returning. */  /* Set the pointer to the final character before returning. */
1369    
1370  *ptrptr = ptr;  *ptrptr = ptr;
1371  return c;  *chptr = c;
1372    return escape;
1373  }  }
1374    
1375    
# Line 1054  escape sequence. Line 1387  escape sequence.
1387  Argument:  Argument:
1388    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
1389    negptr         points to a boolean that is set TRUE for negation else FALSE    negptr         points to a boolean that is set TRUE for negation else FALSE
1390    dptr           points to an int that is set to the detailed property value    ptypeptr       points to an unsigned int that is set to the type value
1391      pdataptr       points to an unsigned int that is set to the detailed property value
1392    errorcodeptr   points to the error code variable    errorcodeptr   points to the error code variable
1393    
1394  Returns:         type value from ucp_type_table, or -1 for an invalid type  Returns:         TRUE if the type value was found, or FALSE for an invalid type
1395  */  */
1396    
1397  static int  static BOOL
1398  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1399      unsigned int *pdataptr, int *errorcodeptr)
1400  {  {
1401  int c, i, bot, top;  pcre_uchar c;
1402  const uschar *ptr = *ptrptr;  int i, bot, top;
1403  char name[32];  const pcre_uchar *ptr = *ptrptr;
1404    pcre_uchar name[32];
1405    
1406  c = *(++ptr);  c = *(++ptr);
1407  if (c == 0) goto ERROR_RETURN;  if (c == CHAR_NULL) goto ERROR_RETURN;
1408    
1409  *negptr = FALSE;  *negptr = FALSE;
1410    
# Line 1082  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1418  if (c == CHAR_LEFT_CURLY_BRACKET)
1418      *negptr = TRUE;      *negptr = TRUE;
1419      ptr++;      ptr++;
1420      }      }
1421    for (i = 0; i < (int)sizeof(name) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1422      {      {
1423      c = *(++ptr);      c = *(++ptr);
1424      if (c == 0) goto ERROR_RETURN;      if (c == CHAR_NULL) goto ERROR_RETURN;
1425      if (c == CHAR_RIGHT_CURLY_BRACKET) break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1426      name[i] = c;      name[i] = c;
1427      }      }
# Line 1106  else Line 1442  else
1442  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1443    
1444  bot = 0;  bot = 0;
1445  top = _pcre_utt_size;  top = PRIV(utt_size);
1446    
1447  while (bot < top)  while (bot < top)
1448    {    {
1449      int r;
1450    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1451    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1452    if (c == 0)    if (r == 0)
1453      {      {
1454      *dptr = _pcre_utt[i].value;      *ptypeptr = PRIV(utt)[i].type;
1455      return _pcre_utt[i].type;      *pdataptr = PRIV(utt)[i].value;
1456        return TRUE;
1457      }      }
1458    if (c > 0) bot = i + 1; else top = i;    if (r > 0) bot = i + 1; else top = i;
1459    }    }
1460    
1461  *errorcodeptr = ERR47;  *errorcodeptr = ERR47;
1462  *ptrptr = ptr;  *ptrptr = ptr;
1463  return -1;  return FALSE;
1464    
1465  ERROR_RETURN:  ERROR_RETURN:
1466  *errorcodeptr = ERR46;  *errorcodeptr = ERR46;
1467  *ptrptr = ptr;  *ptrptr = ptr;
1468  return -1;  return FALSE;
1469  }  }
1470  #endif  #endif
1471    
1472    
1473    
   
1474  /*************************************************  /*************************************************
1475  *         Read repeat counts                     *  *         Read repeat counts                     *
1476  *************************************************/  *************************************************/
# Line 1153  Returns:         pointer to '}' on succe Line 1490  Returns:         pointer to '}' on succe
1490                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1491  */  */
1492    
1493  static const uschar *  static const pcre_uchar *
1494  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)
1495  {  {
1496  int min = 0;  int min = 0;
1497  int max = -1;  int max = -1;
# Line 1162  int max = -1; Line 1499  int max = -1;
1499  /* 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
1500  an integer overflow. */  an integer overflow. */
1501    
1502  while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);
1503  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1504    {    {
1505    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 1177  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1514  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1514    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1515      {      {
1516      max = 0;      max = 0;
1517      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);
1518      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1519        {        {
1520        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1202  return p; Line 1539  return p;
1539    
1540    
1541  /*************************************************  /*************************************************
 *  Subroutine for finding forward reference      *  
 *************************************************/  
   
 /* This recursive function is called only from find_parens() below. The  
 top-level call starts at the beginning of the pattern. All other calls must  
 start at a parenthesis. It scans along a pattern's text looking for capturing  
 subpatterns, and counting them. If it finds a named pattern that matches the  
 name it is given, it returns its number. Alternatively, if the name is NULL, it  
 returns when it reaches a given numbered subpattern. Recursion is used to keep  
 track of subpatterns that reset the capturing group numbers - the (?| feature.  
   
 This function was originally called only from the second pass, in which we know  
 that if (?< or (?' or (?P< is encountered, the name will be correctly  
 terminated because that is checked in the first pass. There is now one call to  
 this function in the first pass, to check for a recursive back reference by  
 name (so that we can make the whole group atomic). In this case, we need check  
 only up to the current position in the pattern, and that is still OK because  
 and previous occurrences will have been checked. To make this work, the test  
 for "end of pattern" is a check against cd->end_pattern in the main loop,  
 instead of looking for a binary zero. This means that the special first-pass  
 call can adjust cd->end_pattern temporarily. (Checks for binary zero while  
 processing items within the loop are OK, because afterwards the main loop will  
 terminate.)  
   
 Arguments:  
   ptrptr       address of the current character pointer (updated)  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf8         TRUE if we are in UTF-8 mode  
   count        pointer to the current capturing subpattern number (updated)  
   
 Returns:       the number of the named subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  
   BOOL xmode, BOOL utf8, int *count)  
 {  
 uschar *ptr = *ptrptr;  
 int start_count = *count;  
 int hwm_count = start_count;  
 BOOL dup_parens = FALSE;  
   
 /* If the first character is a parenthesis, check on the type of group we are  
 dealing with. The very first call may not start with a parenthesis. */  
   
 if (ptr[0] == CHAR_LEFT_PARENTHESIS)  
   {  
   /* Handle specials such as (*SKIP) or (*UTF8) etc. */  
   
   if (ptr[1] == CHAR_ASTERISK) ptr += 2;  
   
   /* Handle a normal, unnamed capturing parenthesis. */  
   
   else if (ptr[1] != CHAR_QUESTION_MARK)  
     {  
     *count += 1;  
     if (name == NULL && *count == lorn) return *count;  
     ptr++;  
     }  
   
   /* All cases now have (? at the start. Remember when we are in a group  
   where the parenthesis numbers are duplicated. */  
   
   else if (ptr[2] == CHAR_VERTICAL_LINE)  
     {  
     ptr += 3;  
     dup_parens = TRUE;  
     }  
   
   /* Handle comments; all characters are allowed until a ket is reached. */  
   
   else if (ptr[2] == CHAR_NUMBER_SIGN)  
     {  
     for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;  
     goto FAIL_EXIT;  
     }  
   
   /* Handle a condition. If it is an assertion, just carry on so that it  
   is processed as normal. If not, skip to the closing parenthesis of the  
   condition (there can't be any nested parens). */  
   
   else if (ptr[2] == CHAR_LEFT_PARENTHESIS)  
     {  
     ptr += 2;  
     if (ptr[1] != CHAR_QUESTION_MARK)  
       {  
       while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
       if (*ptr != 0) ptr++;  
       }  
     }  
   
   /* Start with (? but not a condition. */  
   
   else  
     {  
     ptr += 2;  
     if (*ptr == CHAR_P) ptr++;                      /* Allow optional P */  
   
     /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */  
   
     if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&  
         ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)  
       {  
       int term;  
       const uschar *thisname;  
       *count += 1;  
       if (name == NULL && *count == lorn) return *count;  
       term = *ptr++;  
       if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;  
       thisname = ptr;  
       while (*ptr != term) ptr++;  
       if (name != NULL && lorn == ptr - thisname &&  
           strncmp((const char *)name, (const char *)thisname, lorn) == 0)  
         return *count;  
       term++;  
       }  
     }  
   }  
   
 /* Past any initial parenthesis handling, scan for parentheses or vertical  
 bars. Stop if we get to cd->end_pattern. Note that this is important for the  
 first-pass call when this value is temporarily adjusted to stop at the current  
 position. So DO NOT change this to a test for binary zero. */  
   
 for (; ptr < cd->end_pattern; ptr++)  
   {  
   /* Skip over backslashed characters and also entire \Q...\E */  
   
   if (*ptr == CHAR_BACKSLASH)  
     {  
     if (*(++ptr) == 0) goto FAIL_EXIT;  
     if (*ptr == CHAR_Q) for (;;)  
       {  
       while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};  
       if (*ptr == 0) goto FAIL_EXIT;  
       if (*(++ptr) == CHAR_E) break;  
       }  
     continue;  
     }  
   
   /* Skip over character classes; this logic must be similar to the way they  
   are handled for real. If the first character is '^', skip it. Also, if the  
   first few characters (either before or after ^) are \Q\E or \E we skip them  
   too. This makes for compatibility with Perl. Note the use of STR macros to  
   encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */  
   
   if (*ptr == CHAR_LEFT_SQUARE_BRACKET)  
     {  
     BOOL negate_class = FALSE;  
     for (;;)  
       {  
       if (ptr[1] == CHAR_BACKSLASH)  
         {  
         if (ptr[2] == CHAR_E)  
           ptr+= 2;  
         else if (strncmp((const char *)ptr+2,  
                  STR_Q STR_BACKSLASH STR_E, 3) == 0)  
           ptr += 4;  
         else  
           break;  
         }  
       else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)  
         {  
         negate_class = TRUE;  
         ptr++;  
         }  
       else break;  
       }  
   
     /* If the next character is ']', it is a data character that must be  
     skipped, except in JavaScript compatibility mode. */  
   
     if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&  
         (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)  
       ptr++;  
   
     while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)  
       {  
       if (*ptr == 0) return -1;  
       if (*ptr == CHAR_BACKSLASH)  
         {  
         if (*(++ptr) == 0) goto FAIL_EXIT;  
         if (*ptr == CHAR_Q) for (;;)  
           {  
           while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};  
           if (*ptr == 0) goto FAIL_EXIT;  
           if (*(++ptr) == CHAR_E) break;  
           }  
         continue;  
         }  
       }  
     continue;  
     }  
   
   /* Skip comments in /x mode */  
   
   if (xmode && *ptr == CHAR_NUMBER_SIGN)  
     {  
     ptr++;  
     while (*ptr != 0)  
       {  
       if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }  
       ptr++;  
 #ifdef SUPPORT_UTF8  
       if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;  
 #endif  
       }  
     if (*ptr == 0) goto FAIL_EXIT;  
     continue;  
     }  
   
   /* Check for the special metacharacters */  
   
   if (*ptr == CHAR_LEFT_PARENTHESIS)  
     {  
     int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);  
     if (rc > 0) return rc;  
     if (*ptr == 0) goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_RIGHT_PARENTHESIS)  
     {  
     if (dup_parens && *count < hwm_count) *count = hwm_count;  
     goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)  
     {  
     if (*count > hwm_count) hwm_count = *count;  
     *count = start_count;  
     }  
   }  
   
 FAIL_EXIT:  
 *ptrptr = ptr;  
 return -1;  
 }  
   
   
   
   
 /*************************************************  
 *       Find forward referenced subpattern       *  
 *************************************************/  
   
 /* This function scans along a pattern's text looking for capturing  
 subpatterns, and counting them. If it finds a named pattern that matches the  
 name it is given, it returns its number. Alternatively, if the name is NULL, it  
 returns when it reaches a given numbered subpattern. This is used for forward  
 references to subpatterns. We used to be able to start this scan from the  
 current compiling point, using the current count value from cd->bracount, and  
 do it all in a single loop, but the addition of the possibility of duplicate  
 subpattern numbers means that we have to scan from the very start, in order to  
 take account of such duplicates, and to use a recursive function to keep track  
 of the different types of group.  
   
 Arguments:  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf8         TRUE if we are in UTF-8 mode  
   
 Returns:       the number of the found subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode,  
   BOOL utf8)  
 {  
 uschar *ptr = (uschar *)cd->start_pattern;  
 int count = 0;  
 int rc;  
   
 /* If the pattern does not start with an opening parenthesis, the first call  
 to find_parens_sub() will scan right to the end (if necessary). However, if it  
 does start with a parenthesis, find_parens_sub() will return when it hits the  
 matching closing parens. That is why we have to have a loop. */  
   
 for (;;)  
   {  
   rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);  
   if (rc > 0 || *ptr++ == 0) break;  
   }  
   
 return rc;  
 }  
   
   
   
   
 /*************************************************  
1542  *      Find first significant op code            *  *      Find first significant op code            *
1543  *************************************************/  *************************************************/
1544    
# Line 1513  Arguments: Line 1555  Arguments:
1555  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1556  */  */
1557    
1558  static const uschar*  static const pcre_uchar*
1559  first_significant_code(const uschar *code, BOOL skipassert)  first_significant_code(const pcre_uchar *code, BOOL skipassert)
1560  {  {
1561  for (;;)  for (;;)
1562    {    {
# Line 1525  for (;;) Line 1567  for (;;)
1567      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1568      if (!skipassert) return code;      if (!skipassert) return code;
1569      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
1570      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1571      break;      break;
1572    
1573      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
# Line 1535  for (;;) Line 1577  for (;;)
1577    
1578      case OP_CALLOUT:      case OP_CALLOUT:
1579      case OP_CREF:      case OP_CREF:
1580      case OP_NCREF:      case OP_DNCREF:
1581      case OP_RREF:      case OP_RREF:
1582      case OP_NRREF:      case OP_DNRREF:
1583      case OP_DEF:      case OP_DEF:
1584      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1585      break;      break;
1586    
1587      default:      default:
# Line 1551  for (;;) Line 1593  for (;;)
1593    
1594    
1595    
   
1596  /*************************************************  /*************************************************
1597  *        Find the fixed length of a branch       *  *        Find the fixed length of a branch       *
1598  *************************************************/  *************************************************/
# Line 1569  and doing the check at the end; a flag s Line 1610  and doing the check at the end; a flag s
1610    
1611  Arguments:  Arguments:
1612    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1613    utf8     TRUE in UTF-8 mode    utf      TRUE in UTF-8 / UTF-16 / UTF-32 mode
1614    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1615    cd       the "compile data" structure    cd       the "compile data" structure
1616    
# Line 1581  Returns:   the fixed length, Line 1622  Returns:   the fixed length,
1622  */  */
1623    
1624  static int  static int
1625  find_fixedlength(uschar *code, BOOL utf8, BOOL atend, compile_data *cd)  find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1626  {  {
1627  int length = -1;  int length = -1;
1628    
1629  register int branchlength = 0;  register int branchlength = 0;
1630  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1631    
1632  /* 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
1633  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1594  branch, check the length against that of Line 1635  branch, check the length against that of
1635  for (;;)  for (;;)
1636    {    {
1637    int d;    int d;
1638    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1639    register int op = *cc;    register pcre_uchar op = *cc;
1640    
1641    switch (op)    switch (op)
1642      {      {
1643      /* We only need to continue for OP_CBRA (normal capturing bracket) and      /* We only need to continue for OP_CBRA (normal capturing bracket) and
# Line 1608  for (;;) Line 1650  for (;;)
1650      case OP_ONCE:      case OP_ONCE:
1651      case OP_ONCE_NC:      case OP_ONCE_NC:
1652      case OP_COND:      case OP_COND:
1653      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), utf8, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd);
1654      if (d < 0) return d;      if (d < 0) return d;
1655      branchlength += d;      branchlength += d;
1656      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 1639  for (;;) Line 1681  for (;;)
1681    
1682      case OP_RECURSE:      case OP_RECURSE:
1683      if (!atend) return -3;      if (!atend) return -3;
1684      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1685      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1686      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1687      d = find_fixedlength(cs + 2, utf8, atend, cd);      d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd);
1688      if (d < 0) return d;      if (d < 0) return d;
1689      branchlength += d;      branchlength += d;
1690      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1655  for (;;) Line 1697  for (;;)
1697      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1698      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1699      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1700      /* Fall through */      cc += PRIV(OP_lengths)[*cc];
1701        break;
1702    
1703      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1704    
# Line 1663  for (;;) Line 1706  for (;;)
1706      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1707      case OP_SKIP_ARG:      case OP_SKIP_ARG:
1708      case OP_THEN_ARG:      case OP_THEN_ARG:
1709      cc += cc[1] + _pcre_OP_lengths[*cc];      cc += cc[1] + PRIV(OP_lengths)[*cc];
1710      break;      break;
1711    
1712      case OP_CALLOUT:      case OP_CALLOUT:
# Line 1673  for (;;) Line 1716  for (;;)
1716      case OP_COMMIT:      case OP_COMMIT:
1717      case OP_CREF:      case OP_CREF:
1718      case OP_DEF:      case OP_DEF:
1719        case OP_DNCREF:
1720        case OP_DNRREF:
1721      case OP_DOLL:      case OP_DOLL:
1722      case OP_DOLLM:      case OP_DOLLM:
1723      case OP_EOD:      case OP_EOD:
1724      case OP_EODN:      case OP_EODN:
1725      case OP_FAIL:      case OP_FAIL:
     case OP_NCREF:  
     case OP_NRREF:  
1726      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1727      case OP_PRUNE:      case OP_PRUNE:
1728      case OP_REVERSE:      case OP_REVERSE:
# Line 1690  for (;;) Line 1733  for (;;)
1733      case OP_SOM:      case OP_SOM:
1734      case OP_THEN:      case OP_THEN:
1735      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1736      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1737      break;      break;
1738    
1739      /* Handle literal characters */      /* Handle literal characters */
# Line 1701  for (;;) Line 1744  for (;;)
1744      case OP_NOTI:      case OP_NOTI:
1745      branchlength++;      branchlength++;
1746      cc += 2;      cc += 2;
1747  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1748      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1749  #endif  #endif
1750      break;      break;
1751    
# Line 1713  for (;;) Line 1756  for (;;)
1756      case OP_EXACTI:      case OP_EXACTI:
1757      case OP_NOTEXACT:      case OP_NOTEXACT:
1758      case OP_NOTEXACTI:      case OP_NOTEXACTI:
1759      branchlength += GET2(cc,1);      branchlength += (int)GET2(cc,1);
1760      cc += 4;      cc += 2 + IMM2_SIZE;
1761  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1762      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1763  #endif  #endif
1764      break;      break;
1765    
1766      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1767      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1768      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1769      cc += 4;        cc += 2;
1770        cc += 1 + IMM2_SIZE + 1;
1771      break;      break;
1772    
1773      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1749  for (;;) Line 1793  for (;;)
1793      cc++;      cc++;
1794      break;      break;
1795    
1796      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1797      otherwise \C is coded as OP_ALLANY. */      otherwise \C is coded as OP_ALLANY. */
1798    
1799      case OP_ANYBYTE:      case OP_ANYBYTE:
# Line 1757  for (;;) Line 1801  for (;;)
1801    
1802      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1803    
 #ifdef SUPPORT_UTF8  
     case OP_XCLASS:  
     cc += GET(cc, 1) - 33;  
     /* Fall through */  
 #endif  
   
1804      case OP_CLASS:      case OP_CLASS:
1805      case OP_NCLASS:      case OP_NCLASS:
1806      cc += 33;  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1807        case OP_XCLASS:
1808        /* The original code caused an unsigned overflow in 64 bit systems,
1809        so now we use a conditional statement. */
1810        if (op == OP_XCLASS)
1811          cc += GET(cc, 1);
1812        else
1813          cc += PRIV(OP_lengths)[OP_CLASS];
1814    #else
1815        cc += PRIV(OP_lengths)[OP_CLASS];
1816    #endif
1817    
1818      switch (*cc)      switch (*cc)
1819        {        {
# Line 1779  for (;;) Line 1827  for (;;)
1827    
1828        case OP_CRRANGE:        case OP_CRRANGE:
1829        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1830        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1831        branchlength += GET2(cc,1);        branchlength += (int)GET2(cc,1);
1832        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1833        break;        break;
1834    
1835        default:        default:
# Line 1847  for (;;) Line 1895  for (;;)
1895      case OP_QUERYI:      case OP_QUERYI:
1896      case OP_REF:      case OP_REF:
1897      case OP_REFI:      case OP_REFI:
1898        case OP_DNREF:
1899        case OP_DNREFI:
1900      case OP_SBRA:      case OP_SBRA:
1901      case OP_SBRAPOS:      case OP_SBRAPOS:
1902      case OP_SCBRA:      case OP_SCBRA:
# Line 1883  for (;;) Line 1933  for (;;)
1933    
1934    
1935    
   
1936  /*************************************************  /*************************************************
1937  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
1938  *************************************************/  *************************************************/
# Line 1896  length. Line 1945  length.
1945    
1946  Arguments:  Arguments:
1947    code        points to start of expression    code        points to start of expression
1948    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1949    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
1950    
1951  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
1952  */  */
1953    
1954  const uschar *  const pcre_uchar *
1955  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
1956  {  {
1957  for (;;)  for (;;)
1958    {    {
1959    register int c = *code;    register pcre_uchar c = *code;
1960    
1961    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1962    
# Line 1921  for (;;) Line 1970  for (;;)
1970    
1971    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
1972      {      {
1973      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
1974      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1975      }      }
1976    
1977    /* Handle capturing bracket */    /* Handle capturing bracket */
# Line 1930  for (;;) Line 1979  for (;;)
1979    else if (c == OP_CBRA || c == OP_SCBRA ||    else if (c == OP_CBRA || c == OP_SCBRA ||
1980             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
1981      {      {
1982      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1983      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1984      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1985      }      }
1986    
1987    /* 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 1960  for (;;) Line 2009  for (;;)
2009        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2010        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2011        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
2012        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2013            code += 2;
2014        break;        break;
2015    
2016        case OP_MARK:        case OP_MARK:
2017        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2018        case OP_SKIP_ARG:        case OP_SKIP_ARG:
2019          case OP_THEN_ARG:
2020        code += code[1];        code += code[1];
2021        break;        break;
2022          }
       case OP_THEN_ARG:  
       code += code[1];  
       break;  
       }  
2023    
2024      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2025    
2026      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2027    
2028    /* 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
2029    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
2030    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
2031    
2032  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2033      if (utf8) switch(c)      if (utf) switch(c)
2034        {        {
2035        case OP_CHAR:        case OP_CHAR:
2036        case OP_CHARI:        case OP_CHARI:
# Line 2013  for (;;) Line 2060  for (;;)
2060        case OP_MINQUERYI:        case OP_MINQUERYI:
2061        case OP_POSQUERY:        case OP_POSQUERY:
2062        case OP_POSQUERYI:        case OP_POSQUERYI:
2063        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2064        break;        break;
2065        }        }
2066  #else  #else
2067      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2068  #endif  #endif
2069      }      }
2070    }    }
# Line 2034  instance of OP_RECURSE. Line 2081  instance of OP_RECURSE.
2081    
2082  Arguments:  Arguments:
2083    code        points to start of expression    code        points to start of expression
2084    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2085    
2086  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
2087  */  */
2088    
2089  static const uschar *  static const pcre_uchar *
2090  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
2091  {  {
2092  for (;;)  for (;;)
2093    {    {
2094    register int c = *code;    register pcre_uchar c = *code;
2095    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2096    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
2097    
# Line 2079  for (;;) Line 2126  for (;;)
2126        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2127        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2128        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2129        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2130            code += 2;
2131        break;        break;
2132    
2133        case OP_MARK:        case OP_MARK:
2134        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2135        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
2136        case OP_THEN_ARG:        case OP_THEN_ARG:
2137        code += code[1];        code += code[1];
2138        break;        break;
# Line 2095  for (;;) Line 2140  for (;;)
2140    
2141      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2142    
2143      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2144    
2145      /* 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
2146      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
2147      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
2148    
2149  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2150      if (utf8) switch(c)      if (utf) switch(c)
2151        {        {
2152        case OP_CHAR:        case OP_CHAR:
2153        case OP_CHARI:        case OP_CHARI:
2154          case OP_NOT:
2155          case OP_NOTI:
2156        case OP_EXACT:        case OP_EXACT:
2157        case OP_EXACTI:        case OP_EXACTI:
2158          case OP_NOTEXACT:
2159          case OP_NOTEXACTI:
2160        case OP_UPTO:        case OP_UPTO:
2161        case OP_UPTOI:        case OP_UPTOI:
2162          case OP_NOTUPTO:
2163          case OP_NOTUPTOI:
2164        case OP_MINUPTO:        case OP_MINUPTO:
2165        case OP_MINUPTOI:        case OP_MINUPTOI:
2166          case OP_NOTMINUPTO:
2167          case OP_NOTMINUPTOI:
2168        case OP_POSUPTO:        case OP_POSUPTO:
2169        case OP_POSUPTOI:        case OP_POSUPTOI:
2170          case OP_NOTPOSUPTO:
2171          case OP_NOTPOSUPTOI:
2172        case OP_STAR:        case OP_STAR:
2173        case OP_STARI:        case OP_STARI:
2174          case OP_NOTSTAR:
2175          case OP_NOTSTARI:
2176        case OP_MINSTAR:        case OP_MINSTAR:
2177        case OP_MINSTARI:        case OP_MINSTARI:
2178          case OP_NOTMINSTAR:
2179          case OP_NOTMINSTARI:
2180        case OP_POSSTAR:        case OP_POSSTAR:
2181        case OP_POSSTARI:        case OP_POSSTARI:
2182          case OP_NOTPOSSTAR:
2183          case OP_NOTPOSSTARI:
2184        case OP_PLUS:        case OP_PLUS:
2185        case OP_PLUSI:        case OP_PLUSI:
2186          case OP_NOTPLUS:
2187          case OP_NOTPLUSI:
2188        case OP_MINPLUS:        case OP_MINPLUS:
2189        case OP_MINPLUSI:        case OP_MINPLUSI:
2190          case OP_NOTMINPLUS:
2191          case OP_NOTMINPLUSI:
2192        case OP_POSPLUS:        case OP_POSPLUS:
2193        case OP_POSPLUSI:        case OP_POSPLUSI:
2194          case OP_NOTPOSPLUS:
2195          case OP_NOTPOSPLUSI:
2196        case OP_QUERY:        case OP_QUERY:
2197        case OP_QUERYI:        case OP_QUERYI:
2198          case OP_NOTQUERY:
2199          case OP_NOTQUERYI:
2200        case OP_MINQUERY:        case OP_MINQUERY:
2201        case OP_MINQUERYI:        case OP_MINQUERYI:
2202          case OP_NOTMINQUERY:
2203          case OP_NOTMINQUERYI:
2204        case OP_POSQUERY:        case OP_POSQUERY:
2205        case OP_POSQUERYI:        case OP_POSQUERYI:
2206        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_NOTPOSQUERY:
2207          case OP_NOTPOSQUERYI:
2208          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2209        break;        break;
2210        }        }
2211  #else  #else
2212      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2213  #endif  #endif
2214      }      }
2215    }    }
# Line 2159  bracket whose current branch will alread Line 2232  bracket whose current branch will alread
2232  Arguments:  Arguments:
2233    code        points to start of search    code        points to start of search
2234    endcode     points to where to stop    endcode     points to where to stop
2235    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2236    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2237      recurses    chain of recurse_check to catch mutual recursion
2238    
2239  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2240  */  */
2241    
2242    typedef struct recurse_check {
2243      struct recurse_check *prev;
2244      const pcre_uchar *group;
2245    } recurse_check;
2246    
2247  static BOOL  static BOOL
2248  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2249    compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2250  {  {
2251  register int c;  register pcre_uchar c;
2252  for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE);  recurse_check this_recurse;
2253    
2254    for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2255       code < endcode;       code < endcode;
2256       code = first_significant_code(code + _pcre_OP_lengths[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2257    {    {
2258    const uschar *ccode;    const pcre_uchar *ccode;
2259    
2260    c = *code;    c = *code;
2261    
# Line 2197  for (code = first_significant_code(code Line 2278  for (code = first_significant_code(code
2278    
2279    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2280      {      {
2281      const uschar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2282      BOOL empty_branch;      BOOL empty_branch;
2283    
2284      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2285        when called to scan a completed pattern by setting cd->start_workspace to
2286        NULL. */
2287    
2288        if (cd->start_workspace != NULL)
2289          {
2290          const pcre_uchar *tcode;
2291          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2292            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2293          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2294          }
2295    
2296        /* If we are scanning a completed pattern, there are no forward references
2297        and all groups are complete. We need to detect whether this is a recursive
2298        call, as otherwise there will be an infinite loop. If it is a recursion,
2299        just skip over it. Simple recursions are easily detected. For mutual
2300        recursions we keep a chain on the stack. */
2301    
2302        else
2303          {
2304          recurse_check *r = recurses;
2305          const pcre_uchar *endgroup = scode;
2306    
2307          do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2308          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2309    
2310      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)        for (r = recurses; r != NULL; r = r->prev)
2311        if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;          if (r->group == scode) break;
2312          if (r != NULL) continue;   /* Mutual recursion */
2313          }
2314    
2315      /* Not a forward reference, test for completed backward reference */      /* Completed reference; scan the referenced group, remembering it on the
2316        stack chain to detect mutual recursions. */
2317    
2318      empty_branch = FALSE;      empty_branch = FALSE;
2319      scode = cd->start_code + GET(code, 1);      this_recurse.prev = recurses;
2320      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      this_recurse.group = scode;
   
     /* Completed backwards reference */  
2321    
2322      do      do
2323        {        {
2324        if (could_be_empty_branch(scode, endcode, utf8, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2325          {          {
2326          empty_branch = TRUE;          empty_branch = TRUE;
2327          break;          break;
# Line 2233  for (code = first_significant_code(code Line 2339  for (code = first_significant_code(code
2339    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2340        c == OP_BRAPOSZERO)        c == OP_BRAPOSZERO)
2341      {      {
2342      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2343      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
2344      c = *code;      c = *code;
2345      continue;      continue;
# Line 2271  for (code = first_significant_code(code Line 2377  for (code = first_significant_code(code
2377        empty_branch = FALSE;        empty_branch = FALSE;
2378        do        do
2379          {          {
2380          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2381            empty_branch = TRUE;            empty_branch = TRUE;
2382          code += GET(code, 1);          code += GET(code, 1);
2383          }          }
# Line 2289  for (code = first_significant_code(code Line 2395  for (code = first_significant_code(code
2395      {      {
2396      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2397      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2398      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2399      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"
2400      here. */      here. */
2401    
2402  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2403      case OP_XCLASS:      case OP_XCLASS:
2404      ccode = code += GET(code, 1);      ccode = code += GET(code, 1);
2405      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
# Line 2301  for (code = first_significant_code(code Line 2407  for (code = first_significant_code(code
2407    
2408      case OP_CLASS:      case OP_CLASS:
2409      case OP_NCLASS:      case OP_NCLASS:
2410      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2411    
2412  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2413      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
2414  #endif  #endif
2415    
# Line 2329  for (code = first_significant_code(code Line 2435  for (code = first_significant_code(code
2435    
2436      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2437    
2438        case OP_ANY:
2439        case OP_ALLANY:
2440        case OP_ANYBYTE:
2441    
2442      case OP_PROP:      case OP_PROP:
2443      case OP_NOTPROP:      case OP_NOTPROP:
2444        case OP_ANYNL:
2445    
2446        case OP_NOT_HSPACE:
2447        case OP_HSPACE:
2448        case OP_NOT_VSPACE:
2449        case OP_VSPACE:
2450      case OP_EXTUNI:      case OP_EXTUNI:
2451    
2452      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2453      case OP_DIGIT:      case OP_DIGIT:
2454      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2455      case OP_WHITESPACE:      case OP_WHITESPACE:
2456      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2457      case OP_WORDCHAR:      case OP_WORDCHAR:
2458      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2459      case OP_CHAR:      case OP_CHAR:
2460      case OP_CHARI:      case OP_CHARI:
2461      case OP_NOT:      case OP_NOT:
2462      case OP_NOTI:      case OP_NOTI:
2463    
2464      case OP_PLUS:      case OP_PLUS:
2465        case OP_PLUSI:
2466      case OP_MINPLUS:      case OP_MINPLUS:
2467      case OP_POSPLUS:      case OP_MINPLUSI:
2468      case OP_EXACT:  
2469      case OP_NOTPLUS:      case OP_NOTPLUS:
2470        case OP_NOTPLUSI:
2471      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2472        case OP_NOTMINPLUSI:
2473    
2474        case OP_POSPLUS:
2475        case OP_POSPLUSI:
2476      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2477        case OP_NOTPOSPLUSI:
2478    
2479        case OP_EXACT:
2480        case OP_EXACTI:
2481      case OP_NOTEXACT:      case OP_NOTEXACT:
2482        case OP_NOTEXACTI:
2483    
2484      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2485      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2486      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2487      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2488    
2489      return FALSE;      return FALSE;
2490    
2491      /* These are going to continue, as they may be empty, but we have to      /* These are going to continue, as they may be empty, but we have to
# Line 2376  for (code = first_significant_code(code Line 2505  for (code = first_significant_code(code
2505      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2506      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2507      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2508      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2509          code += 2;
2510      break;      break;
2511    
2512      /* End of branch */      /* End of branch */
# Line 2389  for (code = first_significant_code(code Line 2519  for (code = first_significant_code(code
2519      return TRUE;      return TRUE;
2520    
2521      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2522      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2523        followed by a multibyte character. */
2524    
2525  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2526      case OP_STAR:      case OP_STAR:
2527      case OP_STARI:      case OP_STARI:
2528        case OP_NOTSTAR:
2529        case OP_NOTSTARI:
2530    
2531      case OP_MINSTAR:      case OP_MINSTAR:
2532      case OP_MINSTARI:      case OP_MINSTARI:
2533        case OP_NOTMINSTAR:
2534        case OP_NOTMINSTARI:
2535    
2536      case OP_POSSTAR:      case OP_POSSTAR:
2537      case OP_POSSTARI:      case OP_POSSTARI:
2538        case OP_NOTPOSSTAR:
2539        case OP_NOTPOSSTARI:
2540    
2541      case OP_QUERY:      case OP_QUERY:
2542      case OP_QUERYI:      case OP_QUERYI:
2543        case OP_NOTQUERY:
2544        case OP_NOTQUERYI:
2545    
2546      case OP_MINQUERY:      case OP_MINQUERY:
2547      case OP_MINQUERYI:      case OP_MINQUERYI:
2548        case OP_NOTMINQUERY:
2549        case OP_NOTMINQUERYI:
2550    
2551      case OP_POSQUERY:      case OP_POSQUERY:
2552      case OP_POSQUERYI:      case OP_POSQUERYI:
2553      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      case OP_NOTPOSQUERY:
2554        case OP_NOTPOSQUERYI:
2555    
2556        if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2557      break;      break;
2558    
2559      case OP_UPTO:      case OP_UPTO:
2560      case OP_UPTOI:      case OP_UPTOI:
2561        case OP_NOTUPTO:
2562        case OP_NOTUPTOI:
2563    
2564      case OP_MINUPTO:      case OP_MINUPTO:
2565      case OP_MINUPTOI:      case OP_MINUPTOI:
2566        case OP_NOTMINUPTO:
2567        case OP_NOTMINUPTOI:
2568    
2569      case OP_POSUPTO:      case OP_POSUPTO:
2570      case OP_POSUPTOI:      case OP_POSUPTOI:
2571      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      case OP_NOTPOSUPTO:
2572        case OP_NOTPOSUPTOI:
2573    
2574        if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2575      break;      break;
2576  #endif  #endif
2577    
# Line 2423  for (code = first_significant_code(code Line 2581  for (code = first_significant_code(code
2581      case OP_MARK:      case OP_MARK:
2582      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2583      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     code += code[1];  
     break;  
   
2584      case OP_THEN_ARG:      case OP_THEN_ARG:
2585      code += code[1];      code += code[1];
2586      break;      break;
# Line 2457  Arguments: Line 2612  Arguments:
2612    code        points to start of the recursion    code        points to start of the recursion
2613    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2614    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2615    utf8        TRUE if in UTF-8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2616    cd          pointers to tables etc    cd          pointers to tables etc
2617    
2618  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2619  */  */
2620    
2621  static BOOL  static BOOL
2622  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2623    BOOL utf8, compile_data *cd)    branch_chain *bcptr, BOOL utf, compile_data *cd)
2624  {  {
2625  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2626    {    {
2627    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2628      return FALSE;      return FALSE;
2629    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2630    }    }
# Line 2479  return TRUE; Line 2634  return TRUE;
2634    
2635    
2636  /*************************************************  /*************************************************
2637  *           Check for POSIX class syntax         *  *        Base opcode of repeated opcodes         *
2638  *************************************************/  *************************************************/
2639    
2640  /* This function is called when the sequence "[:" or "[." or "[=" is  /* Returns the base opcode for repeated single character type opcodes. If the
2641  encountered in a character class. It checks whether this is followed by a  opcode is not a repeated character type, it returns with the original value.
 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we  
 reach an unescaped ']' without the special preceding character, return FALSE.  
   
 Originally, this function only recognized a sequence of letters between the  
 terminators, but it seems that Perl recognizes any sequence of characters,  
 though of course unknown POSIX names are subsequently rejected. Perl gives an  
 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE  
 didn't consider this to be a POSIX class. Likewise for [:1234:].  
   
 The problem in trying to be exactly like Perl is in the handling of escapes. We  
 have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX  
 class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code  
 below handles the special case of \], but does not try to do any other escape  
 processing. This makes it different from Perl for cases such as [:l\ower:]  
 where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize  
 "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,  
 I think.  
   
 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.  
 It seems that the appearance of a nested POSIX class supersedes an apparent  
 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or  
 a digit.  
   
 In Perl, unescaped square brackets may also appear as part of class names. For  
 example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for  
 [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not  
 seem right at all. PCRE does not allow closing square brackets in POSIX class  
 names.  
   
 Arguments:  
   ptr      pointer to the initial [  
   endptr   where to return the end pointer  
2642    
2643  Returns:   TRUE or FALSE  Arguments:  c opcode
2644    Returns:    base opcode for the type
2645  */  */
2646    
2647  static BOOL  static pcre_uchar
2648  check_posix_syntax(const uschar *ptr, const uschar **endptr)  get_repeat_base(pcre_uchar c)
2649  {  {
2650  int terminator;          /* Don't combine these lines; the Solaris cc */  return (c > OP_TYPEPOSUPTO)? c :
2651  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */         (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2652  for (++ptr; *ptr != 0; ptr++)         (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2653    {         (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2654    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)         (c >= OP_STARI)?      OP_STARI :
2655      ptr++;                               OP_STAR;
   else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;  
   else  
     {  
     if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)  
       {  
       *endptr = ptr;  
       return TRUE;  
       }  
     if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&  
          (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||  
           ptr[1] == CHAR_EQUALS_SIGN) &&  
         check_posix_syntax(ptr, endptr))  
       return FALSE;  
     }  
   }  
 return FALSE;  
2656  }  }
2657    
2658    
2659    
2660    #ifdef SUPPORT_UCP
2661  /*************************************************  /*************************************************
2662  *          Check POSIX class name                *  *        Check a character and a property        *
2663  *************************************************/  *************************************************/
2664    
2665  /* This function is called to check the name given in a POSIX-style class entry  /* This function is called by check_auto_possessive() when a property item
2666  such as [:alnum:].  is adjacent to a fixed character.
2667    
2668  Arguments:  Arguments:
2669    ptr        points to the first letter    c            the character
2670    len        the length of the name    ptype        the property type
2671      pdata        the data for the type
2672      negated      TRUE if it's a negated property (\P or \p{^)
2673    
2674  Returns:     a value representing the name, or -1 if unknown  Returns:       TRUE if auto-possessifying is OK
2675  */  */
2676    
2677  static int  static BOOL
2678  check_posix_name(const uschar *ptr, int len)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2679      BOOL negated)
2680  {  {
2681  const char *pn = posix_names;  const pcre_uint32 *p;
2682  register int yield = 0;  const ucd_record *prop = GET_UCD(c);
 while (posix_name_lengths[yield] != 0)  
   {  
   if (len == posix_name_lengths[yield] &&  
     strncmp((const char *)ptr, pn, len) == 0) return yield;  
   pn += posix_name_lengths[yield] + 1;  
   yield++;  
   }  
 return -1;  
 }  
   
   
 /*************************************************  
 *    Adjust OP_RECURSE items in repeated group   *  
 *************************************************/  
2683    
2684  /* OP_RECURSE items contain an offset from the start of the regex to the group  switch(ptype)
2685  that is referenced. This means that groups can be replicated for fixed    {
2686  repetition simply by copying (because the recursion is allowed to refer to    case PT_LAMP:
2687  earlier groups that are outside the current group). However, when a group is    return (prop->chartype == ucp_Lu ||
2688  optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is            prop->chartype == ucp_Ll ||
2689  inserted before it, after it has been compiled. This means that any OP_RECURSE            prop->chartype == ucp_Lt) == negated;
 items within it that refer to the group itself or any contained groups have to  
 have their offsets adjusted. That one of the jobs of this function. Before it  
 is called, the partially compiled regex must be temporarily terminated with  
 OP_END.  
2690    
2691  This function has been extended with the possibility of forward references for    case PT_GC:
2692  recursions and subroutine calls. It must also check the list of such references    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
 for the group we are dealing with. If it finds that one of the recursions in  
 the current group is on this list, it adjusts the offset in the list, not the  
 value in the reference (which is a group number).  
2693    
2694  Arguments:    case PT_PC:
2695    group      points to the start of the group    return (pdata == prop->chartype) == negated;
   adjust     the amount by which the group is to be moved  
   utf8       TRUE in UTF-8 mode  
   cd         contains pointers to tables etc.  
   save_hwm   the hwm forward reference pointer at the start of the group  
2696    
2697  Returns:     nothing    case PT_SC:
2698  */    return (pdata == prop->script) == negated;
2699    
2700  static void    /* These are specials */
 adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  
   uschar *save_hwm)  
 {  
 uschar *ptr = group;  
2701    
2702  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)    case PT_ALNUM:
2703    {    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2704    int offset;            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
   uschar *hc;  
2705    
2706    /* See if this recursion is on the forward reference list. If so, adjust the    /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2707    reference. */    means that Perl space and POSIX space are now identical. PCRE was changed
2708      at release 8.34. */
2709    
2710    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    case PT_SPACE:    /* Perl space */
2711      case PT_PXSPACE:  /* POSIX space */
2712      switch(c)
2713      {      {
2714      offset = GET(hc, 0);      HSPACE_CASES:
2715      if (cd->start_code + offset == ptr + 1)      VSPACE_CASES:
2716        {      return negated;
2717        PUT(hc, 0, offset + adjust);  
2718        break;      default:
2719        }      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
2720      }      }
2721      break;  /* Control never reaches here */
2722    
2723    /* Otherwise, adjust the recursion offset if it's after the start of this    case PT_WORD:
2724    group. */    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2725              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2726              c == CHAR_UNDERSCORE) == negated;
2727    
2728    if (hc >= cd->hwm)    case PT_CLIST:
2729      p = PRIV(ucd_caseless_sets) + prop->caseset;
2730      for (;;)
2731      {      {
2732      offset = GET(ptr, 1);      if (c < *p) return !negated;
2733      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      if (c == *p++) return negated;
2734      }      }
2735      break;  /* Control never reaches here */
   ptr += 1 + LINK_SIZE;  
2736    }    }
2737    
2738    return FALSE;
2739  }  }
2740    #endif  /* SUPPORT_UCP */
2741    
2742    
2743    
2744  /*************************************************  /*************************************************
2745  *        Insert an automatic callout point       *  *        Fill the character property list        *
2746  *************************************************/  *************************************************/
2747    
2748  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert  /* Checks whether the code points to an opcode that can take part in auto-
2749  callout points before each pattern item.  possessification, and if so, fills a list with its properties.
2750    
2751  Arguments:  Arguments:
2752    code           current code pointer    code        points to start of expression
2753    ptr            current pattern pointer    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2754    cd             pointers to tables etc    fcc         points to case-flipping table
2755      list        points to output list
2756                  list[0] will be filled with the opcode
2757                  list[1] will be non-zero if this opcode
2758                    can match an empty character string
2759                  list[2..7] depends on the opcode
2760    
2761  Returns:         new code pointer  Returns:      points to the start of the next opcode if *code is accepted
2762                  NULL if *code is not accepted
2763  */  */
2764    
2765  static uschar *  static const pcre_uchar *
2766  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  get_chr_property_list(const pcre_uchar *code, BOOL utf,
2767      const pcre_uint8 *fcc, pcre_uint32 *list)
2768  {  {
2769  *code++ = OP_CALLOUT;  pcre_uchar c = *code;
2770  *code++ = 255;  const pcre_uchar *end;
2771  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  const pcre_uint32 *clist_src;
2772  PUT(code, LINK_SIZE, 0);                       /* Default length */  pcre_uint32 *clist_dest;
2773  return code + 2*LINK_SIZE;  pcre_uint32 chr;
2774  }  pcre_uchar base;
2775    
2776    list[0] = c;
2777    list[1] = FALSE;
2778    code++;
2779    
2780    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2781      {
2782      base = get_repeat_base(c);
2783      c -= (base - OP_STAR);
2784    
2785  /*************************************************    if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2786  *         Complete a callout item                *      code += IMM2_SIZE;
 *************************************************/  
2787    
2788  /* A callout item contains the length of the next item in the pattern, which    list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
 we can't fill in till after we have reached the relevant point. This is used  
 for both automatic and manual callouts.  
2789    
2790  Arguments:    switch(base)
2791    previous_callout   points to previous callout item      {
2792    ptr                current pattern pointer      case OP_STAR:
2793    cd                 pointers to tables etc      list[0] = OP_CHAR;
2794        break;
2795    
2796        case OP_STARI:
2797        list[0] = OP_CHARI;
2798        break;
2799    
2800        case OP_NOTSTAR:
2801        list[0] = OP_NOT;
2802        break;
2803    
2804        case OP_NOTSTARI:
2805        list[0] = OP_NOTI;
2806        break;
2807    
2808        case OP_TYPESTAR:
2809        list[0] = *code;
2810        code++;
2811        break;
2812        }
2813      c = list[0];
2814      }
2815    
2816    switch(c)
2817      {
2818      case OP_NOT_DIGIT:
2819      case OP_DIGIT:
2820      case OP_NOT_WHITESPACE:
2821      case OP_WHITESPACE:
2822      case OP_NOT_WORDCHAR:
2823      case OP_WORDCHAR:
2824      case OP_ANY:
2825      case OP_ALLANY:
2826      case OP_ANYNL:
2827      case OP_NOT_HSPACE:
2828      case OP_HSPACE:
2829      case OP_NOT_VSPACE:
2830      case OP_VSPACE:
2831      case OP_EXTUNI:
2832      case OP_EODN:
2833      case OP_EOD:
2834      case OP_DOLL:
2835      case OP_DOLLM:
2836      return code;
2837    
2838      case OP_CHAR:
2839      case OP_NOT:
2840      GETCHARINCTEST(chr, code);
2841      list[2] = chr;
2842      list[3] = NOTACHAR;
2843      return code;
2844    
2845      case OP_CHARI:
2846      case OP_NOTI:
2847      list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2848      GETCHARINCTEST(chr, code);
2849      list[2] = chr;
2850    
2851    #ifdef SUPPORT_UCP
2852      if (chr < 128 || (chr < 256 && !utf))
2853        list[3] = fcc[chr];
2854      else
2855        list[3] = UCD_OTHERCASE(chr);
2856    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2857      list[3] = (chr < 256) ? fcc[chr] : chr;
2858    #else
2859      list[3] = fcc[chr];
2860    #endif
2861    
2862      /* The othercase might be the same value. */
2863    
2864      if (chr == list[3])
2865        list[3] = NOTACHAR;
2866      else
2867        list[4] = NOTACHAR;
2868      return code;
2869    
2870    #ifdef SUPPORT_UCP
2871      case OP_PROP:
2872      case OP_NOTPROP:
2873      if (code[0] != PT_CLIST)
2874        {
2875        list[2] = code[0];
2876        list[3] = code[1];
2877        return code + 2;
2878        }
2879    
2880      /* Convert only if we have enough space. */
2881    
2882      clist_src = PRIV(ucd_caseless_sets) + code[1];
2883      clist_dest = list + 2;
2884      code += 2;
2885    
2886      do {
2887         if (clist_dest >= list + 8)
2888           {
2889           /* Early return if there is not enough space. This should never
2890           happen, since all clists are shorter than 5 character now. */
2891           list[2] = code[0];
2892           list[3] = code[1];
2893           return code;
2894           }
2895         *clist_dest++ = *clist_src;
2896         }
2897      while(*clist_src++ != NOTACHAR);
2898    
2899      /* All characters are stored. The terminating NOTACHAR
2900      is copied form the clist itself. */
2901    
2902      list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2903      return code;
2904    #endif
2905    
2906      case OP_NCLASS:
2907      case OP_CLASS:
2908    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2909      case OP_XCLASS:
2910    
2911      if (c == OP_XCLASS)
2912        end = code + GET(code, 0);
2913      else
2914    #endif
2915        end = code + 32 / sizeof(pcre_uchar);
2916    
2917      switch(*end)
2918        {
2919        case OP_CRSTAR:
2920        case OP_CRMINSTAR:
2921        case OP_CRQUERY:
2922        case OP_CRMINQUERY:
2923        list[1] = TRUE;
2924        end++;
2925        break;
2926    
2927        case OP_CRRANGE:
2928        case OP_CRMINRANGE:
2929        list[1] = (GET2(end, 1) == 0);
2930        end += 1 + 2 * IMM2_SIZE;
2931        break;
2932        }
2933      list[2] = end - code;
2934      return end;
2935      }
2936    return NULL;    /* Opcode not accepted */
2937    }
2938    
2939    
2940    
2941    /*************************************************
2942    *    Scan further character sets for match       *
2943    *************************************************/
2944    
2945    /* Checks whether the base and the current opcode have a common character, in
2946    which case the base cannot be possessified.
2947    
2948    Arguments:
2949      code        points to the byte code
2950      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2951      cd          static compile data
2952      base_list   the data list of the base opcode
2953    
2954    Returns:      TRUE if the auto-possessification is possible
2955    */
2956    
2957    static BOOL
2958    compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2959      const pcre_uint32* base_list)
2960    {
2961    pcre_uchar c;
2962    pcre_uint32 list[8];
2963    const pcre_uint32* chr_ptr;
2964    const pcre_uint32* ochr_ptr;
2965    const pcre_uint32* list_ptr;
2966    const pcre_uchar *next_code;
2967    pcre_uint32 chr;
2968    
2969    /* Note: the base_list[1] contains whether the current opcode has greedy
2970    (represented by a non-zero value) quantifier. This is a different from
2971    other character type lists, which stores here that the character iterator
2972    matches to an empty string (also represented by a non-zero value). */
2973    
2974    for(;;)
2975      {
2976      c = *code;
2977    
2978      /* Skip over callouts */
2979    
2980      if (c == OP_CALLOUT)
2981        {
2982        code += PRIV(OP_lengths)[c];
2983        continue;
2984        }
2985    
2986      if (c == OP_ALT)
2987        {
2988        do code += GET(code, 1); while (*code == OP_ALT);
2989        c = *code;
2990        }
2991    
2992      switch(c)
2993        {
2994        case OP_END:
2995        case OP_KETRPOS:
2996        /* TRUE only in greedy case. The non-greedy case could be replaced by
2997        an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT
2998        uses more memory, which we cannot get at this stage.) */
2999    
3000        return base_list[1] != 0;
3001    
3002        case OP_KET:
3003        /* If the bracket is capturing, and referenced by an OP_RECURSE, or
3004        it is an atomic sub-pattern (assert, once, etc.) the non-greedy case
3005        cannot be converted to a possessive form. */
3006    
3007        if (base_list[1] == 0) return FALSE;
3008    
3009        switch(*(code - GET(code, 1)))
3010          {
3011          case OP_ASSERT:
3012          case OP_ASSERT_NOT:
3013          case OP_ASSERTBACK:
3014          case OP_ASSERTBACK_NOT:
3015          case OP_ONCE:
3016          case OP_ONCE_NC:
3017          /* Atomic sub-patterns and assertions can always auto-possessify their
3018          last iterator. */
3019          return TRUE;
3020          }
3021    
3022        code += PRIV(OP_lengths)[c];
3023        continue;
3024    
3025        case OP_ONCE:
3026        case OP_ONCE_NC:
3027        case OP_BRA:
3028        case OP_CBRA:
3029        next_code = code;
3030        do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3031    
3032        /* We do not support repeated brackets, because they can lead to
3033        infinite recursion. */
3034    
3035        if (*next_code != OP_KET) return FALSE;
3036    
3037        next_code = code + GET(code, 1);
3038        code += PRIV(OP_lengths)[c];
3039    
3040        while (*next_code == OP_ALT)
3041          {
3042          if (!compare_opcodes(code, utf, cd, base_list)) return FALSE;
3043          code = next_code + 1 + LINK_SIZE;
3044          next_code += GET(next_code, 1);
3045          }
3046        continue;
3047    
3048        case OP_BRAZERO:
3049        case OP_BRAMINZERO:
3050    
3051        next_code = code + 1;
3052        if (*next_code != OP_BRA && *next_code != OP_CBRA)
3053          return FALSE;
3054    
3055        do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3056    
3057        /* We do not support repeated brackets, because they can lead to
3058        infinite recursion. */
3059        if (*next_code != OP_KET) return FALSE;
3060    
3061        /* The bracket content will be checked by the
3062        OP_BRA/OP_CBRA case above. */
3063        next_code += 1 + LINK_SIZE;
3064        if (!compare_opcodes(next_code, utf, cd, base_list)) return FALSE;
3065    
3066        code += PRIV(OP_lengths)[c];
3067        continue;
3068        }
3069    
3070      /* Check for a supported opcode, and load its properties. */
3071    
3072      code = get_chr_property_list(code, utf, cd->fcc, list);
3073      if (code == NULL) return FALSE;    /* Unsupported */
3074    
3075      /* If either opcode is a small character list, set pointers for comparing
3076      characters from that list with another list, or with a property. */
3077    
3078      if (base_list[0] == OP_CHAR)
3079        {
3080        chr_ptr = base_list + 2;
3081        list_ptr = list;
3082        }
3083      else if (list[0] == OP_CHAR)
3084        {
3085        chr_ptr = list + 2;
3086        list_ptr = base_list;
3087        }
3088    
3089      /* Some property combinations also acceptable. Unicode property opcodes are
3090      processed specially; the rest can be handled with a lookup table. */
3091    
3092      else
3093        {
3094        pcre_uint32 leftop, rightop;
3095    
3096        if (list[1] != 0) return FALSE;   /* Must match at least one character */
3097        leftop = base_list[0];
3098        rightop = list[0];
3099    
3100    #ifdef SUPPORT_UCP
3101        if (leftop == OP_PROP || leftop == OP_NOTPROP)
3102          {
3103          if (rightop == OP_EOD) return TRUE;
3104          if (rightop == OP_PROP || rightop == OP_NOTPROP)
3105            {
3106            int n;
3107            const pcre_uint8 *p;
3108            BOOL same = leftop == rightop;
3109            BOOL lisprop = leftop == OP_PROP;
3110            BOOL risprop = rightop == OP_PROP;
3111            BOOL bothprop = lisprop && risprop;
3112    
3113            /* There's a table that specifies how each combination is to be
3114            processed:
3115              0   Always return FALSE (never auto-possessify)
3116              1   Character groups are distinct (possessify if both are OP_PROP)
3117              2   Check character categories in the same group (general or particular)
3118              3   Return TRUE if the two opcodes are not the same
3119              ... see comments below
3120            */
3121    
3122            n = propposstab[base_list[2]][list[2]];
3123            switch(n)
3124              {
3125              case 0: return FALSE;
3126              case 1: return bothprop;
3127              case 2: return (base_list[3] == list[3]) != same;
3128              case 3: return !same;
3129    
3130              case 4:  /* Left general category, right particular category */
3131              return risprop && catposstab[base_list[3]][list[3]] == same;
3132    
3133              case 5:  /* Right general category, left particular category */
3134              return lisprop && catposstab[list[3]][base_list[3]] == same;
3135    
3136              /* This code is logically tricky. Think hard before fiddling with it.
3137              The posspropstab table has four entries per row. Each row relates to
3138              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3139              Only WORD actually needs all four entries, but using repeats for the
3140              others means they can all use the same code below.
3141    
3142              The first two entries in each row are Unicode general categories, and
3143              apply always, because all the characters they include are part of the
3144              PCRE character set. The third and fourth entries are a general and a
3145              particular category, respectively, that include one or more relevant
3146              characters. One or the other is used, depending on whether the check
3147              is for a general or a particular category. However, in both cases the
3148              category contains more characters than the specials that are defined
3149              for the property being tested against. Therefore, it cannot be used
3150              in a NOTPROP case.
3151    
3152              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3153              Underscore is covered by ucp_P or ucp_Po. */
3154    
3155              case 6:  /* Left alphanum vs right general category */
3156              case 7:  /* Left space vs right general category */
3157              case 8:  /* Left word vs right general category */
3158              p = posspropstab[n-6];
3159              return risprop && lisprop ==
3160                (list[3] != p[0] &&
3161                 list[3] != p[1] &&
3162                (list[3] != p[2] || !lisprop));
3163    
3164              case 9:   /* Right alphanum vs left general category */
3165              case 10:  /* Right space vs left general category */
3166              case 11:  /* Right word vs left general category */
3167              p = posspropstab[n-9];
3168              return lisprop && risprop ==
3169                (base_list[3] != p[0] &&
3170                 base_list[3] != p[1] &&
3171                (base_list[3] != p[2] || !risprop));
3172    
3173              case 12:  /* Left alphanum vs right particular category */
3174              case 13:  /* Left space vs right particular category */
3175              case 14:  /* Left word vs right particular category */
3176              p = posspropstab[n-12];
3177              return risprop && lisprop ==
3178                (catposstab[p[0]][list[3]] &&
3179                 catposstab[p[1]][list[3]] &&
3180                (list[3] != p[3] || !lisprop));
3181    
3182              case 15:  /* Right alphanum vs left particular category */
3183              case 16:  /* Right space vs left particular category */
3184              case 17:  /* Right word vs left particular category */
3185              p = posspropstab[n-15];
3186              return lisprop && risprop ==
3187                (catposstab[p[0]][base_list[3]] &&
3188                 catposstab[p[1]][base_list[3]] &&
3189                (base_list[3] != p[3] || !risprop));
3190              }
3191            }
3192          return FALSE;
3193          }
3194    
3195        else
3196    #endif  /* SUPPORT_UCP */
3197    
3198        return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3199               rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3200               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3201        }
3202    
3203      /* Control reaches here only if one of the items is a small character list.
3204      All characters are checked against the other side. */
3205    
3206      do
3207        {
3208        chr = *chr_ptr;
3209    
3210        switch(list_ptr[0])
3211          {
3212          case OP_CHAR:
3213          ochr_ptr = list_ptr + 2;
3214          do
3215            {
3216            if (chr == *ochr_ptr) return FALSE;
3217            ochr_ptr++;
3218            }
3219          while(*ochr_ptr != NOTACHAR);
3220          break;
3221    
3222          case OP_NOT:
3223          ochr_ptr = list_ptr + 2;
3224          do
3225            {
3226            if (chr == *ochr_ptr)
3227              break;
3228            ochr_ptr++;
3229            }
3230          while(*ochr_ptr != NOTACHAR);
3231          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3232          break;
3233    
3234          /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3235          set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3236    
3237          case OP_DIGIT:
3238          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3239          break;
3240    
3241          case OP_NOT_DIGIT:
3242          if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3243          break;
3244    
3245          case OP_WHITESPACE:
3246          if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3247          break;
3248    
3249          case OP_NOT_WHITESPACE:
3250          if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3251          break;
3252    
3253          case OP_WORDCHAR:
3254          if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3255          break;
3256    
3257          case OP_NOT_WORDCHAR:
3258          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3259          break;
3260    
3261          case OP_HSPACE:
3262          switch(chr)
3263            {
3264            HSPACE_CASES: return FALSE;
3265            default: break;
3266            }
3267          break;
3268    
3269          case OP_NOT_HSPACE:
3270          switch(chr)
3271            {
3272            HSPACE_CASES: break;
3273            default: return FALSE;
3274            }
3275          break;
3276    
3277          case OP_ANYNL:
3278          case OP_VSPACE:
3279          switch(chr)
3280            {
3281            VSPACE_CASES: return FALSE;
3282            default: break;
3283            }
3284          break;
3285    
3286          case OP_NOT_VSPACE:
3287          switch(chr)
3288            {
3289            VSPACE_CASES: break;
3290            default: return FALSE;
3291            }
3292          break;
3293    
3294          case OP_DOLL:
3295          case OP_EODN:
3296          switch (chr)
3297            {
3298            case CHAR_CR:
3299            case CHAR_LF:
3300            case CHAR_VT:
3301            case CHAR_FF:
3302            case CHAR_NEL:
3303    #ifndef EBCDIC
3304            case 0x2028:
3305            case 0x2029:
3306    #endif  /* Not EBCDIC */
3307            return FALSE;
3308            }
3309          break;
3310    
3311          case OP_EOD:    /* Can always possessify before \z */
3312          break;
3313    
3314          case OP_PROP:
3315          case OP_NOTPROP:
3316          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3317                list_ptr[0] == OP_NOTPROP))
3318            return FALSE;
3319          break;
3320    
3321          /* The class comparisons work only when the class is the second item
3322          of the pair, because there are at present no possessive forms of the
3323          class opcodes. Note also that the "code" variable that is used below
3324          points after the second item, and that the pointer for the first item
3325          is not available, so even if there were possessive forms of the class
3326          opcodes, the correct comparison could not be done. */
3327    
3328          case OP_NCLASS:
3329          if (chr > 255) return FALSE;
3330          /* Fall through */
3331    
3332          case OP_CLASS:
3333          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3334          if (chr > 255) break;
3335          if ((((pcre_uint8 *)(code - list_ptr[2]))[chr >> 3] & (1 << (chr & 7))) != 0)
3336            return FALSE;
3337          break;
3338    
3339    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3340          case OP_XCLASS:
3341          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3342          if (PRIV(xclass)(chr, code - list_ptr[2] + LINK_SIZE, utf))
3343            return FALSE;
3344          break;
3345    #endif
3346    
3347          default:
3348          return FALSE;
3349          }
3350    
3351        chr_ptr++;
3352        }
3353      while(*chr_ptr != NOTACHAR);
3354    
3355      /* At least one character must be matched from this opcode. */
3356    
3357      if (list[1] == 0) return TRUE;
3358      }
3359    
3360    return FALSE;
3361    }
3362    
3363    
3364    
3365    /*************************************************
3366    *    Scan compiled regex for auto-possession     *
3367    *************************************************/
3368    
3369    /* Replaces single character iterations with their possessive alternatives
3370    if appropriate. This function modifies the compiled opcode!
3371    
3372    Arguments:
3373      code        points to start of the byte code
3374      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3375      cd          static compile data
3376    
3377    Returns:      nothing
3378    */
3379    
3380    static void
3381    auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
3382    {
3383    register pcre_uchar c;
3384    const pcre_uchar *end;
3385    pcre_uint32 list[8];
3386    
3387    for (;;)
3388      {
3389      c = *code;
3390    
3391      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3392        {
3393        c -= get_repeat_base(c) - OP_STAR;
3394        end = (c <= OP_MINUPTO) ?
3395          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3396        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3397    
3398        if (end != NULL && compare_opcodes(end, utf, cd, list))
3399          {
3400          switch(c)
3401            {
3402            case OP_STAR:
3403            *code += OP_POSSTAR - OP_STAR;
3404            break;
3405    
3406            case OP_MINSTAR:
3407            *code += OP_POSSTAR - OP_MINSTAR;
3408            break;
3409    
3410            case OP_PLUS:
3411            *code += OP_POSPLUS - OP_PLUS;
3412            break;
3413    
3414            case OP_MINPLUS:
3415            *code += OP_POSPLUS - OP_MINPLUS;
3416            break;
3417    
3418            case OP_QUERY:
3419            *code += OP_POSQUERY - OP_QUERY;
3420            break;
3421    
3422            case OP_MINQUERY:
3423            *code += OP_POSQUERY - OP_MINQUERY;
3424            break;
3425    
3426            case OP_UPTO:
3427            *code += OP_POSUPTO - OP_UPTO;
3428            break;
3429    
3430            case OP_MINUPTO:
3431            *code += OP_MINUPTO - OP_UPTO;
3432            break;
3433            }
3434          }
3435        c = *code;
3436        }
3437    
3438      switch(c)
3439        {
3440        case OP_END:
3441        return;
3442    
3443        case OP_TYPESTAR:
3444        case OP_TYPEMINSTAR:
3445        case OP_TYPEPLUS:
3446        case OP_TYPEMINPLUS:
3447        case OP_TYPEQUERY:
3448        case OP_TYPEMINQUERY:
3449        case OP_TYPEPOSSTAR:
3450        case OP_TYPEPOSPLUS:
3451        case OP_TYPEPOSQUERY:
3452        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3453        break;
3454    
3455        case OP_TYPEUPTO:
3456        case OP_TYPEMINUPTO:
3457        case OP_TYPEEXACT:
3458        case OP_TYPEPOSUPTO:
3459        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3460          code += 2;
3461        break;
3462    
3463        case OP_XCLASS:
3464        code += GET(code, 1);
3465        break;
3466    
3467        case OP_MARK:
3468        case OP_PRUNE_ARG:
3469        case OP_SKIP_ARG:
3470        case OP_THEN_ARG:
3471        code += code[1];
3472        break;
3473        }
3474    
3475      /* Add in the fixed length from the table */
3476    
3477      code += PRIV(OP_lengths)[c];
3478    
3479      /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3480      a multi-byte character. The length in the table is a minimum, so we have to
3481      arrange to skip the extra bytes. */
3482    
3483    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3484      if (utf) switch(c)
3485        {
3486        case OP_CHAR:
3487        case OP_CHARI:
3488        case OP_NOT:
3489        case OP_NOTI:
3490        case OP_STAR:
3491        case OP_MINSTAR:
3492        case OP_PLUS:
3493        case OP_MINPLUS:
3494        case OP_QUERY:
3495        case OP_MINQUERY:
3496        case OP_UPTO:
3497        case OP_MINUPTO:
3498        case OP_EXACT:
3499        case OP_POSSTAR:
3500        case OP_POSPLUS:
3501        case OP_POSQUERY:
3502        case OP_POSUPTO:
3503        case OP_STARI:
3504        case OP_MINSTARI:
3505        case OP_PLUSI:
3506        case OP_MINPLUSI:
3507        case OP_QUERYI:
3508        case OP_MINQUERYI:
3509        case OP_UPTOI:
3510        case OP_MINUPTOI:
3511        case OP_EXACTI:
3512        case OP_POSSTARI:
3513        case OP_POSPLUSI:
3514        case OP_POSQUERYI:
3515        case OP_POSUPTOI:
3516        case OP_NOTSTAR:
3517        case OP_NOTMINSTAR:
3518        case OP_NOTPLUS:
3519        case OP_NOTMINPLUS:
3520        case OP_NOTQUERY:
3521        case OP_NOTMINQUERY:
3522        case OP_NOTUPTO:
3523        case OP_NOTMINUPTO:
3524        case OP_NOTEXACT:
3525        case OP_NOTPOSSTAR:
3526        case OP_NOTPOSPLUS:
3527        case OP_NOTPOSQUERY:
3528        case OP_NOTPOSUPTO:
3529        case OP_NOTSTARI:
3530        case OP_NOTMINSTARI:
3531        case OP_NOTPLUSI:
3532        case OP_NOTMINPLUSI:
3533        case OP_NOTQUERYI:
3534        case OP_NOTMINQUERYI:
3535        case OP_NOTUPTOI:
3536        case OP_NOTMINUPTOI:
3537        case OP_NOTEXACTI:
3538        case OP_NOTPOSSTARI:
3539        case OP_NOTPOSPLUSI:
3540        case OP_NOTPOSQUERYI:
3541        case OP_NOTPOSUPTOI:
3542        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3543        break;
3544        }
3545    #else
3546      (void)(utf);  /* Keep compiler happy by referencing function argument */
3547    #endif
3548      }
3549    }
3550    
3551    
3552    
3553    /*************************************************
3554    *           Check for POSIX class syntax         *
3555    *************************************************/
3556    
3557    /* This function is called when the sequence "[:" or "[." or "[=" is
3558    encountered in a character class. It checks whether this is followed by a
3559    sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3560    reach an unescaped ']' without the special preceding character, return FALSE.
3561    
3562    Originally, this function only recognized a sequence of letters between the
3563    terminators, but it seems that Perl recognizes any sequence of characters,
3564    though of course unknown POSIX names are subsequently rejected. Perl gives an
3565    "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3566    didn't consider this to be a POSIX class. Likewise for [:1234:].
3567    
3568    The problem in trying to be exactly like Perl is in the handling of escapes. We
3569    have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3570    class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3571    below handles the special case of \], but does not try to do any other escape
3572    processing. This makes it different from Perl for cases such as [:l\ower:]
3573    where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
3574    "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
3575    I think.
3576    
3577    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3578    It seems that the appearance of a nested POSIX class supersedes an apparent
3579    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3580    a digit.
3581    
3582    In Perl, unescaped square brackets may also appear as part of class names. For
3583    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3584    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3585    seem right at all. PCRE does not allow closing square brackets in POSIX class
3586    names.
3587    
3588    Arguments:
3589      ptr      pointer to the initial [
3590      endptr   where to return the end pointer
3591    
3592    Returns:   TRUE or FALSE
3593    */
3594    
3595    static BOOL
3596    check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3597    {
3598    pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3599    terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3600    for (++ptr; *ptr != CHAR_NULL; ptr++)
3601      {
3602      if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3603        ptr++;
3604      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3605      else
3606        {
3607        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3608          {
3609          *endptr = ptr;
3610          return TRUE;
3611          }
3612        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3613             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3614              ptr[1] == CHAR_EQUALS_SIGN) &&
3615            check_posix_syntax(ptr, endptr))
3616          return FALSE;
3617        }
3618      }
3619    return FALSE;
3620    }
3621    
3622    
3623    
3624    
3625    /*************************************************
3626    *          Check POSIX class name                *
3627    *************************************************/
3628    
3629  Returns:             nothing  /* This function is called to check the name given in a POSIX-style class entry
3630    such as [:alnum:].
3631    
3632    Arguments:
3633      ptr        points to the first letter
3634      len        the length of the name
3635    
3636    Returns:     a value representing the name, or -1 if unknown
3637  */  */
3638    
3639  static void  static int
3640  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  check_posix_name(const pcre_uchar *ptr, int len)
3641  {  {
3642  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  const char *pn = posix_names;
3643  PUT(previous_callout, 2 + LINK_SIZE, length);  register int yield = 0;
3644    while (posix_name_lengths[yield] != 0)
3645      {
3646      if (len == posix_name_lengths[yield] &&
3647        STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3648      pn += posix_name_lengths[yield] + 1;
3649      yield++;
3650      }
3651    return -1;
3652  }  }
3653    
3654    
   
 #ifdef SUPPORT_UCP  
3655  /*************************************************  /*************************************************
3656  *           Get othercase range                  *  *    Adjust OP_RECURSE items in repeated group   *
3657  *************************************************/  *************************************************/
3658    
3659  /* This function is passed the start and end of a class range, in UTF-8 mode  /* OP_RECURSE items contain an offset from the start of the regex to the group
3660  with UCP support. It searches up the characters, looking for internal ranges of  that is referenced. This means that groups can be replicated for fixed
3661  characters in the "other" case. Each call returns the next one, updating the  repetition simply by copying (because the recursion is allowed to refer to
3662  start address.  earlier groups that are outside the current group). However, when a group is
3663    optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
3664    inserted before it, after it has been compiled. This means that any OP_RECURSE
3665    items within it that refer to the group itself or any contained groups have to
3666    have their offsets adjusted. That one of the jobs of this function. Before it
3667    is called, the partially compiled regex must be temporarily terminated with
3668    OP_END.
3669    
3670    This function has been extended with the possibility of forward references for
3671    recursions and subroutine calls. It must also check the list of such references
3672    for the group we are dealing with. If it finds that one of the recursions in
3673    the current group is on this list, it adjusts the offset in the list, not the
3674    value in the reference (which is a group number).
3675    
3676  Arguments:  Arguments:
3677    cptr        points to starting character value; updated    group      points to the start of the group
3678    d           end value    adjust     the amount by which the group is to be moved
3679    ocptr       where to put start of othercase range    utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3680    odptr       where to put end of othercase range    cd         contains pointers to tables etc.
3681      save_hwm   the hwm forward reference pointer at the start of the group
3682    
3683  Yield:        TRUE when range returned; FALSE when no more  Returns:     nothing
3684  */  */
3685    
3686  static BOOL  static void
3687  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
3688    unsigned int *odptr)    pcre_uchar *save_hwm)
3689  {  {
3690  unsigned int c, othercase, next;  pcre_uchar *ptr = group;
3691    
3692  for (c = *cptr; c <= d; c++)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
3693    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
3694      int offset;
3695      pcre_uchar *hc;
3696    
3697  if (c > d) return FALSE;    /* See if this recursion is on the forward reference list. If so, adjust the
3698      reference. */
3699    
3700  *ocptr = othercase;    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
3701  next = othercase + 1;      {
3702        offset = (int)GET(hc, 0);
3703        if (cd->start_code + offset == ptr + 1)
3704          {
3705          PUT(hc, 0, offset + adjust);
3706          break;
3707          }
3708        }
3709    
3710  for (++c; c <= d; c++)    /* Otherwise, adjust the recursion offset if it's after the start of this
3711    {    group. */
   if (UCD_OTHERCASE(c) != next) break;  
   next++;  
   }  
3712    
3713  *odptr = next - 1;    if (hc >= cd->hwm)
3714  *cptr = c;      {
3715        offset = (int)GET(ptr, 1);
3716        if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
3717        }
3718    
3719  return TRUE;    ptr += 1 + LINK_SIZE;
3720      }
3721  }  }
3722    
3723    
3724    
3725  /*************************************************  /*************************************************
3726  *        Check a character and a property        *  *        Insert an automatic callout point       *
3727  *************************************************/  *************************************************/
3728    
3729  /* This function is called by check_auto_possessive() when a property item  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
3730  is adjacent to a fixed character.  callout points before each pattern item.
3731    
3732  Arguments:  Arguments:
3733    c            the character    code           current code pointer
3734    ptype        the property type    ptr            current pattern pointer
3735    pdata        the data for the type    cd             pointers to tables etc
   negated      TRUE if it's a negated property (\P or \p{^)  
3736    
3737  Returns:       TRUE if auto-possessifying is OK  Returns:         new code pointer
3738  */  */
3739    
3740  static BOOL  static pcre_uchar *
3741  check_char_prop(int c, int ptype, int pdata, BOOL negated)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
3742  {  {
3743  const ucd_record *prop = GET_UCD(c);  *code++ = OP_CALLOUT;
3744  switch(ptype)  *code++ = 255;
3745    {  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
3746    case PT_LAMP:  PUT(code, LINK_SIZE, 0);                       /* Default length */
3747    return (prop->chartype == ucp_Lu ||  return code + 2 * LINK_SIZE;
3748            prop->chartype == ucp_Ll ||  }
           prop->chartype == ucp_Lt) == negated;  
   
   case PT_GC:  
   return (pdata == _pcre_ucp_gentype[prop->chartype]) == negated;  
3749    
   case PT_PC:  
   return (pdata == prop->chartype) == negated;  
3750    
   case PT_SC:  
   return (pdata == prop->script) == negated;  
3751    
3752    /* These are specials */  /*************************************************
3753    *         Complete a callout item                *
3754    *************************************************/
3755    
3756    case PT_ALNUM:  /* A callout item contains the length of the next item in the pattern, which
3757    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||  we can't fill in till after we have reached the relevant point. This is used
3758            _pcre_ucp_gentype[prop->chartype] == ucp_N) == negated;  for both automatic and manual callouts.
3759    
3760    case PT_SPACE:    /* Perl space */  Arguments:
3761    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    previous_callout   points to previous callout item
3762            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)    ptr                current pattern pointer
3763            == negated;    cd                 pointers to tables etc
3764    
3765    case PT_PXSPACE:  /* POSIX space */  Returns:             nothing
3766    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||  */
           c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||  
           c == CHAR_FF || c == CHAR_CR)  
           == negated;  
3767    
3768    case PT_WORD:  static void
3769    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
3770            _pcre_ucp_gentype[prop->chartype] == ucp_N ||  {
3771            c == CHAR_UNDERSCORE) == negated;  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
3772    }  PUT(previous_callout, 2 + LINK_SIZE, length);
 return FALSE;  
3773  }  }
 #endif  /* SUPPORT_UCP */  
3774    
3775    
3776    
3777    #ifdef SUPPORT_UCP
3778  /*************************************************  /*************************************************
3779  *     Check if auto-possessifying is possible    *  *           Get othercase range                  *
3780  *************************************************/  *************************************************/
3781    
3782  /* This function is called for unlimited repeats of certain items, to see  /* This function is passed the start and end of a class range, in UTF-8 mode
3783  whether the next thing could possibly match the repeated item. If not, it makes  with UCP support. It searches up the characters, looking for ranges of
3784  sense to automatically possessify the repeated item.  characters in the "other" case. Each call returns the next one, updating the
3785    start address. A character with multiple other cases is returned on its own
3786    with a special return value.
3787    
3788  Arguments:  Arguments:
3789    previous      pointer to the repeated opcode    cptr        points to starting character value; updated
3790    utf8          TRUE in UTF-8 mode    d           end value
3791    ptr           next character in pattern    ocptr       where to put start of othercase range
3792    options       options bits    odptr       where to put end of othercase range
   cd            contains pointers to tables etc.  
3793    
3794  Returns:        TRUE if possessifying is wanted  Yield:        -1 when no more
3795                   0 when a range is returned
3796                  >0 the CASESET offset for char with multiple other cases
3797                    in this case, ocptr contains the original
3798  */  */
3799    
3800  static BOOL  static int
3801  check_auto_possessive(const uschar *previous, BOOL utf8, const uschar *ptr,  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
3802    int options, compile_data *cd)    pcre_uint32 *odptr)
3803  {  {
3804  int c, next;  pcre_uint32 c, othercase, next;
3805  int op_code = *previous++;  unsigned int co;
3806    
3807  /* Skip whitespace and comments in extended mode */  /* Find the first character that has an other case. If it has multiple other
3808    cases, return its case offset value. */
3809    
3810  if ((options & PCRE_EXTENDED) != 0)  for (c = *cptr; c <= d; c++)
3811    {    {
3812    for (;;)    if ((co = UCD_CASESET(c)) != 0)
3813      {      {
3814      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      *ocptr = c++;   /* Character that has the set */
3815      if (*ptr == CHAR_NUMBER_SIGN)      *cptr = c;      /* Rest of input range */
3816        {      return (int)co;
       ptr++;  
       while (*ptr != 0)  
         {  
         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }  
         ptr++;  
 #ifdef SUPPORT_UTF8  
         if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;  
 #endif  
         }  
       }  
     else break;  
3817      }      }
3818      if ((othercase = UCD_OTHERCASE(c)) != c) break;
3819    }    }
3820    
3821  /* If the next item is one that we can handle, get its value. A non-negative  if (c > d) return -1;  /* Reached end of range */
 value is a character, a negative value is an escape value. */  
3822    
3823  if (*ptr == CHAR_BACKSLASH)  *ocptr = othercase;
3824    {  next = othercase + 1;
   int temperrorcode = 0;  
   next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);  
   if (temperrorcode != 0) return FALSE;  
   ptr++;    /* Point after the escape sequence */  
   }  
3825    
3826  else if ((cd->ctypes[*ptr] & ctype_meta) == 0)  for (++c; c <= d; c++)
3827    {    {
3828  #ifdef SUPPORT_UTF8    if (UCD_OTHERCASE(c) != next) break;
3829    if (utf8) { GETCHARINC(next, ptr); } else    next++;
 #endif  
   next = *ptr++;  
3830    }    }
3831    
3832  else return FALSE;  *odptr = next - 1;     /* End of othercase range */
3833    *cptr = c;             /* Rest of input range */
3834  /* Skip whitespace and comments in extended mode */  return 0;
3835    }
3836  if ((options & PCRE_EXTENDED) != 0)  #endif  /* SUPPORT_UCP */
   {  
   for (;;)  
     {  
     while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;  
     if (*ptr == CHAR_NUMBER_SIGN)  
       {  
       ptr++;  
       while (*ptr != 0)  
         {  
         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }  
         ptr++;  
 #ifdef SUPPORT_UTF8  
         if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;  
 #endif  
         }  
       }  
     else break;  
     }  
   }  
3837    
 /* If the next thing is itself optional, we have to give up. */  
3838    
 if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  
   strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)  
     return FALSE;  
3839    
3840  /* Now compare the next item with the previous opcode. First, handle cases when  /*************************************************
3841  the next item is a character. */  *        Add a character or range to a class     *
3842    *************************************************/
3843    
3844  if (next >= 0) switch(op_code)  /* This function packages up the logic of adding a character or range of
3845    {  characters to a class. The character values in the arguments will be within the
3846    case OP_CHAR:  valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3847  #ifdef SUPPORT_UTF8  mutually recursive with the function immediately below.
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   return c != next;  
3848    
3849    /* For CHARI (caseless character) we must check the other case. If we have  Arguments:
3850    Unicode property support, we can use it to test the other case of    classbits     the bit map for characters < 256
3851    high-valued characters. */    uchardptr     points to the pointer for extra data
3852      options       the options word
3853      cd            contains pointers to tables etc.
3854      start         start of range character
3855      end           end of range character
3856    
3857    case OP_CHARI:  Returns:        the number of < 256 characters added
3858  #ifdef SUPPORT_UTF8                  the pointer to extra data is updated
3859    GETCHARTEST(c, previous);  */
 #else  
   c = *previous;  
 #endif  
   if (c == next) return FALSE;  
 #ifdef SUPPORT_UTF8  
   if (utf8)  
     {  
     unsigned int othercase;  
     if (next < 128) othercase = cd->fcc[next]; else  
 #ifdef SUPPORT_UCP  
     othercase = UCD_OTHERCASE((unsigned int)next);  
 #else  
     othercase = NOTACHAR;  
 #endif  
     return (unsigned int)c != othercase;  
     }  
   else  
 #endif  /* SUPPORT_UTF8 */  
   return (c != cd->fcc[next]);  /* Non-UTF-8 mode */  
3860    
3861    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These  static int
3862    opcodes are not used for multi-byte characters, because they are coded using  add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3863    an XCLASS instead. */    compile_data *cd, pcre_uint32 start, pcre_uint32 end)
3864    {
3865    pcre_uint32 c;
3866    int n8 = 0;
3867    
3868    case OP_NOT:  /* If caseless matching is required, scan the range and process alternate
3869    return (c = *previous) == next;  cases. In Unicode, there are 8-bit characters that have alternate cases that
3870    are greater than 255 and vice-versa. Sometimes we can just extend the original
3871    range. */
3872    
3873    case OP_NOTI:  if ((options & PCRE_CASELESS) != 0)
3874    if ((c = *previous) == next) return TRUE;    {
 #ifdef SUPPORT_UTF8  
   if (utf8)  
     {  
     unsigned int othercase;  
     if (next < 128) othercase = cd->fcc[next]; else  
3875  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3876      othercase = UCD_OTHERCASE(next);    if ((options & PCRE_UTF8) != 0)
3877  #else      {
3878      othercase = NOTACHAR;      int rc;
3879  #endif      pcre_uint32 oc, od;
     return (unsigned int)c == othercase;  
     }  
   else  
 #endif  /* SUPPORT_UTF8 */  
   return (c == cd->fcc[next]);  /* Non-UTF-8 mode */  
   
   /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.  
   When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */  
3880    
3881    case OP_DIGIT:      options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3882    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;      c = start;
3883    
3884    case OP_NOT_DIGIT:      while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3885    return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;        {
3886          /* Handle a single character that has more than one other case. */
3887    
3888    case OP_WHITESPACE:        if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3889    return next > 127 || (cd->ctypes[next] & ctype_space) == 0;          PRIV(ucd_caseless_sets) + rc, oc);
3890    
3891    case OP_NOT_WHITESPACE:        /* Do nothing if the other case range is within the original range. */
   return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;  
3892    
3893    case OP_WORDCHAR:        else if (oc >= start && od <= end) continue;
   return next > 127 || (cd->ctypes[next] & ctype_word) == 0;  
3894    
3895    case OP_NOT_WORDCHAR:        /* Extend the original range if there is overlap, noting that if oc < c, we
3896    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;        can't have od > end because a subrange is always shorter than the basic
3897          range. Otherwise, use a recursive call to add the additional range. */
3898    
3899    case OP_HSPACE:        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3900    case OP_NOT_HSPACE:        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3901    switch(next)        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3902      {        }
     case 0x09:  
     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:  
     return op_code == OP_NOT_HSPACE;  
     default:  
     return op_code != OP_NOT_HSPACE;  
3903      }      }
3904      else
3905    #endif  /* SUPPORT_UCP */
3906    
3907    case OP_ANYNL:    /* Not UTF-mode, or no UCP */
3908    case OP_VSPACE:  
3909    case OP_NOT_VSPACE:    for (c = start; c <= end && c < 256; c++)
   switch(next)  
3910      {      {
3911      case 0x0a:      SETBIT(classbits, cd->fcc[c]);
3912      case 0x0b:      n8++;
     case 0x0c:  
     case 0x0d:  
     case 0x85:  
     case 0x2028:  
     case 0x2029:  
     return op_code == OP_NOT_VSPACE;  
     default:  
     return op_code != OP_NOT_VSPACE;  
3913      }      }
3914      }
3915    
3916  #ifdef SUPPORT_UCP  /* Now handle the original range. Adjust the final value according to the bit
3917    case OP_PROP:  length - this means that the same lists of (e.g.) horizontal spaces can be used
3918    return check_char_prop(next, previous[0], previous[1], FALSE);  in all cases. */
3919    
3920    case OP_NOTPROP:  #if defined COMPILE_PCRE8
3921    return check_char_prop(next, previous[0], previous[1], TRUE);  #ifdef SUPPORT_UTF
3922      if ((options & PCRE_UTF8) == 0)
3923  #endif  #endif
3924      if (end > 0xff) end = 0xff;
3925    
3926    default:  #elif defined COMPILE_PCRE16
3927    return FALSE;  #ifdef SUPPORT_UTF
3928    }    if ((options & PCRE_UTF16) == 0)
3929    #endif
3930      if (end > 0xffff) end = 0xffff;
3931    
3932    #endif /* COMPILE_PCRE[8|16] */
3933    
3934  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  /* If all characters are less than 256, use the bit map. Otherwise use extra
3935  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  data. */
 generated only when PCRE_UCP is *not* set, that is, when only ASCII  
 characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are  
 replaced by OP_PROP codes when PCRE_UCP is set. */  
3936    
3937  switch(op_code)  if (end < 0x100)
3938    {    {
3939    case OP_CHAR:    for (c = start; c <= end; c++)
   case OP_CHARI:  
 #ifdef SUPPORT_UTF8  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   switch(-next)  
3940      {      {
3941      case ESC_d:      n8++;
3942      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;      SETBIT(classbits, c);
3943        }
3944      case ESC_D:    }
     return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;  
   
     case ESC_s:  
     return c > 127 || (cd->ctypes[c] & ctype_space) == 0;  
   
     case ESC_S:  
     return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;  
   
     case ESC_w:  
     return c > 127 || (cd->ctypes[c] & ctype_word) == 0;  
3945    
3946      case ESC_W:  else
3947      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;    {
3948      pcre_uchar *uchardata = *uchardptr;
3949    
3950      case ESC_h:  #ifdef SUPPORT_UTF
3951      case ESC_H:    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3952      switch(c)      {
3953        if (start < end)
3954        {        {
3955        case 0x09:        *uchardata++ = XCL_RANGE;
3956        case 0x20:        uchardata += PRIV(ord2utf)(start, uchardata);
3957        case 0xa0:        uchardata += PRIV(ord2utf)(end, uchardata);
       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:  
       return -next != ESC_h;  
       default:  
       return -next == ESC_h;  
3958        }        }
3959        else if (start == end)
     case ESC_v:  
     case ESC_V:  
     switch(c)  
3960        {        {
3961        case 0x0a:        *uchardata++ = XCL_SINGLE;
3962        case 0x0b:        uchardata += PRIV(ord2utf)(start, uchardata);
       case 0x0c:  
       case 0x0d:  
       case 0x85:  
       case 0x2028:  
       case 0x2029:  
       return -next != ESC_v;  
       default:  
       return -next == ESC_v;  
       }  
   
     /* When PCRE_UCP is set, these values get generated for \d etc. Find  
     their substitutions and process them. The result will always be either  
     -ESC_p or -ESC_P. Then fall through to process those values. */  
   
 #ifdef SUPPORT_UCP  
     case ESC_du:  
     case ESC_DU:  
     case ESC_wu:  
     case ESC_WU:  
     case ESC_su:  
     case ESC_SU:  
       {  
       int temperrorcode = 0;  
       ptr = substitutes[-next - ESC_DU];  
       next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);  
       if (temperrorcode != 0) return FALSE;  
      &nbs