/[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 457 by ph10, Sat Oct 3 16:24:08 2009 UTC revision 503 by ph10, Sun Mar 7 17:35:52 2010 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2009 University of Cambridge             Copyright (c) 1997-2010 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 53  supporting internal functions that are n Line 53  supporting internal functions that are n
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    
56  /* When DEBUG is defined, we need the pcre_printint() function, which is also  /* When PCRE_DEBUG is defined, we need the pcre_printint() function, which is
57  used by pcretest. DEBUG is not defined when building a production library. */  also used by pcretest. PCRE_DEBUG is not defined when building a production
58    library. */
59    
60  #ifdef DEBUG  #ifdef PCRE_DEBUG
61  #include "pcre_printint.src"  #include "pcre_printint.src"
62  #endif  #endif
63    
# Line 262  the number of relocations needed when a Line 263  the number of relocations needed when a
263  it is now one long string. We cannot use a table of offsets, because the  it is now one long string. We cannot use a table of offsets, because the
264  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
265  simply count through to the one we want - this isn't a performance issue  simply count through to the one we want - this isn't a performance issue
266  because these strings are used only when there is a compilation error. */  because these strings are used only when there is a compilation error.
267    
268    Each substring ends with \0 to insert a null character. This includes the final
269    substring, so that the whole string ends with \0\0, which can be detected when
270    counting through. */
271    
272  static const char error_texts[] =  static const char error_texts[] =
273    "no error\0"    "no error\0"
# Line 343  static const char error_texts[] = Line 348  static const char error_texts[] =
348    "digit expected after (?+\0"    "digit expected after (?+\0"
349    "] is an invalid data character in JavaScript compatibility mode\0"    "] is an invalid data character in JavaScript compatibility mode\0"
350    /* 65 */    /* 65 */
351    "different names for subpatterns of the same number are not allowed";    "different names for subpatterns of the same number are not allowed\0";
   
352    
353  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
354  patterns. Note that the tables in chartables are dependent on the locale, and  patterns. Note that the tables in chartables are dependent on the locale, and
# Line 502  static const char * Line 506  static const char *
506  find_error_text(int n)  find_error_text(int n)
507  {  {
508  const char *s = error_texts;  const char *s = error_texts;
509  for (; n > 0; n--) while (*s++ != 0) {};  for (; n > 0; n--)
510      {
511      while (*s++ != 0) {};
512      if (*s == 0) return "Error text not found (please report)";
513      }
514  return s;  return s;
515  }  }
516    
# Line 1102  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1110  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1110        if (name != NULL && lorn == ptr - thisname &&        if (name != NULL && lorn == ptr - thisname &&
1111            strncmp((const char *)name, (const char *)thisname, lorn) == 0)            strncmp((const char *)name, (const char *)thisname, lorn) == 0)
1112          return *count;          return *count;
1113        term++;        term++;
1114        }        }
1115      }      }
1116    }    }
# Line 1148  for (; *ptr != 0; ptr++) Line 1156  for (; *ptr != 0; ptr++)
1156            break;            break;
1157          }          }
1158        else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)        else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1159          {          {
1160          negate_class = TRUE;          negate_class = TRUE;
1161          ptr++;          ptr++;
1162          }          }
1163        else break;        else break;
1164        }        }
1165    
# Line 1317  for (;;) Line 1325  for (;;)
1325    
1326      case OP_CALLOUT:      case OP_CALLOUT:
1327      case OP_CREF:      case OP_CREF:
1328        case OP_NCREF:
1329      case OP_RREF:      case OP_RREF:
1330        case OP_NRREF:
1331      case OP_DEF:      case OP_DEF:
1332      code += _pcre_OP_lengths[*code];      code += _pcre_OP_lengths[*code];
1333      break;      break;
# Line 1338  for (;;) Line 1348  for (;;)
1348    
1349  /* Scan a branch and compute the fixed length of subject that will match it,  /* Scan a branch and compute the fixed length of subject that will match it,
1350  if the length is fixed. This is needed for dealing with backward assertions.  if the length is fixed. This is needed for dealing with backward assertions.
1351  In UTF8 mode, the result is in characters rather than bytes. The branch is  In UTF8 mode, the result is in characters rather than bytes. The branch is
1352  temporarily terminated with OP_END when this function is called.  temporarily terminated with OP_END when this function is called.
1353    
1354  This function is called when a backward assertion is encountered, so that if it  This function is called when a backward assertion is encountered, so that if it
1355  fails, the error message can point to the correct place in the pattern.  fails, the error message can point to the correct place in the pattern.
1356  However, we cannot do this when the assertion contains subroutine calls,  However, we cannot do this when the assertion contains subroutine calls,
1357  because they can be forward references. We solve this by remembering this case  because they can be forward references. We solve this by remembering this case
1358  and doing the check at the end; a flag specifies which mode we are running in.  and doing the check at the end; a flag specifies which mode we are running in.
1359    
1360  Arguments:  Arguments:
1361    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1362    options  the compiling options    options  the compiling options
1363    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1364    cd       the "compile data" structure    cd       the "compile data" structure
1365    
1366  Returns:   the fixed length,  Returns:   the fixed length,
1367               or -1 if there is no fixed length,               or -1 if there is no fixed length,
1368               or -2 if \C was encountered               or -2 if \C was encountered
1369               or -3 if an OP_RECURSE item was encountered and atend is FALSE               or -3 if an OP_RECURSE item was encountered and atend is FALSE
# Line 1403  for (;;) Line 1413  for (;;)
1413      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1414      branchlength = 0;      branchlength = 0;
1415      break;      break;
1416    
1417      /* A true recursion implies not fixed length, but a subroutine call may      /* A true recursion implies not fixed length, but a subroutine call may
1418      be OK. If the subroutine is a forward reference, we can't deal with      be OK. If the subroutine is a forward reference, we can't deal with
1419      it until the end of the pattern, so return -3. */      it until the end of the pattern, so return -3. */
1420    
1421      case OP_RECURSE:      case OP_RECURSE:
1422      if (!atend) return -3;      if (!atend) return -3;
1423      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1424      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */
1425      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                /* Recursion */
1426      d = find_fixedlength(cs + 2, options, atend, cd);      d = find_fixedlength(cs + 2, options, atend, cd);
1427      if (d < 0) return d;      if (d < 0) return d;
1428      branchlength += d;      branchlength += d;
1429      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
1430      break;      break;
1431    
1432      /* Skip over assertive subpatterns */      /* Skip over assertive subpatterns */
1433    
# Line 1432  for (;;) Line 1442  for (;;)
1442    
1443      case OP_REVERSE:      case OP_REVERSE:
1444      case OP_CREF:      case OP_CREF:
1445        case OP_NCREF:
1446      case OP_RREF:      case OP_RREF:
1447        case OP_NRREF:
1448      case OP_DEF:      case OP_DEF:
1449      case OP_OPT:      case OP_OPT:
1450      case OP_CALLOUT:      case OP_CALLOUT:
1451      case OP_SOD:      case OP_SOD:
1452      case OP_SOM:      case OP_SOM:
1453        case OP_SET_SOM:
1454      case OP_EOD:      case OP_EOD:
1455      case OP_EODN:      case OP_EODN:
1456      case OP_CIRC:      case OP_CIRC:
# Line 1455  for (;;) Line 1468  for (;;)
1468      branchlength++;      branchlength++;
1469      cc += 2;      cc += 2;
1470  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1471      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)
1472        cc += _pcre_utf8_table4[cc[-1] & 0x3f];        cc += _pcre_utf8_table4[cc[-1] & 0x3f];
1473  #endif  #endif
1474      break;      break;
# Line 1467  for (;;) Line 1480  for (;;)
1480      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1481      cc += 4;      cc += 4;
1482  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1483      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)      if ((options & PCRE_UTF8) != 0 && cc[-1] >= 0xc0)
1484        cc += _pcre_utf8_table4[cc[-1] & 0x3f];        cc += _pcre_utf8_table4[cc[-1] & 0x3f];
1485  #endif  #endif
1486      break;      break;
# Line 1552  for (;;) Line 1565  for (;;)
1565    
1566  /* This little function scans through a compiled pattern until it finds a  /* This little function scans through a compiled pattern until it finds a
1567  capturing bracket with the given number, or, if the number is negative, an  capturing bracket with the given number, or, if the number is negative, an
1568  instance of OP_REVERSE for a lookbehind. The function is global in the C sense  instance of OP_REVERSE for a lookbehind. The function is global in the C sense
1569  so that it can be called from pcre_study() when finding the minimum matching  so that it can be called from pcre_study() when finding the minimum matching
1570  length.  length.
1571    
1572  Arguments:  Arguments:
# Line 1577  for (;;) Line 1590  for (;;)
1590    the table is zero; the actual length is stored in the compiled code. */    the table is zero; the actual length is stored in the compiled code. */
1591    
1592    if (c == OP_XCLASS) code += GET(code, 1);    if (c == OP_XCLASS) code += GET(code, 1);
1593    
1594    /* Handle recursion */    /* Handle recursion */
1595    
1596    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
1597      {      {
1598      if (number < 0) return (uschar *)code;      if (number < 0) return (uschar *)code;
1599      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1600      }      }
1601    
# Line 1772  Arguments: Line 1785  Arguments:
1785    code        points to start of search    code        points to start of search
1786    endcode     points to where to stop    endcode     points to where to stop
1787    utf8        TRUE if in UTF8 mode    utf8        TRUE if in UTF8 mode
1788      cd          contains pointers to tables etc.
1789    
1790  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
1791  */  */
1792    
1793  static BOOL  static BOOL
1794  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,
1795      compile_data *cd)
1796  {  {
1797  register int c;  register int c;
1798  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);
# Line 1787  for (code = first_significant_code(code Line 1802  for (code = first_significant_code(code
1802    const uschar *ccode;    const uschar *ccode;
1803    
1804    c = *code;    c = *code;
1805    
1806    /* Skip over forward assertions; the other assertions are skipped by    /* Skip over forward assertions; the other assertions are skipped by
1807    first_significant_code() with a TRUE final argument. */    first_significant_code() with a TRUE final argument. */
1808    
# Line 1807  for (code = first_significant_code(code Line 1822  for (code = first_significant_code(code
1822      c = *code;      c = *code;
1823      continue;      continue;
1824      }      }
1825    
1826      /* For a recursion/subroutine call, if its end has been reached, which
1827      implies a subroutine call, we can scan it. */
1828    
1829      if (c == OP_RECURSE)
1830        {
1831        const uschar *scode = cd->start_code + GET(code, 1);
1832        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
1833        do
1834          {
1835          if (!could_be_empty_branch(scode, endcode, utf8, cd)) return FALSE;
1836          scode += GET(scode, 1);
1837          }
1838        while (*scode == OP_ALT);
1839        continue;
1840        }
1841    
1842    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
1843    
# Line 1826  for (code = first_significant_code(code Line 1857  for (code = first_significant_code(code
1857        empty_branch = FALSE;        empty_branch = FALSE;
1858        do        do
1859          {          {
1860          if (!empty_branch && could_be_empty_branch(code, endcode, utf8))          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))
1861            empty_branch = TRUE;            empty_branch = TRUE;
1862          code += GET(code, 1);          code += GET(code, 1);
1863          }          }
# Line 1953  for (code = first_significant_code(code Line 1984  for (code = first_significant_code(code
1984      case OP_POSQUERY:      case OP_POSQUERY:
1985      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];
1986      break;      break;
1987    
1988      case OP_UPTO:      case OP_UPTO:
1989      case OP_MINUPTO:      case OP_MINUPTO:
1990      case OP_POSUPTO:      case OP_POSUPTO:
1991      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];
1992      break;      break;
1993  #endif  #endif
1994    
1995        /* None of the remaining opcodes are required to match a character. */
1996    
1997        default:
1998        break;
1999      }      }
2000    }    }
2001    
# Line 1982  Arguments: Line 2018  Arguments:
2018    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2019    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2020    utf8        TRUE if in UTF-8 mode    utf8        TRUE if in UTF-8 mode
2021      cd          pointers to tables etc
2022    
2023  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2024  */  */
2025    
2026  static BOOL  static BOOL
2027  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,
2028    BOOL utf8)    BOOL utf8, compile_data *cd)
2029  {  {
2030  while (bcptr != NULL && bcptr->current >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2031    {    {
2032    if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))
2033        return FALSE;
2034    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2035    }    }
2036  return TRUE;  return TRUE;
# Line 2654  BOOL utf8 = FALSE; Line 2692  BOOL utf8 = FALSE;
2692  uschar *utf8_char = NULL;  uschar *utf8_char = NULL;
2693  #endif  #endif
2694    
2695  #ifdef DEBUG  #ifdef PCRE_DEBUG
2696  if (lengthptr != NULL) DPRINTF((">> start branch\n"));  if (lengthptr != NULL) DPRINTF((">> start branch\n"));
2697  #endif  #endif
2698    
# Line 2713  for (;; ptr++) Line 2751  for (;; ptr++)
2751    
2752    if (lengthptr != NULL)    if (lengthptr != NULL)
2753      {      {
2754  #ifdef DEBUG  #ifdef PCRE_DEBUG
2755      if (code > cd->hwm) cd->hwm = code;                 /* High water info */      if (code > cd->hwm) cd->hwm = code;                 /* High water info */
2756  #endif  #endif
2757      if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */      if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */
# Line 3911  we set the flag only if there is a liter Line 3949  we set the flag only if there is a liter
3949    
3950        if (repeat_max == 0) goto END_REPEAT;        if (repeat_max == 0) goto END_REPEAT;
3951    
3952        /*--------------------------------------------------------------------*/        /*--------------------------------------------------------------------*/
3953        /* This code is obsolete from release 8.00; the restriction was finally        /* This code is obsolete from release 8.00; the restriction was finally
3954        removed: */        removed: */
3955    
3956        /* All real repeats make it impossible to handle partial matching (maybe        /* All real repeats make it impossible to handle partial matching (maybe
3957        one day we will be able to remove this restriction). */        one day we will be able to remove this restriction). */
3958    
3959        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
3960        /*--------------------------------------------------------------------*/        /*--------------------------------------------------------------------*/
3961    
3962        /* Combine the op_type with the repeat_type */        /* Combine the op_type with the repeat_type */
3963    
# Line 4066  we set the flag only if there is a liter Line 4104  we set the flag only if there is a liter
4104          goto END_REPEAT;          goto END_REPEAT;
4105          }          }
4106    
4107        /*--------------------------------------------------------------------*/        /*--------------------------------------------------------------------*/
4108        /* This code is obsolete from release 8.00; the restriction was finally        /* This code is obsolete from release 8.00; the restriction was finally
4109        removed: */        removed: */
4110    
# Line 4074  we set the flag only if there is a liter Line 4112  we set the flag only if there is a liter
4112        one day we will be able to remove this restriction). */        one day we will be able to remove this restriction). */
4113    
4114        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */        /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
4115        /*--------------------------------------------------------------------*/        /*--------------------------------------------------------------------*/
4116    
4117        if (repeat_min == 0 && repeat_max == -1)        if (repeat_min == 0 && repeat_max == -1)
4118          *code++ = OP_CRSTAR + repeat_type;          *code++ = OP_CRSTAR + repeat_type;
# Line 4209  we set the flag only if there is a liter Line 4247  we set the flag only if there is a liter
4247            {            {
4248            /* In the pre-compile phase, we don't actually do the replication. We            /* In the pre-compile phase, we don't actually do the replication. We
4249            just adjust the length as if we had. Do some paranoid checks for            just adjust the length as if we had. Do some paranoid checks for
4250            potential integer overflow. */            potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
4251              integer type when available, otherwise double. */
4252    
4253            if (lengthptr != NULL)            if (lengthptr != NULL)
4254              {              {
4255              int delta = (repeat_min - 1)*length_prevgroup;              int delta = (repeat_min - 1)*length_prevgroup;
4256              if ((double)(repeat_min - 1)*(double)length_prevgroup >              if ((INT64_OR_DOUBLE)(repeat_min - 1)*
4257                                                              (double)INT_MAX ||                    (INT64_OR_DOUBLE)length_prevgroup >
4258                        (INT64_OR_DOUBLE)INT_MAX ||
4259                  OFLOW_MAX - *lengthptr < delta)                  OFLOW_MAX - *lengthptr < delta)
4260                {                {
4261                *errorcodeptr = ERR20;                *errorcodeptr = ERR20;
# Line 4261  we set the flag only if there is a liter Line 4301  we set the flag only if there is a liter
4301          just adjust the length as if we had. For each repetition we must add 1          just adjust the length as if we had. For each repetition we must add 1
4302          to the length for BRAZERO and for all but the last repetition we must          to the length for BRAZERO and for all but the last repetition we must
4303          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some          add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
4304          paranoid checks to avoid integer overflow. */          paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
4305            a 64-bit integer type when available, otherwise double. */
4306    
4307          if (lengthptr != NULL && repeat_max > 0)          if (lengthptr != NULL && repeat_max > 0)
4308            {            {
4309            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -            int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
4310                        2 - 2*LINK_SIZE;   /* Last one doesn't nest */                        2 - 2*LINK_SIZE;   /* Last one doesn't nest */
4311            if ((double)repeat_max *            if ((INT64_OR_DOUBLE)repeat_max *
4312                  (double)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)                  (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
4313                    > (double)INT_MAX ||                    > (INT64_OR_DOUBLE)INT_MAX ||
4314                OFLOW_MAX - *lengthptr < delta)                OFLOW_MAX - *lengthptr < delta)
4315              {              {
4316              *errorcodeptr = ERR20;              *errorcodeptr = ERR20;
# Line 4346  we set the flag only if there is a liter Line 4387  we set the flag only if there is a liter
4387            uschar *scode = bracode;            uschar *scode = bracode;
4388            do            do
4389              {              {
4390              if (could_be_empty_branch(scode, ketcode, utf8))              if (could_be_empty_branch(scode, ketcode, utf8, cd))
4391                {                {
4392                *bracode += OP_SBRA - OP_BRA;                *bracode += OP_SBRA - OP_BRA;
4393                break;                break;
# Line 4389  we set the flag only if there is a liter Line 4430  we set the flag only if there is a liter
4430      if (possessive_quantifier)      if (possessive_quantifier)
4431        {        {
4432        int len;        int len;
4433    
4434        if (*tempcode == OP_TYPEEXACT)        if (*tempcode == OP_TYPEEXACT)
4435          tempcode += _pcre_OP_lengths[*tempcode] +          tempcode += _pcre_OP_lengths[*tempcode] +
4436            ((tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP)? 2 : 0);            ((tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP)? 2 : 0);
4437    
4438        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
4439          {          {
4440          tempcode += _pcre_OP_lengths[*tempcode];          tempcode += _pcre_OP_lengths[*tempcode];
# Line 4401  we set the flag only if there is a liter Line 4442  we set the flag only if there is a liter
4442          if (utf8 && tempcode[-1] >= 0xc0)          if (utf8 && tempcode[-1] >= 0xc0)
4443            tempcode += _pcre_utf8_table4[tempcode[-1] & 0x3f];            tempcode += _pcre_utf8_table4[tempcode[-1] & 0x3f];
4444  #endif  #endif
4445          }          }
4446    
4447        len = code - tempcode;        len = code - tempcode;
4448        if (len > 0) switch (*tempcode)        if (len > 0) switch (*tempcode)
4449          {          {
# Line 4421  we set the flag only if there is a liter Line 4462  we set the flag only if there is a liter
4462          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
4463          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;
4464    
4465            /* Because we are moving code along, we must ensure that any
4466            pending recursive references are updated. */
4467    
4468          default:          default:
4469            *code = OP_END;
4470            adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);
4471          memmove(tempcode + 1+LINK_SIZE, tempcode, len);          memmove(tempcode + 1+LINK_SIZE, tempcode, len);
4472          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
4473          len += 1 + LINK_SIZE;          len += 1 + LINK_SIZE;
# Line 4481  we set the flag only if there is a liter Line 4527  we set the flag only if there is a liter
4527              strncmp((char *)name, vn, namelen) == 0)              strncmp((char *)name, vn, namelen) == 0)
4528            {            {
4529            /* Check for open captures before ACCEPT */            /* Check for open captures before ACCEPT */
4530    
4531            if (verbs[i].op == OP_ACCEPT)            if (verbs[i].op == OP_ACCEPT)
4532              {              {
4533              open_capitem *oc;              open_capitem *oc;
4534              cd->had_accept = TRUE;              cd->had_accept = TRUE;
4535              for (oc = cd->open_caps; oc != NULL; oc = oc->next)              for (oc = cd->open_caps; oc != NULL; oc = oc->next)
4536                {                {
4537                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
4538                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
4539                }                }
4540              }              }
4541            *code++ = verbs[i].op;            *code++ = verbs[i].op;
4542            break;            break;
4543            }            }
# Line 4654  we set the flag only if there is a liter Line 4700  we set the flag only if there is a liter
4700            }            }
4701    
4702          /* Otherwise (did not start with "+" or "-"), start by looking for the          /* Otherwise (did not start with "+" or "-"), start by looking for the
4703          name. */          name. If we find a name, add one to the opcode to change OP_CREF or
4704            OP_RREF into OP_NCREF or OP_NRREF. These behave exactly the same,
4705            except they record that the reference was originally to a name. The
4706            information is used to check duplicate names. */
4707    
4708          slot = cd->name_table;          slot = cd->name_table;
4709          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
# Line 4669  we set the flag only if there is a liter Line 4718  we set the flag only if there is a liter
4718            {            {
4719            recno = GET2(slot, 0);            recno = GET2(slot, 0);
4720            PUT2(code, 2+LINK_SIZE, recno);            PUT2(code, 2+LINK_SIZE, recno);
4721              code[1+LINK_SIZE]++;
4722            }            }
4723    
4724          /* Search the pattern for a forward reference */          /* Search the pattern for a forward reference */
# Line 4677  we set the flag only if there is a liter Line 4727  we set the flag only if there is a liter
4727                          (options & PCRE_EXTENDED) != 0)) > 0)                          (options & PCRE_EXTENDED) != 0)) > 0)
4728            {            {
4729            PUT2(code, 2+LINK_SIZE, i);            PUT2(code, 2+LINK_SIZE, i);
4730              code[1+LINK_SIZE]++;
4731            }            }
4732    
4733          /* If terminator == 0 it means that the name followed directly after          /* If terminator == 0 it means that the name followed directly after
# Line 4878  we set the flag only if there is a liter Line 4929  we set the flag only if there is a liter
4929            is because the number of names, and hence the table size, is computed            is because the number of names, and hence the table size, is computed
4930            in the pre-compile, and it affects various numbers and pointers which            in the pre-compile, and it affects various numbers and pointers which
4931            would all have to be modified, and the compiled code moved down, if            would all have to be modified, and the compiled code moved down, if
4932            duplicates with the same number were omitted from the table. This            duplicates with the same number were omitted from the table. This
4933            doesn't seem worth the hassle. However, *different* names for the            doesn't seem worth the hassle. However, *different* names for the
4934            same number are not permitted. */            same number are not permitted. */
4935    
# Line 4886  we set the flag only if there is a liter Line 4937  we set the flag only if there is a liter
4937              {              {
4938              BOOL dupname = FALSE;              BOOL dupname = FALSE;
4939              slot = cd->name_table;              slot = cd->name_table;
4940    
4941              for (i = 0; i < cd->names_found; i++)              for (i = 0; i < cd->names_found; i++)
4942                {                {
4943                int crc = memcmp(name, slot+2, namelen);                int crc = memcmp(name, slot+2, namelen);
# Line 4900  we set the flag only if there is a liter Line 4951  we set the flag only if there is a liter
4951                      *errorcodeptr = ERR43;                      *errorcodeptr = ERR43;
4952                      goto FAILED;                      goto FAILED;
4953                      }                      }
4954                    else dupname = TRUE;                    else dupname = TRUE;
4955                    }                    }
4956                  else crc = -1;      /* Current name is a substring */                  else crc = -1;      /* Current name is a substring */
4957                  }                  }
4958    
4959                /* Make space in the table and break the loop for an earlier                /* Make space in the table and break the loop for an earlier
4960                name. For a duplicate or later name, carry on. We do this for                name. For a duplicate or later name, carry on. We do this for
4961                duplicates so that in the simple case (when ?(| is not used) they                duplicates so that in the simple case (when ?(| is not used) they
4962                are in order of their numbers. */                are in order of their numbers. */
4963    
4964                if (crc < 0)                if (crc < 0)
4965                  {                  {
4966                  memmove(slot + cd->name_entry_size, slot,                  memmove(slot + cd->name_entry_size, slot,
4967                    (cd->names_found - i) * cd->name_entry_size);                    (cd->names_found - i) * cd->name_entry_size);
4968                  break;                  break;
4969                  }                  }
4970    
4971                /* Continue the loop for a later or duplicate name */                /* Continue the loop for a later or duplicate name */
4972    
4973                slot += cd->name_entry_size;                slot += cd->name_entry_size;
4974                }                }
4975    
4976              /* For non-duplicate names, check for a duplicate number before              /* For non-duplicate names, check for a duplicate number before
4977              adding the new name. */              adding the new name. */
4978    
4979              if (!dupname)              if (!dupname)
4980                {                {
4981                uschar *cslot = cd->name_table;                uschar *cslot = cd->name_table;
# Line 4936  we set the flag only if there is a liter Line 4987  we set the flag only if there is a liter
4987                      {                      {
4988                      *errorcodeptr = ERR65;                      *errorcodeptr = ERR65;
4989                      goto FAILED;                      goto FAILED;
4990                      }                      }
4991                    }                    }
4992                  else i--;                  else i--;
4993                  cslot += cd->name_entry_size;                  cslot += cd->name_entry_size;
4994                  }                  }
4995                }                }
4996    
4997              PUT2(slot, 0, cd->bracount + 1);              PUT2(slot, 0, cd->bracount + 1);
4998              memcpy(slot + 2, name, namelen);              memcpy(slot + 2, name, namelen);
# Line 5122  we set the flag only if there is a liter Line 5173  we set the flag only if there is a liter
5173            if (lengthptr == NULL)            if (lengthptr == NULL)
5174              {              {
5175              *code = OP_END;              *code = OP_END;
5176              if (recno != 0)              if (recno != 0)
5177                called = _pcre_find_bracket(cd->start_code, utf8, recno);                called = _pcre_find_bracket(cd->start_code, utf8, recno);
5178    
5179              /* Forward reference */              /* Forward reference */
# Line 5135  we set the flag only if there is a liter Line 5186  we set the flag only if there is a liter
5186                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
5187                  goto FAILED;                  goto FAILED;
5188                  }                  }
5189    
5190                  /* Fudge the value of "called" so that when it is inserted as an
5191                  offset below, what it actually inserted is the reference number
5192                  of the group. */
5193    
5194                called = cd->start_code + recno;                called = cd->start_code + recno;
5195                PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code);                PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code);
5196                }                }
# Line 5144  we set the flag only if there is a liter Line 5200  we set the flag only if there is a liter
5200              recursion that could loop for ever, and diagnose that case. */              recursion that could loop for ever, and diagnose that case. */
5201    
5202              else if (GET(called, 1) == 0 &&              else if (GET(called, 1) == 0 &&
5203                       could_be_empty(called, code, bcptr, utf8))                       could_be_empty(called, code, bcptr, utf8, cd))
5204                {                {
5205                *errorcodeptr = ERR40;                *errorcodeptr = ERR40;
5206                goto FAILED;                goto FAILED;
# Line 5239  we set the flag only if there is a liter Line 5295  we set the flag only if there is a liter
5295              {              {
5296              cd->external_options = newoptions;              cd->external_options = newoptions;
5297              }              }
5298           else            else
5299              {              {
5300              if ((options & PCRE_IMS) != (newoptions & PCRE_IMS))              if ((options & PCRE_IMS) != (newoptions & PCRE_IMS))
5301                {                {
# Line 5576  we set the flag only if there is a liter Line 5632  we set the flag only if there is a liter
5632    
5633        if (-c >= ESC_REF)        if (-c >= ESC_REF)
5634          {          {
5635            open_capitem *oc;
5636          recno = -c - ESC_REF;          recno = -c - ESC_REF;
5637    
5638          HANDLE_REFERENCE:    /* Come here from named backref handling */          HANDLE_REFERENCE:    /* Come here from named backref handling */
# Line 5585  we set the flag only if there is a liter Line 5642  we set the flag only if there is a liter
5642          PUT2INC(code, 0, recno);          PUT2INC(code, 0, recno);
5643          cd->backref_map |= (recno < 32)? (1 << recno) : 1;          cd->backref_map |= (recno < 32)? (1 << recno) : 1;
5644          if (recno > cd->top_backref) cd->top_backref = recno;          if (recno > cd->top_backref) cd->top_backref = recno;
5645    
5646            /* Check to see if this back reference is recursive, that it, it
5647            is inside the group that it references. A flag is set so that the
5648            group can be made atomic. */
5649    
5650            for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5651              {
5652              if (oc->number == recno)
5653                {
5654                oc->flag = TRUE;
5655                break;
5656                }
5657              }
5658          }          }
5659    
5660        /* So are Unicode property matches, if supported. */        /* So are Unicode property matches, if supported. */
# Line 5774  int branchfirstbyte, branchreqbyte; Line 5844  int branchfirstbyte, branchreqbyte;
5844  int length;  int length;
5845  int orig_bracount;  int orig_bracount;
5846  int max_bracount;  int max_bracount;
5847    int old_external_options = cd->external_options;
5848  branch_chain bc;  branch_chain bc;
5849    
5850  bc.outer = bcptr;  bc.outer = bcptr;
5851  bc.current = code;  bc.current_branch = code;
5852    
5853  firstbyte = reqbyte = REQ_UNSET;  firstbyte = reqbyte = REQ_UNSET;
5854    
# Line 5796  them global. It tests the value of lengt Line 5867  them global. It tests the value of lengt
5867  pre-compile phase to find out whether anything has yet been compiled or not. */  pre-compile phase to find out whether anything has yet been compiled or not. */
5868    
5869  /* 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
5870  so that we can detect them if (*ACCEPT) is encountered. */  so that we can detect them if (*ACCEPT) is encountered. This is also used to
5871    detect groups that contain recursive back references to themselves. */
5872    
5873  if (*code == OP_CBRA)  if (*code == OP_CBRA)
5874    {    {
5875    capnumber = GET2(code, 1 + LINK_SIZE);    capnumber = GET2(code, 1 + LINK_SIZE);
5876    capitem.number = capnumber;    capitem.number = capnumber;
5877    capitem.next = cd->open_caps;    capitem.next = cd->open_caps;
5878    cd->open_caps = &capitem;    capitem.flag = FALSE;
5879    }    cd->open_caps = &capitem;
5880      }
5881    
5882  /* Offset is set zero to mark that this bracket is still open */  /* Offset is set zero to mark that this bracket is still open */
5883    
# Line 5850  for (;;) Line 5923  for (;;)
5923      return FALSE;      return FALSE;
5924      }      }
5925    
5926      /* If the external options have changed during this branch, it means that we
5927      are at the top level, and a leading option setting has been encountered. We
5928      need to re-set the original option values to take account of this so that,
5929      during the pre-compile phase, we know to allow for a re-set at the start of
5930      subsequent branches. */
5931    
5932      if (old_external_options != cd->external_options)
5933        oldims = cd->external_options & PCRE_IMS;
5934    
5935    /* Keep the highest bracket count in case (?| was used and some branch    /* Keep the highest bracket count in case (?| was used and some branch
5936    has fewer than the rest. */    has fewer than the rest. */
5937    
# Line 5900  for (;;) Line 5982  for (;;)
5982    
5983      /* If lookbehind, check that this branch matches a fixed-length string, and      /* If lookbehind, check that this branch matches a fixed-length string, and
5984      put the length into the OP_REVERSE item. Temporarily mark the end of the      put the length into the OP_REVERSE item. Temporarily mark the end of the
5985      branch with OP_END. If the branch contains OP_RECURSE, the result is -3      branch with OP_END. If the branch contains OP_RECURSE, the result is -3
5986      because there may be forward references that we can't check here. Set a      because there may be forward references that we can't check here. Set a
5987      flag to cause another lookbehind check at the end. Why not do it all at the      flag to cause another lookbehind check at the end. Why not do it all at the
5988      end? Because common, erroneous checks are picked up here and the offset of      end? Because common, erroneous checks are picked up here and the offset of
5989      the problem can be shown. */      the problem can be shown. */
5990    
5991      if (lookbehind)      if (lookbehind)
# Line 5914  for (;;) Line 5996  for (;;)
5996        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
5997        if (fixed_length == -3)        if (fixed_length == -3)
5998          {          {
5999          cd->check_lookbehind = TRUE;          cd->check_lookbehind = TRUE;
6000          }          }
6001        else if (fixed_length < 0)        else if (fixed_length < 0)
6002          {          {
6003          *errorcodeptr = (fixed_length == -2)? ERR36 : ERR25;          *errorcodeptr = (fixed_length == -2)? ERR36 : ERR25;
# Line 5949  for (;;) Line 6031  for (;;)
6031          }          }
6032        while (branch_length > 0);        while (branch_length > 0);
6033        }        }
   
     /* If it was a capturing subpattern, remove it from the chain. */  
   
     if (capnumber > 0) cd->open_caps = cd->open_caps->next;  
6034    
6035      /* Fill in the ket */      /* Fill in the ket */
6036    
# Line 5960  for (;;) Line 6038  for (;;)
6038      PUT(code, 1, code - start_bracket);      PUT(code, 1, code - start_bracket);
6039      code += 1 + LINK_SIZE;      code += 1 + LINK_SIZE;
6040    
6041      /* Resetting option if needed */      /* If it was a capturing subpattern, check to see if it contained any
6042        recursive back references. If so, we must wrap it in atomic brackets.
6043        In any event, remove the block from the chain. */
6044    
6045        if (capnumber > 0)
6046          {
6047          if (cd->open_caps->flag)
6048            {
6049            memmove(start_bracket + 1 + LINK_SIZE, start_bracket,
6050              code - start_bracket);
6051            *start_bracket = OP_ONCE;
6052            code += 1 + LINK_SIZE;
6053            PUT(start_bracket, 1, code - start_bracket);
6054            *code = OP_KET;
6055            PUT(code, 1, code - start_bracket);
6056            code += 1 + LINK_SIZE;
6057            length += 2 + 2*LINK_SIZE;
6058            }
6059          cd->open_caps = cd->open_caps->next;
6060          }
6061    
6062        /* Reset options if needed. */
6063    
6064      if ((options & PCRE_IMS) != oldims && *ptr == CHAR_RIGHT_PARENTHESIS)      if ((options & PCRE_IMS) != oldims && *ptr == CHAR_RIGHT_PARENTHESIS)
6065        {        {
# Line 6009  for (;;) Line 6108  for (;;)
6108      {      {
6109      *code = OP_ALT;      *code = OP_ALT;
6110      PUT(code, 1, code - last_branch);      PUT(code, 1, code - last_branch);
6111      bc.current = last_branch = code;      bc.current_branch = last_branch = code;
6112      code += 1 + LINK_SIZE;      code += 1 + LINK_SIZE;
6113      }      }
6114    
# Line 6156  do { Line 6255  do {
6255       switch (*scode)       switch (*scode)
6256         {         {
6257         case OP_CREF:         case OP_CREF:
6258           case OP_NCREF:
6259         case OP_RREF:         case OP_RREF:
6260           case OP_NRREF:
6261         case OP_DEF:         case OP_DEF:
6262         return FALSE;         return FALSE;
6263    
# Line 6423  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 6524  while (ptr[skipatstart] == CHAR_LEFT_PAR
6524    
6525  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
6526  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&
6527       (*erroroffset = _pcre_valid_utf8((uschar *)pattern, -1)) >= 0)       (*erroroffset = _pcre_valid_utf8((USPTR)pattern, -1)) >= 0)
6528    {    {
6529    errorcode = ERR44;    errorcode = ERR44;
6530    goto PCRE_EARLY_ERROR_RETURN2;    goto PCRE_EARLY_ERROR_RETURN2;
# Line 6620  if debugging, leave the test till after Line 6721  if debugging, leave the test till after
6721    
6722  *code++ = OP_END;  *code++ = OP_END;
6723    
6724  #ifndef DEBUG  #ifndef PCRE_DEBUG
6725  if (code - codestart > length) errorcode = ERR23;  if (code - codestart > length) errorcode = ERR23;
6726  #endif  #endif
6727    
# Line 6643  subpattern. */ Line 6744  subpattern. */
6744    
6745  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
6746    
6747  /* If there were any lookbehind assertions that contained OP_RECURSE  /* If there were any lookbehind assertions that contained OP_RECURSE
6748  (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,
6749  because they may contain forward references. Actual recursions can't be fixed  because they may contain forward references. Actual recursions can't be fixed
6750  length, but subroutine calls can. It is done like this so that those without  length, but subroutine calls can. It is done like this so that those without
# Line 6654  length, and set their lengths. */ Line 6755  length, and set their lengths. */
6755  if (cd->check_lookbehind)  if (cd->check_lookbehind)
6756    {    {
6757    uschar *cc = (uschar *)codestart;    uschar *cc = (uschar *)codestart;
6758    
6759    /* Loop, searching for OP_REVERSE items, and process those that do not have    /* Loop, searching for OP_REVERSE items, and process those that do not have
6760    their length set. (Actually, it will also re-process any that have a length    their length set. (Actually, it will also re-process any that have a length
6761    of zero, but that is a pathological case, and it does no harm.) When we find    of zero, but that is a pathological case, and it does no harm.) When we find
6762    one, we temporarily terminate the branch it is in while we scan it. */    one, we temporarily terminate the branch it is in while we scan it. */
6763    
6764    for (cc = (uschar *)_pcre_find_bracket(codestart, utf8, -1);    for (cc = (uschar *)_pcre_find_bracket(codestart, utf8, -1);
6765         cc != NULL;         cc != NULL;
6766         cc = (uschar *)_pcre_find_bracket(cc, utf8, -1))         cc = (uschar *)_pcre_find_bracket(cc, utf8, -1))
6767      {      {
6768      if (GET(cc, 1) == 0)      if (GET(cc, 1) == 0)
6769        {        {
6770        int fixed_length;        int fixed_length;
6771        uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);        uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);
6772        int end_op = *be;        int end_op = *be;
6773        *be = OP_END;        *be = OP_END;
6774        fixed_length = find_fixedlength(cc, re->options, TRUE, cd);        fixed_length = find_fixedlength(cc, re->options, TRUE, cd);
6775        *be = end_op;        *be = end_op;
# Line 6676  if (cd->check_lookbehind) Line 6777  if (cd->check_lookbehind)
6777        if (fixed_length < 0)        if (fixed_length < 0)
6778          {          {
6779          errorcode = (fixed_length == -2)? ERR36 : ERR25;          errorcode = (fixed_length == -2)? ERR36 : ERR25;
6780          break;          break;
6781          }          }
6782        PUT(cc, 1, fixed_length);        PUT(cc, 1, fixed_length);
6783        }        }
6784      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
6785      }      }
6786    }    }
6787    
6788  /* Failed to compile, or error while post-processing */  /* Failed to compile, or error while post-processing */
6789    
# Line 6744  if (reqbyte >= 0 && Line 6845  if (reqbyte >= 0 &&
6845  /* Print out the compiled data if debugging is enabled. This is never the  /* Print out the compiled data if debugging is enabled. This is never the
6846  case when building a production library. */  case when building a production library. */
6847    
6848  #ifdef DEBUG  #ifdef PCRE_DEBUG
   
6849  printf("Length = %d top_bracket = %d top_backref = %d\n",  printf("Length = %d top_bracket = %d top_backref = %d\n",
6850    length, re->top_bracket, re->top_backref);    length, re->top_bracket, re->top_backref);
6851    
# Line 6782  if (code - codestart > length) Line 6882  if (code - codestart > length)
6882    if (errorcodeptr != NULL) *errorcodeptr = ERR23;    if (errorcodeptr != NULL) *errorcodeptr = ERR23;
6883    return NULL;    return NULL;
6884    }    }
6885  #endif   /* DEBUG */  #endif   /* PCRE_DEBUG */
6886    
6887  return (pcre *)re;  return (pcre *)re;
6888  }  }

Legend:
Removed from v.457  
changed lines
  Added in v.503

  ViewVC Help
Powered by ViewVC 1.1.5