/[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 1266 by ph10, Sun Mar 3 11:14:26 2013 UTC revision 1364 by ph10, Sat Oct 5 15:45:11 2013 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2012 University of Cambridge             Copyright (c) 1997-2013 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 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 487  static const char error_texts[] = Line 494  static const char error_texts[] =
494    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
495    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
496    /* 60 */    /* 60 */
497    "(*VERB) not recognized\0"    "(*VERB) not recognized or malformed\0"
498    "number is too big\0"    "number is too big\0"
499    "subpattern name expected\0"    "subpattern name expected\0"
500    "digit expected after (?+\0"    "digit expected after (?+\0"
# Line 508  static const char error_texts[] = Line 515  static const char error_texts[] =
515    "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"    "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
516    "character value in \\u.... sequence is too large\0"    "character value in \\u.... sequence is too large\0"
517    "invalid UTF-32 string\0"    "invalid UTF-32 string\0"
518      "setting UTF is disabled by the application\0"
519    ;    ;
520    
521  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 647  static const pcre_uint8 ebcdic_chartab[] Line 655  static const pcre_uint8 ebcdic_chartab[]
655  #endif  #endif
656    
657    
658    /* This table is used to check whether auto-possessification is possible
659    between adjacent character-type opcodes. The left-hand (repeated) opcode is
660    used to select the row, and the right-hand opcode is use to select the column.
661    A value of 1 means that auto-possessification is OK. For example, the second
662    value in the first row means that \D+\d can be turned into \D++\d.
663    
664    The Unicode property types (\P and \p) have to be present to fill out the table
665    because of what their opcode values are, but the table values should always be
666    zero because property types are handled separately in the code. The last four
667    columns apply to items that cannot be repeated, so there is no need to have
668    rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
669    *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
670    
671    #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
672    #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
673    
674    static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
675    /* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
676      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
677      { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
678      { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
679      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
680      { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
681      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
682      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
683      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
684      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
685      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
686      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
687      { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
688      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
689      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
690      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
691      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
692      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
693    };
694    
695    
696    /* This table is used to check whether auto-possessification is possible
697    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
698    left-hand (repeated) opcode is used to select the row, and the right-hand
699    opcode is used to select the column. The values are as follows:
700    
701      0   Always return FALSE (never auto-possessify)
702      1   Character groups are distinct (possessify if both are OP_PROP)
703      2   Check character categories in the same group (general or particular)
704      3   TRUE if the two opcodes are not the same (PROP vs NOTPROP)
705    
706      4   Check left general category vs right particular category
707      5   Check right general category vs left particular category
708    
709      6   Left alphanum vs right general category
710      7   Left space vs right general category
711      8   Left word vs right general category
712    
713      9   Right alphanum vs left general category
714     10   Right space vs left general category
715     11   Right word vs left general category
716    
717     12   Left alphanum vs right particular category
718     13   Left space vs right particular category
719     14   Left word vs right particular category
720    
721     15   Right alphanum vs left particular category
722     16   Right space vs left particular category
723     17   Right word vs left particular category
724    */
725    
726    static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
727    /* ANY LAMP GC  PC  SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
728      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_ANY */
729      { 0,  3,  0,  0,  0,    3,    1,      1,   0,    0,   0 },  /* PT_LAMP */
730      { 0,  0,  2,  4,  0,    9,   10,     10,  11,    0,   0 },  /* PT_GC */
731      { 0,  0,  5,  2,  0,   15,   16,     16,  17,    0,   0 },  /* PT_PC */
732      { 0,  0,  0,  0,  2,    0,    0,      0,   0,    0,   0 },  /* PT_SC */
733      { 0,  3,  6, 12,  0,    3,    1,      1,   0,    0,   0 },  /* PT_ALNUM */
734      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_SPACE */
735      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_PXSPACE */
736      { 0,  0,  8, 14,  0,    0,    1,      1,   3,    0,   0 },  /* PT_WORD */
737      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_CLIST */
738      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   3 }   /* PT_UCNC */
739    };
740    
741    /* This table is used to check whether auto-possessification is possible
742    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
743    specifies a general category and the other specifies a particular category. The
744    row is selected by the general category and the column by the particular
745    category. The value is 1 if the particular category is not part of the general
746    category. */
747    
748    static const pcre_uint8 catposstab[7][30] = {
749    /* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
750      { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* C */
751      { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* L */
752      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* M */
753      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* N */
754      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },  /* P */
755      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 },  /* S */
756      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 }   /* Z */
757    };
758    
759    /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
760    a general or particular category. The properties in each row are those
761    that apply to the character set in question. Duplication means that a little
762    unnecessary work is done when checking, but this keeps things much simpler
763    because they can all use the same code. For more details see the comment where
764    this table is used.
765    
766    Note: SPACE and PXSPACE used to be different because Perl excluded VT from
767    "space", but from Perl 5.18 it's included, so both categories are treated the
768    same here. */
769    
770    static const pcre_uint8 posspropstab[3][4] = {
771      { ucp_L, ucp_N, ucp_N, ucp_Nl },  /* ALNUM, 3rd and 4th values redundant */
772      { ucp_Z, ucp_Z, ucp_C, ucp_Cc },  /* SPACE and PXSPACE, 2nd value redundant */
773      { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
774    };
775    
776    
777    
778  /*************************************************  /*************************************************
779  *            Find an error text                  *  *            Find an error text                  *
# Line 674  return s; Line 801  return s;
801  }  }
802    
803    
804    
805  /*************************************************  /*************************************************
806  *           Expand the workspace                 *  *           Expand the workspace                 *
807  *************************************************/  *************************************************/
# Line 797  Otherwise further processing may be requ Line 925  Otherwise further processing may be requ
925  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
926  /* Not alphanumeric */  /* Not alphanumeric */
927  else if (c < CHAR_0 || c > CHAR_z) {}  else if (c < CHAR_0 || c > CHAR_z) {}
928  else if ((i = escapes[c - CHAR_0]) != 0)  else if ((i = escapes[c - CHAR_0]) != 0)
929    { if (i > 0) c = (pcre_uint32)i; else escape = -i; }    { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
930    
931  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
# Line 1191  if ((options & PCRE_UCP) != 0 && escape Line 1319  if ((options & PCRE_UCP) != 0 && escape
1319  return escape;  return escape;
1320  }  }
1321    
1322    
1323    
1324  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
1325  /*************************************************  /*************************************************
1326  *               Handle \P and \p                 *  *               Handle \P and \p                 *
# Line 1288  return FALSE; Line 1418  return FALSE;
1418    
1419    
1420    
   
1421  /*************************************************  /*************************************************
1422  *         Read repeat counts                     *  *         Read repeat counts                     *
1423  *************************************************/  *************************************************/
# Line 1357  return p; Line 1486  return p;
1486    
1487    
1488  /*************************************************  /*************************************************
 *  Subroutine for finding forward reference      *  
 *************************************************/  
   
 /* This recursive function is called only from find_parens() below. The  
 top-level call starts at the beginning of the pattern. All other calls must  
 start at a parenthesis. It scans along a pattern's text looking for capturing  
 subpatterns, and counting them. If it finds a named pattern that matches the  
 name it is given, it returns its number. Alternatively, if the name is NULL, it  
 returns when it reaches a given numbered subpattern. Recursion is used to keep  
 track of subpatterns that reset the capturing group numbers - the (?| feature.  
   
 This function was originally called only from the second pass, in which we know  
 that if (?< or (?' or (?P< is encountered, the name will be correctly  
 terminated because that is checked in the first pass. There is now one call to  
 this function in the first pass, to check for a recursive back reference by  
 name (so that we can make the whole group atomic). In this case, we need check  
 only up to the current position in the pattern, and that is still OK because  
 and previous occurrences will have been checked. To make this work, the test  
 for "end of pattern" is a check against cd->end_pattern in the main loop,  
 instead of looking for a binary zero. This means that the special first-pass  
 call can adjust cd->end_pattern temporarily. (Checks for binary zero while  
 processing items within the loop are OK, because afterwards the main loop will  
 terminate.)  
   
 Arguments:  
   ptrptr       address of the current character pointer (updated)  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf          TRUE if we are in UTF-8 / UTF-16 / UTF-32 mode  
   count        pointer to the current capturing subpattern number (updated)  
   
 Returns:       the number of the named subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,  
   BOOL xmode, BOOL utf, int *count)  
 {  
 pcre_uchar *ptr = *ptrptr;  
 int start_count = *count;  
 int hwm_count = start_count;  
 BOOL dup_parens = FALSE;  
   
 /* If the first character is a parenthesis, check on the type of group we are  
 dealing with. The very first call may not start with a parenthesis. */  
   
 if (ptr[0] == CHAR_LEFT_PARENTHESIS)  
   {  
   /* Handle specials such as (*SKIP) or (*UTF8) etc. */  
   
   if (ptr[1] == CHAR_ASTERISK) ptr += 2;  
   
   /* Handle a normal, unnamed capturing parenthesis. */  
   
   else if (ptr[1] != CHAR_QUESTION_MARK)  
     {  
     *count += 1;  
     if (name == NULL && *count == lorn) return *count;  
     ptr++;  
     }  
   
   /* All cases now have (? at the start. Remember when we are in a group  
   where the parenthesis numbers are duplicated. */  
   
   else if (ptr[2] == CHAR_VERTICAL_LINE)  
     {  
     ptr += 3;  
     dup_parens = TRUE;  
     }  
   
   /* Handle comments; all characters are allowed until a ket is reached. */  
   
   else if (ptr[2] == CHAR_NUMBER_SIGN)  
     {  
     for (ptr += 3; *ptr != 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;  
 }  
   
   
   
   
 /*************************************************  
1489  *      Find first significant op code            *  *      Find first significant op code            *
1490  *************************************************/  *************************************************/
1491    
# Line 1707  for (;;) Line 1540  for (;;)
1540    
1541    
1542    
   
1543  /*************************************************  /*************************************************
1544  *        Find the fixed length of a branch       *  *        Find the fixed length of a branch       *
1545  *************************************************/  *************************************************/
# Line 2010  for (;;) Line 1842  for (;;)
1842      case OP_QUERYI:      case OP_QUERYI:
1843      case OP_REF:      case OP_REF:
1844      case OP_REFI:      case OP_REFI:
1845        case OP_DNREF:
1846        case OP_DNREFI:
1847      case OP_SBRA:      case OP_SBRA:
1848      case OP_SBRAPOS:      case OP_SBRAPOS:
1849      case OP_SCBRA:      case OP_SCBRA:
# Line 2046  for (;;) Line 1880  for (;;)
1880    
1881    
1882    
   
1883  /*************************************************  /*************************************************
1884  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
1885  *************************************************/  *************************************************/
# Line 2130  for (;;) Line 1963  for (;;)
1963        case OP_MARK:        case OP_MARK:
1964        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1965        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
1966        case OP_THEN_ARG:        case OP_THEN_ARG:
1967        code += code[1];        code += code[1];
1968        break;        break;
# Line 2250  for (;;) Line 2080  for (;;)
2080        case OP_MARK:        case OP_MARK:
2081        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
2082        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       code += code[1];  
       break;  
   
2083        case OP_THEN_ARG:        case OP_THEN_ARG:
2084        code += code[1];        code += code[1];
2085        break;        break;
# Line 2354  Arguments: Line 2181  Arguments:
2181    endcode     points to where to stop    endcode     points to where to stop
2182    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2183    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2184      recurses    chain of recurse_check to catch mutual recursion
2185    
2186  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2187  */  */
2188    
2189    typedef struct recurse_check {
2190      struct recurse_check *prev;
2191      const pcre_uchar *group;
2192    } recurse_check;
2193    
2194  static BOOL  static BOOL
2195  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2196    BOOL utf, compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2197  {  {
2198  register pcre_uchar c;  register pcre_uchar c;
2199    recurse_check this_recurse;
2200    
2201  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2202       code < endcode;       code < endcode;
2203       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
# Line 2390  for (code = first_significant_code(code Line 2225  for (code = first_significant_code(code
2225    
2226    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2227      {      {
2228      const pcre_uchar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2229      BOOL empty_branch;      BOOL empty_branch;
2230    
2231      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2232        when called to scan a completed pattern by setting cd->start_workspace to
2233        NULL. */
2234    
2235        if (cd->start_workspace != NULL)
2236          {
2237          const pcre_uchar *tcode;
2238          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2239            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2240          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2241          }
2242    
2243        /* If we are scanning a completed pattern, there are no forward references
2244        and all groups are complete. We need to detect whether this is a recursive
2245        call, as otherwise there will be an infinite loop. If it is a recursion,
2246        just skip over it. Simple recursions are easily detected. For mutual
2247        recursions we keep a chain on the stack. */
2248    
2249        else
2250          {
2251          recurse_check *r = recurses;
2252          const pcre_uchar *endgroup = scode;
2253    
2254          do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2255          if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2256    
2257      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)        for (r = recurses; r != NULL; r = r->prev)
2258        if ((int)GET(scode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;          if (r->group == scode) break;
2259          if (r != NULL) continue;   /* Mutual recursion */
2260          }
2261    
2262      /* Not a forward reference, test for completed backward reference */      /* Completed reference; scan the referenced group, remembering it on the
2263        stack chain to detect mutual recursions. */
2264    
2265      empty_branch = FALSE;      empty_branch = FALSE;
2266      scode = cd->start_code + GET(code, 1);      this_recurse.prev = recurses;
2267      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      this_recurse.group = scode;
   
     /* Completed backwards reference */  
2268    
2269      do      do
2270        {        {
2271        if (could_be_empty_branch(scode, endcode, utf, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2272          {          {
2273          empty_branch = TRUE;          empty_branch = TRUE;
2274          break;          break;
# Line 2464  for (code = first_significant_code(code Line 2324  for (code = first_significant_code(code
2324        empty_branch = FALSE;        empty_branch = FALSE;
2325        do        do
2326          {          {
2327          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2328            empty_branch = TRUE;            empty_branch = TRUE;
2329          code += GET(code, 1);          code += GET(code, 1);
2330          }          }
# Line 2522  for (code = first_significant_code(code Line 2382  for (code = first_significant_code(code
2382    
2383      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2384    
2385        case OP_ANY:
2386        case OP_ALLANY:
2387        case OP_ANYBYTE:
2388    
2389      case OP_PROP:      case OP_PROP:
2390      case OP_NOTPROP:      case OP_NOTPROP:
2391        case OP_ANYNL:
2392    
2393        case OP_NOT_HSPACE:
2394        case OP_HSPACE:
2395        case OP_NOT_VSPACE:
2396        case OP_VSPACE:
2397      case OP_EXTUNI:      case OP_EXTUNI:
2398    
2399      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2400      case OP_DIGIT:      case OP_DIGIT:
2401      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2402      case OP_WHITESPACE:      case OP_WHITESPACE:
2403      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2404      case OP_WORDCHAR:      case OP_WORDCHAR:
2405      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2406      case OP_CHAR:      case OP_CHAR:
2407      case OP_CHARI:      case OP_CHARI:
2408      case OP_NOT:      case OP_NOT:
2409      case OP_NOTI:      case OP_NOTI:
2410    
2411      case OP_PLUS:      case OP_PLUS:
2412        case OP_PLUSI:
2413      case OP_MINPLUS:      case OP_MINPLUS:
2414      case OP_POSPLUS:      case OP_MINPLUSI:
2415      case OP_EXACT:  
2416      case OP_NOTPLUS:      case OP_NOTPLUS:
2417        case OP_NOTPLUSI:
2418      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2419        case OP_NOTMINPLUSI:
2420    
2421        case OP_POSPLUS:
2422        case OP_POSPLUSI:
2423      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2424        case OP_NOTPOSPLUSI:
2425    
2426        case OP_EXACT:
2427        case OP_EXACTI:
2428      case OP_NOTEXACT:      case OP_NOTEXACT:
2429        case OP_NOTEXACTI:
2430    
2431      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2432      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2433      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2434      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2435    
2436      return FALSE;      return FALSE;
2437    
2438      /* These are going to continue, as they may be empty, but we have to      /* These are going to continue, as they may be empty, but we have to
# Line 2583  for (code = first_significant_code(code Line 2466  for (code = first_significant_code(code
2466      return TRUE;      return TRUE;
2467    
2468      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2469      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2470        followed by a multibyte character. */
2471    
2472  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2473      case OP_STAR:      case OP_STAR:
2474      case OP_STARI:      case OP_STARI:
2475        case OP_NOTSTAR:
2476        case OP_NOTSTARI:
2477    
2478      case OP_MINSTAR:      case OP_MINSTAR:
2479      case OP_MINSTARI:      case OP_MINSTARI:
2480        case OP_NOTMINSTAR:
2481        case OP_NOTMINSTARI:
2482    
2483      case OP_POSSTAR:      case OP_POSSTAR:
2484      case OP_POSSTARI:      case OP_POSSTARI:
2485        case OP_NOTPOSSTAR:
2486        case OP_NOTPOSSTARI:
2487    
2488      case OP_QUERY:      case OP_QUERY:
2489      case OP_QUERYI:      case OP_QUERYI:
2490        case OP_NOTQUERY:
2491        case OP_NOTQUERYI:
2492    
2493      case OP_MINQUERY:      case OP_MINQUERY:
2494      case OP_MINQUERYI:      case OP_MINQUERYI:
2495        case OP_NOTMINQUERY:
2496        case OP_NOTMINQUERYI:
2497    
2498      case OP_POSQUERY:      case OP_POSQUERY:
2499      case OP_POSQUERYI:      case OP_POSQUERYI:
2500        case OP_NOTPOSQUERY:
2501        case OP_NOTPOSQUERYI:
2502    
2503      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2504      break;      break;
2505    
2506      case OP_UPTO:      case OP_UPTO:
2507      case OP_UPTOI:      case OP_UPTOI:
2508        case OP_NOTUPTO:
2509        case OP_NOTUPTOI:
2510    
2511      case OP_MINUPTO:      case OP_MINUPTO:
2512      case OP_MINUPTOI:      case OP_MINUPTOI:
2513        case OP_NOTMINUPTO:
2514        case OP_NOTMINUPTOI:
2515    
2516      case OP_POSUPTO:      case OP_POSUPTO:
2517      case OP_POSUPTOI:      case OP_POSUPTOI:
2518        case OP_NOTPOSUPTO:
2519        case OP_NOTPOSUPTOI:
2520    
2521      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2522      break;      break;
2523  #endif  #endif
# Line 2617  for (code = first_significant_code(code Line 2528  for (code = first_significant_code(code
2528      case OP_MARK:      case OP_MARK:
2529      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2530      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     code += code[1];  
     break;  
   
2531      case OP_THEN_ARG:      case OP_THEN_ARG:
2532      code += code[1];      code += code[1];
2533      break;      break;
# Line 2663  could_be_empty(const pcre_uchar *code, c Line 2571  could_be_empty(const pcre_uchar *code, c
2571  {  {
2572  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2573    {    {
2574    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2575      return FALSE;      return FALSE;
2576    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2577    }    }
# Line 2673  return TRUE; Line 2581  return TRUE;
2581    
2582    
2583  /*************************************************  /*************************************************
2584  *           Check for POSIX class syntax         *  *        Base opcode of repeated opcodes         *
2585  *************************************************/  *************************************************/
2586    
2587  /* This function is called when the sequence "[:" or "[." or "[=" is  /* Returns the base opcode for repeated single character type opcodes. If the
2588  encountered in a character class. It checks whether this is followed by a  opcode is not a repeated character type, it returns with the original value.
 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we  
 reach an unescaped ']' without the special preceding character, return FALSE.  
2589    
2590  Originally, this function only recognized a sequence of letters between the  Arguments:  c opcode
2591  terminators, but it seems that Perl recognizes any sequence of characters,  Returns:    base opcode for the type
2592  though of course unknown POSIX names are subsequently rejected. Perl gives an  */
 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE  
 didn't consider this to be a POSIX class. Likewise for [:1234:].  
2593    
2594  The problem in trying to be exactly like Perl is in the handling of escapes. We  static pcre_uchar
2595  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX  get_repeat_base(pcre_uchar c)
2596  class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code  {
2597  below handles the special case of \], but does not try to do any other escape  return (c > OP_TYPEPOSUPTO)? c :
2598  processing. This makes it different from Perl for cases such as [:l\ower:]         (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2599  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize         (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2600  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,         (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2601  I think.         (c >= OP_STARI)?      OP_STARI :
2602                                 OP_STAR;
2603    }
2604    
 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.  
 It seems that the appearance of a nested POSIX class supersedes an apparent  
 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or  
 a digit.  
2605    
2606  In Perl, unescaped square brackets may also appear as part of class names. For  
2607  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for  #ifdef SUPPORT_UCP
2608  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not  /*************************************************
2609  seem right at all. PCRE does not allow closing square brackets in POSIX class  *        Check a character and a property        *
2610  names.  *************************************************/
2611    
2612    /* This function is called by check_auto_possessive() when a property item
2613    is adjacent to a fixed character.
2614    
2615  Arguments:  Arguments:
2616    ptr      pointer to the initial [    c            the character
2617    endptr   where to return the end pointer    ptype        the property type
2618      pdata        the data for the type
2619      negated      TRUE if it's a negated property (\P or \p{^)
2620    
2621  Returns:   TRUE or FALSE  Returns:       TRUE if auto-possessifying is OK
2622  */  */
2623    
2624  static BOOL  static BOOL
2625  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2626      BOOL negated)
2627  {  {
2628  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */  const pcre_uint32 *p;
2629  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  const ucd_record *prop = GET_UCD(c);
2630  for (++ptr; *ptr != CHAR_NULL; ptr++)  
2631    switch(ptype)
2632    {    {
2633    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)    case PT_LAMP:
2634      ptr++;    return (prop->chartype == ucp_Lu ||
2635    else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;            prop->chartype == ucp_Ll ||
2636    else            prop->chartype == ucp_Lt) == negated;
2637    
2638      case PT_GC:
2639      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2640    
2641      case PT_PC:
2642      return (pdata == prop->chartype) == negated;
2643    
2644      case PT_SC:
2645      return (pdata == prop->script) == negated;
2646    
2647      /* These are specials */
2648    
2649      case PT_ALNUM:
2650      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2651              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2652    
2653      /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2654      means that Perl space and POSIX space are now identical. PCRE was changed
2655      at release 8.34. */
2656    
2657      case PT_SPACE:    /* Perl space */
2658      case PT_PXSPACE:  /* POSIX space */
2659      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2660              c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2661              c == CHAR_FF || c == CHAR_CR)
2662              == negated;
2663    
2664      case PT_WORD:
2665      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2666              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2667              c == CHAR_UNDERSCORE) == negated;
2668    
2669      case PT_CLIST:
2670      p = PRIV(ucd_caseless_sets) + prop->caseset;
2671      for (;;)
2672      {      {
2673      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      if (c < *p) return !negated;
2674        {      if (c == *p++) return negated;
       *endptr = ptr;  
       return TRUE;  
       }  
     if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&  
          (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||  
           ptr[1] == CHAR_EQUALS_SIGN) &&  
         check_posix_syntax(ptr, endptr))  
       return FALSE;  
2675      }      }
2676      break;  /* Control never reaches here */
2677    }    }
2678    
2679  return FALSE;  return FALSE;
2680  }  }
2681    #endif  /* SUPPORT_UCP */
2682    
2683    
2684    
2685  /*************************************************  /*************************************************
2686  *          Check POSIX class name                *  *        Fill the character property list        *
2687  *************************************************/  *************************************************/
2688    
2689  /* This function is called to check the name given in a POSIX-style class entry  /* Checks whether the code points to an opcode that can take part in auto-
2690  such as [:alnum:].  possessification, and if so, fills a list with its properties.
2691    
2692  Arguments:  Arguments:
2693    ptr        points to the first letter    code        points to start of expression
2694    len        the length of the name    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2695      fcc         points to case-flipping table
2696      list        points to output list
2697                  list[0] will be filled with the opcode
2698                  list[1] will be non-zero if this opcode
2699                    can match an empty character string
2700                  list[2..7] depends on the opcode
2701    
2702  Returns:     a value representing the name, or -1 if unknown  Returns:      points to the start of the next opcode if *code is accepted
2703                  NULL if *code is not accepted
2704  */  */
2705    
2706  static int  static const pcre_uchar *
2707  check_posix_name(const pcre_uchar *ptr, int len)  get_chr_property_list(const pcre_uchar *code, BOOL utf,
2708      const pcre_uint8 *fcc, pcre_uint32 *list)
2709  {  {
2710  const char *pn = posix_names;  pcre_uchar c = *code;
2711  register int yield = 0;  const pcre_uchar *end;
2712  while (posix_name_lengths[yield] != 0)  const pcre_uint32 *clist_src;
2713    pcre_uint32 *clist_dest;
2714    pcre_uint32 chr;
2715    pcre_uchar base;
2716    
2717    list[0] = c;
2718    list[1] = FALSE;
2719    code++;
2720    
2721    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2722    {    {
2723    if (len == posix_name_lengths[yield] &&    base = get_repeat_base(c);
2724      STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;    c -= (base - OP_STAR);
   pn += posix_name_lengths[yield] + 1;  
   yield++;  
   }  
 return -1;  
 }  
2725    
2726      if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2727        code += IMM2_SIZE;
2728    
2729  /*************************************************    list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
 *    Adjust OP_RECURSE items in repeated group   *  
 *************************************************/  
2730    
2731  /* OP_RECURSE items contain an offset from the start of the regex to the group    switch(base)
2732  that is referenced. This means that groups can be replicated for fixed      {
2733  repetition simply by copying (because the recursion is allowed to refer to      case OP_STAR:
2734  earlier groups that are outside the current group). However, when a group is      list[0] = OP_CHAR;
2735  optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is      break;
 inserted before it, after it has been compiled. This means that any OP_RECURSE  
 items within it that refer to the group itself or any contained groups have to  
 have their offsets adjusted. That one of the jobs of this function. Before it  
 is called, the partially compiled regex must be temporarily terminated with  
 OP_END.  
2736    
2737  This function has been extended with the possibility of forward references for      case OP_STARI:
2738  recursions and subroutine calls. It must also check the list of such references      list[0] = OP_CHARI;
2739  for the group we are dealing with. If it finds that one of the recursions in      break;
 the current group is on this list, it adjusts the offset in the list, not the  
 value in the reference (which is a group number).  
2740    
2741  Arguments:      case OP_NOTSTAR:
2742    group      points to the start of the group      list[0] = OP_NOT;
2743    adjust     the amount by which the group is to be moved      break;
   utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode  
   cd         contains pointers to tables etc.  
   save_hwm   the hwm forward reference pointer at the start of the group  
2744    
2745  Returns:     nothing      case OP_NOTSTARI:
2746  */      list[0] = OP_NOTI;
2747        break;
2748    
2749  static void      case OP_TYPESTAR:
2750  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,      list[0] = *code;
2751    pcre_uchar *save_hwm)      code++;
2752  {      break;
2753  pcre_uchar *ptr = group;      }
2754      c = list[0];
2755      }
2756    
2757  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)  switch(c)
2758    {    {
2759    int offset;    case OP_NOT_DIGIT:
2760    pcre_uchar *hc;    case OP_DIGIT:
2761      case OP_NOT_WHITESPACE:
2762      case OP_WHITESPACE:
2763      case OP_NOT_WORDCHAR:
2764      case OP_WORDCHAR:
2765      case OP_ANY:
2766      case OP_ALLANY:
2767      case OP_ANYNL:
2768      case OP_NOT_HSPACE:
2769      case OP_HSPACE:
2770      case OP_NOT_VSPACE:
2771      case OP_VSPACE:
2772      case OP_EXTUNI:
2773      case OP_EODN:
2774      case OP_EOD:
2775      case OP_DOLL:
2776      case OP_DOLLM:
2777      return code;
2778    
2779    /* See if this recursion is on the forward reference list. If so, adjust the    case OP_CHAR:
2780    reference. */    case OP_NOT:
2781      GETCHARINCTEST(chr, code);
2782      list[2] = chr;
2783      list[3] = NOTACHAR;
2784      return code;
2785    
2786    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    case OP_CHARI:
2787      {    case OP_NOTI:
2788      offset = (int)GET(hc, 0);    list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2789      if (cd->start_code + offset == ptr + 1)    GETCHARINCTEST(chr, code);
2790        {    list[2] = chr;
       PUT(hc, 0, offset + adjust);  
       break;  
       }  
     }  
2791    
2792    /* Otherwise, adjust the recursion offset if it's after the start of this  #ifdef SUPPORT_UCP
2793    group. */    if (chr < 128 || (chr < 256 && !utf))
2794        list[3] = fcc[chr];
2795      else
2796        list[3] = UCD_OTHERCASE(chr);
2797    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2798      list[3] = (chr < 256) ? fcc[chr] : chr;
2799    #else
2800      list[3] = fcc[chr];
2801    #endif
2802    
2803    if (hc >= cd->hwm)    /* The othercase might be the same value. */
2804    
2805      if (chr == list[3])
2806        list[3] = NOTACHAR;
2807      else
2808        list[4] = NOTACHAR;
2809      return code;
2810    
2811    #ifdef SUPPORT_UCP
2812      case OP_PROP:
2813      case OP_NOTPROP:
2814      if (code[0] != PT_CLIST)
2815      {      {
2816      offset = (int)GET(ptr, 1);      list[2] = code[0];
2817      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      list[3] = code[1];
2818        return code + 2;
2819      }      }
2820    
2821    ptr += 1 + LINK_SIZE;    /* Convert only if we have anough space. */
   }  
 }  
2822    
2823      clist_src = PRIV(ucd_caseless_sets) + code[1];
2824      clist_dest = list + 2;
2825      code += 2;
2826    
2827      do {
2828         /* Early return if there is not enough space. */
2829         if (clist_dest >= list + 8)
2830           {
2831           list[2] = code[0];
2832           list[3] = code[1];
2833           return code;
2834           }
2835         *clist_dest++ = *clist_src;
2836         }
2837       while(*clist_src++ != NOTACHAR);
2838    
2839      /* Enough space to store all characters. */
2840    
2841  /*************************************************    list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2842  *        Insert an automatic callout point       *    return code;
2843  *************************************************/  #endif
2844    
2845  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert    case OP_NCLASS:
2846  callout points before each pattern item.    case OP_CLASS:
2847    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2848      case OP_XCLASS:
2849    
2850  Arguments:    if (c == OP_XCLASS)
2851    code           current code pointer      end = code + GET(code, 0);
2852    ptr            current pattern pointer    else
2853    cd             pointers to tables etc  #endif
2854        end = code + 32 / sizeof(pcre_uchar);
2855    
2856  Returns:         new code pointer    switch(*end)
2857  */      {
2858        case OP_CRSTAR:
2859        case OP_CRMINSTAR:
2860        case OP_CRQUERY:
2861        case OP_CRMINQUERY:
2862        list[1] = TRUE;
2863        end++;
2864        break;
2865    
2866  static pcre_uchar *      case OP_CRRANGE:
2867  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)      case OP_CRMINRANGE:
2868  {      list[1] = (GET2(end, 1) == 0);
2869  *code++ = OP_CALLOUT;      end += 1 + 2 * IMM2_SIZE;
2870  *code++ = 255;      break;
2871  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */      }
2872  PUT(code, LINK_SIZE, 0);                       /* Default length */    list[2] = end - code;
2873  return code + 2 * LINK_SIZE;    return end;
2874      }
2875    return NULL;    /* Opcode not accepted */
2876  }  }
2877    
2878    
2879    
2880  /*************************************************  /*************************************************
2881  *         Complete a callout item                *  *    Scan further character sets for match       *
2882  *************************************************/  *************************************************/
2883    
2884  /* A callout item contains the length of the next item in the pattern, which  /* Checks whether the base and the current opcode have a common character, in
2885  we can't fill in till after we have reached the relevant point. This is used  which case the base cannot be possessified.
 for both automatic and manual callouts.  
2886    
2887  Arguments:  Arguments:
2888    previous_callout   points to previous callout item    code        points to the byte code
2889    ptr                current pattern pointer    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2890    cd                 pointers to tables etc    cd          static compile data
2891      base_list   the data list of the base opcode
2892    
2893  Returns:             nothing  Returns:      TRUE if the auto-possessification is possible
2894  */  */
2895    
2896  static void  static BOOL
2897  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)  compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2898      const pcre_uint32* base_list)
2899  {  {
2900  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  pcre_uchar c;
2901  PUT(previous_callout, 2 + LINK_SIZE, length);  pcre_uint32 list[8];
2902  }  const pcre_uint32* chr_ptr;
2903    const pcre_uint32* ochr_ptr;
2904    const pcre_uint32* list_ptr;
2905    pcre_uint32 chr;
2906    
2907    for(;;)
2908      {
2909      c = *code;
2910    
2911  #ifdef SUPPORT_UCP    /* Skip over callouts */
 /*************************************************  
 *           Get othercase range                  *  
 *************************************************/  
2912    
2913  /* This function is passed the start and end of a class range, in UTF-8 mode    if (c == OP_CALLOUT)
2914  with UCP support. It searches up the characters, looking for ranges of      {
2915  characters in the "other" case. Each call returns the next one, updating the      code += PRIV(OP_lengths)[c];
2916  start address. A character with multiple other cases is returned on its own      continue;
2917  with a special return value.      }
2918    
2919  Arguments:    if (c == OP_ALT)
2920    cptr        points to starting character value; updated      {
2921    d           end value      do code += GET(code, 1); while (*code == OP_ALT);
2922    ocptr       where to put start of othercase range      c = *code;
2923    odptr       where to put end of othercase range      }
2924    
2925  Yield:        -1 when no more    switch(c)
2926                 0 when a range is returned      {
2927                >0 the CASESET offset for char with multiple other cases      case OP_END:
2928                  in this case, ocptr contains the original      /* TRUE only in greedy case. The non-greedy case could be replaced by an
2929  */      OP_EXACT, but it is probably not worth it. (And note that OP_EXACT uses
2930        more memory, which we cannot get at this stage.) */
2931    
2932  static int      return base_list[1] != 0;
 get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,  
   pcre_uint32 *odptr)  
 {  
 pcre_uint32 c, othercase, next;  
 unsigned int co;  
2933    
2934  /* Find the first character that has an other case. If it has multiple other      case OP_KET:
2935  cases, return its case offset value. */      /* If the bracket is capturing, and referenced by an OP_RECURSE, the
2936        non-greedy case cannot be converted to a possessive form. We do not test
2937        the bracket type at the moment, but we might do it in the future to improve
2938        this condition. (But note that recursive calls are always atomic.) */
2939    
2940  for (c = *cptr; c <= d; c++)      if (base_list[1] == 0) return FALSE;
2941    {      code += PRIV(OP_lengths)[c];
2942    if ((co = UCD_CASESET(c)) != 0)      continue;
2943        }
2944    
2945      /* Check for a supported opcode, and load its properties. */
2946    
2947      code = get_chr_property_list(code, utf, cd->fcc, list);
2948      if (code == NULL) return FALSE;    /* Unsupported */
2949    
2950      /* If either opcode is a small character list, set pointers for comparing
2951      characters from that list with another list, or with a property. */
2952    
2953      if (base_list[0] == OP_CHAR)
2954      {      {
2955      *ocptr = c++;   /* Character that has the set */      chr_ptr = base_list + 2;
2956      *cptr = c;      /* Rest of input range */      list_ptr = list;
2957      return (int)co;      }
2958      else if (list[0] == OP_CHAR)
2959        {
2960        chr_ptr = list + 2;
2961        list_ptr = base_list;
2962      }      }
   if ((othercase = UCD_OTHERCASE(c)) != c) break;  
   }  
2963    
2964  if (c > d) return -1;  /* Reached end of range */    /* Some property combinations also acceptable. Unicode property opcodes are
2965      processed specially; the rest can be handled with a lookup table. */
2966    
2967  *ocptr = othercase;    else
2968  next = othercase + 1;      {
2969        pcre_uint32 leftop, rightop;
2970    
2971  for (++c; c <= d; c++)      if (list[1] != 0) return FALSE;   /* Must match at least one character */
2972    {      leftop = base_list[0];
2973    if (UCD_OTHERCASE(c) != next) break;      rightop = list[0];
   next++;  
   }  
2974    
2975  *odptr = next - 1;     /* End of othercase range */  #ifdef SUPPORT_UCP
2976  *cptr = c;             /* Rest of input range */      if (leftop == OP_PROP || leftop == OP_NOTPROP)
2977  return 0;        {
2978  }        if (rightop == OP_EOD) return TRUE;
2979          if (rightop == OP_PROP || rightop == OP_NOTPROP)
2980            {
2981            int n;
2982            const pcre_uint8 *p;
2983            BOOL same = leftop == rightop;
2984            BOOL lisprop = leftop == OP_PROP;
2985            BOOL risprop = rightop == OP_PROP;
2986            BOOL bothprop = lisprop && risprop;
2987    
2988            /* There's a table that specifies how each combination is to be
2989            processed:
2990              0   Always return FALSE (never auto-possessify)
2991              1   Character groups are distinct (possessify if both are OP_PROP)
2992              2   Check character categories in the same group (general or particular)
2993              3   Return TRUE if the two opcodes are not the same
2994              ... see comments below
2995            */
2996    
2997            n = propposstab[base_list[2]][list[2]];
2998            switch(n)
2999              {
3000              case 0: return FALSE;
3001              case 1: return bothprop;
3002              case 2: return (base_list[3] == list[3]) != same;
3003              case 3: return !same;
3004    
3005              case 4:  /* Left general category, right particular category */
3006              return risprop && catposstab[base_list[3]][list[3]] == same;
3007    
3008              case 5:  /* Right general category, left particular category */
3009              return lisprop && catposstab[list[3]][base_list[3]] == same;
3010    
3011              /* This code is logically tricky. Think hard before fiddling with it.
3012              The posspropstab table has four entries per row. Each row relates to
3013              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3014              Only WORD actually needs all four entries, but using repeats for the
3015              others means they can all use the same code below.
3016    
3017              The first two entries in each row are Unicode general categories, and
3018              apply always, because all the characters they include are part of the
3019              PCRE character set. The third and fourth entries are a general and a
3020              particular category, respectively, that include one or more relevant
3021              characters. One or the other is used, depending on whether the check
3022              is for a general or a particular category. However, in both cases the
3023              category contains more characters than the specials that are defined
3024              for the property being tested against. Therefore, it cannot be used
3025              in a NOTPROP case.
3026    
3027              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3028              Underscore is covered by ucp_P or ucp_Po. */
3029    
3030              case 6:  /* Left alphanum vs right general category */
3031              case 7:  /* Left space vs right general category */
3032              case 8:  /* Left word vs right general category */
3033              p = posspropstab[n-6];
3034              return risprop && lisprop ==
3035                (list[3] != p[0] &&
3036                 list[3] != p[1] &&
3037                (list[3] != p[2] || !lisprop));
3038    
3039              case 9:   /* Right alphanum vs left general category */
3040              case 10:  /* Right space vs left general category */
3041              case 11:  /* Right word vs left general category */
3042              p = posspropstab[n-9];
3043              return lisprop && risprop ==
3044                (base_list[3] != p[0] &&
3045                 base_list[3] != p[1] &&
3046                (base_list[3] != p[2] || !risprop));
3047    
3048              case 12:  /* Left alphanum vs right particular category */
3049              case 13:  /* Left space vs right particular category */
3050              case 14:  /* Left word vs right particular category */
3051              p = posspropstab[n-12];
3052              return risprop && lisprop ==
3053                (catposstab[p[0]][list[3]] &&
3054                 catposstab[p[1]][list[3]] &&
3055                (list[3] != p[3] || !lisprop));
3056    
3057              case 15:  /* Right alphanum vs left particular category */
3058              case 16:  /* Right space vs left particular category */
3059              case 17:  /* Right word vs left particular category */
3060              p = posspropstab[n-15];
3061              return lisprop && risprop ==
3062                (catposstab[p[0]][base_list[3]] &&
3063                 catposstab[p[1]][base_list[3]] &&
3064                (base_list[3] != p[3] || !risprop));
3065              }
3066            }
3067          return FALSE;
3068          }
3069    
3070        else
3071    #endif  /* SUPPORT_UCP */
3072    
3073        return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3074               rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3075               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3076        }
3077    
3078  /*************************************************    /* Control reaches here only if one of the items is a small character list.
3079  *        Check a character and a property        *    All characters are checked against the other side. */
 *************************************************/  
3080    
3081  /* This function is called by check_auto_possessive() when a property item    do
3082  is adjacent to a fixed character.      {
3083        chr = *chr_ptr;
3084    
3085  Arguments:      switch(list_ptr[0])
3086    c            the character        {
3087    ptype        the property type        case OP_CHAR:
3088    pdata        the data for the type        ochr_ptr = list_ptr + 2;
3089    negated      TRUE if it's a negated property (\P or \p{^)        do
3090            {
3091            if (chr == *ochr_ptr) return FALSE;
3092            ochr_ptr++;
3093            }
3094          while(*ochr_ptr != NOTACHAR);
3095          break;
3096    
3097  Returns:       TRUE if auto-possessifying is OK        case OP_NOT:
3098  */        ochr_ptr = list_ptr + 2;
3099          do
3100            {
3101            if (chr == *ochr_ptr)
3102              break;
3103            ochr_ptr++;
3104            }
3105          while(*ochr_ptr != NOTACHAR);
3106          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3107          break;
3108    
3109  static BOOL        /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3110  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  
3111    
3112  const ucd_record *prop = GET_UCD(c);        case OP_DIGIT:
3113          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3114          break;
3115    
3116  switch(ptype)        case OP_NOT_DIGIT:
3117    {        if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3118    case PT_LAMP:        break;
   return (prop->chartype == ucp_Lu ||  
           prop->chartype == ucp_Ll ||  
           prop->chartype == ucp_Lt) == negated;  
3119    
3120    case PT_GC:        case OP_WHITESPACE:
3121    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;        if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3122          break;
3123    
3124    case PT_PC:        case OP_NOT_WHITESPACE:
3125    return (pdata == prop->chartype) == negated;        if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3126          break;
3127    
3128    case PT_SC:        case OP_WORDCHAR:
3129    return (pdata == prop->script) == negated;        if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3130          break;
3131    
3132    /* These are specials */        case OP_NOT_WORDCHAR:
3133          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3134          break;
3135    
3136    case PT_ALNUM:        case OP_HSPACE:
3137    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||        switch(chr)
3138            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;          {
3139            HSPACE_CASES: return FALSE;
3140            default: break;
3141            }
3142          break;
3143    
3144    case PT_SPACE:    /* Perl space */        case OP_NOT_HSPACE:
3145    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||        switch(chr)
3146            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)          {
3147            == negated;          HSPACE_CASES: break;
3148            default: return FALSE;
3149            }
3150          break;
3151    
3152    case PT_PXSPACE:  /* POSIX space */        case OP_ANYNL:
3153    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||        case OP_VSPACE:
3154            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||        switch(chr)
3155            c == CHAR_FF || c == CHAR_CR)          {
3156            == negated;          VSPACE_CASES: return FALSE;
3157            default: break;
3158            }
3159          break;
3160    
3161    case PT_WORD:        case OP_NOT_VSPACE:
3162    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||        switch(chr)
3163            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||          {
3164            c == CHAR_UNDERSCORE) == negated;          VSPACE_CASES: break;
3165            default: return FALSE;
3166            }
3167          break;
3168    
3169  #ifdef SUPPORT_UCP        case OP_DOLL:
3170    case PT_CLIST:        case OP_EODN:
3171    p = PRIV(ucd_caseless_sets) + prop->caseset;        switch (chr)
3172    for (;;)          {
3173      {          case CHAR_CR:
3174      if (c < *p) return !negated;          case CHAR_LF:
3175      if (c == *p++) return negated;          case CHAR_VT:
3176      }          case CHAR_FF:
3177    break;  /* Control never reaches here */          case CHAR_NEL:
3178    #ifndef EBCDIC
3179            case 0x2028:
3180            case 0x2029:
3181    #endif  /* Not EBCDIC */
3182            return FALSE;
3183            }
3184          break;
3185    
3186          case OP_EOD:    /* Can always possessify before \z */
3187          break;
3188    
3189          case OP_PROP:
3190          case OP_NOTPROP:
3191          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3192                list_ptr[0] == OP_NOTPROP))
3193            return FALSE;
3194          break;
3195    
3196          /* The class comparisons work only when the class is the second item
3197          of the pair, because there are at present no possessive forms of the
3198          class opcodes. Note also that the "code" variable that is used below
3199          points after the second item, and that the pointer for the first item
3200          is not available, so even if there were possessive forms of the class
3201          opcodes, the correct comparison could not be done. */
3202    
3203          case OP_NCLASS:
3204          if (chr > 255) return FALSE;
3205          /* Fall through */
3206    
3207          case OP_CLASS:
3208          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3209          if (chr > 255) break;
3210          if ((((pcre_uint8 *)(code - list_ptr[2] + 1))[chr >> 3] & (1 << (chr & 7))) != 0)
3211            return FALSE;
3212          break;
3213    
3214    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3215          case OP_XCLASS:
3216          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3217          if (PRIV(xclass)(chr, code - list_ptr[2] + 1 + LINK_SIZE, utf))
3218            return FALSE;
3219          break;
3220  #endif  #endif
3221    
3222          default:
3223          return FALSE;
3224          }
3225    
3226        chr_ptr++;
3227        }
3228      while(*chr_ptr != NOTACHAR);
3229    
3230      /* At least one character must be matched from this opcode. */
3231    
3232      if (list[1] == 0) return TRUE;
3233    }    }
3234    
3235  return FALSE;  return FALSE;
3236  }  }
 #endif  /* SUPPORT_UCP */  
