/[pcre]/code/branches/pcre16/pcre_compile.c
ViewVC logotype

Diff of /code/branches/pcre16/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 545 by ph10, Wed Jun 16 10:51:15 2010 UTC revision 624 by ph10, Tue Jul 19 10:43:28 2011 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-2010 University of Cambridge             Copyright (c) 1997-2011 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 408  static const char error_texts[] = Line 408  static const char error_texts[] =
408    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
409    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
410    "this version of PCRE is not compiled with PCRE_UCP support\0"    "this version of PCRE is not compiled with PCRE_UCP support\0"
411      "\\c must be followed by an ASCII character\0"
412    ;    ;
413    
414  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 544  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 841  else Line 842  else
842      break;      break;
843    
844      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
845      This coding is ASCII-specific, but then the whole concept of \cx is      An error is given if the byte following \c is not an ASCII character. This
846        coding is ASCII-specific, but then the whole concept of \cx is
847      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */      ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
848    
849      case CHAR_c:      case CHAR_c:
# Line 851  else Line 853  else
853        *errorcodeptr = ERR2;        *errorcodeptr = ERR2;
854        break;        break;
855        }        }
856    #ifndef EBCDIC    /* ASCII/UTF-8 coding */
857  #ifndef EBCDIC  /* ASCII/UTF-8 coding */      if (c > 127)  /* Excludes all non-ASCII in either mode */
858          {
859          *errorcodeptr = ERR68;
860          break;
861          }
862      if (c >= CHAR_a && c <= CHAR_z) c -= 32;      if (c >= CHAR_a && c <= CHAR_z) c -= 32;
863      c ^= 0x40;      c ^= 0x40;
864  #else           /* EBCDIC coding */  #else             /* EBCDIC coding */
865      if (c >= CHAR_a && c <= CHAR_z) c += 64;      if (c >= CHAR_a && c <= CHAR_z) c += 64;
866      c ^= 0xC0;      c ^= 0xC0;
867  #endif  #endif
# Line 1099  top-level call starts at the beginning o Line 1105  top-level call starts at the beginning o
1105  start at a parenthesis. It scans along a pattern's text looking for capturing  start at a parenthesis. It scans along a pattern's text looking for capturing
1106  subpatterns, and counting them. If it finds a named pattern that matches the  subpatterns, and counting them. If it finds a named pattern that matches the
1107  name it is given, it returns its number. Alternatively, if the name is NULL, it  name it is given, it returns its number. Alternatively, if the name is NULL, it
1108  returns when it reaches a given numbered subpattern. We know that if (?P< is  returns when it reaches a given numbered subpattern. Recursion is used to keep
1109  encountered, the name will be terminated by '>' because that is checked in the  track of subpatterns that reset the capturing group numbers - the (?| feature.
1110  first pass. Recursion is used to keep track of subpatterns that reset the  
1111  capturing group numbers - the (?| feature.  This function was originally called only from the second pass, in which we know
1112    that if (?< or (?' or (?P< is encountered, the name will be correctly
1113    terminated because that is checked in the first pass. There is now one call to
1114    this function in the first pass, to check for a recursive back reference by
1115    name (so that we can make the whole group atomic). In this case, we need check
1116    only up to the current position in the pattern, and that is still OK because
1117    and previous occurrences will have been checked. To make this work, the test
1118    for "end of pattern" is a check against cd->end_pattern in the main loop,
1119    instead of looking for a binary zero. This means that the special first-pass
1120    call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1121    processing items within the loop are OK, because afterwards the main loop will
1122    terminate.)
1123    
1124  Arguments:  Arguments:
1125    ptrptr       address of the current character pointer (updated)    ptrptr       address of the current character pointer (updated)
# Line 1110  Arguments: Line 1127  Arguments:
1127    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1128    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1129    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1130      utf8         TRUE if we are in UTF-8 mode
1131    count        pointer to the current capturing subpattern number (updated)    count        pointer to the current capturing subpattern number (updated)
1132    
1133  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
# Line 1117  Returns:       the number of the named s Line 1135  Returns:       the number of the named s
1135    
1136  static int  static int
1137  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,
1138    BOOL xmode, int *count)    BOOL xmode, BOOL utf8, int *count)
1139  {  {
1140  uschar *ptr = *ptrptr;  uschar *ptr = *ptrptr;
1141  int start_count = *count;  int start_count = *count;
# Line 1202  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1220  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1220    }    }
1221    
1222  /* Past any initial parenthesis handling, scan for parentheses or vertical  /* Past any initial parenthesis handling, scan for parentheses or vertical
1223  bars. */  bars. Stop if we get to cd->end_pattern. Note that this is important for the
1224    first-pass call when this value is temporarily adjusted to stop at the current
1225    position. So DO NOT change this to a test for binary zero. */
1226    
1227  for (; *ptr != 0; ptr++)  for (; ptr < cd->end_pattern; ptr++)
1228    {    {
1229    /* Skip over backslashed characters and also entire \Q...\E */    /* Skip over backslashed characters and also entire \Q...\E */
1230    
# Line 1278  for (; *ptr != 0; ptr++) Line 1298  for (; *ptr != 0; ptr++)
1298    
1299    if (xmode && *ptr == CHAR_NUMBER_SIGN)    if (xmode && *ptr == CHAR_NUMBER_SIGN)
1300      {      {
1301      while (*(++ptr) != 0 && *ptr != CHAR_NL) {};      ptr++;
1302        while (*ptr != 0)
1303          {
1304          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1305          ptr++;
1306    #ifdef SUPPORT_UTF8
1307          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
1308    #endif
1309          }
1310      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1311      continue;      continue;
1312      }      }
# Line 1287  for (; *ptr != 0; ptr++) Line 1315  for (; *ptr != 0; ptr++)
1315    
1316    if (*ptr == CHAR_LEFT_PARENTHESIS)    if (*ptr == CHAR_LEFT_PARENTHESIS)
1317      {      {
1318      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, count);      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);
1319      if (rc > 0) return rc;      if (rc > 0) return rc;
1320      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1321      }      }
# Line 1333  Arguments: Line 1361  Arguments:
1361    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1362    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1363    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1364      utf8         TRUE if we are in UTF-8 mode
1365    
1366  Returns:       the number of the found subpattern, or -1 if not found  Returns:       the number of the found subpattern, or -1 if not found
1367  */  */
1368    
1369  static int  static int
1370  find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode)  find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode,
1371      BOOL utf8)
1372  {  {
1373  uschar *ptr = (uschar *)cd->start_pattern;  uschar *ptr = (uschar *)cd->start_pattern;
1374  int count = 0;  int count = 0;
# Line 1351  matching closing parens. That is why we Line 1381  matching closing parens. That is why we
1381    
1382  for (;;)  for (;;)
1383    {    {
1384    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, &count);    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);
1385    if (rc > 0 || *ptr++ == 0) break;    if (rc > 0 || *ptr++ == 0) break;
1386    }    }
1387    
# Line 1367  return rc; Line 1397  return rc;
1397    
1398  /* This is called by several functions that scan a compiled expression looking  /* This is called by several functions that scan a compiled expression looking
1399  for a fixed first character, or an anchoring op code etc. It skips over things  for a fixed first character, or an anchoring op code etc. It skips over things
1400  that do not influence this. For some calls, a change of option is important.  that do not influence this. For some calls, it makes sense to skip negative
1401  For some calls, it makes sense to skip negative forward and all backward  forward and all backward assertions, and also the \b assertion; for others it
1402  assertions, and also the \b assertion; for others it does not.  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    {    {
1416    switch ((int)*code)    switch ((int)*code)
1417      {      {
     case OP_OPT:  
     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))  
       *options = (int)code[1];  
     code += 2;  
     break;  
   
1418      case OP_ASSERT_NOT:      case OP_ASSERT_NOT:
1419      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1420      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
# Line 1444  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 1455  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 1472  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 1485  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 1508  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 1531  for (;;) Line 1557  for (;;)
1557      case OP_RREF:      case OP_RREF:
1558      case OP_NRREF:      case OP_NRREF:
1559      case OP_DEF:      case OP_DEF:
     case OP_OPT:  
1560      case OP_CALLOUT:      case OP_CALLOUT:
1561      case OP_SOD:      case OP_SOD:
1562      case OP_SOM:      case OP_SOM:
# Line 1539  for (;;) Line 1564  for (;;)
1564      case OP_EOD:      case OP_EOD:
1565      case OP_EODN:      case OP_EODN:
1566      case OP_CIRC:      case OP_CIRC:
1567        case OP_CIRCM:
1568      case OP_DOLL:      case OP_DOLL:
1569        case OP_DOLLM:
1570      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1571      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1572      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
# Line 1548  for (;;) Line 1575  for (;;)
1575      /* Handle literal characters */      /* Handle literal characters */
1576    
1577      case OP_CHAR:      case OP_CHAR:
1578      case OP_CHARNC:      case OP_CHARI:
1579      case OP_NOT:      case OP_NOT:
1580        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 1565  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 1668  _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 1686  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 1724  for (;;) Line 1752  for (;;)
1752        case OP_MARK:        case OP_MARK:
1753        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1754        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       case OP_THEN_ARG:  
1755        code += code[1];        code += code[1];
1756        break;        break;
1757    
1758          case OP_THEN_ARG:
1759          code += code[1+LINK_SIZE];
1760          break;
1761        }        }
1762    
1763      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1741  for (;;) Line 1772  for (;;)
1772      if (utf8) switch(c)      if (utf8) switch(c)
1773        {        {
1774        case OP_CHAR:        case OP_CHAR:
1775        case OP_CHARNC:        case OP_CHARI:
1776        case OP_EXACT:        case OP_EXACT:
1777          case OP_EXACTI:
1778        case OP_UPTO:        case OP_UPTO:
1779          case OP_UPTOI:
1780        case OP_MINUPTO:        case OP_MINUPTO:
1781          case OP_MINUPTOI:
1782        case OP_POSUPTO:        case OP_POSUPTO:
1783          case OP_POSUPTOI:
1784        case OP_STAR:        case OP_STAR:
1785          case OP_STARI:
1786        case OP_MINSTAR:        case OP_MINSTAR:
1787          case OP_MINSTARI:
1788        case OP_POSSTAR:        case OP_POSSTAR:
1789          case OP_POSSTARI:
1790        case OP_PLUS:        case OP_PLUS:
1791          case OP_PLUSI:
1792        case OP_MINPLUS:        case OP_MINPLUS:
1793          case OP_MINPLUSI:
1794        case OP_POSPLUS:        case OP_POSPLUS:
1795          case OP_POSPLUSI:
1796        case OP_QUERY:        case OP_QUERY:
1797          case OP_QUERYI:
1798        case OP_MINQUERY:        case OP_MINQUERY:
1799          case OP_MINQUERYI:
1800        case OP_POSQUERY:        case OP_POSQUERY:
1801          case OP_POSQUERYI:
1802        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1803        break;        break;
1804        }        }
# Line 1827  for (;;) Line 1871  for (;;)
1871        case OP_MARK:        case OP_MARK:
1872        case OP_PRUNE_ARG:        case OP_PRUNE_ARG:
1873        case OP_SKIP_ARG:        case OP_SKIP_ARG:
       case OP_THEN_ARG:  
