/[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 1359 by ph10, Tue Sep 3 10:10:59 2013 UTC revision 1363 by ph10, Tue Oct 1 16:54:40 2013 UTC
# Line 115  kicks in at the same number of forward r Line 115  kicks in at the same number of forward r
115  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
117    
118  /* This value determines the size of the initial vector that is used for  /* 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,  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  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. */  workspace. The value is the number of slots in the list. */
122    
123  #define NAMED_GROUP_LIST_SIZE  20  #define NAMED_GROUP_LIST_SIZE  20
# Line 655  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 682  return s; Line 801  return s;
801  }  }
802    
803    
804    
805  /*************************************************  /*************************************************
806  *           Expand the workspace                 *  *           Expand the workspace                 *
807  *************************************************/  *************************************************/
# Line 1199  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 1296  return FALSE; Line 1418  return FALSE;
1418    
1419    
1420    
   
1421  /*************************************************  /*************************************************
1422  *         Read repeat counts                     *  *         Read repeat counts                     *
1423  *************************************************/  *************************************************/
# Line 1419  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 1722  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 1758  for (;;) Line 1880  for (;;)
1880    
1881    
1882    
   
1883  /*************************************************  /*************************************************
1884  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
1885  *************************************************/  *************************************************/
# Line 2460  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.  
   
 Originally, this function only recognized a sequence of letters between the  
 terminators, but it seems that Perl recognizes any sequence of characters,  
 though of course unknown POSIX names are subsequently rejected. Perl gives an  
 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE  
 didn't consider this to be a POSIX class. Likewise for [:1234:].  
   
 The problem in trying to be exactly like Perl is in the handling of escapes. We  
 have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX  
 class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code  
 below handles the special case of \], but does not try to do any other escape  
 processing. This makes it different from Perl for cases such as [:l\ower:]  
 where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize  
 "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,  
 I think.  
   
 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.  
 It seems that the appearance of a nested POSIX class supersedes an apparent  
 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or  
 a digit.  
   
 In Perl, unescaped square brackets may also appear as part of class names. For  
 example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for  
 [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not  
 seem right at all. PCRE does not allow closing square brackets in POSIX class  
 names.  
   
 Arguments:  
   ptr      pointer to the initial [  
   endptr   where to return the end pointer  
2589    
2590  Returns:   TRUE or FALSE  Arguments:  c opcode
2591    Returns:    base opcode for the type
2592  */  */
2593    
2594  static BOOL  static pcre_uchar
2595  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  get_repeat_base(pcre_uchar c)
2596  {  {
2597  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */  return (c > OP_TYPEPOSUPTO)? c :
2598  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */         (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2599  for (++ptr; *ptr != CHAR_NULL; ptr++)         (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2600    {         (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2601    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)         (c >= OP_STARI)?      OP_STARI :
2602      ptr++;                               OP_STAR;
   else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;  
   else  
     {  
     if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)  
       {  
       *endptr = ptr;  
       return TRUE;  
       }  
     if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&  
          (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||  
           ptr[1] == CHAR_EQUALS_SIGN) &&  
         check_posix_syntax(ptr, endptr))  
       return FALSE;  
     }  
   }  
 return FALSE;  
2603  }  }
2604    
2605    
2606    
2607    #ifdef SUPPORT_UCP
2608  /*************************************************  /*************************************************
2609  *          Check POSIX class name                *  *        Check a character and a property        *
2610  *************************************************/  *************************************************/
2611    
2612  /* This function is called to check the name given in a POSIX-style class entry  /* This function is called by check_auto_possessive() when a property item
2613  such as [:alnum:].  is adjacent to a fixed character.
2614    
2615  Arguments:  Arguments:
2616    ptr        points to the first letter    c            the character
2617    len        the length of the name    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:     a value representing the name, or -1 if unknown  Returns:       TRUE if auto-possessifying is OK
2622  */  */
2623    
2624  static int  static BOOL
2625  check_posix_name(const pcre_uchar *ptr, int len)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2626      BOOL negated)
2627  {  {
2628  const char *pn = posix_names;  const pcre_uint32 *p;
2629  register int yield = 0;  const ucd_record *prop = GET_UCD(c);
 while (posix_name_lengths[yield] != 0)  
   {  
   if (len == posix_name_lengths[yield] &&  
     STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;  
   pn += posix_name_lengths[yield] + 1;  
   yield++;  
   }  
 return -1;  
 }  
   
   
 /*************************************************  
 *    Adjust OP_RECURSE items in repeated group   *  
 *************************************************/  
2630    
2631  /* OP_RECURSE items contain an offset from the start of the regex to the group  switch(ptype)
2632  that is referenced. This means that groups can be replicated for fixed    {
2633  repetition simply by copying (because the recursion is allowed to refer to    case PT_LAMP:
2634  earlier groups that are outside the current group). However, when a group is    return (prop->chartype == ucp_Lu ||
2635  optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is            prop->chartype == ucp_Ll ||
2636  inserted before it, after it has been compiled. This means that any OP_RECURSE            prop->chartype == ucp_Lt) == negated;
 items within it that refer to the group itself or any contained groups have to  
 have their offsets adjusted. That one of the jobs of this function. Before it  
 is called, the partially compiled regex must be temporarily terminated with  
 OP_END.  
2637    
2638  This function has been extended with the possibility of forward references for    case PT_GC:
2639  recursions and subroutine calls. It must also check the list of such references    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
 for the group we are dealing with. If it finds that one of the recursions in  
 the current group is on this list, it adjusts the offset in the list, not the  
 value in the reference (which is a group number).  
2640    
2641  Arguments:    case PT_PC:
2642    group      points to the start of the group    return (pdata == prop->chartype) == negated;
   adjust     the amount by which the group is to be moved  
   utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode  
   cd         contains pointers to tables etc.  
   save_hwm   the hwm forward reference pointer at the start of the group  
2643    
2644  Returns:     nothing    case PT_SC:
2645  */    return (pdata == prop->script) == negated;
2646    
2647  static void    /* These are specials */
 adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,  
   pcre_uchar *save_hwm)  
 {  
 pcre_uchar *ptr = group;  
2648    
2649  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)    case PT_ALNUM:
2650    {    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2651    int offset;            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
   pcre_uchar *hc;  
2652    
2653    /* See if this recursion is on the forward reference list. If so, adjust the    case PT_SPACE:    /* Perl space */
2654    reference. */    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2655              c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2656              == negated;
2657    
2658    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    case PT_PXSPACE:  /* POSIX space */
2659      {    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2660      offset = (int)GET(hc, 0);            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2661      if (cd->start_code + offset == ptr + 1)            c == CHAR_FF || c == CHAR_CR)
2662        {            == negated;
       PUT(hc, 0, offset + adjust);  
       break;  
       }  
     }  
2663    
2664    /* Otherwise, adjust the recursion offset if it's after the start of this    case PT_WORD:
2665    group. */    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2666              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2667              c == CHAR_UNDERSCORE) == negated;
2668    
2669    if (hc >= cd->hwm)    case PT_CLIST:
2670      p = PRIV(ucd_caseless_sets) + prop->caseset;
2671      for (;;)
2672      {      {
2673      offset = (int)GET(ptr, 1);      if (c < *p) return !negated;
2674      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);      if (c == *p++) return negated;
2675      }      }
2676      break;  /* Control never reaches here */
   ptr += 1 + LINK_SIZE;  
