/[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 240 by ph10, Tue Sep 11 15:47:20 2007 UTC revision 276 by ph10, Wed Nov 21 15:39:20 2007 UTC
# Line 140  static const short int escapes[] = { Line 140  static const short int escapes[] = {
140  #endif  #endif
141    
142    
143  /* Table of special "verbs" like (*PRUNE). This is a short table, so it is  /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
144  searched linearly. Put all the names into a single string, in order to reduce  searched linearly. Put all the names into a single string, in order to reduce
145  the number of relocations when a shared library is dynamically linked. */  the number of relocations when a shared library is dynamically linked. */
146    
147  typedef struct verbitem {  typedef struct verbitem {
# Line 150  typedef struct verbitem { Line 150  typedef struct verbitem {
150  } verbitem;  } verbitem;
151    
152  static const char verbnames[] =  static const char verbnames[] =
153    "ACCEPT\0"    "ACCEPT\0"
154    "COMMIT\0"    "COMMIT\0"
155    "F\0"    "F\0"
156    "FAIL\0"    "FAIL\0"
157    "PRUNE\0"    "PRUNE\0"
158    "SKIP\0"    "SKIP\0"
159    "THEN";    "THEN";
160    
161  static verbitem verbs[] = {  static verbitem verbs[] = {
162    { 6, OP_ACCEPT },    { 6, OP_ACCEPT },
# Line 171  static verbitem verbs[] = { Line 171  static verbitem verbs[] = {
171  static int verbcount = sizeof(verbs)/sizeof(verbitem);  static 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
175  now all in a single string, to reduce the number of relocations when a shared  now all in a single string, to reduce the number of relocations when a shared
176  library is dynamically loaded. The list of lengths is terminated by a zero  library is dynamically loaded. The list of lengths is terminated by a zero
177  length entry. The first three must be alpha, lower, upper, as this is assumed  length entry. The first three must be alpha, lower, upper, as this is assumed
178  for handling case independence. */  for handling case independence. */
179    
180  static const char posix_names[] =  static const char posix_names[] =
181    "alpha\0"  "lower\0"  "upper\0"  "alnum\0"  "ascii\0"  "blank\0"    "alpha\0"  "lower\0"  "upper\0"  "alnum\0"  "ascii\0"  "blank\0"
182    "cntrl\0"  "digit\0"  "graph\0"  "print\0"  "punct\0"  "space\0"    "cntrl\0"  "digit\0"  "graph\0"  "print\0"  "punct\0"  "space\0"
183    "word\0"   "xdigit";    "word\0"   "xdigit";
184    
185  static const uschar posix_name_lengths[] = {  static const uschar posix_name_lengths[] = {
# Line 219  static const int posix_class_maps[] = { Line 219  static const int posix_class_maps[] = {
219  /* The texts of compile-time error messages. These are "char *" because they  /* The texts of compile-time error messages. These are "char *" because they
220  are passed to the outside world. Do not ever re-use any error number, because  are passed to the outside world. Do not ever re-use any error number, because
221  they are documented. Always add a new error instead. Messages marked DEAD below  they are documented. Always add a new error instead. Messages marked DEAD below
222  are no longer used. This used to be a table of strings, but in order to reduce  are no longer used. This used to be a table of strings, but in order to reduce
223  the number of relocations needed when a shared library is loaded dynamically,  the number of relocations needed when a shared library is loaded dynamically,
224  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
225  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
226  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
227  because these strings are used only when there is a compilation error. */  because these strings are used only when there is a compilation error. */
228    
229  static const char error_texts[] =  static const char error_texts[] =
# 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 300  static const char error_texts[] = Line 300  static const char error_texts[] =
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";    "number is too big\0"
304      "subpattern name expected\0"
305      "digit expected after (?+";
306    
307    
308  /* 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 439  static BOOL Line 441  static BOOL
441  *            Find an error text                  *  *            Find an error text                  *
442  *************************************************/  *************************************************/
443    
444  /* The error texts are now all in one long string, to save on relocations. As  /* The error texts are now all in one long string, to save on relocations. As
445  some of the text is of unknown length, we can't use a table of offsets.  some of the text is of unknown length, we can't use a table of offsets.
446  Instead, just count through the strings. This is not a performance issue  Instead, just count through the strings. This is not a performance issue
447  because it happens only when there has been a compilation error.  because it happens only when there has been a compilation error.
448    
449  Argument:   the error number  Argument:   the error number
# Line 452  static const char * Line 454  static const char *
454  find_error_text(int n)  find_error_text(int n)
455  {  {
456  const char *s = error_texts;  const char *s = error_texts;
457  for (; n > 0; n--) while (*s++ != 0);  for (; n > 0; n--) while (*s++ != 0);
458  return s;  return s;
459  }  }
460    
# Line 496  ptr--; /* Set Line 498  ptr--; /* Set
498    
499  if (c == 0) *errorcodeptr = ERR1;  if (c == 0) *errorcodeptr = ERR1;
500    
501  /* 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
502  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.
503  Otherwise further processing may be required. */  Otherwise further processing may be required. */
504    
505  #ifndef EBCDIC  /* ASCII coding */  #ifndef EBCDIC  /* ASCII coding */
506  else if (c < '0' || c > 'z') {}                           /* Not alphameric */  else if (c < '0' || c > 'z') {}                           /* Not alphanumeric */
507  else if ((i = escapes[c - '0']) != 0) c = i;  else if ((i = escapes[c - '0']) != 0) c = i;
508    
509  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
510  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphameric */  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */
511  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  c = i;
512  #endif  #endif
513    
# Line 722  else Line 724  else
724      break;      break;
725    
726      /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any      /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
727      other alphameric following \ is an error if PCRE_EXTRA was set; otherwise,      other alphanumeric following \ is an error if PCRE_EXTRA was set;
728      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
729      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
730      in future, so I haven't "optimized" it. */      be again in future, so I haven't "optimized" it. */
731    
732      default:      default:
733      if ((options & PCRE_EXTRA) != 0) switch(c)      if ((options & PCRE_EXTRA) != 0) switch(c)
# Line 1777  while (posix_name_lengths[yield] != 0) Line 1779  while (posix_name_lengths[yield] != 0)
1779    {    {
1780    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
1781      strncmp((const char *)ptr, pn, len) == 0) return yield;      strncmp((const char *)ptr, pn, len) == 0) return yield;
1782    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
1783    yield++;    yield++;
1784    }    }
1785  return -1;  return -1;
# Line 2383  req_caseopt = ((options & PCRE_CASELESS) Line 2385  req_caseopt = ((options & PCRE_CASELESS)
2385  for (;; ptr++)  for (;; ptr++)
2386    {    {
2387    BOOL negate_class;    BOOL negate_class;
2388      BOOL should_flip_negation;
2389    BOOL possessive_quantifier;    BOOL possessive_quantifier;
2390    BOOL is_quantifier;    BOOL is_quantifier;
2391    BOOL is_recurse;    BOOL is_recurse;
# Line 2631  for (;; ptr++) Line 2634  for (;; ptr++)
2634        else break;        else break;
2635        }        }
2636    
2637        /* If a class contains a negative special such as \S, we need to flip the
2638        negation flag at the end, so that support for characters > 255 works
2639        correctly (they are all included in the class). */
2640    
2641        should_flip_negation = FALSE;
2642    
2643      /* Keep a count of chars with values < 256 so that we can optimize the case      /* Keep a count of chars with values < 256 so that we can optimize the case
2644      of just a single character (as long as it's < 256). However, For higher      of just a single character (as long as it's < 256). However, For higher
2645      valued UTF-8 characters, we don't yet do any optimization. */      valued UTF-8 characters, we don't yet do any optimization. */
# Line 2703  for (;; ptr++) Line 2712  for (;; ptr++)
2712          if (*ptr == '^')          if (*ptr == '^')
2713            {            {
2714            local_negate = TRUE;            local_negate = TRUE;
2715              should_flip_negation = TRUE;  /* Note negative special */
2716            ptr++;            ptr++;
2717            }            }
2718    
# Line 2777  for (;; ptr++) Line 2787  for (;; ptr++)
2787          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);          c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
2788          if (*errorcodeptr != 0) goto FAILED;          if (*errorcodeptr != 0) goto FAILED;
2789    
2790          if (-c == ESC_b) c = '\b';       /* \b is backslash in a class */          if (-c == ESC_b) c = '\b';       /* \b is backspace in a class */
2791          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 */
2792          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 */
2793          else if (-c == ESC_Q)            /* Handle start of quoted string */          else if (-c == ESC_Q)            /* Handle start of quoted string */
# Line 2805  for (;; ptr++) Line 2815  for (;; ptr++)
2815              continue;              continue;
2816    
2817              case ESC_D:              case ESC_D:
2818                should_flip_negation = TRUE;
2819              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];
2820              continue;              continue;
2821    
# Line 2813  for (;; ptr++) Line 2824  for (;; ptr++)
2824              continue;              continue;
2825    
2826              case ESC_W:              case ESC_W:
2827                should_flip_negation = TRUE;
2828              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
2829              continue;              continue;
2830    
# Line 2822  for (;; ptr++) Line 2834  for (;; ptr++)
2834              continue;              continue;
2835    
2836              case ESC_S:              case ESC_S:
2837                should_flip_negation = TRUE;
2838              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
2839              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
2840              continue;              continue;
2841    
             case ESC_E: /* Perl ignores an orphan \E */  
             continue;  
   
2842              default:    /* Not recognized; fall through */              default:    /* Not recognized; fall through */
2843              break;      /* Need "default" setting to stop compiler warning. */              break;      /* Need "default" setting to stop compiler warning. */
2844              }              }
# Line 3063  for (;; ptr++) Line 3073  for (;; ptr++)
3073            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
3074            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
3075    
3076            /* \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
3077            special means the '-' was literal */            special means the '-' was literal */
3078    
3079            if (d < 0)            if (d < 0)
# Line 3327  we set the flag only if there is a liter Line 3337  we set the flag only if there is a liter
3337      zeroreqbyte = reqbyte;      zeroreqbyte = reqbyte;
3338    
3339      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
3340      extended class, with its own opcode. If there are no characters < 256,      extended class, with its own opcode, unless there was a negated special
3341      we can omit the bitmap in the actual compiled code. */      such as \S in the class, because in that case all characters > 255 are in
3342        the class, so any that were explicitly given as well can be ignored. If
3343        (when there are explicit characters > 255 that must be listed) there are no
3344        characters < 256, we can omit the bitmap in the actual compiled code. */
3345    
3346  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3347      if (class_utf8)      if (class_utf8 && !should_flip_negation)
3348        {        {
3349        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */
3350        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
# Line 3357  we set the flag only if there is a liter Line 3370  we set the flag only if there is a liter
3370        }        }
3371  #endif  #endif
3372    
3373      /* If there are no characters > 255, negate the 32-byte map if necessary,      /* If there are no characters > 255, set the opcode to OP_CLASS or
3374      and copy it into the code vector. If this is the first thing in the branch,      OP_NCLASS, depending on whether the whole class was negated and whether
3375      there can be no first char setting, whatever the repeat count. Any reqbyte      there were negative specials such as \S in the class. Then copy the 32-byte
3376      setting must remain unchanged after any kind of repeat. */      map into the code vector, negating it if necessary. */
3377    
3378        *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
3379      if (negate_class)      if (negate_class)
3380        {        {
       *code++ = OP_NCLASS;  
3381        if (lengthptr == NULL)    /* Save time in the pre-compile phase */        if (lengthptr == NULL)    /* Save time in the pre-compile phase */
3382          for (c = 0; c < 32; c++) code[c] = ~classbits[c];          for (c = 0; c < 32; c++) code[c] = ~classbits[c];
3383        }        }
3384      else      else
3385        {        {
       *code++ = OP_CLASS;  
3386        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
3387        }        }
3388      code += 32;      code += 32;
# Line 4064  we set the flag only if there is a liter Line 4076  we set the flag only if there is a liter
4076      if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)      if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)
4077        {        {
4078        int i, namelen;        int i, namelen;
4079        const char *vn = verbnames;        const char *vn = verbnames;
4080        const uschar *name = ++ptr;        const uschar *name = ++ptr;
4081        previous = NULL;        previous = NULL;
4082        while ((cd->ctypes[*++ptr] & ctype_letter) != 0);        while ((cd->ctypes[*++ptr] & ctype_letter) != 0);
# Line 4088  we set the flag only if there is a liter Line 4100  we set the flag only if there is a liter
4100            if (*code++ == OP_ACCEPT) cd->had_accept = TRUE;            if (*code++ == OP_ACCEPT) cd->had_accept = TRUE;
4101            break;            break;
4102            }            }
4103          vn += verbs[i].len + 1;          vn += verbs[i].len + 1;
4104          }          }
4105        if (i < verbcount) continue;        if (i < verbcount) continue;
4106        *errorcodeptr = ERR60;        *errorcodeptr = ERR60;
# Line 4233  we set the flag only if there is a liter Line 4245  we set the flag only if there is a liter
4245              *errorcodeptr = ERR58;              *errorcodeptr = ERR58;
4246              goto FAILED;              goto FAILED;
4247              }              }
4248            if (refsign == '-')            recno = (refsign == '-')?
4249                cd->bracount - recno + 1 : recno +cd->bracount;
4250              if (recno <= 0 || recno > cd->final_bracount)
4251              {              {
4252              recno = cd->bracount - recno + 1;              *errorcodeptr = ERR15;
4253              if (recno <= 0)              goto FAILED;
               {  
               *errorcodeptr = ERR15;  
               goto FAILED;  
               }  
4254              }              }
           else recno += cd->bracount;  