3237    
3238    
3239    
3240  /*************************************************  /*************************************************
3241  *     Check if auto-possessifying is possible    *  *    Scan compiled regex for auto-possession     *
3242  *************************************************/  *************************************************/
3243    
3244  /* This function is called for unlimited repeats of certain items, to see  /* Replaces single character iterations with their possessive alternatives
3245  whether the next thing could possibly match the repeated item. If not, it makes  if appropriate. This function modifies the compiled opcode!
 sense to automatically possessify the repeated item.  
3246    
3247  Arguments:  Arguments:
3248    previous      pointer to the repeated opcode    code        points to start of the byte code
3249    utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3250    ptr           next character in pattern    cd          static compile data
   options       options bits  
   cd            contains pointers to tables etc.  
3251    
3252  Returns:        TRUE if possessifying is wanted  Returns:      nothing
3253  */  */
3254    
3255  static BOOL  static void
3256  check_auto_possessive(const pcre_uchar *previous, BOOL utf,  auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
   const pcre_uchar *ptr, int options, compile_data *cd)  
3257  {  {
3258  pcre_uint32 c = NOTACHAR;  register pcre_uchar c;
3259  pcre_uint32 next;  const pcre_uchar *end;
3260  int escape;  pcre_uint32 list[8];
 pcre_uchar op_code = *previous++;  
   
 /* Skip whitespace and comments in extended mode */  
   
 if ((options & PCRE_EXTENDED) != 0)  
   {  
   for (;;)  
     {  
     while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;  
     if (*ptr == CHAR_NUMBER_SIGN)  
       {  
       ptr++;  
       while (*ptr != CHAR_NULL)  
         {  
         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }  
         ptr++;  
 #ifdef SUPPORT_UTF  
         if (utf) FORWARDCHAR(ptr);  
 #endif  
         }  
       }  
     else break;  
     }  
   }  
   
 /* If the next item is one that we can handle, get its value. A non-negative  
 value is a character, a negative value is an escape value. */  
3261    
3262  if (*ptr == CHAR_BACKSLASH)  for (;;)
   {  
   int temperrorcode = 0;  
   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)  
3263    {    {
3264    escape = 0;    c = *code;
 #ifdef SUPPORT_UTF  
   if (utf) { GETCHARINC(next, ptr); } else  
 #endif  
   next = *ptr++;  
   }  
 else return FALSE;  
   
 /* Skip whitespace and comments in extended mode */  
3265    
3266  if ((options & PCRE_EXTENDED) != 0)    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
   {  
   for (;;)  
3267      {      {
3268      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      c -= get_repeat_base(c) - OP_STAR;
3269      if (*ptr == CHAR_NUMBER_SIGN)      end = (c <= OP_MINUPTO) ?
3270          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3271        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3272    
3273        if (end != NULL && compare_opcodes(end, utf, cd, list))
3274        {        {
3275        ptr++;        switch(c)
       while (*ptr != CHAR_NULL)  
3276          {          {
3277          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          case OP_STAR:
3278          ptr++;          *code += OP_POSSTAR - OP_STAR;
3279  #ifdef SUPPORT_UTF          break;
         if (utf) FORWARDCHAR(ptr);  
 #endif  
         }  
       }  
     else break;  
     }  
   }  
3280    
3281  /* If the next thing is itself optional, we have to give up. */          case OP_MINSTAR:
3282            *code += OP_POSSTAR - OP_MINSTAR;
3283            break;
3284    
3285  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||          case OP_PLUS:
3286    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)          *code += OP_POSPLUS - OP_PLUS;
3287      return FALSE;          break;
3288    
3289  /* If the previous item is a character, get its value. */          case OP_MINPLUS:
3290            *code += OP_POSPLUS - OP_MINPLUS;
3291            break;
3292    
3293  if (op_code == OP_CHAR || op_code == OP_CHARI ||          case OP_QUERY:
3294      op_code == OP_NOT || op_code == OP_NOTI)          *code += OP_POSQUERY - OP_QUERY;
3295    {          break;
 #ifdef SUPPORT_UTF  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   }  
3296    
3297  /* Now compare the next item with the previous opcode. First, handle cases when          case OP_MINQUERY:
3298  the next item is a character. */          *code += OP_POSQUERY - OP_MINQUERY;
3299            break;
3300    
3301  if (escape == 0)          case OP_UPTO:
3302    {          *code += OP_POSUPTO - OP_UPTO;
3303    /* For a caseless UTF match, the next character may have more than one other          break;
   case, which maps to the special PT_CLIST property. Check this first. */  
3304    
3305  #ifdef SUPPORT_UCP          case OP_MINUPTO:
3306    if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)          *code += OP_MINUPTO - OP_UPTO;
3307      {          break;
3308      unsigned int ocs = UCD_CASESET(next);          }
3309      if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);        }
3310        c = *code;
3311      }      }
 #endif  
3312    
3313    switch(op_code)    switch(c)
3314      {      {
3315      case OP_CHAR:      case OP_END:
3316      return c != next;      return;
3317    
3318      /* For CHARI (caseless character) we must check the other case. If we have      case OP_TYPESTAR:
3319      Unicode property support, we can use it to test the other case of      case OP_TYPEMINSTAR:
3320      high-valued characters. We know that next can have only one other case,      case OP_TYPEPLUS:
3321      because multi-other-case characters are dealt with above. */      case OP_TYPEMINPLUS:
3322        case OP_TYPEQUERY:
3323        case OP_TYPEMINQUERY:
3324        case OP_TYPEPOSSTAR:
3325        case OP_TYPEPOSPLUS:
3326        case OP_TYPEPOSQUERY:
3327        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3328        break;
3329    
3330      case OP_CHARI:      case OP_TYPEUPTO:
3331      if (c == next) return FALSE;      case OP_TYPEMINUPTO:
3332  #ifdef SUPPORT_UTF      case OP_TYPEEXACT:
3333      if (utf)      case OP_TYPEPOSUPTO:
3334        {      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3335        pcre_uint32 othercase;        code += 2;
3336        if (next < 128) othercase = cd->fcc[next]; else      break;
 #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 */  
3337    
3338      case OP_NOT:      case OP_XCLASS:
3339      return c == next;      code += GET(code, 1);
3340        break;
3341    
3342        case OP_MARK:
3343        case OP_PRUNE_ARG:
3344        case OP_SKIP_ARG:
3345        case OP_THEN_ARG:
3346        code += code[1];
3347        break;
3348        }
3349    
3350      /* Add in the fixed length from the table */
3351    
3352      code += PRIV(OP_lengths)[c];
3353    
3354      /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3355      a multi-byte character. The length in the table is a minimum, so we have to
3356      arrange to skip the extra bytes. */
3357    
3358    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3359      if (utf) switch(c)
3360        {
3361        case OP_CHAR:
3362        case OP_CHARI:
3363        case OP_NOT:
3364      case OP_NOTI:      case OP_NOTI:
3365      if (c == next) return TRUE;      case OP_STAR:
3366  #ifdef SUPPORT_UTF      case OP_MINSTAR:
3367      if (utf)      case OP_PLUS:
3368        {      case OP_MINPLUS:
3369        pcre_uint32 othercase;      case OP_QUERY:
3370        if (next < 128) othercase = cd->fcc[next]; else      case OP_MINQUERY:
3371  #ifdef SUPPORT_UCP      case OP_UPTO:
3372        othercase = UCD_OTHERCASE(next);      case OP_MINUPTO:
3373        case OP_EXACT:
3374        case OP_POSSTAR:
3375        case OP_POSPLUS:
3376        case OP_POSQUERY:
3377        case OP_POSUPTO:
3378        case OP_STARI:
3379        case OP_MINSTARI:
3380        case OP_PLUSI:
3381        case OP_MINPLUSI:
3382        case OP_QUERYI:
3383        case OP_MINQUERYI:
3384        case OP_UPTOI:
3385        case OP_MINUPTOI:
3386        case OP_EXACTI:
3387        case OP_POSSTARI:
3388        case OP_POSPLUSI:
3389        case OP_POSQUERYI:
3390        case OP_POSUPTOI:
3391        case OP_NOTSTAR:
3392        case OP_NOTMINSTAR:
3393        case OP_NOTPLUS:
3394        case OP_NOTMINPLUS:
3395        case OP_NOTQUERY:
3396        case OP_NOTMINQUERY:
3397        case OP_NOTUPTO:
3398        case OP_NOTMINUPTO:
3399        case OP_NOTEXACT:
3400        case OP_NOTPOSSTAR:
3401        case OP_NOTPOSPLUS:
3402        case OP_NOTPOSQUERY:
3403        case OP_NOTPOSUPTO:
3404        case OP_NOTSTARI:
3405        case OP_NOTMINSTARI:
3406        case OP_NOTPLUSI:
3407        case OP_NOTMINPLUSI:
3408        case OP_NOTQUERYI:
3409        case OP_NOTMINQUERYI:
3410        case OP_NOTUPTOI:
3411        case OP_NOTMINUPTOI:
3412        case OP_NOTEXACTI:
3413        case OP_NOTPOSSTARI:
3414        case OP_NOTPOSPLUSI:
3415        case OP_NOTPOSQUERYI:
3416        case OP_NOTPOSUPTOI:
3417        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3418        break;
3419        }
3420  #else  #else
3421        othercase = NOTACHAR;    (void)(utf);  /* Keep compiler happy by referencing function argument */
3422  #endif  #endif
3423        return c == othercase;    }
3424        }  }
     else  
 #endif  /* SUPPORT_UTF */  
     return (c == TABLE_GET(next, cd->fcc, next));  /* Not UTF */  
3425    
     /* 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. */  
3426    
     case OP_DIGIT:  
     return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;  
3427    
3428      case OP_NOT_DIGIT:  /*************************************************
3429      return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;  *           Check for POSIX class syntax         *
3430    *************************************************/
3431    
3432      case OP_WHITESPACE:  /* This function is called when the sequence "[:" or "[." or "[=" is
3433      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;  encountered in a character class. It checks whether this is followed by a
3434    sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3435    reach an unescaped ']' without the special preceding character, return FALSE.
3436    
3437      case OP_NOT_WHITESPACE:  Originally, this function only recognized a sequence of letters between the
3438      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;  terminators, but it seems that Perl recognizes any sequence of characters,
3439    though of course unknown POSIX names are subsequently rejected. Perl gives an
3440    "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3441    didn't consider this to be a POSIX class. Likewise for [:1234:].
3442    
3443      case OP_WORDCHAR:  The problem in trying to be exactly like Perl is in the handling of escapes. We
3444      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3445    class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3446    below handles the special case of \], but does not try to do any other escape
3447    processing. This makes it different from Perl for cases such as [:l\ower:]
3448    where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
3449    "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
3450    I think.
3451    
3452      case OP_NOT_WORDCHAR:  A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3453      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;  It seems that the appearance of a nested POSIX class supersedes an apparent
3454    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3455    a digit.
3456    
3457      case OP_HSPACE:  In Perl, unescaped square brackets may also appear as part of class names. For
3458      case OP_NOT_HSPACE:  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3459      switch(next)  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3460        {  seem right at all. PCRE does not allow closing square brackets in POSIX class
3461        HSPACE_CASES:  names.
       return op_code == OP_NOT_HSPACE;  
3462    
3463        default:  Arguments:
3464        return op_code != OP_NOT_HSPACE;    ptr      pointer to the initial [
3465        }    endptr   where to return the end pointer
3466    
3467      case OP_ANYNL:  Returns:   TRUE or FALSE
3468      case OP_VSPACE:  */
     case OP_NOT_VSPACE:  
     switch(next)  
       {  
       VSPACE_CASES:  
       return op_code == OP_NOT_VSPACE;  
3469    
3470        default:  static BOOL
3471        return op_code != OP_NOT_VSPACE;  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3472    {
3473    pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3474    terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3475    for (++ptr; *ptr != CHAR_NULL; ptr++)
3476      {
3477      if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3478        ptr++;
3479      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3480      else
3481        {
3482        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3483          {
3484          *endptr = ptr;
3485          return TRUE;
3486        }        }
3487        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3488             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3489              ptr[1] == CHAR_EQUALS_SIGN) &&
3490            check_posix_syntax(ptr, endptr))
3491          return FALSE;
3492        }
3493      }
3494    return FALSE;
3495    }
3496    
 #ifdef SUPPORT_UCP  
     case OP_PROP:  
     return check_char_prop(next, previous[0], previous[1], FALSE);  