2677    }    }
2678    
2679    return FALSE;
2680  }  }
2681    #endif  /* SUPPORT_UCP */
2682    
2683    
2684    
2685  /*************************************************  /*************************************************
2686  *        Insert an automatic callout point       *  *        Fill the character property list        *
2687  *************************************************/  *************************************************/
2688    
2689  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert  /* Checks whether the code points to an opcode that can take part in auto-
2690  callout points before each pattern item.  possessification, and if so, fills a list with its properties.
2691    
2692  Arguments:  Arguments:
2693    code           current code pointer    code        points to start of expression
2694    ptr            current pattern pointer    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2695    cd             pointers to tables etc    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:         new code pointer  Returns:      points to the start of the next opcode if *code is accepted
2703                  NULL if *code is not accepted
2704  */  */
2705    
2706  static pcre_uchar *  static const pcre_uchar *
2707  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)  get_chr_property_list(const pcre_uchar *code, BOOL utf,
2708      const pcre_uint8 *fcc, pcre_uint32 *list)
2709  {  {
2710  *code++ = OP_CALLOUT;  pcre_uchar c = *code;
2711  *code++ = 255;  const pcre_uchar *end;
2712  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  const pcre_uint32 *clist_src;
2713  PUT(code, LINK_SIZE, 0);                       /* Default length */  pcre_uint32 *clist_dest;
2714  return code + 2 * LINK_SIZE;  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      base = get_repeat_base(c);
2724      c -= (base - OP_STAR);
2725    
2726  /*************************************************    if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2727  *         Complete a callout item                *      code += IMM2_SIZE;
 *************************************************/  
2728    
2729  /* A callout item contains the length of the next item in the pattern, which    list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
 we can't fill in till after we have reached the relevant point. This is used  
 for both automatic and manual callouts.  
2730    
2731  Arguments:    switch(base)
2732    previous_callout   points to previous callout item      {
2733    ptr                current pattern pointer      case OP_STAR:
2734    cd                 pointers to tables etc      list[0] = OP_CHAR;
2735        break;
2736    
2737  Returns:             nothing      case OP_STARI:
2738  */      list[0] = OP_CHARI;
2739        break;
2740    
2741  static void      case OP_NOTSTAR:
2742  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)      list[0] = OP_NOT;
2743  {      break;
 int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  
 PUT(previous_callout, 2 + LINK_SIZE, length);  
 }  
2744    
2745        case OP_NOTSTARI:
2746        list[0] = OP_NOTI;
2747        break;
2748    
2749        case OP_TYPESTAR:
2750        list[0] = *code;
2751        code++;
2752        break;
2753        }
2754      c = list[0];
2755      }
2756    
2757  #ifdef SUPPORT_UCP  switch(c)
2758  /*************************************************    {
2759  *           Get othercase range                  *    case OP_NOT_DIGIT:
2760  *************************************************/    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  /* This function is passed the start and end of a class range, in UTF-8 mode    case OP_CHAR:
2780  with UCP support. It searches up the characters, looking for ranges of    case OP_NOT:
2781  characters in the "other" case. Each call returns the next one, updating the    GETCHARINCTEST(chr, code);
2782  start address. A character with multiple other cases is returned on its own    list[2] = chr;
2783  with a special return value.    list[3] = NOTACHAR;
2784      return code;
2785    
2786  Arguments:    case OP_CHARI:
2787    cptr        points to starting character value; updated    case OP_NOTI:
2788    d           end value    list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2789    ocptr       where to put start of othercase range    GETCHARINCTEST(chr, code);
2790    odptr       where to put end of othercase range    list[2] = chr;
2791    
2792  Yield:        -1 when no more  #ifdef SUPPORT_UCP
2793                 0 when a range is returned    if (chr < 128 || (chr < 256 && !utf))
2794                >0 the CASESET offset for char with multiple other cases      list[3] = fcc[chr];
2795                  in this case, ocptr contains the original    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  static int    /* The othercase might be the same value. */
 get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,  
   pcre_uint32 *odptr)  
 {  
 pcre_uint32 c, othercase, next;  
 unsigned int co;  
2804    
2805  /* Find the first character that has an other case. If it has multiple other    if (chr == list[3])
2806  cases, return its case offset value. */      list[3] = NOTACHAR;
2807      else
2808        list[4] = NOTACHAR;
2809      return code;
2810    
2811  for (c = *cptr; c <= d; c++)  #ifdef SUPPORT_UCP
2812    {    case OP_PROP:
2813    if ((co = UCD_CASESET(c)) != 0)    case OP_NOTPROP:
2814      if (code[0] != PT_CLIST)
2815      {      {
2816      *ocptr = c++;   /* Character that has the set */      list[2] = code[0];
2817      *cptr = c;      /* Rest of input range */      list[3] = code[1];
2818      return (int)co;      return code + 2;
2819      }      }
   if ((othercase = UCD_OTHERCASE(c)) != c) break;  
   }  
2820    
2821  if (c > d) return -1;  /* Reached end of range */    /* Convert only if we have anough space. */
2822    
2823  *ocptr = othercase;    clist_src = PRIV(ucd_caseless_sets) + code[1];
2824  next = othercase + 1;    clist_dest = list + 2;
2825      code += 2;
2826    
2827  for (++c; c <= d; c++)    do {
2828    {       /* Early return if there is not enough space. */
2829    if (UCD_OTHERCASE(c) != next) break;       if (clist_dest >= list + 8)
2830    next++;         {
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  *odptr = next - 1;     /* End of othercase range */    /* Enough space to store all characters. */
2840  *cptr = c;             /* Rest of input range */  
2841  return 0;    list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
2842      return code;
2843    #endif
2844    
2845      case OP_NCLASS:
2846      case OP_CLASS:
2847    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2848      case OP_XCLASS:
2849    
2850      if (c == OP_XCLASS)
2851        end = code + GET(code, 0);
2852      else
2853    #endif
2854        end = code + 32 / sizeof(pcre_uchar);
2855    
2856      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        case OP_CRRANGE:
2867        case OP_CRMINRANGE:
2868        list[1] = (GET2(end, 1) == 0);
2869        end += 1 + 2 * IMM2_SIZE;
2870        break;
2871        }
2872      list[2] = end - code;
2873      return end;
2874      }
2875    return NULL;    /* Opcode not accepted */
2876  }  }
2877    
2878    
2879    
2880  /*************************************************  /*************************************************
2881  *        Check a character and a property        *  *    Scan further character sets for match       *
2882  *************************************************/  *************************************************/
2883    
2884  /* This function is called by check_auto_possessive() when a property item  /* Checks whether the base and the current opcode have a common character, in
2885  is adjacent to a fixed character.  which case the base cannot be possessified.
2886    
2887  Arguments:  Arguments:
2888    c            the character    code        points to the byte code
2889    ptype        the property type    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
2890    pdata        the data for the type    cd          static compile data
2891    negated      TRUE if it's a negated property (\P or \p{^)    base_list   the data list of the base opcode
2892    
2893  Returns:       TRUE if auto-possessifying is OK  Returns:      TRUE if the auto-possessification is possible
2894  */  */
2895    
2896  static BOOL  static BOOL
2897  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated)  compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
2898      const pcre_uint32* base_list)
2899  {  {
2900  #ifdef SUPPORT_UCP  pcre_uchar c;
2901  const pcre_uint32 *p;  pcre_uint32 list[8];
2902  #endif  const pcre_uint32* chr_ptr;
2903    const pcre_uint32* ochr_ptr;
2904  const ucd_record *prop = GET_UCD(c);  const pcre_uint32* list_ptr;
2905    pcre_uint32 chr;
2906    
2907  switch(ptype)  for(;;)
2908    {    {
2909    case PT_LAMP:    c = *code;
   return (prop->chartype == ucp_Lu ||  
           prop->chartype == ucp_Ll ||  
           prop->chartype == ucp_Lt) == negated;  
   
   case PT_GC:  
   return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;  
   
   case PT_PC:  
   return (pdata == prop->chartype) == negated;  
   
   case PT_SC:  
   return (pdata == prop->script) == negated;  
   
   /* These are specials */  
   
   case PT_ALNUM:  
   return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||  
           PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;  
   
   case PT_SPACE:    /* Perl space */  
   return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||  
           c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)  
           == negated;  
2910    
2911    case PT_PXSPACE:  /* POSIX space */    /* Skip over callouts */
   return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||  
           c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||  
           c == CHAR_FF || c == CHAR_CR)  
           == negated;  
2912    
2913    case PT_WORD:    if (c == OP_CALLOUT)
2914    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||      {
2915            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||      code += PRIV(OP_lengths)[c];
2916            c == CHAR_UNDERSCORE) == negated;      continue;
2917        }
2918    
2919  #ifdef SUPPORT_UCP    if (c == OP_ALT)
   case PT_CLIST:  
   p = PRIV(ucd_caseless_sets) + prop->caseset;  
   for (;;)  
2920      {      {
2921      if (c < *p) return !negated;      do code += GET(code, 1); while (*code == OP_ALT);
2922      if (c == *p++) return negated;      c = *code;
2923      }      }
   break;  /* Control never reaches here */  
 #endif  
   }  
