/[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 496 by ph10, Tue Mar 2 19:11:17 2010 UTC revision 1363 by ph10, Tue Oct 1 16:54:40 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-2010 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. */  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
109    /(?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
111    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()
113    kicks in at the same number of forward references in all cases. */
114    
115    #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116    #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 COMPILE_WORK_SIZE (4096)  #define NAMED_GROUP_LIST_SIZE  20
124    
125    /* 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. */
127    
128    #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 119  static const short int escapes[] = { Line 165  static const short int escapes[] = {
165       -ESC_H,                  0,       -ESC_H,                  0,
166       0,                       -ESC_K,       0,                       -ESC_K,
167       0,                       0,       0,                       0,
168       0,                       0,       -ESC_N,                  0,
169       -ESC_P,                  -ESC_Q,       -ESC_P,                  -ESC_Q,
170       -ESC_R,                  -ESC_S,       -ESC_R,                  -ESC_S,
171       0,                       0,       0,                       0,
# Line 166  static const short int escapes[] = { Line 212  static const short int escapes[] = {
212  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',
213  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,
214  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,
215  /*  D0 */   '}',     0, -ESC_K,       0,      0,     0,      0, -ESC_P,  /*  D0 */   '}',     0, -ESC_K,       0,      0,-ESC_N,      0, -ESC_P,
216  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,
217  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,
218  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,
# Line 183  string is built from string macros so th Line 229  string is built from string macros so th
229  platforms. */  platforms. */
230    
231  typedef struct verbitem {  typedef struct verbitem {
232    int   len;    int   len;                 /* Length of verb name */
233    int   op;    int   op;                  /* Op when no arg, or -1 if arg mandatory */
234      int   op_arg;              /* Op when arg present, or -1 if not allowed */
235  } verbitem;  } verbitem;
236    
237  static const char verbnames[] =  static const char verbnames[] =
238      "\0"                       /* Empty name is a shorthand for MARK */
239      STRING_MARK0
240    STRING_ACCEPT0    STRING_ACCEPT0
241    STRING_COMMIT0    STRING_COMMIT0
242    STRING_F0    STRING_F0
# Line 197  static const char verbnames[] = Line 246  static const char verbnames[] =
246    STRING_THEN;    STRING_THEN;
247    
248  static const verbitem verbs[] = {  static const verbitem verbs[] = {
249    { 6, OP_ACCEPT },    { 0, -1,        OP_MARK },
250    { 6, OP_COMMIT },    { 4, -1,        OP_MARK },
251    { 1, OP_FAIL },    { 6, OP_ACCEPT, -1 },
252    { 4, OP_FAIL },    { 6, OP_COMMIT, -1 },
253    { 5, OP_PRUNE },    { 1, OP_FAIL,   -1 },
254    { 4, OP_SKIP  },    { 4, OP_FAIL,   -1 },
255    { 4, OP_THEN  }    { 5, OP_PRUNE,  OP_PRUNE_ARG },
256      { 4, OP_SKIP,   OP_SKIP_ARG  },
257      { 4, OP_THEN,   OP_THEN_ARG  }
258  };  };
259    
260  static const int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
# Line 221  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 251  static const int posix_class_maps[] = { Line 302  static const int posix_class_maps[] = {
302    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
303  };  };
304    
305    /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
306    substitutes must be in the order of the names, defined above, and there are
307    both positive and negative cases. NULL means no substitute. */
308    
309    #ifdef SUPPORT_UCP
310    static const pcre_uchar string_PNd[]  = {
311      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
312      CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
313    static const pcre_uchar string_pNd[]  = {
314      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
315      CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
316    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 pcre_uchar string_pL[] =   {
339      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
340      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
341    static const pcre_uchar string_pLl[] =  {
342      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
343      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
344    static const pcre_uchar string_pLu[] =  {
345      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
346      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
347    static const pcre_uchar string_pXan[] = {
348      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
349      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
350    static const pcre_uchar string_h[] =    {
351      CHAR_BACKSLASH, CHAR_h, '\0' };
352    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 */
389      string_PL,            /* ^alpha */
390      string_PLl,           /* ^lower */
391      string_PLu,           /* ^upper */
392      string_PXan,          /* ^alnum */
393      NULL,                 /* ^ascii */
394      string_H,             /* ^blank */
395      NULL,                 /* ^cntrl */
396      string_PNd,           /* ^digit */
397      NULL,                 /* ^graph */
398      NULL,                 /* ^print */
399      NULL,                 /* ^punct */
400      string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
401      string_PXwd,          /* ^word */
402      NULL                  /* ^xdigit */
403    };
404    #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
405    #endif
406    
407  #define STRING(a)  # a  #define STRING(a)  # a
408  #define XSTRING(s) STRING(s)  #define XSTRING(s) STRING(s)
# Line 263  the number of relocations needed when a Line 415  the number of relocations needed when a
415  it is now one long string. We cannot use a table of offsets, because the  it is now one long string. We cannot use a table of offsets, because the
416  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
417  simply count through to the one we want - this isn't a performance issue  simply count through to the one we want - this isn't a performance issue
418  because these strings are used only when there is a compilation error. */  because these strings are used only when there is a compilation error.
419    
420    Each substring ends with \0 to insert a null character. This includes the final
421    substring, so that the whole string ends with \0\0, which can be detected when
422    counting through. */
423    
424  static const char error_texts[] =  static const char error_texts[] =
425    "no error\0"    "no error\0"
# Line 304  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{...} sequence 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"
469    "PCRE does not support \\L, \\l, \\N, \\U, or \\u\0"    "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
470    "number after (?C is > 255\0"    "number after (?C is > 255\0"
471    "closing ) for (?C expected\0"    "closing ) for (?C expected\0"
472    /* 40 */    /* 40 */
# Line 327  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"
490    /* 55 */    /* 55 */
491    "repeating a DEFINE group is not allowed\0"    "repeating a DEFINE group is not allowed\0"  /** DEAD **/
492    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
493    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
494    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
495    "(*VERB) with an argument is not supported\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"
501    "] is an invalid data character in JavaScript compatibility mode\0"    "] is an invalid data character in JavaScript compatibility mode\0"
502    /* 65 */    /* 65 */
503    "different names for subpatterns of the same number are not allowed";    "different names for subpatterns of the same number are not allowed\0"
504      "(*MARK) must have an argument\0"
505      "this version of PCRE is not compiled with Unicode property support\0"
506      "\\c must be followed by an ASCII character\0"
507      "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
508      /* 70 */
509      "internal error: unknown opcode in find_fixedlength()\0"
510      "\\N is not supported in a class\0"
511      "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      ;
520    
521  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
522  patterns. Note that the tables in chartables are dependent on the locale, and  patterns. Note that the tables in chartables are dependent on the locale, and
# Line 363  For convenience, we use the same bit def Line 534  For convenience, we use the same bit def
534    
535  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
536    
537    /* Using a simple comparison for decimal numbers rather than a memory read
538    is much faster, and the resulting code is simpler (the compiler turns it
539    into a subtraction and unsigned comparison). */
540    
541    #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
542    
543  #ifndef EBCDIC  #ifndef EBCDIC
544    
545  /* 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
546  UTF-8 mode. */  UTF-8 mode. */
547    
548  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
549    {    {
550    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
551    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */
# Line 407  static const unsigned char digitab[] = Line 584  static const unsigned char digitab[] =
584    
585  /* 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. */
586    
587  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
588    {    {
589    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
590    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */
# Line 442  static const unsigned char digitab[] = Line 619  static const unsigned char digitab[] =
619    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */
620    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */
621    
622  static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */  static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
623    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */
624    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */
625    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */
# Line 478  static const unsigned char ebcdic_charta Line 655  static const unsigned char ebcdic_charta
655  #endif  #endif
656    
657    
658  /* Definition to allow mutual recursion */  /* This table is used to check whether auto-possessification is possible
659    between adjacent character-type opcodes. The left-hand (repeated) opcode is
660    used to select the row, and the right-hand opcode is use to select the column.
661    A value of 1 means that auto-possessification is OK. For example, the second
662    value in the first row means that \D+\d can be turned into \D++\d.
663    
664    The Unicode property types (\P and \p) have to be present to fill out the table
665    because of what their opcode values are, but the table values should always be
666    zero because property types are handled separately in the code. The last four
667    columns apply to items that cannot be repeated, so there is no need to have
668    rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
669    *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
670    
671    #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
672    #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
673    
674    static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
675    /* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
676      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
677      { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
678      { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
679      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
680      { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
681      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
682      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
683      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
684      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
685      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
686      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
687      { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
688      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
689      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
690      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
691      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
692      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
693    };
694    
695  static BOOL  
696    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,  /* This table is used to check whether auto-possessification is possible
697      int *, int *, branch_chain *, compile_data *, int *);  between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
698    left-hand (repeated) opcode is used to select the row, and the right-hand
699    opcode is used to select the column. The values are as follows:
700    
701      0   Always return FALSE (never auto-possessify)
702      1   Character groups are distinct (possessify if both are OP_PROP)
703      2   Check character categories in the same group (general or particular)
704      3   TRUE if the two opcodes are not the same (PROP vs NOTPROP)
705    
706      4   Check left general category vs right particular category
707      5   Check right general category vs left particular category
708    
709      6   Left alphanum vs right general category
710      7   Left space vs right general category
711      8   Left word vs right general category
712    
713      9   Right alphanum vs left general category
714     10   Right space vs left general category
715     11   Right word vs left general category
716    
717     12   Left alphanum vs right particular category
718     13   Left space vs right particular category
719     14   Left word vs right particular category
720    
721     15   Right alphanum vs left particular category
722     16   Right space vs left particular category
723     17   Right word vs left particular category
724    */
725    
726    static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
727    /* ANY LAMP GC  PC  SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
728      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_ANY */
729      { 0,  3,  0,  0,  0,    3,    1,      1,   0,    0,   0 },  /* PT_LAMP */
730      { 0,  0,  2,  4,  0,    9,   10,     10,  11,    0,   0 },  /* PT_GC */
731      { 0,  0,  5,  2,  0,   15,   16,     16,  17,    0,   0 },  /* PT_PC */
732      { 0,  0,  0,  0,  2,    0,    0,      0,   0,    0,   0 },  /* PT_SC */
733      { 0,  3,  6, 12,  0,    3,    1,      1,   0,    0,   0 },  /* PT_ALNUM */
734      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_SPACE */
735      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_PXSPACE */
736      { 0,  0,  8, 14,  0,    0,    1,      1,   3,    0,   0 },  /* PT_WORD */
737      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_CLIST */
738      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   3 }   /* PT_UCNC */
739    };
740    
741    /* This table is used to check whether auto-possessification is possible
742    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
743    specifies a general category and the other specifies a particular category. The
744    row is selected by the general category and the column by the particular
745    category. The value is 1 if the particular category is not part of the general
746    category. */
747    
748    static const pcre_uint8 catposstab[7][30] = {
749    /* 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 */
750      { 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 */
751      { 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 */
752      { 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 */
753      { 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 */
754      { 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 */
755      { 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 */
756      { 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 */
757    };
758    
759    /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
760    a general or particular category. The properties in each row are those
761    that apply to the character set in question. Duplication means that a little
762    unnecessary work is done when checking, but this keeps things much simpler
763    because they can all use the same code. For more details see the comment where
764    this table is used.
765    
766    Note: SPACE and PXSPACE used to be different because Perl excluded VT from
767    "space", but from Perl 5.18 it's included, so both categories are treated the
768    same here. */
769    
770    static const pcre_uint8 posspropstab[3][4] = {
771      { ucp_L, ucp_N, ucp_N, ucp_Nl },  /* ALNUM, 3rd and 4th values redundant */
772      { ucp_Z, ucp_Z, ucp_C, ucp_Cc },  /* SPACE and PXSPACE, 2nd value redundant */
773      { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
774    };
775    
776    
777    
# Line 503  static const char * Line 792  static const char *
792  find_error_text(int n)  find_error_text(int n)
793  {  {
794  const char *s = error_texts;  const char *s = error_texts;
795  for (; n > 0; n--) while (*s++ != 0) {};  for (; n > 0; n--)
796      {
797      while (*s++ != CHAR_NULL) {};
798      if (*s == CHAR_NULL) return "Error text not found (please report)";
799      }
800  return s;  return s;
801  }  }
802    
803    
804    
805    /*************************************************
806    *           Expand the workspace                 *
807    *************************************************/
808    
809    /* This function is called during the second compiling phase, if the number of
810    forward references fills the existing workspace, which is originally a block on
811    the stack. A larger block is obtained from malloc() unless the ultimate limit
812    has been reached or the increase will be rather small.
813    
814    Argument: pointer to the compile data block
815    Returns:  0 if all went well, else an error number
816    */
817    
818    static int
819    expand_workspace(compile_data *cd)
820    {
821    pcre_uchar *newspace;
822    int newsize = cd->workspace_size * 2;
823    
824    if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
825    if (cd->workspace_size >= COMPILE_WORK_SIZE_MAX ||
826        newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
827     return ERR72;
828    
829    newspace = (PUBL(malloc))(IN_UCHARS(newsize));
830    if (newspace == NULL) return ERR21;
831    memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
832    cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
833    if (cd->workspace_size > COMPILE_WORK_SIZE)
834      (PUBL(free))((void *)cd->start_workspace);
835    cd->start_workspace = newspace;
836    cd->workspace_size = newsize;
837    return 0;
838    }
839    
840    
841    
842    /*************************************************
843    *            Check for counted repeat            *
844    *************************************************/
845    
846    /* This function is called when a '{' is encountered in a place where it might
847    start a quantifier. It looks ahead to see if it really is a quantifier or not.
848    It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
849    where the ddds are digits.
850    
851    Arguments:
852      p         pointer to the first char after '{'
853    
854    Returns:    TRUE or FALSE
855    */
856    
857    static BOOL
858    is_counted_repeat(const pcre_uchar *p)
859    {
860    if (!IS_DIGIT(*p)) return FALSE;
861    p++;
862    while (IS_DIGIT(*p)) p++;
863    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
864    
865    if (*p++ != CHAR_COMMA) return FALSE;
866    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
867    
868    if (!IS_DIGIT(*p)) return FALSE;
869    p++;
870    while (IS_DIGIT(*p)) p++;
871    
872    return (*p == CHAR_RIGHT_CURLY_BRACKET);
873    }
874    
875    
876    
877  /*************************************************  /*************************************************
878  *            Handle escapes                      *  *            Handle escapes                      *
879  *************************************************/  *************************************************/
880    
881  /* 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
882  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
883  encodes one of the more complicated things such as \d. A backreference to group  which will be placed in chptr. A backreference to group n is returned as
884  n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When  negative n. When UTF-8 is enabled, a positive value greater than 255 may
885  UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,  be returned in chptr.
886  ptr is pointing at the \. On exit, it is on the final character of the escape  On entry,ptr is pointing at the \. On exit, it is on the final character of the
887  sequence.  escape sequence.
888    
889  Arguments:  Arguments:
890    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
891      chptr          points to the data character
892    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
893    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
894    options        the options bits    options        the options bits
895    isclass        TRUE if inside a character class    isclass        TRUE if inside a character class
896    
897  Returns:         zero or positive => a data character  Returns:         zero => a data character
898                   negative => a special escape sequence                   positive => a special escape sequence
899                     negative => a back reference
900                   on error, errorcodeptr is set                   on error, errorcodeptr is set
901  */  */
902    
903  static int  static int
904  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
905    int options, BOOL isclass)    int bracount, int options, BOOL isclass)
906  {  {
907  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
908  const uschar *ptr = *ptrptr + 1;  BOOL utf = (options & PCRE_UTF8) != 0;
909  int c, i;  const pcre_uchar *ptr = *ptrptr + 1;
910    pcre_uint32 c;
911    int escape = 0;
912    int i;
913    
914  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
915  ptr--;                            /* Set pointer back to the last byte */  ptr--;                            /* Set pointer back to the last byte */
916    
917  /* 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. */
918    
919  if (c == 0) *errorcodeptr = ERR1;  if (c == CHAR_NULL) *errorcodeptr = ERR1;
920    
921  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
922  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.
923  Otherwise further processing may be required. */  Otherwise further processing may be required. */
924    
925  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
926  else if (c < CHAR_0 || c > CHAR_z) {}                     /* Not alphanumeric */  /* Not alphanumeric */
927  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if (c < CHAR_0 || c > CHAR_z) {}
928    else if ((i = escapes[c - CHAR_0]) != 0)
929      { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
930    
931  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
932  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */  /* Not alphanumeric */
933  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
934    else if ((i = escapes[c - 0x48]) != 0)  { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
935  #endif  #endif
936    
937  /* Escapes that need further processing, or are illegal. */  /* Escapes that need further processing, or are illegal. */
938    
939  else  else
940    {    {
941    const uschar *oldptr;    const pcre_uchar *oldptr;
942    BOOL braced, negated;    BOOL braced, negated, overflow;
943      int s;
944    
945    switch (c)    switch (c)
946      {      {
# Line 574  else Line 949  else
949    
950      case CHAR_l:      case CHAR_l:
951      case CHAR_L:      case CHAR_L:
952      case CHAR_N:      *errorcodeptr = ERR37;
953        break;
954    
955      case CHAR_u:      case CHAR_u:
956        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
957          {
958          /* In JavaScript, \u must be followed by four hexadecimal numbers.
959          Otherwise it is a lowercase u letter. */
960          if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
961            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
962            && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
963            && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
964            {
965            c = 0;
966            for (i = 0; i < 4; ++i)
967              {
968              register pcre_uint32 cc = *(++ptr);
969    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
970              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
971              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
972    #else           /* EBCDIC coding */
973              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
974              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
975    #endif
976              }
977    
978    #if defined COMPILE_PCRE8
979            if (c > (utf ? 0x10ffffU : 0xffU))
980    #elif defined COMPILE_PCRE16
981            if (c > (utf ? 0x10ffffU : 0xffffU))
982    #elif defined COMPILE_PCRE32
983            if (utf && c > 0x10ffffU)
984    #endif
985              {
986              *errorcodeptr = ERR76;
987              }
988            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
989            }
990          }
991        else
992          *errorcodeptr = ERR37;
993        break;
994    
995      case CHAR_U:      case CHAR_U:
996      *errorcodeptr = ERR37;      /* In JavaScript, \U is an uppercase U letter. */
997        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
998      break;      break;
999    
1000      /* \g must be followed by one of a number of specific things:      /* In a character class, \g is just a literal "g". Outside a character
1001        class, \g must be followed by one of a number of specific things:
1002    
1003      (1) A number, either plain or braced. If positive, it is an absolute      (1) A number, either plain or braced. If positive, it is an absolute
1004      backreference. If negative, it is a relative backreference. This is a Perl      backreference. If negative, it is a relative backreference. This is a Perl
# Line 594  else Line 1012  else
1012      (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
1013      number either in angle brackets or in single quotes. However, these are      number either in angle brackets or in single quotes. However, these are
1014      (possibly recursive) subroutine calls, _not_ backreferences. Just return      (possibly recursive) subroutine calls, _not_ backreferences. Just return
1015      the -ESC_g code (cf \k). */      the ESC_g code (cf \k). */
1016    
1017      case CHAR_g:      case CHAR_g:
1018        if (isclass) break;
1019      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
1020        {        {
1021        c = -ESC_g;        escape = ESC_g;
1022        break;        break;
1023        }        }
1024    
# Line 607  else Line 1026  else
1026    
1027      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1028        {        {
1029        const uschar *p;        const pcre_uchar *p;
1030        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++)
1031          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1032        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1033          {          {
1034          c = -ESC_k;          escape = ESC_k;
1035          break;          break;
1036          }          }
1037        braced = TRUE;        braced = TRUE;
# Line 627  else Line 1046  else
1046        }        }
1047      else negated = FALSE;      else negated = FALSE;
1048    
1049      c = 0;      /* The integer range is limited by the machine's int representation. */
1050      while ((digitab[ptr[1]] & ctype_digit) != 0)      s = 0;
1051        c = c * 10 + *(++ptr) - CHAR_0;      overflow = FALSE;
1052        while (IS_DIGIT(ptr[1]))
1053      if (c < 0)   /* Integer overflow */        {
1054          if (s > INT_MAX / 10 - 1) /* Integer overflow */
1055            {
1056            overflow = TRUE;
1057            break;
1058            }
1059          s = s * 10 + (int)(*(++ptr) - CHAR_0);
1060          }
1061        if (overflow) /* Integer overflow */
1062        {        {
1063          while (IS_DIGIT(ptr[1]))
1064            ptr++;
1065        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
1066        break;        break;
1067        }        }
# Line 643  else Line 1072  else
1072        break;        break;
1073        }        }
1074    
1075      if (c == 0)      if (s == 0)
1076        {        {
1077        *errorcodeptr = ERR58;        *errorcodeptr = ERR58;
1078        break;        break;
# Line 651  else Line 1080  else
1080    
1081      if (negated)      if (negated)
1082        {        {
1083        if (c > bracount)        if (s > bracount)
1084          {          {
1085          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
1086          break;          break;
1087          }          }
1088        c = bracount - (c - 1);        s = bracount - (s - 1);
1089        }        }
1090    
1091      c = -(ESC_REF + c);      escape = -s;
1092      break;      break;
1093    
1094      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
# Line 680  else Line 1109  else
1109      if (!isclass)      if (!isclass)
1110        {        {
1111        oldptr = ptr;        oldptr = ptr;
1112        c -= CHAR_0;        /* The integer range is limited by the machine's int representation. */
1113        while ((digitab[ptr[1]] & ctype_digit) != 0)        s = (int)(c -CHAR_0);
1114          c = c * 10 + *(++ptr) - CHAR_0;        overflow = FALSE;
1115        if (c < 0)    /* Integer overflow */        while (IS_DIGIT(ptr[1]))
1116            {
1117            if (s > INT_MAX / 10 - 1) /* Integer overflow */
1118              {
1119              overflow = TRUE;
1120              break;
1121              }
1122            s = s * 10 + (int)(*(++ptr) - CHAR_0);
1123            }
1124          if (overflow) /* Integer overflow */
1125          {          {
1126            while (IS_DIGIT(ptr[1]))
1127              ptr++;
1128          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1129          break;          break;
1130          }          }
1131        if (c < 10 || c <= bracount)        if (s < 10 || s <= bracount)
1132          {          {
1133          c = -(ESC_REF + c);          escape = -s;
1134          break;          break;
1135          }          }
1136        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
# Line 710  else Line 1150  else
1150      /* \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
1151      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
1152      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
1153      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,
1154      than 3 octal digits. */      but no more than 3 octal digits. */
1155    
1156      case CHAR_0:      case CHAR_0:
1157      c -= CHAR_0;      c -= CHAR_0;
1158      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1159          c = c * 8 + *(++ptr) - CHAR_0;          c = c * 8 + *(++ptr) - CHAR_0;
1160      if (!utf8 && c > 255) *errorcodeptr = ERR51;  #ifdef COMPILE_PCRE8
1161        if (!utf && c > 0xff) *errorcodeptr = ERR51;
1162    #endif
1163      break;      break;
1164    
1165      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \x is complicated. \x{ddd} is a character number which can be greater
1166      than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is      than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.
1167      treated as a data character. */      If not, { is treated as a data character. */
1168    
1169      case CHAR_x:      case CHAR_x:
1170        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1171          {
1172          /* In JavaScript, \x must be followed by two hexadecimal numbers.
1173          Otherwise it is a lowercase x letter. */
1174          if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1175            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1176            {
1177            c = 0;
1178            for (i = 0; i < 2; ++i)
1179              {
1180              register pcre_uint32 cc = *(++ptr);
1181    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1182              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1183              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1184    #else           /* EBCDIC coding */
1185              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
1186              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1187    #endif
1188              }
1189            }
1190          break;
1191          }
1192    
1193      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1194        {        {
1195        const uschar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
       int count = 0;  
1196    
1197        c = 0;        c = 0;
1198        while ((digitab[*pt] & ctype_xdigit) != 0)        overflow = FALSE;
1199          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
1200          {          {
1201          register int cc = *pt++;          register pcre_uint32 cc = *pt++;
1202          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1203          count++;  
1204    #ifdef COMPILE_PCRE32
1205            if (c >= 0x10000000l) { overflow = TRUE; break; }
1206    #endif
1207    
1208  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1209          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
# Line 744  else Line 1212  else
1212          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 */
1213          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1214  #endif  #endif
1215    
1216    #if defined COMPILE_PCRE8
1217            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1218    #elif defined COMPILE_PCRE16
1219            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1220    #elif defined COMPILE_PCRE32
1221            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1222    #endif
1223            }
1224    
1225          if (overflow)
1226            {
1227            while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1228            *errorcodeptr = ERR34;
1229          }          }
1230    
1231        if (*pt == CHAR_RIGHT_CURLY_BRACKET)        if (*pt == CHAR_RIGHT_CURLY_BRACKET)
1232          {          {
1233          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1234          ptr = pt;          ptr = pt;
1235          break;          break;
1236          }          }
# Line 760  else Line 1242  else
1242      /* Read just a single-byte hex-defined char */      /* Read just a single-byte hex-defined char */
1243    
1244      c = 0;      c = 0;
1245      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1246        {        {
1247        int cc;                                  /* Some compilers don't like */        pcre_uint32 cc;                          /* Some compilers don't like */
1248        cc = *(++ptr);                           /* ++ in initializers */        cc = *(++ptr);                           /* ++ in initializers */
1249  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1250        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
# Line 775  else Line 1257  else
1257      break;      break;
1258    
1259      /* 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.
1260      This coding is ASCII-specific, but then the whole concept of \cx is      An error is given if the byte following \c is not an ASCII character. This
1261        coding is ASCII-specific, but then the whole concept of \cx is
1262      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1263    
1264      case CHAR_c:      case CHAR_c:
1265      c = *(++ptr);      c = *(++ptr);
1266      if (c == 0)      if (c == CHAR_NULL)
1267        {        {
1268        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1269        break;        break;
1270        }        }
1271    #ifndef EBCDIC    /* ASCII/UTF-8 coding */
1272  #ifndef EBCDIC  /* ASCII/UTF-8 coding */      if (c > 127)  /* Excludes all non-ASCII in either mode */
1273          {
1274          *errorcodeptr = ERR68;
1275          break;
1276          }
1277      if (c >= CHAR_a && c <= CHAR_z) c -= 32;      if (c >= CHAR_a && c <= CHAR_z) c -= 32;
1278      c ^= 0x40;      c ^= 0x40;
1279  #else           /* EBCDIC coding */  #else             /* EBCDIC coding */
1280      if (c >= CHAR_a && c <= CHAR_z) c += 64;      if (c >= CHAR_a && c <= CHAR_z) c += 64;
1281      c ^= 0xC0;      c ^= 0xC0;
1282  #endif  #endif
# Line 812  else Line 1299  else
1299      }      }
1300    }    }
1301    
1302    /* Perl supports \N{name} for character names, as well as plain \N for "not
1303    newline". PCRE does not support \N{name}. However, it does support
1304    quantification such as \N{2,3}. */
1305    
1306    if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1307         !is_counted_repeat(ptr+2))
1308      *errorcodeptr = ERR37;
1309    
1310    /* If PCRE_UCP is set, we change the values for \d etc. */
1311    
1312    if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
1313      escape += (ESC_DU - ESC_D);
1314    
1315    /* Set the pointer to the final character before returning. */
1316    
1317  *ptrptr = ptr;  *ptrptr = ptr;
1318  return c;  *chptr = c;
1319    return escape;
1320  }  }
1321    
1322    
# Line 831  escape sequence. Line 1334  escape sequence.
1334  Argument:  Argument:
1335    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
1336    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
1337    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
1338      pdataptr       points to an unsigned int that is set to the detailed property value
1339    errorcodeptr   points to the error code variable    errorcodeptr   points to the error code variable
1340    
1341  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
1342  */  */
1343    
1344  static int  static BOOL
1345  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1346      unsigned int *pdataptr, int *errorcodeptr)
1347  {  {
1348  int c, i, bot, top;  pcre_uchar c;
1349  const uschar *ptr = *ptrptr;  int i, bot, top;
1350  char name[32];  const pcre_uchar *ptr = *ptrptr;
1351    pcre_uchar name[32];
1352    
1353  c = *(++ptr);  c = *(++ptr);
1354  if (c == 0) goto ERROR_RETURN;  if (c == CHAR_NULL) goto ERROR_RETURN;
1355    
1356  *negptr = FALSE;  *negptr = FALSE;
1357    
# Line 859  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1365  if (c == CHAR_LEFT_CURLY_BRACKET)
1365      *negptr = TRUE;      *negptr = TRUE;
1366      ptr++;      ptr++;
1367      }      }
1368    for (i = 0; i < (int)sizeof(name) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1369      {      {
1370      c = *(++ptr);      c = *(++ptr);
1371      if (c == 0) goto ERROR_RETURN;      if (c == CHAR_NULL) goto ERROR_RETURN;
1372      if (c == CHAR_RIGHT_CURLY_BRACKET) break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1373      name[i] = c;      name[i] = c;
1374      }      }
# Line 883  else Line 1389  else
1389  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1390    
1391  bot = 0;  bot = 0;
1392  top = _pcre_utt_size;  top = PRIV(utt_size);
1393    
1394  while (bot < top)  while (bot < top)
1395    {    {
1396      int r;
1397    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1398    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1399    if (c == 0)    if (r == 0)
1400      {      {
1401      *dptr = _pcre_utt[i].value;      *ptypeptr = PRIV(utt)[i].type;
1402      return _pcre_utt[i].type;      *pdataptr = PRIV(utt)[i].value;
1403        return TRUE;
1404      }      }
1405    if (c > 0) bot = i + 1; else top = i;    if (r > 0) bot = i + 1; else top = i;
1406    }    }
1407    
1408  *errorcodeptr = ERR47;  *errorcodeptr = ERR47;
1409  *ptrptr = ptr;  *ptrptr = ptr;
1410  return -1;  return FALSE;
1411    
1412  ERROR_RETURN:  ERROR_RETURN:
1413  *errorcodeptr = ERR46;  *errorcodeptr = ERR46;
1414  *ptrptr = ptr;  *ptrptr = ptr;
1415  return -1;  return FALSE;
1416  }  }
1417  #endif  #endif
1418    
1419    
1420    
   
 /*************************************************  
 *            Check for counted repeat            *  
 *************************************************/  
   
 /* This function is called when a '{' is encountered in a place where it might  
 start a quantifier. It looks ahead to see if it really is a quantifier or not.  
 It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}  
 where the ddds are digits.  
   
 Arguments:  
   p         pointer to the first char after '{'  
   
 Returns:    TRUE or FALSE  
 */  
   
 static BOOL  
 is_counted_repeat(const uschar *p)  
 {  
 if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  
 while ((digitab[*p] & ctype_digit) != 0) p++;  
 if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  
   
 if (*p++ != CHAR_COMMA) return FALSE;  
 if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  
   
 if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  
 while ((digitab[*p] & ctype_digit) != 0) p++;  
   
 return (*p == CHAR_RIGHT_CURLY_BRACKET);  
 }  
   
   
   
1421  /*************************************************  /*************************************************
1422  *         Read repeat counts                     *  *         Read repeat counts                     *
1423  *************************************************/  *************************************************/
# Line 963  Returns:         pointer to '}' on succe Line 1437  Returns:         pointer to '}' on succe
1437                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1438  */  */
1439    
1440  static const uschar *  static const pcre_uchar *
1441  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)
1442  {  {
1443  int min = 0;  int min = 0;
1444  int max = -1;  int max = -1;
# Line 972  int max = -1; Line 1446  int max = -1;
1446  /* 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
1447  an integer overflow. */  an integer overflow. */
1448    
1449  while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);
1450  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1451    {    {
1452    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 987  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1461  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1461    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1462      {      {
1463      max = 0;      max = 0;
1464      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);
1465      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1466        {        {
1467        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1012  return p; Line 1486  return p;
1486    
1487    
1488  /*************************************************  /*************************************************
1489  *  Subroutine for finding forward reference      *  *      Find first significant op code            *
1490  *************************************************/  *************************************************/
1491    
1492  /* This recursive function is called only from find_parens() below. The  /* This is called by several functions that scan a compiled expression looking
1493  top-level call starts at the beginning of the pattern. All other calls must  for a fixed first character, or an anchoring op code etc. It skips over things
1494  start at a parenthesis. It scans along a pattern's text looking for capturing  that do not influence this. For some calls, it makes sense to skip negative
1495  subpatterns, and counting them. If it finds a named pattern that matches the  forward and all backward assertions, and also the \b assertion; for others it
1496  name it is given, it returns its number. Alternatively, if the name is NULL, it  does not.
 returns when it reaches a given numbered subpattern. We know that if (?P< is  
 encountered, the name will be terminated by '>' because that is checked in the  
 first pass. Recursion is used to keep track of subpatterns that reset the  
 capturing group numbers - the (?| feature.  
1497    
1498  Arguments:  Arguments:
1499    ptrptr       address of the current character pointer (updated)    code         pointer to the start of the group
1500    cd           compile background data    skipassert   TRUE if certain assertions are to be skipped
   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  
   count        pointer to the current capturing subpattern number (updated)  
1501    
1502  Returns:       the number of the named subpattern, or -1 if not found  Returns:       pointer to the first significant opcode
1503  */  */
1504    
1505  static int  static const pcre_uchar*
1506  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  first_significant_code(const pcre_uchar *code, BOOL skipassert)
   BOOL xmode, int *count)  
1507  {  {
1508  uschar *ptr = *ptrptr;  for (;;)
 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)  
1509    {    {
1510    if (ptr[1] == CHAR_QUESTION_MARK &&    switch ((int)*code)
       ptr[2] == CHAR_VERTICAL_LINE)  
1511      {      {
1512      ptr += 3;      case OP_ASSERT_NOT:
1513      dup_parens = TRUE;      case OP_ASSERTBACK:
1514      }      case OP_ASSERTBACK_NOT:
1515        if (!skipassert) return code;
1516    /* Handle a normal, unnamed capturing parenthesis */      do code += GET(code, 1); while (*code == OP_ALT);
1517        code += PRIV(OP_lengths)[*code];
1518        break;
1519    
1520    else if (ptr[1] != CHAR_QUESTION_MARK && ptr[1] != CHAR_ASTERISK)      case OP_WORD_BOUNDARY:
1521      {      case OP_NOT_WORD_BOUNDARY:
1522      *count += 1;      if (!skipassert) return code;
1523      if (name == NULL && *count == lorn) return *count;      /* Fall through */
     ptr++;  
     }  
1524    
1525    /* Handle a condition. If it is an assertion, just carry on so that it      case OP_CALLOUT:
1526    is processed as normal. If not, skip to the closing parenthesis of the      case OP_CREF:
1527    condition (there can't be any nested parens. */      case OP_NCREF:
1528        case OP_RREF:
1529        case OP_NRREF:
1530        case OP_DEF:
1531        code += PRIV(OP_lengths)[*code];
1532        break;
1533    
1534    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)      default:
1535      {      return code;
     ptr += 2;  
     if (ptr[1] != CHAR_QUESTION_MARK)  
       {  
       while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
       if (*ptr != 0) ptr++;  
       }  
1536      }      }
1537      }
1538    /* Control never reaches here */
1539    }
1540    
   /* We have either (? or (* and not a condition */  
1541    
   else  
     {  
     ptr += 2;  
     if (*ptr == CHAR_P) ptr++;                      /* Allow optional P */  
1542    
1543      /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */  /*************************************************
1544    *        Find the fixed length of a branch       *
1545    *************************************************/
1546    
1547      if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&  /* Scan a branch and compute the fixed length of subject that will match it,
1548          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)  if the length is fixed. This is needed for dealing with backward assertions.
1549        {  In UTF8 mode, the result is in characters rather than bytes. The branch is
1550        int term;  temporarily terminated with OP_END when this function is called.
       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++;  
       }  
     }  
   }  
1551    
1552  /* Past any initial parenthesis handling, scan for parentheses or vertical  This function is called when a backward assertion is encountered, so that if it
1553  bars. */  fails, the error message can point to the correct place in the pattern.
1554    However, we cannot do this when the assertion contains subroutine calls,
1555    because they can be forward references. We solve this by remembering this case
1556    and doing the check at the end; a flag specifies which mode we are running in.
1557    
1558  for (; *ptr != 0; ptr++)  Arguments:
1559    {    code     points to the start of the pattern (the bracket)
1560    /* Skip over backslashed characters and also entire \Q...\E */    utf      TRUE in UTF-8 / UTF-16 / UTF-32 mode
1561      atend    TRUE if called when the pattern is complete
1562      cd       the "compile data" structure
1563    
1564    if (*ptr == CHAR_BACKSLASH)  Returns:   the fixed length,
1565      {               or -1 if there is no fixed length,
1566      if (*(++ptr) == 0) goto FAIL_EXIT;               or -2 if \C was encountered (in UTF-8 mode only)
1567      if (*ptr == CHAR_Q) for (;;)               or -3 if an OP_RECURSE item was encountered and atend is FALSE
1568        {               or -4 if an unknown opcode was encountered (internal error)
1569        while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};  */
       if (*ptr == 0) goto FAIL_EXIT;  
       if (*(++ptr) == CHAR_E) break;  
       }  
     continue;  
     }  
1570    
1571    /* Skip over character classes; this logic must be similar to the way they  static int
1572    are handled for real. If the first character is '^', skip it. Also, if the  find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1573    first few characters (either before or after ^) are \Q\E or \E we skip them  {
1574    too. This makes for compatibility with Perl. Note the use of STR macros to  int length = -1;
   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)  
     {  
     while (*(++ptr) != 0 && *ptr != CHAR_NL) {};  
     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, 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;  
     *ptrptr = ptr;  
     return -1;  
     }  
   
   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  
   
 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)  
 {  
 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, &count);  
   if (rc > 0 || *ptr++ == 0) break;  
   }  
   
 return rc;  
 }  
   
   
   
   
 /*************************************************  
 *      Find first significant op code            *  
 *************************************************/  
   
 /* This is called by several functions that scan a compiled expression looking  
 for a fixed first character, or an anchoring op code etc. It skips over things  
 that do not influence this. For some calls, a change of option is important.  
 For some calls, it makes sense to skip negative forward and all backward  
 assertions, and also the \b assertion; for others it does not.  
   
 Arguments:  
   code         pointer to the start of the group  
   options      pointer to external options  
   optbit       the option bit whose changing is significant, or  
                  zero if none are  
   skipassert   TRUE if certain assertions are to be skipped  
   
 Returns:       pointer to the first significant opcode  
 */  
   
 static const uschar*  
 first_significant_code(const uschar *code, int *options, int optbit,  
   BOOL skipassert)  
 {  
 for (;;)  
   {  
   switch ((int)*code)  
     {  
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
     case OP_ASSERT_NOT:  
     case OP_ASSERTBACK:  
     case OP_ASSERTBACK_NOT:  
     if (!skipassert) return code;  
     do code += GET(code, 1); while (*code == OP_ALT);  
     code += _pcre_OP_lengths[*code];  
     break;  
   
     case OP_WORD_BOUNDARY:  
     case OP_NOT_WORD_BOUNDARY:  
     if (!skipassert) return code;  
     /* Fall through */  
   
     case OP_CALLOUT:  
     case OP_CREF:  
     case OP_NCREF:  
     case OP_RREF:  
     case OP_NRREF:  
     case OP_DEF:  
     code += _pcre_OP_lengths[*code];  
     break;  
   
     default:  
     return code;  
     }  
   }  
 /* Control never reaches here */  
 }  
   
   
   
   
 /*************************************************  
 *        Find the fixed length of a branch       *  
 *************************************************/  
   
 /* Scan a branch and compute the fixed length of subject that will match it,  
 if the length is fixed. This is needed for dealing with backward assertions.  
 In UTF8 mode, the result is in characters rather than bytes. The branch is  
 temporarily terminated with OP_END when this function is called.  
   
 This function is called when a backward assertion is encountered, so that if it  
 fails, the error message can point to the correct place in the pattern.  
 However, we cannot do this when the assertion contains subroutine calls,  
 because they can be forward references. We solve this by remembering this case  
 and doing the check at the end; a flag specifies which mode we are running in.  
   
 Arguments:  
   code     points to the start of the pattern (the bracket)  
   options  the compiling options  
   atend    TRUE if called when the pattern is complete  
   cd       the "compile data" structure  
   
 Returns:   the fixed length,  
              or -1 if there is no fixed length,  
              or -2 if \C was encountered  
              or -3 if an OP_RECURSE item was encountered and atend is FALSE  
 */  
   
 static int  
 find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)  
 {  
 int length = -1;  
1575    
1576  register int branchlength = 0;  register int branchlength = 0;
1577  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1578    
1579  /* 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
1580  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1376  branch, check the length against that of Line 1582  branch, check the length against that of
1582  for (;;)  for (;;)
1583    {    {
1584    int d;    int d;
1585    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1586    register int op = *cc;    register pcre_uchar op = *cc;
1587    
1588    switch (op)    switch (op)
1589      {      {
1590        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1591        OP_BRA (normal non-capturing bracket) because the other variants of these
1592        opcodes are all concerned with unlimited repeated groups, which of course
1593        are not of fixed length. */
1594    
1595      case OP_CBRA:      case OP_CBRA:
1596      case OP_BRA:      case OP_BRA:
1597      case OP_ONCE:      case OP_ONCE:
1598        case OP_ONCE_NC:
1599      case OP_COND:      case OP_COND:
1600      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd);
1601      if (d < 0) return d;      if (d < 0) return d;
1602      branchlength += d;      branchlength += d;
1603      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1604      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1605      break;      break;
1606    
1607      /* Reached end of a branch; if it's a ket it is the end of a nested      /* Reached end of a branch; if it's a ket it is the end of a nested call.
1608      call. If it's ALT it is an alternation in a nested call. If it is      If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1609      END it's the end of the outer call. All can be handled by the same code. */      an ALT. If it is END it's the end of the outer call. All can be handled by
1610        the same code. Note that we must not include the OP_KETRxxx opcodes here,
1611        because they all imply an unlimited repeat. */
1612    
1613      case OP_ALT:      case OP_ALT:
1614      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1615      case OP_END:      case OP_END:
1616        case OP_ACCEPT:
1617        case OP_ASSERT_ACCEPT:
1618      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1619        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
1620      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
# Line 1413  for (;;) Line 1628  for (;;)
1628    
1629      case OP_RECURSE:      case OP_RECURSE:
1630      if (!atend) return -3;      if (!atend) return -3;
1631      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1632      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1633      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1634      d = find_fixedlength(cs + 2, options, atend, cd);      d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd);
1635      if (d < 0) return d;      if (d < 0) return d;
1636      branchlength += d;      branchlength += d;
1637      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1429  for (;;) Line 1644  for (;;)
1644      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1645      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1646      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1647      /* Fall through */      cc += PRIV(OP_lengths)[*cc];
1648        break;
1649    
1650      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1651    
1652      case OP_REVERSE:      case OP_MARK:
1653        case OP_PRUNE_ARG:
1654        case OP_SKIP_ARG:
1655        case OP_THEN_ARG:
1656        cc += cc[1] + PRIV(OP_lengths)[*cc];
1657        break;
1658    
1659        case OP_CALLOUT:
1660        case OP_CIRC:
1661        case OP_CIRCM:
1662        case OP_CLOSE:
1663        case OP_COMMIT:
1664      case OP_CREF:      case OP_CREF:
     case OP_NCREF:  
     case OP_RREF:  
     case OP_NRREF:  
1665      case OP_DEF:      case OP_DEF:
1666      case OP_OPT:      case OP_DOLL:
1667      case OP_CALLOUT:      case OP_DOLLM:
     case OP_SOD:  
     case OP_SOM:  
1668      case OP_EOD:      case OP_EOD:
1669      case OP_EODN:      case OP_EODN:
1670      case OP_CIRC:      case OP_FAIL:
1671      case OP_DOLL:      case OP_NCREF:
1672        case OP_NRREF:
1673      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1674        case OP_PRUNE:
1675        case OP_REVERSE:
1676        case OP_RREF:
1677        case OP_SET_SOM:
1678        case OP_SKIP:
1679        case OP_SOD:
1680        case OP_SOM:
1681        case OP_THEN:
1682      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1683      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1684      break;      break;
1685    
1686      /* Handle literal characters */      /* Handle literal characters */
1687    
1688      case OP_CHAR:      case OP_CHAR:
1689      case OP_CHARNC:      case OP_CHARI:
1690      case OP_NOT:      case OP_NOT:
1691        case OP_NOTI:
1692      branchlength++;      branchlength++;
1693      cc += 2;      cc += 2;
1694  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1695      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1696  #endif  #endif
1697      break;      break;
1698    
# Line 1469  for (;;) Line 1700  for (;;)
1700      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
1701    
1702      case OP_EXACT:      case OP_EXACT:
1703      branchlength += GET2(cc,1);      case OP_EXACTI:
1704      cc += 4;      case OP_NOTEXACT:
1705  #ifdef SUPPORT_UTF8      case OP_NOTEXACTI:
1706      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      branchlength += (int)GET2(cc,1);
1707        cc += _pcre_utf8_table4[cc[-1] & 0x3f];      cc += 2 + IMM2_SIZE;
1708    #ifdef SUPPORT_UTF
1709        if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1710  #endif  #endif
1711      break;      break;
1712    
1713      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1714      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1715      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1716      cc += 4;        cc += 2;
1717        cc += 1 + IMM2_SIZE + 1;
1718      break;      break;
1719    
1720      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1490  for (;;) Line 1724  for (;;)
1724      cc += 2;      cc += 2;
1725      /* Fall through */      /* Fall through */
1726    
1727        case OP_HSPACE:
1728        case OP_VSPACE:
1729        case OP_NOT_HSPACE:
1730        case OP_NOT_VSPACE:
1731      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1732      case OP_DIGIT:      case OP_DIGIT:
1733      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
# Line 1502  for (;;) Line 1740  for (;;)
1740      cc++;      cc++;
1741      break;      break;
1742    
1743      /* The single-byte matcher isn't allowed */      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1744        otherwise \C is coded as OP_ALLANY. */
1745    
1746      case OP_ANYBYTE:      case OP_ANYBYTE:
1747      return -2;      return -2;
1748    
1749      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1750    
 #ifdef SUPPORT_UTF8  
     case OP_XCLASS:  
     cc += GET(cc, 1) - 33;  
     /* Fall through */  
 #endif  
   
1751      case OP_CLASS:      case OP_CLASS:
1752      case OP_NCLASS:      case OP_NCLASS:
1753      cc += 33;  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1754        case OP_XCLASS:
1755        /* The original code caused an unsigned overflow in 64 bit systems,
1756        so now we use a conditional statement. */
1757        if (op == OP_XCLASS)
1758          cc += GET(cc, 1);
1759        else
1760          cc += PRIV(OP_lengths)[OP_CLASS];
1761    #else
1762        cc += PRIV(OP_lengths)[OP_CLASS];
1763    #endif
1764    
1765      switch (*cc)      switch (*cc)
1766        {        {
1767          case OP_CRPLUS:
1768          case OP_CRMINPLUS:
1769        case OP_CRSTAR:        case OP_CRSTAR:
1770        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1771        case OP_CRQUERY:        case OP_CRQUERY:
# Line 1529  for (;;) Line 1774  for (;;)
1774    
1775        case OP_CRRANGE:        case OP_CRRANGE:
1776        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1777        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1778        branchlength += GET2(cc,1);        branchlength += (int)GET2(cc,1);
1779        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1780        break;        break;
1781    
1782        default:        default:
# Line 1541  for (;;) Line 1786  for (;;)
1786    
1787      /* Anything else is variable length */      /* Anything else is variable length */
1788    
1789      default:      case OP_ANYNL:
1790        case OP_BRAMINZERO:
1791        case OP_BRAPOS:
1792        case OP_BRAPOSZERO:
1793        case OP_BRAZERO:
1794        case OP_CBRAPOS:
1795        case OP_EXTUNI:
1796        case OP_KETRMAX:
1797        case OP_KETRMIN:
1798        case OP_KETRPOS:
1799        case OP_MINPLUS:
1800        case OP_MINPLUSI:
1801        case OP_MINQUERY:
1802        case OP_MINQUERYI:
1803        case OP_MINSTAR:
1804        case OP_MINSTARI:
1805        case OP_MINUPTO:
1806        case OP_MINUPTOI:
1807        case OP_NOTMINPLUS:
1808        case OP_NOTMINPLUSI:
1809        case OP_NOTMINQUERY:
1810        case OP_NOTMINQUERYI:
1811        case OP_NOTMINSTAR:
1812        case OP_NOTMINSTARI:
1813        case OP_NOTMINUPTO:
1814        case OP_NOTMINUPTOI:
1815        case OP_NOTPLUS:
1816        case OP_NOTPLUSI:
1817        case OP_NOTPOSPLUS:
1818        case OP_NOTPOSPLUSI:
1819        case OP_NOTPOSQUERY:
1820        case OP_NOTPOSQUERYI:
1821        case OP_NOTPOSSTAR:
1822        case OP_NOTPOSSTARI:
1823        case OP_NOTPOSUPTO:
1824        case OP_NOTPOSUPTOI:
1825        case OP_NOTQUERY:
1826        case OP_NOTQUERYI:
1827        case OP_NOTSTAR:
1828        case OP_NOTSTARI:
1829        case OP_NOTUPTO:
1830        case OP_NOTUPTOI:
1831        case OP_PLUS:
1832        case OP_PLUSI:
1833        case OP_POSPLUS:
1834        case OP_POSPLUSI:
1835        case OP_POSQUERY:
1836        case OP_POSQUERYI:
1837        case OP_POSSTAR:
1838        case OP_POSSTARI:
1839        case OP_POSUPTO:
1840        case OP_POSUPTOI:
1841        case OP_QUERY:
1842        case OP_QUERYI:
1843        case OP_REF:
1844        case OP_REFI:
1845        case OP_DNREF:
1846        case OP_DNREFI:
1847        case OP_SBRA:
1848        case OP_SBRAPOS:
1849        case OP_SCBRA:
1850        case OP_SCBRAPOS:
1851        case OP_SCOND:
1852        case OP_SKIPZERO:
1853        case OP_STAR:
1854        case OP_STARI:
1855        case OP_TYPEMINPLUS:
1856        case OP_TYPEMINQUERY:
1857        case OP_TYPEMINSTAR:
1858        case OP_TYPEMINUPTO:
1859        case OP_TYPEPLUS:
1860        case OP_TYPEPOSPLUS:
1861        case OP_TYPEPOSQUERY:
1862        case OP_TYPEPOSSTAR:
1863        case OP_TYPEPOSUPTO:
1864        case OP_TYPEQUERY:
1865        case OP_TYPESTAR:
1866        case OP_TYPEUPTO:
1867        case OP_UPTO:
1868        case OP_UPTOI:
1869      return -1;      return -1;
1870    
1871        /* Catch unrecognized opcodes so that when new ones are added they
1872        are not forgotten, as has happened in the past. */
1873    
1874        default:
1875        return -4;
1876      }      }
1877    }    }
1878  /* Control never gets here */  /* Control never gets here */
# Line 1550  for (;;) Line 1880  for (;;)
1880    
1881    
1882    
   
1883  /*************************************************  /*************************************************
1884  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
1885  *************************************************/  *************************************************/
# Line 1563  length. Line 1892  length.
1892    
1893  Arguments:  Arguments:
1894    code        points to start of expression    code        points to start of expression
1895    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1896    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
1897    
1898  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
1899  */  */
1900    
1901  const uschar *  const pcre_uchar *
1902  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
1903  {  {
1904  for (;;)  for (;;)
1905    {    {
1906    register int c = *code;    register pcre_uchar c = *code;
1907    
1908    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1909    
1910    /* XCLASS is used for classes that cannot be represented just by a bit    /* XCLASS is used for classes that cannot be represented just by a bit
# Line 1587  for (;;) Line 1917  for (;;)
1917    
1918    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
1919      {      {
1920      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
1921      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1922      }      }
1923    
1924    /* Handle capturing bracket */    /* Handle capturing bracket */
1925    
1926    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1927               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1928      {      {
1929      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1930      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1931      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1932      }      }
1933    
1934    /* 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
1935    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
1936    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1937      must add in its length. */
1938    
1939    else    else
1940      {      {
# Line 1624  for (;;) Line 1956  for (;;)
1956        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1957        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1958        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1959        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1960            code += 2;
1961          break;
1962    
1963          case OP_MARK:
1964          case OP_PRUNE_ARG:
1965          case OP_SKIP_ARG:
1966          case OP_THEN_ARG:
1967          code += code[1];
1968        break;        break;
1969        }        }
1970    
1971      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
1972    
1973      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1974    
1975    /* 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
1976    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
1977    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
1978    
1979  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1980      if (utf8) switch(c)      if (utf) switch(c)
1981        {        {
1982        case OP_CHAR:        case OP_CHAR:
1983        case OP_CHARNC:        case OP_CHARI:
1984        case OP_EXACT:        case OP_EXACT:
1985          case OP_EXACTI:
1986        case OP_UPTO:        case OP_UPTO:
1987          case OP_UPTOI:
1988        case OP_MINUPTO:        case OP_MINUPTO:
1989          case OP_MINUPTOI:
1990        case OP_POSUPTO:        case OP_POSUPTO:
1991          case OP_POSUPTOI:
1992        case OP_STAR:        case OP_STAR:
1993          case OP_STARI:
1994        case OP_MINSTAR:        case OP_MINSTAR:
1995          case OP_MINSTARI:
1996        case OP_POSSTAR:        case OP_POSSTAR:
1997          case OP_POSSTARI:
1998        case OP_PLUS:        case OP_PLUS:
1999          case OP_PLUSI:
2000        case OP_MINPLUS:        case OP_MINPLUS:
2001          case OP_MINPLUSI:
2002        case OP_POSPLUS:        case OP_POSPLUS:
2003          case OP_POSPLUSI:
2004        case OP_QUERY:        case OP_QUERY:
2005          case OP_QUERYI:
2006        case OP_MINQUERY:        case OP_MINQUERY:
2007          case OP_MINQUERYI:
2008        case OP_POSQUERY:        case OP_POSQUERY:
2009        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2010          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2011        break;        break;
2012        }        }
2013  #else  #else
2014      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2015  #endif  #endif
2016      }      }
2017    }    }
# Line 1675  instance of OP_RECURSE. Line 2028  instance of OP_RECURSE.
2028    
2029  Arguments:  Arguments:
2030    code        points to start of expression    code        points to start of expression
2031    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2032    
2033  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
2034  */  */
2035    
2036  static const uschar *  static const pcre_uchar *
2037  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
2038  {  {
2039  for (;;)  for (;;)
2040    {    {
2041    register int c = *code;    register pcre_uchar c = *code;
2042    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2043    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
2044    
# Line 1697  for (;;) Line 2050  for (;;)
2050    
2051    /* 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
2052    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
2053    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2054      must add in its length. */
2055    
2056    else    else
2057      {      {
# Line 1719  for (;;) Line 2073  for (;;)
2073        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2074        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2075        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2076        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2077            code += 2;
2078          break;
2079    
2080          case OP_MARK:
2081          case OP_PRUNE_ARG:
2082          case OP_SKIP_ARG:
2083          case OP_THEN_ARG:
2084          code += code[1];
2085        break;        break;
2086        }        }
2087    
2088      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2089    
2090      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2091    
2092      /* 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
2093      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
2094      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
2095    
2096  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2097      if (utf8) switch(c)      if (utf) switch(c)
2098        {        {
2099        case OP_CHAR:        case OP_CHAR:
2100        case OP_CHARNC:        case OP_CHARI:
2101          case OP_NOT:
2102          case OP_NOTI:
2103        case OP_EXACT:        case OP_EXACT:
2104          case OP_EXACTI:
2105          case OP_NOTEXACT:
2106          case OP_NOTEXACTI:
2107        case OP_UPTO:        case OP_UPTO:
2108          case OP_UPTOI:
2109          case OP_NOTUPTO:
2110          case OP_NOTUPTOI:
2111        case OP_MINUPTO:        case OP_MINUPTO:
2112          case OP_MINUPTOI:
2113          case OP_NOTMINUPTO:
2114          case OP_NOTMINUPTOI:
2115        case OP_POSUPTO:        case OP_POSUPTO:
2116          case OP_POSUPTOI:
2117          case OP_NOTPOSUPTO:
2118          case OP_NOTPOSUPTOI:
2119        case OP_STAR:        case OP_STAR:
2120          case OP_STARI:
2121          case OP_NOTSTAR:
2122          case OP_NOTSTARI:
2123        case OP_MINSTAR:        case OP_MINSTAR:
2124          case OP_MINSTARI:
2125          case OP_NOTMINSTAR:
2126          case OP_NOTMINSTARI:
2127        case OP_POSSTAR:        case OP_POSSTAR:
2128          case OP_POSSTARI:
2129          case OP_NOTPOSSTAR:
2130          case OP_NOTPOSSTARI:
2131        case OP_PLUS:        case OP_PLUS:
2132          case OP_PLUSI:
2133          case OP_NOTPLUS:
2134          case OP_NOTPLUSI:
2135        case OP_MINPLUS:        case OP_MINPLUS:
2136          case OP_MINPLUSI:
2137          case OP_NOTMINPLUS:
2138          case OP_NOTMINPLUSI:
2139        case OP_POSPLUS:        case OP_POSPLUS:
2140          case OP_POSPLUSI:
2141          case OP_NOTPOSPLUS:
2142          case OP_NOTPOSPLUSI:
2143        case OP_QUERY:        case OP_QUERY:
2144          case OP_QUERYI:
2145          case OP_NOTQUERY:
2146          case OP_NOTQUERYI:
2147        case OP_MINQUERY:        case OP_MINQUERY:
2148          case OP_MINQUERYI:
2149          case OP_NOTMINQUERY:
2150          case OP_NOTMINQUERYI:
2151        case OP_POSQUERY:        case OP_POSQUERY:
2152        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2153          case OP_NOTPOSQUERY:
2154          case OP_NOTPOSQUERYI:
2155          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2156        break;        break;
2157        }        }
2158  #else  #else
2159      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2160  #endif  #endif
2161      }      }
2162    }    }
# Line 1776  bracket whose current branch will alread Line 2179  bracket whose current branch will alread
2179  Arguments:  Arguments:
2180    code        points to start of search    code        points to start of search
2181    endcode     points to where to stop    endcode     points to where to stop
2182    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2183      cd          contains pointers to tables etc.
2184      recurses    chain of recurse_check to catch mutual recursion
2185    
2186  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2187  */  */
2188    
2189    typedef struct recurse_check {
2190      struct recurse_check *prev;
2191      const pcre_uchar *group;
2192    } recurse_check;
2193    
2194  static BOOL  static BOOL
2195  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2196      BOOL utf, compile_data *cd, recurse_check *recurses)
2197  {  {
2198  register int c;  register pcre_uchar c;
2199  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  recurse_check this_recurse;
2200    
2201    for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2202       code < endcode;       code < endcode;
2203       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2204    {    {
2205    const uschar *ccode;    const pcre_uchar *ccode;
2206    
2207    c = *code;    c = *code;
2208    
# Line 1803  for (code = first_significant_code(code Line 2216  for (code = first_significant_code(code
2216      continue;      continue;
2217      }      }
2218    
2219      /* For a recursion/subroutine call, if its end has been reached, which
2220      implies a backward reference subroutine call, we can scan it. If it's a
2221      forward reference subroutine call, we can't. To detect forward reference
2222      we have to scan up the list that is kept in the workspace. This function is
2223      called only when doing the real compile, not during the pre-compile that
2224      measures the size of the compiled pattern. */
2225    
2226      if (c == OP_RECURSE)
2227        {
2228        const pcre_uchar *scode = cd->start_code + GET(code, 1);
2229        BOOL empty_branch;
2230    
2231        /* Test for forward reference or uncompleted reference. This is disabled
2232        when called to scan a completed pattern by setting cd->start_workspace to
2233        NULL. */
2234    
2235        if (cd->start_workspace != NULL)
2236          {
2237          const pcre_uchar *tcode;
2238          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2239            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2240          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2241          }
2242    
2243        /* If we are scanning a completed pattern, there are no forward references
2244        and all groups are complete. We need to detect whether this is a recursive
2245        call, as otherwise there will be an infinite loop. If it is a recursion,
2246        just skip over it. Simple recursions are easily detected. For mutual
2247        recursions we keep a chain on the stack. */
2248    
2249        else
2250          {
2251          recurse_check *r = recurses;
2252          const pcre_uchar *endgroup = scode;
2253    
2254          do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2255          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2256    
2257          for (r = recurses; r != NULL; r = r->prev)
2258            if (r->group == scode) break;
2259          if (r != NULL) continue;   /* Mutual recursion */
2260          }
2261    
2262        /* Completed reference; scan the referenced group, remembering it on the
2263        stack chain to detect mutual recursions. */
2264    
2265        empty_branch = FALSE;
2266        this_recurse.prev = recurses;
2267        this_recurse.group = scode;
2268    
2269        do
2270          {
2271          if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2272            {
2273            empty_branch = TRUE;
2274            break;
2275            }
2276          scode += GET(scode, 1);
2277          }
2278        while (*scode == OP_ALT);
2279    
2280        if (!empty_branch) return FALSE;  /* All branches are non-empty */
2281        continue;
2282        }
2283    
2284    /* Groups with zero repeats can of course be empty; skip them. */    /* Groups with zero repeats can of course be empty; skip them. */
2285    
2286    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO)    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2287          c == OP_BRAPOSZERO)
2288      {      {
2289      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2290      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
2291      c = *code;      c = *code;
2292      continue;      continue;
2293      }      }
2294    
2295    /* For other groups, scan the branches. */    /* A nested group that is already marked as "could be empty" can just be
2296      skipped. */
2297    
2298    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_SBRA  || c == OP_SBRAPOS ||
2299          c == OP_SCBRA || c == OP_SCBRAPOS)
2300      {      {
2301      BOOL empty_branch;      do code += GET(code, 1); while (*code == OP_ALT);
2302      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      c = *code;
2303        continue;
2304        }
2305    
2306      /* For other groups, scan the branches. */
2307    
2308      if (c == OP_BRA  || c == OP_BRAPOS ||
2309          c == OP_CBRA || c == OP_CBRAPOS ||
2310          c == OP_ONCE || c == OP_ONCE_NC ||
2311          c == OP_COND)
2312        {
2313        BOOL empty_branch;
2314        if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
2315    
2316      /* If a conditional group has only one branch, there is a second, implied,      /* If a conditional group has only one branch, there is a second, implied,
2317      empty branch, so just skip over the conditional, because it could be empty.      empty branch, so just skip over the conditional, because it could be empty.
# Line 1831  for (code = first_significant_code(code Line 2324  for (code = first_significant_code(code
2324        empty_branch = FALSE;        empty_branch = FALSE;
2325        do        do
2326          {          {
2327          if (!empty_branch && could_be_empty_branch(code, endcode, utf8))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2328            empty_branch = TRUE;            empty_branch = TRUE;
2329          code += GET(code, 1);          code += GET(code, 1);
2330          }          }
# Line 1849  for (code = first_significant_code(code Line 2342  for (code = first_significant_code(code
2342      {      {
2343      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2344      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2345      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2346      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"
2347      here. */      here. */
2348    
2349  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2350      case OP_XCLASS:      case OP_XCLASS:
2351      ccode = code += GET(code, 1);      ccode = code += GET(code, 1);
2352      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
# Line 1861  for (code = first_significant_code(code Line 2354  for (code = first_significant_code(code
2354    
2355      case OP_CLASS:      case OP_CLASS:
2356      case OP_NCLASS:      case OP_NCLASS:
2357      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2358    
2359  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2360      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
2361  #endif  #endif
2362    
# Line 1889  for (code = first_significant_code(code Line 2382  for (code = first_significant_code(code
2382    
2383      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2384    
2385        case OP_ANY:
2386        case OP_ALLANY:
2387        case OP_ANYBYTE:
2388    
2389      case OP_PROP:      case OP_PROP:
2390      case OP_NOTPROP:      case OP_NOTPROP:
2391        case OP_ANYNL:
2392    
2393        case OP_NOT_HSPACE:
2394        case OP_HSPACE:
2395        case OP_NOT_VSPACE:
2396        case OP_VSPACE:
2397      case OP_EXTUNI:      case OP_EXTUNI:
2398    
2399      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2400      case OP_DIGIT:      case OP_DIGIT:
2401      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2402      case OP_WHITESPACE:      case OP_WHITESPACE:
2403      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2404      case OP_WORDCHAR:      case OP_WORDCHAR:
2405      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2406      case OP_CHAR:      case OP_CHAR:
2407      case OP_CHARNC:      case OP_CHARI:
2408      case OP_NOT:      case OP_NOT:
2409        case OP_NOTI:
2410    
2411      case OP_PLUS:      case OP_PLUS:
2412        case OP_PLUSI:
2413      case OP_MINPLUS:      case OP_MINPLUS:
2414      case OP_POSPLUS:      case OP_MINPLUSI:
2415      case OP_EXACT:  
2416      case OP_NOTPLUS:      case OP_NOTPLUS:
2417        case OP_NOTPLUSI:
2418      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2419        case OP_NOTMINPLUSI:
2420    
2421        case OP_POSPLUS:
2422        case OP_POSPLUSI:
2423      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2424        case OP_NOTPOSPLUSI:
2425    
2426        case OP_EXACT:
2427        case OP_EXACTI:
2428      case OP_NOTEXACT:      case OP_NOTEXACT:
2429        case OP_NOTEXACTI:
2430    
2431      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2432      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2433      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2434      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2435    
2436      return FALSE;      return FALSE;
2437    
2438      /* 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 1935  for (code = first_significant_code(code Line 2452  for (code = first_significant_code(code
2452      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2453      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2454      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2455      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2456          code += 2;
2457      break;      break;
2458    
2459      /* End of branch */      /* End of branch */
# Line 1943  for (code = first_significant_code(code Line 2461  for (code = first_significant_code(code
2461      case OP_KET:      case OP_KET:
2462      case OP_KETRMAX:      case OP_KETRMAX:
2463      case OP_KETRMIN:      case OP_KETRMIN:
2464        case OP_KETRPOS:
2465      case OP_ALT:      case OP_ALT:
2466      return TRUE;      return TRUE;
2467    
2468      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2469      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2470        followed by a multibyte character. */
2471    
2472  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2473      case OP_STAR:      case OP_STAR:
2474        case OP_STARI:
2475        case OP_NOTSTAR:
2476        case OP_NOTSTARI:
2477    
2478      case OP_MINSTAR:      case OP_MINSTAR:
2479        case OP_MINSTARI:
2480        case OP_NOTMINSTAR:
2481        case OP_NOTMINSTARI:
2482    
2483      case OP_POSSTAR:      case OP_POSSTAR:
2484        case OP_POSSTARI:
2485        case OP_NOTPOSSTAR:
2486        case OP_NOTPOSSTARI:
2487    
2488      case OP_QUERY:      case OP_QUERY:
2489        case OP_QUERYI:
2490        case OP_NOTQUERY:
2491        case OP_NOTQUERYI:
2492    
2493      case OP_MINQUERY:      case OP_MINQUERY:
2494        case OP_MINQUERYI:
2495        case OP_NOTMINQUERY:
2496        case OP_NOTMINQUERYI:
2497    
2498      case OP_POSQUERY:      case OP_POSQUERY:
2499      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      case OP_POSQUERYI:
2500        case OP_NOTPOSQUERY:
2501        case OP_NOTPOSQUERYI:
2502    
2503        if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2504      break;      break;
2505    
2506      case OP_UPTO:      case OP_UPTO:
2507        case OP_UPTOI:
2508        case OP_NOTUPTO:
2509        case OP_NOTUPTOI:
2510    
2511      case OP_MINUPTO:      case OP_MINUPTO:
2512        case OP_MINUPTOI:
2513        case OP_NOTMINUPTO:
2514        case OP_NOTMINUPTOI:
2515    
2516      case OP_POSUPTO:      case OP_POSUPTO:
2517      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      case OP_POSUPTOI:
2518        case OP_NOTPOSUPTO:
2519        case OP_NOTPOSUPTOI:
2520    
2521        if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2522      break;      break;
2523  #endif  #endif
2524    
2525        /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2526        string. */
2527    
2528        case OP_MARK:
2529        case OP_PRUNE_ARG:
2530        case OP_SKIP_ARG:
2531        case OP_THEN_ARG:
2532        code += code[1];
2533        break;
2534    
2535        /* None of the remaining opcodes are required to match a character. */
2536    
2537        default:
2538        break;
2539      }      }
2540    }    }
2541    
# Line 1981  return TRUE; Line 2552  return TRUE;
2552  the current branch of the current pattern to see if it could match the empty  the current branch of the current pattern to see if it could match the empty
2553  string. If it could, we must look outwards for branches at other levels,  string. If it could, we must look outwards for branches at other levels,
2554  stopping when we pass beyond the bracket which is the subject of the recursion.  stopping when we pass beyond the bracket which is the subject of the recursion.
2555    This function is called only during the real compile, not during the
2556    pre-compile.
2557    
2558  Arguments:  Arguments:
2559    code        points to start of the recursion    code        points to start of the recursion
2560    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2561    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2562    utf8        TRUE if in UTF-8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2563      cd          pointers to tables etc
2564    
2565  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2566  */  */
2567    
2568  static BOOL  static BOOL
2569  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2570    BOOL utf8)    branch_chain *bcptr, BOOL utf, compile_data *cd)
2571  {  {
2572  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2573    {    {
2574    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2575      return FALSE;      return FALSE;
2576    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2577    }    }
2578  return TRUE;  return TRUE;
2579    }
2580    
2581    
2582    
2583    /*************************************************
2584    *        Base opcode of repeated opcodes         *
2585    *************************************************/
2586    
2587    /* Returns the base opcode for repeated single character type opcodes. If the
2588    opcode is not a repeated character type, it returns with the original value.
2589    
2590    Arguments:  c opcode
2591    Returns:    base opcode for the type
2592    */
2593    
2594    static pcre_uchar
2595    get_repeat_base(pcre_uchar c)
2596    {
2597    return (c > OP_TYPEPOSUPTO)? c :
2598           (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2599           (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2600           (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2601           (c >= OP_STARI)?      OP_STARI :
2602                                 OP_STAR;
2603    }
2604    
2605    
2606    
2607    #ifdef SUPPORT_UCP
2608    /*************************************************
2609    *        Check a character and a property        *
2610    *************************************************/
2611    
2612    /* This function is called by check_auto_possessive() when a property item
2613    is adjacent to a fixed character.
2614    
2615    Arguments:
2616      c            the character
2617      ptype        the property type
2618      pdata        the data for the type
2619      negated      TRUE if it's a negated property (\P or \p{^)
2620    
2621    Returns:       TRUE if auto-possessifying is OK
2622    */
2623    
2624    static BOOL
2625    check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2626      BOOL negated)
2627    {
2628    const pcre_uint32 *p;
2629    const ucd_record *prop = GET_UCD(c);
2630    
2631    switch(ptype)
2632      {
2633      case PT_LAMP:
2634      return (prop->chartype == ucp_Lu ||
2635              prop->chartype == ucp_Ll ||
2636              prop->chartype == ucp_Lt) == negated;
2637    
2638      case PT_GC:
2639      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2640    
2641      case PT_PC:
2642      return (pdata == prop->chartype) == negated;
2643    
2644      case PT_SC:
2645      return (pdata == prop->script) == negated;
2646    
2647      /* These are specials */
2648    
2649      case PT_ALNUM:
2650      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2651              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2652    
2653      case PT_SPACE:    /* Perl space */
2654      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2655              c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2656              == negated;
2657    
2658      case PT_PXSPACE:  /* POSIX space */
2659      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2660              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2661              c == CHAR_FF || c == CHAR_CR)
2662              == negated;
2663    
2664      case PT_WORD:
2665      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2666              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2667              c == CHAR_UNDERSCORE) == negated;
2668    
2669      case PT_CLIST:
2670      p = PRIV(ucd_caseless_sets) + prop->caseset;
2671      for (;;)
2672        {
2673        if (c < *p) return !negated;
2674        if (c == *p++) return negated;
2675        }
2676      break;  /* Control never reaches here */
2677      }
2678    
2679    return FALSE;
2680    }
2681    #endif  /* SUPPORT_UCP */
2682    
2683    
2684    
2685    /*************************************************
2686    *        Fill the character property list        *
2687    *************************************************/
2688    
2689    /* Checks whether the code points to an opcode that can take part in auto-
2690    possessification, and if so, fills a list with its properties.
2691    
2692    Arguments:
2693      code        points to start of expression
2694      utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2695      fcc         points to case-flipping table
2696      list        points to output list
2697                  list[0] will be filled with the opcode
2698                  list[1] will be non-zero if this opcode
2699                    can match an empty character string
2700                  list[2..7] depends on the opcode
2701    
2702    Returns:      points to the start of the next opcode if *code is accepted
2703                  NULL if *code is not accepted
2704    */
2705    
2706    static const pcre_uchar *
2707    get_chr_property_list(const pcre_uchar *code, BOOL utf,
2708      const pcre_uint8 *fcc, pcre_uint32 *list)
2709    {
2710    pcre_uchar c = *code;
2711    const pcre_uchar *end;
2712    const pcre_uint32 *clist_src;
2713    pcre_uint32 *clist_dest;
2714    pcre_uint32 chr;
2715    pcre_uchar base;
2716    
2717    list[0] = c;
2718    list[1] = FALSE;
2719    code++;
2720    
2721    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2722      {
2723      base = get_repeat_base(c);
2724      c -= (base - OP_STAR);
2725    
2726      if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2727        code += IMM2_SIZE;
2728    
2729      list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
2730    
2731      switch(base)
2732        {
2733        case OP_STAR:
2734        list[0] = OP_CHAR;
2735        break;
2736    
2737        case OP_STARI:
2738        list[0] = OP_CHARI;
2739        break;
2740    
2741        case OP_NOTSTAR:
2742        list[0] = OP_NOT;
2743        break;
2744    
2745        case OP_NOTSTARI:
2746        list[0] = OP_NOTI;
2747        break;
2748    
2749        case OP_TYPESTAR:
2750        list[0] = *code;
2751        code++;
2752        break;
2753        }
2754      c = list[0];
2755      }
2756    
2757    switch(c)
2758      {
2759      case OP_NOT_DIGIT:
2760      case OP_DIGIT:
2761      case OP_NOT_WHITESPACE:
2762      case OP_WHITESPACE:
2763      case OP_NOT_WORDCHAR:
2764      case OP_WORDCHAR:
2765      case OP_ANY:
2766      case OP_ALLANY:
2767      case OP_ANYNL:
2768      case OP_NOT_HSPACE:
2769      case OP_HSPACE:
2770      case OP_NOT_VSPACE:
2771      case OP_VSPACE:
2772      case OP_EXTUNI:
2773      case OP_EODN:
2774      case OP_EOD:
2775      case OP_DOLL:
2776      case OP_DOLLM:
2777      return code;
2778    
2779      case OP_CHAR:
2780      case OP_NOT:
2781      GETCHARINCTEST(chr, code);
2782      list[2] = chr;
2783      list[3] = NOTACHAR;
2784      return code;
2785    
2786      case OP_CHARI:
2787      case OP_NOTI:
2788      list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2789      GETCHARINCTEST(chr, code);
2790      list[2] = chr;
2791    
2792    #ifdef SUPPORT_UCP
2793      if (chr < 128 || (chr < 256 && !utf))
2794        list[3] = fcc[chr];
2795      else
2796        list[3] = UCD_OTHERCASE(chr);
2797    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2798      list[3] = (chr < 256) ? fcc[chr] : chr;
2799    #else
2800      list[3] = fcc[chr];
2801    #endif
2802    
2803      /* The othercase might be the same value. */
2804    
2805      if (chr == list[3])
2806        list[3] = NOTACHAR;
2807      else
2808        list[4] = NOTACHAR;
2809      return code;
2810    
2811    #ifdef SUPPORT_UCP
2812      case OP_PROP:
2813      case OP_NOTPROP:
2814      if (code[0] != PT_CLIST)
2815        {
2816        list[2] = code[0];
2817        list[3] = code[1];
2818        return code + 2;
2819        }
2820    
2821      /* Convert only if we have anough space. */
2822    
2823      clist_src = PRIV(ucd_caseless_sets) + code[1];
2824      clist_dest = list + 2;
2825      code += 2;
2826    
2827      do {
2828         /* Early return if there is not enough space. */
2829         if (clist_dest >= list + 8)
2830           {
2831           list[2] = code[0];
2832           list[3] = code[1];
2833           return code;
2834           }
2835         *clist_dest++ = *clist_src;
2836         }
2837       while(*clist_src++ != NOTACHAR);
2838    
2839      /* Enough space to store all characters. */
2840    
2841      list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2842      return code;
2843    #endif
2844    
2845      case OP_NCLASS:
2846      case OP_CLASS:
2847    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2848      case OP_XCLASS:
2849    
2850      if (c == OP_XCLASS)
2851        end = code + GET(code, 0);
2852      else
2853    #endif
2854        end = code + 32 / sizeof(pcre_uchar);
2855    
2856      switch(*end)
2857        {
2858        case OP_CRSTAR:
2859        case OP_CRMINSTAR:
2860        case OP_CRQUERY:
2861        case OP_CRMINQUERY:
2862        list[1] = TRUE;
2863        end++;
2864        break;
2865    
2866        case OP_CRRANGE:
2867        case OP_CRMINRANGE:
2868        list[1] = (GET2(end, 1) == 0);
2869        end += 1 + 2 * IMM2_SIZE;
2870        break;
2871        }
2872      list[2] = end - code;
2873      return end;
2874      }
2875    return NULL;    /* Opcode not accepted */
2876    }
2877    
2878    
2879    
2880    /*************************************************
2881    *    Scan further character sets for match       *
2882    *************************************************/
2883    
2884    /* Checks whether the base and the current opcode have a common character, in
2885    which case the base cannot be possessified.
2886    
2887    Arguments:
2888      code        points to the byte code
2889      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2890      cd          static compile data
2891      base_list   the data list of the base opcode
2892    
2893    Returns:      TRUE if the auto-possessification is possible
2894    */
2895    
2896    static BOOL
2897    compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2898      const pcre_uint32* base_list)
2899    {
2900    pcre_uchar c;
2901    pcre_uint32 list[8];
2902    const pcre_uint32* chr_ptr;
2903    const pcre_uint32* ochr_ptr;
2904    const pcre_uint32* list_ptr;
2905    pcre_uint32 chr;
2906    
2907    for(;;)
2908      {
2909      c = *code;
2910    
2911      /* Skip over callouts */
2912    
2913      if (c == OP_CALLOUT)
2914        {
2915        code += PRIV(OP_lengths)[c];
2916        continue;
2917        }
2918    
2919      if (c == OP_ALT)
2920        {
2921        do code += GET(code, 1); while (*code == OP_ALT);
2922        c = *code;
2923        }
2924    
2925      switch(c)
2926        {
2927        case OP_END:
2928        /* TRUE only in greedy case. The non-greedy case could be replaced by an
2929        OP_EXACT, but it is probably not worth it. (And note that OP_EXACT uses
2930        more memory, which we cannot get at this stage.) */
2931    
2932        return base_list[1] != 0;
2933    
2934        case OP_KET:
2935        /* If the bracket is capturing, and referenced by an OP_RECURSE, the
2936        non-greedy case cannot be converted to a possessive form. We do not test
2937        the bracket type at the moment, but we might do it in the future to improve
2938        this condition. (But note that recursive calls are always atomic.) */
2939    
2940        if (base_list[1] == 0) return FALSE;
2941        code += PRIV(OP_lengths)[c];
2942        continue;
2943        }
2944    
2945      /* Check for a supported opcode, and load its properties. */
2946    
2947      code = get_chr_property_list(code, utf, cd->fcc, list);
2948      if (code == NULL) return FALSE;    /* Unsupported */
2949    
2950      /* If either opcode is a small character list, set pointers for comparing
2951      characters from that list with another list, or with a property. */
2952    
2953      if (base_list[0] == OP_CHAR)
2954        {
2955        chr_ptr = base_list + 2;
2956        list_ptr = list;
2957        }
2958      else if (list[0] == OP_CHAR)
2959        {
2960        chr_ptr = list + 2;
2961        list_ptr = base_list;
2962        }
2963    
2964      /* Some property combinations also acceptable. Unicode property opcodes are
2965      processed specially; the rest can be handled with a lookup table. */
2966    
2967      else
2968        {
2969        pcre_uint32 leftop, rightop;
2970    
2971        if (list[1] != 0) return FALSE;   /* Must match at least one character */
2972        leftop = base_list[0];
2973        rightop = list[0];
2974    
2975    #ifdef SUPPORT_UCP
2976        if (leftop == OP_PROP || leftop == OP_NOTPROP)
2977          {
2978          if (rightop == OP_EOD) return TRUE;
2979          if (rightop == OP_PROP || rightop == OP_NOTPROP)
2980            {
2981            int n;
2982            const pcre_uint8 *p;
2983            BOOL same = leftop == rightop;
2984            BOOL lisprop = leftop == OP_PROP;
2985            BOOL risprop = rightop == OP_PROP;
2986            BOOL bothprop = lisprop && risprop;
2987    
2988            /* There's a table that specifies how each combination is to be
2989            processed:
2990              0   Always return FALSE (never auto-possessify)
2991              1   Character groups are distinct (possessify if both are OP_PROP)
2992              2   Check character categories in the same group (general or particular)
2993              3   Return TRUE if the two opcodes are not the same
2994              ... see comments below
2995            */
2996    
2997            n = propposstab[base_list[2]][list[2]];
2998            switch(n)
2999              {
3000              case 0: return FALSE;
3001              case 1: return bothprop;
3002              case 2: return (base_list[3] == list[3]) != same;
3003              case 3: return !same;
3004    
3005              case 4:  /* Left general category, right particular category */
3006              return risprop && catposstab[base_list[3]][list[3]] == same;
3007    
3008              case 5:  /* Right general category, left particular category */
3009              return lisprop && catposstab[list[3]][base_list[3]] == same;
3010    
3011              /* This code is logically tricky. Think hard before fiddling with it.
3012              The posspropstab table has four entries per row. Each row relates to
3013              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3014              Only WORD actually needs all four entries, but using repeats for the
3015              others means they can all use the same code below.
3016    
3017              The first two entries in each row are Unicode general categories, and
3018              apply always, because all the characters they include are part of the
3019              PCRE character set. The third and fourth entries are a general and a
3020              particular category, respectively, that include one or more relevant
3021              characters. One or the other is used, depending on whether the check
3022              is for a general or a particular category. However, in both cases the
3023              category contains more characters than the specials that are defined
3024              for the property being tested against. Therefore, it cannot be used
3025              in a NOTPROP case.
3026    
3027              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3028              Underscore is covered by ucp_P or ucp_Po. */
3029    
3030              case 6:  /* Left alphanum vs right general category */
3031              case 7:  /* Left space vs right general category */
3032              case 8:  /* Left word vs right general category */
3033              p = posspropstab[n-6];
3034              return risprop && lisprop ==
3035                (list[3] != p[0] &&
3036                 list[3] != p[1] &&
3037                (list[3] != p[2] || !lisprop));
3038    
3039              case 9:   /* Right alphanum vs left general category */
3040              case 10:  /* Right space vs left general category */
3041              case 11:  /* Right word vs left general category */
3042              p = posspropstab[n-9];
3043              return lisprop && risprop ==
3044                (base_list[3] != p[0] &&
3045                 base_list[3] != p[1] &&
3046                (base_list[3] != p[2] || !risprop));
3047    
3048              case 12:  /* Left alphanum vs right particular category */
3049              case 13:  /* Left space vs right particular category */
3050              case 14:  /* Left word vs right particular category */
3051              p = posspropstab[n-12];
3052              return risprop && lisprop ==
3053                (catposstab[p[0]][list[3]] &&
3054                 catposstab[p[1]][list[3]] &&
3055                (list[3] != p[3] || !lisprop));
3056    
3057              case 15:  /* Right alphanum vs left particular category */
3058              case 16:  /* Right space vs left particular category */
3059              case 17:  /* Right word vs left particular category */
3060              p = posspropstab[n-15];
3061              return lisprop && risprop ==
3062                (catposstab[p[0]][base_list[3]] &&
3063                 catposstab[p[1]][base_list[3]] &&
3064                (base_list[3] != p[3] || !risprop));
3065              }
3066            }
3067          return FALSE;
3068          }
3069    
3070        else
3071    #endif  /* SUPPORT_UCP */
3072    
3073        return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3074               rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3075               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3076        }
3077    
3078      /* Control reaches here only if one of the items is a small character list.
3079      All characters are checked against the other side. */
3080    
3081      do
3082        {
3083        chr = *chr_ptr;
3084    
3085        switch(list_ptr[0])
3086          {
3087          case OP_CHAR:
3088          ochr_ptr = list_ptr + 2;
3089          do
3090            {
3091            if (chr == *ochr_ptr) return FALSE;
3092            ochr_ptr++;
3093            }
3094          while(*ochr_ptr != NOTACHAR);
3095          break;
3096    
3097          case OP_NOT:
3098          ochr_ptr = list_ptr + 2;
3099          do
3100            {
3101            if (chr == *ochr_ptr)
3102              break;
3103            ochr_ptr++;
3104            }
3105          while(*ochr_ptr != NOTACHAR);
3106          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3107          break;
3108    
3109          /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3110          set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3111    
3112          case OP_DIGIT:
3113          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3114          break;
3115    
3116          case OP_NOT_DIGIT:
3117          if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3118          break;
3119    
3120          case OP_WHITESPACE:
3121          if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3122          break;
3123    
3124          case OP_NOT_WHITESPACE:
3125          if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3126          break;
3127    
3128          case OP_WORDCHAR:
3129          if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3130          break;
3131    
3132          case OP_NOT_WORDCHAR:
3133          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3134          break;
3135    
3136          case OP_HSPACE:
3137          switch(chr)
3138            {
3139            HSPACE_CASES: return FALSE;
3140            default: break;
3141            }
3142          break;
3143    
3144          case OP_NOT_HSPACE:
3145          switch(chr)
3146            {
3147            HSPACE_CASES: break;
3148            default: return FALSE;
3149            }
3150          break;
3151    
3152          case OP_ANYNL:
3153          case OP_VSPACE:
3154          switch(chr)
3155            {
3156            VSPACE_CASES: return FALSE;
3157            default: break;
3158            }
3159          break;
3160    
3161          case OP_NOT_VSPACE:
3162          switch(chr)
3163            {
3164            VSPACE_CASES: break;
3165            default: return FALSE;
3166            }
3167          break;
3168    
3169          case OP_DOLL:
3170          case OP_EODN:
3171          switch (chr)
3172            {
3173            case CHAR_CR:
3174            case CHAR_LF:
3175            case CHAR_VT:
3176            case CHAR_FF:
3177            case CHAR_NEL:
3178    #ifndef EBCDIC
3179            case 0x2028:
3180            case 0x2029:
3181    #endif  /* Not EBCDIC */
3182            return FALSE;
3183            }
3184          break;
3185    
3186          case OP_EOD:    /* Can always possessify before \z */
3187          break;
3188    
3189          case OP_PROP:
3190          case OP_NOTPROP:
3191          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3192                list_ptr[0] == OP_NOTPROP))
3193            return FALSE;
3194          break;
3195    
3196          /* The class comparisons work only when the class is the second item
3197          of the pair, because there are at present no possessive forms of the
3198          class opcodes. Note also that the "code" variable that is used below
3199          points after the second item, and that the pointer for the first item
3200          is not available, so even if there were possessive forms of the class
3201          opcodes, the correct comparison could not be done. */
3202    
3203          case OP_NCLASS:
3204          if (chr > 255) return FALSE;
3205          /* Fall through */
3206    
3207          case OP_CLASS:
3208          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3209          if (chr > 255) break;
3210          if ((((pcre_uint8 *)(code - list_ptr[2] + 1))[chr >> 3] & (1 << (chr & 7))) != 0)
3211            return FALSE;
3212          break;
3213    
3214    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3215          case OP_XCLASS:
3216          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3217          if (PRIV(xclass)(chr, code - list_ptr[2] + 1 + LINK_SIZE, utf))
3218            return FALSE;
3219          break;
3220    #endif
3221    
3222          default:
3223          return FALSE;
3224          }
3225    
3226        chr_ptr++;
3227        }
3228      while(*chr_ptr != NOTACHAR);
3229    
3230      /* At least one character must be matched from this opcode. */
3231    
3232      if (list[1] == 0) return TRUE;
3233      }
3234    
3235    return FALSE;
3236    }
3237    
3238    
3239    
3240    /*************************************************
3241    *    Scan compiled regex for auto-possession     *
3242    *************************************************/
3243    
3244    /* Replaces single character iterations with their possessive alternatives
3245    if appropriate. This function modifies the compiled opcode!
3246    
3247    Arguments:
3248      code        points to start of the byte code
3249      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3250      cd          static compile data
3251    
3252    Returns:      nothing
3253    */
3254    
3255    static void
3256    auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
3257    {
3258    register pcre_uchar c;
3259    const pcre_uchar *end;
3260    pcre_uint32 list[8];
3261    
3262    for (;;)
3263      {
3264      c = *code;
3265    
3266      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3267        {
3268        c -= get_repeat_base(c) - OP_STAR;
3269        end = (c <= OP_MINUPTO) ?
3270          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3271        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3272    
3273        if (end != NULL && compare_opcodes(end, utf, cd, list))
3274          {
3275          switch(c)
3276            {
3277            case OP_STAR:
3278            *code += OP_POSSTAR - OP_STAR;
3279            break;
3280    
3281            case OP_MINSTAR:
3282            *code += OP_POSSTAR - OP_MINSTAR;
3283            break;
3284    
3285            case OP_PLUS:
3286            *code += OP_POSPLUS - OP_PLUS;
3287            break;
3288    
3289            case OP_MINPLUS:
3290            *code += OP_POSPLUS - OP_MINPLUS;
3291            break;
3292    
3293            case OP_QUERY:
3294            *code += OP_POSQUERY - OP_QUERY;
3295            break;
3296    
3297            case OP_MINQUERY:
3298            *code += OP_POSQUERY - OP_MINQUERY;
3299            break;
3300    
3301            case OP_UPTO:
3302            *code += OP_POSUPTO - OP_UPTO;
3303            break;
3304    
3305            case OP_MINUPTO:
3306            *code += OP_MINUPTO - OP_UPTO;
3307            break;
3308            }
3309          }
3310        c = *code;
3311        }
3312    
3313      switch(c)
3314        {
3315        case OP_END:
3316        return;
3317    
3318        case OP_TYPESTAR:
3319        case OP_TYPEMINSTAR:
3320        case OP_TYPEPLUS:
3321        case OP_TYPEMINPLUS:
3322        case OP_TYPEQUERY:
3323        case OP_TYPEMINQUERY:
3324        case OP_TYPEPOSSTAR:
3325        case OP_TYPEPOSPLUS:
3326        case OP_TYPEPOSQUERY:
3327        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3328        break;
3329    
3330        case OP_TYPEUPTO:
3331        case OP_TYPEMINUPTO:
3332        case OP_TYPEEXACT:
3333        case OP_TYPEPOSUPTO:
3334        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3335          code += 2;
3336        break;
3337    
3338        case OP_XCLASS:
3339        code += GET(code, 1);
3340        break;
3341    
3342        case OP_MARK:
3343        case OP_PRUNE_ARG:
3344        case OP_SKIP_ARG:
3345        case OP_THEN_ARG:
3346        code += code[1];
3347        break;
3348        }
3349    
3350      /* Add in the fixed length from the table */
3351    
3352      code += PRIV(OP_lengths)[c];
3353    
3354      /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3355      a multi-byte character. The length in the table is a minimum, so we have to
3356      arrange to skip the extra bytes. */
3357    
3358    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3359      if (utf) switch(c)
3360        {
3361        case OP_CHAR:
3362        case OP_CHARI:
3363        case OP_NOT:
3364        case OP_NOTI:
3365        case OP_STAR:
3366        case OP_MINSTAR:
3367        case OP_PLUS:
3368        case OP_MINPLUS:
3369        case OP_QUERY:
3370        case OP_MINQUERY:
3371        case OP_UPTO:
3372        case OP_MINUPTO:
3373        case OP_EXACT:
3374        case OP_POSSTAR:
3375        case OP_POSPLUS:
3376        case OP_POSQUERY:
3377        case OP_POSUPTO:
3378        case OP_STARI:
3379        case OP_MINSTARI:
3380        case OP_PLUSI:
3381        case OP_MINPLUSI:
3382        case OP_QUERYI:
3383        case OP_MINQUERYI:
3384        case OP_UPTOI:
3385        case OP_MINUPTOI:
3386        case OP_EXACTI:
3387        case OP_POSSTARI:
3388        case OP_POSPLUSI:
3389        case OP_POSQUERYI:
3390        case OP_POSUPTOI:
3391        case OP_NOTSTAR:
3392        case OP_NOTMINSTAR:
3393        case OP_NOTPLUS:
3394        case OP_NOTMINPLUS:
3395        case OP_NOTQUERY:
3396        case OP_NOTMINQUERY:
3397        case OP_NOTUPTO:
3398        case OP_NOTMINUPTO:
3399        case OP_NOTEXACT:
3400        case OP_NOTPOSSTAR:
3401        case OP_NOTPOSPLUS:
3402        case OP_NOTPOSQUERY:
3403        case OP_NOTPOSUPTO:
3404        case OP_NOTSTARI:
3405        case OP_NOTMINSTARI:
3406        case OP_NOTPLUSI:
3407        case OP_NOTMINPLUSI:
3408        case OP_NOTQUERYI:
3409        case OP_NOTMINQUERYI:
3410        case OP_NOTUPTOI:
3411        case OP_NOTMINUPTOI:
3412        case OP_NOTEXACTI:
3413        case OP_NOTPOSSTARI:
3414        case OP_NOTPOSPLUSI:
3415        case OP_NOTPOSQUERYI:
3416        case OP_NOTPOSUPTOI:
3417        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3418        break;
3419        }
3420    #else
3421      (void)(utf);  /* Keep compiler happy by referencing function argument */
3422    #endif
3423      }
3424  }  }
3425    
3426    
# Line 2030  where Perl recognizes it as the POSIX cl Line 3449  where Perl recognizes it as the POSIX cl
3449  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
3450  I think.  I think.
3451    
3452    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3453    It seems that the appearance of a nested POSIX class supersedes an apparent
3454    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3455    a digit.
3456    
3457    In Perl, unescaped square brackets may also appear as part of class names. For
3458    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3459    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3460    seem right at all. PCRE does not allow closing square brackets in POSIX class
3461    names.
3462    
3463  Arguments:  Arguments:
3464    ptr      pointer to the initial [    ptr      pointer to the initial [
3465    endptr   where to return the end pointer    endptr   where to return the end pointer
# Line 2038  Returns:   TRUE or FALSE Line 3468  Returns:   TRUE or FALSE
3468  */  */
3469    
3470  static BOOL  static BOOL
3471  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3472  {  {
3473  int terminator;          /* Don't combine these lines; the Solaris cc */  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3474  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3475  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != CHAR_NULL; ptr++)
3476    {    {
3477    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET) ptr++; else    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3478        ptr++;
3479      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3480      else
3481      {      {
     if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;  
3482      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3483        {        {
3484        *endptr = ptr;        *endptr = ptr;
3485        return TRUE;        return TRUE;
3486        }        }
3487        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3488             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3489              ptr[1] == CHAR_EQUALS_SIGN) &&
3490            check_posix_syntax(ptr, endptr))
3491          return FALSE;
3492      }      }
3493    }    }
3494  return FALSE;  return FALSE;
# Line 2075  Returns:     a value representing the na Line 3512  Returns:     a value representing the na
3512  */  */
3513    
3514  static int  static int
3515  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
3516  {  {
3517  const char *pn = posix_names;  const char *pn = posix_names;
3518  register int yield = 0;  register int yield = 0;
3519  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
3520    {    {
3521    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
3522      strncmp((const char *)ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3523    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
3524    yield++;    yield++;
3525    }    }
# Line 2114  value in the reference (which is a group Line 3551  value in the reference (which is a group
3551  Arguments:  Arguments:
3552    group      points to the start of the group    group      points to the start of the group
3553    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
3554    utf8       TRUE in UTF-8 mode    utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3555    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
3556    save_hwm   the hwm forward reference pointer at the start of the group    save_hwm   the hwm forward reference pointer at the start of the group
3557    
# Line 2122  Returns:     nothing Line 3559  Returns:     nothing
3559  */  */
3560    
3561  static void  static void
3562  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
3563    uschar *save_hwm)    pcre_uchar *save_hwm)
3564  {  {
3565  uschar *ptr = group;  pcre_uchar *ptr = group;
3566    
3567  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
3568    {    {
3569    int offset;    int offset;
3570    uschar *hc;    pcre_uchar *hc;
3571    
3572    /* See if this recursion is on the forward reference list. If so, adjust the    /* See if this recursion is on the forward reference list. If so, adjust the
3573    reference. */    reference. */
3574    
3575    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
3576      {      {
3577      offset = GET(hc, 0);      offset = (int)GET(hc, 0);
3578      if (cd->start_code + offset == ptr + 1)      if (cd->start_code + offset == ptr + 1)
3579        {        {
3580        PUT(hc, 0, offset + adjust);        PUT(hc, 0, offset + adjust);
# Line 2150  while ((ptr = (uschar *)find_recurse(ptr Line 3587  while ((ptr = (uschar *)find_recurse(ptr
3587    
3588    if (hc >= cd->hwm)    if (hc >= cd->hwm)
3589      {      {
3590      offset = GET(ptr, 1);      offset = (int)GET(ptr, 1);
3591      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
3592      }      }
3593    
# Line 2175  Arguments: Line 3612  Arguments:
3612  Returns:         new code pointer  Returns:         new code pointer
3613  */  */
3614    
3615  static uschar *  static pcre_uchar *
3616  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
3617  {  {
3618  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
3619  *code++ = 255;  *code++ = 255;
3620  PUT(code, 0, ptr - cd->start_pattern);  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
3621  PUT(code, LINK_SIZE, 0);                /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
3622  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
3623  }  }
3624    
3625    
# Line 2204  Returns:             nothing Line 3641  Returns:             nothing
3641  */  */
3642    
3643  static void  static void
3644  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
3645  {  {
3646  int length = ptr - cd->start_pattern - GET(previous_callout, 2);  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
3647  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
3648  }  }
3649    
# Line 2218  PUT(previous_callout, 2 + LINK_SIZE, len Line 3655  PUT(previous_callout, 2 + LINK_SIZE, len
3655  *************************************************/  *************************************************/
3656    
3657  /* This function is passed the start and end of a class range, in UTF-8 mode  /* This function is passed the start and end of a class range, in UTF-8 mode
3658  with UCP support. It searches up the characters, looking for internal ranges of  with UCP support. It searches up the characters, looking for ranges of
3659  characters in the "other" case. Each call returns the next one, updating the  characters in the "other" case. Each call returns the next one, updating the
3660  start address.  start address. A character with multiple other cases is returned on its own
3661    with a special return value.
3662    
3663  Arguments:  Arguments:
3664    cptr        points to starting character value; updated    cptr        points to starting character value; updated
# Line 2228  Arguments: Line 3666  Arguments:
3666    ocptr       where to put start of othercase range    ocptr       where to put start of othercase range
3667    odptr       where to put end of othercase range    odptr       where to put end of othercase range
3668    
3669  Yield:        TRUE when range returned; FALSE when no more  Yield:        -1 when no more
3670                   0 when a range is returned
3671                  >0 the CASESET offset for char with multiple other cases
3672                    in this case, ocptr contains the original
3673  */  */
3674    
3675  static BOOL  static int
3676  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
3677    unsigned int *odptr)    pcre_uint32 *odptr)
3678  {  {
3679  unsigned int c, othercase, next;  pcre_uint32 c, othercase, next;
3680    unsigned int co;
3681    
3682    /* Find the first character that has an other case. If it has multiple other
3683    cases, return its case offset value. */
3684    
3685  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
3686    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
3687      if ((co = UCD_CASESET(c)) != 0)
3688        {
3689        *ocptr = c++;   /* Character that has the set */
3690        *cptr = c;      /* Rest of input range */
3691        return (int)co;
3692        }
3693      if ((othercase = UCD_OTHERCASE(c)) != c) break;
3694      }
3695    
3696  if (c > d) return FALSE;  if (c > d) return -1;  /* Reached end of range */
3697    
3698  *ocptr = othercase;  *ocptr = othercase;
3699  next = othercase + 1;  next = othercase + 1;
# Line 2251  for (++c; c <= d; c++) Line 3704  for (++c; c <= d; c++)
3704    next++;    next++;
3705    }    }
3706    
3707  *odptr = next - 1;  *odptr = next - 1;     /* End of othercase range */
3708  *cptr = c;  *cptr = c;             /* Rest of input range */
3709    return 0;
 return TRUE;  
3710  }  }
3711  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3712    
3713    
3714    
3715  /*************************************************  /*************************************************
3716  *     Check if auto-possessifying is possible    *  *        Add a character or range to a class     *
3717  *************************************************/  *************************************************/
3718    
3719  /* This function is called for unlimited repeats of certain items, to see  /* This function packages up the logic of adding a character or range of
3720  whether the next thing could possibly match the repeated item. If not, it makes  characters to a class. The character values in the arguments will be within the
3721  sense to automatically possessify the repeated item.  valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3722    mutually recursive with the function immediately below.
3723    
3724  Arguments:  Arguments:
3725    op_code       the repeated op code    classbits     the bit map for characters < 256
3726    this          data for this item, depends on the opcode    uchardptr     points to the pointer for extra data
3727    utf8          TRUE in UTF-8 mode    options       the options word
   utf8_char     used for utf8 character bytes, NULL if not relevant  
   ptr           next character in pattern  
   options       options bits  
3728    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
3729      start         start of range character
3730      end           end of range character
3731    
3732  Returns:        TRUE if possessifying is wanted  Returns:        the number of < 256 characters added
3733                    the pointer to extra data is updated
3734  */  */
3735    
3736  static BOOL  static int
3737  check_auto_possessive(int op_code, int item, BOOL utf8, uschar *utf8_char,  add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3738    const uschar *ptr, int options, compile_data *cd)    compile_data *cd, pcre_uint32 start, pcre_uint32 end)
3739  {  {
3740  int next;  pcre_uint32 c;
3741    int n8 = 0;
3742    
3743  /* Skip whitespace and comments in extended mode */  /* If caseless matching is required, scan the range and process alternate
3744    cases. In Unicode, there are 8-bit characters that have alternate cases that
3745    are greater than 255 and vice-versa. Sometimes we can just extend the original
3746    range. */
3747    
3748  if ((options & PCRE_EXTENDED) != 0)  if ((options & PCRE_CASELESS) != 0)
3749    {    {
3750    for (;;)  #ifdef SUPPORT_UCP
3751      if ((options & PCRE_UTF8) != 0)
3752      {      {
3753      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      int rc;
3754      if (*ptr == CHAR_NUMBER_SIGN)      pcre_uint32 oc, od;
3755    
3756        options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3757        c = start;
3758    
3759        while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3760        {        {
3761        while (*(++ptr) != 0)        /* Handle a single character that has more than one other case. */
         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }  
       }  
     else break;  
     }  
   }  
3762    
3763  /* If the next item is one that we can handle, get its value. A non-negative        if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3764  value is a character, a negative value is an escape value. */          PRIV(ucd_caseless_sets) + rc, oc);
3765    
3766  if (*ptr == CHAR_BACKSLASH)        /* Do nothing if the other case range is within the original range. */
   {  
   int temperrorcode = 0;  
   next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);  
   if (temperrorcode != 0) return FALSE;  
   ptr++;    /* Point after the escape sequence */  
   }  
3767    
3768  else if ((cd->ctypes[*ptr] & ctype_meta) == 0)        else if (oc >= start && od <= end) continue;
   {  
 #ifdef SUPPORT_UTF8  
   if (utf8) { GETCHARINC(next, ptr); } else  
 #endif  
   next = *ptr++;  
   }  
3769    
3770  else return FALSE;        /* Extend the original range if there is overlap, noting that if oc < c, we
3771          can't have od > end because a subrange is always shorter than the basic
3772          range. Otherwise, use a recursive call to add the additional range. */
3773    
3774          else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3775          else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3776          else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3777          }
3778        }
3779      else
3780    #endif  /* SUPPORT_UCP */
3781    
3782  /* Skip whitespace and comments in extended mode */    /* Not UTF-mode, or no UCP */
3783    
3784  if ((options & PCRE_EXTENDED) != 0)    for (c = start; c <= end && c < 256; c++)
   {  
   for (;;)  
3785      {      {
3786      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      SETBIT(classbits, cd->fcc[c]);
3787      if (*ptr == CHAR_NUMBER_SIGN)      n8++;
       {  
       while (*(++ptr) != 0)  
         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }  
       }  
     else break;  
3788      }      }
3789    }    }
3790    
3791  /* If the next thing is itself optional, we have to give up. */  /* Now handle the original range. Adjust the final value according to the bit
3792    length - this means that the same lists of (e.g.) horizontal spaces can be used
3793    in all cases. */
3794    
3795  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  #if defined COMPILE_PCRE8
3796    strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)  #ifdef SUPPORT_UTF
3797      return FALSE;    if ((options & PCRE_UTF8) == 0)
3798    #endif
3799      if (end > 0xff) end = 0xff;
3800    
3801  /* Now compare the next item with the previous opcode. If the previous is a  #elif defined COMPILE_PCRE16
3802  positive single character match, "item" either contains the character or, if  #ifdef SUPPORT_UTF
3803  "item" is greater than 127 in utf8 mode, the character's bytes are in    if ((options & PCRE_UTF16) == 0)
3804  utf8_char. */  #endif
3805      if (end > 0xffff) end = 0xffff;
3806    
3807    #endif /* COMPILE_PCRE[8|16] */
3808    
3809  /* Handle cases when the next item is a character. */  /* If all characters are less than 256, use the bit map. Otherwise use extra
3810    data. */
3811    
3812  if (next >= 0) switch(op_code)  if (end < 0x100)
3813    {    {
3814    case OP_CHAR:    for (c = start; c <= end; c++)
 #ifdef SUPPORT_UTF8  
   if (utf8 && item > 127) { GETCHAR(item, utf8_char); }  
 #else  
   (void)(utf8_char);  /* Keep compiler happy by referencing function argument */  
 #endif  
   return item != next;  
   
   /* For CHARNC (caseless character) we must check the other case. If we have  
   Unicode property support, we can use it to test the other case of  
   high-valued characters. */  
   
   case OP_CHARNC:  
 #ifdef SUPPORT_UTF8  
   if (utf8 && item > 127) { GETCHAR(item, utf8_char); }  
 #endif  
   if (item == next) return FALSE;  
 #ifdef SUPPORT_UTF8  
   if (utf8)  
3815      {      {
3816      unsigned int othercase;      n8++;
3817      if (next < 128) othercase = cd->fcc[next]; else      SETBIT(classbits, c);
 #ifdef SUPPORT_UCP  
     othercase = UCD_OTHERCASE((unsigned int)next);  
 #else  
     othercase = NOTACHAR;  
 #endif  
     return (unsigned int)item != othercase;  
3818      }      }
3819    else    }
 #endif  /* SUPPORT_UTF8 */  
   return (item != cd->fcc[next]);  /* Non-UTF-8 mode */  
3820    
3821    /* For OP_NOT, "item" must be a single-byte character. */  else
3822      {
3823      pcre_uchar *uchardata = *uchardptr;
3824    
3825    case OP_NOT:  #ifdef SUPPORT_UTF
3826    if (item == next) return TRUE;    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
   if ((options & PCRE_CASELESS) == 0) return FALSE;  
 #ifdef SUPPORT_UTF8  
   if (utf8)  
3827      {      {
3828      unsigned int othercase;      if (start < end)
3829      if (next < 128) othercase = cd->fcc[next]; else        {
3830  #ifdef SUPPORT_UCP        *uchardata++ = XCL_RANGE;
3831      othercase = UCD_OTHERCASE(next);        uchardata += PRIV(ord2utf)(start, uchardata);
3832  #else        uchardata += PRIV(ord2utf)(end, uchardata);
3833      othercase = NOTACHAR;        }
3834  #endif      else if (start == end)
3835      return (unsigned int)item == othercase;        {
3836          *uchardata++ = XCL_SINGLE;
3837          uchardata += PRIV(ord2utf)(start, uchardata);
3838          }
3839      }      }
3840    else    else
3841  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF */
   return (item == cd->fcc[next]);  /* Non-UTF-8 mode */  
   
   case OP_DIGIT:  
   return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;  
   
   case OP_NOT_DIGIT:  
   return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;  
   
   case OP_WHITESPACE:  
   return next > 127 || (cd->ctypes[next] & ctype_space) == 0;  
   
   case OP_NOT_WHITESPACE:  
   return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;  
   
   case OP_WORDCHAR:  
   return next > 127 || (cd->ctypes[next] & ctype_word) == 0;