3497    
     case OP_NOTPROP:  
     return check_char_prop(next, previous[0], previous[1], TRUE);  
 #endif  
3498    
     default:  
     return FALSE;  
     }  
   }  
3499    
3500  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  /*************************************************
3501  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  *          Check POSIX class name                *
3502  generated only when PCRE_UCP is *not* set, that is, when only ASCII  *************************************************/
 characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are  
 replaced by OP_PROP codes when PCRE_UCP is set. */  
3503    
3504  switch(op_code)  /* This function is called to check the name given in a POSIX-style class entry
3505    such as [:alnum:].
3506    
3507    Arguments:
3508      ptr        points to the first letter
3509      len        the length of the name
3510    
3511    Returns:     a value representing the name, or -1 if unknown
3512    */
3513    
3514    static int
3515    check_posix_name(const pcre_uchar *ptr, int len)
3516    {
3517    const char *pn = posix_names;
3518    register int yield = 0;
3519    while (posix_name_lengths[yield] != 0)
3520    {    {
3521    case OP_CHAR:    if (len == posix_name_lengths[yield] &&
3522    case OP_CHARI:      STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3523    switch(escape)    pn += posix_name_lengths[yield] + 1;
3524      {    yield++;
3525      case ESC_d:    }
3526      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;  return -1;
3527    }
3528    
3529    
3530    /*************************************************
3531    *    Adjust OP_RECURSE items in repeated group   *
3532    *************************************************/
3533    
3534    /* OP_RECURSE items contain an offset from the start of the regex to the group
3535    that is referenced. This means that groups can be replicated for fixed
3536    repetition simply by copying (because the recursion is allowed to refer to
3537    earlier groups that are outside the current group). However, when a group is
3538    optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
3539    inserted before it, after it has been compiled. This means that any OP_RECURSE
3540    items within it that refer to the group itself or any contained groups have to
3541    have their offsets adjusted. That one of the jobs of this function. Before it
3542    is called, the partially compiled regex must be temporarily terminated with
3543    OP_END.
3544    
3545      case ESC_D:  This function has been extended with the possibility of forward references for
3546      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;  recursions and subroutine calls. It must also check the list of such references
3547    for the group we are dealing with. If it finds that one of the recursions in
3548    the current group is on this list, it adjusts the offset in the list, not the
3549    value in the reference (which is a group number).
3550    
3551      case ESC_s:  Arguments:
3552      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;    group      points to the start of the group
3553      adjust     the amount by which the group is to be moved
3554      utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3555      cd         contains pointers to tables etc.
3556      save_hwm   the hwm forward reference pointer at the start of the group
3557    
3558    Returns:     nothing
3559    */
3560    
3561      case ESC_S:  static void
3562      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
3563      pcre_uchar *save_hwm)
3564    {
3565    pcre_uchar *ptr = group;
3566    
3567      case ESC_w:  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
3568      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;    {
3569      int offset;
3570      pcre_uchar *hc;
3571    
3572      case ESC_W:    /* See if this recursion is on the forward reference list. If so, adjust the
3573      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;    reference. */
3574    
3575      case ESC_h:    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
3576      case ESC_H:      {
3577      switch(c)      offset = (int)GET(hc, 0);
3578        if (cd->start_code + offset == ptr + 1)
3579        {        {
3580        HSPACE_CASES:        PUT(hc, 0, offset + adjust);
3581        return escape != ESC_h;        break;
   
       default:  
       return escape == ESC_h;  
3582        }        }
3583        }
3584    
3585      case ESC_v:    /* Otherwise, adjust the recursion offset if it's after the start of this
3586      case ESC_V:    group. */
     switch(c)  
       {  
       VSPACE_CASES:  
       return escape != ESC_v;  
3587    
3588        default:    if (hc >= cd->hwm)
3589        return escape == ESC_v;      {
3590        }      offset = (int)GET(ptr, 1);
3591        if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
3592        }
3593    
3594      /* When PCRE_UCP is set, these values get generated for \d etc. Find    ptr += 1 + LINK_SIZE;
3595      their substitutions and process them. The result will always be either    }
3596      ESC_p or ESC_P. Then fall through to process those values. */  }
3597    
 #ifdef SUPPORT_UCP  
     case ESC_du:  
     case ESC_DU:  
     case ESC_wu:  
     case ESC_WU:  
     case ESC_su:  
     case ESC_SU:  
       {  
       int temperrorcode = 0;  
       ptr = substitutes[escape - ESC_DU];  
       escape = check_escape(&ptr, &next, &temperrorcode, 0, options, FALSE);  
       if (temperrorcode != 0) return FALSE;  
       ptr++;    /* For compatibility */  
       }  
     /* Fall through */  
3598    
     case ESC_p:  
     case ESC_P:  
       {  
       unsigned int ptype = 0, pdata = 0;  
       int errorcodeptr;  
       BOOL negated;  
3599    
3600        ptr--;      /* Make ptr point at the p or P */  /*************************************************
3601        if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr))  *        Insert an automatic callout point       *
3602          return FALSE;  *************************************************/
3603        ptr++;      /* Point past the final curly ket */  
3604    /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
3605    callout points before each pattern item.
3606    
3607    Arguments:
3608      code           current code pointer
3609      ptr            current pattern pointer
3610      cd             pointers to tables etc
3611    
3612    Returns:         new code pointer
3613    */
3614    
3615    static pcre_uchar *
3616    auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
3617    {
3618    *code++ = OP_CALLOUT;
3619    *code++ = 255;
3620    PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
3621    PUT(code, LINK_SIZE, 0);                       /* Default length */
3622    return code + 2 * LINK_SIZE;
3623    }
3624    
3625    
3626    
3627        /* If the property item is optional, we have to give up. (When generated  /*************************************************
3628        from \d etc by PCRE_UCP, this test will have been applied much earlier,  *         Complete a callout item                *
3629        to the original \d etc. At this point, ptr will point to a zero byte. */  *************************************************/
3630    
3631        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  /* A callout item contains the length of the next item in the pattern, which
3632          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
3633            return FALSE;  for both automatic and manual callouts.
3634    
3635        /* Do the property check. */  Arguments:
3636      previous_callout   points to previous callout item
3637      ptr                current pattern pointer
3638      cd                 pointers to tables etc
3639    
3640        return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);  Returns:             nothing
3641        }  */
 #endif  
3642    
3643      default:  static void
3644      return FALSE;  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
3645      }  {
3646    int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
3647    PUT(previous_callout, 2 + LINK_SIZE, length);
3648    }
3649    
   /* 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.) */  
3650    
   case OP_DIGIT:  
   return escape == ESC_D || escape == ESC_s || escape == ESC_W ||  
          escape == ESC_h || escape == ESC_v || escape == ESC_R;  
3651    
3652    case OP_NOT_DIGIT:  #ifdef SUPPORT_UCP
3653    return escape == ESC_d;  /*************************************************
3654    *           Get othercase range                  *
3655    *************************************************/
3656    
3657    case OP_WHITESPACE:  /* This function is passed the start and end of a class range, in UTF-8 mode
3658    return escape == ESC_S || escape == ESC_d || escape == ESC_w;  with UCP support. It searches up the characters, looking for ranges of
3659    characters in the "other" case. Each call returns the next one, updating the
3660    start address. A character with multiple other cases is returned on its own
3661    with a special return value.
3662    
3663    case OP_NOT_WHITESPACE:  Arguments:
3664    return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;    cptr        points to starting character value; updated
3665      d           end value
3666      ocptr       where to put start of othercase range
3667      odptr       where to put end of othercase range
3668    
3669    case OP_HSPACE:  Yield:        -1 when no more
3670    return escape == ESC_S || escape == ESC_H || escape == ESC_d ||                 0 when a range is returned
3671           escape == ESC_w || escape == ESC_v || escape == ESC_R;                >0 the CASESET offset for char with multiple other cases
3672                    in this case, ocptr contains the original
3673    */
3674    
3675    case OP_NOT_HSPACE:  static int
3676    return escape == ESC_h;  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
3677      pcre_uint32 *odptr)
3678    {
3679    pcre_uint32 c, othercase, next;
3680    unsigned int co;
3681    
3682    /* 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
3683    case OP_ANYNL:  cases, return its case offset value. */
   case OP_VSPACE:  
   return escape == ESC_V || escape == ESC_d || escape == ESC_w;  
3684    
3685    case OP_NOT_VSPACE:  for (c = *cptr; c <= d; c++)
3686    return escape == ESC_v || escape == ESC_R;    {
3687      if ((co = UCD_CASESET(c)) != 0)
3688        {
3689        *ocptr = c++;   /* Character that has the set */
3690        *cptr = c;      /* Rest of input range */
3691        return (int)co;
3692        }
3693      if ((othercase = UCD_OTHERCASE(c)) != c) break;
3694      }
3695    
3696    case OP_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;  
3697    
3698    case OP_NOT_WORDCHAR:  *ocptr = othercase;
3699    return escape == ESC_w || escape == ESC_d;  next = othercase + 1;
3700    
3701    default:  for (++c; c <= d; c++)
3702    return FALSE;    {
3703      if (UCD_OTHERCASE(c) != next) break;
3704      next++;
3705    }    }
3706    
3707  /* Control does not reach here */  *odptr = next - 1;     /* End of othercase range */
3708    *cptr = c;             /* Rest of input range */
3709    return 0;
3710  }  }
3711    #endif  /* SUPPORT_UCP */
3712    
3713    
3714    
# Line 3674  to find out the amount of memory needed, Line 3961  to find out the amount of memory needed,
3961  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
3962    
3963  Arguments:  Arguments:
3964    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
3965    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
3966    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
3967    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
3968    firstcharptr    place to put the first required character    firstcharptr      place to put the first required character
3969    firstcharflagsptr place to put the first character flags, or a negative number    firstcharflagsptr place to put the first character flags, or a negative number
3970    reqcharptr     place to put the last required character    reqcharptr        place to put the last required character
3971    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
3972    bcptr          points to current branch chain    bcptr             points to current branch chain
3973    cond_depth     conditional nesting depth    cond_depth        conditional nesting depth
3974    cd             contains pointers to tables etc.    cd                contains pointers to tables etc.
3975    lengthptr      NULL during the real compile phase    lengthptr         NULL during the real compile phase
3976                   points to length accumulator during pre-compile phase                      points to length accumulator during pre-compile phase
3977    
3978  Returns:         TRUE on success  Returns:            TRUE on success
3979                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
3980  */  */
3981    
3982  static BOOL  static BOOL
# Line 4279  for (;; ptr++) Line 4566  for (;; ptr++)
4566    
4567        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
4568          {          {
4569          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,          escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
4570            TRUE);            TRUE);
4571          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
4572          if (escape == 0) c = ec;          if (escape == 0) c = ec;
# Line 4340  for (;; ptr++) Line 4627  for (;; ptr++)
4627              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
4628              continue;              continue;
4629    
4630              /* 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
4631              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
4632              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and              previously set by something earlier in the character class.
4633              EBCDIC, so we lazily just adjust the appropriate bit. */              Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
4634                we could just adjust the appropriate bit. From PCRE 8.34 we no
4635                longer treat \s and \S specially. */
4636    
4637              case ESC_s:              case ESC_s:
4638              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];  
4639              continue;              continue;
4640    
4641              case ESC_S:              case ESC_S:
4642              should_flip_negation = TRUE;              should_flip_negation = TRUE;
4643              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
             classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */  