2924    
2925  return FALSE;    switch(c)
2926  }      {
2927  #endif  /* SUPPORT_UCP */      case OP_END:
2928        /* 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        return base_list[1] != 0;
2933    
2934        case OP_KET:
2935        /* 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  /*************************************************      if (base_list[1] == 0) return FALSE;
2941  *     Check if auto-possessifying is possible    *      code += PRIV(OP_lengths)[c];
2942  *************************************************/      continue;
2943        }
2944    
2945  /* This function is called for unlimited repeats of certain items, to see    /* Check for a supported opcode, and load its properties. */
 whether the next thing could possibly match the repeated item. If not, it makes  
 sense to automatically possessify the repeated item.  
2946    
2947  Arguments:    code = get_chr_property_list(code, utf, cd->fcc, list);
2948    previous      pointer to the repeated opcode    if (code == NULL) return FALSE;    /* Unsupported */
   utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode  
   ptr           next character in pattern  
   options       options bits  
   cd            contains pointers to tables etc.  
2949    
2950  Returns:        TRUE if possessifying is wanted    /* 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  static BOOL    if (base_list[0] == OP_CHAR)
2954  check_auto_possessive(const pcre_uchar *previous, BOOL utf,      {
2955    const pcre_uchar *ptr, int options, compile_data *cd)      chr_ptr = base_list + 2;
2956  {      list_ptr = list;
2957  pcre_uint32 c = NOTACHAR;      }
2958  pcre_uint32 next;    else if (list[0] == OP_CHAR)
2959  int escape;      {
2960  pcre_uchar op_code = *previous++;      chr_ptr = list + 2;
2961        list_ptr = base_list;
2962        }
2963    
2964  /* Skip whitespace and comments in extended mode */    /* Some property combinations also acceptable. Unicode property opcodes are
2965      processed specially; the rest can be handled with a lookup table. */
2966    
2967  if ((options & PCRE_EXTENDED) != 0)    else
   {  
   for (;;)  
2968      {      {
2969      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      pcre_uint32 leftop, rightop;
2970      if (*ptr == CHAR_NUMBER_SIGN)  
2971        if (list[1] != 0) return FALSE;   /* Must match at least one character */
2972        leftop = base_list[0];
2973        rightop = list[0];
2974    
2975    #ifdef SUPPORT_UCP
2976        if (leftop == OP_PROP || leftop == OP_NOTPROP)
2977        {        {
2978        ptr++;        if (rightop == OP_EOD) return TRUE;
2979        while (*ptr != CHAR_NULL)        if (rightop == OP_PROP || rightop == OP_NOTPROP)
2980          {          {
2981          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          int n;
2982          ptr++;          const pcre_uint8 *p;
2983  #ifdef SUPPORT_UTF          BOOL same = leftop == rightop;
2984          if (utf) FORWARDCHAR(ptr);          BOOL lisprop = leftop == OP_PROP;
2985  #endif          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        }        }
     else break;  
     }  
   }  
3069    
3070  /* If the next item is one that we can handle, get its value. A non-negative      else
3071  value is a character, a negative value is an escape value. */  #endif  /* SUPPORT_UCP */
3072    
3073  if (*ptr == CHAR_BACKSLASH)      return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3074    {             rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3075    int temperrorcode = 0;             autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3076    escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options,      }
     FALSE);  
   if (temperrorcode != 0) return FALSE;  
   ptr++;    /* Point after the escape sequence */  
   }  
 else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)  
   {  
   escape = 0;  
 #ifdef SUPPORT_UTF  
   if (utf) { GETCHARINC(next, ptr); } else  
 #endif  
   next = *ptr++;  
   }  
 else return FALSE;  
3077    
3078  /* Skip whitespace and comments in extended mode */    /* Control reaches here only if one of the items is a small character list.
3079      All characters are checked against the other side. */
3080    
3081  if ((options & PCRE_EXTENDED) != 0)    do
   {  
   for (;;)  
3082      {      {
3083      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      chr = *chr_ptr;
3084      if (*ptr == CHAR_NUMBER_SIGN)  
3085        switch(list_ptr[0])
3086        {        {
3087        ptr++;        case OP_CHAR:
3088        while (*ptr != CHAR_NULL)        ochr_ptr = list_ptr + 2;
3089          do
3090          {          {
3091          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (chr == *ochr_ptr) return FALSE;
3092          ptr++;          ochr_ptr++;
 #ifdef SUPPORT_UTF  
         if (utf) FORWARDCHAR(ptr);  
 #endif  
3093          }          }
3094        }        while(*ochr_ptr != NOTACHAR);
3095      else break;        break;
     }  
   }  
3096    
3097  /* If the next thing is itself optional, we have to give up. */        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  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||        /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3110    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)        set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
     return FALSE;  
3111    
3112  /* If the previous item is a character, get its value. */        case OP_DIGIT:
3113          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3114          break;
3115    
3116  if (op_code == OP_CHAR || op_code == OP_CHARI ||        case OP_NOT_DIGIT:
3117      op_code == OP_NOT || op_code == OP_NOTI)        if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3118    {        break;
 #ifdef SUPPORT_UTF  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   }  
3119    
3120  /* Now compare the next item with the previous opcode. First, handle cases when        case OP_WHITESPACE:
3121  the next item is a character. */        if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3122          break;
3123    
3124  if (escape == 0)        case OP_NOT_WHITESPACE:
3125    {        if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3126    /* 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. */  
3127    
3128  #ifdef SUPPORT_UCP        case OP_WORDCHAR:
3129    if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)        if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3130      {        break;
     unsigned int ocs = UCD_CASESET(next);  
     if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);  
     }  
 #endif  
3131    
3132    switch(op_code)        case OP_NOT_WORDCHAR:
3133      {        if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3134      case OP_CHAR:        break;
     return c != next;  
3135    
3136      /* For CHARI (caseless character) we must check the other case. If we have        case OP_HSPACE:
3137      Unicode property support, we can use it to test the other case of        switch(chr)
3138      high-valued characters. We know that next can have only one other case,          {
3139      because multi-other-case characters are dealt with above. */          HSPACE_CASES: return FALSE;
3140            default: break;
3141            }
3142          break;
3143    
3144      case OP_CHARI:        case OP_NOT_HSPACE:
3145      if (c == next) return FALSE;        switch(chr)
3146  #ifdef SUPPORT_UTF          {
3147      if (utf)          HSPACE_CASES: break;
3148        {          default: return FALSE;
3149        pcre_uint32 othercase;          }
3150        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 */  
3151    
3152      case OP_NOT:        case OP_ANYNL:
3153      return c == next;        case OP_VSPACE:
3154          switch(chr)
3155            {
3156            VSPACE_CASES: return FALSE;
3157            default: break;
3158            }
3159          break;
3160    
3161      case OP_NOTI:        case OP_NOT_VSPACE:
3162      if (c == next) return TRUE;        switch(chr)
3163  #ifdef SUPPORT_UTF          {
3164      if (utf)          VSPACE_CASES: break;
3165            default: return FALSE;
3166            }
3167          break;
3168    
3169          case OP_DOLL:
3170          case OP_EODN:
3171          switch (chr)
3172            {
3173            case CHAR_CR:
3174            case CHAR_LF:
3175            case CHAR_VT:
3176            case CHAR_FF:
3177            case CHAR_NEL:
3178    #ifndef EBCDIC
3179            case 0x2028:
3180            case 0x2029:
3181    #endif  /* Not EBCDIC */
3182            return FALSE;
3183            }
3184          break;
3185    
3186          case OP_EOD:    /* Can always possessify before \z */
3187          break;
3188    
3189          case OP_PROP:
3190          case OP_NOTPROP:
3191          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3192                list_ptr[0] == OP_NOTPROP))
3193            return FALSE;
3194          break;
3195    
3196          /* The class comparisons work only when the class is the second item
3197          of the pair, because there are at present no possessive forms of the
3198          class opcodes. Note also that the "code" variable that is used below
3199          points after the second item, and that the pointer for the first item
3200          is not available, so even if there were possessive forms of the class
3201          opcodes, the correct comparison could not be done. */
3202    
3203          case OP_NCLASS:
3204          if (chr > 255) return FALSE;
3205          /* Fall through */
3206    
3207          case OP_CLASS:
3208          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3209          if (chr > 255) break;
3210          if ((((pcre_uint8 *)(code - list_ptr[2] + 1))[chr >> 3] & (1 << (chr & 7))) != 0)
3211            return FALSE;
3212          break;
3213    
3214    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3215          case OP_XCLASS:
3216          if (list_ptr != list) return FALSE;   /* Class is first opcode */
3217          if (PRIV(xclass)(chr, code - list_ptr[2] + 1 + LINK_SIZE, utf))
3218            return FALSE;
3219          break;
3220    #endif
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;
3236    }
3237    
3238    
3239    
3240    /*************************************************
3241    *    Scan compiled regex for auto-possession     *
3242    *************************************************/
3243    
3244    /* Replaces single character iterations with their possessive alternatives
3245    if appropriate. This function modifies the compiled opcode!
3246    
3247    Arguments:
3248      code        points to start of the byte code
3249      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3250      cd          static compile data
3251    
3252    Returns:      nothing
3253    */
3254    
3255    static void
3256    auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
3257    {
3258    register pcre_uchar c;
3259    const pcre_uchar *end;
3260    pcre_uint32 list[8];
3261    
3262    for (;;)
3263      {
3264      c = *code;
3265    
3266      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3267        {
3268        c -= get_repeat_base(c) - OP_STAR;
3269        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        pcre_uint32 othercase;        switch(c)
3276        if (next < 128) othercase = cd->fcc[next]; else          {
3277  #ifdef SUPPORT_UCP          case OP_STAR:
3278        othercase = UCD_OTHERCASE(next);          *code += OP_POSSTAR - OP_STAR;
3279            break;
3280    
3281            case OP_MINSTAR:
3282            *code += OP_POSSTAR - OP_MINSTAR;
3283            break;
3284    
3285            case OP_PLUS:
3286            *code += OP_POSPLUS - OP_PLUS;
3287            break;
3288    
3289            case OP_MINPLUS:
3290            *code += OP_POSPLUS - OP_MINPLUS;
3291            break;
3292    
3293            case OP_QUERY:
3294            *code += OP_POSQUERY - OP_QUERY;
3295            break;
3296    
3297            case OP_MINQUERY:
3298            *code += OP_POSQUERY - OP_MINQUERY;
3299            break;
3300    
3301            case OP_UPTO:
3302            *code += OP_POSUPTO - OP_UPTO;
3303            break;
3304    
3305            case OP_MINUPTO:
3306            *code += OP_MINUPTO - OP_UPTO;
3307            break;
3308            }
3309          }
3310        c = *code;
3311        }
3312    
3313      switch(c)
3314        {
3315        case OP_END:
3316        return;
3317    
3318        case OP_TYPESTAR:
3319        case OP_TYPEMINSTAR:
3320        case OP_TYPEPLUS:
3321        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_TYPEUPTO:
3331        case OP_TYPEMINUPTO:
3332        case OP_TYPEEXACT:
3333        case OP_TYPEPOSUPTO:
3334        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3335          code += 2;
3336        break;
3337    
3338        case OP_XCLASS:
3339        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:
3365        case OP_STAR:
3366        case OP_MINSTAR:
3367        case OP_PLUS:
3368        case OP_MINPLUS:
3369        case OP_QUERY:
3370        case OP_MINQUERY:
3371        case OP_UPTO:
3372        case OP_MINUPTO:
3373        case OP_EXACT:
3374        case OP_POSSTAR:
3375        case OP_POSPLUS:
3376        case OP_POSQUERY:
3377        case OP_POSUPTO:
3378        case OP_STARI:
3379        case OP_MINSTARI:
3380        case OP_PLUSI:
3381        case OP_MINPLUSI:
3382        case OP_QUERYI:
3383        case OP_MINQUERYI:
3384        case OP_UPTOI:
3385        case OP_MINUPTOI:
3386        case OP_EXACTI:
3387        case OP_POSSTARI:
3388        case OP_POSPLUSI:
3389        case OP_POSQUERYI:
3390        case OP_POSUPTOI:
3391        case OP_NOTSTAR:
3392        case OP_NOTMINSTAR:
3393        case OP_NOTPLUS:
3394        case OP_NOTMINPLUS:
3395        case OP_NOTQUERY:
3396        case OP_NOTMINQUERY:
3397        case OP_NOTUPTO:
3398        case OP_NOTMINUPTO:
3399        case OP_NOTEXACT:
3400        case OP_NOTPOSSTAR:
3401        case OP_NOTPOSPLUS:
3402        case OP_NOTPOSQUERY:
3403        case OP_NOTPOSUPTO:
3404        case OP_NOTSTARI:
3405        case OP_NOTMINSTARI:
3406        case OP_NOTPLUSI:
3407        case OP_NOTMINPLUSI:
3408        case OP_NOTQUERYI:
3409        case OP_NOTMINQUERYI:
3410        case OP_NOTUPTOI:
3411        case OP_NOTMINUPTOI:
3412        case OP_NOTEXACTI:
3413        case OP_NOTPOSSTARI:
3414        case OP_NOTPOSPLUSI:
3415        case OP_NOTPOSQUERYI:
3416        case OP_NOTPOSUPTOI:
3417        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3418        break;
3419        }
3420  #else  #else
3421        othercase = NOTACHAR;    (void)(utf);  /* Keep compiler happy by referencing function argument */
3422  #endif  #endif
3423        return c == othercase;    }
3424    }
3425    
3426    
3427    
3428    /*************************************************
3429    *           Check for POSIX class syntax         *
3430    *************************************************/
3431    
3432    /* This function is called when the sequence "[:" or "[." or "[=" is
3433    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    Originally, this function only recognized a sequence of letters between the
3438    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    The problem in trying to be exactly like Perl is in the handling of escapes. We
3444    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    A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3453    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    In Perl, unescaped square brackets may also appear as part of class names. For
3458    example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3459    [: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    names.
3462    
3463    Arguments:
3464      ptr      pointer to the initial [
3465      endptr   where to return the end pointer
3466    
3467    Returns:   TRUE or FALSE
3468    */
3469    
3470    static BOOL
3471    check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3472    {
3473    pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3474    terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3475    for (++ptr; *ptr != CHAR_NULL; ptr++)
3476      {
3477      if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3478        ptr++;
3479      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3480      else
3481        {
3482        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3483          {
3484          *endptr = ptr;
3485          return TRUE;
3486        }        }
3487      else      if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3488  #endif  /* SUPPORT_UTF */           (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3489      return (c == TABLE_GET(next, cd->fcc, next));  /* Not UTF */            ptr[1] == CHAR_EQUALS_SIGN) &&
3490            check_posix_syntax(ptr, endptr))
3491          return FALSE;
3492        }
3493      }
3494    return FALSE;
3495    }
3496    
     /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.  
     When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */  
