/[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 964 by ph10, Fri May 4 13:03:39 2012 UTC revision 1361 by ph10, Fri Sep 6 17:47:32 2013 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-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 490  static const char error_texts[] = Line 512  static const char error_texts[] =
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 */    /* 75 */
515    "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"    "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 630  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 657  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 741  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 775  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 784  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 797  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 822  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 831  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 857  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 872  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 892  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 916  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 924  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 954  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 1020  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 1038  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 1051  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 1082  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 1101  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 1141  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 1171  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 1202  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 1227  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 1279  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 1294  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 1319  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 1686  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 1712  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 1832  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 1841  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 1876  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 1899  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 1966  for (;;) Line 1722  for (;;)
1722      case OP_QUERYI:      case OP_QUERYI:
1723      case OP_REF:      case OP_REF:
1724      case OP_REFI:      case OP_REFI:
1725        case OP_DNREF:
1726        case OP_DNREFI:
1727      case OP_SBRA:      case OP_SBRA:
1728      case OP_SBRAPOS:      case OP_SBRAPOS:
1729      case OP_SCBRA:      case OP_SCBRA:
# Line 2015  length. Line 1773  length.
1773    
1774  Arguments:  Arguments:
1775    code        points to start of expression    code        points to start of expression
1776    utf         TRUE in UTF-8 / UTF-16 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1777    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
1778    
1779  Returns:      pointer to the opcode for the bracket, or NULL if not found  Returns:      pointer to the opcode for the bracket, or NULL if not found
# Line 2026  PRIV(find_bracket)(const pcre_uchar *cod Line 1784  PRIV(find_bracket)(const pcre_uchar *cod
1784  {  {
1785  for (;;)  for (;;)
1786    {    {
1787    register int c = *code;    register pcre_uchar c = *code;
1788    
1789    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1790    
# Line 2049  for (;;) Line 1807  for (;;)
1807    else if (c == OP_CBRA || c == OP_SCBRA ||    else if (c == OP_CBRA || c == OP_SCBRA ||
1808             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
1809      {      {
1810      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1811      if (n == number) return (pcre_uchar *)code;      if (n == number) return (pcre_uchar *)code;
1812      code += PRIV(OP_lengths)[c];      code += PRIV(OP_lengths)[c];
1813      }      }
# Line 2079  for (;;) Line 1837  for (;;)
1837        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1838        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1839        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1840        if (code[1 + IMM2_SIZE] == OP_PROP        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1841          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;          code += 2;
1842        break;        break;
1843    
1844        case OP_MARK:        case OP_MARK:
1845        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1846        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
1847        case OP_THEN_ARG:        case OP_THEN_ARG:
1848        code += code[1];        code += code[1];
1849        break;        break;
# Line 2102  for (;;) Line 1857  for (;;)
1857    a multi-byte character. The length in the table is a minimum, so we have to    a multi-byte character. The length in the table is a minimum, so we have to
1858    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
1859    
1860  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1861      if (utf) switch(c)      if (utf) switch(c)
1862        {        {
1863        case OP_CHAR:        case OP_CHAR:
# Line 2154  instance of OP_RECURSE. Line 1909  instance of OP_RECURSE.
1909    
1910  Arguments:  Arguments:
1911    code        points to start of expression    code        points to start of expression
1912    utf         TRUE in UTF-8 / UTF-16 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1913    
1914  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found
1915  */  */
# Line 2164  find_recurse(const pcre_uchar *code, BOO Line 1919  find_recurse(const pcre_uchar *code, BOO
1919  {  {
1920  for (;;)  for (;;)
1921    {    {
1922    register int c = *code;    register pcre_uchar c = *code;
1923    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1924    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
1925    
# Line 2199  for (;;) Line 1954  for (;;)
1954        case OP_TYPEUPTO:        case OP_TYPEUPTO:
1955        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1956        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1957        if (code[1 + IMM2_SIZE] == OP_PROP        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1958          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;          code += 2;
1959        break;        break;
1960    
1961        case OP_MARK:        case OP_MARK:
1962        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1963        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
1964        case OP_THEN_ARG:        case OP_THEN_ARG:
1965        code += code[1];        code += code[1];
1966        break;        break;
# Line 2222  for (;;) Line 1974  for (;;)
1974      by a multi-byte character. The length in the table is a minimum, so we have      by a multi-byte character. The length in the table is a minimum, so we have
1975      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
1976    
1977  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1978      if (utf) switch(c)      if (utf) switch(c)
1979        {        {
1980        case OP_CHAR:        case OP_CHAR:
# Line 2308  bracket whose current branch will alread Line 2060  bracket whose current branch will alread
2060  Arguments:  Arguments:
2061    code        points to start of search    code        points to start of search
2062    endcode     points to where to stop    endcode     points to where to stop
2063    utf         TRUE if in UTF-8 / UTF-16 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2064    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2065      recurses    chain of recurse_check to catch mutual recursion
2066    
2067  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2068  */  */
2069    
2070    typedef struct recurse_check {
2071      struct recurse_check *prev;
2072      const pcre_uchar *group;
2073    } recurse_check;
2074    
2075  static BOOL  static BOOL
2076  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2077    BOOL utf, compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2078  {  {
2079  register int c;  register pcre_uchar c;
2080    recurse_check this_recurse;
2081    
2082  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2083       code < endcode;       code < endcode;
2084       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
# Line 2346  for (code = first_significant_code(code Line 2106  for (code = first_significant_code(code
2106    
2107    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2108      {      {
2109      const pcre_uchar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2110      BOOL empty_branch;      BOOL empty_branch;
2111    
2112      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2113        when called to scan a completed pattern by setting cd->start_workspace to
2114        NULL. */
2115    
2116        if (cd->start_workspace != NULL)
2117          {
2118          const pcre_uchar *tcode;
2119          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2120            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2121          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2122          }
2123    
2124        /* If we are scanning a completed pattern, there are no forward references
2125        and all groups are complete. We need to detect whether this is a recursive
2126        call, as otherwise there will be an infinite loop. If it is a recursion,
2127        just skip over it. Simple recursions are easily detected. For mutual
2128        recursions we keep a chain on the stack. */
2129    
2130      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)      else
2131        if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;        {
2132          recurse_check *r = recurses;
2133          const pcre_uchar *endgroup = scode;
2134    
2135      /* Not a forward reference, test for completed backward reference */        do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2136          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2137    
2138      empty_branch = FALSE;        for (r = recurses; r != NULL; r = r->prev)
2139      scode = cd->start_code + GET(code, 1);          if (r->group == scode) break;
2140      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */        if (r != NULL) continue;   /* Mutual recursion */
2141          }
2142    
2143        /* Completed reference; scan the referenced group, remembering it on the
2144        stack chain to detect mutual recursions. */
2145    
2146      /* Completed backwards reference */      empty_branch = FALSE;
2147        this_recurse.prev = recurses;
2148        this_recurse.group = scode;
2149    
2150      do      do
2151        {        {
2152        if (could_be_empty_branch(scode, endcode, utf, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2153          {          {
2154          empty_branch = TRUE;          empty_branch = TRUE;
2155          break;          break;
# Line 2420  for (code = first_significant_code(code Line 2205  for (code = first_significant_code(code
2205        empty_branch = FALSE;        empty_branch = FALSE;
2206        do        do
2207          {          {
2208          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2209            empty_branch = TRUE;            empty_branch = TRUE;
2210          code += GET(code, 1);          code += GET(code, 1);
2211          }          }
# Line 2478  for (code = first_significant_code(code Line 2263  for (code = first_significant_code(code
2263    
2264      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2265    
2266        case OP_ANY:
2267        case OP_ALLANY:
2268        case OP_ANYBYTE:
2269    
2270      case OP_PROP:      case OP_PROP:
2271      case OP_NOTPROP:      case OP_NOTPROP:
2272        case OP_ANYNL:
2273    
2274        case OP_NOT_HSPACE:
2275        case OP_HSPACE:
2276        case OP_NOT_VSPACE:
2277        case OP_VSPACE:
2278      case OP_EXTUNI:      case OP_EXTUNI:
2279    
2280      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2281      case OP_DIGIT:      case OP_DIGIT:
2282      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2283      case OP_WHITESPACE:      case OP_WHITESPACE:
2284      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2285      case OP_WORDCHAR:      case OP_WORDCHAR:
2286      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2287      case OP_CHAR:      case OP_CHAR:
2288      case OP_CHARI:      case OP_CHARI:
2289      case OP_NOT:      case OP_NOT:
2290      case OP_NOTI:      case OP_NOTI:
2291    
2292      case OP_PLUS:      case OP_PLUS:
2293        case OP_PLUSI:
2294      case OP_MINPLUS:      case OP_MINPLUS:
2295      case OP_POSPLUS:      case OP_MINPLUSI:
2296      case OP_EXACT:  
2297      case OP_NOTPLUS:      case OP_NOTPLUS:
2298        case OP_NOTPLUSI:
2299      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2300        case OP_NOTMINPLUSI:
2301    
2302        case OP_POSPLUS:
2303        case OP_POSPLUSI:
2304      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2305        case OP_NOTPOSPLUSI:
2306    
2307        case OP_EXACT:
2308        case OP_EXACTI:
2309      case OP_NOTEXACT:      case OP_NOTEXACT:
2310        case OP_NOTEXACTI:
2311    
2312      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2313      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2314      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2315      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2316    
2317      return FALSE;      return FALSE;
2318    
2319      /* These are going to continue, as they may be empty, but we have to      /* These are going to continue, as they may be empty, but we have to
# Line 2525  for (code = first_significant_code(code Line 2333  for (code = first_significant_code(code
2333      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2334      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2335      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2336      if (code[1 + IMM2_SIZE] == OP_PROP      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2337        || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;        code += 2;
2338      break;      break;
2339    
2340      /* End of branch */      /* End of branch */
# Line 2539  for (code = first_significant_code(code Line 2347  for (code = first_significant_code(code
2347      return TRUE;      return TRUE;
2348    
2349      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2350      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2351        followed by a multibyte character. */
2352    
2353  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2354      case OP_STAR:      case OP_STAR:
2355      case OP_STARI:      case OP_STARI:
2356        case OP_NOTSTAR:
2357        case OP_NOTSTARI:
2358    
2359      case OP_MINSTAR:      case OP_MINSTAR:
2360      case OP_MINSTARI:      case OP_MINSTARI:
2361        case OP_NOTMINSTAR:
2362        case OP_NOTMINSTARI:
2363    
2364      case OP_POSSTAR:      case OP_POSSTAR:
2365      case OP_POSSTARI:      case OP_POSSTARI:
2366        case OP_NOTPOSSTAR:
2367        case OP_NOTPOSSTARI:
2368    
2369      case OP_QUERY:      case OP_QUERY:
2370      case OP_QUERYI:      case OP_QUERYI:
2371        case OP_NOTQUERY:
2372        case OP_NOTQUERYI:
2373    
2374      case OP_MINQUERY:      case OP_MINQUERY:
2375      case OP_MINQUERYI:      case OP_MINQUERYI:
2376        case OP_NOTMINQUERY:
2377        case OP_NOTMINQUERYI:
2378    
2379      case OP_POSQUERY:      case OP_POSQUERY:
2380      case OP_POSQUERYI:      case OP_POSQUERYI:
2381        case OP_NOTPOSQUERY:
2382        case OP_NOTPOSQUERYI:
2383    
2384      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2385      break;      break;
2386    
2387      case OP_UPTO:      case OP_UPTO:
2388      case OP_UPTOI:      case OP_UPTOI:
2389        case OP_NOTUPTO:
2390        case OP_NOTUPTOI:
2391    
2392      case OP_MINUPTO:      case OP_MINUPTO:
2393      case OP_MINUPTOI:      case OP_MINUPTOI:
2394        case OP_NOTMINUPTO:
2395        case OP_NOTMINUPTOI:
2396    
2397      case OP_POSUPTO:      case OP_POSUPTO:
2398      case OP_POSUPTOI:      case OP_POSUPTOI:
2399        case OP_NOTPOSUPTO:
2400        case OP_NOTPOSUPTOI:
2401    
2402      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]);
2403      break;      break;
2404  #endif  #endif
# Line 2573  for (code = first_significant_code(code Line 2409  for (code = first_significant_code(code
2409      case OP_MARK:      case OP_MARK:
2410      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2411      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     code += code[1];  
     break;  
   
2412      case OP_THEN_ARG:      case OP_THEN_ARG:
2413      code += code[1];      code += code[1];
2414      break;      break;
# Line 2607  Arguments: Line 2440  Arguments:
2440    code        points to start of the recursion    code        points to start of the recursion
2441    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2442    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2443    utf         TRUE if in UTF-8 / UTF-16 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2444    cd          pointers to tables etc    cd          pointers to tables etc
2445    
2446  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
# Line 2619  could_be_empty(const pcre_uchar *code, c Line 2452  could_be_empty(const pcre_uchar *code, c
2452  {  {
2453  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2454    {    {
2455    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2456      return FALSE;      return FALSE;
2457    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2458    }    }
# Line 2673  Returns:   TRUE or FALSE Line 2506  Returns:   TRUE or FALSE
2506  static BOOL  static BOOL
2507  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2508  {  {
2509  int terminator;          /* Don't combine these lines; the Solaris cc */  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
2510  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
2511  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != CHAR_NULL; ptr++)
2512    {    {
2513    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2514      ptr++;      ptr++;
# Line 2722  register int yield = 0; Line 2555  register int yield = 0;
2555  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
2556    {    {
2557    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
2558      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
2559    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
2560    yield++;    yield++;
2561    }    }
# Line 2754  value in the reference (which is a group Line 2587  value in the reference (which is a group
2587  Arguments:  Arguments:
2588    group      points to the start of the group    group      points to the start of the group
2589    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
2590    utf        TRUE in UTF-8 / UTF-16 mode    utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
2591    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
2592    save_hwm   the hwm forward reference pointer at the start of the group    save_hwm   the hwm forward reference pointer at the start of the group
2593    
# Line 2777  while ((ptr = (pcre_uchar *)find_recurse Line 2610  while ((ptr = (pcre_uchar *)find_recurse
2610    
2611    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
2612      {      {
2613      offset = GET(hc, 0);      offset = (int)GET(hc, 0);
2614      if (cd->start_code + offset == ptr + 1)      if (cd->start_code + offset == ptr + 1)
2615        {        {
2616        PUT(hc, 0, offset + adjust);        PUT(hc, 0, offset + adjust);
# Line 2790  while ((ptr = (pcre_uchar *)find_recurse Line 2623  while ((ptr = (pcre_uchar *)find_recurse
2623    
2624    if (hc >= cd->hwm)    if (hc >= cd->hwm)
2625      {      {
2626      offset = GET(ptr, 1);      offset = (int)GET(ptr, 1);
2627      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
2628      }      }
2629    
# Line 2858  PUT(previous_callout, 2 + LINK_SIZE, len Line 2691  PUT(previous_callout, 2 + LINK_SIZE, len
2691  *************************************************/  *************************************************/
2692    
2693  /* This function is passed the start and end of a class range, in UTF-8 mode  /* This function is passed the start and end of a class range, in UTF-8 mode
2694  with UCP support. It searches up the characters, looking for internal ranges of  with UCP support. It searches up the characters, looking for ranges of
2695  characters in the "other" case. Each call returns the next one, updating the  characters in the "other" case. Each call returns the next one, updating the
2696  start address.  start address. A character with multiple other cases is returned on its own
2697    with a special return value.
2698    
2699  Arguments:  Arguments:
2700    cptr        points to starting character value; updated    cptr        points to starting character value; updated
# Line 2868  Arguments: Line 2702  Arguments:
2702    ocptr       where to put start of othercase range    ocptr       where to put start of othercase range
2703    odptr       where to put end of othercase range    odptr       where to put end of othercase range
2704    
2705  Yield:        TRUE when range returned; FALSE when no more  Yield:        -1 when no more
2706                   0 when a range is returned
2707                  >0 the CASESET offset for char with multiple other cases
2708                    in this case, ocptr contains the original
2709  */  */
2710    
2711  static BOOL  static int
2712  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
2713    unsigned int *odptr)    pcre_uint32 *odptr)
2714  {  {
2715  unsigned int c, othercase, next;  pcre_uint32 c, othercase, next;
2716    unsigned int co;
2717    
2718    /* Find the first character that has an other case. If it has multiple other
2719    cases, return its case offset value. */
2720    
2721  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
2722    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
2723      if ((co = UCD_CASESET(c)) != 0)
2724        {
2725        *ocptr = c++;   /* Character that has the set */
2726        *cptr = c;      /* Rest of input range */
2727        return (int)co;
2728        }
2729      if ((othercase = UCD_OTHERCASE(c)) != c) break;
2730      }
2731    
2732  if (c > d) return FALSE;  if (c > d) return -1;  /* Reached end of range */
2733    
2734  *ocptr = othercase;  *ocptr = othercase;
2735  next = othercase + 1;  next = othercase + 1;
# Line 2891  for (++c; c <= d; c++) Line 2740  for (++c; c <= d; c++)
2740    next++;    next++;
2741    }    }
2742    
2743  *odptr = next - 1;  *odptr = next - 1;     /* End of othercase range */
2744  *cptr = c;  *cptr = c;             /* Rest of input range */
2745    return 0;
 return TRUE;  
2746  }  }
2747    
2748    
# Line 2916  Returns:       TRUE if auto-possessifyin Line 2764  Returns:       TRUE if auto-possessifyin
2764  */  */
2765    
2766  static BOOL  static BOOL
2767  check_char_prop(int c, int ptype, int pdata, BOOL negated)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated)
2768  {  {
2769    #ifdef SUPPORT_UCP
2770    const pcre_uint32 *p;
2771    #endif
2772    
2773  const ucd_record *prop = GET_UCD(c);  const ucd_record *prop = GET_UCD(c);
2774    
2775  switch(ptype)  switch(ptype)
2776    {    {
2777    case PT_LAMP:    case PT_LAMP:
# Line 2956  switch(ptype) Line 2809  switch(ptype)
2809    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2810            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2811            c == CHAR_UNDERSCORE) == negated;            c == CHAR_UNDERSCORE) == negated;
2812    
2813    #ifdef SUPPORT_UCP
2814      case PT_CLIST:
2815      p = PRIV(ucd_caseless_sets) + prop->caseset;
2816      for (;;)
2817        {
2818        if (c < *p) return !negated;
2819        if (c == *p++) return negated;
2820        }
2821      break;  /* Control never reaches here */
2822    #endif
2823    }    }
2824    
2825  return FALSE;  return FALSE;
2826  }  }
2827  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
# Line 2973  sense to automatically possessify the re Line 2838  sense to automatically possessify the re
2838    
2839  Arguments:  Arguments:
2840    previous      pointer to the repeated opcode    previous      pointer to the repeated opcode
2841    utf           TRUE in UTF-8 / UTF-16 mode    utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode
2842    ptr           next character in pattern    ptr           next character in pattern
2843    options       options bits    options       options bits
2844    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 2985  static BOOL Line 2850  static BOOL
2850  check_auto_possessive(const pcre_uchar *previous, BOOL utf,  check_auto_possessive(const pcre_uchar *previous, BOOL utf,
2851    const pcre_uchar *ptr, int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
2852  {  {
2853  pcre_int32 c, next;  pcre_uint32 c = NOTACHAR;
2854  int op_code = *previous++;  pcre_uint32 next;
2855    int escape;
2856    pcre_uchar op_code = *previous++;
2857    
2858  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
2859    
# Line 2998  if ((options & PCRE_EXTENDED) != 0) Line 2865  if ((options & PCRE_EXTENDED) != 0)
2865      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2866        {        {
2867        ptr++;        ptr++;
2868        while (*ptr != 0)        while (*ptr != CHAR_NULL)
2869          {          {
2870          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2871          ptr++;          ptr++;
# Line 3017  value is a character, a negative value i Line 2884  value is a character, a negative value i
2884  if (*ptr == CHAR_BACKSLASH)  if (*ptr == CHAR_BACKSLASH)
2885    {    {
2886    int temperrorcode = 0;    int temperrorcode = 0;
2887    next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);    escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options,
2888        FALSE);
2889    if (temperrorcode != 0) return FALSE;    if (temperrorcode != 0) return FALSE;
2890    ptr++;    /* Point after the escape sequence */    ptr++;    /* Point after the escape sequence */
2891    }    }
2892  else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)  else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)
2893    {    {
2894      escape = 0;
2895  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2896    if (utf) { GETCHARINC(next, ptr); } else    if (utf) { GETCHARINC(next, ptr); } else
2897  #endif  #endif
# Line 3040  if ((options & PCRE_EXTENDED) != 0) Line 2909  if ((options & PCRE_EXTENDED) != 0)
2909      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2910        {        {
2911        ptr++;        ptr++;
2912        while (*ptr != 0)        while (*ptr != CHAR_NULL)
2913          {          {
2914          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2915          ptr++;          ptr++;
# Line 3059  if (*ptr == CHAR_ASTERISK || *ptr == CHA Line 2928  if (*ptr == CHAR_ASTERISK || *ptr == CHA
2928    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)
2929      return FALSE;      return FALSE;
2930    
2931  /* Now compare the next item with the previous opcode. First, handle cases when  /* If the previous item is a character, get its value. */
 the next item is a character. */  
2932    
2933  if (next >= 0) switch(op_code)  if (op_code == OP_CHAR || op_code == OP_CHARI ||
2934        op_code == OP_NOT || op_code == OP_NOTI)
2935    {    {
   case OP_CHAR:  
2936  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2937    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
2938  #else  #else
2939    c = *previous;    c = *previous;
2940  #endif  #endif
2941    return c != next;    }
2942    
2943    /* For CHARI (caseless character) we must check the other case. If we have  /* Now compare the next item with the previous opcode. First, handle cases when
2944    Unicode property support, we can use it to test the other case of  the next item is a character. */
   high-valued characters. */  
2945    
2946    case OP_CHARI:  if (escape == 0)
2947  #ifdef SUPPORT_UTF    {
2948    GETCHARTEST(c, previous);    /* For a caseless UTF match, the next character may have more than one other
2949  #else    case, which maps to the special PT_CLIST property. Check this first. */
2950    c = *previous;  
2951    #ifdef SUPPORT_UCP
2952      if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)
2953        {
2954        unsigned int ocs = UCD_CASESET(next);
2955        if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);
2956        }
2957  #endif  #endif
2958    if (c == next) return FALSE;  
2959  #ifdef SUPPORT_UTF    switch(op_code)
   if (utf)  
2960      {      {
2961      unsigned int othercase;      case OP_CHAR:
2962      if (next < 128) othercase = cd->fcc[next]; else      return c != next;
2963    
2964        /* For CHARI (caseless character) we must check the other case. If we have
2965        Unicode property support, we can use it to test the other case of
2966        high-valued characters. We know that next can have only one other case,
2967        because multi-other-case characters are dealt with above. */
2968    
2969        case OP_CHARI:
2970        if (c == next) return FALSE;
2971    #ifdef SUPPORT_UTF
2972        if (utf)
2973          {
2974          pcre_uint32 othercase;
2975          if (next < 128) othercase = cd->fcc[next]; else
2976  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2977      othercase = UCD_OTHERCASE((unsigned int)next);        othercase = UCD_OTHERCASE(next);
2978  #else  #else
2979      othercase = NOTACHAR;        othercase = NOTACHAR;
2980  #endif  #endif
2981      return (unsigned int)c != othercase;        return c != othercase;
2982      }        }
2983    else      else
2984  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
2985    return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */      return (c != TABLE_GET(next, cd->fcc, next));  /* Not UTF */
2986    
2987    case OP_NOT:      case OP_NOT:
2988  #ifdef SUPPORT_UTF      return c == next;
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   return c == next;  
2989    
2990    case OP_NOTI:      case OP_NOTI:
2991  #ifdef SUPPORT_UTF      if (c == next) return TRUE;
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   if (c == next) return TRUE;  
2992  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
2993    if (utf)      if (utf)
2994      {        {
2995      unsigned int othercase;        pcre_uint32 othercase;
2996      if (next < 128) othercase = cd->fcc[next]; else        if (next < 128) othercase = cd->fcc[next]; else
2997  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
2998      othercase = UCD_OTHERCASE((unsigned int)next);        othercase = UCD_OTHERCASE(next);
2999  #else  #else
3000      othercase = NOTACHAR;        othercase = NOTACHAR;
3001  #endif  #endif
3002      return (unsigned int)c == othercase;        return c == othercase;
3003      }        }
3004    else      else
3005  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
3006    return (c == TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */      return (c == TABLE_GET(next, cd->fcc, next));  /* Not UTF */
3007    
3008    /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.      /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3009    When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */      When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3010    
3011    case OP_DIGIT:      case OP_DIGIT:
3012    return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;      return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
3013    
3014    case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
3015    return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
3016    
3017    case OP_WHITESPACE:      case OP_WHITESPACE:
3018    return next > 255 || (cd->ctypes[next] & ctype_space) == 0;      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
3019    
3020    case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
3021    return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
3022    
3023    case OP_WORDCHAR:      case OP_WORDCHAR:
3024    return next > 255 || (cd->ctypes[next] & ctype_word) == 0;      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
3025    
3026    case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
3027    return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
3028    
3029    case OP_HSPACE:      case OP_HSPACE:
3030    case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
3031    switch(next)      switch(next)
3032      {        {
3033      case 0x09:        HSPACE_CASES:
3034      case 0x20:        return op_code == OP_NOT_HSPACE;
     case 0xa0:  
     case 0x1680:  
     case 0x180e:  
     case 0x2000:  
     case 0x2001:  
     case 0x2002:  
     case 0x2003:  
     case 0x2004:  
     case 0x2005:  
     case 0x2006:  
     case 0x2007:  
     case 0x2008:  
     case 0x2009:  
     case 0x200A:  
     case 0x202f:  
     case 0x205f:  
     case 0x3000:  
     return op_code == OP_NOT_HSPACE;  
     default:  
     return op_code != OP_NOT_HSPACE;  
     }  
3035    
3036    case OP_ANYNL:        default:
3037    case OP_VSPACE:        return op_code != OP_NOT_HSPACE;
3038    case OP_NOT_VSPACE:        }
3039    switch(next)  
3040      {      case OP_ANYNL:
3041      case 0x0a:      case OP_VSPACE:
3042      case 0x0b:      case OP_NOT_VSPACE:
3043      case 0x0c:      switch(next)
3044      case 0x0d:        {
3045      case 0x85:        VSPACE_CASES:
3046      case 0x2028:        return op_code == OP_NOT_VSPACE;
3047      case 0x2029:  
3048      return op_code == OP_NOT_VSPACE;        default:
3049      default:        return op_code != OP_NOT_VSPACE;
3050      return op_code != OP_NOT_VSPACE;        }
     }  
3051    
3052  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3053    case OP_PROP:      case OP_PROP:
3054    return check_char_prop(next, previous[0], previous[1], FALSE);      return check_char_prop(next, previous[0], previous[1], FALSE);
3055    
3056    case OP_NOTPROP:      case OP_NOTPROP:
3057    return check_char_prop(next, previous[0], previous[1], TRUE);      return check_char_prop(next, previous[0], previous[1], TRUE);
3058  #endif  #endif
3059    
3060    default:      default:
3061    return FALSE;      return FALSE;
3062        }
3063    }    }
3064    
   
3065  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3066  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3067  generated only when PCRE_UCP is *not* set, that is, when only ASCII  generated only when PCRE_UCP is *not* set, that is, when only ASCII
# Line 3219  switch(op_code) Line 3072  switch(op_code)
3072    {    {
3073    case OP_CHAR:    case OP_CHAR:
3074    case OP_CHARI:    case OP_CHARI:
3075  #ifdef SUPPORT_UTF    switch(escape)
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   switch(-next)  
3076      {      {
3077      case ESC_d:      case ESC_d:
3078      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;
# Line 3248  switch(op_code) Line 3096  switch(op_code)
3096      case ESC_H:      case ESC_H:
3097      switch(c)      switch(c)
3098        {        {
3099        case 0x09:        HSPACE_CASES:
3100        case 0x20:        return escape != ESC_h;
3101        case 0xa0:  
       case 0x1680:  
       case 0x180e:  
       case 0x2000:  
       case 0x2001:  
       case 0x2002:  
       case 0x2003:  
       case 0x2004:  
       case 0x2005:  
       case 0x2006:  
       case 0x2007:  
       case 0x2008:  
       case 0x2009:  
       case 0x200A:  
       case 0x202f:  
       case 0x205f:  
       case 0x3000:  
       return -next != ESC_h;  
3102        default:        default:
3103        return -next == ESC_h;        return escape == ESC_h;
3104        }        }
3105    
3106      case ESC_v:      case ESC_v:
3107      case ESC_V:      case ESC_V:
3108      switch(c)      switch(c)
3109        {        {
3110        case 0x0a:        VSPACE_CASES:
3111        case 0x0b:        return escape != ESC_v;
3112        case 0x0c:  
       case 0x0d:  
       case 0x85:  
       case 0x2028:  
       case 0x2029:  
       return -next != ESC_v;  
3113        default:        default:
3114        return -next == ESC_v;        return escape == ESC_v;
3115        }        }
3116    
3117      /* When PCRE_UCP is set, these values get generated for \d etc. Find      /* When PCRE_UCP is set, these values get generated for \d etc. Find
3118      their substitutions and process them. The result will always be either      their substitutions and process them. The result will always be either
3119      -ESC_p or -ESC_P. Then fall through to process those values. */      ESC_p or ESC_P. Then fall through to process those values. */
3120    
3121  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3122      case ESC_du:      case ESC_du:
# Line 3301  switch(op_code) Line 3127  switch(op_code)
3127      case ESC_SU:      case ESC_SU:
3128        {        {
3129        int temperrorcode = 0;        int temperrorcode = 0;
3130        ptr = substitutes[-next - ESC_DU];        ptr = substitutes[escape - ESC_DU];
3131        next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);        escape = check_escape(&ptr, &next, &temperrorcode, 0, options, FALSE);
3132        if (temperrorcode != 0) return FALSE;        if (temperrorcode != 0) return FALSE;
3133        ptr++;    /* For compatibility */        ptr++;    /* For compatibility */
3134        }        }
# Line 3311  switch(op_code) Line 3137  switch(op_code)
3137      case ESC_p:      case ESC_p:
3138      case ESC_P:      case ESC_P:
3139        {        {
3140        int ptype, pdata, errorcodeptr;        unsigned int ptype = 0, pdata = 0;
3141          int errorcodeptr;
3142        BOOL negated;        BOOL negated;
3143    
3144        ptr--;      /* Make ptr point at the p or P */        ptr--;      /* Make ptr point at the p or P */
3145        ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);        if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr))
3146        if (ptype < 0) return FALSE;          return FALSE;
3147        ptr++;      /* Point past the final curly ket */        ptr++;      /* Point past the final curly ket */
3148    
3149        /* If the property item is optional, we have to give up. (When generated        /* If the property item is optional, we have to give up. (When generated
# Line 3329  switch(op_code) Line 3156  switch(op_code)
3156    
3157        /* Do the property check. */        /* Do the property check. */
3158    
3159        return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);        return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);
3160        }        }
3161  #endif  #endif
3162    
# Line 3344  switch(op_code) Line 3171  switch(op_code)
3171    these op-codes are never generated.) */    these op-codes are never generated.) */
3172    
3173    case OP_DIGIT:    case OP_DIGIT:
3174    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||    return escape == ESC_D || escape == ESC_s || escape == ESC_W ||
3175           next == -ESC_h || next == -ESC_v || next == -ESC_R;           escape == ESC_h || escape == ESC_v || escape == ESC_R;
3176    
3177    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
3178    return next == -ESC_d;    return escape == ESC_d;
3179    
3180    case OP_WHITESPACE:    case OP_WHITESPACE:
3181    return next == -ESC_S || next == -ESC_d || next == -ESC_w;    return escape == ESC_S || escape == ESC_d || escape == ESC_w;
3182    
3183    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3184    return next == -ESC_s || next == -ESC_h || next == -ESC_v || next == -ESC_R;    return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;
3185    
3186    case OP_HSPACE:    case OP_HSPACE:
3187    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||    return escape == ESC_S || escape == ESC_H || escape == ESC_d ||
3188           next == -ESC_w || next == -ESC_v || next == -ESC_R;           escape == ESC_w || escape == ESC_v || escape == ESC_R;
3189    
3190    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
3191    return next == -ESC_h;    return escape == ESC_h;
3192    
3193    /* Can't have \S in here because VT matches \S (Perl anomaly) */    /* Can't have \S in here because VT matches \S (Perl anomaly) */
3194    case OP_ANYNL:    case OP_ANYNL:
3195    case OP_VSPACE:    case OP_VSPACE:
3196    return next == -ESC_V || next == -ESC_d || next == -ESC_w;    return escape == ESC_V || escape == ESC_d || escape == ESC_w;
3197    
3198    case OP_NOT_VSPACE:    case OP_NOT_VSPACE:
3199    return next == -ESC_v || next == -ESC_R;    return escape == ESC_v || escape == ESC_R;
3200    
3201    case OP_WORDCHAR:    case OP_WORDCHAR:
3202    return next == -ESC_W || next == -ESC_s || next == -ESC_h ||    return escape == ESC_W || escape == ESC_s || escape == ESC_h ||
3203           next == -ESC_v || next == -ESC_R;           escape == ESC_v || escape == ESC_R;
3204    
3205    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
3206    return next == -ESC_w || next == -ESC_d;    return escape == ESC_w || escape == ESC_d;
3207    
3208    default:    default:
3209    return FALSE;    return FALSE;
# Line 3388  switch(op_code) Line 3215  switch(op_code)
3215    
3216    
3217  /*************************************************  /*************************************************
3218    *        Add a character or range to a class     *
3219    *************************************************/
3220    
3221    /* This function packages up the logic of adding a character or range of
3222    characters to a class. The character values in the arguments will be within the
3223    valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3224    mutually recursive with the function immediately below.
3225    
3226    Arguments:
3227      classbits     the bit map for characters < 256
3228      uchardptr     points to the pointer for extra data
3229      options       the options word
3230      cd            contains pointers to tables etc.
3231      start         start of range character
3232      end           end of range character
3233    
3234    Returns:        the number of < 256 characters added
3235                    the pointer to extra data is updated
3236    */
3237    
3238    static int
3239    add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3240      compile_data *cd, pcre_uint32 start, pcre_uint32 end)
3241    {
3242    pcre_uint32 c;
3243    int n8 = 0;
3244    
3245    /* If caseless matching is required, scan the range and process alternate
3246    cases. In Unicode, there are 8-bit characters that have alternate cases that
3247    are greater than 255 and vice-versa. Sometimes we can just extend the original
3248    range. */
3249    
3250    if ((options & PCRE_CASELESS) != 0)
3251      {
3252    #ifdef SUPPORT_UCP
3253      if ((options & PCRE_UTF8) != 0)
3254        {
3255        int rc;
3256        pcre_uint32 oc, od;
3257    
3258        options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3259        c = start;
3260    
3261        while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3262          {
3263          /* Handle a single character that has more than one other case. */
3264    
3265          if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3266            PRIV(ucd_caseless_sets) + rc, oc);
3267    
3268          /* Do nothing if the other case range is within the original range. */
3269    
3270          else if (oc >= start && od <= end) continue;
3271    
3272          /* Extend the original range if there is overlap, noting that if oc < c, we
3273          can't have od > end because a subrange is always shorter than the basic
3274          range. Otherwise, use a recursive call to add the additional range. */
3275    
3276          else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3277          else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3278          else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3279          }
3280        }
3281      else
3282    #endif  /* SUPPORT_UCP */
3283    
3284      /* Not UTF-mode, or no UCP */
3285    
3286      for (c = start; c <= end && c < 256; c++)
3287        {
3288        SETBIT(classbits, cd->fcc[c]);
3289        n8++;
3290        }
3291      }
3292    
3293    /* Now handle the original range. Adjust the final value according to the bit
3294    length - this means that the same lists of (e.g.) horizontal spaces can be used
3295    in all cases. */
3296    
3297    #if defined COMPILE_PCRE8
3298    #ifdef SUPPORT_UTF
3299      if ((options & PCRE_UTF8) == 0)
3300    #endif
3301      if (end > 0xff) end = 0xff;
3302    
3303    #elif defined COMPILE_PCRE16
3304    #ifdef SUPPORT_UTF
3305      if ((options & PCRE_UTF16) == 0)
3306    #endif
3307      if (end > 0xffff) end = 0xffff;
3308    
3309    #endif /* COMPILE_PCRE[8|16] */
3310    
3311    /* If all characters are less than 256, use the bit map. Otherwise use extra
3312    data. */
3313    
3314    if (end < 0x100)
3315      {
3316      for (c = start; c <= end; c++)
3317        {
3318        n8++;
3319        SETBIT(classbits, c);
3320        }
3321      }
3322    
3323    else
3324      {
3325      pcre_uchar *uchardata = *uchardptr;
3326    
3327    #ifdef SUPPORT_UTF
3328      if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3329        {
3330        if (start < end)
3331          {
3332          *uchardata++ = XCL_RANGE;
3333          uchardata += PRIV(ord2utf)(start, uchardata);
3334          uchardata += PRIV(ord2utf)(end, uchardata);
3335          }
3336        else if (start == end)
3337          {
3338          *uchardata++ = XCL_SINGLE;
3339          uchardata += PRIV(ord2utf)(start, uchardata);
3340          }
3341        }
3342      else
3343    #endif  /* SUPPORT_UTF */
3344    
3345      /* Without UTF support, character values are constrained by the bit length,
3346      and can only be > 256 for 16-bit and 32-bit libraries. */
3347    
3348    #ifdef COMPILE_PCRE8
3349        {}
3350    #else
3351      if (start < end)
3352        {
3353        *uchardata++ = XCL_RANGE;
3354        *uchardata++ = start;
3355        *uchardata++ = end;
3356        }
3357      else if (start == end)
3358        {
3359        *uchardata++ = XCL_SINGLE;
3360        *uchardata++ = start;
3361        }
3362    #endif
3363    
3364      *uchardptr = uchardata;   /* Updata extra data pointer */
3365      }
3366    
3367    return n8;    /* Number of 8-bit characters */
3368    }
3369    
3370    
3371    
3372    
3373    /*************************************************
3374    *        Add a list of characters to a class     *
3375    *************************************************/
3376    
3377    /* This function is used for adding a list of case-equivalent characters to a
3378    class, and also for adding a list of horizontal or vertical whitespace. If the
3379    list is in order (which it should be), ranges of characters are detected and
3380    handled appropriately. This function is mutually recursive with the function
3381    above.
3382    
3383    Arguments:
3384      classbits     the bit map for characters < 256
3385      uchardptr     points to the pointer for extra data
3386      options       the options word
3387      cd            contains pointers to tables etc.
3388      p             points to row of 32-bit values, terminated by NOTACHAR
3389      except        character to omit; this is used when adding lists of
3390                      case-equivalent characters to avoid including the one we
3391                      already know about
3392    
3393    Returns:        the number of < 256 characters added
3394                    the pointer to extra data is updated
3395    */
3396    
3397    static int
3398    add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3399      compile_data *cd, const pcre_uint32 *p, unsigned int except)
3400    {
3401    int n8 = 0;
3402    while (p[0] < NOTACHAR)
3403      {
3404      int n = 0;
3405      if (p[0] != except)
3406        {
3407        while(p[n+1] == p[0] + n + 1) n++;
3408        n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3409        }
3410      p += n + 1;
3411      }
3412    return n8;
3413    }
3414    
3415    
3416    
3417    /*************************************************
3418    *    Add characters not in a list to a class     *
3419    *************************************************/
3420    
3421    /* This function is used for adding the complement of a list of horizontal or
3422    vertical whitespace to a class. The list must be in order.
3423    
3424    Arguments:
3425      classbits     the bit map for characters < 256
3426      uchardptr     points to the pointer for extra data
3427      options       the options word
3428      cd            contains pointers to tables etc.
3429      p             points to row of 32-bit values, terminated by NOTACHAR
3430    
3431    Returns:        the number of < 256 characters added
3432                    the pointer to extra data is updated
3433    */
3434    
3435    static int
3436    add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3437      int options, compile_data *cd, const pcre_uint32 *p)
3438    {
3439    BOOL utf = (options & PCRE_UTF8) != 0;
3440    int n8 = 0;
3441    if (p[0] > 0)
3442      n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
3443    while (p[0] < NOTACHAR)
3444      {
3445      while (p[1] == p[0] + 1) p++;
3446      n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3447        (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
3448      p++;
3449      }
3450    return n8;
3451    }
3452    
3453    
3454    
3455    /*************************************************
3456  *           Compile one branch                   *  *           Compile one branch                   *
3457  *************************************************/  *************************************************/
3458    
# Line 3398  to find out the amount of memory needed, Line 3463  to find out the amount of memory needed,
3463  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
3464    
3465  Arguments:  Arguments:
3466    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
3467    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
3468    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
3469    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
3470    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr      place to put the first required character
3471    reqcharptr     set to the last literal character required, else < 0    firstcharflagsptr place to put the first character flags, or a negative number
3472    bcptr          points to current branch chain    reqcharptr        place to put the last required character
3473    cond_depth     conditional nesting depth    reqcharflagsptr   place to put the last required character flags, or a negative number
3474    cd             contains pointers to tables etc.    bcptr             points to current branch chain
3475    lengthptr      NULL during the real compile phase    cond_depth        conditional nesting depth
3476                   points to length accumulator during pre-compile phase    cd                contains pointers to tables etc.
3477      lengthptr         NULL during the real compile phase
3478                        points to length accumulator during pre-compile phase
3479    
3480  Returns:         TRUE on success  Returns:            TRUE on success
3481                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
3482  */  */
3483    
3484  static BOOL  static BOOL
3485  compile_branch(int *optionsptr, pcre_uchar **codeptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3486    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,    const pcre_uchar **ptrptr, int *errorcodeptr,
3487    pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,    pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
3488      pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
3489      branch_chain *bcptr, int cond_depth,
3490    compile_data *cd, int *lengthptr)    compile_data *cd, int *lengthptr)
3491  {  {
3492  int repeat_type, op_type;  int repeat_type, op_type;
3493  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3494  int bravalue = 0;  int bravalue = 0;
3495  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3496  pcre_int32 firstchar, reqchar;  pcre_uint32 firstchar, reqchar;
3497  pcre_int32 zeroreqchar, zerofirstchar;  pcre_int32 firstcharflags, reqcharflags;
3498    pcre_uint32 zeroreqchar, zerofirstchar;
3499    pcre_int32 zeroreqcharflags, zerofirstcharflags;
3500  pcre_int32 req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
3501  int options = *optionsptr;               /* May change dynamically */  int options = *optionsptr;               /* May change dynamically */
3502  int after_manual_callout = 0;  int after_manual_callout = 0;
3503  int length_prevgroup = 0;  int length_prevgroup = 0;
3504  register int c;  register pcre_uint32 c;
3505    int escape;
3506  register pcre_uchar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3507  pcre_uchar *last_code = code;  pcre_uchar *last_code = code;
3508  pcre_uchar *orig_code = code;  pcre_uchar *orig_code = code;
# Line 3450  must not do this for other options (e.g. Line 3522  must not do this for other options (e.g.
3522  dynamically as we process the pattern. */  dynamically as we process the pattern. */
3523    
3524  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3525  /* PCRE_UTF16 has the same value as PCRE_UTF8. */  /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
3526  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
3527    #ifndef COMPILE_PCRE32
3528  pcre_uchar utf_chars[6];  pcre_uchar utf_chars[6];
3529    #endif
3530  #else  #else
3531  BOOL utf = FALSE;  BOOL utf = FALSE;
3532  #endif  #endif
3533    
3534  /* Helper variables for OP_XCLASS opcode (for characters > 255). */  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
3535    class_uchardata always so that it can be passed to add_to_class() always,
3536    though it will not be used in non-UTF 8-bit cases. This avoids having to supply
3537    alternative calls for the different cases. */
3538    
3539    pcre_uchar *class_uchardata;
3540  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3541  BOOL xclass;  BOOL xclass;
 pcre_uchar *class_uchardata;  
3542  pcre_uchar *class_uchardata_base;  pcre_uchar *class_uchardata_base;
3543  #endif  #endif
3544    
# Line 3484  to take the zero repeat into account. Th Line 3561  to take the zero repeat into account. Th
3561  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3562  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
3563    
3564  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
3565    firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
3566    
3567  /* The variable req_caseopt contains either the REQ_CASELESS value  /* The variable req_caseopt contains either the REQ_CASELESS value
3568  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 3505  for (;; ptr++) Line 3583  for (;; ptr++)
3583    BOOL is_recurse;    BOOL is_recurse;
3584    BOOL reset_bracount;    BOOL reset_bracount;
3585    int class_has_8bitchar;    int class_has_8bitchar;
3586    int class_single_char;    int class_one_char;
3587    int newoptions;    int newoptions;
3588    int recno;    int recno;
3589    int refsign;    int refsign;
3590    int skipbytes;    int skipbytes;
3591    int subreqchar;    pcre_uint32 subreqchar, subfirstchar;
3592    int subfirstchar;    pcre_int32 subreqcharflags, subfirstcharflags;
3593    int terminator;    int terminator;
3594    int mclength;    unsigned int mclength;
3595    int tempbracount;    unsigned int tempbracount;
3596      pcre_uint32 ec;
3597    pcre_uchar mcbuffer[8];    pcre_uchar mcbuffer[8];
3598    
3599    /* Get next character in the pattern */    /* Get next character in the pattern */
# Line 3524  for (;; ptr++) Line 3603  for (;; ptr++)
3603    /* If we are at the end of a nested substitution, revert to the outer level    /* If we are at the end of a nested substitution, revert to the outer level
3604    string. Nesting only happens one level deep. */    string. Nesting only happens one level deep. */
3605    
3606    if (c == 0 && nestptr != NULL)    if (c == CHAR_NULL && nestptr != NULL)
3607      {      {
3608      ptr = nestptr;      ptr = nestptr;
3609      nestptr = NULL;      nestptr = NULL;
# Line 3599  for (;; ptr++) Line 3678  for (;; ptr++)
3678    
3679    /* If in \Q...\E, check for the end; if not, we have a literal */    /* If in \Q...\E, check for the end; if not, we have a literal */
3680    
3681    if (inescq && c != 0)    if (inescq && c != CHAR_NULL)
3682      {      {
3683      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
3684        {        {
# Line 3647  for (;; ptr++) Line 3726  for (;; ptr++)
3726      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3727        {        {
3728        ptr++;        ptr++;
3729        while (*ptr != 0)        while (*ptr != CHAR_NULL)
3730          {          {
3731          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3732          ptr++;          ptr++;
# Line 3655  for (;; ptr++) Line 3734  for (;; ptr++)
3734          if (utf) FORWARDCHAR(ptr);          if (utf) FORWARDCHAR(ptr);
3735  #endif  #endif
3736          }          }
3737        if (*ptr != 0) continue;        if (*ptr != CHAR_NULL) continue;
3738    
3739        /* Else fall through to handle end of string */        /* Else fall through to handle end of string */
3740        c = 0;        c = 0;
# Line 3677  for (;; ptr++) Line 3756  for (;; ptr++)
3756      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
3757      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
3758      *firstcharptr = firstchar;      *firstcharptr = firstchar;
3759        *firstcharflagsptr = firstcharflags;
3760      *reqcharptr = reqchar;      *reqcharptr = reqchar;
3761        *reqcharflagsptr = reqcharflags;
3762      *codeptr = code;      *codeptr = code;
3763      *ptrptr = ptr;      *ptrptr = ptr;
3764      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 3701  for (;; ptr++) Line 3782  for (;; ptr++)
3782      previous = NULL;      previous = NULL;
3783      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3784        {        {
3785        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
3786        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
3787        }        }
3788      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 3716  for (;; ptr++) Line 3797  for (;; ptr++)
3797      repeats. The value of reqchar doesn't change either. */      repeats. The value of reqchar doesn't change either. */
3798    
3799      case CHAR_DOT:      case CHAR_DOT:
3800      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
3801      zerofirstchar = firstchar;      zerofirstchar = firstchar;
3802        zerofirstcharflags = firstcharflags;
3803      zeroreqchar = reqchar;      zeroreqchar = reqchar;
3804        zeroreqcharflags = reqcharflags;
3805      previous = code;      previous = code;
3806      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
3807      break;      break;
# Line 3792  for (;; ptr++) Line 3875  for (;; ptr++)
3875          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
3876        {        {
3877        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
3878        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
3879        zerofirstchar = firstchar;        zerofirstchar = firstchar;
3880          zerofirstcharflags = firstcharflags;
3881        break;        break;
3882        }        }
3883    
# Line 3803  for (;; ptr++) Line 3887  for (;; ptr++)
3887    
3888      should_flip_negation = FALSE;      should_flip_negation = FALSE;
3889    
3890      /* For optimization purposes, we track some properties of the class.      /* For optimization purposes, we track some properties of the class:
3891      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 <
3892      < 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
3893      a single character. */      character. */
3894    
3895      class_has_8bitchar = 0;      class_has_8bitchar = 0;
3896      class_single_char = 0;      class_one_char = 0;
3897    
3898      /* Initialize the 32-char bit map to all zeros. We build the map in a      /* Initialize the 32-char bit map to all zeros. We build the map in a
3899      temporary bit of memory, in case the class contains only 1 character (less      temporary bit of memory, in case the class contains fewer than two
3900      than 256), because in that case the compiled code doesn't use the bit map.      8-bit characters because in that case the compiled code doesn't use the bit
3901      */      map. */
3902    
3903      memset(classbits, 0, 32 * sizeof(pcre_uint8));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
3904    
3905  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3906      xclass = FALSE;                           /* No chars >= 256 */      xclass = FALSE;
3907      class_uchardata = code + LINK_SIZE + 2;   /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
3908      class_uchardata_base = class_uchardata;   /* For resetting in pass 1 */      class_uchardata_base = class_uchardata;   /* Save the start */
3909  #endif  #endif
3910    
3911      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
3912      means that an initial ] is taken as a data character. At the start of the      means that an initial ] is taken as a data character. At the start of the
3913      loop, c contains the first byte of the character. */      loop, c contains the first byte of the character. */
3914    
3915      if (c != 0) do      if (c != CHAR_NULL) do
3916        {        {
3917        const pcre_uchar *oldptr;        const pcre_uchar *oldptr;
3918    
# Line 3843  for (;; ptr++) Line 3927  for (;; ptr++)
3927        /* In the pre-compile phase, accumulate the length of any extra        /* In the pre-compile phase, accumulate the length of any extra
3928        data and reset the pointer. This is so that very large classes that        data and reset the pointer. This is so that very large classes that
3929        contain a zillion > 255 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
3930        (which is on the stack). */        (which is on the stack). We have to remember that there was XCLASS data,
3931          however. */
3932    
3933        if (lengthptr != NULL)        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
3934          {          {
3935            xclass = TRUE;
3936          *lengthptr += class_uchardata - class_uchardata_base;          *lengthptr += class_uchardata - class_uchardata_base;
3937          class_uchardata = class_uchardata_base;          class_uchardata = class_uchardata_base;
3938          }          }
# Line 3948  for (;; ptr++) Line 4034  for (;; ptr++)
4034              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4035            }            }
4036    
4037          /* Not see if we need to remove any special characters. An option          /* Now see if we need to remove any special characters. An option
4038          value of 1 removes vertical space and 2 removes underscore. */          value of 1 removes vertical space and 2 removes underscore. */
4039    
4040          if (tabopt < 0) tabopt = -tabopt;          if (tabopt < 0) tabopt = -tabopt;
# Line 3964  for (;; ptr++) Line 4050  for (;; ptr++)
4050            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4051    
4052          ptr = tempptr + 1;          ptr = tempptr + 1;
4053          /* Every class contains at least one < 256 characters. */          /* Every class contains at least one < 256 character. */
4054          class_has_8bitchar = 1;          class_has_8bitchar = 1;
4055          /* Every class contains at least two characters. */          /* Every class contains at least two characters. */
4056          class_single_char = 2;          class_one_char = 2;
4057          continue;    /* End of POSIX syntax handling */          continue;    /* End of POSIX syntax handling */
4058          }          }
4059    
# Line 3975  for (;; ptr++) Line 4061  for (;; ptr++)
4061        of the specials, which just set a flag. The sequence \b is a special        of the specials, which just set a flag. The sequence \b is a special
4062        case. Inside a class (and only there) it is treated as backspace. We        case. Inside a class (and only there) it is treated as backspace. We
4063        assume that other escapes have more than one character in them, so        assume that other escapes have more than one character in them, so
4064        speculatively set both class_has_8bitchar and class_single_char bigger        speculatively set both class_has_8bitchar and class_one_char bigger
4065        than one. Unrecognized escapes fall through and are either treated        than one. Unrecognized escapes fall through and are either treated
4066        as literal characters (by default), or are faulted if        as literal characters (by default), or are faulted if
4067        PCRE_EXTRA is set. */        PCRE_EXTRA is set. */
4068    
4069        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
4070          {          {
4071          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
4072              TRUE);
4073          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
4074            if (escape == 0) c = ec;
4075          if (-c == ESC_b) c = CHAR_BS;    /* \b is backspace in a class */          else if (escape == ESC_b) c = CHAR_BS; /* \b is backspace in a class */
4076          else if (-c == ESC_N)            /* \N is not supported in a class */          else if (escape == ESC_N)          /* \N is not supported in a class */
4077            {            {
4078            *errorcodeptr = ERR71;            *errorcodeptr = ERR71;
4079            goto FAILED;            goto FAILED;
4080            }            }
4081          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (escape == ESC_Q)            /* Handle start of quoted string */
4082            {            {
4083            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4084              {              {
# Line 4000  for (;; ptr++) Line 4087  for (;; ptr++)
4087            else inescq = TRUE;            else inescq = TRUE;
4088            continue;            continue;
4089            }            }
4090          else if (-c == ESC_E) continue;  /* Ignore orphan \E */          else if (escape == ESC_E) continue;  /* Ignore orphan \E */
4091    
4092          if (c < 0)          else
4093            {            {
4094            register const pcre_uint8 *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
4095            /* Every class contains at least two < 256 characters. */            /* Every class contains at least two < 256 characters. */
4096            class_has_8bitchar++;            class_has_8bitchar++;
4097            /* Every class contains at least two characters. */            /* Every class contains at least two characters. */
4098            class_single_char += 2;            class_one_char += 2;
4099    
4100            switch (-c)            switch (escape)
4101              {              {
4102  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4103              case ESC_du:     /* These are the values given for \d etc */              case ESC_du:     /* These are the values given for \d etc */
# Line 4020  for (;; ptr++) Line 4107  for (;; ptr++)
4107              case ESC_su:     /* of the default ASCII testing. */              case ESC_su:     /* of the default ASCII testing. */
4108              case ESC_SU:              case ESC_SU:
4109              nestptr = ptr;              nestptr = ptr;
4110              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */              ptr = substitutes[escape - ESC_DU] - 1;  /* Just before substitute */
4111              class_has_8bitchar--;                /* Undo! */              class_has_8bitchar--;                /* Undo! */
4112              continue;              continue;
4113  #endif  #endif
# Line 4044  for (;; ptr++) Line 4131  for (;; ptr++)
4131    
4132              /* Perl 5.004 onwards omits VT from \s, but we must preserve it              /* Perl 5.004 onwards omits VT from \s, but we must preserve it
4133              if it was previously set by something earlier in the character              if it was previously set by something earlier in the character
4134              class. */              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and
4135                EBCDIC, so we lazily just adjust the appropriate bit. */
4136    
4137              case ESC_s:              case ESC_s:
4138              classbits[0] |= cbits[cbit_space];              classbits[0] |= cbits[cbit_space];
# Line 4058  for (;; ptr++) Line 4146  for (;; ptr++)
4146              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
4147              continue;              continue;
4148    
4149                /* The rest apply in both UCP and non-UCP cases. */
4150    
4151              case ESC_h:              case ESC_h:
4152              SETBIT(classbits, 0x09); /* VT */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4153              SETBIT(classbits, 0x20); /* SPACE */                PRIV(hspace_list), NOTACHAR);
             SETBIT(classbits, 0xa0); /* NSBP */  
 #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  
4154              continue;              continue;
4155    
4156              case ESC_H:              case ESC_H:
4157              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4158                {                cd, PRIV(hspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x09/8: x ^= 1 << (0x09%8); break;  
                 case 0x20/8: x ^= 1 << (0x20%8); break;  
                 case 0xa0/8: x ^= 1 << (0xa0%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
 #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  
4159              continue;              continue;
4160    
4161              case ESC_v:              case ESC_v:
4162              SETBIT(classbits, 0x0a); /* LF */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4163              SETBIT(classbits, 0x0b); /* VT */                PRIV(vspace_list), NOTACHAR);
             SETBIT(classbits, 0x0c); /* FF */  
             SETBIT(classbits, 0x0d); /* CR */  
             SETBIT(classbits, 0x85); /* NEL */  
 #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  
4164              continue;              continue;
4165    
4166              case ESC_V:              case ESC_V:
4167              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4168                {                cd, PRIV(vspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x0a/8: x ^= 1 << (0x0a%8);  
                              x ^= 1 << (0x0b%8);  
                              x ^= 1 << (0x0c%8);  
                              x ^= 1 << (0x0d%8);  
                              break;  
                 case 0x85/8: x ^= 1 << (0x85%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
   
 #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  
4169              continue;              continue;
4170    
4171  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 4239  for (;; ptr++) Line 4173  for (;; ptr++)
4173              case ESC_P:              case ESC_P:
4174                {                {
4175                BOOL negated;                BOOL negated;
4176                int pdata;                unsigned int ptype = 0, pdata = 0;
4177                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
4178                if (ptype < 0) goto FAILED;                  goto FAILED;
4179                xclass = TRUE;                *class_uchardata++ = ((escape == ESC_p) != negated)?
               *class_uchardata++ = ((-c == ESC_p) != negated)?  
4180                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
4181                *class_uchardata++ = ptype;                *class_uchardata++ = ptype;
4182                *class_uchardata++ = pdata;                *class_uchardata++ = pdata;
# Line 4262  for (;; ptr++) Line 4195  for (;; ptr++)
4195                goto FAILED;                goto FAILED;
4196                }                }
4197              class_has_8bitchar--;    /* Undo the speculative increase. */              class_has_8bitchar--;    /* Undo the speculative increase. */
4198              class_single_char -= 2;  /* Undo the speculative increase. */              class_one_char -= 2;     /* Undo the speculative increase. */
4199              c = *ptr;                /* Get the final character and fall through */              c = *ptr;                /* Get the final character and fall through */
4200              break;              break;
4201              }              }
4202            }            }
4203    
4204          /* Fall through if we have a single character (c >= 0). This may be          /* Fall through if the escape just defined a single character (c >= 0).
4205          greater than 256. */          This may be greater than 256. */
4206    
4207            escape = 0;
4208    
4209          }   /* End of backslash handling */          }   /* End of backslash handling */
4210    
4211        /* A single character may be followed by '-' to form a range. However,        /* A character may be followed by '-' to form a range. However, Perl does
4212        Perl does not permit ']' to be the end of the range. A '-' character        not permit ']' to be the end of the range. A '-' character at the end is
4213        at the end is treated as a literal. Perl ignores orphaned \E sequences        treated as a literal. Perl ignores orphaned \E sequences entirely. The
4214        entirely. The code for handling \Q and \E is messy. */        code for handling \Q and \E is messy. */
4215    
4216        CHECK_RANGE:        CHECK_RANGE:
4217        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 4284  for (;; ptr++) Line 4219  for (;; ptr++)
4219          inescq = FALSE;          inescq = FALSE;
4220          ptr += 2;          ptr += 2;
4221          }          }
   
4222        oldptr = ptr;        oldptr = ptr;
4223    
4224        /* Remember \r or \n */        /* Remember if \r or \n were explicitly used */
4225    
4226        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4227    
# Line 4295  for (;; ptr++) Line 4229  for (;; ptr++)
4229    
4230        if (!inescq && ptr[1] == CHAR_MINUS)        if (!inescq && ptr[1] == CHAR_MINUS)
4231          {          {
4232          int d;          pcre_uint32 d;
4233          ptr += 2;          ptr += 2;
4234          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;
4235    
# Line 4311  for (;; ptr++) Line 4245  for (;; ptr++)
4245            break;            break;
4246            }            }
4247    
4248          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          /* Minus (hyphen) at the end of a class is treated as a literal, so put
4249            back the pointer and jump to handle the character that preceded it. */
4250    
4251            if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4252            {            {
4253            ptr = oldptr;            ptr = oldptr;
4254            goto LONE_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4255            }            }
4256    
4257            /* Otherwise, we have a potential range; pick up the next character */
4258    
4259  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4260          if (utf)          if (utf)
4261            {                           /* Braces are required because the */            {                           /* Braces are required because the */
# Line 4332  for (;; ptr++) Line 4271  for (;; ptr++)
4271    
4272          if (!inescq && d == CHAR_BACKSLASH)          if (!inescq && d == CHAR_BACKSLASH)
4273            {            {
4274            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            int descape;
4275              descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
4276            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4277    
4278            /* \b is backspace; any other special means the '-' was literal */            /* \b is backspace; any other special means the '-' was literal. */
4279    
4280            if (d < 0)            if (descape != 0)
4281              {              {
4282              if (d == -ESC_b) d = CHAR_BS; else              if (descape == ESC_b) d = CHAR_BS; else
4283                {                {
4284                ptr = oldptr;                ptr = oldptr;
4285                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4286                }                }
4287              }              }
4288            }            }
4289    
4290          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
4291          one-character ranges */          one-character ranges. */
4292    
4293          if (d < c)          if (d < c)
4294            {            {
4295            *errorcodeptr = ERR8;            *errorcodeptr = ERR8;
4296            goto FAILED;            goto FAILED;
4297            }            }
4298            if (d == c) goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4299    
4300            /* We have found a character range, so single character optimizations
4301            cannot be done anymore. Any value greater than 1 indicates that there
4302            is more than one character. */
4303    
4304          if (d == c) goto LONE_SINGLE_CHARACTER;  /* A few lines below */          class_one_char = 2;
4305    
4306          /* Remember \r or \n */          /* Remember an explicit \r or \n, and add the range to the class. */
4307    
4308          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4309    
4310          /* Since we found a character range, single character optimizations          class_has_8bitchar +=
4311          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. */  
4312    
4313  #ifdef SUPPORT_UCP          continue;   /* Go get the next char in the class */
4314  #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;  
                 }  
4315    
4316                if (occ == ocd)        /* Handle a single character - we can get here for a normal non-escape
4317                  {        char, or after \ that introduces a single character or for an apparent
4318                  *class_uchardata++ = XCL_SINGLE;        range that isn't. Only the value 1 matters for class_one_char, so don't
4319                  }        increase it if it is already 2 or more ... just in case there's a class
4320                else        with a zillion characters in it. */
4321                  {  
4322                  *class_uchardata++ = XCL_RANGE;        CLASS_SINGLE_CHARACTER:
4323                  class_uchardata += PRIV(ord2utf)(occ, class_uchardata);        if (class_one_char < 2) class_one_char++;
4324                  }  
4325                class_uchardata += PRIV(ord2utf)(ocd, class_uchardata);        /* If class_one_char is 1, we have the first single character in the
4326                }        class, and there have been no prior ranges, or XCLASS items generated by
4327              }        escapes. If this is the final character in the class, we can optimize by
4328  #endif  /* SUPPORT_UCP */        turning the item into a 1-character OP_CHAR[I] if it's positive, or
4329          OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
4330          to be set. Otherwise, there can be no first char if this item is first,
4331          whatever repeat count may follow. In the case of reqchar, save the
4332          previous value for reinstating. */
4333    
4334            /* Now record the original range, possibly modified for UCP caseless        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
4335            overlapping ranges. */          {
4336            ptr++;
4337            zeroreqchar = reqchar;
4338            zeroreqcharflags = reqcharflags;
4339    
4340            *class_uchardata++ = XCL_RANGE;          if (negate_class)
4341  #ifdef SUPPORT_UTF            {
4342  #ifndef COMPILE_PCRE8  #ifdef SUPPORT_UCP
4343            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);  
4344  #endif  #endif
4345  #else /* SUPPORT_UTF */            if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4346            *class_uchardata++ = c;            zerofirstchar = firstchar;
4347            *class_uchardata++ = d;            zerofirstcharflags = firstcharflags;
 #endif /* SUPPORT_UTF */  
4348    
4349            /* With UCP support, we are done. Without UCP support, there is no            /* For caseless UTF-8 mode when UCP support is available, check
4350            caseless matching for UTF characters > 127; we can use the bit map            whether this character has more than one other case. If so, generate
4351            for the smaller ones. As for 16 bit characters without UTF, we            a special OP_NOTPROP item instead of OP_NOTI. */
           can still use  */  
4352    
4353  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4354  #ifndef COMPILE_PCRE8            if (utf && (options & PCRE_CASELESS) != 0 &&
4355            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)  
4356              {              {
4357              if ((options & PCRE_CASELESS) == 0 || c > 127) continue;              *code++ = OP_NOTPROP;
4358              /* Adjust upper limit and fall through to set up the map */              *code++ = PT_CLIST;
4359              d = 127;              *code++ = d;
4360              }              }
4361            else            else
4362              {  #endif
4363              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. */  
4364    
         if (lengthptr == NULL) for (; c <= d; c++)  
           {  
           classbits[c/8] |= (1 << (c&7));  
           if ((options & PCRE_CASELESS) != 0)  
4365              {              {
4366              int uc = cd->fcc[c]; /* flip case */              *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4367              classbits[uc/8] |= (1 << (uc&7));  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4368                if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4369                  code += PRIV(ord2utf)(c, code);
4370                else
4371    #endif
4372                  *code++ = c;
4373              }              }
           }  
4374    
4375          continue;   /* Go get the next char in the class */            /* We are finished with this character 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 was no use of \p or \P, in other words, no use of any XCLASS  
       features, we can optimize.  
   
       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.  
       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. */  
   
       if (class_single_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)  
         {  
         ptr++;  
         zeroreqchar = reqchar;  
4376    
4377          if (negate_class)            goto END_CLASS;
           {  
           if (firstchar == REQ_UNSET) firstchar = REQ_NONE;  
           zerofirstchar = firstchar;  
           *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;  
 #ifdef SUPPORT_UTF  
           if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)  
             code += PRIV(ord2utf)(c, code);  
           else  
 #endif  
             *code++ = c;  
           goto NOT_CHAR;  
4378            }            }
4379    
4380          /* For a single, positive character, get the value into mcbuffer, and          /* For a single, positive character, get the value into mcbuffer, and
4381          then we can handle this with the normal one-character code. */          then we can handle this with the normal one-character code. */
4382    
4383  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4384          if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)          if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4385            mclength = PRIV(ord2utf)(c, mcbuffer);            mclength = PRIV(ord2utf)(c, mcbuffer);
4386          else          else
 #endif  
           {  
           mcbuffer[0] = c;  
           mclength = 1;  
           }  
         goto ONE_CHAR;  
         }       /* 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)  
4387  #endif  #endif
4388            {            {
4389            unsigned int othercase;            mcbuffer[0] = c;
4390            if ((int)(othercase = UCD_OTHERCASE(c)) != c)            mclength = 1;
             {  
             *class_uchardata++ = XCL_SINGLE;  
             class_uchardata += PRIV(ord2utf)(othercase, class_uchardata);  
             }  
4391            }            }
4392  #endif  /* SUPPORT_UCP */          goto ONE_CHAR;
4393            }       /* End of 1-char optimization */
4394    
4395          }        /* There is more than one character in the class, or an XCLASS item
4396        else        has been generated. Add this character to the class. */
 #endif  /* SUPPORT_UTF || COMPILE_PCRE16 */  
4397    
4398        /* Handle a single-byte character */        class_has_8bitchar +=
4399          {          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));  
           }  
         }  
4400        }        }
4401    
4402      /* 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.
4403      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
4404      string. */      string. */
4405    
4406      while (((c = *(++ptr)) != 0 ||      while (((c = *(++ptr)) != CHAR_NULL ||
4407             (nestptr != NULL &&             (nestptr != NULL &&
4408               (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != 0)) &&               (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != CHAR_NULL)) &&
4409             (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));             (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
4410    
4411      /* Check for missing terminating ']' */      /* Check for missing terminating ']' */
4412    
4413      if (c == 0)      if (c == CHAR_NULL)
4414        {        {
4415        *errorcodeptr = ERR6;        *errorcodeptr = ERR6;
4416        goto FAILED;        goto FAILED;
4417        }        }
4418    
4419        /* We will need an XCLASS if data has been placed in class_uchardata. In
4420        the second phase this is a sufficient test. However, in the pre-compile
4421        phase, class_uchardata gets emptied to prevent workspace overflow, so it
4422        only if the very last character in the class needs XCLASS will it contain
4423        anything at this point. For this reason, xclass gets set TRUE above when
4424        uchar_classdata is emptied, and that's why this code is the way it is here
4425        instead of just doing a test on class_uchardata below. */
4426    
4427    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4428        if (class_uchardata > class_uchardata_base) xclass = TRUE;
4429    #endif
4430    
4431      /* 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
4432      setting, whatever the repeat count. Any reqchar setting must remain      setting, whatever the repeat count. Any reqchar setting must remain
4433      unchanged after any kind of repeat. */      unchanged after any kind of repeat. */
4434    
4435      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4436      zerofirstchar = firstchar;      zerofirstchar = firstchar;
4437        zerofirstcharflags = firstcharflags;
4438      zeroreqchar = reqchar;      zeroreqchar = reqchar;
4439        zeroreqcharflags = reqcharflags;
4440    
4441      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
4442      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 4703  for (;; ptr++) Line 4492  for (;; ptr++)
4492        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
4493        }        }
4494      code += 32 / sizeof(pcre_uchar);      code += 32 / sizeof(pcre_uchar);
4495      NOT_CHAR:  
4496        END_CLASS:
4497      break;      break;
4498    
4499    
# Line 4741  for (;; ptr++) Line 4531  for (;; ptr++)
4531      if (repeat_min == 0)      if (repeat_min == 0)
4532        {        {
4533        firstchar = zerofirstchar;    /* Adjust for zero repeat */        firstchar = zerofirstchar;    /* Adjust for zero repeat */
4534          firstcharflags = zerofirstcharflags;
4535        reqchar = zeroreqchar;        /* Ditto */        reqchar = zeroreqchar;        /* Ditto */
4536          reqcharflags = zeroreqcharflags;
4537        }        }
4538    
4539      /* Remember whether this is a variable length repeat */      /* Remember whether this is a variable length repeat */
# Line 4813  for (;; ptr++) Line 4605  for (;; ptr++)
4605      if (*previous == OP_CHAR || *previous == OP_CHARI      if (*previous == OP_CHAR || *previous == OP_CHARI
4606          || *previous == OP_NOT || *previous == OP_NOTI)          || *previous == OP_NOT || *previous == OP_NOTI)
4607        {        {
4608        switch (*previous)        switch (*previous)
4609          {          {
4610          default: /* Make compiler happy. */          default: /* Make compiler happy. */
4611          case OP_CHAR:  op_type = OP_STAR - OP_STAR; break;          case OP_CHAR:  op_type = OP_STAR - OP_STAR; break;
# Line 4827  for (;; ptr++) Line 4619  for (;; ptr++)
4619        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
4620        it's a length rather than a small character. */        it's a length rather than a small character. */
4621    
4622  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4623        if (utf && NOT_FIRSTCHAR(code[-1]))        if (utf && NOT_FIRSTCHAR(code[-1]))
4624          {          {
4625          pcre_uchar *lastchar = code - 1;          pcre_uchar *lastchar = code - 1;
# Line 4844  for (;; ptr++) Line 4636  for (;; ptr++)
4636          {          {
4637          c = code[-1];          c = code[-1];
4638          if (*previous <= OP_CHARI && repeat_min > 1)          if (*previous <= OP_CHARI && repeat_min > 1)
4639            reqchar = c | req_caseopt | cd->req_varyopt;            {
4640              reqchar = c;
4641              reqcharflags = req_caseopt | cd->req_varyopt;
4642              }
4643          }          }
4644    
4645        /* 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 4901  for (;; ptr++) Line 4696  for (;; ptr++)
4696    
4697        if (repeat_max == 0) goto END_REPEAT;        if (repeat_max == 0) goto END_REPEAT;
4698    
       /*--------------------------------------------------------------------*/  
       /* 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; */  
       /*--------------------------------------------------------------------*/  
   
4699        /* Combine the op_type with the repeat_type */        /* Combine the op_type with the repeat_type */
4700    
4701        repeat_type += op_type;        repeat_type += op_type;
# Line 4963  for (;; ptr++) Line 4748  for (;; ptr++)
4748    
4749          if (repeat_max < 0)          if (repeat_max < 0)
4750            {            {
4751  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4752            if (utf && (c & UTF_LENGTH) != 0)            if (utf && (c & UTF_LENGTH) != 0)
4753              {              {
4754              memcpy(code, utf_chars, IN_UCHARS(c & 7));              memcpy(code, utf_chars, IN_UCHARS(c & 7));
# Line 4988  for (;; ptr++) Line 4773  for (;; ptr++)
4773    
4774          else if (repeat_max != repeat_min)          else if (repeat_max != repeat_min)
4775            {            {
4776  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4777            if (utf && (c & UTF_LENGTH) != 0)            if (utf && (c & UTF_LENGTH) != 0)
4778              {              {
4779              memcpy(code, utf_chars, IN_UCHARS(c & 7));              memcpy(code, utf_chars, IN_UCHARS(c & 7));
# Line 5018  for (;; ptr++) Line 4803  for (;; ptr++)
4803    
4804        /* The character or character type itself comes last in all cases. */        /* The character or character type itself comes last in all cases. */
4805    
4806  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4807        if (utf && (c & UTF_LENGTH) != 0)        if (utf && (c & UTF_LENGTH) != 0)
4808          {          {
4809          memcpy(code, utf_chars, IN_UCHARS(c & 7));          memcpy(code, utf_chars, IN_UCHARS(c & 7));
# Line 5043  for (;; ptr++) Line 4828  for (;; ptr++)
4828      /* If previous was a character class or a back reference, we put the repeat      /* If previous was a character class or a back reference, we put the repeat
4829      stuff after it, but just skip the item if the repeat was {0,0}. */      stuff after it, but just skip the item if the repeat was {0,0}. */
4830    
4831      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
              *previous == OP_NCLASS ||  
4832  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4833               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
4834  #endif  #endif
4835               *previous == OP_REF ||               *previous == OP_REF   || *previous == OP_REFI ||
4836               *previous == OP_REFI)               *previous == OP_DNREF || *previous == OP_DNREFI)
4837        {        {
4838        if (repeat_max == 0)        if (repeat_max == 0)
4839          {          {
# Line 5057  for (;; ptr++) Line 4841  for (;; ptr++)
4841          goto END_REPEAT;          goto END_REPEAT;
4842          }          }
4843    
       /*--------------------------------------------------------------------*/  
       /* 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; */  
       /*--------------------------------------------------------------------*/  
   
4844        if (repeat_min == 0 && repeat_max == -1)        if (repeat_min == 0 && repeat_max == -1)
4845          *code++ = OP_CRSTAR + repeat_type;          *code++ = OP_CRSTAR + repeat_type;
4846        else if (repeat_min == 1 && repeat_max == -1)        else if (repeat_min == 1 && repeat_max == -1)
# Line 5226  for (;; ptr++) Line 5000  for (;; ptr++)
5000    
5001            else            else
5002              {              {
5003              if (groupsetfirstchar && reqchar < 0) reqchar = firstchar;              if (groupsetfirstchar && reqcharflags < 0)
5004                  {
5005                  reqchar = firstchar;
5006                  reqcharflags = firstcharflags;
5007                  }
5008    
5009              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
5010                {                {
# Line 5405  for (;; ptr++) Line 5183  for (;; ptr++)
5183              pcre_uchar *scode = bracode;              pcre_uchar *scode = bracode;
5184              do              do
5185                {                {
5186                if (could_be_empty_branch(scode, ketcode, utf, cd))                if (could_be_empty_branch(scode, ketcode, utf, cd, NULL))
5187                  {                  {
5188                  *bracode += OP_SBRA - OP_BRA;                  *bracode += OP_SBRA - OP_BRA;
5189                  break;                  break;
# Line 5593  for (;; ptr++) Line 5371  for (;; ptr++)
5371        ptr++;        ptr++;
5372        while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;        while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
5373        namelen = (int)(ptr - name);        namelen = (int)(ptr - name);
5374    
5375        /* It appears that Perl allows any characters whatsoever, other than        /* It appears that Perl allows any characters whatsoever, other than
5376        a closing parenthesis, to appear in arguments, so we no longer insist on        a closing parenthesis, to appear in arguments, so we no longer insist on
5377        letters, digits, and underscores. */        letters, digits, and underscores. */
# Line 5601  for (;; ptr++) Line 5379  for (;; ptr++)
5379        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
5380          {          {
5381          arg = ++ptr;          arg = ++ptr;
5382          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5383          arglen = (int)(ptr - arg);          arglen = (int)(ptr - arg);
5384          if (arglen > (int)MAX_MARK)          if ((unsigned int)arglen > MAX_MARK)
5385            {            {
5386            *errorcodeptr = ERR75;            *errorcodeptr = ERR75;
5387            goto FAILED;            goto FAILED;
5388            }            }
5389          }          }
5390    
5391        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
# Line 5623  for (;; ptr++) Line 5401  for (;; ptr++)
5401          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
5402              STRNCMP_UC_C8(name, vn, namelen) == 0)              STRNCMP_UC_C8(name, vn, namelen) == 0)
5403            {            {
5404              int setverb;
5405    
5406            /* Check for open captures before ACCEPT and convert it to            /* Check for open captures before ACCEPT and convert it to
5407            ASSERT_ACCEPT if in an assertion. */            ASSERT_ACCEPT if in an assertion. */
5408    
# Line 5640  for (;; ptr++) Line 5420  for (;; ptr++)
5420                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5421                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5422                }                }
5423              *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;              setverb = *code++ =
5424                  (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5425    
5426              /* Do not set firstchar after *ACCEPT */              /* Do not set firstchar after *ACCEPT */
5427              if (firstchar == REQ_UNSET) firstchar = REQ_NONE;              if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
5428              }              }
5429    
5430            /* Handle other cases with/without an argument */            /* Handle other cases with/without an argument */
# Line 5655  for (;; ptr++) Line 5436  for (;; ptr++)
5436                *errorcodeptr = ERR66;                *errorcodeptr = ERR66;
5437                goto FAILED;                goto FAILED;
5438                }                }
5439              *code = verbs[i].op;              setverb = *code++ = verbs[i].op;
             if (*code++ == OP_THEN) cd->external_flags |= PCRE_HASTHEN;  
5440              }              }
5441    
5442            else            else
# Line 5666  for (;; ptr++) Line 5446  for (;; ptr++)
5446                *errorcodeptr = ERR59;                *errorcodeptr = ERR59;
5447                goto FAILED;                goto FAILED;
5448                }                }
5449              *code = verbs[i].op_arg;              setverb = *code++ = verbs[i].op_arg;
             if (*code++ == OP_THEN_ARG) cd->external_flags |= PCRE_HASTHEN;  
5450              *code++ = arglen;              *code++ = arglen;
5451              memcpy(code, arg, IN_UCHARS(arglen));              memcpy(code, arg, IN_UCHARS(arglen));
5452              code += arglen;              code += arglen;
5453              *code++ = 0;              *code++ = 0;
5454              }              }
5455    
5456              switch (setverb)
5457                {
5458                case OP_THEN:
5459                case OP_THEN_ARG:
5460                cd->external_flags |= PCRE_HASTHEN;
5461                break;
5462    
5463                case OP_PRUNE:
5464                case OP_PRUNE_ARG:
5465                case OP_SKIP:
5466                case OP_SKIP_ARG:
5467                cd->had_pruneorskip = TRUE;
5468                break;
5469                }
5470    
5471            break;  /* Found verb, exit loop */            break;  /* Found verb, exit loop */
5472            }            }
5473    
# Line 5699  for (;; ptr++) Line 5493  for (;; ptr++)
5493          {          {
5494          case CHAR_NUMBER_SIGN:                 /* Comment; skip to ket */          case CHAR_NUMBER_SIGN:                 /* Comment; skip to ket */
5495          ptr++;          ptr++;
5496          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5497          if (*ptr == 0)          if (*ptr == CHAR_NULL)
5498            {            {
5499            *errorcodeptr = ERR18;            *errorcodeptr = ERR18;
5500            goto FAILED;            goto FAILED;
# Line 5723  for (;; ptr++) Line 5517  for (;; ptr++)
5517          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5518          case CHAR_LEFT_PARENTHESIS:          case CHAR_LEFT_PARENTHESIS:
5519          bravalue = OP_COND;       /* Conditional group */          bravalue = OP_COND;       /* Conditional group */
5520            tempptr = ptr;
5521    
5522          /* A condition can be an assertion, a number (referring to a numbered          /* A condition can be an assertion, a number (referring to a numbered
5523          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 5735  for (;; ptr++) Line 5530  for (;; ptr++)
5530          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
5531          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.
5532          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
5533          cases. */          cases.
5534    
5535            For compatibility with auto-callouts, we allow a callout to be
5536            specified before a condition that is an assertion. First, check for the
5537            syntax of a callout; if found, adjust the temporary pointer that is
5538            used to check for an assertion condition. That's all that is needed! */
5539    
5540            if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
5541              {
5542              for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
5543              if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
5544                tempptr += i + 1;
5545              }
5546    
5547          /* For conditions that are assertions, check the syntax, and then exit          /* For conditions that are assertions, check the syntax, and then exit
5548          the switch. This will take control down to where bracketed groups,          the switch. This will take control down to where bracketed groups,
5549          including assertions, are processed. */          including assertions, are processed. */
5550    
5551          if (ptr[1] == CHAR_QUESTION_MARK && (ptr[2] == CHAR_EQUALS_SIGN ||          if (tempptr[1] == CHAR_QUESTION_MARK &&
5552              ptr[2] == CHAR_EXCLAMATION_MARK || ptr[2] == CHAR_LESS_THAN_SIGN))                (tempptr[2] == CHAR_EQUALS_SIGN ||
5553                   tempptr[2] == CHAR_EXCLAMATION_MARK ||
5554                   tempptr[2] == CHAR_LESS_THAN_SIGN))
5555            break;            break;
5556    
5557          /* 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 5776  for (;; ptr++) Line 5585  for (;; ptr++)
5585            }            }
5586          else          else
5587            {            {
5588            terminator = 0;            terminator = CHAR_NULL;
5589            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);
5590            }            }
5591    
# Line 5796  for (;; ptr++) Line 5605  for (;; ptr++)
5605          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
5606            {            {
5607            if (recno >= 0)            if (recno >= 0)
5608              recno = (IS_DIGIT(*ptr))? recno * 10 + *ptr - CHAR_0 : -1;              recno = (IS_DIGIT(*ptr))? recno * 10 + (int)(*ptr - CHAR_0) : -1;
5609            ptr++;            ptr++;
5610            }            }
5611          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
5612    
5613          if ((terminator > 0 && *ptr++ != terminator) ||          if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
5614              *ptr++ != CHAR_RIGHT_PARENTHESIS)              *ptr++ != CHAR_RIGHT_PARENTHESIS)
5615            {            {
5616            ptr--;      /* Error offset */            ptr--;      /* Error offset */
# Line 5848  for (;; ptr++) Line 5657  for (;; ptr++)
5657            slot += cd->name_entry_size;            slot += cd->name_entry_size;
5658            }            }
5659    
5660          /* Found a previous named subpattern */          /* Found the named subpattern */
5661    
5662          if (i < cd->names_found)          if (i < cd->names_found)
5663            {            {
# Line 5857  for (;; ptr++) Line 5666  for (;; ptr++)
5666            code[1+LINK_SIZE]++;            code[1+LINK_SIZE]++;
5667            }            }
5668    
5669          /* Search the pattern for a forward reference */          /* If terminator == CHAR_NULL it means that the name followed directly
5670            after the opening parenthesis [e.g. (?(abc)...] and in this case there
5671          else if ((i = find_parens(cd, name, namelen,          are some further alternatives to try. For the cases where terminator !=
5672                          (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  
5673          now checked all the possibilities, so give an error. */          now checked all the possibilities, so give an error. */
5674    
5675          else if (terminator != 0)          else if (terminator != CHAR_NULL)
5676            {            {
5677            *errorcodeptr = ERR15;            *errorcodeptr = ERR15;
5678            goto FAILED;            goto FAILED;
# Line 6029  for (;; ptr++) Line 5829  for (;; ptr++)
5829          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5830          DEFINE_NAME:    /* Come here from (?< handling */          DEFINE_NAME:    /* Come here from (?< handling */
5831          case CHAR_APOSTROPHE:          case CHAR_APOSTROPHE:
5832            {          terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
5833            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
5834              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;          name = ++ptr;
           name = ++ptr;  
5835    
5836            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5837            namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
5838    
5839            /* In the pre-compile phase, just do a syntax check. */          /* In the pre-compile phase, do a syntax check, remember the longest
5840            name, and then remember the group in a vector, expanding it if
5841            necessary. Duplicates for the same number are skipped; other duplicates
5842            are checked for validity. In the actual compile, there is nothing to
5843            do. */
5844    
5845            if (lengthptr != NULL)          if (lengthptr != NULL)
5846              {
5847              named_group *ng;
5848              pcre_uint32 number = cd->bracount + 1;
5849    
5850              if (*ptr != (pcre_uchar)terminator)
5851              {              {
5852              if (*ptr != terminator)              *errorcodeptr = ERR42;
5853                {              goto FAILED;
5854                *errorcodeptr = ERR42;              }
5855                goto FAILED;  
5856                }            if (cd->names_found >= MAX_NAME_COUNT)
5857              if (cd->names_found >= MAX_NAME_COUNT)              {
5858                *errorcodeptr = ERR49;
5859                goto FAILED;
5860                }
5861    
5862              if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
5863                {
5864                cd->name_entry_size = namelen + IMM2_SIZE + 1;
5865                if (namelen > MAX_NAME_SIZE)
5866                {                {
5867                *errorcodeptr = ERR49;                *errorcodeptr = ERR48;
5868                goto FAILED;                goto FAILED;
5869                }                }
             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;  
                 }  
               }  
5870              }              }
5871    
5872            /* In the real compile, create the entry in the table, maintaining            /* Scan the list to check for duplicates. For duplicate names, if the
5873            alphabetical order. Duplicate names for different numbers are            number is the same, break the loop, which causes the name to be
5874            permitted only if PCRE_DUPNAMES is set. Duplicate names for the same            discarded; otherwise, if DUPNAMES is not set, give an error.
5875            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
5876            appears in the pattern.) In either event, a duplicate name results in            scanning in case this is a duplicate with the same number. For
5877            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. */
5878            is because the number of names, and hence the table size, is computed  
5879            in the pre-compile, and it affects various numbers and pointers which            ng = cd->named_groups;
5880            would all have to be modified, and the compiled code moved down, if            for (i = 0; i < cd->names_found; i++, ng++)
5881            duplicates with the same number were omitted from the table. This              {
5882            doesn't seem worth the hassle. However, *different* names for the              if (namelen == ng->length &&
5883            same number are not permitted. */                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)
5884                  {
5885            else                if (ng->number == number) break;
5886              {                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)  
5887                  {                  {
5888                  if (slot[IMM2_SIZE+namelen] == 0)                  *errorcodeptr = ERR43;
5889                    {                  goto FAILED;
                   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)  
                 {  
                 memmove(slot + cd->name_entry_size, slot,  
                   IN_UCHARS((cd->names_found - i) * cd->name_entry_size));  
                 break;  
5890                  }                  }
5891                  cd->dupnames = TRUE;  /* Duplicate names exist */
5892                  }
5893                else if (ng->number == number)
5894                  {
5895                  *errorcodeptr = ERR65;
5896                  goto FAILED;
5897                  }
5898                }
5899    
5900                /* Continue the loop for a later or duplicate name */            if (i >= cd->names_found)     /* Not a duplicate with same number */
5901                {
5902                slot += cd->name_entry_size;              /* Increase the list size if necessary */
5903                }  
5904                if (cd->names_found >= cd->named_group_list_size)
             /* For non-duplicate names, check for a duplicate number before  
             adding the new name. */  
   
             if (!dupname)  
5905                {                {
5906                pcre_uchar *cslot = cd->name_table;                int newsize = cd->named_group_list_size * 2;
5907                for (i = 0; i < cd->names_found; i++)                named_group *newspace = (PUBL(malloc))
5908                    (newsize * sizeof(named_group));
5909    
5910                  if (newspace == NULL)
5911                  {                  {
5912                  if (cslot != slot)                  *errorcodeptr = ERR21;
5913                    {                  goto FAILED;
5914                    if (GET2(cslot, 0) == cd->bracount + 1)                  }
5915                      {  
5916                      *errorcodeptr = ERR65;                memcpy(newspace, cd->named_groups,
5917                      goto FAILED;                  cd->named_group_list_size * sizeof(named_group));
5918                      }                if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
5919                    }                  (PUBL(free))((void *)cd->named_groups);
5920                  else i--;                cd->named_groups = newspace;
5921                  cslot += cd->name_entry_size;                cd->named_group_list_size = newsize;
5922                  }                }
5923                }  
5924                cd->named_groups[cd->names_found].name = name;
5925              PUT2(slot, 0, cd->bracount + 1);              cd->named_groups[cd->names_found].length = namelen;
5926              memcpy(slot + IMM2_SIZE, name, IN_UCHARS(namelen));              cd->named_groups[cd->names_found].number = number;
5927              slot[IMM2_SIZE + namelen] = 0;              cd->names_found++;
5928              }              }
5929            }            }
5930    
5931          /* 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 ' */  
5932          goto NUMBERED_GROUP;          goto NUMBERED_GROUP;
5933    
5934    
# Line 6176  for (;; ptr++) Line 5958  for (;; ptr++)
5958    
5959          if (lengthptr != NULL)          if (lengthptr != NULL)
5960            {            {
5961            const pcre_uchar *temp;            named_group *ng;
5962    
5963            if (namelen == 0)            if (namelen == 0)
5964              {              {
5965              *errorcodeptr = ERR62;              *errorcodeptr = ERR62;
5966              goto FAILED;              goto FAILED;
5967              }              }
5968            if (*ptr != terminator)            if (*ptr != (pcre_uchar)terminator)
5969              {              {
5970              *errorcodeptr = ERR42;              *errorcodeptr = ERR42;
5971              goto FAILED;              goto FAILED;
# Line 6194  for (;; ptr++) Line 5976  for (;; ptr++)
5976              goto FAILED;              goto FAILED;
5977              }              }
5978    
5979            /* 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
5980            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
5981            pattern to find the number. It is important that we scan it only as            number. If the name is not found, set the value to 0 for a forward
5982            far as we have got because the syntax of named subpatterns has not            reference. */
5983            been checked for the rest of the pattern, and find_parens() assumes  
5984            correct syntax. In any case, it's a waste of resources to scan            ng = cd->named_groups;
5985            further. We stop the scan at the current point by temporarily            for (i = 0; i < cd->names_found; i++, ng++)
5986            adjusting the value of cd->endpattern. */              {
5987                if (namelen == ng->length &&
5988            temp = cd->end_pattern;                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)
5989            cd->end_pattern = ptr;                break;
5990            recno = find_parens(cd, name, namelen,              }
5991              (options & PCRE_EXTENDED) != 0, utf);            recno = (i < cd->names_found)? ng->number : 0;
5992            cd->end_pattern = temp;  
5993            if (recno < 0) recno = 0;    /* Forward ref; set dummy number */            /* Count named back references. */
5994    
5995              if (!is_recurse) cd->namedrefcount++;
5996            }            }
5997    
5998          /* 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
5999          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
6000          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
6001          the comparison will fail without reading beyond the table entry. */          comparison will fail without reading beyond the table entry. */
6002    
6003          else          else
6004            {            {
# Line 6227  for (;; ptr++) Line 6011  for (;; ptr++)
6011              slot += cd->name_entry_size;              slot += cd->name_entry_size;
6012              }              }
6013    
6014            if (i < cd->names_found)         /* Back reference */            if (i < cd->names_found)
6015              {              {
6016              recno = GET2(slot, 0);              recno = GET2(slot, 0);
6017              }              }
6018            else if ((recno =                /* Forward back reference */            else
                     find_parens(cd, name, namelen,  
                       (options & PCRE_EXTENDED) != 0, utf)) <= 0)  
6019              {              {
6020              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
6021              goto FAILED;              goto FAILED;
6022              }              }
6023            }            }
6024    
6025          /* In both phases, we can now go to the code than handles numerical          /* In both phases, for recursions, we can now go to the code than
6026          recursion or backreferences. */          handles numerical recursion. */
6027    
6028          if (is_recurse) goto HANDLE_RECURSION;          if (is_recurse) goto HANDLE_RECURSION;
6029            else goto HANDLE_REFERENCE;  
6030            /* In the second pass we must see if the name is duplicated. If so, we
6031            generate a different opcode. */
6032    
6033            if (lengthptr == NULL && cd->dupnames)
6034              {
6035              int count = 1;
6036              unsigned int index = i;
6037              pcre_uchar *cslot = slot + cd->name_entry_size;
6038    
6039              for (i++; i < cd->names_found; i++)
6040                {
6041                if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break;
6042                count++;
6043                cslot += cd->name_entry_size;
6044                }
6045    
6046              if (count > 1)
6047                {
6048                if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
6049                previous = code;
6050                *code++ = ((options & PCRE_CASELESS) != 0)? OP_DNREFI : OP_DNREF;
6051                PUT2INC(code, 0, index);
6052                PUT2INC(code, 0, count);
6053    
6054                /* Process each potentially referenced group. */
6055    
6056                for (; slot < cslot; slot += cd->name_entry_size)
6057                  {
6058                  open_capitem *oc;
6059                  recno = GET2(slot, 0);
6060                  cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6061                  if (recno > cd->top_backref) cd->top_backref = recno;
6062    
6063                  /* Check to see if this back reference is recursive, that it, it
6064                  is inside the group that it references. A flag is set so that the
6065                  group can be made atomic. */
6066    
6067                  for (oc = cd->open_caps; oc != NULL; oc = oc->next)
6068                    {
6069                    if (oc->number == recno)
6070                      {
6071                      oc->flag = TRUE;
6072                      break;
6073                      }
6074                    }
6075                  }
6076    
6077                continue;  /* End of back ref handling */
6078                }
6079              }
6080    
6081            /* First pass, or a non-duplicated name. */
6082    
6083            goto HANDLE_REFERENCE;
6084    
6085    
6086          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
6087          case CHAR_R:              /* Recursion */          case CHAR_R:              /* Recursion */
# Line 6289  for (;; ptr++) Line 6125  for (;; ptr++)
6125            while(IS_DIGIT(*ptr))            while(IS_DIGIT(*ptr))
6126              recno = recno * 10 + *ptr++ - CHAR_0;              recno = recno * 10 + *ptr++ - CHAR_0;
6127    
6128            if (*ptr != terminator)            if (*ptr != (pcre_uchar)terminator)
6129              {              {
6130              *errorcodeptr = ERR29;              *errorcodeptr = ERR29;
6131              goto FAILED;              goto FAILED;
# Line 6343  for (;; ptr++) Line 6179  for (;; ptr++)
6179    
6180              if (called == NULL)              if (called == NULL)
6181                {                {
6182                if (find_parens(cd, NULL, recno,                if (recno > cd->final_bracount)
                     (options & PCRE_EXTENDED) != 0, utf) < 0)  
6183                  {                  {
6184                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
6185                  goto FAILED;                  goto FAILED;
# Line 6393  for (;; ptr++) Line 6228  for (;; ptr++)
6228    
6229          /* Can't determine a first byte now */          /* Can't determine a first byte now */
6230    
6231          if (firstchar == REQ_UNSET) firstchar = REQ_NONE;          if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
6232          continue;          continue;
6233    
6234    
# Line 6527  for (;; ptr++) Line 6362  for (;; ptr++)
6362           cond_depth +           cond_depth +
6363             ((bravalue == OP_COND)?1:0),   /* Depth of condition subpatterns */             ((bravalue == OP_COND)?1:0),   /* Depth of condition subpatterns */
6364           &subfirstchar,                   /* For possible first char */           &subfirstchar,                   /* For possible first char */
6365             &subfirstcharflags,
6366           &subreqchar,                     /* For possible last char */           &subreqchar,                     /* For possible last char */
6367             &subreqcharflags,
6368           bcptr,                           /* Current branch chain */           bcptr,                           /* Current branch chain */
6369           cd,                              /* Tables block */           cd,                              /* Tables block */
6370           (lengthptr == NULL)? NULL :      /* Actual compile phase */           (lengthptr == NULL)? NULL :      /* Actual compile phase */
# Line 6588  for (;; ptr++) Line 6425  for (;; ptr++)
6425            *errorcodeptr = ERR27;            *errorcodeptr = ERR27;
6426            goto FAILED;            goto FAILED;
6427            }            }
6428          if (condcount == 1) subfirstchar = subreqchar = REQ_NONE;          if (condcount == 1) subfirstcharflags = subreqcharflags = REQ_NONE;
6429          }          }
6430        }        }
6431    
# Line 6637  for (;; ptr++) Line 6474  for (;; ptr++)
6474      back off. */      back off. */
6475    
6476      zeroreqchar = reqchar;      zeroreqchar = reqchar;
6477        zeroreqcharflags = reqcharflags;
6478      zerofirstchar = firstchar;      zerofirstchar = firstchar;
6479        zerofirstcharflags = firstcharflags;
6480      groupsetfirstchar = FALSE;      groupsetfirstchar = FALSE;
6481    
6482      if (bravalue >= OP_ONCE)      if (bravalue >= OP_ONCE)
# Line 6648  for (;; ptr++) Line 6487  for (;; ptr++)
6487        no firstchar, set "none" for the whole branch. In both cases, a zero        no firstchar, set "none" for the whole branch. In both cases, a zero
6488        repeat forces firstchar to "none". */        repeat forces firstchar to "none". */
6489    
6490        if (firstchar == REQ_UNSET)        if (firstcharflags == REQ_UNSET)
6491          {          {
6492          if (subfirstchar >= 0)          if (subfirstcharflags >= 0)
6493            {            {
6494            firstchar = subfirstchar;            firstchar = subfirstchar;
6495              firstcharflags = subfirstcharflags;
6496            groupsetfirstchar = TRUE;            groupsetfirstchar = TRUE;
6497            }            }
6498          else firstchar = REQ_NONE;          else firstcharflags = REQ_NONE;
6499          zerofirstchar = REQ_NONE;          zerofirstcharflags = REQ_NONE;
6500          }          }
6501    
6502        /* If firstchar was previously set, convert the subpattern's firstchar        /* If firstchar was previously set, convert the subpattern's firstchar
6503        into reqchar if there wasn't one, using the vary flag that was in        into reqchar if there wasn't one, using the vary flag that was in
6504        existence beforehand. */        existence beforehand. */
6505    
6506        else if (subfirstchar >= 0 && subreqchar < 0)        else if (subfirstcharflags >= 0 && subreqcharflags < 0)
6507          subreqchar = subfirstchar | tempreqvary;          {
6508            subreqchar = subfirstchar;
6509            subreqcharflags = subfirstcharflags | tempreqvary;
6510            }
6511    
6512        /* If the subpattern set a required byte (or set a first byte that isn't        /* If the subpattern set a required byte (or set a first byte that isn't
6513        really the first byte - see above), set it. */        really the first byte - see above), set it. */
6514    
6515        if (subreqchar >= 0) reqchar = subreqchar;        if (subreqcharflags >= 0)
6516            {
6517            reqchar = subreqchar;
6518            reqcharflags = subreqcharflags;
6519            }
6520        }        }
6521    
6522      /* For a forward assertion, we take the reqchar, if set. This can be      /* For a forward assertion, we take the reqchar, if set. This can be
# Line 6680  for (;; ptr++) Line 6527  for (;; ptr++)
6527      of a firstchar. This is overcome by a scan at the end if there's no      of a firstchar. This is overcome by a scan at the end if there's no
6528      firstchar, looking for an asserted first char. */      firstchar, looking for an asserted first char. */
6529    
6530      else if (bravalue == OP_ASSERT && subreqchar >= 0) reqchar = subreqchar;      else if (bravalue == OP_ASSERT && subreqcharflags >= 0)
6531          {
6532          reqchar = subreqchar;
6533          reqcharflags = subreqcharflags;
6534          }
6535      break;     /* End of processing '(' */      break;     /* End of processing '(' */
6536    
6537    
# Line 6688  for (;; ptr++) Line 6539  for (;; ptr++)
6539      /* Handle metasequences introduced by \. For ones like \d, the ESC_ values      /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
6540      are arranged to be the negation of the corresponding OP_values in the      are arranged to be the negation of the corresponding OP_values in the
6541      default case when PCRE_UCP is not set. For the back references, the values      default case when PCRE_UCP is not set. For the back references, the values
6542      are ESC_REF plus the reference number. Only back references and those types      are negative the reference number. Only back references and those types
6543      that consume a character may be repeated. We can test for values between      that consume a character may be repeated. We can test for values between
6544      ESC_b and ESC_Z for the latter; this may have to change if any new ones are      ESC_b and ESC_Z for the latter; this may have to change if any new ones are
6545      ever created. */      ever created. */
6546    
6547      case CHAR_BACKSLASH:      case CHAR_BACKSLASH:
6548      tempptr = ptr;      tempptr = ptr;
6549      c = check_escape(&ptr, errorcodeptr, cd->bracount, options, FALSE);      escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options, FALSE);
6550      if (*errorcodeptr != 0) goto FAILED;      if (*errorcodeptr != 0) goto FAILED;
6551    
6552      if (c < 0)      if (escape == 0)                  /* The escape coded a single character */
6553          c = ec;
6554        else
6555        {        {
6556        if (-c == ESC_Q)            /* Handle start of quoted string */        if (escape == ESC_Q)            /* Handle start of quoted string */
6557          {          {
6558          if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)          if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
6559            ptr += 2;               /* avoid empty string */            ptr += 2;               /* avoid empty string */
# Line 6708  for (;; ptr++) Line 6561  for (;; ptr++)
6561          continue;          continue;
6562          }          }
6563    
6564        if (-c == ESC_E) continue;  /* Perl ignores an orphan \E */        if (escape == ESC_E) continue;  /* Perl ignores an orphan \E */
6565    
6566        /* For metasequences that actually match a character, we disable the        /* For metasequences that actually match a character, we disable the
6567        setting of a first character if it hasn't already been set. */        setting of a first character if it hasn't already been set. */
6568</