/[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 1348 by ph10, Fri Jul 5 10:38:37 2013 UTC revision 1379 by ph10, Mon Oct 14 13:54:07 2013 UTC
# 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 509  static const char error_texts[] = Line 516  static const char error_texts[] =
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"    "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 648  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 675  return s; Line 805  return s;
805  }  }
806    
807    
808    
809  /*************************************************  /*************************************************
810  *           Expand the workspace                 *  *           Expand the workspace                 *
811  *************************************************/  *************************************************/
# Line 752  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 965  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 1001  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 1009  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 1035  else Line 1167  else
1167  #endif  #endif
1168      break;      break;
1169    
1170      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \o is a relatively new Perl feature, supporting a more general way of
1171      than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.      specifying character codes in octal. The only supported form is \o{ddd}. */
1172      If not, { is treated as a data character. */  
1173        case CHAR_o:
1174        if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
1175          {
1176          ptr += 2;
1177          c = 0;
1178          overflow = FALSE;
1179          while (*ptr >= CHAR_0 && *ptr <= CHAR_7)
1180            {
1181            register pcre_uint32 cc = *ptr++;
1182            if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1183    #ifdef COMPILE_PCRE32
1184            if (c >= 0x20000000l) { overflow = TRUE; break; }
1185    #endif
1186            c = (c << 3) + cc - CHAR_0 ;
1187    #if defined COMPILE_PCRE8
1188            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1189    #elif defined COMPILE_PCRE16
1190            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1191    #elif defined COMPILE_PCRE32
1192            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1193    #endif
1194            }
1195          if (overflow)
1196            {
1197            while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++;
1198            *errorcodeptr = ERR34;
1199            }
1200          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1201            {
1202            if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1203            }
1204          else *errorcodeptr = ERR80;
1205          }
1206        break;
1207    
1208        /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
1209        numbers. Otherwise it is a lowercase x letter. */
1210    
1211      case CHAR_x:      case CHAR_x:
1212      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1213        {        {
       /* In JavaScript, \x must be followed by two hexadecimal numbers.  
       Otherwise it is a lowercase x letter. */  
1214        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1215          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1216          {          {
# Line 1060  else Line 1227  else
1227  #endif  #endif
1228            }            }
1229          }          }
1230        break;        }    /* End JavaScript handling */
       }  
1231    
1232      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      /* Handle \x in Perl's style. \x{ddd} is a character number which can be
1233        {      greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
1234        const pcre_uchar *pt = ptr + 2;      digits. If not, { used to be treated as a data character. However, Perl
1235        seems to read hex digits up to the first non-such, and ignore the rest, so
1236        that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
1237        now gives an error. */
1238    
1239        c = 0;      else
1240        overflow = FALSE;        {
1241        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)        if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1242          {          {
1243          register pcre_uint32 cc = *pt++;          ptr += 2;
1244          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          c = 0;
1245            overflow = FALSE;
1246            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)
1247              {
1248              register pcre_uint32 cc = *ptr++;
1249              if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1250    
1251  #ifdef COMPILE_PCRE32  #ifdef COMPILE_PCRE32
1252          if (c >= 0x10000000l) { overflow = TRUE; break; }            if (c >= 0x10000000l) { overflow = TRUE; break; }
1253  #endif  #endif
1254    
1255  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1256          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1257          c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1258  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1259          if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */            if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
1260          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1261  #endif  #endif
1262    
1263  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
1264          if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1265  #elif defined COMPILE_PCRE16  #elif defined COMPILE_PCRE16
1266          if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1267  #elif defined COMPILE_PCRE32  #elif defined COMPILE_PCRE32
1268          if (utf && c > 0x10ffffU) { overflow = TRUE; break; }            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1269  #endif  #endif
1270          }            }
1271    
1272        if (overflow)          if (overflow)
1273          {            {
1274          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++;
1275          *errorcodeptr = ERR34;            *errorcodeptr = ERR34;
1276          }            }
1277    
1278        if (*pt == CHAR_RIGHT_CURLY_BRACKET)          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1279          {            {
1280          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;            if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1281          ptr = pt;            }
         break;  
         }  
1282    
1283        /* If the sequence of hex digits does not end with '}', then we don't          /* If the sequence of hex digits does not end with '}', give an error.
1284        recognize this construct; fall through to the normal \x handling. */          We used just to recognize this construct and fall through to the normal
1285        }          \x handling, but nowadays Perl gives an error, which seems much more
1286            sensible, so we do too. */
1287    
1288      /* Read just a single-byte hex-defined char */          else *errorcodeptr = ERR79;
1289            }   /* End of \x{} processing */
1290    
1291      c = 0;        /* Read a single-byte hex-defined char (up to two hex digits after \x) */
1292      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)  
1293        {        else
1294        pcre_uint32 cc;                          /* Some compilers don't like */          {
1295        cc = *(++ptr);                           /* ++ in initializers */          c = 0;
1296            while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1297              {
1298              pcre_uint32 cc;                          /* Some compilers don't like */
1299              cc = *(++ptr);                           /* ++ in initializers */
1300  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1301        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
1302        c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1303  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1304        if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */            if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */
1305        c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1306  #endif  #endif
1307        }            }
1308            }     /* End of \xdd handling */
1309          }       /* End of Perl-style \x handling */
1310      break;      break;
1311    
1312      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
# Line 1192  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 1289  return FALSE; Line 1471  return FALSE;
1471    
1472    
1473    
   