3497    
     case OP_DIGIT:  
     return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;  
3498    
     case OP_NOT_DIGIT:  
     return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;  
3499    
3500      case OP_WHITESPACE:  /*************************************************
3501      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;  *          Check POSIX class name                *
3502    *************************************************/
3503    
3504    /* 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      if (len == posix_name_lengths[yield] &&
3522        STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3523      pn += posix_name_lengths[yield] + 1;
3524      yield++;
3525      }
3526    return -1;
3527    }
3528    
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    This function has been extended with the possibility of forward references for
3546    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 OP_NOT_WHITESPACE:  Arguments:
3552      return next <= 255 && (cd->ctypes[next] & 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      case OP_WORDCHAR:  Returns:     nothing
3559      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;  */
3560    
3561      case OP_NOT_WORDCHAR:  static void
3562      return next <= 255 && (cd->ctypes[next] & ctype_word) != 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 OP_HSPACE:  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
3568      case OP_NOT_HSPACE:    {
3569      switch(next)    int offset;
3570        {    pcre_uchar *hc;
       HSPACE_CASES:  
       return op_code == OP_NOT_HSPACE;  
3571    
3572        default:    /* See if this recursion is on the forward reference list. If so, adjust the
3573        return op_code != OP_NOT_HSPACE;    reference. */
       }  
3574    
3575      case OP_ANYNL:    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
3576      case OP_VSPACE:      {
3577      case OP_NOT_VSPACE:      offset = (int)GET(hc, 0);
3578      switch(next)      if (cd->start_code + offset == ptr + 1)
3579        {        {
3580        VSPACE_CASES:        PUT(hc, 0, offset + adjust);
3581        return op_code == OP_NOT_VSPACE;        break;
   
       default:  
       return op_code != OP_NOT_VSPACE;  
3582        }        }
   
 #ifdef SUPPORT_UCP  
     case OP_PROP:  
     return check_char_prop(next, previous[0], previous[1], FALSE);  
   
     case OP_NOTPROP:  
     return check_char_prop(next, previous[0], previous[1], TRUE);  
 #endif  
   
     default:  
     return FALSE;  
3583      }      }
   }  
