/[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 1219 by ph10, Sun Nov 11 18:04:37 2012 UTC revision 1372 by ph10, Fri Oct 11 17:05:19 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, int, int,    compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
88      pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,      pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,
89      compile_data *, int *);      compile_data *, int *);
90    
91    
# Line 115  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 455  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 487  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 508  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 647  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  /*************************************************  /*************************************************
783  *            Find an error text                  *  *            Find an error text                  *
# Line 674  return s; Line 805  return s;
805  }  }
806    
807    
808    
809  /*************************************************  /*************************************************
810  *           Expand the workspace                 *  *           Expand the workspace                 *
811  *************************************************/  *************************************************/
# Line 751  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 773  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 797  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 847  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 963  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 999  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 1007  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 1032  else Line 1166  else
1166      if (!utf && c > 0xff) *errorcodeptr = ERR51;      if (!utf && c > 0xff) *errorcodeptr = ERR51;
1167  #endif  #endif
1168      break;      break;
1169    
1170        /* \o is a relatively new Perl feature, supporting a more general way of
1171        specifying character codes in octal. The only supported form is \o{ddd}. */
1172    
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 >= 0x10000000l) { 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. \x{ddd} is a character number which can be greater      /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
1209      than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.      numbers. Otherwise it is a lowercase x letter. */
     If not, { is treated as a data character. */  
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 1058  else Line 1227  else
1227  #endif  #endif
1228            }            }
1229          }          }
1230        break;        }    /* End JavaScript handling */
1231        }  
1232        /* Handle \x in Perl's style. \x{ddd} is a character number which can be
1233      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
1234        {      digits. If not, { used to be treated as a data character. However, Perl
1235        const pcre_uchar *pt = ptr + 2;      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        c = 0;      now gives an error. */
1238        overflow = FALSE;  
1239        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)      else
1240          {
1241          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;            }
1282          break;  
1283          }          /* If the sequence of hex digits does not end with '}', give an error.
1284            We used just to recognize this construct and fall through to the normal
1285        /* If the sequence of hex digits does not end with '}', then we don't          \x handling, but nowadays Perl gives an error, which seems much more
1286        recognize this construct; fall through to the normal \x handling. */          sensible, so we do too. */
1287        }  
1288            else *errorcodeptr = ERR79;
1289            }   /* End of \x{} processing */
1290    
1291      /* Read just a single-byte hex-defined char */        /* Read a single-byte hex-defined char (up to two hex digits after \x) */
1292    
1293      c = 0;        else
1294      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)          {
1295        {          c = 0;
1296        pcre_uint32 cc;                          /* Some compilers don't like */          while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1297        cc = *(++ptr);                           /* ++ in initializers */            {
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 1190  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 1211  Returns:         TRUE if the type value Line 1395  Returns:         TRUE if the type value
1395  */  */
1396    
1397  static BOOL  static BOOL
1398  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1399    unsigned int *pdataptr, int *errorcodeptr)    unsigned int *pdataptr, int *errorcodeptr)
1400  {  {
1401  pcre_uchar c;  pcre_uchar c;
# Line 1287  return FALSE; Line 1471  return FALSE;
1471    
1472    
1473    
   
1474  /*************************************************  /*************************************************
1475  *         Read repeat counts                     *  *         Read repeat counts                     *
1476  *************************************************/  *************************************************/
# Line 1356  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 != CHAR_NULL; 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 != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
       if (*ptr != CHAR_NULL) 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) == CHAR_NULL) goto FAIL_EXIT;  
     if (*ptr == CHAR_Q) for (;;)  
       {  
       while (*(++ptr) != CHAR_NULL && *ptr != CHAR_BACKSLASH) {};  
       if (*ptr == CHAR_NULL) 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 == CHAR_NULL) return -1;  
       if (*ptr == CHAR_BACKSLASH)  
         {  
         if (*(++ptr) == CHAR_NULL) goto FAIL_EXIT;  
         if (*ptr == CHAR_Q) for (;;)  
           {  
           while (*(++ptr) != CHAR_NULL && *ptr != CHAR_BACKSLASH) {};  
           if (*ptr == CHAR_NULL) goto FAIL_EXIT;  
           if (*(++ptr) == CHAR_E) break;  
           }  
         continue;  
         }  
       }  
     continue;  
     }  
   
   /* Skip comments in /x mode */  
   
   if (xmode && *ptr == CHAR_NUMBER_SIGN)  
     {  
     ptr++;  
     while (*ptr != CHAR_NULL)  
       {  
       if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }  
       ptr++;  
 #ifdef SUPPORT_UTF  
       if (utf) FORWARDCHAR(ptr);  
 #endif  
       }  
     if (*ptr == CHAR_NULL) 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 == CHAR_NULL) 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++ == CHAR_NULL) break;  
   }  
   
 return rc;  
 }  
   
   
   
   
 /*************************************************  
1542  *      Find first significant op code            *  *      Find first significant op code            *
1543  *************************************************/  *************************************************/
1544    
# Line 1690  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 1706  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 1830  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 1879  for (;;) Line 1765  for (;;)
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 2009  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 2045  for (;;) Line 1933  for (;;)
1933    
1934    
1935    
   
1936  /*************************************************  /*************************************************
1937  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
1938  *************************************************/  *************************************************/
# Line 2122  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 2242  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 2353  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 pcre_uchar 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 2389  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        else
2303          {
2304          recurse_check *r = recurses;
2305          const pcre_uchar *endgroup = scode;
2306    
2307          do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2308          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2309    
2310      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)        for (r = recurses; r != NULL; r = r->prev)
2311        if ((int)GET(scode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;          if (r->group == scode) break;
2312          if (r != NULL) continue;   /* Mutual recursion */
2313          }
2314    
2315      /* Not a forward reference, test for completed backward reference */      /* Completed reference; scan the referenced group, remembering it on the
2316        stack chain to detect mutual recursions. */
2317    
2318      empty_branch = FALSE;      empty_branch = FALSE;
2319      scode = cd->start_code + GET(code, 1);      this_recurse.prev = recurses;
2320      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      this_recurse.group = scode;
   
     /* Completed backwards reference */  
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 2463  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 2521  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_ANY:
2439        case OP_ALLANY:
2440        case OP_ANYBYTE:
2441    
2442      case OP_PROP:      case OP_PROP:
2443      case OP_NOTPROP:      case OP_NOTPROP:
2444      case OP_EXTUNI:      case OP_ANYNL:
2445      case OP_NOT_DIGIT:  
2446      case OP_DIGIT:      case OP_NOT_HSPACE:
2447      case OP_NOT_WHITESPACE:      case OP_HSPACE:
2448        case OP_NOT_VSPACE:
2449        case OP_VSPACE:
2450        case OP_EXTUNI:
2451    
2452        case OP_NOT_DIGIT:
2453        case OP_DIGIT:
2454        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 2568  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 2582  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 2616  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 2662  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 2672  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.  
2642    
2643  Originally, this function only recognized a sequence of letters between the  Arguments:  c opcode
2644  terminators, but it seems that Perl recognizes any sequence of characters,  Returns:    base opcode for the type
2645  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:].  
2646    
2647  The problem in trying to be exactly like Perl is in the handling of escapes. We  static pcre_uchar
2648  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX  get_repeat_base(pcre_uchar c)
2649  class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code  {
2650  below handles the special case of \], but does not try to do any other escape  return (c > OP_TYPEPOSUPTO)? c :
2651  processing. This makes it different from Perl for cases such as [:l\ower:]         (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2652  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize         (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2653  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,         (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2654  I think.         (c >= OP_STARI)?      OP_STARI :
2655                                 OP_STAR;
2656    }
2657    
 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.  
2658    
2659  In Perl, unescaped square brackets may also appear as part of class names. For  
2660  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for  #ifdef SUPPORT_UCP
2661  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not  /*************************************************
2662  seem right at all. PCRE does not allow closing square brackets in POSIX class  *        Check a character and a property        *
2663  names.  *************************************************/
2664    
2665    /* This function is called by check_auto_possessive() when a property item
2666    is adjacent to a fixed character.
2667    
2668  Arguments:  Arguments:
2669    ptr      pointer to the initial [    c            the character
2670    endptr   where to return the end pointer    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:   TRUE or FALSE  Returns:       TRUE if auto-possessifying is OK
2675  */  */
2676    
2677  static BOOL  static BOOL
2678  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2679      BOOL negated)
2680  {  {
2681  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */  const pcre_uint32 *p;
2682  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  const ucd_record *prop = GET_UCD(c);
2683  for (++ptr; *ptr != CHAR_NULL; ptr++)  
2684    switch(ptype)
2685    {    {
2686    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)    case PT_LAMP:
2687      ptr++;    return (prop->chartype == ucp_Lu ||
2688    else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;            prop->chartype == ucp_Ll ||
2689    else            prop->chartype == ucp_Lt) == negated;
     {  
     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;  
 }  
2690    
2691      case PT_GC:
2692      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2693    
2694      case PT_PC:
2695      return (pdata == prop->chartype) == negated;
2696    
2697      case PT_SC:
2698      return (pdata == prop->script) == negated;
2699    
2700  /*************************************************    /* These are specials */
 *          Check POSIX class name                *  
 *************************************************/  
2701    
2702  /* This function is called to check the name given in a POSIX-style class entry    case PT_ALNUM:
2703  such as [:alnum:].    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2704              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2705    
2706  Arguments:    /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2707    ptr        points to the first letter    means that Perl space and POSIX space are now identical. PCRE was changed
2708    len        the length of the name    at release 8.34. */
2709    
2710      case PT_SPACE:    /* Perl space */
2711      case PT_PXSPACE:  /* POSIX space */
2712      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2713              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2714              c == CHAR_FF || c == CHAR_CR)
2715              == negated;
2716    
2717  Returns:     a value representing the name, or -1 if unknown    case PT_WORD:
2718  */    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2719              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2720              c == CHAR_UNDERSCORE) == negated;
2721    
2722  static int    case PT_CLIST:
2723  check_posix_name(const pcre_uchar *ptr, int len)    p = PRIV(ucd_caseless_sets) + prop->caseset;
2724  {    for (;;)
2725  const char *pn = posix_names;      {
2726  register int yield = 0;      if (c < *p) return !negated;
2727  while (posix_name_lengths[yield] != 0)      if (c == *p++) return negated;
2728    {      }
2729    if (len == posix_name_lengths[yield] &&    break;  /* Control never reaches here */
     STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;  
   pn += posix_name_lengths[yield] + 1;  
   yield++;  
2730    }    }
2731  return -1;  
2732    return FALSE;
2733  }  }
2734    #endif  /* SUPPORT_UCP */
2735    
2736    
2737    
2738  /*************************************************  /*************************************************
2739  *    Adjust OP_RECURSE items in repeated group   *  *        Fill the character property list        *
2740  *************************************************/  *************************************************/
2741    
2742  /* OP_RECURSE items contain an offset from the start of the regex to the group  /* Checks whether the code points to an opcode that can take part in auto-
2743  that is referenced. This means that groups can be replicated for fixed  possessification, and if so, fills a list with its properties.
 repetition simply by copying (because the recursion is allowed to refer to  
 earlier groups that are outside the current group). However, when a group is  
 optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is  
 inserted before it, after it has been compiled. This means that any OP_RECURSE  
 items within it that refer to the group itself or any contained groups have to  
 have their offsets adjusted. That one of the jobs of this function. Before it  
 is called, the partially compiled regex must be temporarily terminated with  
 OP_END.  
   
 This function has been extended with the possibility of forward references for  
 recursions and subroutine calls. It must also check the list of such references  
 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).  
2744    
2745  Arguments:  Arguments:
2746    group      points to the start of the group    code        points to start of expression
2747    adjust     the amount by which the group is to be moved    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2748    utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode    fcc         points to case-flipping table
2749    cd         contains pointers to tables etc.    list        points to output list
2750    save_hwm   the hwm forward reference pointer at the start of the group                list[0] will be filled with the opcode
2751                  list[1] will be non-zero if this opcode
2752                    can match an empty character string
2753                  list[2..7] depends on the opcode
2754    
2755  Returns:     nothing  Returns:      points to the start of the next opcode if *code is accepted
2756                  NULL if *code is not accepted
2757  */  */
2758    
2759  static void  static const pcre_uchar *
2760  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,  get_chr_property_list(const pcre_uchar *code, BOOL utf,
2761    pcre_uchar *save_hwm)    const pcre_uint8 *fcc, pcre_uint32 *list)
2762  {  {
2763  pcre_uchar *ptr = group;  pcre_uchar c = *code;
2764    const pcre_uchar *end;
2765    const pcre_uint32 *clist_src;
2766    pcre_uint32 *clist_dest;
2767    pcre_uint32 chr;
2768    pcre_uchar base;
2769    
2770  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)  list[0] = c;
2771    {  list[1] = FALSE;
2772    int offset;  code++;
   pcre_uchar *hc;  
2773    
2774    /* See if this recursion is on the forward reference list. If so, adjust the  if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2775    reference. */    {
2776      base = get_repeat_base(c);
2777      c -= (base - OP_STAR);
2778    
2779    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2780      {      code += IMM2_SIZE;
     offset = (int)GET(hc, 0);  
     if (cd->start_code + offset == ptr + 1)  
       {  
       PUT(hc, 0, offset + adjust);  
       break;  
       }  
     }  
2781    
2782    /* Otherwise, adjust the recursion offset if it's after the start of this    list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
   group. */  
2783    
2784    if (hc >= cd->hwm)    switch(base)
2785      {      {
2786      offset = (int)GET(ptr, 1);      case OP_STAR:
2787      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      list[0] = OP_CHAR;
2788      }      break;
2789    
2790    ptr += 1 + LINK_SIZE;      case OP_STARI:
2791    }      list[0] = OP_CHARI;
2792  }      break;
2793    
2794        case OP_NOTSTAR:
2795        list[0] = OP_NOT;
2796        break;
2797    
2798        case OP_NOTSTARI:
2799        list[0] = OP_NOTI;
2800        break;
2801    
2802  /*************************************************      case OP_TYPESTAR:
2803  *        Insert an automatic callout point       *      list[0] = *code;
2804  *************************************************/      code++;
2805        break;
2806        }
2807      c = list[0];
2808      }
2809    
2810  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert  switch(c)
2811  callout points before each pattern item.    {
2812      case OP_NOT_DIGIT:
2813      case OP_DIGIT:
2814      case OP_NOT_WHITESPACE:
2815      case OP_WHITESPACE:
2816      case OP_NOT_WORDCHAR:
2817      case OP_WORDCHAR:
2818      case OP_ANY:
2819      case OP_ALLANY:
2820      case OP_ANYNL:
2821      case OP_NOT_HSPACE:
2822      case OP_HSPACE:
2823      case OP_NOT_VSPACE:
2824      case OP_VSPACE:
2825      case OP_EXTUNI:
2826      case OP_EODN:
2827      case OP_EOD:
2828      case OP_DOLL:
2829      case OP_DOLLM:
2830      return code;
2831    
2832  Arguments:    case OP_CHAR:
2833    code           current code pointer    case OP_NOT:
2834    ptr            current pattern pointer    GETCHARINCTEST(chr, code);
2835    cd             pointers to tables etc    list[2] = chr;
2836      list[3] = NOTACHAR;
2837      return code;
2838    
2839  Returns:         new code pointer    case OP_CHARI:
2840  */    case OP_NOTI:
2841      list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2842      GETCHARINCTEST(chr, code);
2843      list[2] = chr;
2844    
2845  static pcre_uchar *  #ifdef SUPPORT_UCP
2846  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)    if (chr < 128 || (chr < 256 && !utf))
2847  {      list[3] = fcc[chr];
2848  *code++ = OP_CALLOUT;    else
2849  *code++ = 255;      list[3] = UCD_OTHERCASE(chr);
2850  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2851  PUT(code, LINK_SIZE, 0);                       /* Default length */    list[3] = (chr < 256) ? fcc[chr] : chr;
2852  return code + 2 * LINK_SIZE;  #else
2853  }    list[3] = fcc[chr];
2854    #endif
2855    
2856      /* The othercase might be the same value. */
2857    
2858      if (chr == list[3])
2859        list[3] = NOTACHAR;
2860      else
2861        list[4] = NOTACHAR;
2862      return code;
2863    
2864  /*************************************************  #ifdef SUPPORT_UCP
2865  *         Complete a callout item                *    case OP_PROP:
2866  *************************************************/    case OP_NOTPROP:
2867      if (code[0] != PT_CLIST)
2868        {
2869        list[2] = code[0];
2870        list[3] = code[1];
2871        return code + 2;
2872        }
2873    
2874  /* A callout item contains the length of the next item in the pattern, which    /* Convert only if we have anough space. */
 we can't fill in till after we have reached the relevant point. This is used  
 for both automatic and manual callouts.  
