/[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 745 by ph10, Mon Nov 14 11:41:03 2011 UTC revision 1631 by ph10, Wed Feb 10 19:13:17 2016 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2011 University of Cambridge             Copyright (c) 1997-2016 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 47  supporting internal functions that are n Line 47  supporting internal functions that are n
47  #endif  #endif
48    
49  #define NLBLOCK cd             /* Block containing newline information */  #define NLBLOCK cd             /* Block containing newline information */
50  #define PSSTART start_pattern  /* Field containing processed string start */  #define PSSTART start_pattern  /* Field containing pattern start */
51  #define PSEND   end_pattern    /* Field containing processed string end */  #define PSEND   end_pattern    /* Field containing pattern end */
52    
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  /* The overrun tests check for a slightly smaller size so that they detect the
126  overrun before it actually does run off the end of the data block. */  overrun before it actually does run off the end of the data block. */
127    
128  #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)  #define WORK_SIZE_SAFETY_MARGIN (100)
129    
130    /* Private flags added to firstchar and reqchar. */
131    
132    #define REQ_CASELESS    (1 << 0)        /* Indicates caselessness */
133    #define REQ_VARY        (1 << 1)        /* Reqchar followed non-literal item */
134    /* Negative values for the firstchar and reqchar flags */
135    #define REQ_UNSET       (-2)
136    #define REQ_NONE        (-1)
137    
138    /* Repeated character flags. */
139    
140    #define UTF_LENGTH     0x10000000l      /* The char contains its length. */
141    
142  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
143  are simple data values; negative values are for special things like \d and so  are simple data values; negative values are for special things like \d and so
# Line 133  static const short int escapes[] = { Line 174  static const short int escapes[] = {
174       -ESC_Z,                  CHAR_LEFT_SQUARE_BRACKET,       -ESC_Z,                  CHAR_LEFT_SQUARE_BRACKET,
175       CHAR_BACKSLASH,          CHAR_RIGHT_SQUARE_BRACKET,       CHAR_BACKSLASH,          CHAR_RIGHT_SQUARE_BRACKET,
176       CHAR_CIRCUMFLEX_ACCENT,  CHAR_UNDERSCORE,       CHAR_CIRCUMFLEX_ACCENT,  CHAR_UNDERSCORE,
177       CHAR_GRAVE_ACCENT,       7,       CHAR_GRAVE_ACCENT,       ESC_a,
178       -ESC_b,                  0,       -ESC_b,                  0,
179       -ESC_d,                  ESC_e,       -ESC_d,                  ESC_e,
180       ESC_f,                   0,       ESC_f,                   0,
# Line 161  static const short int escapes[] = { Line 202  static const short int escapes[] = {
202  /*  68 */     0,     0,    '|',     ',',    '%',   '_',    '>',    '?',  /*  68 */     0,     0,    '|',     ',',    '%',   '_',    '>',    '?',
203  /*  70 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  70 */     0,     0,      0,       0,      0,     0,      0,      0,
204  /*  78 */     0,   '`',    ':',     '#',    '@',  '\'',    '=',    '"',  /*  78 */     0,   '`',    ':',     '#',    '@',  '\'',    '=',    '"',
205  /*  80 */     0,     7, -ESC_b,       0, -ESC_d, ESC_e,  ESC_f,      0,  /*  80 */     0, ESC_a, -ESC_b,       0, -ESC_d, ESC_e,  ESC_f,      0,
206  /*  88 */-ESC_h,     0,      0,     '{',      0,     0,      0,      0,  /*  88 */-ESC_h,     0,      0,     '{',      0,     0,      0,      0,
207  /*  90 */     0,     0, -ESC_k,     'l',      0, ESC_n,      0, -ESC_p,  /*  90 */     0,     0, -ESC_k,       0,      0, ESC_n,      0, -ESC_p,
208  /*  98 */     0, ESC_r,      0,     '}',      0,     0,      0,      0,  /*  98 */     0, ESC_r,      0,     '}',      0,     0,      0,      0,
209  /*  A0 */     0,   '~', -ESC_s, ESC_tee,      0,-ESC_v, -ESC_w,      0,  /*  A0 */     0,   '~', -ESC_s, ESC_tee,      0,-ESC_v, -ESC_w,      0,
210  /*  A8 */     0,-ESC_z,      0,       0,      0,   '[',      0,      0,  /*  A8 */     0,-ESC_z,      0,       0,      0,   '[',      0,      0,
# Line 178  static const short int escapes[] = { Line 219  static const short int escapes[] = {
219  /*  F0 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  F0 */     0,     0,      0,       0,      0,     0,      0,      0,
220  /*  F8 */     0,     0,      0,       0,      0,     0,      0,      0  /*  F8 */     0,     0,      0,       0,      0,     0,      0,      0
221  };  };
222    
223    /* We also need a table of characters that may follow \c in an EBCDIC
224    environment for characters 0-31. */
225    
226    static unsigned char ebcdic_escape_c[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
227    
228  #endif  #endif
229    
230    
# Line 219  static const verbitem verbs[] = { Line 266  static const verbitem verbs[] = {
266  static const int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
267    
268    
269    /* Substitutes for [[:<:]] and [[:>:]], which mean start and end of word in
270    another regex library. */
271    
272    static const pcre_uchar sub_start_of_word[] = {
273      CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
274      CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w, CHAR_RIGHT_PARENTHESIS, '\0' };
275    
276    static const pcre_uchar sub_end_of_word[] = {
277      CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
278      CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w,
279      CHAR_RIGHT_PARENTHESIS, '\0' };
280    
281    
282  /* Tables of names of POSIX character classes and their lengths. The names are  /* Tables of names of POSIX character classes and their lengths. The names are
283  now all in a single string, to reduce the number of relocations when a shared  now all in a single string, to reduce the number of relocations when a shared
284  library is dynamically loaded. The list of lengths is terminated by a zero  library is dynamically loaded. The list of lengths is terminated by a zero
285  length entry. The first three must be alpha, lower, upper, as this is assumed  length entry. The first three must be alpha, lower, upper, as this is assumed
286  for handling case independence. */  for handling case independence. The indices for graph, print, and punct are
287    needed, so identify them. */
288    
289  static const char posix_names[] =  static const char posix_names[] =
290    STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0    STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
# Line 231  static const char posix_names[] = Line 292  static const char posix_names[] =
292    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
293    STRING_word0  STRING_xdigit;    STRING_word0  STRING_xdigit;
294    
295  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
296    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 };
297    
298    #define PC_GRAPH  8
299    #define PC_PRINT  9
300    #define PC_PUNCT 10
301    
302    
303  /* 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
304  base map, with an optional addition or removal of another map. Then, for some  base map, with an optional addition or removal of another map. Then, for some
305  classes, there is some additional tweaking: for [:blank:] the vertical space  classes, there is some additional tweaking: for [:blank:] the vertical space
# Line 261  static const int posix_class_maps[] = { Line 327  static const int posix_class_maps[] = {
327    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
328  };  };
329    
330  /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class  /* Table of substitutes for \d etc when PCRE_UCP is set. They are replaced by
331  substitutes must be in the order of the names, defined above, and there are  Unicode property escapes. */
 both positive and negative cases. NULL means no substitute. */  