3584    
3585  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP    /* Otherwise, adjust the recursion offset if it's after the start of this
3586  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are    group. */
 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. */  
3587    
3588  switch(op_code)    if (hc >= cd->hwm)
   {  
   case OP_CHAR:  
   case OP_CHARI:  
   switch(escape)  
3589      {      {
3590      case ESC_d:      offset = (int)GET(ptr, 1);
3591      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
3592        }
     case ESC_D:  
     return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;  
   
     case ESC_s:  
     return c > 255 || (cd->ctypes[c] & ctype_space) == 0;  
   
     case ESC_S:  
     return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;  
3593    
3594      case ESC_w:    ptr += 1 + LINK_SIZE;
3595      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;    }
3596    }
3597    
     case ESC_W:  
     return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;  
3598    
     case ESC_h:  
     case ESC_H:  
     switch(c)  
       {  
       HSPACE_CASES:  
       return escape != ESC_h;  
3599    
3600        default:  /*************************************************
3601        return escape == ESC_h;  *        Insert an automatic callout point       *
3602        }  *************************************************/
3603    
3604      case ESC_v:  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
3605      case ESC_V:  callout points before each pattern item.
     switch(c)  
       {  
       VSPACE_CASES:  
       return escape != ESC_v;  
3606    
3607        default:  Arguments:
3608        return escape == ESC_v;    code           current code pointer
3609        }    ptr            current pattern pointer
3610      cd             pointers to tables etc
3611    
3612      /* When PCRE_UCP is set, these values get generated for \d etc. Find  Returns:         new code pointer
3613      their substitutions and process them. The result will always be either  */
     ESC_p or ESC_P. Then fall through to process those values. */  
3614    
3615  #ifdef SUPPORT_UCP  static pcre_uchar *
3616      case ESC_du:  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
3617      case ESC_DU:  {
3618      case ESC_wu:  *code++ = OP_CALLOUT;
3619      case ESC_WU:  *code++ = 255;
3620      case ESC_su:  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
3621      case ESC_SU:  PUT(code, LINK_SIZE, 0);                       /* Default length */
3622        {  return code + 2 * LINK_SIZE;
3623        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 */  
3624    
     case ESC_p:  
     case ESC_P:  
       {  
       unsigned int ptype = 0, pdata = 0;  
       int errorcodeptr;  
       BOOL negated;  
3625    
       ptr--;      /* Make ptr point at the p or P */  
       if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr))  
         return FALSE;  
       ptr++;      /* Point past the final curly ket */  
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 4640  for (;; ptr++) Line 5140  for (;; ptr++)
5140            }            }
5141          }          }
5142    
       /* 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;  
         }  
   
5143        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
5144        }        }
5145    
# Line 4670  for (;; ptr++) Line 5157  for (;; ptr++)
5157        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
5158        c = *previous;        c = *previous;
5159    
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
   
5160        OUTPUT_SINGLE_REPEAT:        OUTPUT_SINGLE_REPEAT:
5161        if (*previous == OP_PROP || *previous == OP_NOTPROP)        if (*previous == OP_PROP || *previous == OP_NOTPROP)
5162          {          {
# Line 4826  for (;; ptr++) Line 5305  for (;; ptr++)
5305      /* If previous was a character class or a back reference, we put the repeat      /* If previous was a character class or a back reference, we put the repeat
5306      stuff after it, but just skip the item if the repeat was {0,0}. */      stuff after it, but just skip the item if the repeat was {0,0}. */
5307    
5308      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
              *previous == OP_NCLASS ||  