1874        code += code[1];        code += code[1];
1875        break;        break;
1876    
1877          case OP_THEN_ARG:
1878          code += code[1+LINK_SIZE];
1879          break;
1880        }        }
1881    
1882      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1844  for (;;) Line 1891  for (;;)
1891      if (utf8) switch(c)      if (utf8) switch(c)
1892        {        {
1893        case OP_CHAR:        case OP_CHAR:
1894        case OP_CHARNC:        case OP_CHARI:
1895        case OP_EXACT:        case OP_EXACT:
1896          case OP_EXACTI:
1897        case OP_UPTO:        case OP_UPTO:
1898          case OP_UPTOI:
1899        case OP_MINUPTO:        case OP_MINUPTO:
1900          case OP_MINUPTOI:
1901        case OP_POSUPTO:        case OP_POSUPTO:
1902          case OP_POSUPTOI:
1903        case OP_STAR:        case OP_STAR:
1904          case OP_STARI:
1905        case OP_MINSTAR:        case OP_MINSTAR:
1906          case OP_MINSTARI:
1907        case OP_POSSTAR:        case OP_POSSTAR:
1908          case OP_POSSTARI:
1909        case OP_PLUS:        case OP_PLUS:
1910          case OP_PLUSI:
1911        case OP_MINPLUS:        case OP_MINPLUS:
1912          case OP_MINPLUSI:
1913        case OP_POSPLUS:        case OP_POSPLUS:
1914          case OP_POSPLUSI:
1915        case OP_QUERY:        case OP_QUERY:
1916          case OP_QUERYI:
1917        case OP_MINQUERY:        case OP_MINQUERY:
1918          case OP_MINQUERYI:
1919        case OP_POSQUERY:        case OP_POSQUERY:
1920          case OP_POSQUERYI:
1921        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1922        break;        break;
1923        }        }
# Line 1896  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 1914  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 backward reference subroutine call, we can scan it. If it's a
1979      forward reference subroutine call, we can't. To detect forward reference
1980      we have to scan up the list that is kept in the workspace. This function is
1981      called only when doing the real compile, not during the pre-compile that
1982      measures the size of the compiled pattern. */
1983    
1984    if (c == OP_RECURSE)    if (c == OP_RECURSE)
1985      {      {
1986      BOOL empty_branch = FALSE;      const uschar *scode;
1987      const uschar *scode = cd->start_code + GET(code, 1);      BOOL empty_branch;
1988    
1989        /* Test for forward reference */
1990    
1991        for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)
1992          if (GET(scode, 0) == code + 1 - cd->start_code) return TRUE;
1993    
1994        /* Not a forward reference, test for completed backward reference */
1995    
1996        empty_branch = FALSE;
1997        scode = cd->start_code + GET(code, 1);
1998      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
1999    
2000        /* Completed backwards reference */
2001    
2002      do      do
2003        {        {
2004        if (could_be_empty_branch(scode, endcode, utf8, cd))        if (could_be_empty_branch(scode, endcode, utf8, cd))
# Line 1942  for (code = first_significant_code(code Line 2009  for (code = first_significant_code(code
2009        scode += GET(scode, 1);        scode += GET(scode, 1);
2010        }        }
2011      while (*scode == OP_ALT);      while (*scode == OP_ALT);
2012    
2013      if (!empty_branch) return FALSE;  /* All branches are non-empty */      if (!empty_branch) return FALSE;  /* All branches are non-empty */
2014      continue;      continue;
2015      }      }
2016    
2017      /* Groups with zero repeats can of course be empty; skip them. */
2018    
2019      if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2020          c == OP_BRAPOSZERO)
2021        {
2022        code += _pcre_OP_lengths[c];
2023        do code += GET(code, 1); while (*code == OP_ALT);
2024        c = *code;
2025        continue;
2026        }
2027    
2028      /* A nested group that is already marked as "could be empty" can just be
2029      skipped. */
2030    
2031      if (c == OP_SBRA  || c == OP_SBRAPOS ||
2032          c == OP_SCBRA || c == OP_SCBRAPOS)
2033        {
2034        do code += GET(code, 1); while (*code == OP_ALT);
2035        c = *code;
2036        continue;
2037        }
2038    
2039    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
2040    
2041    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA  || c == OP_BRAPOS ||
2042          c == OP_CBRA || c == OP_CBRAPOS ||
2043          c == OP_ONCE || c == OP_COND)
2044      {      {
2045      BOOL empty_branch;      BOOL empty_branch;
2046      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */      if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
# Line 2035  for (code = first_significant_code(code Line 2127  for (code = first_significant_code(code
2127      case OP_ALLANY:      case OP_ALLANY:
2128      case OP_ANYBYTE:      case OP_ANYBYTE:
2129      case OP_CHAR:      case OP_CHAR:
2130      case OP_CHARNC:      case OP_CHARI:
2131      case OP_NOT:      case OP_NOT:
2132        case OP_NOTI:
2133      case OP_PLUS:      case OP_PLUS:
2134      case OP_MINPLUS:      case OP_MINPLUS:
2135      case OP_POSPLUS:      case OP_POSPLUS:
# Line 2076  for (code = first_significant_code(code Line 2169  for (code = first_significant_code(code
2169      case OP_KET:      case OP_KET:
2170      case OP_KETRMAX:      case OP_KETRMAX:
2171      case OP_KETRMIN:      case OP_KETRMIN:
2172        case OP_KETRPOS:
2173      case OP_ALT:      case OP_ALT:
2174      return TRUE;      return TRUE;
2175    
# Line 2084  for (code = first_significant_code(code Line 2178  for (code = first_significant_code(code
2178    
2179  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2180      case OP_STAR:      case OP_STAR:
2181        case OP_STARI:
2182      case OP_MINSTAR:      case OP_MINSTAR:
2183        case OP_MINSTARI:
2184      case OP_POSSTAR:      case OP_POSSTAR:
2185        case OP_POSSTARI:
2186      case OP_QUERY:      case OP_QUERY:
2187        case OP_QUERYI:
2188      case OP_MINQUERY:      case OP_MINQUERY:
2189        case OP_MINQUERYI:
2190      case OP_POSQUERY:      case OP_POSQUERY:
2191        case OP_POSQUERYI:
2192      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];
2193      break;      break;
2194    
2195      case OP_UPTO:      case OP_UPTO:
2196        case OP_UPTOI:
2197      case OP_MINUPTO:      case OP_MINUPTO:
2198        case OP_MINUPTOI:
2199      case OP_POSUPTO:      case OP_POSUPTO:
2200        case OP_POSUPTOI:
2201      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];
2202      break;      break;
2203  #endif  #endif
# Line 2105  for (code = first_significant_code(code Line 2208  for (code = first_significant_code(code
2208      case OP_MARK:      case OP_MARK:
2209      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
2210      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     case OP_THEN_ARG:  
2211      code += code[1];      code += code[1];
2212      break;      break;
2213    
2214        case OP_THEN_ARG:
2215        code += code[1+LINK_SIZE];
2216        break;
2217    
2218      /* None of the remaining opcodes are required to match a character. */      /* None of the remaining opcodes are required to match a character. */
2219    
2220      default:      default:
# Line 2129  return TRUE; Line 2235  return TRUE;
2235  the current branch of the current pattern to see if it could match the empty  the current branch of the current pattern to see if it could match the empty
2236  string. If it could, we must look outwards for branches at other levels,  string. If it could, we must look outwards for branches at other levels,
2237  stopping when we pass beyond the bracket which is the subject of the recursion.  stopping when we pass beyond the bracket which is the subject of the recursion.
2238    This function is called only during the real compile, not during the
2239    pre-compile.
2240    
2241  Arguments:  Arguments:
2242    code        points to start of the recursion    code        points to start of the recursion
# Line 2506  if ((options & PCRE_EXTENDED) != 0) Line 2614  if ((options & PCRE_EXTENDED) != 0)
2614      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2615      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2616        {        {
2617        while (*(++ptr) != 0)        ptr++;
2618          while (*ptr != 0)
2619            {
2620          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2621            ptr++;
2622    #ifdef SUPPORT_UTF8
2623            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2624    #endif
2625            }
2626        }        }
2627      else break;      else break;
2628      }      }
# Line 2543  if ((options & PCRE_EXTENDED) != 0) Line 2658  if ((options & PCRE_EXTENDED) != 0)
2658      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
2659      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
2660        {        {
2661        while (*(++ptr) != 0)        ptr++;
2662          while (*ptr != 0)
2663            {
2664          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
2665            ptr++;
2666    #ifdef SUPPORT_UTF8
2667            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
2668    #endif
2669            }
2670        }        }
2671      else break;      else break;
2672      }      }
# Line 2569  if (next >= 0) switch(op_code) Line 2691  if (next >= 0) switch(op_code)
2691  #endif  #endif
2692    return c != next;    return c != next;
2693    
2694    /* For CHARNC (caseless character) we must check the other case. If we have    /* For CHARI (caseless character) we must check the other case. If we have
2695    Unicode property support, we can use it to test the other case of    Unicode property support, we can use it to test the other case of
2696    high-valued characters. */    high-valued characters. */
2697    
2698    case OP_CHARNC:    case OP_CHARI:
2699  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2700    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
2701  #else  #else
# Line 2596  if (next >= 0) switch(op_code) Line 2718  if (next >= 0) switch(op_code)
2718  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF8 */
2719    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */
2720    
2721    /* For OP_NOT, its data is always a single-byte character. */    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These
2722      opcodes are not used for multi-byte characters, because they are coded using
2723      an XCLASS instead. */
2724    
2725    case OP_NOT:    case OP_NOT:
2726      return (c = *previous) == next;
2727    
2728      case OP_NOTI:
2729    if ((c = *previous) == next) return TRUE;    if ((c = *previous) == next) return TRUE;
   if ((options & PCRE_CASELESS) == 0) return FALSE;  