2875    
2876  Arguments:    clist_src = PRIV(ucd_caseless_sets) + code[1];
2877    previous_callout   points to previous callout item    clist_dest = list + 2;
2878    ptr                current pattern pointer    code += 2;
   cd                 pointers to tables etc  
2879    
2880  Returns:             nothing    do {
2881  */       /* Early return if there is not enough space. */
2882         if (clist_dest >= list + 8)
2883           {
2884           list[2] = code[0];
2885           list[3] = code[1];
2886           return code;
2887           }
2888         *clist_dest++ = *clist_src;
2889         }
2890       while(*clist_src++ != NOTACHAR);
2891    
2892  static void    /* Enough space to store all characters. */
2893  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)  
2894  {    list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2895  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));    return code;
2896  PUT(previous_callout, 2 + LINK_SIZE, length);  #endif
2897    
2898      case OP_NCLASS:
2899      case OP_CLASS:
2900    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2901      case OP_XCLASS:
2902    
2903      if (c == OP_XCLASS)
2904        end = code + GET(code, 0);
2905      else
2906    #endif
2907        end = code + 32 / sizeof(pcre_uchar);
2908    
2909      switch(*end)
2910        {
2911        case OP_CRSTAR:
2912        case OP_CRMINSTAR:
2913        case OP_CRQUERY:
2914        case OP_CRMINQUERY:
2915        list[1] = TRUE;
2916        end++;
2917        break;
2918    
2919        case OP_CRRANGE:
2920        case OP_CRMINRANGE:
2921        list[1] = (GET2(end, 1) == 0);
2922        end += 1 + 2 * IMM2_SIZE;
2923        break;
2924        }
2925      list[2] = end - code;
2926      return end;
2927      }
2928    return NULL;    /* Opcode not accepted */
2929  }  }
2930    
2931    
2932    
 #ifdef SUPPORT_UCP  
