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

Contents of /code/trunk/pcre_study.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 469 - (hide annotations) (download)
Mon Oct 19 14:38:48 2009 UTC (5 years, 1 month ago) by ph10
File MIME type: text/plain
File size: 27040 byte(s)
Final doc and source tidies for 8.00

1 nigel 77 /*************************************************
2     * Perl-Compatible Regular Expressions *
3     *************************************************/
4    
5     /* PCRE is a library of functions to support regular expressions whose syntax
6     and semantics are as close as possible to those of the Perl 5 language.
7    
8     Written by Philip Hazel
9 ph10 455 Copyright (c) 1997-2009 University of Cambridge
10 nigel 77
11     -----------------------------------------------------------------------------
12     Redistribution and use in source and binary forms, with or without
13     modification, are permitted provided that the following conditions are met:
14    
15     * Redistributions of source code must retain the above copyright notice,
16     this list of conditions and the following disclaimer.
17    
18     * Redistributions in binary form must reproduce the above copyright
19     notice, this list of conditions and the following disclaimer in the
20     documentation and/or other materials provided with the distribution.
21    
22     * Neither the name of the University of Cambridge nor the names of its
23     contributors may be used to endorse or promote products derived from
24     this software without specific prior written permission.
25    
26     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36     POSSIBILITY OF SUCH DAMAGE.
37     -----------------------------------------------------------------------------
38     */
39    
40    
41     /* This module contains the external function pcre_study(), along with local
42     supporting functions. */
43    
44    
45 ph10 200 #ifdef HAVE_CONFIG_H
46 ph10 236 #include "config.h"
47 ph10 200 #endif
48 ph10 199
49 nigel 77 #include "pcre_internal.h"
50    
51    
52 nigel 93 /* Returns from set_start_bits() */
53    
54     enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE };
55    
56    
57 ph10 455
58 nigel 77 /*************************************************
59 ph10 455 * Find the minimum subject length for a group *
60     *************************************************/
61    
62     /* Scan a parenthesized group and compute the minimum length of subject that
63 ph10 461 is needed to match it. This is a lower bound; it does not mean there is a
64 ph10 455 string of that length that matches. In UTF8 mode, the result is in characters
65     rather than bytes.
66    
67     Arguments:
68     code pointer to start of group (the bracket)
69     startcode pointer to start of the whole pattern
70 ph10 461 options the compiling options
71 ph10 455
72     Returns: the minimum length
73     -1 if \C was encountered
74 ph10 461 -2 internal error (missing capturing bracket)
75 ph10 455 */
76    
77     static int
78     find_minlength(const uschar *code, const uschar *startcode, int options)
79     {
80     int length = -1;
81     BOOL utf8 = (options & PCRE_UTF8) != 0;
82     BOOL had_recurse = FALSE;
83     register int branchlength = 0;
84     register uschar *cc = (uschar *)code + 1 + LINK_SIZE;
85    
86     if (*code == OP_CBRA || *code == OP_SCBRA) cc += 2;
87    
88     /* Scan along the opcodes for this branch. If we get to the end of the
89     branch, check the length against that of the other branches. */
90    
91     for (;;)
92     {
93     int d, min;
94 ph10 461 uschar *cs, *ce;
95 ph10 455 register int op = *cc;
96 ph10 461
97 ph10 455 switch (op)
98     {
99     case OP_CBRA:
100 ph10 461 case OP_SCBRA:
101 ph10 455 case OP_BRA:
102 ph10 461 case OP_SBRA:
103 ph10 455 case OP_ONCE:
104     case OP_COND:
105 ph10 461 case OP_SCOND:
106 ph10 455 d = find_minlength(cc, startcode, options);
107     if (d < 0) return d;
108     branchlength += d;
109     do cc += GET(cc, 1); while (*cc == OP_ALT);
110     cc += 1 + LINK_SIZE;
111     break;
112    
113     /* Reached end of a branch; if it's a ket it is the end of a nested
114     call. If it's ALT it is an alternation in a nested call. If it is
115     END it's the end of the outer call. All can be handled by the same code. */
116    
117     case OP_ALT:
118     case OP_KET:
119     case OP_KETRMAX:
120     case OP_KETRMIN:
121     case OP_END:
122 ph10 461 if (length < 0 || (!had_recurse && branchlength < length))
123 ph10 455 length = branchlength;
124     if (*cc != OP_ALT) return length;
125     cc += 1 + LINK_SIZE;
126     branchlength = 0;
127 ph10 461 had_recurse = FALSE;
128 ph10 455 break;
129    
130     /* Skip over assertive subpatterns */
131    
132     case OP_ASSERT:
133     case OP_ASSERT_NOT:
134     case OP_ASSERTBACK:
135     case OP_ASSERTBACK_NOT:
136     do cc += GET(cc, 1); while (*cc == OP_ALT);
137     /* Fall through */
138    
139     /* Skip over things that don't match chars */
140    
141     case OP_REVERSE:
142     case OP_CREF:
143 ph10 459 case OP_NCREF:
144 ph10 455 case OP_RREF:
145 ph10 459 case OP_NRREF:
146 ph10 455 case OP_DEF:
147     case OP_OPT:
148     case OP_CALLOUT:
149     case OP_SOD:
150     case OP_SOM:
151     case OP_EOD:
152     case OP_EODN:
153     case OP_CIRC:
154     case OP_DOLL:
155     case OP_NOT_WORD_BOUNDARY:
156     case OP_WORD_BOUNDARY:
157     cc += _pcre_OP_lengths[*cc];
158     break;
159 ph10 461
160 ph10 455 /* Skip over a subpattern that has a {0} or {0,x} quantifier */
161    
162     case OP_BRAZERO:
163 ph10 461 case OP_BRAMINZERO:
164 ph10 455 case OP_SKIPZERO:
165     cc += _pcre_OP_lengths[*cc];
166     do cc += GET(cc, 1); while (*cc == OP_ALT);
167     cc += 1 + LINK_SIZE;
168     break;
169    
170     /* Handle literal characters and + repetitions */
171    
172     case OP_CHAR:
173     case OP_CHARNC:
174     case OP_NOT:
175     case OP_PLUS:
176     case OP_MINPLUS:
177     case OP_POSPLUS:
178     case OP_NOTPLUS:
179     case OP_NOTMINPLUS:
180     case OP_NOTPOSPLUS:
181     branchlength++;
182     cc += 2;
183     #ifdef SUPPORT_UTF8
184     if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
185     #endif
186     break;
187 ph10 461
188 ph10 455 case OP_TYPEPLUS:
189     case OP_TYPEMINPLUS:
190 ph10 461 case OP_TYPEPOSPLUS:
191 ph10 455 branchlength++;
192     cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
193     break;
194    
195     /* Handle exact repetitions. The count is already in characters, but we
196     need to skip over a multibyte character in UTF8 mode. */
197    
198     case OP_EXACT:
199 ph10 461 case OP_NOTEXACT:
200 ph10 455 branchlength += GET2(cc,1);
201     cc += 4;
202     #ifdef SUPPORT_UTF8
203     if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
204     #endif
205     break;
206    
207     case OP_TYPEEXACT:
208     branchlength += GET2(cc,1);
209     cc += (cc[3] == OP_PROP || cc[3] == OP_NOTPROP)? 6 : 4;
210     break;
211    
212     /* Handle single-char non-literal matchers */
213    
214     case OP_PROP:
215     case OP_NOTPROP:
216     cc += 2;
217     /* Fall through */
218    
219     case OP_NOT_DIGIT:
220     case OP_DIGIT:
221     case OP_NOT_WHITESPACE:
222     case OP_WHITESPACE:
223     case OP_NOT_WORDCHAR:
224     case OP_WORDCHAR:
225     case OP_ANY:
226     case OP_ALLANY:
227     case OP_EXTUNI:
228 ph10 461 case OP_HSPACE:
229 ph10 455 case OP_NOT_HSPACE:
230     case OP_VSPACE:
231 ph10 461 case OP_NOT_VSPACE:
232 ph10 455 branchlength++;
233     cc++;
234     break;
235 ph10 461
236 ph10 455 /* "Any newline" might match two characters */
237 ph10 461
238 ph10 455 case OP_ANYNL:
239     branchlength += 2;
240     cc++;
241 ph10 461 break;
242 ph10 455
243     /* The single-byte matcher means we can't proceed in UTF-8 mode */
244    
245     case OP_ANYBYTE:
246     #ifdef SUPPORT_UTF8
247     if (utf8) return -1;
248     #endif
249     branchlength++;
250     cc++;
251 ph10 461 break;
252 ph10 455
253     /* For repeated character types, we have to test for \p and \P, which have
254     an extra two bytes of parameters. */
255    
256     case OP_TYPESTAR:
257     case OP_TYPEMINSTAR:
258     case OP_TYPEQUERY:
259     case OP_TYPEMINQUERY:
260     case OP_TYPEPOSSTAR:
261     case OP_TYPEPOSQUERY:
262     if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
263     cc += _pcre_OP_lengths[op];
264     break;
265    
266     case OP_TYPEUPTO:
267     case OP_TYPEMINUPTO:
268     case OP_TYPEPOSUPTO:
269     if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;
270     cc += _pcre_OP_lengths[op];
271     break;
272    
273     /* Check a class for variable quantification */
274    
275     #ifdef SUPPORT_UTF8
276     case OP_XCLASS:
277     cc += GET(cc, 1) - 33;
278     /* Fall through */
279     #endif
280    
281     case OP_CLASS:
282     case OP_NCLASS:
283     cc += 33;
284    
285     switch (*cc)
286     {
287     case OP_CRPLUS:
288     case OP_CRMINPLUS:
289     branchlength++;
290 ph10 461 /* Fall through */
291 ph10 455
292     case OP_CRSTAR:
293     case OP_CRMINSTAR:
294     case OP_CRQUERY:
295     case OP_CRMINQUERY:
296 ph10 461 cc++;
297 ph10 455 break;
298 ph10 461
299 ph10 455 case OP_CRRANGE:
300     case OP_CRMINRANGE:
301     branchlength += GET2(cc,1);
302     cc += 5;
303     break;
304 ph10 461
305 ph10 455 default:
306     branchlength++;
307 ph10 461 break;
308 ph10 455 }
309     break;
310 ph10 461
311     /* Backreferences and subroutine calls are treated in the same way: we find
312     the minimum length for the subpattern. A recursion, however, causes an
313 ph10 455 a flag to be set that causes the length of this branch to be ignored. The
314     logic is that a recursion can only make sense if there is another
315     alternation that stops the recursing. That will provide the minimum length
316     (when no recursion happens). A backreference within the group that it is
317 ph10 469 referencing behaves in the same way.
318    
319 ph10 467 If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
320     matches an empty string (by default it causes a matching failure), so in
321     that case we must set the minimum length to zero. */
322 ph10 461
323 ph10 455 case OP_REF:
324 ph10 467 if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
325 ph10 469 {
326 ph10 467 ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
327     if (cs == NULL) return -2;
328     do ce += GET(ce, 1); while (*ce == OP_ALT);
329     if (cc > cs && cc < ce)
330     {
331     d = 0;
332     had_recurse = TRUE;
333     }
334     else d = find_minlength(cs, startcode, options);
335 ph10 461 }
336 ph10 469 else d = 0;
337 ph10 461 cc += 3;
338 ph10 455
339     /* Handle repeated back references */
340 ph10 461
341 ph10 455 switch (*cc)
342     {
343     case OP_CRSTAR:
344     case OP_CRMINSTAR:
345     case OP_CRQUERY:
346     case OP_CRMINQUERY:
347     min = 0;
348     cc++;
349     break;
350 ph10 461
351 ph10 455 case OP_CRRANGE:
352     case OP_CRMINRANGE:
353     min = GET2(cc, 1);
354     cc += 5;
355     break;
356 ph10 461
357 ph10 455 default:
358     min = 1;
359     break;
360     }
361    
362     branchlength += min * d;
363 ph10 461 break;
364 ph10 455
365 ph10 461 case OP_RECURSE:
366 ph10 455 cs = ce = (uschar *)startcode + GET(cc, 1);
367     if (cs == NULL) return -2;
368     do ce += GET(ce, 1); while (*ce == OP_ALT);
369     if (cc > cs && cc < ce)
370 ph10 461 had_recurse = TRUE;
371     else
372 ph10 455 branchlength += find_minlength(cs, startcode, options);
373     cc += 1 + LINK_SIZE;
374     break;
375    
376     /* Anything else does not or need not match a character. We can get the
377 ph10 461 item's length from the table, but for those that can match zero occurrences
378     of a character, we must take special action for UTF-8 characters. */
379    
380 ph10 455 case OP_UPTO:
381 ph10 461 case OP_NOTUPTO:
382 ph10 455 case OP_MINUPTO:
383 ph10 461 case OP_NOTMINUPTO:
384 ph10 455 case OP_POSUPTO:
385     case OP_STAR:
386     case OP_MINSTAR:
387 ph10 461 case OP_NOTMINSTAR:
388 ph10 455 case OP_POSSTAR:
389 ph10 461 case OP_NOTPOSSTAR:
390 ph10 455 case OP_QUERY:
391     case OP_MINQUERY:
392     case OP_NOTMINQUERY:
393     case OP_POSQUERY:
394 ph10 461 case OP_NOTPOSQUERY:
395 ph10 455 cc += _pcre_OP_lengths[op];
396 ph10 461 #ifdef SUPPORT_UTF8
397 ph10 455 if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
398 ph10 461 #endif
399 ph10 455 break;
400    
401     /* For the record, these are the opcodes that are matched by "default":
402     OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,
403     OP_THEN. */
404 ph10 461
405 ph10 455 default:
406     cc += _pcre_OP_lengths[op];
407     break;
408     }
409     }
410     /* Control never gets here */
411     }
412    
413    
414    
415     /*************************************************
416 nigel 77 * Set a bit and maybe its alternate case *
417     *************************************************/
418    
419     /* Given a character, set its bit in the table, and also the bit for the other
420     version of a letter if we are caseless.
421    
422     Arguments:
423     start_bits points to the bit map
424     c is the character
425     caseless the caseless flag
426     cd the block with char table pointers
427    
428     Returns: nothing
429     */
430    
431     static void
432     set_bit(uschar *start_bits, unsigned int c, BOOL caseless, compile_data *cd)
433     {
434     start_bits[c/8] |= (1 << (c&7));
435     if (caseless && (cd->ctypes[c] & ctype_letter) != 0)
436     start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));
437     }
438    
439    
440    
441     /*************************************************
442 nigel 93 * Create bitmap of starting bytes *
443 nigel 77 *************************************************/
444    
445 nigel 93 /* This function scans a compiled unanchored expression recursively and
446     attempts to build a bitmap of the set of possible starting bytes. As time goes
447     by, we may be able to get more clever at doing this. The SSB_CONTINUE return is
448     useful for parenthesized groups in patterns such as (a*)b where the group
449     provides some optional starting bytes but scanning must continue at the outer
450     level to find at least one mandatory byte. At the outermost level, this
451     function fails unless the result is SSB_DONE.
452 nigel 77
453     Arguments:
454     code points to an expression
455     start_bits points to a 32-byte table, initialized to 0
456     caseless the current state of the caseless flag
457     utf8 TRUE if in UTF-8 mode
458     cd the block with char table pointers
459    
460 nigel 93 Returns: SSB_FAIL => Failed to find any starting bytes
461     SSB_DONE => Found mandatory starting bytes
462     SSB_CONTINUE => Found optional starting bytes
463 nigel 77 */
464    
465 nigel 93 static int
466 nigel 77 set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,
467     BOOL utf8, compile_data *cd)
468     {
469     register int c;
470 nigel 93 int yield = SSB_DONE;
471 nigel 77
472 nigel 91 #if 0
473     /* ========================================================================= */
474     /* The following comment and code was inserted in January 1999. In May 2006,
475     when it was observed to cause compiler warnings about unused values, I took it
476     out again. If anybody is still using OS/2, they will have to put it back
477     manually. */
478    
479 nigel 77 /* This next statement and the later reference to dummy are here in order to
480     trick the optimizer of the IBM C compiler for OS/2 into generating correct
481     code. Apparently IBM isn't going to fix the problem, and we would rather not
482     disable optimization (in this module it actually makes a big difference, and
483     the pcre module can use all the optimization it can get). */
484    
485     volatile int dummy;
486 nigel 91 /* ========================================================================= */
487     #endif
488 nigel 77
489     do
490     {
491 nigel 93 const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;
492 nigel 77 BOOL try_next = TRUE;
493    
494 nigel 93 while (try_next) /* Loop for items in this branch */
495 nigel 77 {
496 nigel 93 int rc;
497     switch(*tcode)
498 nigel 77 {
499 nigel 93 /* Fail if we reach something we don't understand */
500 nigel 77
501     default:
502 nigel 93 return SSB_FAIL;
503 nigel 77
504 nigel 93 /* If we hit a bracket or a positive lookahead assertion, recurse to set
505     bits from within the subpattern. If it can't find anything, we have to
506     give up. If it finds some mandatory character(s), we are done for this
507     branch. Otherwise, carry on scanning after the subpattern. */
508    
509     case OP_BRA:
510     case OP_SBRA:
511     case OP_CBRA:
512     case OP_SCBRA:
513     case OP_ONCE:
514     case OP_ASSERT:
515     rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);
516     if (rc == SSB_FAIL) return SSB_FAIL;
517     if (rc == SSB_DONE) try_next = FALSE; else
518     {
519     do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
520     tcode += 1 + LINK_SIZE;
521     }
522     break;
523    
524     /* If we hit ALT or KET, it means we haven't found anything mandatory in
525     this branch, though we might have found something optional. For ALT, we
526     continue with the next alternative, but we have to arrange that the final
527     result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
528     return SSB_CONTINUE: if this is the top level, that indicates failure,
529     but after a nested subpattern, it causes scanning to continue. */
530    
531     case OP_ALT:
532     yield = SSB_CONTINUE;
533     try_next = FALSE;
534     break;
535    
536     case OP_KET:
537     case OP_KETRMAX:
538     case OP_KETRMIN:
539     return SSB_CONTINUE;
540    
541 nigel 77 /* Skip over callout */
542    
543     case OP_CALLOUT:
544     tcode += 2 + 2*LINK_SIZE;
545     break;
546    
547     /* Skip over lookbehind and negative lookahead assertions */
548    
549     case OP_ASSERT_NOT:
550     case OP_ASSERTBACK:
551     case OP_ASSERTBACK_NOT:
552     do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
553 nigel 93 tcode += 1 + LINK_SIZE;
554 nigel 77 break;
555    
556     /* Skip over an option setting, changing the caseless flag */
557    
558     case OP_OPT:
559     caseless = (tcode[1] & PCRE_CASELESS) != 0;
560     tcode += 2;
561     break;
562    
563     /* BRAZERO does the bracket, but carries on. */
564    
565     case OP_BRAZERO:
566     case OP_BRAMINZERO:
567 nigel 93 if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)
568     return SSB_FAIL;
569 nigel 91 /* =========================================================================
570     See the comment at the head of this function concerning the next line,
571     which was an old fudge for the benefit of OS/2.
572 nigel 77 dummy = 1;
573 nigel 91 ========================================================================= */
574 nigel 77 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
575 nigel 93 tcode += 1 + LINK_SIZE;
576 nigel 77 break;
577    
578 ph10 335 /* SKIPZERO skips the bracket. */
579    
580     case OP_SKIPZERO:
581 ph10 358 tcode++;
582 ph10 335 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
583     tcode += 1 + LINK_SIZE;
584     break;
585    
586 nigel 77 /* Single-char * or ? sets the bit and tries the next item */
587    
588     case OP_STAR:
589     case OP_MINSTAR:
590 nigel 93 case OP_POSSTAR:
591 nigel 77 case OP_QUERY:
592     case OP_MINQUERY:
593 nigel 93 case OP_POSQUERY:
594 nigel 77 set_bit(start_bits, tcode[1], caseless, cd);
595     tcode += 2;
596     #ifdef SUPPORT_UTF8
597 nigel 93 if (utf8 && tcode[-1] >= 0xc0)
598     tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];
599 nigel 77 #endif
600     break;
601    
602     /* Single-char upto sets the bit and tries the next */
603    
604     case OP_UPTO:
605     case OP_MINUPTO:
606 nigel 93 case OP_POSUPTO:
607 nigel 77 set_bit(start_bits, tcode[3], caseless, cd);
608     tcode += 4;
609     #ifdef SUPPORT_UTF8
610 nigel 93 if (utf8 && tcode[-1] >= 0xc0)
611     tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];
612 nigel 77 #endif
613     break;
614    
615     /* At least one single char sets the bit and stops */
616    
617     case OP_EXACT: /* Fall through */
618     tcode += 2;
619    
620     case OP_CHAR:
621     case OP_CHARNC:
622     case OP_PLUS:
623     case OP_MINPLUS:
624 nigel 93 case OP_POSPLUS:
625 nigel 77 set_bit(start_bits, tcode[1], caseless, cd);
626     try_next = FALSE;
627     break;
628    
629     /* Single character type sets the bits and stops */
630    
631     case OP_NOT_DIGIT:
632     for (c = 0; c < 32; c++)
633     start_bits[c] |= ~cd->cbits[c+cbit_digit];
634     try_next = FALSE;
635     break;
636    
637     case OP_DIGIT:
638     for (c = 0; c < 32; c++)
639     start_bits[c] |= cd->cbits[c+cbit_digit];
640     try_next = FALSE;
641     break;
642    
643 nigel 91 /* The cbit_space table has vertical tab as whitespace; we have to
644     discard it. */
645    
646 nigel 77 case OP_NOT_WHITESPACE:
647     for (c = 0; c < 32; c++)
648 nigel 91 {
649     int d = cd->cbits[c+cbit_space];
650     if (c == 1) d &= ~0x08;
651     start_bits[c] |= ~d;
652     }
653 nigel 77 try_next = FALSE;
654     break;
655    
656 nigel 91 /* The cbit_space table has vertical tab as whitespace; we have to
657     discard it. */
658    
659 nigel 77 case OP_WHITESPACE:
660     for (c = 0; c < 32; c++)
661 nigel 91 {
662     int d = cd->cbits[c+cbit_space];
663     if (c == 1) d &= ~0x08;
664     start_bits[c] |= d;
665     }
666 nigel 77 try_next = FALSE;
667     break;
668    
669     case OP_NOT_WORDCHAR:
670     for (c = 0; c < 32; c++)
671     start_bits[c] |= ~cd->cbits[c+cbit_word];
672     try_next = FALSE;
673     break;
674    
675     case OP_WORDCHAR:
676     for (c = 0; c < 32; c++)
677     start_bits[c] |= cd->cbits[c+cbit_word];
678     try_next = FALSE;
679     break;
680    
681     /* One or more character type fudges the pointer and restarts, knowing
682     it will hit a single character type and stop there. */
683    
684     case OP_TYPEPLUS:
685     case OP_TYPEMINPLUS:
686     tcode++;
687     break;
688    
689     case OP_TYPEEXACT:
690     tcode += 3;
691     break;
692    
693     /* Zero or more repeats of character types set the bits and then
694     try again. */
695    
696     case OP_TYPEUPTO:
697     case OP_TYPEMINUPTO:
698 nigel 93 case OP_TYPEPOSUPTO:
699 nigel 77 tcode += 2; /* Fall through */
700    
701     case OP_TYPESTAR:
702     case OP_TYPEMINSTAR:
703 nigel 93 case OP_TYPEPOSSTAR:
704 nigel 77 case OP_TYPEQUERY:
705     case OP_TYPEMINQUERY:
706 nigel 93 case OP_TYPEPOSQUERY:
707 nigel 77 switch(tcode[1])
708     {
709     case OP_ANY:
710 ph10 345 case OP_ALLANY:
711 nigel 93 return SSB_FAIL;
712 nigel 77
713     case OP_NOT_DIGIT:
714     for (c = 0; c < 32; c++)
715     start_bits[c] |= ~cd->cbits[c+cbit_digit];
716     break;
717    
718     case OP_DIGIT:
719     for (c = 0; c < 32; c++)
720     start_bits[c] |= cd->cbits[c+cbit_digit];
721     break;
722    
723 nigel 91 /* The cbit_space table has vertical tab as whitespace; we have to
724     discard it. */
725    
726 nigel 77 case OP_NOT_WHITESPACE:
727     for (c = 0; c < 32; c++)
728 nigel 91 {
729     int d = cd->cbits[c+cbit_space];
730     if (c == 1) d &= ~0x08;
731     start_bits[c] |= ~d;
732     }
733 nigel 77 break;
734    
735 nigel 91 /* The cbit_space table has vertical tab as whitespace; we have to
736     discard it. */
737    
738 nigel 77 case OP_WHITESPACE:
739     for (c = 0; c < 32; c++)
740 nigel 91 {
741     int d = cd->cbits[c+cbit_space];
742     if (c == 1) d &= ~0x08;
743     start_bits[c] |= d;
744     }
745 nigel 77 break;
746    
747     case OP_NOT_WORDCHAR:
748     for (c = 0; c < 32; c++)
749     start_bits[c] |= ~cd->cbits[c+cbit_word];
750     break;
751    
752     case OP_WORDCHAR:
753     for (c = 0; c < 32; c++)
754     start_bits[c] |= cd->cbits[c+cbit_word];
755     break;
756     }
757    
758     tcode += 2;
759     break;
760    
761     /* Character class where all the information is in a bit map: set the
762     bits and either carry on or not, according to the repeat count. If it was
763     a negative class, and we are operating with UTF-8 characters, any byte
764     with a value >= 0xc4 is a potentially valid starter because it starts a
765     character with a value > 255. */
766    
767     case OP_NCLASS:
768 ph10 111 #ifdef SUPPORT_UTF8
769 nigel 77 if (utf8)
770     {
771     start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
772     memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
773     }
774 ph10 111 #endif
775 nigel 77 /* Fall through */
776    
777     case OP_CLASS:
778     {
779     tcode++;
780    
781     /* In UTF-8 mode, the bits in a bit map correspond to character
782     values, not to byte values. However, the bit map we are constructing is
783     for byte values. So we have to do a conversion for characters whose
784     value is > 127. In fact, there are only two possible starting bytes for
785     characters in the range 128 - 255. */
786    
787 ph10 107 #ifdef SUPPORT_UTF8
788 nigel 77 if (utf8)
789     {
790     for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];
791     for (c = 128; c < 256; c++)
792     {
793     if ((tcode[c/8] && (1 << (c&7))) != 0)
794     {
795     int d = (c >> 6) | 0xc0; /* Set bit for this starter */
796     start_bits[d/8] |= (1 << (d&7)); /* and then skip on to the */
797     c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */
798     }
799     }
800     }
801    
802     /* In non-UTF-8 mode, the two bit maps are completely compatible. */
803    
804     else
805 ph10 111 #endif
806 nigel 77 {
807     for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
808     }
809    
810     /* Advance past the bit map, and act on what follows */
811    
812     tcode += 32;
813     switch (*tcode)
814     {
815     case OP_CRSTAR:
816     case OP_CRMINSTAR:
817     case OP_CRQUERY:
818     case OP_CRMINQUERY:
819     tcode++;
820     break;
821    
822     case OP_CRRANGE:
823     case OP_CRMINRANGE:
824     if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;
825     else try_next = FALSE;
826     break;
827    
828     default:
829     try_next = FALSE;
830     break;
831     }
832     }
833     break; /* End of bitmap class handling */
834    
835     } /* End of switch */
836     } /* End of try_next loop */
837    
838     code += GET(code, 1); /* Advance to next branch */
839     }
840     while (*code == OP_ALT);
841 nigel 93 return yield;
842 nigel 77 }
843    
844    
845    
846     /*************************************************
847     * Study a compiled expression *
848     *************************************************/
849    
850     /* This function is handed a compiled expression that it must study to produce
851     information that will speed up the matching. It returns a pcre_extra block
852     which then gets handed back to pcre_exec().
853    
854     Arguments:
855     re points to the compiled expression
856     options contains option bits
857     errorptr points to where to place error messages;
858     set NULL unless error
859    
860     Returns: pointer to a pcre_extra block, with study_data filled in and the
861 ph10 455 appropriate flags set;
862 nigel 77 NULL on error or if no optimization possible
863     */
864    
865 ph10 359 PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
866 nigel 77 pcre_study(const pcre *external_re, int options, const char **errorptr)
867     {
868 ph10 455 int min;
869     BOOL bits_set = FALSE;
870 nigel 77 uschar start_bits[32];
871     pcre_extra *extra;
872     pcre_study_data *study;
873     const uschar *tables;
874 nigel 91 uschar *code;
875     compile_data compile_block;
876 nigel 77 const real_pcre *re = (const real_pcre *)external_re;
877    
878     *errorptr = NULL;
879    
880     if (re == NULL || re->magic_number != MAGIC_NUMBER)
881     {
882     *errorptr = "argument is not a compiled regular expression";
883     return NULL;
884     }
885    
886     if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
887     {
888     *errorptr = "unknown or incorrect option bit(s) set";
889     return NULL;
890     }
891    
892 nigel 91 code = (uschar *)re + re->name_table_offset +
893     (re->name_count * re->name_entry_size);
894    
895 nigel 77 /* For an anchored pattern, or an unanchored pattern that has a first char, or
896 ph10 461 a multiline pattern that matches only at "line starts", there is no point in
897 ph10 455 seeking a list of starting bytes. */
898 nigel 77
899 ph10 455 if ((re->options & PCRE_ANCHORED) == 0 &&
900     (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
901     {
902     /* Set the character tables in the block that is passed around */
903 ph10 461
904 ph10 455 tables = re->tables;
905     if (tables == NULL)
906     (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
907     (void *)(&tables));
908 ph10 461
909 ph10 455 compile_block.lcc = tables + lcc_offset;
910     compile_block.fcc = tables + fcc_offset;
911     compile_block.cbits = tables + cbits_offset;
912     compile_block.ctypes = tables + ctypes_offset;
913 ph10 461
914 ph10 455 /* See if we can find a fixed set of initial characters for the pattern. */
915 ph10 461
916 ph10 455 memset(start_bits, 0, 32 * sizeof(uschar));
917 ph10 461 bits_set = set_start_bits(code, start_bits,
918     (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,
919 ph10 455 &compile_block) == SSB_DONE;
920     }
921 ph10 461
922 ph10 455 /* Find the minimum length of subject string. */
923 nigel 77
924 ph10 455 min = find_minlength(code, code, re->options);
925 nigel 77
926 ph10 455 /* Return NULL if no optimization is possible. */
927 nigel 77
928 ph10 455 if (!bits_set && min < 0) return NULL;
929 nigel 77
930     /* Get a pcre_extra block and a pcre_study_data block. The study data is put in
931     the latter, which is pointed to by the former, which may also get additional
932     data set later by the calling program. At the moment, the size of
933     pcre_study_data is fixed. We nevertheless save it in a field for returning via
934     the pcre_fullinfo() function so that if it becomes variable in the future, we
935     don't have to change that code. */
936    
937     extra = (pcre_extra *)(pcre_malloc)
938     (sizeof(pcre_extra) + sizeof(pcre_study_data));
939    
940     if (extra == NULL)
941     {
942     *errorptr = "failed to get memory";
943     return NULL;
944     }
945    
946     study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
947     extra->flags = PCRE_EXTRA_STUDY_DATA;
948     extra->study_data = study;
949    
950     study->size = sizeof(pcre_study_data);
951 ph10 455 study->flags = 0;
952 nigel 77
953 ph10 455 if (bits_set)
954     {
955     study->flags |= PCRE_STUDY_MAPPED;
956     memcpy(study->start_bits, start_bits, sizeof(start_bits));
957     }
958 ph10 461
959 ph10 455 if (min >= 0)
960     {
961     study->flags |= PCRE_STUDY_MINLEN;
962     study->minlength = min;
963 ph10 461 }
964 ph10 455
965 nigel 77 return extra;
966     }
967    
968     /* End of pcre_study.c */

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12