/[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 1369 by ph10, Tue Oct 8 15:06:46 2013 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-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    
658  /* Definition to allow mutual recursion */  /* This table is used to check whether auto-possessification is possible
659    between adjacent character-type opcodes. The left-hand (repeated) opcode is
660    used to select the row, and the right-hand opcode is use to select the column.
661    A value of 1 means that auto-possessification is OK. For example, the second
662    value in the first row means that \D+\d can be turned into \D++\d.
663    
664    The Unicode property types (\P and \p) have to be present to fill out the table
665    because of what their opcode values are, but the table values should always be
666    zero because property types are handled separately in the code. The last four
667    columns apply to items that cannot be repeated, so there is no need to have
668    rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
669    *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
670    
671    #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
672    #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
673    
674    static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
675    /* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
676      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
677      { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
678      { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
679      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
680      { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
681      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
682      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
683      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
684      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
685      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
686      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
687      { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
688      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
689      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
690      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
691      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
692      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
693    };
694    
695  static BOOL  
696    compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,  /* This table is used to check whether auto-possessification is possible
697      int *, int *, branch_chain *, compile_data *, int *);  between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
698    left-hand (repeated) opcode is used to select the row, and the right-hand
699    opcode is used to select the column. The values are as follows:
700    
701      0   Always return FALSE (never auto-possessify)
702      1   Character groups are distinct (possessify if both are OP_PROP)
703      2   Check character categories in the same group (general or particular)
704      3   TRUE if the two opcodes are not the same (PROP vs NOTPROP)
705    
706      4   Check left general category vs right particular category
707      5   Check right general category vs left particular category
708    
709      6   Left alphanum vs right general category
710      7   Left space vs right general category
711      8   Left word vs right general category
712    
713      9   Right alphanum vs left general category
714     10   Right space vs left general category
715     11   Right word vs left general category
716    
717     12   Left alphanum vs right particular category
718     13   Left space vs right particular category
719     14   Left word vs right particular category
720    
721     15   Right alphanum vs left particular category
722     16   Right space vs left particular category
723     17   Right word vs left particular category
724    */
725    
726    static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
727    /* ANY LAMP GC  PC  SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
728      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_ANY */
729      { 0,  3,  0,  0,  0,    3,    1,      1,   0,    0,   0 },  /* PT_LAMP */
730      { 0,  0,  2,  4,  0,    9,   10,     10,  11,    0,   0 },  /* PT_GC */
731      { 0,  0,  5,  2,  0,   15,   16,     16,  17,    0,   0 },  /* PT_PC */
732      { 0,  0,  0,  0,  2,    0,    0,      0,   0,    0,   0 },  /* PT_SC */
733      { 0,  3,  6, 12,  0,    3,    1,      1,   0,    0,   0 },  /* PT_ALNUM */
734      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_SPACE */
735      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_PXSPACE */
736      { 0,  0,  8, 14,  0,    0,    1,      1,   3,    0,   0 },  /* PT_WORD */
737      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_CLIST */
738      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   3 }   /* PT_UCNC */
739    };
740    
741    /* This table is used to check whether auto-possessification is possible
742    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
743    specifies a general category and the other specifies a particular category. The
744    row is selected by the general category and the column by the particular
745    category. The value is 1 if the particular category is not part of the general
746    category. */
747    
748    static const pcre_uint8 catposstab[7][30] = {
749    /* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
750      { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* C */
751      { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* L */
752      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* M */
753      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* N */
754      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },  /* P */
755      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 },  /* S */
756      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 }   /* Z */
757    };
758    
759    /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
760    a general or particular category. The properties in each row are those
761    that apply to the character set in question. Duplication means that a little
762    unnecessary work is done when checking, but this keeps things much simpler
763    because they can all use the same code. For more details see the comment where
764    this table is used.
765    
766    Note: SPACE and PXSPACE used to be different because Perl excluded VT from
767    "space", but from Perl 5.18 it's included, so both categories are treated the
768    same here. */
769    
770    static const pcre_uint8 posspropstab[3][4] = {
771      { ucp_L, ucp_N, ucp_N, ucp_Nl },  /* ALNUM, 3rd and 4th values redundant */
772      { ucp_Z, ucp_Z, ucp_C, ucp_Cc },  /* SPACE and PXSPACE, 2nd value redundant */
773      { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
774    };
775    
776    
777    
# Line 657  find_error_text(int n) Line 794  find_error_text(int n)
794  const char *s = error_texts;  const char *s = error_texts;
795  for (; n > 0; n--)  for (; n > 0; n--)
796    {    {
797    while (*s++ != 0) {};    while (*s++ != CHAR_NULL) {};
798    if (*s == 0) return "Error text not found (please report)";    if (*s == CHAR_NULL) return "Error text not found (please report)";
799    }    }
800  return s;  return s;
801  }  }
802    
803    
804    
805  /*************************************************  /*************************************************
806  *           Expand the workspace                 *  *           Expand the workspace                 *
807  *************************************************/  *************************************************/
# Line 741  return (*p == CHAR_RIGHT_CURLY_BRACKET); Line 879  return (*p == CHAR_RIGHT_CURLY_BRACKET);
879  *************************************************/  *************************************************/
880    
881  /* This function is called when a \ has been encountered. It either returns a  /* This function is called when a \ has been encountered. It either returns a
882  positive value for a simple escape such as \n, or a negative value which  positive value for a simple escape such as \n, or 0 for a data character which
883  encodes one of the more complicated things such as \d. A backreference to group  will be placed in chptr. A backreference to group n is returned as negative n.
884  n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When  When UTF-8 is enabled, a positive value greater than 255 may be returned in
885  UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,  chptr. On entry, ptr is pointing at the \. On exit, it is on the final
886  ptr is pointing at the \. On exit, it is on the final character of the escape  character of the escape sequence.
 sequence.  
887    
888  Arguments:  Arguments:
889    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
890      chptr          points to a returned data character
891    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
892    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
893    options        the options bits    options        the options bits
894    isclass        TRUE if inside a character class    isclass        TRUE if inside a character class
895    
896  Returns:         zero or positive => a data character  Returns:         zero => a data character
897                   negative => a special escape sequence                   positive => a special escape sequence
898                     negative => a back reference
899                   on error, errorcodeptr is set                   on error, errorcodeptr is set
900  */  */
901    
902  static int  static int
903  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
904    int options, BOOL isclass)    int bracount, int options, BOOL isclass)
905  {  {
906  /* PCRE_UTF16 has the same value as PCRE_UTF8. */  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
907  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
908  const pcre_uchar *ptr = *ptrptr + 1;  const pcre_uchar *ptr = *ptrptr + 1;
909  pcre_int32 c;  pcre_uint32 c;
910    int escape = 0;
911  int i;  int i;
912    
913  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
# Line 775  ptr--;                            /* Set Line 915  ptr--;                            /* Set
915    
916  /* If backslash is at the end of the pattern, it's an error. */  /* If backslash is at the end of the pattern, it's an error. */
917    
918  if (c == 0) *errorcodeptr = ERR1;  if (c == CHAR_NULL) *errorcodeptr = ERR1;
919    
920  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
921  in a table. A non-zero result is something that can be returned immediately.  in a table. A non-zero result is something that can be returned immediately.
# Line 784  Otherwise further processing may be requ Line 924  Otherwise further processing may be requ
924  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
925  /* Not alphanumeric */  /* Not alphanumeric */
926  else if (c < CHAR_0 || c > CHAR_z) {}  else if (c < CHAR_0 || c > CHAR_z) {}
927  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if ((i = escapes[c - CHAR_0]) != 0)
928      { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
929    
930  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
931  /* Not alphanumeric */  /* Not alphanumeric */
932  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)) {}
933  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; }
934  #endif  #endif
935    
936  /* 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 938  else if ((i = escapes[c - 0x48]) != 0)
938  else  else
939    {    {
940    const pcre_uchar *oldptr;    const pcre_uchar *oldptr;
941    BOOL braced, negated;    BOOL braced, negated, overflow;
942      int s;
943    
944    switch (c)    switch (c)
945      {      {
# Line 822  else Line 964  else
964          c = 0;          c = 0;
965          for (i = 0; i < 4; ++i)          for (i = 0; i < 4; ++i)
966            {            {
967            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
968  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
969            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
970            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 973  else
973            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
974  #endif  #endif
975            }            }
976    
977    #if defined COMPILE_PCRE8
978            if (c > (utf ? 0x10ffffU : 0xffU))
979    #elif defined COMPILE_PCRE16
980            if (c > (utf ? 0x10ffffU : 0xffffU))
981    #elif defined COMPILE_PCRE32
982            if (utf && c > 0x10ffffU)
983    #endif
984              {
985              *errorcodeptr = ERR76;
986              }
987            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
988          }          }
989        }        }
990      else      else
# Line 857  else Line 1011  else
1011      (3) For Oniguruma compatibility we also support \g followed by a name or a      (3) For Oniguruma compatibility we also support \g followed by a name or a
1012      number either in angle brackets or in single quotes. However, these are      number either in angle brackets or in single quotes. However, these are
1013      (possibly recursive) subroutine calls, _not_ backreferences. Just return      (possibly recursive) subroutine calls, _not_ backreferences. Just return
1014      the -ESC_g code (cf \k). */      the ESC_g code (cf \k). */
1015    
1016      case CHAR_g:      case CHAR_g:
1017      if (isclass) break;      if (isclass) break;
1018      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
1019        {        {
1020        c = -ESC_g;        escape = ESC_g;
1021        break;        break;
1022        }        }
1023    
# Line 872  else Line 1026  else
1026      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1027        {        {
1028        const pcre_uchar *p;        const pcre_uchar *p;
1029        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++)
1030          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1031        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1032          {          {
1033          c = -ESC_k;          escape = ESC_k;
1034          break;          break;
1035          }          }
1036        braced = TRUE;        braced = TRUE;
# Line 892  else Line 1046  else
1046      else negated = FALSE;      else negated = FALSE;
1047    
1048      /* The integer range is limited by the machine's int representation. */      /* The integer range is limited by the machine's int representation. */
1049      c = 0;      s = 0;
1050        overflow = FALSE;
1051      while (IS_DIGIT(ptr[1]))      while (IS_DIGIT(ptr[1]))
1052        {        {
1053        if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */        if (s > INT_MAX / 10 - 1) /* Integer overflow */
1054          {          {
1055          c = -1;          overflow = TRUE;
1056          break;          break;
1057          }          }
1058        c = c * 10 + *(++ptr) - CHAR_0;        s = s * 10 + (int)(*(++ptr) - CHAR_0);
1059        }        }
1060      if (((unsigned int)c) > INT_MAX) /* Integer overflow */      if (overflow) /* Integer overflow */
1061        {        {
1062        while (IS_DIGIT(ptr[1]))        while (IS_DIGIT(ptr[1]))
1063          ptr++;          ptr++;
# Line 916  else Line 1071  else
1071        break;        break;
1072        }        }
1073    
1074      if (c == 0)      if (s == 0)
1075        {        {
1076        *errorcodeptr = ERR58;        *errorcodeptr = ERR58;
1077        break;        break;
# Line 924  else Line 1079  else
1079    
1080      if (negated)      if (negated)
1081        {        {
1082        if (c > bracount)        if (s > bracount)
1083          {          {
1084          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
1085          break;          break;
1086          }          }
1087        c = bracount - (c - 1);        s = bracount - (s - 1);
1088        }        }
1089    
1090      c = -(ESC_REF + c);      escape = -s;
1091      break;      break;
1092    
1093      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
1094      starting with one that is not zero is not straightforward. By experiment,      starting with one that is not zero is not straightforward. Perl has changed
1095      the way Perl works seems to be as follows:      over the years. Nowadays \g{} for backreferences and \o{} for octal are
1096        recommended to avoid the ambiguities in the old syntax.
1097    
1098      Outside a character class, the digits are read as a decimal number. If the      Outside a character class, the digits are read as a decimal number. If the
1099      number is less than 10, or if there are that many previous extracting      number is less than 8 (used to be 10), or if there are that many previous
1100      left brackets, then it is a back reference. Otherwise, up to three octal      extracting left brackets, then it is a back reference. Otherwise, up to
1101      digits are read to form an escaped byte. Thus \123 is likely to be octal      three octal digits are read to form an escaped byte. Thus \123 is likely to
1102      123 (cf \0123, which is octal 012 followed by the literal 3). If the octal      be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If
1103      value is greater than 377, the least significant 8 bits are taken. Inside a      the octal value is greater than 377, the least significant 8 bits are
1104      character class, \ followed by a digit is always an octal number. */      taken. \8 and \9 are treated as the literal characters 8 and 9.
1105    
1106        Inside a character class, \ followed by a digit is always either a literal
1107        8 or 9 or an octal number. */
1108    
1109      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
1110      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
# Line 954  else Line 1113  else
1113        {        {
1114        oldptr = ptr;        oldptr = ptr;
1115        /* The integer range is limited by the machine's int representation. */        /* The integer range is limited by the machine's int representation. */
1116        c -= CHAR_0;        s = (int)(c -CHAR_0);
1117          overflow = FALSE;
1118        while (IS_DIGIT(ptr[1]))        while (IS_DIGIT(ptr[1]))
1119          {          {
1120          if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */          if (s > INT_MAX / 10 - 1) /* Integer overflow */
1121            {            {
1122            c = -1;            overflow = TRUE;
1123            break;            break;
1124            }            }
1125          c = c * 10 + *(++ptr) - CHAR_0;          s = s * 10 + (int)(*(++ptr) - CHAR_0);
1126          }          }
1127        if (((unsigned int)c) > INT_MAX) /* Integer overflow */        if (overflow) /* Integer overflow */
1128          {          {
1129          while (IS_DIGIT(ptr[1]))          while (IS_DIGIT(ptr[1]))
1130            ptr++;            ptr++;
1131          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1132          break;          break;
1133          }          }
1134        if (c < 10 || c <= bracount)        if (s < 8 || s <= bracount)  /* Check for back reference */
1135          {          {
1136          c = -(ESC_REF + c);          escape = -s;
1137          break;          break;
1138          }          }
1139        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
1140        }        }
1141    
1142      /* Handle an octal number following \. If the first digit is 8 or 9, Perl      /* Handle a digit following \ when the number is not a back reference. If
1143      generates a binary zero byte and treats the digit as a following literal.      the first digit is 8 or 9, Perl used to generate a binary zero byte and
1144      Thus we have to pull back the pointer by one. */      then treat the digit as a following literal. At least by Perl 5.18 this
1145        changed so as not to insert the binary zero. */
1146      if ((c = *ptr) >= CHAR_8)  
1147        {      if ((c = *ptr) >= CHAR_8) break;
1148        ptr--;  
1149        c = 0;      /* Fall through with a digit less than 8 */
       break;  
       }  
1150    
1151      /* \0 always starts an octal number, but we may drop through to here with a      /* \0 always starts an octal number, but we may drop through to here with a
1152      larger first octal digit. The original code used just to take the least      larger first octal digit. The original code used just to take the least
# Line 1020  else Line 1178  else
1178          c = 0;          c = 0;
1179          for (i = 0; i < 2; ++i)          for (i = 0; i < 2; ++i)
1180            {            {
1181            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
1182  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1183            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1184            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 1196  else
1196        const pcre_uchar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
1197    
1198        c = 0;        c = 0;
1199          overflow = FALSE;
1200        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
1201          {          {
1202          register int cc = *pt++;          register pcre_uint32 cc = *pt++;
1203          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1204    
1205    #ifdef COMPILE_PCRE32
1206            if (c >= 0x10000000l) { overflow = TRUE; break; }
1207    #endif
1208    
1209  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1210          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1211          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 1214  else
1214          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1215  #endif  #endif
1216    
1217  #ifdef COMPILE_PCRE8  #if defined COMPILE_PCRE8
1218          if (c > (utf ? 0x10ffff : 0xff)) { c = -1; break; }          if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1219  #else  #elif defined COMPILE_PCRE16
1220  #ifdef COMPILE_PCRE16          if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1221          if (c > (utf ? 0x10ffff : 0xffff)) { c = -1; break; }  #elif defined COMPILE_PCRE32
1222  #endif          if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1223  #endif  #endif
1224          }          }
1225    
1226        if (c < 0)        if (overflow)
1227          {          {
1228          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1229          *errorcodeptr = ERR34;          *errorcodeptr = ERR34;
# Line 1082  else Line 1245  else
1245      c = 0;      c = 0;
1246      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)
1247        {        {
1248        int cc;                                  /* Some compilers don't like */        pcre_uint32 cc;                          /* Some compilers don't like */
1249        cc = *(++ptr);                           /* ++ in initializers */        cc = *(++ptr);                           /* ++ in initializers */
1250  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1251        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
# Line 1101  else Line 1264  else
1264    
1265      case CHAR_c:      case CHAR_c:
1266      c = *(++ptr);      c = *(++ptr);
1267      if (c == 0)      if (c == CHAR_NULL)
1268        {        {
1269        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1270        break;        break;
# Line 1141  else Line 1304  else
1304  newline". PCRE does not support \N{name}. However, it does support  newline". PCRE does not support \N{name}. However, it does support
1305  quantification such as \N{2,3}. */  quantification such as \N{2,3}. */
1306    
1307  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&  if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1308       !is_counted_repeat(ptr+2))       !is_counted_repeat(ptr+2))
1309    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
1310    
1311  /* If PCRE_UCP is set, we change the values for \d etc. */  /* If PCRE_UCP is set, we change the values for \d etc. */
1312    
1313  if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)  if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
1314    c -= (ESC_DU - ESC_D);    escape += (ESC_DU - ESC_D);
1315    
1316  /* Set the pointer to the final character before returning. */  /* Set the pointer to the final character before returning. */
1317    
1318  *ptrptr = ptr;  *ptrptr = ptr;
1319  return c;  *chptr = c;
1320    return escape;
1321  }  }
1322    
1323    
# Line 1171  escape sequence. Line 1335  escape sequence.
1335  Argument:  Argument:
1336    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
1337    negptr         points to a boolean that is set TRUE for negation else FALSE    negptr         points to a boolean that is set TRUE for negation else FALSE
1338    dptr           points to an int that is set to the detailed property value    ptypeptr       points to an unsigned int that is set to the type value
1339      pdataptr       points to an unsigned int that is set to the detailed property value
1340    errorcodeptr   points to the error code variable    errorcodeptr   points to the error code variable
1341    
1342  Returns:         type value from ucp_type_table, or -1 for an invalid type  Returns:         TRUE if the type value was found, or FALSE for an invalid type
1343  */  */
1344    
1345  static int  static BOOL
1346  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1347      unsigned int *pdataptr, int *errorcodeptr)
1348  {  {
1349  int c, i, bot, top;  pcre_uchar c;
1350    int i, bot, top;
1351  const pcre_uchar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1352  pcre_uchar name[32];  pcre_uchar name[32];
1353    
1354  c = *(++ptr);  c = *(++ptr);
1355  if (c == 0) goto ERROR_RETURN;  if (c == CHAR_NULL) goto ERROR_RETURN;
1356    
1357  *negptr = FALSE;  *negptr = FALSE;
1358    
# Line 1202  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1369  if (c == CHAR_LEFT_CURLY_BRACKET)
1369    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1370      {      {
1371      c = *(++ptr);      c = *(++ptr);
1372      if (c == 0) goto ERROR_RETURN;      if (c == CHAR_NULL) goto ERROR_RETURN;
1373      if (c == CHAR_RIGHT_CURLY_BRACKET) break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1374      name[i] = c;      name[i] = c;
1375      }      }
# Line 1227  top = PRIV(utt_size); Line 1394  top = PRIV(utt_size);
1394    
1395  while (bot < top)  while (bot < top)
1396    {    {
1397      int r;
1398    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1399    c = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);    r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1400    if (c == 0)    if (r == 0)
1401      {      {
1402      *dptr = PRIV(utt)[i].value;      *ptypeptr = PRIV(utt)[i].type;
1403      return PRIV(utt)[i].type;      *pdataptr = PRIV(utt)[i].value;
1404        return TRUE;
1405      }      }
1406    if (c > 0) bot = i + 1; else top = i;    if (r > 0) bot = i + 1; else top = i;
1407    }    }
1408    
1409  *errorcodeptr = ERR47;  *errorcodeptr = ERR47;
1410  *ptrptr = ptr;  *ptrptr = ptr;
1411  return -1;  return FALSE;
1412    
1413  ERROR_RETURN:  ERROR_RETURN:
1414  *errorcodeptr = ERR46;  *errorcodeptr = ERR46;
1415  *ptrptr = ptr;  *ptrptr = ptr;
1416  return -1;  return FALSE;
1417  }  }
1418  #endif  #endif
1419    
1420    
1421    
   
