/[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 1056 by chpe, Tue Oct 16 15:53:41 2012 UTC revision 1363 by ph10, Tue Oct 1 16:54:40 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 80  to check them every time. */ Line 80  to check them every time. */
80  /* Definitions to allow mutual recursion */  /* Definitions to allow mutual recursion */
81    
82  static int  static int
83    add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,    add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
84      const pcre_uint32 *, unsigned int);      const pcre_uint32 *, unsigned int);
85    
86  static BOOL  static BOOL
87    compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL,    compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
88      int, int, int *, int *, branch_chain *, compile_data *, int *);      pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,
89        compile_data *, int *);
90    
91    
92    
# Line 114  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 121  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  #define REQ_MASK       (REQ_CASELESS | REQ_VARY)  /* 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 484  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 505  static const char error_texts[] = Line 515  static const char error_texts[] =
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"    "character value in \\u.... sequence is too large\0"
517    "invalid UTF-32 string\0"    "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 644  static const pcre_uint8 ebcdic_chartab[] Line 655  static const pcre_uint8 ebcdic_chartab[]
655  #endif  #endif
656    
657    
658    /* 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    
696    /* This table is used to check whether auto-possessification is possible
697    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    
778  /*************************************************  /*************************************************
# Line 665  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 749  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
883  encodes one of the more complicated things such as \d. A backreference to group  which will be placed in chptr. A backreference to group n is returned as
884  n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When  negative n. When UTF-8 is enabled, a positive value greater than 255 may
885  UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,  be returned in chptr.
886  ptr is pointing at the \. On exit, it is on the final character of the escape  On entry,ptr is pointing at the \. On exit, it is on the final character of the
887  sequence.  escape sequence.
888    
889  Arguments:  Arguments:
890    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
891      chptr          points to the data character
892    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
893    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
894    options        the options bits    options        the options bits
895    isclass        TRUE if inside a character class    isclass        TRUE if inside a character class
896    
897  Returns:         zero or positive => a data character  Returns:         zero => a data character
898                   negative => a special escape sequence                   positive => a special escape sequence
899                     negative => a back reference
900                   on error, errorcodeptr is set                   on error, errorcodeptr is set
901  */  */
902    
903  static int  static int
904  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
905    int options, BOOL isclass)    int bracount, int options, BOOL isclass)
906  {  {
907  /* PCRE_UTF16 has the same value as PCRE_UTF8. */  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
908  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
909  const pcre_uchar *ptr = *ptrptr + 1;  const pcre_uchar *ptr = *ptrptr + 1;
910  pcre_int32 c;  pcre_uint32 c;
911    int escape = 0;
912  int i;  int i;
913    
914  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
# Line 783  ptr--;                            /* Set Line 916  ptr--;                            /* Set
916    
917  /* 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. */
918    
919  if (c == 0) *errorcodeptr = ERR1;  if (c == CHAR_NULL) *errorcodeptr = ERR1;
920    
921  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
922  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 792  Otherwise further processing may be requ Line 925  Otherwise further processing may be requ
925  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
926  /* Not alphanumeric */  /* Not alphanumeric */
927  else if (c < CHAR_0 || c > CHAR_z) {}  else if (c < CHAR_0 || c > CHAR_z) {}
928  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if ((i = escapes[c - CHAR_0]) != 0)
929      { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
930    
931  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
932  /* Not alphanumeric */  /* Not alphanumeric */
933  else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}  else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
934  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; }
935  #endif  #endif
936    
937  /* Escapes that need further processing, or are illegal. */  /* Escapes that need further processing, or are illegal. */
# Line 805  else if ((i = escapes[c - 0x48]) != 0) Line 939  else if ((i = escapes[c - 0x48]) != 0)
939  else  else
940    {    {
941    const pcre_uchar *oldptr;    const pcre_uchar *oldptr;
942    BOOL braced, negated;    BOOL braced, negated, overflow;
943      int s;
944    
945    switch (c)    switch (c)
946      {      {
# Line 830  else Line 965  else
965          c = 0;          c = 0;
966          for (i = 0; i < 4; ++i)          for (i = 0; i < 4; ++i)
967            {            {
968            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
969  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
970            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
971            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 841  else Line 976  else
976            }            }
977    
978  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
979          if (c > (utf ? 0x10ffff : 0xff))          if (c > (utf ? 0x10ffffU : 0xffU))
980  #elif defined COMPILE_PCRE16  #elif defined COMPILE_PCRE16
981          if (c > (utf ? 0x10ffff : 0xffff))          if (c > (utf ? 0x10ffffU : 0xffffU))
982  #elif defined COMPILE_PCRE32  #elif defined COMPILE_PCRE32
983          if (utf && c > 0x10ffff)          if (utf && c > 0x10ffffU)
984  #endif  #endif
985            {            {
986            *errorcodeptr = ERR76;            *errorcodeptr = ERR76;
# Line 877  else Line 1012  else
1012      (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
1013      number either in angle brackets or in single quotes. However, these are      number either in angle brackets or in single quotes. However, these are
1014      (possibly recursive) subroutine calls, _not_ backreferences. Just return      (possibly recursive) subroutine calls, _not_ backreferences. Just return
1015      the -ESC_g code (cf \k). */      the ESC_g code (cf \k). */
1016    
1017      case CHAR_g:      case CHAR_g:
1018      if (isclass) break;      if (isclass) break;
1019      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)      if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
1020        {        {
1021        c = -ESC_g;        escape = ESC_g;
1022        break;        break;
1023        }        }
1024    
# Line 892  else Line 1027  else
1027      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1028        {        {
1029        const pcre_uchar *p;        const pcre_uchar *p;
1030        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++)
1031          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1032        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1033          {          {
1034          c = -ESC_k;          escape = ESC_k;
1035          break;          break;
1036          }          }
1037        braced = TRUE;        braced = TRUE;
# Line 912  else Line 1047  else
1047      else negated = FALSE;      else negated = FALSE;
1048    
1049      /* The integer range is limited by the machine's int representation. */      /* The integer range is limited by the machine's int representation. */
1050      c = 0;      s = 0;
1051        overflow = FALSE;
1052      while (IS_DIGIT(ptr[1]))      while (IS_DIGIT(ptr[1]))
1053        {        {
1054        if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */        if (s > INT_MAX / 10 - 1) /* Integer overflow */
1055          {          {
1056          c = -1;          overflow = TRUE;
1057          break;          break;
1058          }          }
1059        c = c * 10 + *(++ptr) - CHAR_0;        s = s * 10 + (int)(*(++ptr) - CHAR_0);
1060        }        }
1061      if (((unsigned int)c) > INT_MAX) /* Integer overflow */      if (overflow) /* Integer overflow */
1062        {        {
1063        while (IS_DIGIT(ptr[1]))        while (IS_DIGIT(ptr[1]))
1064          ptr++;          ptr++;
# Line 936  else Line 1072  else
1072        break;        break;
1073        }        }
1074    
1075      if (c == 0)      if (s == 0)
1076        {        {
1077        *errorcodeptr = ERR58;        *errorcodeptr = ERR58;
1078        break;        break;
# Line 944  else Line 1080  else
1080    
1081      if (negated)      if (negated)
1082        {        {
1083        if (c > bracount)        if (s > bracount)
1084          {          {
1085          *errorcodeptr = ERR15;          *errorcodeptr = ERR15;
1086          break;          break;
1087          }          }
1088        c = bracount - (c - 1);        s = bracount - (s - 1);
1089        }        }
1090    
1091      c = -(ESC_REF + c);      escape = -s;
1092      break;      break;
1093    
1094      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
# Line 974  else Line 1110  else
1110        {        {
1111        oldptr = ptr;        oldptr = ptr;
1112        /* The integer range is limited by the machine's int representation. */        /* The integer range is limited by the machine's int representation. */
1113        c -= CHAR_0;        s = (int)(c -CHAR_0);
1114          overflow = FALSE;
1115        while (IS_DIGIT(ptr[1]))        while (IS_DIGIT(ptr[1]))
1116          {          {
1117          if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */          if (s > INT_MAX / 10 - 1) /* Integer overflow */
1118            {            {
1119            c = -1;            overflow = TRUE;
1120            break;            break;
1121            }            }
1122          c = c * 10 + *(++ptr) - CHAR_0;          s = s * 10 + (int)(*(++ptr) - CHAR_0);
1123          }          }
1124        if (((unsigned int)c) > INT_MAX) /* Integer overflow */        if (overflow) /* Integer overflow */
1125          {          {
1126          while (IS_DIGIT(ptr[1]))          while (IS_DIGIT(ptr[1]))
1127            ptr++;            ptr++;
1128          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1129          break;          break;
1130          }          }
1131        if (c < 10 || c <= bracount)        if (s < 10 || s <= bracount)
1132          {          {
1133          c = -(ESC_REF + c);          escape = -s;
1134          break;          break;
1135          }          }
1136        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
# Line 1040  else Line 1177  else
1177          c = 0;          c = 0;
1178          for (i = 0; i < 2; ++i)          for (i = 0; i < 2; ++i)
1179            {            {
1180            register int cc = *(++ptr);            register pcre_uint32 cc = *(++ptr);
1181  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1182            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1183            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 1058  else Line 1195  else
1195        const pcre_uchar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
1196    
1197        c = 0;        c = 0;
1198          overflow = FALSE;
1199        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
1200          {          {
1201          register int cc = *pt++;          register pcre_uint32 cc = *pt++;
1202          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1203    
1204    #ifdef COMPILE_PCRE32
1205            if (c >= 0x10000000l) { overflow = TRUE; break; }
1206    #endif
1207    
1208  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1209          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1210          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 1072  else Line 1214  else
1214  #endif  #endif
1215    
1216  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
1217          if (c > (utf ? 0x10ffff : 0xff)) { c = -1; break; }          if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1218  #elif defined COMPILE_PCRE16  #elif defined COMPILE_PCRE16
1219          if (c > (utf ? 0x10ffff : 0xffff)) { c = -1; break; }          if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1220  #elif defined COMPILE_PCRE32  #elif defined COMPILE_PCRE32
1221          if (utf && c > 0x10ffff) { c = -1; break; }          if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1222  #endif  #endif
1223          }          }
1224    
1225        if (c < 0)        if (overflow)
1226          {          {
1227          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1228          *errorcodeptr = ERR34;          *errorcodeptr = ERR34;
# Line 1102  else Line 1244  else
1244      c = 0;      c = 0;
1245      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)
1246        {        {
1247        int cc;                                  /* Some compilers don't like */        pcre_uint32 cc;                          /* Some compilers don't like */
1248        cc = *(++ptr);                           /* ++ in initializers */        cc = *(++ptr);                           /* ++ in initializers */
1249  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1250        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
# Line 1121  else Line 1263  else
1263    
1264      case CHAR_c:      case CHAR_c:
1265      c = *(++ptr);      c = *(++ptr);
1266      if (c == 0)      if (c == CHAR_NULL)
1267        {        {
1268        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1269        break;        break;
# Line 1161  else Line 1303  else
1303  newline". PCRE does not support \N{name}. However, it does support  newline". PCRE does not support \N{name}. However, it does support
1304  quantification such as \N{2,3}. */  quantification such as \N{2,3}. */
1305    
1306  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&  if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1307       !is_counted_repeat(ptr+2))       !is_counted_repeat(ptr+2))
1308    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
1309    
1310  /* If PCRE_UCP is set, we change the values for \d etc. */  /* If PCRE_UCP is set, we change the values for \d etc. */
1311    
1312  if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)  if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
1313    c -= (ESC_DU - ESC_D);    escape += (ESC_DU - ESC_D);
1314    
1315  /* Set the pointer to the final character before returning. */  /* Set the pointer to the final character before returning. */
1316    
1317  *ptrptr = ptr;  *ptrptr = ptr;
1318  return c;  *chptr = c;
1319    return escape;
1320  }  }
1321    
1322    
# Line 1191  escape sequence. Line 1334  escape sequence.
1334  Argument:  Argument:
1335    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
1336    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
1337    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
1338      pdataptr       points to an unsigned int that is set to the detailed property value
1339    errorcodeptr   points to the error code variable    errorcodeptr   points to the error code variable
1340    
1341  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
1342  */  */
1343    
1344  static int  static BOOL
1345  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1346      unsigned int *pdataptr, int *errorcodeptr)
1347  {  {
1348  int c, i, bot, top;  pcre_uchar c;
1349    int i, bot, top;
1350  const pcre_uchar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1351  pcre_uchar name[32];  pcre_uchar name[32];
1352    
1353  c = *(++ptr);  c = *(++ptr);
1354  if (c == 0) goto ERROR_RETURN;  if (c == CHAR_NULL) goto ERROR_RETURN;
1355    
1356  *negptr = FALSE;  *negptr = FALSE;
1357    
# Line 1222  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1368  if (c == CHAR_LEFT_CURLY_BRACKET)
1368    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1369      {      {
1370      c = *(++ptr);      c = *(++ptr);
1371      if (c == 0) goto ERROR_RETURN;      if (c == CHAR_NULL) goto ERROR_RETURN;
1372      if (c == CHAR_RIGHT_CURLY_BRACKET) break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1373      name[i] = c;      name[i] = c;
1374      }      }
# Line 1247  top = PRIV(utt_size); Line 1393  top = PRIV(utt_size);
1393    
1394  while (bot < top)  while (bot < top)
1395    {    {
1396      int r;
1397    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1398    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);
1399    if (c == 0)    if (r == 0)
1400      {      {
1401      *dptr = PRIV(utt)[i].value;      *ptypeptr = PRIV(utt)[i].type;
1402      return PRIV(utt)[i].type;      *pdataptr = PRIV(utt)[i].value;
1403        return TRUE;
1404      }      }
1405    if (c > 0) bot = i + 1; else top = i;    if (r > 0) bot = i + 1; else top = i;
1406    }    }
1407    
1408  *errorcodeptr = ERR47;  *errorcodeptr = ERR47;
1409  *ptrptr = ptr;  *ptrptr = ptr;
1410  return -1;  return FALSE;
1411    
1412  ERROR_RETURN:  ERROR_RETURN:
1413  *errorcodeptr = ERR46;  *errorcodeptr = ERR46;
1414  *ptrptr = ptr;  *ptrptr = ptr;
1415  return -1;  return FALSE;
1416  }  }
1417  #endif  #endif
1418    
1419    
1420    
   
1421  /*************************************************  /*************************************************
1422  *         Read repeat counts                     *  *         Read repeat counts                     *
1423  *************************************************/  *************************************************/
# Line 1299  int max = -1; Line 1446  int max = -1;
1446  /* 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
1447  an integer overflow. */  an integer overflow. */
1448    
1449  while (IS_DIGIT(*p)) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);
1450  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1451    {    {
1452    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 1314  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1461  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1461    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1462      {      {
1463      max = 0;      max = 0;
1464      while(IS_DIGIT(*p)) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);
1465      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1466        {        {
1467        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1339  return p; Line 1486  return p;
1486    
1487    
1488  /*************************************************  /*************************************************
 *  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 / UTF-32 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 / UTF-32 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;  
 }  
   
   
   
   
 /*************************************************  
1489  *      Find first significant op code            *  *      Find first significant op code            *
1490  *************************************************/  *************************************************/
1491    
# Line 1688  for (;;) Line 1540  for (;;)
1540    
1541    
1542    
   
1543  /*************************************************  /*************************************************
1544  *        Find the fixed length of a branch       *  *        Find the fixed length of a branch       *
1545  *************************************************/  *************************************************/
# Line 1732  for (;;) Line 1583  for (;;)
1583    {    {
1584    int d;    int d;
1585    pcre_uchar *ce, *cs;    pcre_uchar *ce, *cs;
1586    register int op = *cc;    register pcre_uchar op = *cc;
1587    
1588    switch (op)    switch (op)
1589      {      {
# Line 1840  for (;;) Line 1691  for (;;)
1691      case OP_NOTI:      case OP_NOTI:
1692      branchlength++;      branchlength++;
1693      cc += 2;      cc += 2;
1694  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #ifdef SUPPORT_UTF
1695      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1696  #endif  #endif
1697      break;      break;
# Line 1852  for (;;) Line 1703  for (;;)
1703      case OP_EXACTI:      case OP_EXACTI:
1704      case OP_NOTEXACT:      case OP_NOTEXACT:
1705      case OP_NOTEXACTI:      case OP_NOTEXACTI:
1706      branchlength += GET2(cc,1);      branchlength += (int)GET2(cc,1);
1707      cc += 2 + IMM2_SIZE;      cc += 2 + IMM2_SIZE;
1708  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #ifdef SUPPORT_UTF
1709      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1710  #endif  #endif
1711      break;      break;
1712    
1713      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1714      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1715      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1716        cc += 2;        cc += 2;
1717      cc += 1 + IMM2_SIZE + 1;      cc += 1 + IMM2_SIZE + 1;
1718      break;      break;
# Line 1897  for (;;) Line 1748  for (;;)
1748    
1749      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1750    
 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32  
     case OP_XCLASS:  
     cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];  
     /* Fall through */  
 #endif  
   
1751      case OP_CLASS:      case OP_CLASS:
1752      case OP_NCLASS:      case OP_NCLASS:
1753    #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1754        case OP_XCLASS:
1755        /* The original code caused an unsigned overflow in 64 bit systems,
1756        so now we use a conditional statement. */
1757        if (op == OP_XCLASS)
1758          cc += GET(cc, 1);
1759        else
1760          cc += PRIV(OP_lengths)[OP_CLASS];
1761    #else
1762      cc += PRIV(OP_lengths)[OP_CLASS];      cc += PRIV(OP_lengths)[OP_CLASS];
1763    #endif
1764    
1765      switch (*cc)      switch (*cc)
1766        {        {
# Line 1920  for (;;) Line 1775  for (;;)
1775        case OP_CRRANGE:        case OP_CRRANGE:
1776        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1777        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1778        branchlength += GET2(cc,1);        branchlength += (int)GET2(cc,1);
1779        cc += 1 + 2 * IMM2_SIZE;        cc += 1 + 2 * IMM2_SIZE;
1780        break;        break;
1781    
# Line 1987  for (;;) Line 1842  for (;;)
1842      case OP_QUERYI:      case OP_QUERYI:
1843      case OP_REF:      case OP_REF:
1844      case OP_REFI:      case OP_REFI:
1845        case OP_DNREF:
1846        case OP_DNREFI:
1847      case OP_SBRA:      case OP_SBRA:
1848      case OP_SBRAPOS:      case OP_SBRAPOS:
1849      case OP_SCBRA:      case OP_SCBRA:
# Line 2023  for (;;) Line 1880  for (;;)
1880    
1881    
1882    
   
1883  /*************************************************  /*************************************************
1884  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
1885  *************************************************/  *************************************************/
# Line 2047  PRIV(find_bracket)(const pcre_uchar *cod Line 1903  PRIV(find_bracket)(const pcre_uchar *cod
1903  {  {
1904  for (;;)  for (;;)
1905    {    {
1906    register int c = *code;    register pcre_uchar c = *code;
1907    
1908    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1909    
# Line 2070  for (;;) Line 1926  for (;;)
1926    else if (c == OP_CBRA || c == OP_SCBRA ||    else if (c == OP_CBRA || c == OP_SCBRA ||
1927             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
1928      {      {
1929      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1930      if (n == number) return (pcre_uchar *)code;      if (n == number) return (pcre_uchar *)code;
1931      code += PRIV(OP_lengths)[c];      code += PRIV(OP_lengths)[c];
1932      }      }
# Line 2100  for (;;) Line 1956  for (;;)
1956        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1957        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1958        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1959        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1960          code += 2;          code += 2;
1961        break;        break;
1962    
1963        case OP_MARK:        case OP_MARK:
1964        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1965        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
1966        case OP_THEN_ARG:        case OP_THEN_ARG:
1967        code += code[1];        code += code[1];
1968        break;        break;
# Line 2185  find_recurse(const pcre_uchar *code, BOO Line 2038  find_recurse(const pcre_uchar *code, BOO
2038  {  {
2039  for (;;)  for (;;)
2040    {    {
2041    register int c = *code;    register pcre_uchar c = *code;
2042    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2043    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
2044    
# Line 2220  for (;;) Line 2073  for (;;)
2073        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2074        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2075        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2076        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2077          code += 2;          code += 2;
2078        break;        break;
2079    
2080        case OP_MARK:        case OP_MARK:
2081        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2082        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
2083        case OP_THEN_ARG:        case OP_THEN_ARG:
2084        code += code[1];        code += code[1];
2085        break;        break;
# Line 2331  Arguments: Line 2181  Arguments:
2181    endcode     points to where to stop    endcode     points to where to stop
2182    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2183    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2184      recurses    chain of recurse_check to catch mutual recursion
2185    
2186  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2187  */  */
2188    
2189    typedef struct recurse_check {
2190      struct recurse_check *prev;
2191      const pcre_uchar *group;
2192    } recurse_check;
2193    
2194  static BOOL  static BOOL
2195  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2196    BOOL utf, compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2197  {  {
2198  register int c;  register pcre_uchar c;
2199    recurse_check this_recurse;
2200    
2201  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2202       code < endcode;       code < endcode;
2203       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
# Line 2367  for (code = first_significant_code(code Line 2225  for (code = first_significant_code(code
2225    
2226    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2227      {      {
2228      const pcre_uchar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2229      BOOL empty_branch;      BOOL empty_branch;
2230    
2231      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2232        when called to scan a completed pattern by setting cd->start_workspace to
2233        NULL. */
2234    
2235        if (cd->start_workspace != NULL)
2236          {
2237          const pcre_uchar *tcode;
2238          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2239            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2240          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2241          }
2242    
2243        /* If we are scanning a completed pattern, there are no forward references
2244        and all groups are complete. We need to detect whether this is a recursive
2245        call, as otherwise there will be an infinite loop. If it is a recursion,
2246        just skip over it. Simple recursions are easily detected. For mutual
2247        recursions we keep a chain on the stack. */
2248    
2249      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)      else
2250        if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;        {
2251          recurse_check *r = recurses;
2252          const pcre_uchar *endgroup = scode;
2253    
2254      /* Not a forward reference, test for completed backward reference */        do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2255          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2256    
2257      empty_branch = FALSE;        for (r = recurses; r != NULL; r = r->prev)
2258      scode = cd->start_code + GET(code, 1);          if (r->group == scode) break;
2259      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */        if (r != NULL) continue;   /* Mutual recursion */
2260          }
2261    
2262      /* Completed backwards reference */      /* Completed reference; scan the referenced group, remembering it on the
2263        stack chain to detect mutual recursions. */
2264    
2265        empty_branch = FALSE;
2266        this_recurse.prev = recurses;
2267        this_recurse.group = scode;
2268    
2269      do      do
2270        {        {
2271        if (could_be_empty_branch(scode, endcode, utf, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2272          {          {
2273          empty_branch = TRUE;          empty_branch = TRUE;
2274          break;          break;
# Line 2441  for (code = first_significant_code(code Line 2324  for (code = first_significant_code(code
2324        empty_branch = FALSE;        empty_branch = FALSE;
2325        do        do
2326          {          {
2327          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2328            empty_branch = TRUE;            empty_branch = TRUE;
2329          code += GET(code, 1);          code += GET(code, 1);
2330          }          }
# Line 2499  for (code = first_significant_code(code Line 2382  for (code = first_significant_code(code
2382    
2383      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2384    
2385        case OP_ANY:
2386        case OP_ALLANY:
2387        case OP_ANYBYTE:
2388    
2389      case OP_PROP:      case OP_PROP:
2390      case OP_NOTPROP:      case OP_NOTPROP:
2391        case OP_ANYNL:
2392    
2393        case OP_NOT_HSPACE:
2394        case OP_HSPACE:
2395        case OP_NOT_VSPACE:
2396        case OP_VSPACE:
2397      case OP_EXTUNI:      case OP_EXTUNI:
2398    
2399      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2400      case OP_DIGIT:      case OP_DIGIT:
2401      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2402      case OP_WHITESPACE:      case OP_WHITESPACE:
2403      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2404      case OP_WORDCHAR:      case OP_WORDCHAR:
2405      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2406      case OP_CHAR:      case OP_CHAR:
2407      case OP_CHARI:      case OP_CHARI:
2408      case OP_NOT:      case OP_NOT:
2409      case OP_NOTI:      case OP_NOTI:
2410    
2411      case OP_PLUS:      case OP_PLUS:
2412        case OP_PLUSI:
2413      case OP_MINPLUS:      case OP_MINPLUS:
2414      case OP_POSPLUS:      case OP_MINPLUSI:
2415      case OP_EXACT:  
2416      case OP_NOTPLUS:      case OP_NOTPLUS:
2417        case OP_NOTPLUSI:
2418      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2419        case OP_NOTMINPLUSI:
2420    
2421        case OP_POSPLUS:
2422        case OP_POSPLUSI:
2423      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2424        case OP_NOTPOSPLUSI:
2425    
2426        case OP_EXACT:
2427        case OP_EXACTI:
2428      case OP_NOTEXACT:      case OP_NOTEXACT:
2429        case OP_NOTEXACTI:
2430    
2431      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2432      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2433      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2434      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2435    
2436      return FALSE;      return FALSE;
2437    
2438      /* 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 2546  for (code = first_significant_code(code Line 2452  for (code = first_significant_code(code
2452      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2453      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2454      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2455      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2456        code += 2;        code += 2;
2457      break;      break;
2458    
# Line 2560  for (code = first_significant_code(code Line 2466  for (code = first_significant_code(code
2466      return TRUE;      return TRUE;
2467    
2468      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2469      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2470        followed by a multibyte character. */
2471    
2472  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2473      case OP_STAR:      case OP_STAR:
2474      case OP_STARI:      case OP_STARI:
2475        case OP_NOTSTAR:
2476        case OP_NOTSTARI:
2477    
2478      case OP_MINSTAR:      case OP_MINSTAR:
2479      case OP_MINSTARI:      case OP_MINSTARI:
2480        case OP_NOTMINSTAR:
2481        case OP_NOTMINSTARI:
2482    
2483      case OP_POSSTAR:      case OP_POSSTAR:
2484      case OP_POSSTARI:      case OP_POSSTARI:
2485        case OP_NOTPOSSTAR:
2486        case OP_NOTPOSSTARI:
2487    
2488      case OP_QUERY:      case OP_QUERY:
2489      case OP_QUERYI:      case OP_QUERYI:
2490        case OP_NOTQUERY:
2491        case OP_NOTQUERYI:
2492    
2493      case OP_MINQUERY:      case OP_MINQUERY:
2494      case OP_MINQUERYI:      case OP_MINQUERYI:
2495        case OP_NOTMINQUERY:
2496        case OP_NOTMINQUERYI:
2497    
2498      case OP_POSQUERY:      case OP_POSQUERY:
2499      case OP_POSQUERYI:      case OP_POSQUERYI:
2500        case OP_NOTPOSQUERY:
2501        case OP_NOTPOSQUERYI:
2502    
2503      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2504      break;      break;
2505    
2506      case OP_UPTO:      case OP_UPTO:
2507      case OP_UPTOI:      case OP_UPTOI:
2508        case OP_NOTUPTO:
2509        case OP_NOTUPTOI:
2510    
2511      case OP_MINUPTO:      case OP_MINUPTO:
2512      case OP_MINUPTOI:      case OP_MINUPTOI:
2513        case OP_NOTMINUPTO:
2514        case OP_NOTMINUPTOI:
2515    
2516      case OP_POSUPTO:      case OP_POSUPTO:
2517      case OP_POSUPTOI:      case OP_POSUPTOI:
2518        case OP_NOTPOSUPTO:
2519        case OP_NOTPOSUPTOI:
2520    
2521      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]);
2522      break;      break;
2523  #endif  #endif
# Line 2594  for (code = first_significant_code(code Line 2528  for (code = first_significant_code(code
2528      case OP_MARK:      case OP_MARK:
2529      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2530      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     code += code[1];  
     break;  
   
2531      case OP_THEN_ARG:      case OP_THEN_ARG:
2532      code += code[1];      code += code[1];
2533      break;      break;
# Line 2640  could_be_empty(const pcre_uchar *code, c Line 2571  could_be_empty(const pcre_uchar *code, c
2571  {  {
2572  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2573    {    {
2574    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2575      return FALSE;      return FALSE;
2576    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2577    }    }
# Line 2650  return TRUE; Line 2581  return TRUE;
2581    
2582    
2583  /*************************************************  /*************************************************
2584  *           Check for POSIX class syntax         *  *        Base opcode of repeated opcodes         *
2585  *************************************************/  *************************************************/
2586    
2587  /* This function is called when the sequence "[:" or "[." or "[=" is  /* Returns the base opcode for repeated single character type opcodes. If the
2588  encountered in a character class. It checks whether this is followed by a  opcode is not a repeated character type, it returns with the original value.
 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we  
 reach an unescaped ']' without the special preceding character, return FALSE.  
2589    
2590  Originally, this function only recognized a sequence of letters between the  Arguments:  c opcode
2591  terminators, but it seems that Perl recognizes any sequence of characters,  Returns:    base opcode for the type
2592  though of course unknown POSIX names are subsequently rejected. Perl gives an  */
 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE  
 didn't consider this to be a POSIX class. Likewise for [:1234:].  
2593    
2594  The problem in trying to be exactly like Perl is in the handling of escapes. We  static pcre_uchar
2595  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX  get_repeat_base(pcre_uchar c)
2596  class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code  {
2597  below handles the special case of \], but does not try to do any other escape  return (c > OP_TYPEPOSUPTO)? c :
2598  processing. This makes it different from Perl for cases such as [:l\ower:]         (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2599  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize         (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2600  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,         (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2601  I think.         (c >= OP_STARI)?      OP_STARI :
2602                                 OP_STAR;
2603    }
2604    
 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.  
 It seems that the appearance of a nested POSIX class supersedes an apparent  
 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or  
 a digit.  
2605    
2606  In Perl, unescaped square brackets may also appear as part of class names. For  
2607  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for  #ifdef SUPPORT_UCP
2608  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not  /*************************************************
2609  seem right at all. PCRE does not allow closing square brackets in POSIX class  *        Check a character and a property        *
2610  names.  *************************************************/
2611    
2612    /* This function is called by check_auto_possessive() when a property item
2613    is adjacent to a fixed character.
2614    
2615  Arguments:  Arguments:
2616    ptr      pointer to the initial [    c            the character
2617    endptr   where to return the end pointer    ptype        the property type
2618      pdata        the data for the type
2619      negated      TRUE if it's a negated property (\P or \p{^)
2620    
2621  Returns:   TRUE or FALSE  Returns:       TRUE if auto-possessifying is OK
2622  */  */
2623    
2624  static BOOL  static BOOL
2625  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2626      BOOL negated)
2627  {  {
2628  int terminator;          /* Don't combine these lines; the Solaris cc */  const pcre_uint32 *p;
2629  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  const ucd_record *prop = GET_UCD(c);
2630  for (++ptr; *ptr != 0; ptr++)  
2631    switch(ptype)
2632    {    {
2633    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)    case PT_LAMP:
2634      ptr++;    return (prop->chartype == ucp_Lu ||
2635    else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;            prop->chartype == ucp_Ll ||
2636    else            prop->chartype == ucp_Lt) == negated;
2637    
2638      case PT_GC:
2639      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2640    
2641      case PT_PC:
2642      return (pdata == prop->chartype) == negated;
2643    
2644      case PT_SC:
2645      return (pdata == prop->script) == negated;
2646    
2647      /* These are specials */
2648    
2649      case PT_ALNUM:
2650      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2651              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2652    
2653      case PT_SPACE:    /* Perl space */
2654      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2655              c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2656              == negated;
2657    
2658      case PT_PXSPACE:  /* POSIX space */
2659      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2660              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2661              c == CHAR_FF || c == CHAR_CR)
2662              == negated;
2663    
2664      case PT_WORD:
2665      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2666              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2667              c == CHAR_UNDERSCORE) == negated;
2668    
2669      case PT_CLIST:
2670      p = PRIV(ucd_caseless_sets) + prop->caseset;
2671      for (;;)
2672      {      {
2673      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      if (c < *p) return !negated;
2674        {      if (c == *p++) return negated;
       *endptr = ptr;  
       return TRUE;  
       }  
     if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&  
          (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||  
           ptr[1] == CHAR_EQUALS_SIGN) &&  
         check_posix_syntax(ptr, endptr))  
       return FALSE;  
2675      }      }
2676      break;  /* Control never reaches here */
2677    }    }
2678    
2679  return FALSE;  return FALSE;
2680  }  }
2681    #endif  /* SUPPORT_UCP */
2682    
2683    
2684    
2685  /*************************************************  /*************************************************
2686  *          Check POSIX class name                *  *        Fill the character property list        *
2687  *************************************************/  *************************************************/
2688    
2689  /* This function is called to check the name given in a POSIX-style class entry  /* Checks whether the code points to an opcode that can take part in auto-
2690  such as [:alnum:].  possessification, and if so, fills a list with its properties.
2691    
2692  Arguments:  Arguments:
2693    ptr        points to the first letter    code        points to start of expression
2694    len        the length of the name    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2695      fcc         points to case-flipping table
2696      list        points to output list
2697                  list[0] will be filled with the opcode
2698                  list[1] will be non-zero if this opcode
2699                    can match an empty character string
2700                  list[2..7] depends on the opcode
2701    
2702  Returns:     a value representing the name, or -1 if unknown  Returns:      points to the start of the next opcode if *code is accepted
2703                  NULL if *code is not accepted
2704  */  */
2705    
2706  static int  static const pcre_uchar *
2707  check_posix_name(const pcre_uchar *ptr, int len)  get_chr_property_list(const pcre_uchar *code, BOOL utf,
2708      const pcre_uint8 *fcc, pcre_uint32 *list)
2709  {  {
2710  const char *pn = posix_names;  pcre_uchar c = *code;
2711  register int yield = 0;  const pcre_uchar *end;
2712  while (posix_name_lengths[yield] != 0)  const pcre_uint32 *clist_src;
2713    pcre_uint32 *clist_dest;
2714    pcre_uint32 chr;
2715    pcre_uchar base;
2716    
2717    list[0] = c;
2718    list[1] = FALSE;
2719    code++;
2720    
2721    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2722    {    {
2723    if (len == posix_name_lengths[yield] &&    base = get_repeat_base(c);
2724      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;    c -= (base - OP_STAR);
   pn += posix_name_lengths[yield] + 1;  
   yield++;  
   }  
 return -1;  
 }  
2725    
2726      if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2727        code += IMM2_SIZE;
2728    
2729  /*************************************************    list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
 *    Adjust OP_RECURSE items in repeated group   *  
 *************************************************/  
2730    
2731  /* OP_RECURSE items contain an offset from the start of the regex to the group    switch(base)
2732  that is referenced. This means that groups can be replicated for fixed      {
2733  repetition simply by copying (because the recursion is allowed to refer to      case OP_STAR:
2734  earlier groups that are outside the current group). However, when a group is      list[0] = OP_CHAR;
2735  optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is      break;
 inserted before it, after it has been compiled. This means that any OP_RECURSE  
 items within it that refer to the group itself or any contained groups have to  
 have their offsets adjusted. That one of the jobs of this function. Before it  
 is called, the partially compiled regex must be temporarily terminated with  
 OP_END.  
2736    
2737  This function has been extended with the possibility of forward references for      case OP_STARI:
2738  recursions and subroutine calls. It must also check the list of such references      list[0] = OP_CHARI;
2739  for the group we are dealing with. If it finds that one of the recursions in      break;
 the current group is on this list, it adjusts the offset in the list, not the  
 value in the reference (which is a group number).  
2740    
2741  Arguments:      case OP_NOTSTAR:
2742    group      points to the start of the group      list[0] = OP_NOT;
2743    adjust     the amount by which the group is to be moved      break;
   utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode  
   cd         contains pointers to tables etc.  
   save_hwm   the hwm forward reference pointer at the start of the group  
2744    
2745  Returns:     nothing      case OP_NOTSTARI:
2746  */      list[0] = OP_NOTI;
2747        break;
2748    
2749  static void      case OP_TYPESTAR:
2750  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,      list[0] = *code;
2751    pcre_uchar *save_hwm)      code++;
2752  {      break;
2753  pcre_uchar *ptr = group;      }
2754      c = list[0];
2755      }
2756    
2757  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)  switch(c)
2758    {    {
2759    int offset;    case OP_NOT_DIGIT:
2760    pcre_uchar *hc;    case OP_DIGIT:
2761      case OP_NOT_WHITESPACE:
2762      case OP_WHITESPACE:
2763      case OP_NOT_WORDCHAR:
2764      case OP_WORDCHAR:
2765      case OP_ANY:
2766      case OP_ALLANY:
2767      case OP_ANYNL:
2768      case OP_NOT_HSPACE:
2769      case OP_HSPACE:
2770      case OP_NOT_VSPACE:
2771      case OP_VSPACE:
2772      case OP_EXTUNI:
2773      case OP_EODN:
2774      case OP_EOD:
2775      case OP_DOLL:
2776      case OP_DOLLM:
2777      return code;
2778    
2779    /* See if this recursion is on the forward reference list. If so, adjust the    case OP_CHAR:
2780    reference. */    case OP_NOT:
2781      GETCHARINCTEST(chr, code);
2782      list[2] = chr;
2783      list[3] = NOTACHAR;
2784      return code;
2785    
2786    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    case OP_CHARI:
2787      {    case OP_NOTI:
2788      offset = GET(hc, 0);    list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2789      if (cd->start_code + offset == ptr + 1)    GETCHARINCTEST(chr, code);
2790        {    list[2] = chr;
       PUT(hc, 0, offset + adjust);  
       break;  
       }  
     }  
2791    
2792    /* Otherwise, adjust the recursion offset if it's after the start of this  #ifdef SUPPORT_UCP
2793    group. */    if (chr < 128 || (chr < 256 && !utf))
2794        list[3] = fcc[chr];
2795      else
2796        list[3] = UCD_OTHERCASE(chr);
2797    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2798      list[3] = (chr < 256) ? fcc[chr] : chr;
2799    #else
2800      list[3] = fcc[chr];
2801    #endif
2802    
2803    if (hc >= cd->hwm)    /* The othercase might be the same value. */
2804    
2805      if (chr == list[3])
2806        list[3] = NOTACHAR;
2807      else
2808        list[4] = NOTACHAR;
2809      return code;
2810    
2811    #ifdef SUPPORT_UCP
2812      case OP_PROP:
2813      case OP_NOTPROP:
2814      if (code[0] != PT_CLIST)
2815      {      {
2816      offset = GET(ptr, 1);      list[2] = code[0];
2817      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      list[3] = code[1];
2818        return code + 2;
2819      }      }
2820    
2821    ptr += 1 + LINK_SIZE;    /* Convert only if we have anough space. */
   }  
 }  
2822    
2823      clist_src = PRIV(ucd_caseless_sets) + code[1];
2824      clist_dest = list + 2;
2825      code += 2;
2826    
2827      do {
2828         /* Early return if there is not enough space. */
2829         if (clist_dest >= list + 8)
2830           {
2831           list[2] = code[0];
2832           list[3] = code[1];
2833           return code;
2834           }
2835         *clist_dest++ = *clist_src;
2836         }
2837       while(*clist_src++ != NOTACHAR);
2838    
2839  /*************************************************    /* Enough space to store all characters. */
 *        Insert an automatic callout point       *  
 *************************************************/  
2840    
2841  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert    list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2842  callout points before each pattern item.    return code;
2843    #endif
2844    
2845  Arguments:    case OP_NCLASS:
2846    code           current code pointer    case OP_CLASS:
2847    ptr            current pattern pointer  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2848    cd             pointers to tables etc    case OP_XCLASS:
2849    
2850  Returns:         new code pointer    if (c == OP_XCLASS)
2851  */      end = code + GET(code, 0);
2852      else
2853    #endif
2854        end = code + 32 / sizeof(pcre_uchar);
2855    
2856  static pcre_uchar *    switch(*end)
2857  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)      {
2858  {      case OP_CRSTAR:
2859  *code++ = OP_CALLOUT;      case OP_CRMINSTAR:
2860  *code++ = 255;      case OP_CRQUERY:
2861  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */      case OP_CRMINQUERY:
2862  PUT(code, LINK_SIZE, 0);                       /* Default length */      list[1] = TRUE;
2863  return code + 2 * LINK_SIZE;      end++;
2864        break;
2865    
2866        case OP_CRRANGE:
2867        case OP_CRMINRANGE:
2868        list[1] = (GET2(end, 1) == 0);
2869        end += 1 + 2 * IMM2_SIZE;
2870        break;
2871        }
2872      list[2] = end - code;
2873      return end;
2874      }
2875    return NULL;    /* Opcode not accepted */
2876  }  }
2877    
2878    
2879    
2880  /*************************************************  /*************************************************
2881  *         Complete a callout item                *  *    Scan further character sets for match       *
2882  *************************************************/  *************************************************/
2883    
2884  /* A callout item contains the length of the next item in the pattern, which  /* Checks whether the base and the current opcode have a common character, in
2885  we can't fill in till after we have reached the relevant point. This is used  which case the base cannot be possessified.
 for both automatic and manual callouts.  
2886    
2887  Arguments:  Arguments:
2888    previous_callout   points to previous callout item    code        points to the byte code
2889    ptr                current pattern pointer    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2890    cd                 pointers to tables etc    cd          static compile data
2891      base_list   the data list of the base opcode
2892    
2893  Returns:             nothing  Returns:      TRUE if the auto-possessification is possible
2894  */  */
2895    
2896  static void  static BOOL
2897  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)  compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2898      const pcre_uint32* base_list)
2899  {  {
2900  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  pcre_uchar c;
2901  PUT(previous_callout, 2 + LINK_SIZE, length);  pcre_uint32 list[8];
2902  }  const pcre_uint32* chr_ptr;
2903    const pcre_uint32* ochr_ptr;
2904    const pcre_uint32* list_ptr;
2905    pcre_uint32 chr;
2906    
2907    for(;;)
2908      {
2909      c = *code;
2910    
2911      /* Skip over callouts */
2912    
2913  #ifdef SUPPORT_UCP    if (c == OP_CALLOUT)
2914  /*************************************************      {
2915  *           Get othercase range                  *      code += PRIV(OP_lengths)[c];
2916  *************************************************/      continue;
2917        }
2918    
2919  /* This function is passed the start and end of a class range, in UTF-8 mode    if (c == OP_ALT)
2920  with UCP support. It searches up the characters, looking for ranges of      {
2921  characters in the "other" case. Each call returns the next one, updating the      do code += GET(code, 1); while (*code == OP_ALT);
2922  start address. A character with multiple other cases is returned on its own      c = *code;
2923  with a special return value.      }
2924    
2925  Arguments:    switch(c)
2926    cptr        points to starting character value; updated      {
2927    d           end value      case OP_END:
2928    ocptr       where to put start of othercase range      /* TRUE only in greedy case. The non-greedy case could be replaced by an
2929    odptr       where to put end of othercase range      OP_EXACT, but it is probably not worth it. (And note that OP_EXACT uses
2930        more memory, which we cannot get at this stage.) */
2931    
2932  Yield:        -1 when no more      return base_list[1] != 0;
                0 when a range is returned  
               >0 the CASESET offset for char with multiple other cases  
                 in this case, ocptr contains the original  
 */  
2933    
2934  static int      case OP_KET:
2935  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,      /* If the bracket is capturing, and referenced by an OP_RECURSE, the
2936    unsigned int *odptr)      non-greedy case cannot be converted to a possessive form. We do not test
2937  {      the bracket type at the moment, but we might do it in the future to improve
2938  unsigned int c, othercase, next;      this condition. (But note that recursive calls are always atomic.) */
 int co;  
2939    
2940  /* Find the first character that has an other case. If it has multiple other      if (base_list[1] == 0) return FALSE;
2941  cases, return its case offset value. */      code += PRIV(OP_lengths)[c];
2942        continue;
2943        }
2944    
2945  for (c = *cptr; c <= d; c++)    /* Check for a supported opcode, and load its properties. */
2946    {  
2947    if ((co = UCD_CASESET(c)) != 0)    code = get_chr_property_list(code, utf, cd->fcc, list);
2948      if (code == NULL) return FALSE;    /* Unsupported */
2949    
2950      /* If either opcode is a small character list, set pointers for comparing
2951      characters from that list with another list, or with a property. */
2952    
2953      if (base_list[0] == OP_CHAR)
2954      {      {
2955      *ocptr = c++;   /* Character that has the set */      chr_ptr = base_list + 2;
2956      *cptr = c;      /* Rest of input range */      list_ptr = list;
2957      return co;      }
2958      }    else if (list[0] == OP_CHAR)
2959    if ((othercase = UCD_OTHERCASE(c)) != c) break;      {
2960    }      chr_ptr = list + 2;
2961        list_ptr = base_list;
2962        }
2963    
2964  if (c > d) return -1;  /* Reached end of range */    /* Some property combinations also acceptable. Unicode property opcodes are
2965      processed specially; the rest can be handled with a lookup table. */
2966    
2967  *ocptr = othercase;    else
2968  next = othercase + 1;      {
2969        pcre_uint32 leftop, rightop;
2970    
2971  for (++c; c <= d; c++)      if (list[1] != 0) return FALSE;   /* Must match at least one character */
2972    {      leftop = base_list[0];
2973    if (UCD_OTHERCASE(c) != next) break;      rightop = list[0];
   next++;  
   }  
2974    
2975  *odptr = next - 1;     /* End of othercase range */  #ifdef SUPPORT_UCP
2976  *cptr = c;             /* Rest of input range */      if (leftop == OP_PROP || leftop == OP_NOTPROP)
2977  return 0;        {
2978  }        if (rightop == OP_EOD) return TRUE;
2979          if (rightop == OP_PROP || rightop == OP_NOTPROP)
2980            {
2981            int n;
2982            const pcre_uint8 *p;
2983            BOOL same = leftop == rightop;
2984            BOOL lisprop = leftop == OP_PROP;
2985            BOOL risprop = rightop == OP_PROP;
2986            BOOL bothprop = lisprop && risprop;
2987    
2988            /* There's a table that specifies how each combination is to be
2989            processed:
2990              0   Always return FALSE (never auto-possessify)
2991              1   Character groups are distinct (possessify if both are OP_PROP)
2992              2   Check character categories in the same group (general or particular)
2993              3   Return TRUE if the two opcodes are not the same
2994              ... see comments below
2995            */
2996    
2997            n = propposstab[base_list[2]][list[2]];
2998            switch(n)
2999              {
3000              case 0: return FALSE;
3001              case 1: return bothprop;
3002              case 2: return (base_list[3] == list[3]) != same;
3003              case 3: return !same;
3004    
3005              case 4:  /* Left general category, right particular category */
3006              return risprop && catposstab[base_list[3]][list[3]] == same;
3007    
3008              case 5:  /* Right general category, left particular category */
3009              return lisprop && catposstab[list[3]][base_list[3]] == same;
3010    
3011              /* This code is logically tricky. Think hard before fiddling with it.
3012              The posspropstab table has four entries per row. Each row relates to
3013              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3014              Only WORD actually needs all four entries, but using repeats for the
3015              others means they can all use the same code below.
3016    
3017              The first two entries in each row are Unicode general categories, and
3018              apply always, because all the characters they include are part of the
3019              PCRE character set. The third and fourth entries are a general and a
3020              particular category, respectively, that include one or more relevant
3021              characters. One or the other is used, depending on whether the check
3022              is for a general or a particular category. However, in both cases the
3023              category contains more characters than the specials that are defined
3024              for the property being tested against. Therefore, it cannot be used
3025              in a NOTPROP case.
3026    
3027              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3028              Underscore is covered by ucp_P or ucp_Po. */
3029    
3030              case 6:  /* Left alphanum vs right general category */
3031              case 7:  /* Left space vs right general category */
3032              case 8:  /* Left word vs right general category */
3033              p = posspropstab[n-6];
3034              return risprop && lisprop ==
3035                (list[3] != p[0] &&
3036                 list[3] != p[1] &&
3037                (list[3] != p[2] || !lisprop));
3038    
3039              case 9:   /* Right alphanum vs left general category */
3040              case 10:  /* Right space vs left general category */
3041              case 11:  /* Right word vs left general category */
3042              p = posspropstab[n-9];
3043              return lisprop && risprop ==
3044                (base_list[3] != p[0] &&
3045                 base_list[3] != p[1] &&
3046                (base_list[3] != p[2] || !risprop));
3047    
3048              case 12:  /* Left alphanum vs right particular category */
3049              case 13:  /* Left space vs right particular category */
3050              case 14:  /* Left word vs right particular category */
3051              p = posspropstab[n-12];
3052              return risprop && lisprop ==
3053                (catposstab[p[0]][list[3]] &&
3054                 catposstab[p[1]][list[3]] &&
3055                (list[3] != p[3] || !lisprop));
3056    
3057              case 15:  /* Right alphanum vs left particular category */
3058              case 16:  /* Right space vs left particular category */
3059              case 17:  /* Right word vs left particular category */
3060              p = posspropstab[n-15];
3061              return lisprop && risprop ==
3062                (catposstab[p[0]][base_list[3]] &&
3063                 catposstab[p[1]][base_list[3]] &&
3064                (base_list[3] != p[3] || !risprop));
3065              }
3066            }
3067          return FALSE;
3068          }
3069    
3070        else
3071    #endif  /* SUPPORT_UCP */
3072    
3073  /*************************************************      return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3074  *        Check a character and a property        *             rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3075  *************************************************/             autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3076        }
3077    
3078  /* This function is called by check_auto_possessive() when a property item    /* Control reaches here only if one of the items is a small character list.
3079  is adjacent to a fixed character.    All characters are checked against the other side. */
3080    
3081  Arguments:    do
3082    c            the character      {
3083    ptype        the property type      chr = *chr_ptr;
   pdata        the data for the type  
   negated      TRUE if it's a negated property (\P or \p{^)  
3084    
3085  Returns:       TRUE if auto-possessifying is OK      switch(list_ptr[0])
3086  */        {
3087          case OP_CHAR:
3088          ochr_ptr = list_ptr + 2;
3089          do
3090            {
3091            if (chr == *ochr_ptr) return FALSE;
3092            ochr_ptr++;
3093            }
3094          while(*ochr_ptr != NOTACHAR);
3095          break;
3096    
3097  static BOOL        case OP_NOT:
3098  check_char_prop(int c, int ptype, int pdata, BOOL negated)        ochr_ptr = list_ptr + 2;
3099  {        do
3100  #ifdef SUPPORT_UCP          {
3101  const pcre_uint32 *p;          if (chr == *ochr_ptr)
3102  #endif            break;
3103            ochr_ptr++;
3104            }
3105          while(*ochr_ptr != NOTACHAR);
3106          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3107          break;
3108    
3109  const ucd_record *prop = GET_UCD(c);        /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3110          set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3111    
3112  switch(ptype)        case OP_DIGIT:
3113    {        if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3114    case PT_LAMP:        break;
   return (prop->chartype == ucp_Lu ||  
           prop->chartype == ucp_Ll ||  
           prop->chartype == ucp_Lt) == negated;  
3115    
3116    case PT_GC:        case OP_NOT_DIGIT:
3117    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;        if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3118          break;
3119    
3120    case PT_PC:        case OP_WHITESPACE:
3121    return (pdata == prop->chartype) == negated;        if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3122          break;
3123    
3124    case PT_SC:        case OP_NOT_WHITESPACE:
3125    return (pdata == prop->script) == negated;        if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3126          break;
3127    
3128    /* These are specials */        case OP_WORDCHAR:
3129          if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3130          break;
3131    
3132    case PT_ALNUM:        case OP_NOT_WORDCHAR:
3133    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||        if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3134            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;        break;
3135    
3136    case PT_SPACE:    /* Perl space */        case OP_HSPACE:
3137    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||        switch(chr)
3138            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)          {
3139            == negated;          HSPACE_CASES: return FALSE;
3140            default: break;
3141            }
3142          break;
3143    
3144    case PT_PXSPACE:  /* POSIX space */        case OP_NOT_HSPACE:
3145    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||        switch(chr)
3146            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||          {
3147            c == CHAR_FF || c == CHAR_CR)          HSPACE_CASES: break;
3148            == negated;          default: return FALSE;
3149            }
3150          break;
3151    
3152    case PT_WORD:        case OP_ANYNL:
3153    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||        case OP_VSPACE:
3154            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||        switch(chr)
3155            c == CHAR_UNDERSCORE) == negated;          {
3156            VSPACE_CASES: return FALSE;
3157  #ifdef SUPPORT_UCP          default: break;
3158    case PT_CLIST:          }
3159    p = PRIV(ucd_caseless_sets) + prop->caseset;        break;
3160    for (;;)  
3161      {        case OP_NOT_VSPACE:
3162      if ((unsigned int)c < *p) return !negated;        switch(chr)
3163      if ((unsigned int)c == *p++) return negated;          {
3164      }          VSPACE_CASES: break;
3165    break;  /* Control never reaches here */          default: return FALSE;
3166            }
3167          break;
3168    
3169          case OP_DOLL:
3170          case OP_EODN:
3171          switch (chr)
3172            {
3173            case CHAR_CR:
3174            case CHAR_LF:
3175            case CHAR_VT:
3176            case CHAR_FF:
3177            case CHAR_NEL:
3178    #ifndef EBCDIC
3179            case 0x2028:
3180            case 0x2029:
3181    #endif  /* Not EBCDIC */
3182            return FALSE;
3183            }
3184          break;
3185    
3186          case OP_EOD:    /* Can always possessify before \z */
3187          break;
3188    
3189          case OP_PROP:
3190          case OP_NOTPROP:
3191          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3192                list_ptr[0] == OP_NOTPROP))
3193            return FALSE;
3194          break;
3195    
3196          /* The class comparisons work only when the class is the second item
3197          of the pair, because there are at present no possessive forms of the
3198          class opcodes. Note also that the "code" variable that is used below
3199          points after the second item, and that the pointer for the first item
3200          is not available, so even if there were possessive forms of the class
3201          opcodes, the correct comparison could not be done. */
3202    
3203          case OP_NCLASS:
3204          if (chr > 255) return FALSE;
3205          /* Fall through */
3206    
3207          case OP_CLASS:
3208          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3209          if (chr > 255) break;
3210          if ((((pcre_uint8 *)(code - list_ptr[2] + 1))[chr >> 3] & (1 << (chr & 7))) != 0)
3211            return FALSE;
3212          break;
3213    
3214    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3215          case OP_XCLASS:
3216          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3217          if (PRIV(xclass)(chr, code - list_ptr[2] + 1 + LINK_SIZE, utf))
3218            return FALSE;
3219          break;
3220  #endif  #endif
3221    
3222          default:
3223          return FALSE;
3224          }
3225    
3226        chr_ptr++;
3227        }
3228      while(*chr_ptr != NOTACHAR);
3229    
3230      /* At least one character must be matched from this opcode. */
3231    
3232      if (list[1] == 0) return TRUE;
3233    }    }
3234    
3235  return FALSE;  return FALSE;
3236  }  }
 #endif  /* SUPPORT_UCP */  
3237    
3238    
3239    
3240  /*************************************************  /*************************************************
3241  *     Check if auto-possessifying is possible    *  *    Scan compiled regex for auto-possession     *
3242  *************************************************/  *************************************************/
3243    
3244  /* This function is called for unlimited repeats of certain items, to see  /* Replaces single character iterations with their possessive alternatives
3245  whether the next thing could possibly match the repeated item. If not, it makes  if appropriate. This function modifies the compiled opcode!
 sense to automatically possessify the repeated item.  
3246    
3247  Arguments:  Arguments:
3248    previous      pointer to the repeated opcode    code        points to start of the byte code
3249    utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3250    ptr           next character in pattern    cd          static compile data
   options       options bits  
   cd            contains pointers to tables etc.  
3251    
3252  Returns:        TRUE if possessifying is wanted  Returns:      nothing
3253  */  */
3254    
3255  static BOOL  static void
3256  check_auto_possessive(const pcre_uchar *previous, BOOL utf,  auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
   const pcre_uchar *ptr, int options, compile_data *cd)  
3257  {  {
3258  pcre_int32 c = NOTACHAR;  register pcre_uchar c;
3259  pcre_int32 next;  const pcre_uchar *end;
3260  int op_code = *previous++;  pcre_uint32 list[8];
   
 /* Skip whitespace and comments in extended mode */  
3261    
3262  if ((options & PCRE_EXTENDED) != 0)  for (;;)
3263    {    {
3264    for (;;)    c = *code;
3265    
3266      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3267      {      {
3268      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      c -= get_repeat_base(c) - OP_STAR;
3269      if (*ptr == CHAR_NUMBER_SIGN)      end = (c <= OP_MINUPTO) ?
3270          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3271        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3272    
3273        if (end != NULL && compare_opcodes(end, utf, cd, list))
3274        {        {
3275        ptr++;        switch(c)
       while (*ptr != 0)  
3276          {          {
3277          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          case OP_STAR:
3278          ptr++;          *code += OP_POSSTAR - OP_STAR;
3279  #ifdef SUPPORT_UTF          break;
3280          if (utf) FORWARDCHAR(ptr);  
3281  #endif          case OP_MINSTAR:
3282            *code += OP_POSSTAR - OP_MINSTAR;
3283            break;
3284    
3285            case OP_PLUS:
3286            *code += OP_POSPLUS - OP_PLUS;
3287            break;
3288    
3289            case OP_MINPLUS:
3290            *code += OP_POSPLUS - OP_MINPLUS;
3291            break;
3292    
3293            case OP_QUERY:
3294            *code += OP_POSQUERY - OP_QUERY;
3295            break;
3296    
3297            case OP_MINQUERY:
3298            *code += OP_POSQUERY - OP_MINQUERY;
3299            break;
3300    
3301            case OP_UPTO:
3302            *code += OP_POSUPTO - OP_UPTO;
3303            break;
3304    
3305            case OP_MINUPTO:
3306            *code += OP_MINUPTO - OP_UPTO;
3307            break;
3308          }          }
3309        }        }
3310      else break;      c = *code;
3311      }      }
   }  
3312    
3313  /* If the next item is one that we can handle, get its value. A non-negative    switch(c)
3314  value is a character, a negative value is an escape value. */      {
3315        case OP_END:
3316        return;
3317    
3318  if (*ptr == CHAR_BACKSLASH)      case OP_TYPESTAR:
3319    {      case OP_TYPEMINSTAR:
3320    int temperrorcode = 0;      case OP_TYPEPLUS:
3321    next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);      case OP_TYPEMINPLUS:
3322    if (temperrorcode != 0) return FALSE;      case OP_TYPEQUERY:
3323    ptr++;    /* Point after the escape sequence */      case OP_TYPEMINQUERY:
3324    }      case OP_TYPEPOSSTAR:
3325  else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)      case OP_TYPEPOSPLUS:
3326    {      case OP_TYPEPOSQUERY:
3327  #ifdef SUPPORT_UTF      if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3328    if (utf) { GETCHARINC(next, ptr); } else      break;
 #endif  
   next = *ptr++;  
   }  
 else return FALSE;  
3329    
3330  /* Skip whitespace and comments in extended mode */      case OP_TYPEUPTO:
3331        case OP_TYPEMINUPTO:
3332        case OP_TYPEEXACT:
3333        case OP_TYPEPOSUPTO:
3334        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3335          code += 2;
3336        break;
3337    
3338  if ((options & PCRE_EXTENDED) != 0)      case OP_XCLASS:
3339    {      code += GET(code, 1);
3340    for (;;)      break;
3341      {  
3342      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      case OP_MARK:
3343      if (*ptr == CHAR_NUMBER_SIGN)      case OP_PRUNE_ARG:
3344        {      case OP_SKIP_ARG:
3345        ptr++;      case OP_THEN_ARG:
3346        while (*ptr != 0)      code += code[1];
3347          {      break;
         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }  
         ptr++;  
 #ifdef SUPPORT_UTF  
         if (utf) FORWARDCHAR(ptr);  
 #endif  
         }  
       }  
     else break;  
3348      }      }
   }  
3349    
3350  /* If the next thing is itself optional, we have to give up. */    /* Add in the fixed length from the table */
3351    
3352  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||    code += PRIV(OP_lengths)[c];
   STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)  
     return FALSE;  
3353    
3354  /* If the previous item is a character, get its value. */    /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3355      a multi-byte character. The length in the table is a minimum, so we have to
3356      arrange to skip the extra bytes. */
3357    
3358  if (op_code == OP_CHAR || op_code == OP_CHARI ||  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3359      op_code == OP_NOT || op_code == OP_NOTI)    if (utf) switch(c)
3360    {      {
3361  #ifdef SUPPORT_UTF      case OP_CHAR:
3362    GETCHARTEST(c, previous);      case OP_CHARI:
3363        case OP_NOT:
3364        case OP_NOTI:
3365        case OP_STAR:
3366        case OP_MINSTAR:
3367        case OP_PLUS:
3368        case OP_MINPLUS:
3369        case OP_QUERY:
3370        case OP_MINQUERY:
3371        case OP_UPTO:
3372        case OP_MINUPTO:
3373        case OP_EXACT:
3374        case OP_POSSTAR:
3375        case OP_POSPLUS:
3376        case OP_POSQUERY:
3377        case OP_POSUPTO:
3378        case OP_STARI:
3379        case OP_MINSTARI:
3380        case OP_PLUSI:
3381        case OP_MINPLUSI:
3382        case OP_QUERYI:
3383        case OP_MINQUERYI:
3384        case OP_UPTOI:
3385        case OP_MINUPTOI:
3386        case OP_EXACTI:
3387        case OP_POSSTARI:
3388        case OP_POSPLUSI:
3389        case OP_POSQUERYI:
3390        case OP_POSUPTOI:
3391        case OP_NOTSTAR:
3392        case OP_NOTMINSTAR:
3393        case OP_NOTPLUS:
3394        case OP_NOTMINPLUS:
3395        case OP_NOTQUERY:
3396        case OP_NOTMINQUERY:
3397        case OP_NOTUPTO:
3398        case OP_NOTMINUPTO:
3399        case OP_NOTEXACT:
3400        case OP_NOTPOSSTAR:
3401        case OP_NOTPOSPLUS:
3402        case OP_NOTPOSQUERY:
3403        case OP_NOTPOSUPTO:
3404        case OP_NOTSTARI:
3405        case OP_NOTMINSTARI:
3406        case OP_NOTPLUSI:
3407        case OP_NOTMINPLUSI:
3408        case OP_NOTQUERYI:
3409        case OP_NOTMINQUERYI:
3410        case OP_NOTUPTOI:
3411        case OP_NOTMINUPTOI:
3412        case OP_NOTEXACTI:
3413        case OP_NOTPOSSTARI:
3414        case OP_NOTPOSPLUSI:
3415        case OP_NOTPOSQUERYI:
3416        case OP_NOTPOSUPTOI:
3417        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3418        break;
3419        }
3420  #else  #else
3421    c = *previous;    (void)(utf);  /* Keep compiler happy by referencing function argument */
3422  #endif  #endif
3423    }    }
3424    }
3425    
 /* Now compare the next item with the previous opcode. First, handle cases when  
 the next item is a character. */  
3426    
 if (next >= 0)  
   {  
   /* For a caseless UTF match, the next character may have more than one other  
   case, which maps to the special PT_CLIST property. Check this first. */  
   
 #ifdef SUPPORT_UCP  
   if (utf && (unsigned int)c != NOTACHAR && (options & PCRE_CASELESS) != 0)  
     {  
     int ocs = UCD_CASESET(next);  
     if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);  
     }  
 #endif  
3427    
3428    switch(op_code)  /*************************************************
3429      {  *           Check for POSIX class syntax         *
3430      case OP_CHAR:  *************************************************/
     return c != next;  
3431    
3432      /* For CHARI (caseless character) we must check the other case. If we have  /* This function is called when the sequence "[:" or "[." or "[=" is
3433      Unicode property support, we can use it to test the other case of  encountered in a character class. It checks whether this is followed by a
3434      high-valued characters. We know that next can have only one other case,  sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3435      because multi-other-case characters are dealt with above. */  reach an unescaped ']' without the special preceding character, return FALSE.
3436    
3437      case OP_CHARI:  Originally, this function only recognized a sequence of letters between the
3438      if (c == next) return FALSE;  terminators, but it seems that Perl recognizes any sequence of characters,
3439  #ifdef SUPPORT_UTF  though of course unknown POSIX names are subsequently rejected. Perl gives an
3440      if (utf)  "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3441        {  didn't consider this to be a POSIX class. Likewise for [:1234:].
3442        unsigned int othercase;  
3443        if (next < 128) othercase = cd->fcc[next]; else  The problem in trying to be exactly like Perl is in the handling of escapes. We
3444  #ifdef SUPPORT_UCP  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3445        othercase = UCD_OTHERCASE((unsigned int)next);  class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3446  #else  below handles the special case of \], but does not try to do any other escape
3447        othercase = NOTACHAR;  processing. This makes it different from Perl for cases such as [:l\ower:]
3448  #endif  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
3449        return (unsigned int)c != othercase;  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
3450        }  I think.
3451      else  
3452  #endif  /* SUPPORT_UTF */  A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3453      return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Not UTF */  It seems that the appearance of a nested POSIX class supersedes an apparent
3454    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3455      case OP_NOT:  a digit.
3456      return c == next;  
3457    In Perl, unescaped square brackets may also appear as part of class names. For
3458      case OP_NOTI:  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3459      if (c == next) return TRUE;  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3460  #ifdef SUPPORT_UTF  seem right at all. PCRE does not allow closing square brackets in POSIX class
3461      if (utf)  names.
3462    
3463    Arguments:
3464      ptr      pointer to the initial [
3465      endptr   where to return the end pointer
3466    
3467    Returns:   TRUE or FALSE
3468    */
3469    
3470    static BOOL
3471    check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3472    {
3473    pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3474    terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3475    for (++ptr; *ptr != CHAR_NULL; ptr++)
3476      {
3477      if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3478        ptr++;
3479      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3480      else
3481        {
3482        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3483        {        {
3484        unsigned int othercase;        *endptr = ptr;
3485        if (next < 128) othercase = cd->fcc[next]; else        return TRUE;
 #ifdef SUPPORT_UCP  
       othercase = UCD_OTHERCASE((unsigned int)next);  
 #else  
       othercase = NOTACHAR;  
 #endif  
       return (unsigned int)c == othercase;  
3486        }        }
3487      else      if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3488  #endif  /* SUPPORT_UTF */           (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3489      return (c == TABLE_GET((unsigned int)next, cd->fcc, next));  /* Not UTF */            ptr[1] == CHAR_EQUALS_SIGN) &&
3490            check_posix_syntax(ptr, endptr))
3491          return FALSE;
3492        }
3493      }
3494    return FALSE;
3495    }
3496    
     /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.  
     When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */  
3497    
     case OP_DIGIT:  
     return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;  
3498    
     case OP_NOT_DIGIT:  
     return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;  
3499    
3500      case OP_WHITESPACE:  /*************************************************
3501      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;  *          Check POSIX class name                *
3502    *************************************************/
3503    
3504      case OP_NOT_WHITESPACE:  /* This function is called to check the name given in a POSIX-style class entry
3505      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;  such as [:alnum:].
3506    
3507      case OP_WORDCHAR:  Arguments:
3508      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;    ptr        points to the first letter
3509      len        the length of the name
3510    
3511      case OP_NOT_WORDCHAR:  Returns:     a value representing the name, or -1 if unknown
3512      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;  */
3513    
3514      case OP_HSPACE:  static int
3515      case OP_NOT_HSPACE:  check_posix_name(const pcre_uchar *ptr, int len)
3516      switch(next)  {
3517        {  const char *pn = posix_names;
3518        HSPACE_CASES:  register int yield = 0;
3519        return op_code == OP_NOT_HSPACE;  while (posix_name_lengths[yield] != 0)
3520      {
3521      if (len == posix_name_lengths[yield] &&
3522        STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3523      pn += posix_name_lengths[yield] + 1;
3524      yield++;
3525      }
3526    return -1;
3527    }
3528    
       default:  
       return op_code != OP_NOT_HSPACE;  
       }  
3529    
3530      case OP_ANYNL:  /*************************************************
3531      case OP_VSPACE:  *    Adjust OP_RECURSE items in repeated group   *
3532      case OP_NOT_VSPACE:  *************************************************/
     switch(next)  
       {  
       VSPACE_CASES:  
       return op_code == OP_NOT_VSPACE;  
3533    
3534        default:  /* OP_RECURSE items contain an offset from the start of the regex to the group
3535        return op_code != OP_NOT_VSPACE;  that is referenced. This means that groups can be replicated for fixed
3536        }  repetition simply by copying (because the recursion is allowed to refer to
3537    earlier groups that are outside the current group). However, when a group is
3538    optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
3539    inserted before it, after it has been compiled. This means that any OP_RECURSE
3540    items within it that refer to the group itself or any contained groups have to
3541    have their offsets adjusted. That one of the jobs of this function. Before it
3542    is called, the partially compiled regex must be temporarily terminated with
3543    OP_END.
3544    
3545  #ifdef SUPPORT_UCP  This function has been extended with the possibility of forward references for
3546      case OP_PROP:  recursions and subroutine calls. It must also check the list of such references
3547      return check_char_prop(next, previous[0], previous[1], FALSE);  for the group we are dealing with. If it finds that one of the recursions in
3548    the current group is on this list, it adjusts the offset in the list, not the
3549    value in the reference (which is a group number).
3550    
3551      case OP_NOTPROP:  Arguments:
3552      return check_char_prop(next, previous[0], previous[1], TRUE);    group      points to the start of the group
3553  #endif    adjust     the amount by which the group is to be moved
3554      utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3555      cd         contains pointers to tables etc.
3556      save_hwm   the hwm forward reference pointer at the start of the group
3557    
3558      default:  Returns:     nothing
3559      return FALSE;  */
     }  
   }  
3560    
3561  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  static void
3562  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
3563  generated only when PCRE_UCP is *not* set, that is, when only ASCII    pcre_uchar *save_hwm)
3564  characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are  {
3565  replaced by OP_PROP codes when PCRE_UCP is set. */  pcre_uchar *ptr = group;
3566    
3567  switch(op_code)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
3568    {    {
3569    case OP_CHAR:    int offset;
3570    case OP_CHARI:    pcre_uchar *hc;
   switch(-next)  
     {  
     case ESC_d:  
     return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;  
3571    
3572      case ESC_D:    /* See if this recursion is on the forward reference list. If so, adjust the
3573      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;    reference. */
3574    
3575      case ESC_s:    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
3576      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;      {
3577        offset = (int)GET(hc, 0);
3578        if (cd->start_code + offset == ptr + 1)
3579          {
3580          PUT(hc, 0, offset + adjust);
3581          break;
3582          }
3583        }
3584    
3585      case ESC_S:    /* Otherwise, adjust the recursion offset if it's after the start of this
3586      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;    group. */
3587    
3588      case ESC_w:    if (hc >= cd->hwm)
3589      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;      {
3590        offset = (int)GET(ptr, 1);
3591        if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
3592        }
3593    
3594      case ESC_W:    ptr += 1 + LINK_SIZE;
3595      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;    }
3596    }
3597    
     case ESC_h:  
     case ESC_H:  
     switch(c)  
       {  
       HSPACE_CASES:  
       return -next != ESC_h;  
3598    
       default:  
       return -next == ESC_h;  
       }  
3599    
3600      case ESC_v:  /*************************************************
3601      case ESC_V:  *        Insert an automatic callout point       *
3602      switch(c)  *************************************************/
       {  
       VSPACE_CASES:  
       return -next != ESC_v;  
3603    
3604        default:  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
3605        return -next == ESC_v;  callout points before each pattern item.
       }  
3606    
3607      /* When PCRE_UCP is set, these values get generated for \d etc. Find  Arguments:
3608      their substitutions and process them. The result will always be either    code           current code pointer
3609      -ESC_p or -ESC_P. Then fall through to process those values. */    ptr            current pattern pointer
3610      cd             pointers to tables etc
3611    
3612  #ifdef SUPPORT_UCP  Returns:         new code pointer
3613      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 */  
       }  
     /* Fall through */  
3614    
3615      case ESC_p:  static pcre_uchar *
3616      case ESC_P:  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
3617        {  {
3618        int ptype, pdata, errorcodeptr;  *code++ = OP_CALLOUT;
3619        BOOL negated;  *code++ = 255;
3620    PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
3621    PUT(code, LINK_SIZE, 0);                       /* Default length */
3622    return code + 2 * LINK_SIZE;
3623    }
3624    
       ptr--;      /* Make ptr point at the p or P */  
       ptype = get_ucp(&ptr, &negated, &pdata, &errorcodeptr);  
       if (ptype < 0) return FALSE;  
       ptr++;      /* Point past the final curly ket */  
3625    
       /* If the property item is optional, we have to give up. (When generated  
       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. */  
3626    
3627        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  /*************************************************
3628          STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)  *         Complete a callout item                *
3629            return FALSE;  *************************************************/
3630    
3631        /* Do the property check. */  /* A callout item contains the length of the next item in the pattern, which
3632    we can't fill in till after we have reached the relevant point. This is used
3633    for both automatic and manual callouts.
3634    
3635        return check_char_prop(c, ptype, pdata, (next == -ESC_P) != negated);  Arguments:
3636        }    previous_callout   points to previous callout item
3637  #endif    ptr                current pattern pointer
3638      cd                 pointers to tables etc
3639    
3640      default:  Returns:             nothing
3641      return FALSE;  */
     }  
3642    
3643    /* In principle, support for Unicode properties should be integrated here as  static void
3644    well. It means re-organizing the above code so as to get hold of the property  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
3645    values before switching on the op-code. However, I wonder how many patterns  {
3646    combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
3647    these op-codes are never generated.) */  PUT(previous_callout, 2 + LINK_SIZE, length);
3648    }
3649    
   case OP_DIGIT:  
   return next == -ESC_D || next == -ESC_s || next == -ESC_W ||  
          next == -ESC_h || next == -ESC_v || next == -ESC_R;  
3650    
   case OP_NOT_DIGIT:  
   return next == -ESC_d;  
3651    
3652    case OP_WHITESPACE:  #ifdef SUPPORT_UCP
3653    return next == -ESC_S || next == -ESC_d || next == -ESC_w;  /*************************************************
3654    *           Get othercase range                  *
3655    *************************************************/
3656    
3657    case OP_NOT_WHITESPACE:  /* This function is passed the start and end of a class range, in UTF-8 mode
3658    return next == -ESC_s || next == -ESC_h || next == -ESC_v || next == -ESC_R;  with UCP support. It searches up the characters, looking for ranges of
3659    characters in the "other" case. Each call returns the next one, updating the
3660    start address. A character with multiple other cases is returned on its own
3661    with a special return value.
3662    
3663    case OP_HSPACE:  Arguments:
3664    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||    cptr        points to starting character value; updated
3665           next == -ESC_w || next == -ESC_v || next == -ESC_R;    d           end value
3666      ocptr       where to put start of othercase range
3667      odptr       where to put end of othercase range
3668    
3669    case OP_NOT_HSPACE:  Yield:        -1 when no more
3670    return next == -ESC_h;                 0 when a range is returned
3671                  >0 the CASESET offset for char with multiple other cases
3672                    in this case, ocptr contains the original
3673    */
3674    
3675    /* Can't have \S in here because VT matches \S (Perl anomaly) */  static int
3676    case OP_ANYNL:  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
3677    case OP_VSPACE:    pcre_uint32 *odptr)
3678    return next == -ESC_V || next == -ESC_d || next == -ESC_w;  {
3679    pcre_uint32 c, othercase, next;
3680    unsigned int co;
3681    
3682    case OP_NOT_VSPACE:  /* Find the first character that has an other case. If it has multiple other
3683    return next == -ESC_v || next == -ESC_R;  cases, return its case offset value. */
3684    
3685    case OP_WORDCHAR:  for (c = *cptr; c <= d; c++)
3686    return next == -ESC_W || next == -ESC_s || next == -ESC_h ||    {
3687           next == -ESC_v || next == -ESC_R;    if ((co = UCD_CASESET(c)) != 0)
3688        {
3689        *ocptr = c++;   /* Character that has the set */
3690        *cptr = c;      /* Rest of input range */
3691        return (int)co;
3692        }
3693      if ((othercase = UCD_OTHERCASE(c)) != c) break;
3694      }
3695    
3696    case OP_NOT_WORDCHAR:  if (c > d) return -1;  /* Reached end of range */
3697    return next == -ESC_w || next == -ESC_d;  
3698    *ocptr = othercase;
3699    next = othercase + 1;
3700    
3701    default:  for (++c; c <= d; c++)
3702    return FALSE;    {
3703      if (UCD_OTHERCASE(c) != next) break;
3704      next++;
3705    }    }
3706    
3707  /* Control does not reach here */  *odptr = next - 1;     /* End of othercase range */
3708    *cptr = c;             /* Rest of input range */
3709    return 0;
3710  }  }
3711    #endif  /* SUPPORT_UCP */
3712    
3713    
3714    
# Line 3403  switch(op_code) Line 3717  switch(op_code)
3717  *************************************************/  *************************************************/
3718    
3719  /* This function packages up the logic of adding a character or range of  /* This function packages up the logic of adding a character or range of
3720  characters to a class. The character values in the arguments will be within the  characters to a class. The character values in the arguments will be within the
3721  valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is  valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3722  mutually recursive with the function immediately below.  mutually recursive with the function immediately below.
3723    
3724  Arguments:  Arguments:
3725    classbits     the bit map for characters < 256    classbits     the bit map for characters < 256
3726    uchardptr     points to the pointer for extra data    uchardptr     points to the pointer for extra data
3727    options       the options word    options       the options word
3728    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
3729    start         start of range character    start         start of range character
3730    end           end of range character    end           end of range character
3731    
3732  Returns:        the number of < 256 characters added  Returns:        the number of < 256 characters added
3733                  the pointer to extra data is updated                  the pointer to extra data is updated
3734  */  */
# Line 3426  add_to_class(pcre_uint8 *classbits, pcre Line 3740  add_to_class(pcre_uint8 *classbits, pcre
3740  pcre_uint32 c;  pcre_uint32 c;
3741  int n8 = 0;  int n8 = 0;
3742    
3743  /* If caseless matching is required, scan the range and process alternate  /* If caseless matching is required, scan the range and process alternate
3744  cases. In Unicode, there are 8-bit characters that have alternate cases that  cases. In Unicode, there are 8-bit characters that have alternate cases that
3745  are greater than 255 and vice-versa. Sometimes we can just extend the original  are greater than 255 and vice-versa. Sometimes we can just extend the original
3746  range. */  range. */
3747    
3748  if ((options & PCRE_CASELESS) != 0)  if ((options & PCRE_CASELESS) != 0)
3749    {    {
3750  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3751    if ((options & PCRE_UTF8) != 0)    if ((options & PCRE_UTF8) != 0)
3752      {      {
3753      int rc;      int rc;
3754      pcre_uint32 oc, od;      pcre_uint32 oc, od;
3755    
3756      options &= ~PCRE_CASELESS;   /* Remove for recursive calls */      options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3757      c = start;      c = start;
3758    
3759      while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)      while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3760        {        {
3761        /* Handle a single character that has more than one other case. */        /* Handle a single character that has more than one other case. */
3762    
3763        if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,        if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3764          PRIV(ucd_caseless_sets) + rc, oc);          PRIV(ucd_caseless_sets) + rc, oc);
3765    
3766        /* Do nothing if the other case range is within the original range. */        /* Do nothing if the other case range is within the original range. */
3767    
3768        else if (oc >= start && od <= end) continue;        else if (oc >= start && od <= end) continue;
3769    
3770        /* Extend the original range if there is overlap, noting that if oc < c, we        /* Extend the original range if there is overlap, noting that if oc < c, we
3771        can't have od > end because a subrange is always shorter than the basic        can't have od > end because a subrange is always shorter than the basic
3772        range. Otherwise, use a recursive call to add the additional range. */        range. Otherwise, use a recursive call to add the additional range. */
3773    
3774        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3775        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3776        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
# Line 3466  if ((options & PCRE_CASELESS) != 0) Line 3780  if ((options & PCRE_CASELESS) != 0)
3780  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3781    
3782    /* Not UTF-mode, or no UCP */    /* Not UTF-mode, or no UCP */
3783    
3784    for (c = start; c <= end && c < 256; c++)    for (c = start; c <= end && c < 256; c++)
3785      {      {
3786      SETBIT(classbits, cd->fcc[c]);      SETBIT(classbits, cd->fcc[c]);
3787      n8++;      n8++;
3788      }      }
3789    }    }
3790    
3791  /* Now handle the original range. Adjust the final value according to the bit  /* Now handle the original range. Adjust the final value according to the bit
3792  length - this means that the same lists of (e.g.) horizontal spaces can be used  length - this means that the same lists of (e.g.) horizontal spaces can be used
3793  in all cases. */  in all cases. */
# Line 3499  if (end < 0x100) Line 3813  if (end < 0x100)
3813    {    {
3814    for (c = start; c <= end; c++)    for (c = start; c <= end; c++)
3815      {      {
3816      n8++;      n8++;
3817      SETBIT(classbits, c);      SETBIT(classbits, c);
3818      }      }
3819    }    }
3820    
3821  else  else
3822    {    {
3823    pcre_uchar *uchardata = *uchardptr;    pcre_uchar *uchardata = *uchardptr;
3824    
3825  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3826    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3827      {      {
3828      if (start < end)      if (start < end)
3829        {        {
3830        *uchardata++ = XCL_RANGE;        *uchardata++ = XCL_RANGE;
3831        uchardata += PRIV(ord2utf)(start, uchardata);        uchardata += PRIV(ord2utf)(start, uchardata);
3832        uchardata += PRIV(ord2utf)(end, uchardata);        uchardata += PRIV(ord2utf)(end, uchardata);
3833        }        }
3834      else if (start == end)      else if (start == end)
3835        {        {
3836        *uchardata++ = XCL_SINGLE;        *uchardata++ = XCL_SINGLE;
3837        uchardata += PRIV(ord2utf)(start, uchardata);        uchardata += PRIV(ord2utf)(start, uchardata);
3838        }        }
3839      }      }
3840    else    else
3841  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
3842    
3843    /* Without UTF support, character values are constrained by the bit length,    /* Without UTF support, character values are constrained by the bit length,
3844    and can only be > 256 for 16-bit and 32-bit libraries. */    and can only be > 256 for 16-bit and 32-bit libraries. */
3845    
3846  #ifdef COMPILE_PCRE8  #ifdef COMPILE_PCRE8
3847      {}      {}
3848  #else  #else
3849    if (start < end)    if (start < end)
3850      {      {
3851      *uchardata++ = XCL_RANGE;      *uchardata++ = XCL_RANGE;
# Line 3542  else Line 3856  else
3856      {      {
3857      *uchardata++ = XCL_SINGLE;      *uchardata++ = XCL_SINGLE;
3858      *uchardata++ = start;      *uchardata++ = start;
3859      }      }
3860  #endif  #endif
3861    
3862    *uchardptr = uchardata;   /* Updata extra data pointer */    *uchardptr = uchardata;   /* Updata extra data pointer */
3863    }    }
3864    
3865  return n8;    /* Number of 8-bit characters */  return n8;    /* Number of 8-bit characters */
3866  }  }
3867    
3868    
3869    
3870    
3871  /*************************************************  /*************************************************
3872  *        Add a list of characters to a class     *  *        Add a list of characters to a class     *
3873  *************************************************/  *************************************************/
3874    
3875  /* This function is used for adding a list of case-equivalent characters to a  /* This function is used for adding a list of case-equivalent characters to a
3876  class, and also for adding a list of horizontal or vertical whitespace. If the  class, and also for adding a list of horizontal or vertical whitespace. If the
3877  list is in order (which it should be), ranges of characters are detected and  list is in order (which it should be), ranges of characters are detected and
3878  handled appropriately. This function is mutually recursive with the function  handled appropriately. This function is mutually recursive with the function
# Line 3568  Arguments: Line 3882  Arguments:
3882    classbits     the bit map for characters < 256    classbits     the bit map for characters < 256
3883    uchardptr     points to the pointer for extra data    uchardptr     points to the pointer for extra data
3884    options       the options word    options       the options word
3885    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
3886    p             points to row of 32-bit values, terminated by NOTACHAR    p             points to row of 32-bit values, terminated by NOTACHAR
3887    except        character to omit; this is used when adding lists of    except        character to omit; this is used when adding lists of
3888                    case-equivalent characters to avoid including the one we                    case-equivalent characters to avoid including the one we
3889                    already know about                    already know about
3890    
3891  Returns:        the number of < 256 characters added  Returns:        the number of < 256 characters added
3892                  the pointer to extra data is updated                  the pointer to extra data is updated
3893  */  */
# Line 3587  while (p[0] < NOTACHAR) Line 3901  while (p[0] < NOTACHAR)
3901    {    {
3902    int n = 0;    int n = 0;
3903    if (p[0] != except)    if (p[0] != except)
3904      {      {
3905      while(p[n+1] == p[0] + n + 1) n++;      while(p[n+1] == p[0] + n + 1) n++;
3906      n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);      n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3907      }      }
3908    p += n + 1;    p += n + 1;
3909    }    }
3910  return n8;  return n8;
3911  }  }
3912    
3913    
3914    
# Line 3609  Arguments: Line 3923  Arguments:
3923    classbits     the bit map for characters < 256    classbits     the bit map for characters < 256
3924    uchardptr     points to the pointer for extra data    uchardptr     points to the pointer for extra data
3925    options       the options word    options       the options word
3926    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
3927    p             points to row of 32-bit values, terminated by NOTACHAR    p             points to row of 32-bit values, terminated by NOTACHAR
3928    
3929  Returns:        the number of < 256 characters added  Returns:        the number of < 256 characters added
3930                  the pointer to extra data is updated                  the pointer to extra data is updated
3931  */  */
3932    
3933  static int  static int
3934  add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,  add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3935    int options, compile_data *cd, const pcre_uint32 *p)    int options, compile_data *cd, const pcre_uint32 *p)
3936  {  {
3937  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
# Line 3629  while (p[0] < NOTACHAR) Line 3943  while (p[0] < NOTACHAR)
3943    while (p[1] == p[0] + 1) p++;    while (p[1] == p[0] + 1) p++;
3944    n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,    n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3945      (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);      (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
3946    p++;    p++;
3947    }    }
3948  return n8;  return n8;
3949  }  }
3950    
3951    
3952    
# Line 3647  to find out the amount of memory needed, Line 3961  to find out the amount of memory needed,
3961  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
3962    
3963  Arguments:  Arguments:
3964    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
3965    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
3966    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
3967    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
3968    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr      place to put the first required character
3969    reqcharptr     set to the last literal character required, else < 0    firstcharflagsptr place to put the first character flags, or a negative number
3970    bcptr          points to current branch chain    reqcharptr        place to put the last required character
3971    cond_depth     conditional nesting depth    reqcharflagsptr   place to put the last required character flags, or a negative number
3972    cd             contains pointers to tables etc.    bcptr             points to current branch chain
3973    lengthptr      NULL during the real compile phase    cond_depth        conditional nesting depth
3974                   points to length accumulator during pre-compile phase    cd                contains pointers to tables etc.
3975      lengthptr         NULL during the real compile phase
3976                        points to length accumulator during pre-compile phase
3977    
3978  Returns:         TRUE on success  Returns:            TRUE on success
3979                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
3980  */  */
3981    
3982  static BOOL  static BOOL
3983  compile_branch(int *optionsptr, pcre_uchar **codeptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3984    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,    const pcre_uchar **ptrptr, int *errorcodeptr,
3985    pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,    pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
3986      pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
3987      branch_chain *bcptr, int cond_depth,
3988    compile_data *cd, int *lengthptr)    compile_data *cd, int *lengthptr)
3989  {  {
3990  int repeat_type, op_type;  int repeat_type, op_type;
3991  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3992  int bravalue = 0;  int bravalue = 0;
3993  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3994  pcre_int32 firstchar, reqchar;  pcre_uint32 firstchar, reqchar;
3995  pcre_int32 zeroreqchar, zerofirstchar;  pcre_int32 firstcharflags, reqcharflags;
3996    pcre_uint32 zeroreqchar, zerofirstchar;
3997    pcre_int32 zeroreqcharflags, zerofirstcharflags;
3998  pcre_int32 req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
3999  int options = *optionsptr;               /* May change dynamically */  int options = *optionsptr;               /* May change dynamically */
4000  int after_manual_callout = 0;  int after_manual_callout = 0;
4001  int length_prevgroup = 0;  int length_prevgroup = 0;
4002  register int c;  register pcre_uint32 c;
4003    int escape;
4004  register pcre_uchar *code = *codeptr;  register pcre_uchar *code = *codeptr;
4005  pcre_uchar *last_code = code;  pcre_uchar *last_code = code;
4006  pcre_uchar *orig_code = code;  pcre_uchar *orig_code = code;
# Line 3699  must not do this for other options (e.g. Line 4020  must not do this for other options (e.g.
4020  dynamically as we process the pattern. */  dynamically as we process the pattern. */
4021    
4022  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4023  /* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */  /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
4024  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
4025    #ifndef COMPILE_PCRE32
4026  pcre_uchar utf_chars[6];  pcre_uchar utf_chars[6];
4027    #endif
4028  #else  #else
4029  BOOL utf = FALSE;  BOOL utf = FALSE;
4030  #endif  #endif
4031    
4032  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
4033  class_uchardata always so that it can be passed to add_to_class() always,  class_uchardata always so that it can be passed to add_to_class() always,
4034  though it will not be used in non-UTF 8-bit cases. This avoids having to supply  though it will not be used in non-UTF 8-bit cases. This avoids having to supply
4035  alternative calls for the different cases. */  alternative calls for the different cases. */
4036    
4037  pcre_uchar *class_uchardata;  pcre_uchar *class_uchardata;
# Line 3736  to take the zero repeat into account. Th Line 4059  to take the zero repeat into account. Th
4059  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
4060  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
4061    
4062  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
4063    firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
4064    
4065  /* The variable req_caseopt contains either the REQ_CASELESS value  /* The variable req_caseopt contains either the REQ_CASELESS value
4066  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 3762  for (;; ptr++) Line 4086  for (;; ptr++)
4086    int recno;    int recno;
4087    int refsign;    int refsign;
4088    int skipbytes;    int skipbytes;
4089    int subreqchar;    pcre_uint32 subreqchar, subfirstchar;
4090    int subfirstchar;    pcre_int32 subreqcharflags, subfirstcharflags;
4091    int terminator;    int terminator;
4092    int mclength;    unsigned int mclength;
4093    int tempbracount;    unsigned int tempbracount;
4094      pcre_uint32 ec;
4095    pcre_uchar mcbuffer[8];    pcre_uchar mcbuffer[8];
4096    
4097    /* Get next character in the pattern */    /* Get next character in the pattern */
# Line 3776  for (;; ptr++) Line 4101  for (;; ptr++)
4101    /* 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
4102    string. Nesting only happens one level deep. */    string. Nesting only happens one level deep. */
4103    
4104    if (c == 0 && nestptr != NULL)    if (c == CHAR_NULL && nestptr != NULL)
4105      {      {
4106      ptr = nestptr;      ptr = nestptr;
4107      nestptr = NULL;      nestptr = NULL;
# Line 3851  for (;; ptr++) Line 4176  for (;; ptr++)
4176    
4177    /* 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 */
4178    
4179    if (inescq && c != 0)    if (inescq && c != CHAR_NULL)
4180      {      {
4181      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
4182        {        {
# Line 3899  for (;; ptr++) Line 4224  for (;; ptr++)
4224      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
4225        {        {
4226        ptr++;        ptr++;
4227        while (*ptr != 0)        while (*ptr != CHAR_NULL)
4228          {          {
4229          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
4230          ptr++;          ptr++;
# Line 3907  for (;; ptr++) Line 4232  for (;; ptr++)
4232          if (utf) FORWARDCHAR(ptr);          if (utf) FORWARDCHAR(ptr);
4233  #endif  #endif
4234          }          }
4235        if (*ptr != 0) continue;        if (*ptr != CHAR_NULL) continue;
4236    
4237        /* Else fall through to handle end of string */        /* Else fall through to handle end of string */
4238        c = 0;        c = 0;
# Line 3929  for (;; ptr++) Line 4254  for (;; ptr++)
4254      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
4255      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
4256      *firstcharptr = firstchar;      *firstcharptr = firstchar;
4257        *firstcharflagsptr = firstcharflags;
4258      *reqcharptr = reqchar;      *reqcharptr = reqchar;
4259        *reqcharflagsptr = reqcharflags;
4260      *codeptr = code;      *codeptr = code;
4261      *ptrptr = ptr;      *ptrptr = ptr;
4262      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 3953  for (;; ptr++) Line 4280  for (;; ptr++)
4280      previous = NULL;      previous = NULL;
4281      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
4282        {        {
4283        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4284        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
4285        }        }
4286      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 3968  for (;; ptr++) Line 4295  for (;; ptr++)
4295      repeats. The value of reqchar doesn't change either. */      repeats. The value of reqchar doesn't change either. */
4296    
4297      case CHAR_DOT:      case CHAR_DOT:
4298      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4299      zerofirstchar = firstchar;      zerofirstchar = firstchar;
4300        zerofirstcharflags = firstcharflags;
4301      zeroreqchar = reqchar;      zeroreqchar = reqchar;
4302        zeroreqcharflags = reqcharflags;
4303      previous = code;      previous = code;
4304      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
4305      break;      break;
# Line 4044  for (;; ptr++) Line 4373  for (;; ptr++)
4373          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4374        {        {
4375        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
4376        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4377        zerofirstchar = firstchar;        zerofirstchar = firstchar;
4378          zerofirstcharflags = firstcharflags;
4379        break;        break;
4380        }        }
4381    
# Line 4080  for (;; ptr++) Line 4410  for (;; ptr++)
4410      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
4411      loop, c contains the first byte of the character. */      loop, c contains the first byte of the character. */
4412    
4413      if (c != 0) do      if (c != CHAR_NULL) do
4414        {        {
4415        const pcre_uchar *oldptr;        const pcre_uchar *oldptr;
4416    
4417  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #ifdef SUPPORT_UTF
4418        if (utf && HAS_EXTRALEN(c))        if (utf && HAS_EXTRALEN(c))
4419          {                           /* Braces are required because the */          {                           /* Braces are required because the */
4420          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
# Line 4095  for (;; ptr++) Line 4425  for (;; ptr++)
4425        /* In the pre-compile phase, accumulate the length of any extra        /* In the pre-compile phase, accumulate the length of any extra
4426        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
4427        contain a zillion > 255 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
4428        (which is on the stack). We have to remember that there was XCLASS data,        (which is on the stack). We have to remember that there was XCLASS data,
4429        however. */        however. */
4430    
4431        if (lengthptr != NULL && class_uchardata > class_uchardata_base)        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
# Line 4159  for (;; ptr++) Line 4489  for (;; ptr++)
4489          alpha. This relies on the fact that the class table starts with          alpha. This relies on the fact that the class table starts with
4490          alpha, lower, upper as the first 3 entries. */          alpha, lower, upper as the first 3 entries. */
4491    
4492          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
4493            posix_class = 0;            posix_class = 0;
4494    
4495          /* When PCRE_UCP is set, some of the POSIX classes are converted to          /* When PCRE_UCP is set, some of the POSIX classes are converted to
# Line 4236  for (;; ptr++) Line 4566  for (;; ptr++)
4566    
4567        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
4568          {          {
4569          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
4570              TRUE);
4571          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
4572            if (escape == 0) c = ec;
4573          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 */
4574          else if (-c == ESC_N)            /* \N is not supported in a class */          else if (escape == ESC_N)          /* \N is not supported in a class */
4575            {            {
4576            *errorcodeptr = ERR71;            *errorcodeptr = ERR71;
4577            goto FAILED;            goto FAILED;
4578            }            }
4579          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (escape == ESC_Q)            /* Handle start of quoted string */
4580            {            {
4581            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)            if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4582              {              {
# Line 4254  for (;; ptr++) Line 4585  for (;; ptr++)
4585            else inescq = TRUE;            else inescq = TRUE;
4586            continue;            continue;
4587            }            }
4588          else if (-c == ESC_E) continue;  /* Ignore orphan \E */          else if (escape == ESC_E) continue;  /* Ignore orphan \E */
4589    
4590          if (c < 0)          else
4591            {            {
4592            register const pcre_uint8 *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
4593            /* Every class contains at least two < 256 characters. */            /* Every class contains at least two < 256 characters. */
# Line 4264  for (;; ptr++) Line 4595  for (;; ptr++)
4595            /* Every class contains at least two characters. */            /* Every class contains at least two characters. */
4596            class_one_char += 2;            class_one_char += 2;
4597    
4598            switch (-c)            switch (escape)
4599              {              {
4600  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4601              case ESC_du:     /* These are the values given for \d etc */              case ESC_du:     /* These are the values given for \d etc */
# Line 4274  for (;; ptr++) Line 4605  for (;; ptr++)
4605              case ESC_su:     /* of the default ASCII testing. */              case ESC_su:     /* of the default ASCII testing. */
4606              case ESC_SU:              case ESC_SU:
4607              nestptr = ptr;              nestptr = ptr;
4608              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */              ptr = substitutes[escape - ESC_DU] - 1;  /* Just before substitute */
4609              class_has_8bitchar--;                /* Undo! */              class_has_8bitchar--;                /* Undo! */
4610              continue;              continue;
4611  #endif  #endif
# Line 4312  for (;; ptr++) Line 4643  for (;; ptr++)
4643              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
4644              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              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. */              /* The rest apply in both UCP and non-UCP cases. */
4648    
4649              case ESC_h:              case ESC_h:
4650              (void)add_list_to_class(classbits, &class_uchardata, options, cd,              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4651                PRIV(hspace_list), NOTACHAR);                PRIV(hspace_list), NOTACHAR);
4652              continue;              continue;
4653    
4654              case ESC_H:              case ESC_H:
4655              (void)add_not_list_to_class(classbits, &class_uchardata, options,              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4656                cd, PRIV(hspace_list));                cd, PRIV(hspace_list));
4657              continue;              continue;
4658    
4659              case ESC_v:              case ESC_v:
4660              (void)add_list_to_class(classbits, &class_uchardata, options, cd,              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4661                PRIV(vspace_list), NOTACHAR);                PRIV(vspace_list), NOTACHAR);
4662              continue;              continue;
4663    
4664              case ESC_V:              case ESC_V:
4665              (void)add_not_list_to_class(classbits, &class_uchardata, options,              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4666                cd, PRIV(vspace_list));                cd, PRIV(vspace_list));
4667              continue;              continue;
4668    
4669  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 4340  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                *class_uchardata++ = ((-c == ESC_p) != negated)?                *class_uchardata++ = ((escape == 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 4370  for (;; ptr++) Line 4701  for (;; ptr++)
4701    
4702          /* Fall through if the escape just defined a single character (c >= 0).          /* Fall through if the escape just defined a single character (c >= 0).
4703          This may be 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 character may be followed by '-' to form a range. However, Perl does        /* A character may be followed by '-' to form a range. However, Perl does
# Line 4394  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 4409  for (;; ptr++) Line 4742  for (;; ptr++)
4742            inescq = TRUE;            inescq = TRUE;
4743            break;            break;
4744            }            }
4745    
4746          /* Minus (hyphen) at the end of a class is treated as a literal, so put          /* 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. */          back the pointer and jump to handle the character that preceded it. */
4748    
4749          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4750            {            {
4751            ptr = oldptr;            ptr = oldptr;
4752            goto CLASS_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4753            }            }
4754    
4755          /* Otherwise, we have a potential range; pick up the next character */          /* Otherwise, we have a potential range; pick up the next character */
4756    
4757  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
# Line 4436  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 CLASS_SINGLE_CHARACTER;  /* A few lines below */                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
# Line 4464  for (;; ptr++) Line 4798  for (;; ptr++)
4798          /* We have found a character range, so single character optimizations          /* We have found a character range, so single character optimizations
4799          cannot be done anymore. Any value greater than 1 indicates that there          cannot be done anymore. Any value greater than 1 indicates that there
4800          is more than one character. */          is more than one character. */
4801    
4802          class_one_char = 2;          class_one_char = 2;
4803    
4804          /* Remember an explicit \r or \n, and add the range to the class. */          /* 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          class_has_8bitchar +=          class_has_8bitchar +=
4809            add_to_class(classbits, &class_uchardata, options, cd, c, d);            add_to_class(classbits, &class_uchardata, options, cd, c, d);
4810    
4811          continue;   /* Go get the next char in the class */          continue;   /* Go get the next char in the class */
4812          }          }
4813    
4814        /* Handle a single character - we can get here for a normal non-escape        /* 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        char, or after \ that introduces a single character or for an apparent
4816        range that isn't. Only the value 1 matters for class_one_char, so don't        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        increase it if it is already 2 or more ... just in case there's a class
4818        with a zillion characters in it. */        with a zillion characters in it. */
4819    
4820        CLASS_SINGLE_CHARACTER:        CLASS_SINGLE_CHARACTER:
# Line 4499  for (;; ptr++) Line 4833  for (;; ptr++)
4833          {          {
4834          ptr++;          ptr++;
4835          zeroreqchar = reqchar;          zeroreqchar = reqchar;
4836            zeroreqcharflags = reqcharflags;
4837    
4838          if (negate_class)          if (negate_class)
4839            {            {
4840  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
             // FIXMEchpe pcreuint32?  
4841            int d;            int d;
4842  #endif  #endif
4843            if (firstchar == REQ_UNSET) firstchar = REQ_NONE;            if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4844            zerofirstchar = firstchar;            zerofirstchar = firstchar;
4845              zerofirstcharflags = firstcharflags;
4846    
4847            /* For caseless UTF-8 mode when UCP support is available, check            /* For caseless UTF-8 mode when UCP support is available, check
4848            whether this character has more than one other case. If so, generate            whether this character has more than one other case. If so, generate
4849            a special OP_NOTPROP item instead of OP_NOTI. */            a special OP_NOTPROP item instead of OP_NOTI. */
4850    
4851  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4852            if (utf && (options & PCRE_CASELESS) != 0 &&            if (utf && (options & PCRE_CASELESS) != 0 &&
4853                (d = UCD_CASESET(c)) != 0)                (d = UCD_CASESET(c)) != 0)
4854              {              {
4855              *code++ = OP_NOTPROP;              *code++ = OP_NOTPROP;
4856              *code++ = PT_CLIST;              *code++ = PT_CLIST;
4857              *code++ = d;              *code++ = d;
4858              }              }
4859            else            else
4860  #endif  #endif
4861            /* Char has only one other case, or UCP not available */            /* Char has only one other case, or UCP not available */
4862    
# Line 4534  for (;; ptr++) Line 4869  for (;; ptr++)
4869  #endif  #endif
4870                *code++ = c;                *code++ = c;
4871              }              }
4872    
4873            /* We are finished with this character class */            /* We are finished with this character class */
4874    
4875            goto END_CLASS;            goto END_CLASS;
4876            }            }
4877    
# Line 4554  for (;; ptr++) Line 4889  for (;; ptr++)
4889            }            }
4890          goto ONE_CHAR;          goto ONE_CHAR;
4891          }       /* End of 1-char optimization */          }       /* End of 1-char optimization */
4892    
4893        /* There is more than one character in the class, or an XCLASS item        /* There is more than one character in the class, or an XCLASS item
4894        has been generated. Add this character to the class. */        has been generated. Add this character to the class. */
4895    
4896        class_has_8bitchar +=        class_has_8bitchar +=
4897          add_to_class(classbits, &class_uchardata, options, cd, c, c);          add_to_class(classbits, &class_uchardata, options, cd, c, c);
4898        }        }
4899    
# Line 4566  for (;; ptr++) Line 4901  for (;; ptr++)
4901      If we are at the end of an internal nested string, revert to the outer      If we are at the end of an internal nested string, revert to the outer
4902      string. */      string. */
4903    
4904      while (((c = *(++ptr)) != 0 ||      while (((c = *(++ptr)) != CHAR_NULL ||
4905             (nestptr != NULL &&             (nestptr != NULL &&
4906               (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != 0)) &&               (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != CHAR_NULL)) &&
4907             (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));             (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
4908    
4909      /* Check for missing terminating ']' */      /* Check for missing terminating ']' */
4910    
4911      if (c == 0)      if (c == CHAR_NULL)
4912        {        {
4913        *errorcodeptr = ERR6;        *errorcodeptr = ERR6;
4914        goto FAILED;        goto FAILED;
4915        }        }
4916    
4917      /* We will need an XCLASS if data has been placed in class_uchardata. In      /* We will need an XCLASS if data has been placed in class_uchardata. In
4918      the second phase this is a sufficient test. However, in the pre-compile      the second phase this is a sufficient test. However, in the pre-compile
4919      phase, class_uchardata gets emptied to prevent workspace overflow, so it      phase, class_uchardata gets emptied to prevent workspace overflow, so it
4920      only if the very last character in the class needs XCLASS will it contain      only if the very last character in the class needs XCLASS will it contain
4921      anything at this point. For this reason, xclass gets set TRUE above when      anything at this point. For this reason, xclass gets set TRUE above when
4922      uchar_classdata is emptied, and that's why this code is the way it is here      uchar_classdata is emptied, and that's why this code is the way it is here
4923      instead of just doing a test on class_uchardata below. */      instead of just doing a test on class_uchardata below. */
4924    
4925  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4926      if (class_uchardata > class_uchardata_base) xclass = TRUE;      if (class_uchardata > class_uchardata_base) xclass = TRUE;
4927  #endif  #endif
# Line 4595  for (;; ptr++) Line 4930  for (;; ptr++)
4930      setting, whatever the repeat count. Any reqchar setting must remain      setting, whatever the repeat count. Any reqchar setting must remain
4931      unchanged after any kind of repeat. */      unchanged after any kind of repeat. */
4932    
4933      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4934      zerofirstchar = firstchar;      zerofirstchar = firstchar;
4935        zerofirstcharflags = firstcharflags;
4936      zeroreqchar = reqchar;      zeroreqchar = reqchar;
4937        zeroreqcharflags = reqcharflags;
4938    
4939      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
4940      extended class, with its own opcode, unless there was a negated special      extended class, with its own opcode, unless there was a negated special
# Line 4653  for (;; ptr++) Line 4990  for (;; ptr++)
4990        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
4991        }        }
4992      code += 32 / sizeof(pcre_uchar);      code += 32 / sizeof(pcre_uchar);
4993    
4994      END_CLASS:      END_CLASS:
4995      break;      break;
4996    
# Line 4692  for (;; ptr++) Line 5029  for (;; ptr++)
5029      if (repeat_min == 0)      if (repeat_min == 0)
5030        {        {
5031        firstchar = zerofirstchar;    /* Adjust for zero repeat */        firstchar = zerofirstchar;    /* Adjust for zero repeat */
5032          firstcharflags = zerofirstcharflags;
5033        reqchar = zeroreqchar;        /* Ditto */        reqchar = zeroreqchar;        /* Ditto */
5034          reqcharflags = zeroreqcharflags;
5035        }        }
5036    
5037      /* Remember whether this is a variable length repeat */      /* Remember whether this is a variable length repeat */
# Line 4795  for (;; ptr++) Line 5134  for (;; ptr++)
5134          {          {
5135          c = code[-1];          c = code[-1];
5136          if (*previous <= OP_CHARI && repeat_min > 1)          if (*previous <= OP_CHARI && repeat_min > 1)
5137            reqchar = c | req_caseopt | cd->req_varyopt;            {
5138          }            reqchar = c;
5139              reqcharflags = req_caseopt | cd->req_varyopt;
5140        /* If the repetition is unlimited, it pays to see if the next thing on            }
       the line is something that cannot possibly match this character. If so,  
       automatically possessifying this item gains some performance in the case  
       where the match fails. */  
   
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
5141          }          }
5142    
5143        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
# Line 4828  for (;; ptr++) Line 5157  for (;; ptr++)
5157        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
5158        c = *previous;        c = *previous;
5159    
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
   
5160        OUTPUT_SINGLE_REPEAT:        OUTPUT_SINGLE_REPEAT:
5161        if (*previous == OP_PROP || *previous == OP_NOTPROP)        if (*previous == OP_PROP || *previous == OP_NOTPROP)
5162          {          {
# Line 4852  for (;; ptr++) Line 5173  for (;; ptr++)
5173    
5174        if (repeat_max == 0) goto END_REPEAT;        if (repeat_max == 0) goto END_REPEAT;
5175    
       /*--------------------------------------------------------------------*/  
       /* This code is obsolete from release 8.00; the restriction was finally  
       removed: */  
   
       /* All real repeats make it impossible to handle partial matching (maybe  
       one day we will be able to remove this restriction). */  
   
       /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */  
       /*--------------------------------------------------------------------*/  
   
5176        /* Combine the op_type with the repeat_type */        /* Combine the op_type with the repeat_type */
5177    
5178        repeat_type += op_type;        repeat_type += op_type;
# Line 4994  for (;; ptr++) Line 5305  for (;; ptr++)
5305      /* If previous was a character class or a back reference, we put the repeat      /* If previous was a character class or a back reference, we put the repeat
5306      stuff after it, but just skip the item if the repeat was {0,0}. */      stuff after it, but just skip the item if the repeat was {0,0}. */
5307    
5308      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
              *previous == OP_NCLASS ||  
5309  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5310               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
5311  #endif  #endif
5312               *previous == OP_REF ||               *previous == OP_REF   || *previous == OP_REFI ||
5313               *previous == OP_REFI)               *previous == OP_DNREF || *previous == OP_DNREFI)
5314        {        {
5315        if (repeat_max == 0)        if (repeat_max == 0)
5316          {          {
# Line 5008  for (;; ptr++) Line 5318  for (;; ptr++)
5318          goto END_REPEAT;          goto END_REPEAT;
5319          }          }
5320    
       /*--------------------------------------------------------------------*/  
       /* This code is obsolete from release 8.00; the restriction was finally  
       removed: */  
   
       /* All real repeats make it impossible to handle partial matching (maybe  
       one day we will be able to remove this restriction). */  
   
       /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */  
       /*--------------------------------------------------------------------*/  
   
5321        if (repeat_min == 0 && repeat_max == -1)        if (repeat_min == 0 && repeat_max == -1)
5322          *code++ = OP_CRSTAR + repeat_type;          *code++ = OP_CRSTAR + repeat_type;
5323        else if (repeat_min == 1 && repeat_max == -1)        else if (repeat_min == 1 && repeat_max == -1)
# Line 5177  for (;; ptr++) Line 5477  for (;; ptr++)
5477    
5478            else            else
5479              {              {
5480              if (groupsetfirstchar && reqchar < 0) reqchar = firstchar;              if (groupsetfirstchar && reqcharflags < 0)
5481                  {
5482                  reqchar = firstchar;
5483                  reqcharflags = firstcharflags;
5484                  }
5485    
5486              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
5487                {                {
# Line 5356  for (;; ptr++) Line 5660  for (;; ptr++)
5660              pcre_uchar *scode = bracode;              pcre_uchar *scode = bracode;
5661              do              do
5662                {                {
5663                if (could_be_empty_branch(scode, ketcode, utf, cd))                if (could_be_empty_branch(scode, ketcode, utf, cd, NULL))
5664                  {                  {
5665                  *bracode += OP_SBRA - OP_BRA;                  *bracode += OP_SBRA - OP_BRA;
5666                  break;                  break;
# Line 5456  for (;; ptr++) Line 5760  for (;; ptr++)
5760        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
5761          {          {
5762          tempcode += PRIV(OP_lengths)[*tempcode];          tempcode += PRIV(OP_lengths)[*tempcode];
5763  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #ifdef SUPPORT_UTF
5764          if (utf && HAS_EXTRALEN(tempcode[-1]))          if (utf && HAS_EXTRALEN(tempcode[-1]))
5765            tempcode += GET_EXTRALEN(tempcode[-1]);            tempcode += GET_EXTRALEN(tempcode[-1]);
5766  #endif  #endif
# Line 5552  for (;; ptr++) Line 5856  for (;; ptr++)
5856        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
5857          {          {
5858          arg = ++ptr;          arg = ++ptr;
5859          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5860          arglen = (int)(ptr - arg);          arglen = (int)(ptr - arg);
5861          if ((unsigned int)arglen > MAX_MARK)          if ((unsigned int)arglen > MAX_MARK)
5862            {            {
# Line 5597  for (;; ptr++) Line 5901  for (;; ptr++)
5901                (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;                (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5902    
5903              /* Do not set firstchar after *ACCEPT */              /* Do not set firstchar after *ACCEPT */
5904              if (firstchar == REQ_UNSET) firstchar = REQ_NONE;              if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
5905              }              }
5906    
5907            /* Handle other cases with/without an argument */            /* Handle other cases with/without an argument */
# Line 5666  for (;; ptr++) Line 5970  for (;; ptr++)
5970          {          {
5971          case CHAR_NUMBER_SIGN:                 /* Comment; skip to ket */          case CHAR_NUMBER_SIGN:                 /* Comment; skip to ket */
5972          ptr++;          ptr++;
5973          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5974          if (*ptr == 0)          if (*ptr == CHAR_NULL)
5975            {            {
5976            *errorcodeptr = ERR18;            *errorcodeptr = ERR18;
5977            goto FAILED;            goto FAILED;
# Line 5690  for (;; ptr++) Line 5994  for (;; ptr++)
5994          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5995          case CHAR_LEFT_PARENTHESIS:          case CHAR_LEFT_PARENTHESIS:
5996          bravalue = OP_COND;       /* Conditional group */          bravalue = OP_COND;       /* Conditional group */
5997            tempptr = ptr;
5998    
5999          /* A condition can be an assertion, a number (referring to a numbered          /* A condition can be an assertion, a number (referring to a numbered
6000          group), a name (referring to a named group), or 'R', referring to          group), a name (referring to a named group), or 'R', referring to
# Line 5702  for (;; ptr++) Line 6007  for (;; ptr++)
6007          be the recursive thing or the name 'R' (and similarly for 'R' followed          be the recursive thing or the name 'R' (and similarly for 'R' followed
6008          by digits), and (b) a number could be a name that consists of digits.          by digits), and (b) a number could be a name that consists of digits.
6009          In both cases, we look for a name first; if not found, we try the other          In both cases, we look for a name first; if not found, we try the other
6010          cases. */          cases.
6011    
6012            For compatibility with auto-callouts, we allow a callout to be
6013            specified before a condition that is an assertion. First, check for the
6014            syntax of a callout; if found, adjust the temporary pointer that is
6015            used to check for an assertion condition. That's all that is needed! */
6016    
6017            if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
6018              {
6019              for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
6020              if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
6021                tempptr += i + 1;
6022              }
6023    
6024          /* For conditions that are assertions, check the syntax, and then exit          /* For conditions that are assertions, check the syntax, and then exit
6025          the switch. This will take control down to where bracketed groups,          the switch. This will take control down to where bracketed groups,
6026          including assertions, are processed. */          including assertions, are processed. */
6027    
6028          if (ptr[1] == CHAR_QUESTION_MARK && (ptr[2] == CHAR_EQUALS_SIGN ||          if (tempptr[1] == CHAR_QUESTION_MARK &&
6029              ptr[2] == CHAR_EXCLAMATION_MARK || ptr[2] == CHAR_LESS_THAN_SIGN))                (tempptr[2] == CHAR_EQUALS_SIGN ||
6030                   tempptr[2] == CHAR_EXCLAMATION_MARK ||
6031                   tempptr[2] == CHAR_LESS_THAN_SIGN))
6032            break;            break;
6033    
6034          /* Most other conditions use OP_CREF (a couple change to OP_RREF          /* Most other conditions use OP_CREF (a couple change to OP_RREF
# Line 5743  for (;; ptr++) Line 6062  for (;; ptr++)
6062            }            }
6063          else          else
6064            {            {
6065            terminator = 0;            terminator = CHAR_NULL;
6066            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);
6067            }            }
6068    
# Line 5763  for (;; ptr++) Line 6082  for (;; ptr++)
6082          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
6083            {            {
6084            if (recno >= 0)            if (recno >= 0)
6085              recno = (IS_DIGIT(*ptr))? recno * 10 + *ptr - CHAR_0 : -1;              recno = (IS_DIGIT(*ptr))? recno * 10 + (int)(*ptr - CHAR_0) : -1;
6086            ptr++;            ptr++;
6087            }            }
6088          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
6089    
6090          if ((terminator > 0 && *ptr++ != terminator) ||          if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
6091              *ptr++ != CHAR_RIGHT_PARENTHESIS)              *ptr++ != CHAR_RIGHT_PARENTHESIS)
6092            {            {
6093            ptr--;      /* Error offset */            ptr--;      /* Error offset */
# Line 5815  for (;; ptr++) Line 6134  for (;; ptr++)
6134            slot += cd->name_entry_size;            slot += cd->name_entry_size;
6135            }            }
6136    
6137          /* Found a previous named subpattern */          /* Found the named subpattern */
6138    
6139          if (i < cd->names_found)          if (i < cd->names_found)
6140            {            {
# Line 5824  for (;; ptr++) Line 6143  for (;; ptr++)
6143            code[1+LINK_SIZE]++;            code[1+LINK_SIZE]++;
6144            }            }
6145    
6146          /* Search the pattern for a forward reference */          /* If terminator == CHAR_NULL it means that the name followed directly
6147            after the opening parenthesis [e.g. (?(abc)...] and in this case there
6148          else if ((i = find_parens(cd, name, namelen,          are some further alternatives to try. For the cases where terminator !=
6149                          (options & PCRE_EXTENDED) != 0, utf)) > 0)          0 [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have
           {  
           PUT2(code, 2+LINK_SIZE, i);  
           code[1+LINK_SIZE]++;  
           }  
   
         /* If terminator == 0 it means that the name followed directly after  
         the opening parenthesis [e.g. (?(abc)...] and in this case there are  
         some further alternatives to try. For the cases where terminator != 0  
         [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have  
6150          now checked all the possibilities, so give an error. */          now checked all the possibilities, so give an error. */
6151    
6152          else if (terminator != 0)          else if (terminator != CHAR_NULL)
6153            {            {
6154            *errorcodeptr = ERR15;            *errorcodeptr = ERR15;
6155            goto FAILED;            goto FAILED;
# Line 5996  for (;; ptr++) Line 6306  for (;; ptr++)
6306          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
6307          DEFINE_NAME:    /* Come here from (?< handling */          DEFINE_NAME:    /* Come here from (?< handling */
6308          case CHAR_APOSTROPHE:          case CHAR_APOSTROPHE:
6309            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
6310              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
6311            name = ++ptr;
6312    
6313            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
6314            namelen = (int)(ptr - name);
6315    
6316            /* In the pre-compile phase, do a syntax check, remember the longest
6317            name, and then remember the group in a vector, expanding it if
6318            necessary. Duplicates for the same number are skipped; other duplicates
6319            are checked for validity. In the actual compile, there is nothing to
6320            do. */
6321    
6322            if (lengthptr != NULL)
6323            {            {
6324            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?            named_group *ng;
6325              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;            pcre_uint32 number = cd->bracount + 1;
           name = ++ptr;  
6326    
6327            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;            if (*ptr != (pcre_uchar)terminator)
6328            namelen = (int)(ptr - name);              {
6329                *errorcodeptr = ERR42;
6330                goto FAILED;
6331                }
6332    
6333            /* In the pre-compile phase, just do a syntax check. */            if (cd->names_found >= MAX_NAME_COUNT)
6334                {
6335                *errorcodeptr = ERR49;
6336                goto FAILED;
6337                }
6338    
6339            if (lengthptr != NULL)            if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
6340              {              {
6341              if (*ptr != terminator)              cd->name_entry_size = namelen + IMM2_SIZE + 1;
6342                {              if (namelen > MAX_NAME_SIZE)
               *errorcodeptr = ERR42;  
               goto FAILED;  
               }  
             if (cd->names_found >= MAX_NAME_COUNT)  
6343                {                {
6344                *errorcodeptr = ERR49;                *errorcodeptr = ERR48;
6345                goto FAILED;                goto FAILED;
6346                }                }
6347              if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)              }
6348    
6349              /* Scan the list to check for duplicates. For duplicate names, if the
6350              number is the same, break the loop, which causes the name to be
6351              discarded; otherwise, if DUPNAMES is not set, give an error.
6352              If it is set, allow the name with a different number, but continue
6353              scanning in case this is a duplicate with the same number. For
6354              non-duplicate names, give an error if the number is duplicated. */
6355    
6356              ng = cd->named_groups;
6357              for (i = 0; i < cd->names_found; i++, ng++)
6358                {
6359                if (namelen == ng->length &&
6360                    STRNCMP_UC_UC(name, ng->name, namelen) == 0)
6361                {                {
6362                cd->name_entry_size = namelen + IMM2_SIZE + 1;                if (ng->number == number) break;
6363                if (namelen > MAX_NAME_SIZE)                if ((options & PCRE_DUPNAMES) == 0)
6364                  {                  {
6365                  *errorcodeptr = ERR48;                  *errorcodeptr = ERR43;
6366                  goto FAILED;                  goto FAILED;
6367                  }                  }
6368                  cd->dupnames = TRUE;  /* Duplicate names exist */
6369                  }
6370                else if (ng->number == number)
6371                  {
6372                  *errorcodeptr = ERR65;
6373                  goto FAILED;
6374                }                }
6375              }              }
6376    
6377            /* In the real compile, create the entry in the table, maintaining            if (i >= cd->names_found)     /* Not a duplicate with same number */
           alphabetical order. Duplicate names for different numbers are  
           permitted only if PCRE_DUPNAMES is set. Duplicate names fo