/[pcre]/code/branches/pcre16/pcre_compile.c
ViewVC logotype

Diff of /code/branches/pcre16/pcre_compile.c

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

code/trunk/pcre_compile.c revision 180 by ph10, Wed Jun 13 10:59:18 2007 UTC code/branches/pcre16/pcre_compile.c revision 774 by zherczeg, Thu Dec 1 06:08:45 2011 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2007 University of Cambridge             Copyright (c) 1997-2011 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 42  POSSIBILITY OF SUCH DAMAGE. Line 42  POSSIBILITY OF SUCH DAMAGE.
42  supporting internal functions that are not used by other modules. */  supporting internal functions that are not used by other modules. */
43    
44    
45    #ifdef HAVE_CONFIG_H
46    #include "config.h"
47    #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 processed string start */
51  #define PSEND   end_pattern    /* Field containing processed string end */  #define PSEND   end_pattern    /* Field containing processed string end */
52    
   
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    
56  /* When DEBUG is defined, we need the pcre_printint() function, which is also  /* When PCRE_DEBUG is defined, we need the pcre_printint() function, which is
57  used by pcretest. DEBUG is not defined when building a production library. */  also used by pcretest. PCRE_DEBUG is not defined when building a production
58    library. */
59    
60  #ifdef DEBUG  #ifdef PCRE_DEBUG
61  #include "pcre_printint.src"  #include "pcre_printint.src"
62  #endif  #endif
63    
# Line 62  used by pcretest. DEBUG is not defined w Line 66  used by pcretest. DEBUG is not defined w
66    
67  #define SETBIT(a,b) a[b/8] |= (1 << (b%8))  #define SETBIT(a,b) a[b/8] |= (1 << (b%8))
68    
69    /* Maximum length value to check against when making sure that the integer that
70    holds the compiled pattern length does not overflow. We make it a bit less than
71    INT_MAX to allow for adding in group terminating bytes, so that we don't have
72    to check them every time. */
73    
74    #define OFLOW_MAX (INT_MAX - 20)
75    
76    
77  /*************************************************  /*************************************************
78  *      Code parameters and static tables         *  *      Code parameters and static tables         *
# Line 81  is 4 there is plenty of room. */ Line 92  is 4 there is plenty of room. */
92    
93  #define COMPILE_WORK_SIZE (4096)  #define COMPILE_WORK_SIZE (4096)
94    
95    /* The overrun tests check for a slightly smaller size so that they detect the
96    overrun before it actually does run off the end of the data block. */
97    
98    #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)
99    
100    /* Private flags added to firstchar and reqchar. */
101    
102    #define REQ_CASELESS   0x10000000l      /* Indicates caselessness */
103    #define REQ_VARY       0x20000000l      /* Reqchar followed non-literal item */
104    
105  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
106  are simple data values; negative values are for special things like \d and so  are simple data values; negative values are for special things like \d and so
107  on. Zero means further processing is needed (for things like \x), or the escape  on. Zero means further processing is needed (for things like \x), or the escape
108  is invalid. */  is invalid. */
109    
110  #ifndef EBCDIC  /* This is the "normal" table for ASCII systems */  #ifndef EBCDIC
111    
112    /* This is the "normal" table for ASCII systems or for EBCDIC systems running
113    in UTF-8 mode. */
114    
115  static const short int escapes[] = {  static const short int escapes[] = {
116       0,      0,      0,      0,      0,      0,      0,      0,   /* 0 - 7 */       0,                       0,
117       0,      0,    ':',    ';',    '<',    '=',    '>',    '?',   /* 8 - ? */       0,                       0,
118     '@', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E,      0, -ESC_G,   /* @ - G */       0,                       0,
119  -ESC_H,      0,      0, -ESC_K,      0,      0,      0,      0,   /* H - O */       0,                       0,
120  -ESC_P, -ESC_Q, -ESC_R, -ESC_S,      0,      0, -ESC_V, -ESC_W,   /* P - W */       0,                       0,
121  -ESC_X,      0, -ESC_Z,    '[',   '\\',    ']',    '^',    '_',   /* X - _ */       CHAR_COLON,              CHAR_SEMICOLON,
122     '`',      7, -ESC_b,      0, -ESC_d,  ESC_e,  ESC_f,      0,   /* ` - g */       CHAR_LESS_THAN_SIGN,     CHAR_EQUALS_SIGN,
123  -ESC_h,      0,      0, -ESC_k,      0,      0,  ESC_n,      0,   /* h - o */       CHAR_GREATER_THAN_SIGN,  CHAR_QUESTION_MARK,
124  -ESC_p,      0,  ESC_r, -ESC_s,  ESC_tee,    0, -ESC_v, -ESC_w,   /* p - w */       CHAR_COMMERCIAL_AT,      -ESC_A,
125       0,      0, -ESC_z                                            /* x - z */       -ESC_B,                  -ESC_C,
126         -ESC_D,                  -ESC_E,
127         0,                       -ESC_G,
128         -ESC_H,                  0,
129         0,                       -ESC_K,
130         0,                       0,
131         -ESC_N,                  0,
132         -ESC_P,                  -ESC_Q,
133         -ESC_R,                  -ESC_S,
134         0,                       0,
135         -ESC_V,                  -ESC_W,
136         -ESC_X,                  0,
137         -ESC_Z,                  CHAR_LEFT_SQUARE_BRACKET,
138         CHAR_BACKSLASH,          CHAR_RIGHT_SQUARE_BRACKET,
139         CHAR_CIRCUMFLEX_ACCENT,  CHAR_UNDERSCORE,
140         CHAR_GRAVE_ACCENT,       7,
141         -ESC_b,                  0,
142         -ESC_d,                  ESC_e,
143         ESC_f,                   0,
144         -ESC_h,                  0,
145         0,                       -ESC_k,
146         0,                       0,
147         ESC_n,                   0,
148         -ESC_p,                  0,
149         ESC_r,                   -ESC_s,
150         ESC_tee,                 0,
151         -ESC_v,                  -ESC_w,
152         0,                       0,
153         -ESC_z
154  };  };
155    
156  #else           /* This is the "abnormal" table for EBCDIC systems */  #else
157    
158    /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
159    
160  static const short int escapes[] = {  static const short int escapes[] = {
161  /*  48 */     0,     0,      0,     '.',    '<',   '(',    '+',    '|',  /*  48 */     0,     0,      0,     '.',    '<',   '(',    '+',    '|',
162  /*  50 */   '&',     0,      0,       0,      0,     0,      0,      0,  /*  50 */   '&',     0,      0,       0,      0,     0,      0,      0,
# Line 120  static const short int escapes[] = { Line 175  static const short int escapes[] = {
175  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',
176  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,
177  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,
178  /*  D0 */   '}',     0,      0,       0,      0,     0,      0, -ESC_P,  /*  D0 */   '}',     0, -ESC_K,       0,      0,-ESC_N,      0, -ESC_P,
179  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,
180  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,
181  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,
# Line 130  static const short int escapes[] = { Line 185  static const short int escapes[] = {
185  #endif  #endif
186    
187    
188  /* Tables of names of POSIX character classes and their lengths. The list is  /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
189  terminated by a zero length entry. The first three must be alpha, lower, upper,  searched linearly. Put all the names into a single string, in order to reduce
190  as this is assumed for handling case independence. */  the number of relocations when a shared library is dynamically linked. The
191    string is built from string macros so that it works in UTF-8 mode on EBCDIC
192  static const char *const posix_names[] = {  platforms. */
193    "alpha", "lower", "upper",  
194    "alnum", "ascii", "blank", "cntrl", "digit", "graph",  typedef struct verbitem {
195    "print", "punct", "space", "word",  "xdigit" };    int   len;                 /* Length of verb name */
196      int   op;                  /* Op when no arg, or -1 if arg mandatory */
197      int   op_arg;              /* Op when arg present, or -1 if not allowed */
198    } verbitem;
199    
200    static const char verbnames[] =
201      "\0"                       /* Empty name is a shorthand for MARK */
202      STRING_MARK0
203      STRING_ACCEPT0
204      STRING_COMMIT0
205      STRING_F0
206      STRING_FAIL0
207      STRING_PRUNE0
208      STRING_SKIP0
209      STRING_THEN;
210    
211    static const verbitem verbs[] = {
212      { 0, -1,        OP_MARK },
213      { 4, -1,        OP_MARK },
214      { 6, OP_ACCEPT, -1 },
215      { 6, OP_COMMIT, -1 },
216      { 1, OP_FAIL,   -1 },
217      { 4, OP_FAIL,   -1 },
218      { 5, OP_PRUNE,  OP_PRUNE_ARG },
219      { 4, OP_SKIP,   OP_SKIP_ARG  },
220      { 4, OP_THEN,   OP_THEN_ARG  }
221    };
222    
223    static const int verbcount = sizeof(verbs)/sizeof(verbitem);
224    
225    
226    /* Tables of names of POSIX character classes and their lengths. The names are
227    now all in a single string, to reduce the number of relocations when a shared
228    library is dynamically loaded. The list of lengths is terminated by a zero
229    length entry. The first three must be alpha, lower, upper, as this is assumed
230    for handling case independence. */
231    
232    static const char posix_names[] =
233      STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
234      STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
235      STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
236      STRING_word0  STRING_xdigit;
237    
238  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
239    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
240    
241  /* Table of class bit maps for each POSIX class. Each class is formed from a  /* Table of class bit maps for each POSIX class. Each class is formed from a
# Line 169  static const int posix_class_maps[] = { Line 265  static const int posix_class_maps[] = {
265    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
266  };  };
267    
268    /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
269    substitutes must be in the order of the names, defined above, and there are
270    both positive and negative cases. NULL means no substitute. */
271    
272    #ifdef SUPPORT_UCP
273    static const pcre_uchar string_PNd[]  = {
274      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
275      CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
276    static const pcre_uchar string_pNd[]  = {
277      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
278      CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
279    static const pcre_uchar string_PXsp[] = {
280      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
281      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
282    static const pcre_uchar string_pXsp[] = {
283      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
284      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
285    static const pcre_uchar string_PXwd[] = {
286      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
287      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
288    static const pcre_uchar string_pXwd[] = {
289      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
290      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
291    
292    static const pcre_uchar *substitutes[] = {
293      string_PNd,           /* \D */
294      string_pNd,           /* \d */
295      string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
296      string_pXsp,          /* \s */
297      string_PXwd,          /* \W */
298      string_pXwd           /* \w */
299    };
300    
301    static const pcre_uchar string_pL[] =   {
302      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
303      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
304    static const pcre_uchar string_pLl[] =  {
305      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
306      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
307    static const pcre_uchar string_pLu[] =  {
308      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
309      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
310    static const pcre_uchar string_pXan[] = {
311      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
312      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
313    static const pcre_uchar string_h[] =    {
314      CHAR_BACKSLASH, CHAR_h, '\0' };
315    static const pcre_uchar string_pXps[] = {
316      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
317      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
318    static const pcre_uchar string_PL[] =   {
319      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
320      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
321    static const pcre_uchar string_PLl[] =  {
322      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
323      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
324    static const pcre_uchar string_PLu[] =  {
325      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
326      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
327    static const pcre_uchar string_PXan[] = {
328      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
329      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
330    static const pcre_uchar string_H[] =    {
331      CHAR_BACKSLASH, CHAR_H, '\0' };
332    static const pcre_uchar string_PXps[] = {
333      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
334      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
335    
336    static const pcre_uchar *posix_substitutes[] = {
337      string_pL,            /* alpha */
338      string_pLl,           /* lower */
339      string_pLu,           /* upper */
340      string_pXan,          /* alnum */
341      NULL,                 /* ascii */
342      string_h,             /* blank */
343      NULL,                 /* cntrl */
344      string_pNd,           /* digit */
345      NULL,                 /* graph */
346      NULL,                 /* print */
347      NULL,                 /* punct */
348      string_pXps,          /* space */    /* NOTE: Xps is POSIX space */
349      string_pXwd,          /* word */
350      NULL,                 /* xdigit */
351      /* Negated cases */
352      string_PL,            /* ^alpha */
353      string_PLl,           /* ^lower */
354      string_PLu,           /* ^upper */
355      string_PXan,          /* ^alnum */
356      NULL,                 /* ^ascii */
357      string_H,             /* ^blank */
358      NULL,                 /* ^cntrl */
359      string_PNd,           /* ^digit */
360      NULL,                 /* ^graph */
361      NULL,                 /* ^print */
362      NULL,                 /* ^punct */
363      string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
364      string_PXwd,          /* ^word */
365      NULL                  /* ^xdigit */
366    };
367    #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
368    #endif
369    
370  #define STRING(a)  # a  #define STRING(a)  # a
371  #define XSTRING(s) STRING(s)  #define XSTRING(s) STRING(s)
# Line 176  static const int posix_class_maps[] = { Line 373  static const int posix_class_maps[] = {
373  /* The texts of compile-time error messages. These are "char *" because they  /* The texts of compile-time error messages. These are "char *" because they
374  are passed to the outside world. Do not ever re-use any error number, because  are passed to the outside world. Do not ever re-use any error number, because
375  they are documented. Always add a new error instead. Messages marked DEAD below  they are documented. Always add a new error instead. Messages marked DEAD below
376  are no longer used. */  are no longer used. This used to be a table of strings, but in order to reduce
377    the number of relocations needed when a shared library is loaded dynamically,
378  static const char *error_texts[] = {  it is now one long string. We cannot use a table of offsets, because the
379    "no error",  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
380    "\\ at end of pattern",  simply count through to the one we want - this isn't a performance issue
381    "\\c at end of pattern",  because these strings are used only when there is a compilation error.
382    "unrecognized character follows \\",  
383    "numbers out of order in {} quantifier",  Each substring ends with \0 to insert a null character. This includes the final
384    substring, so that the whole string ends with \0\0, which can be detected when
385    counting through. */
386    
387    static const char error_texts[] =
388      "no error\0"
389      "\\ at end of pattern\0"
390      "\\c at end of pattern\0"
391      "unrecognized character follows \\\0"
392      "numbers out of order in {} quantifier\0"
393    /* 5 */    /* 5 */
394    "number too big in {} quantifier",    "number too big in {} quantifier\0"
395    "missing terminating ] for character class",    "missing terminating ] for character class\0"
396    "invalid escape sequence in character class",    "invalid escape sequence in character class\0"
397    "range out of order in character class",    "range out of order in character class\0"
398    "nothing to repeat",    "nothing to repeat\0"
399    /* 10 */    /* 10 */
400    "operand of unlimited repeat could match the empty string",  /** DEAD **/    "operand of unlimited repeat could match the empty string\0"  /** DEAD **/
401    "internal error: unexpected repeat",    "internal error: unexpected repeat\0"
402    "unrecognized character after (?",    "unrecognized character after (? or (?-\0"
403    "POSIX named classes are supported only within a class",    "POSIX named classes are supported only within a class\0"
404    "missing )",    "missing )\0"
405    /* 15 */    /* 15 */
406    "reference to non-existent subpattern",    "reference to non-existent subpattern\0"
407    "erroffset passed as NULL",    "erroffset passed as NULL\0"
408    "unknown option bit(s) set",    "unknown option bit(s) set\0"
409    "missing ) after comment",    "missing ) after comment\0"
410    "parentheses nested too deeply",  /** DEAD **/    "parentheses nested too deeply\0"  /** DEAD **/
411    /* 20 */    /* 20 */
412    "regular expression too large",    "regular expression is too large\0"
413    "failed to get memory",    "failed to get memory\0"
414    "unmatched parentheses",    "unmatched parentheses\0"
415    "internal error: code overflow",    "internal error: code overflow\0"
416    "unrecognized character after (?<",    "unrecognized character after (?<\0"
417    /* 25 */    /* 25 */
418    "lookbehind assertion is not fixed length",    "lookbehind assertion is not fixed length\0"
419    "malformed number or name after (?(",    "malformed number or name after (?(\0"
420    "conditional group contains more than two branches",    "conditional group contains more than two branches\0"
421    "assertion expected after (?(",    "assertion expected after (?(\0"
422    "(?R or (?[+-]digits must be followed by )",    "(?R or (?[+-]digits must be followed by )\0"
423    /* 30 */    /* 30 */
424    "unknown POSIX class name",    "unknown POSIX class name\0"
425    "POSIX collating elements are not supported",    "POSIX collating elements are not supported\0"
426    "this version of PCRE is not compiled with PCRE_UTF8 support",    "this version of PCRE is not compiled with PCRE_UTF8 support\0"
427    "spare error",  /** DEAD **/    "spare error\0"  /** DEAD **/
428    "character value in \\x{...} sequence is too large",    "character value in \\x{...} sequence is too large\0"
429    /* 35 */    /* 35 */
430    "invalid condition (?(0)",    "invalid condition (?(0)\0"
431    "\\C not allowed in lookbehind assertion",    "\\C not allowed in lookbehind assertion\0"
432    "PCRE does not support \\L, \\l, \\N, \\U, or \\u",    "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
433    "number after (?C is > 255",    "number after (?C is > 255\0"
434    "closing ) for (?C expected",    "closing ) for (?C expected\0"
435    /* 40 */    /* 40 */
436    "recursive call could loop indefinitely",    "recursive call could loop indefinitely\0"
437    "unrecognized character after (?P",    "unrecognized character after (?P\0"
438    "syntax error in subpattern name (missing terminator)",    "syntax error in subpattern name (missing terminator)\0"
439    "two named subpatterns have the same name",    "two named subpatterns have the same name\0"
440    "invalid UTF-8 string",    "invalid UTF-8 string\0"
441    /* 45 */    /* 45 */
442    "support for \\P, \\p, and \\X has not been compiled",    "support for \\P, \\p, and \\X has not been compiled\0"
443    "malformed \\P or \\p sequence",    "malformed \\P or \\p sequence\0"
444    "unknown property name after \\P or \\p",    "unknown property name after \\P or \\p\0"
445    "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)",    "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
446    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")",    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
447    /* 50 */    /* 50 */
448    "repeated subpattern is too long",    "repeated subpattern is too long\0"    /** DEAD **/
449    "octal value is greater than \\377 (not in UTF-8 mode)",    "octal value is greater than \\377 (not in UTF-8 mode)\0"
450    "internal error: overran compiling workspace",    "internal error: overran compiling workspace\0"
451    "internal error: previously-checked referenced subpattern not found",    "internal error: previously-checked referenced subpattern not found\0"
452    "DEFINE group contains more than one branch",    "DEFINE group contains more than one branch\0"
453    /* 55 */    /* 55 */
454    "repeating a DEFINE group is not allowed",    "repeating a DEFINE group is not allowed\0"  /** DEAD **/
455    "inconsistent NEWLINE options",    "inconsistent NEWLINE options\0"
456    "\\g is not followed by a braced name or an optionally braced non-zero number",    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
457    "(?+ or (?- or (?(+ or (?(- must be followed by a non-zero number"    "a numbered reference must not be zero\0"
458  };    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
459      /* 60 */
460      "(*VERB) not recognized\0"
461      "number is too big\0"
462      "subpattern name expected\0"
463      "digit expected after (?+\0"
464      "] is an invalid data character in JavaScript compatibility mode\0"
465      /* 65 */
466      "different names for subpatterns of the same number are not allowed\0"
467      "(*MARK) must have an argument\0"
468      "this version of PCRE is not compiled with PCRE_UCP support\0"
469      "\\c must be followed by an ASCII character\0"
470      "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
471      /* 70 */
472      "internal error: unknown opcode in find_fixedlength()\0"
473      ;
474    
475  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
476  patterns. Note that the tables in chartables are dependent on the locale, and  patterns. Note that the tables in chartables are dependent on the locale, and
# Line 268  For convenience, we use the same bit def Line 488  For convenience, we use the same bit def
488    
489  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
490    
491  #ifndef EBCDIC  /* This is the "normal" case, for ASCII systems */  /* Using a simple comparison for decimal numbers rather than a memory read
492  static const unsigned char digitab[] =  is much faster, and the resulting code is simpler (the compiler turns it
493    into a subtraction and unsigned comparison). */
494    
495    #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
496    
497    #ifndef EBCDIC
498    
499    /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
500    UTF-8 mode. */
501    
502    static const pcre_uint8 digitab[] =
503    {    {
504    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
505    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */
# Line 304  static const unsigned char digitab[] = Line 534  static const unsigned char digitab[] =
534    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
535    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
536    
537  #else           /* This is the "abnormal" case, for EBCDIC systems */  #else
538  static const unsigned char digitab[] =  
539    /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
540    
541    static const pcre_unit8 digitab[] =
542    {    {
543    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
544    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */
# Line 340  static const unsigned char digitab[] = Line 573  static const unsigned char digitab[] =
573    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */
574    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */
575    
576  static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */  static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
577    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */
578    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */
579    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */
# Line 379  static const unsigned char ebcdic_charta Line 612  static const unsigned char ebcdic_charta
612  /* Definition to allow mutual recursion */  /* Definition to allow mutual recursion */
613    
614  static BOOL  static BOOL
615    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,    compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
616      int *, int *, branch_chain *, compile_data *, int *);      int *, int *, branch_chain *, compile_data *, int *);
617    
618    
619    
620  /*************************************************  /*************************************************
621    *            Find an error text                  *
622    *************************************************/
623    
624    /* The error texts are now all in one long string, to save on relocations. As
625    some of the text is of unknown length, we can't use a table of offsets.
626    Instead, just count through the strings. This is not a performance issue
627    because it happens only when there has been a compilation error.
628    
629    Argument:   the error number
630    Returns:    pointer to the error string
631    */
632    
633    static const char *
634    find_error_text(int n)
635    {
636    const char *s = error_texts;
637    for (; n > 0; n--)
638      {
639      while (*s++ != 0) {};
640      if (*s == 0) return "Error text not found (please report)";
641      }
642    return s;
643    }
644    
645    
646    /*************************************************
647    *            Check for counted repeat            *
648    *************************************************/
649    
650    /* This function is called when a '{' is encountered in a place where it might
651    start a quantifier. It looks ahead to see if it really is a quantifier or not.
652    It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
653    where the ddds are digits.
654    
655    Arguments:
656      p         pointer to the first char after '{'
657    
658    Returns:    TRUE or FALSE
659    */
660    
661    static BOOL
662    is_counted_repeat(const pcre_uchar *p)
663    {
664    if (!IS_DIGIT(*p)) return FALSE;
665    p++;
666    while (IS_DIGIT(*p)) p++;
667    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
668    
669    if (*p++ != CHAR_COMMA) return FALSE;
670    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
671    
672    if (!IS_DIGIT(*p)) return FALSE;
673    p++;
674    while (IS_DIGIT(*p)) p++;
675    
676    return (*p == CHAR_RIGHT_CURLY_BRACKET);
677    }
678    
679    
680    
681    /*************************************************
682  *            Handle escapes                      *  *            Handle escapes                      *
683  *************************************************/  *************************************************/
684    
# Line 405  Arguments: Line 699  Arguments:
699    
700  Returns:         zero or positive => a data character  Returns:         zero or positive => a data character
701                   negative => a special escape sequence                   negative => a special escape sequence
702                   on error, errorptr is set                   on error, errorcodeptr is set
703  */  */
704    
705  static int  static int
706  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,
707    int options, BOOL isclass)    int options, BOOL isclass)
708  {  {
709  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
710  const uschar *ptr = *ptrptr + 1;  const pcre_uchar *ptr = *ptrptr + 1;
711  int c, i;  int c, i;
712    
713  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
# Line 423  ptr--;                            /* Set Line 717  ptr--;                            /* Set
717    
718  if (c == 0) *errorcodeptr = ERR1;  if (c == 0) *errorcodeptr = ERR1;
719    
720  /* Non-alphamerics are literals. For digits or letters, do an initial lookup in  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
721  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.
722  Otherwise further processing may be required. */  Otherwise further processing may be required. */
723    
724  #ifndef EBCDIC  /* ASCII coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
725  else if (c < '0' || c > 'z') {}                           /* Not alphameric */  /* Not alphanumeric */
726  else if ((i = escapes[c - '0']) != 0) c = i;  else if (c < CHAR_0 || c > CHAR_z) {}
727    else if ((i = escapes[c - CHAR_0]) != 0) c = i;
728    
729  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
730  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphameric */  /* Not alphanumeric */
731    else if (c < 'a' || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
732  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  c = i;
733  #endif  #endif
734    
# Line 440  else if ((i = escapes[c - 0x48]) != 0) Line 736  else if ((i = escapes[c - 0x48]) != 0)
736    
737  else  else
738    {    {
739    const uschar *oldptr;    const pcre_uchar *oldptr;
740    BOOL braced, negated;    BOOL braced, negated;
741    
742    switch (c)    switch (c)
# Line 448  else Line 744  else
744      /* A number of Perl escapes are not handled by PCRE. We give an explicit      /* A number of Perl escapes are not handled by PCRE. We give an explicit
745      error. */      error. */
746    
747      case 'l':      case CHAR_l:
748      case 'L':      case CHAR_L:
     case 'N':  
     case 'u':  
     case 'U':  
749      *errorcodeptr = ERR37;      *errorcodeptr = ERR37;
750      break;      break;
751    
752      /* \g must be followed by a number, either plain or braced. If positive, it      case CHAR_u:
753      is an absolute backreference. If negative, it is a relative backreference.      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
754      This is a Perl 5.10 feature. Perl 5.10 also supports \g{name} as a        {
755      reference to a named group. This is part of Perl's movement towards a        /* In JavaScript, \u must be followed by four hexadecimal numbers.
756      unified syntax for back references. As this is synonymous with \k{name}, we        Otherwise it is a lowercase u letter. */
757      fudge it up by pretending it really was \k. */        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
758            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
759      case 'g':          && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
760      if (ptr[1] == '{')          && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
761        {          {
762        const uschar *p;          c = 0;
763        for (p = ptr+2; *p != 0 && *p != '}'; p++)          for (i = 0; i < 4; ++i)
764          if (*p != '-' && (digitab[*p] & ctype_digit) == 0) break;            {
765        if (*p != 0 && *p != '}')            register int cc = *(++ptr);
766    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
767              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
768              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
769    #else           /* EBCDIC coding */
770              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
771              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
772    #endif
773              }
774            }
775          }
776        else
777          *errorcodeptr = ERR37;
778        break;
779    
780        case CHAR_U:
781        /* In JavaScript, \U is an uppercase U letter. */
782        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
783        break;
784    
785        /* In a character class, \g is just a literal "g". Outside a character
786        class, \g must be followed by one of a number of specific things:
787    
788        (1) A number, either plain or braced. If positive, it is an absolute
789        backreference. If negative, it is a relative backreference. This is a Perl
790        5.10 feature.
791    
792        (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
793        is part of Perl's movement towards a unified syntax for back references. As
794        this is synonymous with \k{name}, we fudge it up by pretending it really
795        was \k.
796    
797        (3) For Oniguruma compatibility we also support \g followed by a name or a
798        number either in angle brackets or in single quotes. However, these are
799        (possibly recursive) subroutine calls, _not_ backreferences. Just return
800        the -ESC_g code (cf \k). */
801    
802        case CHAR_g:
803        if (isclass) break;
804        if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
805          {
806          c = -ESC_g;
807          break;
808          }
809    
810        /* Handle the Perl-compatible cases */
811    
812        if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
813          {
814          const pcre_uchar *p;
815          for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
816            if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
817          if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
818          {          {
819          c = -ESC_k;          c = -ESC_k;
820          break;          break;
# Line 479  else Line 824  else
824        }        }
825      else braced = FALSE;      else braced = FALSE;
826    
827      if (ptr[1] == '-')      if (ptr[1] == CHAR_MINUS)
828        {        {
829        negated = TRUE;        negated = TRUE;
830        ptr++;        ptr++;
831        }        }
832      else negated = FALSE;      else negated = FALSE;
833    
834        /* The integer range is limited by the machine's int representation. */
835      c = 0;      c = 0;
836      while ((digitab[ptr[1]] & ctype_digit) != 0)      while (IS_DIGIT(ptr[1]))
837        c = c * 10 + *(++ptr) - '0';        {
838          if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
839            {
840            c = -1;
841            break;
842            }
843          c = c * 10 + *(++ptr) - CHAR_0;
844          }
845        if (((unsigned int)c) > INT_MAX) /* Integer overflow */
846          {
847          while (IS_DIGIT(ptr[1]))
848            ptr++;
849          *errorcodeptr = ERR61;
850          break;
851          }
852    
853      if (c == 0 || (braced && *(++ptr) != '}'))      if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET)
854        {        {
855        *errorcodeptr = ERR57;        *errorcodeptr = ERR57;
856        return 0;        break;
857          }
858    
859        if (c == 0)
860          {
861          *errorcodeptr = ERR58;
862          break;
863        }        }
864    
865      if (negated)      if (negated)
# Line 501  else Line 867  else
867        if (c > bracount)        if (c > bracount)
868          {          {
869          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
870          return 0;          break;
871          }          }
872        c = bracount - (c - 1);        c = bracount - (c - 1);
873        }        }
# Line 521  else Line 887  else
887      value is greater than 377, the least significant 8 bits are taken. Inside a      value is greater than 377, the least significant 8 bits are taken. Inside a
888      character class, \ followed by a digit is always an octal number. */      character class, \ followed by a digit is always an octal number. */
889    
890      case '1': case '2': case '3': case '4': case '5':      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
891      case '6': case '7': case '8': case '9':      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
892    
893      if (!isclass)      if (!isclass)
894        {        {
895        oldptr = ptr;        oldptr = ptr;
896        c -= '0';        /* The integer range is limited by the machine's int representation. */
897        while ((digitab[ptr[1]] & ctype_digit) != 0)        c -= CHAR_0;
898          c = c * 10 + *(++ptr) - '0';        while (IS_DIGIT(ptr[1]))
899            {
900            if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
901              {
902              c = -1;
903              break;
904              }
905            c = c * 10 + *(++ptr) - CHAR_0;
906            }
907          if (((unsigned int)c) > INT_MAX) /* Integer overflow */
908            {
909            while (IS_DIGIT(ptr[1]))
910              ptr++;
911            *errorcodeptr = ERR61;
912            break;
913            }
914        if (c < 10 || c <= bracount)        if (c < 10 || c <= bracount)
915          {          {
916          c = -(ESC_REF + c);          c = -(ESC_REF + c);
# Line 542  else Line 923  else
923      generates a binary zero byte and treats the digit as a following literal.      generates a binary zero byte and treats the digit as a following literal.
924      Thus we have to pull back the pointer by one. */      Thus we have to pull back the pointer by one. */
925    
926      if ((c = *ptr) >= '8')      if ((c = *ptr) >= CHAR_8)
927        {        {
928        ptr--;        ptr--;
929        c = 0;        c = 0;
# Line 555  else Line 936  else
936      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, but no more
937      than 3 octal digits. */      than 3 octal digits. */
938    
939      case '0':      case CHAR_0:
940      c -= '0';      c -= CHAR_0;
941      while(i++ < 2 && ptr[1] >= '0' && ptr[1] <= '7')      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
942          c = c * 8 + *(++ptr) - '0';          c = c * 8 + *(++ptr) - CHAR_0;
943      if (!utf8 && c > 255) *errorcodeptr = ERR51;      if (!utf8 && c > 0xff) *errorcodeptr = ERR51;
944      break;      break;
945    
946      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \x is complicated. \x{ddd} is a character number which can be greater
947      than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is      than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is
948      treated as a data character. */      treated as a data character. */
949    
950      case 'x':      case CHAR_x:
951      if (ptr[1] == '{')      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
952        {        {
953        const uschar *pt = ptr + 2;        /* In JavaScript, \x must be followed by two hexadecimal numbers.
954          Otherwise it is a lowercase x letter. */
955          if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
956            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
957            {
958            c = 0;
959            for (i = 0; i < 2; ++i)
960              {
961              register int cc = *(++ptr);
962    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
963              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
964              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
965    #else           /* EBCDIC coding */
966              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
967              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
968    #endif
969              }
970            }
971          break;
972          }
973    
974        if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
975          {
976          const pcre_uchar *pt = ptr + 2;
977        int count = 0;        int count = 0;
978    
979        c = 0;        c = 0;
980        while ((digitab[*pt] & ctype_xdigit) != 0)        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
981          {          {
982          register int cc = *pt++;          register int cc = *pt++;
983          if (c == 0 && cc == '0') continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
984          count++;          count++;
985    
986  #ifndef EBCDIC  /* ASCII coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
987          if (cc >= 'a') cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
988          c = (c << 4) + cc - ((cc < 'A')? '0' : ('A' - 10));          c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
989  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
990          if (cc >= 'a' && cc <= 'z') cc += 64;  /* Convert to upper case */          if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
991          c = (c << 4) + cc - ((cc >= '0')? '0' : ('A' - 10));          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
992  #endif  #endif
993          }          }
994    
995        if (*pt == '}')        if (*pt == CHAR_RIGHT_CURLY_BRACKET)
996          {          {
997          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;  #ifdef COMPILE_PCRE8
998            if (c < 0 || count > (utf8? 8:2)) *errorcodeptr = ERR34;
999    #else
1000    #ifdef COMPILE_PCRE16
1001            if (c < 0 || count > (utf8? 8:4)) *errorcodeptr = ERR34;
1002    #endif
1003    #endif
1004          ptr = pt;          ptr = pt;
1005          break;          break;
1006          }          }
# Line 602  else Line 1012  else
1012      /* Read just a single-byte hex-defined char */      /* Read just a single-byte hex-defined char */
1013    
1014      c = 0;      c = 0;
1015      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1016        {        {
1017        int cc;                               /* Some compilers don't like ++ */        int cc;                                  /* Some compilers don't like */
1018        cc = *(++ptr);                        /* in initializers */        cc = *(++ptr);                           /* ++ in initializers */
1019  #ifndef EBCDIC  /* ASCII coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1020        if (cc >= 'a') cc -= 32;              /* Convert to upper case */        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
1021        c = c * 16 + cc - ((cc < 'A')? '0' : ('A' - 10));        c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1022  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1023        if (cc <= 'z') cc += 64;              /* Convert to upper case */        if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */
1024        c = c * 16 + cc - ((cc >= '0')? '0' : ('A' - 10));        c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1025  #endif  #endif
1026        }        }
1027      break;      break;
1028    
1029      /* 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.
1030      This coding is ASCII-specific, but then the whole concept of \cx is      An error is given if the byte following \c is not an ASCII character. This
1031        coding is ASCII-specific, but then the whole concept of \cx is
1032      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1033    
1034      case 'c':      case CHAR_c:
1035      c = *(++ptr);      c = *(++ptr);
1036      if (c == 0)      if (c == 0)
1037        {        {
1038        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1039        return 0;        break;
1040        }        }
1041    #ifndef EBCDIC    /* ASCII/UTF-8 coding */
1042  #ifndef EBCDIC  /* ASCII coding */      if (c > 127)  /* Excludes all non-ASCII in either mode */
1043      if (c >= 'a' && c <= 'z') c -= 32;        {
1044          *errorcodeptr = ERR68;
1045          break;
1046          }
1047        if (c >= CHAR_a && c <= CHAR_z) c -= 32;
1048      c ^= 0x40;      c ^= 0x40;
1049  #else           /* EBCDIC coding */  #else             /* EBCDIC coding */
1050      if (c >= 'a' && c <= 'z') c += 64;      if (c >= CHAR_a && c <= CHAR_z) c += 64;
1051      c ^= 0xC0;      c ^= 0xC0;
1052  #endif  #endif
1053      break;      break;
1054    
1055      /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any      /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
1056      other alphameric following \ is an error if PCRE_EXTRA was set; otherwise,      other alphanumeric following \ is an error if PCRE_EXTRA was set;
1057      for Perl compatibility, it is a literal. This code looks a bit odd, but      otherwise, for Perl compatibility, it is a literal. This code looks a bit
1058      there used to be some cases other than the default, and there may be again      odd, but there used to be some cases other than the default, and there may
1059      in future, so I haven't "optimized" it. */      be again in future, so I haven't "optimized" it. */
1060    
1061      default:      default:
1062      if ((options & PCRE_EXTRA) != 0) switch(c)      if ((options & PCRE_EXTRA) != 0) switch(c)
# Line 654  else Line 1069  else
1069      }      }
1070    }    }
1071    
1072    /* Perl supports \N{name} for character names, as well as plain \N for "not
1073    newline". PCRE does not support \N{name}. However, it does support
1074    quantification such as \N{2,3}. */
1075    
1076    if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1077         !is_counted_repeat(ptr+2))
1078      *errorcodeptr = ERR37;
1079    
1080    /* If PCRE_UCP is set, we change the values for \d etc. */
1081    
1082    if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)
1083      c -= (ESC_DU - ESC_D);
1084    
1085    /* Set the pointer to the final character before returning. */
1086    
1087  *ptrptr = ptr;  *ptrptr = ptr;
1088  return c;  return c;
1089  }  }
# Line 680  Returns:         type value from ucp_typ Line 1110  Returns:         type value from ucp_typ
1110  */  */
1111    
1112  static int  static int
1113  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
1114  {  {
1115  int c, i, bot, top;  int c, i, bot, top;
1116  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1117  char name[32];  pcre_uchar name[32];
1118    
1119  c = *(++ptr);  c = *(++ptr);
1120  if (c == 0) goto ERROR_RETURN;  if (c == 0) goto ERROR_RETURN;
# Line 694  if (c == 0) goto ERROR_RETURN; Line 1124  if (c == 0) goto ERROR_RETURN;
1124  /* \P or \p can be followed by a name in {}, optionally preceded by ^ for  /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
1125  negation. */  negation. */
1126    
1127  if (c == '{')  if (c == CHAR_LEFT_CURLY_BRACKET)
1128    {    {
1129    if (ptr[1] == '^')    if (ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1130      {      {
1131      *negptr = TRUE;      *negptr = TRUE;
1132      ptr++;      ptr++;
1133      }      }
1134    for (i = 0; i < sizeof(name) - 1; i++)    for (i = 0; i < (int)sizeof(name) - 1; i++)
1135      {      {
1136      c = *(++ptr);      c = *(++ptr);
1137      if (c == 0) goto ERROR_RETURN;      if (c == 0) goto ERROR_RETURN;
1138      if (c == '}') break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1139      name[i] = c;      name[i] = c;
1140      }      }
1141    if (c !='}') goto ERROR_RETURN;    if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN;
1142    name[i] = 0;    name[i] = 0;
1143    }    }
1144    
# Line 725  else Line 1155  else
1155  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1156    
1157  bot = 0;  bot = 0;
1158  top = _pcre_utt_size;  top = PRIV(utt_size);
1159    
1160  while (bot < top)  while (bot < top)
1161    {    {
1162    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1163    c = strcmp(name, _pcre_utt[i].name);    c = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1164    if (c == 0)    if (c == 0)
1165      {      {
1166      *dptr = _pcre_utt[i].value;      *dptr = PRIV(utt)[i].value;
1167      return _pcre_utt[i].type;      return PRIV(utt)[i].type;
1168      }      }
1169    if (c > 0) bot = i + 1; else top = i;    if (c > 0) bot = i + 1; else top = i;
1170    }    }
# Line 754  return -1; Line 1184  return -1;
1184    
1185    
1186  /*************************************************  /*************************************************
 *            Check for counted repeat            *  
 *************************************************/  
   
 /* This function is called when a '{' is encountered in a place where it might  
 start a quantifier. It looks ahead to see if it really is a quantifier or not.  
 It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}  
 where the ddds are digits.  
   
 Arguments:  
   p         pointer to the first char after '{'  
   
 Returns:    TRUE or FALSE  
 */  
   
 static BOOL  
 is_counted_repeat(const uschar *p)  
 {  
 if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  
 while ((digitab[*p] & ctype_digit) != 0) p++;  
 if (*p == '}') return TRUE;  
   
 if (*p++ != ',') return FALSE;  
 if (*p == '}') return TRUE;  
   
 if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  
 while ((digitab[*p] & ctype_digit) != 0) p++;  
   
 return (*p == '}');  
 }  
   
   
   
 /*************************************************  
1187  *         Read repeat counts                     *  *         Read repeat counts                     *
1188  *************************************************/  *************************************************/
1189    
# Line 805  Returns:         pointer to '}' on succe Line 1202  Returns:         pointer to '}' on succe
1202                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1203  */  */
1204    
1205  static const uschar *  static const pcre_uchar *
1206  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)
1207  {  {
1208  int min = 0;  int min = 0;
1209  int max = -1;  int max = -1;
# Line 814  int max = -1; Line 1211  int max = -1;
1211  /* Read the minimum value and do a paranoid check: a negative value indicates  /* Read the minimum value and do a paranoid check: a negative value indicates
1212  an integer overflow. */  an integer overflow. */
1213    
1214  while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - '0';  while (IS_DIGIT(*p)) min = min * 10 + *p++ - CHAR_0;
1215  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1216    {    {
1217    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 824  if (min < 0 || min > 65535) Line 1221  if (min < 0 || min > 65535)
1221  /* Read the maximum value if there is one, and again do a paranoid on its size.  /* Read the maximum value if there is one, and again do a paranoid on its size.
1222  Also, max must not be less than min. */  Also, max must not be less than min. */
1223    
1224  if (*p == '}') max = min; else  if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
1225    {    {
1226    if (*(++p) != '}')    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1227      {      {
1228      max = 0;      max = 0;
1229      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - '0';      while(IS_DIGIT(*p)) max = max * 10 + *p++ - CHAR_0;
1230      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1231        {        {
1232        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 854  return p; Line 1251  return p;
1251    
1252    
1253  /*************************************************  /*************************************************
1254  *       Find forward referenced subpattern       *  *  Subroutine for finding forward reference      *
1255  *************************************************/  *************************************************/
1256    
1257  /* This function scans along a pattern's text looking for capturing  /* This recursive function is called only from find_parens() below. The
1258    top-level call starts at the beginning of the pattern. All other calls must
1259    start at a parenthesis. It scans along a pattern's text looking for capturing
1260  subpatterns, and counting them. If it finds a named pattern that matches the  subpatterns, and counting them. If it finds a named pattern that matches the
1261  name it is given, it returns its number. Alternatively, if the name is NULL, it  name it is given, it returns its number. Alternatively, if the name is NULL, it
1262  returns when it reaches a given numbered subpattern. This is used for forward  returns when it reaches a given numbered subpattern. Recursion is used to keep
1263  references to subpatterns. We know that if (?P< is encountered, the name will  track of subpatterns that reset the capturing group numbers - the (?| feature.
1264  be terminated by '>' because that is checked in the first pass.  
1265    This function was originally called only from the second pass, in which we know
1266    that if (?< or (?' or (?P< is encountered, the name will be correctly
1267    terminated because that is checked in the first pass. There is now one call to
1268    this function in the first pass, to check for a recursive back reference by
1269    name (so that we can make the whole group atomic). In this case, we need check
1270    only up to the current position in the pattern, and that is still OK because
1271    and previous occurrences will have been checked. To make this work, the test
1272    for "end of pattern" is a check against cd->end_pattern in the main loop,
1273    instead of looking for a binary zero. This means that the special first-pass
1274    call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1275    processing items within the loop are OK, because afterwards the main loop will
1276    terminate.)
1277    
1278  Arguments:  Arguments:
1279    ptr          current position in the pattern    ptrptr       address of the current character pointer (updated)
1280    count        current count of capturing parens so far encountered    cd           compile background data
1281    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1282    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1283    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1284      utf8         TRUE if we are in UTF-8 mode
1285      count        pointer to the current capturing subpattern number (updated)
1286    
1287  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
1288  */  */
1289    
1290  static int  static int
1291  find_parens(const uschar *ptr, int count, const uschar *name, int lorn,  find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,
1292    BOOL xmode)    BOOL xmode, BOOL utf8, int *count)
1293  {  {
1294  const uschar *thisname;  pcre_uchar *ptr = *ptrptr;
1295    int start_count = *count;
1296    int hwm_count = start_count;
1297    BOOL dup_parens = FALSE;
1298    
1299  for (; *ptr != 0; ptr++)  /* If the first character is a parenthesis, check on the type of group we are
1300    dealing with. The very first call may not start with a parenthesis. */
1301    
1302    if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1303    {    {
1304    int term;    /* Handle specials such as (*SKIP) or (*UTF8) etc. */
1305    
1306      if (ptr[1] == CHAR_ASTERISK) ptr += 2;
1307    
1308      /* Handle a normal, unnamed capturing parenthesis. */
1309    
1310      else if (ptr[1] != CHAR_QUESTION_MARK)
1311        {
1312        *count += 1;
1313        if (name == NULL && *count == lorn) return *count;
1314        ptr++;
1315        }
1316    
1317      /* All cases now have (? at the start. Remember when we are in a group
1318      where the parenthesis numbers are duplicated. */
1319    
1320      else if (ptr[2] == CHAR_VERTICAL_LINE)
1321        {
1322        ptr += 3;
1323        dup_parens = TRUE;
1324        }
1325    
1326      /* Handle comments; all characters are allowed until a ket is reached. */
1327    
1328      else if (ptr[2] == CHAR_NUMBER_SIGN)
1329        {
1330        for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;
1331        goto FAIL_EXIT;
1332        }
1333    
1334      /* Handle a condition. If it is an assertion, just carry on so that it
1335      is processed as normal. If not, skip to the closing parenthesis of the
1336      condition (there can't be any nested parens). */
1337    
1338      else if (ptr[2] == CHAR_LEFT_PARENTHESIS)
1339        {
1340        ptr += 2;
1341        if (ptr[1] != CHAR_QUESTION_MARK)
1342          {
1343          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
1344          if (*ptr != 0) ptr++;
1345          }
1346        }
1347    
1348      /* Start with (? but not a condition. */
1349    
1350      else
1351        {
1352        ptr += 2;
1353        if (*ptr == CHAR_P) ptr++;                      /* Allow optional P */
1354    
1355        /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */
1356    
1357        if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&
1358            ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1359          {
1360          int term;
1361          const pcre_uchar *thisname;
1362          *count += 1;
1363          if (name == NULL && *count == lorn) return *count;
1364          term = *ptr++;
1365          if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;
1366          thisname = ptr;
1367          while (*ptr != term) ptr++;
1368          if (name != NULL && lorn == ptr - thisname &&
1369              STRNCMP_UC_UC(name, thisname, lorn) == 0)
1370            return *count;
1371          term++;
1372          }
1373        }
1374      }
1375    
1376    /* Past any initial parenthesis handling, scan for parentheses or vertical
1377    bars. Stop if we get to cd->end_pattern. Note that this is important for the
1378    first-pass call when this value is temporarily adjusted to stop at the current
1379    position. So DO NOT change this to a test for binary zero. */
1380    
1381    for (; ptr < cd->end_pattern; ptr++)
1382      {
1383    /* Skip over backslashed characters and also entire \Q...\E */    /* Skip over backslashed characters and also entire \Q...\E */
1384    
1385    if (*ptr == '\\')    if (*ptr == CHAR_BACKSLASH)
1386      {      {
1387      if (*(++ptr) == 0) return -1;      if (*(++ptr) == 0) goto FAIL_EXIT;
1388      if (*ptr == 'Q') for (;;)      if (*ptr == CHAR_Q) for (;;)
1389        {        {
1390        while (*(++ptr) != 0 && *ptr != '\\');        while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
1391        if (*ptr == 0) return -1;        if (*ptr == 0) goto FAIL_EXIT;
1392        if (*(++ptr) == 'E') break;        if (*(++ptr) == CHAR_E) break;
1393        }        }
1394      continue;      continue;
1395      }      }
1396    
1397    /* Skip over character classes */    /* Skip over character classes; this logic must be similar to the way they
1398      are handled for real. If the first character is '^', skip it. Also, if the
1399      first few characters (either before or after ^) are \Q\E or \E we skip them
1400      too. This makes for compatibility with Perl. Note the use of STR macros to
1401      encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */
1402    
1403      if (*ptr == CHAR_LEFT_SQUARE_BRACKET)
1404        {
1405        BOOL negate_class = FALSE;
1406        for (;;)
1407          {
1408          if (ptr[1] == CHAR_BACKSLASH)
1409            {
1410            if (ptr[2] == CHAR_E)
1411              ptr+= 2;
1412            else if (STRNCMP_UC_C8(ptr + 2,
1413                     STR_Q STR_BACKSLASH STR_E, 3) == 0)
1414              ptr += 4;
1415            else
1416              break;
1417            }
1418          else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1419            {
1420            negate_class = TRUE;
1421            ptr++;
1422            }
1423          else break;
1424          }
1425    
1426        /* If the next character is ']', it is a data character that must be
1427        skipped, except in JavaScript compatibility mode. */
1428    
1429    if (*ptr == '[')      if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&
1430      {          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)
1431      while (*(++ptr) != ']')        ptr++;
1432    
1433        while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)
1434        {        {
1435        if (*ptr == '\\')        if (*ptr == 0) return -1;
1436          if (*ptr == CHAR_BACKSLASH)
1437          {          {
1438          if (*(++ptr) == 0) return -1;          if (*(++ptr) == 0) goto FAIL_EXIT;
1439          if (*ptr == 'Q') for (;;)          if (*ptr == CHAR_Q) for (;;)
1440            {            {
1441            while (*(++ptr) != 0 && *ptr != '\\');            while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};
1442            if (*ptr == 0) return -1;            if (*ptr == 0) goto FAIL_EXIT;
1443            if (*(++ptr) == 'E') break;            if (*(++ptr) == CHAR_E) break;
1444            }            }
1445          continue;          continue;
1446          }          }
# Line 921  for (; *ptr != 0; ptr++) Line 1450  for (; *ptr != 0; ptr++)
1450    
1451    /* Skip comments in /x mode */    /* Skip comments in /x mode */
1452    
1453    if (xmode && *ptr == '#')    if (xmode && *ptr == CHAR_NUMBER_SIGN)
1454      {      {
1455      while (*(++ptr) != 0 && *ptr != '\n');      ptr++;
1456      if (*ptr == 0) return -1;      while (*ptr != 0)
1457          {
1458          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1459          ptr++;
1460    #ifdef SUPPORT_UTF8
1461          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
1462    #endif
1463          }
1464        if (*ptr == 0) goto FAIL_EXIT;
1465      continue;      continue;
1466      }      }
1467    
1468    /* An opening parens must now be a real metacharacter */    /* Check for the special metacharacters */
1469    
1470    if (*ptr != '(') continue;    if (*ptr == CHAR_LEFT_PARENTHESIS)
   if (ptr[1] != '?')  
1471      {      {
1472      count++;      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);
1473      if (name == NULL && count == lorn) return count;      if (rc > 0) return rc;
1474      continue;      if (*ptr == 0) goto FAIL_EXIT;
1475      }      }
1476    
1477    ptr += 2;    else if (*ptr == CHAR_RIGHT_PARENTHESIS)
1478    if (*ptr == 'P') ptr++;                      /* Allow optional P */      {
1479        if (dup_parens && *count < hwm_count) *count = hwm_count;
1480        goto FAIL_EXIT;
1481        }
1482    
1483    /* We have to disambiguate (?<! and (?<= from (?<name> */    else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)
1484        {
1485        if (*count > hwm_count) hwm_count = *count;
1486        *count = start_count;
1487        }
1488      }
1489    
1490    if ((*ptr != '<' || ptr[1] == '!' || ptr[1] == '=') &&  FAIL_EXIT:
1491         *ptr != '\'')  *ptrptr = ptr;
1492      continue;  return -1;
1493    }
1494    
1495    
1496    
1497    
1498    /*************************************************
1499    *       Find forward referenced subpattern       *
1500    *************************************************/
1501    
1502    /* This function scans along a pattern's text looking for capturing
1503    subpatterns, and counting them. If it finds a named pattern that matches the
1504    name it is given, it returns its number. Alternatively, if the name is NULL, it
1505    returns when it reaches a given numbered subpattern. This is used for forward
1506    references to subpatterns. We used to be able to start this scan from the
1507    current compiling point, using the current count value from cd->bracount, and
1508    do it all in a single loop, but the addition of the possibility of duplicate
1509    subpattern numbers means that we have to scan from the very start, in order to
1510    take account of such duplicates, and to use a recursive function to keep track
1511    of the different types of group.
1512    
1513    Arguments:
1514      cd           compile background data
1515      name         name to seek, or NULL if seeking a numbered subpattern
1516      lorn         name length, or subpattern number if name is NULL
1517      xmode        TRUE if we are in /x mode
1518      utf8         TRUE if we are in UTF-8 mode
1519    
1520    count++;  Returns:       the number of the found subpattern, or -1 if not found
1521    */
1522    
1523    static int
1524    find_parens(compile_data *cd, const pcre_uchar *name, int lorn, BOOL xmode,
1525      BOOL utf8)
1526    {
1527    pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1528    int count = 0;
1529    int rc;
1530    
1531    /* If the pattern does not start with an opening parenthesis, the first call
1532    to find_parens_sub() will scan right to the end (if necessary). However, if it
1533    does start with a parenthesis, find_parens_sub() will return when it hits the
1534    matching closing parens. That is why we have to have a loop. */
1535    
1536    if (name == NULL && count == lorn) return count;  for (;;)
1537    term = *ptr++;    {
1538    if (term == '<') term = '>';    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);
1539    thisname = ptr;    if (rc > 0 || *ptr++ == 0) break;
   while (*ptr != term) ptr++;  
   if (name != NULL && lorn == ptr - thisname &&  
       strncmp((const char *)name, (const char *)thisname, lorn) == 0)  
     return count;  
1540    }    }
1541    
1542  return -1;  return rc;
1543  }  }
1544    
1545    
1546    
1547    
1548  /*************************************************  /*************************************************
1549  *      Find first significant op code            *  *      Find first significant op code            *
1550  *************************************************/  *************************************************/
1551    
1552  /* This is called by several functions that scan a compiled expression looking  /* This is called by several functions that scan a compiled expression looking
1553  for a fixed first character, or an anchoring op code etc. It skips over things  for a fixed first character, or an anchoring op code etc. It skips over things
1554  that do not influence this. For some calls, a change of option is important.  that do not influence this. For some calls, it makes sense to skip negative
1555  For some calls, it makes sense to skip negative forward and all backward  forward and all backward assertions, and also the \b assertion; for others it
1556  assertions, and also the \b assertion; for others it does not.  does not.
1557    
1558  Arguments:  Arguments:
1559    code         pointer to the start of the group    code         pointer to the start of the group
   options      pointer to external options  
   optbit       the option bit whose changing is significant, or  
                  zero if none are  
1560    skipassert   TRUE if certain assertions are to be skipped    skipassert   TRUE if certain assertions are to be skipped
1561    
1562  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1563  */  */
1564    
1565  static const uschar*  static const pcre_uchar*
1566  first_significant_code(const uschar *code, int *options, int optbit,  first_significant_code(const pcre_uchar *code, BOOL skipassert)
   BOOL skipassert)  
1567  {  {
1568  for (;;)  for (;;)
1569    {    {
1570    switch ((int)*code)    switch ((int)*code)
1571      {      {
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
1572      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1573      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1574      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1575      if (!skipassert) return code;      if (!skipassert) return code;
1576      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
1577      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1578      break;      break;
1579    
1580      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
# Line 1013  for (;;) Line 1584  for (;;)
1584    
1585      case OP_CALLOUT:      case OP_CALLOUT:
1586      case OP_CREF:      case OP_CREF:
1587        case OP_NCREF:
1588      case OP_RREF:      case OP_RREF:
1589        case OP_NRREF:
1590      case OP_DEF:      case OP_DEF:
1591      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1592      break;      break;
1593    
1594      default:      default:
# Line 1029  for (;;) Line 1602  for (;;)
1602    
1603    
1604  /*************************************************  /*************************************************
1605  *        Find the fixed length of a pattern      *  *        Find the fixed length of a branch       *
1606  *************************************************/  *************************************************/
1607    
1608  /* Scan a pattern and compute the fixed length of subject that will match it,  /* Scan a branch and compute the fixed length of subject that will match it,
1609  if the length is fixed. This is needed for dealing with backward assertions.  if the length is fixed. This is needed for dealing with backward assertions.
1610  In UTF8 mode, the result is in characters rather than bytes.  In UTF8 mode, the result is in characters rather than bytes. The branch is
1611    temporarily terminated with OP_END when this function is called.
1612    
1613    This function is called when a backward assertion is encountered, so that if it
1614    fails, the error message can point to the correct place in the pattern.
1615    However, we cannot do this when the assertion contains subroutine calls,
1616    because they can be forward references. We solve this by remembering this case
1617    and doing the check at the end; a flag specifies which mode we are running in.
1618    
1619  Arguments:  Arguments:
1620    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1621    options  the compiling options    utf8     TRUE in UTF-8 mode
1622      atend    TRUE if called when the pattern is complete
1623  Returns:   the fixed length, or -1 if there is no fixed length,    cd       the "compile data" structure
1624               or -2 if \C was encountered  
1625    Returns:   the fixed length,
1626                 or -1 if there is no fixed length,
1627                 or -2 if \C was encountered (in UTF-8 mode only)
1628                 or -3 if an OP_RECURSE item was encountered and atend is FALSE
1629                 or -4 if an unknown opcode was encountered (internal error)
1630  */  */
1631    
1632  static int  static int
1633  find_fixedlength(uschar *code, int options)  find_fixedlength(pcre_uchar *code, BOOL utf8, BOOL atend, compile_data *cd)
1634  {  {
1635  int length = -1;  int length = -1;
1636    
1637  register int branchlength = 0;  register int branchlength = 0;
1638  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1639    
1640  /* Scan along the opcodes for this branch. If we get to the end of the  /* Scan along the opcodes for this branch. If we get to the end of the
1641  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1058  branch, check the length against that of Line 1643  branch, check the length against that of
1643  for (;;)  for (;;)
1644    {    {
1645    int d;    int d;
1646      pcre_uchar *ce, *cs;
1647    register int op = *cc;    register int op = *cc;
   
1648    switch (op)    switch (op)
1649      {      {
1650        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1651        OP_BRA (normal non-capturing bracket) because the other variants of these
1652        opcodes are all concerned with unlimited repeated groups, which of course
1653        are not of fixed length. */
1654    
1655      case OP_CBRA:      case OP_CBRA:
1656      case OP_BRA:      case OP_BRA:
1657      case OP_ONCE:      case OP_ONCE:
1658        case OP_ONCE_NC:
1659      case OP_COND:      case OP_COND:
1660      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options);      d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf8, atend, cd);
1661      if (d < 0) return d;      if (d < 0) return d;
1662      branchlength += d;      branchlength += d;
1663      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1664      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1665      break;      break;
1666    
1667      /* Reached end of a branch; if it's a ket it is the end of a nested      /* Reached end of a branch; if it's a ket it is the end of a nested call.
1668      call. If it's ALT it is an alternation in a nested call. If it is      If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1669      END it's the end of the outer call. All can be handled by the same code. */      an ALT. If it is END it's the end of the outer call. All can be handled by
1670        the same code. Note that we must not include the OP_KETRxxx opcodes here,
1671        because they all imply an unlimited repeat. */
1672    
1673      case OP_ALT:      case OP_ALT:
1674      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1675      case OP_END:      case OP_END:
1676        case OP_ACCEPT:
1677        case OP_ASSERT_ACCEPT:
1678      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1679        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
1680      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
# Line 1089  for (;;) Line 1682  for (;;)
1682      branchlength = 0;      branchlength = 0;
1683      break;      break;
1684    
1685        /* A true recursion implies not fixed length, but a subroutine call may
1686        be OK. If the subroutine is a forward reference, we can't deal with
1687        it until the end of the pattern, so return -3. */
1688    
1689        case OP_RECURSE:
1690        if (!atend) return -3;
1691        cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1692        do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1693        if (cc > cs && cc < ce) return -1;                    /* Recursion */
1694        d = find_fixedlength(cs + 2, utf8, atend, cd);
1695        if (d < 0) return d;
1696        branchlength += d;
1697        cc += 1 + LINK_SIZE;
1698        break;
1699    
1700      /* Skip over assertive subpatterns */      /* Skip over assertive subpatterns */
1701    
1702      case OP_ASSERT:      case OP_ASSERT:
# Line 1100  for (;;) Line 1708  for (;;)
1708    
1709      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1710    
1711      case OP_REVERSE:      case OP_MARK:
1712        case OP_PRUNE_ARG:
1713        case OP_SKIP_ARG:
1714        case OP_THEN_ARG:
1715        cc += cc[1] + PRIV(OP_lengths)[*cc];
1716        break;
1717    
1718        case OP_CALLOUT:
1719        case OP_CIRC:
1720        case OP_CIRCM:
1721        case OP_CLOSE:
1722        case OP_COMMIT:
1723      case OP_CREF:      case OP_CREF:
     case OP_RREF:  
1724      case OP_DEF:      case OP_DEF:
1725      case OP_OPT:      case OP_DOLL:
1726      case OP_CALLOUT:      case OP_DOLLM:
     case OP_SOD:  
     case OP_SOM:  
1727      case OP_EOD:      case OP_EOD:
1728      case OP_EODN:      case OP_EODN:
1729      case OP_CIRC:      case OP_FAIL:
1730      case OP_DOLL:      case OP_NCREF:
1731        case OP_NRREF:
1732      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1733        case OP_PRUNE:
1734        case OP_REVERSE:
1735        case OP_RREF:
1736        case OP_SET_SOM:
1737        case OP_SKIP:
1738        case OP_SOD:
1739        case OP_SOM:
1740        case OP_THEN:
1741      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1742      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1743      break;      break;
1744    
1745      /* Handle literal characters */      /* Handle literal characters */
1746    
1747      case OP_CHAR:      case OP_CHAR:
1748      case OP_CHARNC:      case OP_CHARI:
1749      case OP_NOT:      case OP_NOT:
1750        case OP_NOTI:
1751      branchlength++;      branchlength++;
1752      cc += 2;      cc += 2;
1753  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1754      if ((options & PCRE_UTF8) != 0)      if (utf8 && cc[-1] >= 0xc0) cc += PRIV(utf8_table4)[cc[-1] & 0x3f];
       {  
       while ((*cc & 0xc0) == 0x80) cc++;  
       }  
1755  #endif  #endif
1756      break;      break;
1757    
# Line 1136  for (;;) Line 1759  for (;;)
1759      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
1760    
1761      case OP_EXACT:      case OP_EXACT:
1762        case OP_EXACTI:
1763        case OP_NOTEXACT:
1764        case OP_NOTEXACTI:
1765      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1766      cc += 4;      cc += 2 + IMM2_SIZE;
1767  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1768      if ((options & PCRE_UTF8) != 0)      if (utf8 && cc[-1] >= 0xc0) cc += PRIV(utf8_table4)[cc[-1] & 0x3f];
       {  
       while((*cc & 0x80) == 0x80) cc++;  
       }  
1769  #endif  #endif
1770      break;      break;
1771    
1772      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1773      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1774      cc += 4;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
1775        cc += 1 + IMM2_SIZE + 1;
1776      break;      break;
1777    
1778      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1158  for (;;) Line 1782  for (;;)
1782      cc += 2;      cc += 2;
1783      /* Fall through */      /* Fall through */
1784    
1785        case OP_HSPACE:
1786        case OP_VSPACE:
1787        case OP_NOT_HSPACE:
1788        case OP_NOT_VSPACE:
1789      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1790      case OP_DIGIT:      case OP_DIGIT:
1791      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
# Line 1165  for (;;) Line 1793  for (;;)
1793      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
1794      case OP_WORDCHAR:      case OP_WORDCHAR:
1795      case OP_ANY:      case OP_ANY:
1796        case OP_ALLANY:
1797      branchlength++;      branchlength++;
1798      cc++;      cc++;
1799      break;      break;
1800    
1801      /* The single-byte matcher isn't allowed */      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1802        otherwise \C is coded as OP_ALLANY. */
1803    
1804      case OP_ANYBYTE:      case OP_ANYBYTE:
1805      return -2;      return -2;
1806    
1807      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1808    
1809  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || defined COMPILE_PCRE16
1810      case OP_XCLASS:      case OP_XCLASS:
1811      cc += GET(cc, 1) - 33;      cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];
1812      /* Fall through */      /* Fall through */
1813  #endif  #endif
1814    
1815      case OP_CLASS:      case OP_CLASS:
1816      case OP_NCLASS:      case OP_NCLASS:
1817      cc += 33;      cc += PRIV(OP_lengths)[OP_CLASS];
1818    
1819      switch (*cc)      switch (*cc)
1820        {        {
1821          case OP_CRPLUS:
1822          case OP_CRMINPLUS:
1823        case OP_CRSTAR:        case OP_CRSTAR:
1824        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1825        case OP_CRQUERY:        case OP_CRQUERY:
# Line 1196  for (;;) Line 1828  for (;;)
1828    
1829        case OP_CRRANGE:        case OP_CRRANGE:
1830        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1831        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1832        branchlength += GET2(cc,1);        branchlength += GET2(cc,1);
1833        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1834        break;        break;
1835    
1836        default:        default:
# Line 1208  for (;;) Line 1840  for (;;)
1840    
1841      /* Anything else is variable length */      /* Anything else is variable length */
1842    
1843      default:      case OP_ANYNL:
1844        case OP_BRAMINZERO:
1845        case OP_BRAPOS:
1846        case OP_BRAPOSZERO:
1847        case OP_BRAZERO:
1848        case OP_CBRAPOS:
1849        case OP_EXTUNI:
1850        case OP_KETRMAX:
1851        case OP_KETRMIN:
1852        case OP_KETRPOS:
1853        case OP_MINPLUS:
1854        case OP_MINPLUSI:
1855        case OP_MINQUERY:
1856        case OP_MINQUERYI:
1857        case OP_MINSTAR:
1858        case OP_MINSTARI:
1859        case OP_MINUPTO:
1860        case OP_MINUPTOI:
1861        case OP_NOTMINPLUS:
1862        case OP_NOTMINPLUSI:
1863        case OP_NOTMINQUERY:
1864        case OP_NOTMINQUERYI:
1865        case OP_NOTMINSTAR:
1866        case OP_NOTMINSTARI:
1867        case OP_NOTMINUPTO:
1868        case OP_NOTMINUPTOI:
1869        case OP_NOTPLUS:
1870        case OP_NOTPLUSI:
1871        case OP_NOTPOSPLUS:
1872        case OP_NOTPOSPLUSI:
1873        case OP_NOTPOSQUERY:
1874        case OP_NOTPOSQUERYI:
1875        case OP_NOTPOSSTAR:
1876        case OP_NOTPOSSTARI:
1877        case OP_NOTPOSUPTO:
1878        case OP_NOTPOSUPTOI:
1879        case OP_NOTQUERY:
1880        case OP_NOTQUERYI:
1881        case OP_NOTSTAR:
1882        case OP_NOTSTARI:
1883        case OP_NOTUPTO:
1884        case OP_NOTUPTOI:
1885        case OP_PLUS:
1886        case OP_PLUSI:
1887        case OP_POSPLUS:
1888        case OP_POSPLUSI:
1889        case OP_POSQUERY:
1890        case OP_POSQUERYI:
1891        case OP_POSSTAR:
1892        case OP_POSSTARI:
1893        case OP_POSUPTO:
1894        case OP_POSUPTOI:
1895        case OP_QUERY:
1896        case OP_QUERYI:
1897        case OP_REF:
1898        case OP_REFI:
1899        case OP_SBRA:
1900        case OP_SBRAPOS:
1901        case OP_SCBRA:
1902        case OP_SCBRAPOS:
1903        case OP_SCOND:
1904        case OP_SKIPZERO:
1905        case OP_STAR:
1906        case OP_STARI:
1907        case OP_TYPEMINPLUS:
1908        case OP_TYPEMINQUERY:
1909        case OP_TYPEMINSTAR:
1910        case OP_TYPEMINUPTO:
1911        case OP_TYPEPLUS:
1912        case OP_TYPEPOSPLUS:
1913        case OP_TYPEPOSQUERY:
1914        case OP_TYPEPOSSTAR:
1915        case OP_TYPEPOSUPTO:
1916        case OP_TYPEQUERY:
1917        case OP_TYPESTAR:
1918        case OP_TYPEUPTO:
1919        case OP_UPTO:
1920        case OP_UPTOI:
1921      return -1;      return -1;
1922    
1923        /* Catch unrecognized opcodes so that when new ones are added they
1924        are not forgotten, as has happened in the past. */
1925    
1926        default:
1927        return -4;
1928      }      }
1929    }    }
1930  /* Control never gets here */  /* Control never gets here */
# Line 1219  for (;;) Line 1934  for (;;)
1934    
1935    
1936  /*************************************************  /*************************************************
1937  *    Scan compiled regex for numbered bracket    *  *    Scan compiled regex for specific bracket    *
1938  *************************************************/  *************************************************/
1939    
1940  /* This little function scans through a compiled pattern until it finds a  /* This little function scans through a compiled pattern until it finds a
1941  capturing bracket with the given number.  capturing bracket with the given number, or, if the number is negative, an
1942    instance of OP_REVERSE for a lookbehind. The function is global in the C sense
1943    so that it can be called from pcre_study() when finding the minimum matching
1944    length.
1945    
1946  Arguments:  Arguments:
1947    code        points to start of expression    code        points to start of expression
1948    utf8        TRUE in UTF-8 mode    utf8        TRUE in UTF-8 mode
1949    number      the required bracket number    number      the required bracket number or negative to find a lookbehind
1950    
1951  Returns:      pointer to the opcode for the bracket, or NULL if not found  Returns:      pointer to the opcode for the bracket, or NULL if not found
1952  */  */
1953    
1954  static const uschar *  const pcre_uchar *
1955  find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf8, int number)
1956  {  {
1957  for (;;)  for (;;)
1958    {    {
1959    register int c = *code;    register int c = *code;
1960    
1961    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1962    
1963    /* XCLASS is used for classes that cannot be represented just by a bit    /* XCLASS is used for classes that cannot be represented just by a bit
# Line 1247  for (;;) Line 1966  for (;;)
1966    
1967    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
1968    
1969      /* Handle recursion */
1970    
1971      else if (c == OP_REVERSE)
1972        {
1973        if (number < 0) return (pcre_uchar *)code;
1974        code += PRIV(OP_lengths)[c];
1975        }
1976    
1977    /* Handle capturing bracket */    /* Handle capturing bracket */
1978    
1979    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1980               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1981      {      {
1982      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
1983      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1984      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1985      }      }
1986    
1987      /* Otherwise, we can get the item's length from the table, except that for
1988      repeated character types, we have to test for \p and \P, which have an extra
1989      two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1990      must add in its length. */
1991    
1992      else
1993        {
1994        switch(c)
1995          {
1996          case OP_TYPESTAR:
1997          case OP_TYPEMINSTAR:
1998          case OP_TYPEPLUS:
1999          case OP_TYPEMINPLUS:
2000          case OP_TYPEQUERY:
2001          case OP_TYPEMINQUERY:
2002          case OP_TYPEPOSSTAR:
2003          case OP_TYPEPOSPLUS:
2004          case OP_TYPEPOSQUERY:
2005          if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2006          break;
2007    
2008          case OP_TYPEUPTO:
2009          case OP_TYPEMINUPTO:
2010          case OP_TYPEEXACT:
2011          case OP_TYPEPOSUPTO:
2012          if (code[1 + IMM2_SIZE] == OP_PROP
2013            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2014          break;
2015    
2016          case OP_MARK:
2017          case OP_PRUNE_ARG:
2018          case OP_SKIP_ARG:
2019          code += code[1];
2020          break;
2021    
2022          case OP_THEN_ARG:
2023          code += code[1];
2024          break;
2025          }
2026    
2027        /* Add in the fixed length from the table */
2028    
2029        code += PRIV(OP_lengths)[c];
2030    
2031    /* 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
2032    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
2033    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
2034    
   else  
     {  
     code += _pcre_OP_lengths[c];  
2035  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2036      if (utf8) switch(c)      if (utf8) switch(c)
2037        {        {
2038        case OP_CHAR:        case OP_CHAR:
2039        case OP_CHARNC:        case OP_CHARI:
2040        case OP_EXACT:        case OP_EXACT:
2041          case OP_EXACTI:
2042        case OP_UPTO:        case OP_UPTO:
2043          case OP_UPTOI:
2044        case OP_MINUPTO:        case OP_MINUPTO:
2045          case OP_MINUPTOI:
2046        case OP_POSUPTO:        case OP_POSUPTO:
2047          case OP_POSUPTOI:
2048        case OP_STAR:        case OP_STAR:
2049          case OP_STARI:
2050        case OP_MINSTAR:        case OP_MINSTAR:
2051          case OP_MINSTARI:
2052        case OP_POSSTAR:        case OP_POSSTAR:
2053          case OP_POSSTARI:
2054        case OP_PLUS:        case OP_PLUS:
2055          case OP_PLUSI:
2056        case OP_MINPLUS:        case OP_MINPLUS:
2057          case OP_MINPLUSI:
2058        case OP_POSPLUS:        case OP_POSPLUS:
2059          case OP_POSPLUSI:
2060        case OP_QUERY:        case OP_QUERY:
2061          case OP_QUERYI:
2062        case OP_MINQUERY:        case OP_MINQUERY:
2063          case OP_MINQUERYI:
2064        case OP_POSQUERY:        case OP_POSQUERY:
2065        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2066          if (code[-1] >= 0xc0) code += PRIV(utf8_table4)[code[-1] & 0x3f];
2067        break;        break;
2068        }        }
2069    #else
2070        (void)(utf8);  /* Keep compiler happy by referencing function argument */
2071  #endif  #endif
2072      }      }
2073    }    }
# Line 1305  Arguments: Line 2089  Arguments:
2089  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
2090  */  */
2091    
2092  static const uschar *  static const pcre_uchar *
2093  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf8)
2094  {  {
2095  for (;;)  for (;;)
2096    {    {
# Line 1320  for (;;) Line 2104  for (;;)
2104    
2105    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
2106    
2107    /* Otherwise, we get the item's length from the table. In UTF-8 mode, opcodes    /* Otherwise, we can get the item's length from the table, except that for
2108    that are followed by a character may be followed by a multi-byte character.    repeated character types, we have to test for \p and \P, which have an extra
2109    The length in the table is a minimum, so we have to arrange to skip the extra    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2110    bytes. */    must add in its length. */
2111    
2112    else    else
2113      {      {
2114      code += _pcre_OP_lengths[c];      switch(c)
2115          {
2116          case OP_TYPESTAR:
2117          case OP_TYPEMINSTAR:
2118          case OP_TYPEPLUS:
2119          case OP_TYPEMINPLUS:
2120          case OP_TYPEQUERY:
2121          case OP_TYPEMINQUERY:
2122          case OP_TYPEPOSSTAR:
2123          case OP_TYPEPOSPLUS:
2124          case OP_TYPEPOSQUERY:
2125          if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2126          break;
2127    
2128          case OP_TYPEPOSUPTO:
2129          case OP_TYPEUPTO:
2130          case OP_TYPEMINUPTO:
2131          case OP_TYPEEXACT:
2132          if (code[1 + IMM2_SIZE] == OP_PROP
2133            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2134          break;
2135    
2136          case OP_MARK:
2137          case OP_PRUNE_ARG:
2138          case OP_SKIP_ARG:
2139          code += code[1];
2140          break;
2141    
2142          case OP_THEN_ARG:
2143          code += code[1];
2144          break;
2145          }
2146    
2147        /* Add in the fixed length from the table */
2148    
2149        code += PRIV(OP_lengths)[c];
2150    
2151        /* In UTF-8 mode, opcodes that are followed by a character may be followed
2152        by a multi-byte character. The length in the table is a minimum, so we have
2153        to arrange to skip the extra bytes. */
2154    
2155  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2156      if (utf8) switch(c)      if (utf8) switch(c)
2157        {        {
2158        case OP_CHAR:        case OP_CHAR:
2159        case OP_CHARNC:        case OP_CHARI:
2160        case OP_EXACT:        case OP_EXACT:
2161          case OP_EXACTI:
2162        case OP_UPTO:        case OP_UPTO:
2163          case OP_UPTOI:
2164        case OP_MINUPTO:        case OP_MINUPTO:
2165          case OP_MINUPTOI:
2166        case OP_POSUPTO:        case OP_POSUPTO:
2167          case OP_POSUPTOI:
2168        case OP_STAR:        case OP_STAR:
2169          case OP_STARI:
2170        case OP_MINSTAR:        case OP_MINSTAR:
2171          case OP_MINSTARI:
2172        case OP_POSSTAR:        case OP_POSSTAR:
2173          case OP_POSSTARI:
2174        case OP_PLUS:        case OP_PLUS:
2175          case OP_PLUSI:
2176        case OP_MINPLUS:        case OP_MINPLUS:
2177          case OP_MINPLUSI:
2178        case OP_POSPLUS:        case OP_POSPLUS:
2179          case OP_POSPLUSI:
2180        case OP_QUERY:        case OP_QUERY:
2181          case OP_QUERYI:
2182        case OP_MINQUERY:        case OP_MINQUERY:
2183          case OP_MINQUERYI:
2184        case OP_POSQUERY:        case OP_POSQUERY:
2185        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2186          if (code[-1] >= 0xc0) code += PRIV(utf8_table4)[code[-1] & 0x3f];
2187        break;        break;
2188        }        }
2189    #else
2190        (void)(utf8);  /* Keep compiler happy by referencing function argument */
2191  #endif  #endif
2192      }      }
2193    }    }
# Line 1364  for (;;) Line 2203  for (;;)
2203  can match the empty string or not. It is called from could_be_empty()  can match the empty string or not. It is called from could_be_empty()
2204  below and from compile_branch() when checking for an unlimited repeat of a  below and from compile_branch() when checking for an unlimited repeat of a
2205  group that can match nothing. Note that first_significant_code() skips over  group that can match nothing. Note that first_significant_code() skips over
2206  assertions. If we hit an unclosed bracket, we return "empty" - this means we've  backward and negative forward assertions when its final argument is TRUE. If we
2207  struck an inner bracket whose current branch will already have been scanned.  hit an unclosed bracket, we return "empty" - this means we've struck an inner
2208    bracket whose current branch will already have been scanned.
2209    
2210  Arguments:  Arguments:
2211    code        points to start of search    code        points to start of search
2212    endcode     points to where to stop    endcode     points to where to stop
2213    utf8        TRUE if in UTF8 mode    utf8        TRUE if in UTF8 mode
2214      cd          contains pointers to tables etc.
2215    
2216  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2217  */  */
2218    
2219  static BOOL  static BOOL
2220  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2221      BOOL utf8, compile_data *cd)
2222  {  {
2223  register int c;  register int c;
2224  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2225       code < endcode;       code < endcode;
2226       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2227    {    {
2228    const uschar *ccode;    const pcre_uchar *ccode;
2229    
2230    c = *code;    c = *code;
2231    
2232    /* Groups with zero repeats can of course be empty; skip them. */    /* Skip over forward assertions; the other assertions are skipped by
2233      first_significant_code() with a TRUE final argument. */
2234    
2235    if (c == OP_BRAZERO || c == OP_BRAMINZERO)    if (c == OP_ASSERT)
2236      {      {
     code += _pcre_OP_lengths[c];  
2237      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
2238      c = *code;      c = *code;
2239      continue;      continue;
2240      }      }
2241    
2242    /* For other groups, scan the branches. */    /* For a recursion/subroutine call, if its end has been reached, which
2243      implies a backward reference subroutine call, we can scan it. If it's a
2244      forward reference subroutine call, we can't. To detect forward reference
2245      we have to scan up the list that is kept in the workspace. This function is
2246      called only when doing the real compile, not during the pre-compile that
2247      measures the size of the compiled pattern. */
2248    
2249    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE)    if (c == OP_RECURSE)
2250      {      {
2251        const pcre_uchar *scode;
2252      BOOL empty_branch;      BOOL empty_branch;
     if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */  
2253    
2254      /* Scan a closed bracket */      /* Test for forward reference */
2255    
2256        for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
2257          if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;
2258    
2259        /* Not a forward reference, test for completed backward reference */
2260    
2261      empty_branch = FALSE;      empty_branch = FALSE;
2262        scode = cd->start_code + GET(code, 1);
2263        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2264    
2265        /* Completed backwards reference */
2266    
2267      do      do
2268        {        {
2269        if (!empty_branch && could_be_empty_branch(code, endcode, utf8))        if (could_be_empty_branch(scode, endcode, utf8, cd))
2270            {
2271          empty_branch = TRUE;          empty_branch = TRUE;
2272            break;
2273            }
2274          scode += GET(scode, 1);
2275          }
2276        while (*scode == OP_ALT);
2277    
2278        if (!empty_branch) return FALSE;  /* All branches are non-empty */
2279        continue;
2280        }
2281    
2282      /* Groups with zero repeats can of course be empty; skip them. */
2283    
2284      if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2285          c == OP_BRAPOSZERO)
2286        {
2287        code += PRIV(OP_lengths)[c];
2288        do code += GET(code, 1); while (*code == OP_ALT);
2289        c = *code;
2290        continue;
2291        }
2292    
2293      /* A nested group that is already marked as "could be empty" can just be
2294      skipped. */
2295    
2296      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2297          c == OP_SCBRA || c == OP_SCBRAPOS)
2298        {
2299        do code += GET(code, 1); while (*code == OP_ALT);
2300        c = *code;
2301        continue;
2302        }
2303    
2304      /* For other groups, scan the branches. */
2305    
2306      if (c == OP_BRA  || c == OP_BRAPOS ||
2307          c == OP_CBRA || c == OP_CBRAPOS ||
2308          c == OP_ONCE || c == OP_ONCE_NC ||
2309          c == OP_COND)
2310        {
2311        BOOL empty_branch;
2312        if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
2313    
2314        /* If a conditional group has only one branch, there is a second, implied,
2315        empty branch, so just skip over the conditional, because it could be empty.
2316        Otherwise, scan the individual branches of the group. */
2317    
2318        if (c == OP_COND && code[GET(code, 1)] != OP_ALT)
2319        code += GET(code, 1);        code += GET(code, 1);
2320        else
2321          {
2322          empty_branch = FALSE;
2323          do
2324            {
2325            if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))
2326              empty_branch = TRUE;
2327            code += GET(code, 1);
2328            }
2329          while (*code == OP_ALT);
2330          if (!empty_branch) return FALSE;   /* All branches are non-empty */
2331        }        }
2332      while (*code == OP_ALT);  
     if (!empty_branch) return FALSE;   /* All branches are non-empty */  
2333      c = *code;      c = *code;
2334      continue;      continue;
2335      }      }
# Line 1423  for (code = first_significant_code(code Line 2338  for (code = first_significant_code(code
2338    
2339    switch (c)    switch (c)
2340      {      {
2341      /* Check for quantifiers after a class */      /* Check for quantifiers after a class. XCLASS is used for classes that
2342        cannot be represented just by a bit map. This includes negated single
2343        high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2344        actual length is stored in the compiled code, so we must update "code"
2345        here. */
2346    
2347  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2348      case OP_XCLASS:      case OP_XCLASS:
2349      ccode = code + GET(code, 1);      ccode = code += GET(code, 1);
2350      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
2351  #endif  #endif
2352    
2353      case OP_CLASS:      case OP_CLASS:
2354      case OP_NCLASS:      case OP_NCLASS:
2355      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2356    
2357  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2358      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
# Line 1471  for (code = first_significant_code(code Line 2390  for (code = first_significant_code(code
2390      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2391      case OP_WORDCHAR:      case OP_WORDCHAR:
2392      case OP_ANY:      case OP_ANY:
2393        case OP_ALLANY:
2394      case OP_ANYBYTE:      case OP_ANYBYTE:
2395      case OP_CHAR:      case OP_CHAR:
2396      case OP_CHARNC:      case OP_CHARI:
2397      case OP_NOT:      case OP_NOT:
2398        case OP_NOTI:
2399      case OP_PLUS:      case OP_PLUS:
2400      case OP_MINPLUS:      case OP_MINPLUS:
2401      case OP_POSPLUS:      case OP_POSPLUS:
# Line 1489  for (code = first_significant_code(code Line 2410  for (code = first_significant_code(code
2410      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2411      return FALSE;      return FALSE;
2412    
2413        /* These are going to continue, as they may be empty, but we have to
2414        fudge the length for the \p and \P cases. */
2415    
2416        case OP_TYPESTAR:
2417        case OP_TYPEMINSTAR:
2418        case OP_TYPEPOSSTAR:
2419        case OP_TYPEQUERY:
2420        case OP_TYPEMINQUERY:
2421        case OP_TYPEPOSQUERY:
2422        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2423        break;
2424    
2425        /* Same for these */
2426    
2427        case OP_TYPEUPTO:
2428        case OP_TYPEMINUPTO:
2429        case OP_TYPEPOSUPTO:
2430        if (code[1 + IMM2_SIZE] == OP_PROP
2431          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2432        break;
2433    
2434      /* End of branch */      /* End of branch */
2435    
2436      case OP_KET:      case OP_KET:
2437      case OP_KETRMAX:      case OP_KETRMAX:
2438      case OP_KETRMIN:      case OP_KETRMIN:
2439        case OP_KETRPOS:
2440      case OP_ALT:      case OP_ALT:
2441      return TRUE;      return TRUE;
2442    
# Line 1502  for (code = first_significant_code(code Line 2445  for (code = first_significant_code(code
2445    
2446  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2447      case OP_STAR:      case OP_STAR:
2448        case OP_STARI:
2449      case OP_MINSTAR:      case OP_MINSTAR:
2450        case OP_MINSTARI:
2451      case OP_POSSTAR:      case OP_POSSTAR:
2452        case OP_POSSTARI:
2453      case OP_QUERY:      case OP_QUERY:
2454        case OP_QUERYI:
2455      case OP_MINQUERY:      case OP_MINQUERY:
2456        case OP_MINQUERYI:
2457      case OP_POSQUERY:      case OP_POSQUERY:
2458        case OP_POSQUERYI:
2459        if (utf8 && code[1] >= 0xc0) code += PRIV(utf8_table4)[code[1] & 0x3f];
2460        break;
2461    
2462      case OP_UPTO:      case OP_UPTO:
2463        case OP_UPTOI:
2464      case OP_MINUPTO:      case OP_MINUPTO:
2465        case OP_MINUPTOI:
2466      case OP_POSUPTO:      case OP_POSUPTO:
2467      if (utf8) while ((code[2] & 0xc0) == 0x80) code++;      case OP_POSUPTOI:
2468        if (utf8 && code[1 + IMM2_SIZE] >= 0xc0) code += PRIV(utf8_table4)[code[1 + IMM2_SIZE] & 0x3f];
2469      break;      break;
2470  #endif  #endif
2471    
2472        /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2473        string. */
2474    
2475        case OP_MARK:
2476        case OP_PRUNE_ARG:
2477        case OP_SKIP_ARG:
2478        code += code[1];
2479        break;
2480    
2481        case OP_THEN_ARG:
2482        code += code[1];
2483        break;
2484    
2485        /* None of the remaining opcodes are required to match a character. */
2486    
2487        default:
2488        break;
2489      }      }
2490    }    }
2491    
# Line 1529  return TRUE; Line 2502  return TRUE;
2502  the current branch of the current pattern to see if it could match the empty  the current branch of the current pattern to see if it could match the empty
2503  string. If it could, we must look outwards for branches at other levels,  string. If it could, we must look outwards for branches at other levels,
2504  stopping when we pass beyond the bracket which is the subject of the recursion.  stopping when we pass beyond the bracket which is the subject of the recursion.
2505    This function is called only during the real compile, not during the
2506    pre-compile.
2507    
2508  Arguments:  Arguments:
2509    code        points to start of the recursion    code        points to start of the recursion
2510    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2511    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2512    utf8        TRUE if in UTF-8 mode    utf8        TRUE if in UTF-8 mode
2513      cd          pointers to tables etc
2514    
2515  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2516  */  */
2517    
2518  static BOOL  static BOOL
2519  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2520    BOOL utf8)    branch_chain *bcptr, BOOL utf8, compile_data *cd)
2521  {  {
2522  while (bcptr != NULL && bcptr->current >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2523    {    {
2524    if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))
2525        return FALSE;
2526    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2527    }    }
2528  return TRUE;  return TRUE;
# Line 1558  return TRUE; Line 2535  return TRUE;
2535  *************************************************/  *************************************************/
2536    
2537  /* This function is called when the sequence "[:" or "[." or "[=" is  /* This function is called when the sequence "[:" or "[." or "[=" is
2538  encountered in a character class. It checks whether this is followed by an  encountered in a character class. It checks whether this is followed by a
2539  optional ^ and then a sequence of letters, terminated by a matching ":]" or  sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
2540  ".]" or "=]".  reach an unescaped ']' without the special preceding character, return FALSE.
2541    
2542    Originally, this function only recognized a sequence of letters between the
2543    terminators, but it seems that Perl recognizes any sequence of characters,
2544    though of course unknown POSIX names are subsequently rejected. Perl gives an
2545    "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
2546    didn't consider this to be a POSIX class. Likewise for [:1234:].
2547    
2548    The problem in trying to be exactly like Perl is in the handling of escapes. We
2549    have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
2550    class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
2551    below handles the special case of \], but does not try to do any other escape
2552    processing. This makes it different from Perl for cases such as [:l\ower:]
2553    where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
2554    "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
2555    I think.
2556    
2557    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2558    It seems that the appearance of a nested POSIX class supersedes an apparent
2559    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2560    a digit.
2561    
2562    In Perl, unescaped square brackets may also appear as part of class names. For
2563    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2564    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2565    seem right at all. PCRE does not allow closing square brackets in POSIX class
2566    names.
2567    
2568  Argument:  Arguments:
2569    ptr      pointer to the initial [    ptr      pointer to the initial [
2570    endptr   where to return the end pointer    endptr   where to return the end pointer
   cd       pointer to compile data  
2571    
2572  Returns:   TRUE or FALSE  Returns:   TRUE or FALSE
2573  */  */
2574    
2575  static BOOL  static BOOL
2576  check_posix_syntax(const uschar *ptr, const uschar **endptr, compile_data *cd)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2577  {  {
2578  int terminator;          /* Don't combine these lines; the Solaris cc */  int terminator;          /* Don't combine these lines; the Solaris cc */
2579  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
2580  if (*(++ptr) == '^') ptr++;  for (++ptr; *ptr != 0; ptr++)
 while ((cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;  
 if (*ptr == terminator && ptr[1] == ']')  
2581    {    {
2582    *endptr = ptr;    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2583    return TRUE;      ptr++;
2584      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
2585      else
2586        {
2587        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2588          {
2589          *endptr = ptr;
2590          return TRUE;
2591          }
2592        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
2593             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
2594              ptr[1] == CHAR_EQUALS_SIGN) &&
2595            check_posix_syntax(ptr, endptr))
2596          return FALSE;
2597        }
2598    }    }
2599  return FALSE;  return FALSE;
2600  }  }
# Line 1603  Returns:     a value representing the na Line 2617  Returns:     a value representing the na
2617  */  */
2618    
2619  static int  static int
2620  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
2621  {  {
2622    const char *pn = posix_names;
2623  register int yield = 0;  register int yield = 0;
2624  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
2625    {    {
2626    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
2627      strncmp((const char *)ptr, posix_names[yield], len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;
2628      pn += posix_name_lengths[yield] + 1;
2629    yield++;    yield++;
2630    }    }
2631  return -1;  return -1;
# Line 1624  return -1; Line 2640  return -1;
2640  that is referenced. This means that groups can be replicated for fixed  that is referenced. This means that groups can be replicated for fixed
2641  repetition simply by copying (because the recursion is allowed to refer to  repetition simply by copying (because the recursion is allowed to refer to
2642  earlier groups that are outside the current group). However, when a group is  earlier groups that are outside the current group). However, when a group is
2643  optional (i.e. the minimum quantifier is zero), OP_BRAZERO is inserted before  optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
2644  it, after it has been compiled. This means that any OP_RECURSE items within it  inserted before it, after it has been compiled. This means that any OP_RECURSE
2645  that refer to the group itself or any contained groups have to have their  items within it that refer to the group itself or any contained groups have to
2646  offsets adjusted. That one of the jobs of this function. Before it is called,  have their offsets adjusted. That one of the jobs of this function. Before it
2647  the partially compiled regex must be temporarily terminated with OP_END.  is called, the partially compiled regex must be temporarily terminated with
2648    OP_END.
2649    
2650  This function has been extended with the possibility of forward references for  This function has been extended with the possibility of forward references for
2651  recursions and subroutine calls. It must also check the list of such references  recursions and subroutine calls. It must also check the list of such references
# Line 1647  Returns:     nothing Line 2664  Returns:     nothing
2664  */  */
2665    
2666  static void  static void
2667  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf8, compile_data *cd,
2668    uschar *save_hwm)    pcre_uchar *save_hwm)
2669  {  {
2670  uschar *ptr = group;  pcre_uchar *ptr = group;
2671  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  
2672    while ((ptr = (pcre_uchar *)find_recurse(ptr, utf8)) != NULL)
2673    {    {
2674    int offset;    int offset;
2675    uschar *hc;    pcre_uchar *hc;
2676    
2677    /* See if this recursion is on the forward reference list. If so, adjust the    /* See if this recursion is on the forward reference list. If so, adjust the
2678    reference. */    reference. */
# Line 1699  Arguments: Line 2717  Arguments:
2717  Returns:         new code pointer  Returns:         new code pointer
2718  */  */
2719    
2720  static uschar *  static pcre_uchar *
2721  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2722  {  {
2723  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2724  *code++ = 255;  *code++ = 255;
2725  PUT(code, 0, ptr - cd->start_pattern);  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2726  PUT(code, LINK_SIZE, 0);                /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2727  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
2728  }  }
2729    
2730    
# Line 1728  Returns:             nothing Line 2746  Returns:             nothing
2746  */  */
2747    
2748  static void  static void
2749  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2750  {  {
2751  int length = ptr - cd->start_pattern - GET(previous_callout, 2);  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2752  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
2753  }  }
2754    
# Line 1762  get_othercase_range(unsigned int *cptr, Line 2780  get_othercase_range(unsigned int *cptr,
2780  unsigned int c, othercase, next;  unsigned int c, othercase, next;
2781    
2782  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
2783    { if ((othercase = _pcre_ucp_othercase(c)) != NOTACHAR) break; }    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }
2784    
2785  if (c > d) return FALSE;  if (c > d) return FALSE;
2786    
# Line 1771  next = othercase + 1; Line 2789  next = othercase + 1;
2789    
2790  for (++c; c <= d; c++)  for (++c; c <= d; c++)
2791    {    {
2792    if (_pcre_ucp_othercase(c) != next) break;    if (UCD_OTHERCASE(c) != next) break;
2793    next++;    next++;
2794    }    }
2795    
# Line 1780  for (++c; c <= d; c++) Line 2798  for (++c; c <= d; c++)
2798    
2799  return TRUE;  return TRUE;
2800  }  }
2801    
2802    
2803    
2804    /*************************************************
2805    *        Check a character and a property        *
2806    *************************************************/
2807    
2808    /* This function is called by check_auto_possessive() when a property item
2809    is adjacent to a fixed character.
2810    
2811    Arguments:
2812      c            the character
2813      ptype        the property type
2814      pdata        the data for the type
2815      negated      TRUE if it's a negated property (\P or \p{^)
2816    
2817    Returns:       TRUE if auto-possessifying is OK
2818    */
2819    
2820    static BOOL
2821    check_char_prop(int c, int ptype, int pdata, BOOL negated)
2822    {
2823    const ucd_record *prop = GET_UCD(c);
2824    switch(ptype)
2825      {
2826      case PT_LAMP:
2827      return (prop->chartype == ucp_Lu ||
2828              prop->chartype == ucp_Ll ||
2829              prop->chartype == ucp_Lt) == negated;
2830    
2831      case PT_GC:
2832      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2833    
2834      case PT_PC:
2835      return (pdata == prop->chartype) == negated;
2836    
2837      case PT_SC:
2838      return (pdata == prop->script) == negated;
2839    
2840      /* These are specials */
2841    
2842      case PT_ALNUM:
2843      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2844              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2845    
2846      case PT_SPACE:    /* Perl space */
2847      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2848              c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2849              == negated;
2850    
2851      case PT_PXSPACE:  /* POSIX space */
2852      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2853              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2854              c == CHAR_FF || c == CHAR_CR)
2855              == negated;
2856    
2857      case PT_WORD:
2858      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2859              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2860              c == CHAR_UNDERSCORE) == negated;
2861      }
2862    return FALSE;
2863    }
2864  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
2865    
2866    
# Line 1793  whether the next thing could possibly ma Line 2874  whether the next thing could possibly ma
2874  sense to automatically possessify the repeated item.  sense to automatically possessify the repeated item.
2875    
2876  Arguments:  Arguments:
2877    op_code       the repeated op code    previous      pointer to the repeated opcode
   this          data for this item, depends on the opcode  
2878    utf8          TRUE in UTF-8 mode    utf8          TRUE in UTF-8 mode
   utf8_char     used for utf8 character bytes, NULL if not relevant  
2879    ptr           next character in pattern    ptr           next character in pattern
2880    options       options bits    options       options bits
2881    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 1805  Returns:        TRUE if possessifying is Line 2884  Returns:        TRUE if possessifying is
2884  */  */
2885    
2886  static BOOL  static BOOL
2887  check_auto_possessive(int op_code, int item, BOOL utf8, uschar *utf8_char,  check_auto_possessive(const pcre_uchar *previous, BOOL utf8,
2888    const uschar *ptr, int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
2889  {  {
2890  int next;  int c, next;
2891    int op_code = *previous++;
2892    
2893  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
2894    
# Line 1817  if ((options & PCRE_EXTENDED) != 0) Line 2897  if ((options & PCRE_EXTENDED) != 0)
2897    for (;;)    for (;;)
2898      {      {
2899      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2900      if (*ptr == '#')      if (*ptr == CHAR_NUMBER_SIGN)
2901        {        {
2902        while (*(++ptr) != 0)        ptr++;
2903          while (*ptr != 0)
2904            {
2905          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2906            ptr++;
2907    #ifdef SUPPORT_UTF8
2908            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2909    #endif
2910            }
2911        }        }
2912      else break;      else break;
2913      }      }
# Line 1829  if ((options & PCRE_EXTENDED) != 0) Line 2916  if ((options & PCRE_EXTENDED) != 0)
2916  /* If the next item is one that we can handle, get its value. A non-negative  /* If the next item is one that we can handle, get its value. A non-negative
2917  value is a character, a negative value is an escape value. */  value is a character, a negative value is an escape value. */
2918    
2919  if (*ptr == '\\')  if (*ptr == CHAR_BACKSLASH)
2920    {    {
2921    int temperrorcode = 0;    int temperrorcode = 0;
2922    next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);    next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);
# Line 1854  if ((options & PCRE_EXTENDED) != 0) Line 2941  if ((options & PCRE_EXTENDED) != 0)
2941    for (;;)    for (;;)
2942      {      {
2943      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2944      if (*ptr == '#')      if (*ptr == CHAR_NUMBER_SIGN)
2945        {        {
2946        while (*(++ptr) != 0)        ptr++;
2947          while (*ptr != 0)
2948            {
2949          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2950            ptr++;
2951    #ifdef SUPPORT_UTF8
2952            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2953    #endif
2954            }
2955        }        }
2956      else break;      else break;
2957      }      }
# Line 1865  if ((options & PCRE_EXTENDED) != 0) Line 2959  if ((options & PCRE_EXTENDED) != 0)
2959    
2960  /* If the next thing is itself optional, we have to give up. */  /* If the next thing is itself optional, we have to give up. */
2961    
2962  if (*ptr == '*' || *ptr == '?' || strncmp((char *)ptr, "{0,", 3) == 0)  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
2963    return FALSE;    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
2964        return FALSE;
 /* Now compare the next item with the previous opcode. If the previous is a  
 positive single character match, "item" either contains the character or, if  
 "item" is greater than 127 in utf8 mode, the character's bytes are in  
 utf8_char. */  
   
2965    
2966  /* Handle cases when the next item is a character. */  /* Now compare the next item with the previous opcode. First, handle cases when
2967    the next item is a character. */
2968    
2969  if (next >= 0) switch(op_code)  if (next >= 0) switch(op_code)
2970    {    {
2971    case OP_CHAR:    case OP_CHAR:
2972  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2973    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
2974    #else
2975      c = *previous;
2976  #endif  #endif
2977    return item != next;    return c != next;
2978    
2979    /* For CHARNC (caseless character) we must check the other case. If we have    /* For CHARI (caseless character) we must check the other case. If we have
2980    Unicode property support, we can use it to test the other case of    Unicode property support, we can use it to test the other case of
2981    high-valued characters. */    high-valued characters. */
2982    
2983    case OP_CHARNC:    case OP_CHARI:
2984  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2985    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
2986    #else
2987      c = *previous;
2988  #endif  #endif
2989    if (item == next) return FALSE;    if (c == next) return FALSE;
2990  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2991    if (utf8)    if (utf8)
2992      {      {
2993      unsigned int othercase;      unsigned int othercase;
2994      if (next < 128) othercase = cd->fcc[next]; else      if (next < 128) othercase = cd->fcc[next]; else
2995  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2996      othercase = _pcre_ucp_othercase((unsigned int)next);      othercase = UCD_OTHERCASE((unsigned int)next);
2997  #else  #else
2998      othercase = NOTACHAR;      othercase = NOTACHAR;
2999  #endif  #endif
3000      return (unsigned int)item != othercase;      return (unsigned int)c != othercase;
3001      }      }
3002    else    else
3003  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
3004    return (item != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */
3005    
3006    /* For OP_NOT, "item" must be a single-byte character. */    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These
3007      opcodes are not used for multi-byte characters, because they are coded using
3008      an XCLASS instead. */
3009    
3010    case OP_NOT:    case OP_NOT:
3011    if (next < 0) return FALSE;  /* Not a character */    return (c = *previous) == next;
3012    if (item == next) return TRUE;  
3013    if ((options & PCRE_CASELESS) == 0) return FALSE;    case OP_NOTI:
3014      if ((c = *previous) == next) return TRUE;
3015  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3016    if (utf8)    if (utf8)
3017      {      {
3018      unsigned int othercase;      unsigned int othercase;
3019      if (next < 128) othercase = cd->fcc[next]; else      if (next < 128) othercase = cd->fcc[next]; else
3020  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3021      othercase = _pcre_ucp_othercase(next);      othercase = UCD_OTHERCASE(next);
3022  #else  #else
3023      othercase = NOTACHAR;      othercase = NOTACHAR;
3024  #endif  #endif
3025      return (unsigned int)item == othercase;      return (unsigned int)c == othercase;
3026      }      }
3027    else    else
3028  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
3029    return (item == cd->fcc[next]);  /* Non-UTF-8 mode */    return (c == cd->fcc[next]);  /* Non-UTF-8 mode */
3030    
3031      /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3032      When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3033    
3034    case OP_DIGIT:    case OP_DIGIT:
3035    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;
# Line 1972  if (next >= 0) switch(op_code) Line 3072  if (next >= 0) switch(op_code)
3072      case 0x202f:      case 0x202f:
3073      case 0x205f:      case 0x205f:
3074      case 0x3000:      case 0x3000:
3075      return op_code != OP_HSPACE;      return op_code == OP_NOT_HSPACE;
3076      default:      default:
3077      return op_code == OP_HSPACE;      return op_code != OP_NOT_HSPACE;
3078      }      }
3079    
3080      case OP_ANYNL:
3081    case OP_VSPACE:    case OP_VSPACE:
3082    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3083    switch(next)    switch(next)
# Line 1988  if (next >= 0) switch(op_code) Line 3089  if (next >= 0) switch(op_code)
3089      case 0x85:      case 0x85:
3090      case 0x2028:      case 0x2028:
3091      case 0x2029:      case 0x2029:
3092      return op_code != OP_VSPACE;      return op_code == OP_NOT_VSPACE;
3093      default:      default:
3094      return op_code == OP_VSPACE;      return op_code != OP_NOT_VSPACE;
3095      }      }
3096    
3097    #ifdef SUPPORT_UCP
3098      case OP_PROP:
3099      return check_char_prop(next, previous[0], previous[1], FALSE);
3100    
3101      case OP_NOTPROP:
3102      return check_char_prop(next, previous[0], previous[1], TRUE);
3103    #endif
3104    
3105    default:    default:
3106    return FALSE;    return FALSE;
3107    }    }
3108    
3109    
3110  /* Handle the case when the next item is \d, \s, etc. */  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3111    is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3112    generated only when PCRE_UCP is *not* set, that is, when only ASCII
3113    characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are
3114    replaced by OP_PROP codes when PCRE_UCP is set. */
3115    
3116  switch(op_code)  switch(op_code)
3117    {    {
3118    case OP_CHAR:    case OP_CHAR:
3119    case OP_CHARNC:    case OP_CHARI:
3120  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3121    if (utf8 && item > 127) { GETCHAR(item, utf8_char); }    GETCHARTEST(c, previous);
3122    #else
3123      c = *previous;
3124  #endif  #endif
3125    switch(-next)    switch(-next)
3126      {      {
3127      case ESC_d:      case ESC_d:
3128      return item > 127 || (cd->ctypes[item] & ctype_digit) == 0;      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;
3129    
3130      case ESC_D:      case ESC_D:
3131      return item <= 127 && (cd->ctypes[item] & ctype_digit) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;
3132    
3133      case ESC_s:      case ESC_s:
3134      return item > 127 || (cd->ctypes[item] & ctype_space) == 0;      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;
3135    
3136      case ESC_S:      case ESC_S:
3137      return item <= 127 && (cd->ctypes[item] & ctype_space) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;
3138    
3139      case ESC_w:      case ESC_w:
3140      return item > 127 || (cd->ctypes[item] & ctype_word) == 0;      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;
3141    
3142      case ESC_W:      case ESC_W:
3143      return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;
3144    
3145      case ESC_h:      case ESC_h:
3146      case ESC_H:      case ESC_H:
3147      switch(item)      switch(c)
3148        {        {
3149        case 0x09:        case 0x09:
3150        case 0x20:        case 0x20:
# Line 2053  switch(op_code) Line 3168  switch(op_code)
3168        return -next != ESC_h;        return -next != ESC_h;
3169        default:        default:
3170        return -next == ESC_h;        return -next == ESC_h;
3171        }        }
3172    
3173      case ESC_v:      case ESC_v:
3174      case ESC_V:      case ESC_V:
3175      switch(item)      switch(c)
3176        {        {
3177        case 0x0a:        case 0x0a:
3178        case 0x0b:        case 0x0b:
# Line 2069  switch(op_code) Line 3184  switch(op_code)
3184        return -next != ESC_v;        return -next != ESC_v;
3185        default:        default:
3186        return -next == ESC_v;        return -next == ESC_v;
3187        }        }
3188    
3189        /* When PCRE_UCP is set, these values get generated for \d etc. Find
3190        their substitutions and process them. The result will always be either
3191        -ESC_p or -ESC_P. Then fall through to process those values. */
3192    
3193    #ifdef SUPPORT_UCP
3194        case ESC_du:
3195        case ESC_DU:
3196        case ESC_wu:
3197        case ESC_WU:
3198        case ESC_su:
3199        case ESC_SU:
3200          {
3201          int temperrorcode = 0;
3202          ptr = substitutes[-next - ESC_DU];
3203          next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);
3204          if (temperrorcode != 0) return FALSE;
3205          ptr++;    /* For compatibility */
3206          }
3207        /* Fall through */
3208    
3209        case ESC_p:
3210        case ESC_P:
3211          {
3212          int ptype, pdata, errorcodeptr;
3213          BOOL negated;
3214    
3215          ptr--;      /* Make ptr point at the p or P */
3216          ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);
3217          if (ptype < 0) return FALSE;
3218          ptr++;      /* Point past the final curly ket */
3219    
3220          /* If the property item is optional, we have to give up. (When generated
3221          from \d etc by PCRE_UCP, this test will have been applied much earlier,
3222          to the original \d etc. At this point, ptr will point to a zero byte. */
3223    
3224          if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3225            STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3226              return FALSE;
3227    
3228          /* Do the property check. */
3229    
3230          return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);
3231          }
3232    #endif
3233    
3234      default:      default:
3235      return FALSE;      return FALSE;
3236      }      }
3237    
3238      /* In principle, support for Unicode properties should be integrated here as
3239      well. It means re-organizing the above code so as to get hold of the property
3240      values before switching on the op-code. However, I wonder how many patterns
3241      combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,
3242      these op-codes are never generated.) */
3243    
3244    case OP_DIGIT:    case OP_DIGIT:
3245    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||
3246           next == -ESC_h || next == -ESC_v;           next == -ESC_h || next == -ESC_v || next == -ESC_R;
3247    
3248    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
3249    return next == -ESC_d;    return next == -ESC_d;
3250    
3251    case OP_WHITESPACE:    case OP_WHITESPACE:
3252    return next == -ESC_S || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;
3253    
3254    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3255    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_s || next == -ESC_h || next == -ESC_v;
3256    
3257    case OP_HSPACE:    case OP_HSPACE:
3258    return next == -ESC_S || next == -ESC_H || next == -ESC_d || next == -ESC_w;    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||
3259             next == -ESC_w || next == -ESC_v || next == -ESC_R;
3260    
3261    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
3262    return next == -ESC_h;    return next == -ESC_h;
3263    
3264    /* Can't have \S in here because VT matches \S (Perl anomaly) */    /* Can't have \S in here because VT matches \S (Perl anomaly) */
3265    case OP_VSPACE:    case OP_ANYNL:
3266      case OP_VSPACE:
3267    return next == -ESC_V || next == -ESC_d || next == -ESC_w;    return next == -ESC_V || next == -ESC_d || next == -ESC_w;
3268    
3269    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3270    return next == -ESC_v;    return next == -ESC_v || next == -ESC_R;
3271    
3272    case OP_WORDCHAR:    case OP_WORDCHAR:
3273    return next == -ESC_W || next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_W || next == -ESC_s || next == -ESC_h ||
3274             next == -ESC_v || next == -ESC_R;
3275    
3276    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
3277    return next == -ESC_w || next == -ESC_d;    return next == -ESC_w || next == -ESC_d;
3278    
3279    default:    default:
3280    return FALSE;    return FALSE;
3281    }    }
# Line 2131  Arguments: Line 3300  Arguments:
3300    codeptr        points to the pointer to the current code point    codeptr        points to the pointer to the current code point
3301    ptrptr         points to the current pattern pointer    ptrptr         points to the current pattern pointer
3302    errorcodeptr   points to error code variable    errorcodeptr   points to error code variable
3303    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3304    reqbyteptr     set to the last literal character required, else < 0    reqcharptr     set to the last literal character required, else < 0
3305    bcptr          points to current branch chain    bcptr          points to current branch chain
3306      cond_depth     conditional nesting depth
3307    cd             contains pointers to tables etc.    cd             contains pointers to tables etc.
3308    lengthptr      NULL during the real compile phase    lengthptr      NULL during the real compile phase
3309                   points to length accumulator during pre-compile phase                   points to length accumulator during pre-compile phase
# Line 2143  Returns:         TRUE on success Line 3313  Returns:         TRUE on success
3313  */  */
3314    
3315  static BOOL  static BOOL
3316  compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3317    int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,
3318      pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,
3319    compile_data *cd, int *lengthptr)    compile_data *cd, int *lengthptr)
3320  {  {
3321  int repeat_type, op_type;  int repeat_type, op_type;
3322  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3323  int bravalue = 0;  int bravalue = 0;
3324  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3325  int firstbyte, reqbyte;  pcre_int32 firstchar, reqchar;
3326  int zeroreqbyte, zerofirstbyte;  pcre_int32 zeroreqchar, zerofirstchar;
3327  int req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
3328  int options = *optionsptr;  int options = *optionsptr;               /* May change dynamically */
3329  int after_manual_callout = 0;  int after_manual_callout = 0;
3330  int length_prevgroup = 0;  int length_prevgroup = 0;
3331  register int c;  register int c;
3332  register uschar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3333  uschar *last_code = code;  pcre_uchar *last_code = code;
3334  uschar *orig_code = code;  pcre_uchar *orig_code = code;
3335  uschar *tempcode;  pcre_uchar *tempcode;
3336  BOOL inescq = FALSE;  BOOL inescq = FALSE;
3337  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstchar = FALSE;
3338  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
3339  const uschar *tempptr;  const pcre_uchar *tempptr;
3340  uschar *previous = NULL;  const pcre_uchar *nestptr = NULL;
3341  uschar *previous_callout = NULL;  pcre_uchar *previous = NULL;
3342  uschar *save_hwm = NULL;  pcre_uchar *previous_callout = NULL;
3343  uschar classbits[32];  pcre_uchar *save_hwm = NULL;
3344    pcre_uint8 classbits[32];
3345    
3346    /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3347    must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3348    dynamically as we process the pattern. */
3349    
3350  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
 BOOL class_utf8;  
3351  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
3352  uschar *class_utf8data;  pcre_uint8 utf8_char[6];
 uschar utf8_char[6];  
3353  #else  #else
3354  BOOL utf8 = FALSE;  BOOL utf8 = FALSE;
 uschar *utf8_char = NULL;  
3355  #endif  #endif
3356    
3357  #ifdef DEBUG  /* Helper variables for OP_XCLASS opcode (for characters > 255). */
3358    
3359    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3360    BOOL xclass;
3361    pcre_uchar *class_uchardata;
3362    pcre_uchar *class_uchardata_base;
3363    #endif
3364    
3365    #ifdef PCRE_DEBUG
3366  if (lengthptr != NULL) DPRINTF((">> start branch\n"));  if (lengthptr != NULL) DPRINTF((">> start branch\n"));
3367  #endif  #endif
3368    
# Line 2192  greedy_non_default = greedy_default ^ 1; Line 3373  greedy_non_default = greedy_default ^ 1;
3373    
3374  /* Initialize no first byte, no required byte. REQ_UNSET means "no char  /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3375  matching encountered yet". It gets changed to REQ_NONE if we hit something that  matching encountered yet". It gets changed to REQ_NONE if we hit something that
3376  matches a non-fixed char first char; reqbyte just remains unset if we never  matches a non-fixed char first char; reqchar just remains unset if we never
3377  find one.  find one.
3378    
3379  When we hit a repeat whose minimum is zero, we may have to adjust these values  When we hit a repeat whose minimum is zero, we may have to adjust these values
3380  to take the zero repeat into account. This is implemented by setting them to  to take the zero repeat into account. This is implemented by setting them to
3381  zerofirstbyte and zeroreqbyte when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3382  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
3383    
3384  firstbyte = reqbyte = zerofirstbyte = zeroreqbyte = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;
3385    
3386  /* The variable req_caseopt contains either the REQ_CASELESS value or zero,  /* The variable req_caseopt contains either the REQ_CASELESS value
3387  according to the current setting of the caseless flag. REQ_CASELESS is a bit  or zero, according to the current setting of the caseless flag. The
3388  value > 255. It is added into the firstbyte or reqbyte variables to record the  REQ_CASELESS leaves the lower 28 bit empty. It is added into the
3389  case status of the value. This is used only for ASCII characters. */  firstchar or reqchar variables to record the case status of the
3390    value. This is used only for ASCII characters. */
3391    
3392  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
3393    
3394  /* Switch on next character until the end of the branch */  /* Switch on next character until the end of the branch */
3395    
3396  for (;; ptr++)  for (;; ptr++)
3397    {    {
3398    BOOL negate_class;    BOOL negate_class;
3399      BOOL should_flip_negation;
3400    BOOL possessive_quantifier;    BOOL possessive_quantifier;
3401    BOOL is_quantifier;    BOOL is_quantifier;
3402    BOOL is_recurse;    BOOL is_recurse;
# Line 2224  for (;; ptr++) Line 3407  for (;; ptr++)
3407    int recno;    int recno;
3408    int refsign;    int refsign;
3409    int skipbytes;    int skipbytes;
3410    int subreqbyte;    int subreqchar;
3411    int subfirstbyte;    int subfirstchar;
3412    int terminator;    int terminator;
3413    int mclength;    int mclength;
3414    uschar mcbuffer[8];    int tempbracount;
3415      pcre_uchar mcbuffer[8];
3416    
3417    /* Get next byte in the pattern */    /* Get next byte in the pattern */
3418    
3419    c = *ptr;    c = *ptr;
3420    
3421      /* If we are at the end of a nested substitution, revert to the outer level
3422      string. Nesting only happens one level deep. */
3423    
3424      if (c == 0 && nestptr != NULL)
3425        {
3426        ptr = nestptr;
3427        nestptr = NULL;
3428        c = *ptr;
3429        }
3430    
3431    /* If we are in the pre-compile phase, accumulate the length used for the    /* If we are in the pre-compile phase, accumulate the length used for the
3432    previous cycle of this loop. */    previous cycle of this loop. */
3433    
3434    if (lengthptr != NULL)    if (lengthptr != NULL)
3435      {      {
3436  #ifdef DEBUG  #ifdef PCRE_DEBUG
3437      if (code > cd->hwm) cd->hwm = code;                 /* High water info */      if (code > cd->hwm) cd->hwm = code;                 /* High water info */
3438  #endif  #endif
3439      if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */      if (code > cd->start_workspace + WORK_SIZE_CHECK)   /* Check for overrun */
3440        {        {
3441        *errorcodeptr = ERR52;        *errorcodeptr = ERR52;
3442        goto FAILED;        goto FAILED;
# Line 2255  for (;; ptr++) Line 3449  for (;; ptr++)
3449      */      */
3450    
3451      if (code < last_code) code = last_code;      if (code < last_code) code = last_code;
3452      *lengthptr += code - last_code;  
3453      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, code - last_code, c));      /* Paranoid check for integer overflow */
3454    
3455        if (OFLOW_MAX - *lengthptr < code - last_code)
3456          {
3457          *errorcodeptr = ERR20;
3458          goto FAILED;
3459          }
3460    
3461        *lengthptr += (int)(code - last_code);
3462        DPRINTF(("length=%d added %d c=%c\n", *lengthptr, (int)(code - last_code),
3463          c));
3464    
3465      /* If "previous" is set and it is not at the start of the work space, move      /* If "previous" is set and it is not at the start of the work space, move
3466      it back to there, in order to avoid filling up the work space. Otherwise,      it back to there, in order to avoid filling up the work space. Otherwise,
# Line 2266  for (;; ptr++) Line 3470  for (;; ptr++)
3470        {        {
3471        if (previous > orig_code)        if (previous > orig_code)
3472          {          {
3473          memmove(orig_code, previous, code - previous);          memmove(orig_code, previous, IN_UCHARS(code - previous));
3474          code -= previous - orig_code;          code -= previous - orig_code;
3475          previous = orig_code;          previous = orig_code;
3476          }          }
# Line 2282  for (;; ptr++) Line 3486  for (;; ptr++)
3486    /* In the real compile phase, just check the workspace used by the forward    /* In the real compile phase, just check the workspace used by the forward
3487    reference list. */    reference list. */
3488    
3489    else if (cd->hwm > cd->start_workspace + COMPILE_WORK_SIZE)    else if (cd->hwm > cd->start_workspace + WORK_SIZE_CHECK)
3490      {      {
3491      *errorcodeptr = ERR52;      *errorcodeptr = ERR52;
3492      goto FAILED;      goto FAILED;
# Line 2292  for (;; ptr++) Line 3496  for (;; ptr++)
3496    
3497    if (inescq && c != 0)    if (inescq && c != 0)
3498      {      {
3499      if (c == '\\' && ptr[1] == 'E')      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
3500        {        {
3501        inescq = FALSE;        inescq = FALSE;
3502        ptr++;        ptr++;
# Line 2318  for (;; ptr++) Line 3522  for (;; ptr++)
3522    /* Fill in length of a previous callout, except when the next thing is    /* Fill in length of a previous callout, except when the next thing is
3523    a quantifier. */    a quantifier. */
3524    
3525    is_quantifier = c == '*' || c == '+' || c == '?' ||    is_quantifier =
3526      (c == '{' && is_counted_repeat(ptr+1));      c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
3527        (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
3528    
3529    if (!is_quantifier && previous_callout != NULL &&    if (!is_quantifier && previous_callout != NULL &&
3530         after_manual_callout-- <= 0)         after_manual_callout-- <= 0)
# Line 2329  for (;; ptr++) Line 3534  for (;; ptr++)
3534      previous_callout = NULL;      previous_callout = NULL;
3535      }      }
3536    
3537    /* In extended mode, skip white space and comments */    /* In extended mode, skip white space and comments. */
3538    
3539    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
3540      {      {
3541      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if ((cd->ctypes[c] & ctype_space) != 0) continue;
3542      if (c == '#')      if (c == CHAR_NUMBER_SIGN)
3543        {        {
3544        while (*(++ptr) != 0)        ptr++;
3545          while (*ptr != 0)
3546          {          {
3547          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3548            ptr++;
3549    #ifdef SUPPORT_UTF8
3550            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
3551    #endif
3552          }          }
3553        if (*ptr != 0) continue;        if (*ptr != 0) continue;
3554    
# Line 2359  for (;; ptr++) Line 3569  for (;; ptr++)
3569      {      {
3570      /* ===================================================================*/      /* ===================================================================*/
3571      case 0:                        /* The branch terminates at string end */      case 0:                        /* The branch terminates at string end */
3572      case '|':                      /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
3573      case ')':      case CHAR_RIGHT_PARENTHESIS:
3574      *firstbyteptr = firstbyte;      *firstcharptr = firstchar;
3575      *reqbyteptr = reqbyte;      *reqcharptr = reqchar;
3576      *codeptr = code;      *codeptr = code;
3577      *ptrptr = ptr;      *ptrptr = ptr;
3578      if (lengthptr != NULL)      if (lengthptr != NULL)
3579        {        {
3580        *lengthptr += code - last_code;   /* To include callout length */        if (OFLOW_MAX - *lengthptr < code - last_code)
3581            {
3582            *errorcodeptr = ERR20;
3583            goto FAILED;
3584            }
3585          *lengthptr += (int)(code - last_code);   /* To include callout length */
3586        DPRINTF((">> end branch\n"));        DPRINTF((">> end branch\n"));
3587        }        }
3588      return TRUE;      return TRUE;
# Line 2377  for (;; ptr++) Line 3592  for (;; ptr++)
3592      /* Handle single-character metacharacters. In multiline mode, ^ disables      /* Handle single-character metacharacters. In multiline mode, ^ disables
3593      the setting of any following char as a first character. */      the setting of any following char as a first character. */
3594    
3595      case '^':      case CHAR_CIRCUMFLEX_ACCENT:
3596        previous = NULL;
3597      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3598        {        {
3599        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3600          *code++ = OP_CIRCM;
3601        }        }
3602      previous = NULL;      else *code++ = OP_CIRC;
     *code++ = OP_CIRC;  
3603      break;      break;
3604    
3605      case '$':      case CHAR_DOLLAR_SIGN:
3606      previous = NULL;      previous = NULL;
3607      *code++ = OP_DOLL;      *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
3608      break;      break;
3609    
3610      /* There can never be a first char if '.' is first, whatever happens about      /* There can never be a first char if '.' is first, whatever happens about
3611      repeats. The value of reqbyte doesn't change either. */      repeats. The value of reqchar doesn't change either. */
3612    
3613      case '.':      case CHAR_DOT:
3614      if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3615      zerofirstbyte = firstbyte;      zerofirstchar = firstchar;
3616      zeroreqbyte = reqbyte;      zeroreqchar = reqchar;
3617      previous = code;      previous = code;
3618      *code++ = OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
3619      break;      break;
3620    
3621    
# Line 2414  for (;; ptr++) Line 3630  for (;; ptr++)
3630      opcode is compiled. It may optionally have a bit map for characters < 256,      opcode is compiled. It may optionally have a bit map for characters < 256,
3631      but those above are are explicitly listed afterwards. A flag byte tells      but those above are are explicitly listed afterwards. A flag byte tells
3632      whether the bitmap is present, and whether this is a negated class or not.      whether the bitmap is present, and whether this is a negated class or not.
     */  
3633    
3634      case '[':      In JavaScript compatibility mode, an isolated ']' causes an error. In
3635        default (Perl) mode, it is treated as a data character. */
3636    
3637        case CHAR_RIGHT_SQUARE_BRACKET:
3638        if ((cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
3639          {
3640          *errorcodeptr = ERR64;
3641          goto FAILED;
3642          }
3643        goto NORMAL_CHAR;
3644    
3645        case CHAR_LEFT_SQUARE_BRACKET:
3646      previous = code;      previous = code;
3647    
3648      /* PCRE supports POSIX class stuff inside a class. Perl gives an error if      /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
3649      they are encountered at the top level, so we'll do that too. */      they are encountered at the top level, so we'll do that too. */
3650    
3651      if ((ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&      if ((ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3652          check_posix_syntax(ptr, &tempptr, cd))           ptr[1] == CHAR_EQUALS_SIGN) &&
3653            check_posix_syntax(ptr, &tempptr))
3654        {        {
3655        *errorcodeptr = (ptr[1] == ':')? ERR13 : ERR31;        *errorcodeptr = (ptr[1] == CHAR_COLON)? ERR13 : ERR31;
3656        goto FAILED;        goto FAILED;
3657        }        }
3658    
3659      /* If the first character is '^', set the negation flag and skip it. */      /* If the first character is '^', set the negation flag and skip it. Also,
3660        if the first few characters (either before or after ^) are \Q\E or \E we
3661        skip them too. This makes for compatibility with Perl. */
3662    
3663      if ((c = *(++ptr)) == '^')      negate_class = FALSE;
3664        for (;;)
3665        {        {
       negate_class = TRUE;  
3666        c = *(++ptr);        c = *(++ptr);
3667          if (c == CHAR_BACKSLASH)
3668            {
3669            if (ptr[1] == CHAR_E)
3670              ptr++;
3671            else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0)
3672              ptr += 3;
3673            else
3674              break;
3675            }
3676          else if (!negate_class && c == CHAR_CIRCUMFLEX_ACCENT)
3677            negate_class = TRUE;
3678          else break;
3679          }
3680    
3681        /* Empty classes are allowed in JavaScript compatibility mode. Otherwise,
3682        an initial ']' is taken as a data character -- the code below handles
3683        that. In JS mode, [] must always fail, so generate OP_FAIL, whereas
3684        [^] must match any character, so generate OP_ALLANY. */
3685    
3686        if (c == CHAR_RIGHT_SQUARE_BRACKET &&
3687            (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
3688          {
3689          *code++ = negate_class? OP_ALLANY : OP_FAIL;
3690          if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3691          zerofirstchar = firstchar;
3692          break;
3693        }        }
3694      else  
3695        {      /* If a class contains a negative special such as \S, we need to flip the
3696        negate_class = FALSE;      negation flag at the end, so that support for characters > 255 works
3697        }      correctly (they are all included in the class). */
3698    
3699        should_flip_negation = FALSE;
3700    
3701      /* Keep a count of chars with values < 256 so that we can optimize the case      /* Keep a count of chars with values < 256 so that we can optimize the case
3702      of just a single character (as long as it's < 256). However, For higher      of just a single character (as long as it's < 256). However, For higher
# Line 2453  for (;; ptr++) Line 3710  for (;; ptr++)
3710      than 256), because in that case the compiled code doesn't use the bit map.      than 256), because in that case the compiled code doesn't use the bit map.
3711      */      */
3712    
3713      memset(classbits, 0, 32 * sizeof(uschar));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
3714    
3715  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3716      class_utf8 = FALSE;                       /* No chars >= 256 */      xclass = FALSE;                           /* No chars >= 256 */
3717      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For UTF-8 items */
3718        class_uchardata_base = class_uchardata;   /* For resetting in pass 1 */
3719  #endif  #endif
3720    
3721      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
# Line 2466  for (;; ptr++) Line 3724  for (;; ptr++)
3724    
3725      if (c != 0) do      if (c != 0) do
3726        {        {
3727        const uschar *oldptr;        const pcre_uchar *oldptr;
3728    
3729  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3730        if (utf8 && c > 127)        if (utf8 && c > 127)
# Line 2475  for (;; ptr++) Line 3733  for (;; ptr++)
3733          }          }
3734  #endif  #endif
3735    
3736    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3737          /* In the pre-compile phase, accumulate the length of any extra
3738          data and reset the pointer. This is so that very large classes that
3739          contain a zillion > 255 characters no longer overwrite the work space
3740          (which is on the stack). */
3741    
3742          if (lengthptr != NULL)
3743            {
3744            *lengthptr += class_uchardata - class_uchardata_base;
3745            class_uchardata = class_uchardata_base;
3746            }
3747    #endif
3748    
3749        /* Inside \Q...\E everything is literal except \E */        /* Inside \Q...\E everything is literal except \E */
3750    
3751        if (inescq)        if (inescq)
3752          {          {
3753          if (c == '\\' && ptr[1] == 'E')     /* If we are at \E */          if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)  /* If we are at \E */
3754            {            {
3755            inescq = FALSE;                   /* Reset literal state */            inescq = FALSE;                   /* Reset literal state */
3756            ptr++;                            /* Skip the 'E' */            ptr++;                            /* Skip the 'E' */
# Line 2494  for (;; ptr++) Line 3765  for (;; ptr++)
3765        [.ch.] and [=ch=] ("collating elements") and fault them, as Perl        [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
3766        5.6 and 5.8 do. */        5.6 and 5.8 do. */
3767    
3768        if (c == '[' &&        if (c == CHAR_LEFT_SQUARE_BRACKET &&
3769            (ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&            (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3770            check_posix_syntax(ptr, &tempptr, cd))             ptr[1] == CHAR_EQUALS_SIGN) && check_posix_syntax(ptr, &tempptr))
3771          {          {
3772          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
3773          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
3774          register const uschar *cbits = cd->cbits;          register const pcre_uint8 *cbits = cd->cbits;
3775          uschar pbits[32];          pcre_uint8 pbits[32];
3776    
3777          if (ptr[1] != ':')          if (ptr[1] != CHAR_COLON)
3778            {            {
3779            *errorcodeptr = ERR31;            *errorcodeptr = ERR31;
3780            goto FAILED;            goto FAILED;
3781            }            }
3782    
3783          ptr += 2;          ptr += 2;
3784          if (*ptr == '^')          if (*ptr == CHAR_CIRCUMFLEX_ACCENT)
3785            {            {
3786            local_negate = TRUE;            local_negate = TRUE;
3787              should_flip_negation = TRUE;  /* Note negative special */
3788            ptr++;            ptr++;
3789            }            }
3790    
3791          posix_class = check_posix_name(ptr, tempptr - ptr);          posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
3792          if (posix_class < 0)          if (posix_class < 0)
3793            {            {
3794            *errorcodeptr = ERR30;            *errorcodeptr = ERR30;
# Line 2530  for (;; ptr++) Line 3802  for (;; ptr++)
3802          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
3803            posix_class = 0;            posix_class = 0;
3804    
3805          /* We build the bit map for the POSIX class in a chunk of local store          /* When PCRE_UCP is set, some of the POSIX classes are converted to
3806          because we may be adding and subtracting from it, and we don't want to          different escape sequences that use Unicode properties. */
3807          subtract bits that may be in the main map already. At the end we or the  
3808          result into the bit map that is being built. */  #ifdef SUPPORT_UCP
3809            if ((options & PCRE_UCP) != 0)
3810              {
3811              int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
3812              if (posix_substitutes[pc] != NULL)
3813                {
3814                nestptr = tempptr + 1;
3815                ptr = posix_substitutes[pc] - 1;
3816                continue;
3817                }
3818              }
3819    #endif
3820            /* In the non-UCP case, we build the bit map for the POSIX class in a
3821            chunk of local store because we may be adding and subtracting from it,
3822            and we don't want to subtract bits that may be in the main map already.
3823            At the end we or the result into the bit map that is being built. */
3824    
3825          posix_class *= 3;          posix_class *= 3;
3826    
3827          /* Copy in the first table (always present) */          /* Copy in the first table (always present) */
3828    
3829          memcpy(pbits, cbits + posix_class_maps[posix_class],          memcpy(pbits, cbits + posix_class_maps[posix_class],
3830            32 * sizeof(uschar));            32 * sizeof(pcre_uint8));
3831    
3832          /* If there is a second table, add or remove it as required. */          /* If there is a second table, add or remove it as required. */
3833    
# Line 2577  for (;; ptr++) Line 3864  for (;; ptr++)
3864    
3865        /* Backslash may introduce a single character, or it may introduce one        /* Backslash may introduce a single character, or it may introduce one
3866        of the specials, which just set a flag. The sequence \b is a special        of the specials, which just set a flag. The sequence \b is a special
3867        case. Inside a class (and only there) it is treated as backspace.        case. Inside a class (and only there) it is treated as backspace. We
3868        Elsewhere it marks a word boundary. Other escapes have preset maps ready        assume that other escapes have more than one character in them, so set
3869        to or into the one we are building. We assume they have more than one        class_charcount bigger than one. Unrecognized escapes fall through and
3870        character in them, so set class_charcount bigger than one. */        are either treated as literal characters (by default), or are faulted if
3871          PCRE_EXTRA is set. */
3872    
3873        if (c == '\\')        if (c == CHAR_BACKSLASH)
3874          {          {
3875          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3876          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
3877    
3878          if (-c == ESC_b) c = '\b';       /* \b is backslash in a class */          if (-c == ESC_b) c = CHAR_BS;    /* \b is backspace in a class */
         else if (-c == ESC_X) c = 'X';   /* \X is literal X in a class */  
         else if (-c == ESC_R) c = 'R';   /* \R is literal R in a class */  
3879          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (-c == ESC_Q)            /* Handle start of quoted string */
3880            {            {
3881            if (ptr[1] == '\\' && ptr[2] == 'E')            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
3882              {              {
3883              ptr += 2; /* avoid empty string */              ptr += 2; /* avoid empty string */
3884              }              }
3885            else inescq = TRUE;            else inescq = TRUE;
3886            continue;            continue;
3887            }            }
3888            else if (-c == ESC_E) continue;  /* Ignore orphan \E */
3889    
3890          if (c < 0)          if (c < 0)
3891            {            {
3892            register const uschar *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
3893            class_charcount += 2;     /* Greater than 1 is what matters */            class_charcount += 2;     /* Greater than 1 is what matters */
3894    
3895            /* Save time by not doing this in the pre-compile phase. */            switch (-c)
   
           if (lengthptr == NULL) switch (-c)  
3896              {              {
3897    #ifdef SUPPORT_UCP
3898                case ESC_du: &nbs