1474  /*************************************************  /*************************************************
1475  *         Read repeat counts                     *  *         Read repeat counts                     *
1476  *************************************************/  *************************************************/
# Line 1358  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;  
     while (ptr < cd->end_pattern && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
     }  
   
   /* 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 1696  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 1712  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 1836  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 1937  for (;;) Line 1817  for (;;)
1817    
1818      switch (*cc)      switch (*cc)
1819        {        {
       case OP_CRPLUS:  
       case OP_CRMINPLUS:  
1820        case OP_CRSTAR:        case OP_CRSTAR:
1821        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1822          case OP_CRPLUS:
1823          case OP_CRMINPLUS:
1824        case OP_CRQUERY:        case OP_CRQUERY:
1825        case OP_CRMINQUERY:        case OP_CRMINQUERY:
1826          case OP_CRPOSSTAR:
1827          case OP_CRPOSPLUS:
1828          case OP_CRPOSQUERY:
1829        return -1;        return -1;
1830    
1831        case OP_CRRANGE:        case OP_CRRANGE:
1832        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1833          case OP_CRPOSRANGE:
1834        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1835        branchlength += (int)GET2(cc,1);        branchlength += (int)GET2(cc,1);
1836        cc += 1 + 2 * IMM2_SIZE;        cc += 1 + 2 * IMM2_SIZE;
# Line 2015  for (;;) Line 1899  for (;;)
1899      case OP_QUERYI:      case OP_QUERYI:
1900      case OP_REF:      case OP_REF:
1901      case OP_REFI:      case OP_REFI:
1902        case OP_DNREF:
1903        case OP_DNREFI:
1904      case OP_SBRA:      case OP_SBRA:
1905      case OP_SBRAPOS:      case OP_SBRAPOS:
1906      case OP_SCBRA:      case OP_SCBRA:
# Line 2051  for (;;) Line 1937  for (;;)
1937    
1938    
1939    
   
1940  /*************************************************  /*************************************************
1941  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
1942  *************************************************/  *************************************************/
# Line 2361  Returns:      TRUE if what is matched co Line 2246  Returns:      TRUE if what is matched co
2246  typedef struct recurse_check {  typedef struct recurse_check {
2247    struct recurse_check *prev;    struct recurse_check *prev;
2248    const pcre_uchar *group;    const pcre_uchar *group;
2249  } recurse_check;  } recurse_check;
2250    
2251  static BOOL  static BOOL
2252  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
# Line 2377  for (code = first_significant_code(code Line 2262  for (code = first_significant_code(code
2262    const pcre_uchar *ccode;    const pcre_uchar *ccode;
2263    
2264    c = *code;    c = *code;
2265    
2266    /* Skip over forward assertions; the other assertions are skipped by    /* Skip over forward assertions; the other assertions are skipped by
2267    first_significant_code() with a TRUE final argument. */    first_significant_code() with a TRUE final argument. */
2268    
# Line 2405  for (code = first_significant_code(code Line 2290  for (code = first_significant_code(code
2290      NULL. */      NULL. */
2291    
2292      if (cd->start_workspace != NULL)      if (cd->start_workspace != NULL)
2293        {        {
2294        const pcre_uchar *tcode;        const pcre_uchar *tcode;
2295        for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)        for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2296          if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;          if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2297        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2298        }        }
2299    
2300      /* If we are scanning a completed pattern, there are no forward references      /* If we are scanning a completed pattern, there are no forward references
2301      and all groups are complete. We need to detect whether this is a recursive      and all groups are complete. We need to detect whether this is a recursive
2302      call, as otherwise there will be an infinite loop. If it is a recursion,      call, as otherwise there will be an infinite loop. If it is a recursion,
2303      just skip over it. Simple recursions are easily detected. For mutual      just skip over it. Simple recursions are easily detected. For mutual
2304      recursions we keep a chain on the stack. */      recursions we keep a chain on the stack. */
2305    
2306      else      else
2307        {        {
2308        recurse_check *r = recurses;        recurse_check *r = recurses;
2309        const pcre_uchar *endgroup = scode;        const pcre_uchar *endgroup = scode;
2310    
2311        do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);        do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2312        if (code >= scode && code <= endgroup) continue;  /* Simple recursion */        if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2313    
2314        for (r = recurses; r != NULL; r = r->prev)        for (r = recurses; r != NULL; r = r->prev)
2315          if (r->group == scode) break;          if (r->group == scode) break;
2316        if (r != NULL) continue;   /* Mutual recursion */        if (r != NULL) continue;   /* Mutual recursion */
# Line 2436  for (code = first_significant_code(code Line 2321  for (code = first_significant_code(code
2321    
2322      empty_branch = FALSE;      empty_branch = FALSE;
2323      this_recurse.prev = recurses;      this_recurse.prev = recurses;
2324      this_recurse.group = scode;      this_recurse.group = scode;
2325    
2326      do      do
2327        {        {
2328        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
# Line 2538  for (code = first_significant_code(code Line 2423  for (code = first_significant_code(code
2423        case OP_CRMINSTAR:        case OP_CRMINSTAR:
2424        case OP_CRQUERY:        case OP_CRQUERY:
2425        case OP_CRMINQUERY:        case OP_CRMINQUERY:
2426          case OP_CRPOSSTAR:
2427          case OP_CRPOSQUERY:
2428        break;        break;
2429    
2430        default:                   /* Non-repeat => class must match */        default:                   /* Non-repeat => class must match */
2431        case OP_CRPLUS:            /* These repeats aren't empty */        case OP_CRPLUS:            /* These repeats aren't empty */
2432        case OP_CRMINPLUS:        case OP_CRMINPLUS:
2433          case OP_CRPOSPLUS:
2434        return FALSE;        return FALSE;
2435    
2436        case OP_CRRANGE:        case OP_CRRANGE:
2437        case OP_CRMINRANGE:        case OP_CRMINRANGE:
2438          case OP_CRPOSRANGE:
2439        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */
2440        break;        break;
2441        }        }
# Line 2557  for (code = first_significant_code(code Line 2446  for (code = first_significant_code(code
2446      case OP_ANY:      case OP_ANY:
2447      case OP_ALLANY:      case OP_ALLANY:
2448      case OP_ANYBYTE:      case OP_ANYBYTE:
2449    
2450      case OP_PROP:      case OP_PROP:
2451      case OP_NOTPROP:      case OP_NOTPROP:
2452      case OP_ANYNL:      case OP_ANYNL:
2453    
2454      case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
2455      case OP_HSPACE:      case OP_HSPACE:
2456      case OP_NOT_VSPACE:      case OP_NOT_VSPACE:
2457      case OP_VSPACE:      case OP_VSPACE:
2458      case OP_EXTUNI:      case OP_EXTUNI:
2459    
2460      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2461      case OP_DIGIT:      case OP_DIGIT:
2462      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2463      case OP_WHITESPACE:      case OP_WHITESPACE:
2464      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2465      case OP_WORDCHAR:      case OP_WORDCHAR:
2466    
2467      case OP_CHAR:      case OP_CHAR:
2468      case OP_CHARI:      case OP_CHARI:
2469      case OP_NOT:      case OP_NOT:
2470      case OP_NOTI:      case OP_NOTI:
2471    
2472      case OP_PLUS:      case OP_PLUS:
2473      case OP_PLUSI:      case OP_PLUSI:
2474      case OP_MINPLUS:      case OP_MINPLUS:
# Line 2589  for (code = first_significant_code(code Line 2478  for (code = first_significant_code(code
2478      case OP_NOTPLUSI:      case OP_NOTPLUSI:
2479      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2480      case OP_NOTMINPLUSI:      case OP_NOTMINPLUSI:
2481    
2482      case OP_POSPLUS:      case OP_POSPLUS:
2483      case OP_POSPLUSI:      case OP_POSPLUSI:
2484      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2485      case OP_NOTPOSPLUSI:      case OP_NOTPOSPLUSI:
2486    
2487      case OP_EXACT:      case OP_EXACT:
2488      case OP_EXACTI:      case OP_EXACTI:
2489      case OP_NOTEXACT:      case OP_NOTEXACT:
2490      case OP_NOTEXACTI:      case OP_NOTEXACTI:
2491    
2492      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2493      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2494      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2495      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2496    
2497      return FALSE;      return FALSE;
2498    
2499      /* 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 2644  for (code = first_significant_code(code Line 2533  for (code = first_significant_code(code
2533  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2534      case OP_STAR:      case OP_STAR:
2535      case OP_STARI:      case OP_STARI:
2536      case OP_NOTSTAR:      case OP_NOTSTAR:
2537      case OP_NOTSTARI:      case OP_NOTSTARI:
2538    
2539      case OP_MINSTAR:      case OP_MINSTAR:
2540      case OP_MINSTARI:      case OP_MINSTARI:
2541      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
2542      case OP_NOTMINSTARI:      case OP_NOTMINSTARI:
2543    
2544      case OP_POSSTAR:      case OP_POSSTAR:
2545      case OP_POSSTARI:      case OP_POSSTARI:
2546      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
2547      case OP_NOTPOSSTARI:      case OP_NOTPOSSTARI:
2548    
2549      case OP_QUERY:      case OP_QUERY:
2550      case OP_QUERYI:      case OP_QUERYI:
2551      case OP_NOTQUERY:      case OP_NOTQUERY:
2552      case OP_NOTQUERYI:      case OP_NOTQUERYI:
2553    
2554      case OP_MINQUERY:      case OP_MINQUERY:
2555      case OP_MINQUERYI:      case OP_MINQUERYI:
2556      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
2557      case OP_NOTMINQUERYI:      case OP_NOTMINQUERYI:
2558    
2559      case OP_POSQUERY:      case OP_POSQUERY:
2560      case OP_POSQUERYI:      case OP_POSQUERYI:
2561      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
2562      case OP_NOTPOSQUERYI:      case OP_NOTPOSQUERYI:
2563    
2564      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2565      break;      break;
2566    
2567      case OP_UPTO:      case OP_UPTO:
2568      case OP_UPTOI:      case OP_UPTOI:
2569      case OP_NOTUPTO:      case OP_NOTUPTO:
2570      case OP_NOTUPTOI:      case OP_NOTUPTOI:
2571    
2572      case OP_MINUPTO:      case OP_MINUPTO:
2573      case OP_MINUPTOI:      case OP_MINUPTOI:
2574      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
2575      case OP_NOTMINUPTOI:      case OP_NOTMINUPTOI:
2576    
2577      case OP_POSUPTO:      case OP_POSUPTO:
2578      case OP_POSUPTOI:      case OP_POSUPTOI:
2579      case OP_NOTPOSUPTO:      case OP_NOTPOSUPTO:
2580      case OP_NOTPOSUPTOI:      case OP_NOTPOSUPTOI:
2581    
2582      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]);
2583      break;      break;
2584  #endif  #endif
# Line 2753  return TRUE; Line 2642  return TRUE;
2642    
2643    
2644  /*************************************************  /*************************************************
2645  *           Check for POSIX class syntax         *  *        Base opcode of repeated opcodes         *
2646  *************************************************/  *************************************************/
2647    
2648  /* This function is called when the sequence "[:" or "[." or "[=" is  /* Returns the base opcode for repeated single character type opcodes. If the
2649  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.  
2650    
2651  Originally, this function only recognized a sequence of letters between the  Arguments:  c opcode
2652  terminators, but it seems that Perl recognizes any sequence of characters,  Returns:    base opcode for the type
2653  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:].  
2654    
2655  The problem in trying to be exactly like Perl is in the handling of escapes. We  static pcre_uchar
2656  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX  get_repeat_base(pcre_uchar c)
2657  class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code  {
2658  below handles the special case of \], but does not try to do any other escape  return (c > OP_TYPEPOSUPTO)? c :
2659  processing. This makes it different from Perl for cases such as [:l\ower:]         (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2660  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize         (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2661  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,         (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2662  I think.         (c >= OP_STARI)?      OP_STARI :
2663                                 OP_STAR;
2664    }
2665    
 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.  
2666    
2667  In Perl, unescaped square brackets may also appear as part of class names. For  
2668  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for  #ifdef SUPPORT_UCP
2669  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not  /*************************************************
2670  seem right at all. PCRE does not allow closing square brackets in POSIX class  *        Check a character and a property        *
2671  names.  *************************************************/
2672    
2673    /* This function is called by check_auto_possessive() when a property item
2674    is adjacent to a fixed character.
2675    
2676  Arguments:  Arguments:
2677    ptr      pointer to the initial [    c            the character
2678    endptr   where to return the end pointer    ptype        the property type
2679      pdata        the data for the type
2680      negated      TRUE if it's a negated property (\P or \p{^)
2681    
2682  Returns:   TRUE or FALSE  Returns:       TRUE if auto-possessifying is OK
2683  */  */
2684    
2685  static BOOL  static BOOL
2686  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2687      BOOL negated)
2688  {  {
2689  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */  const pcre_uint32 *p;
2690  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  const ucd_record *prop = GET_UCD(c);
2691  for (++ptr; *ptr != CHAR_NULL; ptr++)  
2692    switch(ptype)
2693    {    {
2694    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)    case PT_LAMP:
2695      ptr++;    return (prop->chartype == ucp_Lu ||
2696    else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;            prop->chartype == ucp_Ll ||
2697    else            prop->chartype == ucp_Lt) == negated;
2698    
2699      case PT_GC:
2700      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2701    
2702      case PT_PC:
2703      return (pdata == prop->chartype) == negated;
2704    
2705      case PT_SC:
2706      return (pdata == prop->script) == negated;
2707    
2708      /* These are specials */
2709    
2710      case PT_ALNUM:
2711      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2712              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2713    
2714      /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2715      means that Perl space and POSIX space are now identical. PCRE was changed
2716      at release 8.34. */
2717    
2718      case PT_SPACE:    /* Perl space */
2719      case PT_PXSPACE:  /* POSIX space */
2720      switch(c)
2721      {      {
2722      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      HSPACE_CASES:
2723        {      VSPACE_CASES:
2724        *endptr = ptr;      return negated;
2725        return TRUE;  
2726        }      default:
2727      if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
2728           (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||      }
2729            ptr[1] == CHAR_EQUALS_SIGN) &&    break;  /* Control never reaches here */
2730          check_posix_syntax(ptr, endptr))  
2731        return FALSE;    case PT_WORD:
2732      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2733              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2734              c == CHAR_UNDERSCORE) == negated;
2735    
2736      case PT_CLIST:
2737      p = PRIV(ucd_caseless_sets) + prop->caseset;
2738      for (;;)
2739        {
2740        if (c < *p) return !negated;
2741        if (c == *p++) return negated;
2742      }      }
2743      break;  /* Control never reaches here */
2744    }    }
2745    
2746  return FALSE;  return FALSE;
2747  }  }
2748    #endif  /* SUPPORT_UCP */
2749    
2750    
2751    
2752  /*************************************************  /*************************************************
2753  *          Check POSIX class name                *  *        Fill the character property list        *
2754  *************************************************/  *************************************************/
2755    
2756  /* This function is called to check the name given in a POSIX-style class entry  /* Checks whether the code points to an opcode that can take part in auto-
2757  such as [:alnum:].  possessification, and if so, fills a list with its properties.
2758    
2759  Arguments:  Arguments:
2760    ptr        points to the first letter    code        points to start of expression
2761    len        the length of the name    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2762      fcc         points to case-flipping table
2763      list        points to output list
2764                  list[0] will be filled with the opcode
2765                  list[1] will be non-zero if this opcode
2766                    can match an empty character string
2767                  list[2..7] depends on the opcode
2768    
2769  Returns:     a value representing the name, or -1 if unknown  Returns:      points to the start of the next opcode if *code is accepted
2770                  NULL if *code is not accepted
2771  */  */
2772    
2773  static int  static const pcre_uchar *
2774  check_posix_name(const pcre_uchar *ptr, int len)  get_chr_property_list(const pcre_uchar *code, BOOL utf,
2775      const pcre_uint8 *fcc, pcre_uint32 *list)
2776  {  {
2777  const char *pn = posix_names;  pcre_uchar c = *code;
2778  register int yield = 0;  const pcre_uchar *end;
2779  while (posix_name_lengths[yield] != 0)  const pcre_uint32 *clist_src;
2780    pcre_uint32 *clist_dest;
2781    pcre_uint32 chr;
2782    pcre_uchar base;
2783    
2784    list[0] = c;
2785    list[1] = FALSE;
2786    code++;
2787    
2788    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2789    {    {
2790    if (len == posix_name_lengths[yield] &&    base = get_repeat_base(c);
2791      STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;    c -= (base - OP_STAR);
   pn += posix_name_lengths[yield] + 1;  
   yield++;  
   }  
 return -1;  
 }  
2792    
2793      if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2794        code += IMM2_SIZE;
2795    
2796  /*************************************************    list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
 *    Adjust OP_RECURSE items in repeated group   *  
 *************************************************/  
2797    
2798  /* OP_RECURSE items contain an offset from the start of the regex to the group    switch(base)
2799  that is referenced. This means that groups can be replicated for fixed      {
2800  repetition simply by copying (because the recursion is allowed to refer to      case OP_STAR:
2801  earlier groups that are outside the current group). However, when a group is      list[0] = OP_CHAR;
2802  optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is      break;
 inserted before it, after it has been compiled. This means that any OP_RECURSE  
 items within it that refer to the group itself or any contained groups have to  
 have their offsets adjusted. That one of the jobs of this function. Before it  
 is called, the partially compiled regex must be temporarily terminated with  
 OP_END.  
2803    
2804  This function has been extended with the possibility of forward references for      case OP_STARI:
2805  recursions and subroutine calls. It must also check the list of such references      list[0] = OP_CHARI;
2806  for the group we are dealing with. If it finds that one of the recursions in      break;
 the current group is on this list, it adjusts the offset in the list, not the  
 value in the reference (which is a group number).  
2807    
2808  Arguments:      case OP_NOTSTAR:
2809    group      points to the start of the group      list[0] = OP_NOT;
2810    adjust     the amount by which the group is to be moved      break;
   utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode  
   cd         contains pointers to tables etc.  
   save_hwm   the hwm forward reference pointer at the start of the group  
2811    
2812  Returns:     nothing      case OP_NOTSTARI:
2813  */      list[0] = OP_NOTI;
2814        break;
2815    
2816  static void      case OP_TYPESTAR:
2817  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,      list[0] = *code;
2818    pcre_uchar *save_hwm)      code++;
2819  {      break;
2820  pcre_uchar *ptr = group;      }
2821      c = list[0];
2822      }
2823    
2824  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)  switch(c)
2825    {    {
2826    int offset;    case OP_NOT_DIGIT:
2827    pcre_uchar *hc;    case OP_DIGIT:
2828      case OP_NOT_WHITESPACE:
2829      case OP_WHITESPACE:
2830      case OP_NOT_WORDCHAR:
2831      case OP_WORDCHAR:
2832      case OP_ANY:
2833      case OP_ALLANY:
2834      case OP_ANYNL:
2835      case OP_NOT_HSPACE:
2836      case OP_HSPACE:
2837      case OP_NOT_VSPACE:
2838      case OP_VSPACE:
2839      case OP_EXTUNI:
2840      case OP_EODN:
2841      case OP_EOD:
2842      case OP_DOLL:
2843      case OP_DOLLM:
2844      return code;
2845    
2846    /* See if this recursion is on the forward reference list. If so, adjust the    case OP_CHAR:
2847    reference. */    case OP_NOT:
2848      GETCHARINCTEST(chr, code);
2849      list[2] = chr;
2850      list[3] = NOTACHAR;
2851      return code;
2852    
2853    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    case OP_CHARI:
2854      case OP_NOTI:
2855      list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2856      GETCHARINCTEST(chr, code);
2857      list[2] = chr;
2858    
2859    #ifdef SUPPORT_UCP
2860      if (chr < 128 || (chr < 256 && !utf))
2861        list[3] = fcc[chr];
2862      else
2863        list[3] = UCD_OTHERCASE(chr);
2864    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2865      list[3] = (chr < 256) ? fcc[chr] : chr;
2866    #else
2867      list[3] = fcc[chr];
2868    #endif
2869    
2870      /* The othercase might be the same value. */
2871    
2872      if (chr == list[3])
2873        list[3] = NOTACHAR;
2874      else
2875        list[4] = NOTACHAR;
2876      return code;
2877    
2878    #ifdef SUPPORT_UCP
2879      case OP_PROP:
2880      case OP_NOTPROP:
2881      if (code[0] != PT_CLIST)
2882      {      {
2883      offset = (int)GET(hc, 0);      list[2] = code[0];
2884      if (cd->start_code + offset == ptr + 1)      list[3] = code[1];
2885        {      return code + 2;
       PUT(hc, 0, offset + adjust);  
       break;  
       }  
2886      }      }
2887    
2888    /* Otherwise, adjust the recursion offset if it's after the start of this    /* Convert only if we have enough space. */
   group. */  
2889    
2890    if (hc >= cd->hwm)    clist_src = PRIV(ucd_caseless_sets) + code[1];
2891      clist_dest = list + 2;
2892      code += 2;
2893    
2894      do {
2895         if (clist_dest >= list + 8)
2896           {
2897           /* Early return if there is not enough space. This should never
2898           happen, since all clists are shorter than 5 character now. */
2899           list[2] = code[0];
2900           list[3] = code[1];
2901           return code;
2902           }
2903         *clist_dest++ = *clist_src;
2904         }
2905      while(*clist_src++ != NOTACHAR);
2906    
2907      /* All characters are stored. The terminating NOTACHAR
2908      is copied form the clist itself. */
2909    
2910      list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2911      return code;
2912    #endif
2913    
2914      case OP_NCLASS:
2915      case OP_CLASS:
2916    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2917      case OP_XCLASS:
2918    
2919      if (c == OP_XCLASS)
2920        end = code + GET(code, 0);
2921      else
2922    #endif
2923        end = code + 32 / sizeof(pcre_uchar);
2924    
2925      switch(*end)
2926      {      {
2927      offset = (int)GET(ptr, 1);      case OP_CRSTAR:
2928      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      case OP_CRMINSTAR:
2929      }      case OP_CRQUERY:
2930        case OP_CRMINQUERY:
2931        case OP_CRPOSSTAR:
2932        case OP_CRPOSQUERY:
2933        list[1] = TRUE;
2934        end++;
2935        break;
2936    
2937    ptr += 1 + LINK_SIZE;      case OP_CRPLUS:
2938        case OP_CRMINPLUS:
2939        case OP_CRPOSPLUS:
2940        end++;
2941        break;
2942    
2943        case OP_CRRANGE:
2944        case OP_CRMINRANGE:
2945        case OP_CRPOSRANGE:
2946        list[1] = (GET2(end, 1) == 0);
2947        end += 1 + 2 * IMM2_SIZE;
2948        break;
2949        }
2950      list[2] = end - code;
2951      return end;
2952    }    }
2953    return NULL;    /* Opcode not accepted */
2954  }  }
2955    
2956    
2957    
2958  /*************************************************  /*************************************************
2959  *        Insert an automatic callout point       *  *    Scan further character sets for match       *
2960  *************************************************/  *************************************************/
2961    
2962  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert  /* Checks whether the base and the current opcode have a common character, in
2963  callout points before each pattern item.  which case the base cannot be possessified.
2964    
2965  Arguments:  Arguments:
2966    code           current code pointer    code        points to the byte code
2967    ptr            current pattern pointer    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2968    cd             pointers to tables etc    cd          static compile data
2969      base_list   the data list of the base opcode
2970    
2971  Returns:         new code pointer  Returns:      TRUE if the auto-possessification is possible
2972  */  */
2973    
2974  static pcre_uchar *  static BOOL
2975  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)  compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2976      const pcre_uint32* base_list, const pcre_uchar *base_end)
2977  {  {
2978  *code++ = OP_CALLOUT;  pcre_uchar c;
2979  *code++ = 255;  pcre_uint32 list[8];
2980  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  const pcre_uint32* chr_ptr;
2981  PUT(code, LINK_SIZE, 0);                       /* Default length */  const pcre_uint32* ochr_ptr;
2982  return code + 2 * LINK_SIZE;  const pcre_uint32* list_ptr;
2983  }  const pcre_uchar *next_code;
2984    const pcre_uint8 *class_bits;
2985    pcre_uint32 chr;
2986    
2987    /* Note: the base_list[1] contains whether the current opcode has greedy
2988    (represented by a non-zero value) quantifier. This is a different from
2989    other character type lists, which stores here that the character iterator
2990    matches to an empty string (also represented by a non-zero value). */
2991    
2992    for(;;)
2993      {
2994      c = *code;
2995    
2996      /* Skip over callouts */
2997    
2998  /*************************************************    if (c == OP_CALLOUT)
2999  *         Complete a callout item                *      {
3000  *************************************************/      code += PRIV(OP_lengths)[c];
3001        continue;
3002        }
3003    
3004  /* A callout item contains the length of the next item in the pattern, which    if (c == OP_ALT)
3005  we can't fill in till after we have reached the relevant point. This is used      {
3006  for both automatic and manual callouts.      do code += GET(code, 1); while (*code == OP_ALT);
3007        c = *code;
3008        }
3009    
3010  Arguments:    switch(c)
3011    previous_callout   points to previous callout item      {
3012    ptr                current pattern pointer      case OP_END:
3013    cd                 pointers to tables etc      case OP_KETRPOS:
3014        /* TRUE only in greedy case. The non-greedy case could be replaced by
3015        an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT
3016        uses more memory, which we cannot get at this stage.) */
3017    
3018  Returns:             nothing      return base_list[1] != 0;
 */  
3019    
3020  static void      case OP_KET:
3021  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)      /* If the bracket is capturing, and referenced by an OP_RECURSE, or
3022  {      it is an atomic sub-pattern (assert, once, etc.) the non-greedy case
3023  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));      cannot be converted to a possessive form. */
3024  PUT(previous_callout, 2 + LINK_SIZE, length);  
3025  }      if (base_list[1] == 0) return FALSE;
3026    
3027        switch(*(code - GET(code, 1)))
3028          {
3029          case OP_ASSERT:
3030          case OP_ASSERT_NOT:
3031          case OP_ASSERTBACK:
3032          case OP_ASSERTBACK_NOT:
3033          case OP_ONCE:
3034          case OP_ONCE_NC:
3035          /* Atomic sub-patterns and assertions can always auto-possessify their
3036          last iterator. */
3037          return TRUE;
3038          }
3039    
3040        code += PRIV(OP_lengths)[c];
3041        continue;
3042    
3043        case OP_ONCE:
3044        case OP_ONCE_NC:
3045        case OP_BRA:
3046        case OP_CBRA:
3047        next_code = code;
3048        do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3049    
3050  #ifdef SUPPORT_UCP      /* We do not support repeated brackets, because they can lead to
3051  /*************************************************      infinite recursion. */
 *           Get othercase range                  *  
 *************************************************/  