1422  /*************************************************  /*************************************************
1423  *         Read repeat counts                     *  *         Read repeat counts                     *
1424  *************************************************/  *************************************************/
# Line 1279  int max = -1; Line 1447  int max = -1;
1447  /* Read the minimum value and do a paranoid check: a negative value indicates  /* Read the minimum value and do a paranoid check: a negative value indicates
1448  an integer overflow. */  an integer overflow. */
1449    
1450  while (IS_DIGIT(*p)) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);
1451  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1452    {    {
1453    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 1294  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1462  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1462    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1463      {      {
1464      max = 0;      max = 0;
1465      while(IS_DIGIT(*p)) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);
1466      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1467        {        {
1468        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1319  return p; Line 1487  return p;
1487    
1488    
1489  /*************************************************  /*************************************************
 *  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;  
 }  
   
   
   
   
 /*************************************************  
1490  *      Find first significant op code            *  *      Find first significant op code            *
1491  *************************************************/  *************************************************/
1492    
# Line 1652  for (;;) Line 1525  for (;;)
1525    
1526      case OP_CALLOUT:      case OP_CALLOUT:
1527      case OP_CREF:      case OP_CREF:
1528      case OP_NCREF:      case OP_DNCREF:
1529      case OP_RREF:      case OP_RREF:
1530      case OP_NRREF:      case OP_DNRREF:
1531      case OP_DEF:      case OP_DEF:
1532      code += PRIV(OP_lengths)[*code];      code += PRIV(OP_lengths)[*code];
1533      break;      break;
# Line 1668  for (;;) Line 1541  for (;;)
1541    
1542    
1543    
   
1544  /*************************************************  /*************************************************
1545  *        Find the fixed length of a branch       *  *        Find the fixed length of a branch       *
1546  *************************************************/  *************************************************/
# Line 1686  and doing the check at the end; a flag s Line 1558  and doing the check at the end; a flag s
1558    
1559  Arguments:  Arguments:
1560    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1561    utf      TRUE in UTF-8 / UTF-16 mode    utf      TRUE in UTF-8 / UTF-16 / UTF-32 mode
1562    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1563    cd       the "compile data" structure    cd       the "compile data" structure
1564    
# Line 1712  for (;;) Line 1584  for (;;)
1584    {    {
1585    int d;    int d;
1586    pcre_uchar *ce, *cs;    pcre_uchar *ce, *cs;
1587    register int op = *cc;    register pcre_uchar op = *cc;
1588    
1589    switch (op)    switch (op)
1590      {      {
# Line 1792  for (;;) Line 1664  for (;;)
1664      case OP_COMMIT:      case OP_COMMIT:
1665      case OP_CREF:      case OP_CREF:
1666      case OP_DEF:      case OP_DEF:
1667        case OP_DNCREF:
1668        case OP_DNRREF:
1669      case OP_DOLL:      case OP_DOLL:
1670      case OP_DOLLM:      case OP_DOLLM:
1671      case OP_EOD:      case OP_EOD:
1672      case OP_EODN:      case OP_EODN:
1673      case OP_FAIL:      case OP_FAIL:
     case OP_NCREF:  
     case OP_NRREF:  
1674      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1675      case OP_PRUNE:      case OP_PRUNE:
1676      case OP_REVERSE:      case OP_REVERSE:
# Line 1832  for (;;) Line 1704  for (;;)
1704      case OP_EXACTI:      case OP_EXACTI:
1705      case OP_NOTEXACT:      case OP_NOTEXACT:
1706      case OP_NOTEXACTI:      case OP_NOTEXACTI:
1707      branchlength += GET2(cc,1);      branchlength += (int)GET2(cc,1);
1708      cc += 2 + IMM2_SIZE;      cc += 2 + IMM2_SIZE;
1709  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
1710      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 1713  for (;;)
1713    
1714      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1715      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1716      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)
1717          cc += 2;
1718      cc += 1 + IMM2_SIZE + 1;      cc += 1 + IMM2_SIZE + 1;
1719      break;      break;
1720    
# Line 1876  for (;;) Line 1749  for (;;)
1749    
1750      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1751    
 #if defined SUPPORT_UTF || defined COMPILE_PCRE16  
     case OP_XCLASS:  
     cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];  
     /* Fall through */  
 #endif  
   
1752      case OP_CLASS:      case OP_CLASS:
1753      case OP_NCLASS:      case OP_NCLASS:
1754    #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1755        case OP_XCLASS:
1756        /* The original code caused an unsigned overflow in 64 bit systems,
1757        so now we use a conditional statement. */
1758        if (op == OP_XCLASS)
1759          cc += GET(cc, 1);
1760        else
1761          cc += PRIV(OP_lengths)[OP_CLASS];
1762    #else
1763      cc += PRIV(OP_lengths)[OP_CLASS];      cc += PRIV(OP_lengths)[OP_CLASS];
1764    #endif
1765    
1766      switch (*cc)      switch (*cc)
1767        {        {
# Line 1899  for (;;) Line 1776  for (;;)
1776        case OP_CRRANGE:        case OP_CRRANGE:
1777        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1778        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1779        branchlength += GET2(cc,1);        branchlength += (int)GET2(cc,1);
1780        cc += 1 + 2 * IMM2_SIZE;        cc += 1 + 2 * IMM2_SIZE;
1781        break;        break;
1782    
# Line 1966  for (;;) Line 1843  for (;;)
1843      case OP_QUERYI:      case OP_QUERYI:
1844      case OP_REF:      case OP_REF:
1845      case OP_REFI:      case OP_REFI:
1846        case OP_DNREF:
1847        case OP_DNREFI:
1848      case OP_SBRA:      case OP_SBRA:
1849      case OP_SBRAPOS:      case OP_SBRAPOS:
1850      case OP_SCBRA:      case OP_SCBRA:
# Line 2002  for (;;) Line 1881  for (;;)
1881    
1882    
1883    
   
1884  /*************************************************  /*************************************************
1885  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
1886  *************************************************/  *************************************************/
# Line 2015  length. Line 1893  length.
1893    
1894  Arguments:  Arguments:
1895    code        points to start of expression    code        points to start of expression
1896    utf         TRUE in UTF-8 / UTF-16 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
1897    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
1898    
1899  Returns:      pointer to the opcode for the bracket, or NULL if not found  Returns:      pointer to the opcode for the bracket, or NULL if not found
# Line 2026  PRIV(find_bracket)(const pcre_uchar *cod Line 1904  PRIV(find_bracket)(const pcre_uchar *cod
1904  {  {
1905  for (;;)  for (;;)
1906    {    {
1907    register int c = *code;    register pcre_uchar c = *code;
1908    
1909    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1910    
# Line 2049  for (;;) Line 1927  for (;;)
1927    else if (c == OP_CBRA || c == OP_SCBRA ||    else if (c == OP_CBRA || c == OP_SCBRA ||
1928             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
1929      {      {
1930      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1931      if (n == number) return (pcre_uchar *)code;      if (n == number) return (pcre_uchar *)code;
1932      code += PRIV(OP_lengths)[c];      code += PRIV(OP_lengths)[c];
1933      }      }
# Line 2079  for (;;) Line 1957  for (;;)
1957        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1958        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1959        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1960        if (code[1 + IMM2_SIZE] == OP_PROP        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1961          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;          code += 2;
1962        break;        break;
1963    
1964        case OP_MARK:        case OP_MARK:
1965        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1966        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
1967        case OP_THEN_ARG:        case OP_THEN_ARG:
1968        code += code[1];        code += code[1];
1969        break;        break;
# Line 2102  for (;;) Line 1977  for (;;)
1977    a multi-byte character. The length in the table is a minimum, so we have to    a multi-byte character. The length in the table is a minimum, so we have to
1978    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
1979    
1980  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1981      if (utf) switch(c)      if (utf) switch(c)
1982        {        {
1983        case OP_CHAR:        case OP_CHAR:
# Line 2154  instance of OP_RECURSE. Line 2029  instance of OP_RECURSE.
2029    
2030  Arguments:  Arguments:
2031    code        points to start of expression    code        points to start of expression
2032    utf         TRUE in UTF-8 / UTF-16 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2033    
2034  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found
2035  */  */
# Line 2164  find_recurse(const pcre_uchar *code, BOO Line 2039  find_recurse(const pcre_uchar *code, BOO
2039  {  {
2040  for (;;)  for (;;)
2041    {    {
2042    register int c = *code;    register pcre_uchar c = *code;
2043    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2044    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
2045    
# Line 2199  for (;;) Line 2074  for (;;)
2074        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2075        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2076        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2077        if (code[1 + IMM2_SIZE] == OP_PROP        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2078          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;          code += 2;
2079        break;        break;
2080    
2081        case OP_MARK:        case OP_MARK:
2082        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2083        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
2084        case OP_THEN_ARG:        case OP_THEN_ARG:
2085        code += code[1];        code += code[1];
2086        break;        break;
# Line 2222  for (;;) Line 2094  for (;;)
2094      by a multi-byte character. The length in the table is a minimum, so we have      by a multi-byte character. The length in the table is a minimum, so we have
2095      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
2096    
2097  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2098      if (utf) switch(c)      if (utf) switch(c)
2099        {        {
2100        case OP_CHAR:        case OP_CHAR:
# Line 2308  bracket whose current branch will alread Line 2180  bracket whose current branch will alread
2180  Arguments:  Arguments:
2181    code        points to start of search    code        points to start of search
2182    endcode     points to where to stop    endcode     points to where to stop
2183    utf         TRUE if in UTF-8 / UTF-16 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2184    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2185      recurses    chain of recurse_check to catch mutual recursion
2186    
2187  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2188  */  */
2189    
2190    typedef struct recurse_check {
2191      struct recurse_check *prev;
2192      const pcre_uchar *group;
2193    } recurse_check;
2194    
2195  static BOOL  static BOOL
2196  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2197    BOOL utf, compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2198  {  {
2199  register int c;  register pcre_uchar c;
2200    recurse_check this_recurse;
2201    
2202  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2203       code < endcode;       code < endcode;
2204       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 2226  for (code = first_significant_code(code
2226    
2227    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2228      {      {
2229      const pcre_uchar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2230      BOOL empty_branch;      BOOL empty_branch;
2231    
2232      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2233        when called to scan a completed pattern by setting cd->start_workspace to
2234        NULL. */
2235    
2236        if (cd->start_workspace != NULL)
2237          {
2238          const pcre_uchar *tcode;
2239          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2240            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2241          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2242          }
2243    
2244        /* If we are scanning a completed pattern, there are no forward references
2245        and all groups are complete. We need to detect whether this is a recursive
2246        call, as otherwise there will be an infinite loop. If it is a recursion,
2247        just skip over it. Simple recursions are easily detected. For mutual
2248        recursions we keep a chain on the stack. */
2249    
2250      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)      else
2251        if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;        {
2252          recurse_check *r = recurses;
2253          const pcre_uchar *endgroup = scode;
2254    
2255      /* Not a forward reference, test for completed backward reference */        do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2256          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2257    
2258      empty_branch = FALSE;        for (r = recurses; r != NULL; r = r->prev)
2259      scode = cd->start_code + GET(code, 1);          if (r->group == scode) break;
2260      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */        if (r != NULL) continue;   /* Mutual recursion */
2261          }
2262    
2263        /* Completed reference; scan the referenced group, remembering it on the
2264        stack chain to detect mutual recursions. */
2265    
2266      /* Completed backwards reference */      empty_branch = FALSE;
2267        this_recurse.prev = recurses;
2268        this_recurse.group = scode;
2269    
2270      do      do
2271        {        {
2272        if (could_be_empty_branch(scode, endcode, utf, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2273          {          {
2274          empty_branch = TRUE;          empty_branch = TRUE;
2275          break;          break;
# Line 2420  for (code = first_significant_code(code Line 2325  for (code = first_significant_code(code
2325        empty_branch = FALSE;        empty_branch = FALSE;
2326        do        do
2327          {          {
2328          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2329            empty_branch = TRUE;            empty_branch = TRUE;
2330          code += GET(code, 1);          code += GET(code, 1);
2331          }          }
# Line 2478  for (code = first_significant_code(code Line 2383  for (code = first_significant_code(code
2383    
2384      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2385    
2386        case OP_ANY:
2387        case OP_ALLANY:
2388        case OP_ANYBYTE:
2389    
2390      case OP_PROP:      case OP_PROP:
2391      case OP_NOTPROP:      case OP_NOTPROP:
2392        case OP_ANYNL:
2393    
2394        case OP_NOT_HSPACE:
2395        case OP_HSPACE:
2396        case OP_NOT_VSPACE:
2397        case OP_VSPACE:
2398      case OP_EXTUNI:      case OP_EXTUNI:
2399    
2400      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2401      case OP_DIGIT:      case OP_DIGIT:
2402      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2403      case OP_WHITESPACE:      case OP_WHITESPACE:
2404      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2405      case OP_WORDCHAR:      case OP_WORDCHAR:
2406      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2407      case OP_CHAR:      case OP_CHAR:
2408      case OP_CHARI:      case OP_CHARI:
2409      case OP_NOT:      case OP_NOT:
2410      case OP_NOTI:      case OP_NOTI:
2411    
2412      case OP_PLUS:      case OP_PLUS:
2413        case OP_PLUSI:
2414      case OP_MINPLUS:      case OP_MINPLUS:
2415      case OP_POSPLUS:      case OP_MINPLUSI:
2416      case OP_EXACT:  
2417      case OP_NOTPLUS:      case OP_NOTPLUS:
2418        case OP_NOTPLUSI:
2419      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2420        case OP_NOTMINPLUSI:
2421    
2422        case OP_POSPLUS:
2423        case OP_POSPLUSI:
2424      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2425        case OP_NOTPOSPLUSI:
2426    
2427        case OP_EXACT:
2428        case OP_EXACTI:
2429      case OP_NOTEXACT:      case OP_NOTEXACT:
2430        case OP_NOTEXACTI:
2431    
2432      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2433      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2434      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2435      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2436    
2437      return FALSE;      return FALSE;
2438    
2439      /* These are going to continue, as they may be empty, but we have to      /* These are going to continue, as they may be empty, but we have to
# Line 2525  for (code = first_significant_code(code Line 2453  for (code = first_significant_code(code
2453      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2454      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2455      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2456      if (code[1 + IMM2_SIZE] == OP_PROP      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2457        || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;        code += 2;
2458      break;      break;
2459    
2460      /* End of branch */      /* End of branch */
# Line 2539  for (code = first_significant_code(code Line 2467  for (code = first_significant_code(code
2467      return TRUE;      return TRUE;
2468    
2469      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2470      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2471        followed by a multibyte character. */
2472    
2473  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2474      case OP_STAR:      case OP_STAR:
2475      case OP_STARI:      case OP_STARI:
2476        case OP_NOTSTAR:
2477        case OP_NOTSTARI:
2478    
2479      case OP_MINSTAR:      case OP_MINSTAR:
2480      case OP_MINSTARI:      case OP_MINSTARI:
2481        case OP_NOTMINSTAR:
2482        case OP_NOTMINSTARI:
2483    
2484      case OP_POSSTAR:      case OP_POSSTAR:
2485      case OP_POSSTARI:      case OP_POSSTARI:
2486        case OP_NOTPOSSTAR:
2487        case OP_NOTPOSSTARI:
2488    
2489      case OP_QUERY:      case OP_QUERY:
2490      case OP_QUERYI:      case OP_QUERYI:
2491        case OP_NOTQUERY:
2492        case OP_NOTQUERYI:
2493    
2494      case OP_MINQUERY:      case OP_MINQUERY:
2495      case OP_MINQUERYI:      case OP_MINQUERYI:
2496        case OP_NOTMINQUERY:
2497        case OP_NOTMINQUERYI:
2498    
2499      case OP_POSQUERY:      case OP_POSQUERY:
2500      case OP_POSQUERYI:      case OP_POSQUERYI:
2501        case OP_NOTPOSQUERY:
2502        case OP_NOTPOSQUERYI:
2503    
2504      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2505      break;      break;
2506    
2507      case OP_UPTO:      case OP_UPTO:
2508      case OP_UPTOI:      case OP_UPTOI:
2509      case OP_MINUPTO:      case OP_NOTUPTO:
2510        case OP_NOTUPTOI:
2511    
2512        case OP_MINUPTO:
2513      case OP_MINUPTOI:      case OP_MINUPTOI:
2514        case OP_NOTMINUPTO:
2515        case OP_NOTMINUPTOI:
2516    
2517      case OP_POSUPTO:      case OP_POSUPTO:
2518      case OP_POSUPTOI:      case OP_POSUPTOI:
2519        case OP_NOTPOSUPTO:
2520        case OP_NOTPOSUPTOI:
2521    
2522      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2523      break;      break;
2524  #endif  #endif
# Line 2573  for (code = first_significant_code(code Line 2529  for (code = first_significant_code(code
2529      case OP_MARK:      case OP_MARK:
2530      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2531      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     code += code[1];  
     break;  
   
2532      case OP_THEN_ARG:      case OP_THEN_ARG:
2533      code += code[1];      code += code[1];
2534      break;      break;
2535    
2536      /* None of the remaining opcodes are required to match a character. */      /* None of the remaining opcodes are required to match a character. */
2537    
2538      default:      default:
2539      break;      break;
2540        }
2541      }
2542    
2543    return TRUE;
2544    }
2545    
2546    
2547    
2548    /*************************************************
2549    *    Scan compiled regex for non-emptiness       *
2550    *************************************************/
2551    
2552    /* This function is called to check for left recursive calls. We want to check
2553    the current branch of the current pattern to see if it could match the empty
2554    string. If it could, we must look outwards for branches at other levels,
2555    stopping when we pass beyond the bracket which is the subject of the recursion.
2556    This function is called only during the real compile, not during the
2557    pre-compile.
2558    
2559    Arguments:
2560      code        points to start of the recursion
2561      endcode     points to where to stop (current RECURSE item)
2562      bcptr       points to the chain of current (unclosed) branch starts
2563      utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2564      cd          pointers to tables etc
2565    
2566    Returns:      TRUE if what is matched could be empty
2567    */
2568    
2569    static BOOL
2570    could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2571      branch_chain *bcptr, BOOL utf, compile_data *cd)
2572    {
2573    while (bcptr != NULL && bcptr->current_branch >= code)
2574      {
2575      if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2576        return FALSE;
2577      bcptr = bcptr->outer;
2578      }
2579    return TRUE;
2580    }
2581    
2582    
2583    
2584    /*************************************************
2585    *        Base opcode of repeated opcodes         *
2586    *************************************************/
2587    
2588    /* Returns the base opcode for repeated single character type opcodes. If the
2589    opcode is not a repeated character type, it returns with the original value.
2590    
2591    Arguments:  c opcode
2592    Returns:    base opcode for the type
2593    */
2594    
2595    static pcre_uchar
2596    get_repeat_base(pcre_uchar c)
2597    {
2598    return (c > OP_TYPEPOSUPTO)? c :
2599           (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2600           (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2601           (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2602           (c >= OP_STARI)?      OP_STARI :
2603                                 OP_STAR;
2604    }
2605    
2606    
2607    
2608    #ifdef SUPPORT_UCP
2609    /*************************************************
2610    *        Check a character and a property        *
2611    *************************************************/
2612    
2613    /* This function is called by check_auto_possessive() when a property item
2614    is adjacent to a fixed character.
2615    
2616    Arguments:
2617      c            the character
2618      ptype        the property type
2619      pdata        the data for the type
2620      negated      TRUE if it's a negated property (\P or \p{^)
2621    
2622    Returns:       TRUE if auto-possessifying is OK
2623    */
2624    
2625    static BOOL
2626    check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2627      BOOL negated)
2628    {
2629    const pcre_uint32 *p;
2630    const ucd_record *prop = GET_UCD(c);
2631    
2632    switch(ptype)
2633      {
2634      case PT_LAMP:
2635      return (prop->chartype == ucp_Lu ||
2636              prop->chartype == ucp_Ll ||
2637              prop->chartype == ucp_Lt) == negated;
2638    
2639      case PT_GC:
2640      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2641    
2642      case PT_PC:
2643      return (pdata == prop->chartype) == negated;
2644    
2645      case PT_SC:
2646      return (pdata == prop->script) == negated;
2647    
2648      /* These are specials */
2649    
2650      case PT_ALNUM:
2651      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2652              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2653    
2654      /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2655      means that Perl space and POSIX space are now identical. PCRE was changed
2656      at release 8.34. */
2657    
2658      case PT_SPACE:    /* Perl space */
2659      case PT_PXSPACE:  /* POSIX space */
2660      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2661              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2662              c == CHAR_FF || c == CHAR_CR)
2663              == negated;
2664    
2665      case PT_WORD:
2666      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2667              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2668              c == CHAR_UNDERSCORE) == negated;
2669    
2670      case PT_CLIST:
2671      p = PRIV(ucd_caseless_sets) + prop->caseset;
2672      for (;;)
2673        {
2674        if (c < *p) return !negated;
2675        if (c == *p++) return negated;
2676        }
2677      break;  /* Control never reaches here */
2678      }
2679    
2680    return FALSE;
2681    }
2682    #endif  /* SUPPORT_UCP */
2683    
2684    
2685    
2686    /*************************************************
2687    *        Fill the character property list        *
2688    *************************************************/
2689    
2690    /* Checks whether the code points to an opcode that can take part in auto-
2691    possessification, and if so, fills a list with its properties.
2692    
2693    Arguments:
2694      code        points to start of expression
2695      utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2696      fcc         points to case-flipping table
2697      list        points to output list
2698                  list[0] will be filled with the opcode
2699                  list[1] will be non-zero if this opcode
2700                    can match an empty character string
2701                  list[2..7] depends on the opcode
2702    
2703    Returns:      points to the start of the next opcode if *code is accepted
2704                  NULL if *code is not accepted
2705    */
2706    
2707    static const pcre_uchar *
2708    get_chr_property_list(const pcre_uchar *code, BOOL utf,
2709      const pcre_uint8 *fcc, pcre_uint32 *list)
2710    {
2711    pcre_uchar c = *code;
2712    const pcre_uchar *end;
2713    const pcre_uint32 *clist_src;
2714    pcre_uint32 *clist_dest;
2715    pcre_uint32 chr;
2716    pcre_uchar base;
2717    
2718    list[0] = c;
2719    list[1] = FALSE;
2720    code++;
2721    
2722    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2723      {
2724      base = get_repeat_base(c);
2725      c -= (base - OP_STAR);
2726    
2727      if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2728        code += IMM2_SIZE;
2729    
2730      list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
2731    
2732      switch(base)
2733        {
2734        case OP_STAR:
2735        list[0] = OP_CHAR;
2736        break;
2737    
2738        case OP_STARI:
2739        list[0] = OP_CHARI;
2740        break;
2741    
2742        case OP_NOTSTAR:
2743        list[0] = OP_NOT;
2744        break;
2745    
2746        case OP_NOTSTARI:
2747        list[0] = OP_NOTI;
2748        break;
2749    
2750        case OP_TYPESTAR:
2751        list[0] = *code;
2752        code++;
2753        break;
2754        }
2755      c = list[0];
2756      }
2757    
2758    switch(c)
2759      {
2760      case OP_NOT_DIGIT:
2761      case OP_DIGIT:
2762      case OP_NOT_WHITESPACE:
2763      case OP_WHITESPACE:
2764      case OP_NOT_WORDCHAR:
2765      case OP_WORDCHAR:
2766      case OP_ANY:
2767      case OP_ALLANY:
2768      case OP_ANYNL:
2769      case OP_NOT_HSPACE:
2770      case OP_HSPACE:
2771      case OP_NOT_VSPACE:
2772      case OP_VSPACE:
2773      case OP_EXTUNI:
2774      case OP_EODN:
2775      case OP_EOD:
2776      case OP_DOLL:
2777      case OP_DOLLM:
2778      return code;
2779    
2780      case OP_CHAR:
2781      case OP_NOT:
2782      GETCHARINCTEST(chr, code);
2783      list[2] = chr;
2784      list[3] = NOTACHAR;
2785      return code;
2786    
2787      case OP_CHARI:
2788      case OP_NOTI:
2789      list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2790      GETCHARINCTEST(chr, code);
2791      list[2] = chr;
2792    
2793    #ifdef SUPPORT_UCP
2794      if (chr < 128 || (chr < 256 && !utf))
2795        list[3] = fcc[chr];
2796      else
2797        list[3] = UCD_OTHERCASE(chr);
2798    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2799      list[3] = (chr < 256) ? fcc[chr] : chr;
2800    #else
2801      list[3] = fcc[chr];
2802    #endif
2803    
2804      /* The othercase might be the same value. */
2805    
2806      if (chr == list[3])
2807        list[3] = NOTACHAR;
2808      else
2809        list[4] = NOTACHAR;
2810      return code;
2811    
2812    #ifdef SUPPORT_UCP
2813      case OP_PROP:
2814      case OP_NOTPROP:
2815      if (code[0] != PT_CLIST)
2816        {
2817        list[2] = code[0];
2818        list[3] = code[1];
2819        return code + 2;
2820        }
2821    
2822      /* Convert only if we have anough space. */
2823    
2824      clist_src = PRIV(ucd_caseless_sets) + code[1];
2825      clist_dest = list + 2;
2826      code += 2;
2827    
2828      do {
2829         /* Early return if there is not enough space. */
2830         if (clist_dest >= list + 8)
2831           {
2832           list[2] = code[0];
2833           list[3] = code[1];
2834           return code;
2835           }
2836         *clist_dest++ = *clist_src;
2837         }
2838       while(*clist_src++ != NOTACHAR);
2839    
2840      /* Enough space to store all characters. */
2841    
2842      list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2843      return code;
2844    #endif
2845    
2846      case OP_NCLASS:
2847      case OP_CLASS:
2848    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2849      case OP_XCLASS:
2850    
2851      if (c == OP_XCLASS)
2852        end = code + GET(code, 0);
2853      else
2854    #endif
2855        end = code + 32 / sizeof(pcre_uchar);
2856    
2857      switch(*end)
2858        {
2859        case OP_CRSTAR:
2860        case OP_CRMINSTAR:
2861        case OP_CRQUERY:
2862        case OP_CRMINQUERY:
2863        list[1] = TRUE;
2864        end++;
2865        break;
2866    
2867        case OP_CRRANGE:
2868        case OP_CRMINRANGE:
2869        list[1] = (GET2(end, 1) == 0);
2870        end += 1 + 2 * IMM2_SIZE;
2871        break;
2872        }
2873      list[2] = end - code;
2874      return end;
2875      }
2876    return NULL;    /* Opcode not accepted */
2877    }
2878    
2879    
2880    
2881    /*************************************************
2882    *    Scan further character sets for match       *
2883    *************************************************/
2884    
2885    /* Checks whether the base and the current opcode have a common character, in
2886    which case the base cannot be possessified.
2887    
2888    Arguments:
2889      code        points to the byte code
2890      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2891      cd          static compile data
2892      base_list   the data list of the base opcode
2893    
2894    Returns:      TRUE if the auto-possessification is possible
2895    */
2896    
2897    static BOOL
2898    compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2899      const pcre_uint32* base_list)
2900    {
2901    pcre_uchar c;
2902    pcre_uint32 list[8];
2903    const pcre_uint32* chr_ptr;
2904    const pcre_uint32* ochr_ptr;
2905    const pcre_uint32* list_ptr;
2906    pcre_uint32 chr;
2907    
2908    for(;;)
2909      {
2910      c = *code;
2911    
2912      /* Skip over callouts */
2913    
2914      if (c == OP_CALLOUT)
2915        {
2916        code += PRIV(OP_lengths)[c];
2917        continue;
2918        }
2919    
2920      if (c == OP_ALT)
2921        {
2922        do code += GET(code, 1); while (*code == OP_ALT);
2923        c = *code;
2924        }
2925    
2926      switch(c)
2927        {
2928        case OP_END:
2929        /* TRUE only in greedy case. The non-greedy case could be replaced by an
2930        OP_EXACT, but it is probably not worth it. (And note that OP_EXACT uses
2931        more memory, which we cannot get at this stage.) */
2932    
2933        return base_list[1] != 0;
2934    
2935        case OP_KET:
2936        /* If the bracket is capturing, and referenced by an OP_RECURSE, the
2937        non-greedy case cannot be converted to a possessive form. We do not test
2938        the bracket type at the moment, but we might do it in the future to improve
2939        this condition. (But note that recursive calls are always atomic.) */
2940    
2941        if (base_list[1] == 0) return FALSE;
2942        code += PRIV(OP_lengths)[c];
2943        continue;
2944        }
2945    
2946      /* Check for a supported opcode, and load its properties. */
2947    
2948      code = get_chr_property_list(code, utf, cd->fcc, list);
2949      if (code == NULL) return FALSE;    /* Unsupported */
2950    
2951      /* If either opcode is a small character list, set pointers for comparing
2952      characters from that list with another list, or with a property. */
2953    
2954      if (base_list[0] == OP_CHAR)
2955        {
2956        chr_ptr = base_list + 2;
2957        list_ptr = list;
2958        }
2959      else if (list[0] == OP_CHAR)
2960        {
2961        chr_ptr = list + 2;
2962        list_ptr = base_list;
2963        }
2964    
2965      /* Some property combinations also acceptable. Unicode property opcodes are
2966      processed specially; the rest can be handled with a lookup table. */
2967    
2968      else
2969        {
2970        pcre_uint32 leftop, rightop;
2971    
2972        if (list[1] != 0) return FALSE;   /* Must match at least one character */
2973        leftop = base_list[0];
2974        rightop = list[0];
2975    
2976    #ifdef SUPPORT_UCP
2977        if (leftop == OP_PROP || leftop == OP_NOTPROP)
2978          {
2979          if (rightop == OP_EOD) return TRUE;
2980          if (rightop == OP_PROP || rightop == OP_NOTPROP)
2981            {
2982            int n;
2983            const pcre_uint8 *p;
2984            BOOL same = leftop == rightop;
2985            BOOL lisprop = leftop == OP_PROP;
2986            BOOL risprop = rightop == OP_PROP;
2987            BOOL bothprop = lisprop && risprop;
2988    
2989            /* There's a table that specifies how each combination is to be
2990            processed:
2991              0   Always return FALSE (never auto-possessify)
2992              1   Character groups are distinct (possessify if both are OP_PROP)
2993              2   Check character categories in the same group (general or particular)
2994              3   Return TRUE if the two opcodes are not the same
2995              ... see comments below
2996            */
2997    
2998            n = propposstab[base_list[2]][list[2]];
2999            switch(n)
3000              {
3001              case 0: return FALSE;
3002              case 1: return bothprop;
3003              case 2: return (base_list[3] == list[3]) != same;
3004              case 3: return !same;
3005    
3006              case 4:  /* Left general category, right particular category */
3007              return risprop && catposstab[base_list[3]][list[3]] == same;
3008    
3009              case 5:  /* Right general category, left particular category */
3010              return lisprop && catposstab[list[3]][base_list[3]] == same;
3011    
3012              /* This code is logically tricky. Think hard before fiddling with it.
3013              The posspropstab table has four entries per row. Each row relates to
3014              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3015              Only WORD actually needs all four entries, but using repeats for the
3016              others means they can all use the same code below.
3017    
3018              The first two entries in each row are Unicode general categories, and
3019              apply always, because all the characters they include are part of the
3020              PCRE character set. The third and fourth entries are a general and a
3021              particular category, respectively, that include one or more relevant
3022              characters. One or the other is used, depending on whether the check
3023              is for a general or a particular category. However, in both cases the
3024              category contains more characters than the specials that are defined
3025              for the property being tested against. Therefore, it cannot be used
3026              in a NOTPROP case.
3027    
3028              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3029              Underscore is covered by ucp_P or ucp_Po. */
3030    
3031              case 6:  /* Left alphanum vs right general category */
3032              case 7:  /* Left space vs right general category */
3033              case 8:  /* Left word vs right general category */
3034              p = posspropstab[n-6];
3035              return risprop && lisprop ==
3036                (list[3] != p[0] &&
3037                 list[3] != p[1] &&
3038                (list[3] != p[2] || !lisprop));
3039    
3040              case 9:   /* Right alphanum vs left general category */
3041              case 10:  /* Right space vs left general category */
3042              case 11:  /* Right word vs left general category */
3043              p = posspropstab[n-9];
3044              return lisprop && risprop ==
3045                (base_list[3] != p[0] &&
3046                 base_list[3] != p[1] &&
3047                (base_list[3] != p[2] || !risprop));
3048    
3049              case 12:  /* Left alphanum vs right particular category */
3050              case 13:  /* Left space vs right particular category */
3051              case 14:  /* Left word vs right particular category */
3052              p = posspropstab[n-12];
3053              return risprop && lisprop ==
3054                (catposstab[p[0]][list[3]] &&
3055                 catposstab[p[1]][list[3]] &&
3056                (list[3] != p[3] || !lisprop));
3057    
3058              case 15:  /* Right alphanum vs left particular category */
3059              case 16:  /* Right space vs left particular category */
3060              case 17:  /* Right word vs left particular category */
3061              p = posspropstab[n-15];
3062              return lisprop && risprop ==
3063                (catposstab[p[0]][base_list[3]] &&
3064                 catposstab[p[1]][base_list[3]] &&
3065                (base_list[3] != p[3] || !risprop));
3066              }
3067            }
3068          return FALSE;
3069          }
3070    
3071        else
3072    #endif  /* SUPPORT_UCP */
3073    
3074        return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3075               rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3076               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3077        }
3078    
3079      /* Control reaches here only if one of the items is a small character list.
3080      All characters are checked against the other side. */
3081    
3082      do
3083        {
3084        chr = *chr_ptr;
3085    
3086        switch(list_ptr[0])
3087          {
3088          case OP_CHAR:
3089          ochr_ptr = list_ptr + 2;
3090          do
3091            {
3092            if (chr == *ochr_ptr) return FALSE;
3093            ochr_ptr++;
3094            }
3095          while(*ochr_ptr != NOTACHAR);
3096          break;
3097    
3098          case OP_NOT:
3099          ochr_ptr = list_ptr + 2;
3100          do
3101            {
3102            if (chr == *ochr_ptr)
3103              break;
3104            ochr_ptr++;
3105            }
3106          while(*ochr_ptr != NOTACHAR);
3107          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3108          break;
3109    
3110          /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3111          set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3112    
3113          case OP_DIGIT:
3114          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3115          break;
3116    
3117          case OP_NOT_DIGIT:
3118          if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3119          break;
3120    
3121          case OP_WHITESPACE:
3122          if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3123          break;
3124    
3125          case OP_NOT_WHITESPACE:
3126          if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3127          break;
3128    
3129          case OP_WORDCHAR:
3130          if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3131          break;
3132    
3133          case OP_NOT_WORDCHAR:
3134          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3135          break;
3136    
3137          case OP_HSPACE:
3138          switch(chr)
3139            {
3140            HSPACE_CASES: return FALSE;
3141            default: break;
3142            }
3143          break;
3144    
3145          case OP_NOT_HSPACE:
3146          switch(chr)
3147            {
3148            HSPACE_CASES: break;
3149            default: return FALSE;
3150            }
3151          break;
3152    
3153          case OP_ANYNL:
3154          case OP_VSPACE:
3155          switch(chr)
3156            {
3157            VSPACE_CASES: return FALSE;
3158            default: break;
3159            }
3160          break;
3161    
3162          case OP_NOT_VSPACE:
3163          switch(chr)
3164            {
3165            VSPACE_CASES: break;
3166            default: return FALSE;
3167            }
3168          break;
3169    
3170          case OP_DOLL:
3171          case OP_EODN:
3172          switch (chr)
3173            {
3174            case CHAR_CR:
3175            case CHAR_LF:
3176            case CHAR_VT:
3177            case CHAR_FF:
3178            case CHAR_NEL:
3179    #ifndef EBCDIC
3180            case 0x2028:
3181            case 0x2029:
3182    #endif  /* Not EBCDIC */
3183            return FALSE;
3184            }
3185          break;
3186    
3187          case OP_EOD:    /* Can always possessify before \z */
3188          break;
3189    
3190          case OP_PROP:
3191          case OP_NOTPROP:
3192          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3193                list_ptr[0] == OP_NOTPROP))
3194            return FALSE;
3195          break;
3196    
3197          /* The class comparisons work only when the class is the second item
3198          of the pair, because there are at present no possessive forms of the
3199          class opcodes. Note also that the "code" variable that is used below
3200          points after the second item, and that the pointer for the first item
3201          is not available, so even if there were possessive forms of the class
3202          opcodes, the correct comparison could not be done. */
3203    
3204          case OP_NCLASS:
3205          if (chr > 255) return FALSE;
3206          /* Fall through */
3207    
3208          case OP_CLASS:
3209          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3210          if (chr > 255) break;
3211          if ((((pcre_uint8 *)(code - list_ptr[2] + 1))[chr >> 3] & (1 << (chr & 7))) != 0)
3212            return FALSE;
3213          break;
3214    
3215    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3216          case OP_XCLASS:
3217          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3218          if (PRIV(xclass)(chr, code - list_ptr[2] + 1 + LINK_SIZE, utf))
3219            return FALSE;
3220          break;
3221    #endif
3222    
3223          default:
3224          return FALSE;
3225          }
3226    
3227        chr_ptr++;
3228        }
3229      while(*chr_ptr != NOTACHAR);
3230    
3231      /* At least one character must be matched from this opcode. */
3232    
3233      if (list[1] == 0) return TRUE;
3234      }
3235    
3236    return FALSE;
3237    }
3238    
3239    
3240    
3241    /*************************************************
3242    *    Scan compiled regex for auto-possession     *
3243    *************************************************/
3244    
3245    /* Replaces single character iterations with their possessive alternatives
3246    if appropriate. This function modifies the compiled opcode!
3247    
3248    Arguments:
3249      code        points to start of the byte code
3250      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3251      cd          static compile data
3252    
3253    Returns:      nothing
3254    */
3255    
3256    static void
3257    auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
3258    {
3259    register pcre_uchar c;
3260    const pcre_uchar *end;
3261    pcre_uint32 list[8];
3262    
3263    for (;;)
3264      {
3265      c = *code;
3266    
3267      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3268        {
3269        c -= get_repeat_base(c) - OP_STAR;
3270        end = (c <= OP_MINUPTO) ?
3271          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3272        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3273    
3274        if (end != NULL && compare_opcodes(end, utf, cd, list))
3275          {
3276          switch(c)
3277            {
3278            case OP_STAR:
3279            *code += OP_POSSTAR - OP_STAR;
3280            break;
3281    
3282            case OP_MINSTAR:
3283            *code += OP_POSSTAR - OP_MINSTAR;
3284            break;
3285    
3286            case OP_PLUS:
3287            *code += OP_POSPLUS - OP_PLUS;
3288            break;
3289    
3290            case OP_MINPLUS:
3291            *code += OP_POSPLUS - OP_MINPLUS;
3292            break;
3293    
3294            case OP_QUERY:
3295            *code += OP_POSQUERY - OP_QUERY;
3296            break;
3297    
3298            case OP_MINQUERY:
3299            *code += OP_POSQUERY - OP_MINQUERY;
3300            break;
3301    
3302            case OP_UPTO:
3303            *code += OP_POSUPTO - OP_UPTO;
3304            break;
3305    
3306            case OP_MINUPTO:
3307            *code += OP_MINUPTO - OP_UPTO;
3308            break;
3309            }
3310          }
3311        c = *code;
3312        }
3313    
3314      switch(c)
3315        {
3316        case OP_END:
3317        return;
3318    
3319        case OP_TYPESTAR:
3320        case OP_TYPEMINSTAR:
3321        case OP_TYPEPLUS:
3322        case OP_TYPEMINPLUS:
3323        case OP_TYPEQUERY:
3324        case OP_TYPEMINQUERY:
3325        case OP_TYPEPOSSTAR:
3326        case OP_TYPEPOSPLUS:
3327        case OP_TYPEPOSQUERY:
3328        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3329        break;
3330    
3331        case OP_TYPEUPTO:
3332        case OP_TYPEMINUPTO:
3333        case OP_TYPEEXACT:
3334        case OP_TYPEPOSUPTO:
3335        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3336          code += 2;
3337        break;
3338    
3339        case OP_XCLASS:
3340        code += GET(code, 1);
3341        break;
3342    
3343        case OP_MARK:
3344        case OP_PRUNE_ARG:
3345        case OP_SKIP_ARG:
3346        case OP_THEN_ARG:
3347        code += code[1];
3348        break;
3349      }      }
   }  
   
 return TRUE;  
 }  
   
   
   
 /*************************************************  
 *    Scan compiled regex for non-emptiness       *  
 *************************************************/  
3350    
3351  /* This function is called to check for left recursive calls. We want to check    /* Add in the fixed length from the table */
 the current branch of the current pattern to see if it could match the empty  
 string. If it could, we must look outwards for branches at other levels,  
 stopping when we pass beyond the bracket which is the subject of the recursion.  
 This function is called only during the real compile, not during the  
 pre-compile.  
3352    
3353  Arguments:    code += PRIV(OP_lengths)[c];
   code        points to start of the recursion  
   endcode     points to where to stop (current RECURSE item)  
   bcptr       points to the chain of current (unclosed) branch starts  
   utf         TRUE if in UTF-8 / UTF-16 mode  
   cd          pointers to tables etc  
3354    
3355  Returns:      TRUE if what is matched could be empty    /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3356  */    a multi-byte character. The length in the table is a minimum, so we have to
3357      arrange to skip the extra bytes. */
3358    
3359  static BOOL  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3360  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,    if (utf) switch(c)
3361    branch_chain *bcptr, BOOL utf, compile_data *cd)      {
3362  {      case OP_CHAR:
3363  while (bcptr != NULL && bcptr->current_branch >= code)      case OP_CHARI:
3364    {      case OP_NOT:
3365    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))      case OP_NOTI:
3366      return FALSE;      case OP_STAR:
3367    bcptr = bcptr->outer;      case OP_MINSTAR:
3368        case OP_PLUS:
3369        case OP_MINPLUS:
3370        case OP_QUERY:
3371        case OP_MINQUERY:
3372        case OP_UPTO:
3373        case OP_MINUPTO:
3374        case OP_EXACT:
3375        case OP_POSSTAR:
3376        case OP_POSPLUS:
3377        case OP_POSQUERY:
3378        case OP_POSUPTO:
3379        case OP_STARI:
3380        case OP_MINSTARI:
3381        case OP_PLUSI:
3382        case OP_MINPLUSI:
3383        case OP_QUERYI:
3384        case OP_MINQUERYI:
3385        case OP_UPTOI:
3386        case OP_MINUPTOI:
3387        case OP_EXACTI:
3388        case OP_POSSTARI:
3389        case OP_POSPLUSI:
3390        case OP_POSQUERYI:
3391        case OP_POSUPTOI:
3392        case OP_NOTSTAR:
3393        case OP_NOTMINSTAR:
3394        case OP_NOTPLUS:
3395        case OP_NOTMINPLUS:
3396        case OP_NOTQUERY:
3397        case OP_NOTMINQUERY:
3398        case OP_NOTUPTO:
3399        case OP_NOTMINUPTO:
3400        case OP_NOTEXACT:
3401        case OP_NOTPOSSTAR:
3402        case OP_NOTPOSPLUS:
3403        case OP_NOTPOSQUERY:
3404        case OP_NOTPOSUPTO:
3405        case OP_NOTSTARI:
3406        case OP_NOTMINSTARI:
3407        case OP_NOTPLUSI:
3408        case OP_NOTMINPLUSI:
3409        case OP_NOTQUERYI:
3410        case OP_NOTMINQUERYI:
3411        case OP_NOTUPTOI:
3412        case OP_NOTMINUPTOI:
3413        case OP_NOTEXACTI:
3414        case OP_NOTPOSSTARI:
3415        case OP_NOTPOSPLUSI:
3416        case OP_NOTPOSQUERYI:
3417        case OP_NOTPOSUPTOI:
3418        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3419        break;
3420        }
3421    #else
3422      (void)(utf);  /* Keep compiler happy by referencing function argument */
3423    #endif
3424    }    }
 return TRUE;  
3425  }  }
3426    
3427    
# Line 2673  Returns:   TRUE or FALSE Line 3471  Returns:   TRUE or FALSE
3471  static BOOL  static BOOL
3472  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3473  {  {
3474  int terminator;          /* Don't combine these lines; the Solaris cc */  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3475  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3476  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != CHAR_NULL; ptr++)
3477    {    {
3478    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3479      ptr++;      ptr++;
# Line 2722  register int yield = 0; Line 3520  register int yield = 0;
3520  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
3521    {    {
3522    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
3523      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3524    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
3525    yield++;    yield++;
3526    }    }
# Line 2754  value in the reference (which is a group Line 3552  value in the reference (which is a group
3552  Arguments:  Arguments:
3553    group      points to the start of the group    group      points to the start of the group
3554    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
3555    utf        TRUE in UTF-8 / UTF-16 mode    utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3556    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
3557    save_hwm   the hwm forward reference pointer at the start of the group    save_hwm   the hwm forward reference pointer at the start of the group
3558    
# Line 2777  while ((ptr = (pcre_uchar *)find_recurse Line 3575  while ((ptr = (pcre_uchar *)find_recurse
3575    
3576    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
3577      {      {
3578      offset = GET(hc, 0);      offset = (int)GET(hc, 0);
3579      if (cd->start_code + offset == ptr + 1)      if (cd->start_code + offset == ptr + 1)
3580        {        {
3581        PUT(hc, 0, offset + adjust);        PUT(hc, 0, offset + adjust);
# Line 2790  while ((ptr = (pcre_uchar *)find_recurse Line 3588  while ((ptr = (pcre_uchar *)find_recurse
3588    
3589    if (hc >= cd->hwm)    if (hc >= cd->hwm)
3590      {      {
3591      offset = GET(ptr, 1);      offset = (int)GET(ptr, 1);
3592      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
3593      }      }
3594    
# Line 2858  PUT(previous_callout, 2 + LINK_SIZE, len Line 3656  PUT(previous_callout, 2 + LINK_SIZE, len
3656  *************************************************/  *************************************************/
3657    
3658  /* This function is passed the start and end of a class range, in UTF-8 mode  /* This function is passed the start and end of a class range, in UTF-8 mode
3659  with UCP support. It searches up the characters, looking for internal ranges of  with UCP support. It searches up the characters, looking for ranges of
3660  characters in the "other" case. Each call returns the next one, updating the  characters in the "other" case. Each call returns the next one, updating the
3661  start address.  start address. A character with multiple other cases is returned on its own
3662    with a special return value.
3663    
3664  Arguments:  Arguments:
3665    cptr        points to starting character value; updated    cptr        points to starting character value; updated
# Line 2868  Arguments: Line 3667  Arguments:
3667    ocptr       where to put start of othercase range    ocptr       where to put start of othercase range
3668    odptr       where to put end of othercase range    odptr       where to put end of othercase range
3669    
3670  Yield:        TRUE when range returned; FALSE when no more  Yield:        -1 when no more
3671                   0 when a range is returned
3672                  >0 the CASESET offset for char with multiple other cases
3673                    in this case, ocptr contains the original
3674  */  */
3675    
3676  static BOOL  static int
3677  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
3678    unsigned int *odptr)    pcre_uint32 *odptr)
3679  {  {
3680  unsigned int c, othercase, next;  pcre_uint32 c, othercase, next;
3681    unsigned int co;
3682    
3683    /* Find the first character that has an other case. If it has multiple other
3684    cases, return its case offset value. */
3685    
3686  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
3687    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
3688      if ((co = UCD_CASESET(c)) != 0)
3689        {
3690        *ocptr = c++;   /* Character that has the set */
3691        *cptr = c;      /* Rest of input range */
3692        return (int)co;
3693        }
3694      if ((othercase = UCD_OTHERCASE(c)) != c) break;
3695      }
3696    
3697  if (c > d) return FALSE;  if (c > d) return -1;  /* Reached end of range */
3698    
3699  *ocptr = othercase;  *ocptr = othercase;
3700  next = othercase + 1;  next = othercase + 1;
# Line 2891  for (++c; c <= d; c++) Line 3705  for (++c; c <= d; c++)
3705    next++;    next++;
3706    }    }
3707    
3708  *odptr = next - 1;  *odptr = next - 1;     /* End of othercase range */
3709  *cptr = c;  *cptr = c;             /* Rest of input range */
3710    return 0;
 return TRUE;  
 }  
   
   
   
 /*************************************************  
 *        Check a character and a property        *  
 *************************************************/  
   
 /* This function is called by check_auto_possessive() when a property item  
 is adjacent to a fixed character.  
   
 Arguments:  
   c            the character  
   ptype        the property type  
   pdata        the data for the type  
   negated      TRUE if it's a negated property (\P or \p{^)  
   
 Returns:       TRUE if auto-possessifying is OK  
 */  
   
 static BOOL  
 check_char_prop(int c, int ptype, int pdata, BOOL negated)  
 {  
 const ucd_record *prop = GET_UCD(c);  
 switch(ptype)  
   {  
   case PT_LAMP:  
   return (prop->chartype == ucp_Lu ||  
           prop->chartype == ucp_Ll ||  
           prop->chartype == ucp_Lt) == negated;  
   
   case PT_GC:  
   return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;  
   
   case PT_PC:  
   return (pdata == prop->chartype) == negated;  
   
   case PT_SC:  
   return (pdata == prop->script) == negated;  
   
   /* These are specials */  
   
   case PT_ALNUM:  
   return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||  
           PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;  
   
   case PT_SPACE:    /* Perl space */  
   return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||  
           c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)  
           == negated;  
   
   case PT_PXSPACE:  /* POSIX space */  
   return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||  
           c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||  
           c == CHAR_FF || c == CHAR_CR)  
           == negated;  
   
   case PT_WORD:  
   return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||  
           PRIV(ucp_gentype)[prop->chartype] == ucp_N ||  
           c == CHAR_UNDERSCORE) == negated;  
   }  
 return FALSE;  
3711  }  }
3712  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3713    
3714    
3715    
3716  /*************************************************  /*************************************************
3717  *     Check if auto-possessifying is possible    *  *        Add a character or range to a class     *
3718  *************************************************/  *************************************************/
3719    
3720  /* This function is called for unlimited repeats of certain items, to see  /* This function packages up the logic of adding a character or range of
3721  whether the next thing could possibly match the repeated item. If not, it makes  characters to a class. The character values in the arguments will be within the
3722  sense to automatically possessify the repeated item.  valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3723    mutually recursive with the function immediately below.
3724    
3725  Arguments:  Arguments:
3726    previous      pointer to the repeated opcode    classbits     the bit map for characters < 256
3727    utf           TRUE in UTF-8 / UTF-16 mode    uchardptr     points to the pointer for extra data
3728    ptr           next character in pattern    options       the options word
   options       options bits  
3729    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
3730      start         start of range character
3731      end           end of range character
3732    
3733  Returns:        TRUE if possessifying is wanted  Returns:        the number of < 256 characters added
3734                    the pointer to extra data is updated
3735  */  */
3736    
3737  static BOOL  static int
3738  check_auto_possessive(const pcre_uchar *previous, BOOL utf,  add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3739    const pcre_uchar *ptr, int options, compile_data *cd)    compile_data *cd, pcre_uint32 start, pcre_uint32 end)
3740  {  {
3741  pcre_int32 c, next;  pcre_uint32 c;
3742  int op_code = *previous++;  int n8 = 0;
   
 /* Skip whitespace and comments in extended mode */  
   
 if ((options & PCRE_EXTENDED) != 0)  
   {  
   for (;;)  
     {  
     while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;  
     if (*ptr == CHAR_NUMBER_SIGN)  
       {  
       ptr++;  
       while (*ptr != 0)  
         {  
         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }  
         ptr++;  
 #ifdef SUPPORT_UTF  
         if (utf) FORWARDCHAR(ptr);  
 #endif  
         }  
       }  
     else break;  
     }  
   }  
   
 /* If the next item is one that we can handle, get its value. A non-negative  
 value is a character, a negative value is an escape value. */  
   
 if (*ptr == CHAR_BACKSLASH)  
   {  
   int temperrorcode = 0;  
   next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);  
   if (temperrorcode != 0) return FALSE;  
   ptr++;    /* Point after the escape sequence */  
   }  
 else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)  
   {  
 #ifdef SUPPORT_UTF  
   if (utf) { GETCHARINC(next, ptr); } else  
 #endif  
   next = *ptr++;  
   }  
 else return FALSE;  
   
 /* Skip whitespace and comments in extended mode */  
   
 if ((options & PCRE_EXTENDED) != 0)  
   {  
   for (;;)  
     {  
     while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;  
     if (*ptr == CHAR_NUMBER_SIGN)  
       {  
       ptr++;  
       while (*ptr != 0)  
         {  
         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }  
         ptr++;  
 #ifdef SUPPORT_UTF  
         if (utf) FORWARDCHAR(ptr);  
 #endif  
         }  
       }  
     else break;  
     }  
   }  
   
 /* If the next thing is itself optional, we have to give up. */  
   
 if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  
   STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)  
     return FALSE;  
3743    
3744  /* Now compare the next item with the previous opcode. First, handle cases when  /* If caseless matching is required, scan the range and process alternate
3745  the next item is a character. */  cases. In Unicode, there are 8-bit characters that have alternate cases that
3746    are greater than 255 and vice-versa. Sometimes we can just extend the original
3747    range. */
3748    
3749  if (next >= 0) switch(op_code)  if ((options & PCRE_CASELESS) != 0)
3750    {    {
   case OP_CHAR:  
 #ifdef SUPPORT_UTF  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   return c != next;  
   
   /* For CHARI (caseless character) we must check the other case. If we have  
   Unicode property support, we can use it to test the other case of  
   high-valued characters. */  
   
   case OP_CHARI:  
 #ifdef SUPPORT_UTF  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   if (c == next) return FALSE;  
 #ifdef SUPPORT_UTF  
   if (utf)  
     {  
     unsigned int othercase;  
     if (next < 128) othercase = cd->fcc[next]; else  
3751  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3752      othercase = UCD_OTHERCASE((unsigned int)next);    if ((options & PCRE_UTF8) != 0)
 #else  
     othercase = NOTACHAR;  
 #endif  
     return (unsigned int)c != othercase;  
     }  
   else  
 #endif  /* SUPPORT_UTF */  
   return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */  
   
   case OP_NOT:  
 #ifdef SUPPORT_UTF  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   return c == next;  
   
   case OP_NOTI:  
 #ifdef SUPPORT_UTF  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   if (c == next) return TRUE;  
 #ifdef SUPPORT_UTF  
   if (utf)  
3753      {      {
3754      unsigned int othercase;      int rc;
3755      if (next < 128) othercase = cd->fcc[next]; else      pcre_uint32 oc, od;
 #ifdef SUPPORT_UCP  
     othercase = UCD_OTHERCASE((unsigned int)next);  
 #else  
     othercase = NOTACHAR;  
 #endif  
     return (unsigned int)c == othercase;  
     }  
   else  
 #endif  /* SUPPORT_UTF */  
   return (c == TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */  
3756    
3757    /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.      options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3758    When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */      c = start;
   
   case OP_DIGIT:  
   return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;  
3759    
3760    case OP_NOT_DIGIT:      while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3761    return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;        {
3762          /* Handle a single character that has more than one other case. */
3763    
3764    case OP_WHITESPACE:        if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3765    return next > 255 || (cd->ctypes[next] & ctype_space) == 0;          PRIV(ucd_caseless_sets) + rc, oc);
3766    
3767    case OP_NOT_WHITESPACE:        /* Do nothing if the other case range is within the original range. */
   return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;  
3768    
3769    case OP_WORDCHAR:        else if (oc >= start && od <= end) continue;
   return next > 255 || (cd->ctypes[next] & ctype_word) == 0;  
3770    
3771    case OP_NOT_WORDCHAR:        /* Extend the original range if there is overlap, noting that if oc < c, we
3772    return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;        can't have od > end because a subrange is always shorter than the basic
3773          range. Otherwise, use a recursive call to add the additional range. */
3774    
3775    case OP_HSPACE:        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3776    case OP_NOT_HSPACE:        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3777    switch(next)        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3778      {        }
     case 0x09:  
     case 0x20:  
     case 0xa0:  
     case 0x1680:  
     case 0x180e:  
     case 0x2000:  
     case 0x2001:  
     case 0x2002:  
     case 0x2003:  
     case 0x2004:  
     case 0x2005:  
     case 0x2006:  
     case 0x2007:  
     case 0x2008:  
     case 0x2009:  
     case 0x200A:  
     case 0x202f:  
     case 0x205f:  
     case 0x3000:  
     return op_code == OP_NOT_HSPACE;  
     default:  
     return op_code != OP_NOT_HSPACE;  
3779      }      }
3780      else
3781    #endif  /* SUPPORT_UCP */
3782    
3783    case OP_ANYNL:    /* Not UTF-mode, or no UCP */
3784    case OP_VSPACE:  
3785    case OP_NOT_VSPACE:    for (c = start; c <= end && c < 256; c++)
   switch(next)  
3786      {      {
3787      case 0x0a:      SETBIT(classbits, cd->fcc[c]);
3788      case 0x0b:      n8++;
     case 0x0c:  
     case 0x0d:  
     case 0x85:  
     case 0x2028:  
     case 0x2029:  
     return op_code == OP_NOT_VSPACE;  
     default:  
     return op_code != OP_NOT_VSPACE;  
3789      }      }
   
 #ifdef SUPPORT_UCP  
   case OP_PROP:  
   return check_char_prop(next, previous[0], previous[1], FALSE);  
   
   case OP_NOTPROP:  
   return check_char_prop(next, previous[0], previous[1], TRUE);  
 #endif  
   
   default:  
   return FALSE;  
3790    }    }
3791    
3792    /* Now handle the original range. Adjust the final value according to the bit
3793    length - this means that the same lists of (e.g.) horizontal spaces can be used
3794    in all cases. */
3795    
3796  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  #if defined COMPILE_PCRE8
 is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  
 generated only when PCRE_UCP is *not* set, that is, when only ASCII  
 characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are  
 replaced by OP_PROP codes when PCRE_UCP is set. */  
   
 switch(op_code)  
   {  
   case OP_CHAR:  
   case OP_CHARI:  
3797  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3798    GETCHARTEST(c, previous);    if ((options & PCRE_UTF8) == 0)
 #else  
   c = *previous;  
3799  #endif  #endif
3800    switch(-next)    if (end > 0xff) end = 0xff;
     {  
     case ESC_d:  
     return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;  
3801    
3802      case ESC_D:  #elif defined COMPILE_PCRE16
3803      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;  #ifdef SUPPORT_UTF
3804      if ((options & PCRE_UTF16) == 0)
3805    #endif
3806      if (end > 0xffff) end = 0xffff;
3807    
3808      case ESC_s:  #endif /* COMPILE_PCRE[8|16] */
     return c > 255 || (cd->ctypes[c] & ctype_space) == 0;  
3809    
3810      case ESC_S:  /* If all characters are less than 256, use the bit map. Otherwise use extra
3811      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;  data. */
3812    
3813      case ESC_w:  if (end < 0x100)
3814      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;    {
3815      for (c = start; c <= end; c++)
3816        {
3817        n8++;
3818        SETBIT(classbits, c);
3819        }
3820      }
3821    
3822      case ESC_W:  else
3823      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;    {
3824      pcre_uchar *uchardata = *uchardptr;
3825    
3826      case ESC_h:  #ifdef SUPPORT_UTF
3827      case ESC_H:    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3828      switch(c)      {
3829        if (start < end)
3830        {        {
3831        case 0x09:        *uchardata++ = XCL_RANGE;
3832        case 0x20:        uchardata += PRIV(ord2utf)(start, uchardata);
3833        case 0xa0:        uchardata += PRIV(ord2utf)(end, uchardata);
       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;  
       default:  
       return -next == ESC_h;  
3834        }        }
3835        else if (start == end)
     case ESC_v:  
     case ESC_V:  
     switch(c)  
3836        {        {
3837        case 0x0a:        *uchardata++ = XCL_SINGLE;
3838        case 0x0b:        uchardata += PRIV(ord2utf)(start, uchardata);
       case 0x0c:  
       case 0x0d:  
       case 0x85:  
       case 0x2028:  
       case 0x2029:  
       return -next != ESC_v;  
       default:  
       return -next == ESC_v;  
       }  
   
     /* When PCRE_UCP is set, these values get generated for \d etc. Find  
     their substitutions and process them. The result will always be either  
     -ESC_p or -ESC_P. Then fall through to process those values. */  
   
 #ifdef SUPPORT_UCP  
     case ESC_du:  
     case ESC_DU:  
     case ESC_wu:  
     case ESC_WU:  
     case ESC_su:  
     case ESC_SU:  
       {  
       int temperrorcode = 0;  
       ptr = substitutes[-next - ESC_DU];  
       next = check_escape(&ptr, &temperrorcode, 0, options, FALSE);  
       if (temperrorcode != 0) return FALSE;  
       ptr++;    /* For compatibility */  
3839        }        }
3840      /* Fall through */      }
3841      else
3842    #endif  /* SUPPORT_UTF */
3843    
3844      case ESC_p:    /* Without UTF support, character values are constrained by the bit length,
3845      case ESC_P:    and can only be > 256 for 16-bit and 32-bit libraries. */
       {  
       int ptype, pdata, errorcodeptr;  
       BOOL negated;  
3846    
3847        ptr--;      /* Make ptr point at the p or P */  #ifdef COMPILE_PCRE8
3848        ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);      {}
3849        if (ptype < 0) return FALSE;  #else
3850        ptr++;      /* Point past the final curly ket */    if (start < end)
3851        {
3852        *uchardata++ = XCL_RANGE;
3853        *uchardata++ = start;
3854        *uchardata++ = end;
3855        }
3856      else if (start == end)
3857        {
3858        *uchardata++ = XCL_SINGLE;
3859        *uchardata++ = start;
3860        }
3861    #endif
3862    
3863        /* If the property item is optional, we have to give up. (When generated    *uchardptr = uchardata;   /* Updata extra data pointer */
3864        from \d etc by PCRE_UCP, this test will have been applied much earlier,    }
       to the original \d etc. At this point, ptr will point to a zero byte. */  
3865    
3866        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  return n8;    /* Number of 8-bit characters */
3867          STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)  }
           return FALSE;  
3868    
       /* Do the property check. */  
3869    
       return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);  
       }  
 #endif  
3870    
     default:  
     return FALSE;  
     }  
3871    
3872    /* In principle, support for Unicode properties should be integrated here as  /*************************************************
3873    well. It means re-organizing the above code so as to get hold of the property  *        Add a list of characters to a class     *
3874    values before switching on the op-code. However, I wonder how many patterns  *************************************************/
   combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,  
   these op-codes are never generated.) */  
3875    
3876    case OP_DIGIT:  /* This function is used for adding a list of case-equivalent characters to a
3877    return next == -ESC_D || next == -ESC_s || next == -ESC_W ||  class, and also for adding a list of horizontal or vertical whitespace. If the
3878           next == -ESC_h || next == -ESC_v || next == -ESC_R;  list is in order (which it should be), ranges of characters are detected and
3879    handled appropriately. This function is mutually recursive with the function
3880    above.
3881    
3882    case OP_NOT_DIGIT:  Arguments:
3883    return next == -ESC_d;    classbits     the bit map for characters < 256
3884      uchardptr     points to the pointer for extra data
3885      options       the options word
3886      cd            contains pointers to tables etc.
3887      p             points to row of 32-bit values, terminated by NOTACHAR
3888      except        character to omit; this is used when adding lists of
3889                      case-equivalent characters to avoid including the one we
3890                      already know about
3891    
3892    case OP_WHITESPACE:  Returns:        the number of < 256 characters added
3893    return next == -ESC_S || next == -ESC_d || next == -ESC_w;                  the pointer to extra data is updated
3894    */
3895    
3896    case OP_NOT_WHITESPACE:  static int
3897    return next == -ESC_s || next == -ESC_h || next == -ESC_v || next == -ESC_R;  add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3898      compile_data *cd, const pcre_uint32 *p, unsigned int except)
3899    {
3900    int n8 = 0;
3901    while (p[0] < NOTACHAR)
3902      {
3903      int n = 0;
3904      if (p[0] != except)
3905        {
3906        while(p[n+1] == p[0] + n + 1) n++;
3907        n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3908        }
3909      p += n + 1;
3910      }
3911    return n8;
3912    }
3913    
   case OP_HSPACE:  
   return next == -ESC_S || next == -ESC_H || next == -ESC_d ||  
          next == -ESC_w || next == -ESC_v || next == -ESC_R;  
3914    
   case OP_NOT_HSPACE:  
   return next == -ESC_h;  
3915    
3916    /* Can't have \S in here because VT matches \S (Perl anomaly) */  /*************************************************
3917    case OP_ANYNL:  *    Add characters not in a list to a class     *
3918    case OP_VSPACE:  *************************************************/
   return next == -ESC_V || next == -ESC_d || next == -ESC_w;  
3919    
3920    case OP_NOT_VSPACE:  /* This function is used for adding the complement of a list of horizontal or
3921    return next == -ESC_v || next == -ESC_R;  vertical whitespace to a class. The list must be in order.
3922    
3923    case OP_WORDCHAR:  Arguments:
3924    return next == -ESC_W || next == -ESC_s || next == -ESC_h ||    classbits     the bit map for characters < 256
3925           next == -ESC_v || next == -ESC_R;    uchardptr     points to the pointer for extra data
3926      options       the options word
3927      cd            contains pointers to tables etc.
3928      p             points to row of 32-bit values, terminated by NOTACHAR
3929    
3930    case OP_NOT_WORDCHAR:  Returns:        the number of < 256 characters added
3931    return next == -ESC_w || next == -ESC_d;                  the pointer to extra data is updated
3932    */
3933    
3934    default:  static int
3935    return FALSE;  add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3936      int options, compile_data *cd, const pcre_uint32 *p)
3937    {
3938    BOOL utf = (options & PCRE_UTF8) != 0;
3939    int n8 = 0;
3940    if (p[0] > 0)
3941      n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
3942    while (p[0] < NOTACHAR)
3943      {
3944      while (p[1] == p[0] + 1) p++;
3945      n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3946        (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
3947      p++;
3948    }    }
3949    return n8;
 /* Control does not reach here */  
3950  }  }
3951    
3952    
# Line 3398  to find out the amount of memory needed, Line 3962  to find out the amount of memory needed,
3962  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
3963    
3964  Arguments:  Arguments:
3965    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
3966    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
3967    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
3968    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
3969    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr      place to put the first required character
3970    reqcharptr     set to the last literal character required, else < 0    firstcharflagsptr place to put the first character flags, or a negative number
3971    bcptr          points to current branch chain    reqcharptr        place to put the last required character
3972    cond_depth     conditional nesting depth    reqcharflagsptr   place to put the last required character flags, or a negative number
3973    cd             contains pointers to tables etc.    bcptr             points to current branch chain
3974    lengthptr      NULL during the real compile phase    cond_depth        conditional nesting depth
3975                   points to length accumulator during pre-compile phase    cd                contains pointers to tables etc.
3976      lengthptr         NULL during the real compile phase
3977                        points to length accumulator during pre-compile phase
3978    
3979  Returns:         TRUE on success  Returns:            TRUE on success
3980                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
3981  */  */
3982    
3983  static BOOL  static BOOL
3984  compile_branch(int *optionsptr, pcre_uchar **codeptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3985    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,    const pcre_uchar **ptrptr, int *errorcodeptr,
3986    pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,    pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
3987      pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
3988      branch_chain *bcptr, int cond_depth,
3989    compile_data *cd, int *lengthptr)    compile_data *cd, int *lengthptr)
3990  {  {
3991  int repeat_type, op_type;  int repeat_type, op_type;
3992  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3993  int bravalue = 0;  int bravalue = 0;
3994  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3995  pcre_int32 firstchar, reqchar;  pcre_uint32 firstchar, reqchar;
3996  pcre_int32 zeroreqchar, zerofirstchar;  pcre_int32 firstcharflags, reqcharflags;
3997    pcre_uint32 zeroreqchar, zerofirstchar;
3998    pcre_int32 zeroreqcharflags, zerofirstcharflags;
3999  pcre_int32 req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
4000  int options = *optionsptr;               /* May change dynamically */  int options = *optionsptr;               /* May change dynamically */
4001  int after_manual_callout = 0;  int after_manual_callout = 0;
4002  int length_prevgroup = 0;  int length_prevgroup = 0;
4003  register int c;  register pcre_uint32 c;
4004    int escape;
4005  register pcre_uchar *code = *codeptr;  register pcre_uchar *code = *codeptr;
4006  pcre_uchar *last_code = code;  pcre_uchar *last_code = code;
4007  pcre_uchar *orig_code = code;  pcre_uchar *orig_code = code;
# Line 3450  must not do this for other options (e.g. Line 4021  must not do this for other options (e.g.
4021  dynamically as we process the pattern. */  dynamically as we process the pattern. */
4022    
4023  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4024  /* PCRE_UTF16 has the same value as PCRE_UTF8. */  /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
4025  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
4026    #ifndef COMPILE_PCRE32
4027  pcre_uchar utf_chars[6];  pcre_uchar utf_chars[6];
4028    #endif
4029  #else  #else
4030  BOOL utf = FALSE;  BOOL utf = FALSE;
4031  #endif  #endif
4032    
4033  /* Helper variables for OP_XCLASS opcode (for characters > 255). */  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
4034    class_uchardata always so that it can be passed to add_to_class() always,
4035    though it will not be used in non-UTF 8-bit cases. This avoids having to supply
4036    alternative calls for the different cases. */
4037    
4038    pcre_uchar *class_uchardata;
4039  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4040  BOOL xclass;  BOOL xclass;
 pcre_uchar *class_uchardata;  
4041  pcre_uchar *class_uchardata_base;  pcre_uchar *class_uchardata_base;
4042  #endif  #endif
4043    
# Line 3484  to take the zero repeat into account. Th Line 4060  to take the zero repeat into account. Th
4060  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
4061  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
4062    
4063  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
4064    firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
4065    
4066  /* The variable req_caseopt contains either the REQ_CASELESS value  /* The variable req_caseopt contains either the REQ_CASELESS value
4067  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 4082  for (;; ptr++)
4082    BOOL is_recurse;    BOOL is_recurse;
4083    BOOL reset_bracount;    BOOL reset_bracount;
4084    int class_has_8bitchar;    int class_has_8bitchar;
4085    int class_single_char;    int class_one_char;
4086    int newoptions;    int newoptions;
4087    int recno;    int recno;
4088    int refsign;    int refsign;
4089    int skipbytes;    int skipbytes;
4090    int subreqchar;    pcre_uint32 subreqchar, subfirstchar;
4091    int subfirstchar;    pcre_int32 subreqcharflags, subfirstcharflags;
4092    int terminator;    int terminator;
4093    int mclength;    unsigned int mclength;
4094    int tempbracount;    unsigned int tempbracount;
4095      pcre_uint32 ec;
4096    pcre_uchar mcbuffer[8];    pcre_uchar mcbuffer[8];
4097    
4098    /* Get next character in the pattern */    /* Get next character in the pattern */
# Line 3524  for (;; ptr++) Line 4102  for (;; ptr++)
4102    /* 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
4103    string. Nesting only happens one level deep. */    string. Nesting only happens one level deep. */
4104    
4105    if (c == 0 && nestptr != NULL)    if (c == CHAR_NULL && nestptr != NULL)
4106      {      {
4107      ptr = nestptr;      ptr = nestptr;
4108      nestptr = NULL;      nestptr = NULL;
# Line 3599  for (;; ptr++) Line 4177  for (;; ptr++)
4177    
4178    /* 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 */
4179    
4180    if (inescq && c != 0)    if (inescq && c != CHAR_NULL)
4181      {      {
4182      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
4183        {        {
# Line 3647  for (;; ptr++) Line 4225  for (;; ptr++)
4225      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
4226        {        {
4227        ptr++;        ptr++;
4228        while (*ptr != 0)        while (*ptr != CHAR_NULL)
4229          {          {
4230          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
4231          ptr++;          ptr++;
# Line 3655  for (;; ptr++) Line 4233  for (;; ptr++)
4233          if (utf) FORWARDCHAR(ptr);          if (utf) FORWARDCHAR(ptr);
4234  #endif  #endif
4235          }          }
4236        if (*ptr != 0) continue;        if (*ptr != CHAR_NULL) continue;
4237    
4238        /* Else fall through to handle end of string */        /* Else fall through to handle end of string */
4239        c = 0;        c = 0;
# Line 3677  for (;; ptr++) Line 4255  for (;; ptr++)
4255      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
4256      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
4257      *firstcharptr = firstchar;      *firstcharptr = firstchar;
4258        *firstcharflagsptr = firstcharflags;
4259      *reqcharptr = reqchar;      *reqcharptr = reqchar;
4260        *reqcharflagsptr = reqcharflags;
4261      *codeptr = code;      *codeptr = code;
4262      *ptrptr = ptr;      *ptrptr = ptr;
4263      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 3701  for (;; ptr++) Line 4281  for (;; ptr++)
4281      previous = NULL;      previous = NULL;
4282      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
4283        {        {
4284        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4285        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
4286        }        }
4287      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 3716  for (;; ptr++) Line 4296  for (;; ptr++)
4296      repeats. The value of reqchar doesn't change either. */      repeats. The value of reqchar doesn't change either. */
4297    
4298      case CHAR_DOT:      case CHAR_DOT:
4299      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4300      zerofirstchar = firstchar;      zerofirstchar = firstchar;
4301        zerofirstcharflags = firstcharflags;
4302      zeroreqchar = reqchar;      zeroreqchar = reqchar;
4303        zeroreqcharflags = reqcharflags;
4304      previous = code;      previous = code;
4305      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
4306      break;      break;
# Line 3792  for (;; ptr++) Line 4374  for (;; ptr++)
4374          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4375        {        {
4376        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
4377        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4378        zerofirstchar = firstchar;        zerofirstchar = firstchar;
4379          zerofirstcharflags = firstcharflags;
4380        break;        break;
4381        }        }
4382    
# Line 3803  for (;; ptr++) Line 4386  for (;; ptr++)
4386    
4387      should_flip_negation = FALSE;      should_flip_negation = FALSE;
4388    
4389      /* For optimization purposes, we track some properties of the class.      /* For optimization purposes, we track some properties of the class:
4390      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 <
4391      < 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
4392      a single character. */      character. */
4393    
4394      class_has_8bitchar = 0;      class_has_8bitchar = 0;
4395      class_single_char = 0;      class_one_char = 0;
4396    
4397      /* 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
4398      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
4399      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
4400      */      map. */
4401    
4402      memset(classbits, 0, 32 * sizeof(pcre_uint8));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
4403    
4404  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4405      xclass = FALSE;                           /* No chars >= 256 */      xclass = FALSE;
4406      class_uchardata = code + LINK_SIZE + 2;   /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
4407      class_uchardata_base = class_uchardata;   /* For resetting in pass 1 */      class_uchardata_base = class_uchardata;   /* Save the start */
4408  #endif  #endif
4409    
4410      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
4411      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
4412      loop, c contains the first byte of the character. */      loop, c contains the first byte of the character. */
4413    
4414      if (c != 0) do      if (c != CHAR_NULL) do
4415        {        {
4416        const pcre_uchar *oldptr;        const pcre_uchar *oldptr;
4417    
# Line 3843  for (;; ptr++) Line 4426  for (;; ptr++)
4426        /* In the pre-compile phase, accumulate the length of any extra        /* In the pre-compile phase, accumulate the length of any extra
4427        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
4428        contain a zillion > 255 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
4429        (which is on the stack). */        (which is on the stack). We have to remember that there was XCLASS data,
4430          however. */
4431    
4432        if (lengthptr != NULL)        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
4433          {          {
4434            xclass = TRUE;
4435          *lengthptr += class_uchardata - class_uchardata_base;          *lengthptr += class_uchardata - class_uchardata_base;
4436          class_uchardata = class_uchardata_base;          class_uchardata = class_uchardata_base;
4437          }          }
# Line 3948  for (;; ptr++) Line 4533  for (;; ptr++)
4533              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4534            }            }
4535    
4536          /* Not see if we need to remove any special characters. An option          /* Now see if we need to remove any special characters. An option
4537          value of 1 removes vertical space and 2 removes underscore. */          value of 1 removes vertical space and 2 removes underscore. */
4538    
4539          if (tabopt < 0) tabopt = -tabopt;          if (tabopt < 0) tabopt = -tabopt;
# Line 3964  for (;; ptr++) Line 4549  for (;; ptr++)
4549            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4550    
4551          ptr = tempptr + 1;          ptr = tempptr + 1;
4552          /* Every class contains at least one < 256 characters. */          /* Every class contains at least one < 256 character. */
4553          class_has_8bitchar = 1;          class_has_8bitchar = 1;
4554          /* Every class contains at least two characters. */          /* Every class contains at least two characters. */
4555          class_single_char = 2;          class_one_char = 2;
4556          continue;    /* End of POSIX syntax handling */          continue;    /* End of POSIX syntax handling */
4557          }          }
4558    
# Line 3975  for (;; ptr++) Line 4560  for (;; ptr++)
4560        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
4561        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
4562        assume that other escapes have more than one character in them, so        assume that other escapes have more than one character in them, so
4563        speculatively set both class_has_8bitchar and class_single_char bigger        speculatively set both class_has_8bitchar and class_one_char bigger
4564        than one. Unrecognized escapes fall through and are either treated        than one. Unrecognized escapes fall through and are either treated
4565        as literal characters (by default), or are faulted if        as literal characters (by default), or are faulted if
4566        PCRE_EXTRA is set. */        PCRE_EXTRA is set. */
4567    
4568        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
4569          {          {
4570          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
4571              TRUE);
4572          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
4573            if (escape == 0) c = ec;
4574          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 */
4575          else if (-c == ESC_N)            /* \N is not supported in a class */          else if (escape == ESC_N)          /* \N is not supported in a class */
4576            {            {
4577            *errorcodeptr = ERR71;            *errorcodeptr = ERR71;
4578            goto FAILED;            goto FAILED;
4579            }            }
4580          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (escape == ESC_Q)            /* Handle start of quoted string */
4581            {            {
4582            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4583              {              {
# Line 4000  for (;; ptr++) Line 4586  for (;; ptr++)
4586            else inescq = TRUE;            else inescq = TRUE;
4587            continue;            continue;
4588            }            }
4589          else if (-c == ESC_E) continue;  /* Ignore orphan \E */          else if (escape == ESC_E) continue;  /* Ignore orphan \E */
4590    
4591          if (c < 0)          else
4592            {            {
4593            register const pcre_uint8 *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
4594            /* Every class contains at least two < 256 characters. */            /* Every class contains at least two < 256 characters. */
4595            class_has_8bitchar++;            class_has_8bitchar++;
4596            /* Every class contains at least two characters. */            /* Every class contains at least two characters. */
4597            class_single_char += 2;            class_one_char += 2;
4598    
4599            switch (-c)            switch (escape)
4600              {              {
4601  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4602              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 4606  for (;; ptr++)
4606              case ESC_su:     /* of the default ASCII testing. */              case ESC_su:     /* of the default ASCII testing. */
4607              case ESC_SU:              case ESC_SU:
4608              nestptr = ptr;              nestptr = ptr;
4609              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */              ptr = substitutes[escape - ESC_DU] - 1;  /* Just before substitute */
4610              class_has_8bitchar--;                /* Undo! */              class_has_8bitchar--;                /* Undo! */
4611              continue;              continue;
4612  #endif  #endif
# Line 4042  for (;; ptr++) Line 4628  for (;; ptr++)
4628              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
4629              continue;              continue;
4630    
4631              /* Perl 5.004 onwards omits VT from \s, but we must preserve it              /* Perl 5.004 onwards omitted VT from \s, but restored it at Perl
4632              if it was previously set by something earlier in the character              5.18. Before PCRE 8.34, we had to preserve the VT bit if it was
4633              class. */              previously set by something earlier in the character class.
4634                Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
4635                we could just adjust the appropriate bit. From PCRE 8.34 we no
4636                longer treat \s and \S specially. */
4637    
4638              case ESC_s:              case ESC_s:
4639              classbits[0] |= cbits[cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
             classbits[1] |= cbits[cbit_space+1] & ~0x08;  
             for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];  
4640              continue;              continue;
4641    
4642              case ESC_S:              case ESC_S:
4643              should_flip_negation = TRUE;              should_flip_negation = TRUE;
4644              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
             classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */  
4645              continue;              continue;
4646    
4647                /* The rest apply in both UCP and non-UCP cases. */
4648    
4649              case ESC_h:              case ESC_h:
4650              SETBIT(classbits, 0x09); /* VT */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4651              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  
4652              continue;              continue;
4653    
4654              case ESC_H:              case ESC_H:
4655              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4656                {                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  
4657              continue;              continue;
4658    
4659              case ESC_v:              case ESC_v:
4660              SETBIT(classbits, 0x0a); /* LF */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4661              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  
4662              continue;              continue;
4663    
4664              case ESC_V:              case ESC_V:
4665              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4666                {                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  
4667              continue;              continue;
4668    
4669  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 4239  for (;; ptr++) Line 4671  for (;; ptr++)
4671              case ESC_P:              case ESC_P:
4672                {                {
4673                BOOL negated;                BOOL negated;
4674                int pdata;                unsigned int ptype = 0, pdata = 0;
4675                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
4676                if (ptype < 0) goto FAILED;                  goto FAILED;
4677                xclass = TRUE;                *class_uchardata++ = ((escape == ESC_p) != negated)?
               *class_uchardata++ = ((-c == ESC_p) != negated)?  
4678                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
4679                *class_uchardata++ = ptype;                *class_uchardata++ = ptype;
4680                *class_uchardata++ = pdata;                *class_uchardata++ = pdata;
# Line 4262  for (;; ptr++) Line 4693  for (;; ptr++)
4693                goto FAILED;                goto FAILED;
4694                }                }
4695              class_has_8bitchar--;    /* Undo the speculative increase. */              class_has_8bitchar--;    /* Undo the speculative increase. */
4696              class_single_char -= 2;  /* Undo the speculative increase. */              class_one_char -= 2;     /* Undo the speculative increase. */
4697              c = *ptr;                /* Get the final character and fall through */              c = *ptr;                /* Get the final character and fall through */
4698              break;              break;
4699              }              }
4700            }            }
4701    
4702          /* 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).
4703          greater than 256. */          This may be greater than 256. */
4704    
4705            escape = 0;
4706    
4707          }   /* End of backslash handling */          }   /* End of backslash handling */
4708    
4709        /* 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
4710        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
4711        at the end is treated as a literal. Perl ignores orphaned \E sequences        treated as a literal. Perl ignores orphaned \E sequences entirely. The
4712        entirely. The code for handling \Q and \E is messy. */        code for handling \Q and \E is messy. */
4713    
4714        CHECK_RANGE:        CHECK_RANGE:
4715        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 4284  for (;; ptr++) Line 4717  for (;; ptr++)
4717          inescq = FALSE;          inescq = FALSE;
4718          ptr += 2;          ptr += 2;
4719          }          }
   
4720        oldptr = ptr;        oldptr = ptr;
4721    
4722        /* Remember \r or \n */        /* Remember if \r or \n were explicitly used */
4723    
4724        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4725    
# Line 4295  for (;; ptr++) Line 4727  for (;; ptr++)
4727    
4728        if (!inescq && ptr[1] == CHAR_MINUS)        if (!inescq && ptr[1] == CHAR_MINUS)
4729          {          {
4730          int d;          pcre_uint32 d;
4731          ptr += 2;          ptr += 2;
4732          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;
4733    
# Line 4311  for (;; ptr++) Line 4743  for (;; ptr++)
4743            break;            break;
4744            }            }
4745    
4746          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          /* Minus (hyphen) at the end of a class is treated as a literal, so put
4747            back the pointer and jump to handle the character that preceded it. */
4748    
4749            if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4750            {            {
4751            ptr = oldptr;            ptr = oldptr;
4752            goto LONE_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4753            }            }
4754    
4755            /* Otherwise, we have a potential range; pick up the next character */
4756    
4757  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4758          if (utf)          if (utf)
4759            {                           /* Braces are required because the */            {                           /* Braces are required because the */
# Line 4332  for (;; ptr++) Line 4769  for (;; ptr++)
4769    
4770          if (!inescq && d == CHAR_BACKSLASH)          if (!inescq && d == CHAR_BACKSLASH)
4771            {            {
4772            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            int descape;
4773              descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
4774            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4775    
4776            /* \b is backspace; any other special means the '-' was literal */            /* \b is backspace; any other special means the '-' was literal. */
4777    
4778            if (d < 0)            if (descape != 0)
4779              {              {
4780              if (d == -ESC_b) d = CHAR_BS; else              if (descape == ESC_b) d = CHAR_BS; else
4781                {                {
4782                ptr = oldptr;                ptr = oldptr;
4783                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4784                }                }
4785              }              }
4786            }            }
4787    
4788          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
4789          one-character ranges */          one-character ranges. */
4790    
4791          if (d < c)          if (d < c)
4792            {            {
4793            *errorcodeptr = ERR8;            *errorcodeptr = ERR8;
4794            goto FAILED;            goto FAILED;
4795            }            }
4796            if (d == c) goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4797    
4798            /* We have found a character range, so single character optimizations
4799            cannot be done anymore. Any value greater than 1 indicates that there
4800            is more than one character. */
4801    
4802          if (d == c) goto LONE_SINGLE_CHARACTER;  /* A few lines below */          class_one_char = 2;
4803    
4804          /* Remember \r or \n */          /* Remember an explicit \r or \n, and add the range to the class. */
4805    
4806          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4807    
4808          /* Since we found a character range, single character optimizations          class_has_8bitchar +=
4809          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. */  
4810    
4811  #ifdef SUPPORT_UCP          continue;   /* Go get the next char in the class */
4812  #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;  
                 }  
4813    
4814                if (occ == ocd)        /* Handle a single character - we can get here for a normal non-escape
4815                  {        char, or after \ that introduces a single character or for an apparent
4816                  *class_uchardata++ = XCL_SINGLE;        range that isn't. Only the value 1 matters for class_one_char, so don't
4817                  }        increase it if it is already 2 or more ... just in case there's a class
4818                else        with a zillion characters in it. */
4819                  {  
4820                  *class_uchardata++ = XCL_RANGE;        CLASS_SINGLE_CHARACTER:
4821                  class_uchardata += PRIV(ord2utf)(occ, class_uchardata);        if (class_one_char < 2) class_one_char++;
4822                  }  
4823                class_uchardata += PRIV(ord2utf)(ocd, class_uchardata);        /* If class_one_char is 1, we have the first single character in the
4824                }        class, and there have been no prior ranges, or XCLASS items generated by
4825              }        escapes. If this is the final character in the class, we can optimize by
4826  #endif  /* SUPPORT_UCP */        turning the item into a 1-character OP_CHAR[I] if it's positive, or
4827          OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
4828          to be set. Otherwise, there can be no first char if this item is first,
4829          whatever repeat count may follow. In the case of reqchar, save the
4830          previous value for reinstating. */
4831    
4832            /* Now record the original range, possibly modified for UCP caseless        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
4833            overlapping ranges. */          {
4834            ptr++;
4835            zeroreqchar = reqchar;
4836            zeroreqcharflags = reqcharflags;
4837    
4838            *class_uchardata++ = XCL_RANGE;          if (negate_class)
4839  #ifdef SUPPORT_UTF            {
4840  #ifndef COMPILE_PCRE8  #ifdef SUPPORT_UCP
4841            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);  
4842  #endif  #endif
4843  #else /* SUPPORT_UTF */            if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4844            *class_uchardata++ = c;            zerofirstchar = firstchar;
4845            *class_uchardata++ = d;            zerofirstcharflags = firstcharflags;
 #endif /* SUPPORT_UTF */  
4846    
4847            /* With UCP support, we are done. Without UCP support, there is no            /* For caseless UTF-8 mode when UCP support is available, check
4848            caseless matching for UTF characters > 127; we can use the bit map            whether this character has more than one other case. If so, generate
4849            for the smaller ones. As for 16 bit characters without UTF, we            a special OP_NOTPROP item instead of OP_NOTI. */
           can still use  */  
4850    
4851  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4852  #ifndef COMPILE_PCRE8            if (utf && (options & PCRE_CASELESS) != 0 &&
4853            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)  
4854              {              {
4855              if ((options & PCRE_CASELESS) == 0 || c > 127) continue;              *code++ = OP_NOTPROP;
4856              /* Adjust upper limit and fall through to set up the map */              *code++ = PT_CLIST;
4857              d = 127;              *code++ = d;
4858              }              }
4859            else            else
4860              {  #endif
4861              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. */  
4862    
         if (lengthptr == NULL) for (; c <= d; c++)  
           {  
           classbits[c/8] |= (1 << (c&7));  
           if ((options & PCRE_CASELESS) != 0)  
4863              {              {
4864              int uc = cd->fcc[c]; /* flip case */              *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4865              classbits[uc/8] |= (1 << (uc&7));  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4866                if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4867                  code += PRIV(ord2utf)(c, code);
4868                else
4869    #endif
4870                  *code++ = c;
4871              }              }
           }  
   
         continue;   /* Go get the next char in the class */  
         }  
4872    
4873        /* Handle a lone single character - we can get here for a normal            /* We are finished with this character class */
       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. */  
4874    
4875        if (class_single_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)            goto END_CLASS;
         {  
         ptr++;  
         zeroreqchar = reqchar;  
   
         if (negate_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;  
4876            }            }
4877    
4878          /* For a single, positive character, get the value into mcbuffer, and          /* For a single, positive character, get the value into mcbuffer, and
4879          then we can handle this with the normal one-character code. */          then we can handle this with the normal one-character code. */
4880    
4881  #ifdef SUPPORT_UTF  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4882          if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)          if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4883            mclength = PRIV(ord2utf)(c, mcbuffer);            mclength = PRIV(ord2utf)(c, mcbuffer);
4884          else          else