2730  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2731    if (utf8)    if (utf8)
2732      {      {
# Line 2705  replaced by OP_PROP codes when PCRE_UCP Line 2831  replaced by OP_PROP codes when PCRE_UCP
2831  switch(op_code)  switch(op_code)
2832    {    {
2833    case OP_CHAR:    case OP_CHAR:
2834    case OP_CHARNC:    case OP_CHARI:
2835  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2836    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
2837  #else  #else
# Line 3117  for (;; ptr++) Line 3243  for (;; ptr++)
3243      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if ((cd->ctypes[c] & ctype_space) != 0) continue;
3244      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3245        {        {
3246        while (*(++ptr) != 0)        ptr++;
3247          while (*ptr != 0)
3248          {          {
3249          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3250            ptr++;
3251    #ifdef SUPPORT_UTF8
3252            if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;
3253    #endif
3254          }          }
3255        if (*ptr != 0) continue;        if (*ptr != 0) continue;
3256    
# Line 3164  for (;; ptr++) Line 3295  for (;; ptr++)
3295      the setting of any following char as a first character. */      the setting of any following char as a first character. */
3296    
3297      case CHAR_CIRCUMFLEX_ACCENT:      case CHAR_CIRCUMFLEX_ACCENT:
3298        previous = NULL;
3299      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3300        {        {
3301        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
3302          *code++ = OP_CIRCM;
3303        }        }
3304      previous = NULL;      else *code++ = OP_CIRC;
     *code++ = OP_CIRC;  
3305      break;      break;
3306    
3307      case CHAR_DOLLAR_SIGN:      case CHAR_DOLLAR_SIGN:
3308      previous = NULL;      previous = NULL;
3309      *code++ = OP_DOLL;      *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
3310      break;      break;
3311    
3312      /* There can never be a first char if '.' is first, whatever happens about      /* There can never be a first char if '.' is first, whatever happens about
# Line 3494  for (;; ptr++) Line 3626  for (;; ptr++)
3626              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
3627              continue;              continue;
3628    
3629                /* Perl 5.004 onwards omits VT from \s, but we must preserve it
3630                if it was previously set by something earlier in the character
3631                class. */
3632    
3633              case ESC_s:              case ESC_s:
3634              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];              classbits[0] |= cbits[cbit_space];
3635              classbits[1] &= ~0x08;   /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= cbits[cbit_space+1] & ~0x08;
3636                for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
3637              continue;              continue;
3638    
3639              case ESC_S:              case ESC_S:
# Line 3915  for (;; ptr++) Line 4052  for (;; ptr++)
4052    
4053      In UTF-8 mode, we can optimize the negative case only if there were no      In UTF-8 mode, we can optimize the negative case only if there were no
4054      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR
4055      operate on single-bytes only. This is an historical hangover. Maybe one day      operate on single-bytes characters only. This is an historical hangover.
4056      we can tidy these opcodes to handle multi-byte characters.      Maybe one day we can tidy these opcodes to handle multi-byte characters.
4057    
4058      The optimization throws away the bit map. We turn the item into a      The optimization throws away the bit map. We turn the item into a
4059      1-character OP_CHAR[NC] if it's positive, or OP_NOT if it's negative. Note      1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.
4060      that OP_NOT does not support multibyte characters. In the positive case, it      Note that OP_NOT[I] does not support multibyte characters. In the positive
4061      can cause firstbyte to be set. Otherwise, there can be no first char if      case, it can cause firstbyte to be set. Otherwise, there can be no first
4062      this item is first, whatever repeat count may follow. In the case of      char if this item is first, whatever repeat count may follow. In the case
4063      reqbyte, save the previous value for reinstating. */      of reqbyte, save the previous value for reinstating. */
4064    
4065  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4066      if (class_charcount == 1 && !class_utf8 &&      if (class_charcount == 1 && !class_utf8 &&
# Line 3934  for (;; ptr++) Line 4071  for (;; ptr++)
4071        {        {
4072        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
4073    
4074        /* The OP_NOT opcode works on one-byte characters only. */        /* The OP_NOT[I] opcodes work on one-byte characters only. */
4075    
4076        if (negate_class)        if (negate_class)
4077          {          {
4078          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
4079          zerofirstbyte = firstbyte;          zerofirstbyte = firstbyte;
4080          *code++ = OP_NOT;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4081          *code++ = class_lastchar;          *code++ = class_lastchar;
4082          break;          break;
4083          }          }
# Line 4091  for (;; ptr++) Line 4228  for (;; ptr++)
4228        ptr++;        ptr++;
4229        }        }
4230      else repeat_type = greedy_default;      else repeat_type = greedy_default;
4231    
4232        /* If previous was a recursion call, wrap it in atomic brackets so that
4233        previous becomes the atomic group. All recursions were so wrapped in the
4234        past, but it no longer happens for non-repeated recursions. In fact, the
4235        repeated ones could be re-implemented independently so as not to need this,
4236        but for the moment we rely on the code for repeating groups. */
4237    
4238        if (*previous == OP_RECURSE)
4239          {
4240          memmove(previous + 1 + LINK_SIZE, previous, 1 + LINK_SIZE);
4241          *previous = OP_ONCE;
4242          PUT(previous, 1, 2 + 2*LINK_SIZE);
4243          previous[2 + 2*LINK_SIZE] = OP_KET;
4244          PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4245          code += 2 + 2 * LINK_SIZE;
4246          length_prevgroup = 3 + 3*LINK_SIZE;
4247    
4248          /* When actually compiling, we need to check whether this was a forward
4249          reference, and if so, adjust the offset. */
4250    
4251          if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4252            {
4253            int offset = GET(cd->hwm, -LINK_SIZE);
4254            if (offset == previous + 1 - cd->start_code)
4255              PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
4256            }
4257          }
4258    
4259        /* Now handle repetition for the different types of item. */
4260    
4261      /* If previous was a character match, abolish the item and generate a      /* If previous was a character match, abolish the item and generate a
4262      repeat item instead. If a char item has a minumum of more than one, ensure      repeat item instead. If a char item has a minumum of more than one, ensure
# Line 4098  for (;; ptr++) Line 4264  for (;; ptr++)
4264      the first thing in a branch because the x will have gone into firstbyte      the first thing in a branch because the x will have gone into firstbyte
4265      instead.  */      instead.  */
4266    
4267      if (*previous == OP_CHAR || *previous == OP_CHARNC)      if (*previous == OP_CHAR || *previous == OP_CHARI)
4268        {        {
4269          op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;
4270    
4271        /* 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
4272        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
4273        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 4144  for (;; ptr++) Line 4312  for (;; ptr++)
4312      /* If previous was a single negated character ([^a] or similar), we use      /* If previous was a single negated character ([^a] or similar), we use
4313      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-
4314      character repeats by setting opt_type to add a suitable offset into      character repeats by setting opt_type to add a suitable offset into
4315      repeat_type. We can also test for auto-possessification. OP_NOT is      repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI
4316      currently used only for single-byte chars. */      are currently used only for single-byte chars. */
4317    
4318      else if (*previous == OP_NOT)      else if (*previous == OP_NOT || *previous == OP_NOTI)
4319        {        {
4320        op_type = OP_NOTSTAR - OP_STAR;  /* Use "not" opcodes */        op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;
4321        c = previous[1];        c = previous[1];
4322        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4323            repeat_max < 0 &&            repeat_max < 0 &&
# Line 4346  for (;; ptr++) Line 4514  for (;; ptr++)
4514  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
4515               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
4516  #endif  #endif
4517               *previous == OP_REF)               *previous == OP_REF ||
4518                 *previous == OP_REFI)
4519        {        {
4520        if (repeat_max == 0)        if (repeat_max == 0)
4521          {          {
# Line 4380  for (;; ptr++) Line 4549  for (;; ptr++)
4549        }        }
4550    
4551      /* 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
4552      cases. */      cases. Note that at this point we can encounter only the "basic" BRA and
4553        KET opcodes, as this is the place where they get converted into the more
4554        special varieties. */
4555    
4556      else if (*previous == OP_BRA  || *previous == OP_CBRA ||      else if (*previous == OP_BRA  || *previous == OP_CBRA ||
4557               *previous == OP_ONCE || *previous == OP_COND)               *previous == OP_ONCE || *previous == OP_COND)
4558        {        {
4559        register int i;        register int i;
       int ketoffset = 0;  
4560        int len = (int)(code - previous);        int len = (int)(code - previous);
4561        uschar *bralink = NULL;        uschar *bralink = NULL;
4562          uschar *brazeroptr = NULL;
4563    
4564        /* Repeating a DEFINE group is pointless */        /* Repeating a DEFINE group is pointless */
4565    
4566        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)        if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
# Line 4398  for (;; ptr++) Line 4569  for (;; ptr++)
4569          goto FAILED;          goto FAILED;
4570          }          }
4571    
       /* 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. There may be an OP_OPT setting following  
       the final KET, so we can't find the end just by going back from the code  
       pointer. */  
   
       if (repeat_max == -1)  
         {  
         register uschar *ket = previous;  
         do ket += GET(ket, 1); while (*ket != OP_KET);  
         ketoffset = (int)(code - ket);  
         }  
   
4572        /* 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
4573        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
4574        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 4452  for (;; ptr++) Line 4610  for (;; ptr++)
4610              *previous++ = OP_SKIPZERO;              *previous++ = OP_SKIPZERO;
4611              goto END_REPEAT;              goto END_REPEAT;
4612              }              }
4613              brazeroptr = previous;    /* Save for possessive optimizing */
4614            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
4615            }            }
4616    
# Line 4616  for (;; ptr++) Line 4775  for (;; ptr++)
4775            }            }
4776          }          }
4777    
4778        /* 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
4779        can't just offset backwards from the current code point, because we        ONCE brackets, that's all we need to do. However, possessively repeated
4780        don't know if there's been an options resetting after the ket. The        ONCE brackets can be converted into non-capturing brackets, as the
4781        correct offset was computed above.        behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
4782          deal with possessive ONCEs specially.
4783    
4784          Otherwise, if the quantifier was possessive, we convert the BRA code to
4785          the POS form, and the KET code to KETRPOS. (It turns out to be convenient
4786          at runtime to detect this kind of subpattern at both the start and at the
4787          end.) The use of special opcodes makes it possible to reduce greatly the
4788          stack usage in pcre_exec(). If the group is preceded by OP_BRAZERO,
4789          convert this to OP_BRAPOSZERO. Then cancel the possessive flag so that
4790          the default action below, of wrapping everything inside atomic brackets,
4791          does not happen.
4792    
4793        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
4794        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
4795        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
4796        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
4797        atomic groups at runtime, but in a different way.] */        runtime, but in a different way.] */
4798    
4799        else        else
4800          {          {
4801          uschar *ketcode = code - ketoffset;          uschar *ketcode = code - 1 - LINK_SIZE;
4802          uschar *bracode = ketcode - GET(ketcode, 1);          uschar *bracode = ketcode - GET(ketcode, 1);
4803          *ketcode = OP_KETRMAX + repeat_type;  
4804          if (lengthptr == NULL && *bracode != OP_ONCE)          if (*bracode == OP_ONCE && possessive_quantifier) *bracode = OP_BRA;
4805            if (*bracode == OP_ONCE)
4806              *ketcode = OP_KETRMAX + repeat_type;
4807            else
4808            {            {
4809            uschar *scode = bracode;            if (possessive_quantifier)
4810            do              {
4811                *bracode += 1;                   /* Switch to xxxPOS opcodes */
4812                *ketcode = OP_KETRPOS;
4813                if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
4814                possessive_quantifier = FALSE;
4815                }
4816              else *ketcode = OP_KETRMAX + repeat_type;
4817    
4818              if (lengthptr == NULL)
4819              {              {
4820              if (could_be_empty_branch(scode, ketcode, utf8, cd))              uschar *scode = bracode;
4821                do
4822                {                {
4823                *bracode += OP_SBRA - OP_BRA;                if (could_be_empty_branch(scode, ketcode, utf8, cd))
4824                break;                  {
4825                    *bracode += OP_SBRA - OP_BRA;
4826                    break;
4827                    }
4828                  scode += GET(scode, 1);
4829                }                }
4830              scode += GET(scode, 1);              while (*scode == OP_ALT);
4831              }              }
           while (*scode == OP_ALT);  
4832            }            }
4833          }          }
4834        }        }
# Line 4665  for (;; ptr++) Line 4849  for (;; ptr++)
4849        }        }
4850    
4851      /* If the character following a repeat is '+', or if certain optimization      /* If the character following a repeat is '+', or if certain optimization
4852      tests above succeeded, possessive_quantifier is TRUE. For some of the      tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
4853      simpler opcodes, there is an special alternative opcode for this. For      there are special alternative opcodes for this case. For anything else, we
4854      anything else, we wrap the entire repeated item inside OP_ONCE brackets.      wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
4855      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
4856      but the special opcodes can optimize it a bit. The repeated item starts at      special opcodes can optimize it.
4857      tempcode, not at previous, which might be the first part of a string whose  
4858      (former) last char we repeated.      Possessively repeated subpatterns have already been handled in the code
4859        just above, so possessive_quantifier is always FALSE for them at this
4860        stage.
4861    
4862        Note that the repeated item starts at tempcode, not at previous, which
4863        might be the first part of a string whose (former) last char we repeated.
4864    
4865      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
4866      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 4702  for (;; ptr++) Line 4891  for (;; ptr++)
4891          case OP_QUERY: *tempcode = OP_POSQUERY; break;          case OP_QUERY: *tempcode = OP_POSQUERY; break;
4892          case OP_UPTO:  *tempcode = OP_POSUPTO; break;          case OP_UPTO:  *tempcode = OP_POSUPTO; break;
4893    
4894          case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;          case OP_STARI:  *tempcode = OP_POSSTARI; break;
4895          case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;          case OP_PLUSI:  *tempcode = OP_POSPLUSI; break;
4896          case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;          case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
4897          case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;          case OP_UPTOI:  *tempcode = OP_POSUPTOI; break;
4898    
4899          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;          case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;
4900          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;          case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;
4901          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;          case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
4902          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;          case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;
4903    
4904            case OP_NOTSTARI:  *tempcode = OP_NOTPOSSTARI; break;
4905            case OP_NOTPLUSI:  *tempcode = OP_NOTPOSPLUSI; break;
4906            case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
4907            case OP_NOTUPTOI:  *tempcode = OP_NOTPOSUPTOI; break;
4908    
4909            case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;
4910            case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;
4911            case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
4912            case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;
4913    
4914          /* Because we are moving code along, we must ensure that any          /* Because we are moving code along, we must ensure that any
4915          pending recursive references are updated. */          pending recursive references are updated. */
4916    
# Line 4786  for (;; ptr++) Line 4985  for (;; ptr++)
4985          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
4986              strncmp((char *)name, vn, namelen) == 0)              strncmp((char *)name, vn, namelen) == 0)
4987            {            {
4988            /* Check for open captures before ACCEPT */            /* Check for open captures before ACCEPT and convert it to
4989              ASSERT_ACCEPT if in an assertion. */
4990    
4991            if (verbs[i].op == OP_ACCEPT)            if (verbs[i].op == OP_ACCEPT)
4992              {              {
4993              open_capitem *oc;              open_capitem *oc;
4994                if (arglen != 0)
4995                  {
4996                  *errorcodeptr = ERR59;
4997                  goto FAILED;
4998                  }
4999              cd->had_accept = TRUE;              cd->had_accept = TRUE;
5000              for (oc = cd->open_caps; oc != NULL; oc = oc->next)              for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5001                {                {
5002                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5003                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5004                }                }
5005                *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5006              }              }
5007    
5008            /* Handle the cases with/without an argument */            /* Handle other cases with/without an argument */
5009    
5010            if (arglen == 0)            else if (arglen == 0)
5011              {              {
5012              if (verbs[i].op < 0)   /* Argument is mandatory */              if (verbs[i].op < 0)   /* Argument is mandatory */
5013                {                {
5014                *errorcodeptr = ERR66;                *errorcodeptr = ERR66;
5015                goto FAILED;                goto FAILED;
5016                }                }
5017              *code++ = verbs[i].op;              *code = verbs[i].op;
5018                if (*code++ == OP_THEN)
5019                  {
5020                  PUT(code, 0, code - bcptr->current_branch - 1);
5021                  code += LINK_SIZE;
5022                  }
5023              }              }
5024    
5025            else            else
# Line 4818  for (;; ptr++) Line 5029  for (;; ptr++)
5029                *errorcodeptr = ERR59;                *errorcodeptr = ERR59;
5030                goto FAILED;                goto FAILED;
5031                }                }
5032              *code++ = verbs[i].op_arg;              *code = verbs[i].op_arg;
5033                if (*code++ == OP_THEN_ARG)
5034                  {
5035                  PUT(code, 0, code - bcptr->current_branch - 1);
5036                  code += LINK_SIZE;
5037                  }
5038              *code++ = arglen;              *code++ = arglen;
5039              memcpy(code, arg, arglen);              memcpy(code, arg, arglen);
5040              code += arglen;              code += arglen;
# Line 5012  for (;; ptr++) Line 5228  for (;; ptr++)
5228          /* Search the pattern for a forward reference */          /* Search the pattern for a forward reference */
5229    
5230          else if ((i = find_parens(cd, name, namelen,          else if ((i = find_parens(cd, name, namelen,
5231                          (options & PCRE_EXTENDED) != 0)) > 0)                          (options & PCRE_EXTENDED) != 0, utf8)) > 0)
5232            {            {
5233            PUT2(code, 2+LINK_SIZE, i);            PUT2(code, 2+LINK_SIZE, i);
5234            code[1+LINK_SIZE]++;            code[1+LINK_SIZE]++;
# Line 5080  for (;; ptr++) Line 5296  for (;; ptr++)
5296          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
5297          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */          case CHAR_EQUALS_SIGN:                 /* Positive lookahead */
5298          bravalue = OP_ASSERT;          bravalue = OP_ASSERT;
5299            cd->assert_depth += 1;
5300          ptr++;          ptr++;
5301          break;          break;
5302    
# Line 5094  for (;; ptr++) Line 5311  for (;; ptr++)
5311            continue;            continue;
5312            }            }
5313          bravalue = OP_ASSERT_NOT;          bravalue = OP_ASSERT_NOT;
5314            cd->assert_depth += 1;
5315          break;          break;
5316    
5317    
# Line 5103  for (;; ptr++) Line 5321  for (;; ptr++)
5321            {            {
5322            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */            case CHAR_EQUALS_SIGN:               /* Positive lookbehind */
5323            bravalue = OP_ASSERTBACK;            bravalue = OP_ASSERTBACK;
5324              cd->assert_depth += 1;
5325            ptr += 2;            ptr += 2;
5326            break;            break;
5327    
5328            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */            case CHAR_EXCLAMATION_MARK:          /* Negative lookbehind */
5329            bravalue = OP_ASSERTBACK_NOT;            bravalue = OP_ASSERTBACK_NOT;
5330              cd->assert_depth += 1;
5331            ptr += 2;            ptr += 2;
5332            break;            break;
5333    
# Line 5313  for (;; ptr++) Line 5533  for (;; ptr++)
5533          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5534          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
5535    
5536          /* In the pre-compile phase, do a syntax check and set a dummy          /* In the pre-compile phase, do a syntax check. We used to just set
5537          reference number. */          a dummy reference number, because it was not used in the first pass.
5538            However, with the change of recursive back references to be atomic,
5539            we have to look for the number so that this state can be identified, as
5540            otherwise the incorrect length is computed. If it's not a backwards
5541            reference, the dummy number will do. */
5542    
5543          if (lengthptr != NULL)          if (lengthptr != NULL)
5544            {            {
5545              const uschar *temp;
5546    
5547            if (namelen == 0)            if (namelen == 0)
5548              {              {
5549              *errorcodeptr = ERR62;              *errorcodeptr = ERR62;
# Line 5333  for (;; ptr++) Line 5559  for (;; ptr++)
5559              *errorcodeptr = ERR48;              *errorcodeptr = ERR48;
5560              goto FAILED;              goto FAILED;
5561              }              }
5562            recno = 0;  
5563              /* The name table does not exist in the first pass, so we cannot
5564              do a simple search as in the code below. Instead, we have to scan the
5565              pattern to find the number. It is important that we scan it only as
5566              far as we have got because the syntax of named subpatterns has not
5567              been checked for the rest of the pattern, and find_parens() assumes
5568              correct syntax. In any case, it's a waste of resources to scan
5569              further. We stop the scan at the current point by temporarily
5570              adjusting the value of cd->endpattern. */
5571    
5572              temp = cd->end_pattern;
5573              cd->end_pattern = ptr;
5574              recno = find_parens(cd, name, namelen,
5575                (options & PCRE_EXTENDED) != 0, utf8);
5576              cd->end_pattern = temp;
5577              if (recno < 0) recno = 0;    /* Forward ref; set dummy number */
5578            }            }
5579    
5580          /* In the real compile, seek the name in the table. We check the name          /* In the real compile, seek the name in the table. We check the name
# Line 5358  for (;; ptr++) Line 5599  for (;; ptr++)
5599              }              }
5600            else if ((recno =                /* Forward back reference */            else if ((recno =                /* Forward back reference */
5601                      find_parens(cd, name, namelen,                      find_parens(cd, name, namelen,
5602                        (options & PCRE_EXTENDED) != 0)) <= 0)                        (options & PCRE_EXTENDED) != 0, utf8)) <= 0)
5603              {              {
5604              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
5605              goto FAILED;              goto FAILED;
# Line 5469  for (;; ptr++) Line 5710  for (;; ptr++)
5710              if (called == NULL)              if (called == NULL)
5711                {                {
5712                if (find_parens(cd, NULL, recno,                if (find_parens(cd, NULL, recno,
5713                      (options & PCRE_EXTENDED) != 0) < 0)                      (options & PCRE_EXTENDED) != 0, utf8) < 0)
5714                  {                  {
5715                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
5716                  goto FAILED;                  goto FAILED;
# Line 5477  for (;; ptr++) Line 5718  for (;; ptr++)
5718    
5719                /* Fudge the value of "called" so that when it is inserted as an                /* Fudge the value of "called" so that when it is inserted as an
5720                offset below, what it actually inserted is the reference number                offset below, what it actually inserted is the reference number
5721                of the group. */                of the group. Then remember the forward reference. */
5722    
5723                called = cd->start_code + recno;                called = cd->start_code + recno;
5724                PUTINC(cd->hwm, 0, (int)(code + 2 + LINK_SIZE - cd->start_code));                PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
5725                }                }
5726    
5727              /* If not a forward reference, and the subpattern is still open,              /* If not a forward reference, and the subpattern is still open,
# Line 5495  for (;; ptr++) Line 5736  for (;; ptr++)
5736                }                }
5737              }              }
5738    
5739            /* Insert the recursion/subroutine item, automatically wrapped inside            /* Insert the recursion/subroutine item. */
5740            "once" brackets. Set up a "previous group" length so that a  
           subsequent quantifier will work. */  
   
           *code = OP_ONCE;  
           PUT(code, 1, 2 + 2*LINK_SIZE);  
           code += 1 + LINK_SIZE;  
   
5741            *code = OP_RECURSE;            *code = OP_RECURSE;
5742            PUT(code, 1, (int)(called - cd->start_code));            PUT(code, 1, (int)(called - cd->start_code));
5743            code += 1 + LINK_SIZE;            code += 1 + LINK_SIZE;
   
           *code = OP_KET;  
           PUT(code, 1, 2 + 2*LINK_SIZE);  
           code += 1 + LINK_SIZE;  
   
           length_prevgroup = 3 + 3*LINK_SIZE;  
5744            }            }
5745    
5746          /* Can't determine a first byte now */          /* Can't determine a first byte now */
# Line 5572  for (;; ptr++) Line 5801  for (;; ptr++)
5801          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
5802          both phases.          both phases.
5803    
5804          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
5805          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. */  
5806    
5807          if (*ptr == CHAR_RIGHT_PARENTHESIS)          if (*ptr == CHAR_RIGHT_PARENTHESIS)
5808            {            {
# Line 5585  for (;; ptr++) Line 5813  for (;; ptr++)
5813              }              }
5814            else            else
5815              {              {
             if ((options & PCRE_IMS) != (newoptions & PCRE_IMS))  
               {  
               *code++ = OP_OPT;  
               *code++ = newoptions & PCRE_IMS;  
               }  
5816              greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);              greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
5817              greedy_non_default = greedy_default ^ 1;              greedy_non_default = greedy_default ^ 1;
5818              req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;              req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;
5819              }              }
5820    
5821            /* Change options at this level, and pass them back for use            /* Change options at this level, and pass them back for use
5822            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). */  
5823    
5824            *optionsptr = options = newoptions;            *optionsptr = options = newoptions;
5825            previous = NULL;       /* This item can't be repeated */            previous = NULL;       /* This item can't be repeated */
# Line 5636  for (;; ptr++) Line 5857  for (;; ptr++)
5857    
5858      /* Process nested bracketed regex. Assertions may not be repeated, but      /* Process nested bracketed regex. Assertions may not be repeated, but
5859      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
5860      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
5861      compilers complain otherwise. Pass in a new setting for the ims options if      because some compilers complain otherwise. */
     they have changed. */  
5862    
5863      previous = (bravalue >= OP_ONCE)? code : NULL;      previous = (bravalue >= OP_ONCE)? code : NULL;
5864      *code = bravalue;      *code = bravalue;
# Line 5648  for (;; ptr++) Line 5868  for (;; ptr++)
5868    
5869      if (!compile_regex(      if (!compile_regex(
5870           newoptions,                   /* The complete new option state */           newoptions,                   /* The complete new option state */
          options & PCRE_IMS,           /* The previous ims option state */  
5871           &tempcode,                    /* Where to put code (updated) */           &tempcode,                    /* Where to put code (updated) */
5872           &ptr,                         /* Input pointer (updated) */           &ptr,                         /* Input pointer (updated) */
5873           errorcodeptr,                 /* Where to put an error message */           errorcodeptr,                 /* Where to put an error message */
# Line 5664  for (;; ptr++) Line 5883  for (;; ptr++)
5883             &length_prevgroup           /* Pre-compile phase */             &length_prevgroup           /* Pre-compile phase */
5884           ))           ))
5885        goto FAILED;        goto FAILED;
5886    
5887        if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
5888          cd->assert_depth -= 1;
5889    
5890      /* 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
5891      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 5735  for (;; ptr++) Line 5957  for (;; ptr++)
5957          goto FAILED;          goto FAILED;
5958          }          }
5959        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;        *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
5960        *code++ = OP_BRA;        code++;   /* This already contains bravalue */
5961        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
5962        *code++ = OP_KET;        *code++ = OP_KET;
5963        PUTINC(code, 0, 1 + LINK_SIZE);        PUTINC(code, 0, 1 + LINK_SIZE);
# Line 5927  for (;; ptr++) Line 6149  for (;; ptr++)
6149          HANDLE_REFERENCE:    /* Come here from named backref handling */          HANDLE_REFERENCE:    /* Come here from named backref handling */
6150          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
6151          previous = code;          previous = code;
6152          *code++ = OP_REF;          *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
6153          PUT2INC(code, 0, recno);          PUT2INC(code, 0, recno);
6154          cd->backref_map |= (recno < 32)? (1 << recno) : 1;          cd->backref_map |= (recno < 32)? (1 << recno) : 1;
6155          if (recno > cd->top_backref) cd->top_backref = recno;          if (recno > cd->top_backref) cd->top_backref = recno;
# Line 6035  for (;; ptr++) Line 6257  for (;; ptr++)
6257    
6258      ONE_CHAR:      ONE_CHAR:
6259      previous = code;      previous = code;
6260      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARNC : OP_CHAR;      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR;
6261      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
6262    
6263      /* Remember if \r or \n were seen */      /* Remember if \r or \n were seen */
# Line 6099  return FALSE; Line 6321  return FALSE;
6321  /* On entry, ptr is pointing past the bracket character, but on return it  /* On entry, ptr is pointing past the bracket character, but on return it
6322  points to the closing bracket, or vertical bar, or end of string. The code  points to the closing bracket, or vertical bar, or end of string. The code
6323  variable is pointing at the byte into which the BRA operator has been stored.  variable is pointing at the byte into which the BRA operator has been stored.
 If the ims options are changed at the start (for a (?ims: group) or during any  
 branch, we need to insert an OP_OPT item at the start of every following branch  
 to ensure they get set correctly at run time, and also pass the new options  
 into every subsequent branch compile.  
   
6324  This function is used during the pre-compile phase when we are trying to find  This function is used during the pre-compile phase when we are trying to find
6325  out the amount of memory needed, as well as during the real compile phase. The  out the amount of memory needed, as well as during the real compile phase. The
6326  value of lengthptr distinguishes the two phases.  value of lengthptr distinguishes the two phases.
6327    
6328  Arguments:  Arguments:
6329    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  
6330    codeptr        -> the address of the current code pointer    codeptr        -> the address of the current code pointer
6331    ptrptr         -> the address of the current pattern pointer    ptrptr         -> the address of the current pattern pointer
6332    errorcodeptr   -> pointer to error code variable    errorcodeptr   -> pointer to error code variable
# Line 6128  Returns:         TRUE on success Line 6344  Returns:         TRUE on success
6344  */  */
6345    
6346  static BOOL  static BOOL
6347  compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,  compile_regex(int options, uschar **codeptr, const uschar **ptrptr,
6348    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,    int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
6349    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,    int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr, compile_data *cd,
6350    int *lengthptr)    int *lengthptr)
# Line 6145  int branchfirstbyte, branchreqbyte; Line 6361  int branchfirstbyte, branchreqbyte;
6361  int length;  int length;
6362  int orig_bracount;  int orig_bracount;
6363  int max_bracount;  int max_bracount;
 int old_external_options = cd->external_options;  