2933  /*************************************************  /*************************************************
2934  *           Get othercase range                  *  *    Scan further character sets for match       *
2935  *************************************************/  *************************************************/
2936    
2937  /* This function is passed the start and end of a class range, in UTF-8 mode  /* Checks whether the base and the current opcode have a common character, in
2938  with UCP support. It searches up the characters, looking for ranges of  which case the base cannot be possessified.
 characters in the "other" case. Each call returns the next one, updating the  
 start address. A character with multiple other cases is returned on its own  
 with a special return value.  
2939    
2940  Arguments:  Arguments:
2941    cptr        points to starting character value; updated    code        points to the byte code
2942    d           end value    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2943    ocptr       where to put start of othercase range    cd          static compile data
2944    odptr       where to put end of othercase range    base_list   the data list of the base opcode
2945    
2946  Yield:        -1 when no more  Returns:      TRUE if the auto-possessification is possible
                0 when a range is returned  
               >0 the CASESET offset for char with multiple other cases  
                 in this case, ocptr contains the original  
2947  */  */
2948    
2949  static int  static BOOL
2950  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,  compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2951    pcre_uint32 *odptr)    const pcre_uint32* base_list)
2952  {  {
2953  pcre_uint32 c, othercase, next;  pcre_uchar c;
2954  unsigned int co;  pcre_uint32 list[8];
2955    const pcre_uint32* chr_ptr;
2956    const pcre_uint32* ochr_ptr;
2957    const pcre_uint32* list_ptr;
2958    pcre_uint32 chr;
2959    
2960  /* Find the first character that has an other case. If it has multiple other  for(;;)
2961  cases, return its case offset value. */    {
2962      c = *code;
2963    
2964  for (c = *cptr; c <= d; c++)    /* Skip over callouts */
2965    {  
2966    if ((co = UCD_CASESET(c)) != 0)    if (c == OP_CALLOUT)
2967      {      {
2968      *ocptr = c++;   /* Character that has the set */      code += PRIV(OP_lengths)[c];
2969      *cptr = c;      /* Rest of input range */      continue;
2970      return (int)co;      }
     }  
   if ((othercase = UCD_OTHERCASE(c)) != c) break;  
   }  
2971    
2972  if (c > d) return -1;  /* Reached end of range */    if (c == OP_ALT)
2973        {
2974        do code += GET(code, 1); while (*code == OP_ALT);
2975        c = *code;
2976        }
2977    
2978  *ocptr = othercase;    switch(c)
2979  next = othercase + 1;      {
2980        case OP_END:
2981        /* TRUE only in greedy case. The non-greedy case could be replaced by an
2982        OP_EXACT, but it is probably not worth it. (And note that OP_EXACT uses
2983        more memory, which we cannot get at this stage.) */
2984    
2985  for (++c; c <= d; c++)      return base_list[1] != 0;
   {  
   if (UCD_OTHERCASE(c) != next) break;  
   next++;  
   }  
2986    
2987  *odptr = next - 1;     /* End of othercase range */      case OP_KET:
2988  *cptr = c;             /* Rest of input range */      /* If the bracket is capturing, and referenced by an OP_RECURSE, the
2989  return 0;      non-greedy case cannot be converted to a possessive form. We do not test
2990  }      the bracket type at the moment, but we might do it in the future to improve
2991        this condition. (But note that recursive calls are always atomic.) */
2992    
2993        if (base_list[1] == 0) return FALSE;
2994        code += PRIV(OP_lengths)[c];
2995        continue;
2996        }
2997    
2998      /* Check for a supported opcode, and load its properties. */
2999    
3000  /*************************************************    code = get_chr_property_list(code, utf, cd->fcc, list);
3001  *        Check a character and a property        *    if (code == NULL) return FALSE;    /* Unsupported */
 *************************************************/  
3002    
3003  /* This function is called by check_auto_possessive() when a property item    /* If either opcode is a small character list, set pointers for comparing
3004  is adjacent to a fixed character.    characters from that list with another list, or with a property. */
3005    
3006  Arguments:    if (base_list[0] == OP_CHAR)
3007    c            the character      {
3008    ptype        the property type      chr_ptr = base_list + 2;
3009    pdata        the data for the type      list_ptr = list;
3010    negated      TRUE if it's a negated property (\P or \p{^)      }
3011      else if (list[0] == OP_CHAR)
3012        {
3013        chr_ptr = list + 2;
3014        list_ptr = base_list;
3015        }
3016    
3017  Returns:       TRUE if auto-possessifying is OK    /* Some property combinations also acceptable. Unicode property opcodes are
3018  */    processed specially; the rest can be handled with a lookup table. */
3019    
3020      else
3021        {
3022        pcre_uint32 leftop, rightop;
3023    
3024        if (list[1] != 0) return FALSE;   /* Must match at least one character */
3025        leftop = base_list[0];
3026        rightop = list[0];
3027    
 static BOOL  
 check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated)  
 {  
3028  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3029  const pcre_uint32 *p;      if (leftop == OP_PROP || leftop == OP_NOTPROP)
3030  #endif        {
3031          if (rightop == OP_EOD) return TRUE;
3032          if (rightop == OP_PROP || rightop == OP_NOTPROP)
3033            {
3034            int n;
3035            const pcre_uint8 *p;
3036            BOOL same = leftop == rightop;
3037            BOOL lisprop = leftop == OP_PROP;
3038            BOOL risprop = rightop == OP_PROP;
3039            BOOL bothprop = lisprop && risprop;
3040    
3041            /* There's a table that specifies how each combination is to be
3042            processed:
3043              0   Always return FALSE (never auto-possessify)
3044              1   Character groups are distinct (possessify if both are OP_PROP)
3045              2   Check character categories in the same group (general or particular)
3046              3   Return TRUE if the two opcodes are not the same
3047              ... see comments below
3048            */
3049    
3050            n = propposstab[base_list[2]][list[2]];
3051            switch(n)
3052              {
3053              case 0: return FALSE;
3054              case 1: return bothprop;
3055              case 2: return (base_list[3] == list[3]) != same;
3056              case 3: return !same;
3057    
3058              case 4:  /* Left general category, right particular category */
3059              return risprop && catposstab[base_list[3]][list[3]] == same;
3060    
3061              case 5:  /* Right general category, left particular category */
3062              return lisprop && catposstab[list[3]][base_list[3]] == same;
3063    
3064              /* This code is logically tricky. Think hard before fiddling with it.
3065              The posspropstab table has four entries per row. Each row relates to
3066              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3067              Only WORD actually needs all four entries, but using repeats for the
3068              others means they can all use the same code below.
3069    
3070              The first two entries in each row are Unicode general categories, and
3071              apply always, because all the characters they include are part of the
3072              PCRE character set. The third and fourth entries are a general and a
3073              particular category, respectively, that include one or more relevant
3074              characters. One or the other is used, depending on whether the check
3075              is for a general or a particular category. However, in both cases the
3076              category contains more characters than the specials that are defined
3077              for the property being tested against. Therefore, it cannot be used
3078              in a NOTPROP case.
3079    
3080              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3081              Underscore is covered by ucp_P or ucp_Po. */
3082    
3083  const ucd_record *prop = GET_UCD(c);            case 6:  /* Left alphanum vs right general category */
3084              case 7:  /* Left space vs right general category */
3085              case 8:  /* Left word vs right general category */
3086              p = posspropstab[n-6];
3087              return risprop && lisprop ==
3088                (list[3] != p[0] &&
3089                 list[3] != p[1] &&
3090                (list[3] != p[2] || !lisprop));
3091    
3092              case 9:   /* Right alphanum vs left general category */
3093              case 10:  /* Right space vs left general category */
3094              case 11:  /* Right word vs left general category */
3095              p = posspropstab[n-9];
3096              return lisprop && risprop ==
3097                (base_list[3] != p[0] &&
3098                 base_list[3] != p[1] &&
3099                (base_list[3] != p[2] || !risprop));
3100    
3101              case 12:  /* Left alphanum vs right particular category */
3102              case 13:  /* Left space vs right particular category */
3103              case 14:  /* Left word vs right particular category */
3104              p = posspropstab[n-12];
3105              return risprop && lisprop ==
3106                (catposstab[p[0]][list[3]] &&
3107                 catposstab[p[1]][list[3]] &&
3108                (list[3] != p[3] || !lisprop));
3109    
3110              case 15:  /* Right alphanum vs left particular category */
3111              case 16:  /* Right space vs left particular category */
3112              case 17:  /* Right word vs left particular category */
3113              p = posspropstab[n-15];
3114              return lisprop && risprop ==
3115                (catposstab[p[0]][base_list[3]] &&
3116                 catposstab[p[1]][base_list[3]] &&
3117                (base_list[3] != p[3] || !risprop));
3118              }
3119            }
3120          return FALSE;
3121          }
3122    
3123  switch(ptype)      else
3124    {  #endif  /* SUPPORT_UCP */
   case PT_LAMP:  
   return (prop->chartype == ucp_Lu ||  
           prop->chartype == ucp_Ll ||  
           prop->chartype == ucp_Lt) == negated;  
3125    
3126    case PT_GC:      return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3127    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;             rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3128               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3129        }
3130    
3131    case PT_PC:    /* Control reaches here only if one of the items is a small character list.
3132    return (pdata == prop->chartype) == negated;    All characters are checked against the other side. */
3133    
3134    case PT_SC:    do
3135    return (pdata == prop->script) == negated;      {
3136        chr = *chr_ptr;
3137    
3138    /* These are specials */      switch(list_ptr[0])
3139          {
3140          case OP_CHAR:
3141          ochr_ptr = list_ptr + 2;
3142          do
3143            {
3144            if (chr == *ochr_ptr) return FALSE;
3145            ochr_ptr++;
3146            }
3147          while(*ochr_ptr != NOTACHAR);
3148          break;
3149    
3150    case PT_ALNUM:        case OP_NOT:
3151    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||        ochr_ptr = list_ptr + 2;
3152            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;        do
3153            {
3154            if (chr == *ochr_ptr)
3155              break;
3156            ochr_ptr++;
3157            }
3158          while(*ochr_ptr != NOTACHAR);
3159          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3160          break;
3161    
3162    case PT_SPACE:    /* Perl space */        /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3163    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||        set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
           c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)  
           == negated;  
3164    
3165    case PT_PXSPACE:  /* POSIX space */        case OP_DIGIT:
3166    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||        if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3167            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||        break;
           c == CHAR_FF || c == CHAR_CR)  
           == negated;  
3168    
3169    case PT_WORD:        case OP_NOT_DIGIT:
3170    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||        if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3171            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||        break;
3172            c == CHAR_UNDERSCORE) == negated;  
3173          case OP_WHITESPACE:
3174  #ifdef SUPPORT_UCP        if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3175    case PT_CLIST:        break;
3176    p = PRIV(ucd_caseless_sets) + prop->caseset;  
3177    for (;;)        case OP_NOT_WHITESPACE:
3178      {        if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3179      if (c < *p) return !negated;        break;
3180      if (c == *p++) return negated;  
3181      }        case OP_WORDCHAR:
3182    break;  /* Control never reaches here */        if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3183          break;
3184    
3185          case OP_NOT_WORDCHAR:
3186          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3187          break;
3188    
3189          case OP_HSPACE:
3190          switch(chr)
3191            {
3192            HSPACE_CASES: return FALSE;
3193            default: break;
3194            }
3195          break;
3196    
3197          case OP_NOT_HSPACE:
3198          switch(chr)
3199            {
3200            HSPACE_CASES: break;
3201            default: return FALSE;
3202            }
3203          break;
3204    
3205          case OP_ANYNL:
3206          case OP_VSPACE:
3207          switch(chr)
3208            {
3209            VSPACE_CASES: return FALSE;
3210            default: break;
3211            }
3212          break;
3213    
3214          case OP_NOT_VSPACE:
3215          switch(chr)
3216            {
3217            VSPACE_CASES: break;
3218            default: return FALSE;
3219            }
3220          break;
3221    
3222          case OP_DOLL:
3223          case OP_EODN:
3224          switch (chr)
3225            {
3226            case CHAR_CR:
3227            case CHAR_LF:
3228            case CHAR_VT:
3229            case CHAR_FF:
3230            case CHAR_NEL:
3231    #ifndef EBCDIC
3232            case 0x2028:
3233            case 0x2029:
3234    #endif  /* Not EBCDIC */
3235            return FALSE;
3236            }
3237          break;
3238    
3239          case OP_EOD:    /* Can always possessify before \z */
3240          break;
3241    
3242          case OP_PROP:
3243          case OP_NOTPROP:
3244          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3245                list_ptr[0] == OP_NOTPROP))
3246            return FALSE;
3247          break;
3248    
3249          /* The class comparisons work only when the class is the second item
3250          of the pair, because there are at present no possessive forms of the
3251          class opcodes. Note also that the "code" variable that is used below
3252          points after the second item, and that the pointer for the first item
3253          is not available, so even if there were possessive forms of the class
3254          opcodes, the correct comparison could not be done. */
3255    
3256          case OP_NCLASS:
3257          if (chr > 255) return FALSE;
3258          /* Fall through */
3259    
3260          case OP_CLASS:
3261          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3262          if (chr > 255) break;
3263          if ((((pcre_uint8 *)(code - list_ptr[2] + 1))[chr >> 3] & (1 << (chr & 7))) != 0)
3264            return FALSE;
3265          break;
3266    
3267    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3268          case OP_XCLASS:
3269          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3270          if (PRIV(xclass)(chr, code - list_ptr[2] + 1 + LINK_SIZE, utf))
3271            return FALSE;
3272          break;
3273  #endif  #endif
3274    
3275          default:
3276          return FALSE;
3277          }
3278    
3279        chr_ptr++;
3280        }
3281      while(*chr_ptr != NOTACHAR);
3282    
3283      /* At least one character must be matched from this opcode. */
3284    
3285      if (list[1] == 0) return TRUE;
3286    }    }
3287    
3288  return FALSE;  return FALSE;
3289  }  }
 #endif  /* SUPPORT_UCP */  
3290    
3291    
3292    
3293  /*************************************************  /*************************************************
3294  *     Check if auto-possessifying is possible    *  *    Scan compiled regex for auto-possession     *
3295  *************************************************/  *************************************************/
3296    
3297  /* This function is called for unlimited repeats of certain items, to see  /* Replaces single character iterations with their possessive alternatives
3298  whether the next thing could possibly match the repeated item. If not, it makes  if appropriate. This function modifies the compiled opcode!
 sense to automatically possessify the repeated item.  
3299    
3300  Arguments:  Arguments:
3301    previous      pointer to the repeated opcode    code        points to start of the byte code
3302    utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3303    ptr           next character in pattern    cd          static compile data
   options       options bits  
   cd            contains pointers to tables etc.  
3304    
3305  Returns:        TRUE if possessifying is wanted  Returns:      nothing
3306  */  */
3307    
3308  static BOOL  static void
3309  check_auto_possessive(const pcre_uchar *previous, BOOL utf,  auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
   const pcre_uchar *ptr, int options, compile_data *cd)  
3310  {  {
3311  pcre_uint32 c = NOTACHAR;  register pcre_uchar c;
3312  pcre_uint32 next;  const pcre_uchar *end;
3313  int escape;  pcre_uint32 list[8];
 pcre_uchar op_code = *previous++;  
   
 /* Skip whitespace and comments in extended mode */  
3314    
3315  if ((options & PCRE_EXTENDED) != 0)  for (;;)
3316    {    {
3317    for (;;)    c = *code;
3318    
3319      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3320      {      {
3321      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      c -= get_repeat_base(c) - OP_STAR;
3322      if (*ptr == CHAR_NUMBER_SIGN)      end = (c <= OP_MINUPTO) ?
3323          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3324        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3325    
3326        if (end != NULL && compare_opcodes(end, utf, cd, list))
3327        {        {
3328        ptr++;        switch(c)
       while (*ptr != CHAR_NULL)  
3329          {          {
3330          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          case OP_STAR:
3331          ptr++;          *code += OP_POSSTAR - OP_STAR;
3332  #ifdef SUPPORT_UTF          break;
3333          if (utf) FORWARDCHAR(ptr);  
3334  #endif          case OP_MINSTAR:
3335            *code += OP_POSSTAR - OP_MINSTAR;
3336            break;
3337    
3338            case OP_PLUS:
3339            *code += OP_POSPLUS - OP_PLUS;
3340            break;
3341    
3342            case OP_MINPLUS:
3343            *code += OP_POSPLUS - OP_MINPLUS;
3344            break;
3345    
3346            case OP_QUERY:
3347            *code += OP_POSQUERY - OP_QUERY;
3348            break;
3349    
3350            case OP_MINQUERY:
3351            *code += OP_POSQUERY - OP_MINQUERY;
3352            break;
3353    
3354            case OP_UPTO:
3355            *code += OP_POSUPTO - OP_UPTO;
3356            break;
3357    
3358            case OP_MINUPTO:
3359            *code += OP_MINUPTO - OP_UPTO;
3360            break;
3361          }          }
3362        }        }
3363      else break;      c = *code;
3364      }      }
   }  
3365    
3366  /* If the next item is one that we can handle, get its value. A non-negative    switch(c)
3367  value is a character, a negative value is an escape value. */      {
3368        case OP_END:
3369        return;
3370    
3371  if (*ptr == CHAR_BACKSLASH)      case OP_TYPESTAR:
3372    {      case OP_TYPEMINSTAR:
3373    int temperrorcode = 0;      case OP_TYPEPLUS:
3374    escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options, FALSE);      case OP_TYPEMINPLUS:
3375    if (temperrorcode != 0) return FALSE;      case OP_TYPEQUERY:
3376    ptr++;    /* Point after the escape sequence */      case OP_TYPEMINQUERY:
3377    }      case OP_TYPEPOSSTAR:
3378  else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)      case OP_TYPEPOSPLUS:
3379    {      case OP_TYPEPOSQUERY:
3380    escape = 0;      if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3381  #ifdef SUPPORT_UTF      break;
   if (utf) { GETCHARINC(next, ptr); } else  
 #endif  
   next = *ptr++;  
   }  
 else return FALSE;  
3382    
3383  /* Skip whitespace and comments in extended mode */      case OP_TYPEUPTO:
3384        case OP_TYPEMINUPTO:
3385        case OP_TYPEEXACT:
3386        case OP_TYPEPOSUPTO:
3387        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3388          code += 2;
3389        break;
3390    
3391  if ((options & PCRE_EXTENDED) != 0)      case OP_XCLASS:
3392    {      code += GET(code, 1);
3393    for (;;)      break;
3394      {  
3395      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      case OP_MARK:
3396      if (*ptr == CHAR_NUMBER_SIGN)      case OP_PRUNE_ARG:
3397        {      case OP_SKIP_ARG:
3398        ptr++;      case OP_THEN_ARG:
3399        while (*ptr != CHAR_NULL)      code += code[1];
3400          {      break;
         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }  
         ptr++;  
 #ifdef SUPPORT_UTF  
         if (utf) FORWARDCHAR(ptr);  
 #endif  
         }  
       }  
     else break;  
3401      }      }
   }  