5309  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5310               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
5311  #endif  #endif
5312               *previous == OP_REF ||               *previous == OP_REF   || *previous == OP_REFI ||
5313               *previous == OP_REFI)               *previous == OP_DNREF || *previous == OP_DNREFI)
5314        {        {
5315        if (repeat_max == 0)        if (repeat_max == 0)
5316          {          {
# Line 5837  for (;; ptr++) Line 6315  for (;; ptr++)
6315    
6316          /* In the pre-compile phase, do a syntax check, remember the longest          /* In the pre-compile phase, do a syntax check, remember the longest
6317          name, and then remember the group in a vector, expanding it if          name, and then remember the group in a vector, expanding it if
6318          necessary. Duplicates for the same number are skipped; other duplicates          necessary. Duplicates for the same number are skipped; other duplicates
6319          are checked for validity. In the actual compile, there is nothing to          are checked for validity. In the actual compile, there is nothing to
6320          do. */          do. */
6321    
6322          if (lengthptr != NULL)          if (lengthptr != NULL)
6323            {            {
6324            named_group *ng;            named_group *ng;
6325            pcre_uint32 number = cd->bracount + 1;            pcre_uint32 number = cd->bracount + 1;
6326    
6327            if (*ptr != (pcre_uchar)terminator)            if (*ptr != (pcre_uchar)terminator)
6328              {              {
6329              *errorcodeptr = ERR42;              *errorcodeptr = ERR42;
6330              goto FAILED;              goto FAILED;
6331              }              }
6332    
6333            if (cd->names_found >= MAX_NAME_COUNT)            if (cd->names_found >= MAX_NAME_COUNT)
6334              {              {
6335              *errorcodeptr = ERR49;              *errorcodeptr = ERR49;
6336              goto FAILED;              goto FAILED;
6337              }              }
6338    
6339            if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)            if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
6340              {              {
6341              cd->name_entry_size = namelen + IMM2_SIZE + 1;              cd->name_entry_size = namelen + IMM2_SIZE + 1;
# Line 5867  for (;; ptr++) Line 6345  for (;; ptr++)
6345                goto FAILED;                goto FAILED;
6346                }                }
6347              }              }
6348    
6349            /* Scan the list to check for duplicates. For duplicate names, if the            /* Scan the list to check for duplicates. For duplicate names, if the
6350            number is the same, break the loop, which causes the name to be            number is the same, break the loop, which causes the name to be
6351            discarded; otherwise, if DUPNAMES is not set, give an error.            discarded; otherwise, if DUPNAMES is not set, give an error.
6352            If it is set, allow the name with a different number, but continue            If it is set, allow the name with a different number, but continue
6353            scanning in case this is a duplicate with the same number. For            scanning in case this is a duplicate with the same number. For
6354            non-duplicate names, give an error if the number is duplicated. */            non-duplicate names, give an error if the number is duplicated. */
6355    
6356            ng = cd->named_groups;            ng = cd->named_groups;
6357            for (i = 0; i < cd->names_found; i++, ng++)            for (i = 0; i < cd->names_found; i++, ng++)
6358              {              {
6359              if (namelen == ng->length &&              if (namelen == ng->length &&
6360                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)
6361                {                {
6362                if (ng->number == number) break;                if (ng->number == number) break;
6363                if ((options & PCRE_DUPNAMES) == 0)                if ((options & PCRE_DUPNAMES) == 0)
6364                  {                  {
6365                  *errorcodeptr = ERR43;                  *errorcodeptr = ERR43;
6366                  goto FAILED;                  goto FAILED;
6367                  }                  }
6368                }                cd->dupnames = TRUE;  /* Duplicate names exist */
6369                  }
6370              else if (ng->number == number)              else if (ng->number == number)
6371                {                {
6372                *errorcodeptr = ERR65;                *errorcodeptr = ERR65;
6373                goto FAILED;                goto FAILED;
6374                }                }
6375              }              }
6376    
6377            if (i >= cd->names_found)     /* Not a duplicate with same number */            if (i >= cd->names_found)     /* Not a duplicate with same number */
6378              {              {
6379              /* Increase the list size if necessary */              /* Increase the list size if necessary */
6380    
6381              if (cd->names_found >= cd->named_group_list_size)              if (cd->names_found >= cd->named_group_list_size)
6382                {                {
6383                int newsize = cd->named_group_list_size * 2;                int newsize = cd->named_group_list_size * 2;
6384                named_group *newspace = (PUBL(malloc))                named_group *newspace = (PUBL(malloc))
6385                  (newsize * sizeof(named_group));                  (newsize * sizeof(named_group));
6386    
6387                if (newspace == NULL)                if (newspace == NULL)
6388                  {                  {
6389                  *errorcodeptr = ERR21;                  *errorcodeptr = ERR21;
6390                  goto FAILED;                  goto FAILED;
6391                  }                  }
6392    
6393                memcpy(newspace, cd->named_groups,                memcpy(newspace, cd->named_groups,
6394                  cd->named_group_list_size * sizeof(named_group));                  cd->named_group_list_size * sizeof(named_group));
6395                if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)                if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
6396                  (PUBL(free))((void *)cd->named_groups);                  (PUBL(free))((void *)cd->named_groups);
6397                cd->named_groups = newspace;                cd->named_groups = newspace;
6398                cd->named_group_list_size = newsize;                cd->named_group_list_size = newsize;
6399                }                }
6400    
6401              cd->named_groups[cd->names_found].name = name;              cd->named_groups[cd->names_found].name = name;
6402              cd->named_groups[cd->names_found].length = namelen;              cd->named_groups[cd->names_found].length = namelen;
6403              cd->named_groups[cd->names_found].number = number;              cd->named_groups[cd->names_found].number = number;
# Line 5957  for (;; ptr++) Line 6436  for (;; ptr++)
6436          if (lengthptr != NULL)          if (lengthptr != NULL)
6437            {            {
6438            named_group *ng;            named_group *ng;
6439    
6440            if (namelen == 0)            if (namelen == 0)
6441              {              {
6442              *errorcodeptr = ERR62;              *errorcodeptr = ERR62;
# Line 5975  for (;; ptr++) Line 6454  for (;; ptr++)
6454              }              }
6455    
6456            /* The name table does not exist in the first pass; instead we must            /* The name table does not exist in the first pass; instead we must
6457            scan the list of names encountered so far in order to get the            scan the list of names encountered so far in order to get the
6458            number. The number may be negative if it is for a name that may be            number. If the name is not found, set the value to 0 for a forward
6459            duplicated. If the name is not found, set the value to 0 for a            reference. */
           forward reference. */  
6460    
6461            ng = cd->named_groups;            ng = cd->named_groups;
6462            for (i = 0; i < cd->names_found; i++, ng++)            for (i = 0; i < cd->names_found; i++, ng++)
# Line 5986  for (;; ptr++) Line 6464  for (;; ptr++)
6464              if (namelen == ng->length &&              if (namelen == ng->length &&
6465                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)                  STRNCMP_UC_UC(name, ng->name, namelen) == 0)
6466                break;                break;
6467              }              }
6468            recno = (i < cd->names_found)? ng->number : 0;            recno = (i < cd->names_found)? ng->number : 0;
6469    
6470              /* Count named back references. */
6471    
6472              if (!is_recurse) cd->namedrefcount++;
6473            }            }
6474    
6475          /* In the real compile, search the name table. We check the name          /* In the real compile, search the name table. We check the name
# Line 6010  for (;; ptr++) Line 6492  for (;; ptr++)
6492              {              {
6493              recno = GET2(slot, 0);              recno = GET2(slot, 0);
6494              }              }
6495            else            else
6496              {              {
6497              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
6498              goto FAILED;              goto FAILED;
6499              }              }
6500            }            }
6501    
6502          /* 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
6503          recursion or backreferences. */          handles numerical recursion. */
6504    
6505          if (is_recurse) goto HANDLE_RECURSION;          if (is_recurse) goto HANDLE_RECURSION;
6506            else goto HANDLE_REFERENCE;  
6507            /* In the second pass we must see if the name is duplicated. If so, we
6508            generate a different opcode. */
6509    
6510            if (lengthptr == NULL && cd->dupnames)
6511              {
6512              int count = 1;
6513              unsigned int index = i;
6514              pcre_uchar *cslot = slot + cd->name_entry_size;
6515    
6516              for (i++; i < cd->names_found; i++)
6517                {
6518                if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break;
6519                count++;
6520                cslot += cd->name_entry_size;
6521                }
6522    
6523              if (count > 1)
6524                {
6525                if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
6526                previous = code;
6527                *code++ = ((options & PCRE_CASELESS) != 0)? OP_DNREFI : OP_DNREF;
6528                PUT2INC(code, 0, index);
6529                PUT2INC(code, 0, count);
6530    
6531                /* Process each potentially referenced group. */
6532    
6533                for (; slot < cslot; slot += cd->name_entry_size)
6534                  {
6535                  open_capitem *oc;
6536                  recno = GET2(slot, 0);
6537                  cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6538                  if (recno > cd->top_backref) cd->top_backref = recno;
6539    
6540                  /* Check to see if this back reference is recursive, that it, it
6541                  is inside the group that it references. A flag is set so that the
6542                  group can be made atomic. */
6543    
6544                  for (oc = cd->open_caps; oc != NULL; oc = oc->next)
6545                    {
6546                    if (oc->number == recno)
6547                      {
6548                      oc->flag = TRUE;
6549                      break;
6550                      }
6551                    }
6552                  }
6553    
6554                continue;  /* End of back ref handling */
6555                }
6556              }
6557    
6558            /* First pass, or a non-duplicated name. */
6559    
6560            goto HANDLE_REFERENCE;
6561    
6562    
6563          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
# Line 6604  for (;; ptr++) Line 7140  for (;; ptr++)
7140          open_capitem *oc;          open_capitem *oc;
7141          recno = -escape;          recno = -escape;
7142    
7143          HANDLE_REFERENCE:    /* Come here from named backref handling */          /* Come here from named backref handling when the reference is to a
7144            single group (i.e. not to a duplicated name. */
7145    
7146            HANDLE_REFERENCE:
7147          if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;          if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
7148          previous = code;          previous = code;
7149          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
# Line 7469  return c; Line 8008  return c;
8008  *************************************************/  *************************************************/
8009    
8010  /* This function is called between compiling passes to add an entry to the  /* This function is called between compiling passes to add an entry to the
8011  name/number table, maintaining alphabetical order. Checking for permitted  name/number table, maintaining alphabetical order. Checking for permitted
8012  and forbidden duplicates has already been done.  and forbidden duplicates has already been done.
8013    
8014  Arguments:  Arguments:
8015    cd           the compile data block    cd           the compile data block
8016    name         the name to add    name         the name to add
8017    length       the length of the name    length       the length of the name
8018    groupno      the group number    groupno      the group number
8019    
8020  Returns:       nothing  Returns:       nothing
8021  */  */
8022    
8023  static void  static void
8024  add_name(compile_data *cd, const pcre_uchar *name, int length,  add_name(compile_data *cd, const pcre_uchar *name, int length,
8025    unsigned int groupno)    unsigned int groupno)
8026  {  {
8027  int i;  int i;
# Line 7491  pcre_uchar *slot = cd->name_table; Line 8030  pcre_uchar *slot = cd->name_table;
8030  for (i = 0; i < cd->names_found; i++)  for (i = 0; i < cd->names_found; i++)
8031    {    {
8032    int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(length));    int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(length));
8033    if (crc == 0 && slot[IMM2_SIZE+length] != 0)    if (crc == 0 && slot[IMM2_SIZE+length] != 0)
8034      crc = -1; /* Current name is a substring */      crc = -1; /* Current name is a substring */
8035    
8036    /* Make space in the table and break the loop for an earlier name. For a    /* Make space in the table and break the loop for an earlier name. For a
# Line 7606  new memory is obtained from malloc(). */ Line 8145  new memory is obtained from malloc(). */
8145    
8146  pcre_uchar cworkspace[COMPILE_WORK_SIZE];  pcre_uchar cworkspace[COMPILE_WORK_SIZE];
8147    
8148  /* This vector is used for remembering name groups during the pre-compile. In a  /* This vector is used for remembering name groups during the pre-compile. In a
8149  similar way to cworkspace, it can be expanded using malloc() if necessary. */  similar way to cworkspace, it can be expanded using malloc() if necessary. */
8150    
8151  named_group named_groups[NAMED_GROUP_LIST_SIZE];  named_group named_groups[NAMED_GROUP_LIST_SIZE];
# Line 7873  cd->bracount = cd->final_bracount = 0; Line 8412  cd->bracount = cd->final_bracount = 0;
8412  cd->names_found = 0;  cd->names_found = 0;
8413  cd->name_entry_size = 0;  cd->name_entry_size = 0;
8414  cd->name_table = NULL;  cd->name_table = NULL;
8415    cd->dupnames = FALSE;
8416    cd->namedrefcount = 0;
8417  cd->start_code = cworkspace;  cd->start_code = cworkspace;
8418  cd->hwm = cworkspace;  cd->hwm = cworkspace;
8419  cd->start_workspace = cworkspace;  cd->start_workspace = cworkspace;
# Line 7910  if (length > MAX_PATTERN_SIZE) Line 8451  if (length > MAX_PATTERN_SIZE)
8451    goto PCRE_EARLY_ERROR_RETURN;    goto PCRE_EARLY_ERROR_RETURN;
8452    }    }
8453    
8454  /* 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
8455  externally provided function. Integer overflow should no longer be possible  name, we must allow for the possibility of named references to duplicated
8456  because nowadays we limit the maximum value of cd->names_found and  groups. These require an extra data item each. */
 cd->name_entry_size. */  
8457    
8458  size = sizeof(REAL_PCRE) + (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar);  if (cd->dupnames && cd->namedrefcount > 0)
8459  re = (REAL_PCRE *)(PUBL(malloc))(size);    length += cd->namedrefcount * IMM2_SIZE * sizeof(pcre_uchar);
8460    
8461    /* Compute the size of the data block for storing the compiled pattern. Integer
8462    overflow should no longer be possible because nowadays we limit the maximum
8463    value of cd->names_found and cd->name_entry_size. */
8464    
8465    size = sizeof(REAL_PCRE) +
8466      (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar);
8467    
8468    /* Get the memory. */
8469    
8470    re = (REAL_PCRE *)(PUBL(malloc))(size);
8471  if (re == NULL)  if (re == NULL)
8472    {    {
8473    errorcode = ERR21;    errorcode = ERR21;
# Line 7971  cd->had_pruneorskip = FALSE; Line 8521  cd->had_pruneorskip = FALSE;
8521  cd->check_lookbehind = FALSE;  cd->check_lookbehind = FALSE;
8522  cd->open_caps = NULL;  cd->open_caps = NULL;
8523    
8524  /* If any named groups were found, create the name/number table from the list  /* If any named groups were found, create the name/number table from the list
8525  created in the first pass. */  created in the first pass. */
8526    
8527  if (cd->names_found > 0)  if (cd->names_found > 0)
8528    {    {
8529    int i = cd->names_found;    int i = cd->names_found;
8530    named_group *ng = cd->named_groups;    named_group *ng = cd->named_groups;
8531    cd->names_found = 0;    cd->names_found = 0;
8532    for (; i > 0; i--, ng++)    for (; i > 0; i--, ng++)
8533      add_name(cd, ng->name, ng->length, ng->number);      add_name(cd, ng->name, ng->length, ng->number);
8534    if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)    if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
8535      (PUBL(free))((void *)cd->named_groups);      (PUBL(free))((void *)cd->named_groups);
8536    }    }
8537    
8538  /* Set up a starting, non-extracting bracket, then compile the expression. On  /* Set up a starting, non-extracting bracket, then compile the expression. On
8539  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
# Line 8060  subpattern. */ Line 8610  subpattern. */
8610    
8611  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
8612    
8613    /* Unless disabled, check whether single character iterators can be
8614    auto-possessified. The function overwrites the appropriate opcode values. */
8615    
8616    if ((options & PCRE_NO_AUTO_POSSESSIFY) == 0)
8617      auto_possessify((pcre_uchar *)codestart, utf, cd);
8618    
8619  /* If there were any lookbehind assertions that contained OP_RECURSE  /* If there were any lookbehind assertions that contained OP_RECURSE
8620  (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,
8621  because they may contain forward references. Actual recursions cannot be fixed  because they may contain forward references. Actual recursions cannot be fixed

Legend:
Removed from v.1359  
changed lines
  Added in v.1363

  ViewVC Help
Powered by ViewVC 1.1.5