6364  branch_chain bc;  branch_chain bc;
6365    
6366  bc.outer = bcptr;  bc.outer = bcptr;
# Line 6169  pre-compile phase to find out whether an Line 6384  pre-compile phase to find out whether an
6384    
6385  /* 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
6386  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
6387  detect groups that contain recursive back references to themselves. */  detect groups that contain recursive back references to themselves. Note that
6388    only OP_CBRA need be tested here; changing this opcode to one of its variants,
6389    e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
6390    
6391  if (*code == OP_CBRA)  if (*code == OP_CBRA)
6392    {    {
# Line 6195  for (;;) Line 6412  for (;;)
6412    
6413    if (reset_bracount) cd->bracount = orig_bracount;    if (reset_bracount) cd->bracount = orig_bracount;
6414    
   /* Handle a change of ims options at the start of the branch */  
   
   if ((options & PCRE_IMS) != oldims)  
     {  
     *code++ = OP_OPT;  
     *code++ = options & PCRE_IMS;  
     length += 2;  
     }  
   
6415    /* Set up dummy OP_REVERSE if lookbehind assertion */    /* Set up dummy OP_REVERSE if lookbehind assertion */
6416    
6417    if (lookbehind)    if (lookbehind)
# Line 6224  for (;;) Line 6432  for (;;)
6432      return FALSE;      return FALSE;
6433      }      }
6434    
   /* 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;  
   
6435    /* Keep the highest bracket count in case (?| was used and some branch    /* Keep the highest bracket count in case (?| was used and some branch
6436    has fewer than the rest. */    has fewer than the rest. */
6437    
# Line 6293  for (;;) Line 6492  for (;;)
6492        {        {
6493        int fixed_length;        int fixed_length;
6494        *code = OP_END;        *code = OP_END;
6495        fixed_length = find_fixedlength(last_branch, options, FALSE, cd);        fixed_length = find_fixedlength(last_branch,  (options & PCRE_UTF8) != 0,
6496            FALSE, cd);
6497        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
6498        if (fixed_length == -3)        if (fixed_length == -3)
6499          {          {
# Line 6314  for (;;) Line 6514  for (;;)
6514    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
6515    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
6516    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
6517    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. */  
6518    
6519    if (*ptr != CHAR_VERTICAL_LINE)    if (*ptr != CHAR_VERTICAL_LINE)
6520      {      {
# Line 6360  for (;;) Line 6558  for (;;)
6558        cd->open_caps = cd->open_caps->next;        cd->open_caps = cd->open_caps->next;
6559        }        }
6560    
     /* Reset options if needed. */  
   
     if ((options & PCRE_IMS) != oldims && *ptr == CHAR_RIGHT_PARENTHESIS)  
       {  
       *code++ = OP_OPT;  
       *code++ = oldims;  
       length += 2;  
       }  
   
6561      /* Retain the highest bracket number, in case resetting was used. */      /* Retain the highest bracket number, in case resetting was used. */
6562    
6563      cd->bracount = max_bracount;      cd->bracount = max_bracount;
# Line 6428  for (;;) Line 6617  for (;;)
6617  /* Try to find out if this is an anchored regular expression. Consider each  /* Try to find out if this is an anchored regular expression. Consider each
6618  alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket  alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket
6619  all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then  all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then
6620  it's anchored. However, if this is a multiline pattern, then only OP_SOD  it's anchored. However, if this is a multiline pattern, then only OP_SOD will
6621  counts, since OP_CIRC can match in the middle.  be found, because ^ generates OP_CIRCM in that mode.
6622    
6623  We can also consider a regex to be anchored if OP_SOM starts all its branches.  We can also consider a regex to be anchored if OP_SOM starts all its branches.
6624  This is the code for \G, which means "match at start of match position, taking  This is the code for \G, which means "match at start of match position, taking
# Line 6450  of the more common cases more precisely. Line 6639  of the more common cases more precisely.
6639    
6640  Arguments:  Arguments:
6641    code           points to start of expression (the bracket)    code           points to start of expression (the bracket)
   options        points to the options setting  
6642    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
6643                    handles up to substring 31; after that we just have to take                    handles up to substring 31; after that we just have to take
6644                    the less precise approach                    the less precise approach
# Line 6460  Returns:     TRUE or FALSE Line 6648  Returns:     TRUE or FALSE
6648  */  */
6649    
6650  static BOOL  static BOOL
6651  is_anchored(register const uschar *code, int *options, unsigned int bracket_map,  is_anchored(register const uschar *code, unsigned int bracket_map,
6652    unsigned int backref_map)    unsigned int backref_map)
6653  {  {
6654  do {  do {
6655     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],
6656       options, PCRE_MULTILINE, FALSE);       FALSE);
6657     register int op = *scode;     register int op = *scode;
6658    
6659     /* Non-capturing brackets */     /* Non-capturing brackets */
6660    
6661     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
6662           op == OP_SBRA || op == OP_SBRAPOS)
6663       {       {
6664       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
6665       }       }
6666    
6667     /* Capturing brackets */     /* Capturing brackets */
6668    
6669     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
6670                op == OP_SCBRA || op == OP_SCBRAPOS)
6671       {       {
6672       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
6673       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
6674       if (!is_anchored(scode, options, new_map, backref_map)) return FALSE;       if (!is_anchored(scode, new_map, backref_map)) return FALSE;
6675       }       }
6676    
6677     /* Other brackets */     /* Other brackets */
6678    
6679     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)
6680       {       {
6681       if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;
6682       }       }
6683    
6684     /* .* 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 6503  do { Line 6693  do {
6693    
6694     /* Check for explicit anchoring */     /* Check for explicit anchoring */
6695    
6696     else if (op != OP_SOD && op != OP_SOM &&     else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE;
            ((*options & PCRE_MULTILINE) != 0 || op != OP_CIRC))  
      return FALSE;  
6697     code += GET(code, 1);     code += GET(code, 1);
6698     }     }
6699  while (*code == OP_ALT);   /* Loop for each alternative */  while (*code == OP_ALT);   /* Loop for each alternative */
# Line 6541  is_startline(const uschar *code, unsigne Line 6729  is_startline(const uschar *code, unsigne
6729  {  {
6730  do {  do {
6731     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],     const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],
6732       NULL, 0, FALSE);       FALSE);
6733     register int op = *scode;     register int op = *scode;
6734    
6735     /* 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 6568  do { Line 6756  do {
6756         scode += 1 + LINK_SIZE;         scode += 1 + LINK_SIZE;
6757         break;         break;
6758         }         }
6759       scode = first_significant_code(scode, NULL, 0, FALSE);       scode = first_significant_code(scode, FALSE);
6760       op = *scode;       op = *scode;
6761       }       }
6762    
6763     /* Non-capturing brackets */     /* Non-capturing brackets */
6764    
6765     if (op == OP_BRA)     if (op == OP_BRA  || op == OP_BRAPOS ||
6766           op == OP_SBRA || op == OP_SBRAPOS)
6767       {       {
6768       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;
6769       }       }
6770    
6771     /* Capturing brackets */     /* Capturing brackets */
6772    
6773     else if (op == OP_CBRA)     else if (op == OP_CBRA  || op == OP_CBRAPOS ||
6774                op == OP_SCBRA || op == OP_SCBRAPOS)
6775       {       {
6776       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
6777       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
# Line 6605  do { Line 6795  do {
6795    
6796     /* Check for explicit circumflex */     /* Check for explicit circumflex */
6797    
6798     else if (op != OP_CIRC) return FALSE;     else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;
6799    
6800     /* Move on to the next alternative */     /* Move on to the next alternative */
6801    
# Line 6631  we return that char, otherwise -1. Line 6821  we return that char, otherwise -1.
6821    
6822  Arguments:  Arguments:
6823    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)  
6824    inassert   TRUE if in an assertion    inassert   TRUE if in an assertion
6825    
6826  Returns:     -1 or the fixed first char  Returns:     -1 or the fixed first char
6827  */  */
6828    
6829  static int  static int
6830  find_firstassertedchar(const uschar *code, int *options, BOOL inassert)  find_firstassertedchar(const uschar *code, BOOL inassert)
6831  {  {
6832  register int c = -1;  register int c = -1;
6833  do {  do {
6834     int d;     int d;
6835     const uschar *scode =     int xl = (*code == OP_CBRA || *code == OP_SCBRA ||
6836       first_significant_code(code + 1+LINK_SIZE, options, PCRE_CASELESS, TRUE);               *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? 2:0;
6837       const uschar *scode = first_significant_code(code + 1+LINK_SIZE + xl, TRUE);
6838     register int op = *scode;     register int op = *scode;
6839    
6840     switch(op)     switch(op)
# Line 6653  do { Line 6843  do {
6843       return -1;       return -1;
6844    
6845       case OP_BRA:       case OP_BRA:
6846         case OP_BRAPOS:
6847       case OP_CBRA:       case OP_CBRA:
6848         case OP_SCBRA:
6849         case OP_CBRAPOS:
6850         case OP_SCBRAPOS:
6851       case OP_ASSERT:       case OP_ASSERT:
6852       case OP_ONCE:       case OP_ONCE:
6853       case OP_COND:       case OP_COND:
6854       if ((d = find_firstassertedchar(scode, options, op == OP_ASSERT)) < 0)       if ((d = find_firstassertedchar(scode, op == OP_ASSERT)) < 0)
6855         return -1;         return -1;
6856       if (c < 0) c = d; else if (c != d) return -1;       if (c < 0) c = d; else if (c != d) return -1;
6857       break;       break;
6858    
6859       case OP_EXACT:       /* Fall through */       case OP_EXACT:
6860       scode += 2;       scode += 2;
6861         /* Fall through */
6862    
6863       case OP_CHAR:       case OP_CHAR:
      case OP_CHARNC:  
6864       case OP_PLUS:       case OP_PLUS:
6865       case OP_MINPLUS:       case OP_MINPLUS:
6866       case OP_POSPLUS:       case OP_POSPLUS:
6867       if (!inassert) return -1;       if (!inassert) return -1;
6868       if (c < 0)       if (c < 0) c = scode[1];
6869         {         else if (c != scode[1]) return -1;
6870         c = scode[1];       break;
6871         if ((*options & PCRE_CASELESS) != 0) c |= REQ_CASELESS;  
6872         }       case OP_EXACTI:
6873       else if (c != scode[1]) return -1;       scode += 2;
6874         /* Fall through */
6875    
6876         case OP_CHARI:
6877         case OP_PLUSI:
6878         case OP_MINPLUSI:
6879         case OP_POSPLUSI:
6880         if (!inassert) return -1;
6881         if (c < 0) c = scode[1] | REQ_CASELESS;
6882           else if (c != scode[1]) return -1;
6883       break;       break;
6884       }       }
6885    
# Line 6799  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 7002  while (ptr[skipatstart] == CHAR_LEFT_PAR
7002      { skipatstart += 7; options |= PCRE_UTF8; continue; }      { skipatstart += 7; options |= PCRE_UTF8; continue; }
7003    else if (strncmp((char *)(ptr+skipatstart+2), STRING_UCP_RIGHTPAR, 4) == 0)    else if (strncmp((char *)(ptr+skipatstart+2), STRING_UCP_RIGHTPAR, 4) == 0)
7004      { skipatstart += 6; options |= PCRE_UCP; continue; }      { skipatstart += 6; options |= PCRE_UCP; continue; }
7005      else if (strncmp((char *)(ptr+skipatstart+2), STRING_NO_START_OPT_RIGHTPAR, 13) == 0)
7006        { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }
7007    
7008    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)
7009      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
# Line 6825  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 7030  while (ptr[skipatstart] == CHAR_LEFT_PAR
7030    
7031  utf8 = (options & PCRE_UTF8) != 0;  utf8 = (options & PCRE_UTF8) != 0;
7032    
7033  /* Can't support UTF8 unless PCRE has been compiled to include the code. */  /* Can't support UTF8 unless PCRE has been compiled to include the code. The
7034    return of an error code from _pcre_valid_utf8() is a new feature, introduced in
7035    release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
7036    not used here. */
7037    
7038  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
7039  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&  if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&
7040       (*erroroffset = _pcre_valid_utf8((USPTR)pattern, -1)) >= 0)       (errorcode = _pcre_valid_utf8((USPTR)pattern, -1, erroroffset)) != 0)
7041    {    {
7042    errorcode = ERR44;    errorcode = ERR44;
7043    goto PCRE_EARLY_ERROR_RETURN2;    goto PCRE_EARLY_ERROR_RETURN2;
# Line 6945  outside can help speed up starting point Line 7153  outside can help speed up starting point
7153  ptr += skipatstart;  ptr += skipatstart;
7154  code = cworkspace;  code = cworkspace;
7155  *code = OP_BRA;  *code = OP_BRA;
7156  (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,  (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,
7157    &code, &ptr, &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd,    FALSE, 0, &firstbyte, &reqbyte, NULL, cd, &length);
   &length);  
7158  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;  if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
7159    
7160  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,  DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
# Line 7001  field; this time it's used for rememberi Line 7208  field; this time it's used for rememberi
7208  */  */
7209    
7210  cd->final_bracount = cd->bracount;  /* Save for checking forward references */  cd->final_bracount = cd->bracount;  /* Save for checking forward references */
7211    cd->assert_depth = 0;
7212  cd->bracount = 0;  cd->bracount = 0;
7213  cd->names_found = 0;  cd->names_found = 0;
7214  cd->name_table = (uschar *)re + re->name_table_offset;  cd->name_table = (uschar *)re + re->name_table_offset;
# Line 7019  of the function here. */ Line 7227  of the function here. */
7227  ptr = (const uschar *)pattern + skipatstart;  ptr = (const uschar *)pattern + skipatstart;
7228  code = (uschar *)codestart;  code = (uschar *)codestart;
7229  *code = OP_BRA;  *code = OP_BRA;
7230  (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,  (void)compile_regex(re->options, &code, &ptr, &errorcode, FALSE, FALSE, 0,
7231    &errorcode, FALSE, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);    &firstbyte, &reqbyte, NULL, cd, NULL);
7232  re->top_bracket = cd->bracount;  re->top_bracket = cd->bracount;
7233  re->top_backref = cd->top_backref;  re->top_backref = cd->top_backref;
7234  re->flags = cd->external_flags;  re->flags = cd->external_flags;
# Line 7086  if (cd->check_lookbehind) Line 7294  if (cd->check_lookbehind)
7294        uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);        uschar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);
7295        int end_op = *be;        int end_op = *be;
7296        *be = OP_END;        *be = OP_END;
7297        fixed_length = find_fixedlength(cc, re->options, TRUE, cd);        fixed_length = find_fixedlength(cc, (re->options & PCRE_UTF8) != 0, TRUE,
7298            cd);
7299        *be = end_op;        *be = end_op;
7300        DPRINTF(("fixed length = %d\n", fixed_length));        DPRINTF(("fixed length = %d\n", fixed_length));
7301        if (fixed_length < 0)        if (fixed_length < 0)
# Line 7125  start with ^. and also when all branches Line 7334  start with ^. and also when all branches
7334    
7335  if ((re->options & PCRE_ANCHORED) == 0)  if ((re->options & PCRE_ANCHORED) == 0)
7336    {    {
7337    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))  
7338      re->options |= PCRE_ANCHORED;      re->options |= PCRE_ANCHORED;
7339    else    else
7340      {      {
7341      if (firstbyte < 0)      if (firstbyte < 0)
7342        firstbyte = find_firstassertedchar(codestart, &temp_options, FALSE);        firstbyte = find_firstassertedchar(codestart, FALSE);
7343      if (firstbyte >= 0)   /* Remove caseless flag for non-caseable chars */      if (firstbyte >= 0)   /* Remove caseless flag for non-caseable chars */
7344        {        {
7345        int ch = firstbyte & 255;        int ch = firstbyte & 255;

Legend:
Removed from v.545  
changed lines
  Added in v.624

  ViewVC Help
Powered by ViewVC 1.1.5