4255            PUT2(code, 2+LINK_SIZE, recno);            PUT2(code, 2+LINK_SIZE, recno);
4256            break;            break;
4257            }            }
# Line 4314  we set the flag only if there is a liter Line 4323  we set the flag only if there is a liter
4323            skipbytes = 1;            skipbytes = 1;
4324            }            }
4325    
4326          /* Check for the "name" actually being a subpattern number. */          /* Check for the "name" actually being a subpattern number. We are
4327            in the second pass here, so final_bracount is set. */
4328    
4329          else if (recno > 0)          else if (recno > 0 && recno <= cd->final_bracount)
4330            {            {
4331            PUT2(code, 2+LINK_SIZE, recno);            PUT2(code, 2+LINK_SIZE, recno);
4332            }            }
# Line 4510  we set the flag only if there is a liter Line 4520  we set the flag only if there is a liter
4520    
4521          /* We come here from the Python syntax above that handles both          /* We come here from the Python syntax above that handles both
4522          references (?P=name) and recursion (?P>name), as well as falling          references (?P=name) and recursion (?P>name), as well as falling
4523          through from the Perl recursion syntax (?&name). */          through from the Perl recursion syntax (?&name). We also come here from
4524            the Perl \k<name> or \k'name' back reference syntax and the \k{name}
4525            .NET syntax. */
4526    
4527          NAMED_REF_OR_RECURSE:          NAMED_REF_OR_RECURSE:
4528          name = ++ptr;          name = ++ptr;
# Line 4522  we set the flag only if there is a liter Line 4534  we set the flag only if there is a liter
4534    
4535          if (lengthptr != NULL)          if (lengthptr != NULL)
4536            {            {
4537              if (namelen == 0)
4538                {
4539                *errorcodeptr = ERR62;
4540                goto FAILED;
4541                }
4542            if (*ptr != terminator)            if (*ptr != terminator)
4543              {              {
4544              *errorcodeptr = ERR42;              *errorcodeptr = ERR42;
# Line 4535  we set the flag only if there is a liter Line 4552  we set the flag only if there is a liter
4552            recno = 0;            recno = 0;
4553            }            }
4554    
4555          /* In the real compile, seek the name in the table */          /* In the real compile, seek the name in the table. We check the name
4556            first, and then check that we have reached the end of the name in the
4557            table. That way, if the name that is longer than any in the table,
4558            the comparison will fail without reading beyond the table entry. */
4559    
4560          else          else
4561            {            {
4562            slot = cd->name_table;            slot = cd->name_table;
4563            for (i = 0; i < cd->names_found; i++)            for (i = 0; i < cd->names_found; i++)
4564              {              {
4565              if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break;              if (strncmp((char *)name, (char *)slot+2, namelen) == 0 &&
4566                    slot[2+namelen] == 0)
4567                  break;
4568              slot += cd->name_entry_size;              slot += cd->name_entry_size;
4569              }              }
4570    
# Line 4579  we set the flag only if there is a liter Line 4601  we set the flag only if there is a liter
4601            {            {
4602            const uschar *called;            const uschar *called;
4603    
4604            if ((refsign = *ptr) == '+') ptr++;            if ((refsign = *ptr) == '+')
4605                {
4606                ptr++;
4607                if ((digitab[*ptr] & ctype_digit) == 0)
4608                  {
4609                  *errorcodeptr = ERR63;
4610                  goto FAILED;
4611                  }
4612                }
4613            else if (refsign == '-')            else if (refsign == '-')
4614              {              {
4615              if ((digitab[ptr[1]] & ctype_digit) == 0)              if ((digitab[ptr[1]] & ctype_digit) == 0)
# Line 5906  to compile parts of the pattern into; th Line 5936  to compile parts of the pattern into; th
5936  no longer needed, so hopefully this workspace will never overflow, though there  no longer needed, so hopefully this workspace will never overflow, though there
5937  is a test for its doing so. */  is a test for its doing so. */
5938    
5939  cd->bracount = 0;  cd->bracount = cd->final_bracount = 0;
5940  cd->names_found = 0;  cd->names_found = 0;
5941  cd->name_entry_size = 0;  cd->name_entry_size = 0;
5942  cd->name_table = NULL;  cd->name_table = NULL;
# Line 5983  field. Reset the bracket count and the n Line 6013  field. Reset the bracket count and the n
6013  field; this time it's used for remembering forward references to subpatterns.  field; this time it's used for remembering forward references to subpatterns.
6014  */  */
6015    
6016    cd->final_bracount = cd->bracount;  /* Save for checking forward references */
6017  cd->bracount = 0;  cd->bracount = 0;
6018  cd->names_found = 0;  cd->names_found = 0;
6019  cd->name_table = (uschar *)re + re->name_table_offset;  cd->name_table = (uschar *)re + re->name_table_offset;

Legend:
Removed from v.240  
changed lines
  Added in v.276

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12