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

Diff of /code/trunk/pcre_study.c

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

revision 455 by ph10, Sat Sep 26 19:12:32 2009 UTC revision 603 by ph10, Fri May 27 10:14:09 2011 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2009 University of Cambridge             Copyright (c) 1997-2010 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 48  supporting functions. */ Line 48  supporting functions. */
48    
49  #include "pcre_internal.h"  #include "pcre_internal.h"
50    
51    #define SET_BIT(c) start_bits[c/8] |= (1 << (c&7))
52    
53  /* Returns from set_start_bits() */  /* Returns from set_start_bits() */
54    
# Line 60  enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE Line 61  enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE
61  *************************************************/  *************************************************/
62    
63  /* Scan a parenthesized group and compute the minimum length of subject that  /* Scan a parenthesized group and compute the minimum length of subject that
64  is needed to match it. This is a lower bound; it does not mean there is a  is needed to match it. This is a lower bound; it does not mean there is a
65  string of that length that matches. In UTF8 mode, the result is in characters  string of that length that matches. In UTF8 mode, the result is in characters
66  rather than bytes.  rather than bytes.
67    
68  Arguments:  Arguments:
69    code       pointer to start of group (the bracket)    code       pointer to start of group (the bracket)
70    startcode  pointer to start of the whole pattern    startcode  pointer to start of the whole pattern
71    options    the compiling options    options    the compiling options
72    
73  Returns:   the minimum length  Returns:   the minimum length
74             -1 if \C was encountered             -1 if \C was encountered
75             -2 internal error (missing capturing bracket)             -2 internal error (missing capturing bracket)
76               -3 internal error (opcode not listed)
77  */  */
78    
79  static int  static int
# Line 91  branch, check the length against that of Line 93  branch, check the length against that of
93  for (;;)  for (;;)
94    {    {
95    int d, min;    int d, min;
96    uschar *cs, *ce;    uschar *cs, *ce;
97    register int op = *cc;    register int op = *cc;
98    
99    switch (op)    switch (op)
100      {      {
101        case OP_COND:
102        case OP_SCOND:
103    
104        /* If there is only one branch in a condition, the implied branch has zero
105        length, so we don't add anything. This covers the DEFINE "condition"
106        automatically. */
107    
108        cs = cc + GET(cc, 1);
109        if (*cs != OP_ALT)
110          {
111          cc = cs + 1 + LINK_SIZE;
112          break;
113          }
114    
115        /* Otherwise we can fall through and treat it the same as any other
116        subpattern. */
117    
118      case OP_CBRA:      case OP_CBRA:
119      case OP_SCBRA:      case OP_SCBRA:
120      case OP_BRA:      case OP_BRA:
121      case OP_SBRA:      case OP_SBRA:
122      case OP_ONCE:      case OP_ONCE:
     case OP_COND:  
     case OP_SCOND:  
123      d = find_minlength(cc, startcode, options);      d = find_minlength(cc, startcode, options);
124      if (d < 0) return d;      if (d < 0) return d;
125      branchlength += d;      branchlength += d;
# Line 119  for (;;) Line 136  for (;;)
136      case OP_KETRMAX:      case OP_KETRMAX:
137      case OP_KETRMIN:      case OP_KETRMIN:
138      case OP_END:      case OP_END:
139      if (length < 0 || (!had_recurse && branchlength < length))      if (length < 0 || (!had_recurse && branchlength < length))
140        length = branchlength;        length = branchlength;
141      if (*cc != OP_ALT) return length;      if (*cc != OP_ALT) return length;
142      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
143      branchlength = 0;      branchlength = 0;
144      had_recurse = FALSE;      had_recurse = FALSE;
145      break;      break;
146    
147      /* Skip over assertive subpatterns */      /* Skip over assertive subpatterns */
# Line 140  for (;;) Line 157  for (;;)
157    
158      case OP_REVERSE:      case OP_REVERSE:
159      case OP_CREF:      case OP_CREF:
160        case OP_NCREF:
161      case OP_RREF:      case OP_RREF:
162        case OP_NRREF:
163      case OP_DEF:      case OP_DEF:
     case OP_OPT:  
164      case OP_CALLOUT:      case OP_CALLOUT:
165      case OP_SOD:      case OP_SOD:
166      case OP_SOM:      case OP_SOM:
167      case OP_EOD:      case OP_EOD:
168      case OP_EODN:      case OP_EODN:
169      case OP_CIRC:      case OP_CIRC:
170        case OP_CIRCM:
171      case OP_DOLL:      case OP_DOLL:
172        case OP_DOLLM:
173      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
174      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
175      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
176      break;      break;
177    
178      /* Skip over a subpattern that has a {0} or {0,x} quantifier */      /* Skip over a subpattern that has a {0} or {0,x} quantifier */
179    
180      case OP_BRAZERO:      case OP_BRAZERO:
181      case OP_BRAMINZERO:      case OP_BRAMINZERO:
182      case OP_SKIPZERO:      case OP_SKIPZERO:
183      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
184      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 168  for (;;) Line 188  for (;;)
188      /* Handle literal characters and + repetitions */      /* Handle literal characters and + repetitions */
189    
190      case OP_CHAR:      case OP_CHAR:
191      case OP_CHARNC:      case OP_CHARI:
192      case OP_NOT:      case OP_NOT:
193        case OP_NOTI:
194      case OP_PLUS:      case OP_PLUS:
195        case OP_PLUSI:
196      case OP_MINPLUS:      case OP_MINPLUS:
197        case OP_MINPLUSI:
198      case OP_POSPLUS:      case OP_POSPLUS:
199        case OP_POSPLUSI:
200      case OP_NOTPLUS:      case OP_NOTPLUS:
201        case OP_NOTPLUSI:
202      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
203        case OP_NOTMINPLUSI:
204      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
205        case OP_NOTPOSPLUSI:
206      branchlength++;      branchlength++;
207      cc += 2;      cc += 2;
208  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
209      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
210  #endif  #endif
211      break;      break;
212    
213      case OP_TYPEPLUS:      case OP_TYPEPLUS:
214      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
215      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
216      branchlength++;      branchlength++;
217      cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;      cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
218      break;      break;
# Line 194  for (;;) Line 221  for (;;)
221      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
222    
223      case OP_EXACT:      case OP_EXACT:
224      case OP_NOTEXACT:      case OP_EXACTI:
225        case OP_NOTEXACT:
226        case OP_NOTEXACTI:
227      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
228      cc += 4;      cc += 4;
229  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 223  for (;;) Line 252  for (;;)
252      case OP_ANY:      case OP_ANY:
253      case OP_ALLANY:      case OP_ALLANY:
254      case OP_EXTUNI:      case OP_EXTUNI:
255      case OP_HSPACE:      case OP_HSPACE:
256      case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
257      case OP_VSPACE:      case OP_VSPACE:
258      case OP_NOT_VSPACE:      case OP_NOT_VSPACE:
259      branchlength++;      branchlength++;
260      cc++;      cc++;
261      break;      break;
262    
263      /* "Any newline" might match two characters */      /* "Any newline" might match two characters */
264    
265      case OP_ANYNL:      case OP_ANYNL:
266      branchlength += 2;      branchlength += 2;
267      cc++;      cc++;
268      break;      break;
269    
270      /* The single-byte matcher means we can't proceed in UTF-8 mode */      /* The single-byte matcher means we can't proceed in UTF-8 mode */
271    
# Line 246  for (;;) Line 275  for (;;)
275  #endif  #endif
276      branchlength++;      branchlength++;
277      cc++;      cc++;
278      break;      break;
279    
280      /* For repeated character types, we have to test for \p and \P, which have      /* For repeated character types, we have to test for \p and \P, which have
281      an extra two bytes of parameters. */      an extra two bytes of parameters. */
# Line 285  for (;;) Line 314  for (;;)
314        case OP_CRPLUS:        case OP_CRPLUS:
315        case OP_CRMINPLUS:        case OP_CRMINPLUS:
316        branchlength++;        branchlength++;
317        /* Fall through */        /* Fall through */
318    
319        case OP_CRSTAR:        case OP_CRSTAR:
320        case OP_CRMINSTAR:        case OP_CRMINSTAR:
321        case OP_CRQUERY:        case OP_CRQUERY:
322        case OP_CRMINQUERY:        case OP_CRMINQUERY:
323        cc++;        cc++;
324        break;        break;
325    
326        case OP_CRRANGE:        case OP_CRRANGE:
327        case OP_CRMINRANGE:        case OP_CRMINRANGE:
328        branchlength += GET2(cc,1);        branchlength += GET2(cc,1);
329        cc += 5;        cc += 5;
330        break;        break;
331    
332        default:        default:
333        branchlength++;        branchlength++;
334        break;        break;
335        }        }
336      break;      break;
337    
338      /* Backreferences and subroutine calls are treated in the same way: we find      /* Backreferences and subroutine calls are treated in the same way: we find
339      the minimum length for the subpattern. A recursion, however, causes an      the minimum length for the subpattern. A recursion, however, causes an
340      a flag to be set that causes the length of this branch to be ignored. The      a flag to be set that causes the length of this branch to be ignored. The
341      logic is that a recursion can only make sense if there is another      logic is that a recursion can only make sense if there is another
342      alternation that stops the recursing. That will provide the minimum length      alternation that stops the recursing. That will provide the minimum length
343      (when no recursion happens). A backreference within the group that it is      (when no recursion happens). A backreference within the group that it is
344      referencing behaves in the same way. */      referencing behaves in the same way.
345    
346        If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
347        matches an empty string (by default it causes a matching failure), so in
348        that case we must set the minimum length to zero. */
349    
350      case OP_REF:      case OP_REF:
351      ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));      case OP_REFI:
352      if (cs == NULL) return -2;      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
     do ce += GET(ce, 1); while (*ce == OP_ALT);  
     if (cc > cs && cc < ce)  
