/[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 602 by ph10, Wed May 25 08:29:03 2011 UTC revision 618 by ph10, Sat Jul 16 17:24:16 2011 UTC
# Line 545  static const unsigned char ebcdic_charta Line 545  static const unsigned char ebcdic_charta
545  /* Definition to allow mutual recursion */  /* Definition to allow mutual recursion */
546    
547  static BOOL  static BOOL
548    compile_regex(int, int, uschar **, const uschar **, int *, BOOL, BOOL, int,    compile_regex(int, uschar **, const uschar **, int *, BOOL, BOOL, int, int *,
549      int *, int *, branch_chain *, compile_data *, int *);      int *, branch_chain *, compile_data *, int *);
550    
551    
552    
# Line 1403  does not. Line 1403  does not.
1403    
1404  Arguments:  Arguments:
1405    code         pointer to the start of the group    code         pointer to the start of the group
   options      pointer to external options  
   optbit       the option bit whose changing is significant, or  
                  zero if none are  
1406    skipassert   TRUE if certain assertions are to be skipped    skipassert   TRUE if certain assertions are to be skipped
1407    
1408  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1409  */  */
1410    
1411  static const uschar*  static const uschar*
1412  first_significant_code(const uschar *code, int *options, int optbit,  first_significant_code(const uschar *code, BOOL skipassert)
   BOOL skipassert)  
1413  {  {
1414  for (;;)  for (;;)
1415    {    {
# Line 1468  and doing the check at the end; a flag s Line 1464  and doing the check at the end; a flag s
1464    
1465  Arguments:  Arguments:
1466    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1467    options  the compiling options    utf8     TRUE in UTF-8 mode
1468    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1469    cd       the "compile data" structure    cd       the "compile data" structure
1470    
# Line 1479  Returns:   the fixed length, Line 1475  Returns:   the fixed length,
1475  */  */
1476    
1477  static int  static int
1478  find_fixedlength(uschar *code, int options, BOOL atend, compile_data *cd)  find_fixedlength(uschar *code, BOOL utf8, BOOL atend, compile_data *cd)
1479  {  {
1480  int length = -1;  int length = -1;
1481    
# Line 1496  for (;;) Line 1492  for (;;)
1492    register int op = *cc;    register int op = *cc;
1493    switch (op)    switch (op)
1494      {      {
1495        /* We only need to continue for OP_CBRA (normal capturing bracket) and
1496        OP_BRA (normal non-capturing bracket) because the other variants of these
1497        opcodes are all concerned with unlimited repeated groups, which of course
1498        are not of fixed length. They will cause a -1 response from the default
1499        case of this switch. */
1500    
1501      case OP_CBRA:      case OP_CBRA:
1502      case OP_BRA:      case OP_BRA:
1503      case OP_ONCE:      case OP_ONCE:
1504      case OP_COND:      case OP_COND:
1505      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), utf8, atend, cd);
1506      if (d < 0) return d;      if (d < 0) return d;
1507      branchlength += d;      branchlength += d;
1508      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 1509  for (;;) Line 1511  for (;;)
1511    
1512      /* Reached end of a branch; if it's a ket it is the end of a nested      /* Reached end of a branch; if it's a ket it is the end of a nested
1513      call. If it's ALT it is an alternation in a nested call. If it is      call. If it's ALT it is an alternation in a nested call. If it is
1514      END it's the end of the outer call. All can be handled by the same code. */      END it's the end of the outer call. All can be handled by the same code.
1515        Note that we must not include the OP_KETRxxx opcodes here, because they
1516        all imply an unlimited repeat. */
1517    
1518      case OP_ALT:      case OP_ALT:
1519      case OP_KET:      case OP_KET:
     case OP_KETRMAX:  
     case OP_KETRMIN:  
1520      case OP_END:      case OP_END:
1521      if (length < 0) length = branchlength;      if (length < 0) length = branchlength;
1522        else if (length != branchlength) return -1;        else if (length != branchlength) return -1;
# Line 1532  for (;;) Line 1534  for (;;)
1534      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1535      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */
1536      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                /* Recursion */
1537      d = find_fixedlength(cs + 2, options, atend, cd);      d = find_fixedlength(cs + 2, utf8, atend, cd);
1538      if (d < 0) return d;      if (d < 0) return d;
1539      branchlength += d;      branchlength += d;
1540      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1575  for (;;) Line 1577  for (;;)
1577      case OP_CHAR:      case OP_CHAR:
1578      case OP_CHARI:      case OP_CHARI:
1579      case OP_NOT:      case OP_NOT:
1580      case OP_NOTI:      case OP_NOTI:
1581      branchlength++;      branchlength++;
1582      cc += 2;      cc += 2;
1583  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1584      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1585  #endif  #endif
1586      break;      break;
1587    
# Line 1591  for (;;) Line 1592  for (;;)
1592      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1593      cc += 4;      cc += 4;
1594  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1595      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
       cc += _pcre_utf8_table4[cc[-1] & 0x3f];  
1596  #endif  #endif
1597      break;      break;
1598    
# Line 1694  _pcre_find_bracket(const uschar *code, B Line 1694  _pcre_find_bracket(const uschar *code, B
1694  for (;;)  for (;;)
1695    {    {
1696    register int c = *code;    register int c = *code;
1697    
1698    if (c == OP_END) return NULL;    if (c == OP_END) return NULL;
1699    
1700    /* XCLASS is used for classes that cannot be represented just by a bit    /* XCLASS is used for classes that cannot be represented just by a bit
# Line 1712  for (;;) Line 1713  for (;;)
1713    
1714    /* Handle capturing bracket */    /* Handle capturing bracket */
1715    
1716    else if (c == OP_CBRA)    else if (c == OP_CBRA || c == OP_SCBRA ||
1717               c == OP_CBRAPOS || c == OP_SCBRAPOS)
1718      {      {
1719      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
1720      if (n == number) return (uschar *)code;      if (n == number) return (uschar *)code;
# Line 1954  could_be_empty_branch(const uschar *code Line 1956  could_be_empty_branch(const uschar *code
1956    compile_data *cd)    compile_data *cd)
1957  {  {
1958  register int c;  register int c;
1959  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE);
1960       code < endcode;       code < endcode;
1961       code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))       code = first_significant_code(code + _pcre_OP_lengths[c], TRUE))
1962    {    {
1963    const uschar *ccode;    const uschar *ccode;
1964    
# Line 1972  for (code = first_significant_code(code Line 1974  for (code = first_significant_code(code
1974      continue;      continue;
1975      }      }
1976    
   /* Groups with zero repeats can of course be empty; skip them. */  
   
   if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO)  
     {  
     code += _pcre_OP_lengths[c];  
     do code += GET(code, 1); while (*code == OP_ALT);  
     c = *code;  
     continue;  
     }  
   
1977    /* For a recursion/subroutine call, if its end has been reached, which    /* For a recursion/subroutine call, if its end has been reached, which
1978    implies a subroutine call, we can scan it. */    implies a subroutine call, we can scan it. */
1979    
# Line 2004  for (code = first_significant_code(code Line 1996  for (code = first_significant_code(code
1996      continue;      continue;
1997      }      }
1998    
1999      /* Groups with zero repeats can of course be empty; skip them. */
2000    
2001      if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2002          c == OP_BRAPOSZERO)
2003        {
2004        code += _pcre_OP_lengths[c];
2005        do code += GET(code, 1); while (*code == OP_ALT);
2006        c = *code;
2007        continue;
2008        }
2009    
2010      /* A nested group that is already marked as "could be empty" can just be
2011      skipped. */
2012    
2013      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2014          c == OP_SCBRA || c == OP_SCBRAPOS)
2015        {
2016        do code += GET(code, 1); while (*code == OP_ALT);
2017        c = *code;
2018        continue;
2019        }
2020    
2021    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
2022    
2023    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA  || c == OP_BRAPOS ||
2024          c == OP_CBRA || c == OP_CBRAPOS ||
2025          c == OP_ONCE || c == OP_COND)
2026      {      {
2027      BOOL empty_branch;      BOOL empty_branch;
2028      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 2135  for (code = first_significant_code(code Line 2151  for (code = first_significant_code(code
2151      case OP_KET:      case OP_KET:
2152      case OP_KETRMAX:      case OP_KETRMAX:
2153      case OP_KETRMIN:      case OP_KETRMIN:
2154        case OP_KETRPOS:
2155      case OP_ALT:      case OP_ALT:
2156      return TRUE;      return TRUE;
2157    
# Line 2682  if (next >= 0) switch(op_code) Line 2699  if (next >= 0) switch(op_code)
2699    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */
2700    
2701    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These
2702    opcodes are not used for multi-byte characters, because they are coded using    opcodes are not used for multi-byte characters, because they are coded using
2703    an XCLASS instead. */    an XCLASS instead. */
2704    
2705    case OP_NOT:    case OP_NOT:
2706    return (c = *previous) == next;    return (c = *previous) == next;
2707    
2708    case OP_NOTI:    case OP_NOTI:
2709    if ((c = *previous) == next) return TRUE;    if ((c = *previous) == next) return TRUE;
2710  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2711    if (utf8)    if (utf8)
# Line 4201  for (;; ptr++) Line 4218  for (;; ptr++)
4218      if (*previous == OP_CHAR || *previous == OP_CHARI)      if (*previous == OP_CHAR || *previous == OP_CHARI)
4219        {        {
4220        op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;        op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;
4221    
4222        /* Deal with UTF-8 characters that take up more than one byte. It's        /* Deal with UTF-8 characters that take up more than one byte. It's
4223        easier to write this out separately than try to macrify it. Use c to        easier to write this out separately than try to macrify it. Use c to
4224        hold the length of the character in bytes, plus 0x80 to flag that it's a        hold the length of the character in bytes, plus 0x80 to flag that it's a
# Line 4246  for (;; ptr++) Line 4263  for (;; ptr++)
4263      /* If previous was a single negated character ([^a] or similar), we use      /* If previous was a single negated character ([^a] or similar), we use
4264      one of the special opcodes, replacing it. The code is shared with single-      one of the special opcodes, replacing it. The code is shared with single-
4265      character repeats by setting opt_type to add a suitable offset into      character repeats by setting opt_type to add a suitable offset into
4266      repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI      repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI
4267      are currently used only for single-byte chars. */      are currently used only for single-byte chars. */
4268    
4269      else if (*previous == OP_NOT || *previous == OP_NOTI)      else if (*previous == OP_NOT || *previous == OP_NOTI)
# Line 4483  for (;; ptr++) Line 4500  for (;; ptr++)
4500        }        }
4501    
4502      /* If previous was a bracket group, we may have to replicate it in certain      /* If previous was a bracket group, we may have to replicate it in certain
4503      cases. */      cases. Note that at this point we can encounter only the "basic" BRA and
4504        KET opcodes, as this is the place where they get converted into the more
4505        special varieties. */
4506    
4507      else if (*previous == OP_BRA  || *previous == OP_CBRA ||      else if (*previous == OP_BRA  || *previous == OP_CBRA ||
4508               *previous == OP_ONCE || *previous == OP_COND)               *previous == OP_ONCE || *previous == OP_COND)
4509        {        {
4510        register int i;        register int i;
       int ketoffset = 0;  
4511        int len = (int)(code - previous);        int len = (int)(code - previous);
4512        uschar *bralink = NULL;        uschar *bralink = NULL;
4513          uschar *brazeroptr = NULL;
4514    
4515        /* Repeating a DEFINE group is pointless */        /* Repeating a DEFINE group is pointless */
4516    
4517        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
# Line 4501  for (;; ptr++) Line 4520  for (;; ptr++)
4520          goto FAILED;          goto FAILED;
4521          }          }
4522    
       /* If the maximum repeat count is unlimited, find the end of the bracket  
       by scanning through from the start, and compute the offset back to it  
       from the current code pointer. */  
   
       if (repeat_max == -1)  
         {  
         register uschar *ket = previous;  
         do ket += GET(ket, 1); while (*ket != OP_KET);  
         ketoffset = (int)(code - ket);  
         }  
   
4523        /* The case of a zero minimum is special because of the need to stick        /* The case of a zero minimum is special because of the need to stick
4524        OP_BRAZERO in front of it, and because the group appears once in the        OP_BRAZERO in front of it, and because the group appears once in the
4525        data, whereas in other cases it appears the minimum number of times. For        data, whereas in other cases it appears the minimum number of times. For
# Line 4553  for (;; ptr++) Line 4561  for (;; ptr++)
4561              *previous++ = OP_SKIPZERO;              *previous++ = OP_SKIPZERO;
4562              goto END_REPEAT;              goto END_REPEAT;
4563              }              }
4564              brazeroptr = previous;    /* Save for possessive optimizing */
4565            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
4566            }            }
4567    
# Line 4717  for (;; ptr++) Line 4726  for (;; ptr++)
4726            }            }
4727          }          }
4728    
4729        /* If the maximum is unlimited, set a repeater in the final copy. We        /* If the maximum is unlimited, set a repeater in the final copy. For
4730        can't just offset backwards from the current code point, because we        ONCE brackets, that's all we need to do.
4731        don't know if there's been an options resetting after the ket. The  
4732        correct offset was computed above.        (To be done next, after recursion adjusted)
4733          However, possessively repeated
4734          ONCE brackets can be converted into non-capturing brackets, as the
4735          behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
4736          deal with possessive ONCEs specially.
4737          (....)
4738    
4739          Otherwise, if the quantifier was possessive, we convert the BRA code to
4740          the POS form, and the KET code to KETRPOS. (It turns out to be convenient
4741          at runtime to detect this kind of subpattern at both the start and at the
4742          end.) The use of special opcodes makes it possible to reduce greatly the
4743          stack usage in pcre_exec(). If the group is preceded by OP_BRAZERO,
4744          convert this to OP_BRAPOSZERO. Then cancel the possessive flag so that
4745          the default action below, of wrapping everything inside atomic brackets,
4746          does not happen.
4747    
4748        Then, when we are doing the actual compile phase, check to see whether        Then, when we are doing the actual compile phase, check to see whether
4749        this group is a non-atomic one that could match an empty string. If so,        this group is one that could match an empty string. If so, convert the
4750        convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so        initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so that runtime
4751        that runtime checking can be done. [This check is also applied to        checking can be done. [This check is also applied to ONCE groups at
4752        atomic groups at runtime, but in a different way.] */        runtime, but in a different way.] */
4753    
4754        else        else
4755          {          {
4756          uschar *ketcode = code - ketoffset;          uschar *ketcode = code - 1 - LINK_SIZE;
4757          uschar *bracode = ketcode - GET(ketcode, 1);          uschar *bracode = ketcode - GET(ketcode, 1);
4758          *ketcode = OP_KETRMAX + repeat_type;  
4759          if (lengthptr == NULL && *bracode != OP_ONCE)  /****
4760            if (*bracode == OP_ONCE && possessive_quantifier)
4761              *bracode = OP_BRA;
4762    ****/
4763    
4764            if (*bracode == OP_ONCE)
4765              *ketcode = OP_KETRMAX + repeat_type;
4766            else
4767            {            {
4768            uschar *scode = bracode;            if (possessive_quantifier)
           do  
4769              {              {
4770              if (could_be_empty_branch(scode, ketcode, utf8, cd))              *bracode += 1;                   /* Switch to xxxPOS opcodes */
4771                *ketcode = OP_KETRPOS;
4772                if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
4773                possessive_quantifier = FALSE;
4774                }
4775              else *ketcode = OP_KETRMAX + repeat_type;
4776    
4777              if (lengthptr == NULL)
4778                {
4779                uschar *scode = bracode;
4780                do
4781                {                {
4782                *bracode += OP_SBRA - OP_BRA;                if (could_be_empty_branch(scode, ketcode, utf8, cd))
4783                break;                  {
4784                    *bracode += OP_SBRA - OP_BRA;
4785                    break;
4786                    }
4787                  scode += GET(scode, 1);
4788                }                }
4789              scode += GET(scode, 1);              while (*scode == OP_ALT);
4790              }              }
           while (*scode == OP_ALT);  
4791            }            }
4792          }          }
4793        }        }
# Line 4766  for (;; ptr++) Line 4808  for (;; ptr++)
4808        }        }
4809    
4810      /* If the character following a repeat is '+', or if certain optimization      /* If the character following a repeat is '+', or if certain optimization
4811      tests above succeeded, possessive_quantifier is TRUE. For some of the      tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
4812      simpler opcodes, there is an special alternative opcode for this. For      there are special alternative opcodes for this case. For anything else, we
4813      anything else, we wrap the entire repeated item inside OP_ONCE brackets.      wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
4814      The '+' notation is just syntactic sugar, taken from Sun's Java package,      notation is just syntactic sugar, taken from Sun's Java package, but the
4815      but the special opcodes can optimize it a bit. The repeated item starts at      special opcodes can optimize it.
4816      tempcode, not at previous, which might be the first part of a string whose  
4817      (former) last char we repeated.      Possessively repeated subpatterns have already been handled in the code
4818        just above, so possessive_quantifier is always FALSE for them at this
4819        stage.
4820    
4821        Note that the repeated item starts at tempcode, not at previous, which
4822        might be the first part of a string whose (former) last char we repeated.
4823    
4824      Possessifying an 'exact' quantifier has no effect, so we can ignore it. But      Possessifying an 'exact' quantifier has no effect, so we can ignore it. But
4825      an 'upto' may follow. We skip over an 'exact' item, and then test the      an 'upto' may follow. We skip over an 'exact' item, and then test the
# Line 4897  for (;; ptr++) Line 4944  for (;; ptr++)
4944          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
4945              strncmp((char *)name, vn, namelen) == 0)              strncmp((char *)name, vn, namelen) == 0)
4946            {            {
4947            /* Check for open captures before ACCEPT */            /* Check for open captures before ACCEPT and convert it to
4948              ASSERT_ACCEPT if in an assertion. */
4949    
4950            if (verbs[i].op == OP_ACCEPT)            if (verbs[i].op == OP_ACCEPT)
4951              {              {
4952              open_capitem *oc;              open_capitem *oc;
4953                if (arglen != 0)
4954                  {
4955                  *errorcodeptr = ERR59;
4956                  goto FAILED;
4957                  }
4958              cd->had_accept = TRUE;              cd->had_accept = TRUE;
4959              for (oc = cd->open_caps; oc != NULL; oc = oc->next)              for (oc = cd->open_caps; oc != NULL; oc = oc->next)
4960                {                {
4961                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
4962                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
4963                }                }
4964                *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
4965              }              }
4966    
4967            /* Handle the cases with/without an argument */            /* Handle other cases with/without an argument */
4968    
4969            if (arglen == 0)            else if (arglen == 0)
4970              {              {
4971              if (verbs[i].op < 0)   /* Argument is mandatory */              if (verbs[i].op < 0)   /* Argument is mandatory */
4972                {                {
# Line 5201  for (;; ptr++) Line 5255  for (;; ptr++)
5255          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5256          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */
5257          bravalue = OP_ASSERT;          bravalue = OP_ASSERT;
5258            cd->assert_depth += 1;
5259          ptr++;          ptr++;
5260          break;          break;
5261    
# Line 5215  for (;; ptr++) Line 5270  for (;; ptr++)
5270            continue;            continue;
5271            }            }
5272          bravalue = OP_ASSERT_NOT;          bravalue = OP_ASSERT_NOT;
5273            cd->assert_depth += 1;
5274          break;          break;
5275    
5276    
# Line 5224  for (;; ptr++) Line 5280  for (;; ptr++)
5280            {            {
5281            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */
5282            bravalue = OP_ASSERTBACK;            bravalue = OP_ASSERTBACK;
5283              cd->assert_depth += 1;
5284            ptr += 2;            ptr += 2;
5285            break;            break;
5286    
5287            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */
5288            bravalue = OP_ASSERTBACK_NOT;            bravalue = OP_ASSERTBACK_NOT;
5289              cd->assert_depth += 1;
5290            ptr += 2;            ptr += 2;
5291            break;            break;
5292    
# Line 5640  for (;; ptr++) Line 5698  for (;; ptr++)
5698            /* Insert the recursion/subroutine item, automatically wrapped inside            /* Insert the recursion/subroutine item, automatically wrapped inside
5699            "once" brackets. Set up a "previous group" length so that a            "once" brackets. Set up a "previous group" length so that a
5700            subsequent quantifier will work. */            subsequent quantifier will work. */
5701    
5702            *code = OP_ONCE;            *code = OP_ONCE;
5703            PUT(code, 1, 2 + 2*LINK_SIZE);            PUT(code, 1, 2 + 2*LINK_SIZE);
5704            code += 1 + LINK_SIZE;            code += 1 + LINK_SIZE;
# Line 5714  for (;; ptr++) Line 5772  for (;; ptr++)
5772          is necessary to ensure we correctly detect the start of the pattern in          is necessary to ensure we correctly detect the start of the pattern in
5773          both phases.          both phases.
5774    
5775          If we are not at the pattern start, compile code to change the ims          If we are not at the pattern start, reset the greedy defaults and the
5776          options if this setting actually changes any of them, and reset the          case value for firstbyte and reqbyte. */
         greedy defaults and the case value for firstbyte and reqbyte. */  
5777    
5778          if (*ptr == CHAR_RIGHT_PARENTHESIS)          if (*ptr == CHAR_RIGHT_PARENTHESIS)
5779            {            {
# Line 5733  for (;; ptr++) Line 5790  for (;; ptr++)
5790              }              }
5791    
5792            /* Change options at this level, and pass them back for use            /* Change options at this level, and pass them back for use
5793            in subsequent branches. When not at the start of the pattern, this            in subsequent branches. */
           information is also necessary so that a resetting item can be  
           compiled at the end of a group (if we are in a group). */  
5794    
5795            *optionsptr = options = newoptions;            *optionsptr = options = newoptions;
5796            previous = NULL;       /* This item can't be repeated */            previous = NULL;       /* This item can't be repeated */
# Line 5773  for (;; ptr++) Line 5828  for (;; ptr++)
5828    
5829      /* Process nested bracketed regex. Assertions may not be repeated, but      /* Process nested bracketed regex. Assertions may not be repeated, but
5830      other kinds can be. All their opcodes are >= OP_ONCE. We copy code into a      other kinds can be. All their opcodes are >= OP_ONCE. We copy code into a
5831      non-register variable in order to be able to pass its address because some      non-register variable (tempcode) in order to be able to pass its address
5832      compilers complain otherwise. Pass in a new setting for the ims options if      because some compilers complain otherwise. */
     they have changed. */  
5833    
5834      previous = (bravalue >= OP_ONCE)? code : NULL;      previous = (bravalue >= OP_ONCE)? code : NULL;
5835      *code = bravalue;      *code = bravalue;
# Line 5785  for (;; ptr++) Line 5839  for (;; ptr++)
5839    
5840      if (!compile_regex(      if (!compile_regex(
5841           newoptions,                   /* The complete new option state */           newoptions,                   /* The complete new option state */
          options & PCRE_IMS,           /* The previous ims option state */  
5842           &tempcode,                    /* Where to put code (updated) */           &tempcode,                    /* Where to put code (updated) */
5843           &ptr,                         /* Input pointer (updated) */           &ptr,                         /* Input pointer (updated) */
5844           errorcodeptr,                 /* Where to put an error message */           errorcodeptr,                 /* Where to put an error message */
# Line 5801  for (;; ptr++) Line 5854  for (;; ptr++)
5854             &length_prevgroup           /* Pre-compile phase */             &length_prevgroup           /* Pre-compile phase */
5855           ))           ))
5856        goto FAILED;        goto FAILED;
5857    
5858        if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
5859          cd->assert_depth -= 1;
5860    
5861      /* At the end of compiling, code is still pointing to the start of the      /* At the end of compiling, code is still pointing to the start of the
5862      group, while tempcode has been updated to point past the end of the group      group, while tempcode has been updated to point past the end of the group
# Line 5872  for (;; ptr++) Line 5928  for (;; ptr++)
5928          goto FAILED;          goto FAILED;
5929          }          }
5930        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
5931        *code++ = OP_BRA;        code++;   /* This already contains bravalue */
5932        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
5933        *code++ = OP_KET;        *code++ = OP_KET;
5934        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
# Line 6242  value of lengthptr distinguishes the two Line 6298  value of lengthptr distinguishes the two
6298    
6299  Arguments:  Arguments:
6300    options        option bits, including any changes for this subpattern    options        option bits, including any changes for this subpattern
   oldims         previous settings of ims option bits  
6301    codeptr        -> the address of the current code pointer    codeptr        -> the address of the current code pointer
6302    ptrptr         -> the address of the current pattern pointer    ptrptr         -> the address of the current pattern pointer
6303    errorcodeptr   -> pointer to error code variable    errorcodeptr   -> pointer to error code variable
# Line 6260  Returns:         TRUE on success Line 6315  Returns:         TRUE on success
6315  */  */
6316    
6317  static BOOL  static BOOL
6318  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,  compile_regex(int options, uschar **codeptr, const uschar **ptrptr,
6319    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
6320    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,
6321    int *lengthptr)    int *lengthptr)
# Line 6277  int branchfirstbyte, branchreqbyte; Line 6332  int branchfirstbyte, branchreqbyte;
6332  int length;  int length;
6333  int orig_bracount;  int orig_bracount;
6334  int max_bracount;  int max_bracount;
 int old_external_options = cd->external_options;  
6335  branch_chain bc;  branch_chain bc;
6336    
6337  bc.outer = bcptr;  bc.outer = bcptr;
# Line 6301  pre-compile phase to find out whether an Line 6355  pre-compile phase to find out whether an
6355    
6356  /* If this is a capturing subpattern, add to the chain of open capturing items  /* If this is a capturing subpattern, add to the chain of open capturing items
6357  so that we can detect them if (*ACCEPT) is encountered. This is also used to  so that we can detect them if (*ACCEPT) is encountered. This is also used to
6358  detect groups that contain recursive back references to themselves. */  detect groups that contain recursive back references to themselves. Note that
6359    only OP_CBRA need be tested here; changing this opcode to one of its variants,
6360    e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
6361    
6362  if (*code == OP_CBRA)  if (*code == OP_CBRA)
6363    {    {
# Line 6347  for (;;) Line 6403  for (;;)
6403      return FALSE;      return FALSE;
6404      }      }
6405    
   /* If the external options have changed during this branch, it means that we  
   are at the top level, and a leading option setting has been encountered. We  
   need to re-set the original option values to take account of this so that,  
   during the pre-compile phase, we know to allow for a re-set at the start of  
   subsequent branches. */  
   
   if (old_external_options != cd->external_options)  
     oldims = cd->external_options & PCRE_IMS;  
   
6406    /* Keep the highest bracket count in case (?| was used and some branch    /* Keep the highest bracket count in case (?| was used and some branch
6407    has fewer than the rest. */    has fewer than the rest. */
6408    
# Line 6416  for (;;) Line 6463  for (;;)
6463        {        {
6464        int fixed_length;        int fixed_length;
6465        *code = OP_END;        *code = OP_END;
6466        fixed_length = find_fixedlength(last_branch, options, FALSE, cd);        fixed_length = find_fixedlength(last_branch,  (options & PCRE_UTF8) != 0,
6467            FALSE, cd);
6468        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
6469        if (fixed_length == -3)        if (fixed_length == -3)
6470          {          {
# Line 6437  for (;;) Line 6485  for (;;)
6485    of offsets, with the field in the BRA item now becoming an offset to the    of offsets, with the field in the BRA item now becoming an offset to the
6486    first alternative. If there are no alternatives, it points to the end of the    first alternative. If there are no alternatives, it points to the end of the
6487    group. The length in the terminating ket is always the length of the whole    group. The length in the terminating ket is always the length of the whole
6488    bracketed item. If any of the ims options were changed inside the group,    bracketed item. Return leaving the pointer at the terminating char. */
   compile a resetting op-code following, except at the very end of the pattern.  
   Return leaving the pointer at the terminating char. */  
6489    
6490    if (*ptr != CHAR_VERTICAL_LINE)    if (*ptr != CHAR_VERTICAL_LINE)
6491      {      {
# Line 6564  of the more common cases more precisely. Line 6610  of the more common cases more precisely.
6610    
6611  Arguments:  Arguments:
6612    code           points to start of expression (the bracket)    code           points to start of expression (the bracket)
   options        points to the options setting  
6613    bracket_map    a bitmap of which brackets we are inside while testing; this    bracket_map    a bitmap of which brackets we are inside while testing; this
6614                    handles up to substring 31; after that we just have to take                    handles up to substring 31; after that we just have to take
6615                    the less precise approach                    the less precise approach
# Line 6574  Returns:     TRUE or FALSE Line 6619  Returns:     TRUE or FALSE
6619  */  */
6620    
6621  static BOOL  static BOOL
6622  is_anchored(register const uschar *code, int *options, unsigned int bracket_map,  is_anchored(register const uschar *code, unsigned int bracket_map,
6623    unsigned int backref_map)    unsigned int backref_map)
6624  {  {
6625  do {  do {
6626     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],
6627       options, PCRE_MULTILINE, FALSE);       FALSE);
6628     register int op = *scode;     register int op = *scode;
6629    
6630     /* Non-capturing brackets */     /* Non-capturing brackets */
6631    
6632     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
6633           op == OP_SBRA || op == OP_SBRAPOS)
6634       {       {
6635       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
6636       }       }
6637    
6638     /* Capturing brackets */     /* Capturing brackets */
6639    
6640     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
6641                op == OP_SCBRA || op == OP_SCBRAPOS)
6642       {       {
6643       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
6644       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
6645       if (!is_anchored(scode, options, new_map, backref_map)) return FALSE;       if (!is_anchored(scode, new_map, backref_map)) return FALSE;
6646       }       }
6647    
6648     /* Other brackets */     /* Other brackets */
6649    
6650     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)
6651       {       {
6652       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
6653       }       }
6654    
6655     /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and     /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and
# Line 6653  is_startline(const uschar *code, unsigne Line 6700  is_startline(const uschar *code, unsigne
6700  {  {
6701  do {  do {
6702     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],
6703       NULL, 0, FALSE);       FALSE);
6704     register int op = *scode;     register int op = *scode;
6705    
6706     /* If we are at the start of a conditional assertion group, *both* the     /* If we are at the start of a conditional assertion group, *both* the
# Line 6680  do { Line 6727  do {
6727         scode += 1 + LINK_SIZE;         scode += 1 + LINK_SIZE;
6728         break;         break;
6729         }         }
6730       scode = first_significant_code(scode, NULL, 0, FALSE);       scode = first_significant_code(scode, FALSE);
6731       op = *scode;       op = *scode;
6732       }       }
6733    
6734     /* Non-capturing brackets */     /* Non-capturing brackets */
6735    
6736     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
6737           op == OP_SBRA || op == OP_SBRAPOS)
6738       {       {
6739       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;
6740       }       }
6741    
6742     /* Capturing brackets */     /* Capturing brackets */
6743    
6744     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
6745                op == OP_SCBRA || op == OP_SCBRAPOS)
6746       {       {
6747       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
6748       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
# Line 6743  we return that char, otherwise -1. Line 6792  we return that char, otherwise -1.
6792    
6793  Arguments:  Arguments:
6794    code       points to start of expression (the bracket)    code       points to start of expression (the bracket)
   options    pointer to the options (used to check casing changes)  
6795    inassert   TRUE if in an assertion    inassert   TRUE if in an assertion
6796    
6797  Returns:     -1 or the fixed first char  Returns:     -1 or the fixed first char
6798  */  */
6799    
6800  static int  static int
6801  find_firstassertedchar(const uschar *code, int *options, BOOL inassert)  find_firstassertedchar(const uschar *code, BOOL inassert)
6802  {  {
6803  register int c = -1;  register int c = -1;
6804  do {  do {
6805     int d;     int d;
6806     const uschar *scode =     int xl = (*code == OP_CBRA || *code == OP_SCBRA ||
6807       first_significant_code(code + 1+LINK_SIZE, options, PCRE_CASELESS, TRUE);               *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? 2:0;
6808       const uschar *scode = first_significant_code(code + 1+LINK_SIZE + xl, TRUE);
6809     register int op = *scode;     register int op = *scode;
6810    
6811     switch(op)     switch(op)
# Line 6765  do { Line 6814  do {
6814       return -1;       return -1;
6815    
6816       case OP_BRA:       case OP_BRA:
6817         case OP_BRAPOS:
6818       case OP_CBRA:       case OP_CBRA:
6819         case OP_SCBRA:
6820         case OP_CBRAPOS:
6821         case OP_SCBRAPOS:
6822       case OP_ASSERT:       case OP_ASSERT:
6823       case OP_ONCE:       case OP_ONCE:
6824       case OP_COND:       case OP_COND:
6825       if ((d = find_firstassertedchar(scode, options, op == OP_ASSERT)) < 0)       if ((d = find_firstassertedchar(scode, op == OP_ASSERT)) < 0)
6826         return -1;         return -1;
6827       if (c < 0) c = d; else if (c != d) return -1;       if (c < 0) c = d; else if (c != d) return -1;
6828       break;       break;
6829    
6830       case OP_EXACT:       /* Fall through */       case OP_EXACT:
6831       scode += 2;       scode += 2;
6832         /* Fall through */
6833    
6834       case OP_CHAR:       case OP_CHAR:
      case OP_CHARI:  
6835       case OP_PLUS:       case OP_PLUS:
6836       case OP_MINPLUS:       case OP_MINPLUS:
6837       case OP_POSPLUS:       case OP_POSPLUS:
6838       if (!inassert) return -1;       if (!inassert) return -1;
6839       if (c < 0)       if (c < 0) c = scode[1];
6840         {         else if (c != scode[1]) return -1;
6841         c = scode[1];       break;
6842         if ((*options & PCRE_CASELESS) != 0) c |= REQ_CASELESS;  
6843         }       case OP_EXACTI:
6844       else if (c != scode[1]) return -1;       scode += 2;
6845         /* Fall through */
6846    
6847         case OP_CHARI:
6848         case OP_PLUSI:
6849         case OP_MINPLUSI:
6850         case OP_POSPLUSI:
6851         if (!inassert) return -1;
6852         if (c < 0) c = scode[1] | REQ_CASELESS;
6853           else if (c != scode[1]) return -1;
6854       break;       break;
6855       }       }
6856    
# Line 6939  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 7001  while (ptr[skipatstart] == CHAR_LEFT_PAR
7001    
7002  utf8 = (options & PCRE_UTF8) != 0;  utf8 = (options & PCRE_UTF8) != 0;
7003    
7004  /* Can't support UTF8 unless PCRE has been compiled to include the code. The  /* Can't support UTF8 unless PCRE has been compiled to include the code. The
7005  return of an error code from _pcre_valid_utf8() is a new feature, introduced in  return of an error code from _pcre_valid_utf8() is a new feature, introduced in
7006  release 8.13. The only use we make of it here is to adjust the offset value to  release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
7007  the end of the string for a short string error, for compatibility with previous  not used here. */
 versions. */  
7008    
7009  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
7010  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&
7011       (*erroroffset = _pcre_valid_utf8((USPTR)pattern, -1, &errorcode)) >= 0)       (errorcode = _pcre_valid_utf8((USPTR)pattern, -1, erroroffset)) != 0)
7012    {    {
7013    errorcode = ERR44;    errorcode = ERR44;
7014    goto PCRE_EARLY_ERROR_RETURN2;    goto PCRE_EARLY_ERROR_RETURN2;
# Line 7063  outside can help speed up starting point Line 7124  outside can help speed up starting point
7124  ptr += skipatstart;  ptr += skipatstart;
7125  code = cworkspace;  code = cworkspace;
7126  *code = OP_BRA;  *code = OP_BRA;
7127  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,  (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,
7128    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,    FALSE, 0, &firstbyte, &reqbyte, NULL, cd, &length);
   &length);  
7129  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
7130    
7131  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
# Line 7119  field; this time it's used for rememberi Line 7179  field; this time it's used for rememberi
7179  */  */
7180    
7181  cd->final_bracount = cd->bracount;  /* Save for checking forward references */  cd->final_bracount = cd->bracount;  /* Save for checking forward references */
7182    cd->assert_depth = 0;
7183  cd->bracount = 0;  cd->bracount = 0;
7184  cd->names_found = 0;  cd->names_found = 0;
7185  cd->name_table = (uschar *)re + re->name_table_offset;  cd->name_table = (uschar *)re + re->name_table_offset;
# Line 7137  of the function here. */ Line 7198  of the function here. */
7198  ptr = (const uschar *)pattern + skipatstart;  ptr = (const uschar *)pattern + skipatstart;
7199  code = (uschar *)codestart;  code = (uschar *)codestart;
7200  *code = OP_BRA;  *code = OP_BRA;
7201  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,  (void)compile_regex(re->options, &code, &ptr, &errorcode, FALSE, FALSE, 0,
7202    &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);    &firstbyte, &reqbyte, NULL, cd, NULL);
7203  re->top_bracket = cd->bracount;  re->top_bracket = cd->bracount;
7204  re->top_backref = cd->top_backref;  re->top_backref = cd->top_backref;
7205  re->flags = cd->external_flags;  re->flags = cd->external_flags;
# Line 7204  if (cd->check_lookbehind) Line 7265  if (cd->check_lookbehind)
7265        uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);        uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);
7266        int end_op = *be;        int end_op = *be;
7267        *be = OP_END;        *be = OP_END;
7268        fixed_length = find_fixedlength(cc, re->options, TRUE, cd);        fixed_length = find_fixedlength(cc, (re->options & PCRE_UTF8) != 0, TRUE,
7269            cd);
7270        *be = end_op;        *be = end_op;
7271        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
7272        if (fixed_length < 0)        if (fixed_length < 0)
# Line 7243  start with ^. and also when all branches Line 7305  start with ^. and also when all branches
7305    
7306  if ((re->options & PCRE_ANCHORED) == 0)  if ((re->options & PCRE_ANCHORED) == 0)
7307    {    {
7308    int temp_options = re->options;   /* May get changed during these scans */    if (is_anchored(codestart, 0, cd->backref_map))
   if (is_anchored(codestart, &temp_options, 0, cd->backref_map))  
7309      re->options |= PCRE_ANCHORED;      re->options |= PCRE_ANCHORED;
7310    else    else
7311      {      {
7312      if (firstbyte < 0)      if (firstbyte < 0)
7313        firstbyte = find_firstassertedchar(codestart, &temp_options, FALSE);        firstbyte = find_firstassertedchar(codestart, FALSE);
7314      if (firstbyte >= 0)   /* Remove caseless flag for non-caseable chars */      if (firstbyte >= 0)   /* Remove caseless flag for non-caseable chars */
7315        {        {
7316        int ch = firstbyte & 255;        int ch = firstbyte & 255;

Legend:
Removed from v.602  
changed lines
  Added in v.618

  ViewVC Help
Powered by ViewVC 1.1.5