/[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 903 by ph10, Sat Jan 21 16:37:17 2012 UTC revision 1359 by ph10, Tue Sep 3 10:10:59 2013 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2012 University of Cambridge             Copyright (c) 1997-2013 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 53  supporting internal functions that are n Line 53  supporting internal functions that are n
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    
56  /* When PCRE_DEBUG is defined, we need the pcre(16)_printint() function, which  /* When PCRE_DEBUG is defined, we need the pcre(16|32)_printint() function, which
57  is 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. We do not need to select pcre16_printint.c specially, because the  library. We do not need to select pcre16_printint.c specially, because the
59  COMPILE_PCREx macro will already be appropriately set. */  COMPILE_PCREx macro will already be appropriately set. */
# Line 68  COMPILE_PCREx macro will already be appr Line 68  COMPILE_PCREx macro will already be appr
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 77  to check them every time. */ Line 77  to check them every time. */
77    
78  #define OFLOW_MAX (INT_MAX - 20)  #define OFLOW_MAX (INT_MAX - 20)
79    
80    /* Definitions to allow mutual recursion */
81    
82    static int
83      add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
84        const pcre_uint32 *, unsigned int);
85    
86    static BOOL
87      compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
88        pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,
89        compile_data *, int *);
90    
91    
92    
93  /*************************************************  /*************************************************
94  *      Code parameters and static tables         *  *      Code parameters and static tables         *
# Line 103  kicks in at the same number of forward r Line 115  kicks in at the same number of forward r
115  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
117    
118    /* This value determines the size of the initial vector that is used for
119    remembering named groups during the pre-compile. It is allocated on the stack,
120    but if it is too small, it is expanded using malloc(), in a similar way to the
121    workspace. The value is the number of slots in the list. */
122    
123    #define NAMED_GROUP_LIST_SIZE  20
124    
125  /* The overrun tests check for a slightly smaller size so that they detect the  /* The overrun tests check for a slightly smaller size so that they detect the
126  overrun before it actually does run off the end of the data block. */  overrun before it actually does run off the end of the data block. */
127    
# Line 110  overrun before it actually does run off Line 129  overrun before it actually does run off
129    
130  /* Private flags added to firstchar and reqchar. */  /* Private flags added to firstchar and reqchar. */
131    
132  #define REQ_CASELESS   0x10000000l      /* Indicates caselessness */  #define REQ_CASELESS    (1 << 0)        /* Indicates caselessness */
133  #define REQ_VARY       0x20000000l      /* Reqchar followed non-literal item */  #define REQ_VARY        (1 << 1)        /* Reqchar followed non-literal item */
134    /* Negative values for the firstchar and reqchar flags */
135    #define REQ_UNSET       (-2)
136    #define REQ_NONE        (-1)
137    
138  /* Repeated character flags. */  /* Repeated character flags. */
139    
# Line 472  static const char error_texts[] = Line 494  static const char error_texts[] =
494    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
495    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
496    /* 60 */    /* 60 */
497    "(*VERB) not recognized\0"    "(*VERB) not recognized or malformed\0"
498    "number is too big\0"    "number is too big\0"
499    "subpattern name expected\0"    "subpattern name expected\0"
500    "digit expected after (?+\0"    "digit expected after (?+\0"
# Line 489  static const char error_texts[] = Line 511  static const char error_texts[] =
511    "too many forward references\0"    "too many forward references\0"
512    "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"    "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
513    "invalid UTF-16 string\0"    "invalid UTF-16 string\0"
514      /* 75 */
515      "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
516      "character value in \\u.... sequence is too large\0"
517      "invalid UTF-32 string\0"
518      "setting UTF is disabled by the application\0"
519    ;    ;
520    
521  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 628  static const pcre_uint8 ebcdic_chartab[] Line 655  static const pcre_uint8 ebcdic_chartab[]
655  #endif  #endif
656    
657    
 /* Definition to allow mutual recursion */  
   
 static BOOL  
   compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,  
     int *, int *, branch_chain *, compile_data *, int *);  
   
   
