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

Diff of /code/trunk/pcre_compile.c

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

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