3052    
3053  /* This function is passed the start and end of a class range, in UTF-8 mode      if (*next_code != OP_KET) return FALSE;
 with UCP support. It searches up the characters, looking for ranges of  
 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.  
3054    
3055  Arguments:      next_code = code + GET(code, 1);
3056    cptr        points to starting character value; updated      code += PRIV(OP_lengths)[c];
   d           end value  
   ocptr       where to put start of othercase range  
   odptr       where to put end of othercase range  
3057    
3058  Yield:        -1 when no more      while (*next_code == OP_ALT)
3059                 0 when a range is returned        {
3060                >0 the CASESET offset for char with multiple other cases        if (!compare_opcodes(code, utf, cd, base_list, base_end)) return FALSE;
3061                  in this case, ocptr contains the original        code = next_code + 1 + LINK_SIZE;
3062  */        next_code += GET(next_code, 1);
3063          }
3064        continue;
3065    
3066  static int      case OP_BRAZERO:
3067  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,      case OP_BRAMINZERO:
   pcre_uint32 *odptr)  
 {  
 pcre_uint32 c, othercase, next;  
 unsigned int co;  
3068    
3069  /* Find the first character that has an other case. If it has multiple other      next_code = code + 1;
3070  cases, return its case offset value. */      if (*next_code != OP_BRA && *next_code != OP_CBRA)
3071          return FALSE;
3072    
3073  for (c = *cptr; c <= d; c++)      do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3074    {  
3075    if ((co = UCD_CASESET(c)) != 0)      /* We do not support repeated brackets, because they can lead to
3076        infinite recursion. */
3077        if (*next_code != OP_KET) return FALSE;
3078    
3079        /* The bracket content will be checked by the
3080        OP_BRA/OP_CBRA case above. */
3081        next_code += 1 + LINK_SIZE;
3082        if (!compare_opcodes(next_code, utf, cd, base_list, base_end)) return FALSE;
3083    
3084        code += PRIV(OP_lengths)[c];
3085        continue;
3086        }
3087    
3088      /* Check for a supported opcode, and load its properties. */
3089    
3090      code = get_chr_property_list(code, utf, cd->fcc, list);
3091      if (code == NULL) return FALSE;    /* Unsupported */
3092    
3093      /* If either opcode is a small character list, set pointers for comparing
3094      characters from that list with another list, or with a property. */
3095    
3096      if (base_list[0] == OP_CHAR)
3097      {      {
3098      *ocptr = c++;   /* Character that has the set */      chr_ptr = base_list + 2;
3099      *cptr = c;      /* Rest of input range */      list_ptr = list;
3100      return (int)co;      }
3101      else if (list[0] == OP_CHAR)
3102        {
3103        chr_ptr = list + 2;
3104        list_ptr = base_list;
3105      }      }
   if ((othercase = UCD_OTHERCASE(c)) != c) break;  
   }  
3106    
3107  if (c > d) return -1;  /* Reached end of range */    /* Some property combinations also acceptable. Unicode property opcodes are
3108      processed specially; the rest can be handled with a lookup table. */
3109    
3110  *ocptr = othercase;    else
3111  next = othercase + 1;      {
3112        pcre_uint32 leftop, rightop;
3113    
3114  for (++c; c <= d; c++)      if (list[1] != 0) return FALSE;   /* Must match at least one character */
3115    {      leftop = base_list[0];
3116    if (UCD_OTHERCASE(c) != next) break;      rightop = list[0];
   next++;  
   }  
3117    
3118  *odptr = next - 1;     /* End of othercase range */  #ifdef SUPPORT_UCP
3119  *cptr = c;             /* Rest of input range */      if (leftop == OP_PROP || leftop == OP_NOTPROP)
3120  return 0;        {
3121  }        if (rightop == OP_EOD) return TRUE;
3122          if (rightop == OP_PROP || rightop == OP_NOTPROP)
3123            {
3124            int n;
3125            const pcre_uint8 *p;
3126            BOOL same = leftop == rightop;
3127            BOOL lisprop = leftop == OP_PROP;
3128            BOOL risprop = rightop == OP_PROP;
3129            BOOL bothprop = lisprop && risprop;
3130    
3131            /* There's a table that specifies how each combination is to be
3132            processed:
3133              0   Always return FALSE (never auto-possessify)
3134              1   Character groups are distinct (possessify if both are OP_PROP)
3135              2   Check character categories in the same group (general or particular)
3136              3   Return TRUE if the two opcodes are not the same
3137              ... see comments below
3138            */
3139    
3140            n = propposstab[base_list[2]][list[2]];
3141            switch(n)
3142              {
3143              case 0: return FALSE;
3144              case 1: return bothprop;
3145              case 2: return (base_list[3] == list[3]) != same;
3146              case 3: return !same;
3147    
3148              case 4:  /* Left general category, right particular category */
3149              return risprop && catposstab[base_list[3]][list[3]] == same;
3150    
3151              case 5:  /* Right general category, left particular category */
3152              return lisprop && catposstab[list[3]][base_list[3]] == same;
3153    
3154              /* This code is logically tricky. Think hard before fiddling with it.
3155              The posspropstab table has four entries per row. Each row relates to
3156              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3157              Only WORD actually needs all four entries, but using repeats for the
3158              others means they can all use the same code below.
3159    
3160              The first two entries in each row are Unicode general categories, and
3161              apply always, because all the characters they include are part of the
3162              PCRE character set. The third and fourth entries are a general and a
3163              particular category, respectively, that include one or more relevant
3164              characters. One or the other is used, depending on whether the check
3165              is for a general or a particular category. However, in both cases the
3166              category contains more characters than the specials that are defined
3167              for the property being tested against. Therefore, it cannot be used
3168              in a NOTPROP case.
3169    
3170              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3171              Underscore is covered by ucp_P or ucp_Po. */
3172    
3173              case 6:  /* Left alphanum vs right general category */
3174              case 7:  /* Left space vs right general category */
3175              case 8:  /* Left word vs right general category */
3176              p = posspropstab[n-6];
3177              return risprop && lisprop ==
3178                (list[3] != p[0] &&
3179                 list[3] != p[1] &&
3180                (list[3] != p[2] || !lisprop));
3181    
3182              case 9:   /* Right alphanum vs left general category */
3183              case 10:  /* Right space vs left general category */
3184              case 11:  /* Right word vs left general category */
3185              p = posspropstab[n-9];
3186              return lisprop && risprop ==
3187                (base_list[3] != p[0] &&
3188                 base_list[3] != p[1] &&
3189                (base_list[3] != p[2] || !risprop));
3190    
3191              case 12:  /* Left alphanum vs right particular category */
3192              case 13:  /* Left space vs right particular category */
3193              case 14:  /* Left word vs right particular category */
3194              p = posspropstab[n-12];
3195              return risprop && lisprop ==
3196                (catposstab[p[0]][list[3]] &&
3197                 catposstab[p[1]][list[3]] &&
3198                (list[3] != p[3] || !lisprop));
3199    
3200              case 15:  /* Right alphanum vs left particular category */
3201              case 16:  /* Right space vs left particular category */
3202              case 17:  /* Right word vs left particular category */
3203              p = posspropstab[n-15];
3204              return lisprop && risprop ==
3205                (catposstab[p[0]][base_list[3]] &&
3206                 catposstab[p[1]][base_list[3]] &&
3207                (base_list[3] != p[3] || !risprop));
3208              }
3209            }
3210          return FALSE;
3211          }
3212    
3213        else
3214    #endif  /* SUPPORT_UCP */
3215    
3216        return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3217               rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3218               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3219        }
3220    
3221  /*************************************************    /* Control reaches here only if one of the items is a small character list.
3222  *        Check a character and a property        *    All characters are checked against the other side. */
 *************************************************/  