3402    
3403  /* If the next thing is itself optional, we have to give up. */    /* Add in the fixed length from the table */
3404    
3405  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||    code += PRIV(OP_lengths)[c];
   STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)  
     return FALSE;  
3406    
3407  /* If the previous item is a character, get its value. */    /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3408      a multi-byte character. The length in the table is a minimum, so we have to
3409      arrange to skip the extra bytes. */
3410    
3411  if (op_code == OP_CHAR || op_code == OP_CHARI ||  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3412      op_code == OP_NOT || op_code == OP_NOTI)    if (utf) switch(c)
3413    {      {
3414  #ifdef SUPPORT_UTF      case OP_CHAR:
3415    GETCHARTEST(c, previous);      case OP_CHARI:
3416        case OP_NOT:
3417        case OP_NOTI:
3418        case OP_STAR:
3419        case OP_MINSTAR:
3420        case OP_PLUS:
3421        case OP_MINPLUS:
3422        case OP_QUERY:
3423        case OP_MINQUERY:
3424        case OP_UPTO:
3425        case OP_MINUPTO:
3426        case OP_EXACT:
3427        case OP_POSSTAR:
3428        case OP_POSPLUS:
3429        case OP_POSQUERY:
3430        case OP_POSUPTO:
3431        case OP_STARI:
3432        case OP_MINSTARI:
3433        case OP_PLUSI:
3434        case OP_MINPLUSI:
3435        case OP_QUERYI:
3436        case OP_MINQUERYI:
3437        case OP_UPTOI:
3438        case OP_MINUPTOI:
3439        case OP_EXACTI:
3440        case OP_POSSTARI:
3441        case OP_POSPLUSI:
3442        case OP_POSQUERYI:
3443        case OP_POSUPTOI:
3444        case OP_NOTSTAR:
3445        case OP_NOTMINSTAR:
3446        case OP_NOTPLUS:
3447        case OP_NOTMINPLUS:
3448        case OP_NOTQUERY:
3449        case OP_NOTMINQUERY:
3450        case OP_NOTUPTO:
3451        case OP_NOTMINUPTO:
3452        case OP_NOTEXACT:
3453        case OP_NOTPOSSTAR:
3454        case OP_NOTPOSPLUS:
3455        case OP_NOTPOSQUERY:
3456        case OP_NOTPOSUPTO:
3457        case OP_NOTSTARI:
3458        case OP_NOTMINSTARI:
3459        case OP_NOTPLUSI:
3460        case OP_NOTMINPLUSI:
3461        case OP_NOTQUERYI:
3462        case OP_NOTMINQUERYI:
3463        case OP_NOTUPTOI:
3464        case OP_NOTMINUPTOI:
3465        case OP_NOTEXACTI:
3466        case OP_NOTPOSSTARI:
3467        case OP_NOTPOSPLUSI:
3468        case OP_NOTPOSQUERYI:
3469        case OP_NOTPOSUPTOI:
3470        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3471        break;
3472        }
3473  #else  #else
3474    c = *previous;    (void)(utf);  /* Keep compiler happy by referencing function argument */
3475  #endif  #endif
3476    }    }
3477    }
3478    
 /* Now compare the next item with the previous opcode. First, handle cases when  
 the next item is a character. */  
3479    
 if (escape == 0)  
   {  
   /* For a caseless UTF match, the next character may have more than one other  
   case, which maps to the special PT_CLIST property. Check this first. */  
   
 #ifdef SUPPORT_UCP  
   if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)  
     {  
     unsigned int ocs = UCD_CASESET(next);  
     if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);  
     }  
 #endif  
3480    
3481    switch(op_code)  /*************************************************
3482      {  *           Check for POSIX class syntax         *
3483      case OP_CHAR:  *************************************************/
     return c != next;  
3484    
3485      /* For CHARI (caseless character) we must check the other case. If we have  /* This function is called when the sequence "[:" or "[." or "[=" is
3486      Unicode property support, we can use it to test the other case of  encountered in a character class. It checks whether this is followed by a
3487      high-valued characters. We know that next can have only one other case,  sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3488      because multi-other-case characters are dealt with above. */  reach an unescaped ']' without the special preceding character, return FALSE.
3489    
3490      case OP_CHARI:  Originally, this function only recognized a sequence of letters between the
3491      if (c == next) return FALSE;  terminators, but it seems that Perl recognizes any sequence of characters,
3492  #ifdef SUPPORT_UTF  though of course unknown POSIX names are subsequently rejected. Perl gives an
3493      if (utf)  "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3494        {  didn't consider this to be a POSIX class. Likewise for [:1234:].
3495        pcre_uint32 othercase;  
3496        if (next < 128) othercase = cd->fcc[next]; else  The problem in trying to be exactly like Perl is in the handling of escapes. We
3497  #ifdef SUPPORT_UCP  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3498        othercase = UCD_OTHERCASE(next);  class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3499  #else  below handles the special case of \], but does not try to do any other escape
3500        othercase = NOTACHAR;  processing. This makes it different from Perl for cases such as [:l\ower:]
3501  #endif  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
3502        return c != othercase;  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
3503        }  I think.
3504      else  
3505  #endif  /* SUPPORT_UTF */  A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3506      return (c != TABLE_GET(next, cd->fcc, next));  /* Not UTF */  It seems that the appearance of a nested POSIX class supersedes an apparent
3507    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3508      case OP_NOT:  a digit.
3509      return c == next;  
3510    In Perl, unescaped square brackets may also appear as part of class names. For
3511      case OP_NOTI:  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3512      if (c == next) return TRUE;  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3513  #ifdef SUPPORT_UTF  seem right at all. PCRE does not allow closing square brackets in POSIX class
3514      if (utf)  names.
3515    
3516    Arguments:
3517      ptr      pointer to the initial [
3518      endptr   where to return the end pointer
3519    
3520    Returns:   TRUE or FALSE
3521    */
3522    
3523    static BOOL
3524    check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3525    {
3526    pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3527    terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3528    for (++ptr; *ptr != CHAR_NULL; ptr++)
3529      {
3530      if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3531        ptr++;
3532      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3533      else
3534        {
3535        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3536        {        {
3537        pcre_uint32 othercase;        *endptr = ptr;
3538        if (next < 128) othercase = cd->fcc[next]; else        return TRUE;
 #ifdef SUPPORT_UCP  
       othercase = UCD_OTHERCASE(next);  
 #else  
       othercase = NOTACHAR;  
 #endif  
       return c == othercase;  
3539        }        }
3540      else      if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3541  #endif  /* SUPPORT_UTF */           (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3542      return (c == TABLE_GET(next, cd->fcc, next));  /* Not UTF */            ptr[1] == CHAR_EQUALS_SIGN) &&
3543            check_posix_syntax(ptr, endptr))
3544          return FALSE;
3545        }
3546      }
3547    return FALSE;
3548    }
3549    
     /* 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. */  
3550    
     case OP_DIGIT:  
     return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;  
3551    
     case OP_NOT_DIGIT:  
     return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;  
3552    
3553      case OP_WHITESPACE:  /*************************************************
3554      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;  *          Check POSIX class name                *
3555    *************************************************/
3556    
3557      case OP_NOT_WHITESPACE:  /* This function is called to check the name given in a POSIX-style class entry
3558      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;  such as [:alnum:].
3559    
3560      case OP_WORDCHAR:  Arguments:
3561      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;    ptr        points to the first letter
3562      len        the length of the name
3563    
3564      case OP_NOT_WORDCHAR:  Returns:     a value representing the name, or -1 if unknown
3565      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;  */
3566    
3567      case OP_HSPACE:  static int
3568      case OP_NOT_HSPACE:  check_posix_name(const pcre_uchar *ptr, int len)
3569      switch(next)  {
3570        {  const char *pn = posix_names;
3571        HSPACE_CASES:  register int yield = 0;
3572        return op_code == OP_NOT_HSPACE;  while (posix_name_lengths[yield] != 0)
3573      {
3574      if (len == posix_name_lengths[yield] &&
3575        STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3576      pn += posix_name_lengths[yield] + 1;
3577      yield++;
3578      }
3579    return -1;
3580    }
3581    
       default:  
       return op_code != OP_NOT_HSPACE;  
       }  
3582    
3583      case OP_ANYNL:  /*************************************************
3584      case OP_VSPACE:  *    Adjust OP_RECURSE items in repeated group   *
3585      case OP_NOT_VSPACE:  *************************************************/
     switch(next)  
       {  
       VSPACE_CASES:  
       return op_code == OP_NOT_VSPACE;  
3586    
3587        default:  /* OP_RECURSE items contain an offset from the start of the regex to the group
3588        return op_code != OP_NOT_VSPACE;  that is referenced. This means that groups can be replicated for fixed
3589        }  repetition simply by copying (because the recursion is allowed to refer to
3590    earlier groups that are outside the current group). However, when a group is
3591    optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
3592    inserted before it, after it has been compiled. This means that any OP_RECURSE
3593    items within it that refer to the group itself or any contained groups have to
3594    have their offsets adjusted. That one of the jobs of this function. Before it
3595    is called, the partially compiled regex must be temporarily terminated with
3596    OP_END.
3597    
3598  #ifdef SUPPORT_UCP  This function has been extended with the possibility of forward references for
3599      case OP_PROP:  recursions and subroutine calls. It must also check the list of such references
3600      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
3601    the current group is on this list, it adjusts the offset in the list, not the
3602    value in the reference (which is a group number).
3603    
3604      case OP_NOTPROP:  Arguments:
3605      return check_char_prop(next, previous[0], previous[1], TRUE);    group      points to the start of the group
3606  #endif    adjust     the amount by which the group is to be moved
3607      utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3608      cd         contains pointers to tables etc.
3609      save_hwm   the hwm forward reference pointer at the start of the group
3610    
3611      default:  Returns:     nothing
3612      return FALSE;  */
     }  
   }  