658    
659  /*************************************************  /*************************************************
660  *            Find an error text                  *  *            Find an error text                  *
# Line 655  find_error_text(int n) Line 675  find_error_text(int n)
675  const char *s = error_texts;  const char *s = error_texts;
676  for (; n > 0; n--)  for (; n > 0; n--)
677    {    {
678    while (*s++ != 0) {};    while (*s++ != CHAR_NULL) {};
679    if (*s == 0) return "Error text not found (please report)";    if (*s == CHAR_NULL) return "Error text not found (please report)";
680    }    }
681  return s;  return s;
682  }  }
# Line 739  return (*p == CHAR_RIGHT_CURLY_BRACKET); Line 759  return (*p == CHAR_RIGHT_CURLY_BRACKET);
759  *************************************************/  *************************************************/
760    
761  /* This function is called when a \ has been encountered. It either returns a  /* This function is called when a \ has been encountered. It either returns a
762  positive value for a simple escape such as \n, or a negative value which  positive value for a simple escape such as \n, or 0 for a data character
763  encodes one of the more complicated things such as \d. A backreference to group  which will be placed in chptr. A backreference to group n is returned as
764  n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When  negative n. When UTF-8 is enabled, a positive value greater than 255 may
765  UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,  be returned in chptr.
766  ptr is pointing at the \. On exit, it is on the final character of the escape  On entry,ptr is pointing at the \. On exit, it is on the final character of the
767  sequence.  escape sequence.
768    
769  Arguments:  Arguments:
770    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
771      chptr          points to the data character
772    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
773    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
774    options        the options bits    options        the options bits
775    isclass        TRUE if inside a character class    isclass        TRUE if inside a character class
776    
777  Returns:         zero or positive => a data character  Returns:         zero => a data character
778                   negative => a special escape sequence                   positive => a special escape sequence
779                     negative => a back reference
780                   on error, errorcodeptr is set                   on error, errorcodeptr is set
781  */  */
782    
783  static int  static int
784  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
785    int options, BOOL isclass)    int bracount, int options, BOOL isclass)
786  {  {
787  /* PCRE_UTF16 has the same value as PCRE_UTF8. */  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
788  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
789  const pcre_uchar *ptr = *ptrptr + 1;  const pcre_uchar *ptr = *ptrptr + 1;
790  pcre_int32 c;  pcre_uint32 c;
791    int escape = 0;
792  int i;  int i;
793    
794  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
# Line 773  ptr--;                            /* Set Line 796  ptr--;                            /* Set
796    
797  /* If backslash is at the end of the pattern, it's an error. */  /* If backslash is at the end of the pattern, it's an error. */
798    
799  if (c == 0) *errorcodeptr = ERR1;  if (c == CHAR_NULL) *errorcodeptr = ERR1;
800    
801  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
802  in a table. A non-zero result is something that can be returned immediately.  in a table. A non-zero result is something that can be returned immediately.
# Line 782  Otherwise further processing may be requ Line 805  Otherwise further processing may be requ
805  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
806  /* Not alphanumeric */  /* Not alphanumeric */
807  else if (c < CHAR_0 || c > CHAR_z) {}  else if (c < CHAR_0 || c > CHAR_z) {}
808  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if ((i = escapes[c - CHAR_0]) != 0)
809      { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
810    
811  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
812  /* Not alphanumeric */  /* Not alphanumeric */
813  else if (c < 'a' || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}  else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
814  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
815  #endif  #endif
816    
817  /* Escapes that need further processing, or are illegal. */  /* Escapes that need further processing, or are illegal. */
# Line 795  else if ((i = escapes[c - 0x48]) != 0) Line 819  else if ((i = escapes[c - 0x48]) != 0)
819  else  else
820    {    {
821    const pcre_uchar *oldptr;    const pcre_uchar *oldptr;
822    BOOL braced, negated;    BOOL braced, negated, overflow;
823      int s;
824    
825    switch (c)    switch (c)
826      {      {
# Line 820  else Line 845  else
845          c = 0;          c = 0;
846          for (i = 0; i < 4; ++i)          for (i = 0; i < 4; ++i)
847            {            {
848            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
849  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
850            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
851            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
# Line 829  else Line 854  else
854            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
855  #endif  #endif
856            }            }
857    
858    #if defined COMPILE_PCRE8
859            if (c > (utf ? 0x10ffffU : 0xffU))
860    #elif defined COMPILE_PCRE16
861            if (c > (utf ? 0x10ffffU : 0xffffU))
862    #elif defined COMPILE_PCRE32
863            if (utf && c > 0x10ffffU)
864    #endif
865              {
866              *errorcodeptr = ERR76;
867              }
868            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
869          }          }
870        }        }
871      else      else
# Line 855  else Line 892  else
892      (3) For Oniguruma compatibility we also support \g followed by a name or a      (3) For Oniguruma compatibility we also support \g followed by a name or a
893      number either in angle brackets or in single quotes. However, these are      number either in angle brackets or in single quotes. However, these are
894      (possibly recursive) subroutine calls, _not_ backreferences. Just return      (possibly recursive) subroutine calls, _not_ backreferences. Just return
895      the -ESC_g code (cf \k). */      the ESC_g code (cf \k). */
896    
897      case CHAR_g:      case CHAR_g:
898      if (isclass) break;      if (isclass) break;
899      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
900        {        {
901        c = -ESC_g;        escape = ESC_g;
902        break;        break;
903        }        }
904    
# Line 870  else Line 907  else
907      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
908        {        {
909        const pcre_uchar *p;        const pcre_uchar *p;
910        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
911          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
912        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
913          {          {
914          c = -ESC_k;          escape = ESC_k;
915          break;          break;
916          }          }
917        braced = TRUE;        braced = TRUE;
# Line 890  else Line 927  else
927      else negated = FALSE;      else negated = FALSE;
928    
929      /* The integer range is limited by the machine's int representation. */      /* The integer range is limited by the machine's int representation. */
930      c = 0;      s = 0;
931        overflow = FALSE;
932      while (IS_DIGIT(ptr[1]))      while (IS_DIGIT(ptr[1]))
933        {        {
934        if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */        if (s > INT_MAX / 10 - 1) /* Integer overflow */
935          {          {
936          c = -1;          overflow = TRUE;
937          break;          break;
938          }          }
939        c = c * 10 + *(++ptr) - CHAR_0;        s = s * 10 + (int)(*(++ptr) - CHAR_0);
940        }        }
941      if (((unsigned int)c) > INT_MAX) /* Integer overflow */      if (overflow) /* Integer overflow */
942        {        {
943        while (IS_DIGIT(ptr[1]))        while (IS_DIGIT(ptr[1]))
944          ptr++;          ptr++;
# Line 914  else Line 952  else
952        break;        break;
953        }        }
954    
955      if (c == 0)      if (s == 0)
956        {        {
957        *errorcodeptr = ERR58;        *errorcodeptr = ERR58;
958        break;        break;
# Line 922  else Line 960  else
960    
961      if (negated)      if (negated)
962        {        {
963        if (c > bracount)        if (s > bracount)
964          {          {
965          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
966          break;          break;
967          }          }
968        c = bracount - (c - 1);        s = bracount - (s - 1);
969        }        }
970    
971      c = -(ESC_REF + c);      escape = -s;
972      break;      break;
973    
974      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
# Line 952  else Line 990  else
990        {        {
991        oldptr = ptr;        oldptr = ptr;
992        /* The integer range is limited by the machine's int representation. */        /* The integer range is limited by the machine's int representation. */
993        c -= CHAR_0;        s = (int)(c -CHAR_0);
994          overflow = FALSE;
995        while (IS_DIGIT(ptr[1]))        while (IS_DIGIT(ptr[1]))
996          {          {
997          if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */          if (s > INT_MAX / 10 - 1) /* Integer overflow */
998            {            {
999            c = -1;            overflow = TRUE;
1000            break;            break;
1001            }            }
1002          c = c * 10 + *(++ptr) - CHAR_0;          s = s * 10 + (int)(*(++ptr) - CHAR_0);
1003          }          }
1004        if (((unsigned int)c) > INT_MAX) /* Integer overflow */        if (overflow) /* Integer overflow */
1005          {          {
1006          while (IS_DIGIT(ptr[1]))          while (IS_DIGIT(ptr[1]))
1007            ptr++;            ptr++;
1008          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1009          break;          break;
1010          }          }
1011        if (c < 10 || c <= bracount)        if (s < 10 || s <= bracount)
1012          {          {
1013          c = -(ESC_REF + c);          escape = -s;
1014          break;          break;
1015          }          }
1016        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
# Line 1018  else Line 1057  else
1057          c = 0;          c = 0;
1058          for (i = 0; i < 2; ++i)          for (i = 0; i < 2; ++i)
1059            {            {
1060            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
1061  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1062            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1063            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
# Line 1036  else Line 1075  else
1075        const pcre_uchar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
1076    
1077        c = 0;        c = 0;
1078          overflow = FALSE;
1079        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
1080          {          {
1081          register int cc = *pt++;          register pcre_uint32 cc = *pt++;
1082          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1083    
1084    #ifdef COMPILE_PCRE32
1085            if (c >= 0x10000000l) { overflow = TRUE; break; }
1086    #endif
1087    
1088  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1089          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1090          c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));          c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
# Line 1049  else Line 1093  else
1093          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1094  #endif  #endif
1095    
1096  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
1097          if (c > (utf ? 0x10ffff : 0xff)) { c = -1; break; }          if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1098  #else  #elif defined COMPILE_PCRE16
1099  #ifdef COMPILE_PCRE16          if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1100          if (c > (utf ? 0x10ffff : 0xffff)) { c = -1; break; }  #elif defined COMPILE_PCRE32
1101  #endif          if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1102  #endif  #endif
1103          }          }
1104    
1105        if (c < 0)        if (overflow)
1106          {          {
1107          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1108          *errorcodeptr = ERR34;          *errorcodeptr = ERR34;
# Line 1080  else Line 1124  else
1124      c = 0;      c = 0;
1125      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1126        {        {
1127        int cc;                                  /* Some compilers don't like */        pcre_uint32 cc;                          /* Some compilers don't like */
1128        cc = *(++ptr);                           /* ++ in initializers */        cc = *(++ptr);                           /* ++ in initializers */
1129  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1130        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
# Line 1099  else Line 1143  else
1143    
1144      case CHAR_c:      case CHAR_c:
1145      c = *(++ptr);      c = *(++ptr);
1146      if (c == 0)      if (c == CHAR_NULL)
1147        {        {
1148        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1149        break;        break;
# Line 1139  else Line 1183  else
1183  newline". PCRE does not support \N{name}. However, it does support  newline". PCRE does not support \N{name}. However, it does support
1184  quantification such as \N{2,3}. */  quantification such as \N{2,3}. */
1185    
1186  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&  if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1187       !is_counted_repeat(ptr+2))       !is_counted_repeat(ptr+2))
1188    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
1189    
1190  /* If PCRE_UCP is set, we change the values for \d etc. */  /* If PCRE_UCP is set, we change the values for \d etc. */
1191    
1192  if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)  if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
1193    c -= (ESC_DU - ESC_D);    escape += (ESC_DU - ESC_D);
1194    
1195  /* Set the pointer to the final character before returning. */  /* Set the pointer to the final character before returning. */
1196    
1197  *ptrptr = ptr;  *ptrptr = ptr;
1198  return c;  *chptr = c;
1199    return escape;
1200  }  }
1201    
   
   
1202  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
1203  /*************************************************  /*************************************************
1204  *               Handle \P and \p                 *  *               Handle \P and \p                 *
# Line 1169  escape sequence. Line 1212  escape sequence.
1212  Argument:  Argument:
1213    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
1214    negptr         points to a boolean that is set TRUE for negation else FALSE    negptr         points to a boolean that is set TRUE for negation else FALSE
1215    dptr           points to an int that is set to the detailed property value    ptypeptr       points to an unsigned int that is set to the type value
1216      pdataptr       points to an unsigned int that is set to the detailed property value
1217    errorcodeptr   points to the error code variable    errorcodeptr   points to the error code variable
1218    
1219  Returns:         type value from ucp_type_table, or -1 for an invalid type  Returns:         TRUE if the type value was found, or FALSE for an invalid type
1220  */  */
1221    
1222  static int  static BOOL
1223  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1224      unsigned int *pdataptr, int *errorcodeptr)
1225  {  {
1226  int c, i, bot, top;  pcre_uchar c;
1227    int i, bot, top;
1228  const pcre_uchar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1229  pcre_uchar name[32];  pcre_uchar name[32];
1230    
1231  c = *(++ptr);  c = *(++ptr);
1232  if (c == 0) goto ERROR_RETURN;  if (c == CHAR_NULL) goto ERROR_RETURN;
1233    
1234  *negptr = FALSE;  *negptr = FALSE;
1235    
# Line 1200  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1246  if (c == CHAR_LEFT_CURLY_BRACKET)
1246    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1247      {      {
1248      c = *(++ptr);      c = *(++ptr);
1249      if (c == 0) goto ERROR_RETURN;      if (c == CHAR_NULL) goto ERROR_RETURN;
1250      if (c == CHAR_RIGHT_CURLY_BRACKET) break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1251      name[i] = c;      name[i] = c;
1252      }      }
# Line 1225  top = PRIV(utt_size); Line 1271  top = PRIV(utt_size);
1271    
1272  while (bot < top)  while (bot < top)
1273    {    {
1274      int r;
1275    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1276    c = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);    r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1277    if (c == 0)    if (r == 0)
1278      {      {
1279      *dptr = PRIV(utt)[i].value;      *ptypeptr = PRIV(utt)[i].type;
1280      return PRIV(utt)[i].type;      *pdataptr = PRIV(utt)[i].value;
1281        return TRUE;
1282      }      }
1283    if (c > 0) bot = i + 1; else top = i;    if (r > 0) bot = i + 1; else top = i;
1284    }    }
1285    
1286  *errorcodeptr = ERR47;  *errorcodeptr = ERR47;
1287  *ptrptr = ptr;  *ptrptr = ptr;
1288  return -1;  return FALSE;
1289    
1290  ERROR_RETURN:  ERROR_RETURN:
1291  *errorcodeptr = ERR46;  *errorcodeptr = ERR46;
1292  *ptrptr = ptr;  *ptrptr = ptr;
1293  return -1;  return FALSE;
1294  }  }
1295  #endif  #endif
1296    
# Line 1277  int max = -1; Line 1325  int max = -1;
1325  /* Read the minimum value and do a paranoid check: a negative value indicates  /* Read the minimum value and do a paranoid check: a negative value indicates
1326  an integer overflow. */  an integer overflow. */
1327    
1328  while (IS_DIGIT(*p)) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);
1329  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1330    {    {
1331    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 1292  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1340  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1340    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1341      {      {
1342      max = 0;      max = 0;
1343      while(IS_DIGIT(*p)) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);
1344      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1345        {        {
1346        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1317  return p; Line 1365  return p;
1365    
1366    
1367  /*************************************************  /*************************************************
 *  Subroutine for finding forward reference      *  
 *************************************************/  
   
 /* This recursive function is called only from find_parens() below. The  
 top-level call starts at the beginning of the pattern. All other calls must  
 start at a parenthesis. It scans along a pattern's text looking for capturing  
 subpatterns, and counting them. If it finds a named pattern that matches the  
 name it is given, it returns its number. Alternatively, if the name is NULL, it  
 returns when it reaches a given numbered subpattern. Recursion is used to keep  
 track of subpatterns that reset the capturing group numbers - the (?| feature.  
   
 This function was originally called only from the second pass, in which we know  
 that if (?< or (?' or (?P< is encountered, the name will be correctly  
 terminated because that is checked in the first pass. There is now one call to  
 this function in the first pass, to check for a recursive back reference by  
 name (so that we can make the whole group atomic). In this case, we need check  
 only up to the current position in the pattern, and that is still OK because  
 and previous occurrences will have been checked. To make this work, the test  
 for "end of pattern" is a check against cd->end_pattern in the main loop,  
 instead of looking for a binary zero. This means that the special first-pass  
 call can adjust cd->end_pattern temporarily. (Checks for binary zero while  
 processing items within the loop are OK, because afterwards the main loop will  
 terminate.)  
   
 Arguments:  
   ptrptr       address of the current character pointer (updated)  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf          TRUE if we are in UTF-8 / UTF-16 mode  
   count        pointer to the current capturing subpattern number (updated)  
   
 Returns:       the number of the named subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,  
   BOOL xmode, BOOL utf, int *count)  
 {  
 pcre_uchar *ptr = *ptrptr;  
 int start_count = *count;  
 int hwm_count = start_count;  
 BOOL dup_parens = FALSE;  
   
 /* If the first character is a parenthesis, check on the type of group we are  
 dealing with. The very first call may not start with a parenthesis. */  
   
 if (ptr[0] == CHAR_LEFT_PARENTHESIS)  
   {  
   /* Handle specials such as (*SKIP) or (*UTF8) etc. */  
   
   if (ptr[1] == CHAR_ASTERISK) ptr += 2;  
   
   /* Handle a normal, unnamed capturing parenthesis. */  
   
   else if (ptr[1] != CHAR_QUESTION_MARK)  
     {  
     *count += 1;  
     if (name == NULL && *count == lorn) return *count;  
     ptr++;  
     }  
   
   /* All cases now have (? at the start. Remember when we are in a group  
   where the parenthesis numbers are duplicated. */  
   
   else if (ptr[2] == CHAR_VERTICAL_LINE)  
     {  
     ptr += 3;  
     dup_parens = TRUE;  
     }  
   
   /* Handle comments; all characters are allowed until a ket is reached. */  
   
   else if (ptr[2] == CHAR_NUMBER_SIGN)  
     {  
     for (ptr += 3; *ptr != 0; ptr++) if (*ptr == CHAR_RIGHT_PARENTHESIS) break;  
     goto FAIL_EXIT;  
     }  
   
   /* Handle a condition. If it is an assertion, just carry on so that it  
   is processed as normal. If not, skip to the closing parenthesis of the  
   condition (there can't be any nested parens). */  
   
   else if (ptr[2] == CHAR_LEFT_PARENTHESIS)  
     {  
     ptr += 2;  
     if (ptr[1] != CHAR_QUESTION_MARK)  
       {  
       while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
       if (*ptr != 0) ptr++;  
       }  
     }  
   
   /* Start with (? but not a condition. */  
   
   else  
     {  
     ptr += 2;  
     if (*ptr == CHAR_P) ptr++;                      /* Allow optional P */  
   
     /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */  
   
     if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&  
         ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)  
       {  
       int term;  
       const pcre_uchar *thisname;  
       *count += 1;  
       if (name == NULL && *count == lorn) return *count;  
       term = *ptr++;  
       if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;  
       thisname = ptr;  
       while (*ptr != term) ptr++;  
       if (name != NULL && lorn == ptr - thisname &&  
           STRNCMP_UC_UC(name, thisname, lorn) == 0)  
         return *count;  
       term++;  
       }  
     }  
   }  
   
 /* Past any initial parenthesis handling, scan for parentheses or vertical  
 bars. Stop if we get to cd->end_pattern. Note that this is important for the  
 first-pass call when this value is temporarily adjusted to stop at the current  
 position. So DO NOT change this to a test for binary zero. */  
   
 for (; ptr < cd->end_pattern; ptr++)  
   {  
   /* Skip over backslashed characters and also entire \Q...\E */  
   
   if (*ptr == CHAR_BACKSLASH)  
     {  
     if (*(++ptr) == 0) goto FAIL_EXIT;  
     if (*ptr == CHAR_Q) for (;;)  
       {  
       while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};  
       if (*ptr == 0) goto FAIL_EXIT;  
       if (*(++ptr) == CHAR_E) break;  
       }  
     continue;  
     }  
   
   /* Skip over character classes; this logic must be similar to the way they  
   are handled for real. If the first character is '^', skip it. Also, if the  
   first few characters (either before or after ^) are \Q\E or \E we skip them  
   too. This makes for compatibility with Perl. Note the use of STR macros to  
   encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */  
   
   if (*ptr == CHAR_LEFT_SQUARE_BRACKET)  
     {  
     BOOL negate_class = FALSE;  
     for (;;)  
       {  
       if (ptr[1] == CHAR_BACKSLASH)  
         {  
         if (ptr[2] == CHAR_E)  
           ptr+= 2;  
         else if (STRNCMP_UC_C8(ptr + 2,  
                  STR_Q STR_BACKSLASH STR_E, 3) == 0)  
           ptr += 4;  
         else  
           break;  
         }  
       else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)  
         {  
         negate_class = TRUE;  
         ptr++;  
         }  
       else break;  
       }  
   
     /* If the next character is ']', it is a data character that must be  
     skipped, except in JavaScript compatibility mode. */  
   
     if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&  
         (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)  
       ptr++;  
   
     while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)  
       {  
       if (*ptr == 0) return -1;  
       if (*ptr == CHAR_BACKSLASH)  
         {  
         if (*(++ptr) == 0) goto FAIL_EXIT;  
         if (*ptr == CHAR_Q) for (;;)  
           {  
           while (*(++ptr) != 0 && *ptr != CHAR_BACKSLASH) {};  
           if (*ptr == 0) goto FAIL_EXIT;  
           if (*(++ptr) == CHAR_E) break;  
           }  
         continue;  
         }  
       }  
     continue;  
     }  
   
   /* Skip comments in /x mode */  
   
   if (xmode && *ptr == CHAR_NUMBER_SIGN)  
     {  
     ptr++;  
     while (*ptr != 0)  
       {  
       if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }  
       ptr++;  
 #ifdef SUPPORT_UTF  
       if (utf) FORWARDCHAR(ptr);  
 #endif  
       }  
     if (*ptr == 0) goto FAIL_EXIT;  
     continue;  
     }  
   
   /* Check for the special metacharacters */  
   
   if (*ptr == CHAR_LEFT_PARENTHESIS)  
     {  
     int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, count);  
     if (rc > 0) return rc;  
     if (*ptr == 0) goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_RIGHT_PARENTHESIS)  
     {  
     if (dup_parens && *count < hwm_count) *count = hwm_count;  
     goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)  
     {  
     if (*count > hwm_count) hwm_count = *count;  
     *count = start_count;  
     }  
   }  
   
 FAIL_EXIT:  
 *ptrptr = ptr;  
 return -1;  
 }  
   
   
   
   
 /*************************************************  
 *       Find forward referenced subpattern       *  
 *************************************************/  
   
 /* This function scans along a pattern's text looking for capturing  
 subpatterns, and counting them. If it finds a named pattern that matches the  
 name it is given, it returns its number. Alternatively, if the name is NULL, it  
 returns when it reaches a given numbered subpattern. This is used for forward  
 references to subpatterns. We used to be able to start this scan from the  
 current compiling point, using the current count value from cd->bracount, and  
 do it all in a single loop, but the addition of the possibility of duplicate  
 subpattern numbers means that we have to scan from the very start, in order to  
 take account of such duplicates, and to use a recursive function to keep track  
 of the different types of group.  
   
 Arguments:  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf          TRUE if we are in UTF-8 / UTF-16 mode  
   
 Returns:       the number of the found subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens(compile_data *cd, const pcre_uchar *name, int lorn, BOOL xmode,  
   BOOL utf)  
 {  
 pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;  
 int count = 0;  
 int rc;  
   
 /* If the pattern does not start with an opening parenthesis, the first call  
 to find_parens_sub() will scan right to the end (if necessary). However, if it  
 does start with a parenthesis, find_parens_sub() will return when it hits the  
 matching closing parens. That is why we have to have a loop. */  
   
 for (;;)  
   {  
   rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, &count);  
   if (rc > 0 || *ptr++ == 0) break;  
   }  
   
 return rc;  
 }  
   
   
   
   
 /*************************************************  
1368  *      Find first significant op code            *  *      Find first significant op code            *
1369  *************************************************/  *************************************************/
1370    
# Line 1684  and doing the check at the end; a flag s Line 1437  and doing the check at the end; a flag s
1437    
1438  Arguments:  Arguments:
1439    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1440    utf      TRUE in UTF-8 / UTF-16 mode    utf      TRUE in UTF-8 / UTF-16 / UTF-32 mode
1441    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1442    cd       the "compile data" structure    cd       the "compile data" structure
1443    
# Line 1710  for (;;) Line 1463  for (;;)
1463    {    {
1464    int d;    int d;
1465    pcre_uchar *ce, *cs;    pcre_uchar *ce, *cs;
1466    register int op = *cc;    register pcre_uchar op = *cc;
1467    
1468    switch (op)    switch (op)
1469      {      {
# Line 1830  for (;;) Line 1583  for (;;)
1583      case OP_EXACTI:      case OP_EXACTI:
1584      case OP_NOTEXACT:      case OP_NOTEXACT:
1585      case OP_NOTEXACTI:      case OP_NOTEXACTI:
1586      branchlength += GET2(cc,1);      branchlength += (int)GET2(cc,1);
1587      cc += 2 + IMM2_SIZE;      cc += 2 + IMM2_SIZE;
1588  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
1589      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
# Line 1839  for (;;) Line 1592  for (;;)
1592    
1593      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1594      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1595      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1596          cc += 2;
1597      cc += 1 + IMM2_SIZE + 1;      cc += 1 + IMM2_SIZE + 1;
1598      break;      break;
1599    
# Line 1874  for (;;) Line 1628  for (;;)
1628    
1629      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1630    
 #if defined SUPPORT_UTF || defined COMPILE_PCRE16  
     case OP_XCLASS:  
     cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];  
     /* Fall through */  
 #endif  
   
1631      case OP_CLASS:      case OP_CLASS:
1632      case OP_NCLASS:      case OP_NCLASS:
1633    #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1634        case OP_XCLASS:
1635        /* The original code caused an unsigned overflow in 64 bit systems,
1636        so now we use a conditional statement. */
1637        if (op == OP_XCLASS)
1638          cc += GET(cc, 1);
1639        else
1640          cc += PRIV(OP_lengths)[OP_CLASS];
1641    #else
1642      cc += PRIV(OP_lengths)[OP_CLASS];      cc += PRIV(OP_lengths)[OP_CLASS];
1643    #endif
1644    
1645      switch (*cc)      switch (*cc)
1646        {        {
# Line 1897  for (;;) Line 1655  for (;;)
1655        case OP_CRRANGE:        case OP_CRRANGE:
1656        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1657        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1658        branchlength += GET2(cc,1);        branchlength += (int)GET2(cc,1);
1659        cc += 1 + 2 * IMM2_SIZE;        cc += 1 + 2 * IMM2_SIZE;
1660        break;        break;
1661    
# Line 2013  length. Line 1771  length.
1771    
1772  Arguments:  Arguments:
1773    code        points to start of expression    code        points to start of expression
1774    utf         TRUE in UTF-8 / UTF-16 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1775    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
1776    
1777  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
# Line 2024  PRIV(find_bracket)(const pcre_uchar *cod Line 1782  PRIV(find_bracket)(const pcre_uchar *cod
1782  {  {
1783  for (;;)  for (;;)
1784    {    {
1785    register int c = *code;    register pcre_uchar c = *code;
1786    
1787    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1788    
# Line 2047  for (;;) Line 1805  for (;;)
1805    else if (c == OP_CBRA || c == OP_SCBRA ||    else if (c == OP_CBRA || c == OP_SCBRA ||
1806             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
1807      {      {
1808      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1809      if (n == number) return (pcre_uchar *)code;      if (n == number) return (pcre_uchar *)code;
1810      code += PRIV(OP_lengths)[c];      code += PRIV(OP_lengths)[c];
1811      }      }
# Line 2077  for (;;) Line 1835  for (;;)
1835        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1836        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1837        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1838        if (code[1 + IMM2_SIZE] == OP_PROP        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1839          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;          code += 2;
1840        break;        break;
1841    
1842        case OP_MARK:        case OP_MARK:
1843        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1844        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
1845        case OP_THEN_ARG:        case OP_THEN_ARG:
1846        code += code[1];        code += code[1];
1847        break;        break;
# Line 2100  for (;;) Line 1855  for (;;)
1855    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
1856    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
1857    
1858  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1859      if (utf) switch(c)      if (utf) switch(c)
1860        {        {
1861        case OP_CHAR:        case OP_CHAR:
# Line 2152  instance of OP_RECURSE. Line 1907  instance of OP_RECURSE.
1907    
1908  Arguments:  Arguments:
1909    code        points to start of expression    code        points to start of expression
1910    utf         TRUE in UTF-8 / UTF-16 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1911    
1912  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
1913  */  */
# Line 2162  find_recurse(const pcre_uchar *code, BOO Line 1917  find_recurse(const pcre_uchar *code, BOO
1917  {  {
1918  for (;;)  for (;;)
1919    {    {
1920    register int c = *code;    register pcre_uchar c = *code;
1921    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1922    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
1923    
# Line 2197  for (;;) Line 1952  for (;;)
1952        case OP_TYPEUPTO:        case OP_TYPEUPTO:
1953        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1954        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1955        if (code[1 + IMM2_SIZE] == OP_PROP        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1956          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;          code += 2;
1957        break;        break;
1958    
1959        case OP_MARK:        case OP_MARK:
1960        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1961        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
1962        case OP_THEN_ARG:        case OP_THEN_ARG:
1963        code += code[1];        code += code[1];
1964        break;        break;
# Line 2220  for (;;) Line 1972  for (;;)
1972      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
1973      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
1974    
1975  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1976      if (utf) switch(c)      if (utf) switch(c)
1977        {        {
1978        case OP_CHAR:        case OP_CHAR:
1979        case OP_CHARI:        case OP_CHARI:
1980          case OP_NOT:
1981          case OP_NOTI:
1982        case OP_EXACT:        case OP_EXACT:
1983        case OP_EXACTI:        case OP_EXACTI:
1984          case OP_NOTEXACT:
1985          case OP_NOTEXACTI:
1986        case OP_UPTO:        case OP_UPTO:
1987        case OP_UPTOI:        case OP_UPTOI:
1988          case OP_NOTUPTO:
1989          case OP_NOTUPTOI:
1990        case OP_MINUPTO:        case OP_MINUPTO:
1991        case OP_MINUPTOI:        case OP_MINUPTOI:
1992          case OP_NOTMINUPTO:
1993          case OP_NOTMINUPTOI:
1994        case OP_POSUPTO:        case OP_POSUPTO:
1995        case OP_POSUPTOI:        case OP_POSUPTOI:
1996          case OP_NOTPOSUPTO:
1997          case OP_NOTPOSUPTOI:
1998        case OP_STAR:        case OP_STAR:
1999        case OP_STARI:        case OP_STARI:
2000          case OP_NOTSTAR:
2001          case OP_NOTSTARI:
2002        case OP_MINSTAR:        case OP_MINSTAR:
2003        case OP_MINSTARI:        case OP_MINSTARI:
2004          case OP_NOTMINSTAR:
2005          case OP_NOTMINSTARI:
2006        case OP_POSSTAR:        case OP_POSSTAR:
2007        case OP_POSSTARI:        case OP_POSSTARI:
2008          case OP_NOTPOSSTAR:
2009          case OP_NOTPOSSTARI:
2010        case OP_PLUS:        case OP_PLUS:
2011        case OP_PLUSI:        case OP_PLUSI:
2012          case OP_NOTPLUS:
2013          case OP_NOTPLUSI:
2014        case OP_MINPLUS:        case OP_MINPLUS:
2015        case OP_MINPLUSI:        case OP_MINPLUSI:
2016          case OP_NOTMINPLUS:
2017          case OP_NOTMINPLUSI:
2018        case OP_POSPLUS:        case OP_POSPLUS:
2019        case OP_POSPLUSI:        case OP_POSPLUSI:
2020          case OP_NOTPOSPLUS:
2021          case OP_NOTPOSPLUSI:
2022        case OP_QUERY:        case OP_QUERY:
2023        case OP_QUERYI:        case OP_QUERYI:
2024          case OP_NOTQUERY:
2025          case OP_NOTQUERYI:
2026        case OP_MINQUERY:        case OP_MINQUERY:
2027        case OP_MINQUERYI:        case OP_MINQUERYI:
2028          case OP_NOTMINQUERY:
2029          case OP_NOTMINQUERYI:
2030        case OP_POSQUERY:        case OP_POSQUERY:
2031        case OP_POSQUERYI:        case OP_POSQUERYI:
2032          case OP_NOTPOSQUERY:
2033          case OP_NOTPOSQUERYI:
2034        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2035        break;        break;
2036        }        }
# Line 2278  bracket whose current branch will alread Line 2058  bracket whose current branch will alread
2058  Arguments:  Arguments:
2059    code        points to start of search    code        points to start of search
2060    endcode     points to where to stop    endcode     points to where to stop
2061    utf         TRUE if in UTF-8 / UTF-16 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2062    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2063      recurses    chain of recurse_check to catch mutual recursion
2064    
2065  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2066  */  */
2067    
2068    typedef struct recurse_check {
2069      struct recurse_check *prev;
2070      const pcre_uchar *group;
2071    } recurse_check;
2072    
2073  static BOOL  static BOOL
2074  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2075    BOOL utf, compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2076  {  {
2077  register int c;  register pcre_uchar c;
2078    recurse_check this_recurse;
2079    
2080  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2081       code < endcode;       code < endcode;
2082       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
# Line 2316  for (code = first_significant_code(code Line 2104  for (code = first_significant_code(code
2104    
2105    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2106      {      {
2107      const pcre_uchar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2108      BOOL empty_branch;      BOOL empty_branch;
2109    
2110      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2111        when called to scan a completed pattern by setting cd->start_workspace to
2112        NULL. */
2113    
2114        if (cd->start_workspace != NULL)
2115          {
2116          const pcre_uchar *tcode;
2117          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2118            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2119          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2120          }
2121    
2122        /* If we are scanning a completed pattern, there are no forward references
2123        and all groups are complete. We need to detect whether this is a recursive
2124        call, as otherwise there will be an infinite loop. If it is a recursion,
2125        just skip over it. Simple recursions are easily detected. For mutual
2126        recursions we keep a chain on the stack. */
2127    
2128      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)      else
2129        if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;        {
2130          recurse_check *r = recurses;
2131          const pcre_uchar *endgroup = scode;
2132    
2133      /* Not a forward reference, test for completed backward reference */        do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2134          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2135    
2136      empty_branch = FALSE;        for (r = recurses; r != NULL; r = r->prev)
2137      scode = cd->start_code + GET(code, 1);          if (r->group == scode) break;
2138      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */        if (r != NULL) continue;   /* Mutual recursion */
2139          }
2140    
2141        /* Completed reference; scan the referenced group, remembering it on the
2142        stack chain to detect mutual recursions. */
2143    
2144      /* Completed backwards reference */      empty_branch = FALSE;
2145        this_recurse.prev = recurses;
2146        this_recurse.group = scode;
2147    
2148      do      do
2149        {        {
2150        if (could_be_empty_branch(scode, endcode, utf, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2151          {          {
2152          empty_branch = TRUE;          empty_branch = TRUE;
2153          break;          break;
# Line 2390  for (code = first_significant_code(code Line 2203  for (code = first_significant_code(code
2203        empty_branch = FALSE;        empty_branch = FALSE;
2204        do        do
2205          {          {
2206          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2207            empty_branch = TRUE;            empty_branch = TRUE;
2208          code += GET(code, 1);          code += GET(code, 1);
2209          }          }
# Line 2448  for (code = first_significant_code(code Line 2261  for (code = first_significant_code(code
2261    
2262      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2263    
2264        case OP_ANY:
2265        case OP_ALLANY:
2266        case OP_ANYBYTE:
2267    
2268      case OP_PROP:      case OP_PROP:
2269      case OP_NOTPROP:      case OP_NOTPROP:
2270        case OP_ANYNL:
2271    
2272        case OP_NOT_HSPACE:
2273        case OP_HSPACE:
2274        case OP_NOT_VSPACE:
2275        case OP_VSPACE:
2276      case OP_EXTUNI:      case OP_EXTUNI:
2277    
2278      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2279      case OP_DIGIT:      case OP_DIGIT:
2280      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2281      case OP_WHITESPACE:      case OP_WHITESPACE:
2282      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2283      case OP_WORDCHAR:      case OP_WORDCHAR:
2284      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2285      case OP_CHAR:      case OP_CHAR:
2286      case OP_CHARI:      case OP_CHARI:
2287      case OP_NOT:      case OP_NOT:
2288      case OP_NOTI:      case OP_NOTI:
2289    
2290      case OP_PLUS:      case OP_PLUS:
2291        case OP_PLUSI:
2292      case OP_MINPLUS:      case OP_MINPLUS:
2293      case OP_POSPLUS:      case OP_MINPLUSI:
2294      case OP_EXACT:  
2295      case OP_NOTPLUS:      case OP_NOTPLUS:
2296        case OP_NOTPLUSI:
2297      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2298        case OP_NOTMINPLUSI:
2299    
2300        case OP_POSPLUS:
2301        case OP_POSPLUSI:
2302      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2303        case OP_NOTPOSPLUSI:
2304    
2305        case OP_EXACT:
2306        case OP_EXACTI:
2307      case OP_NOTEXACT:      case OP_NOTEXACT:
2308        case OP_NOTEXACTI:
2309    
2310      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2311      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2312      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2313      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2314    
2315      return FALSE;      return FALSE;
2316    
2317      /* These are going to continue, as they may be empty, but we have to      /* These are going to continue, as they may be empty, but we have to
# Line 2495  for (code = first_significant_code(code Line 2331  for (code = first_significant_code(code
2331      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2332      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2333      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2334      if (code[1 + IMM2_SIZE] == OP_PROP      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2335        || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;        code += 2;
2336      break;      break;
2337    
2338      /* End of branch */      /* End of branch */
# Line 2509  for (code = first_significant_code(code Line 2345  for (code = first_significant_code(code
2345      return TRUE;      return TRUE;
2346    
2347      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2348      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2349        followed by a multibyte character. */
2350    
2351  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2352      case OP_STAR:      case OP_STAR:
2353      case OP_STARI:      case OP_STARI:
2354        case OP_NOTSTAR:
2355        case OP_NOTSTARI:
2356    
2357      case OP_MINSTAR:      case OP_MINSTAR:
2358      case OP_MINSTARI:      case OP_MINSTARI:
2359        case OP_NOTMINSTAR:
2360        case OP_NOTMINSTARI:
2361    
2362      case OP_POSSTAR:      case OP_POSSTAR:
2363      case OP_POSSTARI:      case OP_POSSTARI:
2364        case OP_NOTPOSSTAR:
2365        case OP_NOTPOSSTARI:
2366    
2367      case OP_QUERY:      case OP_QUERY:
2368      case OP_QUERYI:      case OP_QUERYI:
2369        case OP_NOTQUERY:
2370        case OP_NOTQUERYI:
2371    
2372      case OP_MINQUERY:      case OP_MINQUERY:
2373      case OP_MINQUERYI:      case OP_MINQUERYI:
2374        case OP_NOTMINQUERY:
2375        case OP_NOTMINQUERYI:
2376    
2377      case OP_POSQUERY:      case OP_POSQUERY:
2378      case OP_POSQUERYI:      case OP_POSQUERYI:
2379        case OP_NOTPOSQUERY:
2380        case OP_NOTPOSQUERYI:
2381    
2382      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2383      break;      break;
2384    
2385      case OP_UPTO:      case OP_UPTO:
2386      case OP_UPTOI:      case OP_UPTOI:
2387        case OP_NOTUPTO:
2388        case OP_NOTUPTOI:
2389    
2390      case OP_MINUPTO:      case OP_MINUPTO:
2391      case OP_MINUPTOI:      case OP_MINUPTOI:
2392        case OP_NOTMINUPTO:
2393        case OP_NOTMINUPTOI:
2394    
2395      case OP_POSUPTO:      case OP_POSUPTO:
2396      case OP_POSUPTOI:      case OP_POSUPTOI:
2397        case OP_NOTPOSUPTO:
2398        case OP_NOTPOSUPTOI:
2399    
2400      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2401      break;      break;
2402  #endif  #endif
# Line 2543  for (code = first_significant_code(code Line 2407  for (code = first_significant_code(code
2407      case OP_MARK:      case OP_MARK:
2408      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2409      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     code += code[1];  
     break;  
   
2410      case OP_THEN_ARG:      case OP_THEN_ARG:
2411      code += code[1];      code += code[1];
2412      break;      break;
# Line 2577  Arguments: Line 2438  Arguments:
2438    code        points to start of the recursion    code        points to start of the recursion
2439    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2440    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2441    utf         TRUE if in UTF-8 / UTF-16 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2442    cd          pointers to tables etc    cd          pointers to tables etc
2443    
2444  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
# Line 2589  could_be_empty(const pcre_uchar *code, c Line 2450  could_be_empty(const pcre_uchar *code, c
2450  {  {
2451  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2452    {    {
2453    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2454      return FALSE;      return FALSE;
2455    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2456    }    }
# Line 2643  Returns:   TRUE or FALSE Line 2504  Returns:   TRUE or FALSE
2504  static BOOL  static BOOL
2505  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2506  {  {
2507  int terminator;          /* Don't combine these lines; the Solaris cc */  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
2508  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
2509  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != CHAR_NULL; ptr++)
2510    {    {
2511    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2512      ptr++;      ptr++;
# Line 2692  register int yield = 0; Line 2553  register int yield = 0;
2553  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
2554    {    {
2555    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
2556      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
2557    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
2558    yield++;    yield++;
2559    }    }
# Line 2724  value in the reference (which is a group Line 2585  value in the reference (which is a group
2585  Arguments:  Arguments:
2586    group      points to the start of the group    group      points to the start of the group
2587    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
2588    utf        TRUE in UTF-8 / UTF-16 mode    utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
2589    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
2590    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
2591    
# Line 2747  while ((ptr = (pcre_uchar *)find_recurse Line 2608  while ((ptr = (pcre_uchar *)find_recurse
2608    
2609    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
2610      {      {
2611      offset = GET(hc, 0);      offset = (int)GET(hc, 0);
2612      if (cd->start_code + offset == ptr + 1)      if (cd->start_code + offset == ptr + 1)
2613        {        {
2614        PUT(hc, 0, offset + adjust);        PUT(hc, 0, offset + adjust);
# Line 2760  while ((ptr = (pcre_uchar *)find_recurse Line 2621  while ((ptr = (pcre_uchar *)find_recurse
2621    
2622    if (hc >= cd->hwm)    if (hc >= cd->hwm)
2623      {      {
2624      offset = GET(ptr, 1);      offset = (int)GET(ptr, 1);
2625      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
2626      }      }
2627    
# Line 2828  PUT(previous_callout, 2 + LINK_SIZE, len Line 2689  PUT(previous_callout, 2 + LINK_SIZE, len
2689  *************************************************/  *************************************************/
2690    
2691  /* 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
2692  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
2693  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
2694  start address.  start address. A character with multiple other cases is returned on its own
2695    with a special return value.
2696    
2697  Arguments:  Arguments:
2698    cptr        points to starting character value; updated    cptr        points to starting character value; updated
# Line 2838  Arguments: Line 2700  Arguments:
2700    ocptr       where to put start of othercase range    ocptr       where to put start of othercase range
2701    odptr       where to put end of othercase range    odptr       where to put end of othercase range
2702    
2703  Yield:        TRUE when range returned; FALSE when no more  Yield:        -1 when no more
2704                   0 when a range is returned
2705                  >0 the CASESET offset for char with multiple other cases
2706                    in this case, ocptr contains the original
2707  */  */
2708    
2709  static BOOL  static int
2710  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
2711    unsigned int *odptr)    pcre_uint32 *odptr)
2712  {  {
2713  unsigned int c, othercase, next;  pcre_uint32 c, othercase, next;
2714    unsigned int co;
2715    
2716    /* Find the first character that has an other case. If it has multiple other
2717    cases, return its case offset value. */
2718    
2719  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
2720    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
2721      if ((co = UCD_CASESET(c)) != 0)
2722        {
2723        *ocptr = c++;   /* Character that has the set */
2724        *cptr = c;      /* Rest of input range */
2725        return (int)co;
2726        }
2727      if ((othercase = UCD_OTHERCASE(c)) != c) break;
2728      }
2729    
2730  if (c > d) return FALSE;  if (c > d) return -1;  /* Reached end of range */
2731    
2732  *ocptr = othercase;  *ocptr = othercase;
2733  next = othercase + 1;  next = othercase + 1;
# Line 2861  for (++c; c <= d; c++) Line 2738  for (++c; c <= d; c++)
2738    next++;    next++;
2739    }    }
2740    
2741  *odptr = next - 1;  *odptr = next - 1;     /* End of othercase range */
2742  *cptr = c;  *cptr = c;             /* Rest of input range */
2743    return 0;
 return TRUE;  
2744  }  }
2745    
2746    
# Line 2886  Returns:       TRUE if auto-possessifyin Line 2762  Returns:       TRUE if auto-possessifyin
2762  */  */
2763    
2764  static BOOL  static BOOL
2765  check_char_prop(int c, int ptype, int pdata, BOOL negated)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated)
2766  {  {
2767    #ifdef SUPPORT_UCP
2768    const pcre_uint32 *p;
2769    #endif
2770    
2771  const ucd_record *prop = GET_UCD(c);  const ucd_record *prop = GET_UCD(c);
2772    
2773  switch(ptype)  switch(ptype)
2774    {    {
2775    case PT_LAMP:    case PT_LAMP:
# Line 2926  switch(ptype) Line 2807  switch(ptype)
2807    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2808            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2809            c == CHAR_UNDERSCORE) == negated;            c == CHAR_UNDERSCORE) == negated;
2810    
2811    #ifdef SUPPORT_UCP
2812      case PT_CLIST:
2813      p = PRIV(ucd_caseless_sets) + prop->caseset;
2814      for (;;)
2815        {
2816        if (c < *p) return !negated;
2817        if (c == *p++) return negated;
2818        }
2819      break;  /* Control never reaches here */
2820    #endif
2821    }    }
2822    
2823  return FALSE;  return FALSE;
2824  }  }
2825  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
# Line 2943  sense to automatically possessify the re Line 2836  sense to automatically possessify the re
2836    
2837  Arguments:  Arguments:
2838    previous      pointer to the repeated opcode    previous      pointer to the repeated opcode
2839    utf           TRUE in UTF-8 / UTF-16 mode    utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode
2840    ptr           next character in pattern    ptr           next character in pattern
2841    options       options bits    options       options bits
2842    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 2955  static BOOL Line 2848  static BOOL
2848  check_auto_possessive(const pcre_uchar *previous, BOOL utf,  check_auto_possessive(const pcre_uchar *previous, BOOL utf,
2849    const pcre_uchar *ptr, int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
2850  {  {
2851  pcre_int32 c, next;  pcre_uint32 c = NOTACHAR;
2852  int op_code = *previous++;  pcre_uint32 next;
2853    int escape;
2854    pcre_uchar op_code = *previous++;
2855    
2856  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
2857    
# Line 2968  if ((options & PCRE_EXTENDED) != 0) Line 2863  if ((options & PCRE_EXTENDED) != 0)
2863      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2864        {        {
2865        ptr++;        ptr++;
2866        while (*ptr != 0)        while (*ptr != CHAR_NULL)
2867          {          {
2868          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2869          ptr++;          ptr++;
# Line 2987  value is a character, a negative value i Line 2882  value is a character, a negative value i
2882  if (*ptr == CHAR_BACKSLASH)  if (*ptr == CHAR_BACKSLASH)
2883    {    {
2884    int temperrorcode = 0;    int temperrorcode = 0;
2885    next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);    escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options,
2886        FALSE);
2887    if (temperrorcode != 0) return FALSE;    if (temperrorcode != 0) return FALSE;
2888    ptr++;    /* Point after the escape sequence */    ptr++;    /* Point after the escape sequence */
2889    }    }
2890  else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)  else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)
2891    {    {
2892      escape = 0;
2893  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2894    if (utf) { GETCHARINC(next, ptr); } else    if (utf) { GETCHARINC(next, ptr); } else
2895  #endif  #endif
# Line 3010  if ((options & PCRE_EXTENDED) != 0) Line 2907  if ((options & PCRE_EXTENDED) != 0)
2907      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2908        {        {
2909        ptr++;        ptr++;
2910        while (*ptr != 0)        while (*ptr != CHAR_NULL)
2911          {          {
2912          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2913          ptr++;          ptr++;
# Line 3029  if (*ptr == CHAR_ASTERISK || *ptr == CHA Line 2926  if (*ptr == CHAR_ASTERISK || *ptr == CHA
2926    STRNCMP_UC_C8(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)
2927      return FALSE;      return FALSE;
2928    
2929  /* Now compare the next item with the previous opcode. First, handle cases when  /* If the previous item is a character, get its value. */
 the next item is a character. */  
2930    
2931  if (next >= 0) switch(op_code)  if (op_code == OP_CHAR || op_code == OP_CHARI ||
2932        op_code == OP_NOT || op_code == OP_NOTI)
2933    {    {
   case OP_CHAR:  
2934  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2935    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
2936  #else  #else
2937    c = *previous;    c = *previous;
2938  #endif  #endif
2939    return c != next;    }
2940    
2941    /* For CHARI (caseless character) we must check the other case. If we have  /* Now compare the next item with the previous opcode. First, handle cases when
2942    Unicode property support, we can use it to test the other case of  the next item is a character. */
   high-valued characters. */  
2943    
2944    case OP_CHARI:  if (escape == 0)
2945  #ifdef SUPPORT_UTF    {
2946    GETCHARTEST(c, previous);    /* For a caseless UTF match, the next character may have more than one other
2947  #else    case, which maps to the special PT_CLIST property. Check this first. */
2948    c = *previous;  
2949    #ifdef SUPPORT_UCP
2950      if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)
2951        {
2952        unsigned int ocs = UCD_CASESET(next);
2953        if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);
2954        }
2955  #endif  #endif
2956    if (c == next) return FALSE;  
2957  #ifdef SUPPORT_UTF    switch(op_code)
   if (utf)  
2958      {      {
2959      unsigned int othercase;      case OP_CHAR:
2960      if (next < 128) othercase = cd->fcc[next]; else      return c != next;
2961    
2962        /* For CHARI (caseless character) we must check the other case. If we have
2963        Unicode property support, we can use it to test the other case of
2964        high-valued characters. We know that next can have only one other case,
2965        because multi-other-case characters are dealt with above. */
2966    
2967        case OP_CHARI:
2968        if (c == next) return FALSE;
2969    #ifdef SUPPORT_UTF
2970        if (utf)
2971          {
2972          pcre_uint32 othercase;
2973          if (next < 128) othercase = cd->fcc[next]; else
2974  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2975      othercase = UCD_OTHERCASE((unsigned int)next);        othercase = UCD_OTHERCASE(next);
2976  #else  #else
2977      othercase = NOTACHAR;        othercase = NOTACHAR;
2978  #endif  #endif
2979      return (unsigned int)c != othercase;        return c != othercase;
2980      }        }
2981    else      else
2982  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
2983    return (c != TABLE_GET(next, cd->fcc, next));  /* Non-UTF-8 mode */      return (c != TABLE_GET(next, cd->fcc, next));  /* Not UTF */
   
   /* 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. */  
2984    
2985    case OP_NOT:      case OP_NOT:
2986    return (c = *previous) == next;      return c == next;
2987    
2988    case OP_NOTI:      case OP_NOTI:
2989    if ((c = *previous) == next) return TRUE;      if (c == next) return TRUE;
2990  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2991    if (utf)      if (utf)
2992      {        {
2993      unsigned int othercase;        pcre_uint32 othercase;
2994      if (next < 128) othercase = cd->fcc[next]; else        if (next < 128) othercase = cd->fcc[next]; else
2995  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2996      othercase = UCD_OTHERCASE(next);        othercase = UCD_OTHERCASE(next);
2997  #else  #else
2998      othercase = NOTACHAR;        othercase = NOTACHAR;
2999  #endif  #endif
3000      return (unsigned int)c == othercase;        return c == othercase;
3001      }        }
3002    else      else
3003  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
3004    return (c == TABLE_GET(next, cd->fcc, next));  /* Non-UTF-8 mode */      return (c == TABLE_GET(next, cd->fcc, next));  /* Not UTF */
3005    
3006    /* 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.
3007    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. */
3008    
3009    case OP_DIGIT:      case OP_DIGIT:
3010    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;      return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
3011    
3012    case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
3013    return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
3014    
3015    case OP_WHITESPACE:      case OP_WHITESPACE:
3016    return next > 127 || (cd->ctypes[next] & ctype_space) == 0;      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
3017    
3018    case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
3019    return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
3020    
3021    case OP_WORDCHAR:      case OP_WORDCHAR:
3022    return next > 127 || (cd->ctypes[next] & ctype_word) == 0;      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
3023    
3024    case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
3025    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
3026    
3027    case OP_HSPACE:      case OP_HSPACE:
3028    case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
3029    switch(next)      switch(next)
3030      {        {
3031      case 0x09:        HSPACE_CASES:
3032      case 0x20:        return op_code == OP_NOT_HSPACE;
     case 0xa0:  
     case 0x1680:  
     case 0x180e:  
     case 0x2000:  
     case 0x2001:  
     case 0x2002:  
     case 0x2003:  
     case 0x2004:  
     case 0x2005:  
     case 0x2006:  
     case 0x2007:  
     case 0x2008:  
     case 0x2009:  
     case 0x200A:  
     case 0x202f:  
     case 0x205f:  
     case 0x3000:  
     return op_code == OP_NOT_HSPACE;  
     default:  
     return op_code != OP_NOT_HSPACE;  
     }  
3033    
3034    case OP_ANYNL:        default:
3035    case OP_VSPACE:        return op_code != OP_NOT_HSPACE;
3036    case OP_NOT_VSPACE:        }
3037    switch(next)  
3038      {      case OP_ANYNL:
3039      case 0x0a:      case OP_VSPACE:
3040      case 0x0b:      case OP_NOT_VSPACE:
3041      case 0x0c:      switch(next)
3042      case 0x0d:        {
3043      case 0x85:        VSPACE_CASES:
3044      case 0x2028:        return op_code == OP_NOT_VSPACE;
3045      case 0x2029:  
3046      return op_code == OP_NOT_VSPACE;        default:
3047      default:        return op_code != OP_NOT_VSPACE;
3048      return op_code != OP_NOT_VSPACE;        }
     }  
3049    
3050  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3051    case OP_PROP:      case OP_PROP:
3052    return check_char_prop(next, previous[0], previous[1], FALSE);      return check_char_prop(next, previous[0], previous[1], FALSE);
3053    
3054    case OP_NOTPROP:      case OP_NOTPROP:
3055    return check_char_prop(next, previous[0], previous[1], TRUE);      return check_char_prop(next, previous[0], previous[1], TRUE);
3056  #endif  #endif
3057    
3058    default:      default:
3059    return FALSE;      return FALSE;
3060        }
3061    }    }
3062    
   
3063  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3064  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3065  generated only when PCRE_UCP is *not* set, that is, when only ASCII  generated only when PCRE_UCP is *not* set, that is, when only ASCII
# Line 3183  switch(op_code) Line 3070  switch(op_code)
3070    {    {
3071    case OP_CHAR:    case OP_CHAR:
3072    case OP_CHARI:    case OP_CHARI:
3073  #ifdef SUPPORT_UTF    switch(escape)
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   switch(-next)  
3074      {      {
3075      case ESC_d:      case ESC_d:
3076      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;
3077    
3078      case ESC_D:      case ESC_D:
3079      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;
3080    
3081      case ESC_s:      case ESC_s:
3082      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;
3083    
3084      case ESC_S:      case ESC_S:
3085      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;
3086    
3087      case ESC_w:      case ESC_w:
3088      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;
3089    
3090      case ESC_W:      case ESC_W:
3091      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;
3092    
3093      case ESC_h:      case ESC_h:
3094      case ESC_H:      case ESC_H:
3095      switch(c)      switch(c)
3096        {        {
3097        case 0x09:        HSPACE_CASES:
3098        case 0x20:        return escape != ESC_h;
3099        case 0xa0:  
       case 0x1680:  
       case 0x180e:  
       case 0x2000:  
       case 0x2001:  
       case 0x2002:  
       case 0x2003:  
       case 0x2004:  
       case 0x2005:  
       case 0x2006:  
       case 0x2007:  
       case 0x2008:  
       case 0x2009:  
       case 0x200A:  
       case 0x202f:  
       case 0x205f:  
       case 0x3000:  
       return -next != ESC_h;  
3100        default:        default:
3101        return -next == ESC_h;        return escape == ESC_h;
3102        }        }
3103    
3104      case ESC_v:      case ESC_v:
3105      case ESC_V:      case ESC_V:
3106      switch(c)      switch(c)
3107        {        {
3108        case 0x0a:        VSPACE_CASES:
3109        case 0x0b:        return escape != ESC_v;
3110        case 0x0c:  
       case 0x0d:  
       case 0x85:  
       case 0x2028:  
       case 0x2029:  
       return -next != ESC_v;  
3111        default:        default:
3112        return -next == ESC_v;        return escape == ESC_v;
3113        }        }
3114    
3115      /* When PCRE_UCP is set, these values get generated for \d etc. Find      /* When PCRE_UCP is set, these values get generated for \d etc. Find
3116      their substitutions and process them. The result will always be either      their substitutions and process them. The result will always be either
3117      -ESC_p or -ESC_P. Then fall through to process those values. */      ESC_p or ESC_P. Then fall through to process those values. */
3118    
3119  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3120      case ESC_du:      case ESC_du:
# Line 3265  switch(op_code) Line 3125  switch(op_code)
3125      case ESC_SU:      case ESC_SU:
3126        {        {
3127        int temperrorcode = 0;        int temperrorcode = 0;
3128        ptr = substitutes[-next - ESC_DU];        ptr = substitutes[escape - ESC_DU];
3129        next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);        escape = check_escape(&ptr, &next, &temperrorcode, 0, options, FALSE);
3130        if (temperrorcode != 0) return FALSE;        if (temperrorcode != 0) return FALSE;
3131        ptr++;    /* For compatibility */        ptr++;    /* For compatibility */
3132        }        }
# Line 3275  switch(op_code) Line 3135  switch(op_code)
3135      case ESC_p:      case ESC_p:
3136      case ESC_P:      case ESC_P:
3137        {        {
3138        int ptype, pdata, errorcodeptr;        unsigned int ptype = 0, pdata = 0;
3139          int errorcodeptr;
3140        BOOL negated;        BOOL negated;
3141    
3142        ptr--;      /* Make ptr point at the p or P */        ptr--;      /* Make ptr point at the p or P */
3143        ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);        if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr))
3144        if (ptype < 0) return FALSE;          return FALSE;
3145        ptr++;      /* Point past the final curly ket */        ptr++;      /* Point past the final curly ket */
3146    
3147        /* If the property item is optional, we have to give up. (When generated        /* If the property item is optional, we have to give up. (When generated
# Line 3293  switch(op_code) Line 3154  switch(op_code)
3154    
3155        /* Do the property check. */        /* Do the property check. */
3156    
3157        return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);        return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);
3158        }        }
3159  #endif  #endif
3160    
# Line 3308  switch(op_code) Line 3169  switch(op_code)
3169    these op-codes are never generated.) */    these op-codes are never generated.) */
3170    
3171    case OP_DIGIT:    case OP_DIGIT:
3172    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||    return escape == ESC_D || escape == ESC_s || escape == ESC_W ||
3173           next == -ESC_h || next == -ESC_v || next == -ESC_R;           escape == ESC_h || escape == ESC_v || escape == ESC_R;
3174    
3175    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
3176    return next == -ESC_d;    return escape == ESC_d;
3177    
3178    case OP_WHITESPACE:    case OP_WHITESPACE:
3179    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;    return escape == ESC_S || escape == ESC_d || escape == ESC_w;
3180    
3181    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3182    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;
3183    
3184    case OP_HSPACE:    case OP_HSPACE:
3185    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||    return escape == ESC_S || escape == ESC_H || escape == ESC_d ||
3186           next == -ESC_w || next == -ESC_v || next == -ESC_R;           escape == ESC_w || escape == ESC_v || escape == ESC_R;
3187    
3188    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
3189    return next == -ESC_h;    return escape == ESC_h;
3190    
3191    /* Can't have \S in here because VT matches \S (Perl anomaly) */    /* Can't have \S in here because VT matches \S (Perl anomaly) */
3192    case OP_ANYNL:    case OP_ANYNL:
3193    case OP_VSPACE:    case OP_VSPACE:
3194    return next == -ESC_V || next == -ESC_d || next == -ESC_w;    return escape == ESC_V || escape == ESC_d || escape == ESC_w;
3195    
3196    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3197    return next == -ESC_v || next == -ESC_R;    return escape == ESC_v || escape == ESC_R;
3198    
3199    case OP_WORDCHAR:    case OP_WORDCHAR:
3200    return next == -ESC_W || next == -ESC_s || next == -ESC_h ||    return escape == ESC_W || escape == ESC_s || escape == ESC_h ||
3201           next == -ESC_v || next == -ESC_R;           escape == ESC_v || escape == ESC_R;
3202    
3203    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
3204    return next == -ESC_w || next == -ESC_d;    return escape == ESC_w || escape == ESC_d;
3205    
3206    default:    default:
3207    return FALSE;    return FALSE;
# Line 3352  switch(op_code) Line 3213  switch(op_code)
3213    
3214    
3215  /*************************************************  /*************************************************
3216    *        Add a character or range to a class     *
3217    *************************************************/
3218    
3219    /* This function packages up the logic of adding a character or range of
3220    characters to a class. The character values in the arguments will be within the
3221    valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3222    mutually recursive with the function immediately below.
3223    
3224    Arguments:
3225      classbits     the bit map for characters < 256
3226      uchardptr     points to the pointer for extra data
3227      options       the options word
3228      cd            contains pointers to tables etc.
3229      start         start of range character
3230      end           end of range character
3231    
3232    Returns:        the number of < 256 characters added
3233                    the pointer to extra data is updated
3234    */
3235    
3236    static int
3237    add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3238      compile_data *cd, pcre_uint32 start, pcre_uint32 end)
3239    {
3240    pcre_uint32 c;
3241    int n8 = 0;
3242    
3243    /* If caseless matching is required, scan the range and process alternate
3244    cases. In Unicode, there are 8-bit characters that have alternate cases that
3245    are greater than 255 and vice-versa. Sometimes we can just extend the original
3246    range. */
3247    
3248    if ((options & PCRE_CASELESS) != 0)
3249      {
3250    #ifdef SUPPORT_UCP
3251      if ((options & PCRE_UTF8) != 0)
3252        {
3253        int rc;
3254        pcre_uint32 oc, od;
3255    
3256        options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3257        c = start;
3258    
3259        while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3260          {
3261          /* Handle a single character that has more than one other case. */
3262    
3263          if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3264            PRIV(ucd_caseless_sets) + rc, oc);
3265    
3266          /* Do nothing if the other case range is within the original range. */
3267    
3268          else if (oc >= start && od <= end) continue;
3269    
3270          /* Extend the original range if there is overlap, noting that if oc < c, we
3271          can't have od > end because a subrange is always shorter than the basic
3272          range. Otherwise, use a recursive call to add the additional range. */
3273    
3274          else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3275          else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3276          else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3277          }
3278        }
3279      else
3280    #endif  /* SUPPORT_UCP */
3281    
3282      /* Not UTF-mode, or no UCP */
3283    
3284      for (c = start; c <= end && c < 256; c++)
3285        {
3286        SETBIT(classbits, cd->fcc[c]);
3287        n8++;
3288        }
3289      }
3290    
3291    /* Now handle the original range. Adjust the final value according to the bit
3292    length - this means that the same lists of (e.g.) horizontal spaces can be used
3293    in all cases. */
3294    
3295    #if defined COMPILE_PCRE8
3296    #ifdef SUPPORT_UTF
3297      if ((options & PCRE_UTF8) == 0)
3298    #endif
3299      if (end > 0xff) end = 0xff;
3300    
3301    #elif defined COMPILE_PCRE16
3302    #ifdef SUPPORT_UTF
3303      if ((options & PCRE_UTF16) == 0)
3304    #endif
3305      if (end > 0xffff) end = 0xffff;
3306    
3307    #endif /* COMPILE_PCRE[8|16] */
3308    
3309    /* If all characters are less than 256, use the bit map. Otherwise use extra
3310    data. */
3311    
3312    if (end < 0x100)
3313      {
3314      for (c = start; c <= end; c++)
3315        {
3316        n8++;
3317        SETBIT(classbits, c);
3318        }
3319      }
3320    
3321    else
3322      {
3323      pcre_uchar *uchardata = *uchardptr;
3324    
3325    #ifdef SUPPORT_UTF
3326      if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3327        {
3328        if (start < end)
3329          {
3330          *uchardata++ = XCL_RANGE;
3331          uchardata += PRIV(ord2utf)(start, uchardata);
3332          uchardata += PRIV(ord2utf)(end, uchardata);
3333          }
3334        else if (start == end)
3335          {
3336          *uchardata++ = XCL_SINGLE;
3337          uchardata += PRIV(ord2utf)(start, uchardata);
3338          }
3339        }
3340      else
3341    #endif  /* SUPPORT_UTF */
3342    
3343      /* Without UTF support, character values are constrained by the bit length,
3344      and can only be > 256 for 16-bit and 32-bit libraries. */
3345    
3346    #ifdef COMPILE_PCRE8
3347        {}
3348    #else
3349      if (start < end)
3350        {
3351        *uchardata++ = XCL_RANGE;
3352        *uchardata++ = start;
3353        *uchardata++ = end;
3354        }
3355      else if (start == end)
3356        {
3357        *uchardata++ = XCL_SINGLE;
3358        *uchardata++ = start;
3359        }
3360    #endif
3361    
3362      *uchardptr = uchardata;   /* Updata extra data pointer */
3363      }
3364    
3365    return n8;    /* Number of 8-bit characters */
3366    }
3367    
3368    
3369    
3370    
3371    /*************************************************
3372    *        Add a list of characters to a class     *
3373    *************************************************/
3374    
3375    /* This function is used for adding a list of case-equivalent characters to a
3376    class, and also for adding a list of horizontal or vertical whitespace. If the
3377    list is in order (which it should be), ranges of characters are detected and
3378    handled appropriately. This function is mutually recursive with the function
3379    above.
3380    
3381    Arguments:
3382      classbits     the bit map for characters < 256
3383      uchardptr     points to the pointer for extra data
3384      options       the options word
3385      cd            contains pointers to tables etc.
3386      p             points to row of 32-bit values, terminated by NOTACHAR
3387      except        character to omit; this is used when adding lists of
3388                      case-equivalent characters to avoid including the one we
3389                      already know about
3390    
3391    Returns:        the number of < 256 characters added
3392                    the pointer to extra data is updated
3393    */
3394    
3395    static int
3396    add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3397      compile_data *cd, const pcre_uint32 *p, unsigned int except)
3398    {
3399    int n8 = 0;
3400    while (p[0] < NOTACHAR)
3401      {
3402      int n = 0;
3403      if (p[0] != except)
3404        {
3405        while(p[n+1] == p[0] + n + 1) n++;
3406        n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3407        }
3408      p += n + 1;
3409      }
3410    return n8;
3411    }
3412    
3413    
3414    
3415    /*************************************************
3416    *    Add characters not in a list to a class     *
3417    *************************************************/
3418    
3419    /* This function is used for adding the complement of a list of horizontal or
3420    vertical whitespace to a class. The list must be in order.
3421    
3422    Arguments:
3423      classbits     the bit map for characters < 256
3424      uchardptr     points to the pointer for extra data
3425      options       the options word
3426      cd            contains pointers to tables etc.
3427      p             points to row of 32-bit values, terminated by NOTACHAR
3428    
3429    Returns:        the number of < 256 characters added
3430                    the pointer to extra data is updated
3431    */
3432    
3433    static int
3434    add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3435      int options, compile_data *cd, const pcre_uint32 *p)
3436    {
3437    BOOL utf = (options & PCRE_UTF8) != 0;
3438    int n8 = 0;
3439    if (p[0] > 0)
3440      n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
3441    while (p[0] < NOTACHAR)
3442      {
3443      while (p[1] == p[0] + 1) p++;
3444      n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3445        (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
3446      p++;
3447      }
3448    return n8;
3449    }
3450    
3451    
3452    
3453    /*************************************************
3454  *           Compile one branch                   *  *           Compile one branch                   *
3455  *************************************************/  *************************************************/
3456    
# Line 3362  to find out the amount of memory needed, Line 3461  to find out the amount of memory needed,
3461  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
3462    
3463  Arguments:  Arguments:
3464    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
3465    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
3466    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
3467    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
3468    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr      place to put the first required character
3469    reqcharptr     set to the last literal character required, else < 0    firstcharflagsptr place to put the first character flags, or a negative number
3470    bcptr          points to current branch chain    reqcharptr        place to put the last required character
3471    cond_depth     conditional nesting depth    reqcharflagsptr   place to put the last required character flags, or a negative number
3472    cd             contains pointers to tables etc.    bcptr             points to current branch chain
3473    lengthptr      NULL during the real compile phase    cond_depth        conditional nesting depth
3474                   points to length accumulator during pre-compile phase    cd                contains pointers to tables etc.
3475      lengthptr         NULL during the real compile phase
3476                        points to length accumulator during pre-compile phase
3477    
3478  Returns:         TRUE on success  Returns:            TRUE on success
3479                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
3480  */  */
3481    
3482  static BOOL  static BOOL
3483  compile_branch(int *optionsptr, pcre_uchar **codeptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3484    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,    const pcre_uchar **ptrptr, int *errorcodeptr,
3485    pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,    pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
3486      pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
3487      branch_chain *bcptr, int cond_depth,
3488    compile_data *cd, int *lengthptr)    compile_data *cd, int *lengthptr)
3489  {  {
3490  int repeat_type, op_type;  int repeat_type, op_type;
3491  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3492  int bravalue = 0;  int bravalue = 0;
3493  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3494  pcre_int32 firstchar, reqchar;  pcre_uint32 firstchar, reqchar;
3495  pcre_int32 zeroreqchar, zerofirstchar;  pcre_int32 firstcharflags, reqcharflags;
3496    pcre_uint32 zeroreqchar, zerofirstchar;
3497    pcre_int32 zeroreqcharflags, zerofirstcharflags;
3498  pcre_int32 req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
3499  int options = *optionsptr;               /* May change dynamically */  int options = *optionsptr;               /* May change dynamically */
3500  int after_manual_callout = 0;  int after_manual_callout = 0;
3501  int length_prevgroup = 0;  int length_prevgroup = 0;
3502  register int c;  register pcre_uint32 c;
3503    int escape;
3504  register pcre_uchar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3505  pcre_uchar *last_code = code;  pcre_uchar *last_code = code;
3506  pcre_uchar *orig_code = code;  pcre_uchar *orig_code = code;
# Line 3414  must not do this for other options (e.g. Line 3520  must not do this for other options (e.g.
3520  dynamically as we process the pattern. */  dynamically as we process the pattern. */
3521    
3522  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3523  /* PCRE_UTF16 has the same value as PCRE_UTF8. */  /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
3524  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
3525    #ifndef COMPILE_PCRE32
3526  pcre_uchar utf_chars[6];  pcre_uchar utf_chars[6];
3527    #endif
3528  #else  #else
3529  BOOL utf = FALSE;  BOOL utf = FALSE;
3530  #endif  #endif
3531    
3532  /* Helper variables for OP_XCLASS opcode (for characters > 255). */  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
3533    class_uchardata always so that it can be passed to add_to_class() always,
3534    though it will not be used in non-UTF 8-bit cases. This avoids having to supply
3535    alternative calls for the different cases. */
3536    
3537    pcre_uchar *class_uchardata;
3538  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3539  BOOL xclass;  BOOL xclass;
 pcre_uchar *class_uchardata;  
3540  pcre_uchar *class_uchardata_base;  pcre_uchar *class_uchardata_base;
3541  #endif  #endif
3542    
# Line 3448  to take the zero repeat into account. Th Line 3559  to take the zero repeat into account. Th
3559  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3560  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
3561    
3562  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
3563    firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
3564    
3565  /* The variable req_caseopt contains either the REQ_CASELESS value  /* The variable req_caseopt contains either the REQ_CASELESS value
3566  or zero, according to the current setting of the caseless flag. The  or zero, according to the current setting of the caseless flag. The
# Line 3469  for (;; ptr++) Line 3581  for (;; ptr++)
3581    BOOL is_recurse;    BOOL is_recurse;
3582    BOOL reset_bracount;    BOOL reset_bracount;
3583    int class_has_8bitchar;    int class_has_8bitchar;
3584    int class_single_char;    int class_one_char;
3585    int newoptions;    int newoptions;
3586    int recno;    int recno;
3587    int refsign;    int refsign;
3588    int skipbytes;    int skipbytes;
3589    int subreqchar;    pcre_uint32 subreqchar, subfirstchar;
3590    int subfirstchar;    pcre_int32 subreqcharflags, subfirstcharflags;
3591    int terminator;    int terminator;
3592    int mclength;    unsigned int mclength;
3593    int tempbracount;    unsigned int tempbracount;
3594      pcre_uint32 ec;
3595    pcre_uchar mcbuffer[8];    pcre_uchar mcbuffer[8];
3596    
3597    /* Get next character in the pattern */    /* Get next character in the pattern */
# Line 3488  for (;; ptr++) Line 3601  for (;; ptr++)
3601    /* If we are at the end of a nested substitution, revert to the outer level    /* If we are at the end of a nested substitution, revert to the outer level
3602    string. Nesting only happens one level deep. */    string. Nesting only happens one level deep. */
3603    
3604    if (c == 0 && nestptr != NULL)    if (c == CHAR_NULL && nestptr != NULL)
3605      {      {
3606      ptr = nestptr;      ptr = nestptr;
3607      nestptr = NULL;      nestptr = NULL;
# Line 3563  for (;; ptr++) Line 3676  for (;; ptr++)
3676    
3677    /* If in \Q...\E, check for the end; if not, we have a literal */    /* If in \Q...\E, check for the end; if not, we have a literal */
3678    
3679    if (inescq && c != 0)    if (inescq && c != CHAR_NULL)
3680      {      {
3681      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
3682        {        {
# Line 3611  for (;; ptr++) Line 3724  for (;; ptr++)
3724      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3725        {        {
3726        ptr++;        ptr++;
3727        while (*ptr != 0)        while (*ptr != CHAR_NULL)
3728          {          {
3729          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3730          ptr++;          ptr++;
# Line 3619  for (;; ptr++) Line 3732  for (;; ptr++)
3732          if (utf) FORWARDCHAR(ptr);          if (utf) FORWARDCHAR(ptr);
3733  #endif  #endif
3734          }          }
3735        if (*ptr != 0) continue;        if (*ptr != CHAR_NULL) continue;
3736    
3737        /* Else fall through to handle end of string */        /* Else fall through to handle end of string */
3738        c = 0;        c = 0;
# Line 3641  for (;; ptr++) Line 3754  for (;; ptr++)
3754      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
3755      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
3756      *firstcharptr = firstchar;      *firstcharptr = firstchar;
3757        *firstcharflagsptr = firstcharflags;
3758      *reqcharptr = reqchar;      *reqcharptr = reqchar;
3759        *reqcharflagsptr = reqcharflags;
3760      *codeptr = code;      *codeptr = code;
3761      *ptrptr = ptr;      *ptrptr = ptr;
3762      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 3665  for (;; ptr++) Line 3780  for (;; ptr++)
3780      previous = NULL;      previous = NULL;
3781      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3782        {        {
3783        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
3784        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
3785        }        }
3786      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 3680  for (;; ptr++) Line 3795  for (;; ptr++)
3795      repeats. The value of reqchar doesn't change either. */      repeats. The value of reqchar doesn't change either. */
3796    
3797      case CHAR_DOT:      case CHAR_DOT:
3798      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
3799      zerofirstchar = firstchar;      zerofirstchar = firstchar;
3800        zerofirstcharflags = firstcharflags;
3801      zeroreqchar = reqchar;      zeroreqchar = reqchar;
3802        zeroreqcharflags = reqcharflags;
3803      previous = code;      previous = code;
3804      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
3805      break;      break;
# Line 3756  for (;; ptr++) Line 3873  for (;; ptr++)
3873          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
3874        {        {
3875        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
3876        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
3877        zerofirstchar = firstchar;        zerofirstchar = firstchar;
3878          zerofirstcharflags = firstcharflags;
3879        break;        break;
3880        }        }
3881    
# Line 3767  for (;; ptr++) Line 3885  for (;; ptr++)
3885    
3886      should_flip_negation = FALSE;      should_flip_negation = FALSE;
3887    
3888      /* For optimization purposes, we track some properties of the class.      /* For optimization purposes, we track some properties of the class:
3889      class_has_8bitchar will be non-zero, if the class contains at least one      class_has_8bitchar will be non-zero if the class contains at least one <
3890      < 256 character. class_single_char will be 1 if the class contains only      256 character; class_one_char will be 1 if the class contains just one
3891      a single character. */      character. */
3892    
3893      class_has_8bitchar = 0;      class_has_8bitchar = 0;
3894      class_single_char = 0;      class_one_char = 0;
3895    
3896      /* 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
3897      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
3898      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
3899      */      map. */
3900    
3901      memset(classbits, 0, 32 * sizeof(pcre_uint8));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
3902    
3903  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3904      xclass = FALSE;                           /* No chars >= 256 */      xclass = FALSE;
3905      class_uchardata = code + LINK_SIZE + 2;   /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
3906      class_uchardata_base = class_uchardata;   /* For resetting in pass 1 */      class_uchardata_base = class_uchardata;   /* Save the start */
3907  #endif  #endif
3908    
3909      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
3910      means that an initial ] is taken as a data character. At the start of the      means that an initial ] is taken as a data character. At the start of the
3911      loop, c contains the first byte of the character. */      loop, c contains the first byte of the character. */
3912    
3913      if (c != 0) do      if (c != CHAR_NULL) do
3914        {        {
3915        const pcre_uchar *oldptr;        const pcre_uchar *oldptr;
3916    
# Line 3807  for (;; ptr++) Line 3925  for (;; ptr++)
3925        /* In the pre-compile phase, accumulate the length of any extra        /* In the pre-compile phase, accumulate the length of any extra
3926        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
3927        contain a zillion > 255 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
3928        (which is on the stack). */        (which is on the stack). We have to remember that there was XCLASS data,
3929          however. */
3930    
3931        if (lengthptr != NULL)        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
3932          {          {
3933            xclass = TRUE;
3934          *lengthptr += class_uchardata - class_uchardata_base;          *lengthptr += class_uchardata - class_uchardata_base;
3935          class_uchardata = class_uchardata_base;          class_uchardata = class_uchardata_base;
3936          }          }
# Line 3912  for (;; ptr++) Line 4032  for (;; ptr++)
4032              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4033            }            }
4034    
4035          /* Not see if we need to remove any special characters. An option          /* Now see if we need to remove any special characters. An option
4036          value of 1 removes vertical space and 2 removes underscore. */          value of 1 removes vertical space and 2 removes underscore. */
4037    
4038          if (tabopt < 0) tabopt = -tabopt;          if (tabopt < 0) tabopt = -tabopt;
# Line 3928  for (;; ptr++) Line 4048  for (;; ptr++)
4048            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4049    
4050          ptr = tempptr + 1;          ptr = tempptr + 1;
4051          /* Every class contains at least one < 256 characters. */          /* Every class contains at least one < 256 character. */
4052          class_has_8bitchar = 1;          class_has_8bitchar = 1;
4053          /* Every class contains at least two characters. */          /* Every class contains at least two characters. */
4054          class_single_char = 2;          class_one_char = 2;
4055          continue;    /* End of POSIX syntax handling */          continue;    /* End of POSIX syntax handling */
4056          }          }
4057    
# Line 3939  for (;; ptr++) Line 4059  for (;; ptr++)
4059        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
4060        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
4061        assume that other escapes have more than one character in them, so        assume that other escapes have more than one character in them, so
4062        speculatively set both class_has_8bitchar and class_single_char bigger        speculatively set both class_has_8bitchar and class_one_char bigger
4063        than one. Unrecognized escapes fall through and are either treated        than one. Unrecognized escapes fall through and are either treated
4064        as literal characters (by default), or are faulted if        as literal characters (by default), or are faulted if
4065        PCRE_EXTRA is set. */        PCRE_EXTRA is set. */
4066    
4067        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
4068          {          {
4069          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
4070              TRUE);
4071          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
4072            if (escape == 0) c = ec;
4073          if (-c == ESC_b) c = CHAR_BS;    /* \b is backspace in a class */          else if (escape == ESC_b) c = CHAR_BS; /* \b is backspace in a class */
4074          else if (-c == ESC_N)            /* \N is not supported in a class */          else if (escape == ESC_N)          /* \N is not supported in a class */
4075            {            {
4076            *errorcodeptr = ERR71;            *errorcodeptr = ERR71;
4077            goto FAILED;            goto FAILED;
4078            }            }
4079          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (escape == ESC_Q)            /* Handle start of quoted string */
4080            {            {
4081            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4082              {              {
# Line 3964  for (;; ptr++) Line 4085  for (;; ptr++)
4085            else inescq = TRUE;            else inescq = TRUE;
4086            continue;            continue;
4087            }            }
4088          else if (-c == ESC_E) continue;  /* Ignore orphan \E */          else if (escape == ESC_E) continue;  /* Ignore orphan \E */
4089    
4090          if (c < 0)          else
4091            {            {
4092            register const pcre_uint8 *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
4093            /* Every class contains at least two < 256 characters. */            /* Every class contains at least two < 256 characters. */
4094            class_has_8bitchar++;            class_has_8bitchar++;
4095            /* Every class contains at least two characters. */            /* Every class contains at least two characters. */
4096            class_single_char += 2;            class_one_char += 2;
4097    
4098            switch (-c)            switch (escape)
4099              {              {
4100  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4101              case ESC_du:     /* These are the values given for \d etc */              case ESC_du:     /* These are the values given for \d etc */
# Line 3984  for (;; ptr++) Line 4105  for (;; ptr++)
4105              case ESC_su:     /* of the default ASCII testing. */              case ESC_su:     /* of the default ASCII testing. */
4106              case ESC_SU:              case ESC_SU:
4107              nestptr = ptr;              nestptr = ptr;
4108              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */              ptr = substitutes[escape - ESC_DU] - 1;  /* Just before substitute */
4109              class_has_8bitchar--;                /* Undo! */              class_has_8bitchar--;                /* Undo! */
4110              continue;              continue;
4111  #endif  #endif
# Line 4008  for (;; ptr++) Line 4129  for (;; ptr++)
4129    
4130              /* 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
4131              if it was previously set by something earlier in the character              if it was previously set by something earlier in the character
4132              class. */              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and
4133                EBCDIC, so we lazily just adjust the appropriate bit. */
4134    
4135              case ESC_s:              case ESC_s:
4136              classbits[0] |= cbits[cbit_space];              classbits[0] |= cbits[cbit_space];
# Line 4022  for (;; ptr++) Line 4144  for (;; ptr++)
4144              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
4145              continue;              continue;
4146    
4147                /* The rest apply in both UCP and non-UCP cases. */
4148    
4149              case ESC_h:              case ESC_h:
4150              SETBIT(classbits, 0x09); /* VT */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4151              SETBIT(classbits, 0x20); /* SPACE */                PRIV(hspace_list), NOTACHAR);
             SETBIT(classbits, 0xa0); /* NSBP */  
 #ifndef COMPILE_PCRE8  
             xclass = TRUE;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x1680;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x180e;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x2000;  
             *class_uchardata++ = 0x200a;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x202f;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x205f;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x3000;  
 #elif defined SUPPORT_UTF  
             if (utf)  
               {  
               xclass = TRUE;  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x1680, class_uchardata);  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x180e, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x2000, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x200a, class_uchardata);  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x202f, class_uchardata);  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x205f, class_uchardata);  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x3000, class_uchardata);  
               }  
 #endif  
4152              continue;              continue;
4153    
4154              case ESC_H:              case ESC_H:
4155              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4156                {                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;  
               }  
 #ifndef COMPILE_PCRE8  
             xclass = TRUE;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x0100;  
             *class_uchardata++ = 0x167f;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x1681;  
             *class_uchardata++ = 0x180d;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x180f;  
             *class_uchardata++ = 0x1fff;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x200b;  
             *class_uchardata++ = 0x202e;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x2030;  
             *class_uchardata++ = 0x205e;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x2060;  
             *class_uchardata++ = 0x2fff;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x3001;  
 #ifdef SUPPORT_UTF  
             if (utf)  
               class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);  
             else  
 #endif  
               *class_uchardata++ = 0xffff;  
 #elif defined SUPPORT_UTF  
             if (utf)  
               {  
               xclass = TRUE;  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x0100, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x167f, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x1681, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x180d, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x180f, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x1fff, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x200b, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x202e, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x2030, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x205e, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x2060, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x2fff, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x3001, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);  
               }  
 #endif  
4157              continue;              continue;
4158    
4159              case ESC_v:              case ESC_v:
4160              SETBIT(classbits, 0x0a); /* LF */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4161              SETBIT(classbits, 0x0b); /* VT */                PRIV(vspace_list), NOTACHAR);
             SETBIT(classbits, 0x0c); /* FF */  
             SETBIT(classbits, 0x0d); /* CR */  
             SETBIT(classbits, 0x85); /* NEL */  
 #ifndef COMPILE_PCRE8  
             xclass = TRUE;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x2028;  
             *class_uchardata++ = 0x2029;  
 #elif defined SUPPORT_UTF  
             if (utf)  
               {  
               xclass = TRUE;  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x2028, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x2029, class_uchardata);  
               }  
 #endif  
4162              continue;              continue;
4163    
4164              case ESC_V:              case ESC_V:
4165              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4166                {                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;  
               }  
   
 #ifndef COMPILE_PCRE8  
             xclass = TRUE;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x0100;  
             *class_uchardata++ = 0x2027;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x202a;  
 #ifdef SUPPORT_UTF  
             if (utf)  
               class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);  
             else  
 #endif  
               *class_uchardata++ = 0xffff;  
 #elif defined SUPPORT_UTF  
             if (utf)  
               {  
               xclass = TRUE;  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x0100, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x2027, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x202a, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);  
               }  
 #endif  
4167              continue;              continue;
4168    
4169  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 4203  for (;; ptr++) Line 4171  for (;; ptr++)
4171              case ESC_P:              case ESC_P:
4172                {                {
4173                BOOL negated;                BOOL negated;
4174                int pdata;                unsigned int ptype = 0, pdata = 0;
4175                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
4176                if (ptype < 0) goto FAILED;                  goto FAILED;
4177                xclass = TRUE;                *class_uchardata++ = ((escape == ESC_p) != negated)?
               *class_uchardata++ = ((-c == ESC_p) != negated)?  
4178                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
4179                *class_uchardata++ = ptype;                *class_uchardata++ = ptype;
4180                *class_uchardata++ = pdata;                *class_uchardata++ = pdata;
# Line 4226  for (;; ptr++) Line 4193  for (;; ptr++)
4193                goto FAILED;                goto FAILED;
4194                }                }
4195              class_has_8bitchar--;    /* Undo the speculative increase. */              class_has_8bitchar--;    /* Undo the speculative increase. */
4196              class_single_char -= 2;  /* Undo the speculative increase. */              class_one_char -= 2;     /* Undo the speculative increase. */
4197              c = *ptr;                /* Get the final character and fall through */              c = *ptr;                /* Get the final character and fall through */
4198              break;              break;
4199              }              }
4200            }            }
4201    
4202          /* 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).
4203          greater than 256. */          This may be greater than 256. */
4204    
4205            escape = 0;
4206    
4207          }   /* End of backslash handling */          }   /* End of backslash handling */
4208    
4209        /* 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
4210        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
4211        at the end is treated as a literal. Perl ignores orphaned \E sequences        treated as a literal. Perl ignores orphaned \E sequences entirely. The
4212        entirely. The code for handling \Q and \E is messy. */        code for handling \Q and \E is messy. */
4213    
4214        CHECK_RANGE:        CHECK_RANGE:
4215        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 4248  for (;; ptr++) Line 4217  for (;; ptr++)
4217          inescq = FALSE;          inescq = FALSE;
4218          ptr += 2;          ptr += 2;
4219          }          }
   
4220        oldptr = ptr;        oldptr = ptr;
4221    
4222        /* Remember \r or \n */        /* Remember if \r or \n were explicitly used */
4223    
4224        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4225    
# Line 4259  for (;; ptr++) Line 4227  for (;; ptr++)
4227    
4228        if (!inescq && ptr[1] == CHAR_MINUS)        if (!inescq && ptr[1] == CHAR_MINUS)
4229          {          {
4230          int d;          pcre_uint32 d;
4231          ptr += 2;          ptr += 2;
4232          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;
4233    
# Line 4275  for (;; ptr++) Line 4243  for (;; ptr++)
4243            break;            break;
4244            }            }
4245    
4246          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          /* Minus (hyphen) at the end of a class is treated as a literal, so put
4247            back the pointer and jump to handle the character that preceded it. */
4248    
4249            if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4250            {            {
4251            ptr = oldptr;            ptr = oldptr;
4252            goto LONE_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4253            }            }
4254    
4255            /* Otherwise, we have a potential range; pick up the next character */
4256    
4257  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4258          if (utf)          if (utf)
4259            {                           /* Braces are required because the */            {                           /* Braces are required because the */
# Line 4296  for (;; ptr++) Line 4269  for (;; ptr++)
4269    
4270          if (!inescq && d == CHAR_BACKSLASH)          if (!inescq && d == CHAR_BACKSLASH)
4271            {            {
4272            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            int descape;
4273              descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
4274            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4275    
4276            /* \b is backspace; any other special means the '-' was literal */            /* \b is backspace; any other special means the '-' was literal. */
4277    
4278            if (d < 0)            if (descape != 0)
4279              {              {
4280              if (d == -ESC_b) d = CHAR_BS; else              if (descape == ESC_b) d = CHAR_BS; else
4281                {                {
4282                ptr = oldptr;                ptr = oldptr;
4283                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4284                }                }
4285              }              }
4286            }            }
4287    
4288          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
4289          one-character ranges */          one-character ranges. */
4290    
4291          if (d < c)          if (d < c)
4292            {            {
4293            *errorcodeptr = ERR8;            *errorcodeptr = ERR8;
4294            goto FAILED;            goto FAILED;
4295            }            }
4296            if (d == c) goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4297    
4298            /* We have found a character range, so single character optimizations
4299            cannot be done anymore. Any value greater than 1 indicates that there
4300            is more than one character. */
4301    
4302          if (d == c) goto LONE_SINGLE_CHARACTER;  /* A few lines below */          class_one_char = 2;
4303    
4304          /* Remember \r or \n */          /* Remember an explicit \r or \n, and add the range to the class. */
4305    
4306          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4307    
4308          /* Since we found a character range, single character optimizations          class_has_8bitchar +=
4309          cannot be done anymore. */            add_to_class(classbits, &class_uchardata, options, cd, c, d);
         class_single_char = 2;  
   
         /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless  
         matching, we have to use an XCLASS with extra data items. Caseless  
         matching for characters > 127 is available only if UCP support is  
         available. */  
   
 #if defined SUPPORT_UTF && !(defined COMPILE_PCRE8)  
         if ((d > 255) || (utf && ((options & PCRE_CASELESS) != 0 && d > 127)))  
 #elif defined  SUPPORT_UTF  
         if (utf && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))  
 #elif !(defined COMPILE_PCRE8)  
         if (d > 255)  
 #endif  
 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)  
           {  
           xclass = 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. */  
4310    
4311  #ifdef SUPPORT_UCP          continue;   /* Go get the next char in the class */
4312  #ifndef COMPILE_PCRE8          }
           if (utf && (options & PCRE_CASELESS) != 0)  
 #else  
           if ((options & PCRE_CASELESS) != 0)  
 #endif  
             {  
             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;  
                 }  
4313    
4314                if (occ == ocd)        /* Handle a single character - we can get here for a normal non-escape
4315                  {        char, or after \ that introduces a single character or for an apparent
4316                  *class_uchardata++ = XCL_SINGLE;        range that isn't. Only the value 1 matters for class_one_char, so don't
4317                  }        increase it if it is already 2 or more ... just in case there's a class
4318                else        with a zillion characters in it. */
4319                  {  
4320                  *class_uchardata++ = XCL_RANGE;        CLASS_SINGLE_CHARACTER:
4321                  class_uchardata += PRIV(ord2utf)(occ, class_uchardata);        if (class_one_char < 2) class_one_char++;
4322                  }  
4323                class_uchardata += PRIV(ord2utf)(ocd, class_uchardata);        /* If class_one_char is 1, we have the first single character in the
4324                }        class, and there have been no prior ranges, or XCLASS items generated by
4325              }        escapes. If this is the final character in the class, we can optimize by
4326  #endif  /* SUPPORT_UCP */        turning the item into a 1-character OP_CHAR[I] if it's positive, or
4327          OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
4328          to be set. Otherwise, there can be no first char if this item is first,
4329          whatever repeat count may follow. In the case of reqchar, save the
4330          previous value for reinstating. */
4331    
4332            /* Now record the original range, possibly modified for UCP caseless        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
4333            overlapping ranges. */          {
4334            ptr++;
4335            zeroreqchar = reqchar;
4336            zeroreqcharflags = reqcharflags;
4337    
4338            *class_uchardata++ = XCL_RANGE;          if (negate_class)
4339  #ifdef SUPPORT_UTF            {
4340  #ifndef COMPILE_PCRE8  #ifdef SUPPORT_UCP
4341            if (utf)            int d;
             {  
             class_uchardata += PRIV(ord2utf)(c, class_uchardata);  
             class_uchardata += PRIV(ord2utf)(d, class_uchardata);  
             }  
           else  
             {  
             *class_uchardata++ = c;  
             *class_uchardata++ = d;  
             }  
 #else  
           class_uchardata += PRIV(ord2utf)(c, class_uchardata);  
           class_uchardata += PRIV(ord2utf)(d, class_uchardata);  
4342  #endif  #endif
4343  #else /* SUPPORT_UTF */            if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4344            *class_uchardata++ = c;            zerofirstchar = firstchar;
4345            *class_uchardata++ = d;            zerofirstcharflags = firstcharflags;
 #endif /* SUPPORT_UTF */  
4346    
4347            /* With UCP support, we are done. Without UCP support, there is no            /* For caseless UTF-8 mode when UCP support is available, check
4348            caseless matching for UTF characters > 127; we can use the bit map            whether this character has more than one other case. If so, generate
4349            for the smaller ones. As for 16 bit characters without UTF, we            a special OP_NOTPROP item instead of OP_NOTI. */
           can still use  */  
4350    
4351  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4352  #ifndef COMPILE_PCRE8            if (utf && (options & PCRE_CASELESS) != 0 &&
4353            if (utf)                (d = UCD_CASESET(c)) != 0)
 #endif  
             continue;    /* With next character in the class */  
 #endif  /* SUPPORT_UCP */  
   
 #if defined SUPPORT_UTF && !defined(SUPPORT_UCP) && !(defined COMPILE_PCRE8)  
           if (utf)  
4354              {              {
4355              if ((options & PCRE_CASELESS) == 0 || c > 127) continue;              *code++ = OP_NOTPROP;
4356              /* Adjust upper limit and fall through to set up the map */              *code++ = PT_CLIST;
4357              d = 127;              *code++ = d;
4358              }              }
4359            else            else
4360              {  #endif
4361              if (c > 255) continue;            /* Char has only one other case, or UCP not available */
             /* Adjust upper limit and fall through to set up the map */  
             d = 255;  
             }  
 #elif defined SUPPORT_UTF && !defined(SUPPORT_UCP)  
           if ((options & PCRE_CASELESS) == 0 || c > 127) continue;  
           /* Adjust upper limit and fall through to set up the map */  
           d = 127;  
 #else  
           if (c > 255) continue;  
           /* Adjust upper limit and fall through to set up the map */  
           d = 255;  
 #endif  /* SUPPORT_UTF && !SUPPORT_UCP && !COMPILE_PCRE8 */  
           }  
 #endif  /* SUPPORT_UTF || !COMPILE_PCRE8 */  
   
         /* We use the bit map for 8 bit mode, or when the characters fall  
         partially or entirely to [0-255] ([0-127] for UCP) ranges. */  
   
         class_has_8bitchar = 1;  
   
         /* We can save a bit of time by skipping this in the pre-compile. */  
4362    
         if (lengthptr == NULL) for (; c <= d; c++)  
           {  
           classbits[c/8] |= (1 << (c&7));  
           if ((options & PCRE_CASELESS) != 0)  
4363              {              {
4364              int uc = cd->fcc[c]; /* flip case */              *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4365              classbits[uc/8] |= (1 << (uc&7));  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4366              }              if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4367            }                code += PRIV(ord2utf)(c, code);
4368                else
         continue;   /* Go get the next char in the class */  
         }  
   
       /* Handle a lone single character - we can get here for a normal  
       non-escape char, or after \ that introduces a single character or for an  
       apparent range that isn't. */  
   
       LONE_SINGLE_CHARACTER:  
   
       /* Only the value of 1 matters for class_single_char. */  
       if (class_single_char < 2) class_single_char++;  
   
       /* If class_charcount is 1, we saw precisely one character. As long as  
       there were no negated characters >= 128 and there was no use of \p or \P,  
       in other words, no use of any XCLASS features, we can optimize.  
   
       In UTF-8 mode, we can optimize the negative case only if there were no  
       characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR  
       operate on single-bytes characters only. This is an historical hangover.  
       Maybe one day we can tidy these opcodes to handle multi-byte characters.  
   
       The optimization throws away the bit map. We turn the item into a  
       1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.  
       Note that OP_NOT[I] does not support multibyte characters. In the positive  
       case, it can cause firstchar to be set. Otherwise, there can be no first  
       char if this item is first, whatever repeat count may follow. In the case  
       of reqchar, save the previous value for reinstating. */  
   
 #ifdef SUPPORT_UTF  
       if (class_single_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET  
         && (!utf || !negate_class || c < (MAX_VALUE_FOR_SINGLE_CHAR + 1)))  
 #else  
       if (class_single_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)  
4369  #endif  #endif
4370          {                *code++ = c;
4371          ptr++;              }
         zeroreqchar = reqchar;  
4372    
4373          /* The OP_NOT[I] opcodes work on single characters only. */            /* We are finished with this character class */
4374    
4375          if (negate_class)            goto END_CLASS;
           {  
           if (firstchar == REQ_UNSET) firstchar = REQ_NONE;  
           zerofirstchar = firstchar;  
           *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;  
           *code++ = c;  
           goto NOT_CHAR;  
4376            }            }
4377    
4378          /* For a single, positive character, get the value into mcbuffer, and          /* For a single, positive character, get the value into mcbuffer, and
4379          then we can handle this with the normal one-character code. */          then we can handle this with the normal one-character code. */
4380    
4381  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4382          if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)          if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4383            mclength = PRIV(ord2utf)(c, mcbuffer);            mclength = PRIV(ord2utf)(c, mcbuffer);
4384          else          else
# Line 4533  for (;; ptr++) Line 4387  for (;; ptr++)
4387            mcbuffer[0] = c;            mcbuffer[0] = c;
4388            mclength = 1;            mclength = 1;
4389            }            }
4390          goto ONE_CHAR;          goto ONE_CHAR;
4391          }       /* End of 1-char optimization */          }       /* End of 1-char optimization */
   
       /* Handle a character that cannot go in the bit map. */  
   
 #if defined SUPPORT_UTF && !(defined COMPILE_PCRE8)  
       if ((c > 255) || (utf && ((options & PCRE_CASELESS) != 0 && c > 127)))  
 #elif defined SUPPORT_UTF  
       if (utf && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127)))  
 #elif !(defined COMPILE_PCRE8)  
       if (c > 255)  
 #endif  
   
 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)  
         {  
         xclass = TRUE;  
         *class_uchardata++ = XCL_SINGLE;  
 #ifdef SUPPORT_UTF  
 #ifndef COMPILE_PCRE8  
         /* In non 8 bit mode, we can get here even if we are not in UTF mode. */  
         if (!utf)  
           *class_uchardata++ = c;  
         else  
 #endif  
           class_uchardata += PRIV(ord2utf)(c, class_uchardata);  
 #else /* SUPPORT_UTF */  
         *class_uchardata++ = c;  
 #endif /* SUPPORT_UTF */  
   
 #ifdef SUPPORT_UCP  
 #ifdef COMPILE_PCRE8  
         if ((options & PCRE_CASELESS) != 0)  
 #else  
         /* In non 8 bit mode, we can get here even if we are not in UTF mode. */  
         if (utf && (options & PCRE_CASELESS) != 0)  
 #endif  
           {  
           unsigned int othercase;  
           if ((othercase = UCD_OTHERCASE(c)) != c)  
             {  
             *class_uchardata++ = XCL_SINGLE;  
             class_uchardata += PRIV(ord2utf)(othercase, class_uchardata);  
             }  
           }  
 #endif  /* SUPPORT_UCP */  
4392    
4393          }        /* There is more than one character in the class, or an XCLASS item
4394        else        has been generated. Add this character to the class. */
 #endif  /* SUPPORT_UTF || COMPILE_PCRE16 */  
4395    
4396        /* Handle a single-byte character */        class_has_8bitchar +=
4397          {          add_to_class(classbits, &class_uchardata, options, cd, c, c);
         class_has_8bitchar = 1;  
         classbits[c/8] |= (1 << (c&7));  
         if ((options & PCRE_CASELESS) != 0)  
           {  
           c = cd->fcc[c]; /* flip case */  
           classbits[c/8] |= (1 << (c&7));  
           }  
         }  
4398        }        }
4399    
4400      /* 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.
4401      If we are at the end of an internal nested string, revert to the outer      If we are at the end of an internal nested string, revert to the outer
4402      string. */      string. */
4403    
4404      while (((c = *(++ptr)) != 0 ||      while (((c = *(++ptr)) != CHAR_NULL ||
4405             (nestptr != NULL &&             (nestptr != NULL &&
4406               (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != 0)) &&               (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != CHAR_NULL)) &&
4407             (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));             (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
4408    
4409      /* Check for missing terminating ']' */      /* Check for missing terminating ']' */
4410    
4411      if (c == 0)      if (c == CHAR_NULL)
4412        {        {
4413        *errorcodeptr = ERR6;        *errorcodeptr = ERR6;
4414        goto FAILED;        goto FAILED;
4415        }        }
4416    
4417        /* We will need an XCLASS if data has been placed in class_uchardata. In
4418        the second phase this is a sufficient test. However, in the pre-compile
4419        phase, class_uchardata gets emptied to prevent workspace overflow, so it
4420        only if the very last character in the class needs XCLASS will it contain
4421        anything at this point. For this reason, xclass gets set TRUE above when
4422        uchar_classdata is emptied, and that's why this code is the way it is here
4423        instead of just doing a test on class_uchardata below. */
4424    
4425    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4426        if (class_uchardata > class_uchardata_base) xclass = TRUE;
4427    #endif
4428    
4429      /* If this is the first thing in the branch, there can be no first char      /* If this is the first thing in the branch, there can be no first char
4430      setting, whatever the repeat count. Any reqchar setting must remain      setting, whatever the repeat count. Any reqchar setting must remain
4431      unchanged after any kind of repeat. */      unchanged after any kind of repeat. */
4432    
4433      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4434      zerofirstchar = firstchar;      zerofirstchar = firstchar;
4435        zerofirstcharflags = firstcharflags;
4436      zeroreqchar = reqchar;      zeroreqchar = reqchar;
4437        zeroreqcharflags = reqcharflags;
4438    
4439      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
4440      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 4674  for (;; ptr++) Line 4490  for (;; ptr++)
4490        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
4491        }        }
4492      code += 32 / sizeof(pcre_uchar);      code += 32 / sizeof(pcre_uchar);
4493      NOT_CHAR:  
4494        END_CLASS:
4495      break;      break;
4496    
4497    
# Line 4712  for (;; ptr++) Line 4529  for (;; ptr++)
4529      if (repeat_min == 0)      if (repeat_min == 0)
4530        {        {
4531        firstchar = zerofirstchar;    /* Adjust for zero repeat */        firstchar = zerofirstchar;    /* Adjust for zero repeat */
4532          firstcharflags = zerofirstcharflags;
4533        reqchar = zeroreqchar;        /* Ditto */        reqchar = zeroreqchar;        /* Ditto */
4534          reqcharflags = zeroreqcharflags;
4535        }        }
4536    
4537      /* Remember whether this is a variable length repeat */      /* Remember whether this is a variable length repeat */
# Line 4775  for (;; ptr++) Line 4594  for (;; ptr++)
4594    
4595      /* Now handle repetition for the different types of item. */      /* Now handle repetition for the different types of item. */
4596    
4597      /* If previous was a character match, abolish the item and generate a      /* If previous was a character or negated character match, abolish the item
4598      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
4599      that it is set in reqchar - 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
4600      the first thing in a branch because the x will have gone into firstchar      such as x{3} is the first thing in a branch because the x will have gone
4601      instead.  */      into firstchar instead.  */
4602    
4603      if (*previous == OP_CHAR || *previous == OP_CHARI)      if (*previous == OP_CHAR || *previous == OP_CHARI
4604        {          || *previous == OP_NOT || *previous == OP_NOTI)
4605        op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;        {
4606          switch (*previous)
4607            {
4608            default: /* Make compiler happy. */
4609            case OP_CHAR:  op_type = OP_STAR - OP_STAR; break;
4610            case OP_CHARI: op_type = OP_STARI - OP_STAR; break;
4611            case OP_NOT:   op_type = OP_NOTSTAR - OP_STAR; break;
4612            case OP_NOTI:  op_type = OP_NOTSTARI - OP_STAR; break;
4613            }
4614    
4615        /* Deal with UTF characters that take up more than one character. It's        /* Deal with UTF characters that take up more than one character. It's
4616        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
4617        hold the length of the character in bytes, plus UTF_LENGTH to flag that        hold the length of the character in bytes, plus UTF_LENGTH to flag that
4618        it's a length rather than a small character. */        it's a length rather than a small character. */
4619    
4620  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4621        if (utf && NOT_FIRSTCHAR(code[-1]))        if (utf && NOT_FIRSTCHAR(code[-1]))
4622          {          {
4623          pcre_uchar *lastchar = code - 1;          pcre_uchar *lastchar = code - 1;
# Line 4806  for (;; ptr++) Line 4633  for (;; ptr++)
4633        with UTF disabled, or for a single character UTF character. */        with UTF disabled, or for a single character UTF character. */
4634          {          {
4635          c = code[-1];          c = code[-1];
4636          if (repeat_min > 1) reqchar = c | req_caseopt | cd->req_varyopt;          if (*previous <= OP_CHARI && repeat_min > 1)
4637              {
4638              reqchar = c;
4639              reqcharflags = req_caseopt | cd->req_varyopt;
4640              }
4641          }          }
4642    
4643        /* 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 4825  for (;; ptr++) Line 4656  for (;; ptr++)
4656        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
4657        }        }
4658    
     /* 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, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
       goto OUTPUT_SINGLE_REPEAT;  
       }  
   
4659      /* 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
4660      create a suitable repeat item. The code is shared with single-character      create a suitable repeat item. The code is shared with single-character
4661      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 4883  for (;; ptr++) Line 4694  for (;; ptr++)
4694    
4695        if (repeat_max == 0) goto END_REPEAT;        if (repeat_max == 0) goto END_REPEAT;
4696    
       /*--------------------------------------------------------------------*/  
       /* This code is obsolete from release 8.00; the restriction was finally  
       removed: */  
   
       /* All real repeats make it impossible to handle partial matching (maybe  
       one day we will be able to remove this restriction). */  
   
       /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */  
       /*--------------------------------------------------------------------*/  
   
4697        /* Combine the op_type with the repeat_type */        /* Combine the op_type with the repeat_type */
4698    
4699        repeat_type += op_type;        repeat_type += op_type;
# Line 4945  for (;; ptr++) Line 4746  for (;; ptr++)
4746    
4747          if (repeat_max < 0)          if (repeat_max < 0)
4748            {            {
4749  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4750            if (utf && (c & UTF_LENGTH) != 0)            if (utf && (c & UTF_LENGTH) != 0)
4751              {              {
4752              memcpy(code, utf_chars, IN_UCHARS(c & 7));              memcpy(code, utf_chars, IN_UCHARS(c & 7));
# Line 4970  for (;; ptr++) Line 4771  for (;; ptr++)
4771    
4772          else if (repeat_max != repeat_min)          else if (repeat_max != repeat_min)
4773            {            {
4774  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4775            if (utf && (c & UTF_LENGTH) != 0)            if (utf && (c & UTF_LENGTH) != 0)
4776              {              {
4777              memcpy(code, utf_chars, IN_UCHARS(c & 7));              memcpy(code, utf_chars, IN_UCHARS(c & 7));
# Line 5000  for (;; ptr++) Line 4801  for (;; ptr++)
4801    
4802        /* The character or character type itself comes last in all cases. */        /* The character or character type itself comes last in all cases. */
4803    
4804  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4805        if (utf && (c & UTF_LENGTH) != 0)        if (utf && (c & UTF_LENGTH) != 0)
4806          {          {
4807          memcpy(code, utf_chars, IN_UCHARS(c & 7));          memcpy(code, utf_chars, IN_UCHARS(c & 7));
# Line 5039  for (;; ptr++) Line 4840  for (;; ptr++)
4840          goto END_REPEAT;          goto END_REPEAT;
4841          }          }
4842    
       /*--------------------------------------------------------------------*/  
       /* This code is obsolete from release 8.00; the restriction was finally  
       removed: */  
   
       /* All real repeats make it impossible to handle partial matching (maybe  
       one day we will be able to remove this restriction). */  
   
       /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */  
       /*--------------------------------------------------------------------*/  
   
4843        if (repeat_min == 0 && repeat_max == -1)        if (repeat_min == 0 && repeat_max == -1)
4844          *code++ = OP_CRSTAR + repeat_type;          *code++ = OP_CRSTAR + repeat_type;
4845        else if (repeat_min == 1 && repeat_max == -1)        else if (repeat_min == 1 && repeat_max == -1)
# Line 5208  for (;; ptr++) Line 4999  for (;; ptr++)
4999    
5000            else            else
5001              {              {
5002              if (groupsetfirstchar && reqchar < 0) reqchar = firstchar;              if (groupsetfirstchar && reqcharflags < 0)
5003                  {
5004                  reqchar = firstchar;
5005                  reqcharflags = firstcharflags;
5006                  }
5007    
5008              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
5009                {                {
# Line 5387  for (;; ptr++) Line 5182  for (;; ptr++)
5182              pcre_uchar *scode = bracode;              pcre_uchar *scode = bracode;
5183              do              do
5184                {                {
5185                if (could_be_empty_branch(scode, ketcode, utf, cd))                if (could_be_empty_branch(scode, ketcode, utf, cd, NULL))
5186                  {                  {
5187                  *bracode += OP_SBRA - OP_BRA;                  *bracode += OP_SBRA - OP_BRA;
5188                  break;                  break;
# Line 5583  for (;; ptr++) Line 5378  for (;; ptr++)
5378        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
5379          {          {
5380          arg = ++ptr;          arg = ++ptr;
5381          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5382          arglen = (int)(ptr - arg);          arglen = (int)(ptr - arg);
5383            if ((unsigned int)arglen > MAX_MARK)
5384              {
5385              *errorcodeptr = ERR75;
5386              goto FAILED;
5387              }
5388          }          }
5389    
5390        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
# Line 5600  for (;; ptr++) Line 5400  for (;; ptr++)
5400          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
5401              STRNCMP_UC_C8(name, vn, namelen) == 0)              STRNCMP_UC_C8(name, vn, namelen) == 0)
5402            {            {
5403              int setverb;
5404    
5405            /* Check for open captures before ACCEPT and convert it to            /* Check for open captures before ACCEPT and convert it to
5406            ASSERT_ACCEPT if in an assertion. */            ASSERT_ACCEPT if in an assertion. */
5407    
# Line 5617  for (;; ptr++) Line 5419  for (;; ptr++)
5419                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5420                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5421                }                }
5422              *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;              setverb = *code++ =
5423                  (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5424    
5425              /* Do not set firstchar after *ACCEPT */              /* Do not set firstchar after *ACCEPT */
5426              if (firstchar == REQ_UNSET) firstchar = REQ_NONE;              if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
5427              }              }
5428    
5429            /* Handle other cases with/without an argument */            /* Handle other cases with/without an argument */
# Line 5632  for (;; ptr++) Line 5435  for (;; ptr++)
5435                *errorcodeptr = ERR66;                *errorcodeptr = ERR66;
5436                goto FAILED;                goto FAILED;
5437                }                }
5438              *code = verbs[i].op;              setverb = *code++ = verbs[i].op;
             if (*code++ == OP_THEN) cd->external_flags |= PCRE_HASTHEN;  
5439              }              }
5440    
5441            else            else
# Line 5643  for (;; ptr++) Line 5445  for (;; ptr++)
5445                *errorcodeptr = ERR59;                *errorcodeptr = ERR59;
5446                goto FAILED;                goto FAILED;
5447                }                }
5448              *code = verbs[i].op_arg;              setverb = *code++ = verbs[i].op_arg;
             if (*code++ == OP_THEN_ARG) cd->external_flags |= PCRE_HASTHEN;  
5449              *code++ = arglen;              *code++ = arglen;
5450              memcpy(code, arg, IN_UCHARS(arglen));              memcpy(code, arg, IN_UCHARS(arglen));
5451              code += arglen;              code += arglen;
5452              *code++ = 0;              *code++ = 0;
5453              }              }
5454    
5455              switch (setverb)
5456                {
5457                case OP_THEN:
5458                case OP_THEN_ARG:
5459                cd->external_flags |= PCRE_HASTHEN;
5460                break;
5461    
5462                case OP_PRUNE:
5463                case OP_PRUNE_ARG:
5464                case OP_SKIP:
5465                case OP_SKIP_ARG:
5466                cd->had_pruneorskip = TRUE;
5467                break;
5468                }
5469    
5470            break;  /* Found verb, exit loop */            break;  /* Found verb, exit loop */
5471            }            }
5472    
# Line 5676  for (;; ptr++) Line 5492  for (;; ptr++)
5492          {          {
5493          case CHAR_NUMBER_SIGN:                 /* Comment; skip to ket */          case CHAR_NUMBER_SIGN:                 /* Comment; skip to ket */
5494          ptr++;          ptr++;
5495          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5496          if (*ptr == 0)          if (*ptr == CHAR_NULL)
5497            {            {
5498            *errorcodeptr = ERR18;            *errorcodeptr = ERR18;
5499            goto FAILED;            goto FAILED;
# Line 5700  for (;; ptr++) Line 5516  for (;; ptr++)
5516          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5517          case CHAR_LEFT_PARENTHESIS:          case CHAR_LEFT_PARENTHESIS:
5518          bravalue = OP_COND;       /* Conditional group */          bravalue = OP_COND;       /* Conditional group */
5519            tempptr = ptr;
5520    
5521          /* A condition can be an assertion, a number (referring to a numbered          /* A condition can be an assertion, a number (referring to a numbered
5522          group), a name (referring to a named group), or 'R', referring to          group), a name (referring to a named group), or 'R', referring to
# Line 5712  for (;; ptr++) Line 5529  for (;; ptr++)
5529          be the recursive thing or the name 'R' (and similarly for 'R' followed          be the recursive thing or the name 'R' (and similarly for 'R' followed
5530          by digits), and (b) a number could be a name that consists of digits.          by digits), and (b) a number could be a name that consists of digits.
5531          In both cases, we look for a name first; if not found, we try the other          In both cases, we look for a name first; if not found, we try the other
5532          cases. */          cases.
5533    
5534            For compatibility with auto-callouts, we allow a callout to be
5535            specified before a condition that is an assertion. First, check for the
5536            syntax of a callout; if found, adjust the temporary pointer that is
5537            used to check for an assertion condition. That's all that is needed! */
5538    
5539            if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
5540              {
5541              for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
5542              if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
5543                tempptr += i + 1;
5544              }
5545    
5546          /* For conditions that are assertions, check the syntax, and then exit          /* For conditions that are assertions, check the syntax, and then exit
5547          the switch. This will take control down to where bracketed groups,          the switch. This will take control down to where bracketed groups,
5548          including assertions, are processed. */          including assertions, are processed. */
5549    
5550          if (ptr[1] == CHAR_QUESTION_MARK && (ptr[2] == CHAR_EQUALS_SIGN ||          if (tempptr[1] == CHAR_QUESTION_MARK &&
5551              ptr[2] == CHAR_EXCLAMATION_MARK || ptr[2] == CHAR_LESS_THAN_SIGN))                (tempptr[2] == CHAR_EQUALS_SIGN ||
5552                   tempptr[2] == CHAR_EXCLAMATION_MARK ||
5553                   tempptr[2] == CHAR_LESS_THAN_SIGN))
5554            break;            break;
5555    
5556          /* Most other conditions use OP_CREF (a couple change to OP_RREF          /* Most other conditions use OP_CREF (a couple change to OP_RREF
# Line 5753  for (;; ptr++) Line 5584  for (;; ptr++)
5584            }            }
5585          else          else
5586            {            {
5587            terminator = 0;            terminator = CHAR_NULL;
5588            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);
5589            }            }
5590    
# Line 5773  for (;; ptr++) Line 5604  for (;; ptr++)
5604          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
5605            {            {
5606            if (recno >= 0)            if (recno >= 0)
5607              recno = (IS_DIGIT(*ptr))? recno * 10 + *ptr - CHAR_0 : -1;              recno = (IS_DIGIT(*ptr))? recno * 10 + (int)(*ptr - CHAR_0) : -1;
5608            ptr++;            ptr++;
5609            }            }
5610          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
5611    
5612          if ((terminator > 0 && *ptr++ != terminator) ||          if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
5613              *ptr++ != CHAR_RIGHT_PARENTHESIS)              *ptr++ != CHAR_RIGHT_PARENTHESIS)
5614            {            {
5615            ptr--;      /* Error offset */            ptr--;      /* Error offset */
# Line 5825  for (;; ptr++) Line 5656  for (;; ptr++)
5656            slot += cd->name_entry_size;            slot += cd->name_entry_size;
5657            }            }
5658    
5659          /* Found a previous named subpattern */          /* Found the named subpattern */
5660    
5661          if (i < cd->names_found)          if (i < cd->names_found)
5662            {            {
# Line 5834  for (;; ptr++) Line 5665  for (;; ptr++)
5665            code[1+LINK_SIZE]++;            code[1+LINK_SIZE]++;
5666            }            }
5667    
5668          /* Search the pattern for a forward reference */          /* If terminator == CHAR_NULL it means that the name followed directly
5669            after the opening parenthesis [e.g. (?(abc)...] and in this case there
5670          else if ((i = find_parens(cd, name, namelen,          are some further alternatives to try. For the cases where terminator !=
5671                          (options & PCRE_EXTENDED) != 0, utf)) > 0)          0 [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have
           {  
           PUT2(code, 2+LINK_SIZE, i);  
           code[1+LINK_SIZE]++;  
           }  
   
         /* If terminator == 0 it means that the name followed directly after  
         the opening parenthesis [e.g. (?(abc)...] and in this case there are  
         some further alternatives to try. For the cases where terminator != 0  
         [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have  
5672          now checked all the possibilities, so give an error. */          now checked all the possibilities, so give an error. */
5673    
5674          else if (terminator != 0)          else if (terminator != CHAR_NULL)
5675            {            {
5676            *errorcodeptr = ERR15;            *errorcodeptr = ERR15;
5677            goto FAILED;            goto FAILED;
# Line 6006  for (;; ptr++) Line 5828  for (;; ptr++)
5828          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5829          DEFINE_NAME:    /* Come here from (?< handling */          DEFINE_NAME:    /* Come here from (?< handling */
5830          case CHAR_APOSTROPHE:          case CHAR_APOSTROPHE:
5831            {          terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
5832            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
5833              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;          name = ++ptr;
           name = ++ptr;  
5834    
5835            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5836            namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
5837    
5838            /* In the pre-compile phase, just do a syntax check. */          /* In the pre-compile phase, do a syntax check, remember the longest
5839            name, and then remember the group in a vector, expanding it if
5840            necessary. Duplicates for the same number are skipped; other duplicates
5841            are checked for validity. In the actual compile, there is nothing to
5842            do. */
5843    
5844            if (lengthptr != NULL)          if (lengthptr != NULL)
5845              {
5846              named_group *ng;
5847              pcre_uint32 number = cd->bracount + 1;
5848    
5849              if (*ptr != (pcre_uchar)terminator)
5850              {              {
5851              if (*ptr != terminator)              *errorcodeptr = ERR42;
5852                {              goto FAILED;
5853                *errorcodeptr = ERR42;              }
5854                goto FAILED;  
5855                }            if (cd->names_found >= MAX_NAME_COUNT)
5856              if (cd->names_found >= MAX_NAME_COUNT)              {
5857                *errorcodeptr = ERR49;
5858                goto FAILED;
5859                }
5860    
5861              if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
5862                {
5863                cd->name_entry_size = namelen + IMM2_SIZE + 1;
5864                if (namelen > MAX_NAME_SIZE)
5865                {                {
5866                *errorcodeptr = ERR49;                *errorcodeptr = ERR48;
5867                goto FAILED;                goto FAILED;
5868                }                }
             if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)  
               {  
               cd->name_entry_size = namelen + IMM2_SIZE + 1;  
               if (namelen > MAX_NAME_SIZE)  
                 {  
                 *errorcodeptr = ERR48;  
                 goto FAILED;  
                 }  
               }  
5869              }              }
5870    
5871            /* In the real compile, create the entry in the table, maintaining            /* Scan the list to check for duplicates. For duplicate names, if the
5872            alphabetical order. Duplicate names for different numbers are            number is the same, break the loop, which causes the name to be
5873            permitted only if PCRE_DUPNAMES is set. Duplicate names for the same            discarded; otherwise, if DUPNAMES is not set, give an error.
5874            number are always OK. (An existing number can be re-used if (?|            If it is set, allow the name with a different number, but continue
5875            appears in the pattern.) In either event, a duplicate name results in            scanning in case this is a duplicate with the same number. For
5876            a duplicate entry in the table, even if the number is the same. This            non-duplicate names, give an error if the number is duplicated. */
5877            is because the number of names, and hence the table size, is computed  
5878            in the pre-compile, and it affects various numbers and pointers which            ng = cd->named_groups;
5879            would all have to be modified, and the compiled code moved down, if            for (i = 0; i < cd->names_found; i++, ng++)
5880            duplicates with the same number were omitted from the table. This              {
5881            doesn't seem worth the hassle. However, *different* names for the              if (namelen == ng->length &&
5882            same number are not permitted. */                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)
5883                  {
5884            else                if (ng->number == number) break;
5885              {                if ((options & PCRE_DUPNAMES) == 0)
             BOOL dupname = FALSE;  
             slot = cd->name_table;  
   
             for (i = 0; i < cd->names_found; i++)  
               {  
               int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(namelen));  
               if (crc == 0)  
                 {  
                 if (slot[IMM2_SIZE+namelen] == 0)  
                   {  
                   if (GET2(slot, 0) != cd->bracount + 1 &&  
                       (options & PCRE_DUPNAMES) == 0)  
                     {  
                     *errorcodeptr = ERR43;  
                     goto FAILED;  
                     }  
                   else dupname = TRUE;  
                   }  
                 else crc = -1;      /* Current name is a substring */  
                 }  
   
               /* Make space in the table and break the loop for an earlier  
               name. For a duplicate or later name, carry on. We do this for  
               duplicates so that in the simple case (when ?(| is not used) they  
               are in order of their numbers. */  
   
               if (crc < 0)  
5886                  {                  {
5887                  memmove(slot + cd->name_entry_size, slot,                  *errorcodeptr = ERR43;
5888                    IN_UCHARS((cd->names_found - i) * cd->name_entry_size));                  goto FAILED;
5889                  break;                  }
5890                  }                }
5891                else if (ng->number == number)
5892                /* Continue the loop for a later or duplicate name */                {
5893                  *errorcodeptr = ERR65;
5894                slot += cd->name_entry_size;                goto FAILED;
5895                }                }
5896                }
             /* For non-duplicate names, check for a duplicate number before  
             adding the new name. */  
5897    
5898              if (!dupname)            if (i >= cd->names_found)     /* Not a duplicate with same number */
5899                {
5900                /* Increase the list size if necessary */
5901    
5902                if (cd->names_found >= cd->named_group_list_size)
5903                {                {
5904                pcre_uchar *cslot = cd->name_table;                int newsize = cd->named_group_list_size * 2;
5905                for (i = 0; i < cd->names_found; i++)                named_group *newspace = (PUBL(malloc))
5906                    (newsize * sizeof(named_group));
5907    
5908                  if (newspace == NULL)
5909                  {                  {
5910                  if (cslot != slot)                  *errorcodeptr = ERR21;
5911                    {                  goto FAILED;
5912                    if (GET2(cslot, 0) == cd->bracount + 1)                  }
5913                      {  
5914                      *errorcodeptr = ERR65;                memcpy(newspace, cd->named_groups,
5915                      goto FAILED;                  cd->named_group_list_size * sizeof(named_group));
5916                      }                if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
5917                    }                  (PUBL(free))((void *)cd->named_groups);
5918                  else i--;                cd->named_groups = newspace;
5919                  cslot += cd->name_entry_size;                cd->named_group_list_size = newsize;
5920                  }                }
5921                }  
5922                cd->named_groups[cd->names_found].name = name;
5923              PUT2(slot, 0, cd->bracount + 1);              cd->named_groups[cd->names_found].length = namelen;
5924              memcpy(slot + IMM2_SIZE, name, IN_UCHARS(namelen));              cd->named_groups[cd->names_found].number = number;
5925              slot[IMM2_SIZE + namelen] = 0;              cd->names_found++;
5926              }              }
5927            }            }
5928    
5929          /* In both pre-compile and compile, count the number of names we've          ptr++;                    /* Move past > or ' in both passes. */
         encountered. */  
   
         cd->names_found++;  
         ptr++;                    /* Move past > or ' */  
5930          goto NUMBERED_GROUP;          goto NUMBERED_GROUP;
5931    
5932    
# Line 6153  for (;; ptr++) Line 5956  for (;; ptr++)
5956    
5957          if (lengthptr != NULL)          if (lengthptr != NULL)
5958            {            {
5959            const pcre_uchar *temp;            named_group *ng;
5960    
5961            if (namelen == 0)            if (namelen == 0)
5962              {              {
5963              *errorcodeptr = ERR62;              *errorcodeptr = ERR62;
5964              goto FAILED;              goto FAILED;
5965              }              }
5966            if (*ptr != terminator)            if (*ptr != (pcre_uchar)terminator)
5967              {              {
5968              *errorcodeptr = ERR42;              *errorcodeptr = ERR42;
5969              goto FAILED;              goto FAILED;
# Line 6171  for (;; ptr++) Line 5974  for (;; ptr++)
5974              goto FAILED;              goto FAILED;
5975              }              }
5976    
5977            /* The name table does not exist in the first pass, so we cannot            /* The name table does not exist in the first pass; instead we must
5978            do a simple search as in the code below. Instead, we have to scan the            scan the list of names encountered so far in order to get the
5979            pattern to find the number. It is important that we scan it only as            number. The number may be negative if it is for a name that may be
5980            far as we have got because the syntax of named subpatterns has not            duplicated. If the name is not found, set the value to 0 for a
5981            been checked for the rest of the pattern, and find_parens() assumes            forward reference. */
5982            correct syntax. In any case, it's a waste of resources to scan  
5983            further. We stop the scan at the current point by temporarily            ng = cd->named_groups;
5984            adjusting the value of cd->endpattern. */            for (i = 0; i < cd->names_found; i++, ng++)
5985                {
5986            temp = cd->end_pattern;              if (namelen == ng->length &&
5987            cd->end_pattern = ptr;                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)
5988            recno = find_parens(cd, name, namelen,                break;
5989              (options & PCRE_EXTENDED) != 0, utf);              }
5990            cd->end_pattern = temp;            recno = (i < cd->names_found)? ng->number : 0;
           if (recno < 0) recno = 0;    /* Forward ref; set dummy number */  
5991            }            }
5992    
5993          /* In the real compile, seek the name in the table. We check the name          /* In the real compile, search the name table. We check the name
5994          first, and then check that we have reached the end of the name in the          first, and then check that we have reached the end of the name in the
5995          table. That way, if the name that is longer than any in the table,          table. That way, if the name is longer than any in the table, the
5996          the comparison will fail without reading beyond the table entry. */          comparison will fail without reading beyond the table entry. */
5997    
5998          else          else
5999            {            {
# Line 6204  for (;; ptr++) Line 6006  for (;; ptr++)
6006              slot += cd->name_entry_size;              slot += cd->name_entry_size;
6007              }              }
6008    
6009            if (i < cd->names_found)         /* Back reference */            if (i < cd->names_found)
6010              {              {
6011              recno = GET2(slot, 0);              recno = GET2(slot, 0);
6012              }              }
6013            else if ((recno =                /* Forward back reference */            else
                     find_parens(cd, name, namelen,  
                       (options & PCRE_EXTENDED) != 0, utf)) <= 0)  
6014              {              {
6015              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
6016              goto FAILED;              goto FAILED;
# Line 6266  for (;; ptr++) Line 6066  for (;; ptr++)
6066            while(IS_DIGIT(*ptr))            while(IS_DIGIT(*ptr))
6067              recno = recno * 10 + *ptr++ - CHAR_0;              recno = recno * 10 + *ptr++ - CHAR_0;
6068    
6069            if (*ptr != terminator)            if (*ptr != (pcre_uchar)terminator)
6070              {              {
6071              *errorcodeptr = ERR29;              *errorcodeptr = ERR29;
6072              goto FAILED;              goto FAILED;
# Line 6320  for (;; ptr++) Line 6120  for (;; ptr++)
6120    
6121              if (called == NULL)              if (called == NULL)