332    
333  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
334  static const uschar *substitutes[] = {  static const pcre_uchar string_PNd[]  = {
335    (uschar *)"\\P{Nd}",    /* \D */    CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
336    (uschar *)"\\p{Nd}",    /* \d */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
337    (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */  static const pcre_uchar string_pNd[]  = {
338    (uschar *)"\\p{Xsp}",   /* \s */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
339    (uschar *)"\\P{Xwd}",   /* \W */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
340    (uschar *)"\\p{Xwd}"    /* \w */  static const pcre_uchar string_PXsp[] = {
341      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
342      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
343    static const pcre_uchar string_pXsp[] = {
344      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
345      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
346    static const pcre_uchar string_PXwd[] = {
347      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
348      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
349    static const pcre_uchar string_pXwd[] = {
350      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
351      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
352    
353    static const pcre_uchar *substitutes[] = {
354      string_PNd,           /* \D */
355      string_pNd,           /* \d */
356      string_PXsp,          /* \S */   /* Xsp is Perl space, but from 8.34, Perl */
357      string_pXsp,          /* \s */   /* space and POSIX space are the same. */
358      string_PXwd,          /* \W */
359      string_pXwd           /* \w */
360  };  };
361    
362  static const uschar *posix_substitutes[] = {  /* The POSIX class substitutes must be in the order of the POSIX class names,
363    (uschar *)"\\p{L}",     /* alpha */  defined above, and there are both positive and negative cases. NULL means no
364    (uschar *)"\\p{Ll}",    /* lower */  general substitute of a Unicode property escape (\p or \P). However, for some
365    (uschar *)"\\p{Lu}",    /* upper */  POSIX classes (e.g. graph, print, punct) a special property code is compiled
366    (uschar *)"\\p{Xan}",   /* alnum */  directly. */
367    NULL,                   /* ascii */  
368    (uschar *)"\\h",        /* blank */  static const pcre_uchar string_pL[] =   {
369    NULL,                   /* cntrl */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
370    (uschar *)"\\p{Nd}",    /* digit */    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
371    NULL,                   /* graph */  static const pcre_uchar string_pLl[] =  {
372    NULL,                   /* print */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
373    NULL,                   /* punct */    CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
374    (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */  static const pcre_uchar string_pLu[] =  {
375    (uschar *)"\\p{Xwd}",   /* word */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
376    NULL,                   /* xdigit */    CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
377    static const pcre_uchar string_pXan[] = {
378      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
379      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
380    static const pcre_uchar string_h[] =    {
381      CHAR_BACKSLASH, CHAR_h, '\0' };
382    static const pcre_uchar string_pXps[] = {
383      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
384      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
385    static const pcre_uchar string_PL[] =   {
386      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
387      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
388    static const pcre_uchar string_PLl[] =  {
389      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
390      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
391    static const pcre_uchar string_PLu[] =  {
392      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
393      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
394    static const pcre_uchar string_PXan[] = {
395      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
396      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
397    static const pcre_uchar string_H[] =    {
398      CHAR_BACKSLASH, CHAR_H, '\0' };
399    static const pcre_uchar string_PXps[] = {
400      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
401      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
402    
403    static const pcre_uchar *posix_substitutes[] = {
404      string_pL,            /* alpha */
405      string_pLl,           /* lower */
406      string_pLu,           /* upper */
407      string_pXan,          /* alnum */
408      NULL,                 /* ascii */
409      string_h,             /* blank */
410      NULL,                 /* cntrl */
411      string_pNd,           /* digit */
412      NULL,                 /* graph */
413      NULL,                 /* print */
414      NULL,                 /* punct */
415      string_pXps,          /* space */   /* Xps is POSIX space, but from 8.34 */
416      string_pXwd,          /* word  */   /* Perl and POSIX space are the same */
417      NULL,                 /* xdigit */
418    /* Negated cases */    /* Negated cases */
419    (uschar *)"\\P{L}",     /* ^alpha */    string_PL,            /* ^alpha */
420    (uschar *)"\\P{Ll}",    /* ^lower */    string_PLl,           /* ^lower */
421    (uschar *)"\\P{Lu}",    /* ^upper */    string_PLu,           /* ^upper */
422    (uschar *)"\\P{Xan}",   /* ^alnum */    string_PXan,          /* ^alnum */
423    NULL,                   /* ^ascii */    NULL,                 /* ^ascii */
424    (uschar *)"\\H",        /* ^blank */    string_H,             /* ^blank */
425    NULL,                   /* ^cntrl */    NULL,                 /* ^cntrl */
426    (uschar *)"\\P{Nd}",    /* ^digit */    string_PNd,           /* ^digit */
427    NULL,                   /* ^graph */    NULL,                 /* ^graph */
428    NULL,                   /* ^print */    NULL,                 /* ^print */
429    NULL,                   /* ^punct */    NULL,                 /* ^punct */
430    (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */    string_PXps,          /* ^space */  /* Xps is POSIX space, but from 8.34 */
431    (uschar *)"\\P{Xwd}",   /* ^word */    string_PXwd,          /* ^word */   /* Perl and POSIX space are the same */
432    NULL                    /* ^xdigit */    NULL                  /* ^xdigit */
433  };  };
434  #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
435  #endif  #endif
436    
437  #define STRING(a)  # a  #define STRING(a)  # a
# Line 339  static const char error_texts[] = Line 464  static const char error_texts[] =
464    "range out of order in character class\0"    "range out of order in character class\0"
465    "nothing to repeat\0"    "nothing to repeat\0"
466    /* 10 */    /* 10 */
467    "operand of unlimited repeat could match the empty string\0"  /** DEAD **/    "internal error: invalid forward reference offset\0"
468    "internal error: unexpected repeat\0"    "internal error: unexpected repeat\0"
469    "unrecognized character after (? or (?-\0"    "unrecognized character after (? or (?-\0"
470    "POSIX named classes are supported only within a class\0"    "POSIX named classes are supported only within a class\0"
# Line 365  static const char error_texts[] = Line 490  static const char error_texts[] =
490    /* 30 */    /* 30 */
491    "unknown POSIX class name\0"    "unknown POSIX class name\0"
492    "POSIX collating elements are not supported\0"    "POSIX collating elements are not supported\0"
493    "this version of PCRE is not compiled with PCRE_UTF8 support\0"    "this version of PCRE is compiled without UTF support\0"
494    "spare error\0"  /** DEAD **/    "spare error\0"  /** DEAD **/
495    "character value in \\x{...} sequence is too large\0"    "character value in \\x{} or \\o{} is too large\0"
496    /* 35 */    /* 35 */
497    "invalid condition (?(0)\0"    "invalid condition (?(0)\0"
498    "\\C not allowed in lookbehind assertion\0"    "\\C not allowed in lookbehind assertion\0"
# Line 388  static const char error_texts[] = Line 513  static const char error_texts[] =
513    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
514    /* 50 */    /* 50 */
515    "repeated subpattern is too long\0"    /** DEAD **/    "repeated subpattern is too long\0"    /** DEAD **/
516    "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"
517    "internal error: overran compiling workspace\0"    "internal error: overran compiling workspace\0"
518    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
519    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
# Line 399  static const char error_texts[] = Line 524  static const char error_texts[] =
524    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
525    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
526    /* 60 */    /* 60 */
527    "(*VERB) not recognized\0"    "(*VERB) not recognized or malformed\0"
528    "number is too big\0"    "number is too big\0"
529    "subpattern name expected\0"    "subpattern name expected\0"
530    "digit expected after (?+\0"    "digit expected after (?+\0"
# Line 407  static const char error_texts[] = Line 532  static const char error_texts[] =
532    /* 65 */    /* 65 */
533    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
534    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
535    "this version of PCRE is not compiled with PCRE_UCP support\0"    "this version of PCRE is not compiled with Unicode property support\0"
536    #ifndef EBCDIC
537    "\\c must be followed by an ASCII character\0"    "\\c must be followed by an ASCII character\0"
538    #else
539      "\\c must be followed by a letter or one of [\\]^_?\0"
540    #endif
541    "\\k is not followed by a braced, angle-bracketed, or quoted name\0"    "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
542      /* 70 */
543      "internal error: unknown opcode in find_fixedlength()\0"
544      "\\N is not supported in a class\0"
545      "too many forward references\0"
546      "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
547      "invalid UTF-16 string\0"
548      /* 75 */
549      "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
550      "character value in \\u.... sequence is too large\0"
551      "invalid UTF-32 string\0"
552      "setting UTF is disabled by the application\0"
553      "non-hex character in \\x{} (closing brace missing?)\0"
554      /* 80 */
555      "non-octal character in \\o{} (closing brace missing?)\0"
556      "missing opening brace after \\o\0"
557      "parentheses are too deeply nested\0"
558      "invalid range in character class\0"
559      "group name must start with a non-digit\0"
560      /* 85 */
561      "parentheses are too deeply nested (stack check)\0"
562      "digits missing in \\x{} or \\o{}\0"
563      "regular expression is too complicated\0"
564    ;    ;
565    
566  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 428  For convenience, we use the same bit def Line 579  For convenience, we use the same bit def
579    
580  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
581    
582    /* Using a simple comparison for decimal numbers rather than a memory read
583    is much faster, and the resulting code is simpler (the compiler turns it
584    into a subtraction and unsigned comparison). */
585    
586    #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
587    
588  #ifndef EBCDIC  #ifndef EBCDIC
589    
590  /* 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
591  UTF-8 mode. */  UTF-8 mode. */
592    
593  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
594    {    {
595    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
596    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */
# Line 472  static const unsigned char digitab[] = Line 629  static const unsigned char digitab[] =
629    
630  /* 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. */
631    
632  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
633    {    {
634    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
635    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */
# Line 507  static const unsigned char digitab[] = Line 664  static const unsigned char digitab[] =
664    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */
665    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */
666    
667  static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */  static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
668    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */
669    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */
670    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */
# Line 543  static const unsigned char ebcdic_charta Line 700  static const unsigned char ebcdic_charta
700  #endif  #endif
701    
702    
703  /* Definition to allow mutual recursion */  /* This table is used to check whether auto-possessification is possible
704    between adjacent character-type opcodes. The left-hand (repeated) opcode is
705    used to select the row, and the right-hand opcode is use to select the column.
706    A value of 1 means that auto-possessification is OK. For example, the second
707    value in the first row means that \D+\d can be turned into \D++\d.
708    
709    The Unicode property types (\P and \p) have to be present to fill out the table
710    because of what their opcode values are, but the table values should always be
711    zero because property types are handled separately in the code. The last four
712    columns apply to items that cannot be repeated, so there is no need to have
713    rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
714    *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
715    
716    #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
717    #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
718    
719    static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
720    /* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
721      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
722      { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
723      { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
724      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
725      { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
726      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
727      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
728      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
729      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
730      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
731      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
732      { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
733      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
734      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
735      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
736      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
737      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
738    };
739    
740  static BOOL  
741    compile_regex(int, uschar **, const uschar **, int *, BOOL, BOOL, int, int,  /* This table is used to check whether auto-possessification is possible
742      int *, int *, branch_chain *, compile_data *, int *);  between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
743    left-hand (repeated) opcode is used to select the row, and the right-hand
744    opcode is used to select the column. The values are as follows:
745    
746      0   Always return FALSE (never auto-possessify)
747      1   Character groups are distinct (possessify if both are OP_PROP)
748      2   Check character categories in the same group (general or particular)
749      3   TRUE if the two opcodes are not the same (PROP vs NOTPROP)
750    
751      4   Check left general category vs right particular category
752      5   Check right general category vs left particular category
753    
754      6   Left alphanum vs right general category
755      7   Left space vs right general category
756      8   Left word vs right general category
757    
758      9   Right alphanum vs left general category
759     10   Right space vs left general category
760     11   Right word vs left general category
761    
762     12   Left alphanum vs right particular category
763     13   Left space vs right particular category
764     14   Left word vs right particular category
765    
766     15   Right alphanum vs left particular category
767     16   Right space vs left particular category
768     17   Right word vs left particular category
769    */
770    
771    static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
772    /* ANY LAMP GC  PC  SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
773      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_ANY */
774      { 0,  3,  0,  0,  0,    3,    1,      1,   0,    0,   0 },  /* PT_LAMP */
775      { 0,  0,  2,  4,  0,    9,   10,     10,  11,    0,   0 },  /* PT_GC */
776      { 0,  0,  5,  2,  0,   15,   16,     16,  17,    0,   0 },  /* PT_PC */
777      { 0,  0,  0,  0,  2,    0,    0,      0,   0,    0,   0 },  /* PT_SC */
778      { 0,  3,  6, 12,  0,    3,    1,      1,   0,    0,   0 },  /* PT_ALNUM */
779      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_SPACE */
780      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_PXSPACE */
781      { 0,  0,  8, 14,  0,    0,    1,      1,   3,    0,   0 },  /* PT_WORD */
782      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_CLIST */
783      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   3 }   /* PT_UCNC */
784    };
785    
786    /* This table is used to check whether auto-possessification is possible
787    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
788    specifies a general category and the other specifies a particular category. The
789    row is selected by the general category and the column by the particular
790    category. The value is 1 if the particular category is not part of the general
791    category. */
792    
793    static const pcre_uint8 catposstab[7][30] = {
794    /* 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 */
795      { 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 */
796      { 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 */
797      { 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 */
798      { 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 */
799      { 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 */
800      { 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 */
801      { 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 */
802    };
803    
804    /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
805    a general or particular category. The properties in each row are those
806    that apply to the character set in question. Duplication means that a little
807    unnecessary work is done when checking, but this keeps things much simpler
808    because they can all use the same code. For more details see the comment where
809    this table is used.
810    
811    Note: SPACE and PXSPACE used to be different because Perl excluded VT from
812    "space", but from Perl 5.18 it's included, so both categories are treated the
813    same here. */
814    
815    static const pcre_uint8 posspropstab[3][4] = {
816      { ucp_L, ucp_N, ucp_N, ucp_Nl },  /* ALNUM, 3rd and 4th values redundant */
817      { ucp_Z, ucp_Z, ucp_C, ucp_Cc },  /* SPACE and PXSPACE, 2nd value redundant */
818      { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
819    };
820    
821    /* This table is used when converting repeating opcodes into possessified
822    versions as a result of an explicit possessive quantifier such as ++. A zero
823    value means there is no possessified version - in those cases the item in
824    question must be wrapped in ONCE brackets. The table is truncated at OP_CALLOUT
825    because all relevant opcodes are less than that. */
826    
827    static const pcre_uint8 opcode_possessify[] = {
828      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0 - 15  */
829      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 16 - 31 */
830    
831      0,                       /* NOTI */
832      OP_POSSTAR, 0,           /* STAR, MINSTAR */
833      OP_POSPLUS, 0,           /* PLUS, MINPLUS */
834      OP_POSQUERY, 0,          /* QUERY, MINQUERY */
835      OP_POSUPTO, 0,           /* UPTO, MINUPTO */
836      0,                       /* EXACT */
837      0, 0, 0, 0,              /* POS{STAR,PLUS,QUERY,UPTO} */
838    
839      OP_POSSTARI, 0,          /* STARI, MINSTARI */
840      OP_POSPLUSI, 0,          /* PLUSI, MINPLUSI */
841      OP_POSQUERYI, 0,         /* QUERYI, MINQUERYI */
842      OP_POSUPTOI, 0,          /* UPTOI, MINUPTOI */
843      0,                       /* EXACTI */
844      0, 0, 0, 0,              /* POS{STARI,PLUSI,QUERYI,UPTOI} */
845    
846      OP_NOTPOSSTAR, 0,        /* NOTSTAR, NOTMINSTAR */
847      OP_NOTPOSPLUS, 0,        /* NOTPLUS, NOTMINPLUS */
848      OP_NOTPOSQUERY, 0,       /* NOTQUERY, NOTMINQUERY */
849      OP_NOTPOSUPTO, 0,        /* NOTUPTO, NOTMINUPTO */
850      0,                       /* NOTEXACT */
851      0, 0, 0, 0,              /* NOTPOS{STAR,PLUS,QUERY,UPTO} */
852    
853      OP_NOTPOSSTARI, 0,       /* NOTSTARI, NOTMINSTARI */
854      OP_NOTPOSPLUSI, 0,       /* NOTPLUSI, NOTMINPLUSI */
855      OP_NOTPOSQUERYI, 0,      /* NOTQUERYI, NOTMINQUERYI */
856      OP_NOTPOSUPTOI, 0,       /* NOTUPTOI, NOTMINUPTOI */
857      0,                       /* NOTEXACTI */
858      0, 0, 0, 0,              /* NOTPOS{STARI,PLUSI,QUERYI,UPTOI} */
859    
860      OP_TYPEPOSSTAR, 0,       /* TYPESTAR, TYPEMINSTAR */
861      OP_TYPEPOSPLUS, 0,       /* TYPEPLUS, TYPEMINPLUS */
862      OP_TYPEPOSQUERY, 0,      /* TYPEQUERY, TYPEMINQUERY */
863      OP_TYPEPOSUPTO, 0,       /* TYPEUPTO, TYPEMINUPTO */
864      0,                       /* TYPEEXACT */
865      0, 0, 0, 0,              /* TYPEPOS{STAR,PLUS,QUERY,UPTO} */
866    
867      OP_CRPOSSTAR, 0,         /* CRSTAR, CRMINSTAR */
868      OP_CRPOSPLUS, 0,         /* CRPLUS, CRMINPLUS */
869      OP_CRPOSQUERY, 0,        /* CRQUERY, CRMINQUERY */
870      OP_CRPOSRANGE, 0,        /* CRRANGE, CRMINRANGE */
871      0, 0, 0, 0,              /* CRPOS{STAR,PLUS,QUERY,RANGE} */
872    
873      0, 0, 0,                 /* CLASS, NCLASS, XCLASS */
874      0, 0,                    /* REF, REFI */
875      0, 0,                    /* DNREF, DNREFI */
876      0, 0                     /* RECURSE, CALLOUT */
877    };
878    
879    
880    
# Line 570  find_error_text(int n) Line 897  find_error_text(int n)
897  const char *s = error_texts;  const char *s = error_texts;
898  for (; n > 0; n--)  for (; n > 0; n--)
899    {    {
900    while (*s++ != 0) {};    while (*s++ != CHAR_NULL) {};
901    if (*s == 0) return "Error text not found (please report)";    if (*s == CHAR_NULL) return "Error text not found (please report)";
902    }    }
903  return s;  return s;
904  }  }
905    
906    
907    
908    /*************************************************
909    *           Expand the workspace                 *
910    *************************************************/
911    
912    /* This function is called during the second compiling phase, if the number of
913    forward references fills the existing workspace, which is originally a block on
914    the stack. A larger block is obtained from malloc() unless the ultimate limit
915    has been reached or the increase will be rather small.
916    
917    Argument: pointer to the compile data block
918    Returns:  0 if all went well, else an error number
919    */
920    
921    static int
922    expand_workspace(compile_data *cd)
923    {
924    pcre_uchar *newspace;
925    int newsize = cd->workspace_size * 2;
926    
927    if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
928    if (cd->workspace_size >= COMPILE_WORK_SIZE_MAX ||
929        newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
930     return ERR72;
931    
932    newspace = (PUBL(malloc))(IN_UCHARS(newsize));
933    if (newspace == NULL) return ERR21;
934    memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
935    cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
936    if (cd->workspace_size > COMPILE_WORK_SIZE)
937      (PUBL(free))((void *)cd->start_workspace);
938    cd->start_workspace = newspace;
939    cd->workspace_size = newsize;
940    return 0;
941    }
942    
943    
944    
945  /*************************************************  /*************************************************
946  *            Check for counted repeat            *  *            Check for counted repeat            *
947  *************************************************/  *************************************************/
# Line 593  Returns:    TRUE or FALSE Line 958  Returns:    TRUE or FALSE
958  */  */
959    
960  static BOOL  static BOOL
961  is_counted_repeat(const uschar *p)  is_counted_repeat(const pcre_uchar *p)
962  {  {
963  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
964  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
965    while (IS_DIGIT(*p)) p++;
966  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
967    
968  if (*p++ != CHAR_COMMA) return FALSE;  if (*p++ != CHAR_COMMA) return FALSE;
969  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
970    
971  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
972  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
973    while (IS_DIGIT(*p)) p++;
974    
975  return (*p == CHAR_RIGHT_CURLY_BRACKET);  return (*p == CHAR_RIGHT_CURLY_BRACKET);
976  }  }
# Line 615  return (*p == CHAR_RIGHT_CURLY_BRACKET); Line 982  return (*p == CHAR_RIGHT_CURLY_BRACKET);
982  *************************************************/  *************************************************/
983    
984  /* 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
985  positive value for a simple escape such as \n, or a negative value which  positive value for a simple escape such as \n, or 0 for a data character which
986  encodes one of the more complicated things such as \d. A backreference to group  will be placed in chptr. A backreference to group n is returned as negative n.
987  n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When  When UTF-8 is enabled, a positive value greater than 255 may be returned in
988  UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,  chptr. On entry, ptr is pointing at the \. On exit, it is on the final
989  ptr is pointing at the \. On exit, it is on the final character of the escape  character of the escape sequence.
 sequence.  
990    
991  Arguments:  Arguments:
992    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
993      chptr          points to a returned data character
994    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
995    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
996    options        the options bits    options        the options bits
997    isclass        TRUE if inside a character class    isclass        TRUE if inside a character class
998    
999  Returns:         zero or positive => a data character  Returns:         zero => a data character
1000                   negative => a special escape sequence                   positive => a special escape sequence
1001                     negative => a back reference
1002                   on error, errorcodeptr is set                   on error, errorcodeptr is set
1003  */  */
1004    
1005  static int  static int
1006  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
1007    int options, BOOL isclass)    int bracount, int options, BOOL isclass)
1008  {  {
1009  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
1010  const uschar *ptr = *ptrptr + 1;  BOOL utf = (options & PCRE_UTF8) != 0;
1011  int c, i;  const pcre_uchar *ptr = *ptrptr + 1;
1012    pcre_uint32 c;
1013    int escape = 0;
1014    int i;
1015    
1016  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
1017  ptr--;                            /* Set pointer back to the last byte */  ptr--;                            /* Set pointer back to the last byte */
1018    
1019  /* 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. */
1020    
1021  if (c == 0) *errorcodeptr = ERR1;  if (c == CHAR_NULL) *errorcodeptr = ERR1;
1022    
1023  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
1024  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.
1025  Otherwise further processing may be required. */  Otherwise further processing may be required. */
1026    
1027  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1028  else if (c < CHAR_0 || c > CHAR_z) {}                     /* Not alphanumeric */  /* Not alphanumeric */
1029  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if (c < CHAR_0 || c > CHAR_z) {}
1030    else if ((i = escapes[c - CHAR_0]) != 0)
1031      { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
1032    
1033  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1034  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */  /* Not alphanumeric */
1035  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
1036    else if ((i = escapes[c - 0x48]) != 0)  { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
1037  #endif  #endif
1038    
1039  /* Escapes that need further processing, or are illegal. */  /* Escapes that need further processing, or are illegal. */
1040    
1041  else  else
1042    {    {
1043    const uschar *oldptr;    const pcre_uchar *oldptr;
1044    BOOL braced, negated;    BOOL braced, negated, overflow;
1045      int s;
1046    
1047    switch (c)    switch (c)
1048      {      {
# Line 684  else Line 1059  else
1059        {        {
1060        /* In JavaScript, \u must be followed by four hexadecimal numbers.        /* In JavaScript, \u must be followed by four hexadecimal numbers.
1061        Otherwise it is a lowercase u letter. */        Otherwise it is a lowercase u letter. */
1062        if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1063             && (digitab[ptr[3]] & ctype_xdigit) != 0 && (digitab[ptr[4]] & ctype_xdigit) != 0)          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
1064            && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
1065            && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
1066          {          {
1067          c = 0;          c = 0;
1068          for (i = 0; i < 4; ++i)          for (i = 0; i < 4; ++i)
1069            {            {
1070            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
1071  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1072            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1073            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
# Line 699  else Line 1076  else
1076            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1077  #endif  #endif
1078            }            }
1079    
1080    #if defined COMPILE_PCRE8
1081            if (c > (utf ? 0x10ffffU : 0xffU))
1082    #elif defined COMPILE_PCRE16
1083            if (c > (utf ? 0x10ffffU : 0xffffU))
1084    #elif defined COMPILE_PCRE32
1085            if (utf && c > 0x10ffffU)
1086    #endif
1087              {
1088              *errorcodeptr = ERR76;
1089              }
1090            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1091          }          }
1092        }        }
1093      else      else
# Line 725  else Line 1114  else
1114      (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
1115      number either in angle brackets or in single quotes. However, these are      number either in angle brackets or in single quotes. However, these are
1116      (possibly recursive) subroutine calls, _not_ backreferences. Just return      (possibly recursive) subroutine calls, _not_ backreferences. Just return
1117      the -ESC_g code (cf \k). */      the ESC_g code (cf \k). */
1118    
1119      case CHAR_g:      case CHAR_g:
1120      if (isclass) break;      if (isclass) break;
1121      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
1122        {        {
1123        c = -ESC_g;        escape = ESC_g;
1124        break;        break;
1125        }        }
1126    
# Line 739  else Line 1128  else
1128    
1129      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1130        {        {
1131        const uschar *p;        const pcre_uchar *p;
1132        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++)
1133          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1134        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1135          {          {
1136          c = -ESC_k;          escape = ESC_k;
1137          break;          break;
1138          }          }
1139        braced = TRUE;        braced = TRUE;
# Line 759  else Line 1148  else
1148        }        }
1149      else negated = FALSE;      else negated = FALSE;
1150    
1151      c = 0;      /* The integer range is limited by the machine's int representation. */
1152      while ((digitab[ptr[1]] & ctype_digit) != 0)      s = 0;
1153        c = c * 10 + *(++ptr) - CHAR_0;      overflow = FALSE;
1154        while (IS_DIGIT(ptr[1]))
1155      if (c < 0)   /* Integer overflow */        {
1156          if (s > INT_MAX / 10 - 1) /* Integer overflow */
1157            {
1158            overflow = TRUE;
1159            break;
1160            }
1161          s = s * 10 + (int)(*(++ptr) - CHAR_0);
1162          }
1163        if (overflow) /* Integer overflow */
1164        {        {
1165          while (IS_DIGIT(ptr[1]))
1166            ptr++;
1167        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
1168        break;        break;
1169        }        }
# Line 775  else Line 1174  else
1174        break;        break;
1175        }        }
1176    
1177      if (c == 0)      if (s == 0)
1178        {        {
1179        *errorcodeptr = ERR58;        *errorcodeptr = ERR58;
1180        break;        break;
# Line 783  else Line 1182  else
1182    
1183      if (negated)      if (negated)
1184        {        {
1185        if (c > bracount)        if (s > bracount)
1186          {          {
1187          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
1188          break;          break;
1189          }          }
1190        c = bracount - (c - 1);        s = bracount - (s - 1);
1191        }        }
1192    
1193      c = -(ESC_REF + c);      escape = -s;
1194      break;      break;
1195    
1196      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
1197      starting with one that is not zero is not straightforward. By experiment,      starting with one that is not zero is not straightforward. Perl has changed
1198      the way Perl works seems to be as follows:      over the years. Nowadays \g{} for backreferences and \o{} for octal are
1199        recommended to avoid the ambiguities in the old syntax.
1200    
1201      Outside a character class, the digits are read as a decimal number. If the      Outside a character class, the digits are read as a decimal number. If the
1202      number is less than 10, or if there are that many previous extracting      number is less than 8 (used to be 10), or if there are that many previous
1203      left brackets, then it is a back reference. Otherwise, up to three octal      extracting left brackets, then it is a back reference. Otherwise, up to
1204      digits are read to form an escaped byte. Thus \123 is likely to be octal      three octal digits are read to form an escaped byte. Thus \123 is likely to
1205      123 (cf \0123, which is octal 012 followed by the literal 3). If the octal      be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If
1206      value is greater than 377, the least significant 8 bits are taken. Inside a      the octal value is greater than 377, the least significant 8 bits are
1207      character class, \ followed by a digit is always an octal number. */      taken. \8 and \9 are treated as the literal characters 8 and 9.
1208    
1209        Inside a character class, \ followed by a digit is always either a literal
1210        8 or 9 or an octal number. */
1211    
1212      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
1213      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
# Line 812  else Line 1215  else
1215      if (!isclass)      if (!isclass)
1216        {        {
1217        oldptr = ptr;        oldptr = ptr;
1218        c -= CHAR_0;        /* The integer range is limited by the machine's int representation. */
1219        while ((digitab[ptr[1]] & ctype_digit) != 0)        s = (int)(c -CHAR_0);
1220          c = c * 10 + *(++ptr) - CHAR_0;        overflow = FALSE;
1221        if (c < 0)    /* Integer overflow */        while (IS_DIGIT(ptr[1]))
1222            {
1223            if (s > INT_MAX / 10 - 1) /* Integer overflow */
1224              {
1225              overflow = TRUE;
1226              break;
1227              }
1228            s = s * 10 + (int)(*(++ptr) - CHAR_0);
1229            }
1230          if (overflow) /* Integer overflow */
1231          {          {
1232            while (IS_DIGIT(ptr[1]))
1233              ptr++;
1234          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1235          break;          break;
1236          }          }
1237        if (c < 10 || c <= bracount)        if (s < 8 || s <= bracount)  /* Check for back reference */
1238          {          {
1239          c = -(ESC_REF + c);          escape = -s;
1240          break;          break;
1241          }          }
1242        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
1243        }        }
1244    
1245      /* Handle an octal number following \. If the first digit is 8 or 9, Perl      /* Handle a digit following \ when the number is not a back reference. If
1246      generates a binary zero byte and treats the digit as a following literal.      the first digit is 8 or 9, Perl used to generate a binary zero byte and
1247      Thus we have to pull back the pointer by one. */      then treat the digit as a following literal. At least by Perl 5.18 this
1248        changed so as not to insert the binary zero. */
1249    
1250      if ((c = *ptr) >= CHAR_8)      if ((c = *ptr) >= CHAR_8) break;
1251        {  
1252        ptr--;      /* Fall through with a digit less than 8 */
       c = 0;  
       break;  
       }  
1253    
1254      /* \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
1255      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
1256      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
1257      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,
1258      than 3 octal digits. */      but no more than 3 octal digits. */
1259    
1260      case CHAR_0:      case CHAR_0:
1261      c -= CHAR_0;      c -= CHAR_0;
1262      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1263          c = c * 8 + *(++ptr) - CHAR_0;          c = c * 8 + *(++ptr) - CHAR_0;
1264      if (!utf8 && c > 255) *errorcodeptr = ERR51;  #ifdef COMPILE_PCRE8
1265        if (!utf && c > 0xff) *errorcodeptr = ERR51;
1266    #endif
1267        break;
1268    
1269        /* \o is a relatively new Perl feature, supporting a more general way of
1270        specifying character codes in octal. The only supported form is \o{ddd}. */
1271    
1272        case CHAR_o:
1273        if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
1274        if (ptr[2] == CHAR_RIGHT_CURLY_BRACKET) *errorcodeptr = ERR86; else
1275          {
1276          ptr += 2;
1277          c = 0;
1278          overflow = FALSE;
1279          while (*ptr >= CHAR_0 && *ptr <= CHAR_7)
1280            {
1281            register pcre_uint32 cc = *ptr++;
1282            if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1283    #ifdef COMPILE_PCRE32
1284            if (c >= 0x20000000l) { overflow = TRUE; break; }
1285    #endif
1286            c = (c << 3) + cc - CHAR_0 ;
1287    #if defined COMPILE_PCRE8
1288            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1289    #elif defined COMPILE_PCRE16
1290            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1291    #elif defined COMPILE_PCRE32
1292            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1293    #endif
1294            }
1295          if (overflow)
1296            {
1297            while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++;
1298            *errorcodeptr = ERR34;
1299            }
1300          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1301            {
1302            if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1303            }
1304          else *errorcodeptr = ERR80;
1305          }
1306      break;      break;
1307    
1308      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
1309      than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is      numbers. Otherwise it is a lowercase x letter. */
     treated as a data character. */  
1310    
1311      case CHAR_x:      case CHAR_x:
1312      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1313        {        {
1314        /* In JavaScript, \x must be followed by two hexadecimal numbers.        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1315        Otherwise it is a lowercase x letter. */          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
       if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0)  
1316          {          {
1317          c = 0;          c = 0;
1318          for (i = 0; i < 2; ++i)          for (i = 0; i < 2; ++i)
1319            {            {
1320            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
1321  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1322            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1323            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
# Line 876  else Line 1327  else
1327  #endif  #endif
1328            }            }
1329          }          }
1330        break;        }    /* End JavaScript handling */
       }  
1331    
1332      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      /* Handle \x in Perl's style. \x{ddd} is a character number which can be
1333        {      greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
1334        const uschar *pt = ptr + 2;      digits. If not, { used to be treated as a data character. However, Perl
1335        int count = 0;      seems to read hex digits up to the first non-such, and ignore the rest, so
1336        that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
1337        now gives an error. */
1338    
1339        c = 0;      else
1340        while ((digitab[*pt] & ctype_xdigit) != 0)        {
1341          if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1342          {          {
1343          register int cc = *pt++;          ptr += 2;
1344          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1345          count++;            {
1346              *errorcodeptr = ERR86;
1347              break;
1348              }
1349            c = 0;
1350            overflow = FALSE;
1351            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)
1352              {
1353              register pcre_uint32 cc = *ptr++;
1354              if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1355    
1356    #ifdef COMPILE_PCRE32
1357              if (c >= 0x10000000l) { overflow = TRUE; break; }
1358    #endif
1359    
1360  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1361          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1362          c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1363  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1364          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 */
1365          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1366  #endif  #endif
         }  
1367    
1368        if (*pt == CHAR_RIGHT_CURLY_BRACKET)  #if defined COMPILE_PCRE8
1369          {            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1370          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;  #elif defined COMPILE_PCRE16
1371          ptr = pt;            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1372          break;  #elif defined COMPILE_PCRE32
1373          }            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1374    #endif
1375              }
1376    
1377        /* If the sequence of hex digits does not end with '}', then we don't          if (overflow)
1378        recognize this construct; fall through to the normal \x handling. */            {
1379        }            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++;
1380              *errorcodeptr = ERR34;
1381              }
1382    
1383      /* Read just a single-byte hex-defined char */          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1384              {
1385              if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1386              }
1387    
1388      c = 0;          /* If the sequence of hex digits does not end with '}', give an error.
1389      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)          We used just to recognize this construct and fall through to the normal
1390        {          \x handling, but nowadays Perl gives an error, which seems much more
1391        int cc;                                  /* Some compilers don't like */          sensible, so we do too. */
1392        cc = *(++ptr);                           /* ++ in initializers */  
1393            else *errorcodeptr = ERR79;
1394            }   /* End of \x{} processing */
1395    
1396          /* Read a single-byte hex-defined char (up to two hex digits after \x) */
1397    
1398          else
1399            {
1400            c = 0;
1401            while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1402              {
1403              pcre_uint32 cc;                          /* Some compilers don't like */
1404              cc = *(++ptr);                           /* ++ in initializers */
1405  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1406        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
1407        c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1408  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1409        if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */            if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */
1410        c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1411  #endif  #endif
1412        }            }
1413            }     /* End of \xdd handling */
1414          }       /* End of Perl-style \x handling */
1415      break;      break;
1416    
1417      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
# Line 935  else Line 1421  else
1421    
1422      case CHAR_c:      case CHAR_c:
1423      c = *(++ptr);      c = *(++ptr);
1424      if (c == 0)      if (c == CHAR_NULL)
1425        {        {
1426        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1427        break;        break;
# Line 950  else Line 1436  else
1436      c ^= 0x40;      c ^= 0x40;
1437  #else             /* EBCDIC coding */  #else             /* EBCDIC coding */
1438      if (c >= CHAR_a && c <= CHAR_z) c += 64;      if (c >= CHAR_a && c <= CHAR_z) c += 64;
1439      c ^= 0xC0;      if (c == CHAR_QUESTION_MARK)
1440          c = ('\\' == 188 && '`' == 74)? 0x5f : 0xff;
1441        else
1442          {
1443          for (i = 0; i < 32; i++)
1444            {
1445            if (c == ebcdic_escape_c[i]) break;
1446            }
1447          if (i < 32) c = i; else *errorcodeptr = ERR68;
1448          }
1449  #endif  #endif
1450      break;      break;
1451    
# Line 975  else Line 1470  else
1470  newline". PCRE does not support \N{name}. However, it does support  newline". PCRE does not support \N{name}. However, it does support
1471  quantification such as \N{2,3}. */  quantification such as \N{2,3}. */
1472    
1473  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&  if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1474       !is_counted_repeat(ptr+2))       !is_counted_repeat(ptr+2))
1475    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
1476    
1477  /* If PCRE_UCP is set, we change the values for \d etc. */  /* If PCRE_UCP is set, we change the values for \d etc. */
1478    
1479  if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)  if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
1480    c -= (ESC_DU - ESC_D);    escape += (ESC_DU - ESC_D);
1481    
1482  /* Set the pointer to the final character before returning. */  /* Set the pointer to the final character before returning. */
1483    
1484  *ptrptr = ptr;  *ptrptr = ptr;
1485  return c;  *chptr = c;
1486    return escape;
1487  }  }
1488    
1489    
# Line 1005  escape sequence. Line 1501  escape sequence.
1501  Argument:  Argument:
1502    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
1503    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
1504    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
1505      pdataptr       points to an unsigned int that is set to the detailed property value
1506    errorcodeptr   points to the error code variable    errorcodeptr   points to the error code variable
1507    
1508  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
1509  */  */
1510    
1511  static int  static BOOL
1512  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1513      unsigned int *pdataptr, int *errorcodeptr)
1514  {  {
1515  int c, i, bot, top;  pcre_uchar c;
1516  const uschar *ptr = *ptrptr;  int i, bot, top;
1517  char name[32];  const pcre_uchar *ptr = *ptrptr;
1518    pcre_uchar name[32];
1519    
1520  c = *(++ptr);  c = *(++ptr);
1521  if (c == 0) goto ERROR_RETURN;  if (c == CHAR_NULL) goto ERROR_RETURN;
1522    
1523  *negptr = FALSE;  *negptr = FALSE;
1524    
# Line 1033  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1532  if (c == CHAR_LEFT_CURLY_BRACKET)
1532      *negptr = TRUE;      *negptr = TRUE;
1533      ptr++;      ptr++;
1534      }      }
1535    for (i = 0; i < (int)sizeof(name) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1536      {      {
1537      c = *(++ptr);      c = *(++ptr);
1538      if (c == 0) goto ERROR_RETURN;      if (c == CHAR_NULL) goto ERROR_RETURN;
1539      if (c == CHAR_RIGHT_CURLY_BRACKET) break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1540      name[i] = c;      name[i] = c;
1541      }      }
# Line 1057  else Line 1556  else
1556  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1557    
1558  bot = 0;  bot = 0;
1559  top = _pcre_utt_size;  top = PRIV(utt_size);
1560    
1561  while (bot < top)  while (bot < top)
1562    {    {
1563      int r;
1564    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1565    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1566    if (c == 0)    if (r == 0)
1567      {      {
1568      *dptr = _pcre_utt[i].value;      *ptypeptr = PRIV(utt)[i].type;
1569      return _pcre_utt[i].type;      *pdataptr = PRIV(utt)[i].value;
1570        return TRUE;
1571      }      }
1572    if (c > 0) bot = i + 1; else top = i;    if (r > 0) bot = i + 1; else top = i;
1573    }    }
1574    
1575  *errorcodeptr = ERR47;  *errorcodeptr = ERR47;
1576  *ptrptr = ptr;  *ptrptr = ptr;
1577  return -1;  return FALSE;
1578    
1579  ERROR_RETURN:  ERROR_RETURN:
1580  *errorcodeptr = ERR46;  *errorcodeptr = ERR46;
1581  *ptrptr = ptr;  *ptrptr = ptr;
1582  return -1;  return FALSE;
1583  }  }
1584  #endif  #endif
1585    
1586    
1587    
   
1588  /*************************************************  /*************************************************
1589  *         Read repeat counts                     *  *         Read repeat counts                     *
1590  *************************************************/  *************************************************/
# Line 1104  Returns:         pointer to '}' on succe Line 1604  Returns:         pointer to '}' on succe
1604                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1605  */  */
1606    
1607  static const uschar *  static const pcre_uchar *
1608  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)
1609  {  {
1610  int min = 0;  int min = 0;
1611  int max = -1;  int max = -1;
1612    
1613  /* Read the minimum value and do a paranoid check: a negative value indicates  while (IS_DIGIT(*p))
 an integer overflow. */  
   
 while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;  
 if (min < 0 || min > 65535)  
1614    {    {
1615    *errorcodeptr = ERR5;    min = min * 10 + (int)(*p++ - CHAR_0);
1616    return p;    if (min > 65535)
1617        {
1618        *errorcodeptr = ERR5;
1619        return p;
1620        }
1621    }    }
1622    
 /* Read the maximum value if there is one, and again do a paranoid on its size.  
 Also, max must not be less than min. */  
   
1623  if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else  if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
1624    {    {
1625    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1626      {      {
1627      max = 0;      max = 0;
1628      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p))
     if (max < 0 || max > 65535)  
1629        {        {
1630        *errorcodeptr = ERR5;        max = max * 10 + (int)(*p++ - CHAR_0);
1631        return p;        if (max > 65535)
1632            {
1633            *errorcodeptr = ERR5;
1634            return p;
1635            }
1636        }        }
1637      if (max < min)      if (max < min)
1638        {        {
# Line 1142  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1642  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1642      }      }
1643    }    }
1644    
 /* Fill in the required variables, and pass back the pointer to the terminating  
 '}'. */  
   
1645  *minp = min;  *minp = min;
1646  *maxp = max;  *maxp = max;
1647  return p;  return p;
# Line 1153  return p; Line 1650  return p;
1650    
1651    
1652  /*************************************************  /*************************************************
1653  *  Subroutine for finding forward reference      *  *      Find first significant op code            *
1654  *************************************************/  *************************************************/
1655    
1656  /* This recursive function is called only from find_parens() below. The  /* This is called by several functions that scan a compiled expression looking
1657  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
1658  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
1659  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
1660  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. Recursion is used to keep  
 track of subpatterns that reset the capturing group numbers - the (?| feature.  
   
 This function was originally called only from the second pass, in which we know  
 that if (?< or (?' or (?P< is encountered, the name will be correctly  
 terminated because that is checked in the first pass. There is now one call to  
 this function in the first pass, to check for a recursive back reference by  
 name (so that we can make the whole group atomic). In this case, we need check  
 only up to the current position in the pattern, and that is still OK because  
 and previous occurrences will have been checked. To make this work, the test  
 for "end of pattern" is a check against cd->end_pattern in the main loop,  
 instead of looking for a binary zero. This means that the special first-pass  
 call can adjust cd->end_pattern temporarily. (Checks for binary zero while  
 processing items within the loop are OK, because afterwards the main loop will  
 terminate.)  
1661    
1662  Arguments:  Arguments:
1663    ptrptr       address of the current character pointer (updated)    code         pointer to the start of the group
1664    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  
   utf8         TRUE if we are in UTF-8 mode  
   count        pointer to the current capturing subpattern number (updated)  
1665    
1666  Returns:       the number of the named subpattern, or -1 if not found  Returns:       pointer to the first significant opcode
1667  */  */
1668    
1669  static int  static const pcre_uchar*
1670  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  first_significant_code(const pcre_uchar *code, BOOL skipassert)
   BOOL xmode, BOOL utf8, int *count)  
1671  {  {
1672  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)  
1673    {    {
1674    /* Handle specials such as (*SKIP) or (*UTF8) etc. */    switch ((int)*code)
   
   if (ptr[1] == CHAR_ASTERISK) ptr += 2;  
   
   /* Handle a normal, unnamed capturing parenthesis. */  
   
   else if (ptr[1] != CHAR_QUESTION_MARK)  
1675      {      {
1676      *count += 1;      case OP_ASSERT_NOT:
1677      if (name == NULL && *count == lorn) return *count;      case OP_ASSERTBACK:
1678      ptr++;      case OP_ASSERTBACK_NOT:
1679      }      if (!skipassert) return code;
1680        do code += GET(code, 1); while (*code == OP_ALT);
1681        code += PRIV(OP_lengths)[*code];
1682        break;
1683    
1684    /* All cases now have (? at the start. Remember when we are in a group      case OP_WORD_BOUNDARY:
1685    where the parenthesis numbers are duplicated. */      case OP_NOT_WORD_BOUNDARY:
1686        if (!skipassert) return code;
1687        /* Fall through */
1688    
1689    else if (ptr[2] == CHAR_VERTICAL_LINE)      case OP_CALLOUT:
1690      {      case OP_CREF:
1691      ptr += 3;      case OP_DNCREF:
1692      dup_parens = TRUE;      case OP_RREF:
1693        case OP_DNRREF:
1694        case OP_DEF:
1695        code += PRIV(OP_lengths)[*code];
1696        break;
1697    
1698        default:
1699        return code;
1700      }      }
1701      }
1702    /* Control never reaches here */
1703    }
1704    
   /* Handle comments; all characters are allowed until a ket is reached. */  
1705    
   else if (ptr[2] == CHAR_NUMBER_SIGN)  
     {  
     for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;  
     goto FAIL_EXIT;  
     }  
1706    
1707    /* Handle a condition. If it is an assertion, just carry on so that it  /*************************************************
1708    is processed as normal. If not, skip to the closing parenthesis of the  *        Find the fixed length of a branch       *
1709    condition (there can't be any nested parens). */  *************************************************/
1710    
1711    else if (ptr[2] == CHAR_LEFT_PARENTHESIS)  /* Scan a branch and compute the fixed length of subject that will match it,
1712      {  if the length is fixed. This is needed for dealing with backward assertions.
1713      ptr += 2;  In UTF8 mode, the result is in characters rather than bytes. The branch is
1714      if (ptr[1] != CHAR_QUESTION_MARK)  temporarily terminated with OP_END when this function is called.
       {  
       while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
       if (*ptr != 0) ptr++;  
       }  
     }  
1715    
1716    /* Start with (? but not a condition. */  This function is called when a backward assertion is encountered, so that if it
1717    fails, the error message can point to the correct place in the pattern.
1718    However, we cannot do this when the assertion contains subroutine calls,
1719    because they can be forward references. We solve this by remembering this case
1720    and doing the check at the end; a flag specifies which mode we are running in.
1721    
1722    else  Arguments:
1723      {    code     points to the start of the pattern (the bracket)
1724      ptr += 2;    utf      TRUE in UTF-8 / UTF-16 / UTF-32 mode
1725      if (*ptr == CHAR_P) ptr++;                      /* Allow optional P */    atend    TRUE if called when the pattern is complete
1726      cd       the "compile data" structure
1727      recurses    chain of recurse_check to catch mutual recursion
1728    
1729      /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */  Returns:   the fixed length,
1730                 or -1 if there is no fixed length,
1731                 or -2 if \C was encountered (in UTF-8 mode only)
1732                 or -3 if an OP_RECURSE item was encountered and atend is FALSE
1733                 or -4 if an unknown opcode was encountered (internal error)
1734    */
1735    
1736      if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&  static int
1737          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)  find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd,
1738        {    recurse_check *recurses)
1739        int term;  {
1740        const uschar *thisname;  int length = -1;
1741        *count += 1;  recurse_check this_recurse;
1742        if (name == NULL && *count == lorn) return *count;  register int branchlength = 0;
1743        term = *ptr++;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
       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++;  
       }  
     }  
   }  
1744    
1745  /* Past any initial parenthesis handling, scan for parentheses or vertical  /* Scan along the opcodes for this branch. If we get to the end of the
1746  bars. Stop if we get to cd->end_pattern. Note that this is important for the  branch, check the length against that of the other branches. */
 first-pass call when this value is temporarily adjusted to stop at the current  
 position. So DO NOT change this to a test for binary zero. */  
1747    
1748  for (; ptr < cd->end_pattern; ptr++)  for (;;)
1749    {    {
1750    /* Skip over backslashed characters and also entire \Q...\E */    int d;
1751      pcre_uchar *ce, *cs;
1752      register pcre_uchar op = *cc;
1753    
1754    if (*ptr == CHAR_BACKSLASH)    switch (op)
1755      {      {
1756      if (*(++ptr) == 0) goto FAIL_EXIT;      /* We only need to continue for OP_CBRA (normal capturing bracket) and
1757      if (*ptr == CHAR_Q) for (;;)      OP_BRA (normal non-capturing bracket) because the other variants of these
1758        {      opcodes are all concerned with unlimited repeated groups, which of course
1759        while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};      are not of fixed length. */
       if (*ptr == 0) goto FAIL_EXIT;  
       if (*(++ptr) == CHAR_E) break;  
       }  
     continue;  
     }  
1760    
1761    /* Skip over character classes; this logic must be similar to the way they      case OP_CBRA:
1762    are handled for real. If the first character is '^', skip it. Also, if the      case OP_BRA:
1763    first few characters (either before or after ^) are \Q\E or \E we skip them      case OP_ONCE:
1764    too. This makes for compatibility with Perl. Note the use of STR macros to      case OP_ONCE_NC:
1765    encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */      case OP_COND:
1766        d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd,
1767          recurses);
1768        if (d < 0) return d;
1769        branchlength += d;
1770        do cc += GET(cc, 1); while (*cc == OP_ALT);
1771        cc += 1 + LINK_SIZE;
1772        break;
1773    
1774    if (*ptr == CHAR_LEFT_SQUARE_BRACKET)      /* Reached end of a branch; if it's a ket it is the end of a nested call.
1775      {      If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1776      BOOL negate_class = FALSE;      an ALT. If it is END it's the end of the outer call. All can be handled by
1777      for (;;)      the same code. Note that we must not include the OP_KETRxxx opcodes here,
1778        {      because they all imply an unlimited repeat. */
       if (ptr[1] == CHAR_BACKSLASH)  
         {  
         if (ptr[2] == CHAR_E)  
           ptr+= 2;  
         else if (strncmp((const char *)ptr+2,  
                  STR_Q STR_BACKSLASH STR_E, 3) == 0)  
           ptr += 4;  
         else  
           break;  
         }  
       else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)  
         {  
         negate_class = TRUE;  
         ptr++;  
         }  
       else break;  
       }  
   
     /* If the next character is ']', it is a data character that must be  
     skipped, except in JavaScript compatibility mode. */  
   
     if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&  
         (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)  
       ptr++;  
   
     while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)  
       {  
       if (*ptr == 0) return -1;  
       if (*ptr == CHAR_BACKSLASH)  
         {  
         if (*(++ptr) == 0) goto FAIL_EXIT;  
         if (*ptr == CHAR_Q) for (;;)  
           {  
           while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};  
           if (*ptr == 0) goto FAIL_EXIT;  
           if (*(++ptr) == CHAR_E) break;  
           }  
         continue;  
         }  
       }  
     continue;  
     }  
   
   /* Skip comments in /x mode */  
   
   if (xmode && *ptr == CHAR_NUMBER_SIGN)  
     {  
     ptr++;  
     while (*ptr != 0)  
       {  
       if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }  
       ptr++;  
 #ifdef SUPPORT_UTF8  
       if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;  
 #endif  
       }  
     if (*ptr == 0) goto FAIL_EXIT;  
     continue;  
     }  
   
   /* Check for the special metacharacters */  
   
   if (*ptr == CHAR_LEFT_PARENTHESIS)  
     {  
     int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);  
     if (rc > 0) return rc;  
     if (*ptr == 0) goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_RIGHT_PARENTHESIS)  
     {  
     if (dup_parens && *count < hwm_count) *count = hwm_count;  
     goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)  
     {  
     if (*count > hwm_count) hwm_count = *count;  
     *count = start_count;  
     }  
   }  
   
 FAIL_EXIT:  
 *ptrptr = ptr;  
 return -1;  
 }  
   
   
   
   
 /*************************************************  
 *       Find forward referenced subpattern       *  
 *************************************************/  
   
 /* This function scans along a pattern's text looking for capturing  
 subpatterns, and counting them. If it finds a named pattern that matches the  
 name it is given, it returns its number. Alternatively, if the name is NULL, it  
 returns when it reaches a given numbered subpattern. This is used for forward  
 references to subpatterns. We used to be able to start this scan from the  
 current compiling point, using the current count value from cd->bracount, and  
 do it all in a single loop, but the addition of the possibility of duplicate  
 subpattern numbers means that we have to scan from the very start, in order to  
 take account of such duplicates, and to use a recursive function to keep track  
 of the different types of group.  
   
 Arguments:  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf8         TRUE if we are in UTF-8 mode  
   
 Returns:       the number of the found subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode,  
   BOOL utf8)  
 {  
 uschar *ptr = (uschar *)cd->start_pattern;  
 int count = 0;  
 int rc;  
   
 /* If the pattern does not start with an opening parenthesis, the first call  
 to find_parens_sub() will scan right to the end (if necessary). However, if it  
 does start with a parenthesis, find_parens_sub() will return when it hits the  
 matching closing parens. That is why we have to have a loop. */  
   
 for (;;)  
   {  
   rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);  
   if (rc > 0 || *ptr++ == 0) break;  
   }  
   
 return rc;  
 }  
   
   
   
   
 /*************************************************  
 *      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, 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  
   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, BOOL skipassert)  
 {  
 for (;;)  
   {  
   switch ((int)*code)  
     {  
     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)  
   utf8     TRUE in UTF-8 mode  
   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, BOOL utf8, BOOL atend, compile_data *cd)  
 {  
 int length = -1;  
   
 register int branchlength = 0;  
 register uschar *cc = code + 1 + LINK_SIZE;  
   
 /* Scan along the opcodes for this branch. If we get to the end of the  
 branch, check the length against that of the other branches. */  
   
 for (;;)  
   {  
   int d;  
   uschar *ce, *cs;  
   register int op = *cc;  
   switch (op)  
     {  
     /* We only need to continue for OP_CBRA (normal capturing bracket) and  
     OP_BRA (normal non-capturing bracket) because the other variants of these  
     opcodes are all concerned with unlimited repeated groups, which of course  
     are not of fixed length. They will cause a -1 response from the default  
     case of this switch. */  
   
     case OP_CBRA:  
     case OP_BRA:  
     case OP_ONCE:  
     case OP_ONCE_NC:  
     case OP_COND:  
     d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), utf8, atend, cd);  
     if (d < 0) return d;  
     branchlength += d;  
     do cc += GET(cc, 1); while (*cc == OP_ALT);  
     cc += 1 + LINK_SIZE;  
     break;  
   
     /* Reached end of a branch; if it's a ket it is the end of a nested  
     call. If it's ALT it is an alternation in a nested call. If it is  
     END it's the end of the outer call. All can be handled by the same code.  
     Note that we must not include the OP_KETRxxx opcodes here, because they  
     all imply an unlimited repeat. */  
1779    
1780      case OP_ALT:      case OP_ALT:
1781      case OP_KET:      case OP_KET:
1782      case OP_END:      case OP_END:
1783        case OP_ACCEPT:
1784        case OP_ASSERT_ACCEPT:
1785      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1786        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
1787      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
# Line 1588  for (;;) Line 1795  for (;;)
1795    
1796      case OP_RECURSE:      case OP_RECURSE:
1797      if (!atend) return -3;      if (!atend) return -3;
1798      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1799      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1800      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1801      d = find_fixedlength(cs + 2, utf8, atend, cd);      else   /* Check for mutual recursion */
1802          {
1803          recurse_check *r = recurses;
1804          for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
1805          if (r != NULL) return -1;   /* Mutual recursion */
1806          }
1807        this_recurse.prev = recurses;
1808        this_recurse.group = cs;
1809        d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd, &this_recurse);
1810      if (d < 0) return d;      if (d < 0) return d;
1811      branchlength += d;      branchlength += d;
1812      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1604  for (;;) Line 1819  for (;;)
1819      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1820      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1821      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1822      /* Fall through */      cc += 1 + LINK_SIZE;
1823        break;
1824    
1825      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1826    
1827      case OP_REVERSE:      case OP_MARK:
1828      case OP_CREF:      case OP_PRUNE_ARG:
1829      case OP_NCREF:      case OP_SKIP_ARG:
1830      case OP_RREF:      case OP_THEN_ARG:
1831      case OP_NRREF:      cc += cc[1] + PRIV(OP_lengths)[*cc];
1832      case OP_DEF:      break;
1833    
1834      case OP_CALLOUT:      case OP_CALLOUT:
     case OP_SOD:  
     case OP_SOM:  
     case OP_SET_SOM:  
     case OP_EOD:  
     case OP_EODN:  
1835      case OP_CIRC:      case OP_CIRC:
1836      case OP_CIRCM:      case OP_CIRCM:
1837        case OP_CLOSE:
1838        case OP_COMMIT:
1839        case OP_CREF:
1840        case OP_DEF:
1841        case OP_DNCREF:
1842        case OP_DNRREF:
1843      case OP_DOLL:      case OP_DOLL:
1844      case OP_DOLLM:      case OP_DOLLM:
1845        case OP_EOD:
1846        case OP_EODN:
1847        case OP_FAIL:
1848      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1849        case OP_PRUNE:
1850        case OP_REVERSE:
1851        case OP_RREF:
1852        case OP_SET_SOM:
1853        case OP_SKIP:
1854        case OP_SOD:
1855        case OP_SOM:
1856        case OP_THEN:
1857      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1858      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1859      break;      break;
1860    
1861      /* Handle literal characters */      /* Handle literal characters */
# Line 1637  for (;;) Line 1866  for (;;)
1866      case OP_NOTI:      case OP_NOTI:
1867      branchlength++;      branchlength++;
1868      cc += 2;      cc += 2;
1869  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1870      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1871  #endif  #endif
1872      break;      break;
1873    
# Line 1646  for (;;) Line 1875  for (;;)
1875      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
1876    
1877      case OP_EXACT:      case OP_EXACT:
1878      branchlength += GET2(cc,1);      case OP_EXACTI:
1879      cc += 4;      case OP_NOTEXACT:
1880  #ifdef SUPPORT_UTF8      case OP_NOTEXACTI:
1881      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      branchlength += (int)GET2(cc,1);
1882        cc += 2 + IMM2_SIZE;
1883    #ifdef SUPPORT_UTF
1884        if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1885  #endif  #endif
1886      break;      break;
1887    
1888      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1889      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1890      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1891      cc += 4;        cc += 2;
1892        cc += 1 + IMM2_SIZE + 1;
1893      break;      break;
1894    
1895      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1666  for (;;) Line 1899  for (;;)
1899      cc += 2;      cc += 2;
1900      /* Fall through */      /* Fall through */
1901    
1902        case OP_HSPACE:
1903        case OP_VSPACE:
1904        case OP_NOT_HSPACE:
1905        case OP_NOT_VSPACE:
1906      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1907      case OP_DIGIT:      case OP_DIGIT:
1908      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
# Line 1678  for (;;) Line 1915  for (;;)
1915      cc++;      cc++;
1916      break;      break;
1917    
1918      /* The single-byte matcher isn't allowed */      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1919        otherwise \C is coded as OP_ALLANY. */
1920    
1921      case OP_ANYBYTE:      case OP_ANYBYTE:
1922      return -2;      return -2;
1923    
1924      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1925    
 #ifdef SUPPORT_UTF8  
     case OP_XCLASS:  
     cc += GET(cc, 1) - 33;  
     /* Fall through */  
 #endif  
   
1926      case OP_CLASS:      case OP_CLASS:
1927      case OP_NCLASS:      case OP_NCLASS:
1928      cc += 33;  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1929        case OP_XCLASS:
1930        /* The original code caused an unsigned overflow in 64 bit systems,
1931        so now we use a conditional statement. */
1932        if (op == OP_XCLASS)
1933          cc += GET(cc, 1);
1934        else
1935          cc += PRIV(OP_lengths)[OP_CLASS];
1936    #else
1937        cc += PRIV(OP_lengths)[OP_CLASS];
1938    #endif
1939    
1940      switch (*cc)      switch (*cc)
1941        {        {
1942        case OP_CRSTAR:        case OP_CRSTAR:
1943        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1944          case OP_CRPLUS:
1945          case OP_CRMINPLUS:
1946        case OP_CRQUERY:        case OP_CRQUERY:
1947        case OP_CRMINQUERY:        case OP_CRMINQUERY:
1948          case OP_CRPOSSTAR:
1949          case OP_CRPOSPLUS:
1950          case OP_CRPOSQUERY:
1951        return -1;        return -1;
1952    
1953        case OP_CRRANGE:        case OP_CRRANGE:
1954        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1955        if (GET2(cc,1) != GET2(cc,3)) return -1;        case OP_CRPOSRANGE:
1956        branchlength += GET2(cc,1);        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1957        cc += 5;        branchlength += (int)GET2(cc,1);
1958          cc += 1 + 2 * IMM2_SIZE;
1959        break;        break;
1960    
1961        default:        default:
# Line 1717  for (;;) Line 1965  for (;;)
1965    
1966      /* Anything else is variable length */      /* Anything else is variable length */
1967    
1968      default:      case OP_ANYNL:
1969        case OP_BRAMINZERO:
1970        case OP_BRAPOS:
1971        case OP_BRAPOSZERO:
1972        case OP_BRAZERO:
1973        case OP_CBRAPOS:
1974        case OP_EXTUNI:
1975        case OP_KETRMAX:
1976        case OP_KETRMIN:
1977        case OP_KETRPOS:
1978        case OP_MINPLUS:
1979        case OP_MINPLUSI:
1980        case OP_MINQUERY:
1981        case OP_MINQUERYI:
1982        case OP_MINSTAR:
1983        case OP_MINSTARI:
1984        case OP_MINUPTO:
1985        case OP_MINUPTOI:
1986        case OP_NOTMINPLUS:
1987        case OP_NOTMINPLUSI:
1988        case OP_NOTMINQUERY:
1989        case OP_NOTMINQUERYI:
1990        case OP_NOTMINSTAR:
1991        case OP_NOTMINSTARI:
1992        case OP_NOTMINUPTO:
1993        case OP_NOTMINUPTOI:
1994        case OP_NOTPLUS:
1995        case OP_NOTPLUSI:
1996        case OP_NOTPOSPLUS:
1997        case OP_NOTPOSPLUSI:
1998        case OP_NOTPOSQUERY:
1999        case OP_NOTPOSQUERYI:
2000        case OP_NOTPOSSTAR:
2001        case OP_NOTPOSSTARI:
2002        case OP_NOTPOSUPTO:
2003        case OP_NOTPOSUPTOI:
2004        case OP_NOTQUERY:
2005        case OP_NOTQUERYI:
2006        case OP_NOTSTAR:
2007        case OP_NOTSTARI:
2008        case OP_NOTUPTO:
2009        case OP_NOTUPTOI:
2010        case OP_PLUS:
2011        case OP_PLUSI:
2012        case OP_POSPLUS:
2013        case OP_POSPLUSI:
2014        case OP_POSQUERY:
2015        case OP_POSQUERYI:
2016        case OP_POSSTAR:
2017        case OP_POSSTARI:
2018        case OP_POSUPTO:
2019        case OP_POSUPTOI:
2020        case OP_QUERY:
2021        case OP_QUERYI:
2022        case OP_REF:
2023        case OP_REFI:
2024        case OP_DNREF:
2025        case OP_DNREFI:
2026        case OP_SBRA:
2027        case OP_SBRAPOS:
2028        case OP_SCBRA:
2029        case OP_SCBRAPOS:
2030        case OP_SCOND:
2031        case OP_SKIPZERO:
2032        case OP_STAR:
2033        case OP_STARI:
2034        case OP_TYPEMINPLUS:
2035        case OP_TYPEMINQUERY:
2036        case OP_TYPEMINSTAR:
2037        case OP_TYPEMINUPTO:
2038        case OP_TYPEPLUS:
2039        case OP_TYPEPOSPLUS:
2040        case OP_TYPEPOSQUERY:
2041        case OP_TYPEPOSSTAR:
2042        case OP_TYPEPOSUPTO:
2043        case OP_TYPEQUERY:
2044        case OP_TYPESTAR:
2045        case OP_TYPEUPTO:
2046        case OP_UPTO:
2047        case OP_UPTOI:
2048      return -1;      return -1;
2049    
2050        /* Catch unrecognized opcodes so that when new ones are added they
2051        are not forgotten, as has happened in the past. */
2052    
2053        default:
2054        return -4;
2055      }      }
2056    }    }
2057  /* Control never gets here */  /* Control never gets here */
# Line 1726  for (;;) Line 2059  for (;;)
2059    
2060    
2061    
   
2062  /*************************************************  /*************************************************
2063  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
2064  *************************************************/  *************************************************/
# Line 1739  length. Line 2071  length.
2071    
2072  Arguments:  Arguments:
2073    code        points to start of expression    code        points to start of expression
2074    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2075    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
2076    
2077  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
2078  */  */
2079    
2080  const uschar *  const pcre_uchar *
2081  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
2082  {  {
2083  for (;;)  for (;;)
2084    {    {
2085    register int c = *code;    register pcre_uchar c = *code;
2086    
2087    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2088    
# Line 1764  for (;;) Line 2096  for (;;)
2096    
2097    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
2098      {      {
2099      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
2100      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2101      }      }
2102    
2103    /* Handle capturing bracket */    /* Handle capturing bracket */
# Line 1773  for (;;) Line 2105  for (;;)
2105    else if (c == OP_CBRA || c == OP_SCBRA ||    else if (c == OP_CBRA || c == OP_SCBRA ||
2106             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
2107      {      {
2108      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
2109      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
2110      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2111      }      }
2112    
2113    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
# Line 1803  for (;;) Line 2135  for (;;)
2135        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2136        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2137        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
2138        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2139            code += 2;
2140        break;        break;
2141    
2142        case OP_MARK:        case OP_MARK:
2143        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2144        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
2145        case OP_THEN_ARG:        case OP_THEN_ARG:
2146        code += code[1];        code += code[1];
2147        break;        break;
# Line 1819  for (;;) Line 2149  for (;;)
2149    
2150      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2151    
2152      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2153    
2154    /* 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
2155    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
2156    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
2157    
2158  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2159      if (utf8) switch(c)      if (utf) switch(c)
2160        {        {
2161        case OP_CHAR:        case OP_CHAR:
2162        case OP_CHARI:        case OP_CHARI:
2163          case OP_NOT:
2164          case OP_NOTI:
2165        case OP_EXACT:        case OP_EXACT:
2166        case OP_EXACTI:        case OP_EXACTI:
2167          case OP_NOTEXACT:
2168          case OP_NOTEXACTI:
2169        case OP_UPTO:        case OP_UPTO:
2170        case OP_UPTOI:        case OP_UPTOI:
2171          case OP_NOTUPTO:
2172          case OP_NOTUPTOI:
2173        case OP_MINUPTO:        case OP_MINUPTO:
2174        case OP_MINUPTOI:        case OP_MINUPTOI:
2175          case OP_NOTMINUPTO:
2176          case OP_NOTMINUPTOI:
2177        case OP_POSUPTO:        case OP_POSUPTO:
2178        case OP_POSUPTOI:        case OP_POSUPTOI:
2179          case OP_NOTPOSUPTO:
2180          case OP_NOTPOSUPTOI:
2181        case OP_STAR:        case OP_STAR:
2182        case OP_STARI:        case OP_STARI:
2183          case OP_NOTSTAR:
2184          case OP_NOTSTARI:
2185        case OP_MINSTAR:        case OP_MINSTAR:
2186        case OP_MINSTARI:        case OP_MINSTARI:
2187          case OP_NOTMINSTAR:
2188          case OP_NOTMINSTARI:
2189        case OP_POSSTAR:        case OP_POSSTAR:
2190        case OP_POSSTARI:        case OP_POSSTARI:
2191          case OP_NOTPOSSTAR:
2192          case OP_NOTPOSSTARI:
2193        case OP_PLUS:        case OP_PLUS:
2194        case OP_PLUSI:        case OP_PLUSI:
2195          case OP_NOTPLUS:
2196          case OP_NOTPLUSI:
2197        case OP_MINPLUS:        case OP_MINPLUS:
2198        case OP_MINPLUSI:        case OP_MINPLUSI:
2199          case OP_NOTMINPLUS:
2200          case OP_NOTMINPLUSI:
2201        case OP_POSPLUS:        case OP_POSPLUS:
2202        case OP_POSPLUSI:        case OP_POSPLUSI:
2203          case OP_NOTPOSPLUS:
2204          case OP_NOTPOSPLUSI:
2205        case OP_QUERY:        case OP_QUERY:
2206        case OP_QUERYI:        case OP_QUERYI:
2207          case OP_NOTQUERY:
2208          case OP_NOTQUERYI:
2209        case OP_MINQUERY:        case OP_MINQUERY:
2210        case OP_MINQUERYI:        case OP_MINQUERYI:
2211          case OP_NOTMINQUERY:
2212          case OP_NOTMINQUERYI:
2213        case OP_POSQUERY:        case OP_POSQUERY:
2214        case OP_POSQUERYI:        case OP_POSQUERYI:
2215        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_NOTPOSQUERY:
2216          case OP_NOTPOSQUERYI:
2217          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2218        break;        break;
2219        }        }
2220  #else  #else
2221      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2222  #endif  #endif
2223      }      }
2224    }    }
# Line 1877  instance of OP_RECURSE. Line 2235  instance of OP_RECURSE.
2235    
2236  Arguments:  Arguments:
2237    code        points to start of expression    code        points to start of expression
2238    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2239    
2240  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
2241  */  */
2242    
2243  static const uschar *  static const pcre_uchar *
2244  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
2245  {  {
2246  for (;;)  for (;;)
2247    {    {
2248    register int c = *code;    register pcre_uchar c = *code;
2249    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2250    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
2251    
# Line 1922  for (;;) Line 2280  for (;;)
2280        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2281        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2282        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2283        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2284            code += 2;
2285        break;        break;
2286    
2287        case OP_MARK:        case OP_MARK:
2288        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2289        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
2290        case OP_THEN_ARG:        case OP_THEN_ARG:
2291        code += code[1];        code += code[1];
2292        break;        break;
# Line 1938  for (;;) Line 2294  for (;;)
2294    
2295      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2296    
2297      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2298    
2299      /* 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
2300      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
2301      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
2302    
2303  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2304      if (utf8) switch(c)      if (utf) switch(c)
2305        {        {
2306        case OP_CHAR:        case OP_CHAR:
2307        case OP_CHARI:        case OP_CHARI:
2308          case OP_NOT:
2309          case OP_NOTI:
2310        case OP_EXACT:        case OP_EXACT:
2311        case OP_EXACTI:        case OP_EXACTI:
2312          case OP_NOTEXACT:
2313          case OP_NOTEXACTI:
2314        case OP_UPTO:        case OP_UPTO:
2315        case OP_UPTOI:        case OP_UPTOI:
2316          case OP_NOTUPTO:
2317          case OP_NOTUPTOI:
2318        case OP_MINUPTO:        case OP_MINUPTO:
2319        case OP_MINUPTOI:        case OP_MINUPTOI:
2320          case OP_NOTMINUPTO:
2321          case OP_NOTMINUPTOI:
2322        case OP_POSUPTO:        case OP_POSUPTO:
2323        case OP_POSUPTOI:        case OP_POSUPTOI:
2324          case OP_NOTPOSUPTO:
2325          case OP_NOTPOSUPTOI:
2326        case OP_STAR:        case OP_STAR:
2327        case OP_STARI:        case OP_STARI:
2328          case OP_NOTSTAR:
2329          case OP_NOTSTARI:
2330        case OP_MINSTAR:        case OP_MINSTAR:
2331        case OP_MINSTARI:        case OP_MINSTARI:
2332          case OP_NOTMINSTAR:
2333          case OP_NOTMINSTARI:
2334        case OP_POSSTAR:        case OP_POSSTAR:
2335        case OP_POSSTARI:        case OP_POSSTARI:
2336          case OP_NOTPOSSTAR:
2337          case OP_NOTPOSSTARI:
2338        case OP_PLUS:        case OP_PLUS:
2339        case OP_PLUSI:        case OP_PLUSI:
2340          case OP_NOTPLUS:
2341          case OP_NOTPLUSI:
2342        case OP_MINPLUS:        case OP_MINPLUS:
2343        case OP_MINPLUSI:        case OP_MINPLUSI:
2344          case OP_NOTMINPLUS:
2345          case OP_NOTMINPLUSI:
2346        case OP_POSPLUS:        case OP_POSPLUS:
2347        case OP_POSPLUSI:        case OP_POSPLUSI:
2348          case OP_NOTPOSPLUS:
2349          case OP_NOTPOSPLUSI:
2350        case OP_QUERY:        case OP_QUERY:
2351        case OP_QUERYI:        case OP_QUERYI:
2352          case OP_NOTQUERY:
2353          case OP_NOTQUERYI:
2354        case OP_MINQUERY:        case OP_MINQUERY:
2355        case OP_MINQUERYI:        case OP_MINQUERYI:
2356          case OP_NOTMINQUERY:
2357          case OP_NOTMINQUERYI:
2358        case OP_POSQUERY:        case OP_POSQUERY:
2359        case OP_POSQUERYI:        case OP_POSQUERYI:
2360        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_NOTPOSQUERY:
2361          case OP_NOTPOSQUERYI:
2362          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2363        break;        break;
2364        }        }
2365  #else  #else
2366      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2367  #endif  #endif
2368      }      }
2369    }    }
# Line 2002  bracket whose current branch will alread Line 2386  bracket whose current branch will alread
2386  Arguments:  Arguments:
2387    code        points to start of search    code        points to start of search
2388    endcode     points to where to stop    endcode     points to where to stop
2389    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2390    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2391      recurses    chain of recurse_check to catch mutual recursion
2392    
2393  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2394  */  */
2395    
2396  static BOOL  static BOOL
2397  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2398    compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2399  {  {
2400  register int c;  register pcre_uchar c;
2401  for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE);  recurse_check this_recurse;
2402    
2403    for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2404       code < endcode;       code < endcode;
2405       code = first_significant_code(code + _pcre_OP_lengths[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2406    {    {
2407    const uschar *ccode;    const pcre_uchar *ccode;
2408    
2409    c = *code;    c = *code;
2410    
# Line 2040  for (code = first_significant_code(code Line 2427  for (code = first_significant_code(code
2427    
2428    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2429      {      {
2430      const uschar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2431        const pcre_uchar *endgroup = scode;
2432      BOOL empty_branch;      BOOL empty_branch;
2433    
2434      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2435        when called to scan a completed pattern by setting cd->start_workspace to
2436        NULL. */
2437    
2438      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)      if (cd->start_workspace != NULL)
2439        if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;        {
2440          const pcre_uchar *tcode;
2441          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2442            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2443          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2444          }
2445    
2446      /* Not a forward reference, test for completed backward reference */      /* If the reference is to a completed group, we need to detect whether this
2447        is a recursive call, as otherwise there will be an infinite loop. If it is
2448        a recursion, just skip over it. Simple recursions are easily detected. For
2449        mutual recursions we keep a chain on the stack. */
2450    
2451      empty_branch = FALSE;      do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2452      scode = cd->start_code + GET(code, 1);      if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2453      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      else
2454          {
2455          recurse_check *r = recurses;
2456          for (r = recurses; r != NULL; r = r->prev)
2457            if (r->group == scode) break;
2458          if (r != NULL) continue;   /* Mutual recursion */
2459          }
2460    
2461      /* Completed backwards reference */      /* Completed reference; scan the referenced group, remembering it on the
2462        stack chain to detect mutual recursions. */
2463    
2464        empty_branch = FALSE;
2465        this_recurse.prev = recurses;
2466        this_recurse.group = scode;
2467    
2468      do      do
2469        {        {
2470        if (could_be_empty_branch(scode, endcode, utf8, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2471          {          {
2472          empty_branch = TRUE;          empty_branch = TRUE;
2473          break;          break;
# Line 2076  for (code = first_significant_code(code Line 2485  for (code = first_significant_code(code
2485    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2486        c == OP_BRAPOSZERO)        c == OP_BRAPOSZERO)
2487      {      {
2488      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2489      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
2490      c = *code;      c = *code;
2491      continue;      continue;
# Line 2098  for (code = first_significant_code(code Line 2507  for (code = first_significant_code(code
2507    if (c == OP_BRA  || c == OP_BRAPOS ||    if (c == OP_BRA  || c == OP_BRAPOS ||
2508        c == OP_CBRA || c == OP_CBRAPOS ||        c == OP_CBRA || c == OP_CBRAPOS ||
2509        c == OP_ONCE || c == OP_ONCE_NC ||        c == OP_ONCE || c == OP_ONCE_NC ||
2510        c == OP_COND)        c == OP_COND || c == OP_SCOND)
2511      {      {
2512      BOOL empty_branch;      BOOL empty_branch;
2513      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 2114  for (code = first_significant_code(code Line 2523  for (code = first_significant_code(code
2523        empty_branch = FALSE;        empty_branch = FALSE;
2524        do        do
2525          {          {
2526          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd,
2527            empty_branch = TRUE;            recurses)) empty_branch = TRUE;
2528          code += GET(code, 1);          code += GET(code, 1);
2529          }          }
2530        while (*code == OP_ALT);        while (*code == OP_ALT);
# Line 2132  for (code = first_significant_code(code Line 2541  for (code = first_significant_code(code
2541      {      {
2542      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2543      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2544      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2545      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"
2546      here. */      here. */
2547    
2548  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2549      case OP_XCLASS:      case OP_XCLASS:
2550      ccode = code += GET(code, 1);      ccode = code += GET(code, 1);
2551      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
# Line 2144  for (code = first_significant_code(code Line 2553  for (code = first_significant_code(code
2553    
2554      case OP_CLASS:      case OP_CLASS:
2555      case OP_NCLASS:      case OP_NCLASS:
2556      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2557    
2558  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2559      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
2560  #endif  #endif
2561    
# Line 2156  for (code = first_significant_code(code Line 2565  for (code = first_significant_code(code
2565        case OP_CRMINSTAR:        case OP_CRMINSTAR:
2566        case OP_CRQUERY:        case OP_CRQUERY:
2567        case OP_CRMINQUERY:        case OP_CRMINQUERY:
2568          case OP_CRPOSSTAR:
2569          case OP_CRPOSQUERY:
2570        break;        break;
2571    
2572        default:                   /* Non-repeat => class must match */        default:                   /* Non-repeat => class must match */
2573        case OP_CRPLUS:            /* These repeats aren't empty */        case OP_CRPLUS:            /* These repeats aren't empty */
2574        case OP_CRMINPLUS:        case OP_CRMINPLUS:
2575          case OP_CRPOSPLUS:
2576        return FALSE;        return FALSE;
2577    
2578        case OP_CRRANGE:        case OP_CRRANGE:
2579        case OP_CRMINRANGE:        case OP_CRMINRANGE:
2580          case OP_CRPOSRANGE:
2581        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */
2582        break;        break;
2583        }        }
# Line 2172  for (code = first_significant_code(code Line 2585  for (code = first_significant_code(code
2585    
2586      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2587    
2588        case OP_ANY:
2589        case OP_ALLANY:
2590        case OP_ANYBYTE:
2591    
2592      case OP_PROP:      case OP_PROP:
2593      case OP_NOTPROP:      case OP_NOTPROP:
2594        case OP_ANYNL:
2595    
2596        case OP_NOT_HSPACE:
2597        case OP_HSPACE:
2598        case OP_NOT_VSPACE:
2599        case OP_VSPACE:
2600      case OP_EXTUNI:      case OP_EXTUNI:
2601    
2602      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2603      case OP_DIGIT:      case OP_DIGIT:
2604      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2605      case OP_WHITESPACE:      case OP_WHITESPACE:
2606      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2607      case OP_WORDCHAR:      case OP_WORDCHAR:
2608      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2609      case OP_CHAR:      case OP_CHAR:
2610      case OP_CHARI:      case OP_CHARI:
2611      case OP_NOT:      case OP_NOT:
2612      case OP_NOTI:      case OP_NOTI:
2613    
2614      case OP_PLUS:      case OP_PLUS:
2615        case OP_PLUSI:
2616      case OP_MINPLUS:      case OP_MINPLUS:
2617      case OP_POSPLUS:      case OP_MINPLUSI:
2618      case OP_EXACT:  
2619      case OP_NOTPLUS:      case OP_NOTPLUS:
2620        case OP_NOTPLUSI:
2621      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2622        case OP_NOTMINPLUSI:
2623    
2624        case OP_POSPLUS:
2625        case OP_POSPLUSI:
2626      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2627        case OP_NOTPOSPLUSI:
2628    
2629        case OP_EXACT:
2630        case OP_EXACTI:
2631      case OP_NOTEXACT:      case OP_NOTEXACT:
2632        case OP_NOTEXACTI:
2633    
2634      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2635      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2636      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2637      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2638    
2639      return FALSE;      return FALSE;
2640    
2641      /* 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 2219  for (code = first_significant_code(code Line 2655  for (code = first_significant_code(code
2655      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2656      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2657      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2658      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2659          code += 2;
2660      break;      break;
2661    
2662      /* End of branch */      /* End of branch */
# Line 2232  for (code = first_significant_code(code Line 2669  for (code = first_significant_code(code
2669      return TRUE;      return TRUE;
2670    
2671      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2672      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2673        followed by a multibyte character. */
2674    
2675  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2676      case OP_STAR:      case OP_STAR:
2677      case OP_STARI:      case OP_STARI:
2678        case OP_NOTSTAR:
2679        case OP_NOTSTARI:
2680    
2681      case OP_MINSTAR:      case OP_MINSTAR:
2682      case OP_MINSTARI:      case OP_MINSTARI:
2683        case OP_NOTMINSTAR:
2684        case OP_NOTMINSTARI:
2685    
2686      case OP_POSSTAR:      case OP_POSSTAR:
2687      case OP_POSSTARI:      case OP_POSSTARI:
2688        case OP_NOTPOSSTAR:
2689        case OP_NOTPOSSTARI:
2690    
2691      case OP_QUERY:      case OP_QUERY:
2692      case OP_QUERYI:      case OP_QUERYI:
2693        case OP_NOTQUERY:
2694        case OP_NOTQUERYI:
2695    
2696      case OP_MINQUERY:      case OP_MINQUERY:
2697      case OP_MINQUERYI:      case OP_MINQUERYI:
2698        case OP_NOTMINQUERY:
2699        case OP_NOTMINQUERYI:
2700    
2701      case OP_POSQUERY:      case OP_POSQUERY:
2702      case OP_POSQUERYI:      case OP_POSQUERYI:
2703      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      case OP_NOTPOSQUERY:
2704        case OP_NOTPOSQUERYI:
2705    
2706        if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2707      break;      break;
2708    
2709      case OP_UPTO:      case OP_UPTO:
2710      case OP_UPTOI:      case OP_UPTOI:
2711        case OP_NOTUPTO:
2712        case OP_NOTUPTOI:
2713    
2714      case OP_MINUPTO:      case OP_MINUPTO:
2715      case OP_MINUPTOI:      case OP_MINUPTOI:
2716        case OP_NOTMINUPTO:
2717        case OP_NOTMINUPTOI:
2718    
2719      case OP_POSUPTO:      case OP_POSUPTO:
2720      case OP_POSUPTOI:      case OP_POSUPTOI:
2721      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      case OP_NOTPOSUPTO:
2722        case OP_NOTPOSUPTOI:
2723    
2724        if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2725      break;      break;
2726  #endif  #endif
2727    
# Line 2266  for (code = first_significant_code(code Line 2731  for (code = first_significant_code(code
2731      case OP_MARK:      case OP_MARK:
2732      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2733      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     code += code[1];  
     break;  
   
2734      case OP_THEN_ARG:      case OP_THEN_ARG:
2735      code += code[1];      code += code[1];
2736      break;      break;
# Line 2296  stopping when we pass beyond the bracket Line 2758  stopping when we pass beyond the bracket
2758  This function is called only during the real compile, not during the  This function is called only during the real compile, not during the
2759  pre-compile.  pre-compile.
2760    
2761  Arguments:  Arguments:
2762    code        points to start of the recursion    code        points to start of the recursion
2763    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2764    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2765    utf8        TRUE if in UTF-8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2766    cd          pointers to tables etc    cd          pointers to tables etc
2767    
2768    Returns:      TRUE if what is matched could be empty
2769    */
2770    
2771    static BOOL
2772    could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2773      branch_chain *bcptr, BOOL utf, compile_data *cd)
2774    {
2775    while (bcptr != NULL && bcptr->current_branch >= code)
2776      {
2777      if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2778        return FALSE;
2779      bcptr = bcptr->outer;
2780      }
2781    return TRUE;
2782    }
2783    
2784    
2785    
2786    /*************************************************
2787    *        Base opcode of repeated opcodes         *
2788    *************************************************/
2789    
2790    /* Returns the base opcode for repeated single character type opcodes. If the
2791    opcode is not a repeated character type, it returns with the original value.
2792    
2793    Arguments:  c opcode
2794    Returns:    base opcode for the type
2795    */
2796    
2797    static pcre_uchar
2798    get_repeat_base(pcre_uchar c)
2799    {
2800    return (c > OP_TYPEPOSUPTO)? c :
2801           (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2802           (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2803           (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2804           (c >= OP_STARI)?      OP_STARI :
2805                                 OP_STAR;
2806    }
2807    
2808    
2809    
2810    #ifdef SUPPORT_UCP
2811    /*************************************************
2812    *        Check a character and a property        *
2813    *************************************************/
2814    
2815    /* This function is called by check_auto_possessive() when a property item
2816    is adjacent to a fixed character.
2817    
2818    Arguments:
2819      c            the character
2820      ptype        the property type
2821      pdata        the data for the type
2822      negated      TRUE if it's a negated property (\P or \p{^)
2823    
2824    Returns:       TRUE if auto-possessifying is OK
2825    */
2826    
2827    static BOOL
2828    check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2829      BOOL negated)
2830    {
2831    const pcre_uint32 *p;
2832    const ucd_record *prop = GET_UCD(c);
2833    
2834    switch(ptype)
2835      {
2836      case PT_LAMP:
2837      return (prop->chartype == ucp_Lu ||
2838              prop->chartype == ucp_Ll ||
2839              prop->chartype == ucp_Lt) == negated;
2840    
2841      case PT_GC:
2842      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2843    
2844      case PT_PC:
2845      return (pdata == prop->chartype) == negated;
2846    
2847      case PT_SC:
2848      return (pdata == prop->script) == negated;
2849    
2850      /* These are specials */
2851    
2852      case PT_ALNUM:
2853      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2854              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2855    
2856      /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2857      means that Perl space and POSIX space are now identical. PCRE was changed
2858      at release 8.34. */
2859    
2860      case PT_SPACE:    /* Perl space */
2861      case PT_PXSPACE:  /* POSIX space */
2862      switch(c)
2863        {
2864        HSPACE_CASES:
2865        VSPACE_CASES:
2866        return negated;
2867    
2868        default:
2869        return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
2870        }
2871      break;  /* Control never reaches here */
2872    
2873      case PT_WORD:
2874      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2875              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2876              c == CHAR_UNDERSCORE) == negated;
2877    
2878      case PT_CLIST:
2879      p = PRIV(ucd_caseless_sets) + prop->caseset;
2880      for (;;)
2881        {
2882        if (c < *p) return !negated;
2883        if (c == *p++) return negated;
2884        }
2885      break;  /* Control never reaches here */
2886      }
2887    
2888    return FALSE;
2889    }
2890    #endif  /* SUPPORT_UCP */
2891    
2892    
2893    
2894    /*************************************************
2895    *        Fill the character property list        *
2896    *************************************************/
2897    
2898    /* Checks whether the code points to an opcode that can take part in auto-
2899    possessification, and if so, fills a list with its properties.
2900    
2901    Arguments:
2902      code        points to start of expression
2903      utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2904      fcc         points to case-flipping table
2905      list        points to output list
2906                  list[0] will be filled with the opcode
2907                  list[1] will be non-zero if this opcode
2908                    can match an empty character string
2909                  list[2..7] depends on the opcode
2910    
2911    Returns:      points to the start of the next opcode if *code is accepted
2912                  NULL if *code is not accepted
2913    */
2914    
2915    static const pcre_uchar *
2916    get_chr_property_list(const pcre_uchar *code, BOOL utf,
2917      const pcre_uint8 *fcc, pcre_uint32 *list)
2918    {
2919    pcre_uchar c = *code;
2920    pcre_uchar base;
2921    const pcre_uchar *end;
2922    pcre_uint32 chr;
2923    
2924    #ifdef SUPPORT_UCP
2925    pcre_uint32 *clist_dest;
2926    const pcre_uint32 *clist_src;
2927    #else
2928    utf = utf;  /* Suppress "unused parameter" compiler warning */
2929    #endif
2930    
2931    list[0] = c;
2932    list[1] = FALSE;
2933    code++;
2934    
2935    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2936      {
2937      base = get_repeat_base(c);
2938      c -= (base - OP_STAR);
2939    
2940      if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2941        code += IMM2_SIZE;
2942    
2943      list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
2944    
2945      switch(base)
2946        {
2947        case OP_STAR:
2948        list[0] = OP_CHAR;
2949        break;
2950    
2951        case OP_STARI:
2952        list[0] = OP_CHARI;
2953        break;
2954    
2955        case OP_NOTSTAR:
2956        list[0] = OP_NOT;
2957        break;
2958    
2959        case OP_NOTSTARI:
2960        list[0] = OP_NOTI;
2961        break;
2962    
2963        case OP_TYPESTAR:
2964        list[0] = *code;
2965        code++;
2966        break;
2967        }
2968      c = list[0];
2969      }
2970    
2971    switch(c)
2972      {
2973      case OP_NOT_DIGIT:
2974      case OP_DIGIT:
2975      case OP_NOT_WHITESPACE:
2976      case OP_WHITESPACE:
2977      case OP_NOT_WORDCHAR:
2978      case OP_WORDCHAR:
2979      case OP_ANY:
2980      case OP_ALLANY:
2981      case OP_ANYNL:
2982      case OP_NOT_HSPACE:
2983      case OP_HSPACE:
2984      case OP_NOT_VSPACE:
2985      case OP_VSPACE:
2986      case OP_EXTUNI:
2987      case OP_EODN:
2988      case OP_EOD:
2989      case OP_DOLL:
2990      case OP_DOLLM:
2991      return code;
2992    
2993      case OP_CHAR:
2994      case OP_NOT:
2995      GETCHARINCTEST(chr, code);
2996      list[2] = chr;
2997      list[3] = NOTACHAR;
2998      return code;
2999    
3000      case OP_CHARI:
3001      case OP_NOTI:
3002      list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
3003      GETCHARINCTEST(chr, code);
3004      list[2] = chr;
3005    
3006    #ifdef SUPPORT_UCP
3007      if (chr < 128 || (chr < 256 && !utf))
3008        list[3] = fcc[chr];
3009      else
3010        list[3] = UCD_OTHERCASE(chr);
3011    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
3012      list[3] = (chr < 256) ? fcc[chr] : chr;
3013    #else
3014      list[3] = fcc[chr];
3015    #endif
3016    
3017      /* The othercase might be the same value. */
3018    
3019      if (chr == list[3])
3020        list[3] = NOTACHAR;
3021      else
3022        list[4] = NOTACHAR;
3023      return code;
3024    
3025    #ifdef SUPPORT_UCP
3026      case OP_PROP:
3027      case OP_NOTPROP:
3028      if (code[0] != PT_CLIST)
3029        {
3030        list[2] = code[0];
3031        list[3] = code[1];
3032        return code + 2;
3033        }
3034    
3035      /* Convert only if we have enough space. */
3036    
3037      clist_src = PRIV(ucd_caseless_sets) + code[1];
3038      clist_dest = list + 2;
3039      code += 2;
3040    
3041      do {
3042         if (clist_dest >= list + 8)
3043           {
3044           /* Early return if there is not enough space. This should never
3045           happen, since all clists are shorter than 5 character now. */
3046           list[2] = code[0];
3047           list[3] = code[1];
3048           return code;
3049           }
3050         *clist_dest++ = *clist_src;
3051         }
3052      while(*clist_src++ != NOTACHAR);
3053    
3054      /* All characters are stored. The terminating NOTACHAR
3055      is copied form the clist itself. */
3056    
3057      list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
3058      return code;
3059    #endif
3060    
3061      case OP_NCLASS:
3062      case OP_CLASS:
3063    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3064      case OP_XCLASS:
3065      if (c == OP_XCLASS)
3066        end = code + GET(code, 0) - 1;
3067      else
3068    #endif
3069        end = code + 32 / sizeof(pcre_uchar);
3070    
3071      switch(*end)
3072        {
3073        case OP_CRSTAR:
3074        case OP_CRMINSTAR:
3075        case OP_CRQUERY:
3076        case OP_CRMINQUERY:
3077        case OP_CRPOSSTAR:
3078        case OP_CRPOSQUERY:
3079        list[1] = TRUE;
3080        end++;
3081        break;
3082    
3083        case OP_CRPLUS:
3084        case OP_CRMINPLUS:
3085        case OP_CRPOSPLUS:
3086        end++;
3087        break;
3088    
3089        case OP_CRRANGE:
3090        case OP_CRMINRANGE:
3091        case OP_CRPOSRANGE:
3092        list[1] = (GET2(end, 1) == 0);
3093        end += 1 + 2 * IMM2_SIZE;
3094        break;
3095        }
3096      list[2] = (pcre_uint32)(end - code);
3097      return end;
3098      }
3099    return NULL;    /* Opcode not accepted */
3100    }
3101    
3102    
3103    
3104    /*************************************************
3105    *    Scan further character sets for match       *
3106    *************************************************/
3107    
3108    /* Checks whether the base and the current opcode have a common character, in
3109    which case the base cannot be possessified.
3110    
3111    Arguments:
3112      code        points to the byte code
3113      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3114      cd          static compile data
3115      base_list   the data list of the base opcode
3116    
3117    Returns:      TRUE if the auto-possessification is possible
3118    */
3119    
3120    static BOOL
3121    compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
3122      const pcre_uint32 *base_list, const pcre_uchar *base_end, int *rec_limit)
3123    {
3124    pcre_uchar c;
3125    pcre_uint32 list[8];
3126    const pcre_uint32 *chr_ptr;
3127    const pcre_uint32 *ochr_ptr;
3128    const pcre_uint32 *list_ptr;
3129    const pcre_uchar *next_code;
3130    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3131    const pcre_uchar *xclass_flags;
3132    #endif
3133    const pcre_uint8 *class_bitset;
3134    const pcre_uint8 *set1, *set2, *set_end;
3135    pcre_uint32 chr;
3136    BOOL accepted, invert_bits;
3137    BOOL entered_a_group = FALSE;
3138    
3139    if (*rec_limit == 0) return FALSE;
3140    --(*rec_limit);
3141    
3142    /* Note: the base_list[1] contains whether the current opcode has greedy
3143    (represented by a non-zero value) quantifier. This is a different from
3144    other character type lists, which stores here that the character iterator
3145    matches to an empty string (also represented by a non-zero value). */
3146    
3147    for(;;)
3148      {
3149      /* All operations move the code pointer forward.
3150      Therefore infinite recursions are not possible. */
3151    
3152      c = *code;
3153    
3154      /* Skip over callouts */
3155    
3156      if (c == OP_CALLOUT)
3157        {
3158        code += PRIV(OP_lengths)[c];
3159        continue;
3160        }
3161    
3162      if (c == OP_ALT)
3163        {
3164        do code += GET(code, 1); while (*code == OP_ALT);
3165        c = *code;
3166        }
3167    
3168      switch(c)
3169        {
3170        case OP_END:
3171        case OP_KETRPOS:
3172        /* TRUE only in greedy case. The non-greedy case could be replaced by
3173        an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT
3174        uses more memory, which we cannot get at this stage.) */
3175    
3176        return base_list[1] != 0;
3177    
3178        case OP_KET:
3179        /* If the bracket is capturing, and referenced by an OP_RECURSE, or
3180        it is an atomic sub-pattern (assert, once, etc.) the non-greedy case
3181        cannot be converted to a possessive form. */
3182    
3183        if (base_list[1] == 0) return FALSE;
3184    
3185        switch(*(code - GET(code, 1)))
3186          {
3187          case OP_ASSERT:
3188          case OP_ASSERT_NOT:
3189          case OP_ASSERTBACK:
3190          case OP_ASSERTBACK_NOT:
3191          case OP_ONCE:
3192          case OP_ONCE_NC:
3193          /* Atomic sub-patterns and assertions can always auto-possessify their
3194          last iterator. However, if the group was entered as a result of checking
3195          a previous iterator, this is not possible. */
3196    
3197          return !entered_a_group;
3198          }
3199    
3200        code += PRIV(OP_lengths)[c];
3201        continue;
3202    
3203        case OP_ONCE:
3204        case OP_ONCE_NC:
3205        case OP_BRA:
3206        case OP_CBRA:
3207        next_code = code + GET(code, 1);
3208        code += PRIV(OP_lengths)[c];
3209    
3210        while (*next_code == OP_ALT)
3211          {
3212          if (!compare_opcodes(code, utf, cd, base_list, base_end, rec_limit))
3213            return FALSE;
3214          code = next_code + 1 + LINK_SIZE;
3215          next_code += GET(next_code, 1);
3216          }
3217    
3218        entered_a_group = TRUE;
3219        continue;
3220    
3221        case OP_BRAZERO:
3222        case OP_BRAMINZERO:
3223    
3224        next_code = code + 1;
3225        if (*next_code != OP_BRA && *next_code != OP_CBRA
3226            && *next_code != OP_ONCE && *next_code != OP_ONCE_NC) return FALSE;
3227    
3228        do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3229    
3230        /* The bracket content will be checked by the
3231        OP_BRA/OP_CBRA case above. */
3232        next_code += 1 + LINK_SIZE;
3233        if (!compare_opcodes(next_code, utf, cd, base_list, base_end, rec_limit))
3234          return FALSE;
3235    
3236        code += PRIV(OP_lengths)[c];
3237        continue;
3238    
3239        default:
3240        break;
3241        }
3242    
3243      /* Check for a supported opcode, and load its properties. */
3244    
3245      code = get_chr_property_list(code, utf, cd->fcc, list);
3246      if (code == NULL) return FALSE;    /* Unsupported */
3247    
3248      /* If either opcode is a small character list, set pointers for comparing
3249      characters from that list with another list, or with a property. */
3250    
3251      if (base_list[0] == OP_CHAR)
3252        {
3253        chr_ptr = base_list + 2;
3254        list_ptr = list;
3255        }
3256      else if (list[0] == OP_CHAR)
3257        {
3258        chr_ptr = list + 2;
3259        list_ptr = base_list;
3260        }
3261    
3262      /* Character bitsets can also be compared to certain opcodes. */
3263    
3264      else if (base_list[0] == OP_CLASS || list[0] == OP_CLASS
3265    #ifdef COMPILE_PCRE8
3266          /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */
3267          || (!utf && (base_list[0] == OP_NCLASS || list[0] == OP_NCLASS))
3268    #endif
3269          )
3270        {
3271    #ifdef COMPILE_PCRE8
3272        if (base_list[0] == OP_CLASS || (!utf && base_list[0] == OP_NCLASS))
3273    #else
3274        if (base_list[0] == OP_CLASS)
3275    #endif
3276          {
3277          set1 = (pcre_uint8 *)(base_end - base_list[2]);
3278          list_ptr = list;
3279          }
3280        else
3281          {
3282          set1 = (pcre_uint8 *)(code - list[2]);
3283          list_ptr = base_list;
3284          }
3285    
3286        invert_bits = FALSE;
3287        switch(list_ptr[0])
3288          {
3289          case OP_CLASS:
3290          case OP_NCLASS:
3291          set2 = (pcre_uint8 *)
3292            ((list_ptr == list ? code : base_end) - list_ptr[2]);
3293          break;
3294    
3295    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3296          case OP_XCLASS:
3297          xclass_flags = (list_ptr == list ? code : base_end) - list_ptr[2] + LINK_SIZE;
3298          if ((*xclass_flags & XCL_HASPROP) != 0) return FALSE;
3299          if ((*xclass_flags & XCL_MAP) == 0)
3300            {
3301            /* No bits are set for characters < 256. */
3302            if (list[1] == 0) return TRUE;
3303            /* Might be an empty repeat. */
3304            continue;
3305            }
3306          set2 = (pcre_uint8 *)(xclass_flags + 1);
3307          break;
3308    #endif
3309    
3310          case OP_NOT_DIGIT:
3311          invert_bits = TRUE;
3312          /* Fall through */
3313          case OP_DIGIT:
3314          set2 = (pcre_uint8 *)(cd->cbits + cbit_digit);
3315          break;
3316    
3317          case OP_NOT_WHITESPACE:
3318          invert_bits = TRUE;
3319          /* Fall through */
3320          case OP_WHITESPACE:
3321          set2 = (pcre_uint8 *)(cd->cbits + cbit_space);
3322          break;
3323    
3324          case OP_NOT_WORDCHAR:
3325          invert_bits = TRUE;
3326          /* Fall through */
3327          case OP_WORDCHAR:
3328          set2 = (pcre_uint8 *)(cd->cbits + cbit_word);
3329          break;
3330    
3331          default:
3332          return FALSE;
3333          }
3334    
3335        /* Because the sets are unaligned, we need
3336        to perform byte comparison here. */
3337        set_end = set1 + 32;
3338        if (invert_bits)
3339          {
3340          do
3341            {
3342            if ((*set1++ & ~(*set2++)) != 0) return FALSE;
3343            }
3344          while (set1 < set_end);
3345          }
3346        else
3347          {
3348          do
3349            {
3350            if ((*set1++ & *set2++) != 0) return FALSE;
3351            }
3352          while (set1 < set_end);
3353          }
3354    
3355        if (list[1] == 0) return TRUE;
3356        /* Might be an empty repeat. */
3357        continue;
3358        }
3359    
3360      /* Some property combinations also acceptable. Unicode property opcodes are
3361      processed specially; the rest can be handled with a lookup table. */
3362    
3363      else
3364        {
3365        pcre_uint32 leftop, rightop;
3366    
3367        leftop = base_list[0];
3368        rightop = list[0];
3369    
3370    #ifdef SUPPORT_UCP
3371        accepted = FALSE; /* Always set in non-unicode case. */
3372        if (leftop == OP_PROP || leftop == OP_NOTPROP)
3373          {
3374          if (rightop == OP_EOD)
3375            accepted = TRUE;
3376          else if (rightop == OP_PROP || rightop == OP_NOTPROP)
3377            {
3378            int n;
3379            const pcre_uint8 *p;
3380            BOOL same = leftop == rightop;
3381            BOOL lisprop = leftop == OP_PROP;
3382            BOOL risprop = rightop == OP_PROP;
3383            BOOL bothprop = lisprop && risprop;
3384    
3385            /* There's a table that specifies how each combination is to be
3386            processed:
3387              0   Always return FALSE (never auto-possessify)
3388              1   Character groups are distinct (possessify if both are OP_PROP)
3389              2   Check character categories in the same group (general or particular)
3390              3   Return TRUE if the two opcodes are not the same
3391              ... see comments below
3392            */
3393    
3394            n = propposstab[base_list[2]][list[2]];
3395            switch(n)
3396              {
3397              case 0: break;
3398              case 1: accepted = bothprop; break;
3399              case 2: accepted = (base_list[3] == list[3]) != same; break;
3400              case 3: accepted = !same; break;
3401    
3402              case 4:  /* Left general category, right particular category */
3403              accepted = risprop && catposstab[base_list[3]][list[3]] == same;
3404              break;
3405    
3406              case 5:  /* Right general category, left particular category */
3407              accepted = lisprop && catposstab[list[3]][base_list[3]] == same;
3408              break;
3409    
3410              /* This code is logically tricky. Think hard before fiddling with it.
3411              The posspropstab table has four entries per row. Each row relates to
3412              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3413              Only WORD actually needs all four entries, but using repeats for the
3414              others means they can all use the same code below.
3415    
3416              The first two entries in each row are Unicode general categories, and
3417              apply always, because all the characters they include are part of the
3418              PCRE character set. The third and fourth entries are a general and a
3419              particular category, respectively, that include one or more relevant
3420              characters. One or the other is used, depending on whether the check
3421              is for a general or a particular category. However, in both cases the
3422              category contains more characters than the specials that are defined
3423              for the property being tested against. Therefore, it cannot be used
3424              in a NOTPROP case.
3425    
3426              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3427              Underscore is covered by ucp_P or ucp_Po. */
3428    
3429              case 6:  /* Left alphanum vs right general category */
3430              case 7:  /* Left space vs right general category */
3431              case 8:  /* Left word vs right general category */
3432              p = posspropstab[n-6];
3433              accepted = risprop && lisprop ==
3434                (list[3] != p[0] &&
3435                 list[3] != p[1] &&
3436                (list[3] != p[2] || !lisprop));
3437              break;
3438    
3439              case 9:   /* Right alphanum vs left general category */
3440              case 10:  /* Right space vs left general category */
3441              case 11:  /* Right word vs left general category */
3442              p = posspropstab[n-9];
3443              accepted = lisprop && risprop ==
3444                (base_list[3] != p[0] &&
3445                 base_list[3] != p[1] &&
3446                (base_list[3] != p[2] || !risprop));
3447              break;
3448    
3449              case 12:  /* Left alphanum vs right particular category */
3450              case 13:  /* Left space vs right particular category */
3451              case 14:  /* Left word vs right particular category */
3452              p = posspropstab[n-12];
3453              accepted = risprop && lisprop ==
3454                (catposstab[p[0]][list[3]] &&
3455                 catposstab[p[1]][list[3]] &&
3456                (list[3] != p[3] || !lisprop));
3457              break;
3458    
3459              case 15:  /* Right alphanum vs left particular category */
3460              case 16:  /* Right space vs left particular category */
3461              case 17:  /* Right word vs left particular category */
3462              p = posspropstab[n-15];
3463              accepted = lisprop && risprop ==
3464                (catposstab[p[0]][base_list[3]] &&
3465                 catposstab[p[1]][base_list[3]] &&
3466                (base_list[3] != p[3] || !risprop));
3467              break;
3468              }
3469            }
3470          }
3471    
3472        else
3473    #endif  /* SUPPORT_UCP */
3474    
3475        accepted = leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3476               rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3477               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3478    
3479        if (!accepted) return FALSE;
3480    
3481        if (list[1] == 0) return TRUE;
3482        /* Might be an empty repeat. */
3483        continue;
3484        }
3485    
3486      /* Control reaches here only if one of the items is a small character list.
3487      All characters are checked against the other side. */
3488    
3489      do
3490        {
3491        chr = *chr_ptr;
3492    
3493        switch(list_ptr[0])
3494          {
3495          case OP_CHAR:
3496          ochr_ptr = list_ptr + 2;
3497          do
3498            {
3499            if (chr == *ochr_ptr) return FALSE;
3500            ochr_ptr++;
3501            }
3502          while(*ochr_ptr != NOTACHAR);
3503          break;
3504    
3505          case OP_NOT:
3506          ochr_ptr = list_ptr + 2;
3507          do
3508            {
3509            if (chr == *ochr_ptr)
3510              break;
3511            ochr_ptr++;
3512            }
3513          while(*ochr_ptr != NOTACHAR);
3514          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3515          break;
3516    
3517          /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3518          set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3519    
3520          case OP_DIGIT:
3521          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3522          break;
3523    
3524          case OP_NOT_DIGIT:
3525          if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3526          break;
3527    
3528          case OP_WHITESPACE:
3529          if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3530          break;
3531    
3532          case OP_NOT_WHITESPACE:
3533          if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3534          break;
3535    
3536          case OP_WORDCHAR:
3537          if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3538          break;
3539    
3540          case OP_NOT_WORDCHAR:
3541          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3542          break;
3543    
3544          case OP_HSPACE:
3545          switch(chr)
3546            {
3547            HSPACE_CASES: return FALSE;
3548            default: break;
3549            }
3550          break;
3551    
3552          case OP_NOT_HSPACE:
3553          switch(chr)
3554            {
3555            HSPACE_CASES: break;
3556            default: return FALSE;
3557            }
3558          break;
3559    
3560          case OP_ANYNL:
3561          case OP_VSPACE:
3562          switch(chr)
3563            {
3564            VSPACE_CASES: return FALSE;
3565            default: break;
3566            }
3567          break;
3568    
3569          case OP_NOT_VSPACE:
3570          switch(chr)
3571            {
3572            VSPACE_CASES: break;
3573            default: return FALSE;
3574            }
3575          break;
3576    
3577          case OP_DOLL:
3578          case OP_EODN:
3579          switch (chr)
3580            {
3581            case CHAR_CR:
3582            case CHAR_LF:
3583            case CHAR_VT:
3584            case CHAR_FF:
3585            case CHAR_NEL:
3586    #ifndef EBCDIC
3587            case 0x2028:
3588            case 0x2029:
3589    #endif  /* Not EBCDIC */
3590            return FALSE;
3591            }
3592          break;
3593    
3594          case OP_EOD:    /* Can always possessify before \z */
3595          break;
3596    
3597    #ifdef SUPPORT_UCP
3598          case OP_PROP:
3599          case OP_NOTPROP:
3600          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3601                list_ptr[0] == OP_NOTPROP))
3602            return FALSE;
3603          break;
3604    #endif
3605    
3606          case OP_NCLASS: