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

Contents of /code/trunk/pcre_study.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 520 - (hide annotations) (download)
Sat May 22 18:54:05 2010 UTC (4 years, 5 months ago) by ph10
File MIME type: text/plain
File size: 28252 byte(s)
Fix caseless bug with pcre_study() for starting letter greater than 127.

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 475 Copyright (c) 1997-2010 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 ph10 471 case OP_COND:
100     case OP_SCOND:
101    
102     /* If there is only one branch in a condition, the implied branch has zero
103     length, so we don't add anything. This covers the DEFINE "condition"
104     automatically. */
105    
106     cs = cc + GET(cc, 1);
107     if (*cs != OP_ALT)
108     {
109     cc = cs + 1 + LINK_SIZE;
110     break;
111     }
112    
113     /* Otherwise we can fall through and treat it the same as any other
114     subpattern. */
115    
116 ph10 455 case OP_CBRA:
117 ph10 461 case OP_SCBRA:
118 ph10 455 case OP_BRA:
119 ph10 461 case OP_SBRA:
120 ph10 455 case OP_ONCE:
121     d = find_minlength(cc, startcode, options);
122     if (d < 0) return d;
123     branchlength += d;
124     do cc += GET(cc, 1); while (*cc == OP_ALT);
125     cc += 1 + LINK_SIZE;
126     break;
127    
128     /* Reached end of a branch; if it's a ket it is the end of a nested
129     call. If it's ALT it is an alternation in a nested call. If it is
130     END it's the end of the outer call. All can be handled by the same code. */
131    
132     case OP_ALT:
133     case OP_KET:
134     case OP_KETRMAX:
135     case OP_KETRMIN:
136     case OP_END:
137 ph10 461 if (length < 0 || (!had_recurse && branchlength < length))
138 ph10 455 length = branchlength;
139     if (*cc != OP_ALT) return length;
140     cc += 1 + LINK_SIZE;
141     branchlength = 0;
142 ph10 461 had_recurse = FALSE;
143 ph10 455 break;
144    
145     /* Skip over assertive subpatterns */
146    
147     case OP_ASSERT:
148     case OP_ASSERT_NOT:
149     case OP_ASSERTBACK:
150     case OP_ASSERTBACK_NOT:
151     do cc += GET(cc, 1); while (*cc == OP_ALT);
152     /* Fall through */
153    
154     /* Skip over things that don't match chars */
155    
156     case OP_REVERSE:
157     case OP_CREF:
158 ph10 459 case OP_NCREF:
159 ph10 455 case OP_RREF:
160 ph10 459 case OP_NRREF:
161 ph10 455 case OP_DEF:
162     case OP_OPT:
163     case OP_CALLOUT:
164     case OP_SOD:
165     case OP_SOM:
166     case OP_EOD:
167     case OP_EODN:
168     case OP_CIRC:
169     case OP_DOLL:
170     case OP_NOT_WORD_BOUNDARY:
171     case OP_WORD_BOUNDARY:
172     cc += _pcre_OP_lengths[*cc];
173     break;
174 ph10 461
175 ph10 455 /* Skip over a subpattern that has a {0} or {0,x} quantifier */
176    
177     case OP_BRAZERO:
178 ph10 461 case OP_BRAMINZERO:
179 ph10 455 case OP_SKIPZERO:
180     cc += _pcre_OP_lengths[*cc];
181     do cc += GET(cc, 1); while (*cc == OP_ALT);
182     cc += 1 + LINK_SIZE;
183     break;
184    
185     /* Handle literal characters and + repetitions */
186    
187     case OP_CHAR:
188     case OP_CHARNC:
189     case OP_NOT:
190     case OP_PLUS:
191     case OP_MINPLUS:
192     case OP_POSPLUS:
193     case OP_NOTPLUS:
194     case OP_NOTMINPLUS:
195     case OP_NOTPOSPLUS:
196     branchlength++;
197     cc += 2;
198     #ifdef SUPPORT_UTF8
199     if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
200     #endif
201     break;
202 ph10 461
203 ph10 455 case OP_TYPEPLUS:
204     case OP_TYPEMINPLUS:
205 ph10 461 case OP_TYPEPOSPLUS:
206 ph10 455 branchlength++;
207     cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
208     break;
209    
210     /* Handle exact repetitions. The count is already in characters, but we
211     need to skip over a multibyte character in UTF8 mode. */
212    
213     case OP_EXACT:
214 ph10 461 case OP_NOTEXACT:
215 ph10 455 branchlength += GET2(cc,1);
216     cc += 4;
217     #ifdef SUPPORT_UTF8
218     if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
219     #endif
220     break;
221    
222     case OP_TYPEEXACT:
223     branchlength += GET2(cc,1);
224     cc += (cc[3] == OP_PROP || cc[3] == OP_NOTPROP)? 6 : 4;
225     break;
226    
227     /* Handle single-char non-literal matchers */
228    
229     case OP_PROP:
230     case OP_NOTPROP:
231     cc += 2;
232     /* Fall through */
233    
234     case OP_NOT_DIGIT:
235     case OP_DIGIT:
236     case OP_NOT_WHITESPACE:
237     case OP_WHITESPACE:
238     case OP_NOT_WORDCHAR:
239     case OP_WORDCHAR:
240     case OP_ANY:
241     case OP_ALLANY:
242     case OP_EXTUNI:
243 ph10 461 case OP_HSPACE:
244 ph10 455 case OP_NOT_HSPACE:
245     case OP_VSPACE:
246 ph10 461 case OP_NOT_VSPACE:
247 ph10 455 branchlength++;
248     cc++;
249     break;
250 ph10 461
251 ph10 455 /* "Any newline" might match two characters */
252 ph10 461
253 ph10 455 case OP_ANYNL:
254     branchlength += 2;
255     cc++;
256 ph10 461 break;
257 ph10 455
258     /* The single-byte matcher means we can't proceed in UTF-8 mode */
259    
260     case OP_ANYBYTE:
261     #ifdef SUPPORT_UTF8
262     if (utf8) return -1;
263     #endif
264     branchlength++;
265     cc++;
266 ph10 461 break;
267 ph10 455
268     /* For repeated character types, we have to test for \p and \P, which have
269     an extra two bytes of parameters. */
270    
271     case OP_TYPESTAR:
272     case OP_TYPEMINSTAR:
273     case OP_TYPEQUERY:
274     case OP_TYPEMINQUERY:
275     case OP_TYPEPOSSTAR:
276     case OP_TYPEPOSQUERY:
277     if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
278     cc += _pcre_OP_lengths[op];
279     break;
280    
281     case OP_TYPEUPTO:
282     case OP_TYPEMINUPTO:
283     case OP_TYPEPOSUPTO:
284     if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;
285     cc += _pcre_OP_lengths[op];
286     break;
287    
288     /* Check a class for variable quantification */
289    
290     #ifdef SUPPORT_UTF8
291     case OP_XCLASS:
292     cc += GET(cc, 1) - 33;
293     /* Fall through */
294     #endif
295    
296     case OP_CLASS:
297     case OP_NCLASS:
298     cc += 33;
299    
300     switch (*cc)
301     {
302     case OP_CRPLUS:
303     case OP_CRMINPLUS:
304     branchlength++;
305 ph10 461 /* Fall through */
306 ph10 455
307     case OP_CRSTAR:
308     case OP_CRMINSTAR:
309     case OP_CRQUERY:
310     case OP_CRMINQUERY:
311 ph10 461 cc++;
312 ph10 455 break;
313 ph10 461
314 ph10 455 case OP_CRRANGE:
315     case OP_CRMINRANGE:
316     branchlength += GET2(cc,1);
317     cc += 5;
318     break;
319 ph10 461
320 ph10 455 default:
321     branchlength++;
322 ph10 461 break;
323 ph10 455 }
324     break;
325 ph10 461
326     /* Backreferences and subroutine calls are treated in the same way: we find
327     the minimum length for the subpattern. A recursion, however, causes an
328 ph10 455 a flag to be set that causes the length of this branch to be ignored. The
329     logic is that a recursion can only make sense if there is another
330     alternation that stops the recursing. That will provide the minimum length
331     (when no recursion happens). A backreference within the group that it is
332 ph10 469 referencing behaves in the same way.
333    
334 ph10 467 If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
335     matches an empty string (by default it causes a matching failure), so in
336     that case we must set the minimum length to zero. */
337 ph10 461
338 ph10 455 case OP_REF:
339 ph10 467 if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
340 ph10 469 {
341 ph10 467 ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
342     if (cs == NULL) return -2;
343     do ce += GET(ce, 1); while (*ce == OP_ALT);
344     if (cc > cs && cc < ce)
345     {
346     d = 0;
347     had_recurse = TRUE;
348     }
349     else d = find_minlength(cs, startcode, options);
350 ph10 461 }
351 ph10 469 else d = 0;
352 ph10 461 cc += 3;
353 ph10 455
354     /* Handle repeated back references */
355 ph10 461
356 ph10 455 switch (*cc)
357     {
358     case OP_CRSTAR:
359     case OP_CRMINSTAR:
360     case OP_CRQUERY:
361     case OP_CRMINQUERY:
362     min = 0;
363     cc++;
364     break;
365 ph10 461
366 ph10 455 case OP_CRRANGE:
367     case OP_CRMINRANGE:
368     min = GET2(cc, 1);
369     cc += 5;
370     break;
371 ph10 461
372 ph10 455 default:
373     min = 1;
374     break;
375     }
376    
377     branchlength += min * d;
378 ph10 461 break;
379 ph10 455
380 ph10 461 case OP_RECURSE:
381 ph10 455 cs = ce = (uschar *)startcode + GET(cc, 1);
382     if (cs == NULL) return -2;
383     do ce += GET(ce, 1); while (*ce == OP_ALT);
384     if (cc > cs && cc < ce)
385 ph10 461 had_recurse = TRUE;
386     else
387 ph10 455 branchlength += find_minlength(cs, startcode, options);
388     cc += 1 + LINK_SIZE;
389     break;
390    
391     /* Anything else does not or need not match a character. We can get the
392 ph10 461 item's length from the table, but for those that can match zero occurrences
393     of a character, we must take special action for UTF-8 characters. */
394    
395 ph10 455 case OP_UPTO:
396 ph10 461 case OP_NOTUPTO:
397 ph10 455 case OP_MINUPTO:
398 ph10 461 case OP_NOTMINUPTO:
399 ph10 455 case OP_POSUPTO:
400     case OP_STAR:
401     case OP_MINSTAR:
402 ph10 461 case OP_NOTMINSTAR:
403 ph10 455 case OP_POSSTAR:
404 ph10 461 case OP_NOTPOSSTAR:
405 ph10 455 case OP_QUERY:
406     case OP_MINQUERY:
407     case OP_NOTMINQUERY:
408     case OP_POSQUERY:
409 ph10 461 case OP_NOTPOSQUERY:
410 ph10 455 cc += _pcre_OP_lengths[op];
411 ph10 461 #ifdef SUPPORT_UTF8
412 ph10 455 if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
413 ph10 461 #endif
414 ph10 455 break;
415 ph10 512
416 ph10 510 /* Skip these, but we need to add in the name length. */
417 ph10 512
418 ph10 510 case OP_MARK:
419     case OP_PRUNE_ARG:
420     case OP_SKIP_ARG:
421     case OP_THEN_ARG:
422     cc += _pcre_OP_lengths[op] + cc[1];
423 ph10 512 break;
424 ph10 455
425     /* For the record, these are the opcodes that are matched by "default":
426     OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,
427     OP_THEN. */
428 ph10 461
429 ph10 455 default:
430     cc += _pcre_OP_lengths[op];
431     break;
432     }
433     }
434     /* Control never gets here */
435     }
436    
437    
438    
439     /*************************************************
440 nigel 77 * Set a bit and maybe its alternate case *
441     *************************************************/
442    
443 ph10 520 /* Given a character, set its first byte's bit in the table, and also the
444     corresponding bit for the other version of a letter if we are caseless. In
445     UTF-8 mode, for characters greater than 127, we can only do the caseless thing
446     when Unicode property support is available.
447 nigel 77
448     Arguments:
449     start_bits points to the bit map
450 ph10 520 p points to the character
451 nigel 77 caseless the caseless flag
452     cd the block with char table pointers
453 ph10 520 utf8 TRUE for UTF-8 mode
454 nigel 77
455 ph10 520 Returns: pointer after the character
456 nigel 77 */
457    
458 ph10 520 static const uschar *
459     set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,
460     compile_data *cd, BOOL utf8)
461 nigel 77 {
462 ph10 520 unsigned int c = *p;
463 nigel 77 start_bits[c/8] |= (1 << (c&7));
464 ph10 520
465     #ifdef SUPPORT_UTF8
466     if (utf8 && c > 127)
467     {
468     GETCHARINC(c, p);
469     #ifdef SUPPORT_UCP
470     if (caseless)
471     {
472     uschar buff[8];
473     c = UCD_OTHERCASE(c);
474     (void)_pcre_ord2utf8(c, buff);
475     c = buff[0];
476     start_bits[c/8] |= (1 << (c&7));
477     }
478     #endif
479     return p;
480     }
481     #endif
482    
483     /* Not UTF-8 mode, or character is less than 127. */
484    
485 nigel 77 if (caseless && (cd->ctypes[c] & ctype_letter) != 0)
486     start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));
487 ph10 520 return p + 1;
488 nigel 77 }
489    
490    
491    
492     /*************************************************
493 nigel 93 * Create bitmap of starting bytes *
494 nigel 77 *************************************************/
495    
496 nigel 93 /* This function scans a compiled unanchored expression recursively and
497     attempts to build a bitmap of the set of possible starting bytes. As time goes
498     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 nigel 77
504     Arguments:
505     code points to an expression
506     start_bits points to a 32-byte table, initialized to 0
507     caseless the current state of the caseless flag
508     utf8 TRUE if in UTF-8 mode
509     cd the block with char table pointers
510    
511 nigel 93 Returns: SSB_FAIL => Failed to find any starting bytes
512     SSB_DONE => Found mandatory starting bytes
513     SSB_CONTINUE => Found optional starting bytes
514 nigel 77 */
515    
516 nigel 93 static int
517 nigel 77 set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,
518     BOOL utf8, compile_data *cd)
519     {
520     register int c;
521 nigel 93 int yield = SSB_DONE;
522 nigel 77
523 nigel 91 #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 nigel 77 /* 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
532     code. Apparently IBM isn't going to fix the problem, and we would rather not
533     disable optimization (in this module it actually makes a big difference, and
534     the pcre module can use all the optimization it can get). */
535    
536     volatile int dummy;
537 nigel 91 /* ========================================================================= */
538     #endif
539 nigel 77
540     do
541     {
542 nigel 93 const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;
543 nigel 77 BOOL try_next = TRUE;
544    
545 nigel 93 while (try_next) /* Loop for items in this branch */
546 nigel 77 {
547 nigel 93 int rc;
548     switch(*tcode)
549 nigel 77 {
550 nigel 93 /* Fail if we reach something we don't understand */
551 nigel 77
552     default:
553 nigel 93 return SSB_FAIL;
554 nigel 77
555 nigel 93 /* 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     /* If we hit ALT or KET, it means we haven't found anything mandatory in
576     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;
586    
587     case OP_KET:
588     case OP_KETRMAX:
589     case OP_KETRMIN:
590     return SSB_CONTINUE;
591    
592 nigel 77 /* Skip over callout */
593    
594     case OP_CALLOUT:
595     tcode += 2 + 2*LINK_SIZE;
596     break;
597    
598     /* Skip over lookbehind and negative lookahead assertions */
599    
600     case OP_ASSERT_NOT:
601     case OP_ASSERTBACK:
602     case OP_ASSERTBACK_NOT:
603     do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
604 nigel 93 tcode += 1 + LINK_SIZE;
605 nigel 77 break;
606    
607     /* Skip over an option setting, changing the caseless flag */
608    
609     case OP_OPT:
610     caseless = (tcode[1] & PCRE_CASELESS) != 0;
611     tcode += 2;
612     break;
613    
614     /* BRAZERO does the bracket, but carries on. */
615    
616     case OP_BRAZERO:
617     case OP_BRAMINZERO:
618 nigel 93 if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)
619     return SSB_FAIL;
620 nigel 91 /* =========================================================================
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 nigel 77 dummy = 1;
624 nigel 91 ========================================================================= */
625 nigel 77 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
626 nigel 93 tcode += 1 + LINK_SIZE;
627 nigel 77 break;
628    
629 ph10 335 /* SKIPZERO skips the bracket. */
630    
631     case OP_SKIPZERO:
632 ph10 358 tcode++;
633 ph10 335 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
634     tcode += 1 + LINK_SIZE;
635     break;
636    
637 nigel 77 /* Single-char * or ? sets the bit and tries the next item */
638    
639     case OP_STAR:
640     case OP_MINSTAR:
641 nigel 93 case OP_POSSTAR:
642 nigel 77 case OP_QUERY:
643     case OP_MINQUERY:
644 nigel 93 case OP_POSQUERY:
645 ph10 520 tcode = set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);
646 nigel 77 break;
647    
648     /* Single-char upto sets the bit and tries the next */
649    
650     case OP_UPTO:
651     case OP_MINUPTO:
652 nigel 93 case OP_POSUPTO:
653 ph10 520 tcode = set_table_bit(start_bits, tcode + 3, caseless, cd, utf8);
654 nigel 77 break;
655    
656     /* At least one single char sets the bit and stops */
657    
658     case OP_EXACT: /* Fall through */
659     tcode += 2;
660    
661     case OP_CHAR:
662     case OP_CHARNC:
663     case OP_PLUS:
664     case OP_MINPLUS:
665 nigel 93 case OP_POSPLUS:
666 ph10 520 (void)set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);
667 nigel 77 try_next = FALSE;
668     break;
669    
670 ph10 520 /* Single character types set the bits and stop. Note that if PCRE_UCP
671     is set, we do not see these op codes because \d etc are converted to
672     properties. Therefore, these apply in the case when only ASCII characters
673     are recognized to match the types. */
674 nigel 77
675     case OP_NOT_DIGIT:
676     for (c = 0; c < 32; c++)
677     start_bits[c] |= ~cd->cbits[c+cbit_digit];
678     try_next = FALSE;
679     break;
680    
681     case OP_DIGIT:
682     for (c = 0; c < 32; c++)
683     start_bits[c] |= cd->cbits[c+cbit_digit];
684     try_next = FALSE;
685     break;
686    
687 nigel 91 /* The cbit_space table has vertical tab as whitespace; we have to
688     discard it. */
689    
690 nigel 77 case OP_NOT_WHITESPACE:
691     for (c = 0; c < 32; c++)
692 nigel 91 {
693     int d = cd->cbits[c+cbit_space];
694     if (c == 1) d &= ~0x08;
695     start_bits[c] |= ~d;
696     }
697 nigel 77 try_next = FALSE;
698     break;
699    
700 nigel 91 /* The cbit_space table has vertical tab as whitespace; we have to
701     discard it. */
702    
703 nigel 77 case OP_WHITESPACE:
704     for (c = 0; c < 32; c++)
705 nigel 91 {
706     int d = cd->cbits[c+cbit_space];
707     if (c == 1) d &= ~0x08;
708     start_bits[c] |= d;
709     }
710 nigel 77 try_next = FALSE;
711     break;
712    
713     case OP_NOT_WORDCHAR:
714     for (c = 0; c < 32; c++)
715     start_bits[c] |= ~cd->cbits[c+cbit_word];
716     try_next = FALSE;
717     break;
718    
719     case OP_WORDCHAR:
720     for (c = 0; c < 32; c++)
721     start_bits[c] |= cd->cbits[c+cbit_word];
722     try_next = FALSE;
723     break;
724    
725     /* One or more character type fudges the pointer and restarts, knowing
726     it will hit a single character type and stop there. */
727    
728     case OP_TYPEPLUS:
729     case OP_TYPEMINPLUS:
730     tcode++;
731     break;
732    
733     case OP_TYPEEXACT:
734     tcode += 3;
735     break;
736    
737     /* Zero or more repeats of character types set the bits and then
738     try again. */
739    
740     case OP_TYPEUPTO:
741     case OP_TYPEMINUPTO:
742 nigel 93 case OP_TYPEPOSUPTO:
743 nigel 77 tcode += 2; /* Fall through */
744    
745     case OP_TYPESTAR:
746     case OP_TYPEMINSTAR:
747 nigel 93 case OP_TYPEPOSSTAR:
748 nigel 77 case OP_TYPEQUERY:
749     case OP_TYPEMINQUERY:
750 nigel 93 case OP_TYPEPOSQUERY:
751 nigel 77 switch(tcode[1])
752     {
753     case OP_ANY:
754 ph10 345 case OP_ALLANY:
755 nigel 93 return SSB_FAIL;
756 nigel 77
757     case OP_NOT_DIGIT:
758     for (c = 0; c < 32; c++)
759     start_bits[c] |= ~cd->cbits[c+cbit_digit];
760     break;
761    
762     case OP_DIGIT:
763     for (c = 0; c < 32; c++)
764     start_bits[c] |= cd->cbits[c+cbit_digit];
765     break;
766    
767 nigel 91 /* The cbit_space table has vertical tab as whitespace; we have to
768     discard it. */
769    
770 nigel 77 case OP_NOT_WHITESPACE:
771     for (c = 0; c < 32; c++)
772 nigel 91 {
773     int d = cd->cbits[c+cbit_space];
774     if (c == 1) d &= ~0x08;
775     start_bits[c] |= ~d;
776     }
777 nigel 77 break;
778    
779 nigel 91 /* The cbit_space table has vertical tab as whitespace; we have to
780     discard it. */
781    
782 nigel 77 case OP_WHITESPACE:
783     for (c = 0; c < 32; c++)
784 nigel 91 {
785     int d = cd->cbits[c+cbit_space];
786     if (c == 1) d &= ~0x08;
787     start_bits[c] |= d;
788     }
789 nigel 77 break;
790    
791     case OP_NOT_WORDCHAR:
792     for (c = 0; c < 32; c++)
793     start_bits[c] |= ~cd->cbits[c+cbit_word];
794     break;
795    
796     case OP_WORDCHAR:
797     for (c = 0; c < 32; c++)
798     start_bits[c] |= cd->cbits[c+cbit_word];
799     break;
800     }
801    
802     tcode += 2;
803     break;
804    
805     /* Character class where all the information is in a bit map: set the
806     bits and either carry on or not, according to the repeat count. If it was
807     a negative class, and we are operating with UTF-8 characters, any byte
808     with a value >= 0xc4 is a potentially valid starter because it starts a
809     character with a value > 255. */
810    
811     case OP_NCLASS:
812 ph10 111 #ifdef SUPPORT_UTF8
813 nigel 77 if (utf8)
814     {
815     start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
816     memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
817     }
818 ph10 111 #endif
819 nigel 77 /* Fall through */
820    
821     case OP_CLASS:
822     {
823     tcode++;
824    
825     /* In UTF-8 mode, the bits in a bit map correspond to character
826     values, not to byte values. However, the bit map we are constructing is
827     for byte values. So we have to do a conversion for characters whose
828     value is > 127. In fact, there are only two possible starting bytes for
829     characters in the range 128 - 255. */
830    
831 ph10 107 #ifdef SUPPORT_UTF8
832 nigel 77 if (utf8)
833     {
834     for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];
835     for (c = 128; c < 256; c++)
836     {
837     if ((tcode[c/8] && (1 << (c&7))) != 0)
838     {
839     int d = (c >> 6) | 0xc0; /* Set bit for this starter */
840     start_bits[d/8] |= (1 << (d&7)); /* and then skip on to the */
841     c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */
842     }
843     }
844     }
845    
846     /* In non-UTF-8 mode, the two bit maps are completely compatible. */
847    
848     else
849 ph10 111 #endif
850 nigel 77 {
851     for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
852     }
853    
854     /* Advance past the bit map, and act on what follows */
855    
856     tcode += 32;
857     switch (*tcode)
858     {
859     case OP_CRSTAR:
860     case OP_CRMINSTAR:
861     case OP_CRQUERY:
862     case OP_CRMINQUERY:
863     tcode++;
864     break;
865    
866     case OP_CRRANGE:
867     case OP_CRMINRANGE:
868     if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;
869     else try_next = FALSE;
870     break;
871    
872     default:
873     try_next = FALSE;
874     break;
875     }
876     }
877     break; /* End of bitmap class handling */
878    
879     } /* End of switch */
880     } /* End of try_next loop */
881    
882     code += GET(code, 1); /* Advance to next branch */
883     }
884     while (*code == OP_ALT);
885 nigel 93 return yield;
886 nigel 77 }
887    
888    
889    
890     /*************************************************
891     * Study a compiled expression *
892     *************************************************/
893    
894     /* This function is handed a compiled expression that it must study to produce
895     information that will speed up the matching. It returns a pcre_extra block
896     which then gets handed back to pcre_exec().
897    
898     Arguments:
899     re points to the compiled expression
900     options contains option bits
901     errorptr points to where to place error messages;
902     set NULL unless error
903    
904     Returns: pointer to a pcre_extra block, with study_data filled in and the
905 ph10 455 appropriate flags set;
906 nigel 77 NULL on error or if no optimization possible
907     */
908    
909 ph10 359 PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
910 nigel 77 pcre_study(const pcre *external_re, int options, const char **errorptr)
911     {
912 ph10 455 int min;
913     BOOL bits_set = FALSE;
914 nigel 77 uschar start_bits[32];
915     pcre_extra *extra;
916     pcre_study_data *study;
917     const uschar *tables;
918 nigel 91 uschar *code;
919     compile_data compile_block;
920 nigel 77 const real_pcre *re = (const real_pcre *)external_re;
921    
922     *errorptr = NULL;
923    
924     if (re == NULL || re->magic_number != MAGIC_NUMBER)
925     {
926     *errorptr = "argument is not a compiled regular expression";
927     return NULL;
928     }
929    
930     if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
931     {
932     *errorptr = "unknown or incorrect option bit(s) set";
933     return NULL;
934     }
935    
936 nigel 91 code = (uschar *)re + re->name_table_offset +
937     (re->name_count * re->name_entry_size);
938    
939 nigel 77 /* For an anchored pattern, or an unanchored pattern that has a first char, or
940 ph10 461 a multiline pattern that matches only at "line starts", there is no point in
941 ph10 455 seeking a list of starting bytes. */
942 nigel 77
943 ph10 455 if ((re->options & PCRE_ANCHORED) == 0 &&
944     (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
945     {
946     /* Set the character tables in the block that is passed around */
947 ph10 461
948 ph10 455 tables = re->tables;
949     if (tables == NULL)
950     (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
951     (void *)(&tables));
952 ph10 461
953 ph10 455 compile_block.lcc = tables + lcc_offset;
954     compile_block.fcc = tables + fcc_offset;
955     compile_block.cbits = tables + cbits_offset;
956     compile_block.ctypes = tables + ctypes_offset;
957 ph10 461
958 ph10 455 /* See if we can find a fixed set of initial characters for the pattern. */
959 ph10 461
960 ph10 455 memset(start_bits, 0, 32 * sizeof(uschar));
961 ph10 461 bits_set = set_start_bits(code, start_bits,
962     (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,
963 ph10 455 &compile_block) == SSB_DONE;
964     }
965 ph10 461
966 ph10 455 /* Find the minimum length of subject string. */
967 nigel 77
968 ph10 455 min = find_minlength(code, code, re->options);
969 nigel 77
970 ph10 455 /* Return NULL if no optimization is possible. */
971 nigel 77
972 ph10 455 if (!bits_set && min < 0) return NULL;
973 nigel 77
974     /* Get a pcre_extra block and a pcre_study_data block. The study data is put in
975     the latter, which is pointed to by the former, which may also get additional
976     data set later by the calling program. At the moment, the size of
977     pcre_study_data is fixed. We nevertheless save it in a field for returning via
978     the pcre_fullinfo() function so that if it becomes variable in the future, we
979     don't have to change that code. */
980    
981     extra = (pcre_extra *)(pcre_malloc)
982     (sizeof(pcre_extra) + sizeof(pcre_study_data));
983    
984     if (extra == NULL)
985     {
986     *errorptr = "failed to get memory";
987     return NULL;
988     }
989    
990     study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
991     extra->flags = PCRE_EXTRA_STUDY_DATA;
992     extra->study_data = study;
993    
994     study->size = sizeof(pcre_study_data);
995 ph10 455 study->flags = 0;
996 nigel 77
997 ph10 455 if (bits_set)
998     {
999     study->flags |= PCRE_STUDY_MAPPED;
1000     memcpy(study->start_bits, start_bits, sizeof(start_bits));
1001     }
1002 ph10 461
1003 ph10 455 if (min >= 0)
1004     {
1005     study->flags |= PCRE_STUDY_MINLEN;
1006     study->minlength = min;
1007 ph10 461 }
1008 ph10 455
1009 nigel 77 return extra;
1010     }
1011    
1012     /* 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