3223    
3224  /* This function is called by check_auto_possessive() when a property item    do
3225  is adjacent to a fixed character.      {
3226        chr = *chr_ptr;
3227    
3228  Arguments:      switch(list_ptr[0])
3229    c            the character        {
3230    ptype        the property type        case OP_CHAR:
3231    pdata        the data for the type        ochr_ptr = list_ptr + 2;
3232    negated      TRUE if it's a negated property (\P or \p{^)        do
3233            {
3234            if (chr == *ochr_ptr) return FALSE;
3235            ochr_ptr++;
3236            }
3237          while(*ochr_ptr != NOTACHAR);
3238          break;
3239    
3240  Returns:       TRUE if auto-possessifying is OK        case OP_NOT:
3241  */        ochr_ptr = list_ptr + 2;
3242          do
3243            {
3244            if (chr == *ochr_ptr)
3245              break;
3246            ochr_ptr++;
3247            }
3248          while(*ochr_ptr != NOTACHAR);
3249          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3250          break;
3251    
3252  static BOOL        /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3253  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated)        set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
 {  
 #ifdef SUPPORT_UCP  
 const pcre_uint32 *p;  
 #endif  
3254    
3255  const ucd_record *prop = GET_UCD(c);        case OP_DIGIT:
3256          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3257          break;
3258    
3259  switch(ptype)        case OP_NOT_DIGIT:
3260    {        if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3261    case PT_LAMP:        break;
   return (prop->chartype == ucp_Lu ||  
           prop->chartype == ucp_Ll ||  
           prop->chartype == ucp_Lt) == negated;  
3262    
3263    case PT_GC:        case OP_WHITESPACE:
3264    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;        if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3265          break;
3266    
3267    case PT_PC:        case OP_NOT_WHITESPACE:
3268    return (pdata == prop->chartype) == negated;        if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3269          break;
3270    
3271    case PT_SC:        case OP_WORDCHAR:
3272    return (pdata == prop->script) == negated;        if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3273          break;
3274    
3275    /* These are specials */        case OP_NOT_WORDCHAR:
3276          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3277          break;
3278    
3279    case PT_ALNUM:        case OP_HSPACE:
3280    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||        switch(chr)
3281            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;          {
3282            HSPACE_CASES: return FALSE;
3283            default: break;
3284            }
3285          break;
3286    
3287    case PT_SPACE:    /* Perl space */        case OP_NOT_HSPACE:
3288    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||        switch(chr)
3289            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)          {
3290            == negated;          HSPACE_CASES: break;
3291            default: return FALSE;
3292            }
3293          break;
3294    
3295    case PT_PXSPACE:  /* POSIX space */        case OP_ANYNL:
3296    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||        case OP_VSPACE:
3297            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||        switch(chr)
3298            c == CHAR_FF || c == CHAR_CR)          {
3299            == negated;          VSPACE_CASES: return FALSE;
3300            default: break;
3301            }
3302          break;
3303    
3304    case PT_WORD:        case OP_NOT_VSPACE:
3305    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||        switch(chr)
3306            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||          {
3307            c == CHAR_UNDERSCORE) == negated;          VSPACE_CASES: break;
3308            default: return FALSE;
3309            }
3310          break;
3311    
3312  #ifdef SUPPORT_UCP        case OP_DOLL:
3313    case PT_CLIST:        case OP_EODN:
3314    p = PRIV(ucd_caseless_sets) + prop->caseset;        switch (chr)
3315    for (;;)          {
3316      {          case CHAR_CR:
3317      if (c < *p) return !negated;          case CHAR_LF:
3318      if (c == *p++) return negated;          case CHAR_VT:
3319      }          case CHAR_FF:
3320    break;  /* Control never reaches here */          case CHAR_NEL:
3321    #ifndef EBCDIC
3322            case 0x2028:
3323            case 0x2029:
3324    #endif  /* Not EBCDIC */
3325            return FALSE;
3326            }
3327          break;
3328    
3329          case OP_EOD:    /* Can always possessify before \z */
3330          break;
3331    
3332          case OP_PROP:
3333          case OP_NOTPROP:
3334          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3335                list_ptr[0] == OP_NOTPROP))
3336            return FALSE;
3337          break;
3338    
3339          case OP_NCLASS:
3340          if (chr > 255) return FALSE;
3341          /* Fall through */
3342    
3343          case OP_CLASS:
3344          if (chr > 255) break;
3345          class_bits = (pcre_uint8 *)((list_ptr == list ? code : base_end) - list_ptr[2]);
3346          if ((class_bits[chr >> 3] & (1 << (chr & 7))) != 0)
3347            return FALSE;
3348          break;
3349    
3350    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3351          case OP_XCLASS:
3352          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3353          if (PRIV(xclass)(chr, code - list_ptr[2] + LINK_SIZE, utf))
3354            return FALSE;
3355          break;
3356  #endif  #endif
3357    
3358          default:
3359          return FALSE;
3360          }
3361    
3362        chr_ptr++;
3363        }
3364      while(*chr_ptr != NOTACHAR);
3365    
3366      /* At least one character must be matched from this opcode. */
3367    
3368      if (list[1] == 0) return TRUE;
3369    }    }
3370    
3371  return FALSE;  return FALSE;
3372  }  }
 #endif  /* SUPPORT_UCP */  
3373    
3374    
3375    
3376  /*************************************************  /*************************************************
3377  *     Check if auto-possessifying is possible    *  *    Scan compiled regex for auto-possession     *
3378  *************************************************/  *************************************************/
3379    
3380  /* This function is called for unlimited repeats of certain items, to see  /* Replaces single character iterations with their possessive alternatives
3381  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.  
3382    
3383  Arguments:  Arguments:
3384    previous      pointer to the repeated opcode    code        points to start of the byte code
3385    utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3386    ptr           next character in pattern    cd          static compile data
   options       options bits  
   cd            contains pointers to tables etc.  
3387    
3388  Returns:        TRUE if possessifying is wanted  Returns:      nothing
3389  */  */
3390    
3391  static BOOL  static void
3392  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)  
3393  {  {
3394  pcre_uint32 c = NOTACHAR;  register pcre_uchar c, d;
3395  pcre_uint32 next;  const pcre_uchar *end;
3396  int escape;  pcre_uchar *repeat_code;
3397  pcre_uchar op_code = *previous++;  pcre_uint32 list[8];
   
 /* Skip whitespace and comments in extended mode */  
3398    
3399  if ((options & PCRE_EXTENDED) != 0)  for (;;)
3400    {    {
3401    for (;;)    c = *code;
3402    
3403      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3404      {      {
3405      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      c -= get_repeat_base(c) - OP_STAR;
3406      if (*ptr == CHAR_NUMBER_SIGN)      end = (c <= OP_MINUPTO) ?
3407          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3408        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3409    
3410        if (end != NULL && compare_opcodes(end, utf, cd, list, end))
3411        {        {
3412        ptr++;        switch(c)
       while (*ptr != CHAR_NULL)  
3413          {          {
3414          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          case OP_STAR:
3415          ptr++;          *code += OP_POSSTAR - OP_STAR;
3416  #ifdef SUPPORT_UTF          break;
         if (utf) FORWARDCHAR(ptr);  
 #endif  
         }  
       }  
     else break;  
     }  
   }  
3417    
3418  /* If the next item is one that we can handle, get its value. A non-negative          case OP_MINSTAR:
3419  value is a character, a negative value is an escape value. */          *code += OP_POSSTAR - OP_MINSTAR;
3420            break;
3421    
3422  if (*ptr == CHAR_BACKSLASH)          case OP_PLUS:
3423    {          *code += OP_POSPLUS - OP_PLUS;
3424    int temperrorcode = 0;          break;
   escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options,  
     FALSE);  
   if (temperrorcode != 0) return FALSE;  
   ptr++;    /* Point after the escape sequence */  
   }  
 else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)  
   {  
   escape = 0;  
 #ifdef SUPPORT_UTF  
   if (utf) { GETCHARINC(next, ptr); } else  
 #endif  
   next = *ptr++;  
   }  
 else return FALSE;  
3425    
3426  /* Skip whitespace and comments in extended mode */          case OP_MINPLUS:
3427            *code += OP_POSPLUS - OP_MINPLUS;
3428            break;
3429    
3430  if ((options & PCRE_EXTENDED) != 0)          case OP_QUERY:
3431    {          *code += OP_POSQUERY - OP_QUERY;
3432    for (;;)          break;
3433    
3434            case OP_MINQUERY:
3435            *code += OP_POSQUERY - OP_MINQUERY;
3436            break;
3437    
3438            case OP_UPTO:
3439            *code += OP_POSUPTO - OP_UPTO;
3440            break;
3441    
3442            case OP_MINUPTO:
3443            *code += OP_MINUPTO - OP_UPTO;
3444            break;
3445            }
3446          }
3447        c = *code;
3448        }
3449      else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS)
3450      {      {
3451      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3452      if (*ptr == CHAR_NUMBER_SIGN)      if (c == OP_XCLASS)
3453          repeat_code = code + 1 + GET(code, 1);
3454        else
3455    #endif
3456          repeat_code = code + 1 + (32 / sizeof(pcre_uchar));
3457    
3458        d = *repeat_code;
3459        if (d >= OP_CRSTAR && d <= OP_CRMINRANGE)
3460        {        {
3461        ptr++;        /* end must not be NULL. */
3462        while (*ptr != CHAR_NULL)        end = get_chr_property_list(code, utf, cd->fcc, list);
3463    
3464          list[1] = d == OP_CRSTAR || d == OP_CRPLUS || d == OP_CRQUERY ||
3465            d == OP_CRRANGE;
3466    
3467          if (compare_opcodes(end, utf, cd, list, end))
3468          {          {
3469          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          switch (d)
3470          ptr++;            {
3471  #ifdef SUPPORT_UTF            case OP_CRSTAR:
3472          if (utf) FORWARDCHAR(ptr);            *repeat_code = OP_CRPOSSTAR;
3473  #endif            break;
3474    
3475              case OP_CRPLUS:
3476              *repeat_code = OP_CRPOSPLUS;
3477              break;
3478    
3479              case OP_CRQUERY:
3480              *repeat_code = OP_CRPOSQUERY;
3481              break;
3482    
3483              case OP_CRRANGE:
3484              *repeat_code = OP_CRPOSRANGE;
3485              break;
3486              }
3487          }          }
3488        }        }
     else break;  
3489      }      }
   }  
