/[pcre]/code/tags/pcre-8.02/pcre_study.c
ViewVC logotype

Contents of /code/tags/pcre-8.02/pcre_study.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 461 - (show annotations) (download)
Mon Oct 5 10:59:35 2009 UTC (4 years, 11 months ago) by ph10
Original Path: code/trunk/pcre_study.c
File MIME type: text/plain
File size: 26735 byte(s)
Tidy up, remove trailing spaces, etc. for 8.00-RC1.

1 /*************************************************
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 Copyright (c) 1997-2009 University of Cambridge
10
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 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include "pcre_internal.h"
50
51
52 /* Returns from set_start_bits() */
53
54 enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE };
55
56
57
58 /*************************************************
59 * Find the minimum subject length for a group *
60 *************************************************/
61
62 /* Scan a parenthesized group and compute the minimum length of subject that
63 is needed to match it. This is a lower bound; it does not mean there is a
64 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 options the compiling options
71
72 Returns: the minimum length
73 -1 if \C was encountered
74 -2 internal error (missing capturing bracket)
75 */
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 uschar *cs, *ce;
95 register int op = *cc;
96
97 switch (op)
98 {
99 case OP_CBRA:
100 case OP_SCBRA:
101 case OP_BRA:
102 case OP_SBRA:
103 case OP_ONCE:
104 case OP_COND:
105 case OP_SCOND:
106 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 if (length < 0 || (!had_recurse && branchlength < length))
123 length = branchlength;
124 if (*cc != OP_ALT) return length;
125 cc += 1 + LINK_SIZE;
126 branchlength = 0;
127 had_recurse = FALSE;
128 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 case OP_NCREF:
144 case OP_RREF:
145 case OP_NRREF:
146 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
160 /* Skip over a subpattern that has a {0} or {0,x} quantifier */
161
162 case OP_BRAZERO:
163 case OP_BRAMINZERO:
164 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
188 case OP_TYPEPLUS:
189 case OP_TYPEMINPLUS:
190 case OP_TYPEPOSPLUS:
191 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 case OP_NOTEXACT:
200 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 case OP_HSPACE:
229 case OP_NOT_HSPACE:
230 case OP_VSPACE:
231 case OP_NOT_VSPACE:
232 branchlength++;
233 cc++;
234 break;
235
236 /* "Any newline" might match two characters */
237
238 case OP_ANYNL:
239 branchlength += 2;
240 cc++;
241 break;
242
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 break;
252
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 /* Fall through */
291
292 case OP_CRSTAR:
293 case OP_CRMINSTAR:
294 case OP_CRQUERY:
295 case OP_CRMINQUERY:
296 cc++;
297 break;
298
299 case OP_CRRANGE:
300 case OP_CRMINRANGE:
301 branchlength += GET2(cc,1);
302 cc += 5;
303 break;
304
305 default:
306 branchlength++;
307 break;
308 }
309 break;
310
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 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 referencing behaves in the same way. */
318
319 case OP_REF:
320 ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
321 if (cs == NULL) return -2;
322 do ce += GET(ce, 1); while (*ce == OP_ALT);
323 if (cc > cs && cc < ce)
324 {
325 d = 0;
326 had_recurse = TRUE;
327 }
328 else d = find_minlength(cs, startcode, options);
329 cc += 3;
330
331 /* Handle repeated back references */
332
333 switch (*cc)
334 {
335 case OP_CRSTAR:
336 case OP_CRMINSTAR:
337 case OP_CRQUERY:
338 case OP_CRMINQUERY:
339 min = 0;
340 cc++;
341 break;
342
343 case OP_CRRANGE:
344 case OP_CRMINRANGE:
345 min = GET2(cc, 1);
346 cc += 5;
347 break;
348
349 default:
350 min = 1;
351 break;
352 }
353
354 branchlength += min * d;
355 break;
356
357 case OP_RECURSE:
358 cs = ce = (uschar *)startcode + GET(cc, 1);
359 if (cs == NULL) return -2;
360 do ce += GET(ce, 1); while (*ce == OP_ALT);
361 if (cc > cs && cc < ce)
362 had_recurse = TRUE;
363 else
364 branchlength += find_minlength(cs, startcode, options);
365 cc += 1 + LINK_SIZE;
366 break;
367
368 /* Anything else does not or need not match a character. We can get the
369 item's length from the table, but for those that can match zero occurrences
370 of a character, we must take special action for UTF-8 characters. */
371
372 case OP_UPTO:
373 case OP_NOTUPTO:
374 case OP_MINUPTO:
375 case OP_NOTMINUPTO:
376 case OP_POSUPTO:
377 case OP_STAR:
378 case OP_MINSTAR:
379 case OP_NOTMINSTAR:
380 case OP_POSSTAR:
381 case OP_NOTPOSSTAR:
382 case OP_QUERY:
383 case OP_MINQUERY:
384 case OP_NOTMINQUERY:
385 case OP_POSQUERY:
386 case OP_NOTPOSQUERY:
387 cc += _pcre_OP_lengths[op];
388 #ifdef SUPPORT_UTF8
389 if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
390 #endif
391 break;
392
393 /* For the record, these are the opcodes that are matched by "default":
394 OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,
395 OP_THEN. */
396
397 default:
398 cc += _pcre_OP_lengths[op];
399 break;
400 }
401 }
402 /* Control never gets here */
403 }
404
405
406
407 /*************************************************
408 * Set a bit and maybe its alternate case *
409 *************************************************/
410
411 /* Given a character, set its bit in the table, and also the bit for the other
412 version of a letter if we are caseless.
413
414 Arguments:
415 start_bits points to the bit map
416 c is the character
417 caseless the caseless flag
418 cd the block with char table pointers
419
420 Returns: nothing
421 */
422
423 static void
424 set_bit(uschar *start_bits, unsigned int c, BOOL caseless, compile_data *cd)
425 {
426 start_bits[c/8] |= (1 << (c&7));
427 if (caseless && (cd->ctypes[c] & ctype_letter) != 0)
428 start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));
429 }
430
431
432
433 /*************************************************
434 * Create bitmap of starting bytes *
435 *************************************************/
436
437 /* This function scans a compiled unanchored expression recursively and
438 attempts to build a bitmap of the set of possible starting bytes. As time goes
439 by, we may be able to get more clever at doing this. The SSB_CONTINUE return is
440 useful for parenthesized groups in patterns such as (a*)b where the group
441 provides some optional starting bytes but scanning must continue at the outer
442 level to find at least one mandatory byte. At the outermost level, this
443 function fails unless the result is SSB_DONE.
444
445 Arguments:
446 code points to an expression
447 start_bits points to a 32-byte table, initialized to 0
448 caseless the current state of the caseless flag
449 utf8 TRUE if in UTF-8 mode
450 cd the block with char table pointers
451
452 Returns: SSB_FAIL => Failed to find any starting bytes
453 SSB_DONE => Found mandatory starting bytes
454 SSB_CONTINUE => Found optional starting bytes
455 */
456
457 static int
458 set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,
459 BOOL utf8, compile_data *cd)
460 {
461 register int c;
462 int yield = SSB_DONE;
463
464 #if 0
465 /* ========================================================================= */
466 /* The following comment and code was inserted in January 1999. In May 2006,
467 when it was observed to cause compiler warnings about unused values, I took it
468 out again. If anybody is still using OS/2, they will have to put it back
469 manually. */
470
471 /* This next statement and the later reference to dummy are here in order to
472 trick the optimizer of the IBM C compiler for OS/2 into generating correct
473 code. Apparently IBM isn't going to fix the problem, and we would rather not
474 disable optimization (in this module it actually makes a big difference, and
475 the pcre module can use all the optimization it can get). */
476
477 volatile int dummy;
478 /* ========================================================================= */
479 #endif
480
481 do
482 {
483 const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;
484 BOOL try_next = TRUE;
485
486 while (try_next) /* Loop for items in this branch */
487 {
488 int rc;
489 switch(*tcode)
490 {
491 /* Fail if we reach something we don't understand */
492
493 default:
494 return SSB_FAIL;
495
496 /* If we hit a bracket or a positive lookahead assertion, recurse to set
497 bits from within the subpattern. If it can't find anything, we have to
498 give up. If it finds some mandatory character(s), we are done for this
499 branch. Otherwise, carry on scanning after the subpattern. */
500
501 case OP_BRA:
502 case OP_SBRA:
503 case OP_CBRA:
504 case OP_SCBRA:
505 case OP_ONCE:
506 case OP_ASSERT:
507 rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);
508 if (rc == SSB_FAIL) return SSB_FAIL;
509 if (rc == SSB_DONE) try_next = FALSE; else
510 {
511 do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
512 tcode += 1 + LINK_SIZE;
513 }
514 break;
515
516 /* If we hit ALT or KET, it means we haven't found anything mandatory in
517 this branch, though we might have found something optional. For ALT, we
518 continue with the next alternative, but we have to arrange that the final
519 result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
520 return SSB_CONTINUE: if this is the top level, that indicates failure,
521 but after a nested subpattern, it causes scanning to continue. */
522
523 case OP_ALT:
524 yield = SSB_CONTINUE;
525 try_next = FALSE;
526 break;
527
528 case OP_KET:
529 case OP_KETRMAX:
530 case OP_KETRMIN:
531 return SSB_CONTINUE;
532
533 /* Skip over callout */
534
535 case OP_CALLOUT:
536 tcode += 2 + 2*LINK_SIZE;
537 break;
538
539 /* Skip over lookbehind and negative lookahead assertions */
540
541 case OP_ASSERT_NOT:
542 case OP_ASSERTBACK:
543 case OP_ASSERTBACK_NOT:
544 do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
545 tcode += 1 + LINK_SIZE;
546 break;
547
548 /* Skip over an option setting, changing the caseless flag */
549
550 case OP_OPT:
551 caseless = (tcode[1] & PCRE_CASELESS) != 0;
552 tcode += 2;
553 break;
554
555 /* BRAZERO does the bracket, but carries on. */
556
557 case OP_BRAZERO:
558 case OP_BRAMINZERO:
559 if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)
560 return SSB_FAIL;
561 /* =========================================================================
562 See the comment at the head of this function concerning the next line,
563 which was an old fudge for the benefit of OS/2.
564 dummy = 1;
565 ========================================================================= */
566 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
567 tcode += 1 + LINK_SIZE;
568 break;
569
570 /* SKIPZERO skips the bracket. */
571
572 case OP_SKIPZERO:
573 tcode++;
574 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
575 tcode += 1 + LINK_SIZE;
576 break;
577
578 /* Single-char * or ? sets the bit and tries the next item */
579
580 case OP_STAR:
581 case OP_MINSTAR:
582 case OP_POSSTAR:
583 case OP_QUERY:
584 case OP_MINQUERY:
585 case OP_POSQUERY:
586 set_bit(start_bits, tcode[1], caseless, cd);
587 tcode += 2;
588 #ifdef SUPPORT_UTF8
589 if (utf8 && tcode[-1] >= 0xc0)
590 tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];
591 #endif
592 break;
593
594 /* Single-char upto sets the bit and tries the next */
595
596 case OP_UPTO:
597 case OP_MINUPTO:
598 case OP_POSUPTO:
599 set_bit(start_bits, tcode[3], caseless, cd);
600 tcode += 4;
601 #ifdef SUPPORT_UTF8
602 if (utf8 && tcode[-1] >= 0xc0)
603 tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];
604 #endif
605 break;
606
607 /* At least one single char sets the bit and stops */
608
609 case OP_EXACT: /* Fall through */
610 tcode += 2;
611
612 case OP_CHAR:
613 case OP_CHARNC:
614 case OP_PLUS:
615 case OP_MINPLUS:
616 case OP_POSPLUS:
617 set_bit(start_bits, tcode[1], caseless, cd);
618 try_next = FALSE;
619 break;
620
621 /* Single character type sets the bits and stops */
622
623 case OP_NOT_DIGIT:
624 for (c = 0; c < 32; c++)
625 start_bits[c] |= ~cd->cbits[c+cbit_digit];
626 try_next = FALSE;
627 break;
628
629 case OP_DIGIT:
630 for (c = 0; c < 32; c++)
631 start_bits[c] |= cd->cbits[c+cbit_digit];
632 try_next = FALSE;
633 break;
634
635 /* The cbit_space table has vertical tab as whitespace; we have to
636 discard it. */
637
638 case OP_NOT_WHITESPACE:
639 for (c = 0; c < 32; c++)
640 {
641 int d = cd->cbits[c+cbit_space];
642 if (c == 1) d &= ~0x08;
643 start_bits[c] |= ~d;
644 }
645 try_next = FALSE;
646 break;
647
648 /* The cbit_space table has vertical tab as whitespace; we have to
649 discard it. */
650
651 case OP_WHITESPACE:
652 for (c = 0; c < 32; c++)
653 {
654 int d = cd->cbits[c+cbit_space];
655 if (c == 1) d &= ~0x08;
656 start_bits[c] |= d;
657 }
658 try_next = FALSE;
659 break;
660
661 case OP_NOT_WORDCHAR:
662 for (c = 0; c < 32; c++)
663 start_bits[c] |= ~cd->cbits[c+cbit_word];
664 try_next = FALSE;
665 break;
666
667 case OP_WORDCHAR:
668 for (c = 0; c < 32; c++)
669 start_bits[c] |= cd->cbits[c+cbit_word];
670 try_next = FALSE;
671 break;
672
673 /* One or more character type fudges the pointer and restarts, knowing
674 it will hit a single character type and stop there. */
675
676 case OP_TYPEPLUS:
677 case OP_TYPEMINPLUS:
678 tcode++;
679 break;
680
681 case OP_TYPEEXACT:
682 tcode += 3;
683 break;
684
685 /* Zero or more repeats of character types set the bits and then
686 try again. */
687
688 case OP_TYPEUPTO:
689 case OP_TYPEMINUPTO:
690 case OP_TYPEPOSUPTO:
691 tcode += 2; /* Fall through */
692
693 case OP_TYPESTAR:
694 case OP_TYPEMINSTAR:
695 case OP_TYPEPOSSTAR:
696 case OP_TYPEQUERY:
697 case OP_TYPEMINQUERY:
698 case OP_TYPEPOSQUERY:
699 switch(tcode[1])
700 {
701 case OP_ANY:
702 case OP_ALLANY:
703 return SSB_FAIL;
704
705 case OP_NOT_DIGIT:
706 for (c = 0; c < 32; c++)
707 start_bits[c] |= ~cd->cbits[c+cbit_digit];
708 break;
709
710 case OP_DIGIT:
711 for (c = 0; c < 32; c++)
712 start_bits[c] |= cd->cbits[c+cbit_digit];
713 break;
714
715 /* The cbit_space table has vertical tab as whitespace; we have to
716 discard it. */
717
718 case OP_NOT_WHITESPACE:
719 for (c = 0; c < 32; c++)
720 {
721 int d = cd->cbits[c+cbit_space];
722 if (c == 1) d &= ~0x08;
723 start_bits[c] |= ~d;
724 }
725 break;
726
727 /* The cbit_space table has vertical tab as whitespace; we have to
728 discard it. */
729
730 case OP_WHITESPACE:
731 for (c = 0; c < 32; c++)
732 {
733 int d = cd->cbits[c+cbit_space];
734 if (c == 1) d &= ~0x08;
735 start_bits[c] |= d;
736 }
737 break;
738
739 case OP_NOT_WORDCHAR:
740 for (c = 0; c < 32; c++)
741 start_bits[c] |= ~cd->cbits[c+cbit_word];
742 break;
743
744 case OP_WORDCHAR:
745 for (c = 0; c < 32; c++)
746 start_bits[c] |= cd->cbits[c+cbit_word];
747 break;
748 }
749
750 tcode += 2;
751 break;
752
753 /* Character class where all the information is in a bit map: set the
754 bits and either carry on or not, according to the repeat count. If it was
755 a negative class, and we are operating with UTF-8 characters, any byte
756 with a value >= 0xc4 is a potentially valid starter because it starts a
757 character with a value > 255. */
758
759 case OP_NCLASS:
760 #ifdef SUPPORT_UTF8
761 if (utf8)
762 {
763 start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
764 memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
765 }
766 #endif
767 /* Fall through */
768
769 case OP_CLASS:
770 {
771 tcode++;
772
773 /* In UTF-8 mode, the bits in a bit map correspond to character
774 values, not to byte values. However, the bit map we are constructing is
775 for byte values. So we have to do a conversion for characters whose
776 value is > 127. In fact, there are only two possible starting bytes for
777 characters in the range 128 - 255. */
778
779 #ifdef SUPPORT_UTF8
780 if (utf8)
781 {
782 for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];
783 for (c = 128; c < 256; c++)
784 {
785 if ((tcode[c/8] && (1 << (c&7))) != 0)
786 {
787 int d = (c >> 6) | 0xc0; /* Set bit for this starter */
788 start_bits[d/8] |= (1 << (d&7)); /* and then skip on to the */
789 c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */
790 }
791 }
792 }
793
794 /* In non-UTF-8 mode, the two bit maps are completely compatible. */
795
796 else
797 #endif
798 {
799 for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
800 }
801
802 /* Advance past the bit map, and act on what follows */
803
804 tcode += 32;
805 switch (*tcode)
806 {
807 case OP_CRSTAR:
808 case OP_CRMINSTAR:
809 case OP_CRQUERY:
810 case OP_CRMINQUERY:
811 tcode++;
812 break;
813
814 case OP_CRRANGE:
815 case OP_CRMINRANGE:
816 if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;
817 else try_next = FALSE;
818 break;
819
820 default:
821 try_next = FALSE;
822 break;
823 }
824 }
825 break; /* End of bitmap class handling */
826
827 } /* End of switch */
828 } /* End of try_next loop */
829
830 code += GET(code, 1); /* Advance to next branch */
831 }
832 while (*code == OP_ALT);
833 return yield;
834 }
835
836
837
838 /*************************************************
839 * Study a compiled expression *
840 *************************************************/
841
842 /* This function is handed a compiled expression that it must study to produce
843 information that will speed up the matching. It returns a pcre_extra block
844 which then gets handed back to pcre_exec().
845
846 Arguments:
847 re points to the compiled expression
848 options contains option bits
849 errorptr points to where to place error messages;
850 set NULL unless error
851
852 Returns: pointer to a pcre_extra block, with study_data filled in and the
853 appropriate flags set;
854 NULL on error or if no optimization possible
855 */
856
857 PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
858 pcre_study(const pcre *external_re, int options, const char **errorptr)
859 {
860 int min;
861 BOOL bits_set = FALSE;
862 uschar start_bits[32];
863 pcre_extra *extra;
864 pcre_study_data *study;
865 const uschar *tables;
866 uschar *code;
867 compile_data compile_block;
868 const real_pcre *re = (const real_pcre *)external_re;
869
870 *errorptr = NULL;
871
872 if (re == NULL || re->magic_number != MAGIC_NUMBER)
873 {
874 *errorptr = "argument is not a compiled regular expression";
875 return NULL;
876 }
877
878 if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
879 {
880 *errorptr = "unknown or incorrect option bit(s) set";
881 return NULL;
882 }
883
884 code = (uschar *)re + re->name_table_offset +
885 (re->name_count * re->name_entry_size);
886
887 /* For an anchored pattern, or an unanchored pattern that has a first char, or
888 a multiline pattern that matches only at "line starts", there is no point in
889 seeking a list of starting bytes. */
890
891 if ((re->options & PCRE_ANCHORED) == 0 &&
892 (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
893 {
894 /* Set the character tables in the block that is passed around */
895
896 tables = re->tables;
897 if (tables == NULL)
898 (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
899 (void *)(&tables));
900
901 compile_block.lcc = tables + lcc_offset;
902 compile_block.fcc = tables + fcc_offset;
903 compile_block.cbits = tables + cbits_offset;
904 compile_block.ctypes = tables + ctypes_offset;
905
906 /* See if we can find a fixed set of initial characters for the pattern. */
907
908 memset(start_bits, 0, 32 * sizeof(uschar));
909 bits_set = set_start_bits(code, start_bits,
910 (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,
911 &compile_block) == SSB_DONE;
912 }
913
914 /* Find the minimum length of subject string. */
915
916 min = find_minlength(code, code, re->options);
917
918 /* Return NULL if no optimization is possible. */
919
920 if (!bits_set && min < 0) return NULL;
921
922 /* Get a pcre_extra block and a pcre_study_data block. The study data is put in
923 the latter, which is pointed to by the former, which may also get additional
924 data set later by the calling program. At the moment, the size of
925 pcre_study_data is fixed. We nevertheless save it in a field for returning via
926 the pcre_fullinfo() function so that if it becomes variable in the future, we
927 don't have to change that code. */
928
929 extra = (pcre_extra *)(pcre_malloc)
930 (sizeof(pcre_extra) + sizeof(pcre_study_data));
931
932 if (extra == NULL)
933 {
934 *errorptr = "failed to get memory";
935 return NULL;
936 }
937
938 study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
939 extra->flags = PCRE_EXTRA_STUDY_DATA;
940 extra->study_data = study;
941
942 study->size = sizeof(pcre_study_data);
943 study->flags = 0;
944
945 if (bits_set)
946 {
947 study->flags |= PCRE_STUDY_MAPPED;
948 memcpy(study->start_bits, start_bits, sizeof(start_bits));
949 }
950
951 if (min >= 0)
952 {
953 study->flags |= PCRE_STUDY_MINLEN;
954 study->minlength = min;
955 }
956
957 return extra;
958 }
959
960 /* 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