/[pcre]/code/trunk/pcre_compile.c
ViewVC logotype

Diff of /code/trunk/pcre_compile.c

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

revision 500 by ph10, Sat Mar 6 19:00:29 2010 UTC revision 512 by ph10, Tue Mar 30 11:11:52 2010 UTC
# Line 92  is 4 there is plenty of room. */ Line 92  is 4 there is plenty of room. */
92    
93  #define COMPILE_WORK_SIZE (4096)  #define COMPILE_WORK_SIZE (4096)
94    
95    /* The overrun tests check for a slightly smaller size so that they detect the
96    overrun before it actually does run off the end of the data block. */
97    
98    #define WORK_SIZE_CHECK (COMPILE_WORK_SIZE - 100)
99    
100    
101  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
102  are simple data values; negative values are for special things like \d and so  are simple data values; negative values are for special things like \d and so
# Line 183  string is built from string macros so th Line 188  string is built from string macros so th
188  platforms. */  platforms. */
189    
190  typedef struct verbitem {  typedef struct verbitem {
191    int   len;    int   len;                 /* Length of verb name */
192    int   op;    int   op;                  /* Op when no arg, or -1 if arg mandatory */
193      int   op_arg;              /* Op when arg present, or -1 if not allowed */
194  } verbitem;  } verbitem;
195    
196  static const char verbnames[] =  static const char verbnames[] =
197      "\0"                       /* Empty name is a shorthand for MARK */
198      STRING_MARK0
199    STRING_ACCEPT0    STRING_ACCEPT0
200    STRING_COMMIT0    STRING_COMMIT0
201    STRING_F0    STRING_F0
# Line 197  static const char verbnames[] = Line 205  static const char verbnames[] =
205    STRING_THEN;    STRING_THEN;
206    
207  static const verbitem verbs[] = {  static const verbitem verbs[] = {
208    { 6, OP_ACCEPT },    { 0, -1,        OP_MARK },
209    { 6, OP_COMMIT },    { 4, -1,        OP_MARK },
210    { 1, OP_FAIL },    { 6, OP_ACCEPT, -1 },
211    { 4, OP_FAIL },    { 6, OP_COMMIT, -1 },
212    { 5, OP_PRUNE },    { 1, OP_FAIL,   -1 },
213    { 4, OP_SKIP  },    { 4, OP_FAIL,   -1 },
214    { 4, OP_THEN  }    { 5, OP_PRUNE,  OP_PRUNE_ARG },
215      { 4, OP_SKIP,   OP_SKIP_ARG  },
216      { 4, OP_THEN,   OP_THEN_ARG  }
217  };  };
218    
219  static const int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
# Line 263  the number of relocations needed when a Line 273  the number of relocations needed when a
273  it is now one long string. We cannot use a table of offsets, because the  it is now one long string. We cannot use a table of offsets, because the
274  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we  lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
275  simply count through to the one we want - this isn't a performance issue  simply count through to the one we want - this isn't a performance issue
276  because these strings are used only when there is a compilation error.  because these strings are used only when there is a compilation error.
277    
278  Each substring ends with \0 to insert a null character. This includes the final  Each substring ends with \0 to insert a null character. This includes the final
279  substring, so that the whole string ends with \0\0, which can be detected when  substring, so that the whole string ends with \0\0, which can be detected when
280  counting through. */  counting through. */
281    
282  static const char error_texts[] =  static const char error_texts[] =
# Line 340  static const char error_texts[] = Line 350  static const char error_texts[] =
350    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
351    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
352    "a numbered reference must not be zero\0"    "a numbered reference must not be zero\0"
353    "(*VERB) with an argument is not supported\0"    "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
354    /* 60 */    /* 60 */
355    "(*VERB) not recognized\0"    "(*VERB) not recognized\0"
356    "number is too big\0"    "number is too big\0"
# Line 348  static const char error_texts[] = Line 358  static const char error_texts[] =
358    "digit expected after (?+\0"    "digit expected after (?+\0"
359    "] is an invalid data character in JavaScript compatibility mode\0"    "] is an invalid data character in JavaScript compatibility mode\0"
360    /* 65 */    /* 65 */
361    "different names for subpatterns of the same number are not allowed\0";    "different names for subpatterns of the same number are not allowed\0"
362      "(*MARK) must have an argument\0"
363      ;
364    
365  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
366  patterns. Note that the tables in chartables are dependent on the locale, and  patterns. Note that the tables in chartables are dependent on the locale, and
# Line 506  static const char * Line 518  static const char *
518  find_error_text(int n)  find_error_text(int n)
519  {  {
520  const char *s = error_texts;  const char *s = error_texts;
521  for (; n > 0; n--)  for (; n > 0; n--)
522    {    {
523    while (*s++ != 0) {};    while (*s++ != 0) {};
524    if (*s == 0) return "Error text not found (please report)";    if (*s == 0) return "Error text not found (please report)";
525    }    }
526  return s;  return s;
527  }  }
528    
# Line 1610  for (;;) Line 1622  for (;;)
1622    
1623    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
1624    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
1625    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1626      must add in its length. */
1627    
1628    else    else
1629      {      {
# Line 1634  for (;;) Line 1647  for (;;)
1647        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1648        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1649        break;        break;
1650    
1651          case OP_MARK:
1652          case OP_PRUNE_ARG:
1653          case OP_SKIP_ARG:
1654          case OP_THEN_ARG:
1655          code += code[1];
1656          break;
1657        }        }
1658    
1659      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1705  for (;;) Line 1725  for (;;)
1725    
1726    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
1727    repeated character types, we have to test for \p and \P, which have an extra    repeated character types, we have to test for \p and \P, which have an extra
1728    two bytes of parameters. */    two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
1729      must add in its length. */
1730    
1731    else    else
1732      {      {
# Line 1729  for (;;) Line 1750  for (;;)
1750        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1751        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;
1752        break;        break;
1753    
1754          case OP_MARK:
1755          case OP_PRUNE_ARG:
1756          case OP_SKIP_ARG:
1757          case OP_THEN_ARG:
1758          code += code[1];
1759          break;
1760        }        }
1761    
1762      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
# Line 1785  Arguments: Line 1813  Arguments:
1813    code        points to start of search    code        points to start of search
1814    endcode     points to where to stop    endcode     points to where to stop
1815    utf8        TRUE if in UTF8 mode    utf8        TRUE if in UTF8 mode
1816      cd          contains pointers to tables etc.
1817    
1818  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
1819  */  */
1820    
1821  static BOOL  static BOOL
1822  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,
1823      compile_data *cd)
1824  {  {
1825  register int c;  register int c;
1826  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);  for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);
# Line 1821  for (code = first_significant_code(code Line 1851  for (code = first_significant_code(code
1851      continue;      continue;
1852      }      }
1853    
1854      /* For a recursion/subroutine call, if its end has been reached, which
1855      implies a subroutine call, we can scan it. */
1856    
1857      if (c == OP_RECURSE)
1858        {
1859        BOOL empty_branch = FALSE;
1860        const uschar *scode = cd->start_code + GET(code, 1);
1861        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
1862        do
1863          {
1864          if (could_be_empty_branch(scode, endcode, utf8, cd))
1865            {
1866            empty_branch = TRUE;
1867            break;
1868            }
1869          scode += GET(scode, 1);
1870          }
1871        while (*scode == OP_ALT);
1872        if (!empty_branch) return FALSE;  /* All branches are non-empty */
1873        continue;
1874        }
1875    
1876    /* For other groups, scan the branches. */    /* For other groups, scan the branches. */
1877    
1878    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)    if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE || c == OP_COND)
# Line 1839  for (code = first_significant_code(code Line 1891  for (code = first_significant_code(code
1891        empty_branch = FALSE;        empty_branch = FALSE;
1892        do        do
1893          {          {
1894          if (!empty_branch && could_be_empty_branch(code, endcode, utf8))          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))
1895            empty_branch = TRUE;            empty_branch = TRUE;
1896          code += GET(code, 1);          code += GET(code, 1);
1897          }          }
# Line 1973  for (code = first_significant_code(code Line 2025  for (code = first_significant_code(code
2025      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];
2026      break;      break;
2027  #endif  #endif
2028    
2029        /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2030        string. */
2031    
2032        case OP_MARK:
2033        case OP_PRUNE_ARG:
2034        case OP_SKIP_ARG:
2035        case OP_THEN_ARG:
2036        code += code[1];
2037        break;
2038    
2039        /* None of the remaining opcodes are required to match a character. */
2040    
2041        default:
2042        break;
2043      }      }
2044    }    }
2045    
# Line 1995  Arguments: Line 2062  Arguments:
2062    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2063    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2064    utf8        TRUE if in UTF-8 mode    utf8        TRUE if in UTF-8 mode
2065      cd          pointers to tables etc
2066    
2067  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2068  */  */
2069    
2070  static BOOL  static BOOL
2071  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,
2072    BOOL utf8)    BOOL utf8, compile_data *cd)
2073  {  {
2074  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2075    {    {
2076    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))
2077      return FALSE;      return FALSE;
2078    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2079    }    }
# Line 2730  for (;; ptr++) Line 2798  for (;; ptr++)
2798  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
2799      if (code > cd->hwm) cd->hwm = code;                 /* High water info */      if (code > cd->hwm) cd->hwm = code;                 /* High water info */
2800  #endif  #endif
2801      if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */      if (code > cd->start_workspace + WORK_SIZE_CHECK)   /* Check for overrun */
2802        {        {
2803        *errorcodeptr = ERR52;        *errorcodeptr = ERR52;
2804        goto FAILED;        goto FAILED;
# Line 2779  for (;; ptr++) Line 2847  for (;; ptr++)
2847    /* In the real compile phase, just check the workspace used by the forward    /* In the real compile phase, just check the workspace used by the forward
2848    reference list. */    reference list. */
2849    
2850    else if (cd->hwm > cd->start_workspace + COMPILE_WORK_SIZE)    else if (cd->hwm > cd->start_workspace + WORK_SIZE_CHECK)
2851      {      {
2852      *errorcodeptr = ERR52;      *errorcodeptr = ERR52;
2853      goto FAILED;      goto FAILED;
# Line 4363  we set the flag only if there is a liter Line 4431  we set the flag only if there is a liter
4431            uschar *scode = bracode;            uschar *scode = bracode;
4432            do            do
4433              {              {
4434              if (could_be_empty_branch(scode, ketcode, utf8))              if (could_be_empty_branch(scode, ketcode, utf8, cd))
4435                {                {
4436                *bracode += OP_SBRA - OP_BRA;                *bracode += OP_SBRA - OP_BRA;
4437                break;                break;
# Line 4440  we set the flag only if there is a liter Line 4508  we set the flag only if there is a liter
4508    
4509          /* Because we are moving code along, we must ensure that any          /* Because we are moving code along, we must ensure that any
4510          pending recursive references are updated. */          pending recursive references are updated. */
4511    
4512          default:          default:
4513          *code = OP_END;          *code = OP_END;
4514          adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);          adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);
# Line 4479  we set the flag only if there is a liter Line 4547  we set the flag only if there is a liter
4547    
4548      /* First deal with various "verbs" that can be introduced by '*'. */      /* First deal with various "verbs" that can be introduced by '*'. */
4549    
4550      if (*(++ptr) == CHAR_ASTERISK && (cd->ctypes[ptr[1]] & ctype_letter) != 0)      if (*(++ptr) == CHAR_ASTERISK &&
4551             ((cd->ctypes[ptr[1]] & ctype_letter) != 0 || ptr[1] == ':'))
4552        {        {
4553        int i, namelen;        int i, namelen;
4554          int arglen = 0;
4555        const char *vn = verbnames;        const char *vn = verbnames;
4556        const uschar *name = ++ptr;        const uschar *name = ptr + 1;
4557          const uschar *arg = NULL;
4558        previous = NULL;        previous = NULL;
4559        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};
4560          namelen = ptr - name;
4561    
4562        if (*ptr == CHAR_COLON)        if (*ptr == CHAR_COLON)
4563          {          {
4564          *errorcodeptr = ERR59;   /* Not supported */          arg = ++ptr;
4565          goto FAILED;          while ((cd->ctypes[*ptr] & (ctype_letter|ctype_digit)) != 0
4566              || *ptr == '_') ptr++;
4567            arglen = ptr - arg;
4568          }          }
4569    
4570        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
4571          {          {
4572          *errorcodeptr = ERR60;          *errorcodeptr = ERR60;
4573          goto FAILED;          goto FAILED;
4574          }          }
4575        namelen = ptr - name;  
4576          /* Scan the table of verb names */
4577    
4578        for (i = 0; i < verbcount; i++)        for (i = 0; i < verbcount; i++)
4579          {          {
4580          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
# Line 4514  we set the flag only if there is a liter Line 4592  we set the flag only if there is a liter
4592                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
4593                }                }
4594              }              }
4595            *code++ = verbs[i].op;  
4596            break;            /* Handle the cases with/without an argument */
4597    
4598              if (arglen == 0)
4599                {
4600                if (verbs[i].op < 0)   /* Argument is mandatory */
4601                  {
4602                  *errorcodeptr = ERR66;
4603                  goto FAILED;
4604                  }
4605                *code++ = verbs[i].op;
4606                }
4607    
4608              else
4609                {
4610                if (verbs[i].op_arg < 0)   /* Argument is forbidden */
4611                  {
4612                  *errorcodeptr = ERR59;
4613                  goto FAILED;
4614                  }
4615                *code++ = verbs[i].op_arg;
4616                *code++ = arglen;
4617                memcpy(code, arg, arglen);
4618                code += arglen;
4619                *code++ = 0;
4620                }
4621    
4622              break;  /* Found verb, exit loop */
4623            }            }
4624    
4625          vn += verbs[i].len + 1;          vn += verbs[i].len + 1;
4626          }          }
4627        if (i < verbcount) continue;  
4628        *errorcodeptr = ERR60;        if (i < verbcount) continue;    /* Successfully handled a verb */
4629          *errorcodeptr = ERR60;          /* Verb not recognized */
4630        goto FAILED;        goto FAILED;
4631        }        }
4632    
# Line 5162  we set the flag only if there is a liter Line 5268  we set the flag only if there is a liter
5268                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
5269                  goto FAILED;                  goto FAILED;
5270                  }                  }
5271    
5272                /* 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
5273                offset below, what it actually inserted is the reference number                offset below, what it actually inserted is the reference number
5274                of the group. */                of the group. */
5275    
5276                called = cd->start_code + recno;                called = cd->start_code + recno;
5277                PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code);                PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code);
5278                }                }
# Line 5176  we set the flag only if there is a liter Line 5282  we set the flag only if there is a liter
5282              recursion that could loop for ever, and diagnose that case. */              recursion that could loop for ever, and diagnose that case. */
5283    
5284              else if (GET(called, 1) == 0 &&              else if (GET(called, 1) == 0 &&
5285                       could_be_empty(called, code, bcptr, utf8))                       could_be_empty(called, code, bcptr, utf8, cd))
5286                {                {
5287                *errorcodeptr = ERR40;                *errorcodeptr = ERR40;
5288                goto FAILED;                goto FAILED;
# Line 5303  we set the flag only if there is a liter Line 5409  we set the flag only if there is a liter
5409          }     /* End of switch for character following (? */          }     /* End of switch for character following (? */
5410        }       /* End of (? handling */        }       /* End of (? handling */
5411    
5412      /* Opening parenthesis not followed by '?'. If PCRE_NO_AUTO_CAPTURE is set,      /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
5413      all unadorned brackets become non-capturing and behave like (?:...)      is set, all unadorned brackets become non-capturing and behave like (?:...)
5414      brackets. */      brackets. */
5415    
5416      else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)      else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)

Legend:
Removed from v.500  
changed lines
  Added in v.512

  ViewVC Help
Powered by ViewVC 1.1.5