3613    
3614  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  static void
3615  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,
3616  generated only when PCRE_UCP is *not* set, that is, when only ASCII    pcre_uchar *save_hwm)
3617  characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are  {
3618  replaced by OP_PROP codes when PCRE_UCP is set. */  pcre_uchar *ptr = group;
3619    
3620  switch(op_code)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
3621    {    {
3622    case OP_CHAR:    int offset;
3623    case OP_CHARI:    pcre_uchar *hc;
3624    switch(escape)  
3625      /* See if this recursion is on the forward reference list. If so, adjust the
3626      reference. */
3627    
3628      for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
3629      {      {
3630      case ESC_d:      offset = (int)GET(hc, 0);
3631      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;      if (cd->start_code + offset == ptr + 1)
3632          {
3633          PUT(hc, 0, offset + adjust);
3634          break;
3635          }
3636        }
3637    
3638      case ESC_D:    /* Otherwise, adjust the recursion offset if it's after the start of this
3639      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;    group. */
3640    
3641      case ESC_s:    if (hc >= cd->hwm)
3642      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;      {
3643        offset = (int)GET(ptr, 1);
3644        if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
3645        }
3646    
3647      case ESC_S:    ptr += 1 + LINK_SIZE;
3648      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;    }
3649    }
3650    
     case ESC_w:  
     return c > 255 || (cd->ctypes[c] & ctype_word) == 0;  
3651    
     case ESC_W:  
     return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;  
3652    
3653      case ESC_h:  /*************************************************
3654      case ESC_H:  *        Insert an automatic callout point       *
3655      switch(c)  *************************************************/
       {  
       HSPACE_CASES:  
       return escape != ESC_h;  
   
       default:  
       return escape == ESC_h;  
       }  
3656    
3657      case ESC_v:  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
3658      case ESC_V:  callout points before each pattern item.
     switch(c)  
       {  
       VSPACE_CASES:  
       return escape != ESC_v;  
3659    
3660        default:  Arguments:
3661        return escape == ESC_v;    code           current code pointer
3662        }    ptr            current pattern pointer
3663      cd             pointers to tables etc
3664    
3665      /* When PCRE_UCP is set, these values get generated for \d etc. Find  Returns:         new code pointer
3666      their substitutions and process them. The result will always be either  */
     ESC_p or ESC_P. Then fall through to process those values. */  
3667    
3668  #ifdef SUPPORT_UCP  static pcre_uchar *
3669      case ESC_du:  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
3670      case ESC_DU:  {
3671      case ESC_wu:  *code++ = OP_CALLOUT;
3672      case ESC_WU:  *code++ = 255;
3673      case ESC_su:  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
3674      case ESC_SU:  PUT(code, LINK_SIZE, 0);                       /* Default length */
3675        {  return code + 2 * LINK_SIZE;
3676        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 */  
3677    
     case ESC_p:  
     case ESC_P:  
       {  
       unsigned int ptype = 0, pdata = 0;  
       int errorcodeptr;  
       BOOL negated;  
3678    
       ptr--;      /* Make ptr point at the p or P */  
       if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr))  
         return FALSE;  
       ptr++;      /* Point past the final curly ket */  
3679    
3680        /* If the property item is optional, we have to give up. (When generated  /*************************************************
3681        from \d etc by PCRE_UCP, this test will have been applied much earlier,  *         Complete a callout item                *
3682        to the original \d etc. At this point, ptr will point to a zero byte. */  *************************************************/
3683    
3684        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  /* A callout item contains the length of the next item in the pattern, which
3685          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
3686            return FALSE;  for both automatic and manual callouts.
3687    
3688        /* Do the property check. */  Arguments:
3689      previous_callout   points to previous callout item
3690      ptr                current pattern pointer
3691      cd                 pointers to tables etc
3692    
3693        return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);  Returns:             nothing
3694        }  */
 #endif  
3695    
3696      default:  static void
3697      return FALSE;  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
3698      }  {
3699    int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
3700    PUT(previous_callout, 2 + LINK_SIZE, length);
3701    }
3702    
   /* 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.) */  
3703    
   case OP_DIGIT:  
   return escape == ESC_D || escape == ESC_s || escape == ESC_W ||  
          escape == ESC_h || escape == ESC_v || escape == ESC_R;  
3704    
3705    case OP_NOT_DIGIT:  #ifdef SUPPORT_UCP
3706    return escape == ESC_d;  /*************************************************
3707    *           Get othercase range                  *
3708    *************************************************/
3709    
3710    case OP_WHITESPACE:  /* This function is passed the start and end of a class range, in UTF-8 mode
3711    return escape == ESC_S || escape == ESC_d || escape == ESC_w;  with UCP support. It searches up the characters, looking for ranges of
3712    characters in the "other" case. Each call returns the next one, updating the
3713    start address. A character with multiple other cases is returned on its own
3714    with a special return value.
3715    
3716    case OP_NOT_WHITESPACE:  Arguments:
3717    return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;    cptr        points to starting character value; updated
3718      d           end value
3719      ocptr       where to put start of othercase range
3720      odptr       where to put end of othercase range
3721    
3722    case OP_HSPACE:  Yield:        -1 when no more
3723    return escape == ESC_S || escape == ESC_H || escape == ESC_d ||                 0 when a range is returned
3724           escape == ESC_w || escape == ESC_v || escape == ESC_R;                >0 the CASESET offset for char with multiple other cases
3725                    in this case, ocptr contains the original
3726    */
3727    
3728    case OP_NOT_HSPACE:  static int
3729    return escape == ESC_h;  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
3730      pcre_uint32 *odptr)
3731    {
3732    pcre_uint32 c, othercase, next;
3733    unsigned int co;
3734    
3735    /* 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
3736    case OP_ANYNL:  cases, return its case offset value. */
   case OP_VSPACE:  
   return escape == ESC_V || escape == ESC_d || escape == ESC_w;  
3737    
3738    case OP_NOT_VSPACE:  for (c = *cptr; c <= d; c++)
3739    return escape == ESC_v || escape == ESC_R;    {
3740      if ((co = UCD_CASESET(c)) != 0)
3741        {
3742        *ocptr = c++;   /* Character that has the set */
3743        *cptr = c;      /* Rest of input range */
3744        return (int)co;
3745        }
3746      if ((othercase = UCD_OTHERCASE(c)) != c) break;
3747      }
3748    
3749    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;  
3750    
3751    case OP_NOT_WORDCHAR:  *ocptr = othercase;
3752    return escape == ESC_w || escape == ESC_d;  next = othercase + 1;
3753    
3754    default:  for (++c; c <= d; c++)
3755    return FALSE;    {
3756      if (UCD_OTHERCASE(c) != next) break;
3757      next++;
3758    }    }
3759    
3760  /* Control does not reach here */  *odptr = next - 1;     /* End of othercase range */
3761    *cptr = c;             /* Rest of input range */
3762    return 0;
3763  }  }
3764    #endif  /* SUPPORT_UCP */
3765    
3766    
3767    
# Line 3428  switch(op_code) Line 3770  switch(op_code)
3770  *************************************************/  *************************************************/
3771    
3772  /* 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
3773  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
3774  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
3775  mutually recursive with the function immediately below.  mutually recursive with the function immediately below.
3776    
3777  Arguments:  Arguments:
3778    classbits     the bit map for characters < 256    classbits     the bit map for characters < 256
3779    uchardptr     points to the pointer for extra data    uchardptr     points to the pointer for extra data
3780    options       the options word    options       the options word
3781    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
3782    start         start of range character    start         start of range character
3783    end           end of range character    end           end of range character
3784    
3785  Returns:        the number of < 256 characters added  Returns:        the number of < 256 characters added
3786                  the pointer to extra data is updated                  the pointer to extra data is updated
3787  */  */
# Line 3451  add_to_class(pcre_uint8 *classbits, pcre Line 3793  add_to_class(pcre_uint8 *classbits, pcre
3793  pcre_uint32 c;  pcre_uint32 c;
3794  int n8 = 0;  int n8 = 0;
3795    
3796  /* If caseless matching is required, scan the range and process alternate  /* If caseless matching is required, scan the range and process alternate
3797  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
3798  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
3799  range. */  range. */
3800    
3801  if ((options & PCRE_CASELESS) != 0)  if ((options & PCRE_CASELESS) != 0)
3802    {    {
3803  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3804    if ((options & PCRE_UTF8) != 0)    if ((options & PCRE_UTF8) != 0)
3805      {      {
3806      int rc;      int rc;
3807      pcre_uint32 oc, od;      pcre_uint32 oc, od;
3808    
3809      options &= ~PCRE_CASELESS;   /* Remove for recursive calls */      options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3810      c = start;      c = start;
3811    
3812      while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)      while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3813        {        {
3814        /* Handle a single character that has more than one other case. */        /* Handle a single character that has more than one other case. */
3815    
3816        if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,        if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3817          PRIV(ucd_caseless_sets) + rc, oc);          PRIV(ucd_caseless_sets) + rc, oc);
3818    
3819        /* Do nothing if the other case range is within the original range. */        /* Do nothing if the other case range is within the original range. */
3820    
3821        else if (oc >= start && od <= end) continue;        else if (oc >= start && od <= end) continue;
3822    
3823        /* 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
3824        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
3825        range. Otherwise, use a recursive call to add the additional range. */        range. Otherwise, use a recursive call to add the additional range. */
3826    
3827        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3828        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3829        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
# Line 3491  if ((options & PCRE_CASELESS) != 0) Line 3833  if ((options & PCRE_CASELESS) != 0)
3833  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3834    
3835    /* Not UTF-mode, or no UCP */    /* Not UTF-mode, or no UCP */
3836    
3837    for (c = start; c <= end && c < 256; c++)    for (c = start; c <= end && c < 256; c++)
3838      {      {
3839      SETBIT(classbits, cd->fcc[c]);      SETBIT(classbits, cd->fcc[c]);
3840      n8++;      n8++;
3841      }      }
3842    }    }
3843    
3844  /* 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
3845  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
3846  in all cases. */  in all cases. */
# Line 3524  if (end < 0x100) Line 3866  if (end < 0x100)
3866    {    {
3867    for (c = start; c <= end; c++)    for (c = start; c <= end; c++)
3868      {      {
3869      n8++;      n8++;
3870      SETBIT(classbits, c);      SETBIT(classbits, c);
3871      }      }
3872    }    }
3873    
3874  else  else
3875    {    {
3876    pcre_uchar *uchardata = *uchardptr;    pcre_uchar *uchardata = *uchardptr;
3877    
3878  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3879    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3880      {      {
3881      if (start < end)      if (start < end)
3882        {        {
3883        *uchardata++ = XCL_RANGE;        *uchardata++ = XCL_RANGE;
3884        uchardata += PRIV(ord2utf)(start, uchardata);        uchardata += PRIV(ord2utf)(start, uchardata);
3885        uchardata += PRIV(ord2utf)(end, uchardata);        uchardata += PRIV(ord2utf)(end, uchardata);
3886        }        }
3887      else if (start == end)      else if (start == end)
3888        {        {
3889        *uchardata++ = XCL_SINGLE;        *uchardata++ = XCL_SINGLE;
3890        uchardata += PRIV(ord2utf)(start, uchardata);        uchardata += PRIV(ord2utf)(start, uchardata);
3891        }        }
3892      }      }
3893    else    else
3894  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
3895    
3896    /* Without UTF support, character values are constrained by the bit length,    /* Without UTF support, character values are constrained by the bit length,
3897    and can only be > 256 for 16-bit and 32-bit libraries. */    and can only be > 256 for 16-bit and 32-bit libraries. */
3898    
3899  #ifdef COMPILE_PCRE8  #ifdef COMPILE_PCRE8
3900      {}      {}
3901  #else  #else
3902    if (start < end)    if (start < end)
3903      {      {
3904      *uchardata++ = XCL_RANGE;      *uchardata++ = XCL_RANGE;
# Line 3567  else Line 3909  else
3909      {      {
3910      *uchardata++ = XCL_SINGLE;      *uchardata++ = XCL_SINGLE;
3911      *uchardata++ = start;      *uchardata++ = start;
3912      }      }
3913  #endif  #endif
3914    
3915    *uchardptr = uchardata;   /* Updata extra data pointer */    *uchardptr = uchardata;   /* Updata extra data pointer */
3916    }    }
3917    
3918  return n8;    /* Number of 8-bit characters */  return n8;    /* Number of 8-bit characters */
3919  }  }
3920    
3921    
3922    
3923    
3924  /*************************************************  /*************************************************
3925  *        Add a list of characters to a class     *  *        Add a list of characters to a class     *
3926  *************************************************/  *************************************************/
3927    
3928  /* 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
3929  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
3930  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
3931  handled appropriately. This function is mutually recursive with the function  handled appropriately. This function is mutually recursive with the function
# Line 3593  Arguments: Line 3935  Arguments:
3935    classbits     the bit map for characters < 256    classbits     the bit map for characters < 256
3936    uchardptr     points to the pointer for extra data    uchardptr     points to the pointer for extra data
3937    options       the options word    options       the options word
3938    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
3939    p             points to row of 32-bit values, terminated by NOTACHAR    p             points to row of 32-bit values, terminated by NOTACHAR
3940    except        character to omit; this is used when adding lists of    except        character to omit; this is used when adding lists of
3941                    case-equivalent characters to avoid including the one we                    case-equivalent characters to avoid including the one we
3942                    already know about                    already know about
3943    
3944  Returns:        the number of < 256 characters added  Returns:        the number of < 256 characters added
3945                  the pointer to extra data is updated                  the pointer to extra data is updated
3946  */  */
# Line 3612  while (p[0] < NOTACHAR) Line 3954  while (p[0] < NOTACHAR)
3954    {    {
3955    int n = 0;    int n = 0;
3956    if (p[0] != except)    if (p[0] != except)
3957      {      {
3958      while(p[n+1] == p[0] + n + 1) n++;      while(p[n+1] == p[0] + n + 1) n++;
3959      n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);      n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3960      }      }
3961    p += n + 1;    p += n + 1;
3962    }    }
3963  return n8;  return n8;
3964  }  }
3965    
3966    
3967    
# Line 3634  Arguments: Line 3976  Arguments:
3976    classbits     the bit map for characters < 256    classbits     the bit map for characters < 256
3977    uchardptr     points to the pointer for extra data    uchardptr     points to the pointer for extra data
3978    options       the options word    options       the options word
3979    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
3980    p             points to row of 32-bit values, terminated by NOTACHAR    p             points to row of 32-bit values, terminated by NOTACHAR
3981    
3982  Returns:        the number of < 256 characters added  Returns:        the number of < 256 characters added
3983                  the pointer to extra data is updated                  the pointer to extra data is updated
3984  */  */
3985    
3986  static int  static int
3987  add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,  add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3988    int options, compile_data *cd, const pcre_uint32 *p)    int options, compile_data *cd, const pcre_uint32 *p)
3989  {  {
3990  BOOL utf = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
# Line 3654  while (p[0] < NOTACHAR) Line 3996  while (p[0] < NOTACHAR)
3996    while (p[1] == p[0] + 1) p++;    while (p[1] == p[0] + 1) p++;
3997    n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,    n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3998      (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);      (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
3999    p++;    p++;
4000    }    }
4001  return n8;  return n8;
4002  }  }
4003    
4004    
4005    
# Line 3672  to find out the amount of memory needed, Line 4014  to find out the amount of memory needed,
4014  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
4015    
4016  Arguments:  Arguments:
4017    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
4018    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
4019    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
4020    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
4021    firstcharptr    place to put the first required character    firstcharptr      place to put the first required character
4022    firstcharflagsptr place to put the first character flags, or a negative number    firstcharflagsptr place to put the first character flags, or a negative number
4023    reqcharptr     place to put the last required character    reqcharptr        place to put the last required character
4024    reqcharflagsptr place to put the last required character flags, or a negative number    reqcharflagsptr   place to put the last required character flags, or a negative number
4025    bcptr          points to current branch chain    bcptr             points to current branch chain
4026    cond_depth     conditional nesting depth    cond_depth        conditional nesting depth
4027    cd             contains pointers to tables etc.    cd                contains pointers to tables etc.
4028    lengthptr      NULL during the real compile phase    lengthptr         NULL during the real compile phase
4029                   points to length accumulator during pre-compile phase                      points to length accumulator during pre-compile phase
4030    
4031  Returns:         TRUE on success  Returns:            TRUE on success
4032                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
4033  */  */
4034    
4035  static BOOL  static BOOL
# Line 3741  BOOL utf = FALSE; Line 4083  BOOL utf = FALSE;
4083  #endif  #endif
4084    
4085  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
4086  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,
4087  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
4088  alternative calls for the different cases. */  alternative calls for the different cases. */
4089    
4090  pcre_uchar *class_uchardata;  pcre_uchar *class_uchardata;
# Line 3912  for (;; ptr++) Line 4254  for (;; ptr++)
4254        }        }
4255      }      }
4256    
   /* Fill in length of a previous callout, except when the next thing is  
   a quantifier. */  
   
