/[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 305 by ph10, Sun Jan 20 20:07:32 2008 UTC revision 340 by ph10, Fri Apr 18 20:00:21 2008 UTC
# Line 158  static const char verbnames[] = Line 158  static const char verbnames[] =
158    "SKIP\0"    "SKIP\0"
159    "THEN";    "THEN";
160    
161  static verbitem verbs[] = {  static const verbitem verbs[] = {
162    { 6, OP_ACCEPT },    { 6, OP_ACCEPT },
163    { 6, OP_COMMIT },    { 6, OP_COMMIT },
164    { 1, OP_FAIL },    { 1, OP_FAIL },
# Line 168  static verbitem verbs[] = { Line 168  static verbitem verbs[] = {
168    { 4, OP_THEN  }    { 4, OP_THEN  }
169  };  };
170    
171  static int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
172    
173    
174  /* Tables of names of POSIX character classes and their lengths. The names are  /* Tables of names of POSIX character classes and their lengths. The names are
# Line 295  static const char error_texts[] = Line 295  static const char error_texts[] =
295    /* 55 */    /* 55 */
296    "repeating a DEFINE group is not allowed\0"    "repeating a DEFINE group is not allowed\0"
297    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
298    "\\g is not followed by a braced name or an optionally braced non-zero number\0"    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
299    "(?+ or (?- or (?(+ or (?(- must be followed by a non-zero number\0"    "a numbered reference must not be zero\0"
300    "(*VERB) with an argument is not supported\0"    "(*VERB) with an argument is not supported\0"
301    /* 60 */    /* 60 */
302    "(*VERB) not recognized\0"    "(*VERB) not recognized\0"
303    "number is too big\0"    "number is too big\0"
304    "subpattern name expected\0"    "subpattern name expected\0"
305    "digit expected after (?+";    "digit expected after (?+\0"
306      "] is an invalid data character in JavaScript compatibility mode";
307    
308    
309  /* 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 531  else Line 532  else
532      *errorcodeptr = ERR37;      *errorcodeptr = ERR37;
533      break;      break;
534    
535      /* \g must be followed by a number, either plain or braced. If positive, it      /* \g must be followed by one of a number of specific things:
536      is an absolute backreference. If negative, it is a relative backreference.  
537      This is a Perl 5.10 feature. Perl 5.10 also supports \g{name} as a      (1) A number, either plain or braced. If positive, it is an absolute
538      reference to a named group. This is part of Perl's movement towards a      backreference. If negative, it is a relative backreference. This is a Perl
539      unified syntax for back references. As this is synonymous with \k{name}, we      5.10 feature.
540      fudge it up by pretending it really was \k. */  
541        (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
542        is part of Perl's movement towards a unified syntax for back references. As
543        this is synonymous with \k{name}, we fudge it up by pretending it really
544        was \k.
545    
546        (3) For Oniguruma compatibility we also support \g followed by a name or a
547        number either in angle brackets or in single quotes. However, these are
548        (possibly recursive) subroutine calls, _not_ backreferences. Just return
549        the -ESC_g code (cf \k). */
550    
551      case 'g':      case 'g':
552        if (ptr[1] == '<' || ptr[1] == '\'')
553          {
554          c = -ESC_g;
555          break;
556          }
557    
558        /* Handle the Perl-compatible cases */
559    
560      if (ptr[1] == '{')      if (ptr[1] == '{')
561        {        {
562        const uschar *p;        const uschar *p;
# Line 565  else Line 583  else
583      while ((digitab[ptr[1]] & ctype_digit) != 0)      while ((digitab[ptr[1]] & ctype_digit) != 0)
584        c = c * 10 + *(++ptr) - '0';        c = c * 10 + *(++ptr) - '0';
585    
586      if (c < 0)      if (c < 0)   /* Integer overflow */
587        {        {
588        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
589        break;        break;
590        }        }
591    
592      if (c == 0 || (braced && *(++ptr) != '}'))      if (braced && *(++ptr) != '}')
593        {        {
594        *errorcodeptr = ERR57;        *errorcodeptr = ERR57;
595        break;        break;
596        }        }
597    
598        if (c == 0)
599          {
600          *errorcodeptr = ERR58;
601          break;
602          }
603    
604      if (negated)      if (negated)
605        {        {
# Line 611  else Line 635  else
635        c -= '0';        c -= '0';
636        while ((digitab[ptr[1]] & ctype_digit) != 0)        while ((digitab[ptr[1]] & ctype_digit) != 0)
637          c = c * 10 + *(++ptr) - '0';          c = c * 10 + *(++ptr) - '0';
638        if (c < 0)        if (c < 0)    /* Integer overflow */
639          {          {
640          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
641          break;          break;
# Line 984  for (; *ptr != 0; ptr++) Line 1008  for (; *ptr != 0; ptr++)
1008      continue;      continue;
1009      }      }
1010    
1011    /* Skip over character classes */    /* Skip over character classes; this logic must be similar to the way they
1012      are handled for real. If the first character is '^', skip it. Also, if the
1013      first few characters (either before or after ^) are \Q\E or \E we skip them
1014      too. This makes for compatibility with Perl. */
1015    
1016    if (*ptr == '[')    if (*ptr == '[')
1017      {      {
1018        BOOL negate_class = FALSE;
1019        for (;;)
1020          {
1021          int c = *(++ptr);
1022          if (c == '\\')
1023            {
1024            if (ptr[1] == 'E') ptr++;
1025              else if (strncmp((const char *)ptr+1, "Q\\E", 3) == 0) ptr += 3;
1026                else break;
1027            }
1028          else if (!negate_class && c == '^')
1029            negate_class = TRUE;
1030          else break;
1031          }
1032    
1033        /* If the next character is ']', it is a data character that must be
1034        skipped. */
1035    
1036        if (ptr[1] == ']') ptr++;
1037    
1038      while (*(++ptr) != ']')      while (*(++ptr) != ']')
1039        {        {
1040        if (*ptr == 0) return -1;        if (*ptr == 0) return -1;
# Line 1544  for (code = first_significant_code(code Line 1591  for (code = first_significant_code(code
1591    
1592    /* Groups with zero repeats can of course be empty; skip them. */    /* Groups with zero repeats can of course be empty; skip them. */
1593    
1594    if (c == OP_BRAZERO || c == OP_BRAMINZERO)    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO)
1595      {      {
1596      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1597      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
# Line 1824  return -1; Line 1871  return -1;
1871  that is referenced. This means that groups can be replicated for fixed  that is referenced. This means that groups can be replicated for fixed
1872  repetition simply by copying (because the recursion is allowed to refer to  repetition simply by copying (because the recursion is allowed to refer to
1873  earlier groups that are outside the current group). However, when a group is  earlier groups that are outside the current group). However, when a group is
1874  optional (i.e. the minimum quantifier is zero), OP_BRAZERO is inserted before  optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
1875  it, after it has been compiled. This means that any OP_RECURSE items within it  inserted before it, after it has been compiled. This means that any OP_RECURSE
1876  that refer to the group itself or any contained groups have to have their  items within it that refer to the group itself or any contained groups have to
1877  offsets adjusted. That one of the jobs of this function. Before it is called,  have their offsets adjusted. That one of the jobs of this function. Before it
1878  the partially compiled regex must be temporarily terminated with OP_END.  is called, the partially compiled regex must be temporarily terminated with
1879    OP_END.
1880    
1881  This function has been extended with the possibility of forward references for  This function has been extended with the possibility of forward references for
1882  recursions and subroutine calls. It must also check the list of such references  recursions and subroutine calls. It must also check the list of such references
# Line 1859  while ((ptr = (uschar *)find_recurse(ptr Line 1907  while ((ptr = (uschar *)find_recurse(ptr
1907    
1908    /* See if this recursion is on the forward reference list. If so, adjust the    /* See if this recursion is on the forward reference list. If so, adjust the
1909    reference. */    reference. */
1910    
1911    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
1912      {      {
1913      offset = GET(hc, 0);      offset = GET(hc, 0);
# Line 2113  if (next >= 0) switch(op_code) Line 2161  if (next >= 0) switch(op_code)
2161    /* For OP_NOT, "item" must be a single-byte character. */    /* For OP_NOT, "item" must be a single-byte character. */
2162    
2163    case OP_NOT:    case OP_NOT:
   if (next < 0) return FALSE;  /* Not a character */  
2164    if (item == next) return TRUE;    if (item == next) return TRUE;
2165    if ((options & PCRE_CASELESS) == 0) return FALSE;    if ((options & PCRE_CASELESS) == 0) return FALSE;
2166  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 2436  for (;; ptr++) Line 2483  for (;; ptr++)
2483    /* Get next byte in the pattern */    /* Get next byte in the pattern */
2484    
2485    c = *ptr;    c = *ptr;
2486    
2487    /* If we are in the pre-compile phase, accumulate the length used for the    /* If we are in the pre-compile phase, accumulate the length used for the
2488    previous cycle of this loop. */    previous cycle of this loop. */
2489    
# Line 2631  for (;; ptr++) Line 2678  for (;; ptr++)
2678      opcode is compiled. It may optionally have a bit map for characters < 256,      opcode is compiled. It may optionally have a bit map for characters < 256,
2679      but those above are are explicitly listed afterwards. A flag byte tells      but those above are are explicitly listed afterwards. A flag byte tells
2680      whether the bitmap is present, and whether this is a negated class or not.      whether the bitmap is present, and whether this is a negated class or not.
2681      */  
2682        In JavaScript compatibility mode, an isolated ']' causes an error. In
2683        default (Perl) mode, it is treated as a data character. */
2684    
2685        case ']':
2686        if ((cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
2687          {
2688          *errorcodeptr = ERR64;
2689          goto FAILED;
2690          }
2691        goto NORMAL_CHAR;
2692    
2693      case '[':      case '[':
2694      previous = code;      previous = code;
# Line 2688  for (;; ptr++) Line 2745  for (;; ptr++)
2745  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2746      class_utf8 = FALSE;                       /* No chars >= 256 */      class_utf8 = FALSE;                       /* No chars >= 256 */
2747      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */
2748      class_utf8data_base = class_utf8data;     /* For resetting in pass 1 */      class_utf8data_base = class_utf8data;     /* For resetting in pass 1 */
2749  #endif  #endif
2750    
2751      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
# Line 2704  for (;; ptr++) Line 2761  for (;; ptr++)
2761          {                           /* Braces are required because the */          {                           /* Braces are required because the */
2762          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
2763          }          }
2764    
2765        /* In the pre-compile phase, accumulate the length of any UTF-8 extra        /* In the pre-compile phase, accumulate the length of any UTF-8 extra
2766        data and reset the pointer. This is so that very large classes that        data and reset the pointer. This is so that very large classes that
2767        contain a zillion UTF-8 characters no longer overwrite the work space        contain a zillion UTF-8 characters no longer overwrite the work space
2768        (which is on the stack). */        (which is on the stack). */
2769    
2770        if (lengthptr != NULL)        if (lengthptr != NULL)
2771          {          {
2772          *lengthptr += class_utf8data - class_utf8data_base;          *lengthptr += class_utf8data - class_utf8data_base;
2773          class_utf8data = class_utf8data_base;          class_utf8data = class_utf8data_base;
2774          }          }
2775    
2776  #endif  #endif
2777    
2778        /* Inside \Q...\E everything is literal except \E */        /* Inside \Q...\E everything is literal except \E */
# Line 3820  we set the flag only if there is a liter Line 3877  we set the flag only if there is a liter
3877    
3878        if (repeat_min == 0)        if (repeat_min == 0)
3879          {          {
3880          /* If the maximum is also zero, we just omit the group from the output          /* If the maximum is also zero, we used to just omit the group from the
3881          altogether. */          output altogether, like this:
   
         if (repeat_max == 0)  
           {  
           code = previous;  
           goto END_REPEAT;  
           }  
3882    
3883          /* If the maximum is 1 or unlimited, we just have to stick in the          ** if (repeat_max == 0)
3884          BRAZERO and do no more at this point. However, we do need to adjust          **   {
3885          any OP_RECURSE calls inside the group that refer to the group itself or          **   code = previous;
3886          any internal or forward referenced group, because the offset is from          **   goto END_REPEAT;
3887          the start of the whole regex. Temporarily terminate the pattern while          **   }
3888          doing this. */  
3889            However, that fails when a group is referenced as a subroutine from
3890            elsewhere in the pattern, so now we stick in OP_SKIPZERO in front of it
3891            so that it is skipped on execution. As we don't have a list of which
3892            groups are referenced, we cannot do this selectively.
3893    
3894            If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
3895            and do no more at this point. However, we do need to adjust any
3896            OP_RECURSE calls inside the group that refer to the group itself or any
3897            internal or forward referenced group, because the offset is from the
3898            start of the whole regex. Temporarily terminate the pattern while doing
3899            this. */
3900    
3901          if (repeat_max <= 1)          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */
3902            {            {
3903            *code = OP_END;            *code = OP_END;
3904            adjust_recurse(previous, 1, utf8, cd, save_hwm);            adjust_recurse(previous, 1, utf8, cd, save_hwm);
3905            memmove(previous+1, previous, len);            memmove(previous+1, previous, len);
3906            code++;            code++;
3907              if (repeat_max == 0)
3908                {
3909                *previous++ = OP_SKIPZERO;
3910                goto END_REPEAT;
3911                }
3912            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
3913            }            }
3914    
# Line 4116  we set the flag only if there is a liter Line 4183  we set the flag only if there is a liter
4183      bravalue = OP_CBRA;      bravalue = OP_CBRA;
4184      save_hwm = cd->hwm;      save_hwm = cd->hwm;
4185      reset_bracount = FALSE;      reset_bracount = FALSE;
4186    
4187      /* First deal with various "verbs" that can be introduced by '*'. */      /* First deal with various "verbs" that can be introduced by '*'. */
4188    
4189      if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)      if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)
# Line 4568  we set the flag only if there is a liter Line 4635  we set the flag only if there is a liter
4635          references (?P=name) and recursion (?P>name), as well as falling          references (?P=name) and recursion (?P>name), as well as falling
4636          through from the Perl recursion syntax (?&name). We also come here from          through from the Perl recursion syntax (?&name). We also come here from
4637          the Perl \k<name> or \k'name' back reference syntax and the \k{name}          the Perl \k<name> or \k'name' back reference syntax and the \k{name}
4638          .NET syntax. */          .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */
4639    
4640          NAMED_REF_OR_RECURSE:          NAMED_REF_OR_RECURSE:
4641          name = ++ptr;          name = ++ptr;
# Line 4646  we set the flag only if there is a liter Line 4713  we set the flag only if there is a liter
4713          case '5': case '6': case '7': case '8': case '9':   /* subroutine */          case '5': case '6': case '7': case '8': case '9':   /* subroutine */
4714            {            {
4715            const uschar *called;            const uschar *called;
4716              terminator = ')';
4717    
4718              /* Come here from the \g<...> and \g'...' code (Oniguruma
4719              compatibility). However, the syntax has been checked to ensure that
4720              the ... are a (signed) number, so that neither ERR63 nor ERR29 will
4721              be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY
4722              ever be taken. */
4723    
4724              HANDLE_NUMERICAL_RECURSION:
4725    
4726            if ((refsign = *ptr) == '+')            if ((refsign = *ptr) == '+')
4727              {              {
# Line 4667  we set the flag only if there is a liter Line 4743  we set the flag only if there is a liter
4743            while((digitab[*ptr] & ctype_digit) != 0)            while((digitab[*ptr] & ctype_digit) != 0)
4744              recno = recno * 10 + *ptr++ - '0';              recno = recno * 10 + *ptr++ - '0';
4745    
4746            if (*ptr != ')')            if (*ptr != terminator)
4747              {              {
4748              *errorcodeptr = ERR29;              *errorcodeptr = ERR29;
4749              goto FAILED;              goto FAILED;
# Line 5063  we set the flag only if there is a liter Line 5139  we set the flag only if there is a liter
5139      back references and those types that consume a character may be repeated.      back references and those types that consume a character may be repeated.
5140      We can test for values between ESC_b and ESC_Z for the latter; this may      We can test for values between ESC_b and ESC_Z for the latter; this may
5141      have to change if any new ones are ever created. */      have to change if any new ones are ever created. */
5142    
5143      case '\\':      case '\\':
5144      tempptr = ptr;      tempptr = ptr;
5145      c = check_escape(&ptr, errorcodeptr, cd->bracount, options, FALSE);      c = check_escape(&ptr, errorcodeptr, cd->bracount, options, FALSE);
# Line 5090  we set the flag only if there is a liter Line 5166  we set the flag only if there is a liter
5166    
5167        zerofirstbyte = firstbyte;        zerofirstbyte = firstbyte;
5168        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
5169    
5170          /* \g<name> or \g'name' is a subroutine call by name and \g<n> or \g'n'
5171          is a subroutine call by number (Oniguruma syntax). In fact, the value
5172          -ESC_g is returned only for these cases. So we don't need to check for <
5173          or ' if the value is -ESC_g. For the Perl syntax \g{n} the value is
5174          -ESC_REF+n, and for the Perl syntax \g{name} the result is -ESC_k (as
5175          that is a synonym for a named back reference). */
5176    
5177          if (-c == ESC_g)
5178            {
5179            const uschar *p;
5180            save_hwm = cd->hwm;   /* Normally this is set when '(' is read */
5181            terminator = (*(++ptr) == '<')? '>' : '\'';
5182    
5183            /* These two statements stop the compiler for warning about possibly
5184            unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In
5185            fact, because we actually check for a number below, the paths that
5186            would actually be in error are never taken. */
5187    
5188            skipbytes = 0;
5189            reset_bracount = FALSE;
5190    
5191            /* Test for a name */
5192    
5193            if (ptr[1] != '+' && ptr[1] != '-')
5194              {
5195              BOOL isnumber = TRUE;
5196              for (p = ptr + 1; *p != 0 && *p != terminator; p++)
5197                {
5198                if ((cd->ctypes[*p] & ctype_digit) == 0) isnumber = FALSE;
5199                if ((cd->ctypes[*p] & ctype_word) == 0) break;
5200                }
5201              if (*p != terminator)
5202                {
5203                *errorcodeptr = ERR57;
5204                break;
5205                }
5206              if (isnumber)
5207                {
5208                ptr++;
5209                goto HANDLE_NUMERICAL_RECURSION;
5210                }
5211              is_recurse = TRUE;
5212              goto NAMED_REF_OR_RECURSE;
5213              }
5214    
5215            /* Test a signed number in angle brackets or quotes. */
5216    
5217            p = ptr + 2;
5218            while ((digitab[*p] & ctype_digit) != 0) p++;
5219            if (*p != terminator)
5220              {
5221              *errorcodeptr = ERR57;
5222              break;
5223              }
5224            ptr++;
5225            goto HANDLE_NUMERICAL_RECURSION;
5226            }
5227    
5228        /* \k<name> or \k'name' is a back reference by name (Perl syntax).        /* \k<name> or \k'name' is a back reference by name (Perl syntax).
5229        We also support \k{name} (.NET syntax) */        We also support \k{name} (.NET syntax) */
# Line 6109  while (errorcode == 0 && cd->hwm > cwork Line 6243  while (errorcode == 0 && cd->hwm > cwork
6243    if (groupptr == NULL) errorcode = ERR53;    if (groupptr == NULL) errorcode = ERR53;
6244      else PUT(((uschar *)codestart), offset, groupptr - codestart);      else PUT(((uschar *)codestart), offset, groupptr - codestart);
6245    }    }
6246    
6247  /* Give an error if there's back reference to a non-existent capturing  /* Give an error if there's back reference to a non-existent capturing
6248  subpattern. */  subpattern. */
6249    

Legend:
Removed from v.305  
changed lines
  Added in v.340

  ViewVC Help
Powered by ViewVC 1.1.5