4644              continue;              continue;
4645    
4646              /* The rest apply in both UCP and non-UCP cases. */              /* The rest apply in both UCP and non-UCP cases. */
# Line 4853  for (;; ptr++) Line 5139  for (;; ptr++)
5139            }            }
5140          }          }
5141    
       /* 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;  
         }  
   
5142        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
5143        }        }
5144    
# Line 4883  for (;; ptr++) Line 5156  for (;; ptr++)
5156        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
5157        c = *previous;        c = *previous;
5158    
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
   
5159        OUTPUT_SINGLE_REPEAT:        OUTPUT_SINGLE_REPEAT:
5160        if (*previous == OP_PROP || *previous == OP_NOTPROP)        if (*previous == OP_PROP || *previous == OP_NOTPROP)
5161          {          {
# Line 5039  for (;; ptr++) Line 5304  for (;; ptr++)
5304      /* 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
5305      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}. */
5306    
5307      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
              *previous == OP_NCLASS ||  
5308  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5309               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
5310  #endif  #endif
5311               *previous == OP_REF ||               *previous == OP_REF   || *previous == OP_REFI ||
5312               *previous == OP_REFI)               *previous == OP_DNREF || *previous == OP_DNREFI)
5313        {        {
5314        if (repeat_max == 0)        if (repeat_max == 0)
5315          {          {
# Line 5395  for (;; ptr++) Line 5659  for (;; ptr++)
5659              pcre_uchar *scode = bracode;              pcre_uchar *scode = bracode;
5660              do              do
5661                {                {
5662                if (could_be_empty_branch(scode, ketcode, utf, cd))                if (could_be_empty_branch(scode, ketcode, utf, cd, NULL))
5663                  {                  {
5664                  *bracode += OP_SBRA - OP_BRA;                  *bracode += OP_SBRA - OP_BRA;
5665                  break;                  break;
# Line 5729  for (;; ptr++) Line 5993  for (;; ptr++)
5993          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5994          case CHAR_LEFT_PARENTHESIS:          case CHAR_LEFT_PARENTHESIS:
5995          bravalue = OP_COND;       /* Conditional group */          bravalue = OP_COND;       /* Conditional group */
5996          tempptr = ptr;          tempptr = ptr;
5997    
5998          /* A condition can be an assertion, a number (referring to a numbered          /* A condition can be an assertion, a number (referring to a numbered
5999          group), a name (referring to a named group), or 'R', referring to          group), a name (referring to a named group), or 'R', referring to
# Line 5743  for (;; ptr++) Line 6007  for (;; ptr++)
6007          by digits), and (b) a number could be a name that consists of digits.          by digits), and (b) a number could be a name that consists of digits.
6008          In both cases, we look for a name first; if not found, we try the other          In both cases, we look for a name first; if not found, we try the other
6009          cases.          cases.
6010    
6011          For compatibility with auto-callouts, we allow a callout to be          For compatibility with auto-callouts, we allow a callout to be
6012          specified before a condition that is an assertion. First, check for the          specified before a condition that is an assertion. First, check for the
6013          syntax of a callout; if found, adjust the temporary pointer that is          syntax of a callout; if found, adjust the temporary pointer that is
6014          used to check for an assertion condition. That's all that is needed! */          used to check for an assertion condition. That's all that is needed! */
6015    
6016          if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)          if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
6017            {            {
6018            for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;            for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
6019            if (ptr[i] == CHAR_RIGHT_PARENTHESIS)            if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
6020              tempptr += i + 1;              tempptr += i + 1;
6021            }            }
6022    
6023          /* For conditions that are assertions, check the syntax, and then exit          /* For conditions that are assertions, check the syntax, and then exit
6024          the switch. This will take control down to where bracketed groups,          the switch. This will take control down to where bracketed groups,
6025          including assertions, are processed. */          including assertions, are processed. */
6026    
6027          if (tempptr[1] == CHAR_QUESTION_MARK &&          if (tempptr[1] == CHAR_QUESTION_MARK &&
6028                (tempptr[2] == CHAR_EQUALS_SIGN ||                (tempptr[2] == CHAR_EQUALS_SIGN ||
6029                 tempptr[2] == CHAR_EXCLAMATION_MARK ||                 tempptr[2] == CHAR_EXCLAMATION_MARK ||
6030                 tempptr[2] == CHAR_LESS_THAN_SIGN))                 tempptr[2] == CHAR_LESS_THAN_SIGN))
6031            break;            break;
6032    
# Line 5869  for (;; ptr++) Line 6133  for (;; ptr++)
6133            slot += cd->name_entry_size;            slot += cd->name_entry_size;
6134            }            }
6135    
6136          /* Found a previous named subpattern */          /* Found the named subpattern */
6137    
6138          if (i < cd->names_found)          if (i < cd->names_found)
6139            {            {
# Line 5878  for (;; ptr++) Line 6142  for (;; ptr++)
6142            code[1+LINK_SIZE]++;            code[1+LINK_SIZE]++;
6143            }            }
6144    
         /* Search the pattern for a forward reference */  
   
         else if ((i = find_parens(cd, name, namelen,  
                         (options & PCRE_EXTENDED) != 0, utf)) > 0)  
           {  
           PUT2(code, 2+LINK_SIZE, i);  
           code[1+LINK_SIZE]++;  
           }  
   
6145          /* If terminator == CHAR_NULL it means that the name followed directly          /* If terminator == CHAR_NULL it means that the name followed directly
6146          after the opening parenthesis [e.g. (?(abc)...] and in this case there          after the opening parenthesis [e.g. (?(abc)...] and in this case there
6147          are some further alternatives to try. For the cases where terminator !=          are some further alternatives to try. For the cases where terminator !=
# Line 6050  for (;; ptr++) Line 6305  for (;; ptr++)
6305          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
6306          DEFINE_NAME:    /* Come here from (?< handling */          DEFINE_NAME:    /* Come here from (?< handling */
6307          case CHAR_APOSTROPHE:          case CHAR_APOSTROPHE:
6308            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
6309              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
6310            name = ++ptr;
6311    
6312            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
6313            namelen = (int)(ptr - name);
6314    
6315            /* In the pre-compile phase, do a syntax check, remember the longest
6316            name, and then remember the group in a vector, expanding it if
6317            necessary. Duplicates for the same number are skipped; other duplicates
6318            are checked for validity. In the actual compile, there is nothing to
6319            do. */
6320    
6321            if (lengthptr != NULL)
6322            {            {
6323            terminator = (*ptr == CHAR_LESS_THAN_SIGN)?            named_group *ng;
6324              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;            pcre_uint32 number = cd->bracount + 1;
           name = ++ptr;  
6325    
6326            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;            if (*ptr != (pcre_uchar)terminator)
6327            namelen = (int)(ptr - name);              {
6328                *errorcodeptr = ERR42;
6329                goto FAILED;
6330                }
6331    
6332            /* In the pre-compile phase, just do a syntax check. */            if (cd->names_found >= MAX_NAME_COUNT)
6333                {
6334                *errorcodeptr = ERR49;
6335                goto FAILED;
6336                }
6337    
6338            if (lengthptr != NULL)            if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
6339              {              {
6340              if (*ptr != (pcre_uchar)terminator)              cd->name_entry_size = namelen + IMM2_SIZE + 1;
6341                if (namelen > MAX_NAME_SIZE)
6342                {                {
6343                *errorcodeptr = ERR42;                *errorcodeptr = ERR48;
6344                goto FAILED;                goto FAILED;
6345                }                }
6346              if (cd->names_found >= MAX_NAME_COUNT)              }
6347                {  
6348                *errorcodeptr = ERR49;            /* Scan the list to check for duplicates. For duplicate names, if the
6349                goto FAILED;            number is the same, break the loop, which causes the name to be
6350                }            discarded; otherwise, if DUPNAMES is not set, give an error.
6351              if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)            If it is set, allow the name with a different number, but continue
6352              scanning in case this is a duplicate with the same number. For
6353              non-duplicate names, give an error if the number is duplicated. */
6354    
6355              ng = cd->named_groups;
6356              for (i = 0; i < cd->names_found; i++, ng++)
6357                {
6358                if (namelen == ng->length &&
6359                    STRNCMP_UC_UC(name, ng->name, namelen) == 0)
6360                {                {
6361                cd->name_entry_size = namelen + IMM2_SIZE + 1;                if (ng->number == number) break;
6362                if (namelen > MAX_NAME_SIZE)                if ((options & PCRE_DUPNAMES) == 0)
6363                  {                  {
6364                  *errorcodeptr = ERR48;                  *errorcodeptr = ERR43;
6365                  goto FAILED;                  goto FAILED;
6366                  }                  }
6367                  cd->dupnames = TRUE;  /* Duplicate names exist */
6368                  }
6369                else if (ng->number == number)
6370                  {
6371                  *errorcodeptr = ERR65;
6372                  goto FAILED;
6373                }                }
6374              }              }
6375    
6376            /* 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  
6377              {              {
6378              BOOL dupname = FALSE;              /* Increase the list size if necessary */
             slot = cd->name_table;  
6379    
6380              for (i = 0; i < cd->names_found; i++)              if (cd->names_found >= cd->named_group_list_size)
6381                {                {
6382                int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(namelen));                int newsize = cd->named_group_list_size * 2;
6383                if (crc == 0)                named_group *newspace = (PUBL(malloc))
6384                  {                  (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 */  
                 }  
6385    
6386                /* Make space in the table and break the loop for an earlier                if (newspace == NULL)
               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. */  
   
               if (crc < 0)  
6387                  {                  {
6388                  memmove(slot + cd->name_entry_size, slot,                  *errorcodeptr = ERR21;
6389                    IN_UCHARS((cd->names_found - i) * cd->name_entry_size));                  goto FAILED;
                 break;  
6390                  }                  }
6391    
6392                /* Continue the loop for a later or duplicate name */                memcpy(newspace, cd->named_groups,
6393                    cd->named_group_list_size * sizeof(named_group));
6394                slot += cd->name_entry_size;                if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
6395                    (PUBL(free))((void *)cd->named_groups);
6396                  cd->named_groups = newspace;
6397                  cd->named_group_list_size = newsize;
6398                }                }
6399    
6400              /* For non-duplicate names, check for a duplicate number before              cd->named_groups[cd->names_found].name = name;
6401              adding the new name. */              cd->named_groups[cd->names_found].length = namelen;
6402                cd->named_groups[cd->names_found].number = number;
6403              if (!dupname)              cd->names_found++;
               {  
               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;  
                 }  
               }  
   
             PUT2(slot, 0, cd->bracount + 1);  
             memcpy(slot + IMM2_SIZE, name, IN_UCHARS(namelen));  
             slot[IMM2_SIZE + namelen] = 0;  
6404              }              }
6405            }            }
6406    
6407          /* 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 ' */  
6408          goto NUMBERED_GROUP;          goto NUMBERED_GROUP;
6409    
6410    
# Line 6197  for (;; ptr++) Line 6434  for (;; ptr++)
6434    
6435          if (lengthptr != NULL)          if (lengthptr != NULL)
6436            {            {
6437            const pcre_uchar *temp;            named_group *ng;
6438    
6439            if (namelen == 0)            if (namelen == 0)
6440              {              {
# Line 6215  for (;; ptr++) Line 6452  for (;; ptr++)
6452              goto FAILED;              goto FAILED;
6453              }              }
6454    
6455            /* 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
6456            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
6457            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
6458            far as we have got because the syntax of named subpatterns has not            reference. */
6459            been checked for the rest of the pattern, and find_parens() assumes  
6460            correct syntax. In any case, it's a waste of resources to scan            ng = cd->named_groups;
6461            further. We stop the scan at the current point by temporarily            for (i = 0; i < cd->names_found; i++, ng++)
6462            adjusting the value of cd->endpattern. */              {
6463                if (namelen == ng->length &&
6464            temp = cd->end_pattern;                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)
6465            cd->end_pattern = ptr;                break;
6466            recno = find_parens(cd, name, namelen,              }
6467              (options & PCRE_EXTENDED) != 0, utf);            recno = (i < cd->names_found)? ng->number : 0;
6468            cd->end_pattern = temp;  
6469            if (recno < 0) recno = 0;    /* Forward ref; set dummy number */            /* Count named back references. */
6470    
6471              if (!is_recurse) cd->namedrefcount++;
6472            }            }
6473    
6474          /* 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
6475          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
6476          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
6477          the comparison will fail without reading beyond the table entry. */          comparison will fail without reading beyond the table entry. */
6478    
6479          else          else
6480            {            {
# Line 6248  for (;; ptr++) Line 6487  for (;; ptr++)
6487              slot += cd->name_entry_size;              slot += cd->name_entry_size;
6488              }              }
6489    
6490            if (i < cd->names_found)         /* Back reference */            if (i < cd->names_found)
6491              {              {
6492              recno = GET2(slot, 0);              recno = GET2(slot, 0);
6493              }              }
6494            else if ((recno =                /* Forward back reference */            else
                     find_parens(cd, name, namelen,  
                       (options & PCRE_EXTENDED) != 0, utf)) <= 0)  
6495              {              {
6496              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
6497              goto FAILED;              goto FAILED;
6498              }              }
6499            }            }
6500    
6501          /* 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
6502          recursion or backreferences. */          handles numerical recursion. */
6503    
6504          if (is_recurse) goto HANDLE_RECURSION;          if (is_recurse) goto HANDLE_RECURSION;
6505            else goto HANDLE_REFERENCE;  
6506            /* In the second pass we must see if the name is duplicated. If so, we
6507            generate a different opcode. */
6508    
6509            if (lengthptr == NULL && cd->dupnames)
6510              {
6511              int count = 1;
6512              unsigned int index = i;
6513              pcre_uchar *cslot = slot + cd->name_entry_size;
6514    
6515              for (i++; i < cd->names_found; i++)
6516                {
6517                if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break;
6518                count++;
6519                cslot += cd->name_entry_size;
6520                }
6521    
6522              if (count > 1)
6523                {
6524                if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
6525                previous = code;
6526                *code++ = ((options & PCRE_CASELESS) != 0)? OP_DNREFI : OP_DNREF;
6527                PUT2INC(code, 0, index);
6528                PUT2INC(code, 0, count);
6529    
6530                /* Process each potentially referenced group. */
6531    
6532                for (; slot < cslot; slot += cd->name_entry_size)
6533                  {
6534                  open_capitem *oc;
6535                  recno = GET2(slot, 0);
6536                  cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6537                  if (recno > cd->top_backref) cd->top_backref = recno;
6538    
6539                  /* Check to see if this back reference is recursive, that it, it
6540                  is inside the group that it references. A flag is set so that the
6541                  group can be made atomic. */
6542    
6543                  for (oc = cd->open_caps; oc != NULL; oc = oc->next)
6544                    {
6545                    if (oc->number == recno)
6546                      {
6547                      oc->flag = TRUE;
6548                      break;
6549                      }
6550                    }
6551                  }
6552    
6553                continue;  /* End of back ref handling */
6554                }
6555              }
6556    
6557            /* First pass, or a non-duplicated name. */
6558    
6559            goto HANDLE_REFERENCE;
6560    
6561    
6562          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
# Line 6364  for (;; ptr++) Line 6655  for (;; ptr++)
6655    
6656              if (called == NULL)              if (called == NULL)
6657                {                {
6658                if (find_parens(cd, NULL, recno,                if (recno > cd->final_bracount)
                     (options & PCRE_EXTENDED) != 0, utf) < 0)  
6659                  {                  {
6660                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
6661                  goto FAILED;                  goto FAILED;
# Line 6849  for (;; ptr++) Line 7139  for (;; ptr++)
7139          open_capitem *oc;          open_capitem *oc;
7140          recno = -escape;          recno = -escape;
7141    
7142          HANDLE_REFERENCE:    /* Come here from named backref handling */          /* Come here from named backref handling when the reference is to a
7143            single group (i.e. not to a duplicated name. */
7144    
7145            HANDLE_REFERENCE:
7146          if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;          if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
7147          previous = code;          previous = code;
7148          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
# Line 6905  for (;; ptr++) Line 7198  for (;; ptr++)
7198    
7199        else        else
7200          {          {
7201          if ((escape == ESC_b || escape == ESC_B || escape == ESC_A) &&          if ((escape == ESC_b || escape == ESC_B || escape == ESC_A) &&
7202               cd->max_lookbehind == 0)               cd->max_lookbehind == 0)
7203            cd->max_lookbehind = 1;            cd->max_lookbehind = 1;
7204  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 6978  for (;; ptr++) Line 7271  for (;; ptr++)
7271          *code++ = OP_PROP;          *code++ = OP_PROP;
7272          *code++ = PT_CLIST;          *code++ = PT_CLIST;
7273          *code++ = c;          *code++ = c;
7274          if (firstcharflags == REQ_UNSET) firstcharflags = zerofirstcharflags = REQ_NONE;          if (firstcharflags == REQ_UNSET)
7275              firstcharflags = zerofirstcharflags = REQ_NONE;
7276          break;          break;
7277          }          }
7278        }        }
# Line 7067  out the amount of memory needed, as well Line 7361  out the amount of memory needed, as well
7361  value of lengthptr distinguishes the two phases.  value of lengthptr distinguishes the two phases.
7362    
7363  Arguments:  Arguments:
7364    options        option bits, including any changes for this subpattern    options           option bits, including any changes for this subpattern
7365    codeptr        -> the address of the current code pointer    codeptr           -> the address of the current code pointer
7366    ptrptr         -> the address of the current pattern pointer    ptrptr            -> the address of the current pattern pointer
7367    errorcodeptr   -> pointer to error code variable    errorcodeptr      -> pointer to error code variable
7368    lookbehind     TRUE if this is a lookbehind assertion    lookbehind        TRUE if this is a lookbehind assertion
7369    reset_bracount TRUE to reset the count for each branch    reset_bracount    TRUE to reset the count for each branch
7370    skipbytes      skip this many bytes at start (for brackets and OP_COND)    skipbytes         skip this many bytes at start (for brackets and OP_COND)
7371    cond_depth     depth of nesting for conditional subpatterns    cond_depth        depth of nesting for conditional subpatterns
7372    firstcharptr    place to put the first required character    firstcharptr      place to put the first required character
7373    firstcharflagsptr place to put the first character flags, or a negative number    firstcharflagsptr place to put the first character flags, or a negative number
7374    reqcharptr     place to put the last required character    reqcharptr        place to put the last required character
7375    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
7376    bcptr          pointer to the chain of currently open branches    bcptr             pointer to the chain of currently open branches
7377    cd             points to the data block with tables pointers etc.    cd                points to the data block with tables pointers etc.
7378    lengthptr      NULL during the real compile phase    lengthptr         NULL during the real compile phase
7379                   points to length accumulator during pre-compile phase                      points to length accumulator during pre-compile phase
7380    
7381  Returns:         TRUE on success  Returns:            TRUE on success
7382  */  */
7383    
7384  static BOOL  static BOOL
# Line 7621  return TRUE; Line 7915  return TRUE;
7915  discarded, because they can cause conflicts with actual literals that follow.  discarded, because they can cause conflicts with actual literals that follow.
7916  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,
7917  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
7918  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
7919  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
7920  we return that char, otherwise -1.  char (recurse ad lib), then we return that char, with the flags set to zero or
7921    REQ_CASELESS; otherwise return zero with REQ_NONE in the flags.
7922    
7923  Arguments:  Arguments:
7924    code       points to start of expression (the bracket)    code       points to start of expression (the bracket)
7925    flags       points to the first char flags, or to REQ_NONE    flags      points to the first char flags, or to REQ_NONE
7926    inassert   TRUE if in an assertion    inassert   TRUE if in an assertion
7927    
7928  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 7664  do { Line 7959  do {
7959       case OP_ASSERT:       case OP_ASSERT:
7960       case OP_ONCE:       case OP_ONCE:
7961       case OP_ONCE_NC:       case OP_ONCE_NC:
      case OP_COND:  
7962       d = find_firstassertedchar(scode, &dflags, op == OP_ASSERT);       d = find_firstassertedchar(scode, &dflags, op == OP_ASSERT);
7963       if (dflags < 0)       if (dflags < 0)
7964         return 0;         return 0;
# Line 7709  return c; Line 8003  return c;
8003    
8004    
8005  /*************************************************  /*************************************************
8006    *     Add an entry to the name/number table      *
8007    *************************************************/
8008    
8009    /* This function is called between compiling passes to add an entry to the
8010    name/number table, maintaining alphabetical order. Checking for permitted
8011    and forbidden duplicates has already been done.
8012    
8013    Arguments:
8014      cd           the compile data block
8015      name         the name to add
8016      length       the length of the name
8017      groupno      the group number
8018    
8019    Returns:       nothing
8020    */
8021    
8022    static void
8023    add_name(compile_data *cd, const pcre_uchar *name, int length,
8024      unsigned int groupno)
8025    {
8026    int i;
8027    pcre_uchar *slot = cd->name_table;
8028    
8029    for (i = 0; i < cd->names_found; i++)
8030      {
8031      int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(length));
8032      if (crc == 0 && slot[IMM2_SIZE+length] != 0)
8033        crc = -1; /* Current name is a substring */
8034    
8035      /* Make space in the table and break the loop for an earlier name. For a
8036      duplicate or later name, carry on. We do this for duplicates so that in the
8037      simple case (when ?(| is not used) they are in order of their numbers. In all
8038      cases they are in the order in which they appear in the pattern. */
8039    
8040      if (crc < 0)
8041        {
8042        memmove(slot + cd->name_entry_size, slot,
8043          IN_UCHARS((cd->names_found - i) * cd->name_entry_size));
8044        break;
8045        }
8046    
8047      /* Continue the loop for a later or duplicate name */
8048    
8049      slot += cd->name_entry_size;
8050      }
8051    
8052    PUT2(slot, 0, groupno);
8053    memcpy(slot + IMM2_SIZE, name, IN_UCHARS(length));
8054    slot[IMM2_SIZE + length] = 0;
8055    cd->names_found++;
8056    }
8057    
8058    
8059    
8060    /*************************************************
8061  *        Compile a Regular Expression            *  *        Compile a Regular Expression            *
8062  *************************************************/  *************************************************/
8063    
# Line 7770  pcre32_compile2(PCRE_SPTR32 pattern, int Line 8119  pcre32_compile2(PCRE_SPTR32 pattern, int
8119  {  {
8120  REAL_PCRE *re;  REAL_PCRE *re;
8121  int length = 1;  /* For final END opcode */  int length = 1;  /* For final END opcode */
 pcre_uint32 firstchar, reqchar;  
8122  pcre_int32 firstcharflags, reqcharflags;  pcre_int32 firstcharflags, reqcharflags;
8123    pcre_uint32 firstchar, reqchar;
8124    pcre_uint32 limit_match = PCRE_UINT32_MAX;
8125    pcre_uint32 limit_recursion = PCRE_UINT32_MAX;
8126  int newline;  int newline;
8127  int errorcode = 0;  int errorcode = 0;
8128  int skipatstart = 0;  int skipatstart = 0;
8129  BOOL utf;  BOOL utf;
8130    BOOL never_utf = FALSE;
8131  size_t size;  size_t size;
8132  pcre_uchar *code;  pcre_uchar *code;
8133  const pcre_uchar *codestart;  const pcre_uchar *codestart;
# Line 7792  new memory is obtained from malloc(). */ Line 8144  new memory is obtained from malloc(). */
8144    
8145  pcre_uchar cworkspace[COMPILE_WORK_SIZE];  pcre_uchar cworkspace[COMPILE_WORK_SIZE];
8146    
8147    /* This vector is used for remembering name groups during the pre-compile. In a
8148    similar way to cworkspace, it can be expanded using malloc() if necessary. */
8149    
8150    named_group named_groups[NAMED_GROUP_LIST_SIZE];
8151    
8152  /* Set this early so that early errors get offset 0. */  /* Set this early so that early errors get offset 0. */
8153    
8154  ptr = (const pcre_uchar *)pattern;  ptr = (const pcre_uchar *)pattern;
# Line 7835  if ((options & ~PUBLIC_COMPILE_OPTIONS) Line 8192  if ((options & ~PUBLIC_COMPILE_OPTIONS)
8192    goto PCRE_EARLY_ERROR_RETURN;    goto PCRE_EARLY_ERROR_RETURN;
8193    }    }
8194    
8195    /* If PCRE_NEVER_UTF is set, remember it. */
8196    
8197    if ((options & PCRE_NEVER_UTF) != 0) never_utf = TRUE;
8198    
8199  /* Check for global one-time settings at the start of the pattern, and remember  /* Check for global one-time settings at the start of the pattern, and remember
8200  the offset for later. */  the offset for later. */
8201    
8202    cd->external_flags = 0;   /* Initialize here for LIMIT_MATCH/RECURSION */
8203    
8204  while (ptr[skipatstart] == CHAR_LEFT_PARENTHESIS &&  while (ptr[skipatstart] == CHAR_LEFT_PARENTHESIS &&
8205         ptr[skipatstart+1] == CHAR_ASTERISK)         ptr[skipatstart+1] == CHAR_ASTERISK)
8206    {    {
# Line 7868  PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. * Line 8231  PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. *
8231    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_START_OPT_RIGHTPAR, 13) == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_START_OPT_RIGHTPAR, 13) == 0)
8232      { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }      { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }
8233    
8234      else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_MATCH_EQ, 12) == 0)
8235        {
8236        pcre_uint32 c = 0;
8237        int p = skipatstart + 14;
8238        while (isdigit(ptr[p]))
8239          {
8240          if (c > PCRE_UINT32_MAX / 10 - 1) break;   /* Integer overflow */
8241          c = c*10 + ptr[p++] - CHAR_0;
8242          }
8243        if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break;
8244        if (c < limit_match)
8245          {
8246          limit_match = c;
8247          cd->external_flags |= PCRE_MLSET;
8248          }
8249        skipatstart = p;
8250        continue;
8251        }
8252    
8253      else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_RECURSION_EQ, 16) == 0)
8254        {
8255        pcre_uint32 c = 0;
8256        int p = skipatstart + 18;
8257        while (isdigit(ptr[p]))
8258          {
8259          if (c > PCRE_UINT32_MAX / 10 - 1) break;   /* Integer overflow check */
8260          c = c*10 + ptr[p++] - CHAR_0;
8261          }
8262        if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break;
8263        if (c < limit_recursion)
8264          {
8265          limit_recursion = c;
8266          cd->external_flags |= PCRE_RLSET;
8267          }
8268        skipatstart = p;
8269        continue;
8270        }
8271    
8272    if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CR_RIGHTPAR, 3) == 0)    if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CR_RIGHTPAR, 3) == 0)
8273      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
8274    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LF_RIGHTPAR, 3)  == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LF_RIGHTPAR, 3)  == 0)
# Line 7893  PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. * Line 8294  PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. *
8294    
8295  /* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */  /* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */
8296  utf = (options & PCRE_UTF8) != 0;  utf = (options & PCRE_UTF8) != 0;
8297    if (utf && never_utf)
8298      {
8299      errorcode = ERR78;
8300      goto PCRE_EARLY_ERROR_RETURN2;
8301      }
8302    
8303  /* Can't support UTF unless PCRE has been compiled to include the code. The  /* Can't support UTF unless PCRE has been compiled to include the code. The
8304  return of an error code from PRIV(valid_utf)() is a new feature, introduced in  return of an error code from PRIV(valid_utf)() is a new feature, introduced in
# Line 8005  cd->bracount = cd->final_bracount = 0; Line 8411  cd->bracount = cd->final_bracount = 0;
8411  cd->names_found = 0;  cd->names_found = 0;
8412  cd->name_entry_size = 0;  cd->name_entry_size = 0;
8413  cd->name_table = NULL;  cd->name_table = NULL;
8414    cd->dupnames = FALSE;
8415    cd->namedrefcount = 0;
8416  cd->start_code = cworkspace;  cd->start_code = cworkspace;
8417  cd->hwm = cworkspace;  cd->hwm = cworkspace;
8418  cd->start_workspace = cworkspace;  cd->start_workspace = cworkspace;
8419  cd->workspace_size = COMPILE_WORK_SIZE;  cd->workspace_size = COMPILE_WORK_SIZE;
8420    cd->named_groups = named_groups;
8421    cd->named_group_list_size = NAMED_GROUP_LIST_SIZE;
8422  cd->start_pattern = (const pcre_uchar *)pattern;  cd->start_pattern = (const pcre_uchar *)pattern;
8423  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));
8424  cd->req_varyopt = 0;  cd->req_varyopt = 0;
8425  cd->assert_depth = 0;  cd->assert_depth = 0;
8426  cd->max_lookbehind = 0;  cd->max_lookbehind = 0;
8427  cd->external_options = options;  cd->external_options = options;
 cd->external_flags = 0;  
8428  cd->open_caps = NULL;  cd->open_caps = NULL;
8429    
8430  /* Now do the pre-compile. On error, errorcode will be set non-zero, so we  /* Now do the pre-compile. On error, errorcode will be set non-zero, so we
# Line 8041  if (length > MAX_PATTERN_SIZE) Line 8450  if (length > MAX_PATTERN_SIZE)
8450    goto PCRE_EARLY_ERROR_RETURN;    goto PCRE_EARLY_ERROR_RETURN;
8451    }    }
8452    
8453  /* 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
8454  externally provided function. Integer overflow should no longer be possible  name, we must allow for the possibility of named references to duplicated
8455  because nowadays we limit the maximum value of cd->names_found and  groups. These require an extra data item each. */
 cd->name_entry_size. */  