4257    is_quantifier =    is_quantifier =
4258      c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||      c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
4259      (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));      (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
4260    
4261    if (!is_quantifier && previous_callout != NULL &&    /* Fill in length of a previous callout, except when the next thing is a
4262      quantifier or when processing a property substitution string in UCP mode. */
4263    
4264      if (!is_quantifier && previous_callout != NULL && nestptr == NULL &&
4265         after_manual_callout-- <= 0)         after_manual_callout-- <= 0)
4266      {      {
4267      if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */      if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */
# Line 3950  for (;; ptr++) Line 4292  for (;; ptr++)
4292        }        }
4293      }      }
4294    
4295    /* No auto callout for quantifiers. */    /* No auto callout for quantifiers, or while processing property strings that
4296      are substituted for \w etc in UCP mode. */
4297    
4298    if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier)    if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL)
4299      {      {
4300      previous_callout = code;      previous_callout = code;
4301      code = auto_callout(code, ptr, cd);      code = auto_callout(code, ptr, cd);
# Line 4136  for (;; ptr++) Line 4479  for (;; ptr++)
4479        /* In the pre-compile phase, accumulate the length of any extra        /* In the pre-compile phase, accumulate the length of any extra
4480        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
4481        contain a zillion > 255 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
4482        (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,
4483        however. */        however. */
4484    
4485        if (lengthptr != NULL && class_uchardata > class_uchardata_base)        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
# Line 4200  for (;; ptr++) Line 4543  for (;; ptr++)
4543          alpha. This relies on the fact that the class table starts with          alpha. This relies on the fact that the class table starts with
4544          alpha, lower, upper as the first 3 entries. */          alpha, lower, upper as the first 3 entries. */
4545    
4546          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
4547            posix_class = 0;            posix_class = 0;
4548    
4549          /* 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 4277  for (;; ptr++) Line 4620  for (;; ptr++)
4620    
4621        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
4622          {          {
4623          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options, TRUE);          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
4624              TRUE);
4625          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
4626            if (escape == 0) c = ec;
         if (escape == 0)  
           c = ec;  
4627          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 */
4628          else if (escape == ESC_N)            /* \N is not supported in a class */          else if (escape == ESC_N)          /* \N is not supported in a class */
4629            {            {
4630            *errorcodeptr = ERR71;            *errorcodeptr = ERR71;
4631            goto FAILED;            goto FAILED;
# Line 4340  for (;; ptr++) Line 4681  for (;; ptr++)
4681              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
4682              continue;              continue;
4683    
4684              /* 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
4685              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
4686              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and              previously set by something earlier in the character class.
4687              EBCDIC, so we lazily just adjust the appropriate bit. */              Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
4688                we could just adjust the appropriate bit. From PCRE 8.34 we no
4689                longer treat \s and \S specially. */
4690    
4691              case ESC_s:              case ESC_s:
4692              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];  
4693              continue;              continue;
4694    
4695              case ESC_S:              case ESC_S:
4696              should_flip_negation = TRUE;              should_flip_negation = TRUE;
4697              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 */  
4698              continue;              continue;
4699    
4700              /* The rest apply in both UCP and non-UCP cases. */              /* The rest apply in both UCP and non-UCP cases. */
4701    
4702              case ESC_h:              case ESC_h:
4703              (void)add_list_to_class(classbits, &class_uchardata, options, cd,              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4704                PRIV(hspace_list), NOTACHAR);                PRIV(hspace_list), NOTACHAR);
4705              continue;              continue;
4706    
4707              case ESC_H:              case ESC_H:
4708              (void)add_not_list_to_class(classbits, &class_uchardata, options,              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4709                cd, PRIV(hspace_list));                cd, PRIV(hspace_list));
4710              continue;              continue;
4711    
4712              case ESC_v:              case ESC_v:
4713              (void)add_list_to_class(classbits, &class_uchardata, options, cd,              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4714                PRIV(vspace_list), NOTACHAR);                PRIV(vspace_list), NOTACHAR);
4715              continue;              continue;
4716    
4717              case ESC_V:              case ESC_V:
4718              (void)add_not_list_to_class(classbits, &class_uchardata, options,              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4719                cd, PRIV(vspace_list));                cd, PRIV(vspace_list));
4720              continue;              continue;
4721    
4722  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 4414  for (;; ptr++) Line 4754  for (;; ptr++)
4754    
4755          /* Fall through if the escape just defined a single character (c >= 0).          /* Fall through if the escape just defined a single character (c >= 0).
4756          This may be greater than 256. */          This may be greater than 256. */
4757    
4758          escape = 0;          escape = 0;
4759    
4760          }   /* End of backslash handling */          }   /* End of backslash handling */
# Line 4455  for (;; ptr++) Line 4795  for (;; ptr++)
4795            inescq = TRUE;            inescq = TRUE;
4796            break;            break;
4797            }            }
4798    
4799          /* 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
4800          back the pointer and jump to handle the character that preceded it. */          back the pointer and jump to handle the character that preceded it. */
4801    
# Line 4464  for (;; ptr++) Line 4804  for (;; ptr++)
4804            ptr = oldptr;            ptr = oldptr;
4805            goto CLASS_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4806            }            }
4807    
4808          /* Otherwise, we have a potential range; pick up the next character */          /* Otherwise, we have a potential range; pick up the next character */
4809    
4810  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
# Line 4511  for (;; ptr++) Line 4851  for (;; ptr++)
4851          /* We have found a character range, so single character optimizations          /* We have found a character range, so single character optimizations
4852          cannot be done anymore. Any value greater than 1 indicates that there          cannot be done anymore. Any value greater than 1 indicates that there
4853          is more than one character. */          is more than one character. */
4854    
4855          class_one_char = 2;          class_one_char = 2;
4856    
4857          /* 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. */
4858    
4859          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4860    
4861          class_has_8bitchar +=          class_has_8bitchar +=
4862            add_to_class(classbits, &class_uchardata, options, cd, c, d);            add_to_class(classbits, &class_uchardata, options, cd, c, d);
4863    
4864          continue;   /* Go get the next char in the class */          continue;   /* Go get the next char in the class */
4865          }          }
4866    
4867        /* 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
4868        char, or after \ that introduces a single character or for an apparent        char, or after \ that introduces a single character or for an apparent
4869        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
4870        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
4871        with a zillion characters in it. */        with a zillion characters in it. */
4872    
4873        CLASS_SINGLE_CHARACTER:        CLASS_SINGLE_CHARACTER:
# Line 4550  for (;; ptr++) Line 4890  for (;; ptr++)
4890    
4891          if (negate_class)          if (negate_class)
4892            {            {
4893  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4894            int d;            int d;
4895  #endif  #endif
4896            if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;            if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4897            zerofirstchar = firstchar;            zerofirstchar = firstchar;
4898            zerofirstcharflags = firstcharflags;            zerofirstcharflags = firstcharflags;
# Line 4560  for (;; ptr++) Line 4900  for (;; ptr++)
4900            /* For caseless UTF-8 mode when UCP support is available, check            /* For caseless UTF-8 mode when UCP support is available, check
4901            whether this character has more than one other case. If so, generate            whether this character has more than one other case. If so, generate
4902            a special OP_NOTPROP item instead of OP_NOTI. */            a special OP_NOTPROP item instead of OP_NOTI. */
4903    
4904  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4905            if (utf && (options & PCRE_CASELESS) != 0 &&            if (utf && (options & PCRE_CASELESS) != 0 &&
4906                (d = UCD_CASESET(c)) != 0)                (d = UCD_CASESET(c)) != 0)
4907              {              {
4908              *code++ = OP_NOTPROP;              *code++ = OP_NOTPROP;
4909              *code++ = PT_CLIST;              *code++ = PT_CLIST;
4910              *code++ = d;              *code++ = d;
4911              }              }
4912            else            else
4913  #endif  #endif
4914            /* Char has only one other case, or UCP not available */            /* Char has only one other case, or UCP not available */
4915    
# Line 4582  for (;; ptr++) Line 4922  for (;; ptr++)
4922  #endif  #endif
4923                *code++ = c;                *code++ = c;
4924              }              }
4925    
4926            /* We are finished with this character class */            /* We are finished with this character class */
4927    
4928            goto END_CLASS;            goto END_CLASS;
4929            }            }
4930    
# Line 4602  for (;; ptr++) Line 4942  for (;; ptr++)
4942            }            }
4943          goto ONE_CHAR;          goto ONE_CHAR;
4944          }       /* End of 1-char optimization */          }       /* End of 1-char optimization */
4945    
4946        /* 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
4947        has been generated. Add this character to the class. */        has been generated. Add this character to the class. */
4948    
4949        class_has_8bitchar +=        class_has_8bitchar +=
4950          add_to_class(classbits, &class_uchardata, options, cd, c, c);          add_to_class(classbits, &class_uchardata, options, cd, c, c);
4951        }        }
4952    
# Line 4627  for (;; ptr++) Line 4967  for (;; ptr++)
4967        goto FAILED;        goto FAILED;
4968        }        }
4969    
4970      /* 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
4971      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
4972      phase, class_uchardata gets emptied to prevent workspace overflow, so it      phase, class_uchardata gets emptied to prevent workspace overflow, so it
4973      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
4974      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
4975      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
4976      instead of just doing a test on class_uchardata below. */      instead of just doing a test on class_uchardata below. */
4977    
4978  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4979      if (class_uchardata > class_uchardata_base) xclass = TRUE;      if (class_uchardata > class_uchardata_base) xclass = TRUE;
4980  #endif  #endif
# Line 4703  for (;; ptr++) Line 5043  for (;; ptr++)
5043        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
5044        }        }
5045      code += 32 / sizeof(pcre_uchar);      code += 32 / sizeof(pcre_uchar);
5046    
5047      END_CLASS:      END_CLASS:
5048      break;      break;
5049    
# Line 4853  for (;; ptr++) Line 5193  for (;; ptr++)
5193            }            }
5194          }          }
5195    
       /* 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;  
         }  
   
5196        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
5197        }        }
5198    
# Line 4883  for (;; ptr++) Line 5210  for (;; ptr++)
5210        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
5211        c = *previous;        c = *previous;
5212    
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
   
5213        OUTPUT_SINGLE_REPEAT:        OUTPUT_SINGLE_REPEAT:
5214        if (*previous == OP_PROP || *previous == OP_NOTPROP)        if (*previous == OP_PROP || *previous == OP_NOTPROP)
5215          {          {
# Line 4907  for (;; ptr++) Line 5226  for (;; ptr++)
5226    
5227        if (repeat_max == 0) goto END_REPEAT;        if (repeat_max == 0) goto END_REPEAT;
5228    
       /*--------------------------------------------------------------------*/  
       /* 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; */  
       /*--------------------------------------------------------------------*/  
   