3490    
3491  /* If the next thing is itself optional, we have to give up. */    switch(c)
3492        {
3493        case OP_END:
3494        return;
3495    
3496  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||      case OP_TYPESTAR:
3497    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)      case OP_TYPEMINSTAR:
3498      return FALSE;      case OP_TYPEPLUS:
3499        case OP_TYPEMINPLUS:
3500        case OP_TYPEQUERY:
3501        case OP_TYPEMINQUERY:
3502        case OP_TYPEPOSSTAR:
3503        case OP_TYPEPOSPLUS:
3504        case OP_TYPEPOSQUERY:
3505        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3506        break;
3507    
3508  /* If the previous item is a character, get its value. */      case OP_TYPEUPTO:
3509        case OP_TYPEMINUPTO:
3510        case OP_TYPEEXACT:
3511        case OP_TYPEPOSUPTO:
3512        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3513          code += 2;
3514        break;
3515    
3516  if (op_code == OP_CHAR || op_code == OP_CHARI ||  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3517      op_code == OP_NOT || op_code == OP_NOTI)      case OP_XCLASS:
3518    {      code += GET(code, 1);
3519  #ifdef SUPPORT_UTF      break;
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
3520  #endif  #endif
   }  
3521    
3522  /* Now compare the next item with the previous opcode. First, handle cases when      case OP_MARK:
3523  the next item is a character. */      case OP_PRUNE_ARG:
3524        case OP_SKIP_ARG:
3525        case OP_THEN_ARG:
3526        code += code[1];
3527        break;
3528        }
3529    
3530  if (escape == 0)    /* Add in the fixed length from the table */
   {  
   /* 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. */  
3531    
3532  #ifdef SUPPORT_UCP    code += PRIV(OP_lengths)[c];
3533    if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)  
3534      {    /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3535      unsigned int ocs = UCD_CASESET(next);    a multi-byte character. The length in the table is a minimum, so we have to
3536      if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);    arrange to skip the extra bytes. */
     }  
 #endif  
3537    
3538    switch(op_code)  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3539      if (utf) switch(c)
3540      {      {
3541      case OP_CHAR:      case OP_CHAR:
     return c != next;  
   
     /* For CHARI (caseless character) we must check the other case. If we have  
     Unicode property support, we can use it to test the other case of  
     high-valued characters. We know that next can have only one other case,  
     because multi-other-case characters are dealt with above. */  
   
3542      case OP_CHARI:      case OP_CHARI:
     if (c == next) return FALSE;  
 #ifdef SUPPORT_UTF  
     if (utf)  
       {  
       pcre_uint32 othercase;  
       if (next < 128) othercase = cd->fcc[next]; else  
 #ifdef SUPPORT_UCP  
       othercase = UCD_OTHERCASE(next);  
 #else  
       othercase = NOTACHAR;  
 #endif  
       return c != othercase;  
       }  
     else  
 #endif  /* SUPPORT_UTF */  
     return (c != TABLE_GET(next, cd->fcc, next));  /* Not UTF */  
   
3543      case OP_NOT:      case OP_NOT:
     return c == next;  
   
3544      case OP_NOTI:      case OP_NOTI:
3545      if (c == next) return TRUE;      case OP_STAR:
3546  #ifdef SUPPORT_UTF      case OP_MINSTAR:
3547      if (utf)      case OP_PLUS:
3548        {      case OP_MINPLUS:
3549        pcre_uint32 othercase;      case OP_QUERY:
3550        if (next < 128) othercase = cd->fcc[next]; else      case OP_MINQUERY:
3551  #ifdef SUPPORT_UCP      case OP_UPTO:
3552        othercase = UCD_OTHERCASE(next);      case OP_MINUPTO:
3553        case OP_EXACT:
3554        case OP_POSSTAR:
3555        case OP_POSPLUS:
3556        case OP_POSQUERY:
3557        case OP_POSUPTO:
3558        case OP_STARI:
3559        case OP_MINSTARI:
3560        case OP_PLUSI:
3561        case OP_MINPLUSI:
3562        case OP_QUERYI:
3563        case OP_MINQUERYI:
3564        case OP_UPTOI:
3565        case OP_MINUPTOI:
3566        case OP_EXACTI:
3567        case OP_POSSTARI:
3568        case OP_POSPLUSI:
3569        case OP_POSQUERYI:
3570        case OP_POSUPTOI:
3571        case OP_NOTSTAR:
3572        case OP_NOTMINSTAR:
3573        case OP_NOTPLUS:
3574        case OP_NOTMINPLUS:
3575        case OP_NOTQUERY:
3576        case OP_NOTMINQUERY:
3577        case OP_NOTUPTO:
3578        case OP_NOTMINUPTO:
3579        case OP_NOTEXACT:
3580        case OP_NOTPOSSTAR:
3581        case OP_NOTPOSPLUS:
3582        case OP_NOTPOSQUERY:
3583        case OP_NOTPOSUPTO:
3584        case OP_NOTSTARI:
3585        case OP_NOTMINSTARI:
3586        case OP_NOTPLUSI:
3587        case OP_NOTMINPLUSI:
3588        case OP_NOTQUERYI:
3589        case OP_NOTMINQUERYI:
3590        case OP_NOTUPTOI:
3591        case OP_NOTMINUPTOI:
3592        case OP_NOTEXACTI:
3593        case OP_NOTPOSSTARI:
3594        case OP_NOTPOSPLUSI:
3595        case OP_NOTPOSQUERYI:
3596        case OP_NOTPOSUPTOI:
3597        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3598        break;
3599        }
3600  #else  #else
3601        othercase = NOTACHAR;    (void)(utf);  /* Keep compiler happy by referencing function argument */
3602  #endif  #endif
3603        return c == othercase;    }
3604        }  }
     else  
 #endif  /* SUPPORT_UTF */  
     return (c == TABLE_GET(next, cd->fcc, next));  /* Not UTF */  
3605    
     /* 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. */  
3606    
     case OP_DIGIT:  
     return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;  
3607    
3608      case OP_NOT_DIGIT:  /*************************************************
3609      return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;  *           Check for POSIX class syntax         *
3610    *************************************************/
3611    
3612      case OP_WHITESPACE:  /* This function is called when the sequence "[:" or "[." or "[=" is
3613      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;  encountered in a character class. It checks whether this is followed by a
3614    sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3615    reach an unescaped ']' without the special preceding character, return FALSE.
3616    
3617      case OP_NOT_WHITESPACE:  Originally, this function only recognized a sequence of letters between the
3618      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;  terminators, but it seems that Perl recognizes any sequence of characters,
3619    though of course unknown POSIX names are subsequently rejected. Perl gives an
3620    "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3621    didn't consider this to be a POSIX class. Likewise for [:1234:].
3622    
3623      case OP_WORDCHAR:  The problem in trying to be exactly like Perl is in the handling of escapes. We
3624      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3625    class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3626    below handles the special case of \], but does not try to do any other escape
3627    processing. This makes it different from Perl for cases such as [:l\ower:]
3628    where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
3629    "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
3630    I think.
3631    
3632      case OP_NOT_WORDCHAR:  A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3633      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;  It seems that the appearance of a nested POSIX class supersedes an apparent
3634    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3635    a digit.
3636    
3637      case OP_HSPACE:  In Perl, unescaped square brackets may also appear as part of class names. For
3638      case OP_NOT_HSPACE:  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3639      switch(next)  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3640        {  seem right at all. PCRE does not allow closing square brackets in POSIX class
3641        HSPACE_CASES:  names.
       return op_code == OP_NOT_HSPACE;  
3642    
3643        default:  Arguments:
3644        return op_code != OP_NOT_HSPACE;    ptr      pointer to the initial [
3645        }    endptr   where to return the end pointer
3646    
3647      case OP_ANYNL:  Returns:   TRUE or FALSE
3648      case OP_VSPACE:  */
     case OP_NOT_VSPACE:  
     switch(next)  
       {  
       VSPACE_CASES:  
       return op_code == OP_NOT_VSPACE;  
3649    
3650        default:  static BOOL
3651        return op_code != OP_NOT_VSPACE;  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3652    {
3653    pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3654    terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3655    for (++ptr; *ptr != CHAR_NULL; ptr++)
3656      {
3657      if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3658        ptr++;
3659      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3660      else
3661        {
3662        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3663          {
3664          *endptr = ptr;
3665          return TRUE;
3666        }        }
3667        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3668             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3669              ptr[1] == CHAR_EQUALS_SIGN) &&
3670            check_posix_syntax(ptr, endptr))
3671          return FALSE;
3672        }
3673      }
3674    return FALSE;
3675    }
3676    
 #ifdef SUPPORT_UCP  
     case OP_PROP:  
     return check_char_prop(next, previous[0], previous[1], FALSE);  
3677    
     case OP_NOTPROP:  
     return check_char_prop(next, previous[0], previous[1], TRUE);  
 #endif  
3678    
     default:  
     return FALSE;  
     }  
   }  
