/[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 1067 by chpe, Tue Oct 16 15:54:22 2012 UTC revision 1376 by ph10, Sat Oct 12 18:02:11 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 452  static const char error_texts[] = Line 462  static const char error_texts[] =
462    "POSIX collating elements are not supported\0"    "POSIX collating elements are not supported\0"
463    "this version of PCRE is compiled without UTF support\0"    "this version of PCRE is compiled without UTF support\0"
464    "spare error\0"  /** DEAD **/    "spare error\0"  /** DEAD **/
465    "character value in \\x{...} sequence is too large\0"    "character value in \\x{} or \\o{} is too large\0"
466    /* 35 */    /* 35 */
467    "invalid condition (?(0)\0"    "invalid condition (?(0)\0"
468    "\\C not allowed in lookbehind assertion\0"    "\\C not allowed in lookbehind assertion\0"
# 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      "non-hex character in \\x{} (closing brace missing?)\0"
520      /* 80 */
521      "non-octal character in \\o{} (closing brace missing?)\0"
522      "missing opening brace after \\o\0"
523    ;    ;
524    
525  /* 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 659  static const pcre_uint8 ebcdic_chartab[]
659  #endif  #endif
660    
661    
662    /* This table is used to check whether auto-possessification is possible
663    between adjacent character-type opcodes. The left-hand (repeated) opcode is
664    used to select the row, and the right-hand opcode is use to select the column.
665    A value of 1 means that auto-possessification is OK. For example, the second
666    value in the first row means that \D+\d can be turned into \D++\d.
667    
668    The Unicode property types (\P and \p) have to be present to fill out the table
669    because of what their opcode values are, but the table values should always be
670    zero because property types are handled separately in the code. The last four
671    columns apply to items that cannot be repeated, so there is no need to have
672    rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
673    *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
674    
675    #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
676    #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
677    
678    static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
679    /* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
680      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
681      { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
682      { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
683      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
684      { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
685      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
686      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
687      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
688      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
689      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
690      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
691      { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
692      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
693      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
694      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
695      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
696      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
697    };
698    
699    
700    /* This table is used to check whether auto-possessification is possible
701    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
702    left-hand (repeated) opcode is used to select the row, and the right-hand
703    opcode is used to select the column. The values are as follows:
704    
705      0   Always return FALSE (never auto-possessify)
706      1   Character groups are distinct (possessify if both are OP_PROP)
707      2   Check character categories in the same group (general or particular)
708      3   TRUE if the two opcodes are not the same (PROP vs NOTPROP)
709    
710      4   Check left general category vs right particular category
711      5   Check right general category vs left particular category
712    
713      6   Left alphanum vs right general category
714      7   Left space vs right general category
715      8   Left word vs right general category
716    
717      9   Right alphanum vs left general category
718     10   Right space vs left general category
719     11   Right word vs left general category
720    
721     12   Left alphanum vs right particular category
722     13   Left space vs right particular category
723     14   Left word vs right particular category
724    
725     15   Right alphanum vs left particular category
726     16   Right space vs left particular category
727     17   Right word vs left particular category
728    */
729    
730    static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
731    /* ANY LAMP GC  PC  SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
732      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_ANY */
733      { 0,  3,  0,  0,  0,    3,    1,      1,   0,    0,   0 },  /* PT_LAMP */
734      { 0,  0,  2,  4,  0,    9,   10,     10,  11,    0,   0 },  /* PT_GC */
735      { 0,  0,  5,  2,  0,   15,   16,     16,  17,    0,   0 },  /* PT_PC */
736      { 0,  0,  0,  0,  2,    0,    0,      0,   0,    0,   0 },  /* PT_SC */
737      { 0,  3,  6, 12,  0,    3,    1,      1,   0,    0,   0 },  /* PT_ALNUM */
738      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_SPACE */
739      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_PXSPACE */
740      { 0,  0,  8, 14,  0,    0,    1,      1,   3,    0,   0 },  /* PT_WORD */
741      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_CLIST */
742      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   3 }   /* PT_UCNC */
743    };
744    
745    /* This table is used to check whether auto-possessification is possible
746    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
747    specifies a general category and the other specifies a particular category. The
748    row is selected by the general category and the column by the particular
749    category. The value is 1 if the particular category is not part of the general
750    category. */
751    
752    static const pcre_uint8 catposstab[7][30] = {
753    /* 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 */
754      { 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 */
755      { 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 */
756      { 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 */
757      { 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 */
758      { 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 */
759      { 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 */
760      { 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 */
761    };
762    
763    /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
764    a general or particular category. The properties in each row are those
765    that apply to the character set in question. Duplication means that a little
766    unnecessary work is done when checking, but this keeps things much simpler
767    because they can all use the same code. For more details see the comment where
768    this table is used.
769    
770    Note: SPACE and PXSPACE used to be different because Perl excluded VT from
771    "space", but from Perl 5.18 it's included, so both categories are treated the
772    same here. */
773    
774    static const pcre_uint8 posspropstab[3][4] = {
775      { ucp_L, ucp_N, ucp_N, ucp_Nl },  /* ALNUM, 3rd and 4th values redundant */
776      { ucp_Z, ucp_Z, ucp_C, ucp_Cc },  /* SPACE and PXSPACE, 2nd value redundant */
777      { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
778    };
779    
780    
781    
782  /*************************************************  /*************************************************
# Line 665  find_error_text(int n) Line 798  find_error_text(int n)
798  const char *s = error_texts;  const char *s = error_texts;
799  for (; n > 0; n--)  for (; n > 0; n--)
800    {    {
801    while (*s++ != 0) {};    while (*s++ != CHAR_NULL) {};
802    if (*s == 0) return "Error text not found (please report)";    if (*s == CHAR_NULL) return "Error text not found (please report)";
803    }    }
804  return s;  return s;
805  }  }
806    
807    
808    
809  /*************************************************  /*************************************************
810  *           Expand the workspace                 *  *           Expand the workspace                 *
811  *************************************************/  *************************************************/
# Line 749  return (*p == CHAR_RIGHT_CURLY_BRACKET); Line 883  return (*p == CHAR_RIGHT_CURLY_BRACKET);
883  *************************************************/  *************************************************/
884    
885  /* 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
886  positive value for a simple escape such as \n, or 0 for a data character  positive value for a simple escape such as \n, or 0 for a data character which
887  which will be placed in chptr. A backreference to group n is returned as  will be placed in chptr. A backreference to group n is returned as negative n.
888  negative n. When UTF-8 is enabled, a positive value greater than 255 may  When UTF-8 is enabled, a positive value greater than 255 may be returned in
889  be returned in chptr.  chptr. On entry, ptr is pointing at the \. On exit, it is on the final
890  On entry,ptr is pointing at the \. On exit, it is on the final character of the  character of the escape sequence.
 escape sequence.  
891    
892  Arguments:  Arguments:
893    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
894    chptr          points to the data character    chptr          points to a returned data character
895    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
896    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
897    options        the options bits    options        the options bits
# Line 771  Returns:         zero => a data characte Line 904  Returns:         zero => a data characte
904  */  */
905    
906  static int  static int
907  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,  check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
908    int bracount, int options, BOOL isclass)    int bracount, int options, BOOL isclass)
909  {  {
910  /* PCRE_UTF16 has the same value as PCRE_UTF8. */  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
# Line 786  ptr--;                            /* Set Line 919  ptr--;                            /* Set
919    
920  /* 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. */
921    
922  if (c == 0) *errorcodeptr = ERR1;  if (c == CHAR_NULL) *errorcodeptr = ERR1;
923    
924  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
925  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 795  Otherwise further processing may be requ Line 928  Otherwise further processing may be requ
928  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
929  /* Not alphanumeric */  /* Not alphanumeric */
930  else if (c < CHAR_0 || c > CHAR_z) {}  else if (c < CHAR_0 || c > CHAR_z) {}
931  else if ((i = escapes[c - CHAR_0]) != 0) { if (i > 0) c = (pcre_uint32)i; else escape = -i; }  else if ((i = escapes[c - CHAR_0]) != 0)
932      { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
933    
934  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
935  /* Not alphanumeric */  /* Not alphanumeric */
# Line 845  else Line 979  else
979            }            }
980    
981  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
982          if (c > (utf ? 0x10ffff : 0xff))          if (c > (utf ? 0x10ffffU : 0xffU))
983  #elif defined COMPILE_PCRE16  #elif defined COMPILE_PCRE16
984          if (c > (utf ? 0x10ffff : 0xffff))          if (c > (utf ? 0x10ffffU : 0xffffU))
985  #elif defined COMPILE_PCRE32  #elif defined COMPILE_PCRE32
986          if (utf && c > 0x10ffff)          if (utf && c > 0x10ffffU)
987  #endif  #endif
988            {            {
989            *errorcodeptr = ERR76;            *errorcodeptr = ERR76;
# Line 896  else Line 1030  else
1030      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1031        {        {
1032        const pcre_uchar *p;        const pcre_uchar *p;
1033        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++)
1034          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1035        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1036          {          {
1037          escape = ESC_k;          escape = ESC_k;
1038          break;          break;
# Line 961  else Line 1095  else
1095      break;      break;
1096    
1097      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
1098      starting with one that is not zero is not straightforward. By experiment,      starting with one that is not zero is not straightforward. Perl has changed
1099      the way Perl works seems to be as follows:      over the years. Nowadays \g{} for backreferences and \o{} for octal are
1100        recommended to avoid the ambiguities in the old syntax.
1101    
1102      Outside a character class, the digits are read as a decimal number. If the      Outside a character class, the digits are read as a decimal number. If the
1103      number is less than 10, or if there are that many previous extracting      number is less than 8 (used to be 10), or if there are that many previous
1104      left brackets, then it is a back reference. Otherwise, up to three octal      extracting left brackets, then it is a back reference. Otherwise, up to
1105      digits are read to form an escaped byte. Thus \123 is likely to be octal      three octal digits are read to form an escaped byte. Thus \123 is likely to
1106      123 (cf \0123, which is octal 012 followed by the literal 3). If the octal      be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If
1107      value is greater than 377, the least significant 8 bits are taken. Inside a      the octal value is greater than 377, the least significant 8 bits are
1108      character class, \ followed by a digit is always an octal number. */      taken. \8 and \9 are treated as the literal characters 8 and 9.
1109    
1110        Inside a character class, \ followed by a digit is always either a literal
1111        8 or 9 or an octal number. */
1112    
1113      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
1114      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
# Line 997  else Line 1135  else
1135          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1136          break;          break;
1137          }          }
1138        if (s < 10 || s <= bracount)        if (s < 8 || s <= bracount)  /* Check for back reference */
1139          {          {
1140          escape = -s;          escape = -s;
1141          break;          break;
# Line 1005  else Line 1143  else
1143        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
1144        }        }
1145    
1146      /* Handle an octal number following \. If the first digit is 8 or 9, Perl      /* Handle a digit following \ when the number is not a back reference. If
1147      generates a binary zero byte and treats the digit as a following literal.      the first digit is 8 or 9, Perl used to generate a binary zero byte and
1148      Thus we have to pull back the pointer by one. */      then treat the digit as a following literal. At least by Perl 5.18 this
1149        changed so as not to insert the binary zero. */
1150    
1151      if ((c = *ptr) >= CHAR_8)      if ((c = *ptr) >= CHAR_8) break;
1152        {  
1153        ptr--;      /* Fall through with a digit less than 8 */
       c = 0;  
       break;  
       }  
1154    
1155      /* \0 always starts an octal number, but we may drop through to here with a      /* \0 always starts an octal number, but we may drop through to here with a
1156      larger first octal digit. The original code used just to take the least      larger first octal digit. The original code used just to take the least
# Line 1031  else Line 1167  else
1167  #endif  #endif
1168      break;      break;
1169    
1170      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \o is a relatively new Perl feature, supporting a more general way of
1171      than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.      specifying character codes in octal. The only supported form is \o{ddd}. */
1172      If not, { is treated as a data character. */  
1173        case CHAR_o:
1174        if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
1175          {
1176          ptr += 2;
1177          c = 0;
1178          overflow = FALSE;
1179          while (*ptr >= CHAR_0 && *ptr <= CHAR_7)
1180            {
1181            register pcre_uint32 cc = *ptr++;
1182            if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1183    #ifdef COMPILE_PCRE32
1184            if (c >= 0x20000000l) { overflow = TRUE; break; }
1185    #endif
1186            c = (c << 3) + cc - CHAR_0 ;
1187    #if defined COMPILE_PCRE8
1188            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1189    #elif defined COMPILE_PCRE16
1190            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1191    #elif defined COMPILE_PCRE32
1192            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1193    #endif
1194            }
1195          if (overflow)
1196            {
1197            while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++;
1198            *errorcodeptr = ERR34;
1199            }
1200          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1201            {
1202            if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1203            }
1204          else *errorcodeptr = ERR80;
1205          }
1206        break;
1207    
1208        /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
1209        numbers. Otherwise it is a lowercase x letter. */
1210    
1211      case CHAR_x:      case CHAR_x:
1212      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1213        {        {
       /* In JavaScript, \x must be followed by two hexadecimal numbers.  
       Otherwise it is a lowercase x letter. */  
1214        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1215          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1216          {          {
# Line 1056  else Line 1227  else
1227  #endif  #endif
1228            }            }
1229          }          }
1230        break;        }    /* End JavaScript handling */
       }  
1231    
1232      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      /* Handle \x in Perl's style. \x{ddd} is a character number which can be
1233        {      greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
1234        const pcre_uchar *pt = ptr + 2;      digits. If not, { used to be treated as a data character. However, Perl
1235        seems to read hex digits up to the first non-such, and ignore the rest, so
1236        that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
1237        now gives an error. */
1238    
1239        c = 0;      else
1240        overflow = FALSE;        {
1241        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)        if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1242          {          {
1243          register pcre_uint32 cc = *pt++;          ptr += 2;
1244          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          c = 0;
1245            overflow = FALSE;
1246            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)
1247              {
1248              register pcre_uint32 cc = *ptr++;
1249              if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1250    
1251  #ifdef COMPILE_PCRE32  #ifdef COMPILE_PCRE32
1252          if (c >= 0x10000000l) { overflow = TRUE; break; }            if (c >= 0x10000000l) { overflow = TRUE; break; }
1253  #endif  #endif
1254    
1255  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1256          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1257          c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1258  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1259          if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */            if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
1260          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1261  #endif  #endif
1262    
1263  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
1264          if (c > (utf ? 0x10ffff : 0xff)) { overflow = TRUE; break; }            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1265  #elif defined COMPILE_PCRE16  #elif defined COMPILE_PCRE16
1266          if (c > (utf ? 0x10ffff : 0xffff)) { overflow = TRUE; break; }            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1267  #elif defined COMPILE_PCRE32  #elif defined COMPILE_PCRE32
1268          if (utf && c > 0x10ffff) { overflow = TRUE; break; }            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1269  #endif  #endif
1270          }            }
1271    
1272        if (overflow)          if (overflow)
1273          {            {
1274          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++;
1275          *errorcodeptr = ERR34;            *errorcodeptr = ERR34;
1276          }            }
1277    
1278        if (*pt == CHAR_RIGHT_CURLY_BRACKET)          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1279          {            {
1280          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;            if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1281          ptr = pt;            }
         break;  
         }  
1282    
1283        /* If the sequence of hex digits does not end with '}', then we don't          /* If the sequence of hex digits does not end with '}', give an error.
1284        recognize this construct; fall through to the normal \x handling. */          We used just to recognize this construct and fall through to the normal
1285        }          \x handling, but nowadays Perl gives an error, which seems much more
1286            sensible, so we do too. */
1287    
1288      /* Read just a single-byte hex-defined char */          else *errorcodeptr = ERR79;
1289            }   /* End of \x{} processing */
1290    
1291      c = 0;        /* Read a single-byte hex-defined char (up to two hex digits after \x) */
1292      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)  
1293        {        else
1294        pcre_uint32 cc;                          /* Some compilers don't like */          {
1295        cc = *(++ptr);                           /* ++ in initializers */          c = 0;
1296            while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1297              {
1298              pcre_uint32 cc;                          /* Some compilers don't like */
1299              cc = *(++ptr);                           /* ++ in initializers */
1300  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1301        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
1302        c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1303  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1304        if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */            if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */
1305        c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1306  #endif  #endif
1307        }            }
1308            }     /* End of \xdd handling */
1309          }       /* End of Perl-style \x handling */
1310      break;      break;
1311    
1312      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
# Line 1132  else Line 1316  else
1316    
1317      case CHAR_c:      case CHAR_c:
1318      c = *(++ptr);      c = *(++ptr);
1319      if (c == 0)      if (c == CHAR_NULL)
1320        {        {
1321        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
1322        break;        break;
# Line 1188  if ((options & PCRE_UCP) != 0 && escape Line 1372  if ((options & PCRE_UCP) != 0 && escape
1372  return escape;  return escape;
1373  }  }
1374    
1375    
1376    
1377  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
1378  /*************************************************  /*************************************************
1379  *               Handle \P and \p                 *  *               Handle \P and \p                 *
# Line 1201  escape sequence. Line 1387  escape sequence.
1387  Argument:  Argument:
1388    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
1389    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
1390    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
1391      pdataptr       points to an unsigned int that is set to the detailed property value
1392    errorcodeptr   points to the error code variable    errorcodeptr   points to the error code variable
1393    
1394  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
1395  */  */
1396    
1397  static int  static BOOL
1398  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1399      unsigned int *pdataptr, int *errorcodeptr)
1400  {  {
1401  pcre_uchar c;  pcre_uchar c;
1402  int i, bot, top;  int i, bot, top;
# Line 1216  const pcre_uchar *ptr = *ptrptr; Line 1404  const pcre_uchar *ptr = *ptrptr;
1404  pcre_uchar name[32];  pcre_uchar name[32];
1405    
1406  c = *(++ptr);  c = *(++ptr);
1407  if (c == 0) goto ERROR_RETURN;  if (c == CHAR_NULL) goto ERROR_RETURN;
1408    
1409  *negptr = FALSE;  *negptr = FALSE;
1410    
# Line 1233  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1421  if (c == CHAR_LEFT_CURLY_BRACKET)
1421    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1422      {      {
1423      c = *(++ptr);      c = *(++ptr);
1424      if (c == 0) goto ERROR_RETURN;      if (c == CHAR_NULL) goto ERROR_RETURN;
1425      if (c == CHAR_RIGHT_CURLY_BRACKET) break;      if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1426      name[i] = c;      name[i] = c;
1427      }      }
# Line 1263  while (bot < top) Line 1451  while (bot < top)
1451    r = 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);
1452    if (r == 0)    if (r == 0)
1453      {      {
1454      *dptr = PRIV(utt)[i].value;      *ptypeptr = PRIV(utt)[i].type;
1455      return PRIV(utt)[i].type;      *pdataptr = PRIV(utt)[i].value;
1456        return TRUE;
1457      }      }
1458    if (r > 0) bot = i + 1; else top = i;    if (r > 0) bot = i + 1; else top = i;
1459    }    }
1460    
1461  *errorcodeptr = ERR47;  *errorcodeptr = ERR47;
1462  *ptrptr = ptr;  *ptrptr = ptr;
1463  return -1;  return FALSE;
1464    
1465  ERROR_RETURN:  ERROR_RETURN:
1466  *errorcodeptr = ERR46;  *errorcodeptr = ERR46;
1467  *ptrptr = ptr;  *ptrptr = ptr;
1468  return -1;  return FALSE;
1469  }  }
1470  #endif  #endif
1471    
1472    
1473    
   
1474  /*************************************************  /*************************************************
1475  *         Read repeat counts                     *  *         Read repeat counts                     *
1476  *************************************************/  *************************************************/
# Line 1351  return p; Line 1539  return p;
1539    
1540    
1541  /*************************************************  /*************************************************
 *  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)  
       {  
       pcre_uchar 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 == (int)(ptr - thisname) &&  
           STRNCMP_UC_UC(name, thisname, (unsigned int)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;  
 }  
   
   
   
   
 /*************************************************  
1542  *      Find first significant op code            *  *      Find first significant op code            *
1543  *************************************************/  *************************************************/
1544    
# Line 1684  for (;;) Line 1577  for (;;)
1577    
1578      case OP_CALLOUT:      case OP_CALLOUT:
1579      case OP_CREF:      case OP_CREF:
1580      case OP_NCREF:      case OP_DNCREF:
1581      case OP_RREF:      case OP_RREF:
1582      case OP_NRREF:      case OP_DNRREF:
1583      case OP_DEF:      case OP_DEF:
1584      code += PRIV(OP_lengths)[*code];      code += PRIV(OP_lengths)[*code];
1585      break;      break;
# Line 1700  for (;;) Line 1593  for (;;)
1593    
1594    
1595    
   
1596  /*************************************************  /*************************************************
1597  *        Find the fixed length of a branch       *  *        Find the fixed length of a branch       *
1598  *************************************************/  *************************************************/
# Line 1744  for (;;) Line 1636  for (;;)
1636    {    {
1637    int d;    int d;
1638    pcre_uchar *ce, *cs;    pcre_uchar *ce, *cs;
1639    register int op = *cc;    register pcre_uchar op = *cc;
1640    
1641    switch (op)    switch (op)
1642      {      {
# Line 1824  for (;;) Line 1716  for (;;)
1716      case OP_COMMIT:      case OP_COMMIT:
1717      case OP_CREF:      case OP_CREF:
1718      case OP_DEF:      case OP_DEF:
1719        case OP_DNCREF:
1720        case OP_DNRREF:
1721      case OP_DOLL:      case OP_DOLL:
1722      case OP_DOLLM:      case OP_DOLLM:
1723      case OP_EOD:      case OP_EOD:
1724      case OP_EODN:      case OP_EODN:
1725      case OP_FAIL:      case OP_FAIL:
     case OP_NCREF:  
     case OP_NRREF:  
1726      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1727      case OP_PRUNE:      case OP_PRUNE:
1728      case OP_REVERSE:      case OP_REVERSE:
# Line 1852  for (;;) Line 1744  for (;;)
1744      case OP_NOTI:      case OP_NOTI:
1745      branchlength++;      branchlength++;
1746      cc += 2;      cc += 2;
1747  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #ifdef SUPPORT_UTF
1748      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1749  #endif  #endif
1750      break;      break;
# Line 1864  for (;;) Line 1756  for (;;)
1756      case OP_EXACTI:      case OP_EXACTI:
1757      case OP_NOTEXACT:      case OP_NOTEXACT:
1758      case OP_NOTEXACTI:      case OP_NOTEXACTI:
1759      branchlength += GET2(cc,1);      branchlength += (int)GET2(cc,1);
1760      cc += 2 + IMM2_SIZE;      cc += 2 + IMM2_SIZE;
1761  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #ifdef SUPPORT_UTF
1762      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1763  #endif  #endif
1764      break;      break;
1765    
1766      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1767      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1768      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)
1769        cc += 2;        cc += 2;
1770      cc += 1 + IMM2_SIZE + 1;      cc += 1 + IMM2_SIZE + 1;
1771      break;      break;
# Line 1909  for (;;) Line 1801  for (;;)
1801    
1802      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1803    
 #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  
   
1804      case OP_CLASS:      case OP_CLASS:
1805      case OP_NCLASS:      case OP_NCLASS:
1806    #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1807        case OP_XCLASS:
1808        /* The original code caused an unsigned overflow in 64 bit systems,
1809        so now we use a conditional statement. */
1810        if (op == OP_XCLASS)
1811          cc += GET(cc, 1);
1812        else
1813          cc += PRIV(OP_lengths)[OP_CLASS];
1814    #else
1815      cc += PRIV(OP_lengths)[OP_CLASS];      cc += PRIV(OP_lengths)[OP_CLASS];
1816    #endif
1817    
1818      switch (*cc)      switch (*cc)
1819        {        {
# Line 1932  for (;;) Line 1828  for (;;)
1828        case OP_CRRANGE:        case OP_CRRANGE:
1829        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1830        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1831        branchlength += GET2(cc,1);        branchlength += (int)GET2(cc,1);
1832        cc += 1 + 2 * IMM2_SIZE;        cc += 1 + 2 * IMM2_SIZE;
1833        break;        break;
1834    
# Line 1999  for (;;) Line 1895  for (;;)
1895      case OP_QUERYI:      case OP_QUERYI:
1896      case OP_REF:      case OP_REF:
1897      case OP_REFI:      case OP_REFI:
1898        case OP_DNREF:
1899        case OP_DNREFI:
1900      case OP_SBRA:      case OP_SBRA:
1901      case OP_SBRAPOS:      case OP_SBRAPOS:
1902      case OP_SCBRA:      case OP_SCBRA:
# Line 2035  for (;;) Line 1933  for (;;)
1933    
1934    
1935    
   
1936  /*************************************************  /*************************************************
1937  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
1938  *************************************************/  *************************************************/
# Line 2059  PRIV(find_bracket)(const pcre_uchar *cod Line 1956  PRIV(find_bracket)(const pcre_uchar *cod
1956  {  {
1957  for (;;)  for (;;)
1958    {    {
1959    register int c = *code;    register pcre_uchar c = *code;
1960    
1961    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1962    
# Line 2082  for (;;) Line 1979  for (;;)
1979    else if (c == OP_CBRA || c == OP_SCBRA ||    else if (c == OP_CBRA || c == OP_SCBRA ||
1980             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
1981      {      {
1982      int n = GET2(code, 1+LINK_SIZE);      int n = (int)GET2(code, 1+LINK_SIZE);
1983      if (n == number) return (pcre_uchar *)code;      if (n == number) return (pcre_uchar *)code;
1984      code += PRIV(OP_lengths)[c];      code += PRIV(OP_lengths)[c];
1985      }      }
# Line 2112  for (;;) Line 2009  for (;;)
2009        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2010        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2011        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
2012        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)
2013          code += 2;          code += 2;
2014        break;        break;
2015    
2016        case OP_MARK:        case OP_MARK:
2017        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2018        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
2019        case OP_THEN_ARG:        case OP_THEN_ARG:
2020        code += code[1];        code += code[1];
2021        break;        break;
# Line 2197  find_recurse(const pcre_uchar *code, BOO Line 2091  find_recurse(const pcre_uchar *code, BOO
2091  {  {
2092  for (;;)  for (;;)
2093    {    {
2094    register int c = *code;    register pcre_uchar c = *code;
2095    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
2096    if (c == OP_RECURSE) return code;    if (c == OP_RECURSE) return code;
2097    
# Line 2232  for (;;) Line 2126  for (;;)
2126        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2127        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2128        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2129        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)
2130          code += 2;          code += 2;
2131        break;        break;
2132    
2133        case OP_MARK:        case OP_MARK:
2134        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2135        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
2136        case OP_THEN_ARG:        case OP_THEN_ARG:
2137        code += code[1];        code += code[1];
2138        break;        break;
# Line 2343  Arguments: Line 2234  Arguments:
2234    endcode     points to where to stop    endcode     points to where to stop
2235    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2236    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2237      recurses    chain of recurse_check to catch mutual recursion
2238    
2239  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2240  */  */
2241    
2242    typedef struct recurse_check {
2243      struct recurse_check *prev;
2244      const pcre_uchar *group;
2245    } recurse_check;
2246    
2247  static BOOL  static BOOL
2248  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2249    BOOL utf, compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2250  {  {
2251  register int c;  register pcre_uchar c;
2252    recurse_check this_recurse;
2253    
2254  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2255       code < endcode;       code < endcode;
2256       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
# Line 2379  for (code = first_significant_code(code Line 2278  for (code = first_significant_code(code
2278    
2279    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2280      {      {
2281      const pcre_uchar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2282      BOOL empty_branch;      BOOL empty_branch;
2283    
2284      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2285        when called to scan a completed pattern by setting cd->start_workspace to
2286        NULL. */
2287    
2288        if (cd->start_workspace != NULL)
2289          {
2290          const pcre_uchar *tcode;
2291          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2292            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2293          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2294          }
2295    
2296        /* If we are scanning a completed pattern, there are no forward references
2297        and all groups are complete. We need to detect whether this is a recursive
2298        call, as otherwise there will be an infinite loop. If it is a recursion,
2299        just skip over it. Simple recursions are easily detected. For mutual
2300        recursions we keep a chain on the stack. */
2301    
2302      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)      else
2303        if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;        {
2304          recurse_check *r = recurses;
2305          const pcre_uchar *endgroup = scode;
2306    
2307      /* Not a forward reference, test for completed backward reference */        do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2308          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2309    
2310      empty_branch = FALSE;        for (r = recurses; r != NULL; r = r->prev)
2311      scode = cd->start_code + GET(code, 1);          if (r->group == scode) break;
2312      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */        if (r != NULL) continue;   /* Mutual recursion */
2313          }
2314    
2315      /* Completed backwards reference */      /* Completed reference; scan the referenced group, remembering it on the
2316        stack chain to detect mutual recursions. */
2317    
2318        empty_branch = FALSE;
2319        this_recurse.prev = recurses;
2320        this_recurse.group = scode;
2321    
2322      do      do
2323        {        {
2324        if (could_be_empty_branch(scode, endcode, utf, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2325          {          {
2326          empty_branch = TRUE;          empty_branch = TRUE;
2327          break;          break;
# Line 2453  for (code = first_significant_code(code Line 2377  for (code = first_significant_code(code
2377        empty_branch = FALSE;        empty_branch = FALSE;
2378        do        do
2379          {          {
2380          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2381            empty_branch = TRUE;            empty_branch = TRUE;
2382          code += GET(code, 1);          code += GET(code, 1);
2383          }          }
# Line 2511  for (code = first_significant_code(code Line 2435  for (code = first_significant_code(code
2435    
2436      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2437    
2438      case OP_PROP:      case OP_ANY:
2439      case OP_NOTPROP:      case OP_ALLANY:
2440        case OP_ANYBYTE:
2441    
2442        case OP_PROP:
2443        case OP_NOTPROP:
2444        case OP_ANYNL:
2445    
2446        case OP_NOT_HSPACE:
2447        case OP_HSPACE:
2448        case OP_NOT_VSPACE:
2449        case OP_VSPACE:
2450      case OP_EXTUNI:      case OP_EXTUNI:
2451    
2452      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2453      case OP_DIGIT:      case OP_DIGIT:
2454      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2455      case OP_WHITESPACE:      case OP_WHITESPACE:
2456      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2457      case OP_WORDCHAR:      case OP_WORDCHAR:
2458      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2459      case OP_CHAR:      case OP_CHAR:
2460      case OP_CHARI:      case OP_CHARI:
2461      case OP_NOT:      case OP_NOT:
2462      case OP_NOTI:      case OP_NOTI:
2463    
2464      case OP_PLUS:      case OP_PLUS:
2465        case OP_PLUSI:
2466      case OP_MINPLUS:      case OP_MINPLUS:
2467      case OP_POSPLUS:      case OP_MINPLUSI:
2468      case OP_EXACT:  
2469      case OP_NOTPLUS:      case OP_NOTPLUS:
2470        case OP_NOTPLUSI:
2471      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2472        case OP_NOTMINPLUSI:
2473    
2474        case OP_POSPLUS:
2475        case OP_POSPLUSI:
2476      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2477        case OP_NOTPOSPLUSI:
2478    
2479        case OP_EXACT:
2480        case OP_EXACTI:
2481      case OP_NOTEXACT:      case OP_NOTEXACT:
2482        case OP_NOTEXACTI:
2483    
2484      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2485      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2486      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2487      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2488    
2489      return FALSE;      return FALSE;
2490    
2491      /* 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 2558  for (code = first_significant_code(code Line 2505  for (code = first_significant_code(code
2505      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2506      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2507      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2508      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)
2509        code += 2;        code += 2;
2510      break;      break;
2511    
# Line 2572  for (code = first_significant_code(code Line 2519  for (code = first_significant_code(code
2519      return TRUE;      return TRUE;
2520    
2521      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2522      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2523        followed by a multibyte character. */
2524    
2525  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2526      case OP_STAR:      case OP_STAR:
2527      case OP_STARI:      case OP_STARI:
2528        case OP_NOTSTAR:
2529        case OP_NOTSTARI:
2530    
2531      case OP_MINSTAR:      case OP_MINSTAR:
2532      case OP_MINSTARI:      case OP_MINSTARI:
2533        case OP_NOTMINSTAR:
2534        case OP_NOTMINSTARI:
2535    
2536      case OP_POSSTAR:      case OP_POSSTAR:
2537      case OP_POSSTARI:      case OP_POSSTARI:
2538        case OP_NOTPOSSTAR:
2539        case OP_NOTPOSSTARI:
2540    
2541      case OP_QUERY:      case OP_QUERY:
2542      case OP_QUERYI:      case OP_QUERYI:
2543        case OP_NOTQUERY:
2544        case OP_NOTQUERYI:
2545    
2546      case OP_MINQUERY:      case OP_MINQUERY:
2547      case OP_MINQUERYI:      case OP_MINQUERYI:
2548        case OP_NOTMINQUERY:
2549        case OP_NOTMINQUERYI:
2550    
2551      case OP_POSQUERY:      case OP_POSQUERY:
2552      case OP_POSQUERYI:      case OP_POSQUERYI:
2553        case OP_NOTPOSQUERY:
2554        case OP_NOTPOSQUERYI:
2555    
2556      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2557      break;      break;
2558    
2559      case OP_UPTO:      case OP_UPTO:
2560      case OP_UPTOI:      case OP_UPTOI:
2561        case OP_NOTUPTO:
2562        case OP_NOTUPTOI:
2563    
2564      case OP_MINUPTO:      case OP_MINUPTO:
2565      case OP_MINUPTOI:      case OP_MINUPTOI:
2566        case OP_NOTMINUPTO:
2567        case OP_NOTMINUPTOI:
2568    
2569      case OP_POSUPTO:      case OP_POSUPTO:
2570      case OP_POSUPTOI:      case OP_POSUPTOI:
2571        case OP_NOTPOSUPTO:
2572        case OP_NOTPOSUPTOI:
2573    
2574      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]);
2575      break;      break;
2576  #endif  #endif
# Line 2606  for (code = first_significant_code(code Line 2581  for (code = first_significant_code(code
2581      case OP_MARK:      case OP_MARK:
2582      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2583      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     code += code[1];  
     break;  
   
2584      case OP_THEN_ARG:      case OP_THEN_ARG:
2585      code += code[1];      code += code[1];
2586      break;      break;
# Line 2652  could_be_empty(const pcre_uchar *code, c Line 2624  could_be_empty(const pcre_uchar *code, c
2624  {  {
2625  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2626    {    {
2627    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2628      return FALSE;      return FALSE;
2629    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2630    }    }
# Line 2662  return TRUE; Line 2634  return TRUE;
2634    
2635    
2636  /*************************************************  /*************************************************
2637  *           Check for POSIX class syntax         *  *        Base opcode of repeated opcodes         *
2638  *************************************************/  *************************************************/
2639    
2640  /* This function is called when the sequence "[:" or "[." or "[=" is  /* Returns the base opcode for repeated single character type opcodes. If the
2641  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.  
   
 Originally, this function only recognized a sequence of letters between the  
 terminators, but it seems that Perl recognizes any sequence of characters,  
 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:].  
   
 The problem in trying to be exactly like Perl is in the handling of escapes. We  
 have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX  
 class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code  
 below handles the special case of \], but does not try to do any other escape  
 processing. This makes it different from Perl for cases such as [:l\ower:]  
 where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize  
 "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,  
 I think.  
   
 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.  
   
 In Perl, unescaped square brackets may also appear as part of class names. For  
 example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for  
 [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not  
 seem right at all. PCRE does not allow closing square brackets in POSIX class  
 names.  
   
 Arguments:  
   ptr      pointer to the initial [  
   endptr   where to return the end pointer  
2642    
2643  Returns:   TRUE or FALSE  Arguments:  c opcode
2644    Returns:    base opcode for the type
2645  */  */
2646    
2647  static BOOL  static pcre_uchar
2648  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  get_repeat_base(pcre_uchar c)
2649  {  {
2650  int terminator;          /* Don't combine these lines; the Solaris cc */  return (c > OP_TYPEPOSUPTO)? c :
2651  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */         (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2652  for (++ptr; *ptr != 0; ptr++)         (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2653    {         (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2654    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)         (c >= OP_STARI)?      OP_STARI :
2655      ptr++;                               OP_STAR;
   else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;  
   else  
     {  
     if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)  
       {  
       *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;  
     }  
   }  
 return FALSE;  
2656  }  }
2657    
2658    
2659    
2660    #ifdef SUPPORT_UCP
2661  /*************************************************  /*************************************************
2662  *          Check POSIX class name                *  *        Check a character and a property        *
2663  *************************************************/  *************************************************/
2664    
2665  /* This function is called to check the name given in a POSIX-style class entry  /* This function is called by check_auto_possessive() when a property item
2666  such as [:alnum:].  is adjacent to a fixed character.
2667    
2668  Arguments:  Arguments:
2669    ptr        points to the first letter    c            the character
2670    len        the length of the name    ptype        the property type
2671      pdata        the data for the type
2672      negated      TRUE if it's a negated property (\P or \p{^)
2673    
2674  Returns:     a value representing the name, or -1 if unknown  Returns:       TRUE if auto-possessifying is OK
2675  */  */
2676    
2677  static int  static BOOL
2678  check_posix_name(const pcre_uchar *ptr, int len)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2679      BOOL negated)
2680  {  {
2681  const char *pn = posix_names;  const pcre_uint32 *p;
2682  register int yield = 0;  const ucd_record *prop = GET_UCD(c);
 while (posix_name_lengths[yield] != 0)  
   {  
   if (len == posix_name_lengths[yield] &&  
     STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;  
   pn += posix_name_lengths[yield] + 1;  
   yield++;  
   }  
 return -1;  
 }  
   
   
 /*************************************************  
 *    Adjust OP_RECURSE items in repeated group   *  
 *************************************************/  
2683    
2684  /* OP_RECURSE items contain an offset from the start of the regex to the group  switch(ptype)
2685  that is referenced. This means that groups can be replicated for fixed    {
2686  repetition simply by copying (because the recursion is allowed to refer to    case PT_LAMP:
2687  earlier groups that are outside the current group). However, when a group is    return (prop->chartype == ucp_Lu ||
2688  optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is            prop->chartype == ucp_Ll ||
2689  inserted before it, after it has been compiled. This means that any OP_RECURSE            prop->chartype == ucp_Lt) == negated;
 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.  
2690    
2691  This function has been extended with the possibility of forward references for    case PT_GC:
2692  recursions and subroutine calls. It must also check the list of such references    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
 for the group we are dealing with. If it finds that one of the recursions in  
 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).  
2693    
2694  Arguments:    case PT_PC:
2695    group      points to the start of the group    return (pdata == prop->chartype) == negated;
   adjust     the amount by which the group is to be moved  
   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  
2696    
2697  Returns:     nothing    case PT_SC:
2698  */    return (pdata == prop->script) == negated;
2699    
2700  static void    /* These are specials */
 adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,  
   pcre_uchar *save_hwm)  
 {  
 pcre_uchar *ptr = group;  
2701    
2702  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)    case PT_ALNUM:
2703    {    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2704    int offset;            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
   pcre_uchar *hc;  
2705    
2706    /* See if this recursion is on the forward reference list. If so, adjust the    /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2707    reference. */    means that Perl space and POSIX space are now identical. PCRE was changed
2708      at release 8.34. */
2709    
2710    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    case PT_SPACE:    /* Perl space */
2711      case PT_PXSPACE:  /* POSIX space */
2712      switch(c)
2713      {      {
2714      offset = GET(hc, 0);      HSPACE_CASES:
2715      if (cd->start_code + offset == ptr + 1)      VSPACE_CASES:
2716        {      return negated;
2717        PUT(hc, 0, offset + adjust);  
2718        break;      default:
2719        }      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
2720      }      }
2721      break;  /* Control never reaches here */
2722    
2723    /* Otherwise, adjust the recursion offset if it's after the start of this    case PT_WORD:
2724    group. */    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2725              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2726              c == CHAR_UNDERSCORE) == negated;
2727    
2728    if (hc >= cd->hwm)    case PT_CLIST:
2729      p = PRIV(ucd_caseless_sets) + prop->caseset;
2730      for (;;)
2731      {      {
2732      offset = GET(ptr, 1);      if (c < *p) return !negated;
2733      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      if (c == *p++) return negated;
2734      }      }
2735      break;  /* Control never reaches here */
   ptr += 1 + LINK_SIZE;  
2736    }    }
2737    
2738    return FALSE;
2739  }  }
2740    #endif  /* SUPPORT_UCP */
2741    
2742    
2743    
2744  /*************************************************  /*************************************************
2745  *        Insert an automatic callout point       *  *        Fill the character property list        *
2746  *************************************************/  *************************************************/
2747    
2748  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert  /* Checks whether the code points to an opcode that can take part in auto-
2749  callout points before each pattern item.  possessification, and if so, fills a list with its properties.
2750    
2751  Arguments:  Arguments:
2752    code           current code pointer    code        points to start of expression
2753    ptr            current pattern pointer    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2754    cd             pointers to tables etc    fcc         points to case-flipping table
2755      list        points to output list
2756                  list[0] will be filled with the opcode
2757                  list[1] will be non-zero if this opcode
2758                    can match an empty character string
2759                  list[2..7] depends on the opcode
2760    
2761  Returns:         new code pointer  Returns:      points to the start of the next opcode if *code is accepted
2762                  NULL if *code is not accepted
2763  */  */
2764    
2765  static pcre_uchar *  static const pcre_uchar *
2766  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)  get_chr_property_list(const pcre_uchar *code, BOOL utf,
2767      const pcre_uint8 *fcc, pcre_uint32 *list)
2768  {  {
2769  *code++ = OP_CALLOUT;  pcre_uchar c = *code;
2770  *code++ = 255;  const pcre_uchar *end;
2771  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  const pcre_uint32 *clist_src;
2772  PUT(code, LINK_SIZE, 0);                       /* Default length */  pcre_uint32 *clist_dest;
2773  return code + 2 * LINK_SIZE;  pcre_uint32 chr;
2774  }  pcre_uchar base;
2775    
2776    list[0] = c;
2777    list[1] = FALSE;
2778    code++;
2779    
2780    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2781      {
2782      base = get_repeat_base(c);
2783      c -= (base - OP_STAR);
2784    
2785  /*************************************************    if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2786  *         Complete a callout item                *      code += IMM2_SIZE;
 *************************************************/  
2787    
2788  /* A callout item contains the length of the next item in the pattern, which    list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
 we can't fill in till after we have reached the relevant point. This is used  
 for both automatic and manual callouts.  
2789    
2790  Arguments:    switch(base)
2791    previous_callout   points to previous callout item      {
2792    ptr                current pattern pointer      case OP_STAR:
2793    cd                 pointers to tables etc      list[0] = OP_CHAR;
2794        break;
2795    
2796  Returns:             nothing      case OP_STARI:
2797  */      list[0] = OP_CHARI;
2798        break;
2799    
2800  static void      case OP_NOTSTAR:
2801  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)      list[0] = OP_NOT;
2802  {      break;
 int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  
 PUT(previous_callout, 2 + LINK_SIZE, length);  
 }  
2803    
2804        case OP_NOTSTARI:
2805        list[0] = OP_NOTI;
2806        break;
2807    
2808        case OP_TYPESTAR:
2809        list[0] = *code;
2810        code++;
2811        break;
2812        }
2813      c = list[0];
2814      }
2815    
2816  #ifdef SUPPORT_UCP  switch(c)
2817  /*************************************************    {
2818  *           Get othercase range                  *    case OP_NOT_DIGIT:
2819  *************************************************/    case OP_DIGIT:
2820      case OP_NOT_WHITESPACE:
2821      case OP_WHITESPACE:
2822      case OP_NOT_WORDCHAR:
2823      case OP_WORDCHAR:
2824      case OP_ANY:
2825      case OP_ALLANY:
2826      case OP_ANYNL:
2827      case OP_NOT_HSPACE:
2828      case OP_HSPACE:
2829      case OP_NOT_VSPACE:
2830      case OP_VSPACE:
2831      case OP_EXTUNI:
2832      case OP_EODN:
2833      case OP_EOD:
2834      case OP_DOLL:
2835      case OP_DOLLM:
2836      return code;
2837    
2838  /* This function is passed the start and end of a class range, in UTF-8 mode    case OP_CHAR:
2839  with UCP support. It searches up the characters, looking for ranges of    case OP_NOT:
2840  characters in the "other" case. Each call returns the next one, updating the    GETCHARINCTEST(chr, code);
2841  start address. A character with multiple other cases is returned on its own    list[2] = chr;
2842  with a special return value.    list[3] = NOTACHAR;
2843      return code;
2844    
2845  Arguments:    case OP_CHARI:
2846    cptr        points to starting character value; updated    case OP_NOTI:
2847    d           end value    list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2848    ocptr       where to put start of othercase range    GETCHARINCTEST(chr, code);
2849    odptr       where to put end of othercase range    list[2] = chr;
2850    
2851  Yield:        -1 when no more  #ifdef SUPPORT_UCP
2852                 0 when a range is returned    if (chr < 128 || (chr < 256 && !utf))
2853                >0 the CASESET offset for char with multiple other cases      list[3] = fcc[chr];
2854                  in this case, ocptr contains the original    else
2855  */      list[3] = UCD_OTHERCASE(chr);
2856    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2857      list[3] = (chr < 256) ? fcc[chr] : chr;
2858    #else
2859      list[3] = fcc[chr];
2860    #endif
2861    
2862  static int    /* The othercase might be the same value. */
 get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,  
   pcre_uint32 *odptr)  
 {  
 pcre_uint32 c, othercase, next;  
 int co;  
2863    
2864  /* Find the first character that has an other case. If it has multiple other    if (chr == list[3])
2865  cases, return its case offset value. */      list[3] = NOTACHAR;
2866      else
2867        list[4] = NOTACHAR;
2868      return code;
2869    
2870  for (c = *cptr; c <= d; c++)  #ifdef SUPPORT_UCP
2871    {    case OP_PROP:
2872    if ((co = UCD_CASESET(c)) != 0)    case OP_NOTPROP:
2873      if (code[0] != PT_CLIST)
2874      {      {
2875      *ocptr = c++;   /* Character that has the set */      list[2] = code[0];
2876      *cptr = c;      /* Rest of input range */      list[3] = code[1];
2877      return co;      return code + 2;
2878      }      }
   if ((othercase = UCD_OTHERCASE(c)) != c) break;  
   }  
2879    
2880  if (c > d) return -1;  /* Reached end of range */    /* Convert only if we have enough space. */
2881    
2882  *ocptr = othercase;    clist_src = PRIV(ucd_caseless_sets) + code[1];
2883  next = othercase + 1;    clist_dest = list + 2;
2884      code += 2;
2885    
2886  for (++c; c <= d; c++)    do {
2887    {       if (clist_dest >= list + 8)
2888    if (UCD_OTHERCASE(c) != next) break;         {
2889    next++;         /* Early return if there is not enough space. This should never
2890    }         happen, since all clists are shorter than 5 character now. */
2891           list[2] = code[0];
2892           list[3] = code[1];
2893           return code;
2894           }
2895         *clist_dest++ = *clist_src;
2896         }
2897      while(*clist_src++ != NOTACHAR);
2898    
2899  *odptr = next - 1;     /* End of othercase range */    /* All characters are stored. The terminating NOTACHAR
2900  *cptr = c;             /* Rest of input range */    is copied form the clist itself. */
2901  return 0;  
2902      list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2903      return code;
2904    #endif
2905    
2906      case OP_NCLASS:
2907      case OP_CLASS:
2908    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2909      case OP_XCLASS:
2910    
2911      if (c == OP_XCLASS)
2912        end = code + GET(code, 0);
2913      else
2914    #endif
2915        end = code + 32 / sizeof(pcre_uchar);
2916    
2917      switch(*end)
2918        {
2919        case OP_CRSTAR:
2920        case OP_CRMINSTAR:
2921        case OP_CRQUERY:
2922        case OP_CRMINQUERY:
2923        list[1] = TRUE;
2924        end++;
2925        break;
2926    
2927        case OP_CRRANGE:
2928        case OP_CRMINRANGE:
2929        list[1] = (GET2(end, 1) == 0);
2930        end += 1 + 2 * IMM2_SIZE;
2931        break;
2932        }
2933      list[2] = end - code;
2934      return end;
2935      }
2936    return NULL;    /* Opcode not accepted */
2937  }  }
2938    
2939    
2940    
2941  /*************************************************  /*************************************************
2942  *        Check a character and a property        *  *    Scan further character sets for match       *
2943  *************************************************/  *************************************************/
2944    
2945  /* This function is called by check_auto_possessive() when a property item  /* Checks whether the base and the current opcode have a common character, in
2946  is adjacent to a fixed character.  which case the base cannot be possessified.
2947    
2948  Arguments:  Arguments:
2949    c            the character    code        points to the byte code
2950    ptype        the property type    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2951    pdata        the data for the type    cd          static compile data
2952    negated      TRUE if it's a negated property (\P or \p{^)    base_list   the data list of the base opcode
2953    
2954  Returns:       TRUE if auto-possessifying is OK  Returns:      TRUE if the auto-possessification is possible
2955  */  */
2956    
2957  static BOOL  static BOOL
2958  check_char_prop(pcre_uint32 c, int ptype, int pdata, BOOL negated)  compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2959      const pcre_uint32* base_list)
2960  {  {
2961  #ifdef SUPPORT_UCP  pcre_uchar c;
2962  const pcre_uint32 *p;  pcre_uint32 list[8];
2963  #endif  const pcre_uint32* chr_ptr;
2964    const pcre_uint32* ochr_ptr;
2965  const ucd_record *prop = GET_UCD(c);  const pcre_uint32* list_ptr;
2966    const pcre_uchar *next_code;
2967    pcre_uint32 chr;
2968    
2969    /* Note: the base_list[1] contains whether the current opcode has greedy
2970    (represented by a non-zero value) quantifier. This is a different from
2971    other character type lists, which stores here that the character iterator
2972    matches to an empty string (also represented by a non-zero value). */
2973    
2974  switch(ptype)  for(;;)
2975    {    {
2976    case PT_LAMP:    c = *code;
   return (prop->chartype == ucp_Lu ||  
           prop->chartype == ucp_Ll ||  
           prop->chartype == ucp_Lt) == negated;  
2977    
2978    case PT_GC:    /* Skip over callouts */
   return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;  
2979    
2980    case PT_PC:    if (c == OP_CALLOUT)
2981    return (pdata == prop->chartype) == negated;      {
2982        code += PRIV(OP_lengths)[c];
2983        continue;
2984        }
2985    
2986    case PT_SC:    if (c == OP_ALT)
2987    return (pdata == prop->script) == negated;      {
2988        do code += GET(code, 1); while (*code == OP_ALT);
2989        c = *code;
2990        }
2991    
2992    /* These are specials */    switch(c)
2993        {
2994        case OP_END:
2995        case OP_KETRPOS:
2996        /* TRUE only in greedy case. The non-greedy case could be replaced by
2997        an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT
2998        uses more memory, which we cannot get at this stage.) */
2999    
3000    case PT_ALNUM:      return base_list[1] != 0;
   return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||  
           PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;  
3001    
3002    case PT_SPACE:    /* Perl space */      case OP_KET:
3003    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||      /* If the bracket is capturing, and referenced by an OP_RECURSE, or
3004            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)      it is an atomic sub-pattern (assert, once, etc.) the non-greedy case
3005            == negated;      cannot be converted to a possessive form. */
3006    
3007        if (base_list[1] == 0) return FALSE;
3008    
3009        switch(*(code - GET(code, 1)))
3010          {
3011          case OP_ASSERT:
3012          case OP_ASSERT_NOT:
3013          case OP_ASSERTBACK:
3014          case OP_ASSERTBACK_NOT:
3015          case OP_ONCE:
3016          case OP_ONCE_NC:
3017          /* Atomic sub-patterns and assertions can always auto-possessify their
3018          last iterator. */
3019          return TRUE;
3020          }
3021    
3022    case PT_PXSPACE:  /* POSIX space */      code += PRIV(OP_lengths)[c];
3023    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||      continue;
           c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||  
           c == CHAR_FF || c == CHAR_CR)  
           == negated;  
3024    
3025    case PT_WORD:      case OP_ONCE:
3026    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||      case OP_ONCE_NC:
3027            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||      case OP_BRA:
3028            c == CHAR_UNDERSCORE) == negated;      case OP_CBRA:
3029        next_code = code;
3030  #ifdef SUPPORT_UCP      do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
   case PT_CLIST:  
   p = PRIV(ucd_caseless_sets) + prop->caseset;  
   for (;;)  
     {  
     if ((unsigned int)c < *p) return !negated;  
     if ((unsigned int)c == *p++) return negated;  
     }  
   break;  /* Control never reaches here */  
 #endif  
   }  
3031    
3032  return FALSE;      /* We do not support repeated brackets, because they can lead to
3033  }      infinite recursion. */
 #endif  /* SUPPORT_UCP */  
3034    
3035        if (*next_code != OP_KET) return FALSE;
3036    
3037        next_code = code + GET(code, 1);
3038        code += PRIV(OP_lengths)[c];
3039    
3040  /*************************************************      while (*next_code == OP_ALT)
3041  *     Check if auto-possessifying is possible    *        {
3042  *************************************************/        if (!compare_opcodes(code, utf, cd, base_list)) return FALSE;
3043          code = next_code + 1 + LINK_SIZE;
3044          next_code += GET(next_code, 1);
3045          }
3046        continue;
3047    
3048  /* This function is called for unlimited repeats of certain items, to see      case OP_BRAZERO:
3049  whether the next thing could possibly match the repeated item. If not, it makes      case OP_BRAMINZERO:
 sense to automatically possessify the repeated item.  
3050    
3051  Arguments:      next_code = code + 1;
3052    previous      pointer to the repeated opcode      if (*next_code != OP_BRA && *next_code != OP_CBRA)
3053    utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode        return FALSE;
   ptr           next character in pattern  
   options       options bits  
   cd            contains pointers to tables etc.  
3054    
3055  Returns:        TRUE if possessifying is wanted      do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
 */  
3056    
3057  static BOOL      /* We do not support repeated brackets, because they can lead to
3058  check_auto_possessive(const pcre_uchar *previous, BOOL utf,      infinite recursion. */
3059    const pcre_uchar *ptr, int options, compile_data *cd)      if (*next_code != OP_KET) return FALSE;
3060  {  
3061  pcre_uint32 c = NOTACHAR;      /* The bracket content will be checked by the
3062  pcre_uint32 next;      OP_BRA/OP_CBRA case above. */
3063  int escape;      next_code += 1 + LINK_SIZE;
3064  int op_code = *previous++;      if (!compare_opcodes(next_code, utf, cd, base_list)) return FALSE;
3065    
3066  /* Skip whitespace and comments in extended mode */      code += PRIV(OP_lengths)[c];
3067        continue;
3068        }
3069    
3070  if ((options & PCRE_EXTENDED) != 0)    /* Check for a supported opcode, and load its properties. */
3071    {  
3072    for (;;)    code = get_chr_property_list(code, utf, cd->fcc, list);
3073      if (code == NULL) return FALSE;    /* Unsupported */
3074    
3075      /* If either opcode is a small character list, set pointers for comparing
3076      characters from that list with another list, or with a property. */
3077    
3078      if (base_list[0] == OP_CHAR)
3079        {
3080        chr_ptr = base_list + 2;
3081        list_ptr = list;
3082        }
3083      else if (list[0] == OP_CHAR)
3084        {
3085        chr_ptr = list + 2;
3086        list_ptr = base_list;
3087        }
3088    
3089      /* Some property combinations also acceptable. Unicode property opcodes are
3090      processed specially; the rest can be handled with a lookup table. */
3091    
3092      else
3093      {      {
3094      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      pcre_uint32 leftop, rightop;
3095      if (*ptr == CHAR_NUMBER_SIGN)  
3096        if (list[1] != 0) return FALSE;   /* Must match at least one character */
3097        leftop = base_list[0];
3098        rightop = list[0];
3099    
3100    #ifdef SUPPORT_UCP
3101        if (leftop == OP_PROP || leftop == OP_NOTPROP)
3102        {        {
3103        ptr++;        if (rightop == OP_EOD) return TRUE;
3104        while (*ptr != 0)        if (rightop == OP_PROP || rightop == OP_NOTPROP)
3105          {          {
3106          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          int n;
3107          ptr++;          const pcre_uint8 *p;
3108  #ifdef SUPPORT_UTF          BOOL same = leftop == rightop;
3109          if (utf) FORWARDCHAR(ptr);          BOOL lisprop = leftop == OP_PROP;
3110  #endif          BOOL risprop = rightop == OP_PROP;
3111            BOOL bothprop = lisprop && risprop;
3112    
3113            /* There's a table that specifies how each combination is to be
3114            processed:
3115              0   Always return FALSE (never auto-possessify)
3116              1   Character groups are distinct (possessify if both are OP_PROP)
3117              2   Check character categories in the same group (general or particular)
3118              3   Return TRUE if the two opcodes are not the same
3119              ... see comments below
3120            */
3121    
3122            n = propposstab[base_list[2]][list[2]];
3123            switch(n)
3124              {
3125              case 0: return FALSE;
3126              case 1: return bothprop;
3127              case 2: return (base_list[3] == list[3]) != same;
3128              case 3: return !same;
3129    
3130              case 4:  /* Left general category, right particular category */
3131              return risprop && catposstab[base_list[3]][list[3]] == same;
3132    
3133              case 5:  /* Right general category, left particular category */
3134              return lisprop && catposstab[list[3]][base_list[3]] == same;
3135    
3136              /* This code is logically tricky. Think hard before fiddling with it.
3137              The posspropstab table has four entries per row. Each row relates to
3138              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3139              Only WORD actually needs all four entries, but using repeats for the
3140              others means they can all use the same code below.
3141    
3142              The first two entries in each row are Unicode general categories, and
3143              apply always, because all the characters they include are part of the
3144              PCRE character set. The third and fourth entries are a general and a
3145              particular category, respectively, that include one or more relevant
3146              characters. One or the other is used, depending on whether the check
3147              is for a general or a particular category. However, in both cases the
3148              category contains more characters than the specials that are defined
3149              for the property being tested against. Therefore, it cannot be used
3150              in a NOTPROP case.
3151    
3152              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3153              Underscore is covered by ucp_P or ucp_Po. */
3154    
3155              case 6:  /* Left alphanum vs right general category */
3156              case 7:  /* Left space vs right general category */
3157              case 8:  /* Left word vs right general category */
3158              p = posspropstab[n-6];
3159              return risprop && lisprop ==
3160                (list[3] != p[0] &&
3161                 list[3] != p[1] &&
3162                (list[3] != p[2] || !lisprop));
3163    
3164              case 9:   /* Right alphanum vs left general category */
3165              case 10:  /* Right space vs left general category */
3166              case 11:  /* Right word vs left general category */
3167              p = posspropstab[n-9];
3168              return lisprop && risprop ==
3169                (base_list[3] != p[0] &&
3170                 base_list[3] != p[1] &&
3171                (base_list[3] != p[2] || !risprop));
3172    
3173              case 12:  /* Left alphanum vs right particular category */
3174              case 13:  /* Left space vs right particular category */
3175              case 14:  /* Left word vs right particular category */
3176              p = posspropstab[n-12];
3177              return risprop && lisprop ==
3178                (catposstab[p[0]][list[3]] &&
3179                 catposstab[p[1]][list[3]] &&
3180                (list[3] != p[3] || !lisprop));
3181    
3182              case 15:  /* Right alphanum vs left particular category */
3183              case 16:  /* Right space vs left particular category */
3184              case 17:  /* Right word vs left particular category */
3185              p = posspropstab[n-15];
3186              return lisprop && risprop ==
3187                (catposstab[p[0]][base_list[3]] &&
3188                 catposstab[p[1]][base_list[3]] &&
3189                (base_list[3] != p[3] || !risprop));
3190              }
3191          }          }
3192          return FALSE;
3193        }        }
     else break;  
     }  
   }  
3194    
3195  /* If the next item is one that we can handle, get its value. A non-negative      else
3196  value is a character, a negative value is an escape value. */  #endif  /* SUPPORT_UCP */
3197    
3198  if (*ptr == CHAR_BACKSLASH)      return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3199    {             rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3200    int temperrorcode = 0;             autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3201    escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options, FALSE);      }
   if (temperrorcode != 0) return FALSE;  
   ptr++;    /* Point after the escape sequence */  
   }  
 else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)  
   {  
   escape = 0;  
 #ifdef SUPPORT_UTF  
   if (utf) { GETCHARINC(next, ptr); } else  
 #endif  
   next = *ptr++;  
   }  
 else return FALSE;  
3202    
3203  /* Skip whitespace and comments in extended mode */    /* Control reaches here only if one of the items is a small character list.
3204      All characters are checked against the other side. */
3205    
3206  if ((options & PCRE_EXTENDED) != 0)    do
   {  
   for (;;)  
3207      {      {
3208      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      chr = *chr_ptr;
3209      if (*ptr == CHAR_NUMBER_SIGN)  
3210        switch(list_ptr[0])
3211        {        {
3212        ptr++;        case OP_CHAR:
3213        while (*ptr != 0)        ochr_ptr = list_ptr + 2;
3214          do
3215          {          {
3216          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (chr == *ochr_ptr) return FALSE;
3217          ptr++;          ochr_ptr++;
 #ifdef SUPPORT_UTF  
         if (utf) FORWARDCHAR(ptr);  
 #endif  
3218          }          }
3219        }        while(*ochr_ptr != NOTACHAR);
3220      else break;        break;
     }  
   }  
3221    
3222  /* If the next thing is itself optional, we have to give up. */        case OP_NOT:
3223          ochr_ptr = list_ptr + 2;
3224          do
3225            {
3226            if (chr == *ochr_ptr)
3227              break;
3228            ochr_ptr++;
3229            }
3230          while(*ochr_ptr != NOTACHAR);
3231          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3232          break;
3233    
3234  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||        /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3235    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)        set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
     return FALSE;  
3236    
3237  /* If the previous item is a character, get its value. */        case OP_DIGIT:
3238          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3239          break;
3240    
3241  if (op_code == OP_CHAR || op_code == OP_CHARI ||        case OP_NOT_DIGIT:
3242      op_code == OP_NOT || op_code == OP_NOTI)        if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3243    //if (escape == 0) switch(op_code)        break;
3244    {  
3245  #ifdef SUPPORT_UTF        case OP_WHITESPACE:
3246    GETCHARTEST(c, previous);        if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3247  #else        break;
3248    c = *previous;  
3249          case OP_NOT_WHITESPACE:
3250          if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3251          break;
3252    
3253          case OP_WORDCHAR:
3254          if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3255          break;
3256    
3257          case OP_NOT_WORDCHAR:
3258          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3259          break;
3260    
3261          case OP_HSPACE:
3262          switch(chr)
3263            {
3264            HSPACE_CASES: return FALSE;
3265            default: break;
3266            }
3267          break;
3268    
3269          case OP_NOT_HSPACE:
3270          switch(chr)
3271            {
3272            HSPACE_CASES: break;
3273            default: return FALSE;
3274            }
3275          break;
3276    
3277          case OP_ANYNL:
3278          case OP_VSPACE:
3279          switch(chr)
3280            {
3281            VSPACE_CASES: return FALSE;
3282            default: break;
3283            }
3284          break;
3285    
3286          case OP_NOT_VSPACE:
3287          switch(chr)
3288            {
3289            VSPACE_CASES: break;
3290            default: return FALSE;
3291            }
3292          break;
3293    
3294          case OP_DOLL:
3295          case OP_EODN:
3296          switch (chr)
3297            {
3298            case CHAR_CR:
3299            case CHAR_LF:
3300            case CHAR_VT:
3301            case CHAR_FF:
3302            case CHAR_NEL:
3303    #ifndef EBCDIC
3304            case 0x2028:
3305            case 0x2029:
3306    #endif  /* Not EBCDIC */
3307            return FALSE;
3308            }
3309          break;
3310    
3311          case OP_EOD:    /* Can always possessify before \z */
3312          break;
3313    
3314          case OP_PROP:
3315          case OP_NOTPROP:
3316          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3317                list_ptr[0] == OP_NOTPROP))
3318            return FALSE;
3319          break;
3320    
3321          /* The class comparisons work only when the class is the second item
3322          of the pair, because there are at present no possessive forms of the
3323          class opcodes. Note also that the "code" variable that is used below
3324          points after the second item, and that the pointer for the first item
3325          is not available, so even if there were possessive forms of the class
3326          opcodes, the correct comparison could not be done. */
3327    
3328          case OP_NCLASS:
3329          if (chr > 255) return FALSE;
3330          /* Fall through */
3331    
3332          case OP_CLASS:
3333          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3334          if (chr > 255) break;
3335          if ((((pcre_uint8 *)(code - list_ptr[2]))[chr >> 3] & (1 << (chr & 7))) != 0)
3336            return FALSE;
3337          break;
3338    
3339    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3340          case OP_XCLASS:
3341          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3342          if (PRIV(xclass)(chr, code - list_ptr[2] + LINK_SIZE, utf))
3343            return FALSE;
3344          break;
3345  #endif  #endif
3346    
3347          default:
3348          return FALSE;
3349          }
3350    
3351        chr_ptr++;
3352        }
3353      while(*chr_ptr != NOTACHAR);
3354    
3355      /* At least one character must be matched from this opcode. */
3356    
3357      if (list[1] == 0) return TRUE;
3358    }    }
3359    
3360  /* Now compare the next item with the previous opcode. First, handle cases when  return FALSE;
3361  the next item is a character. */  }
3362    
3363    
3364    
3365    /*************************************************
3366    *    Scan compiled regex for auto-possession     *
3367    *************************************************/
3368    
3369    /* Replaces single character iterations with their possessive alternatives
3370    if appropriate. This function modifies the compiled opcode!
3371    
3372    Arguments:
3373      code        points to start of the byte code
3374      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3375      cd          static compile data
3376    
3377    Returns:      nothing
3378    */
3379    
3380    static void
3381    auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
3382    {
3383    register pcre_uchar c;
3384    const pcre_uchar *end;
3385    pcre_uint32 list[8];
3386    
3387  if (escape == 0)  for (;;)
3388    {    {
3389    /* For a caseless UTF match, the next character may have more than one other    c = *code;
3390    case, which maps to the special PT_CLIST property. Check this first. */  
3391      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
 #ifdef SUPPORT_UCP  
   if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)  
3392      {      {
3393      int ocs = UCD_CASESET(next);      c -= get_repeat_base(c) - OP_STAR;
3394      if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);      end = (c <= OP_MINUPTO) ?
3395          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3396        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3397    
3398        if (end != NULL && compare_opcodes(end, utf, cd, list))
3399          {
3400          switch(c)
3401            {
3402            case OP_STAR:
3403            *code += OP_POSSTAR - OP_STAR;
3404            break;
3405    
3406            case OP_MINSTAR:
3407            *code += OP_POSSTAR - OP_MINSTAR;
3408            break;
3409    
3410            case OP_PLUS:
3411            *code += OP_POSPLUS - OP_PLUS;
3412            break;
3413    
3414            case OP_MINPLUS:
3415            *code += OP_POSPLUS - OP_MINPLUS;
3416            break;
3417    
3418            case OP_QUERY:
3419            *code += OP_POSQUERY - OP_QUERY;
3420            break;
3421    
3422            case OP_MINQUERY:
3423            *code += OP_POSQUERY - OP_MINQUERY;
3424            break;
3425    
3426            case OP_UPTO:
3427            *code += OP_POSUPTO - OP_UPTO;
3428            break;
3429    
3430            case OP_MINUPTO:
3431            *code += OP_MINUPTO - OP_UPTO;
3432            break;
3433            }
3434          }
3435        c = *code;
3436      }      }
 #endif  
3437    
3438    switch(op_code)    switch(c)
3439      {      {
3440      case OP_CHAR:      case OP_END:
3441      return c != next;      return;
3442    
3443        case OP_TYPESTAR:
3444        case OP_TYPEMINSTAR:
3445        case OP_TYPEPLUS:
3446        case OP_TYPEMINPLUS:
3447        case OP_TYPEQUERY:
3448        case OP_TYPEMINQUERY:
3449        case OP_TYPEPOSSTAR:
3450        case OP_TYPEPOSPLUS:
3451        case OP_TYPEPOSQUERY:
3452        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3453        break;
3454    
3455      /* For CHARI (caseless character) we must check the other case. If we have      case OP_TYPEUPTO:
3456      Unicode property support, we can use it to test the other case of      case OP_TYPEMINUPTO:
3457      high-valued characters. We know that next can have only one other case,      case OP_TYPEEXACT:
3458      because multi-other-case characters are dealt with above. */      case OP_TYPEPOSUPTO:
3459        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3460          code += 2;
3461        break;
3462    
3463        case OP_XCLASS:
3464        code += GET(code, 1);
3465        break;
3466    
3467        case OP_MARK:
3468        case OP_PRUNE_ARG:
3469        case OP_SKIP_ARG:
3470        case OP_THEN_ARG:
3471        code += code[1];
3472        break;
3473        }
3474    
3475      /* Add in the fixed length from the table */
3476    
3477      code += PRIV(OP_lengths)[c];
3478    
3479      /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3480      a multi-byte character. The length in the table is a minimum, so we have to
3481      arrange to skip the extra bytes. */
3482    
3483    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3484      if (utf) switch(c)
3485        {
3486        case OP_CHAR:
3487      case OP_CHARI:      case OP_CHARI:
     if (c == next) return FALSE;  
 #ifdef SUPPORT_UTF  
     if (utf)  
       {  
       pcre_uint32 othercase;  
       if (next < 128) othercase = cd->fcc[next]; else  
 #ifdef SUPPORT_UCP  
       othercase = UCD_OTHERCASE(next);  
 #else  
       othercase = NOTACHAR;  
 #endif  
       return c != othercase;  
       }  
     else  
 #endif  /* SUPPORT_UTF */  
     return (c != TABLE_GET(next, cd->fcc, next));  /* Not UTF */  
   
3488      case OP_NOT:      case OP_NOT:
     return c == next;  
   
3489      case OP_NOTI:      case OP_NOTI:
3490      if (c == next) return TRUE;      case OP_STAR:
3491  #ifdef SUPPORT_UTF      case OP_MINSTAR:
3492      if (utf)      case OP_PLUS:
3493        {      case OP_MINPLUS:
3494        pcre_uint32 othercase;      case OP_QUERY:
3495        if (next < 128) othercase = cd->fcc[next]; else      case OP_MINQUERY:
3496  #ifdef SUPPORT_UCP      case OP_UPTO:
3497        othercase = UCD_OTHERCASE(next);      case OP_MINUPTO:
3498        case OP_EXACT:
3499        case OP_POSSTAR:
3500        case OP_POSPLUS:
3501        case OP_POSQUERY:
3502        case OP_POSUPTO:
3503        case OP_STARI:
3504        case OP_MINSTARI:
3505        case OP_PLUSI:
3506        case OP_MINPLUSI:
3507        case OP_QUERYI:
3508        case OP_MINQUERYI:
3509        case OP_UPTOI:
3510        case OP_MINUPTOI:
3511        case OP_EXACTI:
3512        case OP_POSSTARI:
3513        case OP_POSPLUSI:
3514        case OP_POSQUERYI:
3515        case OP_POSUPTOI:
3516        case OP_NOTSTAR:
3517        case OP_NOTMINSTAR:
3518        case OP_NOTPLUS:
3519        case OP_NOTMINPLUS:
3520        case OP_NOTQUERY:
3521        case OP_NOTMINQUERY:
3522        case OP_NOTUPTO:
3523        case OP_NOTMINUPTO:
3524        case OP_NOTEXACT:
3525        case OP_NOTPOSSTAR:
3526        case OP_NOTPOSPLUS:
3527        case OP_NOTPOSQUERY:
3528        case OP_NOTPOSUPTO:
3529        case OP_NOTSTARI:
3530        case OP_NOTMINSTARI:
3531        case OP_NOTPLUSI:
3532        case OP_NOTMINPLUSI:
3533        case OP_NOTQUERYI:
3534        case OP_NOTMINQUERYI:
3535        case OP_NOTUPTOI:
3536        case OP_NOTMINUPTOI:
3537        case OP_NOTEXACTI:
3538        case OP_NOTPOSSTARI:
3539        case OP_NOTPOSPLUSI:
3540        case OP_NOTPOSQUERYI:
3541        case OP_NOTPOSUPTOI:
3542        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3543        break;
3544        }
3545  #else  #else
3546        othercase = NOTACHAR;    (void)(utf);  /* Keep compiler happy by referencing function argument */
3547  #endif  #endif
3548        return c == othercase;    }
3549        }  }
3550      else  
3551  #endif  /* SUPPORT_UTF */  
3552      return (c == TABLE_GET(next, cd->fcc, next));  /* Not UTF */  
3553    /*************************************************
3554    *           Check for POSIX class syntax         *
3555    *************************************************/
3556    
3557    /* This function is called when the sequence "[:" or "[." or "[=" is
3558    encountered in a character class. It checks whether this is followed by a
3559    sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3560    reach an unescaped ']' without the special preceding character, return FALSE.
3561    
3562    Originally, this function only recognized a sequence of letters between the
3563    terminators, but it seems that Perl recognizes any sequence of characters,
3564    though of course unknown POSIX names are subsequently rejected. Perl gives an
3565    "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3566    didn't consider this to be a POSIX class. Likewise for [:1234:].
3567    
3568    The problem in trying to be exactly like Perl is in the handling of escapes. We
3569    have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3570    class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3571    below handles the special case of \], but does not try to do any other escape
3572    processing. This makes it different from Perl for cases such as [:l\ower:]
3573    where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
3574    "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
3575    I think.
3576    
3577    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3578    It seems that the appearance of a nested POSIX class supersedes an apparent
3579    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3580    a digit.
3581    
3582    In Perl, unescaped square brackets may also appear as part of class names. For
3583    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3584    [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3585    seem right at all. PCRE does not allow closing square brackets in POSIX class
3586    names.
3587    
3588    Arguments:
3589      ptr      pointer to the initial [
3590      endptr   where to return the end pointer
3591    
3592    Returns:   TRUE or FALSE
3593    */
3594    
3595    static BOOL
3596    check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3597    {
3598    pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3599    terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3600    for (++ptr; *ptr != CHAR_NULL; ptr++)
3601      {
3602      if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3603        ptr++;
3604      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3605      else
3606        {
3607        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3608          {
3609          *endptr = ptr;
3610          return TRUE;
3611          }
3612        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3613             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3614              ptr[1] == CHAR_EQUALS_SIGN) &&
3615            check_posix_syntax(ptr, endptr))
3616          return FALSE;
3617        }
3618      }
3619    return FALSE;
3620    }
3621    
     /* 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. */  
3622    
     case OP_DIGIT:  
     return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;  
3623    
     case OP_NOT_DIGIT:  
     return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;  
3624    
3625      case OP_WHITESPACE:  /*************************************************
3626      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;  *          Check POSIX class name                *
3627    *************************************************/
3628    
3629      case OP_NOT_WHITESPACE:  /* This function is called to check the name given in a POSIX-style class entry
3630      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;  such as [:alnum:].
3631    
3632      case OP_WORDCHAR:  Arguments:
3633      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;    ptr        points to the first letter
3634      len        the length of the name
3635    
3636      case OP_NOT_WORDCHAR:  Returns:     a value representing the name, or -1 if unknown
3637      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;  */
3638    
3639      case OP_HSPACE:  static int
3640      case OP_NOT_HSPACE:  check_posix_name(const pcre_uchar *ptr, int len)
3641      switch(next)  {
3642        {  const char *pn = posix_names;
3643        HSPACE_CASES:  register int yield = 0;
3644        return op_code == OP_NOT_HSPACE;  while (posix_name_lengths[yield] != 0)
3645      {
3646      if (len == posix_name_lengths[yield] &&
3647        STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3648      pn += posix_name_lengths[yield] + 1;
3649      yield++;
3650      }
3651    return -1;
3652    }
3653    
       default:  
       return op_code != OP_NOT_HSPACE;  
       }  
3654    
3655      case OP_ANYNL:  /*************************************************
3656      case OP_VSPACE:  *    Adjust OP_RECURSE items in repeated group   *
3657      case OP_NOT_VSPACE:  *************************************************/
     switch(next)  
       {  
       VSPACE_CASES:  
       return op_code == OP_NOT_VSPACE;  
3658    
3659        default:  /* OP_RECURSE items contain an offset from the start of the regex to the group
3660        return op_code != OP_NOT_VSPACE;  that is referenced. This means that groups can be replicated for fixed
3661        }  repetition simply by copying (because the recursion is allowed to refer to
3662    earlier groups that are outside the current group). However, when a group is
3663    optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
3664    inserted before it, after it has been compiled. This means that any OP_RECURSE
3665    items within it that refer to the group itself or any contained groups have to
3666    have their offsets adjusted. That one of the jobs of this function. Before it
3667    is called, the partially compiled regex must be temporarily terminated with
3668    OP_END.
3669    
3670  #ifdef SUPPORT_UCP  This function has been extended with the possibility of forward references for
3671      case OP_PROP:  recursions and subroutine calls. It must also check the list of such references
3672      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
3673    the current group is on this list, it adjusts the offset in the list, not the
3674    value in the reference (which is a group number).
3675    
3676      case OP_NOTPROP:  Arguments:
3677      return check_char_prop(next, previous[0], previous[1], TRUE);    group      points to the start of the group
3678  #endif    adjust     the amount by which the group is to be moved
3679      utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3680      cd         contains pointers to tables etc.
3681      save_hwm   the hwm forward reference pointer at the start of the group
3682    
3683      default:  Returns:     nothing
3684      return FALSE;  */
     }  
   }  
3685    
3686  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  static void
3687  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,
3688  generated only when PCRE_UCP is *not* set, that is, when only ASCII    pcre_uchar *save_hwm)
3689  characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are  {
3690  replaced by OP_PROP codes when PCRE_UCP is set. */  pcre_uchar *ptr = group;
3691    
3692  switch(op_code)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
3693    {    {
3694    case OP_CHAR:    int offset;
3695    case OP_CHARI:    pcre_uchar *hc;
3696    switch(escape)  
3697      /* See if this recursion is on the forward reference list. If so, adjust the
3698      reference. */
3699    
3700      for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
3701      {      {
3702      case ESC_d:      offset = (int)GET(hc, 0);
3703      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;      if (cd->start_code + offset == ptr + 1)
3704          {
3705          PUT(hc, 0, offset + adjust);
3706          break;
3707          }
3708        }
3709    
3710      case ESC_D:    /* Otherwise, adjust the recursion offset if it's after the start of this
3711      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;    group. */
3712    
3713      case ESC_s:    if (hc >= cd->hwm)
3714      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;      {
3715        offset = (int)GET(ptr, 1);
3716        if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
3717        }
3718    
3719      case ESC_S:    ptr += 1 + LINK_SIZE;
3720      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;    }
3721    }
3722    
     case ESC_w:  
     return c > 255 || (cd->ctypes[c] & ctype_word) == 0;  
3723    
     case ESC_W:  
     return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;  
3724    
3725      case ESC_h:  /*************************************************
3726      case ESC_H:  *        Insert an automatic callout point       *
3727      switch(c)  *************************************************/
       {  
       HSPACE_CASES:  
       return escape != ESC_h;  
   
       default:  
       return escape == ESC_h;  
       }  
3728    
3729      case ESC_v:  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
3730      case ESC_V:  callout points before each pattern item.
     switch(c)  
       {  
       VSPACE_CASES:  
       return escape != ESC_v;  
3731    
3732        default:  Arguments:
3733        return escape == ESC_v;    code           current code pointer
3734        }    ptr            current pattern pointer
3735      cd             pointers to tables etc
3736    
3737      /* When PCRE_UCP is set, these values get generated for \d etc. Find  Returns:         new code pointer
3738      their substitutions and process them. The result will always be either  */
     ESC_p or ESC_P. Then fall through to process those values. */  
3739    
3740  #ifdef SUPPORT_UCP  static pcre_uchar *
3741      case ESC_du:  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
3742      case ESC_DU:  {
3743      case ESC_wu:  *code++ = OP_CALLOUT;
3744      case ESC_WU:  *code++ = 255;
3745      case ESC_su:  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
3746      case ESC_SU:  PUT(code, LINK_SIZE, 0);                       /* Default length */
3747        {  return code + 2 * LINK_SIZE;
3748        int temperrorcode = 0;  }
       ptr = substitutes[escape - ESC_DU];  
       escape = check_escape(&ptr, &next, &temperrorcode, 0, options, FALSE);  
       if (temperrorcode != 0) return FALSE;  
       ptr++;    /* For compatibility */  
       }  
     /* Fall through */  
3749    
     case ESC_p:  
     case ESC_P:  
       {  
       int ptype, pdata, errorcodeptr;  
       BOOL negated;  
3750    
       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 */  
3751    
3752        /* If the property item is optional, we have to give up. (When generated  /*************************************************
3753        from \d etc by PCRE_UCP, this test will have been applied much earlier,  *         Complete a callout item                *
3754        to the original \d etc. At this point, ptr will point to a zero byte. */  *************************************************/
3755    
3756        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  /* A callout item contains the length of the next item in the pattern, which
3757          STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)  we can't fill in till after we have reached the relevant point. This is used
3758            return FALSE;  for both automatic and manual callouts.
3759    
3760        /* Do the property check. */  Arguments:
3761      previous_callout   points to previous callout item
3762      ptr                current pattern pointer
3763      cd                 pointers to tables etc
3764    
3765        return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);  Returns:             nothing
3766        }  */
 #endif  
3767    
3768      default:  static void
3769      return FALSE;  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
3770      }  {
3771    int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
3772    PUT(previous_callout, 2 + LINK_SIZE, length);
3773    }
3774    
   /* In principle, support for Unicode properties should be integrated here as  
   well. It means re-organizing the above code so as to get hold of the property  
   values before switching on the op-code. However, I wonder how many patterns  
   combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,  
   these op-codes are never generated.) */  
3775    
   case OP_DIGIT:  
   return escape == ESC_D || escape == ESC_s || escape == ESC_W ||  
          escape == ESC_h || escape == ESC_v || escape == ESC_R;  
3776    
3777    case OP_NOT_DIGIT:  #ifdef SUPPORT_UCP
3778    return escape == ESC_d;  /*************************************************
3779    *           Get othercase range                  *
3780    *************************************************/
3781    
3782    case OP_WHITESPACE:  /* This function is passed the start and end of a class range, in UTF-8 mode
3783    return escape == ESC_S || escape == ESC_d || escape == ESC_w;  with UCP support. It searches up the characters, looking for ranges of
3784    characters in the "other" case. Each call returns the next one, updating the
3785    start address. A character with multiple other cases is returned on its own
3786    with a special return value.
3787    
3788    case OP_NOT_WHITESPACE:  Arguments:
3789    return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;    cptr        points to starting character value; updated
3790      d           end value
3791      ocptr       where to put start of othercase range
3792      odptr       where to put end of othercase range
3793    
3794    case OP_HSPACE:  Yield:        -1 when no more
3795    return escape == ESC_S || escape == ESC_H || escape == ESC_d ||                 0 when a range is returned
3796           escape == ESC_w || escape == ESC_v || escape == ESC_R;                >0 the CASESET offset for char with multiple other cases
3797                    in this case, ocptr contains the original
3798    */
3799    
3800    case OP_NOT_HSPACE:  static int
3801    return escape == ESC_h;  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
3802      pcre_uint32 *odptr)
3803    {
3804    pcre_uint32 c, othercase, next;
3805    unsigned int co;
3806    
3807    /* Can't have \S in here because VT matches \S (Perl anomaly) */  /* Find the first character that has an other case. If it has multiple other
3808    case OP_ANYNL:  cases, return its case offset value. */
   case OP_VSPACE:  
   return escape == ESC_V || escape == ESC_d || escape == ESC_w;  
3809    
3810    case OP_NOT_VSPACE:  for (c = *cptr; c <= d; c++)
3811    return escape == ESC_v || escape == ESC_R;    {
3812      if ((co = UCD_CASESET(c)) != 0)
3813        {
3814        *ocptr = c++;   /* Character that has the set */
3815        *cptr = c;      /* Rest of input range */
3816        return (int)co;
3817        }
3818      if ((othercase = UCD_OTHERCASE(c)) != c) break;
3819      }
3820    
3821    case OP_WORDCHAR:  if (c > d) return -1;  /* Reached end of range */
   return escape == ESC_W || escape == ESC_s || escape == ESC_h ||  
          escape == ESC_v || escape == ESC_R;  
3822    
3823    case OP_NOT_WORDCHAR:  *ocptr = othercase;
3824    return escape == ESC_w || escape == ESC_d;  next = othercase + 1;
3825    
3826    default:  for (++c; c <= d; c++)
3827    return FALSE;    {
3828      if (UCD_OTHERCASE(c) != next) break;
3829      next++;
3830    }    }
3831    
3832  /* Control does not reach here */  *odptr = next - 1;     /* End of othercase range */
3833    *cptr = c;             /* Rest of input range */
3834    return 0;
3835  }  }
3836    #endif  /* SUPPORT_UCP */
3837    
3838    
3839    
# Line 3418  switch(op_code) Line 3842  switch(op_code)
3842  *************************************************/  *************************************************/
3843    
3844  /* 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
3845  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
3846  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
3847  mutually recursive with the function immediately below.  mutually recursive with the function immediately below.
3848    
3849  Arguments:  Arguments:
3850    classbits     the bit map for characters < 256    classbits     the bit map for characters < 256
3851    uchardptr     points to the pointer for extra data    uchardptr     points to the pointer for extra data
3852    options       the options word    options       the options word
3853    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
3854    start         start of range character    start         start of range character
3855    end           end of range character    end           end of range character
3856    
3857  Returns:        the number of < 256 characters added  Returns:        the number of < 256 characters added
3858                  the pointer to extra data is updated                  the pointer to extra data is updated
3859  */  */
# Line 3441  add_to_class(pcre_uint8 *classbits, pcre Line 3865  add_to_class(pcre_uint8 *classbits, pcre
3865  pcre_uint32 c;  pcre_uint32 c;
3866  int n8 = 0;  int n8 = 0;
3867    
3868  /* If caseless matching is required, scan the range and process alternate  /* If caseless matching is required, scan the range and process alternate
3869  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
3870  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
3871  range. */  range. */
3872    
3873  if ((options & PCRE_CASELESS) != 0)  if ((options & PCRE_CASELESS) != 0)
3874    {    {
3875  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3876    if ((options & PCRE_UTF8) != 0)    if ((options & PCRE_UTF8) != 0)
3877      {      {
3878      int rc;      int rc;
3879      pcre_uint32 oc, od;      pcre_uint32 oc, od;
3880    
3881      options &= ~PCRE_CASELESS;   /* Remove for recursive calls */      options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3882      c = start;      c = start;
3883    
3884      while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)      while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3885        {        {
3886        /* Handle a single character that has more than one other case. */        /* Handle a single character that has more than one other case. */
3887    
3888        if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,        if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3889          PRIV(ucd_caseless_sets) + rc, oc);          PRIV(ucd_caseless_sets) + rc, oc);
3890    
3891        /* Do nothing if the other case range is within the original range. */        /* Do nothing if the other case range is within the original range. */
3892    
3893        else if (oc >= start && od <= end) continue;        else if (oc >= start && od <= end) continue;
3894    
3895        /* 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
3896        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
3897        range. Otherwise, use a recursive call to add the additional range. */        range. Otherwise, use a recursive call to add the additional range. */
3898    
3899        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3900        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3901        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
# Line 3481  if ((options & PCRE_CASELESS) != 0) Line 3905  if ((options & PCRE_CASELESS) != 0)
3905  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3906    
3907    /* Not UTF-mode, or no UCP */    /* Not UTF-mode, or no UCP */
3908    
3909    for (c = start; c <= end && c < 256; c++)    for (c = start; c <= end && c < 256; c++)
3910      {      {
3911      SETBIT(classbits, cd->fcc[c]);      SETBIT(classbits, cd->fcc[c]);
3912      n8++;      n8++;
3913      }      }
3914    }    }
3915    
3916  /* 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
3917  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
3918  in all cases. */  in all cases. */
# Line 3514  if (end < 0x100) Line 3938  if (end < 0x100)
3938    {    {
3939    for (c = start; c <= end; c++)    for (c = start; c <= end; c++)
3940      {      {
3941      n8++;      n8++;
3942      SETBIT(classbits, c);      SETBIT(classbits, c);
3943      }      }
3944    }    }
3945    
3946  else  else
3947    {    {
3948    pcre_uchar *uchardata = *uchardptr;    pcre_uchar *uchardata = *uchardptr;
3949    
3950  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3951    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3952      {      {
3953      if (start < end)      if (start < end)
3954        {        {
3955        *uchardata++ = XCL_RANGE;        *uchardata++ = XCL_RANGE;
3956        uchardata += PRIV(ord2utf)(start, uchardata);        uchardata += PRIV(ord2utf)(start, uchardata);
3957        uchardata += PRIV(ord2utf)(end, uchardata);        uchardata += PRIV(ord2utf)(end, uchardata);
3958        }        }
3959      else if (start == end)      else if (start == end)
3960        {        {
3961        *uchardata++ = XCL_SINGLE;        *uchardata++ = XCL_SINGLE;
3962        uchardata += PRIV(ord2utf)(start, uchardata);        uchardata += PRIV(ord2utf)(start, uchardata);
3963        }        }
3964      }      }
3965    else    else
3966  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
3967    
3968    /* Without UTF support, character values are constrained by the bit length,    /* Without UTF support, character values are constrained by the bit length,
3969    and can only be > 256 for 16-bit and 32-bit libraries. */    and can only be > 256 for 16-bit and 32-bit libraries. */
3970    
3971  #ifdef COMPILE_PCRE8  #ifdef COMPILE_PCRE8
3972      {}      {}
3973  #else  #else
3974    if (start < end)    if (start < end)
3975      {      {
3976      *uchardata++ = XCL_RANGE;      *uchardata++ = XCL_RANGE;
# Line 3557  else Line 3981  else
3981      {      {
3982      *uchardata++ = XCL_SINGLE;      *uchardata++ = XCL_SINGLE;
3983      *uchardata++ = start;      *uchardata++ = start;
3984      }      }
3985  #endif  #endif
3986    
3987    *uchardptr = uchardata;   /* Updata extra data pointer */    *uchardptr = uchardata;   /* Updata extra data pointer */
3988    }    }
3989    
3990  return n8;    /* Number of 8-bit characters */  return n8;    /* Number of 8-bit characters */
3991  }  }
3992    
3993    
3994    
3995    
3996  /*************************************************  /*************************************************
3997  *        Add a list of characters to a class     *  *        Add a list of characters to a class     *
3998  *************************************************/  *************************************************/
3999    
4000  /* 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
4001  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
4002  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
4003  handled appropriately. This function is mutually recursive with the function  handled appropriately. This function is mutually recursive with the function
# Line 3583  Arguments: Line 4007  Arguments:
4007    classbits     the bit map for characters < 256    classbits     the bit map for characters < 256
4008    uchardptr     points to the pointer for extra data    uchardptr     points to the pointer for extra data
4009    options       the options word    options       the options word
4010    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
4011    p             points to row of 32-bit values, terminated by NOTACHAR    p             points to row of 32-bit values, terminated by NOTACHAR
4012    except        character to omit; this is used when adding lists of    except        character to omit; this is used when adding lists of
4013                    case-equivalent characters to avoid including the one we                    case-equivalent characters to avoid including the one we
4014                    already know about                    already know about
4015    
4016  Returns:        the number of < 256 characters added  Returns:        the number of < 256 characters added
4017                  the pointer to extra data is updated                  the pointer to extra data is updated
4018  */  */
# Line 3602  while (p[0] < NOTACHAR) Line 4026  while (p[0] < NOTACHAR)
4026    {    {
4027    int n = 0;    int n = 0;
4028    if (p[0] != except)    if (p[0] != except)
4029      {      {
4030      while(p[n+1] == p[0] + n + 1) n++;      while(p[n+1] == p[0] + n + 1) n++;
4031      n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);      n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
4032      }      }
4033    p += n + 1;    p += n + 1;
4034    }    }
4035  return n8;  return n8;
4036  }  }
4037    
4038    
4039    
# Line 3624  Arguments: Line 4048  Arguments:
4048    classbits     the bit map for characters < 256    classbits     the bit map for characters < 256
4049    uchardptr     points to the pointer for extra data    uchardptr     points to the pointer for extra data
4050    options       the options word    options       the options word
4051    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
4052    p             points to row of 32-bit values, terminated by NOTACHAR    p             points to row of 32-bit values, terminated by NOTACHAR
4053    
4054  Returns:        the number of < 256 characters added  Returns:        the number of < 256 characters added
4055                  the pointer to extra data is updated                  the pointer to extra data is updated
4056  */  */
4057    
4058  static int  static int
4059  add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,  add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
4060    int options, compile_data *cd, const pcre_uint32 *p)    int options, compile_data *cd, const pcre_uint32 *p)
4061  {  {
4062  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
# Line 3644  while (p[0] < NOTACHAR) Line 4068  while (p[0] < NOTACHAR)
4068    while (p[1] == p[0] + 1) p++;    while (p[1] == p[0] + 1) p++;
4069    n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,    n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
4070      (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);      (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
4071    p++;    p++;
4072    }    }
4073  return n8;  return n8;
4074  }  }
4075    
4076    
4077    
# Line 3662  to find out the amount of memory needed, Line 4086  to find out the amount of memory needed,
4086  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
4087    
4088  Arguments:  Arguments:
4089    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
4090    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
4091    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
4092    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
4093    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr      place to put the first required character
4094    reqcharptr     set to the last literal character required, else < 0    firstcharflagsptr place to put the first character flags, or a negative number
4095    bcptr          points to current branch chain    reqcharptr        place to put the last required character
4096    cond_depth     conditional nesting depth    reqcharflagsptr   place to put the last required character flags, or a negative number
4097    cd             contains pointers to tables etc.    bcptr             points to current branch chain
4098    lengthptr      NULL during the real compile phase    cond_depth        conditional nesting depth
4099                   points to length accumulator during pre-compile phase    cd                contains pointers to tables etc.
4100      lengthptr         NULL during the real compile phase
4101                        points to length accumulator during pre-compile phase
4102    
4103  Returns:         TRUE on success  Returns:            TRUE on success
4104                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
4105  */  */
4106    
4107  static BOOL  static BOOL
4108  compile_branch(int *optionsptr, pcre_uchar **codeptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
4109    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,    const pcre_uchar **ptrptr, int *errorcodeptr,
4110    pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,    pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
4111      pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
4112      branch_chain *bcptr, int cond_depth,
4113    compile_data *cd, int *lengthptr)    compile_data *cd, int *lengthptr)
4114  {  {
4115  int repeat_type, op_type;  int repeat_type, op_type;
4116  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
4117  int bravalue = 0;  int bravalue = 0;
4118  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
4119  pcre_int32 firstchar, reqchar;  pcre_uint32 firstchar, reqchar;
4120  pcre_int32 zeroreqchar, zerofirstchar;  pcre_int32 firstcharflags, reqcharflags;
4121    pcre_uint32 zeroreqchar, zerofirstchar;
4122    pcre_int32 zeroreqcharflags, zerofirstcharflags;
4123  pcre_int32 req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
4124  int options = *optionsptr;               /* May change dynamically */  int options = *optionsptr;               /* May change dynamically */
4125  int after_manual_callout = 0;  int after_manual_callout = 0;
# Line 3717  dynamically as we process the pattern. * Line 4147  dynamically as we process the pattern. *
4147  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4148  /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */  /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
4149  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
4150    #ifndef COMPILE_PCRE32
4151  pcre_uchar utf_chars[6];  pcre_uchar utf_chars[6];
4152    #endif
4153  #else  #else
4154  BOOL utf = FALSE;  BOOL utf = FALSE;
4155  #endif  #endif
4156    
4157  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
4158  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,
4159  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
4160  alternative calls for the different cases. */  alternative calls for the different cases. */
4161    
4162  pcre_uchar *class_uchardata;  pcre_uchar *class_uchardata;
# Line 3752  to take the zero repeat into account. Th Line 4184  to take the zero repeat into account. Th
4184  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
4185  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
4186    
4187  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
4188    firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
4189    
4190  /* The variable req_caseopt contains either the REQ_CASELESS value  /* The variable req_caseopt contains either the REQ_CASELESS value
4191  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 3778  for (;; ptr++) Line 4211  for (;; ptr++)
4211    int recno;    int recno;
4212    int refsign;    int refsign;
4213    int skipbytes;    int skipbytes;
4214    int subreqchar;    pcre_uint32 subreqchar, subfirstchar;
4215    int subfirstchar;    pcre_int32 subreqcharflags, subfirstcharflags;
4216    int terminator;    int terminator;
4217    int mclength;    unsigned int mclength;
4218    int tempbracount;    unsigned int tempbracount;
4219    int ec; // FIXMEchpe pcre_uint32    pcre_uint32 ec;
4220    pcre_uchar mcbuffer[8];    pcre_uchar mcbuffer[8];
4221    
4222    /* Get next character in the pattern */    /* Get next character in the pattern */
# Line 3793  for (;; ptr++) Line 4226  for (;; ptr++)
4226    /* 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
4227    string. Nesting only happens one level deep. */    string. Nesting only happens one level deep. */
4228    
4229    if (c == 0 && nestptr != NULL)    if (c == CHAR_NULL && nestptr != NULL)
4230      {      {
4231      ptr = nestptr;      ptr = nestptr;
4232      nestptr = NULL;      nestptr = NULL;
# Line 3868  for (;; ptr++) Line 4301  for (;; ptr++)
4301    
4302    /* 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 */
4303    
4304    if (inescq && c != 0)    if (inescq && c != CHAR_NULL)
4305      {      {
4306      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)      if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
4307        {        {
# Line 3893  for (;; ptr++) Line 4326  for (;; ptr++)
4326        }        }
4327      }      }
4328    
   /* Fill in length of a previous callout, except when the next thing is  
   a quantifier. */  
   
4329    is_quantifier =    is_quantifier =
4330      c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||      c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
4331      (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));      (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
4332    
4333    if (!is_quantifier && previous_callout != NULL &&    /* Fill in length of a previous callout, except when the next thing is a
4334      quantifier or when processing a property substitution string in UCP mode. */
4335    
4336      if (!is_quantifier && previous_callout != NULL && nestptr == NULL &&
4337         after_manual_callout-- <= 0)         after_manual_callout-- <= 0)
4338      {      {
4339      if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */      if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */
# Line 3916  for (;; ptr++) Line 4349  for (;; ptr++)
4349      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
4350        {        {
4351        ptr++;        ptr++;
4352        while (*ptr != 0)        while (*ptr != CHAR_NULL)
4353          {          {
4354          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
4355          ptr++;          ptr++;
# Line 3924  for (;; ptr++) Line 4357  for (;; ptr++)
4357          if (utf) FORWARDCHAR(ptr);          if (utf) FORWARDCHAR(ptr);
4358  #endif  #endif
4359          }          }
4360        if (*ptr != 0) continue;        if (*ptr != CHAR_NULL) continue;
4361    
4362        /* Else fall through to handle end of string */        /* Else fall through to handle end of string */
4363        c = 0;        c = 0;
4364        }        }
4365      }      }
4366    
4367    /* No auto callout for quantifiers. */    /* No auto callout for quantifiers, or while processing property strings that
4368      are substituted for \w etc in UCP mode. */
4369    
4370    if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier)    if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL)
4371      {      {
4372      previous_callout = code;      previous_callout = code;
4373      code = auto_callout(code, ptr, cd);      code = auto_callout(code, ptr, cd);
# Line 3946  for (;; ptr++) Line 4380  for (;; ptr++)
4380      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
4381      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
4382      *firstcharptr = firstchar;      *firstcharptr = firstchar;
4383        *firstcharflagsptr = firstcharflags;
4384      *reqcharptr = reqchar;      *reqcharptr = reqchar;
4385        *reqcharflagsptr = reqcharflags;
4386      *codeptr = code;      *codeptr = code;
4387      *ptrptr = ptr;      *ptrptr = ptr;
4388      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 3970  for (;; ptr++) Line 4406  for (;; ptr++)
4406      previous = NULL;      previous = NULL;
4407      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
4408        {        {
4409        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4410        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
4411        }        }
4412      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 3985  for (;; ptr++) Line 4421  for (;; ptr++)
4421      repeats. The value of reqchar doesn't change either. */      repeats. The value of reqchar doesn't change either. */
4422    
4423      case CHAR_DOT:      case CHAR_DOT:
4424      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4425      zerofirstchar = firstchar;      zerofirstchar = firstchar;
4426        zerofirstcharflags = firstcharflags;
4427      zeroreqchar = reqchar;      zeroreqchar = reqchar;
4428        zeroreqcharflags = reqcharflags;
4429      previous = code;      previous = code;
4430      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
4431      break;      break;
# Line 4061  for (;; ptr++) Line 4499  for (;; ptr++)
4499          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4500        {        {
4501        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
4502        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4503        zerofirstchar = firstchar;        zerofirstchar = firstchar;
4504          zerofirstcharflags = firstcharflags;
4505        break;        break;
4506        }        }
4507    
# Line 4097  for (;; ptr++) Line 4536  for (;; ptr++)
4536      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
4537      loop, c contains the first byte of the character. */      loop, c contains the first byte of the character. */
4538    
4539      if (c != 0) do      if (c != CHAR_NULL) do
4540        {        {
4541        const pcre_uchar *oldptr;        const pcre_uchar *oldptr;
4542    
4543  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #ifdef SUPPORT_UTF
4544        if (utf && HAS_EXTRALEN(c))        if (utf && HAS_EXTRALEN(c))
4545          {                           /* Braces are required because the */          {                           /* Braces are required because the */
4546          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
# Line 4112  for (;; ptr++) Line 4551  for (;; ptr++)
4551        /* In the pre-compile phase, accumulate the length of any extra        /* In the pre-compile phase, accumulate the length of any extra
4552        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
4553        contain a zillion > 255 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
4554        (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,
4555        however. */        however. */
4556    
4557        if (lengthptr != NULL && class_uchardata > class_uchardata_base)        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
# Line 4176  for (;; ptr++) Line 4615  for (;; ptr++)
4615          alpha. This relies on the fact that the class table starts with          alpha. This relies on the fact that the class table starts with
4616          alpha, lower, upper as the first 3 entries. */          alpha, lower, upper as the first 3 entries. */
4617    
4618          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
4619            posix_class = 0;            posix_class = 0;
4620    
4621          /* 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 4253  for (;; ptr++) Line 4692  for (;; ptr++)
4692    
4693        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
4694          {          {
4695          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options, TRUE);          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
4696              TRUE);
4697          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
4698            if (escape == 0) c = ec;
         if (escape == 0)  
           c = ec;  
4699          else if (escape == 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 */
4700          else if (escape == ESC_N)            /* \N is not supported in a class */          else if (escape == ESC_N)          /* \N is not supported in a class */
4701            {            {
4702            *errorcodeptr = ERR71;            *errorcodeptr = ERR71;
4703            goto FAILED;            goto FAILED;
# Line 4316  for (;; ptr++) Line 4753  for (;; ptr++)
4753              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
4754              continue;              continue;
4755    
4756              /* Perl 5.004 onwards omits VT from \s, but we must preserve it              /* Perl 5.004 onwards omitted VT from \s, but restored it at Perl
4757              if it was previously set by something earlier in the character              5.18. Before PCRE 8.34, we had to preserve the VT bit if it was
4758              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and              previously set by something earlier in the character class.
4759              EBCDIC, so we lazily just adjust the appropriate bit. */              Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
4760                we could just adjust the appropriate bit. From PCRE 8.34 we no
4761                longer treat \s and \S specially. */
4762    
4763              case ESC_s:              case ESC_s:
4764              classbits[0] |= cbits[cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
             classbits[1] |= cbits[cbit_space+1] & ~0x08;  
             for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];  
4765              continue;              continue;
4766    
4767              case ESC_S:              case ESC_S:
4768              should_flip_negation = TRUE;              should_flip_negation = TRUE;
4769              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
             classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */  
4770              continue;              continue;
4771    
4772              /* The rest apply in both UCP and non-UCP cases. */              /* The rest apply in both UCP and non-UCP cases. */
4773    
4774              case ESC_h:              case ESC_h:
4775              (void)add_list_to_class(classbits, &class_uchardata, options, cd,              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4776                PRIV(hspace_list), NOTACHAR);                PRIV(hspace_list), NOTACHAR);
4777              continue;              continue;
4778    
4779              case ESC_H:              case ESC_H:
4780              (void)add_not_list_to_class(classbits, &class_uchardata, options,              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4781                cd, PRIV(hspace_list));                cd, PRIV(hspace_list));
4782              continue;              continue;
4783    
4784              case ESC_v:              case ESC_v:
4785              (void)add_list_to_class(classbits, &class_uchardata, options, cd,              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4786                PRIV(vspace_list), NOTACHAR);                PRIV(vspace_list), NOTACHAR);
4787              continue;              continue;
4788    
4789              case ESC_V:              case ESC_V:
4790              (void)add_not_list_to_class(classbits, &class_uchardata, options,              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4791                cd, PRIV(vspace_list));                cd, PRIV(vspace_list));
4792              continue;              continue;
4793    
4794  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 4360  for (;; ptr++) Line 4796  for (;; ptr++)
4796              case ESC_P:              case ESC_P:
4797                {                {
4798                BOOL negated;                BOOL negated;
4799                int pdata;                unsigned int ptype = 0, pdata = 0;
4800                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
4801                if (ptype < 0) goto FAILED;                  goto FAILED;
4802                *class_uchardata++ = ((escape == ESC_p) != negated)?                *class_uchardata++ = ((escape == ESC_p) != negated)?
4803                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
4804                *class_uchardata++ = ptype;                *class_uchardata++ = ptype;
# Line 4390  for (;; ptr++) Line 4826  for (;; ptr++)
4826    
4827          /* Fall through if the escape just defined a single character (c >= 0).          /* Fall through if the escape just defined a single character (c >= 0).
4828          This may be greater than 256. */          This may be greater than 256. */
4829    
4830          escape = 0;          escape = 0;
4831    
4832          }   /* End of backslash handling */          }   /* End of backslash handling */
# Line 4416  for (;; ptr++) Line 4852  for (;; ptr++)
4852    
4853        if (!inescq && ptr[1] == CHAR_MINUS)        if (!inescq && ptr[1] == CHAR_MINUS)
4854          {          {
4855          int d;          pcre_uint32 d;
4856          ptr += 2;          ptr += 2;
4857          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;          while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;
4858    
# Line 4431  for (;; ptr++) Line 4867  for (;; ptr++)
4867            inescq = TRUE;            inescq = TRUE;
4868            break;            break;
4869            }            }
4870    
4871          /* 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
4872          back the pointer and jump to handle the character that preceded it. */          back the pointer and jump to handle the character that preceded it. */
4873    
4874          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4875            {            {
4876            ptr = oldptr;            ptr = oldptr;
4877            goto CLASS_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4878            }            }
4879    
4880          /* Otherwise, we have a potential range; pick up the next character */          /* Otherwise, we have a potential range; pick up the next character */
4881    
4882  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
# Line 4487  for (;; ptr++) Line 4923  for (;; ptr++)
4923          /* We have found a character range, so single character optimizations          /* We have found a character range, so single character optimizations
4924          cannot be done anymore. Any value greater than 1 indicates that there          cannot be done anymore. Any value greater than 1 indicates that there
4925          is more than one character. */          is more than one character. */
4926    
4927          class_one_char = 2;          class_one_char = 2;
4928    
4929          /* 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. */
4930    
4931          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4932    
4933          class_has_8bitchar +=          class_has_8bitchar +=
4934            add_to_class(classbits, &class_uchardata, options, cd, c, d);            add_to_class(classbits, &class_uchardata, options, cd, c, d);
4935    
4936          continue;   /* Go get the next char in the class */          continue;   /* Go get the next char in the class */
4937          }          }
4938    
4939        /* 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
4940        char, or after \ that introduces a single character or for an apparent        char, or after \ that introduces a single character or for an apparent
4941        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
4942        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
4943        with a zillion characters in it. */        with a zillion characters in it. */
4944    
4945        CLASS_SINGLE_CHARACTER:        CLASS_SINGLE_CHARACTER:
# Line 4522  for (;; ptr++) Line 4958  for (;; ptr++)
4958          {          {
4959          ptr++;          ptr++;
4960          zeroreqchar = reqchar;          zeroreqchar = reqchar;
4961            zeroreqcharflags = reqcharflags;
4962    
4963          if (negate_class)          if (negate_class)
4964            {            {
4965  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
             // FIXMEchpe pcreuint32?  
4966            int d;            int d;
4967  #endif  #endif
4968            if (firstchar == REQ_UNSET) firstchar = REQ_NONE;            if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4969            zerofirstchar = firstchar;            zerofirstchar = firstchar;
4970              zerofirstcharflags = firstcharflags;
4971    
4972            /* For caseless UTF-8 mode when UCP support is available, check            /* For caseless UTF-8 mode when UCP support is available, check
4973            whether this character has more than one other case. If so, generate            whether this character has more than one other case. If so, generate
4974            a special OP_NOTPROP item instead of OP_NOTI. */            a special OP_NOTPROP item instead of OP_NOTI. */
4975    
4976  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4977            if (utf && (options & PCRE_CASELESS) != 0 &&            if (utf && (options & PCRE_CASELESS) != 0 &&
4978                (d = UCD_CASESET(c)) != 0)                (d = UCD_CASESET(c)) != 0)
4979              {              {
4980              *code++ = OP_NOTPROP;              *code++ = OP_NOTPROP;
4981              *code++ = PT_CLIST;              *code++ = PT_CLIST;
4982              *code++ = d;              *code++ = d;
4983              }              }
4984            else            else
4985  #endif  #endif
4986            /* Char has only one other case, or UCP not available */            /* Char has only one other case, or UCP not available */
4987    
# Line 4557  for (;; ptr++) Line 4994  for (;; ptr++)
4994  #endif  #endif
4995                *code++ = c;                *code++ = c;
4996              }              }
4997    
4998            /* We are finished with this character class */            /* We are finished with this character class */
4999    
5000            goto END_CLASS;            goto END_CLASS;
5001            }            }
5002    
# Line 4577  for (;; ptr++) Line 5014  for (;; ptr++)
5014            }            }
5015          goto ONE_CHAR;          goto ONE_CHAR;
5016          }       /* End of 1-char optimization */          }       /* End of 1-char optimization */
5017    
5018        /* 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
5019        has been generated. Add this character to the class. */        has been generated. Add this character to the class. */
5020    
5021        class_has_8bitchar +=        class_has_8bitchar +=
5022          add_to_class(classbits, &class_uchardata, options, cd, c, c);          add_to_class(classbits, &class_uchardata, options, cd, c, c);
5023        }        }
5024    
# Line 4589  for (;; ptr++) Line 5026  for (;; ptr++)
5026      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
5027      string. */      string. */
5028    
5029      while (((c = *(++ptr)) != 0 ||      while (((c = *(++ptr)) != CHAR_NULL ||
5030             (nestptr != NULL &&             (nestptr != NULL &&
5031               (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != 0)) &&               (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != CHAR_NULL)) &&
5032             (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));             (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
5033    
5034      /* Check for missing terminating ']' */      /* Check for missing terminating ']' */
5035    
5036      if (c == 0)      if (c == CHAR_NULL)
5037        {        {
5038        *errorcodeptr = ERR6;        *errorcodeptr = ERR6;
5039        goto FAILED;        goto FAILED;
5040        }        }
5041    
5042      /* 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
5043      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
5044      phase, class_uchardata gets emptied to prevent workspace overflow, so it      phase, class_uchardata gets emptied to prevent workspace overflow, so it
5045      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
5046      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
5047      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
5048      instead of just doing a test on class_uchardata below. */      instead of just doing a test on class_uchardata below. */
5049    
5050  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5051      if (class_uchardata > class_uchardata_base) xclass = TRUE;      if (class_uchardata > class_uchardata_base) xclass = TRUE;
5052  #endif  #endif
# Line 4618  for (;; ptr++) Line 5055  for (;; ptr++)
5055      setting, whatever the repeat count. Any reqchar setting must remain      setting, whatever the repeat count. Any reqchar setting must remain
5056      unchanged after any kind of repeat. */      unchanged after any kind of repeat. */
5057    
5058      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;      if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
5059      zerofirstchar = firstchar;      zerofirstchar = firstchar;
5060        zerofirstcharflags = firstcharflags;
5061      zeroreqchar = reqchar;      zeroreqchar = reqchar;
5062        zeroreqcharflags = reqcharflags;
5063    
5064      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
5065      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 4676  for (;; ptr++) Line 5115  for (;; ptr++)
5115        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
5116        }        }
5117      code += 32 / sizeof(pcre_uchar);      code += 32 / sizeof(pcre_uchar);
5118    
5119      END_CLASS:      END_CLASS:
5120      break;      break;
5121    
# Line 4715  for (;; ptr++) Line 5154  for (;; ptr++)
5154      if (repeat_min == 0)      if (repeat_min == 0)
5155        {        {
5156        firstchar = zerofirstchar;    /* Adjust for zero repeat */        firstchar = zerofirstchar;    /* Adjust for zero repeat */
5157          firstcharflags = zerofirstcharflags;
5158        reqchar = zeroreqchar;        /* Ditto */        reqchar = zeroreqchar;        /* Ditto */
5159          reqcharflags = zeroreqcharflags;
5160        }        }
5161    
5162      /* Remember whether this is a variable length repeat */      /* Remember whether this is a variable length repeat */
# Line 4818  for (;; ptr++) Line 5259  for (;; ptr++)
5259          {          {
5260          c = code[-1];          c = code[-1];
5261          if (*previous <= OP_CHARI && repeat_min > 1)          if (*previous <= OP_CHARI && repeat_min > 1)
5262            reqchar = c | req_caseopt | cd->req_varyopt;            {
5263          }            reqchar = c;
5264              reqcharflags = req_caseopt | cd->req_varyopt;
5265        /* 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;  
5266          }          }
5267    
5268        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
# Line 4851  for (;; ptr++) Line 5282  for (;; ptr++)
5282        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
5283        c = *previous;        c = *previous;
5284    
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
   
5285        OUTPUT_SINGLE_REPEAT:        OUTPUT_SINGLE_REPEAT:
5286        if (*previous == OP_PROP || *previous == OP_NOTPROP)        if (*previous == OP_PROP || *previous == OP_NOTPROP)
5287          {          {
# Line 4875  for (;; ptr++) Line 5298  for (;; ptr++)
5298    
5299        if (repeat_max == 0) goto END_REPEAT;        if (repeat_max == 0) goto END_REPEAT;
5300    
       /*--------------------------------------------------------------------*/  
       /* 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; */  
       /*--------------------------------------------------------------------*/  
   
5301        /* Combine the op_type with the repeat_type */        /* Combine the op_type with the repeat_type */
5302    
5303        repeat_type += op_type;        repeat_type += op_type;
# Line 5017  for (;; ptr++) Line 5430  for (;; ptr++)
5430      /* 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
5431      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}. */
5432    
5433      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
              *previous == OP_NCLASS ||  
5434  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5435               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
5436  #endif  #endif
5437               *previous == OP_REF ||               *previous == OP_REF   || *previous == OP_REFI ||
5438               *previous == OP_REFI)               *previous == OP_DNREF || *previous == OP_DNREFI)
5439        {        {
5440        if (repeat_max == 0)        if (repeat_max == 0)
5441          {          {
# Line 5031  for (;; ptr++) Line 5443  for (;; ptr++)
5443          goto END_REPEAT;          goto END_REPEAT;
5444          }          }
5445    
       /*--------------------------------------------------------------------*/  
       /* 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; */  
       /*--------------------------------------------------------------------*/  
   
5446        if (repeat_min == 0 && repeat_max == -1)        if (repeat_min == 0 && repeat_max == -1)
5447          *code++ = OP_CRSTAR + repeat_type;          *code++ = OP_CRSTAR + repeat_type;
5448        else if (repeat_min == 1 && repeat_max == -1)        else if (repeat_min == 1 && repeat_max == -1)
# Line 5200  for (;; ptr++) Line 5602  for (;; ptr++)
5602    
5603            else            else
5604              {              {
5605              if (groupsetfirstchar && reqchar < 0) reqchar = firstchar;              if (groupsetfirstchar && reqcharflags < 0)
5606                  {
5607                  reqchar = firstchar;
5608                  reqcharflags = firstcharflags;
5609                  }
5610    
5611              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
5612                {                {
# Line 5379  for (;; ptr++) Line 5785  for (;; ptr++)
5785              pcre_uchar *scode = bracode;              pcre_uchar *scode = bracode;
5786              do              do
5787                {                {
5788                if (could_be_empty_branch(scode, ketcode, utf, cd))                if (could_be_empty_branch(scode, ketcode, utf, cd, NULL))
5789                  {                  {
5790                  *bracode += OP_SBRA - OP_BRA;                  *bracode += OP_SBRA - OP_BRA;
5791                  break;                  break;
# Line 5479  for (;; ptr++) Line 5885  for (;; ptr++)
5885        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
5886          {          {
5887          tempcode += PRIV(OP_lengths)[*tempcode];          tempcode += PRIV(OP_lengths)[*tempcode];
5888  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #ifdef SUPPORT_UTF
5889          if (utf && HAS_EXTRALEN(tempcode[-1]))          if (utf && HAS_EXTRALEN(tempcode[-1]))
5890            tempcode += GET_EXTRALEN(tempcode[-1]);            tempcode += GET_EXTRALEN(tempcode[-1]);
5891  #endif  #endif
# Line 5575  for (;; ptr++) Line 5981  for (;; ptr++)
5981        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
5982          {          {
5983          arg = ++ptr;          arg = ++ptr;
5984          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5985          arglen = (int)(ptr - arg);          arglen = (int)(ptr - arg);
5986          if ((unsigned int)arglen > MAX_MARK)          if ((unsigned int)arglen > MAX_MARK)
5987            {            {
# Line 5620  for (;; ptr++) Line 6026  for (;; ptr++)
6026                (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;                (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
6027    
6028              /* Do not set firstchar after *ACCEPT */              /* Do not set firstchar after *ACCEPT */
6029              if (firstchar == REQ_UNSET) firstchar = REQ_NONE;              if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
6030              }              }
6031    
6032            /* Handle other cases with/without an argument */            /* Handle other cases with/without an argument */
# Line 5689  for (;; ptr++) Line 6095  for (;; ptr++)
6095          {          {
6096          case CHAR_NUMBER_SIGN:                 /* Comment; skip to ket */          case CHAR_NUMBER_SIGN:                 /* Comment; skip to ket */
6097          ptr++;          ptr++;
6098          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
6099          if (*ptr == 0)          if (*ptr == CHAR_NULL)
6100            {            {
6101            *errorcodeptr = ERR18;            *errorcodeptr = ERR18;
6102            goto FAILED;            goto FAILED;
# Line 5713  for (;; ptr++) Line 6119  for (;; ptr++)
6119          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
6120          case CHAR_LEFT_PARENTHESIS:          case CHAR_LEFT_PARENTHESIS:
6121          bravalue = OP_COND;       /* Conditional group */          bravalue = OP_COND;       /* Conditional group */
6122            tempptr = ptr;
6123    
6124          /* A condition can be an assertion, a number (referring to a numbered          /* A condition can be an assertion, a number (referring to a numbered
6125          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 5725  for (;; ptr++) Line 6132  for (;; ptr++)
6132          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
6133          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.
6134          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
6135          cases. */          cases.
6136    
6137            For compatibility with auto-callouts, we allow a callout to be
6138            specified before a condition that is an assertion. First, check for the
6139            syntax of a callout; if found, adjust the temporary pointer that is
6140            used to check for an assertion condition. That's all that is needed! */
6141    
6142            if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
6143              {
6144              for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
6145              if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
6146                tempptr += i + 1;
6147              }
6148    
6149          /* For conditions that are assertions, check the syntax, and then exit          /* For conditions that are assertions, check the syntax, and then exit
6150          the switch. This will take control down to where bracketed groups,          the switch. This will take control down to where bracketed groups,
6151          including assertions, are processed. */          including assertions, are processed. */
6152    
6153          if (ptr[1] == CHAR_QUESTION_MARK && (ptr[2] == CHAR_EQUALS_SIGN ||          if (tempptr[1] == CHAR_QUESTION_MARK &&
6154              ptr[2] == CHAR_EXCLAMATION_MARK || ptr[2] == CHAR_LESS_THAN_SIGN))                (tempptr[2] == CHAR_EQUALS_SIGN ||
6155                   tempptr[2] == CHAR_EXCLAMATION_MARK ||
6156                   tempptr[2] == CHAR_LESS_THAN_SIGN))
6157            break;            break;
6158    
6159          /* Most other conditions use OP_CREF (a couple change to OP_RREF          /* Other conditions use OP_CREF/OP_DNCREF/OP_RREF/OP_DNRREF, and all
6160          below), and all need to skip 1+IMM2_SIZE bytes at the start of the group. */          need to skip at least 1+IMM2_SIZE bytes at the start of the group. */
6161    
6162          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
6163          skipbytes = 1+IMM2_SIZE;          skipbytes = 1+IMM2_SIZE;
# Line 5752  for (;; ptr++) Line 6173  for (;; ptr++)
6173            }            }
6174    
6175          /* Check for a test for a named group's having been set, using the Perl          /* Check for a test for a named group's having been set, using the Perl
6176          syntax (?(<name>) or (?('name') */          syntax (?(<name>) or (?('name'), and also allow for the original PCRE
6177            syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). As names may
6178            consist entirely of digits, there is scope for ambiguity. */
6179    
6180          else if (ptr[1] == CHAR_LESS_THAN_SIGN)          else if (ptr[1] == CHAR_LESS_THAN_SIGN)
6181            {            {
# Line 5766  for (;; ptr++) Line 6189  for (;; ptr++)
6189            }            }
6190          else          else
6191            {            {
6192            terminator = 0;            terminator = CHAR_NULL;
6193            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);
6194            }            }
6195    
6196          /* We now expect to read a name; any thing else is an error */          /* When a name is one of a number of duplicates, a different opcode is
6197            used and it needs more memory. Unfortunately we cannot tell whether a
6198            name is a duplicate in the first pass, so we have to allow for more
6199            memory except when we know it is a relative numerical reference. */
6200    
6201            if (refsign < 0 && lengthptr != NULL) *lengthptr += IMM2_SIZE;
6202    
6203            /* We now expect to read a name (possibly all digits); any thing else
6204            is an error. In the case of all digits, also get it as a number. */
6205    
6206          if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)          if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)
6207            {