5229        /* Combine the op_type with the repeat_type */        /* Combine the op_type with the repeat_type */
5230    
5231        repeat_type += op_type;        repeat_type += op_type;
# Line 5049  for (;; ptr++) Line 5358  for (;; ptr++)
5358      /* 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
5359      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}. */
5360    
5361      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
              *previous == OP_NCLASS ||  
5362  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5363               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
5364  #endif  #endif
5365               *previous == OP_REF ||               *previous == OP_REF   || *previous == OP_REFI ||
5366               *previous == OP_REFI)               *previous == OP_DNREF || *previous == OP_DNREFI)
5367        {        {
5368        if (repeat_max == 0)        if (repeat_max == 0)
5369          {          {
# Line 5063  for (;; ptr++) Line 5371  for (;; ptr++)
5371          goto END_REPEAT;          goto END_REPEAT;
5372          }          }
5373    
       /*--------------------------------------------------------------------*/  
       /* 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; */  
       /*--------------------------------------------------------------------*/  
   
5374        if (repeat_min == 0 && repeat_max == -1)        if (repeat_min == 0 && repeat_max == -1)
5375          *code++ = OP_CRSTAR + repeat_type;          *code++ = OP_CRSTAR + repeat_type;
5376        else if (repeat_min == 1 && repeat_max == -1)        else if (repeat_min == 1 && repeat_max == -1)
# Line 5415  for (;; ptr++) Line 5713  for (;; ptr++)
5713              pcre_uchar *scode = bracode;              pcre_uchar *scode = bracode;
5714              do              do
5715                {                {
5716                if (could_be_empty_branch(scode, ketcode, utf, cd))                if (could_be_empty_branch(scode, ketcode, utf, cd, NULL))
5717                  {                  {
5718                  *bracode += OP_SBRA - OP_BRA;                  *bracode += OP_SBRA - OP_BRA;
5719                  break;                  break;
# Line 5749  for (;; ptr++) Line 6047  for (;; ptr++)
6047          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
6048          case CHAR_LEFT_PARENTHESIS:          case CHAR_LEFT_PARENTHESIS:
6049          bravalue = OP_COND;       /* Conditional group */          bravalue = OP_COND;       /* Conditional group */
6050            tempptr = ptr;
6051    
6052          /* A condition can be an assertion, a number (referring to a numbered          /* A condition can be an assertion, a number (referring to a numbered
6053          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 5761  for (;; ptr++) Line 6060  for (;; ptr++)
6060          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
6061          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.
6062          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
6063          cases. */          cases.
6064    
6065            For compatibility with auto-callouts, we allow a callout to be
6066            specified before a condition that is an assertion. First, check for the
6067            syntax of a callout; if found, adjust the temporary pointer that is
6068            used to check for an assertion condition. That's all that is needed! */
6069    
6070            if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
6071              {
6072              for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
6073              if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
6074                tempptr += i + 1;
6075              }
6076    
6077          /* For conditions that are assertions, check the syntax, and then exit          /* For conditions that are assertions, check the syntax, and then exit
6078          the switch. This will take control down to where bracketed groups,          the switch. This will take control down to where bracketed groups,
6079          including assertions, are processed. */          including assertions, are processed. */
6080    
6081          if (ptr[1] == CHAR_QUESTION_MARK && (ptr[2] == CHAR_EQUALS_SIGN ||          if (tempptr[1] == CHAR_QUESTION_MARK &&
6082              ptr[2] == CHAR_EXCLAMATION_MARK || ptr[2] == CHAR_LESS_THAN_SIGN))                (tempptr[2] == CHAR_EQUALS_SIGN ||
6083                   tempptr[2] == CHAR_EXCLAMATION_MARK ||
6084                   tempptr[2] == CHAR_LESS_THAN_SIGN))
6085            break;            break;
6086    
6087          /* 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
6088          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. */
6089    
6090          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
6091          skipbytes = 1+IMM2_SIZE;          skipbytes = 1+IMM2_SIZE;
# Line 5788  for (;; ptr++) Line 6101  for (;; ptr++)
6101            }            }
6102    
6103          /* 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
6104          syntax (?(<name>) or (?('name') */          syntax (?(<name>) or (?('name'), and also allow for the original PCRE
6105            syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). As names may
6106            consist entirely of digits, there is scope for ambiguity. */
6107    
6108          else if (ptr[1] == CHAR_LESS_THAN_SIGN)          else if (ptr[1] == CHAR_LESS_THAN_SIGN)
6109            {            {
# Line 5805  for (;; ptr++) Line 6120  for (;; ptr++)
6120            terminator = CHAR_NULL;            terminator = CHAR_NULL;
6121            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);
6122            }            }
6123    
6124            /* When a name is one of a number of duplicates, a different opcode is
6125            used and it needs more memory. Unfortunately we cannot tell whether a
6126            name is a duplicate in the first pass, so we have to allow for more
6127            memory except when we know it is a relative numerical reference. */
6128    
6129            if (refsign < 0 && lengthptr != NULL) *lengthptr += IMM2_SIZE;
6130    
6131          /* We now expect to read a name; any thing else is an error */          /* We now expect to read a name (possibly all digits); any thing else
6132            is an error. In the case of all digits, also get it as a number. */
6133    
6134          if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)          if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)
6135            {            {
# Line 5815  for (;; ptr++) Line 6138  for (;; ptr++)
6138            goto FAILED;            goto FAILED;
6139            }            }
6140    
         /* Read the name, but also get it as a number if it's all digits */  
   
6141          recno = 0;          recno = 0;
6142          name = ++ptr;          name = ++ptr;
6143          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
# Line 5827  for (;; ptr++) Line 6148  for (;; ptr++)
6148            }            }
6149          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
6150    
6151            /* Check the terminator */
6152    
6153          if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||          if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
6154              *ptr++ != CHAR_RIGHT_PARENTHESIS)              *ptr++ != CHAR_RIGHT_PARENTHESIS)
6155            {            {
# Line 5862  for (;; ptr++) Line 6185  for (;; ptr++)
6185            }            }
6186    
6187          /* Otherwise (did not start with "+" or "-"), start by looking for the          /* Otherwise (did not start with "+" or "-"), start by looking for the
6188          name. If we find a name, add one to the opcode to change OP_CREF or          name. */
6189          OP_RREF into OP_NCREF or OP_NRREF. These behave exactly the same,  
         except they record that the reference was originally to a name. The  
         information is used to check duplicate names. */  
   