3679    
3680  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  /*************************************************
3681  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  *          Check POSIX class name                *
3682  generated only when PCRE_UCP is *not* set, that is, when only ASCII  *************************************************/
3683  characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are  
3684  replaced by OP_PROP codes when PCRE_UCP is set. */  /* This function is called to check the name given in a POSIX-style class entry
3685    such as [:alnum:].
3686    
3687  switch(op_code)  Arguments:
3688      ptr        points to the first letter
3689      len        the length of the name
3690    
3691    Returns:     a value representing the name, or -1 if unknown
3692    */
3693    
3694    static int
3695    check_posix_name(const pcre_uchar *ptr, int len)
3696    {
3697    const char *pn = posix_names;
3698    register int yield = 0;
3699    while (posix_name_lengths[yield] != 0)
3700    {    {
3701    case OP_CHAR:    if (len == posix_name_lengths[yield] &&
3702    case OP_CHARI:      STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3703    switch(escape)    pn += posix_name_lengths[yield] + 1;
3704      {    yield++;
3705      case ESC_d:    }
3706      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;  return -1;
3707    }
3708    
3709    
3710    /*************************************************
3711    *    Adjust OP_RECURSE items in repeated group   *
3712    *************************************************/
3713    
3714    /* OP_RECURSE items contain an offset from the start of the regex to the group
3715    that is referenced. This means that groups can be replicated for fixed
3716    repetition simply by copying (because the recursion is allowed to refer to
3717    earlier groups that are outside the current group). However, when a group is
3718    optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
3719    inserted before it, after it has been compiled. This means that any OP_RECURSE
3720    items within it that refer to the group itself or any contained groups have to
3721    have their offsets adjusted. That one of the jobs of this function. Before it
3722    is called, the partially compiled regex must be temporarily terminated with
3723    OP_END.
3724    
3725    This function has been extended with the possibility of forward references for
3726    recursions and subroutine calls. It must also check the list of such references
3727    for the group we are dealing with. If it finds that one of the recursions in
3728    the current group is on this list, it adjusts the offset in the list, not the
3729    value in the reference (which is a group number).
3730    
3731      case ESC_D:  Arguments:
3732      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;    group      points to the start of the group
3733      adjust     the amount by which the group is to be moved
3734      utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3735      cd         contains pointers to tables etc.
3736      save_hwm   the hwm forward reference pointer at the start of the group
3737    
3738      case ESC_s:  Returns:     nothing
3739      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;  */
3740    
3741      case ESC_S:  static void
3742      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
3743      pcre_uchar *save_hwm)
3744    {
3745    pcre_uchar *ptr = group;
3746    
3747      case ESC_w:  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
3748      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;    {
3749      int offset;
3750      pcre_uchar *hc;
3751    
3752      case ESC_W:    /* See if this recursion is on the forward reference list. If so, adjust the
3753      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;    reference. */
3754    
3755      case ESC_h:    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
3756      case ESC_H:      {
3757      switch(c)      offset = (int)GET(hc, 0);
3758        if (cd->start_code + offset == ptr + 1)
3759        {        {
3760        HSPACE_CASES:        PUT(hc, 0, offset + adjust);
3761        return escape != ESC_h;        break;
3762          }
3763        }
3764    
3765      /* Otherwise, adjust the recursion offset if it's after the start of this
3766      group. */
3767    
3768      if (hc >= cd->hwm)
3769        {
3770        offset = (int)GET(ptr, 1);
3771        if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
3772        }
3773    
3774      ptr += 1 + LINK_SIZE;
3775      }
3776    }
3777    
3778    
3779    
3780        default:  /*************************************************
3781        return escape == ESC_h;  *        Insert an automatic callout point       *
3782        }  *************************************************/
3783    
3784      case ESC_v:  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
3785      case ESC_V:  callout points before each pattern item.
     switch(c)  
       {  
       VSPACE_CASES:  
       return escape != ESC_v;  
3786    
3787        default:  Arguments:
3788        return escape == ESC_v;    code           current code pointer
3789        }    ptr            current pattern pointer
3790      cd             pointers to tables etc
3791    
3792      /* When PCRE_UCP is set, these values get generated for \d etc. Find  Returns:         new code pointer
3793      their substitutions and process them. The result will always be either  */
     ESC_p or ESC_P. Then fall through to process those values. */  
3794    
3795  #ifdef SUPPORT_UCP  static pcre_uchar *
3796      case ESC_du:  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
3797      case ESC_DU:  {
3798      case ESC_wu:  *code++ = OP_CALLOUT;
3799      case ESC_WU:  *code++ = 255;
3800      case ESC_su:  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
3801      case ESC_SU:  PUT(code, LINK_SIZE, 0);                       /* Default length */
3802        {  return code + 2 * LINK_SIZE;
3803        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 */  
3804    
     case ESC_p:  
     case ESC_P:  
       {  
       unsigned int ptype = 0, pdata = 0;  
       int errorcodeptr;  
       BOOL negated;  
3805    
       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 */  
3806    
3807        /* If the property item is optional, we have to give up. (When generated  /*************************************************
3808        from \d etc by PCRE_UCP, this test will have been applied much earlier,  *         Complete a callout item                *
3809        to the original \d etc. At this point, ptr will point to a zero byte. */  *************************************************/
3810    
3811        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  /* A callout item contains the length of the next item in the pattern, which
3812          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
3813            return FALSE;  for both automatic and manual callouts.
3814    
3815        /* Do the property check. */  Arguments:
3816      previous_callout   points to previous callout item
3817      ptr                current pattern pointer
3818      cd                 pointers to tables etc
3819    
3820        return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);  Returns:             nothing
3821        }  */
 #endif  
3822    
3823      default:  static void
3824      return FALSE;  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
3825      }  {
3826    int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
3827    PUT(previous_callout, 2 + LINK_SIZE, length);
3828    }
3829    
   /* 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.) */  
3830    
   case OP_DIGIT:  
   return escape == ESC_D || escape == ESC_s || escape == ESC_W ||  
          escape == ESC_h || escape == ESC_v || escape == ESC_R;  
3831    
3832    case OP_NOT_DIGIT:  #ifdef SUPPORT_UCP
3833    return escape == ESC_d;  /*************************************************
3834    *           Get othercase range                  *
3835    *************************************************/
3836    
3837    case OP_WHITESPACE:  /* This function is passed the start and end of a class range, in UTF-8 mode
3838    return escape == ESC_S || escape == ESC_d || escape == ESC_w;  with UCP support. It searches up the characters, looking for ranges of
3839    characters in the "other" case. Each call returns the next one, updating the
3840    start address. A character with multiple other cases is returned on its own
3841    with a special return value.
3842    
3843    case OP_NOT_WHITESPACE:  Arguments:
3844    return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;    cptr        points to starting character value; updated
3845      d           end value
3846      ocptr       where to put start of othercase range
3847      odptr       where to put end of othercase range
3848    
3849    case OP_HSPACE:  Yield:        -1 when no more
3850    return escape == ESC_S || escape == ESC_H || escape == ESC_d ||                 0 when a range is returned
3851           escape == ESC_w || escape == ESC_v || escape == ESC_R;                >0 the CASESET offset for char with multiple other cases
3852                    in this case, ocptr contains the original
3853    */
3854    
3855    case OP_NOT_HSPACE:  static int
3856    return escape == ESC_h;  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
3857      pcre_uint32 *odptr)
3858    {
3859    pcre_uint32 c, othercase, next;
3860    unsigned int co;
3861    
3862    /* 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
3863    case OP_ANYNL:  cases, return its case offset value. */
   case OP_VSPACE:  
   return escape == ESC_V || escape == ESC_d || escape == ESC_w;  
3864    
3865    case OP_NOT_VSPACE:  for (c = *cptr; c <= d; c++)
3866    return escape == ESC_v || escape == ESC_R;    {
3867      if ((co = UCD_CASESET(c)) != 0)
3868        {
3869        *ocptr = c++;   /* Character that has the set */
3870        *cptr = c;      /* Rest of input range */
3871        return (int)co;
3872        }
3873      if ((othercase = UCD_OTHERCASE(c)) != c) break;
3874      }
3875    
3876    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;  
3877    
3878    case OP_NOT_WORDCHAR:  *ocptr = othercase;
3879    return escape == ESC_w || escape == ESC_d;  next = othercase + 1;
3880    
3881    default:  for (++c; c <= d; c++)
3882    return FALSE;    {
3883      if (UCD_OTHERCASE(c) != next) break;
3884      next++;
3885    }    }
3886    
3887  /* Control does not reach here */  *odptr = next - 1;     /* End of othercase range */
3888    *cptr = c;             /* Rest of input range */
3889    return 0;
3890  }  }
3891    #endif  /* SUPPORT_UCP */
3892    
3893    
3894    
# Line 3754  to find out the amount of memory needed, Line 4141  to find out the amount of memory needed,
4141  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
4142    
4143  Arguments:  Arguments:
4144    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
4145    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
4146    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
4147    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
4148    firstcharptr    place to put the first required character    firstcharptr      place to put the first required character
4149    firstcharflagsptr place to put the first character flags, or a negative number    firstcharflagsptr place to put the first character flags, or a negative number
4150    reqcharptr     place to put the last required character    reqcharptr        place to put the last required character
4151    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
4152    bcptr          points to current branch chain    bcptr             points to current branch chain
4153    cond_depth     conditional nesting depth    cond_depth        conditional nesting depth
4154    cd             contains pointers to tables etc.    cd                contains pointers to tables etc.
4155    lengthptr      NULL during the real compile phase    lengthptr         NULL during the real compile phase
4156                   points to length accumulator during pre-compile phase                      points to length accumulator during pre-compile phase
4157    
4158  Returns:         TRUE on success  Returns:            TRUE on success
4159                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
4160  */  */
4161    
4162  static BOOL  static BOOL
# Line 3994  for (;; ptr++) Line 4381  for (;; ptr++)
4381        }        }
4382      }      }
4383    
   /* Fill in length of a previous callout, except when the next thing is  
   a quantifier. */  
   
4384    is_quantifier =    is_quantifier =
4385      c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||      c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
4386      (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));      (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
4387    
4388    if (!is_quantifier && previous_callout != NULL &&    /* Fill in length of a previous callout, except when the next thing is a
4389      quantifier or when processing a property substitution string in UCP mode. */
4390    
4391      if (!is_quantifier && previous_callout != NULL && nestptr == NULL &&
4392         after_manual_callout-- <= 0)         after_manual_callout-- <= 0)
4393      {      {
4394      if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */      if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */
# Line 4032  for (;; ptr++) Line 4419  for (;; ptr++)
4419        }        }
4420      }      }
4421    
4422    /* No auto callout for quantifiers. */    /* No auto callout for quantifiers, or while processing property strings that
4423      are substituted for \w etc in UCP mode. */
4424    
4425    if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier)    if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL)
4426      {      {
4427      previous_callout = code;      previous_callout = code;
4428      code = auto_callout(code, ptr, cd);      code = auto_callout(code, ptr, cd);
# Line 4420  for (;; ptr++) Line 4808  for (;; ptr++)
4808              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
4809              continue;              continue;
4810    
4811              /* 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
4812              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
4813              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and              previously set by something earlier in the character class.
4814              EBCDIC, so we lazily just adjust the appropriate bit. */              Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
4815                we could just adjust the appropriate bit. From PCRE 8.34 we no
4816                longer treat \s and \S specially. */
4817    
4818              case ESC_s:              case ESC_s:
4819              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];  
4820              continue;              continue;
4821    
4822              case ESC_S:              case ESC_S:
4823              should_flip_negation = TRUE;              should_flip_negation = TRUE;
4824              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 */  
4825              continue;              continue;
4826    
4827              /* The rest apply in both UCP and non-UCP cases. */              /* The rest apply in both UCP and non-UCP cases. */
# Line 4933  for (;; ptr++) Line 5320  for (;; ptr++)
5320            }            }
5321          }          }
5322    
       /* 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;  
         }  
   
5323        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
5324        }        }
5325    
# Line 4963  for (;; ptr++) Line 5337  for (;; ptr++)
5337        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
5338        c = *previous;        c = *previous;
5339    
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
   
5340        OUTPUT_SINGLE_REPEAT:        OUTPUT_SINGLE_REPEAT:
5341        if (*previous == OP_PROP || *previous == OP_NOTPROP)        if (*previous == OP_PROP || *previous == OP_NOTPROP)
5342          {          {
# Line 5119  for (;; ptr++) Line 5485  for (;; ptr++)
5485      /* 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
5486      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}. */
5487    
5488      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
              *previous == OP_NCLASS ||  
5489  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5490               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
5491  #endif  #endif
5492               *previous == OP_REF ||               *previous == OP_REF   || *previous == OP_REFI ||
5493               *previous == OP_REFI)               *previous == OP_DNREF || *previous == OP_DNREFI)
5494        {        {
5495        if (repeat_max == 0)        if (repeat_max == 0)
5496          {          {
# Line 5846  for (;; ptr++) Line 6211  for (;; ptr++)
6211                 tempptr[2] == CHAR_LESS_THAN_SIGN))                 tempptr[2] == CHAR_LESS_THAN_SIGN))
6212            break;            break;
6213    
6214          /* 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
6215          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. */
6216    
6217          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
6218          skipbytes = 1+IMM2_SIZE;          skipbytes = 1+IMM2_SIZE;
# Line 5863  for (;; ptr++) Line 6228  for (;; ptr++)
6228            }            }
6229    
6230          /* 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
6231          syntax (?(<name>) or (?('name') */          syntax (?(<name>) or (?('name'), and also allow for the original PCRE
6232            syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). As names may
6233            consist entirely of digits, there is scope for ambiguity. */
6234    
6235          else if (ptr[1] == CHAR_LESS_THAN_SIGN)          else if (ptr[1] == CHAR_LESS_THAN_SIGN)
6236            {            {
# Line 5881  for (;; ptr++) Line 6248  for (;; ptr++)
6248            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);
6249            }            }
6250    
6251          /* We now expect to read a name; any thing else is an error */          /* When a name is one of a number of duplicates, a different opcode is
6252            used and it needs more memory. Unfortunately we cannot tell whether a
6253            name is a duplicate in the first pass, so we have to allow for more
6254            memory except when we know it is a relative numerical reference. */
6255    
6256            if (refsign < 0 && lengthptr != NULL) *lengthptr += IMM2_SIZE;
6257    
6258            /* We now expect to read a name (possibly all digits); any thing else
6259            is an error. In the case of all digits, also get it as a number. */
6260    
6261          if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)          if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)
6262            {            {
# Line 5890  for (;; ptr++) Line 6265  for (;; ptr++)
6265            goto FAILED;            goto FAILED;
6266            }            }
6267    
         /* Read the name, but also get it as a number if it's all digits */  
   
6268          recno = 0;          recno = 0;
6269          name = ++ptr;          name = ++ptr;
6270          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
# Line 5902  for (;; ptr++) Line 6275  for (;; ptr++)
6275            }            }
6276          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
6277    
6278            /* Check the terminator */
6279    
6280          if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||          if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
6281              *ptr++ != CHAR_RIGHT_PARENTHESIS)              *ptr++ != CHAR_RIGHT_PARENTHESIS)
6282            {            {
# Line 5937  for (;; ptr++) Line 6312  for (;; ptr++)
6312            }            }
6313    
6314          /* Otherwise (did not start with "+" or "-"), start by looking for the          /* Otherwise (did not start with "+" or "-"), start by looking for the
6315          name. If we find a name, add one to the opcode to change OP_CREF or          name. */
         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. */  