353        {        {
354        d = 0;        ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
355        had_recurse = TRUE;        if (cs == NULL) return -2;
356        }        do ce += GET(ce, 1); while (*ce == OP_ALT);
357      else d = find_minlength(cs, startcode, options);        if (cc > cs && cc < ce)
358      cc += 3;          {
359            d = 0;
360            had_recurse = TRUE;
361            }
362          else d = find_minlength(cs, startcode, options);
363          }
364        else d = 0;
365        cc += 3;
366    
367      /* Handle repeated back references */      /* Handle repeated back references */
368    
369      switch (*cc)      switch (*cc)
370        {        {
371        case OP_CRSTAR:        case OP_CRSTAR:
# Line 337  for (;;) Line 375  for (;;)
375        min = 0;        min = 0;
376        cc++;        cc++;
377        break;        break;
378    
379        case OP_CRRANGE:        case OP_CRRANGE:
380        case OP_CRMINRANGE:        case OP_CRMINRANGE:
381        min = GET2(cc, 1);        min = GET2(cc, 1);
382        cc += 5;        cc += 5;
383        break;        break;
384    
385        default:        default:
386        min = 1;        min = 1;
387        break;        break;
388        }        }
389    
390      branchlength += min * d;      branchlength += min * d;
391      break;      break;
392    
393      case OP_RECURSE:      case OP_RECURSE:
394      cs = ce = (uschar *)startcode + GET(cc, 1);      cs = ce = (uschar *)startcode + GET(cc, 1);
395      if (cs == NULL) return -2;      if (cs == NULL) return -2;
396      do ce += GET(ce, 1); while (*ce == OP_ALT);      do ce += GET(ce, 1); while (*ce == OP_ALT);
397      if (cc > cs && cc < ce)      if (cc > cs && cc < ce)
398        had_recurse = TRUE;        had_recurse = TRUE;
399      else      else
400        branchlength += find_minlength(cs, startcode, options);        branchlength += find_minlength(cs, startcode, options);
401      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
402      break;      break;
403    
404      /* Anything else does not or need not match a character. We can get the      /* Anything else does not or need not match a character. We can get the
405      item's length from the table, but for those that can match zero occurrences      item's length from the table, but for those that can match zero occurrences
406      of a character, we must take special action for UTF-8 characters. */      of a character, we must take special action for UTF-8 characters. As it
407        happens, the "NOT" versions of these opcodes are used at present only for
408        ASCII characters, so they could be omitted from this list. However, in
409        future that may change, so we include them here so as not to leave a
410        gotcha for a future maintainer. */
411    
412      case OP_UPTO:      case OP_UPTO:
413      case OP_NOTUPTO:      case OP_UPTOI:
414        case OP_NOTUPTO:
415        case OP_NOTUPTOI:
416      case OP_MINUPTO:      case OP_MINUPTO:
417      case OP_NOTMINUPTO:      case OP_MINUPTOI:
418        case OP_NOTMINUPTO:
419        case OP_NOTMINUPTOI:
420      case OP_POSUPTO:      case OP_POSUPTO:
421        case OP_POSUPTOI:
422        case OP_NOTPOSUPTO:
423        case OP_NOTPOSUPTOI:
424    
425      case OP_STAR:      case OP_STAR:
426        case OP_STARI:
427        case OP_NOTSTAR:
428        case OP_NOTSTARI:
429      case OP_MINSTAR:      case OP_MINSTAR:
430      case OP_NOTMINSTAR:      case OP_MINSTARI:
431        case OP_NOTMINSTAR:
432        case OP_NOTMINSTARI:
433      case OP_POSSTAR:      case OP_POSSTAR:
434      case OP_NOTPOSSTAR:      case OP_POSSTARI:
435        case OP_NOTPOSSTAR:
436        case OP_NOTPOSSTARI:
437    
438      case OP_QUERY:      case OP_QUERY:
439        case OP_QUERYI:
440        case OP_NOTQUERY:
441        case OP_NOTQUERYI:
442      case OP_MINQUERY:      case OP_MINQUERY:
443        case OP_MINQUERYI:
444      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
445        case OP_NOTMINQUERYI:
446      case OP_POSQUERY:      case OP_POSQUERY:
447      case OP_NOTPOSQUERY:      case OP_POSQUERYI:
448        case OP_NOTPOSQUERY:
449        case OP_NOTPOSQUERYI:
450    
451      cc += _pcre_OP_lengths[op];      cc += _pcre_OP_lengths[op];
452  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
453      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
454  #endif  #endif
455      break;      break;
456    
457      /* For the record, these are the opcodes that are matched by "default":      /* Skip these, but we need to add in the name length. */
458      OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,  
459      OP_THEN. */      case OP_MARK:
460        case OP_PRUNE_ARG:
461      default:      case OP_SKIP_ARG:
462        cc += _pcre_OP_lengths[op] + cc[1];
463        break;
464    
465        case OP_THEN_ARG:
466        cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE];
467        break;
468    
469        /* The remaining opcodes are just skipped over. */
470    
471        case OP_ACCEPT:
472        case OP_CLOSE:
473        case OP_COMMIT:
474        case OP_FAIL:
475        case OP_PRUNE:
476        case OP_SET_SOM:
477        case OP_SKIP:
478        case OP_THEN:
479      cc += _pcre_OP_lengths[op];      cc += _pcre_OP_lengths[op];
480      break;      break;
481    
482        /* This should not occur: we list all opcodes explicitly so that when
483        new ones get added they are properly considered. */
484    
485        default:
486        return -3;
487      }      }
488    }    }
489  /* Control never gets here */  /* Control never gets here */
# Line 406  for (;;) Line 495  for (;;)
495  *      Set a bit and maybe its alternate case    *  *      Set a bit and maybe its alternate case    *
496  *************************************************/  *************************************************/
497    
498  /* Given a character, set its bit in the table, and also the bit for the other  /* Given a character, set its first byte's bit in the table, and also the
499  version of a letter if we are caseless.  corresponding bit for the other version of a letter if we are caseless. In
500    UTF-8 mode, for characters greater than 127, we can only do the caseless thing
501    when Unicode property support is available.
502    
503  Arguments:  Arguments:
504    start_bits    points to the bit map    start_bits    points to the bit map
505    c             is the character    p             points to the character
506    caseless      the caseless flag    caseless      the caseless flag
507    cd            the block with char table pointers    cd            the block with char table pointers
508      utf8          TRUE for UTF-8 mode
509    
510    Returns:        pointer after the character
511    */
512    
513    static const uschar *
514    set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,
515      compile_data *cd, BOOL utf8)
516    {
517    unsigned int c = *p;
518    
519    SET_BIT(c);
520    
521    #ifdef SUPPORT_UTF8
522    if (utf8 && c > 127)
523      {
524      GETCHARINC(c, p);
525    #ifdef SUPPORT_UCP
526      if (caseless)
527        {
528        uschar buff[8];
529        c = UCD_OTHERCASE(c);
530        (void)_pcre_ord2utf8(c, buff);
531        SET_BIT(buff[0]);
532        }
533    #endif
534      return p;
535      }
536    #endif
537    
538    /* Not UTF-8 mode, or character is less than 127. */
539    
540    if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
541    return p + 1;
542    }
543    
544    
545    
546    /*************************************************
547    *     Set bits for a positive character type     *
548    *************************************************/
549    
550    /* This function sets starting bits for a character type. In UTF-8 mode, we can
551    only do a direct setting for bytes less than 128, as otherwise there can be
552    confusion with bytes in the middle of UTF-8 characters. In a "traditional"
553    environment, the tables will only recognize ASCII characters anyway, but in at
554    least one Windows environment, some higher bytes bits were set in the tables.
555    So we deal with that case by considering the UTF-8 encoding.
556    
557    Arguments:
558      start_bits     the starting bitmap
559      cbit type      the type of character wanted
560      table_limit    32 for non-UTF-8; 16 for UTF-8
561      cd             the block with char table pointers
562    
563  Returns:        nothing  Returns:         nothing
564  */  */
565    
566  static void  static void
567  set_bit(uschar *start_bits, unsigned int c, BOOL caseless, compile_data *cd)  set_type_bits(uschar *start_bits, int cbit_type, int table_limit,
568      compile_data *cd)
569  {  {
570  start_bits[c/8] |= (1 << (c&7));  register int c;
571  if (caseless && (cd->ctypes[c] & ctype_letter) != 0)  for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
572    start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));  if (table_limit == 32) return;
573    for (c = 128; c < 256; c++)
574      {
575      if ((cd->cbits[c/8] & (1 << (c&7))) != 0)
576        {
577        uschar buff[8];
578        (void)_pcre_ord2utf8(c, buff);
579        SET_BIT(buff[0]);
580        }
581      }
582    }
583    
584    
585    /*************************************************
586    *     Set bits for a negative character type     *
587    *************************************************/
588    
589    /* This function sets starting bits for a negative character type such as \D.
590    In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
591    otherwise there can be confusion with bytes in the middle of UTF-8 characters.
592    Unlike in the positive case, where we can set appropriate starting bits for
593    specific high-valued UTF-8 characters, in this case we have to set the bits for
594    all high-valued characters. The lowest is 0xc2, but we overkill by starting at
595    0xc0 (192) for simplicity.
596    
597    Arguments:
598      start_bits     the starting bitmap
599      cbit type      the type of character wanted
600      table_limit    32 for non-UTF-8; 16 for UTF-8
601      cd             the block with char table pointers
602    
603    Returns:         nothing
604    */
605    
606    static void
607    set_nottype_bits(uschar *start_bits, int cbit_type, int table_limit,
608      compile_data *cd)
609    {
610    register int c;
611    for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
612    if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
613  }  }
614    
615    
# Line 443  function fails unless the result is SSB_ Line 629  function fails unless the result is SSB_
629  Arguments:  Arguments:
630    code         points to an expression    code         points to an expression
631    start_bits   points to a 32-byte table, initialized to 0    start_bits   points to a 32-byte table, initialized to 0
   caseless     the current state of the caseless flag  
632    utf8         TRUE if in UTF-8 mode    utf8         TRUE if in UTF-8 mode
633    cd           the block with char table pointers    cd           the block with char table pointers
634    
# Line 453  Returns: SSB_FAIL => Failed to Line 638  Returns: SSB_FAIL => Failed to
638  */  */
639    
640  static int  static int
641  set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,  set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8,
642    BOOL utf8, compile_data *cd)    compile_data *cd)
643  {  {
644  register int c;  register int c;
645  int yield = SSB_DONE;  int yield = SSB_DONE;
646    int table_limit = utf8? 16:32;
647    
648  #if 0  #if 0
649  /* ========================================================================= */  /* ========================================================================= */
# Line 502  do Line 688  do
688        case OP_SCBRA:        case OP_SCBRA:
689        case OP_ONCE:        case OP_ONCE:
690        case OP_ASSERT:        case OP_ASSERT:
691        rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);        rc = set_start_bits(tcode, start_bits, utf8, cd);
692        if (rc == SSB_FAIL) return SSB_FAIL;        if (rc == SSB_FAIL) return SSB_FAIL;
693        if (rc == SSB_DONE) try_next = FALSE; else        if (rc == SSB_DONE) try_next = FALSE; else
694          {          {
# Line 543  do Line 729  do
729        tcode += 1 + LINK_SIZE;        tcode += 1 + LINK_SIZE;
730        break;        break;
731    
       /* Skip over an option setting, changing the caseless flag */  
   
       case OP_OPT:  
       caseless = (tcode[1] & PCRE_CASELESS) != 0;  
       tcode += 2;  
       break;  
   
732        /* BRAZERO does the bracket, but carries on. */        /* BRAZERO does the bracket, but carries on. */
733    
734        case OP_BRAZERO:        case OP_BRAZERO:
735        case OP_BRAMINZERO:        case OP_BRAMINZERO:
736        if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)        if (set_start_bits(++tcode, start_bits, utf8, cd) == SSB_FAIL)
737          return SSB_FAIL;          return SSB_FAIL;
738  /* =========================================================================  /* =========================================================================
739        See the comment at the head of this function concerning the next line,        See the comment at the head of this function concerning the next line,
# Line 581  do Line 760  do
760        case OP_QUERY:        case OP_QUERY:
761        case OP_MINQUERY:        case OP_MINQUERY:
762        case OP_POSQUERY:        case OP_POSQUERY:
763        set_bit(start_bits, tcode[1], caseless, cd);        tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
764        tcode += 2;        break;
765  #ifdef SUPPORT_UTF8  
766        if (utf8 && tcode[-1] >= 0xc0)        case OP_STARI:
767          tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];        case OP_MINSTARI:
768  #endif        case OP_POSSTARI:
769          case OP_QUERYI:
770          case OP_MINQUERYI:
771          case OP_POSQUERYI:
772          tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
773        break;        break;
774    
775        /* Single-char upto sets the bit and tries the next */        /* Single-char upto sets the bit and tries the next */
# Line 594  do Line 777  do
777        case OP_UPTO:        case OP_UPTO:
778        case OP_MINUPTO:        case OP_MINUPTO:
779        case OP_POSUPTO:        case OP_POSUPTO:
780        set_bit(start_bits, tcode[3], caseless, cd);        tcode = set_table_bit(start_bits, tcode + 3, FALSE, cd, utf8);
781        tcode += 4;        break;
782  #ifdef SUPPORT_UTF8  
783        if (utf8 && tcode[-1] >= 0xc0)        case OP_UPTOI:
784          tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];        case OP_MINUPTOI:
785  #endif        case OP_POSUPTOI:
786          tcode = set_table_bit(start_bits, tcode + 3, TRUE, cd, utf8);
787        break;        break;
788    
789        /* At least one single char sets the bit and stops */        /* At least one single char sets the bit and stops */
790    
791        case OP_EXACT:       /* Fall through */        case OP_EXACT:
792        tcode += 2;        tcode += 2;
793          /* Fall through */
794        case OP_CHAR:        case OP_CHAR:
       case OP_CHARNC:  
795        case OP_PLUS:        case OP_PLUS:
796        case OP_MINPLUS:        case OP_MINPLUS:
797        case OP_POSPLUS:        case OP_POSPLUS:
798        set_bit(start_bits, tcode[1], caseless, cd);        (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
799        try_next = FALSE;        try_next = FALSE;
800        break;        break;
801    
802        /* Single character type sets the bits and stops */        case OP_EXACTI:
803          tcode += 2;
804          /* Fall through */
805          case OP_CHARI:
806          case OP_PLUSI:
807          case OP_MINPLUSI:
808          case OP_POSPLUSI:
809          (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
810          try_next = FALSE;
811          break;
812    
813          /* Special spacing and line-terminating items. These recognize specific
814          lists of characters. The difference between VSPACE and ANYNL is that the
815          latter can match the two-character CRLF sequence, but that is not
816          relevant for finding the first character, so their code here is
817          identical. */
818    
819          case OP_HSPACE:
820          SET_BIT(0x09);
821          SET_BIT(0x20);
822          if (utf8)
823            {
824            SET_BIT(0xC2);  /* For U+00A0 */
825            SET_BIT(0xE1);  /* For U+1680, U+180E */
826            SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
827            SET_BIT(0xE3);  /* For U+3000 */
828            }
829          else SET_BIT(0xA0);
830          try_next = FALSE;
831          break;
832    
833          case OP_ANYNL:
834          case OP_VSPACE:
835          SET_BIT(0x0A);
836          SET_BIT(0x0B);
837          SET_BIT(0x0C);
838          SET_BIT(0x0D);
839          if (utf8)
840            {
841            SET_BIT(0xC2);  /* For U+0085 */
842            SET_BIT(0xE2);  /* For U+2028, U+2029 */
843            }
844          else SET_BIT(0x85);
845          try_next = FALSE;
846          break;
847    
848          /* Single character types set the bits and stop. Note that if PCRE_UCP
849          is set, we do not see these op codes because \d etc are converted to
850          properties. Therefore, these apply in the case when only characters less
851          than 256 are recognized to match the types. */
852    
853        case OP_NOT_DIGIT:        case OP_NOT_DIGIT:
854        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_digit];  
855        try_next = FALSE;        try_next = FALSE;
856        break;        break;
857    
858        case OP_DIGIT:        case OP_DIGIT:
859        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_digit];  
860        try_next = FALSE;        try_next = FALSE;
861        break;        break;
862    
863        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
864        discard it. */        ensure it is set as not whitespace. */
865    
866        case OP_NOT_WHITESPACE:        case OP_NOT_WHITESPACE:
867        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_space, table_limit, cd);
868          {        start_bits[1] |= 0x08;
         int d = cd->cbits[c+cbit_space];  
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= ~d;  
         }  
869        try_next = FALSE;        try_next = FALSE;
870        break;        break;
871    
872        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
873        discard it. */        not set it from the table. */
874    
875        case OP_WHITESPACE:        case OP_WHITESPACE:
876        for (c = 0; c < 32; c++)        c = start_bits[1];    /* Save in case it was already set */
877          {        set_type_bits(start_bits, cbit_space, table_limit, cd);
878          int d = cd->cbits[c+cbit_space];        start_bits[1] = (start_bits[1] & ~0x08) | c;
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= d;  
         }  
879        try_next = FALSE;        try_next = FALSE;
880        break;        break;
881    
882        case OP_NOT_WORDCHAR:        case OP_NOT_WORDCHAR:
883        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_word];  
884        try_next = FALSE;        try_next = FALSE;
885        break;        break;
886    
887        case OP_WORDCHAR:        case OP_WORDCHAR:
888        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_word];  
889        try_next = FALSE;        try_next = FALSE;
890        break;        break;
891    
# Line 673  do Line 894  do
894    
895        case OP_TYPEPLUS:        case OP_TYPEPLUS:
896        case OP_TYPEMINPLUS:        case OP_TYPEMINPLUS:
897          case OP_TYPEPOSPLUS:
898        tcode++;        tcode++;
899        break;        break;
900    
# Line 696  do Line 918  do
918        case OP_TYPEPOSQUERY:        case OP_TYPEPOSQUERY:
919        switch(tcode[1])        switch(tcode[1])
920          {          {
921            default:
922          case OP_ANY:          case OP_ANY:
923          case OP_ALLANY:          case OP_ALLANY:
924          return SSB_FAIL;          return SSB_FAIL;
925    
926            case OP_HSPACE:
927            SET_BIT(0x09);
928            SET_BIT(0x20);
929            if (utf8)
930              {
931              SET_BIT(0xC2);  /* For U+00A0 */
932              SET_BIT(0xE1);  /* For U+1680, U+180E */
933              SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
934              SET_BIT(0xE3);  /* For U+3000 */
935              }
936            else SET_BIT(0xA0);
937            break;
938    
939            case OP_ANYNL:
940            case OP_VSPACE:
941            SET_BIT(0x0A);
942            SET_BIT(0x0B);
943            SET_BIT(0x0C);
944            SET_BIT(0x0D);
945            if (utf8)
946              {
947              SET_BIT(0xC2);  /* For U+0085 */
948              SET_BIT(0xE2);  /* For U+2028, U+2029 */
949              }
950            else SET_BIT(0x85);
951            break;
952    
953          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
954          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_digit];  
955          break;          break;
956    
957          case OP_DIGIT:          case OP_DIGIT:
958          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_digit];  
959          break;          break;
960    
961          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
962          discard it. */          ensure it gets set as not whitespace. */
963    
964          case OP_NOT_WHITESPACE:          case OP_NOT_WHITESPACE:
965          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_space, table_limit, cd);
966            {          start_bits[1] |= 0x08;
           int d = cd->cbits[c+cbit_space];  
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= ~d;  
           }  
967          break;          break;
968    
969          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
970          discard it. */          avoid setting it. */
971    
972          case OP_WHITESPACE:          case OP_WHITESPACE:
973          for (c = 0; c < 32; c++)          c = start_bits[1];    /* Save in case it was already set */
974            {          set_type_bits(start_bits, cbit_space, table_limit, cd);
975            int d = cd->cbits[c+cbit_space];          start_bits[1] = (start_bits[1] & ~0x08) | c;
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= d;  
           }  
976          break;          break;
977    
978          case OP_NOT_WORDCHAR:          case OP_NOT_WORDCHAR:
979          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_word];  
980          break;          break;
981    
982          case OP_WORDCHAR:          case OP_WORDCHAR:
983          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_word];  
984          break;          break;
985          }          }
986    
# Line 883  code = (uschar *)re + re->name_table_off Line 1122  code = (uschar *)re + re->name_table_off
1122    (re->name_count * re->name_entry_size);    (re->name_count * re->name_entry_size);
1123    
1124  /* For an anchored pattern, or an unanchored pattern that has a first char, or  /* For an anchored pattern, or an unanchored pattern that has a first char, or
1125  a multiline pattern that matches only at "line starts", there is no point in  a multiline pattern that matches only at "line starts", there is no point in
1126  seeking a list of starting bytes. */  seeking a list of starting bytes. */
1127    
1128  if ((re->options & PCRE_ANCHORED) == 0 &&  if ((re->options & PCRE_ANCHORED) == 0 &&
1129      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1130    {    {
1131    /* Set the character tables in the block that is passed around */    /* Set the character tables in the block that is passed around */
1132    
1133    tables = re->tables;    tables = re->tables;
1134    if (tables == NULL)    if (tables == NULL)
1135      (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,      (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1136      (void *)(&tables));      (void *)(&tables));
1137    
1138    compile_block.lcc = tables + lcc_offset;    compile_block.lcc = tables + lcc_offset;
1139    compile_block.fcc = tables + fcc_offset;    compile_block.fcc = tables + fcc_offset;
1140    compile_block.cbits = tables + cbits_offset;    compile_block.cbits = tables + cbits_offset;
1141    compile_block.ctypes = tables + ctypes_offset;    compile_block.ctypes = tables + ctypes_offset;
1142    
1143    /* See if we can find a fixed set of initial characters for the pattern. */    /* See if we can find a fixed set of initial characters for the pattern. */
1144    
1145    memset(start_bits, 0, 32 * sizeof(uschar));    memset(start_bits, 0, 32 * sizeof(uschar));
1146    bits_set = set_start_bits(code, start_bits,    bits_set = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
     (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,  
1147      &compile_block) == SSB_DONE;      &compile_block) == SSB_DONE;
1148    }    }
1149    
1150  /* Find the minimum length of subject string. */  /* Find the minimum length of subject string. */
1151    
1152  min = find_minlength(code, code, re->options);  switch(min = find_minlength(code, code, re->options))
1153      {
1154      case -2: *errorptr = "internal error: missing capturing bracket"; break;
1155      case -3: *errorptr = "internal error: opcode not recognized"; break;
1156      default: break;
1157      }
1158    
1159  /* Return NULL if no optimization is possible. */  /* Return NULL if no optimization is possible. */
1160    
# Line 945  if (bits_set) Line 1188  if (bits_set)
1188    study->flags |= PCRE_STUDY_MAPPED;    study->flags |= PCRE_STUDY_MAPPED;
1189    memcpy(study->start_bits, start_bits, sizeof(start_bits));    memcpy(study->start_bits, start_bits, sizeof(start_bits));
1190    }    }
1191    
1192  if (min >= 0)  if (min >= 0)
1193    {    {
1194    study->flags |= PCRE_STUDY_MINLEN;    study->flags |= PCRE_STUDY_MINLEN;
1195    study->minlength = min;    study->minlength = min;
1196    }    }
1197    
1198  return extra;  return extra;
1199  }  }

Legend:
Removed from v.455  
changed lines
  Added in v.603

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12