/[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 760 by ph10, Tue Nov 22 11:23:43 2011 UTC revision 1045 by ph10, Sun Sep 23 16:50:00 2012 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-2012 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)_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,
88        int, int, int *, int *, branch_chain *, compile_data *, int *);
89    
90    
91    
92  /*************************************************  /*************************************************
93  *      Code parameters and static tables         *  *      Code parameters and static tables         *
# Line 88  so this number is very generous. Line 103  so this number is very generous.
103  The same workspace is used during the second, actual compile phase for  The same workspace is used during the second, actual compile phase for
104  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
105  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
106  is 4 there is plenty of room. */  is 4 there is plenty of room for most patterns. However, the memory can get
107    filled up by repetitions of forward references, for example patterns like
108    /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
109    that the workspace is expanded using malloc() in this situation. The value
110    below is therefore a minimum, and we put a maximum on it for safety. The
111    minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
112    kicks in at the same number of forward references in all cases. */
113    
114  #define COMPILE_WORK_SIZE (4096)  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
115    #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
116    
117  /* 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
118  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. */
119    
120  #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)  #define WORK_SIZE_SAFETY_MARGIN (100)
121    
122    /* Private flags added to firstchar and reqchar. */
123    
124    #define REQ_CASELESS   0x10000000l      /* Indicates caselessness */
125    #define REQ_VARY       0x20000000l      /* Reqchar followed non-literal item */
126    
127    /* Repeated character flags. */
128    
129    #define UTF_LENGTH     0x10000000l      /* The char contains its length. */
130    
131  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
132  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 231  static const char posix_names[] = Line 261  static const char posix_names[] =
261    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
262    STRING_word0  STRING_xdigit;    STRING_word0  STRING_xdigit;
263    
264  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
265    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 };
266    
267  /* 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 266  substitutes must be in the order of the Line 296  substitutes must be in the order of the
296  both positive and negative cases. NULL means no substitute. */  both positive and negative cases. NULL means no substitute. */
297    
298  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
299  static const uschar *substitutes[] = {  static const pcre_uchar string_PNd[]  = {
300    (uschar *)"\\P{Nd}",    /* \D */    CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
301    (uschar *)"\\p{Nd}",    /* \d */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
302    (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */  static const pcre_uchar string_pNd[]  = {
303    (uschar *)"\\p{Xsp}",   /* \s */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
304    (uschar *)"\\P{Xwd}",   /* \W */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
305    (uschar *)"\\p{Xwd}"    /* \w */  static const pcre_uchar string_PXsp[] = {
306      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
307      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
308    static const pcre_uchar string_pXsp[] = {
309      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
310      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
311    static const pcre_uchar string_PXwd[] = {
312      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
313      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
314    static const pcre_uchar string_pXwd[] = {
315      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
316      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
317    
318    static const pcre_uchar *substitutes[] = {
319      string_PNd,           /* \D */
320      string_pNd,           /* \d */
321      string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
322      string_pXsp,          /* \s */
323      string_PXwd,          /* \W */
324      string_pXwd           /* \w */
325  };  };
326    
327  static const uschar *posix_substitutes[] = {  static const pcre_uchar string_pL[] =   {
328    (uschar *)"\\p{L}",     /* alpha */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
329    (uschar *)"\\p{Ll}",    /* lower */    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
330    (uschar *)"\\p{Lu}",    /* upper */  static const pcre_uchar string_pLl[] =  {
331    (uschar *)"\\p{Xan}",   /* alnum */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
332    NULL,                   /* ascii */    CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
333    (uschar *)"\\h",        /* blank */  static const pcre_uchar string_pLu[] =  {
334    NULL,                   /* cntrl */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
335    (uschar *)"\\p{Nd}",    /* digit */    CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
336    NULL,                   /* graph */  static const pcre_uchar string_pXan[] = {
337    NULL,                   /* print */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
338    NULL,                   /* punct */    CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
339    (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */  static const pcre_uchar string_h[] =    {
340    (uschar *)"\\p{Xwd}",   /* word */    CHAR_BACKSLASH, CHAR_h, '\0' };
341    NULL,                   /* xdigit */  static const pcre_uchar string_pXps[] = {
342      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
343      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
344    static const pcre_uchar string_PL[] =   {
345      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
346      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
347    static const pcre_uchar string_PLl[] =  {
348      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
349      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
350    static const pcre_uchar string_PLu[] =  {
351      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
352      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
353    static const pcre_uchar string_PXan[] = {
354      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
355      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
356    static const pcre_uchar string_H[] =    {
357      CHAR_BACKSLASH, CHAR_H, '\0' };
358    static const pcre_uchar string_PXps[] = {
359      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
360      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
361    
362    static const pcre_uchar *posix_substitutes[] = {
363      string_pL,            /* alpha */
364      string_pLl,           /* lower */
365      string_pLu,           /* upper */
366      string_pXan,          /* alnum */
367      NULL,                 /* ascii */
368      string_h,             /* blank */
369      NULL,                 /* cntrl */
370      string_pNd,           /* digit */
371      NULL,                 /* graph */
372      NULL,                 /* print */
373      NULL,                 /* punct */
374      string_pXps,          /* space */    /* NOTE: Xps is POSIX space */
375      string_pXwd,          /* word */
376      NULL,                 /* xdigit */
377    /* Negated cases */    /* Negated cases */
378    (uschar *)"\\P{L}",     /* ^alpha */    string_PL,            /* ^alpha */
379    (uschar *)"\\P{Ll}",    /* ^lower */    string_PLl,           /* ^lower */
380    (uschar *)"\\P{Lu}",    /* ^upper */    string_PLu,           /* ^upper */
381    (uschar *)"\\P{Xan}",   /* ^alnum */    string_PXan,          /* ^alnum */
382    NULL,                   /* ^ascii */    NULL,                 /* ^ascii */
383    (uschar *)"\\H",        /* ^blank */    string_H,             /* ^blank */
384    NULL,                   /* ^cntrl */    NULL,                 /* ^cntrl */
385    (uschar *)"\\P{Nd}",    /* ^digit */    string_PNd,           /* ^digit */
386    NULL,                   /* ^graph */    NULL,                 /* ^graph */
387    NULL,                   /* ^print */    NULL,                 /* ^print */
388    NULL,                   /* ^punct */    NULL,                 /* ^punct */
389    (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */    string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
390    (uschar *)"\\P{Xwd}",   /* ^word */    string_PXwd,          /* ^word */
391    NULL                    /* ^xdigit */    NULL                  /* ^xdigit */
392  };  };
393  #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
394  #endif  #endif
395    
396  #define STRING(a)  # a  #define STRING(a)  # a
# Line 365  static const char error_texts[] = Line 449  static const char error_texts[] =
449    /* 30 */    /* 30 */
450    "unknown POSIX class name\0"    "unknown POSIX class name\0"
451    "POSIX collating elements are not supported\0"    "POSIX collating elements are not supported\0"
452    "this version of PCRE is not compiled with PCRE_UTF8 support\0"    "this version of PCRE is compiled without UTF support\0"
453    "spare error\0"  /** DEAD **/    "spare error\0"  /** DEAD **/
454    "character value in \\x{...} sequence is too large\0"    "character value in \\x{...} sequence is too large\0"
455    /* 35 */    /* 35 */
# Line 388  static const char error_texts[] = Line 472  static const char error_texts[] =
472    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
473    /* 50 */    /* 50 */
474    "repeated subpattern is too long\0"    /** DEAD **/    "repeated subpattern is too long\0"    /** DEAD **/
475    "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"
476    "internal error: overran compiling workspace\0"    "internal error: overran compiling workspace\0"
477    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
478    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
# Line 407  static const char error_texts[] = Line 491  static const char error_texts[] =
491    /* 65 */    /* 65 */
492    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
493    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
494    "this version of PCRE is not compiled with PCRE_UCP support\0"    "this version of PCRE is not compiled with Unicode property support\0"
495    "\\c must be followed by an ASCII character\0"    "\\c must be followed by an ASCII character\0"
496    "\\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"
497    /* 70 */    /* 70 */
498    "internal error: unknown opcode in find_fixedlength()\0"    "internal error: unknown opcode in find_fixedlength()\0"
499    "\\N is not supported in a class\0"    "\\N is not supported in a class\0"
500    "too many forward references\0"    "too many forward references\0"
501      "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
502      "invalid UTF-16 string\0"
503      /* 75 */
504      "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
505      "character value in \\u.... sequence is too large\0"
506    ;    ;
507    
508  /* 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 432  For convenience, we use the same bit def Line 521  For convenience, we use the same bit def
521    
522  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
523    
524    /* Using a simple comparison for decimal numbers rather than a memory read
525    is much faster, and the resulting code is simpler (the compiler turns it
526    into a subtraction and unsigned comparison). */
527    
528    #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
529    
530  #ifndef EBCDIC  #ifndef EBCDIC
531    
532  /* 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
533  UTF-8 mode. */  UTF-8 mode. */
534    
535  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
536    {    {
537    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
538    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */
# Line 476  static const unsigned char digitab[] = Line 571  static const unsigned char digitab[] =
571    
572  /* 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. */
573    
574  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
575    {    {
576    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
577    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */
# Line 511  static const unsigned char digitab[] = Line 606  static const unsigned char digitab[] =
606    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */
607    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */
608    
609  static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */  static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
610    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */
611    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */
612    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */
# Line 547  static const unsigned char ebcdic_charta Line 642  static const unsigned char ebcdic_charta
642  #endif  #endif
643    
644    
 /* 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 *);  
   
645    
646    
647  /*************************************************  /*************************************************
# Line 582  return s; Line 671  return s;
671    
672    
673  /*************************************************  /*************************************************
674    *           Expand the workspace                 *
675    *************************************************/
676    
677    /* This function is called during the second compiling phase, if the number of
678    forward references fills the existing workspace, which is originally a block on
679    the stack. A larger block is obtained from malloc() unless the ultimate limit
680    has been reached or the increase will be rather small.
681    
682    Argument: pointer to the compile data block
683    Returns:  0 if all went well, else an error number
684    */
685    
686    static int
687    expand_workspace(compile_data *cd)
688    {
689    pcre_uchar *newspace;
690    int newsize = cd->workspace_size * 2;
691    
692    if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
693    if (cd->workspace_size >= COMPILE_WORK_SIZE_MAX ||
694        newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
695     return ERR72;
696    
697    newspace = (PUBL(malloc))(IN_UCHARS(newsize));
698    if (newspace == NULL) return ERR21;
699    memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
700    cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
701    if (cd->workspace_size > COMPILE_WORK_SIZE)
702      (PUBL(free))((void *)cd->start_workspace);
703    cd->start_workspace = newspace;
704    cd->workspace_size = newsize;
705    return 0;
706    }
707    
708    
709    
710    /*************************************************
711  *            Check for counted repeat            *  *            Check for counted repeat            *
712  *************************************************/  *************************************************/
713    
# Line 597  Returns:    TRUE or FALSE Line 723  Returns:    TRUE or FALSE
723  */  */
724    
725  static BOOL  static BOOL
726  is_counted_repeat(const uschar *p)  is_counted_repeat(const pcre_uchar *p)
727  {  {
728  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
729  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
730    while (IS_DIGIT(*p)) p++;
731  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
732    
733  if (*p++ != CHAR_COMMA) return FALSE;  if (*p++ != CHAR_COMMA) return FALSE;
734  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
735    
736  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
737  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
738    while (IS_DIGIT(*p)) p++;
739    
740  return (*p == CHAR_RIGHT_CURLY_BRACKET);  return (*p == CHAR_RIGHT_CURLY_BRACKET);
741  }  }
# Line 639  Returns:         zero or positive => a d Line 767  Returns:         zero or positive => a d
767  */  */
768    
769  static int  static int
770  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,
771    int options, BOOL isclass)    int options, BOOL isclass)
772  {  {
773  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
774  const uschar *ptr = *ptrptr + 1;  BOOL utf = (options & PCRE_UTF8) != 0;
775  int c, i;  const pcre_uchar *ptr = *ptrptr + 1;
776    pcre_int32 c;
777    int i;
778    
779  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
780  ptr--;                            /* Set pointer back to the last byte */  ptr--;                            /* Set pointer back to the last byte */
# Line 658  in a table. A non-zero result is somethi Line 788  in a table. A non-zero result is somethi
788  Otherwise further processing may be required. */  Otherwise further processing may be required. */
789    
790  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
791  else if (c < CHAR_0 || c > CHAR_z) {}                     /* Not alphanumeric */  /* Not alphanumeric */
792    else if (c < CHAR_0 || c > CHAR_z) {}
793  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if ((i = escapes[c - CHAR_0]) != 0) c = i;
794    
795  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
796  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */  /* Not alphanumeric */
797    else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
798  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  c = i;
799  #endif  #endif
800    
# Line 670  else if ((i = escapes[c - 0x48]) != 0) Line 802  else if ((i = escapes[c - 0x48]) != 0)
802    
803  else  else
804    {    {
805    const uschar *oldptr;    const pcre_uchar *oldptr;
806    BOOL braced, negated;    BOOL braced, negated;
807    
808    switch (c)    switch (c)
# Line 688  else Line 820  else
820        {        {
821        /* In JavaScript, \u must be followed by four hexadecimal numbers.        /* In JavaScript, \u must be followed by four hexadecimal numbers.
822        Otherwise it is a lowercase u letter. */        Otherwise it is a lowercase u letter. */
823        if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
824             && (digitab[ptr[3]] & ctype_xdigit) != 0 && (digitab[ptr[4]] & ctype_xdigit) != 0)          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
825            && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
826            && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
827          {          {
828          c = 0;          c = 0;
829          for (i = 0; i < 4; ++i)          for (i = 0; i < 4; ++i)
# Line 703  else Line 837  else
837            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
838  #endif  #endif
839            }            }
840    
841    #ifdef COMPILE_PCRE8
842            if (c > (utf ? 0x10ffff : 0xff))
843    #else
844    #ifdef COMPILE_PCRE16
845            if (c > (utf ? 0x10ffff : 0xffff))
846    #endif
847    #endif
848              {
849              *errorcodeptr = ERR76;
850              }
851            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
852          }          }
853        }        }
854      else      else
# Line 743  else Line 889  else
889    
890      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
891        {        {
892        const uschar *p;        const pcre_uchar *p;
893        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
894          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
895        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
896          {          {
897          c = -ESC_k;          c = -ESC_k;
# Line 763  else Line 909  else
909        }        }
910      else negated = FALSE;      else negated = FALSE;
911    
912        /* The integer range is limited by the machine's int representation. */
913      c = 0;      c = 0;
914      while ((digitab[ptr[1]] & ctype_digit) != 0)      while (IS_DIGIT(ptr[1]))
915          {
916          if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
917            {
918            c = -1;
919            break;
920            }
921        c = c * 10 + *(++ptr) - CHAR_0;        c = c * 10 + *(++ptr) - CHAR_0;
922          }
923      if (c < 0)   /* Integer overflow */      if (((unsigned int)c) > INT_MAX) /* Integer overflow */
924        {        {
925          while (IS_DIGIT(ptr[1]))
926            ptr++;
927        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
928        break;        break;
929        }        }
# Line 816  else Line 971  else
971      if (!isclass)      if (!isclass)
972        {        {
973        oldptr = ptr;        oldptr = ptr;
974          /* The integer range is limited by the machine's int representation. */
975        c -= CHAR_0;        c -= CHAR_0;
976        while ((digitab[ptr[1]] & ctype_digit) != 0)        while (IS_DIGIT(ptr[1]))
977            {
978            if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
979              {
980              c = -1;
981              break;
982              }
983          c = c * 10 + *(++ptr) - CHAR_0;          c = c * 10 + *(++ptr) - CHAR_0;
984        if (c < 0)    /* Integer overflow */          }
985          if (((unsigned int)c) > INT_MAX) /* Integer overflow */
986          {          {
987            while (IS_DIGIT(ptr[1]))
988              ptr++;
989          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
990          break;          break;
991          }          }
# Line 846  else Line 1011  else
1011      /* \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
1012      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
1013      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
1014      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,
1015      than 3 octal digits. */      but no more than 3 octal digits. */
1016    
1017      case CHAR_0:      case CHAR_0:
1018      c -= CHAR_0;      c -= CHAR_0;
1019      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1020          c = c * 8 + *(++ptr) - CHAR_0;          c = c * 8 + *(++ptr) - CHAR_0;
1021      if (!utf8 && c > 255) *errorcodeptr = ERR51;  #ifdef COMPILE_PCRE8
1022        if (!utf && c > 0xff) *errorcodeptr = ERR51;
1023    #endif
1024      break;      break;
1025    
1026      /* \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
1027      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.
1028      treated as a data character. */      If not, { is treated as a data character. */
1029    
1030      case CHAR_x:      case CHAR_x:
1031      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1032        {        {
1033        /* In JavaScript, \x must be followed by two hexadecimal numbers.        /* In JavaScript, \x must be followed by two hexadecimal numbers.
1034        Otherwise it is a lowercase x letter. */        Otherwise it is a lowercase x letter. */
1035        if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0)        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1036            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1037          {          {
1038          c = 0;          c = 0;
1039          for (i = 0; i < 2; ++i)          for (i = 0; i < 2; ++i)
# Line 885  else Line 1053  else
1053    
1054      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1055        {        {
1056        const uschar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
       int count = 0;  
1057    
1058        c = 0;        c = 0;
1059        while ((digitab[*pt] & ctype_xdigit) != 0)        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
1060          {          {
1061          register int cc = *pt++;          register int cc = *pt++;
1062          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
         count++;  
1063    
1064  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1065          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
# Line 902  else Line 1068  else
1068          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 */
1069          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1070  #endif  #endif
1071    
1072    #ifdef COMPILE_PCRE8
1073            if (c > (utf ? 0x10ffff : 0xff)) { c = -1; break; }
1074    #else
1075    #ifdef COMPILE_PCRE16
1076            if (c > (utf ? 0x10ffff : 0xffff)) { c = -1; break; }
1077    #endif
1078    #endif
1079            }
1080    
1081          if (c < 0)
1082            {
1083            while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1084            *errorcodeptr = ERR34;
1085          }          }
1086    
1087        if (*pt == CHAR_RIGHT_CURLY_BRACKET)        if (*pt == CHAR_RIGHT_CURLY_BRACKET)
1088          {          {
1089          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1090          ptr = pt;          ptr = pt;
1091          break;          break;
1092          }          }
# Line 918  else Line 1098  else
1098      /* Read just a single-byte hex-defined char */      /* Read just a single-byte hex-defined char */
1099    
1100      c = 0;      c = 0;
1101      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1102        {        {
1103        int cc;                                  /* Some compilers don't like */        int cc;                                  /* Some compilers don't like */
1104        cc = *(++ptr);                           /* ++ in initializers */        cc = *(++ptr);                           /* ++ in initializers */
# Line 1016  Returns:         type value from ucp_typ Line 1196  Returns:         type value from ucp_typ
1196  */  */
1197    
1198  static int  static int
1199  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
1200  {  {
1201  int c, i, bot, top;  int c, i, bot, top;
1202  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1203  char name[32];  pcre_uchar name[32];
1204    
1205  c = *(++ptr);  c = *(++ptr);
1206  if (c == 0) goto ERROR_RETURN;  if (c == 0) goto ERROR_RETURN;
# Line 1037  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1217  if (c == CHAR_LEFT_CURLY_BRACKET)
1217      *negptr = TRUE;      *negptr = TRUE;
1218      ptr++;      ptr++;
1219      }      }
1220    for (i = 0; i < (int)sizeof(name) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1221      {      {
1222      c = *(++ptr);      c = *(++ptr);
1223      if (c == 0) goto ERROR_RETURN;      if (c == 0) goto ERROR_RETURN;
# Line 1061  else Line 1241  else
1241  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1242    
1243  bot = 0;  bot = 0;
1244  top = _pcre_utt_size;  top = PRIV(utt_size);
1245    
1246  while (bot < top)  while (bot < top)
1247    {    {
1248    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1249    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    c = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1250    if (c == 0)    if (c == 0)
1251      {      {
1252      *dptr = _pcre_utt[i].value;      *dptr = PRIV(utt)[i].value;
1253      return _pcre_utt[i].type;      return PRIV(utt)[i].type;
1254      }      }
1255    if (c > 0) bot = i + 1; else top = i;    if (c > 0) bot = i + 1; else top = i;
1256    }    }
# Line 1108  Returns:         pointer to '}' on succe Line 1288  Returns:         pointer to '}' on succe
1288                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1289  */  */
1290    
1291  static const uschar *  static const pcre_uchar *
1292  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)
1293  {  {
1294  int min = 0;  int min = 0;
1295  int max = -1;  int max = -1;
# Line 1117  int max = -1; Line 1297  int max = -1;
1297  /* 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
1298  an integer overflow. */  an integer overflow. */
1299    
1300  while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + *p++ - CHAR_0;
1301  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1302    {    {
1303    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 1132  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1312  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1312    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1313      {      {
1314      max = 0;      max = 0;
1315      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + *p++ - CHAR_0;
1316      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1317        {        {
1318        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1187  Arguments: Line 1367  Arguments:
1367    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1368    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1369    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1370    utf8         TRUE if we are in UTF-8 mode    utf          TRUE if we are in UTF-8 / UTF-16 mode
1371    count        pointer to the current capturing subpattern number (updated)    count        pointer to the current capturing subpattern number (updated)
1372    
1373  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
1374  */  */
1375    
1376  static int  static int
1377  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,
1378    BOOL xmode, BOOL utf8, int *count)    BOOL xmode, BOOL utf, int *count)
1379  {  {
1380  uschar *ptr = *ptrptr;  pcre_uchar *ptr = *ptrptr;
1381  int start_count = *count;  int start_count = *count;
1382  int hwm_count = start_count;  int hwm_count = start_count;
1383  BOOL dup_parens = FALSE;  BOOL dup_parens = FALSE;
# Line 1264  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1444  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1444          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1445        {        {
1446        int term;        int term;
1447        const uschar *thisname;        const pcre_uchar *thisname;
1448        *count += 1;        *count += 1;
1449        if (name == NULL && *count == lorn) return *count;        if (name == NULL && *count == lorn) return *count;
1450        term = *ptr++;        term = *ptr++;
# Line 1272  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1452  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1452        thisname = ptr;        thisname = ptr;
1453        while (*ptr != term) ptr++;        while (*ptr != term) ptr++;
1454        if (name != NULL && lorn == ptr - thisname &&        if (name != NULL && lorn == ptr - thisname &&
1455            strncmp((const char *)name, (const char *)thisname, lorn) == 0)            STRNCMP_UC_UC(name, thisname, lorn) == 0)
1456          return *count;          return *count;
1457        term++;        term++;
1458        }        }
# Line 1315  for (; ptr < cd->end_pattern; ptr++) Line 1495  for (; ptr < cd->end_pattern; ptr++)
1495          {          {
1496          if (ptr[2] == CHAR_E)          if (ptr[2] == CHAR_E)
1497            ptr+= 2;            ptr+= 2;
1498          else if (strncmp((const char *)ptr+2,          else if (STRNCMP_UC_C8(ptr + 2,
1499                   STR_Q STR_BACKSLASH STR_E, 3) == 0)                   STR_Q STR_BACKSLASH STR_E, 3) == 0)
1500            ptr += 4;            ptr += 4;
1501          else          else
# Line 1363  for (; ptr < cd->end_pattern; ptr++) Line 1543  for (; ptr < cd->end_pattern; ptr++)
1543        {        {
1544        if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }        if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1545        ptr++;        ptr++;
1546  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1547        if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;        if (utf) FORWARDCHAR(ptr);
1548  #endif  #endif
1549        }        }
1550      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
# Line 1375  for (; ptr < cd->end_pattern; ptr++) Line 1555  for (; ptr < cd->end_pattern; ptr++)
1555    
1556    if (*ptr == CHAR_LEFT_PARENTHESIS)    if (*ptr == CHAR_LEFT_PARENTHESIS)
1557      {      {
1558      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, count);
1559      if (rc > 0) return rc;      if (rc > 0) return rc;
1560      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1561      }      }
# Line 1421  Arguments: Line 1601  Arguments:
1601    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1602    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1603    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1604    utf8         TRUE if we are in UTF-8 mode    utf          TRUE if we are in UTF-8 / UTF-16 mode
1605    
1606  Returns:       the number of the found subpattern, or -1 if not found  Returns:       the number of the found subpattern, or -1 if not found
1607  */  */
1608    
1609  static int  static int
1610  find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode,  find_parens(compile_data *cd, const pcre_uchar *name, int lorn, BOOL xmode,
1611    BOOL utf8)    BOOL utf)
1612  {  {
1613  uschar *ptr = (uschar *)cd->start_pattern;  pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1614  int count = 0;  int count = 0;
1615  int rc;  int rc;
1616    
# Line 1441  matching closing parens. That is why we Line 1621  matching closing parens. That is why we
1621    
1622  for (;;)  for (;;)
1623    {    {
1624    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, &count);
1625    if (rc > 0 || *ptr++ == 0) break;    if (rc > 0 || *ptr++ == 0) break;
1626    }    }
1627    
# Line 1468  Arguments: Line 1648  Arguments:
1648  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1649  */  */
1650    
1651  static const uschar*  static const pcre_uchar*
1652  first_significant_code(const uschar *code, BOOL skipassert)  first_significant_code(const pcre_uchar *code, BOOL skipassert)
1653  {  {
1654  for (;;)  for (;;)
1655    {    {
# Line 1480  for (;;) Line 1660  for (;;)
1660      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1661      if (!skipassert) return code;      if (!skipassert) return code;
1662      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
1663      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1664      break;      break;
1665    
1666      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
# Line 1494  for (;;) Line 1674  for (;;)
1674      case OP_RREF:      case OP_RREF:
1675      case OP_NRREF:      case OP_NRREF:
1676      case OP_DEF:      case OP_DEF:
1677      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1678      break;      break;
1679    
1680      default:      default:
# Line 1524  and doing the check at the end; a flag s Line 1704  and doing the check at the end; a flag s
1704    
1705  Arguments:  Arguments:
1706    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1707    utf8     TRUE in UTF-8 mode    utf      TRUE in UTF-8 / UTF-16 mode
1708    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1709    cd       the "compile data" structure    cd       the "compile data" structure
1710    
# Line 1536  Returns:   the fixed length, Line 1716  Returns:   the fixed length,
1716  */  */
1717    
1718  static int  static int
1719  find_fixedlength(uschar *code, BOOL utf8, BOOL atend, compile_data *cd)  find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1720  {  {
1721  int length = -1;  int length = -1;
1722    
1723  register int branchlength = 0;  register int branchlength = 0;
1724  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1725    
1726  /* 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
1727  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1549  branch, check the length against that of Line 1729  branch, check the length against that of
1729  for (;;)  for (;;)
1730    {    {
1731    int d;    int d;
1732    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1733    register int op = *cc;    register int op = *cc;
1734    
1735    switch (op)    switch (op)
1736      {      {
1737      /* 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 1563  for (;;) Line 1744  for (;;)
1744      case OP_ONCE:      case OP_ONCE:
1745      case OP_ONCE_NC:      case OP_ONCE_NC:
1746      case OP_COND:      case OP_COND:
1747      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);
1748      if (d < 0) return d;      if (d < 0) return d;
1749      branchlength += d;      branchlength += d;
1750      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 1594  for (;;) Line 1775  for (;;)
1775    
1776      case OP_RECURSE:      case OP_RECURSE:
1777      if (!atend) return -3;      if (!atend) return -3;
1778      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1779      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1780      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1781      d = find_fixedlength(cs + 2, utf8, atend, cd);      d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd);
1782      if (d < 0) return d;      if (d < 0) return d;
1783      branchlength += d;      branchlength += d;
1784      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1610  for (;;) Line 1791  for (;;)
1791      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1792      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1793      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1794      /* Fall through */      cc += PRIV(OP_lengths)[*cc];
1795        break;
1796    
1797      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1798    
# Line 1618  for (;;) Line 1800  for (;;)
1800      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1801      case OP_SKIP_ARG:      case OP_SKIP_ARG:
1802      case OP_THEN_ARG:      case OP_THEN_ARG:
1803      cc += cc[1] + _pcre_OP_lengths[*cc];      cc += cc[1] + PRIV(OP_lengths)[*cc];
1804      break;      break;
1805    
1806      case OP_CALLOUT:      case OP_CALLOUT:
# Line 1645  for (;;) Line 1827  for (;;)
1827      case OP_SOM:      case OP_SOM:
1828      case OP_THEN:      case OP_THEN:
1829      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1830      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1831      break;      break;
1832    
1833      /* Handle literal characters */      /* Handle literal characters */
# Line 1656  for (;;) Line 1838  for (;;)
1838      case OP_NOTI:      case OP_NOTI:
1839      branchlength++;      branchlength++;
1840      cc += 2;      cc += 2;
1841  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1842      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1843  #endif  #endif
1844      break;      break;
1845    
# Line 1669  for (;;) Line 1851  for (;;)
1851      case OP_NOTEXACT:      case OP_NOTEXACT:
1852      case OP_NOTEXACTI:      case OP_NOTEXACTI:
1853      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1854      cc += 4;      cc += 2 + IMM2_SIZE;
1855  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1856      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1857  #endif  #endif
1858      break;      break;
1859    
1860      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1861      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1862      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
1863      cc += 4;      cc += 1 + IMM2_SIZE + 1;
1864      break;      break;
1865    
1866      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1704  for (;;) Line 1886  for (;;)
1886      cc++;      cc++;
1887      break;      break;
1888    
1889      /* 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;
1890      otherwise \C is coded as OP_ALLANY. */      otherwise \C is coded as OP_ALLANY. */
1891    
1892      case OP_ANYBYTE:      case OP_ANYBYTE:
# Line 1712  for (;;) Line 1894  for (;;)
1894    
1895      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1896    
1897  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || defined COMPILE_PCRE16
1898      case OP_XCLASS:      case OP_XCLASS:
1899      cc += GET(cc, 1) - 33;      cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];
1900      /* Fall through */      /* Fall through */
1901  #endif  #endif
1902    
1903      case OP_CLASS:      case OP_CLASS:
1904      case OP_NCLASS:      case OP_NCLASS:
1905      cc += 33;      cc += PRIV(OP_lengths)[OP_CLASS];
1906    
1907      switch (*cc)      switch (*cc)
1908        {        {
# Line 1734  for (;;) Line 1916  for (;;)
1916    
1917        case OP_CRRANGE:        case OP_CRRANGE:
1918        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1919        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1920        branchlength += GET2(cc,1);        branchlength += GET2(cc,1);
1921        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1922        break;        break;
1923    
1924        default:        default:
# Line 1851  length. Line 2033  length.
2033    
2034  Arguments:  Arguments:
2035    code        points to start of expression    code        points to start of expression
2036    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 mode
2037    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
2038    
2039  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
2040  */  */
2041    
2042  const uschar *  const pcre_uchar *
2043  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
2044  {  {
2045  for (;;)  for (;;)
2046    {    {
# Line 1876  for (;;) Line 2058  for (;;)
2058    
2059    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
2060      {      {
2061      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
2062      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2063      }      }
2064    
2065    /* Handle capturing bracket */    /* Handle capturing bracket */
# Line 1886  for (;;) Line 2068  for (;;)
2068             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
2069      {      {
2070      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
2071      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
2072      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2073      }      }
2074    
2075    /* 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 1915  for (;;) Line 2097  for (;;)
2097        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2098        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2099        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
2100        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP
2101            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2102        break;        break;
2103    
2104        case OP_MARK:        case OP_MARK:
# Line 1931  for (;;) Line 2114  for (;;)
2114    
2115      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2116    
2117      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2118    
2119    /* 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
2120    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
2121    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
2122    
2123  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2124      if (utf8) switch(c)      if (utf) switch(c)
2125        {        {
2126        case OP_CHAR:        case OP_CHAR:
2127        case OP_CHARI:        case OP_CHARI:
# Line 1968  for (;;) Line 2151  for (;;)
2151        case OP_MINQUERYI:        case OP_MINQUERYI:
2152        case OP_POSQUERY:        case OP_POSQUERY:
2153        case OP_POSQUERYI:        case OP_POSQUERYI:
2154        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2155        break;        break;
2156        }        }
2157  #else  #else
2158      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2159  #endif  #endif
2160      }      }
2161    }    }
# Line 1989  instance of OP_RECURSE. Line 2172  instance of OP_RECURSE.
2172    
2173  Arguments:  Arguments:
2174    code        points to start of expression    code        points to start of expression
2175    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 mode
2176    
2177  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
2178  */  */
2179    
2180  static const uschar *  static const pcre_uchar *
2181  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
2182  {  {
2183  for (;;)  for (;;)
2184    {    {
# Line 2034  for (;;) Line 2217  for (;;)
2217        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2218        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2219        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2220        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP
2221            || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2222        break;        break;
2223    
2224        case OP_MARK:        case OP_MARK:
# Line 2050  for (;;) Line 2234  for (;;)
2234    
2235      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2236    
2237      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2238    
2239      /* 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
2240      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
2241      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
2242    
2243  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2244      if (utf8) switch(c)      if (utf) switch(c)
2245        {        {
2246        case OP_CHAR:        case OP_CHAR:
2247        case OP_CHARI:        case OP_CHARI:
2248          case OP_NOT:
2249          case OP_NOTI:
2250        case OP_EXACT:        case OP_EXACT:
2251        case OP_EXACTI:        case OP_EXACTI:
2252          case OP_NOTEXACT:
2253          case OP_NOTEXACTI:
2254        case OP_UPTO:        case OP_UPTO:
2255        case OP_UPTOI:        case OP_UPTOI:
2256          case OP_NOTUPTO:
2257          case OP_NOTUPTOI:
2258        case OP_MINUPTO:        case OP_MINUPTO:
2259        case OP_MINUPTOI:        case OP_MINUPTOI:
2260          case OP_NOTMINUPTO:
2261          case OP_NOTMINUPTOI:
2262        case OP_POSUPTO:        case OP_POSUPTO:
2263        case OP_POSUPTOI:        case OP_POSUPTOI:
2264          case OP_NOTPOSUPTO:
2265          case OP_NOTPOSUPTOI:
2266        case OP_STAR:        case OP_STAR:
2267        case OP_STARI:        case OP_STARI:
2268          case OP_NOTSTAR:
2269          case OP_NOTSTARI:
2270        case OP_MINSTAR:        case OP_MINSTAR:
2271        case OP_MINSTARI:        case OP_MINSTARI:
2272          case OP_NOTMINSTAR:
2273          case OP_NOTMINSTARI:
2274        case OP_POSSTAR:        case OP_POSSTAR:
2275        case OP_POSSTARI:        case OP_POSSTARI:
2276          case OP_NOTPOSSTAR:
2277          case OP_NOTPOSSTARI:
2278        case OP_PLUS:        case OP_PLUS:
2279        case OP_PLUSI:        case OP_PLUSI:
2280          case OP_NOTPLUS:
2281          case OP_NOTPLUSI:
2282        case OP_MINPLUS:        case OP_MINPLUS:
2283        case OP_MINPLUSI:        case OP_MINPLUSI:
2284          case OP_NOTMINPLUS:
2285          case OP_NOTMINPLUSI:
2286        case OP_POSPLUS:        case OP_POSPLUS:
2287        case OP_POSPLUSI:        case OP_POSPLUSI:
2288          case OP_NOTPOSPLUS:
2289          case OP_NOTPOSPLUSI:
2290        case OP_QUERY:        case OP_QUERY:
2291        case OP_QUERYI:        case OP_QUERYI:
2292          case OP_NOTQUERY:
2293          case OP_NOTQUERYI:
2294        case OP_MINQUERY:        case OP_MINQUERY:
2295        case OP_MINQUERYI:        case OP_MINQUERYI:
2296          case OP_NOTMINQUERY:
2297          case OP_NOTMINQUERYI:
2298        case OP_POSQUERY:        case OP_POSQUERY:
2299        case OP_POSQUERYI:        case OP_POSQUERYI:
2300        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_NOTPOSQUERY:
2301          case OP_NOTPOSQUERYI:
2302          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2303        break;        break;
2304        }        }
2305  #else  #else
2306      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2307  #endif  #endif
2308      }      }
2309    }    }
# Line 2114  bracket whose current branch will alread Line 2326  bracket whose current branch will alread
2326  Arguments:  Arguments:
2327    code        points to start of search    code        points to start of search
2328    endcode     points to where to stop    endcode     points to where to stop
2329    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 mode
2330    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2331    
2332  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2333  */  */
2334    
2335  static BOOL  static BOOL
2336  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2337    compile_data *cd)    BOOL utf, compile_data *cd)
2338  {  {
2339  register int c;  register int c;
2340  for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2341       code < endcode;       code < endcode;
2342       code = first_significant_code(code + _pcre_OP_lengths[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2343    {    {
2344    const uschar *ccode;    const pcre_uchar *ccode;
2345    
2346    c = *code;    c = *code;
2347    
# Line 2152  for (code = first_significant_code(code Line 2364  for (code = first_significant_code(code
2364    
2365    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2366      {      {
2367      const uschar *scode;      const pcre_uchar *scode;
2368      BOOL empty_branch;      BOOL empty_branch;
2369    
2370      /* Test for forward reference */      /* Test for forward reference */
# Line 2170  for (code = first_significant_code(code Line 2382  for (code = first_significant_code(code
2382    
2383      do      do
2384        {        {
2385        if (could_be_empty_branch(scode, endcode, utf8, cd))        if (could_be_empty_branch(scode, endcode, utf, cd))
2386          {          {
2387          empty_branch = TRUE;          empty_branch = TRUE;
2388          break;          break;
# Line 2188  for (code = first_significant_code(code Line 2400  for (code = first_significant_code(code
2400    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2401        c == OP_BRAPOSZERO)        c == OP_BRAPOSZERO)
2402      {      {
2403      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2404      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
2405      c = *code;      c = *code;
2406      continue;      continue;
# Line 2226  for (code = first_significant_code(code Line 2438  for (code = first_significant_code(code
2438        empty_branch = FALSE;        empty_branch = FALSE;
2439        do        do
2440          {          {
2441          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))
2442            empty_branch = TRUE;            empty_branch = TRUE;
2443          code += GET(code, 1);          code += GET(code, 1);
2444          }          }
# Line 2244  for (code = first_significant_code(code Line 2456  for (code = first_significant_code(code
2456      {      {
2457      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2458      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2459      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2460      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"
2461      here. */      here. */
2462    
2463  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2464      case OP_XCLASS:      case OP_XCLASS:
2465      ccode = code += GET(code, 1);      ccode = code += GET(code, 1);
2466      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
# Line 2256  for (code = first_significant_code(code Line 2468  for (code = first_significant_code(code
2468    
2469      case OP_CLASS:      case OP_CLASS:
2470      case OP_NCLASS:      case OP_NCLASS:
2471      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2472    
2473  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2474      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
2475  #endif  #endif
2476    
# Line 2331  for (code = first_significant_code(code Line 2543  for (code = first_significant_code(code
2543      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2544      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2545      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2546      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP
2547          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;
2548      break;      break;
2549    
2550      /* End of branch */      /* End of branch */
# Line 2346  for (code = first_significant_code(code Line 2559  for (code = first_significant_code(code
2559      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2560      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO may be followed by a multibyte character */
2561    
2562  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2563      case OP_STAR:      case OP_STAR:
2564      case OP_STARI:      case OP_STARI:
2565      case OP_MINSTAR:      case OP_MINSTAR:
# Line 2359  for (code = first_significant_code(code Line 2572  for (code = first_significant_code(code
2572      case OP_MINQUERYI:      case OP_MINQUERYI:
2573      case OP_POSQUERY:      case OP_POSQUERY:
2574      case OP_POSQUERYI:      case OP_POSQUERYI:
2575      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2576      break;      break;
2577    
2578      case OP_UPTO:      case OP_UPTO:
# Line 2368  for (code = first_significant_code(code Line 2581  for (code = first_significant_code(code
2581      case OP_MINUPTOI:      case OP_MINUPTOI:
2582      case OP_POSUPTO:      case OP_POSUPTO:
2583      case OP_POSUPTOI:      case OP_POSUPTOI:
2584      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2585      break;      break;
2586  #endif  #endif
2587    
# Line 2412  Arguments: Line 2625  Arguments:
2625    code        points to start of the recursion    code        points to start of the recursion
2626    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2627    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2628    utf8        TRUE if in UTF-8 mode    utf         TRUE if in UTF-8 / UTF-16 mode
2629    cd          pointers to tables etc    cd          pointers to tables etc
2630    
2631  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2632  */  */
2633    
2634  static BOOL  static BOOL
2635  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2636    BOOL utf8, compile_data *cd)    branch_chain *bcptr, BOOL utf, compile_data *cd)
2637  {  {
2638  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2639    {    {
2640    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))
2641      return FALSE;      return FALSE;
2642    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2643    }    }
# Line 2476  Returns:   TRUE or FALSE Line 2689  Returns:   TRUE or FALSE
2689  */  */
2690    
2691  static BOOL  static BOOL
2692  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2693  {  {
2694  int terminator;          /* Don't combine these lines; the Solaris cc */  int terminator;          /* Don't combine these lines; the Solaris cc */
2695  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
# Line 2520  Returns:     a value representing the na Line 2733  Returns:     a value representing the na
2733  */  */
2734    
2735  static int  static int
2736  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
2737  {  {
2738  const char *pn = posix_names;  const char *pn = posix_names;
2739  register int yield = 0;  register int yield = 0;
2740  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
2741    {    {
2742    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
2743      strncmp((const char *)ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;
2744    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
2745    yield++;    yield++;
2746    }    }
# Line 2559  value in the reference (which is a group Line 2772  value in the reference (which is a group
2772  Arguments:  Arguments:
2773    group      points to the start of the group    group      points to the start of the group
2774    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
2775    utf8       TRUE in UTF-8 mode    utf        TRUE in UTF-8 / UTF-16 mode
2776    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
2777    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
2778    
# Line 2567  Returns:     nothing Line 2780  Returns:     nothing
2780  */  */
2781    
2782  static void  static void
2783  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
2784    uschar *save_hwm)    pcre_uchar *save_hwm)
2785  {  {
2786  uschar *ptr = group;  pcre_uchar *ptr = group;
2787    
2788  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
2789    {    {
2790    int offset;    int offset;
2791    uschar *hc;    pcre_uchar *hc;
2792    
2793    /* 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
2794    reference. */    reference. */
# Line 2620  Arguments: Line 2833  Arguments:
2833  Returns:         new code pointer  Returns:         new code pointer
2834  */  */
2835    
2836  static uschar *  static pcre_uchar *
2837  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2838  {  {
2839  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2840  *code++ = 255;  *code++ = 255;
2841  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2842  PUT(code, LINK_SIZE, 0);                       /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2843  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
2844  }  }
2845    
2846    
# Line 2649  Returns:             nothing Line 2862  Returns:             nothing
2862  */  */
2863    
2864  static void  static void
2865  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2866  {  {
2867  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2868  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
# Line 2663  PUT(previous_callout, 2 + LINK_SIZE, len Line 2876  PUT(previous_callout, 2 + LINK_SIZE, len
2876  *************************************************/  *************************************************/
2877    
2878  /* 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
2879  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
2880  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
2881  start address.  start address. A character with multiple other cases is returned on its own
2882    with a special return value.
2883    
2884  Arguments:  Arguments:
2885    cptr        points to starting character value; updated    cptr        points to starting character value; updated
# Line 2673  Arguments: Line 2887  Arguments:
2887    ocptr       where to put start of othercase range    ocptr       where to put start of othercase range
2888    odptr       where to put end of othercase range    odptr       where to put end of othercase range
2889    
2890  Yield:        TRUE when range returned; FALSE when no more  Yield:        -1 when no more
2891                   0 when a range is returned
2892                  >0 the CASESET offset for char with multiple other cases
2893                    in this case, ocptr contains the original
2894  */  */
2895    
2896  static BOOL  static int
2897  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,
2898    unsigned int *odptr)    unsigned int *odptr)
2899  {  {
2900  unsigned int c, othercase, next;  unsigned int c, othercase, next;
2901    int co;
2902    
2903    /* Find the first character that has an other case. If it has multiple other
2904    cases, return its case offset value. */
2905    
2906  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
2907    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
2908      if ((co = UCD_CASESET(c)) != 0)
2909        {
2910        *ocptr = c++;   /* Character that has the set */
2911        *cptr = c;      /* Rest of input range */
2912        return co;
2913        }
2914      if ((othercase = UCD_OTHERCASE(c)) != c) break;
2915      }
2916    
2917  if (c > d) return FALSE;  if (c > d) return -1;  /* Reached end of range */
2918    
2919  *ocptr = othercase;  *ocptr = othercase;
2920  next = othercase + 1;  next = othercase + 1;
# Line 2696  for (++c; c <= d; c++) Line 2925  for (++c; c <= d; c++)
2925    next++;    next++;
2926    }    }
2927    
2928  *odptr = next - 1;  *odptr = next - 1;     /* End of othercase range */
2929  *cptr = c;  *cptr = c;             /* Rest of input range */
2930    return 0;
 return TRUE;  
2931  }  }
2932    
2933    
# Line 2732  switch(ptype) Line 2960  switch(ptype)
2960            prop->chartype == ucp_Lt) == negated;            prop->chartype == ucp_Lt) == negated;
2961    
2962    case PT_GC:    case PT_GC:
2963    return (pdata == _pcre_ucp_gentype[prop->chartype]) == negated;    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2964    
2965    case PT_PC:    case PT_PC:
2966    return (pdata == prop->chartype) == negated;    return (pdata == prop->chartype) == negated;
# Line 2743  switch(ptype) Line 2971  switch(ptype)
2971    /* These are specials */    /* These are specials */
2972    
2973    case PT_ALNUM:    case PT_ALNUM:
2974    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2975            _pcre_ucp_gentype[prop->chartype] == ucp_N) == negated;            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2976    
2977    case PT_SPACE:    /* Perl space */    case PT_SPACE:    /* Perl space */
2978    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2979            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2980            == negated;            == negated;
2981    
2982    case PT_PXSPACE:  /* POSIX space */    case PT_PXSPACE:  /* POSIX space */
2983    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2984            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2985            c == CHAR_FF || c == CHAR_CR)            c == CHAR_FF || c == CHAR_CR)
2986            == negated;            == negated;
2987    
2988    case PT_WORD:    case PT_WORD:
2989    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2990            _pcre_ucp_gentype[prop->chartype] == ucp_N ||            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2991            c == CHAR_UNDERSCORE) == negated;            c == CHAR_UNDERSCORE) == negated;
2992    }    }
2993  return FALSE;  return FALSE;
# Line 2778  sense to automatically possessify the re Line 3006  sense to automatically possessify the re
3006    
3007  Arguments:  Arguments:
3008    previous      pointer to the repeated opcode    previous      pointer to the repeated opcode
3009    utf8          TRUE in UTF-8 mode    utf           TRUE in UTF-8 / UTF-16 mode
3010    ptr           next character in pattern    ptr           next character in pattern
3011    options       options bits    options       options bits
3012    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 2787  Returns:        TRUE if possessifying is Line 3015  Returns:        TRUE if possessifying is
3015  */  */
3016    
3017  static BOOL  static BOOL
3018  check_auto_possessive(const uschar *previous, BOOL utf8, const uschar *ptr,  check_auto_possessive(const pcre_uchar *previous, BOOL utf,
3019    int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
3020  {  {
3021  int c, next;  pcre_int32 c, next;
3022  int op_code = *previous++;  int op_code = *previous++;
3023    
3024  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
# Line 2799  if ((options & PCRE_EXTENDED) != 0) Line 3027  if ((options & PCRE_EXTENDED) != 0)
3027    {    {
3028    for (;;)    for (;;)
3029      {      {
3030      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
3031      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
3032        {        {
3033        ptr++;        ptr++;
# Line 2807  if ((options & PCRE_EXTENDED) != 0) Line 3035  if ((options & PCRE_EXTENDED) != 0)
3035          {          {
3036          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3037          ptr++;          ptr++;
3038  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3039          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf) FORWARDCHAR(ptr);
3040  #endif  #endif
3041          }          }
3042        }        }
# Line 2826  if (*ptr == CHAR_BACKSLASH) Line 3054  if (*ptr == CHAR_BACKSLASH)
3054    if (temperrorcode != 0) return FALSE;    if (temperrorcode != 0) return FALSE;
3055    ptr++;    /* Point after the escape sequence */    ptr++;    /* Point after the escape sequence */
3056    }    }
3057    else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)
 else if ((cd->ctypes[*ptr] & ctype_meta) == 0)  
3058    {    {
3059  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3060    if (utf8) { GETCHARINC(next, ptr); } else    if (utf) { GETCHARINC(next, ptr); } else
3061  #endif  #endif
3062    next = *ptr++;    next = *ptr++;
3063    }    }
   
3064  else return FALSE;  else return FALSE;
3065    
3066  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
# Line 2843  if ((options & PCRE_EXTENDED) != 0) Line 3069  if ((options & PCRE_EXTENDED) != 0)
3069    {    {
3070    for (;;)    for (;;)
3071      {      {
3072      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
3073      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
3074        {        {
3075        ptr++;        ptr++;
# Line 2851  if ((options & PCRE_EXTENDED) != 0) Line 3077  if ((options & PCRE_EXTENDED) != 0)
3077          {          {
3078          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3079          ptr++;          ptr++;
3080  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3081          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf) FORWARDCHAR(ptr);
3082  #endif  #endif
3083          }          }
3084        }        }
# Line 2863  if ((options & PCRE_EXTENDED) != 0) Line 3089  if ((options & PCRE_EXTENDED) != 0)
3089  /* If the next thing is itself optional, we have to give up. */  /* If the next thing is itself optional, we have to give up. */
3090    
3091  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3092    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)
3093      return FALSE;      return FALSE;
3094    
3095  /* Now compare the next item with the previous opcode. First, handle cases when  /* Now compare the next item with the previous opcode. First, handle cases when
# Line 2872  the next item is a character. */ Line 3098  the next item is a character. */
3098  if (next >= 0) switch(op_code)  if (next >= 0) switch(op_code)
3099    {    {
3100    case OP_CHAR:    case OP_CHAR:
3101  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3102    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3103  #else  #else
3104    c = *previous;    c = *previous;
# Line 2884  if (next >= 0) switch(op_code) Line 3110  if (next >= 0) switch(op_code)
3110    high-valued characters. */    high-valued characters. */
3111    
3112    case OP_CHARI:    case OP_CHARI:
3113  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3114    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3115  #else  #else
3116    c = *previous;    c = *previous;
3117  #endif  #endif
3118    if (c == next) return FALSE;    if (c == next) return FALSE;
3119  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3120    if (utf8)    if (utf)
3121      {      {
3122      unsigned int othercase;      unsigned int othercase;
3123      if (next < 128) othercase = cd->fcc[next]; else      if (next < 128) othercase = cd->fcc[next]; else
# Line 2903  if (next >= 0) switch(op_code) Line 3129  if (next >= 0) switch(op_code)
3129      return (unsigned int)c != othercase;      return (unsigned int)c != othercase;
3130      }      }
3131    else    else
3132  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF */
3133    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */
   
   /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These  
   opcodes are not used for multi-byte characters, because they are coded using  
   an XCLASS instead. */  
3134    
3135    case OP_NOT:    case OP_NOT:
3136    return (c = *previous) == next;  #ifdef SUPPORT_UTF
3137      GETCHARTEST(c, previous);
3138    #else
3139      c = *previous;
3140    #endif
3141      return c == next;
3142    
3143    case OP_NOTI:    case OP_NOTI:
3144    if ((c = *previous) == next) return TRUE;  #ifdef SUPPORT_UTF
3145  #ifdef SUPPORT_UTF8    GETCHARTEST(c, previous);
3146    if (utf8)  #else
3147      c = *previous;
3148    #endif
3149      if (c == next) return TRUE;
3150    #ifdef SUPPORT_UTF
3151      if (utf)
3152      {      {
3153      unsigned int othercase;      unsigned int othercase;
3154      if (next < 128) othercase = cd->fcc[next]; else      if (next < 128) othercase = cd->fcc[next]; else
3155  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3156      othercase = UCD_OTHERCASE(next);      othercase = UCD_OTHERCASE((unsigned int)next);
3157  #else  #else
3158      othercase = NOTACHAR;      othercase = NOTACHAR;
3159  #endif  #endif
3160      return (unsigned int)c == othercase;      return (unsigned int)c == othercase;
3161      }      }
3162    else    else
3163  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF */
3164    return (c == cd->fcc[next]);  /* Non-UTF-8 mode */    return (c == TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */
3165    
3166    /* 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.
3167    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. */
3168    
3169    case OP_DIGIT:    case OP_DIGIT:
3170    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;    return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
3171    
3172    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
3173    return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;    return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
3174    
3175    case OP_WHITESPACE:    case OP_WHITESPACE:
3176    return next > 127 || (cd->ctypes[next] & ctype_space) == 0;    return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
3177    
3178    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3179    return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;    return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
3180    
3181    case OP_WORDCHAR:    case OP_WORDCHAR:
3182    return next > 127 || (cd->ctypes[next] & ctype_word) == 0;    return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
3183    
3184    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
3185    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;    return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
3186    
3187    case OP_HSPACE:    case OP_HSPACE:
3188    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
3189    switch(next)    switch(next)
3190      {      {
3191      case 0x09:      HSPACE_CASES:
     case 0x20:  
     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:  
3192      return op_code == OP_NOT_HSPACE;      return op_code == OP_NOT_HSPACE;
3193    
3194      default:      default:
3195      return op_code != OP_NOT_HSPACE;      return op_code != OP_NOT_HSPACE;
3196      }      }
# Line 2985  if (next >= 0) switch(op_code) Line 3200  if (next >= 0) switch(op_code)
3200    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3201    switch(next)    switch(next)
3202      {      {
3203      case 0x0a:      VSPACE_CASES:
     case 0x0b:  
     case 0x0c:  
     case 0x0d:  
     case 0x85:  
     case 0x2028:  
     case 0x2029:  
3204      return op_code == OP_NOT_VSPACE;      return op_code == OP_NOT_VSPACE;
3205    
3206      default:      default:
3207      return op_code != OP_NOT_VSPACE;      return op_code != OP_NOT_VSPACE;
3208      }      }
# Line 3020  switch(op_code) Line 3230  switch(op_code)
3230    {    {
3231    case OP_CHAR:    case OP_CHAR:
3232    case OP_CHARI:    case OP_CHARI:
3233  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3234    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3235  #else  #else
3236    c = *previous;    c = *previous;
# Line 3028  switch(op_code) Line 3238  switch(op_code)
3238    switch(-next)    switch(-next)
3239      {      {
3240      case ESC_d:      case ESC_d:
3241      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;
3242    
3243      case ESC_D:      case ESC_D:
3244      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;
3245    
3246      case ESC_s:      case ESC_s:
3247      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;
3248    
3249      case ESC_S:      case ESC_S:
3250      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;
3251    
3252      case ESC_w:      case ESC_w:
3253      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;
3254    
3255      case ESC_W:      case ESC_W:
3256      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;
3257    
3258      case ESC_h:      case ESC_h:
3259      case ESC_H:      case ESC_H:
3260      switch(c)      switch(c)
3261        {        {
3262        case 0x09:        HSPACE_CASES:
       case 0x20:  
       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:  
3263        return -next != ESC_h;        return -next != ESC_h;
3264    
3265        default:        default:
3266        return -next == ESC_h;        return -next == ESC_h;
3267        }        }
# Line 3077  switch(op_code) Line 3270  switch(op_code)
3270      case ESC_V:      case ESC_V:
3271      switch(c)      switch(c)
3272        {        {
3273        case 0x0a:        VSPACE_CASES:
       case 0x0b:  
       case 0x0c:  
       case 0x0d:  
       case 0x85:  
       case 0x2028:  
       case 0x2029:  
3274        return -next != ESC_v;        return -next != ESC_v;
3275    
3276        default:        default:
3277        return -next == ESC_v;        return -next == ESC_v;
3278        }        }
# Line 3125  switch(op_code) Line 3313  switch(op_code)
3313        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. */
3314    
3315        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3316          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)
3317            return FALSE;            return FALSE;
3318    
3319        /* Do the property check. */        /* Do the property check. */
# Line 3152  switch(op_code) Line 3340  switch(op_code)
3340    return next == -ESC_d;    return next == -ESC_d;
3341    
3342    case OP_WHITESPACE:    case OP_WHITESPACE:
3343    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;    return next == -ESC_S || next == -ESC_d || next == -ESC_w;
3344    
3345    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3346    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_s || next == -ESC_h || next == -ESC_v || next == -ESC_R;
3347    
3348    case OP_HSPACE:    case OP_HSPACE:
3349    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||
# Line 3189  switch(op_code) Line 3377  switch(op_code)
3377    
3378    
3379  /*************************************************  /*************************************************
3380    *        Add a character or range to a class     *
3381    *************************************************/
3382    
3383    /* This function packages up the logic of adding a character or range of
3384    characters to a class. The character values in the arguments will be within the
3385    valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3386    mutually recursive with the function immediately below.
3387    
3388    Arguments:
3389      classbits     the bit map for characters < 256
3390      uchardptr     points to the pointer for extra data
3391      options       the options word
3392      cd            contains pointers to tables etc.
3393      start         start of range character
3394      end           end of range character
3395    
3396    Returns:        the number of < 256 characters added
3397                    the pointer to extra data is updated
3398    */
3399    
3400    static int
3401    add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3402      compile_data *cd, unsigned int start, unsigned int end)
3403    {
3404    unsigned int c;
3405    int n8 = 0;
3406    
3407    /* If caseless matching is required, scan the range and process alternate
3408    cases. In Unicode, there are 8-bit characters that have alternate cases that
3409    are greater than 255 and vice-versa. Sometimes we can just extend the original
3410    range. */
3411    
3412    if ((options & PCRE_CASELESS) != 0)
3413      {
3414    #ifdef SUPPORT_UCP
3415      if ((options & PCRE_UTF8) != 0)
3416        {
3417        int rc;
3418        unsigned int oc, od;
3419    
3420        options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3421        c = start;
3422    
3423        while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3424          {
3425          /* Handle a single character that has more than one other case. */
3426    
3427          if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3428            PRIV(ucd_caseless_sets) + rc, oc);
3429    
3430          /* Do nothing if the other case range is within the original range. */
3431    
3432          else if (oc >= start && od <= end) continue;
3433    
3434          /* Extend the original range if there is overlap, noting that if oc < c, we
3435          can't have od > end because a subrange is always shorter than the basic
3436          range. Otherwise, use a recursive call to add the additional range. */
3437    
3438          else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3439          else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3440          else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3441          }
3442        }
3443      else
3444    #endif  /* SUPPORT_UCP */
3445    
3446      /* Not UTF-mode, or no UCP */
3447    
3448      for (c = start; c <= end && c < 256; c++)
3449        {
3450        SETBIT(classbits, cd->fcc[c]);
3451        n8++;
3452        }
3453      }
3454    
3455    /* Now handle the original range. Adjust the final value according to the bit
3456    length - this means that the same lists of (e.g.) horizontal spaces can be used
3457    in all cases. */
3458    
3459    #ifdef COMPILE_PCRE8
3460    #ifdef SUPPORT_UTF
3461      if ((options & PCRE_UTF8) == 0)
3462    #endif
3463      if (end > 0xff) end = 0xff;
3464    #endif
3465    
3466    #ifdef COMPILE_PCRE16
3467    #ifdef SUPPORT_UTF
3468      if ((options & PCRE_UTF16) == 0)
3469    #endif
3470      if (end > 0xffff) end = 0xffff;
3471    #endif
3472    
3473    /* If all characters are less than 256, use the bit map. Otherwise use extra
3474    data. */
3475    
3476    if (end < 0x100)
3477      {
3478      for (c = start; c <= end; c++)
3479        {
3480        n8++;
3481        SETBIT(classbits, c);
3482        }
3483      }
3484    
3485    else
3486      {
3487      pcre_uchar *uchardata = *uchardptr;
3488    
3489    #ifdef SUPPORT_UTF
3490      if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3491        {
3492        if (start < end)
3493          {
3494          *uchardata++ = XCL_RANGE;
3495          uchardata += PRIV(ord2utf)(start, uchardata);
3496          uchardata += PRIV(ord2utf)(end, uchardata);
3497          }
3498        else if (start == end)
3499          {
3500          *uchardata++ = XCL_SINGLE;
3501          uchardata += PRIV(ord2utf)(start, uchardata);
3502          }
3503        }
3504      else
3505    #endif  /* SUPPORT_UTF */
3506    
3507      /* Without UTF support, character values are constrained by the bit length,
3508      and can only be > 256 for 16-bit and 32-bit libraries. */
3509    
3510    #ifdef COMPILE_PCRE8
3511        {}
3512    #else
3513      if (start < end)
3514        {
3515        *uchardata++ = XCL_RANGE;
3516        *uchardata++ = start;
3517        *uchardata++ = end;
3518        }
3519      else if (start == end)
3520        {
3521        *uchardata++ = XCL_SINGLE;
3522        *uchardata++ = start;
3523        }
3524    #endif
3525    
3526      *uchardptr = uchardata;   /* Updata extra data pointer */
3527      }
3528    
3529    return n8;    /* Number of 8-bit characters */
3530    }
3531    
3532    
3533    
3534    
3535    /*************************************************
3536    *        Add a list of characters to a class     *
3537    *************************************************/
3538    
3539    /* This function is used for adding a list of case-equivalent characters to a
3540    class, and also for adding a list of horizontal or vertical whitespace. If the
3541    list is in order (which it should be), ranges of characters are detected and
3542    handled appropriately. This function is mutually recursive with the function
3543    above.
3544    
3545    Arguments:
3546      classbits     the bit map for characters < 256
3547      uchardptr     points to the pointer for extra data
3548      options       the options word
3549      cd            contains pointers to tables etc.
3550      p             points to row of 32-bit values, terminated by NOTACHAR
3551      except        character to omit; this is used when adding lists of
3552                      case-equivalent characters to avoid including the one we
3553                      already know about
3554    
3555    Returns:        the number of < 256 characters added
3556                    the pointer to extra data is updated
3557    */
3558    
3559    static int
3560    add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3561      compile_data *cd, const pcre_uint32 *p, unsigned int except)
3562    {
3563    int n8 = 0;
3564    while (p[0] < NOTACHAR)
3565      {
3566      int n = 0;
3567      if (p[0] != except)
3568        {
3569        while(p[n+1] == p[0] + n + 1) n++;
3570        n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3571        }
3572      p += n + 1;
3573      }
3574    return n8;
3575    }
3576    
3577    
3578    
3579    /*************************************************
3580    *    Add characters not in a list to a class     *
3581    *************************************************/
3582    
3583    /* This function is used for adding the complement of a list of horizontal or
3584    vertical whitespace to a class. The list must be in order.
3585    
3586    Arguments:
3587      classbits     the bit map for characters < 256
3588      uchardptr     points to the pointer for extra data
3589      options       the options word
3590      cd            contains pointers to tables etc.
3591      p             points to row of 32-bit values, terminated by NOTACHAR
3592    
3593    Returns:        the number of < 256 characters added
3594                    the pointer to extra data is updated
3595    */
3596    
3597    static int
3598    add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3599      int options, compile_data *cd, const pcre_uint32 *p)
3600    {
3601    int n8 = 0;
3602    if (p[0] > 0)
3603      n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
3604    while (p[0] < NOTACHAR)
3605      {
3606      while (p[1] == p[0] + 1) p++;
3607      n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3608        (p[1] == NOTACHAR)? 0x10ffff : p[1] - 1);
3609      p++;
3610      }
3611    return n8;
3612    }
3613    
3614    
3615    
3616    /*************************************************
3617  *           Compile one branch                   *  *           Compile one branch                   *
3618  *************************************************/  *************************************************/
3619    
# Line 3203  Arguments: Line 3628  Arguments:
3628    codeptr        points to the pointer to the current code point    codeptr        points to the pointer to the current code point
3629    ptrptr         points to the current pattern pointer    ptrptr         points to the current pattern pointer
3630    errorcodeptr   points to error code variable    errorcodeptr   points to error code variable
3631    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3632    reqbyteptr     set to the last literal character required, else < 0    reqcharptr     set to the last literal character required, else < 0
3633    bcptr          points to current branch chain    bcptr          points to current branch chain
3634    cond_depth     conditional nesting depth    cond_depth     conditional nesting depth
3635    cd             contains pointers to tables etc.    cd             contains pointers to tables etc.
# Line 3216  Returns:         TRUE on success Line 3641  Returns:         TRUE on success
3641  */  */
3642    
3643  static BOOL  static BOOL
3644  compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3645    int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,
3646    int cond_depth, compile_data *cd, int *lengthptr)    pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,
3647      compile_data *cd, int *lengthptr)
3648  {  {
3649  int repeat_type, op_type;  int repeat_type, op_type;
3650  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3651  int bravalue = 0;  int bravalue = 0;
3652  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3653  int firstbyte, reqbyte;  pcre_int32 firstchar, reqchar;
3654  int zeroreqbyte, zerofirstbyte;  pcre_int32 zeroreqchar, zerofirstchar;
3655  int req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
3656  int options = *optionsptr;               /* May change dynamically */  int options = *optionsptr;               /* May change dynamically */
3657  int after_manual_callout = 0;  int after_manual_callout = 0;
3658  int length_prevgroup = 0;  int length_prevgroup = 0;
3659  register int c;  register int c;
3660  register uschar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3661  uschar *last_code = code;  pcre_uchar *last_code = code;
3662  uschar *orig_code = code;  pcre_uchar *orig_code = code;
3663  uschar *tempcode;  pcre_uchar *tempcode;
3664  BOOL inescq = FALSE;  BOOL inescq = FALSE;
3665  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstchar = FALSE;
3666  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
3667  const uschar *tempptr;  const pcre_uchar *tempptr;
3668  const uschar *nestptr = NULL;  const pcre_uchar *nestptr = NULL;
3669  uschar *previous = NULL;  pcre_uchar *previous = NULL;
3670  uschar *previous_callout = NULL;  pcre_uchar *previous_callout = NULL;
3671  uschar *save_hwm = NULL;  pcre_uchar *save_hwm = NULL;
3672  uschar classbits[32];  pcre_uint8 classbits[32];
3673    
3674  /* 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
3675  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
3676  dynamically as we process the pattern. */  dynamically as we process the pattern. */
3677    
3678  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3679  BOOL class_utf8;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
3680  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
3681  uschar *class_utf8data;  pcre_uchar utf_chars[6];
 uschar *class_utf8data_base;  
 uschar utf8_char[6];  
3682  #else  #else
3683  BOOL utf8 = FALSE;  BOOL utf = FALSE;
3684    #endif
3685    
3686    /* Helper variables for OP_XCLASS opcode (for characters > 255). */
3687    
3688    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3689    BOOL xclass;
3690    pcre_uchar *class_uchardata;
3691    pcre_uchar *class_uchardata_base;
3692  #endif  #endif
3693    
3694  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
# Line 3270  greedy_non_default = greedy_default ^ 1; Line 3702  greedy_non_default = greedy_default ^ 1;
3702    
3703  /* Initialize no first byte, no required byte. REQ_UNSET means "no char  /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3704  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
3705  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
3706  find one.  find one.
3707    
3708  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
3709  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
3710  zerofirstbyte and zeroreqbyte when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3711  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
3712    
3713  firstbyte = reqbyte = zerofirstbyte = zeroreqbyte = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;
3714    
3715  /* The variable req_caseopt contains either the REQ_CASELESS value or zero,  /* The variable req_caseopt contains either the REQ_CASELESS value
3716  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
3717  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
3718  case status of the value. This is used only for ASCII characters. */  firstchar or reqchar variables to record the case status of the
3719    value. This is used only for ASCII characters. */
3720    
3721  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
3722    
3723  /* Switch on next character until the end of the branch */  /* Switch on next character until the end of the branch */
3724    
# Line 3297  for (;; ptr++) Line 3730  for (;; ptr++)
3730    BOOL is_quantifier;    BOOL is_quantifier;
3731    BOOL is_recurse;    BOOL is_recurse;
3732    BOOL reset_bracount;    BOOL reset_bracount;
3733    int class_charcount;    int class_has_8bitchar;
3734    int class_lastchar;    int class_one_char;
3735    int newoptions;    int newoptions;
3736    int recno;    int recno;
3737    int refsign;    int refsign;
3738    int skipbytes;    int skipbytes;
3739    int subreqbyte;    int subreqchar;
3740    int subfirstbyte;    int subfirstchar;
3741    int terminator;    int terminator;
3742    int mclength;    int mclength;
3743    int tempbracount;    int tempbracount;
3744    uschar mcbuffer[8];    pcre_uchar mcbuffer[8];
3745    
3746    /* Get next byte in the pattern */    /* Get next character in the pattern */
3747    
3748    c = *ptr;    c = *ptr;
3749    
# Line 3332  for (;; ptr++) Line 3765  for (;; ptr++)
3765  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
3766      if (code > cd->hwm) cd->hwm = code;                 /* High water info */      if (code > cd->hwm) cd->hwm = code;                 /* High water info */
3767  #endif  #endif
3768      if (code > cd->start_workspace + WORK_SIZE_CHECK)   /* Check for overrun */      if (code > cd->start_workspace + cd->workspace_size -
3769            WORK_SIZE_SAFETY_MARGIN)                       /* Check for overrun */
3770        {        {
3771        *errorcodeptr = ERR52;        *errorcodeptr = ERR52;
3772        goto FAILED;        goto FAILED;
# Line 3355  for (;; ptr++) Line 3789  for (;; ptr++)
3789        }        }
3790    
3791      *lengthptr += (int)(code - last_code);      *lengthptr += (int)(code - last_code);
3792      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, (int)(code - last_code),      DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
3793        c));        (int)(code - last_code), c, c));
3794    
3795      /* 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
3796      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 3366  for (;; ptr++) Line 3800  for (;; ptr++)
3800        {        {
3801        if (previous > orig_code)        if (previous > orig_code)
3802          {          {
3803          memmove(orig_code, previous, code - previous);          memmove(orig_code, previous, IN_UCHARS(code - previous));
3804          code -= previous - orig_code;          code -= previous - orig_code;
3805          previous = orig_code;          previous = orig_code;
3806          }          }
# Line 3382  for (;; ptr++) Line 3816  for (;; ptr++)
3816    /* 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
3817    reference list. */    reference list. */
3818    
3819    else if (cd->hwm > cd->start_workspace + WORK_SIZE_CHECK)    else if (cd->hwm > cd->start_workspace + cd->workspace_size -
3820               WORK_SIZE_SAFETY_MARGIN)
3821      {      {
3822      *errorcodeptr = ERR52;      *errorcodeptr = ERR52;
3823      goto FAILED;      goto FAILED;
# Line 3434  for (;; ptr++) Line 3869  for (;; ptr++)
3869    
3870    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
3871      {      {
3872      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if (MAX_255(*ptr) && (cd->ctypes[c] & ctype_space) != 0) continue;
3873      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3874        {        {
3875        ptr++;        ptr++;
# Line 3442  for (;; ptr++) Line 3877  for (;; ptr++)
3877          {          {
3878          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3879          ptr++;          ptr++;
3880  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3881          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf) FORWARDCHAR(ptr);
3882  #endif  #endif
3883          }          }
3884        if (*ptr != 0) continue;        if (*ptr != 0) continue;
# Line 3467  for (;; ptr++) Line 3902  for (;; ptr++)
3902      case 0:                        /* The branch terminates at string end */      case 0:                        /* The branch terminates at string end */
3903      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
3904      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
3905      *firstbyteptr = firstbyte;      *firstcharptr = firstchar;
3906      *reqbyteptr = reqbyte;      *reqcharptr = reqchar;
3907      *codeptr = code;      *codeptr = code;
3908      *ptrptr = ptr;      *ptrptr = ptr;
3909      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 3492  for (;; ptr++) Line 3927  for (;; ptr++)
3927      previous = NULL;      previous = NULL;
3928      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3929        {        {
3930        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3931        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
3932        }        }
3933      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 3504  for (;; ptr++) Line 3939  for (;; ptr++)
3939      break;      break;
3940    
3941      /* 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
3942      repeats. The value of reqbyte doesn't change either. */      repeats. The value of reqchar doesn't change either. */
3943    
3944      case CHAR_DOT:      case CHAR_DOT:
3945      if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3946      zerofirstbyte = firstbyte;      zerofirstchar = firstchar;
3947      zeroreqbyte = reqbyte;      zeroreqchar = reqchar;
3948      previous = code;      previous = code;
3949      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
3950      break;      break;
# Line 3564  for (;; ptr++) Line 3999  for (;; ptr++)
3999          {          {
4000          if (ptr[1] == CHAR_E)          if (ptr[1] == CHAR_E)
4001            ptr++;            ptr++;
4002          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)  
4003            ptr += 3;            ptr += 3;
4004          else          else
4005            break;            break;
# Line 3584  for (;; ptr++) Line 4018  for (;; ptr++)
4018          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4019        {        {
4020        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
4021        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4022        zerofirstbyte = firstbyte;        zerofirstchar = firstchar;
4023        break;        break;
4024        }        }
4025    
# Line 3595  for (;; ptr++) Line 4029  for (;; ptr++)
4029    
4030      should_flip_negation = FALSE;      should_flip_negation = FALSE;
4031    
4032      /* 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:
4033      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 <
4034      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
4035        character. */
4036    
4037      class_charcount = 0;      class_has_8bitchar = 0;
4038      class_lastchar = -1;      class_one_char = 0;
4039    
4040      /* 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
4041      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
4042      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
4043      */      map. */
4044    
4045      memset(classbits, 0, 32 * sizeof(uschar));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
4046    
4047  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4048      class_utf8 = FALSE;                       /* No chars >= 256 */      xclass = FALSE;
4049      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
4050      class_utf8data_base = class_utf8data;     /* For resetting in pass 1 */      class_uchardata_base = class_uchardata;   /* Save the start */
4051  #endif  #endif
4052    
4053      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
# Line 3621  for (;; ptr++) Line 4056  for (;; ptr++)
4056    
4057      if (c != 0) do      if (c != 0) do
4058        {        {
4059        const uschar *oldptr;        const pcre_uchar *oldptr;
4060    
4061  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4062        if (utf8 && c > 127)        if (utf && HAS_EXTRALEN(c))
4063          {                           /* Braces are required because the */          {                           /* Braces are required because the */
4064          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
4065          }          }
4066    #endif
4067    
4068        /* In the pre-compile phase, accumulate the length of any UTF-8 extra  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4069          /* In the pre-compile phase, accumulate the length of any extra
4070        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
4071        contain a zillion UTF-8 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
4072        (which is on the stack). */        (which is on the stack). We have to remember that there was XCLASS data,
4073          however. */
4074        if (lengthptr != NULL)  
4075          {        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
4076          *lengthptr += class_utf8data - class_utf8data_base;          {
4077          class_utf8data = class_utf8data_base;          xclass = TRUE;
4078            *lengthptr += class_uchardata - class_uchardata_base;
4079            class_uchardata = class_uchardata_base;
4080          }          }
   
4081  #endif  #endif
4082    
4083        /* Inside \Q...\E everything is literal except \E */        /* Inside \Q...\E everything is literal except \E */
# Line 3667  for (;; ptr++) Line 4105  for (;; ptr++)
4105          {          {
4106          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
4107          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
4108          register const uschar *cbits = cd->cbits;          register const pcre_uint8 *cbits = cd->cbits;
4109          uschar pbits[32];          pcre_uint8 pbits[32];
4110    
4111          if (ptr[1] != CHAR_COLON)          if (ptr[1] != CHAR_COLON)
4112            {            {
# Line 3723  for (;; ptr++) Line 4161  for (;; ptr++)
4161          /* Copy in the first table (always present) */          /* Copy in the first table (always present) */
4162    
4163          memcpy(pbits, cbits + posix_class_maps[posix_class],          memcpy(pbits, cbits + posix_class_maps[posix_class],
4164            32 * sizeof(uschar));            32 * sizeof(pcre_uint8));
4165    
4166          /* If there is a second table, add or remove it as required. */          /* If there is a second table, add or remove it as required. */
4167    
# Line 3738  for (;; ptr++) Line 4176  for (;; ptr++)
4176              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4177            }            }
4178    
4179          /* Not see if we need to remove any special characters. An option          /* Now see if we need to remove any special characters. An option
4180          value of 1 removes vertical space and 2 removes underscore. */          value of 1 removes vertical space and 2 removes underscore. */
4181    
4182          if (tabopt < 0) tabopt = -tabopt;          if (tabopt < 0) tabopt = -tabopt;
# Line 3754  for (;; ptr++) Line 4192  for (;; ptr++)
4192            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4193    
4194          ptr = tempptr + 1;          ptr = tempptr + 1;
4195          class_charcount = 10;  /* Set > 1; assumes more than 1 per class */          /* Every class contains at least one < 256 character. */
4196            class_has_8bitchar = 1;
4197            /* Every class contains at least two characters. */
4198            class_one_char = 2;
4199          continue;    /* End of POSIX syntax handling */          continue;    /* End of POSIX syntax handling */
4200          }          }
4201    
4202        /* Backslash may introduce a single character, or it may introduce one        /* Backslash may introduce a single character, or it may introduce one
4203        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
4204        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
4205        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
4206        class_charcount bigger than one. Unrecognized escapes fall through and        speculatively set both class_has_8bitchar and class_one_char bigger
4207        are either treated as literal characters (by default), or are faulted if        than one. Unrecognized escapes fall through and are either treated
4208          as literal characters (by default), or are faulted if
4209        PCRE_EXTRA is set. */        PCRE_EXTRA is set. */
4210    
4211        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
# Line 3775  for (;; ptr++) Line 4217  for (;; ptr++)
4217          else if (-c == ESC_N)            /* \N is not supported in a class */          else if (-c == ESC_N)            /* \N is not supported in a class */
4218            {            {
4219            *errorcodeptr = ERR71;            *errorcodeptr = ERR71;
4220            goto FAILED;            goto FAILED;
4221            }            }
4222          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (-c == ESC_Q)            /* Handle start of quoted string */
4223            {            {
4224            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 3790  for (;; ptr++) Line 4232  for (;; ptr++)
4232    
4233          if (c < 0)          if (c < 0)
4234            {            {
4235            register const uschar *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
4236            class_charcount += 2;     /* Greater than 1 is what matters */            /* Every class contains at least two < 256 characters. */
4237              class_has_8bitchar++;
4238              /* Every class contains at least two characters. */
4239              class_one_char += 2;
4240    
4241            switch (-c)            switch (-c)
4242              {              {
# Line 3804  for (;; ptr++) Line 4249  for (;; ptr++)
4249              case ESC_SU:              case ESC_SU:
4250              nestptr = ptr;              nestptr = ptr;
4251              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */
4252              class_charcount -= 2;                /* Undo! */              class_has_8bitchar--;                /* Undo! */
4253              continue;              continue;
4254  #endif  #endif
4255              case ESC_d:              case ESC_d:
# Line 3827  for (;; ptr++) Line 4272  for (;; ptr++)
4272    
4273              /* 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
4274              if it was previously set by something earlier in the character              if it was previously set by something earlier in the character
4275              class. */              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and
4276                EBCDIC, so we lazily just adjust the appropriate bit. */
4277    
4278              case ESC_s:              case ESC_s:
4279              classbits[0] |= cbits[cbit_space];              classbits[0] |= cbits[cbit_space];
# Line 3840  for (;; ptr++) Line 4286  for (;; ptr++)
4286              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
4287              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
4288              continue;              continue;
4289    
4290                /* The rest apply in both UCP and non-UCP cases. */
4291    
4292              case ESC_h:              case ESC_h:
4293              SETBIT(classbits, 0x09); /* VT */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4294              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  
4295              continue;              continue;
4296    
4297              case ESC_H:              case ESC_H:
4298              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4299                {                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  
4300              continue;              continue;
4301    
4302              case ESC_v:              case ESC_v:
4303              SETBIT(classbits, 0x0a); /* LF */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4304              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  
4305              continue;              continue;
4306    
4307              case ESC_V:              case ESC_V:
4308              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4309                {                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  
4310              continue;              continue;
4311    
4312  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 3965  for (;; ptr++) Line 4317  for (;; ptr++)
4317                int pdata;                int pdata;
4318                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
4319                if (ptype < 0) goto FAILED;                if (ptype < 0) goto FAILED;
4320                class_utf8 = TRUE;                *class_uchardata++ = ((-c == ESC_p) != negated)?
               *class_utf8data++ = ((-c == ESC_p) != negated)?  
4321                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
4322                *class_utf8data++ = ptype;                *class_uchardata++ = ptype;
4323                *class_utf8data++ = pdata;                *class_uchardata++ = pdata;
4324                class_charcount -= 2;   /* Not a < 256 character */                class_has_8bitchar--;                /* Undo! */
4325                continue;                continue;
4326                }                }
4327  #endif  #endif
# Line 3984  for (;; ptr++) Line 4335  for (;; ptr++)
4335                *errorcodeptr = ERR7;                *errorcodeptr = ERR7;
4336                goto FAILED;                goto FAILED;
4337                }                }
4338              class_charcount -= 2;  /* Undo the default count from above */              class_has_8bitchar--;    /* Undo the speculative increase. */
4339              c = *ptr;              /* Get the final character and fall through */              class_one_char -= 2;     /* Undo the speculative increase. */
4340                c = *ptr;                /* Get the final character and fall through */
4341              break;              break;
4342              }              }
4343            }            }
4344    
4345          /* 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).
4346          greater than 256 in UTF-8 mode. */          This may be greater than 256. */
4347    
4348          }   /* End of backslash handling */          }   /* End of backslash handling */
4349    
4350        /* 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
4351        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
4352        at the end is treated as a literal. Perl ignores orphaned \E sequences        treated as a literal. Perl ignores orphaned \E sequences entirely. The
4353        entirely. The code for handling \Q and \E is messy. */        code for handling \Q and \E is messy. */
4354    
4355        CHECK_RANGE:        CHECK_RANGE:
4356        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 4006  for (;; ptr++) Line 4358  for (;; ptr++)
4358          inescq = FALSE;          inescq = FALSE;
4359          ptr += 2;          ptr += 2;
4360          }          }
   
4361        oldptr = ptr;        oldptr = ptr;
4362    
4363        /* Remember \r or \n */        /* Remember if \r or \n were explicitly used */
4364    
4365        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4366    
# Line 4032  for (;; ptr++) Line 4383  for (;; ptr++)
4383            inescq = TRUE;            inescq = TRUE;
4384            break;            break;
4385            }            }
4386    
4387            /* Minus (hyphen) at the end of a class is treated as a literal, so put
4388            back the pointer and jump to handle the character that preceded it. */
4389    
4390          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4391            {            {
4392            ptr = oldptr;            ptr = oldptr;
4393            goto LONE_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4394            }            }
4395    
4396            /* Otherwise, we have a potential range; pick up the next character */
4397    
4398  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4399          if (utf8)          if (utf)
4400            {                           /* Braces are required because the */            {                           /* Braces are required because the */
4401            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */
4402            }            }
# Line 4057  for (;; ptr++) Line 4413  for (;; ptr++)
4413            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
4414            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4415    
4416            /* \b is backspace; any other special means the '-' was literal */            /* \b is backspace; any other special means the '-' was literal. */
4417    
4418            if (d < 0)            if (d < 0)
4419              {              {
4420              if (d == -ESC_b) d = CHAR_BS; else              if (d == -ESC_b) d = CHAR_BS; else
4421                {                {
4422                ptr = oldptr;                ptr = oldptr;
4423                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4424                }                }
4425              }              }
4426            }            }
4427    
4428          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
4429          one-character ranges */          one-character ranges. */
4430    
4431          if (d < c)          if (d < c)
4432            {            {
4433            *errorcodeptr = ERR8;            *errorcodeptr = ERR8;
4434            goto FAILED;            goto FAILED;
4435            }            }
4436            if (d == c) goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4437    
4438          if (d == c) goto LONE_SINGLE_CHARACTER;  /* A few lines below */          /* We have found a character range, so single character optimizations
4439            cannot be done anymore. Any value greater than 1 indicates that there
4440            is more than one character. */
4441    
4442            class_one_char = 2;
4443    
4444          /* Remember \r or \n */          /* Remember an explicit \r or \n, and add the range to the class. */
4445    
4446          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4447    
4448          /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless          class_has_8bitchar +=
4449          matching, we have to use an XCLASS with extra data items. Caseless            add_to_class(classbits, &class_uchardata, options, cd, c, d);
4450          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. */  
   
 #ifdef SUPPORT_UCP  
           if ((options & PCRE_CASELESS) != 0)  
             {  
             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;  
                 }  
   
               if (occ == ocd)  
                 {  
                 *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 */  
   
           /* Now record the original range, possibly modified for UCP caseless  
           overlapping ranges. */  
   
           *class_utf8data++ = XCL_RANGE;  
           class_utf8data += _pcre_ord2utf8(c, class_utf8data);  
           class_utf8data += _pcre_ord2utf8(d, class_utf8data);  
   
           /* With UCP support, we are done. Without UCP support, there is no  
           caseless matching for UTF-8 characters > 127; we can use the bit map  
           for the smaller ones. */  
   
 #ifdef SUPPORT_UCP  
           continue;    /* With next character in the class */  
 #else  
           if ((options & PCRE_CASELESS) == 0 || c > 127) continue;  
   
           /* Adjust upper limit and fall through to set up the map */  
   
           d = 127;  
   
 #endif  /* SUPPORT_UCP */  
           }  
 #endif  /* SUPPORT_UTF8 */  
   
         /* We use the bit map for all cases when not in UTF-8 mode; else  
         ranges that lie entirely within 0-127 when there is UCP support; else  
         for partial ranges without UCP support. */  
   
         class_charcount += d - c + 1;  
         class_lastchar = d;  
   
         /* We can save a bit of time by skipping this in the pre-compile. */  
   
         if (lengthptr == NULL) for (; c <= d; c++)  
           {  
           classbits[c/8] |= (1 << (c&7));  
           if ((options & PCRE_CASELESS) != 0)  
             {  
             int uc = cd->fcc[c];           /* flip case */  
             classbits[uc/8] |= (1 << (uc&7));  
             }  
           }  
   
4451          continue;   /* Go get the next char in the class */          continue;   /* Go get the next char in the class */
4452          }          }
4453    
4454        /* Handle a lone single character - we can get here for a normal        /* Handle a single character - we can get here for a normal non-escape
4455        non-escape char, or after \ that introduces a single character or for an        char, or after \ that introduces a single character or for an apparent
4456        apparent range that isn't. */        range that isn't. Only the value 1 matters for class_one_char, so don't
4457          increase it if it is already 2 or more ... just in case there's a class
4458        LONE_SINGLE_CHARACTER:        with a zillion characters in it. */
4459    
4460          CLASS_SINGLE_CHARACTER:
4461          if (class_one_char < 2) class_one_char++;
4462    
4463          /* If class_one_char is 1, we have the first single character in the
4464          class, and there have been no prior ranges, or XCLASS items generated by
4465          escapes. If this is the final character in the class, we can optimize by
4466          turning the item into a 1-character OP_CHAR[I] if it's positive, or
4467          OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
4468          to be set. Otherwise, there can be no first char if this item is first,
4469          whatever repeat count may follow. In the case of reqchar, save the
4470          previous value for reinstating. */
4471    
4472        /* Handle a character that cannot go in the bit map */        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
   
 #ifdef SUPPORT_UTF8  
       if (utf8 && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127)))  
4473          {          {
4474          class_utf8 = TRUE;          ptr++;
4475          *class_utf8data++ = XCL_SINGLE;          zeroreqchar = reqchar;
         class_utf8data += _pcre_ord2utf8(c, class_utf8data);  
4476    
4477  #ifdef SUPPORT_UCP          if (negate_class)
         if ((options & PCRE_CASELESS) != 0)  
4478            {            {
4479            unsigned int othercase;            if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4480            if ((othercase = UCD_OTHERCASE(c)) != c)            zerofirstchar = firstchar;
4481              {            *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4482              *class_utf8data++ = XCL_SINGLE;  #ifdef SUPPORT_UTF
4483              class_utf8data += _pcre_ord2utf8(othercase, class_utf8data);            if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4484              }              code += PRIV(ord2utf)(c, code);
4485              else
4486    #endif
4487                *code++ = c;
4488              goto NOT_CHAR;
4489            }            }
 #endif  /* SUPPORT_UCP */  
4490    
4491          }          /* For a single, positive character, get the value into mcbuffer, and
4492        else          then we can handle this with the normal one-character code. */
 #endif  /* SUPPORT_UTF8 */  
4493    
4494        /* Handle a single-byte character */  #ifdef SUPPORT_UTF
4495          {          if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4496          classbits[c/8] |= (1 << (c&7));            mclength = PRIV(ord2utf)(c, mcbuffer);
4497          if ((options & PCRE_CASELESS) != 0)          else
4498    #endif
4499            {            {
4500            c = cd->fcc[c];   /* flip case */            mcbuffer[0] = c;
4501            classbits[c/8] |= (1 << (c&7));            mclength = 1;
4502            }            }
4503          class_charcount++;          goto ONE_CHAR;
4504          class_lastchar = c;          }       /* End of 1-char optimization */
4505          }  
4506          /* There is more than one character in the class, or an XCLASS item
4507          has been generated. Add this character to the class. */
4508    
4509          class_has_8bitchar +=
4510            add_to_class(classbits, &class_uchardata, options, cd, c, c);
4511        }        }
4512    
4513      /* Loop until ']' reached. This "while" is the end of the "do" far above.      /* Loop until ']' reached. This "while" is the end of the "do" far above.
# Line 4244  for (;; ptr++) Line 4527  for (;; ptr++)
4527        goto FAILED;        goto FAILED;
4528        }        }
4529    
4530      /* If class_charcount is 1, we saw precisely one character whose value is      /* We will need an XCLASS if data has been placed in class_uchardata. In
4531      less than 256. As long as there were no characters >= 128 and there was no      the second phase this is a sufficient test. However, in the pre-compile
4532      use of \p or \P, in other words, no use of any XCLASS features, we can      phase, class_uchardata gets emptied to prevent workspace overflow, so it
4533      optimize.      only if the very last character in the class needs XCLASS will it contain
4534        anything at this point. For this reason, xclass gets set TRUE above when
4535      In UTF-8 mode, we can optimize the negative case only if there were no      uchar_classdata is emptied, and that's why this code is the way it is here
4536      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR      instead of just doing a test on class_uchardata below. */
4537      operate on single-bytes characters only. This is an historical hangover.  
4538      Maybe one day we can tidy these opcodes to handle multi-byte characters.  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4539        if (class_uchardata > class_uchardata_base) xclass = TRUE;
4540      The optimization throws away the bit map. We turn the item into a  #endif
4541      1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.  
4542      Note that OP_NOT[I] does not support multibyte characters. In the positive      /* If this is the first thing in the branch, there can be no first char
4543      case, it can cause firstbyte to be set. Otherwise, there can be no first      setting, whatever the repeat count. Any reqchar setting must remain
4544      char if this item is first, whatever repeat count may follow. In the case      unchanged after any kind of repeat. */
4545      of reqbyte, save the previous value for reinstating. */  
4546        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4547  #ifdef SUPPORT_UTF8      zerofirstchar = firstchar;
4548      if (class_charcount == 1 && !class_utf8 &&      zeroreqchar = reqchar;
       (!utf8 || !negate_class || class_lastchar < 128))  
 #else  
     if (class_charcount == 1)  
 #endif  
       {  
       zeroreqbyte = reqbyte;  
   
       /* The OP_NOT[I] opcodes work on one-byte characters only. */  
   
       if (negate_class)  
         {  
         if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;  
         zerofirstbyte = firstbyte;  
         *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;  
         *code++ = class_lastchar;  
         break;  
         }  
   
       /* For a single, positive character, get the value into mcbuffer, and  
       then we can handle this with the normal one-character code. */  
   
 #ifdef SUPPORT_UTF8  
       if (utf8 && class_lastchar > 127)  
         mclength = _pcre_ord2utf8(class_lastchar, mcbuffer);  
       else  
 #endif  
         {  
         mcbuffer[0] = class_lastchar;  
         mclength = 1;  
         }  
       goto ONE_CHAR;  
       }       /* End of 1-char optimization */  
   
     /* The general case - not the one-char optimization. If this is the first  
     thing in the branch, there can be no first char setting, whatever the  
     repeat count. Any reqbyte setting must remain unchanged after any kind of  
     repeat. */  
   
     if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;  
     zerofirstbyte = firstbyte;  
     zeroreqbyte = reqbyte;  
4549    
4550      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
4551      extended class, with its own opcode, unless there was a negated special      extended class, with its own opcode, unless there was a negated special
# Line 4313  for (;; ptr++) Line 4555  for (;; ptr++)
4555      be listed) there are no characters < 256, we can omit the bitmap in the      be listed) there are no characters < 256, we can omit the bitmap in the
4556      actual compiled code. */      actual compiled code. */
4557    
4558  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4559      if (class_utf8 && (!should_flip_negation || (options & PCRE_UCP) != 0))      if (xclass && (!should_flip_negation || (options & PCRE_UCP) != 0))
4560    #elif !defined COMPILE_PCRE8
4561        if (xclass && !should_flip_negation)
4562    #endif
4563    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4564        {        {
4565        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */        *class_uchardata++ = XCL_END;    /* Marks the end of extra data */
4566        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
4567        code += LINK_SIZE;        code += LINK_SIZE;
4568        *code = negate_class? XCL_NOT : 0;        *code = negate_class? XCL_NOT:0;
4569    
4570        /* If the map is required, move up the extra data to make room for it;        /* If the map is required, move up the extra data to make room for it;
4571        otherwise just move the code pointer to the end of the extra data. */        otherwise just move the code pointer to the end of the extra data. */
4572    
4573        if (class_charcount > 0)        if (class_has_8bitchar > 0)
4574          {          {
4575          *code++ |= XCL_MAP;          *code++ |= XCL_MAP;
4576          memmove(code + 32, code, class_utf8data - code);          memmove(code + (32 / sizeof(pcre_uchar)), code,
4577              IN_UCHARS(class_uchardata - code));
4578          memcpy(code, classbits, 32);          memcpy(code, classbits, 32);
4579          code = class_utf8data + 32;          code = class_uchardata + (32 / sizeof(pcre_uchar));
4580          }          }
4581        else code = class_utf8data;        else code = class_uchardata;
4582    
4583        /* Now fill in the complete length of the item */        /* Now fill in the complete length of the item */
4584    
4585        PUT(previous, 1, code - previous);        PUT(previous, 1, (int)(code - previous));
4586        break;   /* End of class handling */        break;   /* End of class handling */
4587        }        }
4588  #endif  #endif
# Line 4347  for (;; ptr++) Line 4594  for (;; ptr++)
4594      negating it if necessary. */      negating it if necessary. */
4595    
4596      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
4597      if (negate_class)      if (lengthptr == NULL)    /* Save time in the pre-compile phase */
       {  
       if (lengthptr == NULL)    /* Save time in the pre-compile phase */  
         for (c = 0; c < 32; c++) code[c] = ~classbits[c];  
       }  
     else  
4598        {        {
4599          if (negate_class)
4600            for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
4601        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
4602        }        }
4603      code += 32;      code += 32 / sizeof(pcre_uchar);
4604        NOT_CHAR:
4605      break;      break;
4606    
4607    
# Line 4393  for (;; ptr++) Line 4638  for (;; ptr++)
4638    
4639      if (repeat_min == 0)      if (repeat_min == 0)
4640        {        {
4641        firstbyte = zerofirstbyte;    /* Adjust for zero repeat */        firstchar = zerofirstchar;    /* Adjust for zero repeat */
4642        reqbyte = zeroreqbyte;        /* Ditto */        reqchar = zeroreqchar;        /* Ditto */
4643        }        }
4644    
4645      /* Remember whether this is a variable length repeat */      /* Remember whether this is a variable length repeat */
# Line 4433  for (;; ptr++) Line 4678  for (;; ptr++)
4678      past, but it no longer happens for non-repeated recursions. In fact, the      past, but it no longer happens for non-repeated recursions. In fact, the
4679      repeated ones could be re-implemented independently so as not to need this,      repeated ones could be re-implemented independently so as not to need this,
4680      but for the moment we rely on the code for repeating groups. */      but for the moment we rely on the code for repeating groups. */
4681    
4682      if (*previous == OP_RECURSE)      if (*previous == OP_RECURSE)
4683        {        {
4684        memmove(previous + 1 + LINK_SIZE, previous, 1 + LINK_SIZE);        memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
4685        *previous = OP_ONCE;        *previous = OP_ONCE;
4686        PUT(previous, 1, 2 + 2*LINK_SIZE);        PUT(previous, 1, 2 + 2*LINK_SIZE);
4687        previous[2 + 2*LINK_SIZE] = OP_KET;        previous[2 + 2*LINK_SIZE] = OP_KET;
# Line 4457  for (;; ptr++) Line 4702  for (;; ptr++)
4702    
4703      /* Now handle repetition for the different types of item. */      /* Now handle repetition for the different types of item. */
4704    
4705      /* If previous was a character match, abolish the item and generate a      /* If previous was a character or negated character match, abolish the item
4706      repeat item instead. If a char item has a minumum of more than one, ensure      and generate a repeat item instead. If a char item has a minimum of more
4707      that it is set in reqbyte - it might not be if a sequence such as x{3} is      than one, ensure that it is set in reqchar - it might not be if a sequence
4708      the first thing in a branch because the x will have gone into firstbyte      such as x{3} is the first thing in a branch because the x will have gone
4709      instead.  */      into firstchar instead.  */
4710    
4711      if (*previous == OP_CHAR || *previous == OP_CHARI)      if (*previous == OP_CHAR || *previous == OP_CHARI
4712            || *previous == OP_NOT || *previous == OP_NOTI)
4713        {        {
4714        op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;        switch (*previous)
4715            {
4716            default: /* Make compiler happy. */
4717            case OP_CHAR:  op_type = OP_STAR - OP_STAR; break;
4718            case OP_CHARI: op_type = OP_STARI - OP_STAR; break;
4719            case OP_NOT:   op_type = OP_NOTSTAR - OP_STAR; break;
4720            case OP_NOTI:  op_type = OP_NOTSTARI - OP_STAR; break;
4721            }
4722    
4723        /* Deal with UTF-8 characters that take up more than one byte. It's        /* Deal with UTF characters that take up more than one character. It's
4724        easier to write this out separately than try to macrify it. Use c to        easier to write this out separately than try to macrify it. Use c to
4725        hold the length of the character in bytes, plus 0x80 to flag that it's a        hold the length of the character in bytes, plus UTF_LENGTH to flag that
4726        length rather than a small character. */        it's a length rather than a small character. */
4727    
4728  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4729        if (utf8 && (code[-1] & 0x80) != 0)        if (utf && NOT_FIRSTCHAR(code[-1]))
4730          {          {
4731          uschar *lastchar = code - 1;          pcre_uchar *lastchar = code - 1;
4732          while((*lastchar & 0xc0) == 0x80) lastchar--;          BACKCHAR(lastchar);
4733          c = code - lastchar;            /* Length of UTF-8 character */          c = (int)(code - lastchar);     /* Length of UTF-8 character */
4734          memcpy(utf8_char, lastchar, c); /* Save the char */          memcpy(utf_chars, lastchar, IN_UCHARS(c)); /* Save the char */
4735          c |= 0x80;                      /* Flag c as a length */          c |= UTF_LENGTH;                /* Flag c as a length */
4736          }          }
4737        else        else
4738  #endif  #endif /* SUPPORT_UTF */
   
       /* Handle the case of a single byte - either with no UTF8 support, or  
       with UTF-8 disabled, or for a UTF-8 character < 128. */  
4739    
4740          /* Handle the case of a single charater - either with no UTF support, or
4741          with UTF disabled, or for a single character UTF character. */
4742          {          {
4743          c = code[-1];          c = code[-1];
4744          if (repeat_min > 1) reqbyte = c | req_caseopt | cd->req_varyopt;          if (*previous <= OP_CHARI && repeat_min > 1)
4745              reqchar = c | req_caseopt | cd->req_varyopt;
4746          }          }
4747    
4748        /* If the repetition is unlimited, it pays to see if the next thing on        /* If the repetition is unlimited, it pays to see if the next thing on
# Line 4499  for (;; ptr++) Line 4752  for (;; ptr++)
4752    
4753        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4754            repeat_max < 0 &&            repeat_max < 0 &&
4755            check_auto_possessive(previous, utf8, ptr + 1, options, cd))            check_auto_possessive(previous, utf, ptr + 1, options, cd))
4756          {          {
4757          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4758          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 4508  for (;; ptr++) Line 4761  for (;; ptr++)
4761        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
4762        }        }
4763    
     /* If previous was a single negated character ([^a] or similar), we use  
     one of the special opcodes, replacing it. The code is shared with single-  
     character repeats by setting opt_type to add a suitable offset into  
     repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI  
     are currently used only for single-byte chars. */  
   
     else if (*previous == OP_NOT || *previous == OP_NOTI)  
       {  
       op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;  
       c = previous[1];  
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf8, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
       goto OUTPUT_SINGLE_REPEAT;  
       }  
   
4764      /* If previous was a character type match (\d or similar), abolish it and      /* If previous was a character type match (\d or similar), abolish it and
4765      create a suitable repeat item. The code is shared with single-character      create a suitable repeat item. The code is shared with single-character
4766      repeats by setting op_type to add a suitable offset into repeat_type. Note      repeats by setting op_type to add a suitable offset into repeat_type. Note
# Line 4537  for (;; ptr++) Line 4770  for (;; ptr++)
4770    
4771      else if (*previous < OP_EODN)      else if (*previous < OP_EODN)
4772        {        {
4773        uschar *oldcode;        pcre_uchar *oldcode;
4774        int prop_type, prop_value;        int prop_type, prop_value;
4775        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
4776        c = *previous;        c = *previous;
4777    
4778        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4779            repeat_max < 0 &&            repeat_max < 0 &&
4780            check_auto_possessive(previous, utf8, ptr + 1, options, cd))            check_auto_possessive(previous, utf, ptr + 1, options, cd))
4781          {          {
4782          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4783          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 4624  for (;; ptr++) Line 4857  for (;; ptr++)
4857          we have to insert the character for the previous code. For a repeated          we have to insert the character for the previous code. For a repeated
4858          Unicode property match, there are two extra bytes that define the          Unicode property match, there are two extra bytes that define the
4859          required property. In UTF-8 mode, long characters have their length in          required property. In UTF-8 mode, long characters have their length in
4860          c, with the 0x80 bit as a flag. */          c, with the UTF_LENGTH bit as a flag. */
4861    
4862          if (repeat_max < 0)          if (repeat_max < 0)
4863            {            {
4864  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4865            if (utf8 && c >= 128)            if (utf && (c & UTF_LENGTH) != 0)
4866              {              {
4867              memcpy(code, utf8_char, c & 7);              memcpy(code, utf_chars, IN_UCHARS(c & 7));
4868              code += c & 7;              code += c & 7;
4869              }              }
4870            else            else
# Line 4653  for (;; ptr++) Line 4886  for (;; ptr++)
4886    
4887          else if (repeat_max != repeat_min)          else if (repeat_max != repeat_min)
4888            {            {
4889  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4890            if (utf8 && c >= 128)            if (utf && (c & UTF_LENGTH) != 0)
4891              {              {
4892              memcpy(code, utf8_char, c & 7);              memcpy(code, utf_chars, IN_UCHARS(c & 7));
4893              code += c & 7;              code += c & 7;
4894              }              }
4895            else            else
# Line 4683  for (;; ptr++) Line 4916  for (;; ptr++)
4916    
4917        /* The character or character type itself comes last in all cases. */        /* The character or character type itself comes last in all cases. */
4918    
4919  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4920        if (utf8 && c >= 128)        if (utf && (c & UTF_LENGTH) != 0)
4921          {          {
4922          memcpy(code, utf8_char, c & 7);          memcpy(code, utf_chars, IN_UCHARS(c & 7));
4923          code += c & 7;          code += c & 7;
4924          }          }
4925        else        else
# Line 4710  for (;; ptr++) Line 4943  for (;; ptr++)
4943    
4944      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS ||
4945               *previous == OP_NCLASS ||               *previous == OP_NCLASS ||
4946  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4947               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
4948  #endif  #endif
4949               *previous == OP_REF ||               *previous == OP_REF ||
# Line 4759  for (;; ptr++) Line 4992  for (;; ptr++)
4992        {        {
4993        register int i;        register int i;
4994        int len = (int)(code - previous);        int len = (int)(code - previous);
4995        uschar *bralink = NULL;        pcre_uchar *bralink = NULL;
4996        uschar *brazeroptr = NULL;        pcre_uchar *brazeroptr = NULL;
4997    
4998        /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so        /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
4999        we just ignore the repeat. */        we just ignore the repeat. */
# Line 4813  for (;; ptr++) Line 5046  for (;; ptr++)
5046          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */
5047            {            {
5048            *code = OP_END;            *code = OP_END;
5049            adjust_recurse(previous, 1, utf8, cd, save_hwm);            adjust_recurse(previous, 1, utf, cd, save_hwm);
5050            memmove(previous+1, previous, len);            memmove(previous + 1, previous, IN_UCHARS(len));
5051            code++;            code++;
5052            if (repeat_max == 0)            if (repeat_max == 0)
5053              {              {
# Line 4837  for (;; ptr++) Line 5070  for (;; ptr++)
5070            {            {
5071            int offset;            int offset;
5072            *code = OP_END;            *code = OP_END;
5073            adjust_recurse(previous, 2 + LINK_SIZE, utf8, cd, save_hwm);            adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, save_hwm);
5074            memmove(previous + 2 + LINK_SIZE, previous, len);            memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));
5075            code += 2 + LINK_SIZE;            code += 2 + LINK_SIZE;
5076            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
5077            *previous++ = OP_BRA;            *previous++ = OP_BRA;
# Line 4884  for (;; ptr++) Line 5117  for (;; ptr++)
5117              *lengthptr += delta;              *lengthptr += delta;
5118              }              }
5119    
5120            /* This is compiling for real */            /* This is compiling for real. If there is a set first byte for
5121              the group, and we have not yet set a "required byte", set it. Make
5122              sure there is enough workspace for copying forward references before
5123              doing the copy. */
5124    
5125            else            else
5126              {              {
5127              if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte;              if (groupsetfirstchar && reqchar < 0) reqchar = firstchar;
5128    
5129              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
5130                {                {
5131                uschar *hc;                pcre_uchar *hc;
5132                uschar *this_hwm = cd->hwm;                pcre_uchar *this_hwm = cd->hwm;
5133                memcpy(code, previous, len);                memcpy(code, previous, IN_UCHARS(len));
5134    
5135                  while (cd->hwm > cd->start_workspace + cd->workspace_size -
5136                         WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))
5137                    {
5138                    int save_offset = save_hwm - cd->start_workspace;
5139                    int this_offset = this_hwm - cd->start_workspace;
5140                    *errorcodeptr = expand_workspace(cd);
5141                    if (*errorcodeptr != 0) goto FAILED;
5142                    save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;
5143                    this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;
5144                    }
5145    
5146                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
5147                  {                  {
                 if (cd->hwm >= cd->start_workspace + WORK_SIZE_CHECK)  
                   {  
                   *errorcodeptr = ERR72;  
                   goto FAILED;  
                   }  
5148                  PUT(cd->hwm, 0, GET(hc, 0) + len);                  PUT(cd->hwm, 0, GET(hc, 0) + len);
5149                  cd->hwm += LINK_SIZE;                  cd->hwm += LINK_SIZE;
5150                  }                  }
# Line 4928  for (;; ptr++) Line 5172  for (;; ptr++)
5172          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
5173          paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is          paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
5174          a 64-bit integer type when available, otherwise double. */          a 64-bit integer type when available, otherwise double. */
5175    
5176          if (lengthptr != NULL && repeat_max > 0)          if (lengthptr != NULL && repeat_max > 0)
5177            {            {
5178            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
# Line 4948  for (;; ptr++) Line 5192  for (;; ptr++)
5192    
5193          else for (i = repeat_max - 1; i >= 0; i--)          else for (i = repeat_max - 1; i >= 0; i--)
5194            {            {
5195            uschar *hc;            pcre_uchar *hc;
5196            uschar *this_hwm = cd->hwm;            pcre_uchar *this_hwm = cd->hwm;
5197    
5198            *code++ = OP_BRAZERO + repeat_type;            *code++ = OP_BRAZERO + repeat_type;
5199    
# Line 4965  for (;; ptr++) Line 5209  for (;; ptr++)
5209              PUTINC(code, 0, offset);              PUTINC(code, 0, offset);
5210              }              }
5211    
5212            memcpy(code, previous, len);            memcpy(code, previous, IN_UCHARS(len));
5213    
5214              /* Ensure there is enough workspace for forward references before
5215              copying them. */
5216    
5217              while (cd->hwm > cd->start_workspace + cd->workspace_size -
5218                     WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))
5219                {
5220                int save_offset = save_hwm - cd->start_workspace;
5221                int this_offset = this_hwm - cd->start_workspace;
5222                *errorcodeptr = expand_workspace(cd);
5223                if (*errorcodeptr != 0) goto FAILED;
5224                save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;
5225                this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;
5226                }
5227    
5228            for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)            for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
5229              {              {
             if (cd->hwm >= cd->start_workspace + WORK_SIZE_CHECK)  
               {  
               *errorcodeptr = ERR72;  
               goto FAILED;  
               }  
5230              PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));              PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));
5231              cd->hwm += LINK_SIZE;              cd->hwm += LINK_SIZE;
5232              }              }
# Line 4987  for (;; ptr++) Line 5241  for (;; ptr++)
5241            {            {
5242            int oldlinkoffset;            int oldlinkoffset;
5243            int offset = (int)(code - bralink + 1);            int offset = (int)(code - bralink + 1);
5244            uschar *bra = code - offset;            pcre_uchar *bra = code - offset;
5245            oldlinkoffset = GET(bra, 1);            oldlinkoffset = GET(bra, 1);
5246            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
5247            *code++ = OP_KET;            *code++ = OP_KET;
# Line 5001  for (;; ptr++) Line 5255  for (;; ptr++)
5255        ONCE brackets can be converted into non-capturing brackets, as the        ONCE brackets can be converted into non-capturing brackets, as the
5256        behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to        behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
5257        deal with possessive ONCEs specially.        deal with possessive ONCEs specially.
5258    
5259        Otherwise, when we are doing the actual compile phase, check to see        Otherwise, when we are doing the actual compile phase, check to see
5260        whether this group is one that could match an empty string. If so,        whether this group is one that could match an empty string. If so,
5261        convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so        convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
5262        that runtime checking can be done. [This check is also applied to ONCE        that runtime checking can be done. [This check is also applied to ONCE
5263        groups at runtime, but in a different way.]        groups at runtime, but in a different way.]
5264    
5265        Then, if the quantifier was possessive and the bracket is not a        Then, if the quantifier was possessive and the bracket is not a
5266        conditional, we convert the BRA code to the POS form, and the KET code to        conditional, we convert the BRA code to the POS form, and the KET code to
5267        KETRPOS. (It turns out to be convenient at runtime to detect this kind of        KETRPOS. (It turns out to be convenient at runtime to detect this kind of
5268        subpattern at both the start and at the end.) The use of special opcodes        subpattern at both the start and at the end.) The use of special opcodes
5269        makes it possible to reduce greatly the stack usage in pcre_exec(). If        makes it possible to reduce greatly the stack usage in pcre_exec(). If
5270        the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.        the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
5271    
5272        Then, if the minimum number of matches is 1 or 0, cancel the possessive        Then, if the minimum number of matches is 1 or 0, cancel the possessive
5273        flag so that the default action below, of wrapping everything inside        flag so that the default action below, of wrapping everything inside
5274        atomic brackets, does not happen. When the minimum is greater than 1,        atomic brackets, does not happen. When the minimum is greater than 1,
5275        there will be earlier copies of the group, and so we still have to wrap        there will be earlier copies of the group, and so we still have to wrap
5276        the whole thing. */        the whole thing. */
5277    
5278        else        else
5279          {          {
5280          uschar *ketcode = code - 1 - LINK_SIZE;          pcre_uchar *ketcode = code - 1 - LINK_SIZE;
5281          uschar *bracode = ketcode - GET(ketcode, 1);          pcre_uchar *bracode = ketcode - GET(ketcode, 1);
5282    
5283          /* Convert possessive ONCE brackets to non-capturing */          /* Convert possessive ONCE brackets to non-capturing */
5284    
5285          if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&          if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&
5286              possessive_quantifier) *bracode = OP_BRA;              possessive_quantifier) *bracode = OP_BRA;
5287    
5288          /* For non-possessive ONCE brackets, all we need to do is to          /* For non-possessive ONCE brackets, all we need to do is to
5289          set the KET. */          set the KET. */
5290    
5291          if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)          if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)
5292            *ketcode = OP_KETRMAX + repeat_type;            *ketcode = OP_KETRMAX + repeat_type;
5293    
5294          /* Handle non-ONCE brackets and possessive ONCEs (which have been          /* Handle non-ONCE brackets and possessive ONCEs (which have been
5295          converted to non-capturing above). */          converted to non-capturing above). */
5296    
5297          else          else
5298            {            {
5299            /* In the compile phase, check for empty string matching. */            /* In the compile phase, check for empty string matching. */
5300    
5301            if (lengthptr == NULL)            if (lengthptr == NULL)
5302              {              {
5303              uschar *scode = bracode;              pcre_uchar *scode = bracode;
5304              do              do
5305                {                {
5306                if (could_be_empty_branch(scode, ketcode, utf8, cd))                if (could_be_empty_branch(scode, ketcode, utf, cd))
5307                  {                  {
5308                  *bracode += OP_SBRA - OP_BRA;                  *bracode += OP_SBRA - OP_BRA;
5309                  break;                  break;
# Line 5058  for (;; ptr++) Line 5312  for (;; ptr++)
5312                }                }
5313              while (*scode == OP_ALT);              while (*scode == OP_ALT);
5314              }              }
5315    
5316            /* Handle possessive quantifiers. */            /* Handle possessive quantifiers. */
5317    
5318            if (possessive_quantifier)            if (possessive_quantifier)
# Line 5067  for (;; ptr++) Line 5321  for (;; ptr++)
5321              repeated non-capturing bracket, because we have not invented POS              repeated non-capturing bracket, because we have not invented POS
5322              versions of the COND opcodes. Because we are moving code along, we              versions of the COND opcodes. Because we are moving code along, we
5323              must ensure that any pending recursive references are updated. */              must ensure that any pending recursive references are updated. */
5324    
5325              if (*bracode == OP_COND || *bracode == OP_SCOND)              if (*bracode == OP_COND || *bracode == OP_SCOND)
5326                {                {
5327                int nlen = (int)(code - bracode);                int nlen = (int)(code - bracode);
5328                *code = OP_END;                *code = OP_END;
5329                adjust_recurse(bracode, 1 + LINK_SIZE, utf8, cd, save_hwm);                adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, save_hwm);
5330                memmove(bracode + 1+LINK_SIZE, bracode, nlen);                memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));
5331                code += 1 + LINK_SIZE;                code += 1 + LINK_SIZE;
5332                nlen += 1 + LINK_SIZE;                nlen += 1 + LINK_SIZE;
5333                *bracode = OP_BRAPOS;                *bracode = OP_BRAPOS;
5334                *code++ = OP_KETRPOS;                *code++ = OP_KETRPOS;
5335                PUTINC(code, 0, nlen);                PUTINC(code, 0, nlen);
5336                PUT(bracode, 1, nlen);                PUT(bracode, 1, nlen);
5337                }                }
5338    
5339              /* For non-COND brackets, we modify the BRA code and use KETRPOS. */              /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
5340    
5341              else              else
5342                {                {
5343                *bracode += 1;              /* Switch to xxxPOS opcodes */                *bracode += 1;              /* Switch to xxxPOS opcodes */
5344                *ketcode = OP_KETRPOS;                *ketcode = OP_KETRPOS;
5345                }                }
5346    
5347              /* If the minimum is zero, mark it as possessive, then unset the              /* If the minimum is zero, mark it as possessive, then unset the
5348              possessive flag when the minimum is 0 or 1. */              possessive flag when the minimum is 0 or 1. */
5349    
5350              if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;              if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
5351              if (repeat_min < 2) possessive_quantifier = FALSE;              if (repeat_min < 2) possessive_quantifier = FALSE;
5352              }              }
5353    
5354            /* Non-possessive quantifier */            /* Non-possessive quantifier */
5355    
5356            else *ketcode = OP_KETRMAX + repeat_type;            else *ketcode = OP_KETRMAX + repeat_type;
5357            }            }
5358          }          }
# Line 5142  for (;; ptr++) Line 5396  for (;; ptr++)
5396        int len;        int len;
5397    
5398        if (*tempcode == OP_TYPEEXACT)        if (*tempcode == OP_TYPEEXACT)
5399          tempcode += _pcre_OP_lengths[*tempcode] +          tempcode += PRIV(OP_lengths)[*tempcode] +
5400            ((tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP)? 2 : 0);            ((tempcode[1 + IMM2_SIZE] == OP_PROP
5401              || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
5402    
5403        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
5404          {          {
5405          tempcode += _pcre_OP_lengths[*tempcode];          tempcode += PRIV(OP_lengths)[*tempcode];
5406  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
5407          if (utf8 && tempcode[-1] >= 0xc0)          if (utf && HAS_EXTRALEN(tempcode[-1]))
5408            tempcode += _pcre_utf8_table4[tempcode[-1] & 0x3f];            tempcode += GET_EXTRALEN(tempcode[-1]);
5409  #endif  #endif
5410          }          }
5411    
# Line 5187  for (;; ptr++) Line 5442  for (;; ptr++)
5442    
5443          default:          default:
5444          *code = OP_END;          *code = OP_END;
5445          adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);          adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm);
5446          memmove(tempcode + 1+LINK_SIZE, tempcode, len);          memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
5447          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
5448          len += 1 + LINK_SIZE;          len += 1 + LINK_SIZE;
5449          tempcode[0] = OP_ONCE;          tempcode[0] = OP_ONCE;
# Line 5200  for (;; ptr++) Line 5455  for (;; ptr++)
5455        }        }
5456    
5457      /* In all case we no longer have a previous item. We also set the      /* In all case we no longer have a previous item. We also set the
5458      "follows varying string" flag for subsequently encountered reqbytes if      "follows varying string" flag for subsequently encountered reqchars if
5459      it isn't already set and we have just passed a varying length item. */      it isn't already set and we have just passed a varying length item. */
5460    
5461      END_REPEAT:      END_REPEAT:
# Line 5223  for (;; ptr++) Line 5478  for (;; ptr++)
5478    
5479      /* First deal with various "verbs" that can be introduced by '*'. */      /* First deal with various "verbs" that can be introduced by '*'. */
5480    
5481      if (*(++ptr) == CHAR_ASTERISK &&      ptr++;
5482           ((cd->ctypes[ptr[1]] & ctype_letter) != 0 || ptr[1] == ':'))      if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':'
5483             || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0))))
5484        {        {
5485        int i, namelen;        int i, namelen;
5486        int arglen = 0;        int arglen = 0;
5487        const char *vn = verbnames;        const char *vn = verbnames;
5488        const uschar *name = ptr + 1;        const pcre_uchar *name = ptr + 1;
5489        const uschar *arg = NULL;        const pcre_uchar *arg = NULL;
5490        previous = NULL;        previous = NULL;
5491        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};        ptr++;
5492          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
5493        namelen = (int)(ptr - name);        namelen = (int)(ptr - name);
5494    
5495        /* It appears that Perl allows any characters whatsoever, other than        /* It appears that Perl allows any characters whatsoever, other than
# Line 5244  for (;; ptr++) Line 5501  for (;; ptr++)
5501          arg = ++ptr;          arg = ++ptr;
5502          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5503          arglen = (int)(ptr - arg);          arglen = (int)(ptr - arg);
5504            if (arglen > (int)MAX_MARK)
5505              {
5506              *errorcodeptr = ERR75;
5507              goto FAILED;
5508              }
5509          }          }
5510    
5511        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
# Line 5257  for (;; ptr++) Line 5519  for (;; ptr++)
5519        for (i = 0; i < verbcount; i++)        for (i = 0; i < verbcount; i++)
5520          {          {
5521          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
5522              strncmp((char *)name, vn, namelen) == 0)              STRNCMP_UC_C8(name, vn, namelen) == 0)
5523            {            {
5524              int setverb;
5525    
5526            /* Check for open captures before ACCEPT and convert it to            /* Check for open captures before ACCEPT and convert it to
5527            ASSERT_ACCEPT if in an assertion. */            ASSERT_ACCEPT if in an assertion. */
5528    
# Line 5276  for (;; ptr++) Line 5540  for (;; ptr++)
5540                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5541                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5542                }                }
5543              *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;              setverb = *code++ =
5544                  (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5545    
5546              /* Do not set firstbyte after *ACCEPT */              /* Do not set firstchar after *ACCEPT */
5547              if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;              if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
5548              }              }
5549    
5550            /* Handle other cases with/without an argument */            /* Handle other cases with/without an argument */
# Line 5291  for (;; ptr++) Line 5556  for (;; ptr++)
5556                *errorcodeptr = ERR66;                *errorcodeptr = ERR66;
5557                goto FAILED;                goto FAILED;
5558                }                }
5559              *code = verbs[i].op;              setverb = *code++ = verbs[i].op;
             if (*code++ == OP_THEN) cd->external_flags |= PCRE_HASTHEN;  
5560              }              }
5561    
5562            else            else
# Line 5302  for (;; ptr++) Line 5566  for (;; ptr++)
5566                *errorcodeptr = ERR59;                *errorcodeptr = ERR59;
5567                goto FAILED;                goto FAILED;
5568                }                }
5569              *code = verbs[i].op_arg;              setverb = *code++ = verbs[i].op_arg;
             if (*code++ == OP_THEN_ARG) cd->external_flags |= PCRE_HASTHEN;  
5570              *code++ = arglen;              *code++ = arglen;
5571              memcpy(code, arg, arglen);              memcpy(code, arg, IN_UCHARS(arglen));
5572              code += arglen;              code += arglen;
5573              *code++ = 0;              *code++ = 0;
5574              }              }
5575    
5576              switch (setverb)
5577                {
5578                case OP_THEN:
5579                case OP_THEN_ARG:
5580                cd->external_flags |= PCRE_HASTHEN;
5581                break;
5582    
5583                case OP_PRUNE:
5584                case OP_PRUNE_ARG:
5585                case OP_SKIP:
5586                case OP_SKIP_ARG:
5587                cd->had_pruneorskip = TRUE;
5588                break;
5589                }
5590    
5591            break;  /* Found verb, exit loop */            break;  /* Found verb, exit loop */
5592            }            }
5593    
# Line 5328  for (;; ptr++) Line 5606  for (;; ptr++)
5606        {        {
5607        int i, set, unset, namelen;        int i, set, unset, namelen;
5608        int *optset;        int *optset;
5609        const uschar *name;        const pcre_uchar *name;
5610        uschar *slot;        pcre_uchar *slot;
5611    
5612        switch (*(++ptr))        switch (*(++ptr))
5613          {          {
# Line 5382  for (;; ptr++) Line 5660  for (;; ptr++)
5660            break;            break;
5661    
5662          /* Most other conditions use OP_CREF (a couple change to OP_RREF          /* Most other conditions use OP_CREF (a couple change to OP_RREF
5663          below), and all need to skip 3 bytes at the start of the group. */          below), and all need to skip 1+IMM2_SIZE bytes at the start of the group. */
5664    
5665          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
5666          skipbytes = 3;          skipbytes = 1+IMM2_SIZE;
5667          refsign = -1;          refsign = -1;
5668    
5669          /* Check for a test for recursion in a named group. */          /* Check for a test for recursion in a named group. */
# Line 5418  for (;; ptr++) Line 5696  for (;; ptr++)
5696    
5697          /* We now expect to read a name; any thing else is an error */          /* We now expect to read a name; any thing else is an error */
5698    
5699          if ((cd->ctypes[ptr[1]] & ctype_word) == 0)          if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)
5700            {            {
5701            ptr += 1;  /* To get the right offset */            ptr += 1;  /* To get the right offset */
5702            *errorcodeptr = ERR28;            *errorcodeptr = ERR28;
# Line 5429  for (;; ptr++) Line 5707  for (;; ptr++)
5707    
5708          recno = 0;          recno = 0;
5709          name = ++ptr;          name = ++ptr;
5710          while ((cd->ctypes[*ptr] & ctype_word) != 0)          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
5711            {            {
5712            if (recno >= 0)            if (recno >= 0)
5713              recno = ((digitab[*ptr] & ctype_digit) != 0)?              recno = (IS_DIGIT(*ptr))? recno * 10 + *ptr - CHAR_0 : -1;
               recno * 10 + *ptr - CHAR_0 : -1;  
5714            ptr++;            ptr++;
5715            }            }
5716          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
# Line 5481  for (;; ptr++) Line 5758  for (;; ptr++)
5758          slot = cd->name_table;          slot = cd->name_table;
5759          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
5760            {            {
5761            if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break;            if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0) break;
5762            slot += cd->name_entry_size;            slot += cd->name_entry_size;
5763            }            }
5764    
# Line 5497  for (;; ptr++) Line 5774  for (;; ptr++)
5774          /* Search the pattern for a forward reference */          /* Search the pattern for a forward reference */
5775    
5776          else if ((i = find_parens(cd, name, namelen,          else if ((i = find_parens(cd, name, namelen,
5777                          (options & PCRE_EXTENDED) != 0, utf8)) > 0)                          (options & PCRE_EXTENDED) != 0, utf)) > 0)
5778            {            {
5779            PUT2(code, 2+LINK_SIZE, i);            PUT2(code, 2+LINK_SIZE, i);
5780            code[1+LINK_SIZE]++;            code[1+LINK_SIZE]++;
# Line 5523  for (;; ptr++) Line 5800  for (;; ptr++)
5800            recno = 0;            recno = 0;
5801            for (i = 1; i < namelen; i++)            for (i = 1; i < namelen; i++)
5802              {              {
5803              if ((digitab[name[i]] & ctype_digit) == 0)              if (!IS_DIGIT(name[i]))
5804                {                {
5805                *errorcodeptr = ERR15;                *errorcodeptr = ERR15;
5806                goto FAILED;                goto FAILED;
# Line 5538