6316    
6317          slot = cd->name_table;          slot = cd->name_table;
6318          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
# Line 5949  for (;; ptr++) Line 6321  for (;; ptr++)
6321            slot += cd->name_entry_size;            slot += cd->name_entry_size;
6322            }            }
6323    
6324          /* Found a previous named subpattern */          /* Found the named subpattern. If the name is duplicated, add one to
6325            the opcode to change CREF/RREF into DNCREF/DNRREF and insert
6326            appropriate data values. Otherwise, just insert the unique subpattern
6327            number. */
6328    
6329          if (i < cd->names_found)          if (i < cd->names_found)
6330            {            {
6331            recno = GET2(slot, 0);            int offset = i++;
6332            PUT2(code, 2+LINK_SIZE, recno);            int count = 1;
6333            code[1+LINK_SIZE]++;            recno = GET2(slot, 0);   /* Number from first found */
6334            }            for (; i < cd->names_found; i++)
6335                {
6336          /* Search the pattern for a forward reference */              slot += cd->name_entry_size;
6337                if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) != 0) break;
6338          else if ((i = find_parens(cd, name, namelen,              count++;
6339                          (options & PCRE_EXTENDED) != 0, utf)) > 0)              }
6340            {            if (count > 1)
6341            PUT2(code, 2+LINK_SIZE, i);              {
6342            code[1+LINK_SIZE]++;              PUT2(code, 2+LINK_SIZE, offset);
6343                PUT2(code, 2+LINK_SIZE+IMM2_SIZE, count);
6344                skipbytes += IMM2_SIZE;
6345                code[1+LINK_SIZE]++;
6346                }
6347              else  /* Not a duplicated name */
6348                {
6349                PUT2(code, 2+LINK_SIZE, recno);
6350                }
6351            }            }
6352    
6353          /* If terminator == CHAR_NULL it means that the name followed directly          /* If terminator == CHAR_NULL it means that the name followed directly
# Line 6130  for (;; ptr++) Line 6513  for (;; ptr++)
6513          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
6514          DEFINE_NAME:    /* Come here from (?< handling */          DEFINE_NAME:    /* Come here from (?< handling */
6515          case CHAR_APOSTROPHE:          case CHAR_APOSTROPHE:
6516            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
6517              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
6518            name = ++ptr;
6519    
6520            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
6521            namelen = (int)(ptr - name);
6522    
6523            /* In the pre-compile phase, do a syntax check, remember the longest
6524            name, and then remember the group in a vector, expanding it if
6525            necessary. Duplicates for the same number are skipped; other duplicates
6526            are checked for validity. In the actual compile, there is nothing to
6527            do. */
6528    
6529            if (lengthptr != NULL)
6530            {            {
6531            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?            named_group *ng;
6532              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;            pcre_uint32 number = cd->bracount + 1;
           name = ++ptr;  
6533    
6534            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;            if (*ptr != (pcre_uchar)terminator)
6535            namelen = (int)(ptr - name);              {
6536                *errorcodeptr = ERR42;
6537                goto FAILED;
6538                }
6539    
6540            /* In the pre-compile phase, just do a syntax check. */            if (cd->names_found >= MAX_NAME_COUNT)
6541                {
6542                *errorcodeptr = ERR49;
6543                goto FAILED;
6544                }
6545    
6546            if (lengthptr != NULL)            if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
6547              {              {
6548              if (*ptr != (pcre_uchar)terminator)              cd->name_entry_size = namelen + IMM2_SIZE + 1;
6549                {              if (namelen > MAX_NAME_SIZE)
               *errorcodeptr = ERR42;  
               goto FAILED;  
               }  
             if (cd->names_found >= MAX_NAME_COUNT)  
6550                {                {
6551                *errorcodeptr = ERR49;                *errorcodeptr = ERR48;
6552                goto FAILED;                goto FAILED;
6553                }                }
6554              if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)              }
6555    
6556              /* Scan the list to check for duplicates. For duplicate names, if the
6557              number is the same, break the loop, which causes the name to be
6558              discarded; otherwise, if DUPNAMES is not set, give an error.
6559              If it is set, allow the name with a different number, but continue
6560              scanning in case this is a duplicate with the same number. For
6561              non-duplicate names, give an error if the number is duplicated. */
6562    
6563              ng = cd->named_groups;
6564              for (i = 0; i < cd->names_found; i++, ng++)
6565                {
6566                if (namelen == ng->length &&
6567                    STRNCMP_UC_UC(name, ng->name, namelen) == 0)
6568                {                {
6569                cd->name_entry_size = namelen + IMM2_SIZE + 1;                if (ng->number == number) break;
6570                if (namelen > MAX_NAME_SIZE)                if ((options & PCRE_DUPNAMES) == 0)
6571                  {                  {
6572                  *errorcodeptr = ERR48;                  *errorcodeptr = ERR43;
6573                  goto FAILED;                  goto FAILED;
6574                  }                  }
6575                  cd->dupnames = TRUE;  /* Duplicate names exist */
6576                  }
6577                else if (ng->number == number)
6578                  {
6579                  *errorcodeptr = ERR65;
6580                  goto FAILED;
6581                }                }
6582              }              }
6583    
6584            /* 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  
6585              {              {
6586              BOOL dupname = FALSE;              /* Increase the list size if necessary */
             slot = cd->name_table;  
6587    
6588              for (i = 0; i < cd->names_found; i++)              if (cd->names_found >= cd->named_group_list_size)
6589                {                {
6590                int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(namelen));                int newsize = cd->named_group_list_size * 2;
6591                if (crc == 0)                named_group *newspace = (PUBL(malloc))
6592                  {                  (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. */  
6593    
6594                if (crc < 0)                if (newspace == NULL)
6595                  {                  {
6596                  memmove(slot + cd->name_entry_size, slot,                  *errorcodeptr = ERR21;
6597                    IN_UCHARS((cd->names_found - i) * cd->name_entry_size));                  goto FAILED;
                 break;  
6598                  }                  }
6599    
6600                /* Continue the loop for a later or duplicate name */                memcpy(newspace, cd->named_groups,
6601                    cd->named_group_list_size * sizeof(named_group));
6602                slot += cd->name_entry_size;                if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
6603                }                  (PUBL(free))((void *)cd->named_groups);
6604                  cd->named_groups = newspace;
6605              /* 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;  
                 }  
6606                }                }
6607    
6608              PUT2(slot, 0, cd->bracount + 1);              cd->named_groups[cd->names_found].name = name;
6609              memcpy(slot + IMM2_SIZE, name, IN_UCHARS(namelen));              cd->named_groups[cd->names_found].length = namelen;
6610              slot[IMM2_SIZE + namelen] = 0;              cd->named_groups[cd->names_found].number = number;
6611                cd->names_found++;
6612              }              }
6613            }            }
6614    
6615          /* 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 ' */  
6616          goto NUMBERED_GROUP;          goto NUMBERED_GROUP;
6617    
6618    
# Line 6277  for (;; ptr++) Line 6642  for (;; ptr++)
6642    
6643          if (lengthptr != NULL)          if (lengthptr != NULL)
6644            {            {
6645            const pcre_uchar *temp;            named_group *ng;
6646    
6647            if (namelen == 0)            if (namelen == 0)
6648              {              {
# Line 6295  for (;; ptr++) Line 6660  for (;; ptr++)
6660              goto FAILED;              goto FAILED;
6661              }              }
6662    
6663            /* 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
6664            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
6665            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
6666            far as we have got because the syntax of named subpatterns has not            reference. */
6667            been checked for the rest of the pattern, and find_parens() assumes  
6668            correct syntax. In any case, it's a waste of resources to scan            ng = cd->named_groups;
6669            further. We stop the scan at the current point by temporarily            for (i = 0; i < cd->names_found; i++, ng++)
6670            adjusting the value of cd->endpattern. */              {
6671                if (namelen == ng->length &&
6672            temp = cd->end_pattern;                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)
6673            cd->end_pattern = ptr;                break;
6674            recno = find_parens(cd, name, namelen,              }
6675              (options & PCRE_EXTENDED) != 0, utf);            recno = (i < cd->names_found)? ng->number : 0;
6676            cd->end_pattern = temp;  
6677            if (recno < 0) recno = 0;    /* Forward ref; set dummy number */            /* Count named back references. */
6678    
6679              if (!is_recurse) cd->namedrefcount++;
6680            }            }
6681    
6682          /* 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
6683          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
6684          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
6685          the comparison will fail without reading beyond the table entry. */          comparison will fail without reading beyond the table entry. */
6686    
6687          else          else
6688            {            {
# Line 6328  for (;; ptr++) Line 6695  for (;; ptr++)
6695              slot += cd->name_entry_size;              slot += cd->name_entry_size;
6696              }              }
6697    
6698            if (i < cd->names_found)         /* Back reference */            if (i < cd->names_found)
6699              {              {
6700              recno = GET2(slot, 0);              recno = GET2(slot, 0);
6701              }              }
6702            else if ((recno =                /* Forward back reference */            else
                     find_parens(cd, name, namelen,  
                       (options & PCRE_EXTENDED) != 0, utf)) <= 0)  
6703              {              {
6704              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
6705              goto FAILED;              goto FAILED;
6706              }              }
6707            }            }
6708    
6709          /* 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
6710          recursion or backreferences. */          handles numerical recursion. */
6711    
6712          if (is_recurse) goto HANDLE_RECURSION;          if (is_recurse) goto HANDLE_RECURSION;
6713            else goto HANDLE_REFERENCE;  
6714            /* In the second pass we must see if the name is duplicated. If so, we
6715            generate a different opcode. */
6716    
6717            if (lengthptr == NULL && cd->dupnames)
6718              {
6719              int count = 1;
6720              unsigned int index = i;
6721              pcre_uchar *cslot = slot + cd->name_entry_size;
6722    
6723              for (i++; i < cd->names_found; i++)
6724                {
6725                if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break;
6726                count++;
6727                cslot += cd->name_entry_size;
6728                }
6729    
6730              if (count > 1)
6731                {
6732                if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
6733                previous = code;
6734                *code++ = ((options & PCRE_CASELESS) != 0)? OP_DNREFI : OP_DNREF;
6735                PUT2INC(code, 0, index);
6736                PUT2INC(code, 0, count);
6737    
6738                /* Process each potentially referenced group. */
6739    
6740                for (; slot < cslot; slot += cd->name_entry_size)
6741                  {
6742                  open_capitem *oc;
6743                  recno = GET2(slot, 0);
6744                  cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6745                  if (recno > cd->top_backref) cd->top_backref = recno;
6746    
6747                  /* Check to see if this back reference is recursive, that it, it
6748                  is inside the group that it references. A flag is set so that the
6749                  group can be made atomic. */
6750    
6751                  for (oc = cd->open_caps; oc != NULL; oc = oc->next)
6752                    {
6753                    if (oc->number == recno)
6754                      {
6755                      oc->flag = TRUE;
6756                      break;
6757                      }
6758                    }
6759                  }
6760    
6761                continue;  /* End of back ref handling */
6762                }
6763              }
6764    
6765            /* First pass, or a non-duplicated name. */
6766    
6767            goto HANDLE_REFERENCE;
6768    
6769    
6770          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
# Line 6444  for (;; ptr++) Line 6863  for (;; ptr++)
6863    
6864              if (called == NULL)              if (called == NULL)
6865                {                {
6866                if (find_parens(cd, NULL, recno,                if (recno > cd->final_bracount)
                     (options & PCRE_EXTENDED) != 0, utf) < 0)  
6867                  {                  {
6868                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
6869                  goto FAILED;                  goto FAILED;
# Line 6929  for (;; ptr++) Line 7347  for (;; ptr++)
7347          open_capitem *oc;          open_capitem *oc;
7348          recno = -escape;          recno = -escape;
7349    
7350          HANDLE_REFERENCE:    /* Come here from named backref handling */          /* Come here from named backref handling when the reference is to a
7351            single group (i.e. not to a duplicated name. */
7352    
7353            HANDLE_REFERENCE:
7354          if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;          if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
7355          previous = code;          previous = code;
7356          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
# Line 7058  for (;; ptr++) Line 7479  for (;; ptr++)
7479          *code++ = OP_PROP;          *code++ = OP_PROP;
7480          *code++ = PT_CLIST;          *code++ = PT_CLIST;
7481          *code++ = c;          *code++ = c;
7482          if (firstcharflags == REQ_UNSET) firstcharflags = zerofirstcharflags = REQ_NONE;          if (firstcharflags == REQ_UNSET)
7483              firstcharflags = zerofirstcharflags = REQ_NONE;
7484          break;          break;
7485          }          }
7486        }        }
# Line 7147  out the amount of memory needed, as well Line 7569  out the amount of memory needed, as well
7569  value of lengthptr distinguishes the two phases.  value of lengthptr distinguishes the two phases.
7570    
7571  Arguments:  Arguments:
7572    options        option bits, including any changes for this subpattern    options           option bits, including any changes for this subpattern
7573    codeptr        -> the address of the current code pointer    codeptr           -> the address of the current code pointer
7574    ptrptr         -> the address of the current pattern pointer    ptrptr            -> the address of the current pattern pointer
7575    errorcodeptr   -> pointer to error code variable    errorcodeptr      -> pointer to error code variable
7576    lookbehind     TRUE if this is a lookbehind assertion    lookbehind        TRUE if this is a lookbehind assertion
7577    reset_bracount TRUE to reset the count for each branch    reset_bracount    TRUE to reset the count for each branch
7578    skipbytes      skip this many bytes at start (for brackets and OP_COND)    skipbytes         skip this many bytes at start (for brackets and OP_COND)
7579    cond_depth     depth of nesting for conditional subpatterns    cond_depth        depth of nesting for conditional subpatterns
7580    firstcharptr    place to put the first required character    firstcharptr      place to put the first required character
7581    firstcharflagsptr place to put the first character flags, or a negative number    firstcharflagsptr place to put the first character flags, or a negative number
7582    reqcharptr     place to put the last required character    reqcharptr        place to put the last required character
7583    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
7584    bcptr          pointer to the chain of currently open branches    bcptr             pointer to the chain of currently open branches
7585    cd             points to the data block with tables pointers etc.    cd                points to the data block with tables pointers etc.
7586    lengthptr      NULL during the real compile phase    lengthptr         NULL during the real compile phase
7587                   points to length accumulator during pre-compile phase                      points to length accumulator during pre-compile phase
7588    
7589  Returns:         TRUE on success  Returns:            TRUE on success
7590  */  */
7591    
7592  static BOOL  static BOOL
# Line 7615  do { Line 8037  do {
8037       switch (*scode)       switch (*scode)
8038         {         {
8039         case OP_CREF:         case OP_CREF:
8040         case OP_NCREF:         case OP_DNCREF:
8041         case OP_RREF:         case OP_RREF:
8042         case OP_NRREF:         case OP_DNRREF:
8043         case OP_DEF:         case OP_DEF:
8044         return FALSE;         return FALSE;
8045    
# Line 7701  return TRUE; Line 8123  return TRUE;
8123  discarded, because they can cause conflicts with actual literals that follow.  discarded, because they can cause conflicts with actual literals that follow.
8124  However, if we end up without a first char setting for an unanchored pattern,  However, if we end up without a first char setting for an unanchored pattern,
8125  it is worth scanning the regex to see if there is an initial asserted first  it is worth scanning the regex to see if there is an initial asserted first
8126  char. If all branches start with the same asserted char, or with a bracket all  char. If all branches start with the same asserted char, or with a
8127  of whose alternatives start with the same asserted char (recurse ad lib), then  non-conditional bracket all of whose alternatives start with the same asserted
8128  we return that char, otherwise -1.  char (recurse ad lib), then we return that char, with the flags set to zero or
8129    REQ_CASELESS; otherwise return zero with REQ_NONE in the flags.
8130    
8131  Arguments:  Arguments:
8132    code       points to start of expression (the bracket)    code       points to start of expression (the bracket)
8133    flags       points to the first char flags, or to REQ_NONE    flags      points to the first char flags, or to REQ_NONE
8134    inassert   TRUE if in an assertion    inassert   TRUE if in an assertion
8135    
8136  Returns:     the fixed first char, or 0 with REQ_NONE in flags  Returns:     the fixed first char, or 0 with REQ_NONE in flags
# Line 7744  do { Line 8167  do {
8167       case OP_ASSERT:       case OP_ASSERT:
8168       case OP_ONCE:       case OP_ONCE:
8169       case OP_ONCE_NC:       case OP_ONCE_NC:
      case OP_COND:  
8170       d = find_firstassertedchar(scode, &dflags, op == OP_ASSERT);       d = find_firstassertedchar(scode, &dflags, op == OP_ASSERT);
8171       if (dflags < 0)       if (dflags < 0)
8172         return 0;         return 0;
# Line 7789  return c; Line 8211  return c;
8211    
8212    
8213  /*************************************************  /*************************************************
8214    *     Add an entry to the name/number table      *
8215    *************************************************/
8216    
8217    /* This function is called between compiling passes to add an entry to the
8218    name/number table, maintaining alphabetical order. Checking for permitted
8219    and forbidden duplicates has already been done.
8220    
8221    Arguments:
8222      cd           the compile data block
8223      name         the name to add
8224      length       the length of the name
8225      groupno      the group number
8226    
8227    Returns:       nothing
8228    */
8229    
8230    static void
8231    add_name(compile_data *cd, const pcre_uchar *name, int length,
8232      unsigned int groupno)
8233    {
8234    int i;
8235    pcre_uchar *slot = cd->name_table;
8236    
8237    for (i = 0; i < cd->names_found; i++)
8238      {
8239      int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(length));
8240      if (crc == 0 && slot[IMM2_SIZE+length] != 0)
8241        crc = -1; /* Current name is a substring */
8242    
8243      /* Make space in the table and break the loop for an earlier name. For a
8244      duplicate or later name, carry on. We do this for duplicates so that in the
8245      simple case (when ?(| is not used) they are in order of their numbers. In all
8246      cases they are in the order in which they appear in the pattern. */
8247    
8248      if (crc < 0)
8249        {
8250        memmove(slot + cd->name_entry_size, slot,
8251          IN_UCHARS((cd->names_found - i) * cd->name_entry_size));
8252        break;
8253        }
8254    
8255      /* Continue the loop for a later or duplicate name */
8256    
8257      slot += cd->name_entry_size;
8258      }
8259    
8260    PUT2(slot, 0, groupno);
8261    memcpy(slot + IMM2_SIZE, name, IN_UCHARS(length));
8262    slot[IMM2_SIZE + length] = 0;
8263    cd->names_found++;
8264    }
8265    
8266    
8267    
8268    /*************************************************
8269  *        Compile a Regular Expression            *  *        Compile a Regular Expression            *
8270  *************************************************/  *************************************************/
8271    
# Line 7875  new memory is obtained from malloc(). */ Line 8352  new memory is obtained from malloc(). */
8352    
8353  pcre_uchar cworkspace[COMPILE_WORK_SIZE];  pcre_uchar cworkspace[COMPILE_WORK_SIZE];
8354    
8355    /* This vector is used for remembering name groups during the pre-compile. In a
8356    similar way to cworkspace, it can be expanded using malloc() if necessary. */
8357    
8358    named_group named_groups[NAMED_GROUP_LIST_SIZE];
8359    
8360  /* Set this early so that early errors get offset 0. */  /* Set this early so that early errors get offset 0. */
8361    
8362  ptr = (const pcre_uchar *)pattern;  ptr = (const pcre_uchar *)pattern;
# Line 8137  cd->bracount = cd->final_bracount = 0; Line 8619  cd->bracount = cd->final_bracount = 0;
8619  cd->names_found = 0;  cd->names_found = 0;
8620  cd->name_entry_size = 0;  cd->name_entry_size = 0;
8621  cd->name_table = NULL;  cd->name_table = NULL;
8622    cd->dupnames = FALSE;
8623    cd->namedrefcount = 0;
8624  cd->start_code = cworkspace;  cd->start_code = cworkspace;
8625  cd->hwm = cworkspace;  cd->hwm = cworkspace;
8626  cd->start_workspace = cworkspace;  cd->start_workspace = cworkspace;
8627  cd->workspace_size = COMPILE_WORK_SIZE;  cd->workspace_size = COMPILE_WORK_SIZE;
8628    cd->named_groups = named_groups;
8629    cd->named_group_list_size = NAMED_GROUP_LIST_SIZE;
8630  cd->start_pattern = (const pcre_uchar *)pattern;  cd->start_pattern = (const pcre_uchar *)pattern;
8631  cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));  cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));
8632  cd->req_varyopt = 0;  cd->req_varyopt = 0;
# Line 8172  if (length > MAX_PATTERN_SIZE) Line 8658  if (length > MAX_PATTERN_SIZE)
8658    goto PCRE_EARLY_ERROR_RETURN;    goto PCRE_EARLY_ERROR_RETURN;
8659    }    }
8660    
8661  /* Compute the size of data block needed and get it, either from malloc or  /* If there are groups with duplicate names and there are also references by
8662  externally provided function. Integer overflow should no longer be possible  name, we must allow for the possibility of named references to duplicated
8663  because nowadays we limit the maximum value of cd->names_found and  groups. These require an extra data item each. */
 cd->name_entry_size. */  
