/[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 773 by ph10, Wed Nov 30 18:10:27 2011 UTC revision 1361 by ph10, Fri Sep 6 17:47:32 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-2011 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 PCRE_DEBUG is defined, we need the pcre_printint() function, which is  /* When PCRE_DEBUG is defined, we need the pcre(16|32)_printint() function, which
57  also used by pcretest. PCRE_DEBUG is not defined when building a production  is also used by pcretest. PCRE_DEBUG is not defined when building a production
58  library. */  library. We do not need to select pcre16_printint.c specially, because the
59    COMPILE_PCREx macro will already be appropriately set. */
60    
61  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
62  #include "pcre_printint.src"  /* pcre_printint.c should not include any headers */
63    #define PCRE_INCLUDED
64    #include "pcre_printint.c"
65    #undef PCRE_INCLUDED
66  #endif  #endif
67    
68    
69  /* Macro for setting individual bits in class bitmaps. */  /* Macro for setting individual bits in class bitmaps. */
70    
71  #define SETBIT(a,b) a[b/8] |= (1 << (b%8))  #define SETBIT(a,b) a[(b)/8] |= (1 << ((b)&7))
72    
73  /* Maximum length value to check against when making sure that the integer that  /* Maximum length value to check against when making sure that the integer that
74  holds the compiled pattern length does not overflow. We make it a bit less than  holds the compiled pattern length does not overflow. We make it a bit less than
# Line 73  to check them every time. */ Line 77  to check them every time. */
77    
78  #define OFLOW_MAX (INT_MAX - 20)  #define OFLOW_MAX (INT_MAX - 20)
79    
80    /* Definitions to allow mutual recursion */
81    
82    static int
83      add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
84        const pcre_uint32 *, unsigned int);
85    
86    static BOOL
87      compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
88        pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,
89        compile_data *, int *);
90    
91    
92    
93  /*************************************************  /*************************************************
94  *      Code parameters and static tables         *  *      Code parameters and static tables         *
# Line 88  so this number is very generous. Line 104  so this number is very generous.
104  The same workspace is used during the second, actual compile phase for  The same workspace is used during the second, actual compile phase for
105  remembering forward references to groups so that they can be filled in at the  remembering forward references to groups so that they can be filled in at the
106  end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE  end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
107  is 4 there is plenty of room for most patterns. However, the memory can get  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  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  /(?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  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  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()  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. */  kicks in at the same number of forward references in all cases. */
114    
115  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
117    
118    /* This value determines the size of the initial vector that is used for
119    remembering named groups during the pre-compile. It is allocated on the stack,
120    but if it is too small, it is expanded using malloc(), in a similar way to the
121    workspace. The value is the number of slots in the list. */
122    
123    #define NAMED_GROUP_LIST_SIZE  20
124    
125  /* The overrun tests check for a slightly smaller size so that they detect the  /* The overrun tests check for a slightly smaller size so that they detect the
126  overrun before it actually does run off the end of the data block. */  overrun before it actually does run off the end of the data block. */
127    
128  #define WORK_SIZE_SAFETY_MARGIN (100)  #define WORK_SIZE_SAFETY_MARGIN (100)
129    
130    /* Private flags added to firstchar and reqchar. */
131    
132    #define REQ_CASELESS    (1 << 0)        /* Indicates caselessness */
133    #define REQ_VARY        (1 << 1)        /* Reqchar followed non-literal item */
134    /* Negative values for the firstchar and reqchar flags */
135    #define REQ_UNSET       (-2)
136    #define REQ_NONE        (-1)
137    
138    /* Repeated character flags. */
139    
140    #define UTF_LENGTH     0x10000000l      /* The char contains its length. */
141    
142  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
143  are simple data values; negative values are for special things like \d and so  are simple data values; negative values are for special things like \d and so
# Line 238  static const char posix_names[] = Line 272  static const char posix_names[] =
272    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
273    STRING_word0  STRING_xdigit;    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 273  substitutes must be in the order of the Line 307  substitutes must be in the order of the
307  both positive and negative cases. NULL means no substitute. */  both positive and negative cases. NULL means no substitute. */
308    
309  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
310  static const uschar *substitutes[] = {  static const pcre_uchar string_PNd[]  = {
311    (uschar *)"\\P{Nd}",    /* \D */    CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
312    (uschar *)"\\p{Nd}",    /* \d */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
313    (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */  static const pcre_uchar string_pNd[]  = {
314    (uschar *)"\\p{Xsp}",   /* \s */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
315    (uschar *)"\\P{Xwd}",   /* \W */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
316    (uschar *)"\\p{Xwd}"    /* \w */  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 uschar *posix_substitutes[] = {  static const pcre_uchar string_pL[] =   {
339    (uschar *)"\\p{L}",     /* alpha */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
340    (uschar *)"\\p{Ll}",    /* lower */    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
341    (uschar *)"\\p{Lu}",    /* upper */  static const pcre_uchar string_pLl[] =  {
342    (uschar *)"\\p{Xan}",   /* alnum */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
343    NULL,                   /* ascii */    CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
344    (uschar *)"\\h",        /* blank */  static const pcre_uchar string_pLu[] =  {
345    NULL,                   /* cntrl */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
346    (uschar *)"\\p{Nd}",    /* digit */    CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
347    NULL,                   /* graph */  static const pcre_uchar string_pXan[] = {
348    NULL,                   /* print */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
349    NULL,                   /* punct */    CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
350    (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */  static const pcre_uchar string_h[] =    {
351    (uschar *)"\\p{Xwd}",   /* word */    CHAR_BACKSLASH, CHAR_h, '\0' };
352    NULL,                   /* xdigit */  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 */    /* Negated cases */
389    (uschar *)"\\P{L}",     /* ^alpha */    string_PL,            /* ^alpha */
390    (uschar *)"\\P{Ll}",    /* ^lower */    string_PLl,           /* ^lower */
391    (uschar *)"\\P{Lu}",    /* ^upper */    string_PLu,           /* ^upper */
392    (uschar *)"\\P{Xan}",   /* ^alnum */    string_PXan,          /* ^alnum */
393    NULL,                   /* ^ascii */    NULL,                 /* ^ascii */
394    (uschar *)"\\H",        /* ^blank */    string_H,             /* ^blank */
395    NULL,                   /* ^cntrl */    NULL,                 /* ^cntrl */
396    (uschar *)"\\P{Nd}",    /* ^digit */    string_PNd,           /* ^digit */
397    NULL,                   /* ^graph */    NULL,                 /* ^graph */
398    NULL,                   /* ^print */    NULL,                 /* ^print */
399    NULL,                   /* ^punct */    NULL,                 /* ^punct */
400    (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */    string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
401    (uschar *)"\\P{Xwd}",   /* ^word */    string_PXwd,          /* ^word */
402    NULL                    /* ^xdigit */    NULL                  /* ^xdigit */
403  };  };
404  #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
405  #endif  #endif
406    
407  #define STRING(a)  # a  #define STRING(a)  # a
# Line 372  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 */
# Line 395  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"
# Line 406  static const char error_texts[] = Line 494  static const char error_texts[] =
494    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
495    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\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 (?+\0"    "digit expected after (?+\0"
# Line 414  static const char error_texts[] = Line 502  static const char error_texts[] =
502    /* 65 */    /* 65 */
503    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
504    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
505    "this version of PCRE is not compiled with PCRE_UCP support\0"    "this version of PCRE is not compiled with Unicode property support\0"
506    "\\c must be followed by an ASCII character\0"    "\\c must be followed by an ASCII character\0"
507    "\\k is not followed by a braced, angle-bracketed, or quoted name\0"    "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
508    /* 70 */    /* 70 */
509    "internal error: unknown opcode in find_fixedlength()\0"    "internal error: unknown opcode in find_fixedlength()\0"
510    "\\N is not supported in a class\0"    "\\N is not supported in a class\0"
511    "too many forward references\0"    "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
# Line 439  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    /* Using a simple comparison for decimal numbers rather than a memory read
538    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  #ifndef EBCDIC
544    
545  /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in  /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
546  UTF-8 mode. */  UTF-8 mode. */
547    
548  static const unsigned char digitab[] =  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 483  static const unsigned char digitab[] = Line 584  static const unsigned char digitab[] =
584    
585  /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */  /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
586    
587  static const unsigned char digitab[] =  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 518  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 554  static const unsigned char ebcdic_charta Line 655  static const unsigned char ebcdic_charta
655  #endif  #endif
656    
657    
 /* Definition to allow mutual recursion */  
   
 static BOOL  
   compile_regex(int, uschar **, const uschar **, int *, BOOL, BOOL, int, int,  
     int *, int *, branch_chain *, compile_data *, int *);  
   
   
658    
659  /*************************************************  /*************************************************
660  *            Find an error text                  *  *            Find an error text                  *
# Line 581  find_error_text(int n) Line 675  find_error_text(int n)
675  const char *s = error_texts;  const char *s = error_texts;
676  for (; n > 0; n--)  for (; n > 0; n--)
677    {    {
678    while (*s++ != 0) {};    while (*s++ != CHAR_NULL) {};
679    if (*s == 0) return "Error text not found (please report)";    if (*s == CHAR_NULL) return "Error text not found (please report)";
680    }    }
681  return s;  return s;
682  }  }
# Line 592  return s; Line 686  return s;
686  *           Expand the workspace                 *  *           Expand the workspace                 *
687  *************************************************/  *************************************************/
688    
689  /* This function is called during the second compiling phase, if the number of  /* This function is called during the second compiling phase, if the number of
690  forward references fills the existing workspace, which is originally a block on  forward references fills the existing workspace, which is originally a block on
691  the stack. A larger block is obtained from malloc() unless the ultimate limit  the stack. A larger block is obtained from malloc() unless the ultimate limit
692  has been reached or the increase will be rather small.  has been reached or the increase will be rather small.
693    
694  Argument: pointer to the compile data block  Argument: pointer to the compile data block
# Line 604  Returns:  0 if all went well, else an er Line 698  Returns:  0 if all went well, else an er
698  static int  static int
699  expand_workspace(compile_data *cd)  expand_workspace(compile_data *cd)
700  {  {
701  uschar *newspace;  pcre_uchar *newspace;
702  int newsize = cd->workspace_size * 2;  int newsize = cd->workspace_size * 2;
703    
704  if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;  if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
# Line 612  if (cd->workspace_size >= COMPILE_WORK_S Line 706  if (cd->workspace_size >= COMPILE_WORK_S
706      newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)      newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
707   return ERR72;   return ERR72;
708    
709  newspace = (pcre_malloc)(newsize);  newspace = (PUBL(malloc))(IN_UCHARS(newsize));
710  if (newspace == NULL) return ERR21;  if (newspace == NULL) return ERR21;
711    memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
712  memcpy(newspace, cd->start_workspace, cd->workspace_size);  cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
713  cd->hwm = (uschar *)newspace + (cd->hwm - cd->start_workspace);  if (cd->workspace_size > COMPILE_WORK_SIZE)
714  if (cd->workspace_size > COMPILE_WORK_SIZE)    (PUBL(free))((void *)cd->start_workspace);
   (pcre_free)((void *)cd->start_workspace);  
715  cd->start_workspace = newspace;  cd->start_workspace = newspace;
716  cd->workspace_size = newsize;  cd->workspace_size = newsize;
717  return 0;  return 0;
# Line 642  Returns:    TRUE or FALSE Line 735  Returns:    TRUE or FALSE
735  */  */
736    
737  static BOOL  static BOOL
738  is_counted_repeat(const uschar *p)  is_counted_repeat(const pcre_uchar *p)
739  {  {
740  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
741  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
742    while (IS_DIGIT(*p)) p++;
743  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
744    
745  if (*p++ != CHAR_COMMA) return FALSE;  if (*p++ != CHAR_COMMA) return FALSE;
746  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
747    
748  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
749  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
750    while (IS_DIGIT(*p)) p++;
751    
752  return (*p == CHAR_RIGHT_CURLY_BRACKET);  return (*p == CHAR_RIGHT_CURLY_BRACKET);
753  }  }
# Line 664  return (*p == CHAR_RIGHT_CURLY_BRACKET); Line 759  return (*p == CHAR_RIGHT_CURLY_BRACKET);
759  *************************************************/  *************************************************/
760    
761  /* 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
762  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
763  encodes one of the more complicated things such as \d. A backreference to group  which will be placed in chptr. A backreference to group n is returned as
764  n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When  negative n. When UTF-8 is enabled, a positive value greater than 255 may
765  UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,  be returned in chptr.
766  ptr is pointing at the \. On exit, it is on the final character of the escape  On entry,ptr is pointing at the \. On exit, it is on the final character of the
767  sequence.  escape sequence.
768    
769  Arguments:  Arguments:
770    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
771      chptr          points to the data character
772    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
773    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
774    options        the options bits    options        the options bits
775    isclass        TRUE if inside a character class    isclass        TRUE if inside a character class
776    
777  Returns:         zero or positive => a data character  Returns:         zero => a data character
778                   negative => a special escape sequence                   positive => a special escape sequence
779                     negative => a back reference
780                   on error, errorcodeptr is set                   on error, errorcodeptr is set
781  */  */
782    
783  static int  static int
784  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
785    int options, BOOL isclass)    int bracount, int options, BOOL isclass)
786  {  {
787  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
788  const uschar *ptr = *ptrptr + 1;  BOOL utf = (options & PCRE_UTF8) != 0;
789  int c, i;  const pcre_uchar *ptr = *ptrptr + 1;
790    pcre_uint32 c;
791    int escape = 0;
792    int i;
793    
794  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
795  ptr--;                            /* Set pointer back to the last byte */  ptr--;                            /* Set pointer back to the last byte */
796    
797  /* 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. */
798    
799  if (c == 0) *errorcodeptr = ERR1;  if (c == CHAR_NULL) *errorcodeptr = ERR1;
800    
801  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
802  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.
803  Otherwise further processing may be required. */  Otherwise further processing may be required. */
804    
805  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
806  else if (c < CHAR_0 || c > CHAR_z) {}                     /* Not alphanumeric */  /* Not alphanumeric */
807  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if (c < CHAR_0 || c > CHAR_z) {}
808    else if ((i = escapes[c - CHAR_0]) != 0)
809      { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
810    
811  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
812  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */  /* Not alphanumeric */
813  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
814    else if ((i = escapes[c - 0x48]) != 0)  { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
815  #endif  #endif
816    
817  /* Escapes that need further processing, or are illegal. */  /* Escapes that need further processing, or are illegal. */
818    
819  else  else
820    {    {
821    const uschar *oldptr;    const pcre_uchar *oldptr;
822    BOOL braced, negated;    BOOL braced, negated, overflow;
823      int s;
824    
825    switch (c)    switch (c)
826      {      {
# Line 733  else Line 837  else
837        {        {
838        /* In JavaScript, \u must be followed by four hexadecimal numbers.        /* In JavaScript, \u must be followed by four hexadecimal numbers.
839        Otherwise it is a lowercase u letter. */        Otherwise it is a lowercase u letter. */
840        if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
841             && (digitab[ptr[3]] & ctype_xdigit) != 0 && (digitab[ptr[4]] & ctype_xdigit) != 0)          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
842            && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
843            && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
844          {          {
845          c = 0;          c = 0;
846          for (i = 0; i < 4; ++i)          for (i = 0; i < 4; ++i)
847            {            {
848            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
849  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
850            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
851            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
# Line 748  else Line 854  else
854            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
855  #endif  #endif
856            }            }
857    
858    #if defined COMPILE_PCRE8
859            if (c > (utf ? 0x10ffffU : 0xffU))
860    #elif defined COMPILE_PCRE16
861            if (c > (utf ? 0x10ffffU : 0xffffU))
862    #elif defined COMPILE_PCRE32
863            if (utf && c > 0x10ffffU)
864    #endif
865              {
866              *errorcodeptr = ERR76;
867              }
868            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
869          }          }
870        }        }
871      else      else
# Line 774  else Line 892  else
892      (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
893      number either in angle brackets or in single quotes. However, these are      number either in angle brackets or in single quotes. However, these are
894      (possibly recursive) subroutine calls, _not_ backreferences. Just return      (possibly recursive) subroutine calls, _not_ backreferences. Just return
895      the -ESC_g code (cf \k). */      the ESC_g code (cf \k). */
896    
897      case CHAR_g:      case CHAR_g:
898      if (isclass) break;      if (isclass) break;
899      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
900        {        {
901        c = -ESC_g;        escape = ESC_g;
902        break;        break;
903        }        }
904    
# Line 788  else Line 906  else
906    
907      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
908        {        {
909        const uschar *p;        const pcre_uchar *p;
910        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
911          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
912        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
913          {          {
914          c = -ESC_k;          escape = ESC_k;
915          break;          break;
916          }          }
917        braced = TRUE;        braced = TRUE;
# Line 808  else Line 926  else
926        }        }
927      else negated = FALSE;      else negated = FALSE;
928    
929      c = 0;      /* The integer range is limited by the machine's int representation. */
930      while ((digitab[ptr[1]] & ctype_digit) != 0)      s = 0;
931        c = c * 10 + *(++ptr) - CHAR_0;      overflow = FALSE;
932        while (IS_DIGIT(ptr[1]))
933      if (c < 0)   /* Integer overflow */        {
934          if (s > INT_MAX / 10 - 1) /* Integer overflow */
935            {
936            overflow = TRUE;
937            break;
938            }
939          s = s * 10 + (int)(*(++ptr) - CHAR_0);
940          }
941        if (overflow) /* Integer overflow */
942        {        {
943          while (IS_DIGIT(ptr[1]))
944            ptr++;
945        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
946        break;        break;
947        }        }
# Line 824  else Line 952  else
952        break;        break;
953        }        }
954    
955      if (c == 0)      if (s == 0)
956        {        {
957        *errorcodeptr = ERR58;        *errorcodeptr = ERR58;
958        break;        break;
# Line 832  else Line 960  else
960    
961      if (negated)      if (negated)
962        {        {
963        if (c > bracount)        if (s > bracount)
964          {          {
965          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
966          break;          break;
967          }          }
968        c = bracount - (c - 1);        s = bracount - (s - 1);
969        }        }
970    
971      c = -(ESC_REF + c);      escape = -s;
972      break;      break;
973    
974      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
# Line 861  else Line 989  else
989      if (!isclass)      if (!isclass)
990        {        {
991        oldptr = ptr;        oldptr = ptr;
992        c -= CHAR_0;        /* The integer range is limited by the machine's int representation. */
993        while ((digitab[ptr[1]] & ctype_digit) != 0)        s = (int)(c -CHAR_0);
994          c = c * 10 + *(++ptr) - CHAR_0;        overflow = FALSE;
995        if (c < 0)    /* Integer overflow */        while (IS_DIGIT(ptr[1]))
996            {
997            if (s > INT_MAX / 10 - 1) /* Integer overflow */
998              {
999              overflow = TRUE;
1000              break;
1001              }
1002            s = s * 10 + (int)(*(++ptr) - CHAR_0);
1003            }
1004          if (overflow) /* Integer overflow */
1005          {          {
1006            while (IS_DIGIT(ptr[1]))
1007              ptr++;
1008          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1009          break;          break;
1010          }          }
1011        if (c < 10 || c <= bracount)        if (s < 10 || s <= bracount)
1012          {          {
1013          c = -(ESC_REF + c);          escape = -s;
1014          break;          break;
1015          }          }
1016        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
# Line 891  else Line 1030  else
1030      /* \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
1031      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
1032      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
1033      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,
1034      than 3 octal digits. */      but no more than 3 octal digits. */
1035    
1036      case CHAR_0:      case CHAR_0:
1037      c -= CHAR_0;      c -= CHAR_0;
1038      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1039          c = c * 8 + *(++ptr) - CHAR_0;          c = c * 8 + *(++ptr) - CHAR_0;
1040      if (!utf8 && c > 255) *errorcodeptr = ERR51;  #ifdef COMPILE_PCRE8
1041        if (!utf && c > 0xff) *errorcodeptr = ERR51;
1042    #endif
1043      break;      break;
1044    
1045      /* \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
1046      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.
1047      treated as a data character. */      If not, { is treated as a data character. */
1048    
1049      case CHAR_x:      case CHAR_x:
1050      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1051        {        {
1052        /* In JavaScript, \x must be followed by two hexadecimal numbers.        /* In JavaScript, \x must be followed by two hexadecimal numbers.
1053        Otherwise it is a lowercase x letter. */        Otherwise it is a lowercase x letter. */
1054        if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0)        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1055            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1056          {          {
1057          c = 0;          c = 0;
1058          for (i = 0; i < 2; ++i)          for (i = 0; i < 2; ++i)
1059            {            {
1060            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
1061  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1062            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1063            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
# Line 930  else Line 1072  else
1072    
1073      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1074        {        {
1075        const uschar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
       int count = 0;  
1076    
1077        c = 0;        c = 0;
1078        while ((digitab[*pt] & ctype_xdigit) != 0)        overflow = FALSE;
1079          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
1080          {          {
1081          register int cc = *pt++;          register pcre_uint32 cc = *pt++;
1082          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1083          count++;  
1084    #ifdef COMPILE_PCRE32
1085            if (c >= 0x10000000l) { overflow = TRUE; break; }
1086    #endif
1087    
1088  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1089          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
# Line 947  else Line 1092  else
1092          if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */          if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
1093          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1094  #endif  #endif
1095    
1096    #if defined COMPILE_PCRE8
1097            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1098    #elif defined COMPILE_PCRE16
1099            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1100    #elif defined COMPILE_PCRE32
1101            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1102    #endif
1103            }
1104    
1105          if (overflow)
1106            {
1107            while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1108            *errorcodeptr = ERR34;
1109          }          }
1110    
1111        if (*pt == CHAR_RIGHT_CURLY_BRACKET)        if (*pt == CHAR_RIGHT_CURLY_BRACKET)
1112          {          {
1113          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1114          ptr = pt;          ptr = pt;
1115          break;          break;
1116          }          }
# Line 963  else Line 1122  else
1122      /* Read just a single-byte hex-defined char */      /* Read just a single-byte hex-defined char */
1123    
1124      c = 0;      c = 0;
1125      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1126        {        {
1127        int cc;                                  /* Some compilers don't like */        pcre_uint32 cc;                          /* Some compilers don't like */
1128        cc = *(++ptr);                           /* ++ in initializers */        cc = *(++ptr);                           /* ++ in initializers */
1129  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1130        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
# Line 984  else Line 1143  else
1143    
1144      case CHAR_c:      case CHAR_c:
1145      c = *(++ptr);      c = *(++ptr);
1146      if (c == 0)      if (c == CHAR_NULL)
1147        {        {
1148        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1149        break;        break;
# Line 1024  else Line 1183  else
1183  newline". PCRE does not support \N{name}. However, it does support  newline". PCRE does not support \N{name}. However, it does support
1184  quantification such as \N{2,3}. */  quantification such as \N{2,3}. */
1185    
1186  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&  if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1187       !is_counted_repeat(ptr+2))       !is_counted_repeat(ptr+2))
1188    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
1189    
1190  /* If PCRE_UCP is set, we change the values for \d etc. */  /* If PCRE_UCP is set, we change the values for \d etc. */
1191    
1192  if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)  if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
1193    c -= (ESC_DU - ESC_D);    escape += (ESC_DU - ESC_D);
1194    
1195  /* Set the pointer to the final character before returning. */  /* Set the pointer to the final character before returning. */
1196    
1197  *ptrptr = ptr;  *ptrptr = ptr;
1198  return c;  *chptr = c;
1199    return escape;
1200  }  }
1201    
   
   
1202  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
1203  /*************************************************  /*************************************************
1204  *               Handle \P and \p                 *  *               Handle \P and \p                 *
# Line 1054  escape sequence. Line 1212  escape sequence.
1212  Argument:  Argument:
1213    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
1214    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
1215    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
1216      pdataptr       points to an unsigned int that is set to the detailed property value
1217    errorcodeptr   points to the error code variable    errorcodeptr   points to the error code variable
1218    
1219  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
1220  */  */
1221    
1222  static int  static BOOL
1223  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1224      unsigned int *pdataptr, int *errorcodeptr)
1225  {  {
1226  int c, i, bot, top;  pcre_uchar c;
1227  const uschar *ptr = *ptrptr;  int i, bot, top;
1228  char name[32];  const pcre_uchar *ptr = *ptrptr;
1229    pcre_uchar name[32];
1230    
1231  c = *(++ptr);  c = *(++ptr);
1232  if (c == 0) goto ERROR_RETURN;  if (c == CHAR_NULL) goto ERROR_RETURN;
1233    
1234  *negptr = FALSE;  *negptr = FALSE;
1235    
# Line 1082  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1243  if (c == CHAR_LEFT_CURLY_BRACKET)
1243      *negptr = TRUE;      *negptr = TRUE;
1244      ptr++;      ptr++;
1245      }      }
1246    for (i = 0; i < (int)sizeof(name) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1247      {      {
1248      c = *(++ptr);      c = *(++ptr);
1249      if (c == 0) goto ERROR_RETURN;      if (c == CHAR_NULL) goto ERROR_RETURN;
1250      if (c == CHAR_RIGHT_CURLY_BRACKET) break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1251      name[i] = c;      name[i] = c;
1252      }      }
# Line 1106  else Line 1267  else
1267  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1268    
1269  bot = 0;  bot = 0;
1270  top = _pcre_utt_size;  top = PRIV(utt_size);
1271    
1272  while (bot < top)  while (bot < top)
1273    {    {
1274      int r;
1275    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1276    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1277    if (c == 0)    if (r == 0)
1278      {      {
1279      *dptr = _pcre_utt[i].value;      *ptypeptr = PRIV(utt)[i].type;
1280      return _pcre_utt[i].type;      *pdataptr = PRIV(utt)[i].value;
1281        return TRUE;
1282      }      }
1283    if (c > 0) bot = i + 1; else top = i;    if (r > 0) bot = i + 1; else top = i;
1284    }    }
1285    
1286  *errorcodeptr = ERR47;  *errorcodeptr = ERR47;
1287  *ptrptr = ptr;  *ptrptr = ptr;
1288  return -1;  return FALSE;
1289    
1290  ERROR_RETURN:  ERROR_RETURN:
1291  *errorcodeptr = ERR46;  *errorcodeptr = ERR46;
1292  *ptrptr = ptr;  *ptrptr = ptr;
1293  return -1;  return FALSE;
1294  }  }
1295  #endif  #endif
1296    
# Line 1153  Returns:         pointer to '}' on succe Line 1316  Returns:         pointer to '}' on succe
1316                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1317  */  */
1318    
1319  static const uschar *  static const pcre_uchar *
1320  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)
1321  {  {
1322  int min = 0;  int min = 0;
1323  int max = -1;  int max = -1;
# Line 1162  int max = -1; Line 1325  int max = -1;
1325  /* 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
1326  an integer overflow. */  an integer overflow. */
1327    
1328  while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);
1329  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1330    {    {
1331    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 1177  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1340  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1340    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1341      {      {
1342      max = 0;      max = 0;
1343      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);
1344      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1345        {        {
1346        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1202  return p; Line 1365  return p;
1365    
1366    
1367  /*************************************************  /*************************************************
 *  Subroutine for finding forward reference      *  
 *************************************************/  
   
 /* This recursive function is called only from find_parens() below. The  
 top-level call starts at the beginning of the pattern. All other calls must  
 start at a parenthesis. It 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. Recursion is used to keep  
 track of subpatterns that reset the capturing group numbers - the (?| feature.  
   
 This function was originally called only from the second pass, in which we know  
 that if (?< or (?' or (?P< is encountered, the name will be correctly  
 terminated because that is checked in the first pass. There is now one call to  
 this function in the first pass, to check for a recursive back reference by  
 name (so that we can make the whole group atomic). In this case, we need check  
 only up to the current position in the pattern, and that is still OK because  
 and previous occurrences will have been checked. To make this work, the test  
 for "end of pattern" is a check against cd->end_pattern in the main loop,  
 instead of looking for a binary zero. This means that the special first-pass  
 call can adjust cd->end_pattern temporarily. (Checks for binary zero while  
 processing items within the loop are OK, because afterwards the main loop will  
 terminate.)  
   
 Arguments:  
   ptrptr       address of the current character pointer (updated)  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf8         TRUE if we are in UTF-8 mode  
   count        pointer to the current capturing subpattern number (updated)  
   
 Returns:       the number of the named subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  
   BOOL xmode, BOOL utf8, int *count)  
 {  
 uschar *ptr = *ptrptr;  
 int start_count = *count;  
 int hwm_count = start_count;  
 BOOL dup_parens = FALSE;  
   
 /* If the first character is a parenthesis, check on the type of group we are  
 dealing with. The very first call may not start with a parenthesis. */  
   
 if (ptr[0] == CHAR_LEFT_PARENTHESIS)  
   {  
   /* Handle specials such as (*SKIP) or (*UTF8) etc. */  
   
   if (ptr[1] == CHAR_ASTERISK) ptr += 2;  
   
   /* Handle a normal, unnamed capturing parenthesis. */  
   
   else if (ptr[1] != CHAR_QUESTION_MARK)  
     {  
     *count += 1;  
     if (name == NULL && *count == lorn) return *count;  
     ptr++;  
     }  
   
   /* All cases now have (? at the start. Remember when we are in a group  
   where the parenthesis numbers are duplicated. */  
   
   else if (ptr[2] == CHAR_VERTICAL_LINE)  
     {  
     ptr += 3;  
     dup_parens = TRUE;  
     }  
   
   /* Handle comments; all characters are allowed until a ket is reached. */  
   
   else if (ptr[2] == CHAR_NUMBER_SIGN)  
     {  
     for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;  
     goto FAIL_EXIT;  
     }  
   
   /* Handle a condition. If it is an assertion, just carry on so that it  
   is processed as normal. If not, skip to the closing parenthesis of the  
   condition (there can't be any nested parens). */  
   
   else if (ptr[2] == CHAR_LEFT_PARENTHESIS)  
     {  
     ptr += 2;  
     if (ptr[1] != CHAR_QUESTION_MARK)  
       {  
       while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
       if (*ptr != 0) ptr++;  
       }  
     }  
   
   /* Start with (? but not a condition. */  
   
   else  
     {  
     ptr += 2;  
     if (*ptr == CHAR_P) ptr++;                      /* Allow optional P */  
   
     /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */  
   
     if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&  
         ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)  
       {  
       int term;  
       const uschar *thisname;  
       *count += 1;  
       if (name == NULL && *count == lorn) return *count;  
       term = *ptr++;  
       if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;  
       thisname = ptr;  
       while (*ptr != term) ptr++;  
       if (name != NULL && lorn == ptr - thisname &&  
           strncmp((const char *)name, (const char *)thisname, lorn) == 0)  
         return *count;  
       term++;  
       }  
     }  
   }  
   
 /* Past any initial parenthesis handling, scan for parentheses or vertical  
 bars. Stop if we get to cd->end_pattern. Note that this is important for the  
 first-pass call when this value is temporarily adjusted to stop at the current  
 position. So DO NOT change this to a test for binary zero. */  
   
 for (; ptr < cd->end_pattern; ptr++)  
   {  
   /* Skip over backslashed characters and also entire \Q...\E */  
   
   if (*ptr == CHAR_BACKSLASH)  
     {  
     if (*(++ptr) == 0) goto FAIL_EXIT;  
     if (*ptr == CHAR_Q) for (;;)  
       {  
       while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};  
       if (*ptr == 0) goto FAIL_EXIT;  
       if (*(++ptr) == CHAR_E) break;  
       }  
     continue;  
     }  
   
   /* Skip over character classes; this logic must be similar to the way they  
   are handled for real. If the first character is '^', skip it. Also, if the  
   first few characters (either before or after ^) are \Q\E or \E we skip them  
   too. This makes for compatibility with Perl. Note the use of STR macros to  
   encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */  
   
   if (*ptr == CHAR_LEFT_SQUARE_BRACKET)  
     {  
     BOOL negate_class = FALSE;  
     for (;;)  
       {  
       if (ptr[1] == CHAR_BACKSLASH)  
         {  
         if (ptr[2] == CHAR_E)  
           ptr+= 2;  
         else if (strncmp((const char *)ptr+2,  
                  STR_Q STR_BACKSLASH STR_E, 3) == 0)  
           ptr += 4;  
         else  
           break;  
         }  
       else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)  
         {  
         negate_class = TRUE;  
         ptr++;  
         }  
       else break;  
       }  
   
     /* If the next character is ']', it is a data character that must be  
     skipped, except in JavaScript compatibility mode. */  
   
     if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&  
         (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)  
       ptr++;  
   
     while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)  
       {  
       if (*ptr == 0) return -1;  
       if (*ptr == CHAR_BACKSLASH)  
         {  
         if (*(++ptr) == 0) goto FAIL_EXIT;  
         if (*ptr == CHAR_Q) for (;;)  
           {  
           while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};  
           if (*ptr == 0) goto FAIL_EXIT;  
           if (*(++ptr) == CHAR_E) break;  
           }  
         continue;  
         }  
       }  
     continue;  
     }  
   
   /* Skip comments in /x mode */  
   
   if (xmode && *ptr == CHAR_NUMBER_SIGN)  
     {  
     ptr++;  
     while (*ptr != 0)  
       {  
       if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }  
       ptr++;  
 #ifdef SUPPORT_UTF8  
       if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;  
 #endif  
       }  
     if (*ptr == 0) goto FAIL_EXIT;  
     continue;  
     }  
   
   /* Check for the special metacharacters */  
   
   if (*ptr == CHAR_LEFT_PARENTHESIS)  
     {  
     int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);  
     if (rc > 0) return rc;  
     if (*ptr == 0) goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_RIGHT_PARENTHESIS)  
     {  
     if (dup_parens && *count < hwm_count) *count = hwm_count;  
     goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)  
     {  
     if (*count > hwm_count) hwm_count = *count;  
     *count = start_count;  
     }  
   }  
   
 FAIL_EXIT:  
 *ptrptr = ptr;  
 return -1;  
 }  
   
   
   
   
 /*************************************************  
 *       Find forward referenced subpattern       *  
 *************************************************/  
   
 /* This function scans along a pattern's text looking for capturing  
 subpatterns, and counting them. If it finds a named pattern that matches the  
 name it is given, it returns its number. Alternatively, if the name is NULL, it  
 returns when it reaches a given numbered subpattern. This is used for forward  
 references to subpatterns. We used to be able to start this scan from the  
 current compiling point, using the current count value from cd->bracount, and  
 do it all in a single loop, but the addition of the possibility of duplicate  
 subpattern numbers means that we have to scan from the very start, in order to  
 take account of such duplicates, and to use a recursive function to keep track  
 of the different types of group.  
   
 Arguments:  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf8         TRUE if we are in UTF-8 mode  
   
 Returns:       the number of the found subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode,  
   BOOL utf8)  
 {  
 uschar *ptr = (uschar *)cd->start_pattern;  
 int count = 0;  
 int rc;  
   
 /* If the pattern does not start with an opening parenthesis, the first call  
 to find_parens_sub() will scan right to the end (if necessary). However, if it  
 does start with a parenthesis, find_parens_sub() will return when it hits the  
 matching closing parens. That is why we have to have a loop. */  
   
 for (;;)  
   {  
   rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);  
   if (rc > 0 || *ptr++ == 0) break;  
   }  
   
 return rc;  
 }  
   
   
   
   
 /*************************************************  
1368  *      Find first significant op code            *  *      Find first significant op code            *
1369  *************************************************/  *************************************************/
1370    
# Line 1513  Arguments: Line 1381  Arguments:
1381  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1382  */  */
1383    
1384  static const uschar*  static const pcre_uchar*
1385  first_significant_code(const uschar *code, BOOL skipassert)  first_significant_code(const pcre_uchar *code, BOOL skipassert)
1386  {  {
1387  for (;;)  for (;;)
1388    {    {
# Line 1525  for (;;) Line 1393  for (;;)
1393      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1394      if (!skipassert) return code;      if (!skipassert) return code;
1395      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
1396      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1397      break;      break;
1398    
1399      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
# Line 1539  for (;;) Line 1407  for (;;)
1407      case OP_RREF:      case OP_RREF:
1408      case OP_NRREF:      case OP_NRREF:
1409      case OP_DEF:      case OP_DEF:
1410      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1411      break;      break;
1412    
1413      default:      default:
# Line 1569  and doing the check at the end; a flag s Line 1437  and doing the check at the end; a flag s
1437    
1438  Arguments:  Arguments:
1439    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1440    utf8     TRUE in UTF-8 mode    utf      TRUE in UTF-8 / UTF-16 / UTF-32 mode
1441    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1442    cd       the "compile data" structure    cd       the "compile data" structure
1443    
# Line 1581  Returns:   the fixed length, Line 1449  Returns:   the fixed length,
1449  */  */
1450    
1451  static int  static int
1452  find_fixedlength(uschar *code, BOOL utf8, BOOL atend, compile_data *cd)  find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1453  {  {
1454  int length = -1;  int length = -1;
1455    
1456  register int branchlength = 0;  register int branchlength = 0;
1457  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1458    
1459  /* 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
1460  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1594  branch, check the length against that of Line 1462  branch, check the length against that of
1462  for (;;)  for (;;)
1463    {    {
1464    int d;    int d;
1465    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1466    register int op = *cc;    register pcre_uchar op = *cc;
1467    
1468    switch (op)    switch (op)
1469      {      {
1470      /* We only need to continue for OP_CBRA (normal capturing bracket) and      /* We only need to continue for OP_CBRA (normal capturing bracket) and
# Line 1608  for (;;) Line 1477  for (;;)
1477      case OP_ONCE:      case OP_ONCE:
1478      case OP_ONCE_NC:      case OP_ONCE_NC:
1479      case OP_COND:      case OP_COND:
1480      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), utf8, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd);
1481      if (d < 0) return d;      if (d < 0) return d;
1482      branchlength += d;      branchlength += d;
1483      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 1639  for (;;) Line 1508  for (;;)
1508    
1509      case OP_RECURSE:      case OP_RECURSE:
1510      if (!atend) return -3;      if (!atend) return -3;
1511      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1512      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1513      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1514      d = find_fixedlength(cs + 2, utf8, atend, cd);      d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd);
1515      if (d < 0) return d;      if (d < 0) return d;
1516      branchlength += d;      branchlength += d;
1517      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1655  for (;;) Line 1524  for (;;)
1524      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1525      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1526      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1527      /* Fall through */      cc += PRIV(OP_lengths)[*cc];
1528        break;
1529    
1530      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1531    
# Line 1663  for (;;) Line 1533  for (;;)
1533      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1534      case OP_SKIP_ARG:      case OP_SKIP_ARG:
1535      case OP_THEN_ARG:      case OP_THEN_ARG:
1536      cc += cc[1] + _pcre_OP_lengths[*cc];      cc += cc[1] + PRIV(OP_lengths)[*cc];
1537      break;      break;
1538    
1539      case OP_CALLOUT:      case OP_CALLOUT:
# Line 1690  for (;;) Line 1560  for (;;)
1560      case OP_SOM:      case OP_SOM:
1561      case OP_THEN:      case OP_THEN:
1562      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1563      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1564      break;      break;
1565    
1566      /* Handle literal characters */      /* Handle literal characters */
# Line 1701  for (;;) Line 1571  for (;;)
1571      case OP_NOTI:      case OP_NOTI:
1572      branchlength++;      branchlength++;
1573      cc += 2;      cc += 2;
1574  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1575      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1576  #endif  #endif
1577      break;      break;
1578    
# Line 1713  for (;;) Line 1583  for (;;)
1583      case OP_EXACTI:      case OP_EXACTI:
1584      case OP_NOTEXACT:      case OP_NOTEXACT:
1585      case OP_NOTEXACTI:      case OP_NOTEXACTI:
1586      branchlength += GET2(cc,1);      branchlength += (int)GET2(cc,1);
1587      cc += 4;      cc += 2 + IMM2_SIZE;
1588  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1589      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1590  #endif  #endif
1591      break;      break;
1592    
1593      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1594      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1595      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1596      cc += 4;        cc += 2;
1597        cc += 1 + IMM2_SIZE + 1;
1598      break;      break;
1599    
1600      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1749  for (;;) Line 1620  for (;;)
1620      cc++;      cc++;
1621      break;      break;
1622    
1623      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;      /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1624      otherwise \C is coded as OP_ALLANY. */      otherwise \C is coded as OP_ALLANY. */
1625    
1626      case OP_ANYBYTE:      case OP_ANYBYTE:
# Line 1757  for (;;) Line 1628  for (;;)
1628    
1629      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1630    
 #ifdef SUPPORT_UTF8  
     case OP_XCLASS:  
     cc += GET(cc, 1) - 33;  
     /* Fall through */  
 #endif  
   
1631      case OP_CLASS:      case OP_CLASS:
1632      case OP_NCLASS:      case OP_NCLASS:
1633      cc += 33;  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1634        case OP_XCLASS:
1635        /* The original code caused an unsigned overflow in 64 bit systems,
1636        so now we use a conditional statement. */
1637        if (op == OP_XCLASS)
1638          cc += GET(cc, 1);
1639        else
1640          cc += PRIV(OP_lengths)[OP_CLASS];
1641    #else
1642        cc += PRIV(OP_lengths)[OP_CLASS];
1643    #endif
1644    
1645      switch (*cc)      switch (*cc)
1646        {        {
# Line 1779  for (;;) Line 1654  for (;;)
1654    
1655        case OP_CRRANGE:        case OP_CRRANGE:
1656        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1657        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1658        branchlength += GET2(cc,1);        branchlength += (int)GET2(cc,1);
1659        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1660        break;        break;
1661    
1662        default:        default:
# Line 1847  for (;;) Line 1722  for (;;)
1722      case OP_QUERYI:      case OP_QUERYI:
1723      case OP_REF:      case OP_REF:
1724      case OP_REFI:      case OP_REFI:
1725        case OP_DNREF:
1726        case OP_DNREFI:
1727      case OP_SBRA:      case OP_SBRA:
1728      case OP_SBRAPOS:      case OP_SBRAPOS:
1729      case OP_SCBRA:      case OP_SCBRA:
# Line 1896  length. Line 1773  length.
1773    
1774  Arguments:  Arguments:
1775    code        points to start of expression    code        points to start of expression
1776    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1777    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
1778    
1779  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
1780  */  */
1781    
1782  const uschar *  const pcre_uchar *
1783  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
1784  {  {
1785  for (;;)  for (;;)
1786    {    {
1787    register int c = *code;    register pcre_uchar c = *code;
1788    
1789    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1790    
# Line 1921  for (;;) Line 1798  for (;;)
1798    
1799    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
1800      {      {
1801      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
1802      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1803      }      }
1804    
1805    /* Handle capturing bracket */    /* Handle capturing bracket */
# Line 1930  for (;;) Line 1807  for (;;)
1807    else if (c == OP_CBRA || c == OP_SCBRA ||    else if (c == OP_CBRA || c == OP_SCBRA ||
1808             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
1809      {      {
1810      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1811      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
1812      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1813      }      }
1814    
1815    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
# Line 1960  for (;;) Line 1837  for (;;)
1837        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1838        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1839        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1840        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1841            code += 2;
1842        break;        break;
1843    
1844        case OP_MARK:        case OP_MARK:
1845        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1846        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
1847        case OP_THEN_ARG:        case OP_THEN_ARG:
1848        code += code[1];        code += code[1];
1849        break;        break;
# Line 1976  for (;;) Line 1851  for (;;)
1851    
1852      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
1853    
1854      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1855    
1856    /* 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
1857    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
1858    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
1859    
1860  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1861      if (utf8) switch(c)      if (utf) switch(c)
1862        {        {
1863        case OP_CHAR:        case OP_CHAR:
1864        case OP_CHARI:        case OP_CHARI:
# Line 2013  for (;;) Line 1888  for (;;)
1888        case OP_MINQUERYI:        case OP_MINQUERYI:
1889        case OP_POSQUERY:        case OP_POSQUERY:
1890        case OP_POSQUERYI:        case OP_POSQUERYI:
1891        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
1892        break;        break;
1893        }        }
1894  #else  #else
1895      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
1896  #endif  #endif
1897      }      }
1898    }    }
# Line 2034  instance of OP_RECURSE. Line 1909  instance of OP_RECURSE.
1909    
1910  Arguments:  Arguments:
1911    code        points to start of expression    code        points to start of expression
1912    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1913    
1914  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
1915  */  */
1916    
1917  static const uschar *  static const pcre_uchar *
1918  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
1919  {  {
1920  for (;;)  for (;;)
1921    {    {
1922    register int c = *code;    register pcre_uchar c = *code;
1923    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1924    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
1925    
# Line 2079  for (;;) Line 1954  for (;;)
1954        case OP_TYPEUPTO:        case OP_TYPEUPTO:
1955        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1956        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1957        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1958            code += 2;
1959        break;        break;
1960    
1961        case OP_MARK:        case OP_MARK:
1962        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1963        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
1964        case OP_THEN_ARG:        case OP_THEN_ARG:
1965        code += code[1];        code += code[1];
1966        break;        break;
# Line 2095  for (;;) Line 1968  for (;;)
1968    
1969      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
1970    
1971      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
1972    
1973      /* 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
1974      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
1975      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
1976    
1977  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1978      if (utf8) switch(c)      if (utf) switch(c)
1979        {        {
1980        case OP_CHAR:        case OP_CHAR:
1981        case OP_CHARI:        case OP_CHARI:
1982          case OP_NOT:
1983          case OP_NOTI:
1984        case OP_EXACT:        case OP_EXACT:
1985        case OP_EXACTI:        case OP_EXACTI:
1986          case OP_NOTEXACT:
1987          case OP_NOTEXACTI:
1988        case OP_UPTO:        case OP_UPTO:
1989        case OP_UPTOI:        case OP_UPTOI:
1990          case OP_NOTUPTO:
1991          case OP_NOTUPTOI:
1992        case OP_MINUPTO:        case OP_MINUPTO:
1993        case OP_MINUPTOI:        case OP_MINUPTOI:
1994          case OP_NOTMINUPTO:
1995          case OP_NOTMINUPTOI:
1996        case OP_POSUPTO:        case OP_POSUPTO:
1997        case OP_POSUPTOI:        case OP_POSUPTOI:
1998          case OP_NOTPOSUPTO:
1999          case OP_NOTPOSUPTOI:
2000        case OP_STAR:        case OP_STAR:
2001        case OP_STARI:        case OP_STARI:
2002          case OP_NOTSTAR:
2003          case OP_NOTSTARI:
2004        case OP_MINSTAR:        case OP_MINSTAR:
2005        case OP_MINSTARI:        case OP_MINSTARI:
2006          case OP_NOTMINSTAR:
2007          case OP_NOTMINSTARI:
2008        case OP_POSSTAR:        case OP_POSSTAR:
2009        case OP_POSSTARI:        case OP_POSSTARI:
2010          case OP_NOTPOSSTAR:
2011          case OP_NOTPOSSTARI:
2012        case OP_PLUS:        case OP_PLUS:
2013        case OP_PLUSI:        case OP_PLUSI:
2014          case OP_NOTPLUS:
2015          case OP_NOTPLUSI:
2016        case OP_MINPLUS:        case OP_MINPLUS:
2017        case OP_MINPLUSI:        case OP_MINPLUSI:
2018          case OP_NOTMINPLUS:
2019          case OP_NOTMINPLUSI:
2020        case OP_POSPLUS:        case OP_POSPLUS:
2021        case OP_POSPLUSI:        case OP_POSPLUSI:
2022          case OP_NOTPOSPLUS:
2023          case OP_NOTPOSPLUSI:
2024        case OP_QUERY:        case OP_QUERY:
2025        case OP_QUERYI:        case OP_QUERYI:
2026          case OP_NOTQUERY:
2027          case OP_NOTQUERYI:
2028        case OP_MINQUERY:        case OP_MINQUERY:
2029        case OP_MINQUERYI:        case OP_MINQUERYI:
2030          case OP_NOTMINQUERY:
2031          case OP_NOTMINQUERYI:
2032        case OP_POSQUERY:        case OP_POSQUERY:
2033        case OP_POSQUERYI:        case OP_POSQUERYI:
2034        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_NOTPOSQUERY:
2035          case OP_NOTPOSQUERYI:
2036          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2037        break;        break;
2038        }        }
2039  #else  #else
2040      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2041  #endif  #endif
2042      }      }
2043    }    }
# Line 2159  bracket whose current branch will alread Line 2060  bracket whose current branch will alread
2060  Arguments:  Arguments:
2061    code        points to start of search    code        points to start of search
2062    endcode     points to where to stop    endcode     points to where to stop
2063    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2064    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2065      recurses    chain of recurse_check to catch mutual recursion
2066    
2067  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2068  */  */
2069    
2070    typedef struct recurse_check {
2071      struct recurse_check *prev;
2072      const pcre_uchar *group;
2073    } recurse_check;
2074    
2075  static BOOL  static BOOL
2076  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2077    compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2078  {  {
2079  register int c;  register pcre_uchar c;
2080  for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE);  recurse_check this_recurse;
2081    
2082    for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2083       code < endcode;       code < endcode;
2084       code = first_significant_code(code + _pcre_OP_lengths[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2085    {    {
2086    const uschar *ccode;    const pcre_uchar *ccode;
2087    
2088    c = *code;    c = *code;
2089    
# Line 2197  for (code = first_significant_code(code Line 2106  for (code = first_significant_code(code
2106    
2107    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2108      {      {
2109      const uschar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2110      BOOL empty_branch;      BOOL empty_branch;
2111    
2112      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2113        when called to scan a completed pattern by setting cd->start_workspace to
2114        NULL. */
2115    
2116        if (cd->start_workspace != NULL)
2117          {
2118          const pcre_uchar *tcode;
2119          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2120            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2121          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2122          }
2123    
2124        /* If we are scanning a completed pattern, there are no forward references
2125        and all groups are complete. We need to detect whether this is a recursive
2126        call, as otherwise there will be an infinite loop. If it is a recursion,
2127        just skip over it. Simple recursions are easily detected. For mutual
2128        recursions we keep a chain on the stack. */
2129    
2130      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)      else
2131        if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;        {
2132          recurse_check *r = recurses;
2133          const pcre_uchar *endgroup = scode;
2134    
2135      /* Not a forward reference, test for completed backward reference */        do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2136          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2137    
2138      empty_branch = FALSE;        for (r = recurses; r != NULL; r = r->prev)
2139      scode = cd->start_code + GET(code, 1);          if (r->group == scode) break;
2140      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */        if (r != NULL) continue;   /* Mutual recursion */
2141          }
2142    
2143      /* Completed backwards reference */      /* Completed reference; scan the referenced group, remembering it on the
2144        stack chain to detect mutual recursions. */
2145    
2146        empty_branch = FALSE;
2147        this_recurse.prev = recurses;
2148        this_recurse.group = scode;
2149    
2150      do      do
2151        {        {
2152        if (could_be_empty_branch(scode, endcode, utf8, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2153          {          {
2154          empty_branch = TRUE;          empty_branch = TRUE;
2155          break;          break;
# Line 2233  for (code = first_significant_code(code Line 2167  for (code = first_significant_code(code
2167    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2168        c == OP_BRAPOSZERO)        c == OP_BRAPOSZERO)
2169      {      {
2170      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2171      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
2172      c = *code;      c = *code;
2173      continue;      continue;
# Line 2271  for (code = first_significant_code(code Line 2205  for (code = first_significant_code(code
2205        empty_branch = FALSE;        empty_branch = FALSE;
2206        do        do
2207          {          {
2208          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2209            empty_branch = TRUE;            empty_branch = TRUE;
2210          code += GET(code, 1);          code += GET(code, 1);
2211          }          }
# Line 2289  for (code = first_significant_code(code Line 2223  for (code = first_significant_code(code
2223      {      {
2224      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2225      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2226      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2227      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"
2228      here. */      here. */
2229    
2230  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2231      case OP_XCLASS:      case OP_XCLASS:
2232      ccode = code += GET(code, 1);      ccode = code += GET(code, 1);
2233      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
# Line 2301  for (code = first_significant_code(code Line 2235  for (code = first_significant_code(code
2235    
2236      case OP_CLASS:      case OP_CLASS:
2237      case OP_NCLASS:      case OP_NCLASS:
2238      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2239    
2240  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2241      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
2242  #endif  #endif
2243    
# Line 2329  for (code = first_significant_code(code Line 2263  for (code = first_significant_code(code
2263    
2264      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2265    
2266        case OP_ANY:
2267        case OP_ALLANY:
2268        case OP_ANYBYTE:
2269    
2270      case OP_PROP:      case OP_PROP:
2271      case OP_NOTPROP:      case OP_NOTPROP:
2272        case OP_ANYNL:
2273    
2274        case OP_NOT_HSPACE:
2275        case OP_HSPACE:
2276        case OP_NOT_VSPACE:
2277        case OP_VSPACE:
2278      case OP_EXTUNI:      case OP_EXTUNI:
2279    
2280      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2281      case OP_DIGIT:      case OP_DIGIT:
2282      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2283      case OP_WHITESPACE:      case OP_WHITESPACE:
2284      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2285      case OP_WORDCHAR:      case OP_WORDCHAR:
2286      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2287      case OP_CHAR:      case OP_CHAR:
2288      case OP_CHARI:      case OP_CHARI:
2289      case OP_NOT:      case OP_NOT:
2290      case OP_NOTI:      case OP_NOTI:
2291    
2292      case OP_PLUS:      case OP_PLUS:
2293        case OP_PLUSI:
2294      case OP_MINPLUS:      case OP_MINPLUS:
2295      case OP_POSPLUS:      case OP_MINPLUSI:
2296      case OP_EXACT:  
2297      case OP_NOTPLUS:      case OP_NOTPLUS:
2298        case OP_NOTPLUSI:
2299      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2300        case OP_NOTMINPLUSI:
2301    
2302        case OP_POSPLUS:
2303        case OP_POSPLUSI:
2304      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2305        case OP_NOTPOSPLUSI:
2306    
2307        case OP_EXACT:
2308        case OP_EXACTI:
2309      case OP_NOTEXACT:      case OP_NOTEXACT:
2310        case OP_NOTEXACTI:
2311    
2312      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2313      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2314      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2315      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2316    
2317      return FALSE;      return FALSE;
2318    
2319      /* 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 2376  for (code = first_significant_code(code Line 2333  for (code = first_significant_code(code
2333      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2334      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2335      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2336      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2337          code += 2;
2338      break;      break;
2339    
2340      /* End of branch */      /* End of branch */
# Line 2389  for (code = first_significant_code(code Line 2347  for (code = first_significant_code(code
2347      return TRUE;      return TRUE;
2348    
2349      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2350      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2351        followed by a multibyte character. */
2352    
2353  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2354      case OP_STAR:      case OP_STAR:
2355      case OP_STARI:      case OP_STARI:
2356        case OP_NOTSTAR:
2357        case OP_NOTSTARI:
2358    
2359      case OP_MINSTAR:      case OP_MINSTAR:
2360      case OP_MINSTARI:      case OP_MINSTARI:
2361        case OP_NOTMINSTAR:
2362        case OP_NOTMINSTARI:
2363    
2364      case OP_POSSTAR:      case OP_POSSTAR:
2365      case OP_POSSTARI:      case OP_POSSTARI:
2366        case OP_NOTPOSSTAR:
2367        case OP_NOTPOSSTARI:
2368    
2369      case OP_QUERY:      case OP_QUERY:
2370      case OP_QUERYI:      case OP_QUERYI:
2371        case OP_NOTQUERY:
2372        case OP_NOTQUERYI:
2373    
2374      case OP_MINQUERY:      case OP_MINQUERY:
2375      case OP_MINQUERYI:      case OP_MINQUERYI:
2376      case OP_POSQUERY:      case OP_NOTMINQUERY:
2377        case OP_NOTMINQUERYI:
2378    
2379        case OP_POSQUERY:
2380      case OP_POSQUERYI:      case OP_POSQUERYI:
2381      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      case OP_NOTPOSQUERY:
2382        case OP_NOTPOSQUERYI:
2383    
2384        if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2385      break;      break;
2386    
2387      case OP_UPTO:      case OP_UPTO:
2388      case OP_UPTOI:      case OP_UPTOI:
2389        case OP_NOTUPTO:
2390        case OP_NOTUPTOI:
2391    
2392      case OP_MINUPTO:      case OP_MINUPTO:
2393      case OP_MINUPTOI:      case OP_MINUPTOI:
2394        case OP_NOTMINUPTO:
2395        case OP_NOTMINUPTOI:
2396    
2397      case OP_POSUPTO:      case OP_POSUPTO:
2398      case OP_POSUPTOI:      case OP_POSUPTOI:
2399      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      case OP_NOTPOSUPTO:
2400        case OP_NOTPOSUPTOI:
2401    
2402        if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2403      break;      break;
2404  #endif  #endif
2405    
# Line 2423  for (code = first_significant_code(code Line 2409  for (code = first_significant_code(code
2409      case OP_MARK:      case OP_MARK:
2410      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2411      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     code += code[1];  
     break;  
   
2412      case OP_THEN_ARG:      case OP_THEN_ARG:
2413      code += code[1];      code += code[1];
2414      break;      break;
# Line 2457  Arguments: Line 2440  Arguments:
2440    code        points to start of the recursion    code        points to start of the recursion
2441    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2442    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2443    utf8        TRUE if in UTF-8 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2444    cd          pointers to tables etc    cd          pointers to tables etc
2445    
2446  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2447  */  */
2448    
2449  static BOOL  static BOOL
2450  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2451    BOOL utf8, compile_data *cd)    branch_chain *bcptr, BOOL utf, compile_data *cd)
2452  {  {
2453  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2454    {    {
2455    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2456      return FALSE;      return FALSE;
2457    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2458    }    }
# Line 2521  Returns:   TRUE or FALSE Line 2504  Returns:   TRUE or FALSE
2504  */  */
2505    
2506  static BOOL  static BOOL
2507  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2508  {  {
2509  int terminator;          /* Don't combine these lines; the Solaris cc */  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
2510  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
2511  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != CHAR_NULL; ptr++)
2512    {    {
2513    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2514      ptr++;      ptr++;
# Line 2565  Returns:     a value representing the na Line 2548  Returns:     a value representing the na
2548  */  */
2549    
2550  static int  static int
2551  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
2552  {  {
2553  const char *pn = posix_names;  const char *pn = posix_names;
2554  register int yield = 0;  register int yield = 0;
2555  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
2556    {    {
2557    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
2558      strncmp((const char *)ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
2559    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
2560    yield++;    yield++;
2561    }    }
# Line 2604  value in the reference (which is a group Line 2587  value in the reference (which is a group
2587  Arguments:  Arguments:
2588    group      points to the start of the group    group      points to the start of the group
2589    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
2590    utf8       TRUE in UTF-8 mode    utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
2591    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
2592    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
2593    
# Line 2612  Returns:     nothing Line 2595  Returns:     nothing
2595  */  */
2596    
2597  static void  static void
2598  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
2599    uschar *save_hwm)    pcre_uchar *save_hwm)
2600  {  {
2601  uschar *ptr = group;  pcre_uchar *ptr = group;
2602    
2603  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
2604    {    {
2605    int offset;    int offset;
2606    uschar *hc;    pcre_uchar *hc;
2607    
2608    /* 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
2609    reference. */    reference. */
2610    
2611    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
2612      {      {
2613      offset = GET(hc, 0);      offset = (int)GET(hc, 0);
2614      if (cd->start_code + offset == ptr + 1)      if (cd->start_code + offset == ptr + 1)
2615        {        {
2616        PUT(hc, 0, offset + adjust);        PUT(hc, 0, offset + adjust);
# Line 2640  while ((ptr = (uschar *)find_recurse(ptr Line 2623  while ((ptr = (uschar *)find_recurse(ptr
2623    
2624    if (hc >= cd->hwm)    if (hc >= cd->hwm)
2625      {      {
2626      offset = GET(ptr, 1);      offset = (int)GET(ptr, 1);
2627      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
2628      }      }
2629    
# Line 2665  Arguments: Line 2648  Arguments:
2648  Returns:         new code pointer  Returns:         new code pointer
2649  */  */
2650    
2651  static uschar *  static pcre_uchar *
2652  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2653  {  {
2654  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2655  *code++ = 255;  *code++ = 255;
2656  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2657  PUT(code, LINK_SIZE, 0);                       /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2658  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
2659  }  }
2660    
2661    
# Line 2694  Returns:             nothing Line 2677  Returns:             nothing
2677  */  */
2678    
2679  static void  static void
2680  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2681  {  {
2682  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2683  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
# Line 2708  PUT(previous_callout, 2 + LINK_SIZE, len Line 2691  PUT(previous_callout, 2 + LINK_SIZE, len
2691  *************************************************/  *************************************************/
2692    
2693  /* 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
2694  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
2695  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
2696  start address.  start address. A character with multiple other cases is returned on its own
2697    with a special return value.
2698    
2699  Arguments:  Arguments:
2700    cptr        points to starting character value; updated    cptr        points to starting character value; updated
# Line 2718  Arguments: Line 2702  Arguments:
2702    ocptr       where to put start of othercase range    ocptr       where to put start of othercase range
2703    odptr       where to put end of othercase range    odptr       where to put end of othercase range
2704    
2705  Yield:        TRUE when range returned; FALSE when no more  Yield:        -1 when no more
2706                   0 when a range is returned
2707                  >0 the CASESET offset for char with multiple other cases
2708                    in this case, ocptr contains the original
2709  */  */
2710    
2711  static BOOL  static int
2712  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
2713    unsigned int *odptr)    pcre_uint32 *odptr)
2714  {  {
2715  unsigned int c, othercase, next;  pcre_uint32 c, othercase, next;
2716    unsigned int co;
2717    
2718    /* Find the first character that has an other case. If it has multiple other
2719    cases, return its case offset value. */
2720    
2721  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
2722    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
2723      if ((co = UCD_CASESET(c)) != 0)
2724        {
2725        *ocptr = c++;   /* Character that has the set */
2726        *cptr = c;      /* Rest of input range */
2727        return (int)co;
2728        }
2729      if ((othercase = UCD_OTHERCASE(c)) != c) break;
2730      }
2731    
2732  if (c > d) return FALSE;  if (c > d) return -1;  /* Reached end of range */
2733    
2734  *ocptr = othercase;  *ocptr = othercase;
2735  next = othercase + 1;  next = othercase + 1;
# Line 2741  for (++c; c <= d; c++) Line 2740  for (++c; c <= d; c++)
2740    next++;    next++;
2741    }    }
2742    
2743  *odptr = next - 1;  *odptr = next - 1;     /* End of othercase range */
2744  *cptr = c;  *cptr = c;             /* Rest of input range */
2745    return 0;
 return TRUE;  
2746  }  }
2747    
2748    
# Line 2766  Returns:       TRUE if auto-possessifyin Line 2764  Returns:       TRUE if auto-possessifyin
2764  */  */
2765    
2766  static BOOL  static BOOL
2767  check_char_prop(int c, int ptype, int pdata, BOOL negated)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated)
2768  {  {
2769    #ifdef SUPPORT_UCP
2770    const pcre_uint32 *p;
2771    #endif
2772    
2773  const ucd_record *prop = GET_UCD(c);  const ucd_record *prop = GET_UCD(c);
2774    
2775  switch(ptype)  switch(ptype)
2776    {    {
2777    case PT_LAMP:    case PT_LAMP:
# Line 2777  switch(ptype) Line 2780  switch(ptype)
2780            prop->chartype == ucp_Lt) == negated;            prop->chartype == ucp_Lt) == negated;
2781    
2782    case PT_GC:    case PT_GC:
2783    return (pdata == _pcre_ucp_gentype[prop->chartype]) == negated;    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2784    
2785    case PT_PC:    case PT_PC:
2786    return (pdata == prop->chartype) == negated;    return (pdata == prop->chartype) == negated;
# Line 2788  switch(ptype) Line 2791  switch(ptype)
2791    /* These are specials */    /* These are specials */
2792    
2793    case PT_ALNUM:    case PT_ALNUM:
2794    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2795            _pcre_ucp_gentype[prop->chartype] == ucp_N) == negated;            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2796    
2797    case PT_SPACE:    /* Perl space */    case PT_SPACE:    /* Perl space */
2798    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2799            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2800            == negated;            == negated;
2801    
2802    case PT_PXSPACE:  /* POSIX space */    case PT_PXSPACE:  /* POSIX space */
2803    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2804            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2805            c == CHAR_FF || c == CHAR_CR)            c == CHAR_FF || c == CHAR_CR)
2806            == negated;            == negated;
2807    
2808    case PT_WORD:    case PT_WORD:
2809    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2810            _pcre_ucp_gentype[prop->chartype] == ucp_N ||            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2811            c == CHAR_UNDERSCORE) == negated;            c == CHAR_UNDERSCORE) == negated;
2812    
2813    #ifdef SUPPORT_UCP
2814      case PT_CLIST:
2815      p = PRIV(ucd_caseless_sets) + prop->caseset;
2816      for (;;)
2817        {
2818        if (c < *p) return !negated;
2819        if (c == *p++) return negated;
2820        }
2821      break;  /* Control never reaches here */
2822    #endif
2823    }    }
2824    
2825  return FALSE;  return FALSE;
2826  }  }
2827  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
# Line 2823  sense to automatically possessify the re Line 2838  sense to automatically possessify the re
2838    
2839  Arguments:  Arguments:
2840    previous      pointer to the repeated opcode    previous      pointer to the repeated opcode
2841    utf8          TRUE in UTF-8 mode    utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode
2842    ptr           next character in pattern    ptr           next character in pattern
2843    options       options bits    options       options bits
2844    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 2832  Returns:        TRUE if possessifying is Line 2847  Returns:        TRUE if possessifying is
2847  */  */
2848    
2849  static BOOL  static BOOL
2850  check_auto_possessive(const uschar *previous, BOOL utf8, const uschar *ptr,  check_auto_possessive(const pcre_uchar *previous, BOOL utf,
2851    int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
2852  {  {
2853  int c, next;  pcre_uint32 c = NOTACHAR;
2854  int op_code = *previous++;  pcre_uint32 next;
2855    int escape;
2856    pcre_uchar op_code = *previous++;
2857    
2858  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
2859    
# Line 2844  if ((options & PCRE_EXTENDED) != 0) Line 2861  if ((options & PCRE_EXTENDED) != 0)
2861    {    {
2862    for (;;)    for (;;)
2863      {      {
2864      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2865      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2866        {        {
2867        ptr++;        ptr++;
2868        while (*ptr != 0)        while (*ptr != CHAR_NULL)
2869          {          {
2870          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2871          ptr++;          ptr++;
2872  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2873          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf) FORWARDCHAR(ptr);
2874  #endif  #endif
2875          }          }
2876        }        }
# Line 2867  value is a character, a negative value i Line 2884  value is a character, a negative value i
2884  if (*ptr == CHAR_BACKSLASH)  if (*ptr == CHAR_BACKSLASH)
2885    {    {
2886    int temperrorcode = 0;    int temperrorcode = 0;
2887    next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);    escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options,
2888        FALSE);
2889    if (temperrorcode != 0) return FALSE;    if (temperrorcode != 0) return FALSE;
2890    ptr++;    /* Point after the escape sequence */    ptr++;    /* Point after the escape sequence */
2891    }    }
2892    else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)
 else if ((cd->ctypes[*ptr] & ctype_meta) == 0)  
2893    {    {
2894  #ifdef SUPPORT_UTF8    escape = 0;
2895    if (utf8) { GETCHARINC(next, ptr); } else  #ifdef SUPPORT_UTF
2896      if (utf) { GETCHARINC(next, ptr); } else
2897  #endif  #endif
2898    next = *ptr++;    next = *ptr++;
2899    }    }
   
2900  else return FALSE;  else return FALSE;
2901    
2902  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
# Line 2888  if ((options & PCRE_EXTENDED) != 0) Line 2905  if ((options & PCRE_EXTENDED) != 0)
2905    {    {
2906    for (;;)    for (;;)
2907      {      {
2908      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2909      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2910        {        {
2911        ptr++;        ptr++;
2912        while (*ptr != 0)        while (*ptr != CHAR_NULL)
2913          {          {
2914          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2915          ptr++;          ptr++;
2916  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2917          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf) FORWARDCHAR(ptr);
2918  #endif  #endif
2919          }          }
2920        }        }
# Line 2908  if ((options & PCRE_EXTENDED) != 0) Line 2925  if ((options & PCRE_EXTENDED) != 0)
2925  /* If the next thing is itself optional, we have to give up. */  /* If the next thing is itself optional, we have to give up. */
2926    
2927  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
2928    strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
2929      return FALSE;      return FALSE;
2930    
2931  /* Now compare the next item with the previous opcode. First, handle cases when  /* If the previous item is a character, get its value. */
 the next item is a character. */  
2932    
2933  if (next >= 0) switch(op_code)  if (op_code == OP_CHAR || op_code == OP_CHARI ||
2934        op_code == OP_NOT || op_code == OP_NOTI)
2935    {    {
2936    case OP_CHAR:  #ifdef SUPPORT_UTF
 #ifdef SUPPORT_UTF8  
2937    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
2938  #else  #else
2939    c = *previous;    c = *previous;
2940  #endif  #endif
2941    return c != next;    }
2942    
2943    /* For CHARI (caseless character) we must check the other case. If we have  /* Now compare the next item with the previous opcode. First, handle cases when
2944    Unicode property support, we can use it to test the other case of  the next item is a character. */
   high-valued characters. */  
2945    
2946    case OP_CHARI:  if (escape == 0)
2947  #ifdef SUPPORT_UTF8    {
2948    GETCHARTEST(c, previous);    /* For a caseless UTF match, the next character may have more than one other
2949  #else    case, which maps to the special PT_CLIST property. Check this first. */
2950    c = *previous;  
2951    #ifdef SUPPORT_UCP
2952      if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)
2953        {
2954        unsigned int ocs = UCD_CASESET(next);
2955        if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);
2956        }
2957  #endif  #endif
2958    if (c == next) return FALSE;  
2959  #ifdef SUPPORT_UTF8    switch(op_code)
   if (utf8)  
2960      {      {
2961      unsigned int othercase;      case OP_CHAR:
2962      if (next < 128) othercase = cd->fcc[next]; else      return c != next;
2963    
2964        /* For CHARI (caseless character) we must check the other case. If we have
2965        Unicode property support, we can use it to test the other case of
2966        high-valued characters. We know that next can have only one other case,
2967        because multi-other-case characters are dealt with above. */
2968    
2969        case OP_CHARI:
2970        if (c == next) return FALSE;
2971    #ifdef SUPPORT_UTF
2972        if (utf)
2973          {
2974          pcre_uint32 othercase;
2975          if (next < 128) othercase = cd->fcc[next]; else
2976  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2977      othercase = UCD_OTHERCASE((unsigned int)next);        othercase = UCD_OTHERCASE(next);
2978  #else  #else
2979      othercase = NOTACHAR;        othercase = NOTACHAR;
2980  #endif  #endif
2981      return (unsigned int)c != othercase;        return c != othercase;
2982      }        }
2983    else      else
2984  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF */
2985    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */      return (c != TABLE_GET(next, cd->fcc, next));  /* Not UTF */
2986    
2987    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These      case OP_NOT:
2988    opcodes are not used for multi-byte characters, because they are coded using      return c == next;
2989    an XCLASS instead. */  
2990        case OP_NOTI:
2991    case OP_NOT:      if (c == next) return TRUE;
2992    return (c = *previous) == next;  #ifdef SUPPORT_UTF
2993        if (utf)
2994    case OP_NOTI:        {
2995    if ((c = *previous) == next) return TRUE;        pcre_uint32 othercase;
2996  #ifdef SUPPORT_UTF8        if (next < 128) othercase = cd->fcc[next]; else
   if (utf8)  
     {  
     unsigned int othercase;  
     if (next < 128) othercase = cd->fcc[next]; else  
2997  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2998      othercase = UCD_OTHERCASE(next);        othercase = UCD_OTHERCASE(next);
2999  #else  #else
3000      othercase = NOTACHAR;        othercase = NOTACHAR;
3001  #endif  #endif
3002      return (unsigned int)c == othercase;        return c == othercase;
3003      }        }
3004    else      else
3005  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF */
3006    return (c == cd->fcc[next]);  /* Non-UTF-8 mode */      return (c == TABLE_GET(next, cd->fcc, next));  /* Not UTF */
3007    
3008    /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.      /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3009    When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */      When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3010    
3011    case OP_DIGIT:      case OP_DIGIT:
3012    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;      return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
3013    
3014    case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
3015    return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
3016    
3017    case OP_WHITESPACE:      case OP_WHITESPACE:
3018    return next > 127 || (cd->ctypes[next] & ctype_space) == 0;      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
3019    
3020    case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
3021    return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
3022    
3023    case OP_WORDCHAR:      case OP_WORDCHAR:
3024    return next > 127 || (cd->ctypes[next] & ctype_word) == 0;      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
3025    
3026    case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
3027    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
3028    
3029    case OP_HSPACE:      case OP_HSPACE:
3030    case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
3031    switch(next)      switch(next)
3032      {        {
3033      case 0x09:        HSPACE_CASES:
3034      case 0x20:        return op_code == OP_NOT_HSPACE;
     case 0xa0:  
     case 0x1680:  
     case 0x180e:  
     case 0x2000:  
     case 0x2001:  
     case 0x2002:  
     case 0x2003:  
     case 0x2004:  
     case 0x2005:  
     case 0x2006:  
     case 0x2007:  
     case 0x2008:  
     case 0x2009:  
     case 0x200A:  
     case 0x202f:  
     case 0x205f:  
     case 0x3000:  
     return op_code == OP_NOT_HSPACE;  
     default:  
     return op_code != OP_NOT_HSPACE;  
     }  
3035    
3036    case OP_ANYNL:        default:
3037    case OP_VSPACE:        return op_code != OP_NOT_HSPACE;
3038    case OP_NOT_VSPACE:        }
3039    switch(next)  
3040      {      case OP_ANYNL:
3041      case 0x0a:      case OP_VSPACE:
3042      case 0x0b:      case OP_NOT_VSPACE:
3043      case 0x0c:      switch(next)
3044      case 0x0d:        {
3045      case 0x85:        VSPACE_CASES:
3046      case 0x2028:        return op_code == OP_NOT_VSPACE;
3047      case 0x2029:  
3048      return op_code == OP_NOT_VSPACE;        default:
3049      default:        return op_code != OP_NOT_VSPACE;
3050      return op_code != OP_NOT_VSPACE;        }
     }  
3051    
3052  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3053    case OP_PROP:      case OP_PROP:
3054    return check_char_prop(next, previous[0], previous[1], FALSE);      return check_char_prop(next, previous[0], previous[1], FALSE);
3055    
3056    case OP_NOTPROP:      case OP_NOTPROP:
3057    return check_char_prop(next, previous[0], previous[1], TRUE);      return check_char_prop(next, previous[0], previous[1], TRUE);
3058  #endif  #endif
3059    
3060    default:      default:
3061    return FALSE;      return FALSE;
3062        }
3063    }    }
3064    
   
3065  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3066  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3067  generated only when PCRE_UCP is *not* set, that is, when only ASCII  generated only when PCRE_UCP is *not* set, that is, when only ASCII
# Line 3065  switch(op_code) Line 3072  switch(op_code)
3072    {    {
3073    case OP_CHAR:    case OP_CHAR:
3074    case OP_CHARI:    case OP_CHARI:
3075  #ifdef SUPPORT_UTF8    switch(escape)
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   switch(-next)  
3076      {      {
3077      case ESC_d:      case ESC_d:
3078      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;
3079    
3080      case ESC_D:      case ESC_D:
3081      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;
3082    
3083      case ESC_s:      case ESC_s:
3084      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;
3085    
3086      case ESC_S:      case ESC_S:
3087      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;
3088    
3089      case ESC_w:      case ESC_w:
3090      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;
3091    
3092      case ESC_W:      case ESC_W:
3093      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;
3094    
3095      case ESC_h:      case ESC_h:
3096      case ESC_H:      case ESC_H:
3097      switch(c)      switch(c)
3098        {        {
3099        case 0x09:        HSPACE_CASES:
3100        case 0x20:        return escape != ESC_h;
3101        case 0xa0:  
       case 0x1680:  
       case 0x180e:  
       case 0x2000:  
       case 0x2001:  
       case 0x2002:  
       case 0x2003:  
       case 0x2004:  
       case 0x2005:  
       case 0x2006:  
       case 0x2007:  
       case 0x2008:  
       case 0x2009:  
       case 0x200A:  
       case 0x202f:  
       case 0x205f:  
       case 0x3000:  
       return -next != ESC_h;  
3102        default:        default:
3103        return -next == ESC_h;        return escape == ESC_h;
3104        }        }
3105    
3106      case ESC_v:      case ESC_v:
3107      case ESC_V:      case ESC_V:
3108      switch(c)      switch(c)
3109        {        {
3110        case 0x0a:        VSPACE_CASES:
3111        case 0x0b:        return escape != ESC_v;
3112        case 0x0c:  
       case 0x0d:  
       case 0x85:  
       case 0x2028:  
       case 0x2029:  
       return -next != ESC_v;  
3113        default:        default:
3114        return -next == ESC_v;        return escape == ESC_v;
3115        }        }
3116    
3117      /* When PCRE_UCP is set, these values get generated for \d etc. Find      /* When PCRE_UCP is set, these values get generated for \d etc. Find
3118      their substitutions and process them. The result will always be either      their substitutions and process them. The result will always be either
3119      -ESC_p or -ESC_P. Then fall through to process those values. */      ESC_p or ESC_P. Then fall through to process those values. */
3120    
3121  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3122      case ESC_du:      case ESC_du:
# Line 3147  switch(op_code) Line 3127  switch(op_code)
3127      case ESC_SU:      case ESC_SU:
3128        {        {
3129        int temperrorcode = 0;        int temperrorcode = 0;
3130        ptr = substitutes[-next - ESC_DU];        ptr = substitutes[escape - ESC_DU];
3131        next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);        escape = check_escape(&ptr, &next, &temperrorcode, 0, options, FALSE);
3132        if (temperrorcode != 0) return FALSE;        if (temperrorcode != 0) return FALSE;
3133        ptr++;    /* For compatibility */        ptr++;    /* For compatibility */
3134        }        }
# Line 3157  switch(op_code) Line 3137  switch(op_code)
3137      case ESC_p:      case ESC_p:
3138      case ESC_P:      case ESC_P:
3139        {        {
3140        int ptype, pdata, errorcodeptr;        unsigned int ptype = 0, pdata = 0;
3141          int errorcodeptr;
3142        BOOL negated;        BOOL negated;
3143    
3144        ptr--;      /* Make ptr point at the p or P */        ptr--;      /* Make ptr point at the p or P */
3145        ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);        if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr))
3146        if (ptype < 0) return FALSE;          return FALSE;
3147        ptr++;      /* Point past the final curly ket */        ptr++;      /* Point past the final curly ket */
3148    
3149        /* If the property item is optional, we have to give up. (When generated        /* If the property item is optional, we have to give up. (When generated
# Line 3170  switch(op_code) Line 3151  switch(op_code)
3151        to the original \d etc. At this point, ptr will point to a zero byte. */        to the original \d etc. At this point, ptr will point to a zero byte. */
3152    
3153        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3154          strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)          STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3155            return FALSE;            return FALSE;
3156    
3157        /* Do the property check. */        /* Do the property check. */
3158    
3159        return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);        return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);
3160        }        }
3161  #endif  #endif
3162    
# Line 3190  switch(op_code) Line 3171  switch(op_code)
3171    these op-codes are never generated.) */    these op-codes are never generated.) */
3172    
3173    case OP_DIGIT:    case OP_DIGIT:
3174    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||    return escape == ESC_D || escape == ESC_s || escape == ESC_W ||
3175           next == -ESC_h || next == -ESC_v || next == -ESC_R;           escape == ESC_h || escape == ESC_v || escape == ESC_R;
3176    
3177    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
3178    return next == -ESC_d;    return escape == ESC_d;
3179    
3180    case OP_WHITESPACE:    case OP_WHITESPACE:
3181    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;    return escape == ESC_S || escape == ESC_d || escape == ESC_w;
3182    
3183    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3184    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;
3185    
3186    case OP_HSPACE:    case OP_HSPACE:
3187    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||    return escape == ESC_S || escape == ESC_H || escape == ESC_d ||
3188           next == -ESC_w || next == -ESC_v || next == -ESC_R;           escape == ESC_w || escape == ESC_v || escape == ESC_R;
3189    
3190    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
3191    return next == -ESC_h;    return escape == ESC_h;
3192    
3193    /* Can't have \S in here because VT matches \S (Perl anomaly) */    /* Can't have \S in here because VT matches \S (Perl anomaly) */
3194    case OP_ANYNL:    case OP_ANYNL:
3195    case OP_VSPACE:    case OP_VSPACE:
3196    return next == -ESC_V || next == -ESC_d || next == -ESC_w;    return escape == ESC_V || escape == ESC_d || escape == ESC_w;
3197    
3198    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3199    return next == -ESC_v || next == -ESC_R;    return escape == ESC_v || escape == ESC_R;
3200    
3201    case OP_WORDCHAR:    case OP_WORDCHAR:
3202    return next == -ESC_W || next == -ESC_s || next == -ESC_h ||    return escape == ESC_W || escape == ESC_s || escape == ESC_h ||
3203           next == -ESC_v || next == -ESC_R;           escape == ESC_v || escape == ESC_R;
3204    
3205    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
3206    return next == -ESC_w || next == -ESC_d;    return escape == ESC_w || escape == ESC_d;
3207    
3208    default:    default:
3209    return FALSE;    return FALSE;
# Line 3234  switch(op_code) Line 3215  switch(op_code)
3215    
3216    
3217  /*************************************************  /*************************************************
3218    *        Add a character or range to a class     *
3219    *************************************************/
3220    
3221    /* This function packages up the logic of adding a character or range of
3222    characters to a class. The character values in the arguments will be within the
3223    valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3224    mutually recursive with the function immediately below.
3225    
3226    Arguments:
3227      classbits     the bit map for characters < 256
3228      uchardptr     points to the pointer for extra data
3229      options       the options word
3230      cd            contains pointers to tables etc.
3231      start         start of range character
3232      end           end of range character
3233    
3234    Returns:        the number of < 256 characters added
3235                    the pointer to extra data is updated
3236    */
3237    
3238    static int
3239    add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3240      compile_data *cd, pcre_uint32 start, pcre_uint32 end)
3241    {
3242    pcre_uint32 c;
3243    int n8 = 0;
3244    
3245    /* If caseless matching is required, scan the range and process alternate
3246    cases. In Unicode, there are 8-bit characters that have alternate cases that
3247    are greater than 255 and vice-versa. Sometimes we can just extend the original
3248    range. */
3249    
3250    if ((options & PCRE_CASELESS) != 0)
3251      {
3252    #ifdef SUPPORT_UCP
3253      if ((options & PCRE_UTF8) != 0)
3254        {
3255        int rc;
3256        pcre_uint32 oc, od;
3257    
3258        options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3259        c = start;
3260    
3261        while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3262          {
3263          /* Handle a single character that has more than one other case. */
3264    
3265          if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3266            PRIV(ucd_caseless_sets) + rc, oc);
3267    
3268          /* Do nothing if the other case range is within the original range. */
3269    
3270          else if (oc >= start && od <= end) continue;
3271    
3272          /* Extend the original range if there is overlap, noting that if oc < c, we
3273          can't have od > end because a subrange is always shorter than the basic
3274          range. Otherwise, use a recursive call to add the additional range. */
3275    
3276          else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3277          else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3278          else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3279          }
3280        }
3281      else
3282    #endif  /* SUPPORT_UCP */
3283    
3284      /* Not UTF-mode, or no UCP */
3285    
3286      for (c = start; c <= end && c < 256; c++)
3287        {
3288        SETBIT(classbits, cd->fcc[c]);
3289        n8++;
3290        }
3291      }
3292    
3293    /* Now handle the original range. Adjust the final value according to the bit
3294    length - this means that the same lists of (e.g.) horizontal spaces can be used
3295    in all cases. */
3296    
3297    #if defined COMPILE_PCRE8
3298    #ifdef SUPPORT_UTF
3299      if ((options & PCRE_UTF8) == 0)
3300    #endif
3301      if (end > 0xff) end = 0xff;
3302    
3303    #elif defined COMPILE_PCRE16
3304    #ifdef SUPPORT_UTF
3305      if ((options & PCRE_UTF16) == 0)
3306    #endif
3307      if (end > 0xffff) end = 0xffff;
3308    
3309    #endif /* COMPILE_PCRE[8|16] */
3310    
3311    /* If all characters are less than 256, use the bit map. Otherwise use extra
3312    data. */
3313    
3314    if (end < 0x100)
3315      {
3316      for (c = start; c <= end; c++)
3317        {
3318        n8++;
3319        SETBIT(classbits, c);
3320        }
3321      }
3322    
3323    else
3324      {
3325      pcre_uchar *uchardata = *uchardptr;
3326    
3327    #ifdef SUPPORT_UTF
3328      if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3329        {
3330        if (start < end)
3331          {
3332          *uchardata++ = XCL_RANGE;
3333          uchardata += PRIV(ord2utf)(start, uchardata);
3334          uchardata += PRIV(ord2utf)(end, uchardata);
3335          }
3336        else if (start == end)
3337          {
3338          *uchardata++ = XCL_SINGLE;
3339          uchardata += PRIV(ord2utf)(start, uchardata);
3340          }
3341        }
3342      else
3343    #endif  /* SUPPORT_UTF */
3344    
3345      /* Without UTF support, character values are constrained by the bit length,
3346      and can only be > 256 for 16-bit and 32-bit libraries. */
3347    
3348    #ifdef COMPILE_PCRE8
3349        {}
3350    #else
3351      if (start < end)
3352        {
3353        *uchardata++ = XCL_RANGE;
3354        *uchardata++ = start;
3355        *uchardata++ = end;
3356        }
3357      else if (start == end)
3358        {
3359        *uchardata++ = XCL_SINGLE;
3360        *uchardata++ = start;
3361        }
3362    #endif
3363    
3364      *uchardptr = uchardata;   /* Updata extra data pointer */
3365      }
3366    
3367    return n8;    /* Number of 8-bit characters */
3368    }
3369    
3370    
3371    
3372    
3373    /*************************************************
3374    *        Add a list of characters to a class     *
3375    *************************************************/
3376    
3377    /* This function is used for adding a list of case-equivalent characters to a
3378    class, and also for adding a list of horizontal or vertical whitespace. If the
3379    list is in order (which it should be), ranges of characters are detected and
3380    handled appropriately. This function is mutually recursive with the function
3381    above.
3382    
3383    Arguments:
3384      classbits     the bit map for characters < 256
3385      uchardptr     points to the pointer for extra data
3386      options       the options word
3387      cd            contains pointers to tables etc.
3388      p             points to row of 32-bit values, terminated by NOTACHAR
3389      except        character to omit; this is used when adding lists of
3390                      case-equivalent characters to avoid including the one we
3391                      already know about
3392    
3393    Returns:        the number of < 256 characters added
3394                    the pointer to extra data is updated
3395    */
3396    
3397    static int
3398    add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3399      compile_data *cd, const pcre_uint32 *p, unsigned int except)
3400    {
3401    int n8 = 0;
3402    while (p[0] < NOTACHAR)
3403      {
3404      int n = 0;
3405      if (p[0] != except)
3406        {
3407        while(p[n+1] == p[0] + n + 1) n++;
3408        n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3409        }
3410      p += n + 1;
3411      }
3412    return n8;
3413    }
3414    
3415    
3416    
3417    /*************************************************
3418    *    Add characters not in a list to a class     *
3419    *************************************************/
3420    
3421    /* This function is used for adding the complement of a list of horizontal or
3422    vertical whitespace to a class. The list must be in order.
3423    
3424    Arguments:
3425      classbits     the bit map for characters < 256
3426      uchardptr     points to the pointer for extra data
3427      options       the options word
3428      cd            contains pointers to tables etc.
3429      p             points to row of 32-bit values, terminated by NOTACHAR
3430    
3431    Returns:        the number of < 256 characters added
3432                    the pointer to extra data is updated
3433    */
3434    
3435    static int
3436    add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3437      int options, compile_data *cd, const pcre_uint32 *p)
3438    {
3439    BOOL utf = (options & PCRE_UTF8) != 0;
3440    int n8 = 0;
3441    if (p[0] > 0)
3442      n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
3443    while (p[0] < NOTACHAR)
3444      {
3445      while (p[1] == p[0] + 1) p++;
3446      n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3447        (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
3448      p++;
3449      }
3450    return n8;
3451    }
3452    
3453    
3454    
3455    /*************************************************
3456  *           Compile one branch                   *  *           Compile one branch                   *
3457  *************************************************/  *************************************************/
3458    
# Line 3244  to find out the amount of memory needed, Line 3463  to find out the amount of memory needed,
3463  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
3464    
3465  Arguments:  Arguments:
3466    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
3467    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
3468    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
3469    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
3470    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr      place to put the first required character
3471    reqbyteptr     set to the last literal character required, else < 0    firstcharflagsptr place to put the first character flags, or a negative number
3472    bcptr          points to current branch chain    reqcharptr        place to put the last required character
3473    cond_depth     conditional nesting depth    reqcharflagsptr   place to put the last required character flags, or a negative number
3474    cd             contains pointers to tables etc.    bcptr             points to current branch chain
3475    lengthptr      NULL during the real compile phase    cond_depth        conditional nesting depth
3476                   points to length accumulator during pre-compile phase    cd                contains pointers to tables etc.
3477      lengthptr         NULL during the real compile phase
3478                        points to length accumulator during pre-compile phase
3479    
3480  Returns:         TRUE on success  Returns:            TRUE on success
3481                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
3482  */  */
3483    
3484  static BOOL  static BOOL
3485  compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3486    int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,    const pcre_uchar **ptrptr, int *errorcodeptr,
3487    int cond_depth, compile_data *cd, int *lengthptr)    pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
3488      pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
3489      branch_chain *bcptr, int cond_depth,
3490      compile_data *cd, int *lengthptr)
3491  {  {
3492  int repeat_type, op_type;  int repeat_type, op_type;
3493  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3494  int bravalue = 0;  int bravalue = 0;
3495  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3496  int firstbyte, reqbyte;  pcre_uint32 firstchar, reqchar;
3497  int zeroreqbyte, zerofirstbyte;  pcre_int32 firstcharflags, reqcharflags;
3498  int req_caseopt, reqvary, tempreqvary;  pcre_uint32 zeroreqchar, zerofirstchar;
3499    pcre_int32 zeroreqcharflags, zerofirstcharflags;
3500    pcre_int32 req_caseopt, reqvary, tempreqvary;
3501  int options = *optionsptr;               /* May change dynamically */  int options = *optionsptr;               /* May change dynamically */
3502  int after_manual_callout = 0;  int after_manual_callout = 0;
3503  int length_prevgroup = 0;  int length_prevgroup = 0;
3504  register int c;  register pcre_uint32 c;
3505  register uschar *code = *codeptr;  int escape;
3506  uschar *last_code = code;  register pcre_uchar *code = *codeptr;
3507  uschar *orig_code = code;  pcre_uchar *last_code = code;
3508  uschar *tempcode;  pcre_uchar *orig_code = code;
3509    pcre_uchar *tempcode;
3510  BOOL inescq = FALSE;  BOOL inescq = FALSE;
3511  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstchar = FALSE;
3512  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
3513  const uschar *tempptr;  const pcre_uchar *tempptr;
3514  const uschar *nestptr = NULL;  const pcre_uchar *nestptr = NULL;
3515  uschar *previous = NULL;  pcre_uchar *previous = NULL;
3516  uschar *previous_callout = NULL;  pcre_uchar *previous_callout = NULL;
3517  uschar *save_hwm = NULL;  pcre_uchar *save_hwm = NULL;
3518  uschar classbits[32];  pcre_uint8 classbits[32];
3519    
3520  /* We can fish out the UTF-8 setting once and for all into a BOOL, but we  /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3521  must not do this for other options (e.g. PCRE_EXTENDED) because they may change  must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3522  dynamically as we process the pattern. */  dynamically as we process the pattern. */
3523    
3524  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3525  BOOL class_utf8;  /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
3526  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
3527  uschar *class_utf8data;  #ifndef COMPILE_PCRE32
3528  uschar *class_utf8data_base;  pcre_uchar utf_chars[6];
3529  uschar utf8_char[6];  #endif
3530  #else  #else
3531  BOOL utf8 = FALSE;  BOOL utf = FALSE;
3532    #endif
3533    
3534    /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
3535    class_uchardata always so that it can be passed to add_to_class() always,
3536    though it will not be used in non-UTF 8-bit cases. This avoids having to supply
3537    alternative calls for the different cases. */
3538    
3539    pcre_uchar *class_uchardata;
3540    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3541    BOOL xclass;
3542    pcre_uchar *class_uchardata_base;
3543  #endif  #endif
3544    
3545  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
# Line 3315  greedy_non_default = greedy_default ^ 1; Line 3553  greedy_non_default = greedy_default ^ 1;
3553    
3554  /* Initialize no first byte, no required byte. REQ_UNSET means "no char  /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3555  matching encountered yet". It gets changed to REQ_NONE if we hit something that  matching encountered yet". It gets changed to REQ_NONE if we hit something that
3556  matches a non-fixed char first char; reqbyte just remains unset if we never  matches a non-fixed char first char; reqchar just remains unset if we never
3557  find one.  find one.
3558    
3559  When we hit a repeat whose minimum is zero, we may have to adjust these values  When we hit a repeat whose minimum is zero, we may have to adjust these values
3560  to take the zero repeat into account. This is implemented by setting them to  to take the zero repeat into account. This is implemented by setting them to
3561  zerofirstbyte and zeroreqbyte when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3562  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
3563    
3564  firstbyte = reqbyte = zerofirstbyte = zeroreqbyte = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
3565    firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
3566    
3567  /* The variable req_caseopt contains either the REQ_CASELESS value or zero,  /* The variable req_caseopt contains either the REQ_CASELESS value
3568  according to the current setting of the caseless flag. REQ_CASELESS is a bit  or zero, according to the current setting of the caseless flag. The
3569  value > 255. It is added into the firstbyte or reqbyte variables to record the  REQ_CASELESS leaves the lower 28 bit empty. It is added into the
3570  case status of the value. This is used only for ASCII characters. */  firstchar or reqchar variables to record the case status of the
3571    value. This is used only for ASCII characters. */
3572    
3573  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
3574    
3575  /* Switch on next character until the end of the branch */  /* Switch on next character until the end of the branch */
3576    
# Line 3342  for (;; ptr++) Line 3582  for (;; ptr++)
3582    BOOL is_quantifier;    BOOL is_quantifier;
3583    BOOL is_recurse;    BOOL is_recurse;
3584    BOOL reset_bracount;    BOOL reset_bracount;
3585    int class_charcount;    int class_has_8bitchar;
3586    int class_lastchar;    int class_one_char;
3587    int newoptions;    int newoptions;
3588    int recno;    int recno;
3589    int refsign;    int refsign;
3590    int skipbytes;    int skipbytes;
3591    int subreqbyte;    pcre_uint32 subreqchar, subfirstchar;
3592    int subfirstbyte;    pcre_int32 subreqcharflags, subfirstcharflags;
3593    int terminator;    int terminator;
3594    int mclength;    unsigned int mclength;
3595    int tempbracount;    unsigned int tempbracount;
3596    uschar mcbuffer[8];    pcre_uint32 ec;
3597      pcre_uchar mcbuffer[8];
3598    
3599    /* Get next byte in the pattern */    /* Get next character in the pattern */
3600    
3601    c = *ptr;    c = *ptr;
3602    
3603    /* If we are at the end of a nested substitution, revert to the outer level    /* If we are at the end of a nested substitution, revert to the outer level
3604    string. Nesting only happens one level deep. */    string. Nesting only happens one level deep. */
3605    
3606    if (c == 0 && nestptr != NULL)    if (c == CHAR_NULL && nestptr != NULL)
3607      {      {
3608      ptr = nestptr;      ptr = nestptr;
3609      nestptr = NULL;      nestptr = NULL;
# Line 3377  for (;; ptr++) Line 3618  for (;; ptr++)
3618  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
3619      if (code > cd->hwm) cd->hwm = code;                 /* High water info */      if (code > cd->hwm) cd->hwm = code;                 /* High water info */
3620  #endif  #endif
3621      if (code > cd->start_workspace + cd->workspace_size -      if (code > cd->start_workspace + cd->workspace_size -
3622          WORK_SIZE_SAFETY_MARGIN)                       /* Check for overrun */          WORK_SIZE_SAFETY_MARGIN)                       /* Check for overrun */
3623        {        {
3624        *errorcodeptr = ERR52;        *errorcodeptr = ERR52;
# Line 3401  for (;; ptr++) Line 3642  for (;; ptr++)
3642        }        }
3643    
3644      *lengthptr += (int)(code - last_code);      *lengthptr += (int)(code - last_code);
3645      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, (int)(code - last_code),      DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
3646        c));        (int)(code - last_code), c, c));
3647    
3648      /* If "previous" is set and it is not at the start of the work space, move      /* If "previous" is set and it is not at the start of the work space, move
3649      it back to there, in order to avoid filling up the work space. Otherwise,      it back to there, in order to avoid filling up the work space. Otherwise,
# Line 3412  for (;; ptr++) Line 3653  for (;; ptr++)
3653        {        {
3654        if (previous > orig_code)        if (previous > orig_code)
3655          {          {
3656          memmove(orig_code, previous, code - previous);          memmove(orig_code, previous, IN_UCHARS(code - previous));
3657          code -= previous - orig_code;          code -= previous - orig_code;
3658          previous = orig_code;          previous = orig_code;
3659          }          }
# Line 3428  for (;; ptr++) Line 3669  for (;; ptr++)
3669    /* In the real compile phase, just check the workspace used by the forward    /* In the real compile phase, just check the workspace used by the forward
3670    reference list. */    reference list. */
3671    
3672    else if (cd->hwm > cd->start_workspace + cd->workspace_size -    else if (cd->hwm > cd->start_workspace + cd->workspace_size -
3673             WORK_SIZE_SAFETY_MARGIN)             WORK_SIZE_SAFETY_MARGIN)
3674      {      {
3675      *errorcodeptr = ERR52;      *errorcodeptr = ERR52;
# Line 3437  for (;; ptr++) Line 3678  for (;; ptr++)
3678    
3679    /* If in \Q...\E, check for the end; if not, we have a literal */    /* If in \Q...\E, check for the end; if not, we have a literal */
3680    
3681    if (inescq && c != 0)    if (inescq && c != CHAR_NULL)
3682      {      {
3683      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
3684        {        {
# Line 3481  for (;; ptr++) Line 3722  for (;; ptr++)
3722    
3723    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
3724      {      {
3725      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if (MAX_255(*ptr) && (cd->ctypes[c] & ctype_space) != 0) continue;
3726      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3727        {        {
3728        ptr++;        ptr++;
3729        while (*ptr != 0)        while (*ptr != CHAR_NULL)
3730          {          {
3731          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3732          ptr++;          ptr++;
3733  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3734          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf) FORWARDCHAR(ptr);
3735  #endif  #endif
3736          }          }
3737        if (*ptr != 0) continue;        if (*ptr != CHAR_NULL) continue;
3738    
3739        /* Else fall through to handle end of string */        /* Else fall through to handle end of string */
3740        c = 0;        c = 0;
# Line 3514  for (;; ptr++) Line 3755  for (;; ptr++)
3755      case 0:                        /* The branch terminates at string end */      case 0:                        /* The branch terminates at string end */
3756      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
3757      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
3758      *firstbyteptr = firstbyte;      *firstcharptr = firstchar;
3759      *reqbyteptr = reqbyte;      *firstcharflagsptr = firstcharflags;
3760        *reqcharptr = reqchar;
3761        *reqcharflagsptr = reqcharflags;
3762      *codeptr = code;      *codeptr = code;
3763      *ptrptr = ptr;      *ptrptr = ptr;
3764      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 3539  for (;; ptr++) Line 3782  for (;; ptr++)
3782      previous = NULL;      previous = NULL;
3783      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3784        {        {
3785        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
3786        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
3787        }        }
3788      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 3551  for (;; ptr++) Line 3794  for (;; ptr++)
3794      break;      break;
3795    
3796      /* There can never be a first char if '.' is first, whatever happens about      /* There can never be a first char if '.' is first, whatever happens about
3797      repeats. The value of reqbyte doesn't change either. */      repeats. The value of reqchar doesn't change either. */
3798    
3799      case CHAR_DOT:      case CHAR_DOT:
3800      if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
3801      zerofirstbyte = firstbyte;      zerofirstchar = firstchar;
3802      zeroreqbyte = reqbyte;      zerofirstcharflags = firstcharflags;
3803        zeroreqchar = reqchar;
3804        zeroreqcharflags = reqcharflags;
3805      previous = code;      previous = code;
3806      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
3807      break;      break;
# Line 3611  for (;; ptr++) Line 3856  for (;; ptr++)
3856          {          {
3857          if (ptr[1] == CHAR_E)          if (ptr[1] == CHAR_E)
3858            ptr++;            ptr++;
3859          else if (strncmp((const char *)ptr+1,          else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0)
                           STR_Q STR_BACKSLASH STR_E, 3) == 0)  
3860            ptr += 3;            ptr += 3;
3861          else          else
3862            break;            break;
# Line 3631  for (;; ptr++) Line 3875  for (;; ptr++)
3875          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
3876        {        {
3877        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
3878        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
3879        zerofirstbyte = firstbyte;        zerofirstchar = firstchar;
3880          zerofirstcharflags = firstcharflags;
3881        break;        break;
3882        }        }
3883    
# Line 3642  for (;; ptr++) Line 3887  for (;; ptr++)
3887    
3888      should_flip_negation = FALSE;      should_flip_negation = FALSE;
3889    
3890      /* Keep a count of chars with values < 256 so that we can optimize the case      /* For optimization purposes, we track some properties of the class:
3891      of just a single character (as long as it's < 256). However, For higher      class_has_8bitchar will be non-zero if the class contains at least one <
3892      valued UTF-8 characters, we don't yet do any optimization. */      256 character; class_one_char will be 1 if the class contains just one
3893        character. */
3894    
3895      class_charcount = 0;      class_has_8bitchar = 0;
3896      class_lastchar = -1;      class_one_char = 0;
3897    
3898      /* Initialize the 32-char bit map to all zeros. We build the map in a      /* Initialize the 32-char bit map to all zeros. We build the map in a
3899      temporary bit of memory, in case the class contains only 1 character (less      temporary bit of memory, in case the class contains fewer than two
3900      than 256), because in that case the compiled code doesn't use the bit map.      8-bit characters because in that case the compiled code doesn't use the bit
3901      */      map. */
3902    
3903      memset(classbits, 0, 32 * sizeof(uschar));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
3904    
3905  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3906      class_utf8 = FALSE;                       /* No chars >= 256 */      xclass = FALSE;
3907      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
3908      class_utf8data_base = class_utf8data;     /* For resetting in pass 1 */      class_uchardata_base = class_uchardata;   /* Save the start */
3909  #endif  #endif
3910    
3911      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
3912      means that an initial ] is taken as a data character. At the start of the      means that an initial ] is taken as a data character. At the start of the
3913      loop, c contains the first byte of the character. */      loop, c contains the first byte of the character. */
3914    
3915      if (c != 0) do      if (c != CHAR_NULL) do
3916        {        {
3917        const uschar *oldptr;        const pcre_uchar *oldptr;
3918    
3919  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3920        if (utf8 && c > 127)        if (utf && HAS_EXTRALEN(c))
3921          {                           /* Braces are required because the */          {                           /* Braces are required because the */
3922          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
3923          }          }
3924    #endif
3925    
3926        /* In the pre-compile phase, accumulate the length of any UTF-8 extra  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3927          /* In the pre-compile phase, accumulate the length of any extra
3928        data and reset the pointer. This is so that very large classes that        data and reset the pointer. This is so that very large classes that
3929        contain a zillion UTF-8 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
3930        (which is on the stack). */        (which is on the stack). We have to remember that there was XCLASS data,
3931          however. */
3932        if (lengthptr != NULL)  
3933          {        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
3934          *lengthptr += class_utf8data - class_utf8data_base;          {
3935          class_utf8data = class_utf8data_base;          xclass = TRUE;
3936            *lengthptr += class_uchardata - class_uchardata_base;
3937            class_uchardata = class_uchardata_base;
3938          }          }
   
3939  #endif  #endif
3940    
3941        /* Inside \Q...\E everything is literal except \E */        /* Inside \Q...\E everything is literal except \E */
# Line 3714  for (;; ptr++) Line 3963  for (;; ptr++)
3963          {          {
3964          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
3965          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
3966          register const uschar *cbits = cd->cbits;          register const pcre_uint8 *cbits = cd->cbits;
3967          uschar pbits[32];          pcre_uint8 pbits[32];
3968    
3969          if (ptr[1] != CHAR_COLON)          if (ptr[1] != CHAR_COLON)
3970            {            {
# Line 3770  for (;; ptr++) Line 4019  for (;; ptr++)
4019          /* Copy in the first table (always present) */          /* Copy in the first table (always present) */
4020    
4021          memcpy(pbits, cbits + posix_class_maps[posix_class],          memcpy(pbits, cbits + posix_class_maps[posix_class],
4022            32 * sizeof(uschar));            32 * sizeof(pcre_uint8));
4023    
4024          /* If there is a second table, add or remove it as required. */          /* If there is a second table, add or remove it as required. */
4025    
# Line 3785  for (;; ptr++) Line 4034  for (;; ptr++)
4034              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4035            }            }
4036    
4037          /* Not see if we need to remove any special characters. An option          /* Now see if we need to remove any special characters. An option
4038          value of 1 removes vertical space and 2 removes underscore. */          value of 1 removes vertical space and 2 removes underscore. */
4039    
4040          if (tabopt < 0) tabopt = -tabopt;          if (tabopt < 0) tabopt = -tabopt;
# Line 3801  for (;; ptr++) Line 4050  for (;; ptr++)
4050            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4051    
4052          ptr = tempptr + 1;          ptr = tempptr + 1;
4053          class_charcount = 10;  /* Set > 1; assumes more than 1 per class */          /* Every class contains at least one < 256 character. */
4054            class_has_8bitchar = 1;
4055            /* Every class contains at least two characters. */
4056            class_one_char = 2;
4057          continue;    /* End of POSIX syntax handling */          continue;    /* End of POSIX syntax handling */
4058          }          }
4059    
4060        /* Backslash may introduce a single character, or it may introduce one        /* Backslash may introduce a single character, or it may introduce one
4061        of the specials, which just set a flag. The sequence \b is a special        of the specials, which just set a flag. The sequence \b is a special
4062        case. Inside a class (and only there) it is treated as backspace. We        case. Inside a class (and only there) it is treated as backspace. We
4063        assume that other escapes have more than one character in them, so set        assume that other escapes have more than one character in them, so
4064        class_charcount bigger than one. Unrecognized escapes fall through and        speculatively set both class_has_8bitchar and class_one_char bigger
4065        are either treated as literal characters (by default), or are faulted if        than one. Unrecognized escapes fall through and are either treated
4066          as literal characters (by default), or are faulted if
4067        PCRE_EXTRA is set. */        PCRE_EXTRA is set. */
4068    
4069        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
4070          {          {
4071          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
4072              TRUE);
4073          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
4074            if (escape == 0) c = ec;
4075          if (-c == ESC_b) c = CHAR_BS;    /* \b is backspace in a class */          else if (escape == ESC_b) c = CHAR_BS; /* \b is backspace in a class */
4076          else if (-c == ESC_N)            /* \N is not supported in a class */          else if (escape == ESC_N)          /* \N is not supported in a class */
4077            {            {
4078            *errorcodeptr = ERR71;            *errorcodeptr = ERR71;
4079            goto FAILED;            goto FAILED;
4080            }            }
4081          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (escape == ESC_Q)            /* Handle start of quoted string */
4082            {            {
4083            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4084              {              {
# Line 3833  for (;; ptr++) Line 4087  for (;; ptr++)
4087            else inescq = TRUE;            else inescq = TRUE;
4088            continue;            continue;
4089            }            }
4090          else if (-c == ESC_E) continue;  /* Ignore orphan \E */          else if (escape == ESC_E) continue;  /* Ignore orphan \E */
4091    
4092          if (c < 0)          else
4093            {            {
4094            register const uschar *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
4095            class_charcount += 2;     /* Greater than 1 is what matters */            /* Every class contains at least two < 256 characters. */
4096              class_has_8bitchar++;
4097              /* Every class contains at least two characters. */
4098              class_one_char += 2;
4099    
4100            switch (-c)            switch (escape)
4101              {              {
4102  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4103              case ESC_du:     /* These are the values given for \d etc */              case ESC_du:     /* These are the values given for \d etc */
# Line 3850  for (;; ptr++) Line 4107  for (;; ptr++)
4107              case ESC_su:     /* of the default ASCII testing. */              case ESC_su:     /* of the default ASCII testing. */
4108              case ESC_SU:              case ESC_SU:
4109              nestptr = ptr;              nestptr = ptr;
4110              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */              ptr = substitutes[escape - ESC_DU] - 1;  /* Just before substitute */
4111              class_charcount -= 2;                /* Undo! */              class_has_8bitchar--;                /* Undo! */
4112              continue;              continue;
4113  #endif  #endif
4114              case ESC_d:              case ESC_d:
# Line 3874  for (;; ptr++) Line 4131  for (;; ptr++)
4131    
4132              /* Perl 5.004 onwards omits VT from \s, but we must preserve it              /* Perl 5.004 onwards omits VT from \s, but we must preserve it
4133              if it was previously set by something earlier in the character              if it was previously set by something earlier in the character
4134              class. */              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and
4135                EBCDIC, so we lazily just adjust the appropriate bit. */
4136    
4137              case ESC_s:              case ESC_s:
4138              classbits[0] |= cbits[cbit_space];              classbits[0] |= cbits[cbit_space];
# Line 3888  for (;; ptr++) Line 4146  for (;; ptr++)
4146              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
4147              continue;              continue;
4148    
4149                /* The rest apply in both UCP and non-UCP cases. */
4150    
4151              case ESC_h:              case ESC_h:
4152              SETBIT(classbits, 0x09); /* VT */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4153              SETBIT(classbits, 0x20); /* SPACE */                PRIV(hspace_list), NOTACHAR);
             SETBIT(classbits, 0xa0); /* NSBP */  
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);  
               }  
 #endif  
4154              continue;              continue;
4155    
4156              case ESC_H:              case ESC_H:
4157              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4158                {                cd, PRIV(hspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x09/8: x ^= 1 << (0x09%8); break;  
                 case 0x20/8: x ^= 1 << (0x20%8); break;  
                 case 0xa0/8: x ^= 1 << (0xa0%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
   
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);  
               }  
 #endif  
4159              continue;              continue;
4160    
4161              case ESC_v:              case ESC_v:
4162              SETBIT(classbits, 0x0a); /* LF */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4163              SETBIT(classbits, 0x0b); /* VT */                PRIV(vspace_list), NOTACHAR);
             SETBIT(classbits, 0x0c); /* FF */  
             SETBIT(classbits, 0x0d); /* CR */  
             SETBIT(classbits, 0x85); /* NEL */  
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);  
               }  
 #endif  
4164              continue;              continue;
4165    
4166              case ESC_V:              case ESC_V:
4167              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4168                {                cd, PRIV(vspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x0a/8: x ^= 1 << (0x0a%8);  
                              x ^= 1 << (0x0b%8);  
                              x ^= 1 << (0x0c%8);  
                              x ^= 1 << (0x0d%8);  
                              break;  
                 case 0x85/8: x ^= 1 << (0x85%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
   
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);  
               }  
 #endif  
4169              continue;              continue;
4170    
4171  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 4009  for (;; ptr++) Line 4173  for (;; ptr++)
4173              case ESC_P:              case ESC_P:
4174                {                {
4175                BOOL negated;                BOOL negated;
4176                int pdata;                unsigned int ptype = 0, pdata = 0;
4177                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
4178                if (ptype < 0) goto FAILED;                  goto FAILED;
4179                class_utf8 = TRUE;                *class_uchardata++ = ((escape == ESC_p) != negated)?
               *class_utf8data++ = ((-c == ESC_p) != negated)?  
4180                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
4181                *class_utf8data++ = ptype;                *class_uchardata++ = ptype;
4182                *class_utf8data++ = pdata;                *class_uchardata++ = pdata;
4183                class_charcount -= 2;   /* Not a < 256 character */                class_has_8bitchar--;                /* Undo! */
4184                continue;                continue;
4185                }                }
4186  #endif  #endif
# Line 4031  for (;; ptr++) Line 4194  for (;; ptr++)
4194                *errorcodeptr = ERR7;                *errorcodeptr = ERR7;
4195                goto FAILED;                goto FAILED;
4196                }                }
4197              class_charcount -= 2;  /* Undo the default count from above */              class_has_8bitchar--;    /* Undo the speculative increase. */
4198              c = *ptr;              /* Get the final character and fall through */              class_one_char -= 2;     /* Undo the speculative increase. */
4199                c = *ptr;                /* Get the final character and fall through */
4200              break;              break;
4201              }              }
4202            }            }
4203    
4204          /* Fall through if we have a single character (c >= 0). This may be          /* Fall through if the escape just defined a single character (c >= 0).
4205          greater than 256 in UTF-8 mode. */          This may be greater than 256. */
4206    
4207            escape = 0;
4208    
4209          }   /* End of backslash handling */          }   /* End of backslash handling */
4210    
4211        /* A single character may be followed by '-' to form a range. However,        /* A character may be followed by '-' to form a range. However, Perl does
4212        Perl does not permit ']' to be the end of the range. A '-' character        not permit ']' to be the end of the range. A '-' character at the end is
4213        at the end is treated as a literal. Perl ignores orphaned \E sequences        treated as a literal. Perl ignores orphaned \E sequences entirely. The
4214        entirely. The code for handling \Q and \E is messy. */        code for handling \Q and \E is messy. */
4215    
4216        CHECK_RANGE:        CHECK_RANGE:
4217        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 4053  for (;; ptr++) Line 4219  for (;; ptr++)
4219          inescq = FALSE;          inescq = FALSE;
4220          ptr += 2;          ptr += 2;
4221          }          }
   
4222        oldptr = ptr;        oldptr = ptr;
4223    
4224        /* Remember \r or \n */        /* Remember if \r or \n were explicitly used */
4225    
4226        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4227    
# Line 4064  for (;; ptr++) Line 4229  for (;; ptr++)
4229    
4230        if (!inescq && ptr[1] == CHAR_MINUS)        if (!inescq && ptr[1] == CHAR_MINUS)
4231          {          {
4232          int d;          pcre_uint32 d;
4233          ptr += 2;          ptr += 2;
4234          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;
4235    
# Line 4080  for (;; ptr++) Line 4245  for (;; ptr++)
4245            break;            break;
4246            }            }
4247    
4248          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          /* Minus (hyphen) at the end of a class is treated as a literal, so put
4249            back the pointer and jump to handle the character that preceded it. */
4250    
4251            if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4252            {            {
4253            ptr = oldptr;            ptr = oldptr;
4254            goto LONE_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4255            }            }
4256    
4257  #ifdef SUPPORT_UTF8          /* Otherwise, we have a potential range; pick up the next character */
4258          if (utf8)  
4259    #ifdef SUPPORT_UTF
4260            if (utf)
4261            {                           /* Braces are required because the */            {                           /* Braces are required because the */
4262            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */
4263            }            }
# Line 4101  for (;; ptr++) Line 4271  for (;; ptr++)
4271    
4272          if (!inescq && d == CHAR_BACKSLASH)          if (!inescq && d == CHAR_BACKSLASH)
4273            {            {
4274            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            int descape;
4275              descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
4276            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4277    
4278            /* \b is backspace; any other special means the '-' was literal */            /* \b is backspace; any other special means the '-' was literal. */
4279    
4280            if (d < 0)            if (descape != 0)
4281              {              {
4282              if (d == -ESC_b) d = CHAR_BS; else              if (descape == ESC_b) d = CHAR_BS; else
4283                {                {
4284                ptr = oldptr;                ptr = oldptr;
4285                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4286                }                }
4287              }              }
4288            }            }
4289    
4290          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
4291          one-character ranges */          one-character ranges. */
4292    
4293          if (d < c)          if (d < c)
4294            {            {
4295            *errorcodeptr = ERR8;            *errorcodeptr = ERR8;
4296            goto FAILED;            goto FAILED;
4297            }            }
4298            if (d == c) goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4299    
4300          if (d == c) goto LONE_SINGLE_CHARACTER;  /* A few lines below */          /* We have found a character range, so single character optimizations
4301            cannot be done anymore. Any value greater than 1 indicates that there
4302            is more than one character. */
4303    
4304          /* Remember \r or \n */          class_one_char = 2;
4305    
4306          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;          /* Remember an explicit \r or \n, and add the range to the class. */
4307    
4308          /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
         matching, we have to use an XCLASS with extra data items. Caseless  
         matching for characters > 127 is available only if UCP support is  
         available. */  
   
 #ifdef SUPPORT_UTF8  
         if (utf8 && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))  
           {  
           class_utf8 = TRUE;  
   
           /* With UCP support, we can find the other case equivalents of  
           the relevant characters. There may be several ranges. Optimize how  
           they fit with the basic range. */  
4309    
4310  #ifdef SUPPORT_UCP          class_has_8bitchar +=
4311            if ((options & PCRE_CASELESS) != 0)            add_to_class(classbits, &class_uchardata, options, cd, c, d);
             {  
             unsigned int occ, ocd;  
             unsigned int cc = c;  
             unsigned int origd = d;  
             while (get_othercase_range(&cc, origd, &occ, &ocd))  
               {  
               if (occ >= (unsigned int)c &&  
                   ocd <= (unsigned int)d)  
                 continue;                          /* Skip embedded ranges */  
   
               if (occ < (unsigned int)c  &&  
                   ocd >= (unsigned int)c - 1)      /* Extend the basic range */  
                 {                                  /* if there is overlap,   */  
                 c = occ;                           /* noting that if occ < c */  
                 continue;                          /* we can't have ocd > d  */  
                 }                                  /* because a subrange is  */  
               if (ocd > (unsigned int)d &&  
                   occ <= (unsigned int)d + 1)      /* always shorter than    */  
                 {                                  /* the basic range.       */  
                 d = ocd;  
                 continue;  
                 }  
4312    
4313                if (occ == ocd)          continue;   /* Go get the next char in the class */
4314                  {          }
                 *class_utf8data++ = XCL_SINGLE;  
                 }  
               else  
                 {  
                 *class_utf8data++ = XCL_RANGE;  
                 class_utf8data += _pcre_ord2utf8(occ, class_utf8data);  
                 }  
               class_utf8data += _pcre_ord2utf8(ocd, class_utf8data);  
               }  
             }  
 #endif  /* SUPPORT_UCP */  
4315    
4316            /* Now record the original range, possibly modified for UCP caseless        /* Handle a single character - we can get here for a normal non-escape
4317            overlapping ranges. */        char, or after \ that introduces a single character or for an apparent
4318          range that isn't. Only the value 1 matters for class_one_char, so don't
4319          increase it if it is already 2 or more ... just in case there's a class
4320          with a zillion characters in it. */
4321    
4322          CLASS_SINGLE_CHARACTER:
4323          if (class_one_char < 2) class_one_char++;
4324    
4325          /* If class_one_char is 1, we have the first single character in the
4326          class, and there have been no prior ranges, or XCLASS items generated by
4327          escapes. If this is the final character in the class, we can optimize by
4328          turning the item into a 1-character OP_CHAR[I] if it's positive, or
4329          OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
4330          to be set. Otherwise, there can be no first char if this item is first,
4331          whatever repeat count may follow. In the case of reqchar, save the
4332          previous value for reinstating. */
4333    
4334            *class_utf8data++ = XCL_RANGE;        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
4335            class_utf8data += _pcre_ord2utf8(c, class_utf8data);          {
4336            class_utf8data += _pcre_ord2utf8(d, class_utf8data);          ptr++;
4337            zeroreqchar = reqchar;
4338            /* With UCP support, we are done. Without UCP support, there is no          zeroreqcharflags = reqcharflags;
           caseless matching for UTF-8 characters > 127; we can use the bit map  
           for the smaller ones. */  
4339    
4340            if (negate_class)
4341              {
4342  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4343            continue;    /* With next character in the class */            int d;
4344  #else  #endif
4345            if ((options & PCRE_CASELESS) == 0 || c > 127) continue;            if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4346              zerofirstchar = firstchar;
4347            /* Adjust upper limit and fall through to set up the map */            zerofirstcharflags = firstcharflags;
4348    
4349            d = 127;            /* For caseless UTF-8 mode when UCP support is available, check
4350     &