/[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 149 by ph10, Mon Apr 16 15:28:08 2007 UTC revision 1373 by chpe, Sat Oct 12 14:54:53 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-2007 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 42  POSSIBILITY OF SUCH DAMAGE. Line 42  POSSIBILITY OF SUCH DAMAGE.
42  supporting internal functions that are not used by other modules. */  supporting internal functions that are not used by other modules. */
43    
44    
45    #ifdef HAVE_CONFIG_H
46    #include "config.h"
47    #endif
48    
49  #define NLBLOCK cd             /* Block containing newline information */  #define NLBLOCK cd             /* Block containing newline information */
50  #define PSSTART start_pattern  /* Field containing processed string start */  #define PSSTART start_pattern  /* Field containing processed string start */
51  #define PSEND   end_pattern    /* Field containing processed string end */  #define PSEND   end_pattern    /* Field containing processed string end */
52    
   
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    
56  /* When DEBUG is defined, we need the pcre_printint() function, which is also  /* When PCRE_DEBUG is defined, we need the pcre(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    COMPILE_PCREx macro will already be appropriately set. */
60    
61  #ifdef DEBUG  #ifdef PCRE_DEBUG
62  #include "pcre_printint.src"  /* pcre_printint.c should not include any headers */
63    #define PCRE_INCLUDED
64    #include "pcre_printint.c"
65    #undef PCRE_INCLUDED
66  #endif  #endif
67    
68    
69    /* Macro for setting individual bits in class bitmaps. */
70    
71    #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
74    holds the compiled pattern length does not overflow. We make it a bit less than
75    INT_MAX to allow for adding in group terminating bytes, so that we don't have
76    to check them every time. */
77    
78    #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         *
95  *************************************************/  *************************************************/
# Line 72  so this number is very generous. Line 104  so this number is very generous.
104  The same workspace is used during the second, actual compile phase for  The same workspace is used during the second, actual compile phase for
105  remembering forward references to groups so that they can be filled in at the  remembering forward references to groups so that they can be filled in at the
106  end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE  end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
107  is 4 there is plenty of room. */  is 4 there is plenty of room for most patterns. However, the memory can get
108    filled up by repetitions of forward references, for example patterns like
109    /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
110    that the workspace is expanded using malloc() in this situation. The value
111    below is therefore a minimum, and we put a maximum on it for safety. The
112    minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
113    kicks in at the same number of forward references in all cases. */
114    
115    #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116    #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
117    
118    /* This value determines the size of the initial vector that is used for
119    remembering named groups during the pre-compile. It is allocated on the stack,
120    but if it is too small, it is expanded using malloc(), in a similar way to the
121    workspace. The value is the number of slots in the list. */
122    
123  #define COMPILE_WORK_SIZE (4096)  #define NAMED_GROUP_LIST_SIZE  20
124    
125    /* The overrun tests check for a slightly smaller size so that they detect the
126    overrun before it actually does run off the end of the data block. */
127    
128    #define WORK_SIZE_SAFETY_MARGIN (100)
129    
130    /* Private flags added to firstchar and reqchar. */
131    
132    #define REQ_CASELESS    (1 << 0)        /* Indicates caselessness */
133    #define REQ_VARY        (1 << 1)        /* Reqchar followed non-literal item */
134    /* Negative values for the firstchar and reqchar flags */
135    #define REQ_UNSET       (-2)
136    #define REQ_NONE        (-1)
137    
138    /* Repeated character flags. */
139    
140    #define UTF_LENGTH     0x10000000l      /* The char contains its length. */
141    
142  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
143  are simple data values; negative values are for special things like \d and so  are simple data values; negative values are for special things like \d and so
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       0,      0,      0,      0,      0,      0,      0,      0,   /* H - O */       0,                       0,
157  -ESC_P, -ESC_Q, -ESC_R, -ESC_S,      0,      0,      0, -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       0,      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,      0, -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 106  static const short int escapes[] = { Line 203  static const short int escapes[] = {
203  /*  70 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  70 */     0,     0,      0,       0,      0,     0,      0,      0,
204  /*  78 */     0,   '`',    ':',     '#',    '@',  '\'',    '=',    '"',  /*  78 */     0,   '`',    ':',     '#',    '@',  '\'',    '=',    '"',
205  /*  80 */     0,     7, -ESC_b,       0, -ESC_d, ESC_e,  ESC_f,      0,  /*  80 */     0,     7, -ESC_b,       0, -ESC_d, ESC_e,  ESC_f,      0,
206  /*  88 */     0,     0,      0,     '{',      0,     0,      0,      0,  /*  88 */-ESC_h,     0,      0,     '{',      0,     0,      0,      0,
207  /*  90 */     0,     0, -ESC_k,     'l',      0, ESC_n,      0, -ESC_p,  /*  90 */     0,     0, -ESC_k,     'l',      0, ESC_n,      0, -ESC_p,
208  /*  98 */     0, ESC_r,      0,     '}',      0,     0,      0,      0,  /*  98 */     0, ESC_r,      0,     '}',      0,     0,      0,      0,
209  /*  A0 */     0,   '~', -ESC_s, ESC_tee,      0,     0, -ESC_w,      0,  /*  A0 */     0,   '~', -ESC_s, ESC_tee,      0,-ESC_v, -ESC_w,      0,
210  /*  A8 */     0,-ESC_z,      0,       0,      0,   '[',      0,      0,  /*  A8 */     0,-ESC_z,      0,       0,      0,   '[',      0,      0,
211  /*  B0 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  B0 */     0,     0,      0,       0,      0,     0,      0,      0,
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 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  C8 */-ESC_H,     0,      0,       0,      0,     0,      0,      0,
215  /*  D0 */   '}',     0,      0,       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,     0, -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,
219  /*  F0 */     0,     0,      0,       0,      0,     0,      0,      0,  /*  F0 */     0,     0,      0,       0,      0,     0,      0,      0,
220  /*  F8 */     0,     0,      0,       0,      0,     0,      0,      0  /*  F8 */     0,     0,      0,       0,      0,     0,      0,      0
# Line 125  static const short int escapes[] = { Line 222  static const short int escapes[] = {
222  #endif  #endif
223    
224    
225  /* Tables of names of POSIX character classes and their lengths. The list is  /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
226  terminated by a zero length entry. The first three must be alpha, lower, upper,  searched linearly. Put all the names into a single string, in order to reduce
227  as this is assumed for handling case independence. */  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  static const char *const posix_names[] = {  platforms. */
230    "alpha", "lower", "upper",  
231    "alnum", "ascii", "blank", "cntrl", "digit", "graph",  typedef struct verbitem {
232    "print", "punct", "space", "word",  "xdigit" };    int   len;                 /* Length of verb name */
233      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;
236    
237    static const char verbnames[] =
238      "\0"                       /* Empty name is a shorthand for MARK */
239      STRING_MARK0
240      STRING_ACCEPT0
241      STRING_COMMIT0
242      STRING_F0
243      STRING_FAIL0
244      STRING_PRUNE0
245      STRING_SKIP0
246      STRING_THEN;
247    
248    static const verbitem verbs[] = {
249      { 0, -1,        OP_MARK },
250      { 4, -1,        OP_MARK },
251      { 6, OP_ACCEPT, -1 },
252      { 6, OP_COMMIT, -1 },
253      { 1, OP_FAIL,   -1 },
254      { 4, OP_FAIL,   -1 },
255      { 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);
261    
262    
263    /* Tables of names of POSIX character classes and their lengths. The names are
264    now all in a single string, to reduce the number of relocations when a shared
265    library is dynamically loaded. The list of lengths is terminated by a zero
266    length entry. The first three must be alpha, lower, upper, as this is assumed
267    for handling case independence. */
268    
269    static const char posix_names[] =
270      STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
271      STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
272      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 164  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 171  static const int posix_class_maps[] = { Line 410  static const int posix_class_maps[] = {
410  /* The texts of compile-time error messages. These are "char *" because they  /* The texts of compile-time error messages. These are "char *" because they
411  are passed to the outside world. Do not ever re-use any error number, because  are passed to the outside world. Do not ever re-use any error number, because
412  they are documented. Always add a new error instead. Messages marked DEAD below  they are documented. Always add a new error instead. Messages marked DEAD below
413  are no longer used. */  are no longer used. This used to be a table of strings, but in order to reduce
414    the number of relocations needed when a shared library is loaded dynamically,
415  static const char *error_texts[] = {  it is now one long string. We cannot use a table of offsets, because the
416    "no error",  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
417    "\\ at end of pattern",  simply count through to the one we want - this isn't a performance issue
418    "\\c at end of pattern",  because these strings are used only when there is a compilation error.
419    "unrecognized character follows \\",  
420    "numbers out of order in {} quantifier",  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[] =
425      "no error\0"
426      "\\ at end of pattern\0"
427      "\\c at end of pattern\0"
428      "unrecognized character follows \\\0"
429      "numbers out of order in {} quantifier\0"
430    /* 5 */    /* 5 */
431    "number too big in {} quantifier",    "number too big in {} quantifier\0"
432    "missing terminating ] for character class",    "missing terminating ] for character class\0"
433    "invalid escape sequence in character class",    "invalid escape sequence in character class\0"
434    "range out of order in character class",    "range out of order in character class\0"
435    "nothing to repeat",    "nothing to repeat\0"
436    /* 10 */    /* 10 */
437    "operand of unlimited repeat could match the empty string",  /** DEAD **/    "operand of unlimited repeat could match the empty string\0"  /** DEAD **/
438    "internal error: unexpected repeat",    "internal error: unexpected repeat\0"
439    "unrecognized character after (?",    "unrecognized character after (? or (?-\0"
440    "POSIX named classes are supported only within a class",    "POSIX named classes are supported only within a class\0"
441    "missing )",    "missing )\0"
442    /* 15 */    /* 15 */
443    "reference to non-existent subpattern",    "reference to non-existent subpattern\0"
444    "erroffset passed as NULL",    "erroffset passed as NULL\0"
445    "unknown option bit(s) set",    "unknown option bit(s) set\0"
446    "missing ) after comment",    "missing ) after comment\0"
447    "parentheses nested too deeply",  /** DEAD **/    "parentheses nested too deeply\0"  /** DEAD **/
448    /* 20 */    /* 20 */
449    "regular expression too large",    "regular expression is too large\0"
450    "failed to get memory",    "failed to get memory\0"
451    "unmatched parentheses",    "unmatched parentheses\0"
452    "internal error: code overflow",    "internal error: code overflow\0"
453    "unrecognized character after (?<",    "unrecognized character after (?<\0"
454    /* 25 */    /* 25 */
455    "lookbehind assertion is not fixed length",    "lookbehind assertion is not fixed length\0"
456    "malformed number or name after (?(",    "malformed number or name after (?(\0"
457    "conditional group contains more than two branches",    "conditional group contains more than two branches\0"
458    "assertion expected after (?(",    "assertion expected after (?(\0"
459    "(?R or (?digits must be followed by )",    "(?R or (?[+-]digits must be followed by )\0"
460    /* 30 */    /* 30 */
461    "unknown POSIX class name",    "unknown POSIX class name\0"
462    "POSIX collating elements are not supported",    "POSIX collating elements are not supported\0"
463    "this version of PCRE is not compiled with PCRE_UTF8 support",    "this version of PCRE is compiled without UTF support\0"
464    "spare error",  /** DEAD **/    "spare error\0"  /** DEAD **/
465    "character value in \\x{...} sequence is too large",    "character value in \\x{} or \\o{} is too large\0"
466    /* 35 */    /* 35 */
467    "invalid condition (?(0)",    "invalid condition (?(0)\0"
468    "\\C not allowed in lookbehind assertion",    "\\C not allowed in lookbehind assertion\0"
469    "PCRE does not support \\L, \\l, \\N, \\U, or \\u",    "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
470    "number after (?C is > 255",    "number after (?C is > 255\0"
471    "closing ) for (?C expected",    "closing ) for (?C expected\0"
472    /* 40 */    /* 40 */
473    "recursive call could loop indefinitely",    "recursive call could loop indefinitely\0"
474    "unrecognized character after (?P",    "unrecognized character after (?P\0"
475    "syntax error in subpattern name (missing terminator)",    "syntax error in subpattern name (missing terminator)\0"
476    "two named subpatterns have the same name",    "two named subpatterns have the same name\0"
477    "invalid UTF-8 string",    "invalid UTF-8 string\0"
478    /* 45 */    /* 45 */
479    "support for \\P, \\p, and \\X has not been compiled",    "support for \\P, \\p, and \\X has not been compiled\0"
480    "malformed \\P or \\p sequence",    "malformed \\P or \\p sequence\0"
481    "unknown property name after \\P or \\p",    "unknown property name after \\P or \\p\0"
482    "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)",    "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
483    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")",    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
484    /* 50 */    /* 50 */
485    "repeated subpattern is too long",    "repeated subpattern is too long\0"    /** DEAD **/
486    "octal value is greater than \\377 (not in UTF-8 mode)",    "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
487    "internal error: overran compiling workspace",    "internal error: overran compiling workspace\0"
488    "internal error: previously-checked referenced subpattern not found",    "internal error: previously-checked referenced subpattern not found\0"
489    "DEFINE group contains more than one branch",    "DEFINE group contains more than one branch\0"
490    /* 55 */    /* 55 */
491    "repeating a DEFINE group is not allowed",    "repeating a DEFINE group is not allowed\0"  /** DEAD **/
492    "inconsistent NEWLINE options",    "inconsistent NEWLINE options\0"
493    "\\g is not followed by an (optionally braced) non-zero number"    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
494  };    "a numbered reference must not be zero\0"
495      "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
496      /* 60 */
497      "(*VERB) not recognized or malformed\0"
498      "number is too big\0"
499      "subpattern name expected\0"
500      "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 262  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 298  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 334  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 370  static const unsigned char ebcdic_charta Line 659  static const unsigned char ebcdic_charta
659  #endif  #endif
660    
661    
662  /* Definition to allow mutual recursion */  /* This table is used to check whether auto-possessification is possible
663    between adjacent character-type opcodes. The left-hand (repeated) opcode is
664    used to select the row, and the right-hand opcode is use to select the column.
665    A value of 1 means that auto-possessification is OK. For example, the second
666    value in the first row means that \D+\d can be turned into \D++\d.
667    
668    The Unicode property types (\P and \p) have to be present to fill out the table
669    because of what their opcode values are, but the table values should always be
670    zero because property types are handled separately in the code. The last four
671    columns apply to items that cannot be repeated, so there is no need to have
672    rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
673    *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
674    
675    #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
676    #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
677    
678    static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
679    /* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
680      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
681      { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
682      { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
683      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
684      { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
685      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
686      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
687      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
688      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
689      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
690      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
691      { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
692      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
693      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
694      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
695      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
696      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
697    };
698    
699    
700    /* This table is used to check whether auto-possessification is possible
701    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    
782    /*************************************************
783    *            Find an error text                  *
784    *************************************************/
785    
786    /* The error texts are now all in one long string, to save on relocations. As
787    some of the text is of unknown length, we can't use a table of offsets.
788    Instead, just count through the strings. This is not a performance issue
789    because it happens only when there has been a compilation error.
790    
791    Argument:   the error number
792    Returns:    pointer to the error string
793    */
794    
795    static const char *
796    find_error_text(int n)
797    {
798    const char *s = error_texts;
799    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;
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  static BOOL
862    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, int, int *,  is_counted_repeat(const pcre_uchar *p)
863      int *, branch_chain *, compile_data *, int *);  {
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    
# Line 383  static BOOL Line 883  static BOOL
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                   on error, errorptr is set                   negative => a back reference
903                     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-alphamerics are literals. For digits or letters, do an initial lookup in  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
925  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 alphameric */  /* 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 alphameric */  /* 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 a number, either plain or braced. If positive, it      case CHAR_u:
959      is an absolute backreference. If negative, it is a relative backreference.      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
960      This is a Perl 5.10 feature. */        {
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
1007        backreference. If negative, it is a relative backreference. This is a Perl
1008        5.10 feature.
1009    
1010        (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
1012        this is synonymous with \k{name}, we fudge it up by pretending it really
1013        was \k.
1014    
1015        (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
1017        (possibly recursive) subroutine calls, _not_ backreferences. Just return
1018        the ESC_g code (cf \k). */
1019    
1020        case CHAR_g:
1021        if (isclass) break;
1022        if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
1023          {
1024          escape = ESC_g;
1025          break;
1026          }
1027    
1028        /* Handle the Perl-compatible cases */
1029    
1030      case 'g':      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
     if (ptr[1] == '{')  
1031        {        {
1032          const pcre_uchar *p;
1033          for (p = ptr+2; *p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
1034            if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1035          if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1036            {
1037            escape = ESC_k;
1038            break;
1039            }
1040        braced = TRUE;        braced = TRUE;
1041        ptr++;        ptr++;
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          {
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;
1069          break;
1070          }
1071    
1072      if (c == 0 || (braced && *(++ptr) != '}'))      if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET)
1073        {        {
1074        *errorcodeptr = ERR57;        *errorcodeptr = ERR57;
1075        return 0;        break;
1076          }
1077    
1078        if (s == 0)
1079          {
1080          *errorcodeptr = ERR58;
1081          break;
1082        }        }
1083    
1084      if (negated)      if (negated)
1085        {        {
1086        if (c > bracount)        if (s > bracount)
1087          {          {
1088          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
1089          return 0;          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 < 10 || c <= bracount)        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;
1136            break;
1137            }
1138          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      if ((c = *ptr) >= '8')  
1151        {      if ((c = *ptr) >= CHAR_8) break;
1152        ptr--;  
1153        c = 0;      /* Fall through with a digit less than 8 */
       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}. */
1172      treated as a data character. */  
1173        case CHAR_o:
1174      case 'x':      if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
     if (ptr[1] == '{')  
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        return 0;        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;
1337    
1338      /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any      /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
1339      other alphameric following \ is an error if PCRE_EXTRA was set; otherwise,      other alphanumeric following \ is an error if PCRE_EXTRA was set;
1340      for Perl compatibility, it is a literal. This code looks a bit odd, but      otherwise, for Perl compatibility, it is a literal. This code looks a bit
1341      there used to be some cases other than the default, and there may be again      odd, but there used to be some cases other than the default, and there may
1342      in future, so I haven't "optimized" it. */      be again in future, so I haven't "optimized" it. */
1343    
1344      default:      default:
1345      if ((options & PCRE_EXTRA) != 0) switch(c)      if ((options & PCRE_EXTRA) != 0) switch(c)
# Line 637  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 656  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 < 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 708  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[i].name);    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 788  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 797  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 807  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 837  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)
   
   /* Skip over backslashed characters and also entire \Q...\E */  
   
   if (*ptr == '\\')  
1564      {      {
1565      if (*(++ptr) == 0) return -1;      case OP_ASSERT_NOT:
1566      if (*ptr == 'Q') for (;;)      case OP_ASSERTBACK:
1567        {      case OP_ASSERTBACK_NOT:
1568        while (*(++ptr) != 0 && *ptr != '\\');      if (!skipassert) return code;
1569        if (*ptr == 0) return -1;      do code += GET(code, 1); while (*code == OP_ALT);
1570        if (*(++ptr) == 'E') break;      code += PRIV(OP_lengths)[*code];
1571        }      break;
     continue;  
     }  
1572    
1573    /* Skip over character classes */      case OP_WORD_BOUNDARY:
1574        case OP_NOT_WORD_BOUNDARY:
1575    if (*ptr == '[')      if (!skipassert) return code;
1576      {      /* Fall through */
     while (*(++ptr) != ']')  
       {  
       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] != '?')  
     {  
     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 */  
1577    
1578      case OP_CALLOUT:      case OP_CALLOUT:
1579      case OP_CREF:      case OP_CREF:
1580        case OP_DNCREF:
1581      case OP_RREF:      case OP_RREF:
1582        case OP_DNRREF:
1583      case OP_DEF:      case OP_DEF:
1584      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1585      break;      break;
1586    
1587      default:      default:
# Line 1010  for (;;) Line 1593  for (;;)
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 1041  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 1072  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 1079  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 1119  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      cc += 4;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1769          cc += 2;
1770        cc += 1 + IMM2_SIZE + 1;
1771      break;      break;
1772    
1773      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1141  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 1148  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_CRPLUS:
1821          case OP_CRMINPLUS:
1822        case OP_CRSTAR:        case OP_CRSTAR:
1823        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1824        case OP_CRQUERY:        case OP_CRQUERY:
# Line 1179  for (;;) Line 1827  for (;;)
1827    
1828        case OP_CRRANGE:        case OP_CRRANGE:
1829        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1830        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1831        branchlength += GET2(cc,1);        branchlength += (int)GET2(cc,1);
1832        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1833        break;        break;
1834    
1835        default:        default:
# Line 1191  for (;;) Line 1839  for (;;)
1839    
1840      /* Anything else is variable length */      /* Anything else is variable length */
1841    
1842      default:      case OP_ANYNL:
1843        case OP_BRAMINZERO:
1844        case OP_BRAPOS:
1845        case OP_BRAPOSZERO:
1846        case OP_BRAZERO:
1847        case OP_CBRAPOS:
1848        case OP_EXTUNI:
1849        case OP_KETRMAX:
1850        case OP_KETRMIN:
1851        case OP_KETRPOS:
1852        case OP_MINPLUS:
1853        case OP_MINPLUSI:
1854        case OP_MINQUERY:
1855        case OP_MINQUERYI:
1856        case OP_MINSTAR:
1857        case OP_MINSTARI:
1858        case OP_MINUPTO:
1859        case OP_MINUPTOI:
1860        case OP_NOTMINPLUS:
1861        case OP_NOTMINPLUSI:
1862        case OP_NOTMINQUERY:
1863        case OP_NOTMINQUERYI:
1864        case OP_NOTMINSTAR:
1865        case OP_NOTMINSTARI:
1866        case OP_NOTMINUPTO:
1867        case OP_NOTMINUPTOI:
1868        case OP_NOTPLUS:
1869        case OP_NOTPLUSI:
1870        case OP_NOTPOSPLUS:
1871        case OP_NOTPOSPLUSI:
1872        case OP_NOTPOSQUERY:
1873        case OP_NOTPOSQUERYI:
1874        case OP_NOTPOSSTAR:
1875        case OP_NOTPOSSTARI:
1876        case OP_NOTPOSUPTO:
1877        case OP_NOTPOSUPTOI:
1878        case OP_NOTQUERY:
1879        case OP_NOTQUERYI:
1880        case OP_NOTSTAR:
1881        case OP_NOTSTARI:
1882        case OP_NOTUPTO:
1883        case OP_NOTUPTOI:
1884        case OP_PLUS:
1885        case OP_PLUSI:
1886        case OP_POSPLUS:
1887        case OP_POSPLUSI:
1888        case OP_POSQUERY:
1889        case OP_POSQUERYI:
1890        case OP_POSSTAR:
1891        case OP_POSSTARI:
1892        case OP_POSUPTO:
1893        case OP_POSUPTOI:
1894        case OP_QUERY:
1895        case OP_QUERYI:
1896        case OP_REF:
1897        case OP_REFI:
1898        case OP_DNREF:
1899        case OP_DNREFI:
1900        case OP_SBRA:
1901        case OP_SBRAPOS:
1902        case OP_SCBRA:
1903        case OP_SCBRAPOS:
1904        case OP_SCOND:
1905        case OP_SKIPZERO:
1906        case OP_STAR:
1907        case OP_STARI:
1908        case OP_TYPEMINPLUS:
1909        case OP_TYPEMINQUERY:
1910        case OP_TYPEMINSTAR:
1911        case OP_TYPEMINUPTO:
1912        case OP_TYPEPLUS:
1913        case OP_TYPEPOSPLUS:
1914        case OP_TYPEPOSQUERY:
1915        case OP_TYPEPOSSTAR:
1916        case OP_TYPEPOSUPTO:
1917        case OP_TYPEQUERY:
1918        case OP_TYPESTAR:
1919        case OP_TYPEUPTO:
1920        case OP_UPTO:
1921        case OP_UPTOI:
1922      return -1;      return -1;
1923    
1924        /* Catch unrecognized opcodes so that when new ones are added they
1925        are not forgotten, as has happened in the past. */
1926    
1927        default:
1928        return -4;
1929      }      }
1930    }    }
1931  /* Control never gets here */  /* Control never gets here */
# Line 1200  for (;;) Line 1933  for (;;)
1933    
1934    
1935    
   
1936  /*************************************************  /*************************************************
1937  *    Scan compiled regex for numbered bracket    *  *    Scan compiled regex for specific bracket    *
1938  *************************************************/  *************************************************/
1939    
1940  /* This little function scans through a compiled pattern until it finds a  /* This little function scans through a compiled pattern until it finds a
1941  capturing bracket with the given number.  capturing bracket with the given number, or, if the number is negative, an
1942    instance of OP_REVERSE for a lookbehind. The function is global in the C sense
1943    so that it can be called from pcre_study() when finding the minimum matching
1944    length.
1945    
1946  Arguments:  Arguments:
1947    code        points to start of expression    code        points to start of expression
1948    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1949    number      the required bracket number    number      the required bracket number or negative to find a lookbehind
1950    
1951  Returns:      pointer to the opcode for the bracket, or NULL if not found  Returns:      pointer to the opcode for the bracket, or NULL if not found
1952  */  */
1953    
1954  static const uschar *  const pcre_uchar *
1955  find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
1956  {  {
1957  for (;;)  for (;;)
1958    {    {
1959    register int c = *code;    register pcre_uchar c = *code;
1960    
1961    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1962    
1963    /* XCLASS is used for classes that cannot be represented just by a bit    /* XCLASS is used for classes that cannot be represented just by a bit
# Line 1230  for (;;) Line 1966  for (;;)
1966    
1967    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
1968    
1969      /* Handle recursion */
1970    
1971      else if (c == OP_REVERSE)
1972        {
1973        if (number < 0) return (pcre_uchar *)code;
1974        code += PRIV(OP_lengths)[c];
1975        }
1976    
1977    /* Handle capturing bracket */    /* Handle capturing bracket */
1978    
1979    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1980               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1981      {      {
1982      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1983      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1984      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1985      }      }
1986    
1987      /* Otherwise, we can get the item's length from the table, except that for
1988      repeated character types, we have to test for \p and \P, which have an extra
1989      two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1990      must add in its length. */
1991    
1992      else
1993        {
1994        switch(c)
1995          {
1996          case OP_TYPESTAR:
1997          case OP_TYPEMINSTAR:
1998          case OP_TYPEPLUS:
1999          case OP_TYPEMINPLUS:
2000          case OP_TYPEQUERY:
2001          case OP_TYPEMINQUERY:
2002          case OP_TYPEPOSSTAR:
2003          case OP_TYPEPOSPLUS:
2004          case OP_TYPEPOSQUERY:
2005          if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2006          break;
2007    
2008          case OP_TYPEUPTO:
2009          case OP_TYPEMINUPTO:
2010          case OP_TYPEEXACT:
2011          case OP_TYPEPOSUPTO:
2012          if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2013            code += 2;
2014          break;
2015    
2016          case OP_MARK:
2017          case OP_PRUNE_ARG:
2018          case OP_SKIP_ARG:
2019          case OP_THEN_ARG:
2020          code += code[1];
2021          break;
2022          }
2023    
2024        /* Add in the fixed length from the table */
2025    
2026        code += PRIV(OP_lengths)[c];
2027    
2028    /* In UTF-8 mode, opcodes that are followed by a character may be followed by    /* In UTF-8 mode, opcodes that are followed by a character may be followed by
2029    a multi-byte character. The length in the table is a minimum, so we have to    a multi-byte character. The length in the table is a minimum, so we have to
2030    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
2031    
2032    else  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2033      {      if (utf) switch(c)
     code += _pcre_OP_lengths[c];  
 #ifdef SUPPORT_UTF8  
     if (utf8) switch(c)  
2034        {        {
2035        case OP_CHAR:        case OP_CHAR:
2036        case OP_CHARNC:        case OP_CHARI:
2037        case OP_EXACT:        case OP_EXACT:
2038          case OP_EXACTI:
2039        case OP_UPTO:        case OP_UPTO:
2040          case OP_UPTOI:
2041        case OP_MINUPTO:        case OP_MINUPTO:
2042          case OP_MINUPTOI:
2043        case OP_POSUPTO:        case OP_POSUPTO:
2044          case OP_POSUPTOI:
2045        case OP_STAR:        case OP_STAR:
2046          case OP_STARI:
2047        case OP_MINSTAR:        case OP_MINSTAR:
2048          case OP_MINSTARI:
2049        case OP_POSSTAR:        case OP_POSSTAR:
2050          case OP_POSSTARI:
2051        case OP_PLUS:        case OP_PLUS:
2052          case OP_PLUSI:
2053        case OP_MINPLUS:        case OP_MINPLUS:
2054          case OP_MINPLUSI:
2055        case OP_POSPLUS:        case OP_POSPLUS:
2056          case OP_POSPLUSI:
2057        case OP_QUERY:        case OP_QUERY:
2058          case OP_QUERYI:
2059        case OP_MINQUERY:        case OP_MINQUERY:
2060          case OP_MINQUERYI:
2061        case OP_POSQUERY:        case OP_POSQUERY:
2062        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2063          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2064        break;        break;
2065        }        }
2066    #else
2067        (void)(utf);  /* Keep compiler happy by referencing function argument */
2068  #endif  #endif
2069      }      }
2070    }    }
# Line 1283  instance of OP_RECURSE. Line 2081  instance of OP_RECURSE.
2081    
2082  Arguments:  Arguments:
2083    code        points to start of expression    code        points to start of expression
2084    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2085    
2086  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found
2087  */  */
2088    
2089  static const uschar *  static const pcre_uchar *
2090  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
2091  {  {
2092  for (;;)  for (;;)
2093    {    {
2094    register int c = *code;    register pcre_uchar c = *code;
2095    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2096    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
2097    
# Line 1303  for (;;) Line 2101  for (;;)
2101    
2102    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
2103    
2104    /* Otherwise, we get the item's length from the table. In UTF-8 mode, opcodes    /* Otherwise, we can get the item's length from the table, except that for
2105    that are followed by a character may be followed by a multi-byte character.    repeated character types, we have to test for \p and \P, which have an extra
2106    The length in the table is a minimum, so we have to arrange to skip the extra    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2107    bytes. */    must add in its length. */
2108    
2109    else    else
2110      {      {
2111      code += _pcre_OP_lengths[c];      switch(c)
2112  #ifdef SUPPORT_UTF8        {
2113      if (utf8) switch(c)        case OP_TYPESTAR:
2114          case OP_TYPEMINSTAR:
2115          case OP_TYPEPLUS:
2116          case OP_TYPEMINPLUS:
2117          case OP_TYPEQUERY:
2118          case OP_TYPEMINQUERY:
2119          case OP_TYPEPOSSTAR:
2120          case OP_TYPEPOSPLUS:
2121          case OP_TYPEPOSQUERY:
2122          if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2123          break;
2124    
2125          case OP_TYPEPOSUPTO:
2126          case OP_TYPEUPTO:
2127          case OP_TYPEMINUPTO:
2128          case OP_TYPEEXACT:
2129          if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2130            code += 2;
2131          break;
2132    
2133          case OP_MARK:
2134          case OP_PRUNE_ARG:
2135          case OP_SKIP_ARG:
2136          case OP_THEN_ARG:
2137          code += code[1];
2138          break;
2139          }
2140    
2141        /* Add in the fixed length from the table */
2142    
2143        code += PRIV(OP_lengths)[c];
2144    
2145        /* In UTF-8 mode, opcodes that are followed by a character may be followed
2146        by a multi-byte character. The length in the table is a minimum, so we have
2147        to arrange to skip the extra bytes. */
2148    
2149    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2150        if (utf) switch(c)
2151        {        {
2152        case OP_CHAR:        case OP_CHAR:
2153        case OP_CHARNC:        case OP_CHARI:
2154          case OP_NOT:
2155          case OP_NOTI:
2156        case OP_EXACT:        case OP_EXACT:
2157          case OP_EXACTI:
2158          case OP_NOTEXACT:
2159          case OP_NOTEXACTI:
2160        case OP_UPTO:        case OP_UPTO:
2161          case OP_UPTOI:
2162          case OP_NOTUPTO:
2163          case OP_NOTUPTOI:
2164        case OP_MINUPTO:        case OP_MINUPTO:
2165          case OP_MINUPTOI:
2166          case OP_NOTMINUPTO:
2167          case OP_NOTMINUPTOI:
2168        case OP_POSUPTO:        case OP_POSUPTO:
2169          case OP_POSUPTOI:
2170          case OP_NOTPOSUPTO:
2171          case OP_NOTPOSUPTOI:
2172        case OP_STAR:        case OP_STAR:
2173          case OP_STARI:
2174          case OP_NOTSTAR:
2175          case OP_NOTSTARI:
2176        case OP_MINSTAR:        case OP_MINSTAR:
2177          case OP_MINSTARI:
2178          case OP_NOTMINSTAR:
2179          case OP_NOTMINSTARI:
2180        case OP_POSSTAR:        case OP_POSSTAR:
2181          case OP_POSSTARI:
2182          case OP_NOTPOSSTAR:
2183          case OP_NOTPOSSTARI:
2184        case OP_PLUS:        case OP_PLUS:
2185          case OP_PLUSI:
2186          case OP_NOTPLUS:
2187          case OP_NOTPLUSI:
2188        case OP_MINPLUS:        case OP_MINPLUS:
2189          case OP_MINPLUSI:
2190          case OP_NOTMINPLUS:
2191          case OP_NOTMINPLUSI:
2192        case OP_POSPLUS:        case OP_POSPLUS:
2193          case OP_POSPLUSI:
2194          case OP_NOTPOSPLUS:
2195          case OP_NOTPOSPLUSI:
2196        case OP_QUERY:        case OP_QUERY:
2197          case OP_QUERYI:
2198          case OP_NOTQUERY:
2199          case OP_NOTQUERYI:
2200        case OP_MINQUERY:        case OP_MINQUERY:
2201          case OP_MINQUERYI:
2202          case OP_NOTMINQUERY:
2203          case OP_NOTMINQUERYI:
2204        case OP_POSQUERY:        case OP_POSQUERY:
2205        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_POSQUERYI:
2206          case OP_NOTPOSQUERY:
2207          case OP_NOTPOSQUERYI:
2208          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2209        break;        break;
2210        }        }
2211    #else
2212        (void)(utf);  /* Keep compiler happy by referencing function argument */
2213  #endif  #endif
2214      }      }
2215    }    }
# Line 1347  for (;;) Line 2225  for (;;)
2225  can match the empty string or not. It is called from could_be_empty()  can match the empty string or not. It is called from could_be_empty()
2226  below and from compile_branch() when checking for an unlimited repeat of a  below and from compile_branch() when checking for an unlimited repeat of a
2227  group that can match nothing. Note that first_significant_code() skips over  group that can match nothing. Note that first_significant_code() skips over
2228  assertions. If we hit an unclosed bracket, we return "empty" - this means we've  backward and negative forward assertions when its final argument is TRUE. If we
2229  struck an inner bracket whose current branch will already have been scanned.  hit an unclosed bracket, we return "empty" - this means we've struck an inner
2230    bracket whose current branch will already have been scanned.
2231    
2232  Arguments:  Arguments:
2233    code        points to start of search    code        points to start of search
2234    endcode     points to where to stop    endcode     points to where to stop
2235    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2236      cd          contains pointers to tables etc.
2237      recurses    chain of recurse_check to catch mutual recursion
2238    
2239  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2240  */  */
2241    
2242    typedef struct recurse_check {
2243      struct recurse_check *prev;
2244      const pcre_uchar *group;
2245    } recurse_check;
2246    
2247  static BOOL  static BOOL
2248  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2249      BOOL utf, compile_data *cd, recurse_check *recurses)
2250  {  {
2251  register int c;  register pcre_uchar c;
2252  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  recurse_check this_recurse;
2253    
2254    for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2255       code < endcode;       code < endcode;
2256       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2257    {    {
2258    const uschar *ccode;    const pcre_uchar *ccode;
2259    
2260    c = *code;    c = *code;
2261    
2262    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE)    /* Skip over forward assertions; the other assertions are skipped by
2263      first_significant_code() with a TRUE final argument. */
2264    
2265      if (c == OP_ASSERT)
2266        {
2267        do code += GET(code, 1); while (*code == OP_ALT);
2268        c = *code;
2269        continue;
2270        }
2271    
2272      /* For a recursion/subroutine call, if its end has been reached, which
2273      implies a backward reference subroutine call, we can scan it. If it's a
2274      forward reference subroutine call, we can't. To detect forward reference
2275      we have to scan up the list that is kept in the workspace. This function is
2276      called only when doing the real compile, not during the pre-compile that
2277      measures the size of the compiled pattern. */
2278    
2279      if (c == OP_RECURSE)
2280      {      {
2281        const pcre_uchar *scode = cd->start_code + GET(code, 1);
2282      BOOL empty_branch;      BOOL empty_branch;
     if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */  
2283    
2284      /* Scan a closed bracket */      /* Test for forward reference or uncompleted reference. This is disabled
2285        when called to scan a completed pattern by setting cd->start_workspace to
2286        NULL. */
2287    
2288        if (cd->start_workspace != NULL)
2289          {
2290          const pcre_uchar *tcode;
2291          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2292            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2293          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2294          }
2295    
2296        /* If we are scanning a completed pattern, there are no forward references
2297        and all groups are complete. We need to detect whether this is a recursive
2298        call, as otherwise there will be an infinite loop. If it is a recursion,
2299        just skip over it. Simple recursions are easily detected. For mutual
2300        recursions we keep a chain on the stack. */
2301    
2302        else
2303          {
2304          recurse_check *r = recurses;
2305          const pcre_uchar *endgroup = scode;
2306    
2307          do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2308          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2309    
2310          for (r = recurses; r != NULL; r = r->prev)
2311            if (r->group == scode) break;
2312          if (r != NULL) continue;   /* Mutual recursion */
2313          }
2314    
2315        /* Completed reference; scan the referenced group, remembering it on the
2316        stack chain to detect mutual recursions. */
2317    
2318      empty_branch = FALSE;      empty_branch = FALSE;
2319        this_recurse.prev = recurses;
2320        this_recurse.group = scode;
2321    
2322      do      do
2323        {        {
2324        if (!empty_branch && could_be_empty_branch(code, endcode, utf8))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2325            {
2326          empty_branch = TRUE;          empty_branch = TRUE;
2327        code += GET(code, 1);          break;
2328            }
2329          scode += GET(scode, 1);
2330        }        }
2331      while (*code == OP_ALT);      while (*scode == OP_ALT);
2332      if (!empty_branch) return FALSE;   /* All branches are non-empty */  
2333        if (!empty_branch) return FALSE;  /* All branches are non-empty */
2334        continue;
2335        }
2336    
2337      /* Groups with zero repeats can of course be empty; skip them. */
2338    
2339      if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2340          c == OP_BRAPOSZERO)
2341        {
2342        code += PRIV(OP_lengths)[c];
2343        do code += GET(code, 1); while (*code == OP_ALT);
2344        c = *code;
2345        continue;
2346        }
2347    
2348      /* A nested group that is already marked as "could be empty" can just be
2349      skipped. */
2350    
2351      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2352          c == OP_SCBRA || c == OP_SCBRAPOS)
2353        {
2354        do code += GET(code, 1); while (*code == OP_ALT);
2355        c = *code;
2356        continue;
2357        }
2358    
2359      /* For other groups, scan the branches. */
2360    
2361      if (c == OP_BRA  || c == OP_BRAPOS ||
2362          c == OP_CBRA || c == OP_CBRAPOS ||
2363          c == OP_ONCE || c == OP_ONCE_NC ||
2364          c == OP_COND)
2365        {
2366        BOOL empty_branch;
2367        if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
2368    
2369      /* Move past the KET and fudge things so that the increment in the "for"      /* If a conditional group has only one branch, there is a second, implied,
2370      above has no effect. */      empty branch, so just skip over the conditional, because it could be empty.
2371        Otherwise, scan the individual branches of the group. */
2372    
2373        if (c == OP_COND && code[GET(code, 1)] != OP_ALT)
2374          code += GET(code, 1);
2375        else
2376          {
2377          empty_branch = FALSE;
2378          do
2379            {
2380            if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2381              empty_branch = TRUE;
2382            code += GET(code, 1);
2383            }
2384          while (*code == OP_ALT);
2385          if (!empty_branch) return FALSE;   /* All branches are non-empty */
2386          }
2387    
2388      c = OP_END;      c = *code;
     code += 1 + LINK_SIZE - _pcre_OP_lengths[c];  
2389      continue;      continue;
2390      }      }
2391    
# Line 1399  for (code = first_significant_code(code Line 2393  for (code = first_significant_code(code
2393    
2394    switch (c)    switch (c)
2395      {      {
2396      /* Check for quantifiers after a class */      /* Check for quantifiers after a class. XCLASS is used for classes that
2397        cannot be represented just by a bit map. This includes negated single
2398        high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2399        actual length is stored in the compiled code, so we must update "code"
2400        here. */
2401    
2402  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2403      case OP_XCLASS:      case OP_XCLASS:
2404      ccode = code + GET(code, 1);      ccode = code += GET(code, 1);
2405      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
2406  #endif  #endif
2407    
2408      case OP_CLASS:      case OP_CLASS:
2409      case OP_NCLASS:      case OP_NCLASS:
2410      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2411    
2412  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2413      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
2414  #endif  #endif
2415    
# Line 1437  for (code = first_significant_code(code Line 2435  for (code = first_significant_code(code
2435    
2436      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2437    
2438        case OP_ANY:
2439        case OP_ALLANY:
2440        case OP_ANYBYTE:
2441    
2442      case OP_PROP:      case OP_PROP:
2443      case OP_NOTPROP:      case OP_NOTPROP:
2444        case OP_ANYNL:
2445    
2446        case OP_NOT_HSPACE:
2447        case OP_HSPACE:
2448        case OP_NOT_VSPACE:
2449        case OP_VSPACE:
2450      case OP_EXTUNI:      case OP_EXTUNI:
2451    
2452      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2453      case OP_DIGIT:      case OP_DIGIT:
2454      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2455      case OP_WHITESPACE:      case OP_WHITESPACE:
2456      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2457      case OP_WORDCHAR:      case OP_WORDCHAR:
2458      case OP_ANY:  
     case OP_ANYBYTE:  
2459      case OP_CHAR:      case OP_CHAR:
2460      case OP_CHARNC:      case OP_CHARI:
2461      case OP_NOT:      case OP_NOT:
2462        case OP_NOTI:
2463    
2464      case OP_PLUS:      case OP_PLUS:
2465        case OP_PLUSI:
2466      case OP_MINPLUS:      case OP_MINPLUS:
2467      case OP_POSPLUS:      case OP_MINPLUSI:
2468      case OP_EXACT:  
2469      case OP_NOTPLUS:      case OP_NOTPLUS:
2470        case OP_NOTPLUSI:
2471      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2472        case OP_NOTMINPLUSI:
2473    
2474        case OP_POSPLUS:
2475        case OP_POSPLUSI:
2476      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2477        case OP_NOTPOSPLUSI:
2478    
2479        case OP_EXACT:
2480        case OP_EXACTI:
2481      case OP_NOTEXACT:      case OP_NOTEXACT:
2482        case OP_NOTEXACTI:
2483    
2484        case OP_TYPEPLUS:
2485        case OP_TYPEMINPLUS:
2486        case OP_TYPEPOSPLUS:
2487        case OP_TYPEEXACT:
2488    
2489        return FALSE;
2490    
2491        /* These are going to continue, as they may be empty, but we have to
2492        fudge the length for the \p and \P cases. */
2493    
2494        case OP_TYPESTAR:
2495        case OP_TYPEMINSTAR:
2496        case OP_TYPEPOSSTAR:
2497        case OP_TYPEQUERY:
2498        case OP_TYPEMINQUERY:
2499        case OP_TYPEPOSQUERY:
2500        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2501        break;
2502    
2503        /* Same for these */
2504    
2505        case OP_TYPEUPTO:
2506        case OP_TYPEMINUPTO:
2507        case OP_TYPEPOSUPTO:
2508        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2509          code += 2;
2510        break;
2511    
2512        /* End of branch */
2513    
2514        case OP_KET:
2515        case OP_KETRMAX:
2516        case OP_KETRMIN:
2517        case OP_KETRPOS:
2518        case OP_ALT:
2519        return TRUE;
2520    
2521        /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2522        MINUPTO, and POSUPTO and their caseless and negative versions may be
2523        followed by a multibyte character. */
2524    
2525    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2526        case OP_STAR:
2527        case OP_STARI:
2528        case OP_NOTSTAR:
2529        case OP_NOTSTARI:
2530    
2531        case OP_MINSTAR:
2532        case OP_MINSTARI:
2533        case OP_NOTMINSTAR:
2534        case OP_NOTMINSTARI:
2535    
2536        case OP_POSSTAR:
2537        case OP_POSSTARI:
2538        case OP_NOTPOSSTAR:
2539        case OP_NOTPOSSTARI:
2540    
2541        case OP_QUERY:
2542        case OP_QUERYI:
2543        case OP_NOTQUERY:
2544        case OP_NOTQUERYI:
2545    
2546        case OP_MINQUERY:
2547        case OP_MINQUERYI:
2548        case OP_NOTMINQUERY:
2549        case OP_NOTMINQUERYI:
2550    
2551        case OP_POSQUERY:
2552        case OP_POSQUERYI:
2553        case OP_NOTPOSQUERY:
2554        case OP_NOTPOSQUERYI:
2555    
2556        if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2557        break;
2558    
2559        case OP_UPTO:
2560        case OP_UPTOI:
2561        case OP_NOTUPTO:
2562        case OP_NOTUPTOI:
2563    
2564        case OP_MINUPTO:
2565        case OP_MINUPTOI:
2566        case OP_NOTMINUPTO:
2567        case OP_NOTMINUPTOI:
2568    
2569        case OP_POSUPTO:
2570        case OP_POSUPTOI:
2571        case OP_NOTPOSUPTO:
2572        case OP_NOTPOSUPTOI:
2573    
2574        if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2575        break;
2576    #endif
2577    
2578        /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2579        string. */
2580    
2581        case OP_MARK:
2582        case OP_PRUNE_ARG:
2583        case OP_SKIP_ARG:
2584        case OP_THEN_ARG:
2585        code += code[1];
2586        break;
2587    
2588        /* None of the remaining opcodes are required to match a character. */
2589    
2590        default:
2591        break;
2592        }
2593      }
2594    
2595    return TRUE;
2596    }
2597    
2598    
2599    
2600    /*************************************************
2601    *    Scan compiled regex for non-emptiness       *
2602    *************************************************/
2603    
2604    /* This function is called to check for left recursive calls. We want to check
2605    the current branch of the current pattern to see if it could match the empty
2606    string. If it could, we must look outwards for branches at other levels,
2607    stopping when we pass beyond the bracket which is the subject of the recursion.
2608    This function is called only during the real compile, not during the
2609    pre-compile.
2610    
2611    Arguments:
2612      code        points to start of the recursion
2613      endcode     points to where to stop (current RECURSE item)
2614      bcptr       points to the chain of current (unclosed) branch starts
2615      utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2616      cd          pointers to tables etc
2617    
2618    Returns:      TRUE if what is matched could be empty
2619    */
2620    
2621    static BOOL
2622    could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2623      branch_chain *bcptr, BOOL utf, compile_data *cd)
2624    {
2625    while (bcptr != NULL && bcptr->current_branch >= code)
2626      {
2627      if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2628        return FALSE;
2629      bcptr = bcptr->outer;
2630      }
2631    return TRUE;
2632    }
2633    
2634    
2635    
2636    /*************************************************
2637    *        Base opcode of repeated opcodes         *
2638    *************************************************/
2639    
2640    /* Returns the base opcode for repeated single character type opcodes. If the
2641    opcode is not a repeated character type, it returns with the original value.
2642    
2643    Arguments:  c opcode
2644    Returns:    base opcode for the type
2645    */
2646    
2647    static pcre_uchar
2648    get_repeat_base(pcre_uchar c)
2649    {
2650    return (c > OP_TYPEPOSUPTO)? c :
2651           (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2652           (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2653           (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2654           (c >= OP_STARI)?      OP_STARI :
2655                                 OP_STAR;
2656    }
2657    
2658    
2659    
2660    #ifdef SUPPORT_UCP
2661    /*************************************************
2662    *        Check a character and a property        *
2663    *************************************************/
2664    
2665    /* This function is called by check_auto_possessive() when a property item
2666    is adjacent to a fixed character.
2667    
2668    Arguments:
2669      c            the character
2670      ptype        the property type
2671      pdata        the data for the type
2672      negated      TRUE if it's a negated property (\P or \p{^)
2673    
2674    Returns:       TRUE if auto-possessifying is OK
2675    */
2676    
2677    static BOOL
2678    check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2679      BOOL negated)
2680    {
2681    const pcre_uint32 *p;
2682    const ucd_record *prop = GET_UCD(c);
2683    
2684    switch(ptype)
2685      {
2686      case PT_LAMP:
2687      return (prop->chartype == ucp_Lu ||
2688              prop->chartype == ucp_Ll ||
2689              prop->chartype == ucp_Lt) == negated;
2690    
2691      case PT_GC:
2692      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2693    
2694      case PT_PC:
2695      return (pdata == prop->chartype) == negated;
2696    
2697      case PT_SC:
2698      return (pdata == prop->script) == negated;
2699    
2700      /* These are specials */
2701    
2702      case PT_ALNUM:
2703      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2704              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2705    
2706      /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2707      means that Perl space and POSIX space are now identical. PCRE was changed
2708      at release 8.34. */
2709    
2710      case PT_SPACE:    /* Perl space */
2711      case PT_PXSPACE:  /* POSIX space */
2712      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2713              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2714              c == CHAR_FF || c == CHAR_CR)
2715              == negated;
2716    
2717      case PT_WORD:
2718      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2719              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2720              c == CHAR_UNDERSCORE) == negated;
2721    
2722      case PT_CLIST:
2723      p = PRIV(ucd_caseless_sets) + prop->caseset;
2724      for (;;)
2725        {
2726        if (c < *p) return !negated;
2727        if (c == *p++) return negated;
2728        }
2729      break;  /* Control never reaches here */
2730      }
2731    
2732    return FALSE;
2733    }
2734    #endif  /* SUPPORT_UCP */
2735    
2736    
2737    
2738    /*************************************************
2739    *        Fill the character property list        *
2740    *************************************************/
2741    
2742    /* Checks whether the code points to an opcode that can take part in auto-
2743    possessification, and if so, fills a list with its properties.
2744    
2745    Arguments:
2746      code        points to start of expression
2747      utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2748      fcc         points to case-flipping table
2749      list        points to output list
2750                  list[0] will be filled with the opcode
2751                  list[1] will be non-zero if this opcode
2752                    can match an empty character string
2753                  list[2..7] depends on the opcode
2754    
2755    Returns:      points to the start of the next opcode if *code is accepted
2756                  NULL if *code is not accepted
2757    */
2758    
2759    static const pcre_uchar *
2760    get_chr_property_list(const pcre_uchar *code, BOOL utf,
2761      const pcre_uint8 *fcc, pcre_uint32 *list)
2762    {
2763    pcre_uchar c = *code;
2764    const pcre_uchar *end;
2765    const pcre_uint32 *clist_src;
2766    pcre_uint32 *clist_dest;
2767    pcre_uint32 chr;
2768    pcre_uchar base;
2769    
2770    list[0] = c;
2771    list[1] = FALSE;
2772    code++;
2773    
2774    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2775      {
2776      base = get_repeat_base(c);
2777      c -= (base - OP_STAR);
2778    
2779      if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2780        code += IMM2_SIZE;
2781    
2782      list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
2783    
2784      switch(base)
2785        {
2786        case OP_STAR:
2787        list[0] = OP_CHAR;
2788        break;
2789    
2790        case OP_STARI:
2791        list[0] = OP_CHARI;
2792        break;
2793    
2794        case OP_NOTSTAR:
2795        list[0] = OP_NOT;
2796        break;
2797    
2798        case OP_NOTSTARI:
2799        list[0] = OP_NOTI;
2800        break;
2801    
2802        case OP_TYPESTAR:
2803        list[0] = *code;
2804        code++;
2805        break;
2806        }
2807      c = list[0];
2808      }
2809    
2810    switch(c)
2811      {
2812      case OP_NOT_DIGIT:
2813      case OP_DIGIT:
2814      case OP_NOT_WHITESPACE:
2815      case OP_WHITESPACE:
2816      case OP_NOT_WORDCHAR:
2817      case OP_WORDCHAR:
2818      case OP_ANY:
2819      case OP_ALLANY:
2820      case OP_ANYNL:
2821      case OP_NOT_HSPACE:
2822      case OP_HSPACE:
2823      case OP_NOT_VSPACE:
2824      case OP_VSPACE:
2825      case OP_EXTUNI:
2826      case OP_EODN:
2827      case OP_EOD:
2828      case OP_DOLL:
2829      case OP_DOLLM:
2830      return code;
2831    
2832      case OP_CHAR:
2833      case OP_NOT:
2834      GETCHARINCTEST(chr, code);
2835      list[2] = chr;
2836      list[3] = NOTACHAR;
2837      return code;
2838    
2839      case OP_CHARI:
2840      case OP_NOTI:
2841      list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2842      GETCHARINCTEST(chr, code);
2843      list[2] = chr;
2844    
2845    #ifdef SUPPORT_UCP
2846      if (chr < 128 || (chr < 256 && !utf))
2847        list[3] = fcc[chr];
2848      else
2849        list[3] = UCD_OTHERCASE(chr);
2850    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2851      list[3] = (chr < 256) ? fcc[chr] : chr;
2852    #else
2853      list[3] = fcc[chr];
2854    #endif
2855    
2856      /* The othercase might be the same value. */
2857    
2858      if (chr == list[3])
2859        list[3] = NOTACHAR;
2860      else
2861        list[4] = NOTACHAR;
2862      return code;
2863    
2864    #ifdef SUPPORT_UCP
2865      case OP_PROP:
2866      case OP_NOTPROP:
2867      if (code[0] != PT_CLIST)
2868        {
2869        list[2] = code[0];
2870        list[3] = code[1];
2871        return code + 2;
2872        }
2873    
2874      /* Convert only if we have anough space. */
2875    
2876      clist_src = PRIV(ucd_caseless_sets) + code[1];
2877      clist_dest = list + 2;
2878      code += 2;
2879    
2880      do {
2881         /* Early return if there is not enough space. */
2882         if (clist_dest >= list + 8)
2883           {
2884           list[2] = code[0];
2885           list[3] = code[1];
2886           return code;
2887           }
2888         *clist_dest++ = *clist_src;
2889         }
2890       while(*clist_src++ != NOTACHAR);
2891    
2892      /* Enough space to store all characters. */
2893    
2894      list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2895      return code;
2896    #endif
2897    
2898      case OP_NCLASS:
2899      case OP_CLASS:
2900    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2901      case OP_XCLASS:
2902    
2903      if (c == OP_XCLASS)
2904        end = code + GET(code, 0);
2905      else
2906    #endif
2907        end = code + 32 / sizeof(pcre_uchar);
2908    
2909      switch(*end)
2910        {
2911        case OP_CRSTAR:
2912        case OP_CRMINSTAR:
2913        case OP_CRQUERY:
2914        case OP_CRMINQUERY:
2915        list[1] = TRUE;
2916        end++;
2917        break;
2918    
2919        case OP_CRRANGE:
2920        case OP_CRMINRANGE:
2921        list[1] = (GET2(end, 1) == 0);
2922        end += 1 + 2 * IMM2_SIZE;
2923        break;
2924        }
2925      list[2] = end - code;
2926      return end;
2927      }
2928    return NULL;    /* Opcode not accepted */
2929    }
2930    
2931    
2932    
2933    /*************************************************
2934    *    Scan further character sets for match       *
2935    *************************************************/
2936    
2937    /* Checks whether the base and the current opcode have a common character, in
2938    which case the base cannot be possessified.
2939    
2940    Arguments:
2941      code        points to the byte code
2942      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2943      cd          static compile data
2944      base_list   the data list of the base opcode
2945    
2946    Returns:      TRUE if the auto-possessification is possible
2947    */
2948    
2949    static BOOL
2950    compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2951      const pcre_uint32* base_list)
2952    {
2953    pcre_uchar c;
2954    pcre_uint32 list[8];
2955    const pcre_uint32* chr_ptr;
2956    const pcre_uint32* ochr_ptr;
2957    const pcre_uint32* list_ptr;
2958    pcre_uint32 chr;
2959    
2960    for(;;)
2961      {
2962      c = *code;
2963    
2964      /* Skip over callouts */
2965    
2966      if (c == OP_CALLOUT)
2967        {
2968        code += PRIV(OP_lengths)[c];
2969        continue;
2970        }
2971    
2972      if (c == OP_ALT)
2973        {
2974        do code += GET(code, 1); while (*code == OP_ALT);
2975        c = *code;
2976        }
2977    
2978      switch(c)
2979        {
2980        case OP_END:
2981        /* TRUE only in greedy case. The non-greedy case could be replaced by an
2982        OP_EXACT, but it is probably not worth it. (And note that OP_EXACT uses
2983        more memory, which we cannot get at this stage.) */
2984    
2985        return base_list[1] != 0;
2986    
2987        case OP_KET:
2988        /* If the bracket is capturing, and referenced by an OP_RECURSE, the
2989        non-greedy case cannot be converted to a possessive form. We do not test
2990        the bracket type at the moment, but we might do it in the future to improve
2991        this condition. (But note that recursive calls are always atomic.) */
2992    
2993        if (base_list[1] == 0) return FALSE;
2994        code += PRIV(OP_lengths)[c];
2995        continue;
2996        }
2997    
2998      /* Check for a supported opcode, and load its properties. */
2999    
3000      code = get_chr_property_list(code, utf, cd->fcc, list);
3001      if (code == NULL) return FALSE;    /* Unsupported */
3002    
3003      /* If either opcode is a small character list, set pointers for comparing
3004      characters from that list with another list, or with a property. */
3005    
3006      if (base_list[0] == OP_CHAR)
3007        {
3008        chr_ptr = base_list + 2;
3009        list_ptr = list;
3010        }
3011      else if (list[0] == OP_CHAR)
3012        {
3013        chr_ptr = list + 2;
3014        list_ptr = base_list;
3015        }
3016    
3017      /* Some property combinations also acceptable. Unicode property opcodes are
3018      processed specially; the rest can be handled with a lookup table. */
3019    
3020      else
3021        {
3022        pcre_uint32 leftop, rightop;
3023    
3024        if (list[1] != 0) return FALSE;   /* Must match at least one character */
3025        leftop = base_list[0];
3026        rightop = list[0];
3027    
3028    #ifdef SUPPORT_UCP
3029        if (leftop == OP_PROP || leftop == OP_NOTPROP)
3030          {
3031          if (rightop == OP_EOD) return TRUE;
3032          if (rightop == OP_PROP || rightop == OP_NOTPROP)
3033            {
3034            int n;
3035            const pcre_uint8 *p;
3036            BOOL same = leftop == rightop;
3037            BOOL lisprop = leftop == OP_PROP;
3038            BOOL risprop = rightop == OP_PROP;
3039            BOOL bothprop = lisprop && risprop;
3040    
3041            /* There's a table that specifies how each combination is to be
3042            processed:
3043              0   Always return FALSE (never auto-possessify)
3044              1   Character groups are distinct (possessify if both are OP_PROP)
3045              2   Check character categories in the same group (general or particular)
3046              3   Return TRUE if the two opcodes are not the same
3047              ... see comments below
3048            */
3049    
3050            n = propposstab[base_list[2]][list[2]];
3051            switch(n)
3052              {
3053              case 0: return FALSE;
3054              case 1: return bothprop;
3055              case 2: return (base_list[3] == list[3]) != same;
3056              case 3: return !same;
3057    
3058              case 4:  /* Left general category, right particular category */
3059              return risprop && catposstab[base_list[3]][list[3]] == same;
3060    
3061              case 5:  /* Right general category, left particular category */
3062              return lisprop && catposstab[list[3]][base_list[3]] == same;
3063    
3064              /* This code is logically tricky. Think hard before fiddling with it.
3065              The posspropstab table has four entries per row. Each row relates to
3066              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3067              Only WORD actually needs all four entries, but using repeats for the
3068              others means they can all use the same code below.
3069    
3070              The first two entries in each row are Unicode general categories, and
3071              apply always, because all the characters they include are part of the
3072              PCRE character set. The third and fourth entries are a general and a
3073              particular category, respectively, that include one or more relevant
3074              characters. One or the other is used, depending on whether the check
3075              is for a general or a particular category. However, in both cases the
3076              category contains more characters than the specials that are defined
3077              for the property being tested against. Therefore, it cannot be used
3078              in a NOTPROP case.
3079    
3080              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3081              Underscore is covered by ucp_P or ucp_Po. */
3082    
3083              case 6:  /* Left alphanum vs right general category */
3084              case 7:  /* Left space vs right general category */
3085              case 8:  /* Left word vs right general category */
3086              p = posspropstab[n-6];
3087              return risprop && lisprop ==
3088                (list[3] != p[0] &&
3089                 list[3] != p[1] &&
3090                (list[3] != p[2] || !lisprop));
3091    
3092              case 9:   /* Right alphanum vs left general category */
3093              case 10:  /* Right space vs left general category */
3094              case 11:  /* Right word vs left general category */
3095              p = posspropstab[n-9];
3096              return lisprop && risprop ==
3097                (base_list[3] != p[0] &&
3098                 base_list[3] != p[1] &&
3099                (base_list[3] != p[2] || !risprop));
3100    
3101              case 12:  /* Left alphanum vs right particular category */
3102              case 13:  /* Left space vs right particular category */
3103              case 14:  /* Left word vs right particular category */
3104              p = posspropstab[n-12];
3105              return risprop && lisprop ==
3106                (catposstab[p[0]][list[3]] &&
3107                 catposstab[p[1]][list[3]] &&
3108                (list[3] != p[3] || !lisprop));
3109    
3110              case 15:  /* Right alphanum vs left particular category */
3111              case 16:  /* Right space vs left particular category */
3112              case 17:  /* Right word vs left particular category */
3113              p = posspropstab[n-15];
3114              return lisprop && risprop ==
3115                (catposstab[p[0]][base_list[3]] &&
3116                 catposstab[p[1]][base_list[3]] &&
3117                (base_list[3] != p[3] || !risprop));
3118              }
3119            }
3120          return FALSE;
3121          }
3122    
3123        else
3124    #endif  /* SUPPORT_UCP */
3125    
3126        return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3127               rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3128               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3129        }
3130    
3131      /* Control reaches here only if one of the items is a small character list.
3132      All characters are checked against the other side. */
3133    
3134      do
3135        {
3136        chr = *chr_ptr;
3137    
3138        switch(list_ptr[0])
3139          {
3140          case OP_CHAR:
3141          ochr_ptr = list_ptr + 2;
3142          do
3143            {
3144            if (chr == *ochr_ptr) return FALSE;
3145            ochr_ptr++;
3146            }
3147          while(*ochr_ptr != NOTACHAR);
3148          break;
3149    
3150          case OP_NOT:
3151          ochr_ptr = list_ptr + 2;
3152          do
3153            {
3154            if (chr == *ochr_ptr)
3155              break;
3156            ochr_ptr++;
3157            }
3158          while(*ochr_ptr != NOTACHAR);
3159          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3160          break;
3161    
3162          /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3163          set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3164    
3165          case OP_DIGIT:
3166          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3167          break;
3168    
3169          case OP_NOT_DIGIT:
3170          if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3171          break;
3172    
3173          case OP_WHITESPACE:
3174          if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3175          break;
3176    
3177          case OP_NOT_WHITESPACE:
3178          if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3179          break;
3180    
3181          case OP_WORDCHAR:
3182          if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3183          break;
3184    
3185          case OP_NOT_WORDCHAR:
3186          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3187          break;
3188    
3189          case OP_HSPACE:
3190          switch(chr)
3191            {
3192            HSPACE_CASES: return FALSE;
3193            default: break;
3194            }
3195          break;
3196    
3197          case OP_NOT_HSPACE:
3198          switch(chr)
3199            {
3200            HSPACE_CASES: break;
3201            default: return FALSE;
3202            }
3203          break;
3204    
3205          case OP_ANYNL:
3206          case OP_VSPACE:
3207          switch(chr)
3208            {
3209            VSPACE_CASES: return FALSE;
3210            default: break;
3211            }
3212          break;
3213    
3214          case OP_NOT_VSPACE:
3215          switch(chr)
3216            {
3217            VSPACE_CASES: break;
3218            default: return FALSE;
3219            }
3220          break;
3221    
3222          case OP_DOLL:
3223          case OP_EODN:
3224          switch (chr)
3225            {
3226            case CHAR_CR:
3227            case CHAR_LF:
3228            case CHAR_VT:
3229            case CHAR_FF:
3230            case CHAR_NEL:
3231    #ifndef EBCDIC
3232            case 0x2028:
3233            case 0x2029:
3234    #endif  /* Not EBCDIC */
3235            return FALSE;
3236            }
3237          break;
3238    
3239          case OP_EOD:    /* Can always possessify before \z */
3240          break;
3241    
3242          case OP_PROP:
3243          case OP_NOTPROP:
3244          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3245                list_ptr[0] == OP_NOTPROP))
3246            return FALSE;
3247          break;
3248    
3249          /* The class comparisons work only when the class is the second item
3250          of the pair, because there are at present no possessive forms of the
3251          class opcodes. Note also that the "code" variable that is used below
3252          points after the second item, and that the pointer for the first item
3253          is not available, so even if there were possessive forms of the class
3254          opcodes, the correct comparison could not be done. */
3255    
3256          case OP_NCLASS:
3257          if (chr > 255) return FALSE;
3258          /* Fall through */
3259    
3260          case OP_CLASS:
3261          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3262          if (chr > 255) break;
3263          if ((((pcre_uint8 *)(code - list_ptr[2] + 1))[chr >> 3] & (1 << (chr & 7))) != 0)
3264            return FALSE;
3265          break;
3266    
3267    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3268          case OP_XCLASS:
3269          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3270          if (PRIV(xclass)(chr, code - list_ptr[2] + 1 + LINK_SIZE, utf))
3271            return FALSE;
3272          break;
3273    #endif
3274    
3275          default:
3276          return FALSE;
3277          }
3278    
3279        chr_ptr++;
3280        }
3281      while(*chr_ptr != NOTACHAR);
3282    
3283      /* At least one character must be matched from this opcode. */
3284    
3285      if (list[1] == 0) return TRUE;
3286      }
3287    
3288    return FALSE;
3289    }
3290    
3291    
3292    
3293    /*************************************************
3294    *    Scan compiled regex for auto-possession     *
3295    *************************************************/
3296    
3297    /* Replaces single character iterations with their possessive alternatives
3298    if appropriate. This function modifies the compiled opcode!
3299    
3300    Arguments:
3301      code        points to start of the byte code
3302      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3303      cd          static compile data
3304    
3305    Returns:      nothing
3306    */
3307    
3308    static void
3309    auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
3310    {
3311    register pcre_uchar c;
3312    const pcre_uchar *end;
3313    pcre_uint32 list[8];
3314    
3315    for (;;)
3316      {
3317      c = *code;
3318    
3319      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3320        {
3321        c -= get_repeat_base(c) - OP_STAR;
3322        end = (c <= OP_MINUPTO) ?
3323          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3324        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3325    
3326        if (end != NULL && compare_opcodes(end, utf, cd, list))
3327          {
3328          switch(c)
3329            {
3330            case OP_STAR:
3331            *code += OP_POSSTAR - OP_STAR;
3332            break;
3333    
3334            case OP_MINSTAR:
3335            *code += OP_POSSTAR - OP_MINSTAR;
3336            break;
3337    
3338            case OP_PLUS:
3339            *code += OP_POSPLUS - OP_PLUS;
3340            break;
3341    
3342            case OP_MINPLUS:
3343            *code += OP_POSPLUS - OP_MINPLUS;
3344            break;
3345    
3346            case OP_QUERY:
3347            *code += OP_POSQUERY - OP_QUERY;
3348            break;
3349    
3350            case OP_MINQUERY:
3351            *code += OP_POSQUERY - OP_MINQUERY;
3352            break;
3353    
3354            case OP_UPTO:
3355            *code += OP_POSUPTO - OP_UPTO;
3356            break;
3357    
3358            case OP_MINUPTO:
3359            *code += OP_MINUPTO - OP_UPTO;
3360            break;
3361            }
3362          }
3363        c = *code;
3364        }
3365    
3366      switch(c)
3367        {
3368        case OP_END:
3369        return;
3370    
3371        case OP_TYPESTAR:
3372        case OP_TYPEMINSTAR:
3373      case OP_TYPEPLUS:      case OP_TYPEPLUS:
3374      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
3375        case OP_TYPEQUERY:
3376        case OP_TYPEMINQUERY:
3377        case OP_TYPEPOSSTAR:
3378      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
3379        case OP_TYPEPOSQUERY:
3380        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3381        break;
3382    
3383        case OP_TYPEUPTO:
3384        case OP_TYPEMINUPTO:
3385      case OP_TYPEEXACT:      case OP_TYPEEXACT:
3386      return FALSE;      case OP_TYPEPOSUPTO:
3387        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3388          code += 2;
3389        break;
3390    
3391      /* End of branch */      case OP_XCLASS:
3392        code += GET(code, 1);
3393        break;
3394    
3395      case OP_KET:      case OP_MARK:
3396      case OP_KETRMAX:      case OP_PRUNE_ARG:
3397      case OP_KETRMIN:      case OP_SKIP_ARG:
3398      case OP_ALT:      case OP_THEN_ARG:
3399      return TRUE;      code += code[1];
3400        break;
3401        }
3402    
3403      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,    /* Add in the fixed length from the table */
3404      MINUPTO, and POSUPTO may be followed by a multibyte character */  
3405      code += PRIV(OP_lengths)[c];
3406    
3407  #ifdef SUPPORT_UTF8    /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3408      a multi-byte character. The length in the table is a minimum, so we have to
3409      arrange to skip the extra bytes. */
3410    
3411    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3412      if (utf) switch(c)
3413        {
3414        case OP_CHAR:
3415        case OP_CHARI:
3416        case OP_NOT:
3417        case OP_NOTI:
3418      case OP_STAR:      case OP_STAR:
3419      case OP_MINSTAR:      case OP_MINSTAR:
3420      case OP_POSSTAR:      case OP_PLUS:
3421        case OP_MINPLUS:
3422      case OP_QUERY:      case OP_QUERY:
3423      case OP_MINQUERY:      case OP_MINQUERY:
     case OP_POSQUERY:  
3424      case OP_UPTO:      case OP_UPTO:
3425      case OP_MINUPTO:      case OP_MINUPTO:
3426        case OP_EXACT:
3427        case OP_POSSTAR:
3428        case OP_POSPLUS:
3429        case OP_POSQUERY:
3430      case OP_POSUPTO:      case OP_POSUPTO:
3431      if (utf8) while ((code[2] & 0xc0) == 0x80) code++;      case OP_STARI:
3432        case OP_MINSTARI:
3433        case OP_PLUSI:
3434        case OP_MINPLUSI:
3435        case OP_QUERYI:
3436        case OP_MINQUERYI:
3437        case OP_UPTOI:
3438        case OP_MINUPTOI:
3439        case OP_EXACTI:
3440        case OP_POSSTARI:
3441        case OP_POSPLUSI:
3442        case OP_POSQUERYI:
3443        case OP_POSUPTOI:
3444        case OP_NOTSTAR:
3445        case OP_NOTMINSTAR:
3446        case OP_NOTPLUS:
3447        case OP_NOTMINPLUS:
3448        case OP_NOTQUERY:
3449        case OP_NOTMINQUERY:
3450        case OP_NOTUPTO:
3451        case OP_NOTMINUPTO:
3452        case OP_NOTEXACT:
3453        case OP_NOTPOSSTAR:
3454        case OP_NOTPOSPLUS:
3455        case OP_NOTPOSQUERY:
3456        case OP_NOTPOSUPTO:
3457        case OP_NOTSTARI:
3458        case OP_NOTMINSTARI:
3459        case OP_NOTPLUSI:
3460        case OP_NOTMINPLUSI:
3461        case OP_NOTQUERYI:
3462        case OP_NOTMINQUERYI:
3463        case OP_NOTUPTOI:
3464        case OP_NOTMINUPTOI:
3465        case OP_NOTEXACTI:
3466        case OP_NOTPOSSTARI:
3467        case OP_NOTPOSPLUSI:
3468        case OP_NOTPOSQUERYI:
3469        case OP_NOTPOSUPTOI:
3470        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3471      break;      break;
 #endif  
3472      }      }
3473    #else
3474      (void)(utf);  /* Keep compiler happy by referencing function argument */
3475    #endif
3476    }    }
   
 return TRUE;  
 }  
   
   
   
 /*************************************************  
 *    Scan compiled regex for non-emptiness       *  
 *************************************************/  
   
 /* This function is called to check for left recursive calls. We want to check  
 the current branch of the current pattern to see if it could match the empty  
 string. If it could, we must look outwards for branches at other levels,  
 stopping when we pass beyond the bracket which is the subject of the recursion.  
   
 Arguments:  
   code        points to start of the recursion  
   endcode     points to where to stop (current RECURSE item)  
   bcptr       points to the chain of current (unclosed) branch starts  
   utf8        TRUE if in UTF-8 mode  
   
 Returns:      TRUE if what is matched could be empty  
 */  
   
 static BOOL  
 could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  
   BOOL utf8)  
 {  
 while (bcptr != NULL && bcptr->current >= code)  
   {  
   if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;  
   bcptr = bcptr->outer;  
   }  
 return TRUE;  
3477  }  }
3478    
3479    
# Line 1534  return TRUE; Line 3483  return TRUE;
3483  *************************************************/  *************************************************/
3484    
3485  /* This function is called when the sequence "[:" or "[." or "[=" is  /* This function is called when the sequence "[:" or "[." or "[=" is
3486  encountered in a character class. It checks whether this is followed by an  encountered in a character class. It checks whether this is followed by a
3487  optional ^ and then a sequence of letters, terminated by a matching ":]" or  sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3488  ".]" or "=]".  reach an unescaped ']' without the special preceding character, return FALSE.
3489    
3490    Originally, this function only recognized a sequence of letters between the
3491    terminators, but it seems that Perl recognizes any sequence of characters,
3492    though of course unknown POSIX names are subsequently rejected. Perl gives an
3493    "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3494    didn't consider this to be a POSIX class. Likewise for [:1234:].
3495    
3496    The problem in trying to be exactly like Perl is in the handling of escapes. We
3497    have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3498    class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3499    below handles the special case of \], but does not try to do any other escape
3500    processing. This makes it different from Perl for cases such as [:l\ower:]
3501    where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
3502    "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
3503    I think.
3504    
3505    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3506    It seems that the appearance of a nested POSIX class supersedes an apparent
3507    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3508    a digit.
3509    
3510    In Perl, unescaped square brackets may also appear as part of class names. For
3511    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3512    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3513    seem right at all. PCRE does not allow closing square brackets in POSIX class
3514    names.
3515    
3516  Argument:  Arguments:
3517    ptr      pointer to the initial [    ptr      pointer to the initial [
3518    endptr   where to return the end pointer    endptr   where to return the end pointer
   cd       pointer to compile data  
3519    
3520  Returns:   TRUE or FALSE  Returns:   TRUE or FALSE
3521  */  */
3522    
3523  static BOOL  static BOOL
3524  check_posix_syntax(const uschar *ptr, const uschar **endptr, compile_data *cd)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3525  {  {
3526  int terminator;          /* Don't combine these lines; the Solaris cc */  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3527  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3528  if (*(++ptr) == '^') ptr++;  for (++ptr; *ptr != CHAR_NULL; ptr++)
 while ((cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;  
 if (*ptr == terminator && ptr[1] == ']')  
3529    {    {
3530    *endptr = ptr;    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3531    return TRUE;      ptr++;
3532      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3533      else
3534        {
3535        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3536          {
3537          *endptr = ptr;
3538          return TRUE;
3539          }
3540        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3541             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3542              ptr[1] == CHAR_EQUALS_SIGN) &&
3543            check_posix_syntax(ptr, endptr))
3544          return FALSE;
3545        }
3546    }    }
3547  return FALSE;  return FALSE;
3548  }  }
# Line 1579  Returns:     a value representing the na Line 3565  Returns:     a value representing the na
3565