8664    
8665  size = sizeof(REAL_PCRE) + (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar);  if (cd->dupnames && cd->namedrefcount > 0)
8666  re = (REAL_PCRE *)(PUBL(malloc))(size);    length += cd->namedrefcount * IMM2_SIZE * sizeof(pcre_uchar);
8667    
8668    /* Compute the size of the data block for storing the compiled pattern. Integer
8669    overflow should no longer be possible because nowadays we limit the maximum
8670    value of cd->names_found and cd->name_entry_size. */
8671    
8672    size = sizeof(REAL_PCRE) +
8673      (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar);
8674    
8675    /* Get the memory. */
8676    
8677    re = (REAL_PCRE *)(PUBL(malloc))(size);
8678  if (re == NULL)  if (re == NULL)
8679    {    {
8680    errorcode = ERR21;    errorcode = ERR21;
# Line 8223  cd->final_bracount = cd->bracount;  /* S Line 8718  cd->final_bracount = cd->bracount;  /* S
8718  cd->assert_depth = 0;  cd->assert_depth = 0;
8719  cd->bracount = 0;  cd->bracount = 0;
8720  cd->max_lookbehind = 0;  cd->max_lookbehind = 0;
 cd->names_found = 0;  
8721  cd->name_table = (pcre_uchar *)re + re->name_table_offset;  cd->name_table = (pcre_uchar *)re + re->name_table_offset;
8722  codestart = cd->name_table + re->name_entry_size * re->name_count;  codestart = cd->name_table + re->name_entry_size * re->name_count;
8723  cd->start_code = codestart;  cd->start_code = codestart;
# Line 8234  cd->had_pruneorskip = FALSE; Line 8728  cd->had_pruneorskip = FALSE;
8728  cd->check_lookbehind = FALSE;  cd->check_lookbehind = FALSE;
8729  cd->open_caps = NULL;  cd->open_caps = NULL;
8730    
8731    /* If any named groups were found, create the name/number table from the list
8732    created in the first pass. */
8733    
8734    if (cd->names_found > 0)
8735      {
8736      int i = cd->names_found;
8737      named_group *ng = cd->named_groups;
8738      cd->names_found = 0;
8739      for (; i > 0; i--, ng++)
8740        add_name(cd, ng->name, ng->length, ng->number);
8741      if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
8742        (PUBL(free))((void *)cd->named_groups);
8743      }
8744    
8745  /* Set up a starting, non-extracting bracket, then compile the expression. On  /* Set up a starting, non-extracting bracket, then compile the expression. On
8746  error, errorcode will be set non-zero, so we don't need to look at the result  error, errorcode will be set non-zero, so we don't need to look at the result
8747  of the function here. */  of the function here. */
# Line 8297  if (cd->hwm > cd->start_workspace) Line 8805  if (cd->hwm > cd->start_workspace)
8805      }      }
8806    }    }
8807    
8808  /* If the workspace had to be expanded, free the new memory. Set the pointer to  /* If the workspace had to be expanded, free the new memory. Set the pointer to
8809  NULL to indicate that forward references have been filled in. */  NULL to indicate that forward references have been filled in. */
8810    
8811  if (cd->workspace_size > COMPILE_WORK_SIZE)  if (cd->workspace_size > COMPILE_WORK_SIZE)
8812    (PUBL(free))((void *)cd->start_workspace);    (PUBL(free))((void *)cd->start_workspace);
8813  cd->start_workspace = NULL;  cd->start_workspace = NULL;
8814    
8815  /* Give an error if there's back reference to a non-existent capturing  /* Give an error if there's back reference to a non-existent capturing
8816  subpattern. */  subpattern. */
8817    
8818  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
8819    
8820    /* Unless disabled, check whether single character iterators can be
8821    auto-possessified. The function overwrites the appropriate opcode values. */
8822    
8823    if ((options & PCRE_NO_AUTO_POSSESSIFY) == 0)
8824      auto_possessify((pcre_uchar *)codestart, utf, cd);
8825    
8826  /* If there were any lookbehind assertions that contained OP_RECURSE  /* If there were any lookbehind assertions that contained OP_RECURSE
8827  (recursions or subroutine calls), a flag is set for them to be checked here,  (recursions or subroutine calls), a flag is set for them to be checked here,
8828  because they may contain forward references. Actual recursions cannot be fixed  because they may contain forward references. Actual recursions cannot be fixed
# Line 8506  if (code - codestart > length) Line 9020  if (code - codestart > length)
9020    }    }
9021  #endif   /* PCRE_DEBUG */  #endif   /* PCRE_DEBUG */
9022    
9023  /* Check for a pattern than can match an empty string, so that this information  /* Check for a pattern than can match an empty string, so that this information
9024  can be provided to applications. */  can be provided to applications. */
9025    
9026  do  do
# Line 8515  do Line 9029  do
9029      {      {
9030      re->flags |= PCRE_MATCH_EMPTY;      re->flags |= PCRE_MATCH_EMPTY;
9031      break;      break;
9032      }      }
9033    codestart += GET(codestart, 1);    codestart += GET(codestart, 1);
9034    }    }
9035  while (*codestart == OP_ALT);  while (*codestart == OP_ALT);

Legend:
Removed from v.1348  
changed lines
  Added in v.1379

  ViewVC Help
Powered by ViewVC 1.1.5