8456    
8457  size = sizeof(REAL_PCRE) + (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar);  if (cd->dupnames && cd->namedrefcount > 0)
8458  re = (REAL_PCRE *)(PUBL(malloc))(size);    length += cd->namedrefcount * IMM2_SIZE * sizeof(pcre_uchar);
8459    
8460    /* Compute the size of the data block for storing the compiled pattern. Integer
8461    overflow should no longer be possible because nowadays we limit the maximum
8462    value of cd->names_found and cd->name_entry_size. */
8463    
8464    size = sizeof(REAL_PCRE) +
8465      (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar);
8466    
8467    /* Get the memory. */
8468    
8469    re = (REAL_PCRE *)(PUBL(malloc))(size);
8470  if (re == NULL)  if (re == NULL)
8471    {    {
8472    errorcode = ERR21;    errorcode = ERR21;
# Line 8065  re->magic_number = MAGIC_NUMBER; Line 8483  re->magic_number = MAGIC_NUMBER;
8483  re->size = (int)size;  re->size = (int)size;
8484  re->options = cd->external_options;  re->options = cd->external_options;
8485  re->flags = cd->external_flags;  re->flags = cd->external_flags;
8486    re->limit_match = limit_match;
8487    re->limit_recursion = limit_recursion;
8488  re->first_char = 0;  re->first_char = 0;
8489  re->req_char = 0;  re->req_char = 0;
8490  re->name_table_offset = sizeof(REAL_PCRE) / sizeof(pcre_uchar);  re->name_table_offset = sizeof(REAL_PCRE) / sizeof(pcre_uchar);
# Line 8074  re->ref_count = 0; Line 8494  re->ref_count = 0;
8494  re->tables = (tables == PRIV(default_tables))? NULL : tables;  re->tables = (tables == PRIV(default_tables))? NULL : tables;
8495  re->nullpad = NULL;  re->nullpad = NULL;
8496  #ifdef COMPILE_PCRE32  #ifdef COMPILE_PCRE32
8497  re->dummy1 = re->dummy2 = 0;  re->dummy = 0;
8498    #else
8499    re->dummy1 = re->dummy2 = re->dummy3 = 0;
8500  #endif  #endif
8501    
8502  /* The starting points of the name/number translation table and of the code are  /* The starting points of the name/number translation table and of the code are
# Line 8088  cd->final_bracount = cd->bracount;  /* S Line 8510  cd->final_bracount = cd->bracount;  /* S
8510  cd->assert_depth = 0;  cd->assert_depth = 0;
8511  cd->bracount = 0;  cd->bracount = 0;
8512  cd->max_lookbehind = 0;  cd->max_lookbehind = 0;
 cd->names_found = 0;  
8513  cd->name_table = (pcre_uchar *)re + re->name_table_offset;  cd->name_table = (pcre_uchar *)re + re->name_table_offset;
8514  codestart = cd->name_table + re->name_entry_size * re->name_count;  codestart = cd->name_table + re->name_entry_size * re->name_count;
8515  cd->start_code = codestart;  cd->start_code = codestart;
# Line 8099  cd->had_pruneorskip = FALSE; Line 8520  cd->had_pruneorskip = FALSE;
8520  cd->check_lookbehind = FALSE;  cd->check_lookbehind = FALSE;
8521  cd->open_caps = NULL;  cd->open_caps = NULL;
8522    
8523    /* If any named groups were found, create the name/number table from the list
8524    created in the first pass. */
8525    
8526    if (cd->names_found > 0)
8527      {
8528      int i = cd->names_found;
8529      named_group *ng = cd->named_groups;
8530      cd->names_found = 0;
8531      for (; i > 0; i--, ng++)
8532        add_name(cd, ng->name, ng->length, ng->number);
8533      if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
8534        (PUBL(free))((void *)cd->named_groups);
8535      }
8536    
8537  /* Set up a starting, non-extracting bracket, then compile the expression. On  /* Set up a starting, non-extracting bracket, then compile the expression. On
8538  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
8539  of the function here. */  of the function here. */
# Line 8134  if (code - codestart > length) errorcode Line 8569  if (code - codestart > length) errorcode
8569    
8570  #ifdef SUPPORT_VALGRIND  #ifdef SUPPORT_VALGRIND
8571  /* If the estimated length exceeds the really used length, mark the extra  /* If the estimated length exceeds the really used length, mark the extra
8572  allocated memory as unadressable, so that any out-of-bound reads can be  allocated memory as unaddressable, so that any out-of-bound reads can be
8573  detected. */  detected. */
8574  VALGRIND_MAKE_MEM_NOACCESS(code, (length - (code - codestart)) * sizeof(pcre_uchar));  VALGRIND_MAKE_MEM_NOACCESS(code, (length - (code - codestart)) * sizeof(pcre_uchar));
8575  #endif  #endif
# Line 8162  if (cd->hwm > cd->start_workspace) Line 8597  if (cd->hwm > cd->start_workspace)
8597      }      }
8598    }    }
8599    
8600  /* If the workspace had to be expanded, free the new memory. */  /* If the workspace had to be expanded, free the new memory. Set the pointer to
8601    NULL to indicate that forward references have been filled in. */
8602    
8603  if (cd->workspace_size > COMPILE_WORK_SIZE)  if (cd->workspace_size > COMPILE_WORK_SIZE)
8604    (PUBL(free))((void *)cd->start_workspace);    (PUBL(free))((void *)cd->start_workspace);
8605    cd->start_workspace = NULL;
8606    
8607  /* 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
8608  subpattern. */  subpattern. */
8609    
8610  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
8611    
8612    /* Unless disabled, check whether single character iterators can be
8613    auto-possessified. The function overwrites the appropriate opcode values. */
8614    
8615    if ((options & PCRE_NO_AUTO_POSSESSIFY) == 0)
8616      auto_possessify((pcre_uchar *)codestart, utf, cd);
8617    
8618  /* If there were any lookbehind assertions that contained OP_RECURSE  /* If there were any lookbehind assertions that contained OP_RECURSE
8619  (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,
8620  because they may contain forward references. Actual recursions cannot be fixed  because they may contain forward references. Actual recursions cannot be fixed
# Line 8369  if (code - codestart > length) Line 8812  if (code - codestart > length)
8812    }    }
8813  #endif   /* PCRE_DEBUG */  #endif   /* PCRE_DEBUG */
8814    
8815    /* Check for a pattern than can match an empty string, so that this information
8816    can be provided to applications. */
8817    
8818    do
8819      {
8820      if (could_be_empty_branch(codestart, code, utf, cd, NULL))
8821        {
8822        re->flags |= PCRE_MATCH_EMPTY;
8823        break;
8824        }
8825      codestart += GET(codestart, 1);
8826      }
8827    while (*codestart == OP_ALT);
8828    
8829  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
8830  return (pcre *)re;  return (pcre *)re;
8831  #elif defined COMPILE_PCRE16  #elif defined COMPILE_PCRE16

Legend:
Removed from v.1266  
changed lines
  Added in v.1364

  ViewVC Help
Powered by ViewVC 1.1.5