6190          slot = cd->name_table;          slot = cd->name_table;
6191          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
6192            {            {
# Line 5874  for (;; ptr++) Line 6194  for (;; ptr++)
6194            slot += cd->name_entry_size;            slot += cd->name_entry_size;
6195            }            }
6196    
6197          /* Found a previous named subpattern */          /* Found the named subpattern. If the name is duplicated, add one to
6198            the opcode to change CREF/RREF into DNCREF/DNRREF and insert
6199            appropriate data values. Otherwise, just insert the unique subpattern
6200            number. */
6201    
6202          if (i < cd->names_found)          if (i < cd->names_found)
6203            {            {
6204            recno = GET2(slot, 0);            int offset = i++;
6205            PUT2(code, 2+LINK_SIZE, recno);            int count = 1;
6206            code[1+LINK_SIZE]++;            recno = GET2(slot, 0);   /* Number from first found */
6207            }            for (; i < cd->names_found; i++)
6208                {
6209          /* Search the pattern for a forward reference */              slot += cd->name_entry_size;
6210                if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) != 0) break;
6211          else if ((i = find_parens(cd, name, namelen,              count++;
6212                          (options & PCRE_EXTENDED) != 0, utf)) > 0)              }
6213            {            if (count > 1)
6214            PUT2(code, 2+LINK_SIZE, i);              {
6215            code[1+LINK_SIZE]++;              PUT2(code, 2+LINK_SIZE, offset);
6216                PUT2(code, 2+LINK_SIZE+IMM2_SIZE, count);
6217                skipbytes += IMM2_SIZE;
6218                code[1+LINK_SIZE]++;
6219                }
6220              else  /* Not a duplicated name */
6221                {
6222                PUT2(code, 2+LINK_SIZE, recno);
6223                }
6224            }            }
6225    
6226          /* If terminator == CHAR_NULL it means that the name followed directly          /* If terminator == CHAR_NULL it means that the name followed directly
# Line 6055  for (;; ptr++) Line 6386  for (;; ptr++)
6386          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
6387          DEFINE_NAME:    /* Come here from (?< handling */          DEFINE_NAME:    /* Come here from (?< handling */
6388          case CHAR_APOSTROPHE:          case CHAR_APOSTROPHE:
6389            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
6390              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
6391            name = ++ptr;
6392    
6393            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
6394            namelen = (int)(ptr - name);
6395    
6396            /* In the pre-compile phase, do a syntax check, remember the longest
6397            name, and then remember the group in a vector, expanding it if
6398            necessary. Duplicates for the same number are skipped; other duplicates
6399            are checked for validity. In the actual compile, there is nothing to
6400            do. */
6401    
6402            if (lengthptr != NULL)
6403            {            {
6404            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?            named_group *ng;
6405              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;            pcre_uint32 number = cd->bracount + 1;
           name = ++ptr;  
6406    
6407            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;            if (*ptr != (pcre_uchar)terminator)
6408            namelen = (int)(ptr - name);              {
6409                *errorcodeptr = ERR42;
6410                goto FAILED;
6411                }
6412    
6413            /* In the pre-compile phase, just do a syntax check. */            if (cd->names_found >= MAX_NAME_COUNT)
6414                {
6415                *errorcodeptr = ERR49;
6416                goto FAILED;
6417                }
6418    
6419            if (lengthptr != NULL)            if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
6420              {              {
6421              if (*ptr != (pcre_uchar)terminator)              cd->name_entry_size = namelen + IMM2_SIZE + 1;
6422                {              if (namelen > MAX_NAME_SIZE)
               *errorcodeptr = ERR42;  
               goto FAILED;  
               }  
             if (cd->names_found >= MAX_NAME_COUNT)  
6423                {                {
6424                *errorcodeptr = ERR49;                *errorcodeptr = ERR48;
6425                goto FAILED;                goto FAILED;
6426                }                }
6427              if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)              }
6428    
6429              /* Scan the list to check for duplicates. For duplicate names, if the
6430              number is the same, break the loop, which causes the name to be
6431              discarded; otherwise, if DUPNAMES is not set, give an error.
6432              If it is set, allow the name with a different number, but continue
6433              scanning in case this is a duplicate with the same number. For
6434              non-duplicate names, give an error if the number is duplicated. */
6435    
6436              ng = cd->named_groups;
6437              for (i = 0; i < cd->names_found; i++, ng++)
6438                {
6439                if (namelen == ng->length &&
6440                    STRNCMP_UC_UC(name, ng->name, namelen) == 0)
6441                {                {
6442                cd->name_entry_size = namelen + IMM2_SIZE + 1;                if (ng->number == number) break;
6443                if (namelen > MAX_NAME_SIZE)                if ((options & PCRE_DUPNAMES) == 0)
6444                  {                  {
6445                  *errorcodeptr = ERR48;                  *errorcodeptr = ERR43;
6446                  goto FAILED;                  goto FAILED;
6447                  }                  }
6448                  cd->dupnames = TRUE;  /* Duplicate names exist */
6449                  }
6450                else if (ng->number == number)
6451                  {
6452                  *errorcodeptr = ERR65;
6453                  goto FAILED;
6454                }                }
6455              }              }
6456    
6457            /* In the real compile, create the entry in the table, maintaining            if (i >= cd->names_found)     /* Not a duplicate with same number */
           alphabetical order. Duplicate names for different numbers are  
           permitted only if PCRE_DUPNAMES is set. Duplicate names for the same  
           number are always OK. (An existing number can be re-used if (?|  
           appears in the pattern.) In either event, a duplicate name results in  
           a duplicate entry in the table, even if the number is the same. This  
           is because the number of names, and hence the table size, is computed  
           in the pre-compile, and it affects various numbers and pointers which  
           would all have to be modified, and the compiled code moved down, if  
           duplicates with the same number were omitted from the table. This  
           doesn't seem worth the hassle. However, *different* names for the  
           same number are not permitted. */  
   
           else  
6458              {              {
6459              BOOL dupname = FALSE;              /* Increase the list size if necessary */
             slot = cd->name_table;  
6460    
6461              for (i = 0; i < cd->names_found; i++)              if (cd->names_found >= cd->named_group_list_size)
6462                {                {
6463                int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(namelen));                int newsize = cd->named_group_list_size * 2;
6464                if (crc == 0)                named_group *newspace = (PUBL(malloc))
6465                  {                  (newsize * sizeof(named_group));
                 if (slot[IMM2_SIZE+namelen] == 0)  
                   {  
                   if (GET2(slot, 0) != cd->bracount + 1 &&  
                       (options & PCRE_DUPNAMES) == 0)  
                     {  
                     *errorcodeptr = ERR43;  
                     goto FAILED;  
                     }  
                   else dupname = TRUE;  
                   }  
                 else crc = -1;      /* Current name is a substring */  
                 }  
   
               /* Make space in the table and break the loop for an earlier  
               name. For a duplicate or later name, carry on. We do this for  
               duplicates so that in the simple case (when ?(| is not used) they  
               are in order of their numbers. */  
6466    
6467                if (crc < 0)                if (newspace == NULL)
6468                  {                  {
6469                  memmove(slot + cd->name_entry_size, slot,                  *errorcodeptr = ERR21;
6470                    IN_UCHARS((cd->names_found - i) * cd->name_entry_size));                  goto FAILED;
                 break;  
6471                  }                  }
6472    
6473                /* Continue the loop for a later or duplicate name */                memcpy(newspace, cd->named_groups,
6474                    cd->named_group_list_size * sizeof(named_group));
6475                slot += cd->name_entry_size;                if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
6476                }                  (PUBL(free))((void *)cd->named_groups);
6477                  cd->named_groups = newspace;
6478              /* For non-duplicate names, check for a duplicate number before                cd->named_group_list_size = newsize;
             adding the new name. */  
   
             if (!dupname)  
               {  
               pcre_uchar *cslot = cd->name_table;  
               for (i = 0; i < cd->names_found; i++)  
                 {  
                 if (cslot != slot)  
                   {  
                   if (GET2(cslot, 0) == cd->bracount + 1)  
                     {  
                     *errorcodeptr = ERR65;  
                     goto FAILED;  
                     }  
                   }  
                 else i--;  
                 cslot += cd->name_entry_size;  
                 }  
6479                }                }
6480    
6481              PUT2(slot, 0, cd->bracount + 1);              cd->named_groups[cd->names_found].name = name;
6482              memcpy(slot + IMM2_SIZE, name, IN_UCHARS(namelen));              cd->named_groups[cd->names_found].length = namelen;
6483              slot[IMM2_SIZE + namelen] = 0;              cd->named_groups[cd->names_found].number = number;
6484                cd->names_found++;
6485              }              }
6486            }            }
6487    
6488          /* In both pre-compile and compile, count the number of names we've          ptr++;                    /* Move past > or ' in both passes. */
         encountered. */  
   
         cd->names_found++;  
         ptr++;                    /* Move past > or ' */  
6489          goto NUMBERED_GROUP;          goto NUMBERED_GROUP;
6490    
6491    
# Line 6202  for (;; ptr++) Line 6515  for (;; ptr++)
6515    
6516          if (lengthptr != NULL)          if (lengthptr != NULL)
6517            {            {
6518            const pcre_uchar *temp;            named_group *ng;
6519    
6520            if (namelen == 0)            if (namelen == 0)
6521              {              {
# Line 6220  for (;; ptr++) Line 6533  for (;; ptr++)
6533              goto FAILED;              goto FAILED;
6534              }              }
6535    
6536            /* The name table does not exist in the first pass, so we cannot            /* The name table does not exist in the first pass; instead we must
6537            do a simple search as in the code below. Instead, we have to scan the            scan the list of names encountered so far in order to get the
6538            pattern to find the number. It is important that we scan it only as            number. If the name is not found, set the value to 0 for a forward
6539            far as we have got because the syntax of named subpatterns has not            reference. */
6540            been checked for the rest of the pattern, and find_parens() assumes  
6541            correct syntax. In any case, it's a waste of resources to scan            ng = cd->named_groups;
6542            further. We stop the scan at the current point by temporarily            for (i = 0; i < cd->names_found; i++, ng++)
6543            adjusting the value of cd->endpattern. */              {
6544                if (namelen == ng->length &&
6545            temp = cd->end_pattern;                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)
6546            cd->end_pattern = ptr;                break;
6547            recno = find_parens(cd, name, namelen,              }
6548              (options & PCRE_EXTENDED) != 0, utf);            recno = (i < cd->names_found)? ng->number : 0;
6549            cd->end_pattern = temp;  
6550            if (recno < 0) recno = 0;    /* Forward ref; set dummy number */            /* Count named back references. */
6551    
6552              if (!is_recurse) cd->namedrefcount++;
6553            }            }
6554    
6555          /* In the real compile, seek the name in the table. We check the name          /* In the real compile, search the name table. We check the name
6556          first, and then check that we have reached the end of the name in the          first, and then check that we have reached the end of the name in the
6557          table. That way, if the name that is longer than any in the table,          table. That way, if the name is longer than any in the table, the
6558          the comparison will fail without reading beyond the table entry. */          comparison will fail without reading beyond the table entry. */
6559    
6560          else          else
6561            {            {
# Line 6253  for (;; ptr++) Line 6568  for (;; ptr++)
6568              slot += cd->name_entry_size;              slot += cd->name_entry_size;
6569              }              }
6570    
6571            if (i < cd->names_found)         /* Back reference */            if (i < cd->names_found)
6572              {              {
6573              recno = GET2(slot, 0);              recno = GET2(slot, 0);
6574              }              }
6575            else if ((recno =                /* Forward back reference */            else
                     find_parens(cd, name, namelen,  
                       (options & PCRE_EXTENDED) != 0, utf)) <= 0)  
6576              {              {
6577              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
6578              goto FAILED;              goto FAILED;
6579              }              }
6580            }            }
6581    
6582          /* In both phases, we can now go to the code than handles numerical          /* In both phases, for recursions, we can now go to the code than
6583          recursion or backreferences. */          handles numerical recursion. */
6584    
6585          if (is_recurse) goto HANDLE_RECURSION;          if (is_recurse) goto HANDLE_RECURSION;
6586            else goto HANDLE_REFERENCE;  
6587            /* In the second pass we must see if the name is duplicated. If so, we
6588            generate a different opcode. */
6589    
6590            if (lengthptr == NULL && cd->dupnames)
6591              {
6592              int count = 1;
6593              unsigned int index = i;
6594              pcre_uchar *cslot = slot + cd->name_entry_size;
6595    
6596              for (i++; i < cd->names_found; i++)
6597                {
6598                if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break;
6599                count++;
6600                cslot += cd->name_entry_size;
6601                }
6602    
6603              if (count > 1)
6604                {
6605                if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
6606                previous = code;
6607                *code++ = ((options & PCRE_CASELESS) != 0)? OP_DNREFI : OP_DNREF;
6608                PUT2INC(code, 0, index);
6609                PUT2INC(code, 0, count);
6610    
6611                /* Process each potentially referenced group. */
6612    
6613                for (; slot < cslot; slot += cd->name_entry_size)
6614                  {
6615                  open_capitem *oc;
6616                  recno = GET2(slot, 0);
6617                  cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6618                  if (recno > cd->top_backref) cd->top_backref = recno;
6619    
6620                  /* Check to see if this back reference is recursive, that it, it
6621                  is inside the group that it references. A flag is set so that the
6622                  group can be made atomic. */
6623    
6624                  for (oc = cd->open_caps; oc != NULL; oc = oc->next)
6625                    {
6626                    if (oc->number == recno)
6627                      {
6628                      oc->flag = TRUE;
6629                      break;
6630                      }
6631                    }
6632                  }
6633    
6634                continue;  /* End of back ref handling */
6635                }
6636              }
6637    
6638            /* First pass, or a non-duplicated name. */
6639    
6640            goto HANDLE_REFERENCE;
6641    
6642    
6643          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
# Line 6369  for (;; ptr++) Line 6736  for (;; ptr++)
6736    
6737              if (called == NULL)              if (called == NULL)
6738                {                {
6739                if (find_parens(cd, NULL, recno,                if (recno > cd->final_bracount)
                     (options & PCRE_EXTENDED) != 0, utf) < 0)  
6740                  {                  {
6741                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
6742                  goto FAILED;                  goto FAILED;
# Line 6703  for (;; ptr++) Line 7069  for (;; ptr++)
7069        /* If the subpattern set a required byte (or set a first byte that isn't        /* If the subpattern set a required byte (or set a first byte that isn't
7070        really the first byte - see above), set it. */        really the first byte - see above), set it. */
7071    
7072        if (subreqcharflags >= 0)        if (subreqcharflags >= 0)
7073          {          {
7074          reqchar = subreqchar;          reqchar = subreqchar;
7075          reqcharflags = subreqcharflags;          reqcharflags = subreqcharflags;
# Line 6738  for (;; ptr++) Line 7104  for (;; ptr++)
7104      case CHAR_BACKSLASH:      case CHAR_BACKSLASH: