/[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 85 by nigel, Sat Feb 24 21:41:13 2007 UTC revision 524 by ph10, Mon May 24 17:06:28 2010 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-2005 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 42  POSSIBILITY OF SUCH DAMAGE. Line 42  POSSIBILITY OF SUCH DAMAGE.
42  supporting functions. */  supporting functions. */
43    
44    
45    #ifdef HAVE_CONFIG_H
46    #include "config.h"
47    #endif
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() */
54    
55    enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE };
56    
57    
58    
59    /*************************************************
60    *   Find the minimum subject length for a group  *
61    *************************************************/
62    
63    /* 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
65    string of that length that matches. In UTF8 mode, the result is in characters
66    rather than bytes.
67    
68    Arguments:
69      code       pointer to start of group (the bracket)
70      startcode  pointer to start of the whole pattern
71      options    the compiling options
72    
73    Returns:   the minimum length
74               -1 if \C was encountered
75               -2 internal error (missing capturing bracket)
76    */
77    
78    static int
79    find_minlength(const uschar *code, const uschar *startcode, int options)
80    {
81    int length = -1;
82    BOOL utf8 = (options & PCRE_UTF8) != 0;
83    BOOL had_recurse = FALSE;
84    register int branchlength = 0;
85    register uschar *cc = (uschar *)code + 1 + LINK_SIZE;
86    
87    if (*code == OP_CBRA || *code == OP_SCBRA) cc += 2;
88    
89    /* Scan along the opcodes for this branch. If we get to the end of the
90    branch, check the length against that of the other branches. */
91    
92    for (;;)
93      {
94      int d, min;
95      uschar *cs, *ce;
96      register int op = *cc;
97    
98      switch (op)
99        {
100        case OP_COND:
101        case OP_SCOND:
102    
103        /* If there is only one branch in a condition, the implied branch has zero
104        length, so we don't add anything. This covers the DEFINE "condition"
105        automatically. */
106    
107        cs = cc + GET(cc, 1);
108        if (*cs != OP_ALT)
109          {
110          cc = cs + 1 + LINK_SIZE;
111          break;
112          }
113    
114        /* Otherwise we can fall through and treat it the same as any other
115        subpattern. */
116    
117        case OP_CBRA:
118        case OP_SCBRA:
119        case OP_BRA:
120        case OP_SBRA:
121        case OP_ONCE:
122        d = find_minlength(cc, startcode, options);
123        if (d < 0) return d;
124        branchlength += d;
125        do cc += GET(cc, 1); while (*cc == OP_ALT);
126        cc += 1 + LINK_SIZE;
127        break;
128    
129        /* Reached end of a branch; if it's a ket it is the end of a nested
130        call. If it's ALT it is an alternation in a nested call. If it is
131        END it's the end of the outer call. All can be handled by the same code. */
132    
133        case OP_ALT:
134        case OP_KET:
135        case OP_KETRMAX:
136        case OP_KETRMIN:
137        case OP_END:
138        if (length < 0 || (!had_recurse && branchlength < length))
139          length = branchlength;
140        if (*cc != OP_ALT) return length;
141        cc += 1 + LINK_SIZE;
142        branchlength = 0;
143        had_recurse = FALSE;
144        break;
145    
146        /* Skip over assertive subpatterns */
147    
148        case OP_ASSERT:
149        case OP_ASSERT_NOT:
150        case OP_ASSERTBACK:
151        case OP_ASSERTBACK_NOT:
152        do cc += GET(cc, 1); while (*cc == OP_ALT);
153        /* Fall through */
154    
155        /* Skip over things that don't match chars */
156    
157        case OP_REVERSE:
158        case OP_CREF:
159        case OP_NCREF:
160        case OP_RREF:
161        case OP_NRREF:
162        case OP_DEF:
163        case OP_OPT:
164        case OP_CALLOUT:
165        case OP_SOD:
166        case OP_SOM:
167        case OP_EOD:
168        case OP_EODN:
169        case OP_CIRC:
170        case OP_DOLL:
171        case OP_NOT_WORD_BOUNDARY:
172        case OP_WORD_BOUNDARY:
173        cc += _pcre_OP_lengths[*cc];
174        break;
175    
176        /* Skip over a subpattern that has a {0} or {0,x} quantifier */
177    
178        case OP_BRAZERO:
179        case OP_BRAMINZERO:
180        case OP_SKIPZERO:
181        cc += _pcre_OP_lengths[*cc];
182        do cc += GET(cc, 1); while (*cc == OP_ALT);
183        cc += 1 + LINK_SIZE;
184        break;
185    
186        /* Handle literal characters and + repetitions */
187    
188        case OP_CHAR:
189        case OP_CHARNC:
190        case OP_NOT:
191        case OP_PLUS:
192        case OP_MINPLUS:
193        case OP_POSPLUS:
194        case OP_NOTPLUS:
195        case OP_NOTMINPLUS:
196        case OP_NOTPOSPLUS:
197        branchlength++;
198        cc += 2;
199    #ifdef SUPPORT_UTF8
200        if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
201    #endif
202        break;
203    
204        case OP_TYPEPLUS:
205        case OP_TYPEMINPLUS:
206        case OP_TYPEPOSPLUS:
207        branchlength++;
208        cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
209        break;
210    
211        /* Handle exact repetitions. The count is already in characters, but we
212        need to skip over a multibyte character in UTF8 mode.  */
213    
214        case OP_EXACT:
215        case OP_NOTEXACT:
216        branchlength += GET2(cc,1);
217        cc += 4;
218    #ifdef SUPPORT_UTF8
219        if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
220    #endif
221        break;
222    
223        case OP_TYPEEXACT:
224        branchlength += GET2(cc,1);
225        cc += (cc[3] == OP_PROP || cc[3] == OP_NOTPROP)? 6 : 4;
226        break;
227    
228        /* Handle single-char non-literal matchers */
229    
230        case OP_PROP:
231        case OP_NOTPROP:
232        cc += 2;
233        /* Fall through */
234    
235        case OP_NOT_DIGIT:
236        case OP_DIGIT:
237        case OP_NOT_WHITESPACE:
238        case OP_WHITESPACE:
239        case OP_NOT_WORDCHAR:
240        case OP_WORDCHAR:
241        case OP_ANY:
242        case OP_ALLANY:
243        case OP_EXTUNI:
244        case OP_HSPACE:
245        case OP_NOT_HSPACE:
246        case OP_VSPACE:
247        case OP_NOT_VSPACE:
248        branchlength++;
249        cc++;
250        break;
251    
252        /* "Any newline" might match two characters */
253    
254        case OP_ANYNL:
255        branchlength += 2;
256        cc++;
257        break;
258    
259        /* The single-byte matcher means we can't proceed in UTF-8 mode */
260    
261        case OP_ANYBYTE:
262    #ifdef SUPPORT_UTF8
263        if (utf8) return -1;
264    #endif
265        branchlength++;
266        cc++;
267        break;
268    
269        /* For repeated character types, we have to test for \p and \P, which have
270        an extra two bytes of parameters. */
271    
272        case OP_TYPESTAR:
273        case OP_TYPEMINSTAR:
274        case OP_TYPEQUERY:
275        case OP_TYPEMINQUERY:
276        case OP_TYPEPOSSTAR:
277        case OP_TYPEPOSQUERY:
278        if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
279        cc += _pcre_OP_lengths[op];
280        break;
281    
282        case OP_TYPEUPTO:
283        case OP_TYPEMINUPTO:
284        case OP_TYPEPOSUPTO:
285        if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;
286        cc += _pcre_OP_lengths[op];
287        break;
288    
289        /* Check a class for variable quantification */
290    
291    #ifdef SUPPORT_UTF8
292        case OP_XCLASS:
293        cc += GET(cc, 1) - 33;
294        /* Fall through */
295    #endif
296    
297        case OP_CLASS:
298        case OP_NCLASS:
299        cc += 33;
300    
301        switch (*cc)
302          {
303          case OP_CRPLUS:
304          case OP_CRMINPLUS:
305          branchlength++;
306          /* Fall through */
307    
308          case OP_CRSTAR:
309          case OP_CRMINSTAR:
310          case OP_CRQUERY:
311          case OP_CRMINQUERY:
312          cc++;
313          break;
314    
315          case OP_CRRANGE:
316          case OP_CRMINRANGE:
317          branchlength += GET2(cc,1);
318          cc += 5;
319          break;
320    
321          default:
322          branchlength++;
323          break;
324          }
325        break;
326    
327        /* Backreferences and subroutine calls are treated in the same way: we find
328        the minimum length for the subpattern. A recursion, however, causes an
329        a flag to be set that causes the length of this branch to be ignored. The
330        logic is that a recursion can only make sense if there is another
331        alternation that stops the recursing. That will provide the minimum length
332        (when no recursion happens). A backreference within the group that it is
333        referencing behaves in the same way.
334    
335        If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
336        matches an empty string (by default it causes a matching failure), so in
337        that case we must set the minimum length to zero. */
338    
339        case OP_REF:
340        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
341          {
342          ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
343          if (cs == NULL) return -2;
344          do ce += GET(ce, 1); while (*ce == OP_ALT);
345          if (cc > cs && cc < ce)
346            {
347            d = 0;
348            had_recurse = TRUE;
349            }
350          else d = find_minlength(cs, startcode, options);
351          }
352        else d = 0;
353        cc += 3;
354    
355        /* Handle repeated back references */
356    
357        switch (*cc)
358          {
359          case OP_CRSTAR:
360          case OP_CRMINSTAR:
361          case OP_CRQUERY:
362          case OP_CRMINQUERY:
363          min = 0;
364          cc++;
365          break;
366    
367          case OP_CRRANGE:
368          case OP_CRMINRANGE:
369          min = GET2(cc, 1);
370          cc += 5;
371          break;
372    
373          default:
374          min = 1;
375          break;
376          }
377    
378        branchlength += min * d;
379        break;
380    
381        case OP_RECURSE:
382        cs = ce = (uschar *)startcode + GET(cc, 1);
383        if (cs == NULL) return -2;
384        do ce += GET(ce, 1); while (*ce == OP_ALT);
385        if (cc > cs && cc < ce)
386          had_recurse = TRUE;
387        else
388          branchlength += find_minlength(cs, startcode, options);
389        cc += 1 + LINK_SIZE;
390        break;
391    
392        /* Anything else does not or need not match a character. We can get the
393        item's length from the table, but for those that can match zero occurrences
394        of a character, we must take special action for UTF-8 characters. */
395    
396        case OP_UPTO:
397        case OP_NOTUPTO:
398        case OP_MINUPTO:
399        case OP_NOTMINUPTO:
400        case OP_POSUPTO:
401        case OP_STAR:
402        case OP_MINSTAR:
403        case OP_NOTMINSTAR:
404        case OP_POSSTAR:
405        case OP_NOTPOSSTAR:
406        case OP_QUERY:
407        case OP_MINQUERY:
408        case OP_NOTMINQUERY:
409        case OP_POSQUERY:
410        case OP_NOTPOSQUERY:
411        cc += _pcre_OP_lengths[op];
412    #ifdef SUPPORT_UTF8
413        if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
414    #endif
415        break;
416    
417        /* Skip these, but we need to add in the name length. */
418    
419        case OP_MARK:
420        case OP_PRUNE_ARG:
421        case OP_SKIP_ARG:
422        case OP_THEN_ARG:
423        cc += _pcre_OP_lengths[op] + cc[1];
424        break;
425    
426        /* For the record, these are the opcodes that are matched by "default":
427        OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,
428        OP_THEN. */
429    
430        default:
431        cc += _pcre_OP_lengths[op];
432        break;
433        }
434      }
435    /* Control never gets here */
436    }
437    
438    
439    
440  /*************************************************  /*************************************************
441  *      Set a bit and maybe its alternate case    *  *      Set a bit and maybe its alternate case    *
442  *************************************************/  *************************************************/
443    
444  /* 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
445  version of a letter if we are caseless.  corresponding bit for the other version of a letter if we are caseless. In
446    UTF-8 mode, for characters greater than 127, we can only do the caseless thing
447    when Unicode property support is available.
448    
449  Arguments:  Arguments:
450    start_bits    points to the bit map    start_bits    points to the bit map
451    c             is the character    p             points to the character
452    caseless      the caseless flag    caseless      the caseless flag
453    cd            the block with char table pointers    cd            the block with char table pointers
454      utf8          TRUE for UTF-8 mode
455    
456  Returns:        nothing  Returns:        pointer after the character
457  */  */
458    
459  static void  static const uschar *
460  set_bit(uschar *start_bits, unsigned int c, BOOL caseless, compile_data *cd)  set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,
461      compile_data *cd, BOOL utf8)
462  {  {
463  start_bits[c/8] |= (1 << (c&7));  unsigned int c = *p;
464  if (caseless && (cd->ctypes[c] & ctype_letter) != 0)  
465    start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));  SET_BIT(c);
466    
467    #ifdef SUPPORT_UTF8
468    if (utf8 && c > 127)
469      {
470      GETCHARINC(c, p);
471    #ifdef SUPPORT_UCP
472      if (caseless)
473        {
474        uschar buff[8];
475        c = UCD_OTHERCASE(c);
476        (void)_pcre_ord2utf8(c, buff);
477        SET_BIT(buff[0]);
478        }
479    #endif
480      return p;
481      }
482    #endif
483    
484    /* Not UTF-8 mode, or character is less than 127. */
485    
486    if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
487    return p + 1;
488  }  }
489    
490    
491    
492  /*************************************************  /*************************************************
493  *          Create bitmap of starting chars       *  *          Create bitmap of starting bytes       *
494  *************************************************/  *************************************************/
495    
496  /* This function scans a compiled unanchored expression and attempts to build a  /* This function scans a compiled unanchored expression recursively and
497  bitmap of the set of initial characters. If it can't, it returns FALSE. As time  attempts to build a bitmap of the set of possible starting bytes. As time goes
498  goes by, we may be able to get more clever at doing this.  by, we may be able to get more clever at doing this. The SSB_CONTINUE return is
499    useful for parenthesized groups in patterns such as (a*)b where the group
500    provides some optional starting bytes but scanning must continue at the outer
501    level to find at least one mandatory byte. At the outermost level, this
502    function fails unless the result is SSB_DONE.
503    
504  Arguments:  Arguments:
505    code         points to an expression    code         points to an expression
# Line 86  Arguments: Line 508  Arguments:
508    utf8         TRUE if in UTF-8 mode    utf8         TRUE if in UTF-8 mode
509    cd           the block with char table pointers    cd           the block with char table pointers
510    
511  Returns:       TRUE if table built, FALSE otherwise  Returns:       SSB_FAIL     => Failed to find any starting bytes
512                   SSB_DONE     => Found mandatory starting bytes
513                   SSB_CONTINUE => Found optional starting bytes
514  */  */
515    
516  static BOOL  static int
517  set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,  set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,
518    BOOL utf8, compile_data *cd)    BOOL utf8, compile_data *cd)
519  {  {
520  register int c;  register int c;
521    int yield = SSB_DONE;
522    
523    #if 0
524    /* ========================================================================= */
525    /* The following comment and code was inserted in January 1999. In May 2006,
526    when it was observed to cause compiler warnings about unused values, I took it
527    out again. If anybody is still using OS/2, they will have to put it back
528    manually. */
529    
530  /* This next statement and the later reference to dummy are here in order to  /* This next statement and the later reference to dummy are here in order to
531  trick the optimizer of the IBM C compiler for OS/2 into generating correct  trick the optimizer of the IBM C compiler for OS/2 into generating correct
# Line 102  disable optimization (in this module it Line 534  disable optimization (in this module it
534  the pcre module can use all the optimization it can get). */  the pcre module can use all the optimization it can get). */
535    
536  volatile int dummy;  volatile int dummy;
537    /* ========================================================================= */
538    #endif
539    
540  do  do
541    {    {
542    const uschar *tcode = code + 1 + LINK_SIZE;    const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;
543    BOOL try_next = TRUE;    BOOL try_next = TRUE;
544    
545    while (try_next)    while (try_next)    /* Loop for items in this branch */
546      {      {
547      /* If a branch starts with a bracket or a positive lookahead assertion,      int rc;
548      recurse to set bits from within them. That's all for this branch. */      switch(*tcode)
   
     if ((int)*tcode >= OP_BRA || *tcode == OP_ASSERT)  
549        {        {
550        if (!set_start_bits(tcode, start_bits, caseless, utf8, cd))        /* Fail if we reach something we don't understand */
         return FALSE;  
       try_next = FALSE;  
       }  
551    
     else switch(*tcode)  
       {  
552        default:        default:
553        return FALSE;        return SSB_FAIL;
554    
555        /* Skip over callout */        /* If we hit a bracket or a positive lookahead assertion, recurse to set
556          bits from within the subpattern. If it can't find anything, we have to
557          give up. If it finds some mandatory character(s), we are done for this
558          branch. Otherwise, carry on scanning after the subpattern. */
559    
560          case OP_BRA:
561          case OP_SBRA:
562          case OP_CBRA:
563          case OP_SCBRA:
564          case OP_ONCE:
565          case OP_ASSERT:
566          rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);
567          if (rc == SSB_FAIL) return SSB_FAIL;
568          if (rc == SSB_DONE) try_next = FALSE; else
569            {
570            do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
571            tcode += 1 + LINK_SIZE;
572            }
573          break;
574    
575        case OP_CALLOUT:        /* If we hit ALT or KET, it means we haven't found anything mandatory in
576        tcode += 2 + 2*LINK_SIZE;        this branch, though we might have found something optional. For ALT, we
577          continue with the next alternative, but we have to arrange that the final
578          result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
579          return SSB_CONTINUE: if this is the top level, that indicates failure,
580          but after a nested subpattern, it causes scanning to continue. */
581    
582          case OP_ALT:
583          yield = SSB_CONTINUE;
584          try_next = FALSE;
585        break;        break;
586    
587        /* Skip over extended extraction bracket number */        case OP_KET:
588          case OP_KETRMAX:
589          case OP_KETRMIN:
590          return SSB_CONTINUE;
591    
592        case OP_BRANUMBER:        /* Skip over callout */
593        tcode += 3;  
594          case OP_CALLOUT:
595          tcode += 2 + 2*LINK_SIZE;
596        break;        break;
597    
598        /* Skip over lookbehind and negative lookahead assertions */        /* Skip over lookbehind and negative lookahead assertions */
# Line 143  do Line 601  do
601        case OP_ASSERTBACK:        case OP_ASSERTBACK:
602        case OP_ASSERTBACK_NOT:        case OP_ASSERTBACK_NOT:
603        do tcode += GET(tcode, 1); while (*tcode == OP_ALT);        do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
604        tcode += 1+LINK_SIZE;        tcode += 1 + LINK_SIZE;
605        break;        break;
606    
607        /* Skip over an option setting, changing the caseless flag */        /* Skip over an option setting, changing the caseless flag */
# Line 157  do Line 615  do
615    
616        case OP_BRAZERO:        case OP_BRAZERO:
617        case OP_BRAMINZERO:        case OP_BRAMINZERO:
618        if (!set_start_bits(++tcode, start_bits, caseless, utf8, cd))        if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)
619          return FALSE;          return SSB_FAIL;
620    /* =========================================================================
621          See the comment at the head of this function concerning the next line,
622          which was an old fudge for the benefit of OS/2.
623        dummy = 1;        dummy = 1;
624      ========================================================================= */
625          do tcode += GET(tcode,1); while (*tcode == OP_ALT);
626          tcode += 1 + LINK_SIZE;
627          break;
628    
629          /* SKIPZERO skips the bracket. */
630    
631          case OP_SKIPZERO:
632          tcode++;
633        do tcode += GET(tcode,1); while (*tcode == OP_ALT);        do tcode += GET(tcode,1); while (*tcode == OP_ALT);
634        tcode += 1+LINK_SIZE;        tcode += 1 + LINK_SIZE;
635        break;        break;
636    
637        /* Single-char * or ? sets the bit and tries the next item */        /* Single-char * or ? sets the bit and tries the next item */
638    
639        case OP_STAR:        case OP_STAR:
640        case OP_MINSTAR:        case OP_MINSTAR:
641          case OP_POSSTAR:
642        case OP_QUERY:        case OP_QUERY:
643        case OP_MINQUERY:        case OP_MINQUERY:
644        set_bit(start_bits, tcode[1], caseless, cd);        case OP_POSQUERY:
645        tcode += 2;        tcode = set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);
 #ifdef SUPPORT_UTF8  
       if (utf8) while ((*tcode & 0xc0) == 0x80) tcode++;  
 #endif  
646        break;        break;
647    
648        /* Single-char upto sets the bit and tries the next */        /* Single-char upto sets the bit and tries the next */
649    
650        case OP_UPTO:        case OP_UPTO:
651        case OP_MINUPTO:        case OP_MINUPTO:
652        set_bit(start_bits, tcode[3], caseless, cd);        case OP_POSUPTO:
653        tcode += 4;        tcode = set_table_bit(start_bits, tcode + 3, caseless, cd, utf8);
 #ifdef SUPPORT_UTF8  
       if (utf8) while ((*tcode & 0xc0) == 0x80) tcode++;  
 #endif  
654        break;        break;
655    
656        /* At least one single char sets the bit and stops */        /* At least one single char sets the bit and stops */
# Line 197  do Line 662  do
662        case OP_CHARNC:        case OP_CHARNC:
663        case OP_PLUS:        case OP_PLUS:
664        case OP_MINPLUS:        case OP_MINPLUS:
665        set_bit(start_bits, tcode[1], caseless, cd);        case OP_POSPLUS:
666          (void)set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);
667        try_next = FALSE;        try_next = FALSE;
668        break;        break;
669    
670          /* Special spacing and line-terminating items. These recognize specific
671          lists of characters. The difference between VSPACE and ANYNL is that the
672          latter can match the two-character CRLF sequence, but that is not
673          relevant for finding the first character, so their code here is
674          identical. */
675    
676          case OP_HSPACE:
677          SET_BIT(0x09);
678          SET_BIT(0x20);
679          SET_BIT(0xA0);
680          if (utf8)
681            {
682            SET_BIT(0xE1);  /* For U+1680, U+180E */
683            SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
684            SET_BIT(0xE3);  /* For U+3000 */
685            }
686          try_next = FALSE;
687          break;
688    
689          case OP_ANYNL:
690          case OP_VSPACE:
691          SET_BIT(0x0A);
692          SET_BIT(0x0B);
693          SET_BIT(0x0C);
694          SET_BIT(0x0D);
695          SET_BIT(0x85);
696          if (utf8) SET_BIT(0xE2);    /* For U+2028, U+2029 */
697          try_next = FALSE;
698          break;
699    
700        /* Single character type sets the bits and stops */        /* Single character types set the bits and stop. Note that if PCRE_UCP
701          is set, we do not see these op codes because \d etc are converted to
702          properties. Therefore, these apply in the case when only ASCII characters
703          are recognized to match the types. */
704    
705        case OP_NOT_DIGIT:        case OP_NOT_DIGIT:
706        for (c = 0; c < 32; c++)        for (c = 0; c < 32; c++)
# Line 215  do Line 714  do
714        try_next = FALSE;        try_next = FALSE;
715        break;        break;
716    
717          /* The cbit_space table has vertical tab as whitespace; we have to
718          discard it. */
719    
720        case OP_NOT_WHITESPACE:        case OP_NOT_WHITESPACE:
721        for (c = 0; c < 32; c++)        for (c = 0; c < 32; c++)
722          start_bits[c] |= ~cd->cbits[c+cbit_space];          {
723            int d = cd->cbits[c+cbit_space];
724            if (c == 1) d &= ~0x08;
725            start_bits[c] |= ~d;
726            }
727        try_next = FALSE;        try_next = FALSE;
728        break;        break;
729    
730          /* The cbit_space table has vertical tab as whitespace; we have to
731          discard it. */
732    
733        case OP_WHITESPACE:        case OP_WHITESPACE:
734        for (c = 0; c < 32; c++)        for (c = 0; c < 32; c++)
735          start_bits[c] |= cd->cbits[c+cbit_space];          {
736            int d = cd->cbits[c+cbit_space];
737            if (c == 1) d &= ~0x08;
738            start_bits[c] |= d;
739            }
740        try_next = FALSE;        try_next = FALSE;
741        break;        break;
742    
# Line 244  do Line 757  do
757    
758        case OP_TYPEPLUS:        case OP_TYPEPLUS:
759        case OP_TYPEMINPLUS:        case OP_TYPEMINPLUS:
760          case OP_TYPEPOSPLUS:
761        tcode++;        tcode++;
762        break;        break;
763    
# Line 256  do Line 770  do
770    
771        case OP_TYPEUPTO:        case OP_TYPEUPTO:
772        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
773          case OP_TYPEPOSUPTO:
774        tcode += 2;               /* Fall through */        tcode += 2;               /* Fall through */
775    
776        case OP_TYPESTAR:        case OP_TYPESTAR:
777        case OP_TYPEMINSTAR:        case OP_TYPEMINSTAR:
778          case OP_TYPEPOSSTAR:
779        case OP_TYPEQUERY:        case OP_TYPEQUERY:
780        case OP_TYPEMINQUERY:        case OP_TYPEMINQUERY:
781          case OP_TYPEPOSQUERY:
782        switch(tcode[1])        switch(tcode[1])
783          {          {
784            default:
785          case OP_ANY:          case OP_ANY:
786          return FALSE;          case OP_ALLANY:
787            return SSB_FAIL;
788    
789            case OP_HSPACE:
790            SET_BIT(0x09);
791            SET_BIT(0x20);
792            SET_BIT(0xA0);
793            if (utf8)
794              {
795              SET_BIT(0xE1);  /* For U+1680, U+180E */
796              SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
797              SET_BIT(0xE3);  /* For U+3000 */
798              }
799            break;
800    
801            case OP_ANYNL:
802            case OP_VSPACE:
803            SET_BIT(0x0A);
804            SET_BIT(0x0B);
805            SET_BIT(0x0C);
806            SET_BIT(0x0D);
807            SET_BIT(0x85);
808            if (utf8) SET_BIT(0xE2);    /* For U+2028, U+2029 */
809            break;
810    
811          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
812          for (c = 0; c < 32; c++)          for (c = 0; c < 32; c++)
813            start_bits[c] |= ~cd->cbits[c+cbit_digit];            start_bits[c] |= ~cd->cbits[c+cbit_digit];
# Line 277  do Line 818  do
818            start_bits[c] |= cd->cbits[c+cbit_digit];            start_bits[c] |= cd->cbits[c+cbit_digit];
819          break;          break;
820    
821            /* The cbit_space table has vertical tab as whitespace; we have to
822            discard it. */
823    
824          case OP_NOT_WHITESPACE:          case OP_NOT_WHITESPACE:
825          for (c = 0; c < 32; c++)          for (c = 0; c < 32; c++)
826            start_bits[c] |= ~cd->cbits[c+cbit_space];            {
827              int d = cd->cbits[c+cbit_space];
828              if (c == 1) d &= ~0x08;
829              start_bits[c] |= ~d;
830              }
831          break;          break;
832    
833            /* The cbit_space table has vertical tab as whitespace; we have to
834            discard it. */
835    
836          case OP_WHITESPACE:          case OP_WHITESPACE:
837          for (c = 0; c < 32; c++)          for (c = 0; c < 32; c++)
838            start_bits[c] |= cd->cbits[c+cbit_space];            {
839              int d = cd->cbits[c+cbit_space];
840              if (c == 1) d &= ~0x08;
841              start_bits[c] |= d;
842              }
843          break;          break;
844    
845          case OP_NOT_WORDCHAR:          case OP_NOT_WORDCHAR:
# Line 308  do Line 863  do
863        character with a value > 255. */        character with a value > 255. */
864    
865        case OP_NCLASS:        case OP_NCLASS:
866    #ifdef SUPPORT_UTF8
867        if (utf8)        if (utf8)
868          {          {
869          start_bits[24] |= 0xf0;              /* Bits for 0xc4 - 0xc8 */          start_bits[24] |= 0xf0;              /* Bits for 0xc4 - 0xc8 */
870          memset(start_bits+25, 0xff, 7);      /* Bits for 0xc9 - 0xff */          memset(start_bits+25, 0xff, 7);      /* Bits for 0xc9 - 0xff */
871          }          }
872    #endif
873        /* Fall through */        /* Fall through */
874    
875        case OP_CLASS:        case OP_CLASS:
# Line 325  do Line 882  do
882          value is > 127. In fact, there are only two possible starting bytes for          value is > 127. In fact, there are only two possible starting bytes for
883          characters in the range 128 - 255. */          characters in the range 128 - 255. */
884    
885    #ifdef SUPPORT_UTF8
886          if (utf8)          if (utf8)
887            {            {
888            for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];
# Line 342  do Line 900  do
900          /* In non-UTF-8 mode, the two bit maps are completely compatible. */          /* In non-UTF-8 mode, the two bit maps are completely compatible. */
901    
902          else          else
903    #endif
904            {            {
905            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
906            }            }
# Line 377  do Line 936  do
936    code += GET(code, 1);   /* Advance to next branch */    code += GET(code, 1);   /* Advance to next branch */
937    }    }
938  while (*code == OP_ALT);  while (*code == OP_ALT);
939  return TRUE;  return yield;
940  }  }
941    
942    
# Line 397  Arguments: Line 956  Arguments:
956              set NULL unless error              set NULL unless error
957    
958  Returns:    pointer to a pcre_extra block, with study_data filled in and the  Returns:    pointer to a pcre_extra block, with study_data filled in and the
959                appropriate flag set;                appropriate flags set;
960              NULL on error or if no optimization possible              NULL on error or if no optimization possible
961  */  */
962    
963  PCRE_EXPORT pcre_extra *  PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
964  pcre_study(const pcre *external_re, int options, const char **errorptr)  pcre_study(const pcre *external_re, int options, const char **errorptr)
965  {  {
966    int min;
967    BOOL bits_set = FALSE;
968  uschar start_bits[32];  uschar start_bits[32];
969  pcre_extra *extra;  pcre_extra *extra;
970  pcre_study_data *study;  pcre_study_data *study;
971  const uschar *tables;  const uschar *tables;
972  const real_pcre *re = (const real_pcre *)external_re;  uschar *code;
 uschar *code = (uschar *)re + re->name_table_offset +  
   (re->name_count * re->name_entry_size);  
973  compile_data compile_block;  compile_data compile_block;
974    const real_pcre *re = (const real_pcre *)external_re;
975    
976  *errorptr = NULL;  *errorptr = NULL;
977    
# Line 427  if ((options & ~PUBLIC_STUDY_OPTIONS) != Line 987  if ((options & ~PUBLIC_STUDY_OPTIONS) !=
987    return NULL;    return NULL;
988    }    }
989    
990    code = (uschar *)re + re->name_table_offset +
991      (re->name_count * re->name_entry_size);
992    
993  /* 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
994  a multiline pattern that matches only at "line starts", no further processing  a multiline pattern that matches only at "line starts", there is no point in
995  at present. */  seeking a list of starting bytes. */
996    
997  if ((re->options & (PCRE_ANCHORED|PCRE_FIRSTSET|PCRE_STARTLINE)) != 0)  if ((re->options & PCRE_ANCHORED) == 0 &&
998    return NULL;      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
999      {
1000      /* Set the character tables in the block that is passed around */
1001    
1002  /* Set the character tables in the block that is passed around */    tables = re->tables;
1003      if (tables == NULL)
1004        (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1005        (void *)(&tables));
1006    
1007      compile_block.lcc = tables + lcc_offset;
1008      compile_block.fcc = tables + fcc_offset;
1009      compile_block.cbits = tables + cbits_offset;
1010      compile_block.ctypes = tables + ctypes_offset;
1011    
1012      /* See if we can find a fixed set of initial characters for the pattern. */
1013    
1014      memset(start_bits, 0, 32 * sizeof(uschar));
1015      bits_set = set_start_bits(code, start_bits,
1016        (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,
1017        &compile_block) == SSB_DONE;
1018      }
1019    
1020  tables = re->tables;  /* Find the minimum length of subject string. */
1021  if (tables == NULL)  
1022    (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,  min = find_minlength(code, code, re->options);
1023    (void *)(&tables));  
1024    /* Return NULL if no optimization is possible. */
1025  compile_block.lcc = tables + lcc_offset;  
1026  compile_block.fcc = tables + fcc_offset;  if (!bits_set && min < 0) return NULL;
 compile_block.cbits = tables + cbits_offset;  
 compile_block.ctypes = tables + ctypes_offset;  
   
 /* See if we can find a fixed set of initial characters for the pattern. */  
   
 memset(start_bits, 0, 32 * sizeof(uschar));  
 if (!set_start_bits(code, start_bits, (re->options & PCRE_CASELESS) != 0,  
   (re->options & PCRE_UTF8) != 0, &compile_block)) return NULL;  
1027    
1028  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in
1029  the latter, which is pointed to by the former, which may also get additional  the latter, which is pointed to by the former, which may also get additional
# Line 473  extra->flags = PCRE_EXTRA_STUDY_DATA; Line 1046  extra->flags = PCRE_EXTRA_STUDY_DATA;
1046  extra->study_data = study;  extra->study_data = study;
1047    
1048  study->size = sizeof(pcre_study_data);  study->size = sizeof(pcre_study_data);
1049  study->options = PCRE_STUDY_MAPPED;  study->flags = 0;
1050  memcpy(study->start_bits, start_bits, sizeof(start_bits));  
1051    if (bits_set)
1052      {
1053      study->flags |= PCRE_STUDY_MAPPED;
1054      memcpy(study->start_bits, start_bits, sizeof(start_bits));
1055      }
1056    
1057    if (min >= 0)
1058      {
1059      study->flags |= PCRE_STUDY_MINLEN;
1060      study->minlength = min;
1061      }
1062    
1063  return extra;  return extra;
1064  }  }

Legend:
Removed from v.85  
changed lines
  Added in v.524

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12