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

Diff of /code/trunk/pcre_compile.c

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

revision 334 by ph10, Fri Apr 11 15:48:14 2008 UTC revision 1379 by ph10, Mon Oct 14 13:54:07 2013 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2008 University of Cambridge             Copyright (c) 1997-2013 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 53  supporting internal functions that are n Line 53  supporting internal functions that are n
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    
56  /* When DEBUG is defined, we need the pcre_printint() function, which is also  /* When PCRE_DEBUG is defined, we need the pcre(16|32)_printint() function, which
57  used by pcretest. DEBUG is not defined when building a production library. */  is also used by pcretest. PCRE_DEBUG is not defined when building a production
58    library. We do not need to select pcre16_printint.c specially, because the
59  #ifdef DEBUG  COMPILE_PCREx macro will already be appropriately set. */
60  #include "pcre_printint.src"  
61    #ifdef PCRE_DEBUG
62    /* pcre_printint.c should not include any headers */
63    #define PCRE_INCLUDED
64    #include "pcre_printint.c"
65    #undef PCRE_INCLUDED
66  #endif  #endif
67    
68    
69  /* Macro for setting individual bits in class bitmaps. */  /* Macro for setting individual bits in class bitmaps. */
70    
71  #define SETBIT(a,b) a[b/8] |= (1 << (b%8))  #define SETBIT(a,b) a[(b)/8] |= (1 << ((b)&7))
72    
73  /* Maximum length value to check against when making sure that the integer that  /* Maximum length value to check against when making sure that the integer that
74  holds the compiled pattern length does not overflow. We make it a bit less than  holds the compiled pattern length does not overflow. We make it a bit less than
# Line 72  to check them every time. */ Line 77  to check them every time. */
77    
78  #define OFLOW_MAX (INT_MAX - 20)  #define OFLOW_MAX (INT_MAX - 20)
79    
80    /* Definitions to allow mutual recursion */
81    
82    static int
83      add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
84        const pcre_uint32 *, unsigned int);
85    
86    static BOOL
87      compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
88        pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,
89        compile_data *, int *);
90    
91    
92    
93  /*************************************************  /*************************************************
94  *      Code parameters and static tables         *  *      Code parameters and static tables         *
# Line 87  so this number is very generous. Line 104  so this number is very generous.
104  The same workspace is used during the second, actual compile phase for  The same workspace is used during the second, actual compile phase for
105  remembering forward references to groups so that they can be filled in at the  remembering forward references to groups so that they can be filled in at the
106  end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE  end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
107  is 4 there is plenty of room. */  is 4 there is plenty of room for most patterns. However, the memory can get
108    filled up by repetitions of forward references, for example patterns like
109    /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
110    that the workspace is expanded using malloc() in this situation. The value
111    below is therefore a minimum, and we put a maximum on it for safety. The
112    minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
113    kicks in at the same number of forward references in all cases. */
114    
115    #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116    #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
117    
118  #define COMPILE_WORK_SIZE (4096)  /* This value determines the size of the initial vector that is used for
119    remembering named groups during the pre-compile. It is allocated on the stack,
120    but if it is too small, it is expanded using malloc(), in a similar way to the
121    workspace. The value is the number of slots in the list. */
122    
123    #define NAMED_GROUP_LIST_SIZE  20
124    
125    /* The overrun tests check for a slightly smaller size so that they detect the
126    overrun before it actually does run off the end of the data block. */
127    
128    #define WORK_SIZE_SAFETY_MARGIN (100)
129    
130    /* Private flags added to firstchar and reqchar. */
131    
132    #define REQ_CASELESS    (1 << 0)        /* Indicates caselessness */
133    #define REQ_VARY        (1 << 1)        /* Reqchar followed non-literal item */
134    /* Negative values for the firstchar and reqchar flags */
135    #define REQ_UNSET       (-2)
136    #define REQ_NONE        (-1)
137    
138    /* Repeated character flags. */
139    
140    #define UTF_LENGTH     0x10000000l      /* The char contains its length. */
141    
142  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
143  are simple data values; negative values are for special things like \d and so  are simple data values; negative values are for special things like \d and so
144  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
145  is invalid. */  is invalid. */
146    
147  #ifndef EBCDIC  /* This is the "normal" table for ASCII systems */  #ifndef EBCDIC
148    
149    /* This is the "normal" table for ASCII systems or for EBCDIC systems running
150    in UTF-8 mode. */
151    
152  static const short int escapes[] = {  static const short int escapes[] = {
153       0,      0,      0,      0,      0,      0,      0,      0,   /* 0 - 7 */       0,                       0,
154       0,      0,    ':',    ';',    '<',    '=',    '>',    '?',   /* 8 - ? */       0,                       0,
155     '@', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E,      0, -ESC_G,   /* @ - G */       0,                       0,
156  -ESC_H,      0,      0, -ESC_K,      0,      0,      0,      0,   /* H - O */       0,                       0,
157  -ESC_P, -ESC_Q, -ESC_R, -ESC_S,      0,      0, -ESC_V, -ESC_W,   /* P - W */       0,                       0,
158  -ESC_X,      0, -ESC_Z,    '[',   '\\',    ']',    '^',    '_',   /* X - _ */       CHAR_COLON,              CHAR_SEMICOLON,
159     '`',      7, -ESC_b,      0, -ESC_d,  ESC_e,  ESC_f,      0,   /* ` - g */       CHAR_LESS_THAN_SIGN,     CHAR_EQUALS_SIGN,
160  -ESC_h,      0,      0, -ESC_k,      0,      0,  ESC_n,      0,   /* h - o */       CHAR_GREATER_THAN_SIGN,  CHAR_QUESTION_MARK,
161  -ESC_p,      0,  ESC_r, -ESC_s,  ESC_tee,    0, -ESC_v, -ESC_w,   /* p - w */       CHAR_COMMERCIAL_AT,      -ESC_A,
162       0,      0, -ESC_z                                            /* x - z */       -ESC_B,                  -ESC_C,
163         -ESC_D,                  -ESC_E,
164         0,                       -ESC_G,
165         -ESC_H,                  0,
166         0,                       -ESC_K,
167         0,                       0,
168         -ESC_N,                  0,
169         -ESC_P,                  -ESC_Q,
170         -ESC_R,                  -ESC_S,
171         0,                       0,
172         -ESC_V,                  -ESC_W,
173         -ESC_X,                  0,
174         -ESC_Z,                  CHAR_LEFT_SQUARE_BRACKET,
175         CHAR_BACKSLASH,          CHAR_RIGHT_SQUARE_BRACKET,
176         CHAR_CIRCUMFLEX_ACCENT,  CHAR_UNDERSCORE,
177         CHAR_GRAVE_ACCENT,       7,
178         -ESC_b,                  0,
179         -ESC_d,                  ESC_e,
180         ESC_f,                   0,
181         -ESC_h,                  0,
182         0,                       -ESC_k,
183         0,                       0,
184         ESC_n,                   0,
185         -ESC_p,                  0,
186         ESC_r,                   -ESC_s,
187         ESC_tee,                 0,
188         -ESC_v,                  -ESC_w,
189         0,                       0,
190         -ESC_z
191  };  };
192    
193  #else           /* This is the "abnormal" table for EBCDIC systems */  #else
194    
195    /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
196    
197  static const short int escapes[] = {  static const short int escapes[] = {
198  /*  48 */     0,     0,      0,     '.',    '<',   '(',    '+',    '|',  /*  48 */     0,     0,      0,     '.',    '<',   '(',    '+',    '|',
199  /*  50 */   '&',     0,      0,       0,      0,     0,      0,      0,  /*  50 */   '&',     0,      0,       0,      0,     0,      0,      0,
# Line 130  static const short int escapes[] = { Line 212  static const short int escapes[] = {
212  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',  /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',
213  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,  /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,
214  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,
215  /*  D0 */   '}',     0, -ESC_K,       0,      0,     0,      0, -ESC_P,  /*  D0 */   '}',     0, -ESC_K,       0,      0,-ESC_N,      0, -ESC_P,
216  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,  /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,
217  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,  /*  E0 */  '\\',     0, -ESC_S,       0,      0,-ESC_V, -ESC_W, -ESC_X,
218  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,  /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,
# Line 142  static const short int escapes[] = { Line 224  static const short int escapes[] = {
224    
225  /* Table of special "verbs" like (*PRUNE). This is a short table, so it is  /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
226  searched linearly. Put all the names into a single string, in order to reduce  searched linearly. Put all the names into a single string, in order to reduce
227  the number of relocations when a shared library is dynamically linked. */  the number of relocations when a shared library is dynamically linked. The
228    string is built from string macros so that it works in UTF-8 mode on EBCDIC
229    platforms. */
230    
231  typedef struct verbitem {  typedef struct verbitem {
232    int   len;    int   len;                 /* Length of verb name */
233    int   op;    int   op;                  /* Op when no arg, or -1 if arg mandatory */
234      int   op_arg;              /* Op when arg present, or -1 if not allowed */
235  } verbitem;  } verbitem;
236    
237  static const char verbnames[] =  static const char verbnames[] =
238    "ACCEPT\0"    "\0"                       /* Empty name is a shorthand for MARK */
239    "COMMIT\0"    STRING_MARK0
240    "F\0"    STRING_ACCEPT0
241    "FAIL\0"    STRING_COMMIT0
242    "PRUNE\0"    STRING_F0
243    "SKIP\0"    STRING_FAIL0
244    "THEN";    STRING_PRUNE0
245      STRING_SKIP0
246      STRING_THEN;
247    
248  static const verbitem verbs[] = {  static const verbitem verbs[] = {
249    { 6, OP_ACCEPT },    { 0, -1,        OP_MARK },
250    { 6, OP_COMMIT },    { 4, -1,        OP_MARK },
251    { 1, OP_FAIL },    { 6, OP_ACCEPT, -1 },
252    { 4, OP_FAIL },    { 6, OP_COMMIT, -1 },
253    { 5, OP_PRUNE },    { 1, OP_FAIL,   -1 },
254    { 4, OP_SKIP  },    { 4, OP_FAIL,   -1 },
255    { 4, OP_THEN  }    { 5, OP_PRUNE,  OP_PRUNE_ARG },
256      { 4, OP_SKIP,   OP_SKIP_ARG  },
257      { 4, OP_THEN,   OP_THEN_ARG  }
258  };  };
259    
260  static const int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
# Line 178  length entry. The first three must be al Line 267  length entry. The first three must be al
267  for handling case independence. */  for handling case independence. */
268    
269  static const char posix_names[] =  static const char posix_names[] =
270    "alpha\0"  "lower\0"  "upper\0"  "alnum\0"  "ascii\0"  "blank\0"    STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
271    "cntrl\0"  "digit\0"  "graph\0"  "print\0"  "punct\0"  "space\0"    STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
272    "word\0"   "xdigit";    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
273      STRING_word0  STRING_xdigit;
274    
275  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
276    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
277    
278  /* Table of class bit maps for each POSIX class. Each class is formed from a  /* Table of class bit maps for each POSIX class. Each class is formed from a
# Line 212  static const int posix_class_maps[] = { Line 302  static const int posix_class_maps[] = {
302    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
303  };  };
304    
305    /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
306    substitutes must be in the order of the names, defined above, and there are
307    both positive and negative cases. NULL means no substitute. */
308    
309    #ifdef SUPPORT_UCP
310    static const pcre_uchar string_PNd[]  = {
311      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
312      CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
313    static const pcre_uchar string_pNd[]  = {
314      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
315      CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
316    static const pcre_uchar string_PXsp[] = {
317      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
318      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
319    static const pcre_uchar string_pXsp[] = {
320      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
321      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
322    static const pcre_uchar string_PXwd[] = {
323      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
324      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
325    static const pcre_uchar string_pXwd[] = {
326      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
327      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
328    
329    static const pcre_uchar *substitutes[] = {
330      string_PNd,           /* \D */
331      string_pNd,           /* \d */
332      string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
333      string_pXsp,          /* \s */
334      string_PXwd,          /* \W */
335      string_pXwd           /* \w */
336    };
337    
338    static const pcre_uchar string_pL[] =   {
339      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
340      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
341    static const pcre_uchar string_pLl[] =  {
342      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
343      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
344    static const pcre_uchar string_pLu[] =  {
345      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
346      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
347    static const pcre_uchar string_pXan[] = {
348      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
349      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
350    static const pcre_uchar string_h[] =    {
351      CHAR_BACKSLASH, CHAR_h, '\0' };
352    static const pcre_uchar string_pXps[] = {
353      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
354      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
355    static const pcre_uchar string_PL[] =   {
356      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
357      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
358    static const pcre_uchar string_PLl[] =  {
359      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
360      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
361    static const pcre_uchar string_PLu[] =  {
362      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
363      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
364    static const pcre_uchar string_PXan[] = {
365      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
366      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
367    static const pcre_uchar string_H[] =    {
368      CHAR_BACKSLASH, CHAR_H, '\0' };
369    static const pcre_uchar string_PXps[] = {
370      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
371      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
372    
373    static const pcre_uchar *posix_substitutes[] = {
374      string_pL,            /* alpha */
375      string_pLl,           /* lower */
376      string_pLu,           /* upper */
377      string_pXan,          /* alnum */
378      NULL,                 /* ascii */
379      string_h,             /* blank */
380      NULL,                 /* cntrl */
381      string_pNd,           /* digit */
382      NULL,                 /* graph */
383      NULL,                 /* print */
384      NULL,                 /* punct */
385      string_pXps,          /* space */    /* NOTE: Xps is POSIX space */
386      string_pXwd,          /* word */
387      NULL,                 /* xdigit */
388      /* Negated cases */
389      string_PL,            /* ^alpha */
390      string_PLl,           /* ^lower */
391      string_PLu,           /* ^upper */
392      string_PXan,          /* ^alnum */
393      NULL,                 /* ^ascii */
394      string_H,             /* ^blank */
395      NULL,                 /* ^cntrl */
396      string_PNd,           /* ^digit */
397      NULL,                 /* ^graph */
398      NULL,                 /* ^print */
399      NULL,                 /* ^punct */
400      string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
401      string_PXwd,          /* ^word */
402      NULL                  /* ^xdigit */
403    };
404    #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
405    #endif
406    
407  #define STRING(a)  # a  #define STRING(a)  # a
408  #define XSTRING(s) STRING(s)  #define XSTRING(s) STRING(s)
# Line 224  the number of relocations needed when a Line 415  the number of relocations needed when a
415  it is now one long string. We cannot use a table of offsets, because the  it is now one long string. We cannot use a table of offsets, because the
416  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
417  simply count through to the one we want - this isn't a performance issue  simply count through to the one we want - this isn't a performance issue
418  because these strings are used only when there is a compilation error. */  because these strings are used only when there is a compilation error.
419    
420    Each substring ends with \0 to insert a null character. This includes the final
421    substring, so that the whole string ends with \0\0, which can be detected when
422    counting through. */
423    
424  static const char error_texts[] =  static const char error_texts[] =
425    "no error\0"    "no error\0"
# Line 265  static const char error_texts[] = Line 460  static const char error_texts[] =
460    /* 30 */    /* 30 */
461    "unknown POSIX class name\0"    "unknown POSIX class name\0"
462    "POSIX collating elements are not supported\0"    "POSIX collating elements are not supported\0"
463    "this version of PCRE is not compiled with PCRE_UTF8 support\0"    "this version of PCRE is compiled without UTF support\0"
464    "spare error\0"  /** DEAD **/    "spare error\0"  /** DEAD **/
465    "character value in \\x{...} sequence is too large\0"    "character value in \\x{} or \\o{} is too large\0"
466    /* 35 */    /* 35 */
467    "invalid condition (?(0)\0"    "invalid condition (?(0)\0"
468    "\\C not allowed in lookbehind assertion\0"    "\\C not allowed in lookbehind assertion\0"
469    "PCRE does not support \\L, \\l, \\N, \\U, or \\u\0"    "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
470    "number after (?C is > 255\0"    "number after (?C is > 255\0"
471    "closing ) for (?C expected\0"    "closing ) for (?C expected\0"
472    /* 40 */    /* 40 */
# Line 288  static const char error_texts[] = Line 483  static const char error_texts[] =
483    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
484    /* 50 */    /* 50 */
485    "repeated subpattern is too long\0"    /** DEAD **/    "repeated subpattern is too long\0"    /** DEAD **/
486    "octal value is greater than \\377 (not in UTF-8 mode)\0"    "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
487    "internal error: overran compiling workspace\0"    "internal error: overran compiling workspace\0"
488    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
489    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
490    /* 55 */    /* 55 */
491    "repeating a DEFINE group is not allowed\0"    "repeating a DEFINE group is not allowed\0"  /** DEAD **/
492    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
493    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
494    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
495    "(*VERB) with an argument is not supported\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
496    /* 60 */    /* 60 */
497    "(*VERB) not recognized\0"    "(*VERB) not recognized or malformed\0"
498    "number is too big\0"    "number is too big\0"
499    "subpattern name expected\0"    "subpattern name expected\0"
500    "digit expected after (?+";    "digit expected after (?+\0"
501      "] is an invalid data character in JavaScript compatibility mode\0"
502      /* 65 */
503      "different names for subpatterns of the same number are not allowed\0"
504      "(*MARK) must have an argument\0"
505      "this version of PCRE is not compiled with Unicode property support\0"
506      "\\c must be followed by an ASCII character\0"
507      "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
508      /* 70 */
509      "internal error: unknown opcode in find_fixedlength()\0"
510      "\\N is not supported in a class\0"
511      "too many forward references\0"
512      "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
513      "invalid UTF-16 string\0"
514      /* 75 */
515      "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
516      "character value in \\u.... sequence is too large\0"
517      "invalid UTF-32 string\0"
518      "setting UTF is disabled by the application\0"
519      "non-hex character in \\x{} (closing brace missing?)\0"
520      /* 80 */
521      "non-octal character in \\o{} (closing brace missing?)\0"
522      "missing opening brace after \\o\0"
523      ;
524    
525  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
526  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 321  For convenience, we use the same bit def Line 538  For convenience, we use the same bit def
538    
539  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
540    
541  #ifndef EBCDIC  /* This is the "normal" case, for ASCII systems */  /* Using a simple comparison for decimal numbers rather than a memory read
542  static const unsigned char digitab[] =  is much faster, and the resulting code is simpler (the compiler turns it
543    into a subtraction and unsigned comparison). */
544    
545    #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
546    
547    #ifndef EBCDIC
548    
549    /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
550    UTF-8 mode. */
551    
552    static const pcre_uint8 digitab[] =
553    {    {
554    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
555    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */
# Line 357  static const unsigned char digitab[] = Line 584  static const unsigned char digitab[] =
584    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
585    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
586    
587  #else           /* This is the "abnormal" case, for EBCDIC systems */  #else
588  static const unsigned char digitab[] =  
589    /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
590    
591    static const pcre_uint8 digitab[] =
592    {    {
593    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
594    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */
# Line 393  static const unsigned char digitab[] = Line 623  static const unsigned char digitab[] =
623    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */
624    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */
625    
626  static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */  static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
627    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */
628    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */
629    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */
# Line 429  static const unsigned char ebcdic_charta Line 659  static const unsigned char ebcdic_charta
659  #endif  #endif
660    
661    
662  /* Definition to allow mutual recursion */  /* This table is used to check whether auto-possessification is possible
663    between adjacent character-type opcodes. The left-hand (repeated) opcode is
664    used to select the row, and the right-hand opcode is use to select the column.
665    A value of 1 means that auto-possessification is OK. For example, the second
666    value in the first row means that \D+\d can be turned into \D++\d.
667    
668    The Unicode property types (\P and \p) have to be present to fill out the table
669    because of what their opcode values are, but the table values should always be
670    zero because property types are handled separately in the code. The last four
671    columns apply to items that cannot be repeated, so there is no need to have
672    rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
673    *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
674    
675    #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
676    #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
677    
678    static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
679    /* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
680      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
681      { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
682      { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
683      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
684      { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
685      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
686      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
687      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
688      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
689      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
690      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
691      { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
692      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
693      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
694      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
695      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
696      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
697    };
698    
699  static BOOL  
700    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,  /* This table is used to check whether auto-possessification is possible
701      int *, int *, branch_chain *, compile_data *, int *);  between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
702    left-hand (repeated) opcode is used to select the row, and the right-hand
703    opcode is used to select the column. The values are as follows:
704    
705      0   Always return FALSE (never auto-possessify)
706      1   Character groups are distinct (possessify if both are OP_PROP)
707      2   Check character categories in the same group (general or particular)
708      3   TRUE if the two opcodes are not the same (PROP vs NOTPROP)
709    
710      4   Check left general category vs right particular category
711      5   Check right general category vs left particular category
712    
713      6   Left alphanum vs right general category
714      7   Left space vs right general category
715      8   Left word vs right general category
716    
717      9   Right alphanum vs left general category
718     10   Right space vs left general category
719     11   Right word vs left general category
720    
721     12   Left alphanum vs right particular category
722     13   Left space vs right particular category
723     14   Left word vs right particular category
724    
725     15   Right alphanum vs left particular category
726     16   Right space vs left particular category
727     17   Right word vs left particular category
728    */
729    
730    static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
731    /* ANY LAMP GC  PC  SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
732      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_ANY */
733      { 0,  3,  0,  0,  0,    3,    1,      1,   0,    0,   0 },  /* PT_LAMP */
734      { 0,  0,  2,  4,  0,    9,   10,     10,  11,    0,   0 },  /* PT_GC */
735      { 0,  0,  5,  2,  0,   15,   16,     16,  17,    0,   0 },  /* PT_PC */
736      { 0,  0,  0,  0,  2,    0,    0,      0,   0,    0,   0 },  /* PT_SC */
737      { 0,  3,  6, 12,  0,    3,    1,      1,   0,    0,   0 },  /* PT_ALNUM */
738      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_SPACE */
739      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_PXSPACE */
740      { 0,  0,  8, 14,  0,    0,    1,      1,   3,    0,   0 },  /* PT_WORD */
741      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_CLIST */
742      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   3 }   /* PT_UCNC */
743    };
744    
745    /* This table is used to check whether auto-possessification is possible
746    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
747    specifies a general category and the other specifies a particular category. The
748    row is selected by the general category and the column by the particular
749    category. The value is 1 if the particular category is not part of the general
750    category. */
751    
752    static const pcre_uint8 catposstab[7][30] = {
753    /* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
754      { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* C */
755      { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* L */
756      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* M */
757      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* N */
758      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },  /* P */
759      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 },  /* S */
760      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 }   /* Z */
761    };
762    
763    /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
764    a general or particular category. The properties in each row are those
765    that apply to the character set in question. Duplication means that a little
766    unnecessary work is done when checking, but this keeps things much simpler
767    because they can all use the same code. For more details see the comment where
768    this table is used.
769    
770    Note: SPACE and PXSPACE used to be different because Perl excluded VT from
771    "space", but from Perl 5.18 it's included, so both categories are treated the
772    same here. */
773    
774    static const pcre_uint8 posspropstab[3][4] = {
775      { ucp_L, ucp_N, ucp_N, ucp_Nl },  /* ALNUM, 3rd and 4th values redundant */
776      { ucp_Z, ucp_Z, ucp_C, ucp_Cc },  /* SPACE and PXSPACE, 2nd value redundant */
777      { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
778    };
779    
780    
781    
# Line 454  static const char * Line 796  static const char *
796  find_error_text(int n)  find_error_text(int n)
797  {  {
798  const char *s = error_texts;  const char *s = error_texts;
799  for (; n > 0; n--) while (*s++ != 0);  for (; n > 0; n--)
800      {
801      while (*s++ != CHAR_NULL) {};
802      if (*s == CHAR_NULL) return "Error text not found (please report)";
803      }
804  return s;  return s;
805  }  }
806    
807    
808    
809    /*************************************************
810    *           Expand the workspace                 *
811    *************************************************/
812    
813    /* This function is called during the second compiling phase, if the number of
814    forward references fills the existing workspace, which is originally a block on
815    the stack. A larger block is obtained from malloc() unless the ultimate limit
816    has been reached or the increase will be rather small.
817    
818    Argument: pointer to the compile data block
819    Returns:  0 if all went well, else an error number
820    */
821    
822    static int
823    expand_workspace(compile_data *cd)
824    {
825    pcre_uchar *newspace;
826    int newsize = cd->workspace_size * 2;
827    
828    if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
829    if (cd->workspace_size >= COMPILE_WORK_SIZE_MAX ||
830        newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
831     return ERR72;
832    
833    newspace = (PUBL(malloc))(IN_UCHARS(newsize));
834    if (newspace == NULL) return ERR21;
835    memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
836    cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
837    if (cd->workspace_size > COMPILE_WORK_SIZE)
838      (PUBL(free))((void *)cd->start_workspace);
839    cd->start_workspace = newspace;
840    cd->workspace_size = newsize;
841    return 0;
842    }
843    
844    
845    
846    /*************************************************
847    *            Check for counted repeat            *
848    *************************************************/
849    
850    /* This function is called when a '{' is encountered in a place where it might
851    start a quantifier. It looks ahead to see if it really is a quantifier or not.
852    It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
853    where the ddds are digits.
854    
855    Arguments:
856      p         pointer to the first char after '{'
857    
858    Returns:    TRUE or FALSE
859    */
860    
861    static BOOL
862    is_counted_repeat(const pcre_uchar *p)
863    {
864    if (!IS_DIGIT(*p)) return FALSE;
865    p++;
866    while (IS_DIGIT(*p)) p++;
867    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
868    
869    if (*p++ != CHAR_COMMA) return FALSE;
870    if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
871    
872    if (!IS_DIGIT(*p)) return FALSE;
873    p++;
874    while (IS_DIGIT(*p)) p++;
875    
876    return (*p == CHAR_RIGHT_CURLY_BRACKET);
877    }
878    
879    
880    
881  /*************************************************  /*************************************************
882  *            Handle escapes                      *  *            Handle escapes                      *
883  *************************************************/  *************************************************/
884    
885  /* This function is called when a \ has been encountered. It either returns a  /* This function is called when a \ has been encountered. It either returns a
886  positive value for a simple escape such as \n, or a negative value which  positive value for a simple escape such as \n, or 0 for a data character which
887  encodes one of the more complicated things such as \d. A backreference to group  will be placed in chptr. A backreference to group n is returned as negative n.
888  n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When  When UTF-8 is enabled, a positive value greater than 255 may be returned in
889  UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,  chptr. On entry, ptr is pointing at the \. On exit, it is on the final
890  ptr is pointing at the \. On exit, it is on the final character of the escape  character of the escape sequence.
 sequence.  
891    
892  Arguments:  Arguments:
893    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
894      chptr          points to a returned data character
895    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
896    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
897    options        the options bits    options        the options bits
898    isclass        TRUE if inside a character class    isclass        TRUE if inside a character class
899    
900  Returns:         zero or positive => a data character  Returns:         zero => a data character
901                   negative => a special escape sequence                   positive => a special escape sequence
902                     negative => a back reference
903                   on error, errorcodeptr is set                   on error, errorcodeptr is set
904  */  */
905    
906  static int  static int
907  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
908    int options, BOOL isclass)    int bracount, int options, BOOL isclass)
909  {  {
910  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
911  const uschar *ptr = *ptrptr + 1;  BOOL utf = (options & PCRE_UTF8) != 0;
912  int c, i;  const pcre_uchar *ptr = *ptrptr + 1;
913    pcre_uint32 c;
914    int escape = 0;
915    int i;
916    
917  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
918  ptr--;                            /* Set pointer back to the last byte */  ptr--;                            /* Set pointer back to the last byte */
919    
920  /* If backslash is at the end of the pattern, it's an error. */  /* If backslash is at the end of the pattern, it's an error. */
921    
922  if (c == 0) *errorcodeptr = ERR1;  if (c == CHAR_NULL) *errorcodeptr = ERR1;
923    
924  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
925  in a table. A non-zero result is something that can be returned immediately.  in a table. A non-zero result is something that can be returned immediately.
926  Otherwise further processing may be required. */  Otherwise further processing may be required. */
927    
928  #ifndef EBCDIC  /* ASCII coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
929  else if (c < '0' || c > 'z') {}                           /* Not alphanumeric */  /* Not alphanumeric */
930  else if ((i = escapes[c - '0']) != 0) c = i;  else if (c < CHAR_0 || c > CHAR_z) {}
931    else if ((i = escapes[c - CHAR_0]) != 0)
932      { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
933    
934  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
935  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */  /* Not alphanumeric */
936  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
937    else if ((i = escapes[c - 0x48]) != 0)  { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
938  #endif  #endif
939    
940  /* Escapes that need further processing, or are illegal. */  /* Escapes that need further processing, or are illegal. */
941    
942  else  else
943    {    {
944    const uschar *oldptr;    const pcre_uchar *oldptr;
945    BOOL braced, negated;    BOOL braced, negated, overflow;
946      int s;
947    
948    switch (c)    switch (c)
949      {      {
950      /* 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
951      error. */      error. */
952    
953      case 'l':      case CHAR_l:
954      case 'L':      case CHAR_L:
     case 'N':  
     case 'u':  
     case 'U':  
955      *errorcodeptr = ERR37;      *errorcodeptr = ERR37;
956      break;      break;
957    
958      /* \g must be followed by one of a number of specific things:      case CHAR_u:
959        if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
960          {
961          /* In JavaScript, \u must be followed by four hexadecimal numbers.
962          Otherwise it is a lowercase u letter. */
963          if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
964            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
965            && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
966            && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
967            {
968            c = 0;
969            for (i = 0; i < 4; ++i)
970              {
971              register pcre_uint32 cc = *(++ptr);
972    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
973              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
974              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
975    #else           /* EBCDIC coding */
976              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
977              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
978    #endif
979              }
980    
981    #if defined COMPILE_PCRE8
982            if (c > (utf ? 0x10ffffU : 0xffU))
983    #elif defined COMPILE_PCRE16
984            if (c > (utf ? 0x10ffffU : 0xffffU))
985    #elif defined COMPILE_PCRE32
986            if (utf && c > 0x10ffffU)
987    #endif
988              {
989              *errorcodeptr = ERR76;
990              }
991            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
992            }
993          }
994        else
995          *errorcodeptr = ERR37;
996        break;
997    
998        case CHAR_U:
999        /* In JavaScript, \U is an uppercase U letter. */
1000        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
1001        break;
1002    
1003        /* In a character class, \g is just a literal "g". Outside a character
1004        class, \g must be followed by one of a number of specific things:
1005    
1006      (1) A number, either plain or braced. If positive, it is an absolute      (1) A number, either plain or braced. If positive, it is an absolute
1007      backreference. If negative, it is a relative backreference. This is a Perl      backreference. If negative, it is a relative backreference. This is a Perl
1008      5.10 feature.      5.10 feature.
1009    
1010      (2) Perl 5.10 also supports \g{name} as a reference to a named group. This      (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
1011      is part of Perl's movement towards a unified syntax for back references. As      is part of Perl's movement towards a unified syntax for back references. As
1012      this is synonymous with \k{name}, we fudge it up by pretending it really      this is synonymous with \k{name}, we fudge it up by pretending it really
1013      was \k.      was \k.
1014    
1015      (3) For Oniguruma compatibility we also support \g followed by a name or a      (3) For Oniguruma compatibility we also support \g followed by a name or a
1016      number either in angle brackets or in single quotes. However, these are      number either in angle brackets or in single quotes. However, these are
1017      (possibly recursive) subroutine calls, _not_ backreferences. Just return      (possibly recursive) subroutine calls, _not_ backreferences. Just return
1018      the -ESC_g code (cf \k). */      the ESC_g code (cf \k). */
1019    
1020      case 'g':      case CHAR_g:
1021      if (ptr[1] == '<' || ptr[1] == '\'')      if (isclass) break;
1022        {      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
1023        c = -ESC_g;        {
1024        break;        escape = ESC_g;
1025        }        break;
1026          }
1027    
1028      /* Handle the Perl-compatible cases */      /* Handle the Perl-compatible cases */
1029    
1030      if (ptr[1] == '{')      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1031        {        {
1032        const uschar *p;        const pcre_uchar *p;
1033        for (p = ptr+2; *p != 0 && *p != '}'; p++)        for (p = ptr+2; *p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
1034          if (*p != '-' && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1035        if (*p != 0 && *p != '}')        if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1036          {          {
1037          c = -ESC_k;          escape = ESC_k;
1038          break;          break;
1039          }          }
1040        braced = TRUE;        braced = TRUE;
# Line 571  else Line 1042  else
1042        }        }
1043      else braced = FALSE;      else braced = FALSE;
1044    
1045      if (ptr[1] == '-')      if (ptr[1] == CHAR_MINUS)
1046        {        {
1047        negated = TRUE;        negated = TRUE;
1048        ptr++;        ptr++;
1049        }        }
1050      else negated = FALSE;      else negated = FALSE;
1051    
1052      c = 0;      /* The integer range is limited by the machine's int representation. */
1053      while ((digitab[ptr[1]] & ctype_digit) != 0)      s = 0;
1054        c = c * 10 + *(++ptr) - '0';      overflow = FALSE;
1055        while (IS_DIGIT(ptr[1]))
1056      if (c < 0)   /* Integer overflow */        {
1057          if (s > INT_MAX / 10 - 1) /* Integer overflow */
1058            {
1059            overflow = TRUE;
1060            break;
1061            }
1062          s = s * 10 + (int)(*(++ptr) - CHAR_0);
1063          }
1064        if (overflow) /* Integer overflow */
1065        {        {
1066          while (IS_DIGIT(ptr[1]))
1067            ptr++;
1068        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
1069        break;        break;
1070        }        }
1071    
1072      if (braced && *(++ptr) != '}')      if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET)
1073        {        {
1074        *errorcodeptr = ERR57;        *errorcodeptr = ERR57;
1075        break;        break;
1076        }        }
1077    
1078      if (c == 0)      if (s == 0)
1079        {        {
1080        *errorcodeptr = ERR58;        *errorcodeptr = ERR58;
1081        break;        break;
1082        }        }
1083    
1084      if (negated)      if (negated)
1085        {        {
1086        if (c > bracount)        if (s > bracount)
1087          {          {
1088          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
1089          break;          break;
1090          }          }
1091        c = bracount - (c - 1);        s = bracount - (s - 1);
1092        }        }
1093    
1094      c = -(ESC_REF + c);      escape = -s;
1095      break;      break;
1096    
1097      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
1098      starting with one that is not zero is not straightforward. By experiment,      starting with one that is not zero is not straightforward. Perl has changed
1099      the way Perl works seems to be as follows:      over the years. Nowadays \g{} for backreferences and \o{} for octal are
1100        recommended to avoid the ambiguities in the old syntax.
1101    
1102      Outside a character class, the digits are read as a decimal number. If the      Outside a character class, the digits are read as a decimal number. If the
1103      number is less than 10, or if there are that many previous extracting      number is less than 8 (used to be 10), or if there are that many previous
1104      left brackets, then it is a back reference. Otherwise, up to three octal      extracting left brackets, then it is a back reference. Otherwise, up to
1105      digits are read to form an escaped byte. Thus \123 is likely to be octal      three octal digits are read to form an escaped byte. Thus \123 is likely to
1106      123 (cf \0123, which is octal 012 followed by the literal 3). If the octal      be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If
1107      value is greater than 377, the least significant 8 bits are taken. Inside a      the octal value is greater than 377, the least significant 8 bits are
1108      character class, \ followed by a digit is always an octal number. */      taken. \8 and \9 are treated as the literal characters 8 and 9.
1109    
1110        Inside a character class, \ followed by a digit is always either a literal
1111        8 or 9 or an octal number. */
1112    
1113      case '1': case '2': case '3': case '4': case '5':      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
1114      case '6': case '7': case '8': case '9':      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
1115    
1116      if (!isclass)      if (!isclass)
1117        {        {
1118        oldptr = ptr;        oldptr = ptr;
1119        c -= '0';        /* The integer range is limited by the machine's int representation. */
1120        while ((digitab[ptr[1]] & ctype_digit) != 0)        s = (int)(c -CHAR_0);
1121          c = c * 10 + *(++ptr) - '0';        overflow = FALSE;
1122        if (c < 0)    /* Integer overflow */        while (IS_DIGIT(ptr[1]))
1123            {
1124            if (s > INT_MAX / 10 - 1) /* Integer overflow */
1125              {
1126              overflow = TRUE;
1127              break;
1128              }
1129            s = s * 10 + (int)(*(++ptr) - CHAR_0);
1130            }
1131          if (overflow) /* Integer overflow */
1132          {          {
1133            while (IS_DIGIT(ptr[1]))
1134              ptr++;
1135          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1136          break;          break;
1137          }          }
1138        if (c < 10 || c <= bracount)        if (s < 8 || s <= bracount)  /* Check for back reference */
1139          {          {
1140          c = -(ESC_REF + c);          escape = -s;
1141          break;          break;
1142          }          }
1143        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
1144        }        }
1145    
1146      /* Handle an octal number following \. If the first digit is 8 or 9, Perl      /* Handle a digit following \ when the number is not a back reference. If
1147      generates a binary zero byte and treats the digit as a following literal.      the first digit is 8 or 9, Perl used to generate a binary zero byte and
1148      Thus we have to pull back the pointer by one. */      then treat the digit as a following literal. At least by Perl 5.18 this
1149        changed so as not to insert the binary zero. */
1150    
1151      if ((c = *ptr) >= '8')      if ((c = *ptr) >= CHAR_8) break;
1152        {  
1153        ptr--;      /* Fall through with a digit less than 8 */
       c = 0;  
       break;  
       }  
1154    
1155      /* \0 always starts an octal number, but we may drop through to here with a      /* \0 always starts an octal number, but we may drop through to here with a
1156      larger first octal digit. The original code used just to take the least      larger first octal digit. The original code used just to take the least
1157      significant 8 bits of octal numbers (I think this is what early Perls used      significant 8 bits of octal numbers (I think this is what early Perls used
1158      to do). Nowadays we allow for larger numbers in UTF-8 mode, but no more      to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode,
1159      than 3 octal digits. */      but no more than 3 octal digits. */
1160    
1161      case '0':      case CHAR_0:
1162      c -= '0';      c -= CHAR_0;
1163      while(i++ < 2 && ptr[1] >= '0' && ptr[1] <= '7')      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1164          c = c * 8 + *(++ptr) - '0';          c = c * 8 + *(++ptr) - CHAR_0;
1165      if (!utf8 && c > 255) *errorcodeptr = ERR51;  #ifdef COMPILE_PCRE8
1166        if (!utf && c > 0xff) *errorcodeptr = ERR51;
1167    #endif
1168      break;      break;
1169    
1170      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \o is a relatively new Perl feature, supporting a more general way of
1171      than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is      specifying character codes in octal. The only supported form is \o{ddd}. */
     treated as a data character. */  
1172    
1173      case 'x':      case CHAR_o:
1174      if (ptr[1] == '{')      if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
1175        {        {
1176        const uschar *pt = ptr + 2;        ptr += 2;
       int count = 0;  
   
1177        c = 0;        c = 0;
1178        while ((digitab[*pt] & ctype_xdigit) != 0)        overflow = FALSE;
1179          while (*ptr >= CHAR_0 && *ptr <= CHAR_7)
1180          {          {
1181          register int cc = *pt++;          register pcre_uint32 cc = *ptr++;
1182          if (c == 0 && cc == '0') continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1183          count++;  #ifdef COMPILE_PCRE32
1184            if (c >= 0x20000000l) { overflow = TRUE; break; }
1185  #ifndef EBCDIC  /* ASCII coding */  #endif
1186          if (cc >= 'a') cc -= 32;               /* Convert to upper case */          c = (c << 3) + cc - CHAR_0 ;
1187          c = (c << 4) + cc - ((cc < 'A')? '0' : ('A' - 10));  #if defined COMPILE_PCRE8
1188  #else           /* EBCDIC coding */          if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1189          if (cc >= 'a' && cc <= 'z') cc += 64;  /* Convert to upper case */  #elif defined COMPILE_PCRE16
1190          c = (c << 4) + cc - ((cc >= '0')? '0' : ('A' - 10));          if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1191    #elif defined COMPILE_PCRE32
1192            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1193  #endif  #endif
1194          }          }
1195          if (overflow)
       if (*pt == '}')  
1196          {          {
1197          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;          while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++;
1198          ptr = pt;          *errorcodeptr = ERR34;
         break;  
1199          }          }
1200          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1201        /* If the sequence of hex digits does not end with '}', then we don't          {
1202        recognize this construct; fall through to the normal \x handling. */          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1203            }
1204          else *errorcodeptr = ERR80;
1205        }        }
1206        break;
1207    
1208      /* Read just a single-byte hex-defined char */      /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
1209        numbers. Otherwise it is a lowercase x letter. */
1210    
1211      c = 0;      case CHAR_x:
1212      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1213        {        {
1214        int cc;                               /* Some compilers don't like ++ */        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1215        cc = *(++ptr);                        /* in initializers */          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1216  #ifndef EBCDIC  /* ASCII coding */          {
1217        if (cc >= 'a') cc -= 32;              /* Convert to upper case */          c = 0;
1218        c = c * 16 + cc - ((cc < 'A')? '0' : ('A' - 10));          for (i = 0; i < 2; ++i)
1219              {
1220              register pcre_uint32 cc = *(++ptr);
1221    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1222              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1223              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1224  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1225        if (cc <= 'z') cc += 64;              /* Convert to upper case */            if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
1226        c = c * 16 + cc - ((cc >= '0')? '0' : ('A' - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1227  #endif  #endif
1228        }            }
1229            }
1230          }    /* End JavaScript handling */
1231    
1232        /* Handle \x in Perl's style. \x{ddd} is a character number which can be
1233        greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
1234        digits. If not, { used to be treated as a data character. However, Perl
1235        seems to read hex digits up to the first non-such, and ignore the rest, so
1236        that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
1237        now gives an error. */
1238    
1239        else
1240          {
1241          if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1242            {
1243            ptr += 2;
1244            c = 0;
1245            overflow = FALSE;
1246            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)
1247              {
1248              register pcre_uint32 cc = *ptr++;
1249              if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1250    
1251    #ifdef COMPILE_PCRE32
1252              if (c >= 0x10000000l) { overflow = TRUE; break; }
1253    #endif
1254    
1255    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1256              if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1257              c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1258    #else           /* EBCDIC coding */
1259              if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
1260              c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1261    #endif
1262    
1263    #if defined COMPILE_PCRE8
1264              if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1265    #elif defined COMPILE_PCRE16
1266              if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1267    #elif defined COMPILE_PCRE32
1268              if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1269    #endif
1270              }
1271    
1272            if (overflow)
1273              {
1274              while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++;
1275              *errorcodeptr = ERR34;
1276              }
1277    
1278            else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1279              {
1280              if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1281              }
1282    
1283            /* If the sequence of hex digits does not end with '}', give an error.
1284            We used just to recognize this construct and fall through to the normal
1285            \x handling, but nowadays Perl gives an error, which seems much more
1286            sensible, so we do too. */
1287    
1288            else *errorcodeptr = ERR79;
1289            }   /* End of \x{} processing */
1290    
1291          /* Read a single-byte hex-defined char (up to two hex digits after \x) */
1292    
1293          else
1294            {
1295            c = 0;
1296            while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1297              {
1298              pcre_uint32 cc;                          /* Some compilers don't like */
1299              cc = *(++ptr);                           /* ++ in initializers */
1300    #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1301              if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
1302              c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1303    #else           /* EBCDIC coding */
1304              if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */
1305              c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1306    #endif
1307              }
1308            }     /* End of \xdd handling */
1309          }       /* End of Perl-style \x handling */
1310      break;      break;
1311    
1312      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
1313      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
1314        coding is ASCII-specific, but then the whole concept of \cx is
1315      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1316    
1317      case 'c':      case CHAR_c:
1318      c = *(++ptr);      c = *(++ptr);
1319      if (c == 0)      if (c == CHAR_NULL)
1320        {        {
1321        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1322        break;        break;
1323        }        }
1324    #ifndef EBCDIC    /* ASCII/UTF-8 coding */
1325  #ifndef EBCDIC  /* ASCII coding */      if (c > 127)  /* Excludes all non-ASCII in either mode */
1326      if (c >= 'a' && c <= 'z') c -= 32;        {
1327          *errorcodeptr = ERR68;
1328          break;
1329          }
1330        if (c >= CHAR_a && c <= CHAR_z) c -= 32;
1331      c ^= 0x40;      c ^= 0x40;
1332  #else           /* EBCDIC coding */  #else             /* EBCDIC coding */
1333      if (c >= 'a' && c <= 'z') c += 64;      if (c >= CHAR_a && c <= CHAR_z) c += 64;
1334      c ^= 0xC0;      c ^= 0xC0;
1335  #endif  #endif
1336      break;      break;
# Line 763  else Line 1352  else
1352      }      }
1353    }    }
1354    
1355    /* Perl supports \N{name} for character names, as well as plain \N for "not
1356    newline". PCRE does not support \N{name}. However, it does support
1357    quantification such as \N{2,3}. */
1358    
1359    if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1360         !is_counted_repeat(ptr+2))
1361      *errorcodeptr = ERR37;
1362    
1363    /* If PCRE_UCP is set, we change the values for \d etc. */
1364    
1365    if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
1366      escape += (ESC_DU - ESC_D);
1367    
1368    /* Set the pointer to the final character before returning. */
1369    
1370  *ptrptr = ptr;  *ptrptr = ptr;
1371  return c;  *chptr = c;
1372    return escape;
1373  }  }
1374    
1375    
# Line 782  escape sequence. Line 1387  escape sequence.
1387  Argument:  Argument:
1388    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
1389    negptr         points to a boolean that is set TRUE for negation else FALSE    negptr         points to a boolean that is set TRUE for negation else FALSE
1390    dptr           points to an int that is set to the detailed property value    ptypeptr       points to an unsigned int that is set to the type value
1391      pdataptr       points to an unsigned int that is set to the detailed property value
1392    errorcodeptr   points to the error code variable    errorcodeptr   points to the error code variable
1393    
1394  Returns:         type value from ucp_type_table, or -1 for an invalid type  Returns:         TRUE if the type value was found, or FALSE for an invalid type
1395  */  */
1396    
1397  static int  static BOOL
1398  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1399      unsigned int *pdataptr, int *errorcodeptr)
1400  {  {
1401  int c, i, bot, top;  pcre_uchar c;
1402  const uschar *ptr = *ptrptr;  int i, bot, top;
1403  char name[32];  const pcre_uchar *ptr = *ptrptr;
1404    pcre_uchar name[32];
1405    
1406  c = *(++ptr);  c = *(++ptr);
1407  if (c == 0) goto ERROR_RETURN;  if (c == CHAR_NULL) goto ERROR_RETURN;
1408    
1409  *negptr = FALSE;  *negptr = FALSE;
1410    
1411  /* \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
1412  negation. */  negation. */
1413    
1414  if (c == '{')  if (c == CHAR_LEFT_CURLY_BRACKET)
1415    {    {
1416    if (ptr[1] == '^')    if (ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1417      {      {
1418      *negptr = TRUE;      *negptr = TRUE;
1419      ptr++;      ptr++;
1420      }      }
1421    for (i = 0; i < (int)sizeof(name) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1422      {      {
1423      c = *(++ptr);      c = *(++ptr);
1424      if (c == 0) goto ERROR_RETURN;      if (c == CHAR_NULL) goto ERROR_RETURN;
1425      if (c == '}') break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1426      name[i] = c;      name[i] = c;
1427      }      }
1428    if (c !='}') goto ERROR_RETURN;    if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN;
1429    name[i] = 0;    name[i] = 0;
1430    }    }
1431    
# Line 834  else Line 1442  else
1442  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1443    
1444  bot = 0;  bot = 0;
1445  top = _pcre_utt_size;  top = PRIV(utt_size);
1446    
1447  while (bot < top)  while (bot < top)
1448    {    {
1449      int r;
1450    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1451    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1452    if (c == 0)    if (r == 0)
1453      {      {
1454      *dptr = _pcre_utt[i].value;      *ptypeptr = PRIV(utt)[i].type;
1455      return _pcre_utt[i].type;      *pdataptr = PRIV(utt)[i].value;
1456        return TRUE;
1457      }      }
1458    if (c > 0) bot = i + 1; else top = i;    if (r > 0) bot = i + 1; else top = i;
1459    }    }
1460    
1461  *errorcodeptr = ERR47;  *errorcodeptr = ERR47;
1462  *ptrptr = ptr;  *ptrptr = ptr;
1463  return -1;  return FALSE;
1464    
1465  ERROR_RETURN:  ERROR_RETURN:
1466  *errorcodeptr = ERR46;  *errorcodeptr = ERR46;
1467  *ptrptr = ptr;  *ptrptr = ptr;
1468  return -1;  return FALSE;
1469  }  }
1470  #endif  #endif
1471    
1472    
1473    
   
 /*************************************************  
 *            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 == '}');  
 }  
   
   
   
1474  /*************************************************  /*************************************************
1475  *         Read repeat counts                     *  *         Read repeat counts                     *
1476  *************************************************/  *************************************************/
# Line 914  Returns:         pointer to '}' on succe Line 1490  Returns:         pointer to '}' on succe
1490                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1491  */  */
1492    
1493  static const uschar *  static const pcre_uchar *
1494  read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)  read_repeat_counts(const pcre_uchar *p, int *minp, int *maxp, int *errorcodeptr)
1495  {  {
1496  int min = 0;  int min = 0;
1497  int max = -1;  int max = -1;
# Line 923  int max = -1; Line 1499  int max = -1;
1499  /* Read the minimum value and do a paranoid check: a negative value indicates  /* Read the minimum value and do a paranoid check: a negative value indicates
1500  an integer overflow. */  an integer overflow. */
1501    
1502  while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - '0';  while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);
1503  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1504    {    {
1505    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 933  if (min < 0 || min > 65535) Line 1509  if (min < 0 || min > 65535)
1509  /* 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.
1510  Also, max must not be less than min. */  Also, max must not be less than min. */
1511    
1512  if (*p == '}') max = min; else  if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
1513    {    {
1514    if (*(++p) != '}')    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1515      {      {
1516      max = 0;      max = 0;
1517      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - '0';      while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);
1518      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1519        {        {
1520        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 963  return p; Line 1539  return p;
1539    
1540    
1541  /*************************************************  /*************************************************
1542  *       Find forward referenced subpattern       *  *      Find first significant op code            *
1543  *************************************************/  *************************************************/
1544    
1545  /* This function scans along a pattern's text looking for capturing  /* This is called by several functions that scan a compiled expression looking
1546  subpatterns, and counting them. If it finds a named pattern that matches the  for a fixed first character, or an anchoring op code etc. It skips over things
1547  name it is given, it returns its number. Alternatively, if the name is NULL, it  that do not influence this. For some calls, it makes sense to skip negative
1548  returns when it reaches a given numbered subpattern. This is used for forward  forward and all backward assertions, and also the \b assertion; for others it
1549  references to subpatterns. We know that if (?P< is encountered, the name will  does not.
 be terminated by '>' because that is checked in the first pass.  
1550    
1551  Arguments:  Arguments:
1552    ptr          current position in the pattern    code         pointer to the start of the group
1553    count        current count of capturing parens so far encountered    skipassert   TRUE if certain assertions are to be skipped
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
1554    
1555  Returns:       the number of the named subpattern, or -1 if not found  Returns:       pointer to the first significant opcode
1556  */  */
1557    
1558  static int  static const pcre_uchar*
1559  find_parens(const uschar *ptr, int count, const uschar *name, int lorn,  first_significant_code(const pcre_uchar *code, BOOL skipassert)
   BOOL xmode)  
1560  {  {
1561  const uschar *thisname;  for (;;)
   
 for (; *ptr != 0; ptr++)  
1562    {    {
1563    int term;    switch ((int)*code)
1564        {
1565        case OP_ASSERT_NOT:
1566        case OP_ASSERTBACK:
1567        case OP_ASSERTBACK_NOT:
1568        if (!skipassert) return code;
1569        do code += GET(code, 1); while (*code == OP_ALT);
1570        code += PRIV(OP_lengths)[*code];
1571        break;
1572    
1573    /* Skip over backslashed characters and also entire \Q...\E */      case OP_WORD_BOUNDARY:
1574        case OP_NOT_WORD_BOUNDARY:
1575        if (!skipassert) return code;
1576        /* Fall through */
1577    
1578    if (*ptr == '\\')      case OP_CALLOUT:
1579      {      case OP_CREF:
1580      if (*(++ptr) == 0) return -1;      case OP_DNCREF:
1581      if (*ptr == 'Q') for (;;)      case OP_RREF:
1582        {      case OP_DNRREF:
1583        while (*(++ptr) != 0 && *ptr != '\\');      case OP_DEF:
1584        if (*ptr == 0) return -1;      code += PRIV(OP_lengths)[*code];
1585        if (*(++ptr) == 'E') break;      break;
1586        }  
1587      continue;      default:
1588        return code;
1589      }      }
1590      }
1591    /* Skip over character classes */  /* Control never reaches here */
1592    }
   if (*ptr == '[')  
     {  
     while (*(++ptr) != ']')  
       {  
       if (*ptr == 0) return -1;  
       if (*ptr == '\\')  
         {  
         if (*(++ptr) == 0) return -1;  
         if (*ptr == 'Q') for (;;)  
           {  
           while (*(++ptr) != 0 && *ptr != '\\');  
           if (*ptr == 0) return -1;  
           if (*(++ptr) == 'E') break;  
           }  
         continue;  
         }  
       }  
     continue;  
     }  
   
   /* Skip comments in /x mode */  
   
   if (xmode && *ptr == '#')  
     {  
     while (*(++ptr) != 0 && *ptr != '\n');  
     if (*ptr == 0) return -1;  
     continue;  
     }  
   
   /* An opening parens must now be a real metacharacter */  
   
   if (*ptr != '(') continue;  
   if (ptr[1] != '?' && ptr[1] != '*')  
     {  
     count++;  
     if (name == NULL && count == lorn) return count;  
     continue;  
     }  
   
   ptr += 2;  
   if (*ptr == 'P') ptr++;                      /* Allow optional P */  
   
   /* We have to disambiguate (?<! and (?<= from (?<name> */  
   
   if ((*ptr != '<' || ptr[1] == '!' || ptr[1] == '=') &&  
        *ptr != '\'')  
     continue;  
   
   count++;  
   
   if (name == NULL && count == lorn) return count;  
   term = *ptr++;  
   if (term == '<') term = '>';  
   thisname = ptr;  
   while (*ptr != term) ptr++;  
   if (name != NULL && lorn == ptr - thisname &&  
       strncmp((const char *)name, (const char *)thisname, lorn) == 0)  
     return count;  
   }  
   
 return -1;  
 }  
   
   
   
 /*************************************************  
 *      Find first significant op code            *  
 *************************************************/  
   
 /* This is called by several functions that scan a compiled expression looking  
 for a fixed first character, or an anchoring op code etc. It skips over things  
 that do not influence this. For some calls, a change of option is important.  
 For some calls, it makes sense to skip negative forward and all backward  
 assertions, and also the \b assertion; for others it does not.  
   
 Arguments:  
   code         pointer to the start of the group  
   options      pointer to external options  
   optbit       the option bit whose changing is significant, or  
                  zero if none are  
   skipassert   TRUE if certain assertions are to be skipped  
   
 Returns:       pointer to the first significant opcode  
 */  
   
 static const uschar*  
 first_significant_code(const uschar *code, int *options, int optbit,  
   BOOL skipassert)  
 {  
 for (;;)  
   {  
   switch ((int)*code)  
     {  
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
     case OP_ASSERT_NOT:  
     case OP_ASSERTBACK:  
     case OP_ASSERTBACK_NOT:  
     if (!skipassert) return code;  
     do code += GET(code, 1); while (*code == OP_ALT);  
     code += _pcre_OP_lengths[*code];  
     break;  
   
     case OP_WORD_BOUNDARY:  
     case OP_NOT_WORD_BOUNDARY:  
     if (!skipassert) return code;  
     /* Fall through */  
   
     case OP_CALLOUT:  
     case OP_CREF:  
     case OP_RREF:  
     case OP_DEF:  
     code += _pcre_OP_lengths[*code];  
     break;  
   
     default:  
     return code;  
     }  
   }  
 /* Control never reaches here */  
 }  
   
1593    
1594    
1595    
1596  /*************************************************  /*************************************************
1597  *        Find the fixed length of a pattern      *  *        Find the fixed length of a branch       *
1598  *************************************************/  *************************************************/
1599    
1600  /* 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,
1601  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.
1602  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
1603    temporarily terminated with OP_END when this function is called.
1604    
1605    This function is called when a backward assertion is encountered, so that if it
1606    fails, the error message can point to the correct place in the pattern.
1607    However, we cannot do this when the assertion contains subroutine calls,
1608    because they can be forward references. We solve this by remembering this case
1609    and doing the check at the end; a flag specifies which mode we are running in.
1610    
1611  Arguments:  Arguments:
1612    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1613    options  the compiling options    utf      TRUE in UTF-8 / UTF-16 / UTF-32 mode
1614      atend    TRUE if called when the pattern is complete
1615  Returns:   the fixed length, or -1 if there is no fixed length,    cd       the "compile data" structure
1616               or -2 if \C was encountered  
1617    Returns:   the fixed length,
1618                 or -1 if there is no fixed length,
1619                 or -2 if \C was encountered (in UTF-8 mode only)
1620                 or -3 if an OP_RECURSE item was encountered and atend is FALSE
1621                 or -4 if an unknown opcode was encountered (internal error)
1622  */  */
1623    
1624  static int  static int
1625  find_fixedlength(uschar *code, int options)  find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1626  {  {
1627  int length = -1;  int length = -1;
1628    
1629  register int branchlength = 0;  register int branchlength = 0;
1630  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1631    
1632  /* Scan along the opcodes for this branch. If we get to the end of the  /* Scan along the opcodes for this branch. If we get to the end of the
1633  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1168  branch, check the length against that of Line 1635  branch, check the length against that of
1635  for (;;)  for (;;)
1636    {    {
1637    int d;    int d;
1638    register int op = *cc;    pcre_uchar *ce, *cs;
1639      register pcre_uchar op = *cc;
1640    
1641    switch (op)    switch (op)
1642      {      {
1643        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1644        OP_BRA (normal non-capturing bracket) because the other variants of these
1645        opcodes are all concerned with unlimited repeated groups, which of course
1646        are not of fixed length. */
1647    
1648      case OP_CBRA:      case OP_CBRA:
1649      case OP_BRA:      case OP_BRA:
1650      case OP_ONCE:      case OP_ONCE:
1651        case OP_ONCE_NC:
1652      case OP_COND:      case OP_COND:
1653      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options);      d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd);
1654      if (d < 0) return d;      if (d < 0) return d;
1655      branchlength += d;      branchlength += d;
1656      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1657      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1658      break;      break;
1659    
1660      /* 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.
1661      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
1662      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
1663        the same code. Note that we must not include the OP_KETRxxx opcodes here,
1664        because they all imply an unlimited repeat. */
1665    
1666      case OP_ALT:      case OP_ALT:
1667      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1668      case OP_END:      case OP_END:
1669        case OP_ACCEPT:
1670        case OP_ASSERT_ACCEPT:
1671      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1672        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
1673      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
# Line 1198  for (;;) Line 1675  for (;;)
1675      branchlength = 0;      branchlength = 0;
1676      break;      break;
1677    
1678        /* A true recursion implies not fixed length, but a subroutine call may
1679        be OK. If the subroutine is a forward reference, we can't deal with
1680        it until the end of the pattern, so return -3. */
1681    
1682        case OP_RECURSE:
1683        if (!atend) return -3;
1684        cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1685        do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1686        if (cc > cs && cc < ce) return -1;                    /* Recursion */
1687        d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd);
1688        if (d < 0) return d;
1689        branchlength += d;
1690        cc += 1 + LINK_SIZE;
1691        break;
1692    
1693      /* Skip over assertive subpatterns */      /* Skip over assertive subpatterns */
1694    
1695      case OP_ASSERT:      case OP_ASSERT:
# Line 1205  for (;;) Line 1697  for (;;)
1697      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1698      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1699      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1700      /* Fall through */      cc += PRIV(OP_lengths)[*cc];
1701        break;
1702    
1703      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1704    
1705      case OP_REVERSE:      case OP_MARK:
1706        case OP_PRUNE_ARG:
1707        case OP_SKIP_ARG:
1708        case OP_THEN_ARG:
1709        cc += cc[1] + PRIV(OP_lengths)[*cc];
1710        break;
1711    
1712        case OP_CALLOUT:
1713        case OP_CIRC:
1714        case OP_CIRCM:
1715        case OP_CLOSE:
1716        case OP_COMMIT:
1717      case OP_CREF:      case OP_CREF:
     case OP_RREF:  
1718      case OP_DEF:      case OP_DEF:
1719      case OP_OPT:      case OP_DNCREF:
1720      case OP_CALLOUT:      case OP_DNRREF:
1721      case OP_SOD:      case OP_DOLL:
1722      case OP_SOM:      case OP_DOLLM:
1723      case OP_EOD:      case OP_EOD:
1724      case OP_EODN:      case OP_EODN:
1725      case OP_CIRC:      case OP_FAIL:
     case OP_DOLL:  
1726      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1727        case OP_PRUNE:
1728        case OP_REVERSE:
1729        case OP_RREF:
1730        case OP_SET_SOM:
1731        case OP_SKIP:
1732        case OP_SOD:
1733        case OP_SOM:
1734        case OP_THEN:
1735      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1736      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1737      break;      break;
1738    
1739      /* Handle literal characters */      /* Handle literal characters */
1740    
1741      case OP_CHAR:      case OP_CHAR:
1742      case OP_CHARNC:      case OP_CHARI:
1743      case OP_NOT:      case OP_NOT:
1744        case OP_NOTI:
1745      branchlength++;      branchlength++;
1746      cc += 2;      cc += 2;
1747  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1748      if ((options & PCRE_UTF8) != 0)      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
       {  
       while ((*cc & 0xc0) == 0x80) cc++;  
       }  
1749  #endif  #endif
1750      break;      break;
1751    
# Line 1245  for (;;) Line 1753  for (;;)
1753      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
1754    
1755      case OP_EXACT:      case OP_EXACT:
1756      branchlength += GET2(cc,1);      case OP_EXACTI:
1757      cc += 4;      case OP_NOTEXACT:
1758  #ifdef SUPPORT_UTF8      case OP_NOTEXACTI:
1759      if ((options & PCRE_UTF8) != 0)      branchlength += (int)GET2(cc,1);
1760        {      cc += 2 + IMM2_SIZE;
1761        while((*cc & 0x80) == 0x80) cc++;  #ifdef SUPPORT_UTF
1762        }      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1763  #endif  #endif
1764      break;      break;
1765    
1766      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1767      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1768      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1769      cc += 4;        cc += 2;
1770        cc += 1 + IMM2_SIZE + 1;
1771      break;      break;
1772    
1773      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1268  for (;;) Line 1777  for (;;)
1777      cc += 2;      cc += 2;
1778      /* Fall through */      /* Fall through */
1779    
1780        case OP_HSPACE:
1781        case OP_VSPACE:
1782        case OP_NOT_HSPACE:
1783        case OP_NOT_VSPACE:
1784      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
1785      case OP_DIGIT:      case OP_DIGIT:
1786      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
# Line 1275  for (;;) Line 1788  for (;;)
1788      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
1789      case OP_WORDCHAR:      case OP_WORDCHAR:
1790      case OP_ANY:      case OP_ANY:
1791        case OP_ALLANY:
1792      branchlength++;      branchlength++;
1793      cc++;      cc++;
1794      break;      break;
1795    
1796      /* The single-byte matcher isn't allowed */      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1797        otherwise \C is coded as OP_ALLANY. */
1798    
1799      case OP_ANYBYTE:      case OP_ANYBYTE:
1800      return -2;      return -2;
1801    
1802      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1803    
 #ifdef SUPPORT_UTF8  
     case OP_XCLASS:  
     cc += GET(cc, 1) - 33;  
     /* Fall through */  
 #endif  
   
1804      case OP_CLASS:      case OP_CLASS:
1805      case OP_NCLASS:      case OP_NCLASS:
1806      cc += 33;  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1807        case OP_XCLASS:
1808        /* The original code caused an unsigned overflow in 64 bit systems,
1809        so now we use a conditional statement. */
1810        if (op == OP_XCLASS)
1811          cc += GET(cc, 1);
1812        else
1813          cc += PRIV(OP_lengths)[OP_CLASS];
1814    #else
1815        cc += PRIV(OP_lengths)[OP_CLASS];
1816    #endif
1817    
1818      switch (*cc)      switch (*cc)
1819        {        {
1820        case OP_CRSTAR:        case OP_CRSTAR:
1821        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1822          case OP_CRPLUS:
1823          case OP_CRMINPLUS:
1824        case OP_CRQUERY:        case OP_CRQUERY:
1825        case OP_CRMINQUERY:        case OP_CRMINQUERY:
1826          case OP_CRPOSSTAR:
1827          case OP_CRPOSPLUS:
1828          case OP_CRPOSQUERY:
1829        return -1;        return -1;
1830    
1831        case OP_CRRANGE:        case OP_CRRANGE:
1832        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1833        if (GET2(cc,1) != GET2(cc,3)) return -1;        case OP_CRPOSRANGE:
1834        branchlength += GET2(cc,1);        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1835        cc += 5;        branchlength += (int)GET2(cc,1);
1836          cc += 1 + 2 * IMM2_SIZE;
1837        break;        break;
1838    
1839        default:        default:
# Line 1318  for (;;) Line 1843  for (;;)
1843    
1844      /* Anything else is variable length */      /* Anything else is variable length */
1845    
1846      default:      case OP_ANYNL:
1847        case OP_BRAMINZERO:
1848        case OP_BRAPOS:
1849        case OP_BRAPOSZERO:
1850        case OP_BRAZERO:
1851        case OP_CBRAPOS:
1852        case OP_EXTUNI:
1853        case OP_KETRMAX:
1854        case OP_KETRMIN:
1855        case OP_KETRPOS:
1856        case OP_MINPLUS:
1857        case OP_MINPLUSI:
1858        case OP_MINQUERY:
1859        case OP_MINQUERYI:
1860        case OP_MINSTAR:
1861        case OP_MINSTARI:
1862        case OP_MINUPTO:
1863        case OP_MINUPTOI:
1864        case OP_NOTMINPLUS:
1865        case OP_NOTMINPLUSI:
1866        case OP_NOTMINQUERY:
1867        case OP_NOTMINQUERYI:
1868        case OP_NOTMINSTAR:
1869        case OP_NOTMINSTARI:
1870        case OP_NOTMINUPTO:
1871        case OP_NOTMINUPTOI:
1872        case OP_NOTPLUS:
1873        case OP_NOTPLUSI:
1874        case OP_NOTPOSPLUS:
1875        case OP_NOTPOSPLUSI:
1876        case OP_NOTPOSQUERY:
1877        case OP_NOTPOSQUERYI:
1878        case OP_NOTPOSSTAR:
1879        case OP_NOTPOSSTARI:
1880        case OP_NOTPOSUPTO:
1881        case OP_NOTPOSUPTOI:
1882        case OP_NOTQUERY:
1883        case OP_NOTQUERYI:
1884        case OP_NOTSTAR:
1885        case OP_NOTSTARI:
1886        case OP_NOTUPTO:
1887        case OP_NOTUPTOI:
1888        case OP_PLUS:
1889        case OP_PLUSI:
1890        case OP_POSPLUS:
1891        case OP_POSPLUSI:
1892        case OP_POSQUERY:
1893        case OP_POSQUERYI:
1894        case OP_POSSTAR:
1895        case OP_POSSTARI:
1896        case OP_POSUPTO:
1897        case OP_POSUPTOI:
1898        case OP_QUERY:
1899        case OP_QUERYI:
1900        case OP_REF:
1901        case OP_REFI:
1902        case OP_DNREF:
1903        case OP_DNREFI:
1904        case OP_SBRA:
1905        case OP_SBRAPOS:
1906        case OP_SCBRA:
1907        case OP_SCBRAPOS:
1908        case OP_SCOND:
1909        case OP_SKIPZERO:
1910        case OP_STAR:
1911        case OP_STARI:
1912        case OP_TYPEMINPLUS:
1913        case OP_TYPEMINQUERY:
1914        case OP_TYPEMINSTAR:
1915        case OP_TYPEMINUPTO:
1916        case OP_TYPEPLUS:
1917        case OP_TYPEPOSPLUS:
1918        case OP_TYPEPOSQUERY:
1919        case OP_TYPEPOSSTAR:
1920        case OP_TYPEPOSUPTO:
1921        case OP_TYPEQUERY:
1922        case OP_TYPESTAR:
1923        case OP_TYPEUPTO:
1924        case OP_UPTO:
1925        case OP_UPTOI:
1926      return -1;      return -1;
1927    
1928        /* Catch unrecognized opcodes so that when new ones are added they
1929        are not forgotten, as has happened in the past. */
1930    
1931        default:
1932        return -4;
1933      }      }
1934    }    }
1935  /* Control never gets here */  /* Control never gets here */
# Line 1327  for (;;) Line 1937  for (;;)
1937    
1938    
1939    
   
1940  /*************************************************  /*************************************************
1941  *    Scan compiled regex for numbered bracket    *  *    Scan compiled regex for specific bracket    *
1942  *************************************************/  *************************************************/
1943    
1944  /* This little function scans through a compiled pattern until it finds a  /* This little function scans through a compiled pattern until it finds a
1945  capturing bracket with the given number.  capturing bracket with the given number, or, if the number is negative, an
1946    instance of OP_REVERSE for a lookbehind. The function is global in the C sense
1947    so that it can be called from pcre_study() when finding the minimum matching
1948    length.
1949    
1950  Arguments:  Arguments:
1951    code        points to start of expression    code        points to start of expression
1952    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1953    number      the required bracket number    number      the required bracket number or negative to find a lookbehind
1954    
1955  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
1956  */  */
1957    
1958  static const uschar *  const pcre_uchar *
1959  find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
1960  {  {
1961  for (;;)  for (;;)
1962    {    {
1963    register int c = *code;    register pcre_uchar c = *code;
1964    
1965    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1966    
1967    /* 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 1357  for (;;) Line 1970  for (;;)
1970    
1971    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
1972    
1973      /* Handle recursion */
1974    
1975      else if (c == OP_REVERSE)
1976        {
1977        if (number < 0) return (pcre_uchar *)code;
1978        code += PRIV(OP_lengths)[c];
1979        }
1980    
1981    /* Handle capturing bracket */    /* Handle capturing bracket */
1982    
1983    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1984               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1985      {      {
1986      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1987      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1988      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1989      }      }
1990    
1991    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
1992    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
1993    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1994      must add in its length. */
1995    
1996    else    else
1997      {      {
# Line 1390  for (;;) Line 2013  for (;;)
2013        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2014        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2015        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
2016        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2017            code += 2;
2018          break;
2019    
2020          case OP_MARK:
2021          case OP_PRUNE_ARG:
2022          case OP_SKIP_ARG:
2023          case OP_THEN_ARG:
2024          code += code[1];
2025        break;        break;
2026        }        }
2027    
2028      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2029    
2030      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2031    
2032    /* 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
2033    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
2034    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
2035    
2036  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2037      if (utf8) switch(c)      if (utf) switch(c)
2038        {        {
2039        case OP_CHAR:        case OP_CHAR:
2040        case OP_CHARNC:        case OP_CHARI:
2041        case OP_EXACT:        case OP_EXACT:
2042          case OP_EXACTI:
2043        case OP_UPTO:        case OP_UPTO:
2044          case OP_UPTOI:
2045        case OP_MINUPTO:        case OP_MINUPTO:
2046          case OP_MINUPTOI:
2047        case OP_POSUPTO:        case OP_POSUPTO:
2048          case OP_POSUPTOI:
2049        case OP_STAR:        case OP_STAR:
2050          case OP_STARI:
2051        case OP_MINSTAR:        case OP_MINSTAR:
2052          case OP_MINSTARI:
2053        case OP_POSSTAR:        case OP_POSSTAR:
2054          case OP_POSSTARI:
2055        case OP_PLUS:        case OP_PLUS:
2056          case OP_PLUSI:
2057        case OP_MINPLUS:        case OP_MINPLUS:
2058          case OP_MINPLUSI:
2059        case OP_POSPLUS:        case OP_POSPLUS:
2060          case OP_POSPLUSI:
2061        case OP_QUERY:        case OP_QUERY:
2062          case OP_QUERYI:
2063        case OP_MINQUERY:        case OP_MINQUERY:
2064          case OP_MINQUERYI:
2065        case OP_POSQUERY:        case OP_POSQUERY:
2066        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2067          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2068        break;        break;
2069        }        }
2070    #else
2071        (void)(utf);  /* Keep compiler happy by referencing function argument */
2072  #endif  #endif
2073      }      }
2074    }    }
# Line 1439  instance of OP_RECURSE. Line 2085  instance of OP_RECURSE.
2085    
2086  Arguments:  Arguments:
2087    code        points to start of expression    code        points to start of expression
2088    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2089    
2090  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
2091  */  */
2092    
2093  static const uschar *  static const pcre_uchar *
2094  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
2095  {  {
2096  for (;;)  for (;;)
2097    {    {
2098    register int c = *code;    register pcre_uchar c = *code;
2099    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2100    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
2101    
# Line 1461  for (;;) Line 2107  for (;;)
2107    
2108    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
2109    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
2110    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2111      must add in its length. */
2112    
2113    else    else
2114      {      {
# Line 1483  for (;;) Line 2130  for (;;)
2130        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2131        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2132        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2133        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2134            code += 2;
2135          break;
2136    
2137          case OP_MARK:
2138          case OP_PRUNE_ARG:
2139          case OP_SKIP_ARG:
2140          case OP_THEN_ARG:
2141          code += code[1];
2142        break;        break;
2143        }        }
2144    
2145      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2146    
2147      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2148    
2149      /* In UTF-8 mode, opcodes that are followed by a character may be followed      /* In UTF-8 mode, opcodes that are followed by a character may be followed
2150      by a multi-byte character. The length in the table is a minimum, so we have      by a multi-byte character. The length in the table is a minimum, so we have
2151      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
2152    
2153  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2154      if (utf8) switch(c)      if (utf) switch(c)
2155        {        {
2156        case OP_CHAR:        case OP_CHAR:
2157        case OP_CHARNC:        case OP_CHARI:
2158          case OP_NOT:
2159          case OP_NOTI:
2160        case OP_EXACT:        case OP_EXACT:
2161          case OP_EXACTI:
2162          case OP_NOTEXACT:
2163          case OP_NOTEXACTI:
2164        case OP_UPTO:        case OP_UPTO:
2165          case OP_UPTOI:
2166          case OP_NOTUPTO:
2167          case OP_NOTUPTOI:
2168        case OP_MINUPTO:        case OP_MINUPTO:
2169          case OP_MINUPTOI:
2170          case OP_NOTMINUPTO:
2171          case OP_NOTMINUPTOI:
2172        case OP_POSUPTO:        case OP_POSUPTO:
2173          case OP_POSUPTOI:
2174          case OP_NOTPOSUPTO:
2175          case OP_NOTPOSUPTOI:
2176        case OP_STAR:        case OP_STAR:
2177          case OP_STARI:
2178          case OP_NOTSTAR:
2179          case OP_NOTSTARI:
2180        case OP_MINSTAR:        case OP_MINSTAR:
2181          case OP_MINSTARI:
2182          case OP_NOTMINSTAR:
2183          case OP_NOTMINSTARI:
2184        case OP_POSSTAR:        case OP_POSSTAR:
2185          case OP_POSSTARI:
2186          case OP_NOTPOSSTAR:
2187          case OP_NOTPOSSTARI:
2188        case OP_PLUS:        case OP_PLUS:
2189          case OP_PLUSI:
2190          case OP_NOTPLUS:
2191          case OP_NOTPLUSI:
2192        case OP_MINPLUS:        case OP_MINPLUS:
2193          case OP_MINPLUSI:
2194          case OP_NOTMINPLUS:
2195          case OP_NOTMINPLUSI:
2196        case OP_POSPLUS:        case OP_POSPLUS:
2197          case OP_POSPLUSI:
2198          case OP_NOTPOSPLUS:
2199          case OP_NOTPOSPLUSI:
2200        case OP_QUERY:        case OP_QUERY:
2201          case OP_QUERYI:
2202          case OP_NOTQUERY:
2203          case OP_NOTQUERYI:
2204        case OP_MINQUERY:        case OP_MINQUERY:
2205          case OP_MINQUERYI:
2206          case OP_NOTMINQUERY:
2207          case OP_NOTMINQUERYI:
2208        case OP_POSQUERY:        case OP_POSQUERY:
2209        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2210          case OP_NOTPOSQUERY:
2211          case OP_NOTPOSQUERYI:
2212          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2213        break;        break;
2214        }        }
2215    #else
2216        (void)(utf);  /* Keep compiler happy by referencing function argument */
2217  #endif  #endif
2218      }      }
2219    }    }
# Line 1538  bracket whose current branch will alread Line 2236  bracket whose current branch will alread
2236  Arguments:  Arguments:
2237    code        points to start of search    code        points to start of search
2238    endcode     points to where to stop    endcode     points to where to stop
2239    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2240      cd          contains pointers to tables etc.
2241      recurses    chain of recurse_check to catch mutual recursion
2242    
2243  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2244  */  */
2245    
2246    typedef struct recurse_check {
2247      struct recurse_check *prev;
2248      const pcre_uchar *group;
2249    } recurse_check;
2250    
2251  static BOOL  static BOOL
2252  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2253      BOOL utf, compile_data *cd, recurse_check *recurses)
2254  {  {
2255  register int c;  register pcre_uchar c;
2256  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  recurse_check this_recurse;
2257    
2258    for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2259       code < endcode;       code < endcode;
2260       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2261    {    {
2262    const uschar *ccode;    const pcre_uchar *ccode;
2263    
2264    c = *code;    c = *code;
2265    
# Line 1565  for (code = first_significant_code(code Line 2273  for (code = first_significant_code(code
2273      continue;      continue;
2274      }      }
2275    
2276      /* For a recursion/subroutine call, if its end has been reached, which
2277      implies a backward reference subroutine call, we can scan it. If it's a
2278      forward reference subroutine call, we can't. To detect forward reference
2279      we have to scan up the list that is kept in the workspace. This function is
2280      called only when doing the real compile, not during the pre-compile that
2281      measures the size of the compiled pattern. */
2282    
2283      if (c == OP_RECURSE)
2284        {
2285        const pcre_uchar *scode = cd->start_code + GET(code, 1);
2286        BOOL empty_branch;
2287    
2288        /* Test for forward reference or uncompleted reference. This is disabled
2289        when called to scan a completed pattern by setting cd->start_workspace to
2290        NULL. */
2291    
2292        if (cd->start_workspace != NULL)
2293          {
2294          const pcre_uchar *tcode;
2295          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2296            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2297          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2298          }
2299    
2300        /* If we are scanning a completed pattern, there are no forward references
2301        and all groups are complete. We need to detect whether this is a recursive
2302        call, as otherwise there will be an infinite loop. If it is a recursion,
2303        just skip over it. Simple recursions are easily detected. For mutual
2304        recursions we keep a chain on the stack. */
2305    
2306        else
2307          {
2308          recurse_check *r = recurses;
2309          const pcre_uchar *endgroup = scode;
2310    
2311          do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2312          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2313    
2314          for (r = recurses; r != NULL; r = r->prev)
2315            if (r->group == scode) break;
2316          if (r != NULL) continue;   /* Mutual recursion */
2317          }
2318    
2319        /* Completed reference; scan the referenced group, remembering it on the
2320        stack chain to detect mutual recursions. */
2321    
2322        empty_branch = FALSE;
2323        this_recurse.prev = recurses;
2324        this_recurse.group = scode;
2325    
2326        do
2327          {
2328          if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2329            {
2330            empty_branch = TRUE;
2331            break;
2332            }
2333          scode += GET(scode, 1);
2334          }
2335        while (*scode == OP_ALT);
2336    
2337        if (!empty_branch) return FALSE;  /* All branches are non-empty */
2338        continue;
2339        }
2340    
2341    /* Groups with zero repeats can of course be empty; skip them. */    /* Groups with zero repeats can of course be empty; skip them. */
2342    
2343    if (c == OP_BRAZERO || c == OP_BRAMINZERO)    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2344          c == OP_BRAPOSZERO)
2345        {
2346        code += PRIV(OP_lengths)[c];
2347        do code += GET(code, 1); while (*code == OP_ALT);
2348        c = *code;
2349        continue;
2350        }
2351    
2352      /* A nested group that is already marked as "could be empty" can just be
2353      skipped. */
2354    
2355      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2356          c == OP_SCBRA || c == OP_SCBRAPOS)
2357      {      {
     code += _pcre_OP_lengths[c];  
2358      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
2359      c = *code;      c = *code;
2360      continue;      continue;
# Line 1577  for (code = first_significant_code(code Line 2362  for (code = first_significant_code(code
2362    
2363    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
2364    
2365    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA  || c == OP_BRAPOS ||
2366          c == OP_CBRA || c == OP_CBRAPOS ||
2367          c == OP_ONCE || c == OP_ONCE_NC ||
2368          c == OP_COND)
2369      {      {
2370      BOOL empty_branch;      BOOL empty_branch;
2371      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
2372    
2373      /* Scan a closed bracket */      /* If a conditional group has only one branch, there is a second, implied,
2374        empty branch, so just skip over the conditional, because it could be empty.
2375        Otherwise, scan the individual branches of the group. */
2376    
2377      empty_branch = FALSE;      if (c == OP_COND && code[GET(code, 1)] != OP_ALT)
     do  
       {  
       if (!empty_branch && could_be_empty_branch(code, endcode, utf8))  
         empty_branch = TRUE;  
2378        code += GET(code, 1);        code += GET(code, 1);
2379        else
2380          {
2381          empty_branch = FALSE;
2382          do
2383            {
2384            if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2385              empty_branch = TRUE;
2386            code += GET(code, 1);
2387            }
2388          while (*code == OP_ALT);
2389          if (!empty_branch) return FALSE;   /* All branches are non-empty */
2390        }        }
2391      while (*code == OP_ALT);  
     if (!empty_branch) return FALSE;   /* All branches are non-empty */  
2392      c = *code;      c = *code;
2393      continue;      continue;
2394      }      }
# Line 1603  for (code = first_significant_code(code Line 2399  for (code = first_significant_code(code
2399      {      {
2400      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2401      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2402      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2403      actual length is stored in the compiled code, so we must update "code"      actual length is stored in the compiled code, so we must update "code"
2404      here. */      here. */
2405    
2406  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2407      case OP_XCLASS:      case OP_XCLASS:
2408      ccode = code += GET(code, 1);      ccode = code += GET(code, 1);
2409      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
# Line 1615  for (code = first_significant_code(code Line 2411  for (code = first_significant_code(code
2411    
2412      case OP_CLASS:      case OP_CLASS:
2413      case OP_NCLASS:      case OP_NCLASS:
2414      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2415    
2416  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2417      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
2418  #endif  #endif
2419    
# Line 1627  for (code = first_significant_code(code Line 2423  for (code = first_significant_code(code
2423        case OP_CRMINSTAR:        case OP_CRMINSTAR:
2424        case OP_CRQUERY:        case OP_CRQUERY:
2425        case OP_CRMINQUERY:        case OP_CRMINQUERY:
2426          case OP_CRPOSSTAR:
2427          case OP_CRPOSQUERY:
2428        break;        break;
2429    
2430        default:                   /* Non-repeat => class must match */        default:                   /* Non-repeat => class must match */
2431        case OP_CRPLUS:            /* These repeats aren't empty */        case OP_CRPLUS:            /* These repeats aren't empty */
2432        case OP_CRMINPLUS:        case OP_CRMINPLUS:
2433          case OP_CRPOSPLUS:
2434        return FALSE;        return FALSE;
2435    
2436        case OP_CRRANGE:        case OP_CRRANGE:
2437        case OP_CRMINRANGE:        case OP_CRMINRANGE:
2438          case OP_CRPOSRANGE:
2439        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */
2440        break;        break;
2441        }        }
# Line 1643  for (code = first_significant_code(code Line 2443  for (code = first_significant_code(code
2443    
2444      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2445    
2446        case OP_ANY:
2447        case OP_ALLANY:
2448        case OP_ANYBYTE:
2449    
2450      case OP_PROP:      case OP_PROP:
2451      case OP_NOTPROP:      case OP_NOTPROP:
2452        case OP_ANYNL:
2453    
2454        case OP_NOT_HSPACE:
2455        case OP_HSPACE:
2456        case OP_NOT_VSPACE:
2457        case OP_VSPACE:
2458      case OP_EXTUNI:      case OP_EXTUNI:
2459    
2460      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2461      case OP_DIGIT:      case OP_DIGIT:
2462      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2463      case OP_WHITESPACE:      case OP_WHITESPACE:
2464      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2465      case OP_WORDCHAR:      case OP_WORDCHAR:
2466      case OP_ANY:  
     case OP_ANYBYTE:  
2467      case OP_CHAR:      case OP_CHAR:
2468      case OP_CHARNC:      case OP_CHARI:
2469      case OP_NOT:      case OP_NOT:
2470        case OP_NOTI:
2471    
2472      case OP_PLUS:      case OP_PLUS:
2473        case OP_PLUSI:
2474      case OP_MINPLUS:      case OP_MINPLUS:
2475      case OP_POSPLUS:      case OP_MINPLUSI:
2476      case OP_EXACT:  
2477      case OP_NOTPLUS:      case OP_NOTPLUS:
2478        case OP_NOTPLUSI:
2479      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2480        case OP_NOTMINPLUSI:
2481    
2482        case OP_POSPLUS:
2483        case OP_POSPLUSI:
2484      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2485        case OP_NOTPOSPLUSI:
2486    
2487        case OP_EXACT:
2488        case OP_EXACTI:
2489      case OP_NOTEXACT:      case OP_NOTEXACT:
2490        case OP_NOTEXACTI:
2491    
2492      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2493      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2494      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2495      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2496    
2497      return FALSE;      return FALSE;
2498    
2499      /* These are going to continue, as they may be empty, but we have to      /* These are going to continue, as they may be empty, but we have to
# Line 1688  for (code = first_significant_code(code Line 2513  for (code = first_significant_code(code
2513      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2514      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2515      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2516      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2517          code += 2;
2518      break;      break;
2519    
2520      /* End of branch */      /* End of branch */
# Line 1696  for (code = first_significant_code(code Line 2522  for (code = first_significant_code(code
2522      case OP_KET:      case OP_KET:
2523      case OP_KETRMAX:      case OP_KETRMAX:
2524      case OP_KETRMIN:      case OP_KETRMIN:
2525        case OP_KETRPOS:
2526      case OP_ALT:      case OP_ALT:
2527      return TRUE;      return TRUE;
2528    
2529      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2530      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2531        followed by a multibyte character. */
2532    
2533  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2534      case OP_STAR:      case OP_STAR:
2535        case OP_STARI:
2536        case OP_NOTSTAR:
2537        case OP_NOTSTARI:
2538    
2539      case OP_MINSTAR:      case OP_MINSTAR:
2540        case OP_MINSTARI:
2541        case OP_NOTMINSTAR:
2542        case OP_NOTMINSTARI:
2543    
2544      case OP_POSSTAR:      case OP_POSSTAR:
2545        case OP_POSSTARI:
2546        case OP_NOTPOSSTAR:
2547        case OP_NOTPOSSTARI:
2548    
2549      case OP_QUERY:      case OP_QUERY:
2550        case OP_QUERYI:
2551        case OP_NOTQUERY:
2552        case OP_NOTQUERYI:
2553    
2554      case OP_MINQUERY:      case OP_MINQUERY:
2555        case OP_MINQUERYI:
2556        case OP_NOTMINQUERY:
2557        case OP_NOTMINQUERYI:
2558    
2559      case OP_POSQUERY:      case OP_POSQUERY:
2560        case OP_POSQUERYI:
2561        case OP_NOTPOSQUERY:
2562        case OP_NOTPOSQUERYI:
2563    
2564        if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2565        break;
2566    
2567      case OP_UPTO:      case OP_UPTO:
2568        case OP_UPTOI:
2569        case OP_NOTUPTO:
2570        case OP_NOTUPTOI:
2571    
2572      case OP_MINUPTO:      case OP_MINUPTO:
2573        case OP_MINUPTOI:
2574        case OP_NOTMINUPTO:
2575        case OP_NOTMINUPTOI:
2576    
2577      case OP_POSUPTO:      case OP_POSUPTO:
2578      if (utf8) while ((code[2] & 0xc0) == 0x80) code++;      case OP_POSUPTOI:
2579        case OP_NOTPOSUPTO:
2580        case OP_NOTPOSUPTOI:
2581    
2582        if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2583      break;      break;
2584  #endif  #endif
2585    
2586        /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2587        string. */
2588    
2589        case OP_MARK:
2590        case OP_PRUNE_ARG:
2591        case OP_SKIP_ARG:
2592        case OP_THEN_ARG:
2593        code += code[1];
2594        break;
2595    
2596        /* None of the remaining opcodes are required to match a character. */
2597    
2598        default:
2599        break;
2600      }      }
2601    }    }
2602    
# Line 1731  return TRUE; Line 2613  return TRUE;
2613  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
2614  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,
2615  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.
2616    This function is called only during the real compile, not during the
2617    pre-compile.
2618    
2619  Arguments:  Arguments:
2620    code        points to start of the recursion    code        points to start of the recursion
2621    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2622    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2623    utf8        TRUE if in UTF-8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2624      cd          pointers to tables etc
2625    
2626  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2627  */  */
2628    
2629  static BOOL  static BOOL
2630  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2631    BOOL utf8)    branch_chain *bcptr, BOOL utf, compile_data *cd)
2632  {  {
2633  while (bcptr != NULL && bcptr->current >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2634    {    {
2635    if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2636        return FALSE;
2637    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2638    }    }
2639  return TRUE;  return TRUE;
# Line 1756  return TRUE; Line 2642  return TRUE;
2642    
2643    
2644  /*************************************************  /*************************************************
2645  *           Check for POSIX class syntax         *  *        Base opcode of repeated opcodes         *
2646  *************************************************/  *************************************************/
2647    
2648  /* This function is called when the sequence "[:" or "[." or "[=" is  /* Returns the base opcode for repeated single character type opcodes. If the
2649  encountered in a character class. It checks whether this is followed by a  opcode is not a repeated character type, it returns with the original value.
 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we  
 reach an unescaped ']' without the special preceding character, return FALSE.  
2650    
2651  Originally, this function only recognized a sequence of letters between the  Arguments:  c opcode
2652  terminators, but it seems that Perl recognizes any sequence of characters,  Returns:    base opcode for the type
2653    */
2654    
2655    static pcre_uchar
2656    get_repeat_base(pcre_uchar c)
2657    {
2658    return (c > OP_TYPEPOSUPTO)? c :
2659           (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2660           (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2661           (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2662           (c >= OP_STARI)?      OP_STARI :
2663                                 OP_STAR;
2664    }
2665    
2666    
2667    
2668    #ifdef SUPPORT_UCP
2669    /*************************************************
2670    *        Check a character and a property        *
2671    *************************************************/
2672    
2673    /* This function is called by check_auto_possessive() when a property item
2674    is adjacent to a fixed character.
2675    
2676    Arguments:
2677      c            the character
2678      ptype        the property type
2679      pdata        the data for the type
2680      negated      TRUE if it's a negated property (\P or \p{^)
2681    
2682    Returns:       TRUE if auto-possessifying is OK
2683    */
2684    
2685    static BOOL
2686    check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2687      BOOL negated)
2688    {
2689    const pcre_uint32 *p;
2690    const ucd_record *prop = GET_UCD(c);
2691    
2692    switch(ptype)
2693      {
2694      case PT_LAMP:
2695      return (prop->chartype == ucp_Lu ||
2696              prop->chartype == ucp_Ll ||
2697              prop->chartype == ucp_Lt) == negated;
2698    
2699      case PT_GC:
2700      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2701    
2702      case PT_PC:
2703      return (pdata == prop->chartype) == negated;
2704    
2705      case PT_SC:
2706      return (pdata == prop->script) == negated;
2707    
2708      /* These are specials */
2709    
2710      case PT_ALNUM:
2711      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2712              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2713    
2714      /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2715      means that Perl space and POSIX space are now identical. PCRE was changed
2716      at release 8.34. */
2717    
2718      case PT_SPACE:    /* Perl space */
2719      case PT_PXSPACE:  /* POSIX space */
2720      switch(c)
2721        {
2722        HSPACE_CASES:
2723        VSPACE_CASES:
2724        return negated;
2725    
2726        default:
2727        return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
2728        }
2729      break;  /* Control never reaches here */
2730    
2731      case PT_WORD:
2732      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2733              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2734              c == CHAR_UNDERSCORE) == negated;
2735    
2736      case PT_CLIST:
2737      p = PRIV(ucd_caseless_sets) + prop->caseset;
2738      for (;;)
2739        {
2740        if (c < *p) return !negated;
2741        if (c == *p++) return negated;
2742        }
2743      break;  /* Control never reaches here */
2744      }
2745    
2746    return FALSE;
2747    }
2748    #endif  /* SUPPORT_UCP */
2749    
2750    
2751    
2752    /*************************************************
2753    *        Fill the character property list        *
2754    *************************************************/
2755    
2756    /* Checks whether the code points to an opcode that can take part in auto-
2757    possessification, and if so, fills a list with its properties.
2758    
2759    Arguments:
2760      code        points to start of expression
2761      utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2762      fcc         points to case-flipping table
2763      list        points to output list
2764                  list[0] will be filled with the opcode
2765                  list[1] will be non-zero if this opcode
2766                    can match an empty character string
2767                  list[2..7] depends on the opcode
2768    
2769    Returns:      points to the start of the next opcode if *code is accepted
2770                  NULL if *code is not accepted
2771    */
2772    
2773    static const pcre_uchar *
2774    get_chr_property_list(const pcre_uchar *code, BOOL utf,
2775      const pcre_uint8 *fcc, pcre_uint32 *list)
2776    {
2777    pcre_uchar c = *code;
2778    const pcre_uchar *end;
2779    const pcre_uint32 *clist_src;
2780    pcre_uint32 *clist_dest;
2781    pcre_uint32 chr;
2782    pcre_uchar base;
2783    
2784    list[0] = c;
2785    list[1] = FALSE;
2786    code++;
2787    
2788    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2789      {
2790      base = get_repeat_base(c);
2791      c -= (base - OP_STAR);
2792    
2793      if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2794        code += IMM2_SIZE;
2795    
2796      list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
2797    
2798      switch(base)
2799        {
2800        case OP_STAR:
2801        list[0] = OP_CHAR;
2802        break;
2803    
2804        case OP_STARI:
2805        list[0] = OP_CHARI;
2806        break;
2807    
2808        case OP_NOTSTAR:
2809        list[0] = OP_NOT;
2810        break;
2811    
2812        case OP_NOTSTARI:
2813        list[0] = OP_NOTI;
2814        break;
2815    
2816        case OP_TYPESTAR:
2817        list[0] = *code;
2818        code++;
2819        break;
2820        }
2821      c = list[0];
2822      }
2823    
2824    switch(c)
2825      {
2826      case OP_NOT_DIGIT:
2827      case OP_DIGIT:
2828      case OP_NOT_WHITESPACE:
2829      case OP_WHITESPACE:
2830      case OP_NOT_WORDCHAR:
2831      case OP_WORDCHAR:
2832      case OP_ANY:
2833      case OP_ALLANY:
2834      case OP_ANYNL:
2835      case OP_NOT_HSPACE:
2836      case OP_HSPACE:
2837      case OP_NOT_VSPACE:
2838      case OP_VSPACE:
2839      case OP_EXTUNI:
2840      case OP_EODN:
2841      case OP_EOD:
2842      case OP_DOLL:
2843      case OP_DOLLM:
2844      return code;
2845    
2846      case OP_CHAR:
2847      case OP_NOT:
2848      GETCHARINCTEST(chr, code);
2849      list[2] = chr;
2850      list[3] = NOTACHAR;
2851      return code;
2852    
2853      case OP_CHARI:
2854      case OP_NOTI:
2855      list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2856      GETCHARINCTEST(chr, code);
2857      list[2] = chr;
2858    
2859    #ifdef SUPPORT_UCP
2860      if (chr < 128 || (chr < 256 && !utf))
2861        list[3] = fcc[chr];
2862      else
2863        list[3] = UCD_OTHERCASE(chr);
2864    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2865      list[3] = (chr < 256) ? fcc[chr] : chr;
2866    #else
2867      list[3] = fcc[chr];
2868    #endif
2869    
2870      /* The othercase might be the same value. */
2871    
2872      if (chr == list[3])
2873        list[3] = NOTACHAR;
2874      else
2875        list[4] = NOTACHAR;
2876      return code;
2877    
2878    #ifdef SUPPORT_UCP
2879      case OP_PROP:
2880      case OP_NOTPROP:
2881      if (code[0] != PT_CLIST)
2882        {
2883        list[2] = code[0];
2884        list[3] = code[1];
2885        return code + 2;
2886        }
2887    
2888      /* Convert only if we have enough space. */
2889    
2890      clist_src = PRIV(ucd_caseless_sets) + code[1];
2891      clist_dest = list + 2;
2892      code += 2;
2893    
2894      do {
2895         if (clist_dest >= list + 8)
2896           {
2897           /* Early return if there is not enough space. This should never
2898           happen, since all clists are shorter than 5 character now. */
2899           list[2] = code[0];
2900           list[3] = code[1];
2901           return code;
2902           }
2903         *clist_dest++ = *clist_src;
2904         }
2905      while(*clist_src++ != NOTACHAR);
2906    
2907      /* All characters are stored. The terminating NOTACHAR
2908      is copied form the clist itself. */
2909    
2910      list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2911      return code;
2912    #endif
2913    
2914      case OP_NCLASS:
2915      case OP_CLASS:
2916    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2917      case OP_XCLASS:
2918    
2919      if (c == OP_XCLASS)
2920        end = code + GET(code, 0);
2921      else
2922    #endif
2923        end = code + 32 / sizeof(pcre_uchar);
2924    
2925      switch(*end)
2926        {
2927        case OP_CRSTAR:
2928        case OP_CRMINSTAR:
2929        case OP_CRQUERY:
2930        case OP_CRMINQUERY:
2931        case OP_CRPOSSTAR:
2932        case OP_CRPOSQUERY:
2933        list[1] = TRUE;
2934        end++;
2935        break;
2936    
2937        case OP_CRPLUS:
2938        case OP_CRMINPLUS:
2939        case OP_CRPOSPLUS:
2940        end++;
2941        break;
2942    
2943        case OP_CRRANGE:
2944        case OP_CRMINRANGE:
2945        case OP_CRPOSRANGE:
2946        list[1] = (GET2(end, 1) == 0);
2947        end += 1 + 2 * IMM2_SIZE;
2948        break;
2949        }
2950      list[2] = end - code;
2951      return end;
2952      }
2953    return NULL;    /* Opcode not accepted */
2954    }
2955    
2956    
2957    
2958    /*************************************************
2959    *    Scan further character sets for match       *
2960    *************************************************/
2961    
2962    /* Checks whether the base and the current opcode have a common character, in
2963    which case the base cannot be possessified.
2964    
2965    Arguments:
2966      code        points to the byte code
2967      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2968      cd          static compile data
2969      base_list   the data list of the base opcode
2970    
2971    Returns:      TRUE if the auto-possessification is possible
2972    */
2973    
2974    static BOOL
2975    compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2976      const pcre_uint32* base_list, const pcre_uchar *base_end)
2977    {
2978    pcre_uchar c;
2979    pcre_uint32 list[8];
2980    const pcre_uint32* chr_ptr;
2981    const pcre_uint32* ochr_ptr;
2982    const pcre_uint32* list_ptr;
2983    const pcre_uchar *next_code;
2984    const pcre_uint8 *class_bits;
2985    pcre_uint32 chr;
2986    
2987    /* Note: the base_list[1] contains whether the current opcode has greedy
2988    (represented by a non-zero value) quantifier. This is a different from
2989    other character type lists, which stores here that the character iterator
2990    matches to an empty string (also represented by a non-zero value). */
2991    
2992    for(;;)
2993      {
2994      c = *code;
2995    
2996      /* Skip over callouts */
2997    
2998      if (c == OP_CALLOUT)
2999        {
3000        code += PRIV(OP_lengths)[c];
3001        continue;
3002        }
3003    
3004      if (c == OP_ALT)
3005        {
3006        do code += GET(code, 1); while (*code == OP_ALT);
3007        c = *code;
3008        }
3009    
3010      switch(c)
3011        {
3012        case OP_END:
3013        case OP_KETRPOS:
3014        /* TRUE only in greedy case. The non-greedy case could be replaced by
3015        an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT
3016        uses more memory, which we cannot get at this stage.) */
3017    
3018        return base_list[1] != 0;
3019    
3020        case OP_KET:
3021        /* If the bracket is capturing, and referenced by an OP_RECURSE, or
3022        it is an atomic sub-pattern (assert, once, etc.) the non-greedy case
3023        cannot be converted to a possessive form. */
3024    
3025        if (base_list[1] == 0) return FALSE;
3026    
3027        switch(*(code - GET(code, 1)))
3028          {
3029          case OP_ASSERT:
3030          case OP_ASSERT_NOT:
3031          case OP_ASSERTBACK:
3032          case OP_ASSERTBACK_NOT:
3033          case OP_ONCE:
3034          case OP_ONCE_NC:
3035          /* Atomic sub-patterns and assertions can always auto-possessify their
3036          last iterator. */
3037          return TRUE;
3038          }
3039    
3040        code += PRIV(OP_lengths)[c];
3041        continue;
3042    
3043        case OP_ONCE:
3044        case OP_ONCE_NC:
3045        case OP_BRA:
3046        case OP_CBRA:
3047        next_code = code;
3048        do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3049    
3050        /* We do not support repeated brackets, because they can lead to
3051        infinite recursion. */
3052    
3053        if (*next_code != OP_KET) return FALSE;
3054    
3055        next_code = code + GET(code, 1);
3056        code += PRIV(OP_lengths)[c];
3057    
3058        while (*next_code == OP_ALT)
3059          {
3060          if (!compare_opcodes(code, utf, cd, base_list, base_end)) return FALSE;
3061          code = next_code + 1 + LINK_SIZE;
3062          next_code += GET(next_code, 1);
3063          }
3064        continue;
3065    
3066        case OP_BRAZERO:
3067        case OP_BRAMINZERO:
3068    
3069        next_code = code + 1;
3070        if (*next_code != OP_BRA && *next_code != OP_CBRA)
3071          return FALSE;
3072    
3073        do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3074    
3075        /* We do not support repeated brackets, because they can lead to
3076        infinite recursion. */
3077        if (*next_code != OP_KET) return FALSE;
3078    
3079        /* The bracket content will be checked by the
3080        OP_BRA/OP_CBRA case above. */
3081        next_code += 1 + LINK_SIZE;
3082        if (!compare_opcodes(next_code, utf, cd, base_list, base_end)) return FALSE;
3083    
3084        code += PRIV(OP_lengths)[c];
3085        continue;
3086        }
3087    
3088      /* Check for a supported opcode, and load its properties. */
3089    
3090      code = get_chr_property_list(code, utf, cd->fcc, list);
3091      if (code == NULL) return FALSE;    /* Unsupported */
3092    
3093      /* If either opcode is a small character list, set pointers for comparing
3094      characters from that list with another list, or with a property. */
3095    
3096      if (base_list[0] == OP_CHAR)
3097        {
3098        chr_ptr = base_list + 2;
3099        list_ptr = list;
3100        }
3101      else if (list[0] == OP_CHAR)
3102        {
3103        chr_ptr = list + 2;
3104        list_ptr = base_list;
3105        }
3106    
3107      /* Some property combinations also acceptable. Unicode property opcodes are
3108      processed specially; the rest can be handled with a lookup table. */
3109    
3110      else
3111        {
3112        pcre_uint32 leftop, rightop;
3113    
3114        if (list[1] != 0) return FALSE;   /* Must match at least one character */
3115        leftop = base_list[0];
3116        rightop = list[0];
3117    
3118    #ifdef SUPPORT_UCP
3119        if (leftop == OP_PROP || leftop == OP_NOTPROP)
3120          {
3121          if (rightop == OP_EOD) return TRUE;
3122          if (rightop == OP_PROP || rightop == OP_NOTPROP)
3123            {
3124            int n;
3125            const pcre_uint8 *p;
3126            BOOL same = leftop == rightop;
3127            BOOL lisprop = leftop == OP_PROP;
3128            BOOL risprop = rightop == OP_PROP;
3129            BOOL bothprop = lisprop && risprop;
3130    
3131            /* There's a table that specifies how each combination is to be
3132            processed:
3133              0   Always return FALSE (never auto-possessify)
3134              1   Character groups are distinct (possessify if both are OP_PROP)
3135              2   Check character categories in the same group (general or particular)
3136              3   Return TRUE if the two opcodes are not the same
3137              ... see comments below
3138            */
3139    
3140            n = propposstab[base_list[2]][list[2]];
3141            switch(n)
3142              {
3143              case 0: return FALSE;
3144              case 1: return bothprop;
3145              case 2: return (base_list[3] == list[3]) != same;
3146              case 3: return !same;
3147    
3148              case 4:  /* Left general category, right particular category */
3149              return risprop && catposstab[base_list[3]][list[3]] == same;
3150    
3151              case 5:  /* Right general category, left particular category */
3152              return lisprop && catposstab[list[3]][base_list[3]] == same;
3153    
3154              /* This code is logically tricky. Think hard before fiddling with it.
3155              The posspropstab table has four entries per row. Each row relates to
3156              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3157              Only WORD actually needs all four entries, but using repeats for the
3158              others means they can all use the same code below.
3159    
3160              The first two entries in each row are Unicode general categories, and
3161              apply always, because all the characters they include are part of the
3162              PCRE character set. The third and fourth entries are a general and a
3163              particular category, respectively, that include one or more relevant
3164              characters. One or the other is used, depending on whether the check
3165              is for a general or a particular category. However, in both cases the
3166              category contains more characters than the specials that are defined
3167              for the property being tested against. Therefore, it cannot be used
3168              in a NOTPROP case.
3169    
3170              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3171              Underscore is covered by ucp_P or ucp_Po. */
3172    
3173              case 6:  /* Left alphanum vs right general category */
3174              case 7:  /* Left space vs right general category */
3175              case 8:  /* Left word vs right general category */
3176              p = posspropstab[n-6];
3177              return risprop && lisprop ==
3178                (list[3] != p[0] &&
3179                 list[3] != p[1] &&
3180                (list[3] != p[2] || !lisprop));
3181    
3182              case 9:   /* Right alphanum vs left general category */
3183              case 10:  /* Right space vs left general category */
3184              case 11:  /* Right word vs left general category */
3185              p = posspropstab[n-9];
3186              return lisprop && risprop ==
3187                (base_list[3] != p[0] &&
3188                 base_list[3] != p[1] &&
3189                (base_list[3] != p[2] || !risprop));
3190    
3191              case 12:  /* Left alphanum vs right particular category */
3192              case 13:  /* Left space vs right particular category */
3193              case 14:  /* Left word vs right particular category */
3194              p = posspropstab[n-12];
3195              return risprop && lisprop ==
3196                (catposstab[p[0]][list[3]] &&
3197                 catposstab[p[1]][list[3]] &&
3198                (list[3] != p[3] || !lisprop));
3199    
3200              case 15:  /* Right alphanum vs left particular category */
3201              case 16:  /* Right space vs left particular category */
3202              case 17:  /* Right word vs left particular category */
3203              p = posspropstab[n-15];
3204              return lisprop && risprop ==
3205                (catposstab[p[0]][base_list[3]] &&
3206                 catposstab[p[1]][base_list[3]] &&
3207                (base_list[3] != p[3] || !risprop));
3208              }
3209            }
3210          return FALSE;
3211          }
3212    
3213        else
3214    #endif  /* SUPPORT_UCP */
3215    
3216        return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3217               rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3218               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3219        }
3220    
3221      /* Control reaches here only if one of the items is a small character list.
3222      All characters are checked against the other side. */
3223    
3224      do
3225        {
3226        chr = *chr_ptr;
3227    
3228        switch(list_ptr[0])
3229          {
3230          case OP_CHAR:
3231          ochr_ptr = list_ptr + 2;
3232          do
3233            {
3234            if (chr == *ochr_ptr) return FALSE;
3235            ochr_ptr++;
3236            }
3237          while(*ochr_ptr != NOTACHAR);
3238          break;
3239    
3240          case OP_NOT:
3241          ochr_ptr = list_ptr + 2;
3242          do
3243            {
3244            if (chr == *ochr_ptr)
3245              break;
3246            ochr_ptr++;
3247            }
3248          while(*ochr_ptr != NOTACHAR);
3249          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3250          break;
3251    
3252          /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3253          set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3254    
3255          case OP_DIGIT:
3256          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3257          break;
3258    
3259          case OP_NOT_DIGIT:
3260          if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3261          break;
3262    
3263          case OP_WHITESPACE:
3264          if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3265          break;
3266    
3267          case OP_NOT_WHITESPACE:
3268          if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3269          break;
3270    
3271          case OP_WORDCHAR:
3272          if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3273          break;
3274    
3275          case OP_NOT_WORDCHAR:
3276          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3277          break;
3278    
3279          case OP_HSPACE:
3280          switch(chr)
3281            {
3282            HSPACE_CASES: return FALSE;
3283            default: break;
3284            }
3285          break;
3286    
3287          case OP_NOT_HSPACE:
3288          switch(chr)
3289            {
3290            HSPACE_CASES: break;
3291            default: return FALSE;
3292            }
3293          break;
3294    
3295          case OP_ANYNL:
3296          case OP_VSPACE:
3297          switch(chr)
3298            {
3299            VSPACE_CASES: return FALSE;
3300            default: break;
3301            }
3302          break;
3303    
3304          case OP_NOT_VSPACE:
3305          switch(chr)
3306            {
3307            VSPACE_CASES: break;
3308            default: return FALSE;
3309            }
3310          break;
3311    
3312          case OP_DOLL:
3313          case OP_EODN:
3314          switch (chr)
3315            {
3316            case CHAR_CR:
3317            case CHAR_LF:
3318            case CHAR_VT:
3319            case CHAR_FF:
3320            case CHAR_NEL:
3321    #ifndef EBCDIC
3322            case 0x2028:
3323            case 0x2029:
3324    #endif  /* Not EBCDIC */
3325            return FALSE;
3326            }
3327          break;
3328    
3329          case OP_EOD:    /* Can always possessify before \z */
3330          break;
3331    
3332          case OP_PROP:
3333          case OP_NOTPROP:
3334          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3335                list_ptr[0] == OP_NOTPROP))
3336            return FALSE;
3337          break;
3338    
3339          case OP_NCLASS:
3340          if (chr > 255) return FALSE;
3341          /* Fall through */
3342    
3343          case OP_CLASS:
3344          if (chr > 255) break;
3345          class_bits = (pcre_uint8 *)((list_ptr == list ? code : base_end) - list_ptr[2]);
3346          if ((class_bits[chr >> 3] & (1 << (chr & 7))) != 0)
3347            return FALSE;
3348          break;
3349    
3350    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3351          case OP_XCLASS:
3352          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3353          if (PRIV(xclass)(chr, code - list_ptr[2] + LINK_SIZE, utf))
3354            return FALSE;
3355          break;
3356    #endif
3357    
3358          default:
3359          return FALSE;
3360          }
3361    
3362        chr_ptr++;
3363        }
3364      while(*chr_ptr != NOTACHAR);
3365    
3366      /* At least one character must be matched from this opcode. */
3367    
3368      if (list[1] == 0) return TRUE;
3369      }
3370    
3371    return FALSE;
3372    }
3373    
3374    
3375    
3376    /*************************************************
3377    *    Scan compiled regex for auto-possession     *
3378    *************************************************/
3379    
3380    /* Replaces single character iterations with their possessive alternatives
3381    if appropriate. This function modifies the compiled opcode!
3382    
3383    Arguments:
3384      code        points to start of the byte code
3385      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3386      cd          static compile data
3387    
3388    Returns:      nothing
3389    */
3390    
3391    static void
3392    auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
3393    {
3394    register pcre_uchar c, d;
3395    const pcre_uchar *end;
3396    pcre_uchar *repeat_code;
3397    pcre_uint32 list[8];
3398    
3399    for (;;)
3400      {
3401      c = *code;
3402    
3403      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3404        {
3405        c -= get_repeat_base(c) - OP_STAR;
3406        end = (c <= OP_MINUPTO) ?
3407          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3408        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3409    
3410        if (end != NULL && compare_opcodes(end, utf, cd, list, end))
3411          {
3412          switch(c)
3413            {
3414            case OP_STAR:
3415            *code += OP_POSSTAR - OP_STAR;
3416            break;
3417    
3418            case OP_MINSTAR:
3419            *code += OP_POSSTAR - OP_MINSTAR;
3420            break;
3421    
3422            case OP_PLUS:
3423            *code += OP_POSPLUS - OP_PLUS;
3424            break;
3425    
3426            case OP_MINPLUS:
3427            *code += OP_POSPLUS - OP_MINPLUS;
3428            break;
3429    
3430            case OP_QUERY:
3431            *code += OP_POSQUERY - OP_QUERY;
3432            break;
3433    
3434            case OP_MINQUERY:
3435            *code += OP_POSQUERY - OP_MINQUERY;
3436            break;
3437    
3438            case OP_UPTO:
3439            *code += OP_POSUPTO - OP_UPTO;
3440            break;
3441    
3442            case OP_MINUPTO:
3443            *code += OP_MINUPTO - OP_UPTO;
3444            break;
3445            }
3446          }
3447        c = *code;
3448        }
3449      else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS)
3450        {
3451    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3452        if (c == OP_XCLASS)
3453          repeat_code = code + 1 + GET(code, 1);
3454        else
3455    #endif
3456          repeat_code = code + 1 + (32 / sizeof(pcre_uchar));
3457    
3458        d = *repeat_code;
3459        if (d >= OP_CRSTAR && d <= OP_CRMINRANGE)
3460          {
3461          /* end must not be NULL. */
3462          end = get_chr_property_list(code, utf, cd->fcc, list);
3463    
3464          list[1] = d == OP_CRSTAR || d == OP_CRPLUS || d == OP_CRQUERY ||
3465            d == OP_CRRANGE;
3466    
3467          if (compare_opcodes(end, utf, cd, list, end))
3468            {
3469            switch (d)
3470              {
3471              case OP_CRSTAR:
3472              *repeat_code = OP_CRPOSSTAR;
3473              break;
3474    
3475              case OP_CRPLUS:
3476              *repeat_code = OP_CRPOSPLUS;
3477              break;
3478    
3479              case OP_CRQUERY:
3480              *repeat_code = OP_CRPOSQUERY;
3481              break;
3482    
3483              case OP_CRRANGE:
3484              *repeat_code = OP_CRPOSRANGE;
3485              break;
3486              }
3487            }
3488          }
3489        }
3490    
3491      switch(c)
3492        {
3493        case OP_END:
3494        return;
3495    
3496        case OP_TYPESTAR:
3497        case OP_TYPEMINSTAR:
3498        case OP_TYPEPLUS:
3499        case OP_TYPEMINPLUS:
3500        case OP_TYPEQUERY:
3501        case OP_TYPEMINQUERY:
3502        case OP_TYPEPOSSTAR:
3503        case OP_TYPEPOSPLUS:
3504        case OP_TYPEPOSQUERY:
3505        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3506        break;
3507    
3508        case OP_TYPEUPTO:
3509        case OP_TYPEMINUPTO:
3510        case OP_TYPEEXACT:
3511        case OP_TYPEPOSUPTO:
3512        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3513          code += 2;
3514        break;
3515    
3516    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3517        case OP_XCLASS:
3518        code += GET(code, 1);
3519        break;
3520    #endif
3521    
3522        case OP_MARK:
3523        case OP_PRUNE_ARG:
3524        case OP_SKIP_ARG:
3525        case OP_THEN_ARG:
3526        code += code[1];
3527        break;
3528        }
3529    
3530      /* Add in the fixed length from the table */
3531    
3532      code += PRIV(OP_lengths)[c];
3533    
3534      /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3535      a multi-byte character. The length in the table is a minimum, so we have to
3536      arrange to skip the extra bytes. */
3537    
3538    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3539      if (utf) switch(c)
3540        {
3541        case OP_CHAR:
3542        case OP_CHARI:
3543        case OP_NOT:
3544        case OP_NOTI:
3545        case OP_STAR:
3546        case OP_MINSTAR:
3547        case OP_PLUS:
3548        case OP_MINPLUS:
3549        case OP_QUERY:
3550        case OP_MINQUERY:
3551        case OP_UPTO:
3552        case OP_MINUPTO:
3553        case OP_EXACT:
3554        case OP_POSSTAR:
3555        case OP_POSPLUS:
3556        case OP_POSQUERY:
3557        case OP_POSUPTO:
3558        case OP_STARI:
3559        case OP_MINSTARI:
3560        case OP_PLUSI:
3561        case OP_MINPLUSI:
3562        case OP_QUERYI:
3563        case OP_MINQUERYI:
3564        case OP_UPTOI:
3565        case OP_MINUPTOI:
3566        case OP_EXACTI:
3567        case OP_POSSTARI:
3568        case OP_POSPLUSI:
3569        case OP_POSQUERYI:
3570        case OP_POSUPTOI:
3571        case OP_NOTSTAR:
3572        case OP_NOTMINSTAR:
3573        case OP_NOTPLUS:
3574        case OP_NOTMINPLUS:
3575        case OP_NOTQUERY:
3576        case OP_NOTMINQUERY:
3577        case OP_NOTUPTO:
3578        case OP_NOTMINUPTO:
3579        case OP_NOTEXACT:
3580        case OP_NOTPOSSTAR:
3581        case OP_NOTPOSPLUS:
3582        case OP_NOTPOSQUERY:
3583        case OP_NOTPOSUPTO:
3584        case OP_NOTSTARI:
3585        case OP_NOTMINSTARI:
3586        case OP_NOTPLUSI:
3587        case OP_NOTMINPLUSI:
3588        case OP_NOTQUERYI:
3589        case OP_NOTMINQUERYI:
3590        case OP_NOTUPTOI:
3591        case OP_NOTMINUPTOI:
3592        case OP_NOTEXACTI:
3593        case OP_NOTPOSSTARI:
3594        case OP_NOTPOSPLUSI:
3595        case OP_NOTPOSQUERYI:
3596        case OP_NOTPOSUPTOI:
3597        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3598        break;
3599        }
3600    #else
3601      (void)(utf);  /* Keep compiler happy by referencing function argument */
3602    #endif
3603      }
3604    }
3605    
3606    
3607    
3608    /*************************************************
3609    *           Check for POSIX class syntax         *
3610    *************************************************/
3611    
3612    /* This function is called when the sequence "[:" or "[." or "[=" is
3613    encountered in a character class. It checks whether this is followed by a
3614    sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3615    reach an unescaped ']' without the special preceding character, return FALSE.
3616    
3617    Originally, this function only recognized a sequence of letters between the
3618    terminators, but it seems that Perl recognizes any sequence of characters,
3619  though of course unknown POSIX names are subsequently rejected. Perl gives an  though of course unknown POSIX names are subsequently rejected. Perl gives an
3620  "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE  "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3621  didn't consider this to be a POSIX class. Likewise for [:1234:].  didn't consider this to be a POSIX class. Likewise for [:1234:].
# Line 1779  where Perl recognizes it as the POSIX cl Line 3629  where Perl recognizes it as the POSIX cl
3629  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
3630  I think.  I think.
3631    
3632    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3633    It seems that the appearance of a nested POSIX class supersedes an apparent
3634    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3635    a digit.
3636    
3637    In Perl, unescaped square brackets may also appear as part of class names. For
3638    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3639    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3640    seem right at all. PCRE does not allow closing square brackets in POSIX class
3641    names.
3642    
3643  Arguments:  Arguments:
3644    ptr      pointer to the initial [    ptr      pointer to the initial [
3645    endptr   where to return the end pointer    endptr   where to return the end pointer
# Line 1787  Returns:   TRUE or FALSE Line 3648  Returns:   TRUE or FALSE
3648  */  */
3649    
3650  static BOOL  static BOOL
3651  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3652  {  {
3653  int terminator;          /* Don't combine these lines; the Solaris cc */  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3654  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3655  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != CHAR_NULL; ptr++)
3656    {    {
3657    if (*ptr == '\\' && ptr[1] == ']') ptr++; else    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3658        ptr++;
3659      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3660      else
3661      {      {