/[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 268 by ph10, Thu Nov 15 10:28:09 2007 UTC revision 340 by ph10, Fri Apr 18 20:00:21 2008 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-2007 University of Cambridge             Copyright (c) 1997-2008 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 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 241  static const char error_texts[] = Line 241  static const char error_texts[] =
241    /* 10 */    /* 10 */
242    "operand of unlimited repeat could match the empty string\0"  /** DEAD **/    "operand of unlimited repeat could match the empty string\0"  /** DEAD **/
243    "internal error: unexpected repeat\0"    "internal error: unexpected repeat\0"
244    "unrecognized character after (?\0"    "unrecognized character after (? or (?-\0"
245    "POSIX named classes are supported only within a class\0"    "POSIX named classes are supported only within a class\0"
246    "missing )\0"    "missing )\0"
247    /* 15 */    /* 15 */
# 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 after (?&";    "subpattern name expected\0"
305      "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 497  ptr--;                            /* Set Line 499  ptr--;                            /* Set
499    
500  if (c == 0) *errorcodeptr = ERR1;  if (c == 0) *errorcodeptr = ERR1;
501    
502  /* Non-alphamerics are literals. For digits or letters, do an initial lookup in  /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
503  a table. A non-zero result is something that can be returned immediately.  in a table. A non-zero result is something that can be returned immediately.
504  Otherwise further processing may be required. */  Otherwise further processing may be required. */
505    
506  #ifndef EBCDIC  /* ASCII coding */  #ifndef EBCDIC  /* ASCII coding */
507  else if (c < '0' || c > 'z') {}                           /* Not alphameric */  else if (c < '0' || c > 'z') {}                           /* Not alphanumeric */
508  else if ((i = escapes[c - '0']) != 0) c = i;  else if ((i = escapes[c - '0']) != 0) c = i;
509    
510  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
511  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphameric */  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */
512  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  c = i;
513  #endif  #endif
514    
# Line 530  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 564  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 610  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 723  else Line 748  else
748      break;      break;
749    
750      /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any      /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
751      other alphameric following \ is an error if PCRE_EXTRA was set; otherwise,      other alphanumeric following \ is an error if PCRE_EXTRA was set;
752      for Perl compatibility, it is a literal. This code looks a bit odd, but      otherwise, for Perl compatibility, it is a literal. This code looks a bit
753      there used to be some cases other than the default, and there may be again      odd, but there used to be some cases other than the default, and there may
754      in future, so I haven't "optimized" it. */      be again in future, so I haven't "optimized" it. */
755    
756      default:      default:
757      if ((options & PCRE_EXTRA) != 0) switch(c)      if ((options & PCRE_EXTRA) != 0) switch(c)
# Line 983  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 1507  for (;;) Line 1555  for (;;)
1555  can match the empty string or not. It is called from could_be_empty()  can match the empty string or not. It is called from could_be_empty()
1556  below and from compile_branch() when checking for an unlimited repeat of a  below and from compile_branch() when checking for an unlimited repeat of a
1557  group that can match nothing. Note that first_significant_code() skips over  group that can match nothing. Note that first_significant_code() skips over
1558  assertions. If we hit an unclosed bracket, we return "empty" - this means we've  backward and negative forward assertions when its final argument is TRUE. If we
1559  struck an inner bracket whose current branch will already have been scanned.  hit an unclosed bracket, we return "empty" - this means we've struck an inner
1560    bracket whose current branch will already have been scanned.
1561    
1562  Arguments:  Arguments:
1563    code        points to start of search    code        points to start of search
# Line 1530  for (code = first_significant_code(code Line 1579  for (code = first_significant_code(code
1579    
1580    c = *code;    c = *code;
1581    
1582      /* Skip over forward assertions; the other assertions are skipped by
1583      first_significant_code() with a TRUE final argument. */
1584    
1585      if (c == OP_ASSERT)
1586        {
1587        do code += GET(code, 1); while (*code == OP_ALT);
1588        c = *code;
1589        continue;
1590        }
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 1725  return TRUE; Line 1784  return TRUE;
1784  *************************************************/  *************************************************/
1785    
1786  /* This function is called when the sequence "[:" or "[." or "[=" is  /* This function is called when the sequence "[:" or "[." or "[=" is
1787  encountered in a character class. It checks whether this is followed by an  encountered in a character class. It checks whether this is followed by a
1788  optional ^ and then a sequence of letters, terminated by a matching ":]" or  sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
1789  ".]" or "=]".  reach an unescaped ']' without the special preceding character, return FALSE.
1790    
1791    Originally, this function only recognized a sequence of letters between the
1792    terminators, but it seems that Perl recognizes any sequence of characters,
1793    though of course unknown POSIX names are subsequently rejected. Perl gives an
1794    "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
1795    didn't consider this to be a POSIX class. Likewise for [:1234:].
1796    
1797    The problem in trying to be exactly like Perl is in the handling of escapes. We
1798    have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
1799    class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
1800    below handles the special case of \], but does not try to do any other escape
1801    processing. This makes it different from Perl for cases such as [:l\ower:]
1802    where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
1803    "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
1804    I think.
1805    
1806  Argument:  Arguments:
1807    ptr      pointer to the initial [    ptr      pointer to the initial [
1808    endptr   where to return the end pointer    endptr   where to return the end pointer
   cd       pointer to compile data  
1809    
1810  Returns:   TRUE or FALSE  Returns:   TRUE or FALSE
1811  */  */
1812    
1813  static BOOL  static BOOL
1814  check_posix_syntax(const uschar *ptr, const uschar **endptr, compile_data *cd)  check_posix_syntax(const uschar *ptr, const uschar **endptr)
1815  {  {
1816  int terminator;          /* Don't combine these lines; the Solaris cc */  int terminator;          /* Don't combine these lines; the Solaris cc */
1817  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
1818  if (*(++ptr) == '^') ptr++;  for (++ptr; *ptr != 0; ptr++)
 while ((cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;  
 if (*ptr == terminator && ptr[1] == ']')  
1819    {    {
1820    *endptr = ptr;    if (*ptr == '\\' && ptr[1] == ']') ptr++; else
1821    return TRUE;      {
1822        if (*ptr == ']') return FALSE;
1823        if (*ptr == terminator && ptr[1] == ']')
1824          {
1825          *endptr = ptr;
1826          return TRUE;
1827          }
1828        }
1829    }    }
1830  return FALSE;  return FALSE;
1831  }  }
# Line 1793  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 1828  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 2082  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 2345  uschar classbits[32]; Line 2423  uschar classbits[32];
2423  BOOL class_utf8;  BOOL class_utf8;
2424  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
2425  uschar *class_utf8data;  uschar *class_utf8data;
2426    uschar *class_utf8data_base;
2427  uschar utf8_char[6];  uschar utf8_char[6];
2428  #else  #else
2429  BOOL utf8 = FALSE;  BOOL utf8 = FALSE;
# Line 2384  req_caseopt = ((options & PCRE_CASELESS) Line 2463  req_caseopt = ((options & PCRE_CASELESS)
2463  for (;; ptr++)  for (;; ptr++)
2464    {    {
2465    BOOL negate_class;    BOOL negate_class;
2466    BOOL should_flip_negation;    BOOL should_flip_negation;
2467    BOOL possessive_quantifier;    BOOL possessive_quantifier;
2468    BOOL is_quantifier;    BOOL is_quantifier;
2469    BOOL is_recurse;    BOOL is_recurse;
# Line 2404  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 2599  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 2608  for (;; ptr++) Line 2697  for (;; ptr++)
2697      they are encountered at the top level, so we'll do that too. */      they are encountered at the top level, so we'll do that too. */
2698    
2699      if ((ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&      if ((ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&
2700          check_posix_syntax(ptr, &tempptr, cd))          check_posix_syntax(ptr, &tempptr))
2701        {        {
2702        *errorcodeptr = (ptr[1] == ':')? ERR13 : ERR31;        *errorcodeptr = (ptr[1] == ':')? ERR13 : ERR31;
2703        goto FAILED;        goto FAILED;
# Line 2633  for (;; ptr++) Line 2722  for (;; ptr++)
2722        else break;        else break;
2723        }        }
2724    
2725      /* If a class contains a negative special such as \S, we need to flip the      /* If a class contains a negative special such as \S, we need to flip the
2726      negation flag at the end, so that support for characters > 255 works      negation flag at the end, so that support for characters > 255 works
2727      correctly (they are all included in the class). */      correctly (they are all included in the class). */
2728    
2729      should_flip_negation = FALSE;      should_flip_negation = FALSE;
# Line 2656  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 */
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 2671  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
2766          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
2768          (which is on the stack). */
2769    
2770          if (lengthptr != NULL)
2771            {
2772            *lengthptr += class_utf8data - class_utf8data_base;
2773            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 2694  for (;; ptr++) Line 2796  for (;; ptr++)
2796    
2797        if (c == '[' &&        if (c == '[' &&
2798            (ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&            (ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&
2799            check_posix_syntax(ptr, &tempptr, cd))            check_posix_syntax(ptr, &tempptr))
2800          {          {
2801          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
2802          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
# Line 2711  for (;; ptr++) Line 2813  for (;; ptr++)
2813          if (*ptr == '^')          if (*ptr == '^')
2814            {            {
2815            local_negate = TRUE;            local_negate = TRUE;
2816            should_flip_negation = TRUE;  /* Note negative special */            should_flip_negation = TRUE;  /* Note negative special */
2817            ptr++;            ptr++;
2818            }            }
2819    
# Line 2786  for (;; ptr++) Line 2888  for (;; ptr++)
2888          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
2889          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
2890    
2891          if (-c == ESC_b) c = '\b';       /* \b is backslash in a class */          if (-c == ESC_b) c = '\b';       /* \b is backspace in a class */
2892          else if (-c == ESC_X) c = 'X';   /* \X is literal X in a class */          else if (-c == ESC_X) c = 'X';   /* \X is literal X in a class */
2893          else if (-c == ESC_R) c = 'R';   /* \R is literal R in a class */          else if (-c == ESC_R) c = 'R';   /* \R is literal R in a class */
2894          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (-c == ESC_Q)            /* Handle start of quoted string */
# Line 2814  for (;; ptr++) Line 2916  for (;; ptr++)
2916              continue;              continue;
2917    
2918              case ESC_D:              case ESC_D:
2919              should_flip_negation = TRUE;              should_flip_negation = TRUE;
2920              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];
2921              continue;              continue;
2922    
# Line 2823  for (;; ptr++) Line 2925  for (;; ptr++)
2925              continue;              continue;
2926    
2927              case ESC_W:              case ESC_W:
2928              should_flip_negation = TRUE;              should_flip_negation = TRUE;
2929              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
2930              continue;              continue;
2931    
# Line 2833  for (;; ptr++) Line 2935  for (;; ptr++)
2935              continue;              continue;
2936    
2937              case ESC_S:              case ESC_S:
2938              should_flip_negation = TRUE;              should_flip_negation = TRUE;
2939              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
2940              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
2941              continue;              continue;
2942    
             case ESC_E: /* Perl ignores an orphan \E */  
             continue;  
   
2943              default:    /* Not recognized; fall through */              default:    /* Not recognized; fall through */
2944              break;      /* Need "default" setting to stop compiler warning. */              break;      /* Need "default" setting to stop compiler warning. */
2945              }              }
# Line 3075  for (;; ptr++) Line 3174  for (;; ptr++)
3174            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3175            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
3176    
3177            /* \b is backslash; \X is literal X; \R is literal R; any other            /* \b is backspace; \X is literal X; \R is literal R; any other
3178            special means the '-' was literal */            special means the '-' was literal */
3179    
3180            if (d < 0)            if (d < 0)
# Line 3339  we set the flag only if there is a liter Line 3438  we set the flag only if there is a liter
3438      zeroreqbyte = reqbyte;      zeroreqbyte = reqbyte;
3439    
3440      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
3441      extended class, with its own opcode, unless there was a negated special      extended class, with its own opcode, unless there was a negated special
3442      such as \S in the class, because in that case all characters > 255 are in      such as \S in the class, because in that case all characters > 255 are in
3443      the class, so any that were explicitly given as well can be ignored. If      the class, so any that were explicitly given as well can be ignored. If
3444      (when there are explicit characters > 255 that must be listed) there are no      (when there are explicit characters > 255 that must be listed) there are no
3445      characters < 256, we can omit the bitmap in the actual compiled code. */      characters < 256, we can omit the bitmap in the actual compiled code. */
3446    
# Line 3372  we set the flag only if there is a liter Line 3471  we set the flag only if there is a liter
3471        }        }
3472  #endif  #endif
3473    
3474      /* If there are no characters > 255, set the opcode to OP_CLASS or      /* If there are no characters > 255, set the opcode to OP_CLASS or
3475      OP_NCLASS, depending on whether the whole class was negated and whether      OP_NCLASS, depending on whether the whole class was negated and whether
3476      there were negative specials such as \S in the class. Then copy the 32-byte      there were negative specials such as \S in the class. Then copy the 32-byte
3477      map into the code vector, negating it if necessary. */      map into the code vector, negating it if necessary. */
3478    
3479      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
3480      if (negate_class)      if (negate_class)
3481        {        {
# Line 3778  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 4020  we set the flag only if there is a liter Line 4129  we set the flag only if there is a liter
4129        int len;        int len;
4130        if (*tempcode == OP_EXACT || *tempcode == OP_TYPEEXACT ||        if (*tempcode == OP_EXACT || *tempcode == OP_TYPEEXACT ||
4131            *tempcode == OP_NOTEXACT)            *tempcode == OP_NOTEXACT)
4132          tempcode += _pcre_OP_lengths[*tempcode];          tempcode += _pcre_OP_lengths[*tempcode] +
4133              ((*tempcode == OP_TYPEEXACT &&
4134                 (tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP))? 2:0);
4135        len = code - tempcode;        len = code - tempcode;
4136        if (len > 0) switch (*tempcode)        if (len > 0) switch (*tempcode)
4137          {          {
# Line 4072  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 4247  we set the flag only if there is a liter Line 4358  we set the flag only if there is a liter
4358              *errorcodeptr = ERR58;              *errorcodeptr = ERR58;
4359              goto FAILED;              goto FAILED;
4360              }              }
4361            if (refsign == '-')            recno = (refsign == '-')?
4362                cd->bracount - recno + 1 : recno +cd->bracount;
4363              if (recno <= 0 || recno > cd->final_bracount)
4364              {              {
4365              recno = cd->bracount - recno + 1;              *errorcodeptr = ERR15;
4366              if (recno <= 0)              goto FAILED;
               {  
               *errorcodeptr = ERR15;  
               goto FAILED;  
               }  
4367              }              }
           else recno += cd->bracount;  
4368            PUT2(code, 2+LINK_SIZE, recno);            PUT2(code, 2+LINK_SIZE, recno);
4369            break;            break;
4370            }            }
# Line 4328  we set the flag only if there is a liter Line 4436  we set the flag only if there is a liter
4436            skipbytes = 1;            skipbytes = 1;
4437            }            }
4438    
4439          /* Check for the "name" actually being a subpattern number. */          /* Check for the "name" actually being a subpattern number. We are
4440            in the second pass here, so final_bracount is set. */
4441    
4442          else if (recno > 0)          else if (recno > 0 && recno <= cd->final_bracount)
4443            {            {
4444            PUT2(code, 2+LINK_SIZE, recno);            PUT2(code, 2+LINK_SIZE, recno);
4445            }            }
# Line 4524  we set the flag only if there is a liter Line 4633  we set the flag only if there is a liter
4633    
4634          /* We come here from the Python syntax above that handles both          /* We come here from the Python syntax above that handles both
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). */          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}
4638            .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 4540  we set the flag only if there is a liter Line 4651  we set the flag only if there is a liter
4651              {              {
4652              *errorcodeptr = ERR62;              *errorcodeptr = ERR62;
4653              goto FAILED;              goto FAILED;
4654              }              }
4655            if (*ptr != terminator)            if (*ptr != terminator)
4656              {              {
4657              *errorcodeptr = ERR42;              *errorcodeptr = ERR42;
# Line 4554  we set the flag only if there is a liter Line 4665  we set the flag only if there is a liter
4665            recno = 0;            recno = 0;
4666            }            }
4667    
4668          /* 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
4669          first, and then check that we have reached the end of the name in the          first, and then check that we have reached the end of the name in the
4670          table. That way, if the name that is longer than any in the table,          table. That way, if the name that is longer than any in the table,
4671          the comparison will fail without reading beyond the table entry. */          the comparison will fail without reading beyond the table entry. */
4672    
# Line 4565  we set the flag only if there is a liter Line 4676  we set the flag only if there is a liter
4676            for (i = 0; i < cd->names_found; i++)            for (i = 0; i < cd->names_found; i++)
4677              {              {
4678              if (strncmp((char *)name, (char *)slot+2, namelen) == 0 &&              if (strncmp((char *)name, (char *)slot+2, namelen) == 0 &&
4679                  slot[2+namelen] == 0)                  slot[2+namelen] == 0)
4680                break;                break;
4681              slot += cd->name_entry_size;              slot += cd->name_entry_size;
4682              }              }
# Line 4602  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) == '+') ptr++;            if ((refsign = *ptr) == '+')
4727                {
4728                ptr++;
4729                if ((digitab[*ptr] & ctype_digit) == 0)
4730                  {
4731                  *errorcodeptr = ERR63;
4732                  goto FAILED;
4733                  }
4734                }
4735            else if (refsign == '-')            else if (refsign == '-')
4736              {              {
4737              if ((digitab[ptr[1]] & ctype_digit) == 0)              if ((digitab[ptr[1]] & ctype_digit) == 0)
# Line 4615  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 5011  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 5038  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 5769  to fill in forward references to subpatt Line 5955  to fill in forward references to subpatt
5955    
5956  uschar cworkspace[COMPILE_WORK_SIZE];  uschar cworkspace[COMPILE_WORK_SIZE];
5957    
   
5958  /* Set this early so that early errors get offset 0. */  /* Set this early so that early errors get offset 0. */
5959    
5960  ptr = (const uschar *)pattern;  ptr = (const uschar *)pattern;
# Line 5930  to compile parts of the pattern into; th Line 6115  to compile parts of the pattern into; th
6115  no longer needed, so hopefully this workspace will never overflow, though there  no longer needed, so hopefully this workspace will never overflow, though there
6116  is a test for its doing so. */  is a test for its doing so. */
6117    
6118  cd->bracount = 0;  cd->bracount = cd->final_bracount = 0;
6119  cd->names_found = 0;  cd->names_found = 0;
6120  cd->name_entry_size = 0;  cd->name_entry_size = 0;
6121  cd->name_table = NULL;  cd->name_table = NULL;
# Line 6007  field. Reset the bracket count and the n Line 6192  field. Reset the bracket count and the n
6192  field; this time it's used for remembering forward references to subpatterns.  field; this time it's used for remembering forward references to subpatterns.
6193  */  */
6194    
6195    cd->final_bracount = cd->bracount;  /* Save for checking forward references */
6196  cd->bracount = 0;  cd->bracount = 0;
6197  cd->names_found = 0;  cd->names_found = 0;
6198  cd->name_table = (uschar *)re + re->name_table_offset;  cd->name_table = (uschar *)re + re->name_table_offset;
# Line 6057  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.268  
changed lines
  Added in v.340

  ViewVC Help
Powered by ViewVC 1.1.5