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

Contents of /code/trunk/pcre_study.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 730 - (show annotations) (download)
Mon Oct 10 16:07:02 2011 UTC (2 years, 9 months ago) by ph10
File MIME type: text/plain
File size: 38831 byte(s)
Remove redundant line of code.

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-2010 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 #define SET_BIT(c) start_bits[c/8] |= (1 << (c&7))
52
53 /* Returns from set_start_bits() */
54
55 enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN };
56
57
58
59 /*************************************************
60 * Find the minimum subject length for a group *
61 *************************************************/
62
63 /* Scan a parenthesized group and compute the minimum length of subject that
64 is needed to match it. This is a lower bound; it does not mean there is a
65 string of that length that matches. In UTF8 mode, the result is in characters
66 rather than bytes.
67
68 Arguments:
69 code pointer to start of group (the bracket)
70 startcode pointer to start of the whole pattern
71 options the compiling options
72 int RECURSE depth
73
74 Returns: the minimum length
75 -1 if \C in UTF-8 mode or (*ACCEPT) was encountered
76 -2 internal error (missing capturing bracket)
77 -3 internal error (opcode not listed)
78 */
79
80 static int
81 find_minlength(const uschar *code, const uschar *startcode, int options,
82 int recurse_depth)
83 {
84 int length = -1;
85 BOOL utf8 = (options & PCRE_UTF8) != 0;
86 BOOL had_recurse = FALSE;
87 register int branchlength = 0;
88 register uschar *cc = (uschar *)code + 1 + LINK_SIZE;
89
90 if (*code == OP_CBRA || *code == OP_SCBRA ||
91 *code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += 2;
92
93 /* Scan along the opcodes for this branch. If we get to the end of the
94 branch, check the length against that of the other branches. */
95
96 for (;;)
97 {
98 int d, min;
99 uschar *cs, *ce;
100 register int op = *cc;
101
102 switch (op)
103 {
104 case OP_COND:
105 case OP_SCOND:
106
107 /* If there is only one branch in a condition, the implied branch has zero
108 length, so we don't add anything. This covers the DEFINE "condition"
109 automatically. */
110
111 cs = cc + GET(cc, 1);
112 if (*cs != OP_ALT)
113 {
114 cc = cs + 1 + LINK_SIZE;
115 break;
116 }
117
118 /* Otherwise we can fall through and treat it the same as any other
119 subpattern. */
120
121 case OP_CBRA:
122 case OP_SCBRA:
123 case OP_BRA:
124 case OP_SBRA:
125 case OP_CBRAPOS:
126 case OP_SCBRAPOS:
127 case OP_BRAPOS:
128 case OP_SBRAPOS:
129 case OP_ONCE:
130 case OP_ONCE_NC:
131 d = find_minlength(cc, startcode, options, recurse_depth);
132 if (d < 0) return d;
133 branchlength += d;
134 do cc += GET(cc, 1); while (*cc == OP_ALT);
135 cc += 1 + LINK_SIZE;
136 break;
137
138 /* ACCEPT makes things far too complicated; we have to give up. */
139
140 case OP_ACCEPT:
141 case OP_ASSERT_ACCEPT:
142 return -1;
143
144 /* Reached end of a branch; if it's a ket it is the end of a nested
145 call. If it's ALT it is an alternation in a nested call. If it is END it's
146 the end of the outer call. All can be handled by the same code. If an
147 ACCEPT was previously encountered, use the length that was in force at that
148 time, and pass back the shortest ACCEPT length. */
149
150 case OP_ALT:
151 case OP_KET:
152 case OP_KETRMAX:
153 case OP_KETRMIN:
154 case OP_KETRPOS:
155 case OP_END:
156 if (length < 0 || (!had_recurse && branchlength < length))
157 length = branchlength;
158 if (op != OP_ALT) return length;
159 cc += 1 + LINK_SIZE;
160 branchlength = 0;
161 had_recurse = FALSE;
162 break;
163
164 /* Skip over assertive subpatterns */
165
166 case OP_ASSERT:
167 case OP_ASSERT_NOT:
168 case OP_ASSERTBACK:
169 case OP_ASSERTBACK_NOT:
170 do cc += GET(cc, 1); while (*cc == OP_ALT);
171 /* Fall through */
172
173 /* Skip over things that don't match chars */
174
175 case OP_REVERSE:
176 case OP_CREF:
177 case OP_NCREF:
178 case OP_RREF:
179 case OP_NRREF:
180 case OP_DEF:
181 case OP_CALLOUT:
182 case OP_SOD:
183 case OP_SOM:
184 case OP_EOD:
185 case OP_EODN:
186 case OP_CIRC:
187 case OP_CIRCM:
188 case OP_DOLL:
189 case OP_DOLLM:
190 case OP_NOT_WORD_BOUNDARY:
191 case OP_WORD_BOUNDARY:
192 cc += _pcre_OP_lengths[*cc];
193 break;
194
195 /* Skip over a subpattern that has a {0} or {0,x} quantifier */
196
197 case OP_BRAZERO:
198 case OP_BRAMINZERO:
199 case OP_BRAPOSZERO:
200 case OP_SKIPZERO:
201 cc += _pcre_OP_lengths[*cc];
202 do cc += GET(cc, 1); while (*cc == OP_ALT);
203 cc += 1 + LINK_SIZE;
204 break;
205
206 /* Handle literal characters and + repetitions */
207
208 case OP_CHAR:
209 case OP_CHARI:
210 case OP_NOT:
211 case OP_NOTI:
212 case OP_PLUS:
213 case OP_PLUSI:
214 case OP_MINPLUS:
215 case OP_MINPLUSI:
216 case OP_POSPLUS:
217 case OP_POSPLUSI:
218 case OP_NOTPLUS:
219 case OP_NOTPLUSI:
220 case OP_NOTMINPLUS:
221 case OP_NOTMINPLUSI:
222 case OP_NOTPOSPLUS:
223 case OP_NOTPOSPLUSI:
224 branchlength++;
225 cc += 2;
226 #ifdef SUPPORT_UTF8
227 if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
228 #endif
229 break;
230
231 case OP_TYPEPLUS:
232 case OP_TYPEMINPLUS:
233 case OP_TYPEPOSPLUS:
234 branchlength++;
235 cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
236 break;
237
238 /* Handle exact repetitions. The count is already in characters, but we
239 need to skip over a multibyte character in UTF8 mode. */
240
241 case OP_EXACT:
242 case OP_EXACTI:
243 case OP_NOTEXACT:
244 case OP_NOTEXACTI:
245 branchlength += GET2(cc,1);
246 cc += 4;
247 #ifdef SUPPORT_UTF8
248 if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
249 #endif
250 break;
251
252 case OP_TYPEEXACT:
253 branchlength += GET2(cc,1);
254 cc += (cc[3] == OP_PROP || cc[3] == OP_NOTPROP)? 6 : 4;
255 break;
256
257 /* Handle single-char non-literal matchers */
258
259 case OP_PROP:
260 case OP_NOTPROP:
261 cc += 2;
262 /* Fall through */
263
264 case OP_NOT_DIGIT:
265 case OP_DIGIT:
266 case OP_NOT_WHITESPACE:
267 case OP_WHITESPACE:
268 case OP_NOT_WORDCHAR:
269 case OP_WORDCHAR:
270 case OP_ANY:
271 case OP_ALLANY:
272 case OP_EXTUNI:
273 case OP_HSPACE:
274 case OP_NOT_HSPACE:
275 case OP_VSPACE:
276 case OP_NOT_VSPACE:
277 branchlength++;
278 cc++;
279 break;
280
281 /* "Any newline" might match two characters, but it also might match just
282 one. */
283
284 case OP_ANYNL:
285 branchlength += 1;
286 cc++;
287 break;
288
289 /* The single-byte matcher means we can't proceed in UTF-8 mode */
290
291 case OP_ANYBYTE:
292 #ifdef SUPPORT_UTF8
293 if (utf8) return -1;
294 #endif
295 branchlength++;
296 cc++;
297 break;
298
299 /* For repeated character types, we have to test for \p and \P, which have
300 an extra two bytes of parameters. */
301
302 case OP_TYPESTAR:
303 case OP_TYPEMINSTAR:
304 case OP_TYPEQUERY:
305 case OP_TYPEMINQUERY:
306 case OP_TYPEPOSSTAR:
307 case OP_TYPEPOSQUERY:
308 if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
309 cc += _pcre_OP_lengths[op];
310 break;
311
312 case OP_TYPEUPTO:
313 case OP_TYPEMINUPTO:
314 case OP_TYPEPOSUPTO:
315 if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;
316 cc += _pcre_OP_lengths[op];
317 break;
318
319 /* Check a class for variable quantification */
320
321 #ifdef SUPPORT_UTF8
322 case OP_XCLASS:
323 cc += GET(cc, 1) - 33;
324 /* Fall through */
325 #endif
326
327 case OP_CLASS:
328 case OP_NCLASS:
329 cc += 33;
330
331 switch (*cc)
332 {
333 case OP_CRPLUS:
334 case OP_CRMINPLUS:
335 branchlength++;
336 /* Fall through */
337
338 case OP_CRSTAR:
339 case OP_CRMINSTAR:
340 case OP_CRQUERY:
341 case OP_CRMINQUERY:
342 cc++;
343 break;
344
345 case OP_CRRANGE:
346 case OP_CRMINRANGE:
347 branchlength += GET2(cc,1);
348 cc += 5;
349 break;
350
351 default:
352 branchlength++;
353 break;
354 }
355 break;
356
357 /* Backreferences and subroutine calls are treated in the same way: we find
358 the minimum length for the subpattern. A recursion, however, causes an
359 a flag to be set that causes the length of this branch to be ignored. The
360 logic is that a recursion can only make sense if there is another
361 alternation that stops the recursing. That will provide the minimum length
362 (when no recursion happens). A backreference within the group that it is
363 referencing behaves in the same way.
364
365 If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
366 matches an empty string (by default it causes a matching failure), so in
367 that case we must set the minimum length to zero. */
368
369 case OP_REF:
370 case OP_REFI:
371 if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
372 {
373 ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
374 if (cs == NULL) return -2;
375 do ce += GET(ce, 1); while (*ce == OP_ALT);
376 if (cc > cs && cc < ce)
377 {
378 d = 0;
379 had_recurse = TRUE;
380 }
381 else
382 {
383 d = find_minlength(cs, startcode, options, recurse_depth);
384 }
385 }
386 else d = 0;
387 cc += 3;
388
389 /* Handle repeated back references */
390
391 switch (*cc)
392 {
393 case OP_CRSTAR:
394 case OP_CRMINSTAR:
395 case OP_CRQUERY:
396 case OP_CRMINQUERY:
397 min = 0;
398 cc++;
399 break;
400
401 case OP_CRPLUS:
402 case OP_CRMINPLUS:
403 min = 1;
404 cc++;
405 break;
406
407 case OP_CRRANGE:
408 case OP_CRMINRANGE:
409 min = GET2(cc, 1);
410 cc += 5;
411 break;
412
413 default:
414 min = 1;
415 break;
416 }
417
418 branchlength += min * d;
419 break;
420
421 /* We can easily detect direct recursion, but not mutual recursion. This is
422 caught by a recursion depth count. */
423
424 case OP_RECURSE:
425 cs = ce = (uschar *)startcode + GET(cc, 1);
426 do ce += GET(ce, 1); while (*ce == OP_ALT);
427 if ((cc > cs && cc < ce) || recurse_depth > 10)
428 had_recurse = TRUE;
429 else
430 {
431 branchlength += find_minlength(cs, startcode, options, recurse_depth + 1);
432 }
433 cc += 1 + LINK_SIZE;
434 break;
435
436 /* Anything else does not or need not match a character. We can get the
437 item's length from the table, but for those that can match zero occurrences
438 of a character, we must take special action for UTF-8 characters. As it
439 happens, the "NOT" versions of these opcodes are used at present only for
440 ASCII characters, so they could be omitted from this list. However, in
441 future that may change, so we include them here so as not to leave a
442 gotcha for a future maintainer. */
443
444 case OP_UPTO:
445 case OP_UPTOI:
446 case OP_NOTUPTO:
447 case OP_NOTUPTOI:
448 case OP_MINUPTO:
449 case OP_MINUPTOI:
450 case OP_NOTMINUPTO:
451 case OP_NOTMINUPTOI:
452 case OP_POSUPTO:
453 case OP_POSUPTOI:
454 case OP_NOTPOSUPTO:
455 case OP_NOTPOSUPTOI:
456
457 case OP_STAR:
458 case OP_STARI:
459 case OP_NOTSTAR:
460 case OP_NOTSTARI:
461 case OP_MINSTAR:
462 case OP_MINSTARI:
463 case OP_NOTMINSTAR:
464 case OP_NOTMINSTARI:
465 case OP_POSSTAR:
466 case OP_POSSTARI:
467 case OP_NOTPOSSTAR:
468 case OP_NOTPOSSTARI:
469
470 case OP_QUERY:
471 case OP_QUERYI:
472 case OP_NOTQUERY:
473 case OP_NOTQUERYI:
474 case OP_MINQUERY:
475 case OP_MINQUERYI:
476 case OP_NOTMINQUERY:
477 case OP_NOTMINQUERYI:
478 case OP_POSQUERY:
479 case OP_POSQUERYI:
480 case OP_NOTPOSQUERY:
481 case OP_NOTPOSQUERYI:
482
483 cc += _pcre_OP_lengths[op];
484 #ifdef SUPPORT_UTF8
485 if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
486 #endif
487 break;
488
489 /* Skip these, but we need to add in the name length. */
490
491 case OP_MARK:
492 case OP_PRUNE_ARG:
493 case OP_SKIP_ARG:
494 case OP_THEN_ARG:
495 cc += _pcre_OP_lengths[op] + cc[1];
496 break;
497
498 /* The remaining opcodes are just skipped over. */
499
500 case OP_CLOSE:
501 case OP_COMMIT:
502 case OP_FAIL:
503 case OP_PRUNE:
504 case OP_SET_SOM:
505 case OP_SKIP:
506 case OP_THEN:
507 cc += _pcre_OP_lengths[op];
508 break;
509
510 /* This should not occur: we list all opcodes explicitly so that when
511 new ones get added they are properly considered. */
512
513 default:
514 return -3;
515 }
516 }
517 /* Control never gets here */
518 }
519
520
521
522 /*************************************************
523 * Set a bit and maybe its alternate case *
524 *************************************************/
525
526 /* Given a character, set its first byte's bit in the table, and also the
527 corresponding bit for the other version of a letter if we are caseless. In
528 UTF-8 mode, for characters greater than 127, we can only do the caseless thing
529 when Unicode property support is available.
530
531 Arguments:
532 start_bits points to the bit map
533 p points to the character
534 caseless the caseless flag
535 cd the block with char table pointers
536 utf8 TRUE for UTF-8 mode
537
538 Returns: pointer after the character
539 */
540
541 static const uschar *
542 set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,
543 compile_data *cd, BOOL utf8)
544 {
545 unsigned int c = *p;
546
547 SET_BIT(c);
548
549 #ifdef SUPPORT_UTF8
550 if (utf8 && c > 127)
551 {
552 GETCHARINC(c, p);
553 #ifdef SUPPORT_UCP
554 if (caseless)
555 {
556 uschar buff[8];
557 c = UCD_OTHERCASE(c);
558 (void)_pcre_ord2utf8(c, buff);
559 SET_BIT(buff[0]);
560 }
561 #endif
562 return p;
563 }
564 #endif
565
566 /* Not UTF-8 mode, or character is less than 127. */
567
568 if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
569 return p + 1;
570 }
571
572
573
574 /*************************************************
575 * Set bits for a positive character type *
576 *************************************************/
577
578 /* This function sets starting bits for a character type. In UTF-8 mode, we can
579 only do a direct setting for bytes less than 128, as otherwise there can be
580 confusion with bytes in the middle of UTF-8 characters. In a "traditional"
581 environment, the tables will only recognize ASCII characters anyway, but in at
582 least one Windows environment, some higher bytes bits were set in the tables.
583 So we deal with that case by considering the UTF-8 encoding.
584
585 Arguments:
586 start_bits the starting bitmap
587 cbit type the type of character wanted
588 table_limit 32 for non-UTF-8; 16 for UTF-8
589 cd the block with char table pointers
590
591 Returns: nothing
592 */
593
594 static void
595 set_type_bits(uschar *start_bits, int cbit_type, int table_limit,
596 compile_data *cd)
597 {
598 register int c;
599 for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
600 if (table_limit == 32) return;
601 for (c = 128; c < 256; c++)
602 {
603 if ((cd->cbits[c/8] & (1 << (c&7))) != 0)
604 {
605 uschar buff[8];
606 (void)_pcre_ord2utf8(c, buff);
607 SET_BIT(buff[0]);
608 }
609 }
610 }
611
612
613 /*************************************************
614 * Set bits for a negative character type *
615 *************************************************/
616
617 /* This function sets starting bits for a negative character type such as \D.
618 In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
619 otherwise there can be confusion with bytes in the middle of UTF-8 characters.
620 Unlike in the positive case, where we can set appropriate starting bits for
621 specific high-valued UTF-8 characters, in this case we have to set the bits for
622 all high-valued characters. The lowest is 0xc2, but we overkill by starting at
623 0xc0 (192) for simplicity.
624
625 Arguments:
626 start_bits the starting bitmap
627 cbit type the type of character wanted
628 table_limit 32 for non-UTF-8; 16 for UTF-8
629 cd the block with char table pointers
630
631 Returns: nothing
632 */
633
634 static void
635 set_nottype_bits(uschar *start_bits, int cbit_type, int table_limit,
636 compile_data *cd)
637 {
638 register int c;
639 for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
640 if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
641 }
642
643
644
645 /*************************************************
646 * Create bitmap of starting bytes *
647 *************************************************/
648
649 /* This function scans a compiled unanchored expression recursively and
650 attempts to build a bitmap of the set of possible starting bytes. As time goes
651 by, we may be able to get more clever at doing this. The SSB_CONTINUE return is
652 useful for parenthesized groups in patterns such as (a*)b where the group
653 provides some optional starting bytes but scanning must continue at the outer
654 level to find at least one mandatory byte. At the outermost level, this
655 function fails unless the result is SSB_DONE.
656
657 Arguments:
658 code points to an expression
659 start_bits points to a 32-byte table, initialized to 0
660 utf8 TRUE if in UTF-8 mode
661 cd the block with char table pointers
662
663 Returns: SSB_FAIL => Failed to find any starting bytes
664 SSB_DONE => Found mandatory starting bytes
665 SSB_CONTINUE => Found optional starting bytes
666 SSB_UNKNOWN => Hit an unrecognized opcode
667 */
668
669 static int
670 set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8,
671 compile_data *cd)
672 {
673 register int c;
674 int yield = SSB_DONE;
675 int table_limit = utf8? 16:32;
676
677 #if 0
678 /* ========================================================================= */
679 /* The following comment and code was inserted in January 1999. In May 2006,
680 when it was observed to cause compiler warnings about unused values, I took it
681 out again. If anybody is still using OS/2, they will have to put it back
682 manually. */
683
684 /* This next statement and the later reference to dummy are here in order to
685 trick the optimizer of the IBM C compiler for OS/2 into generating correct
686 code. Apparently IBM isn't going to fix the problem, and we would rather not
687 disable optimization (in this module it actually makes a big difference, and
688 the pcre module can use all the optimization it can get). */
689
690 volatile int dummy;
691 /* ========================================================================= */
692 #endif
693
694 do
695 {
696 BOOL try_next = TRUE;
697 const uschar *tcode = code + 1 + LINK_SIZE;
698
699 if (*code == OP_CBRA || *code == OP_SCBRA ||
700 *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += 2;
701
702 while (try_next) /* Loop for items in this branch */
703 {
704 int rc;
705
706 switch(*tcode)
707 {
708 /* If we reach something we don't understand, it means a new opcode has
709 been created that hasn't been added to this code. Hopefully this problem
710 will be discovered during testing. */
711
712 default:
713 return SSB_UNKNOWN;
714
715 /* Fail for a valid opcode that implies no starting bits. */
716
717 case OP_ACCEPT:
718 case OP_ASSERT_ACCEPT:
719 case OP_ALLANY:
720 case OP_ANY:
721 case OP_ANYBYTE:
722 case OP_CIRC:
723 case OP_CIRCM:
724 case OP_CLOSE:
725 case OP_COMMIT:
726 case OP_COND:
727 case OP_CREF:
728 case OP_DEF:
729 case OP_DOLL:
730 case OP_DOLLM:
731 case OP_END:
732 case OP_EOD:
733 case OP_EODN:
734 case OP_EXTUNI:
735 case OP_FAIL:
736 case OP_MARK:
737 case OP_NCREF:
738 case OP_NOT:
739 case OP_NOTEXACT:
740 case OP_NOTEXACTI:
741 case OP_NOTI:
742 case OP_NOTMINPLUS:
743 case OP_NOTMINPLUSI:
744 case OP_NOTMINQUERY:
745 case OP_NOTMINQUERYI:
746 case OP_NOTMINSTAR:
747 case OP_NOTMINSTARI:
748 case OP_NOTMINUPTO:
749 case OP_NOTMINUPTOI:
750 case OP_NOTPLUS:
751 case OP_NOTPLUSI:
752 case OP_NOTPOSPLUS:
753 case OP_NOTPOSPLUSI:
754 case OP_NOTPOSQUERY:
755 case OP_NOTPOSQUERYI:
756 case OP_NOTPOSSTAR:
757 case OP_NOTPOSSTARI:
758 case OP_NOTPOSUPTO:
759 case OP_NOTPOSUPTOI:
760 case OP_NOTPROP:
761 case OP_NOTQUERY:
762 case OP_NOTQUERYI:
763 case OP_NOTSTAR:
764 case OP_NOTSTARI:
765 case OP_NOTUPTO:
766 case OP_NOTUPTOI:
767 case OP_NOT_HSPACE:
768 case OP_NOT_VSPACE:
769 case OP_NRREF:
770 case OP_PROP:
771 case OP_PRUNE:
772 case OP_PRUNE_ARG:
773 case OP_RECURSE:
774 case OP_REF:
775 case OP_REFI:
776 case OP_REVERSE:
777 case OP_RREF:
778 case OP_SCOND:
779 case OP_SET_SOM:
780 case OP_SKIP:
781 case OP_SKIP_ARG:
782 case OP_SOD:
783 case OP_SOM:
784 case OP_THEN:
785 case OP_THEN_ARG:
786 case OP_XCLASS:
787 return SSB_FAIL;
788
789 /* We can ignore word boundary tests. */
790
791 case OP_WORD_BOUNDARY:
792 case OP_NOT_WORD_BOUNDARY:
793 tcode++;
794 break;
795
796 /* If we hit a bracket or a positive lookahead assertion, recurse to set
797 bits from within the subpattern. If it can't find anything, we have to
798 give up. If it finds some mandatory character(s), we are done for this
799 branch. Otherwise, carry on scanning after the subpattern. */
800
801 case OP_BRA:
802 case OP_SBRA:
803 case OP_CBRA:
804 case OP_SCBRA:
805 case OP_BRAPOS:
806 case OP_SBRAPOS:
807 case OP_CBRAPOS:
808 case OP_SCBRAPOS:
809 case OP_ONCE:
810 case OP_ONCE_NC:
811 case OP_ASSERT:
812 rc = set_start_bits(tcode, start_bits, utf8, cd);
813 if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
814 if (rc == SSB_DONE) try_next = FALSE; else
815 {
816 do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
817 tcode += 1 + LINK_SIZE;
818 }
819 break;
820
821 /* If we hit ALT or KET, it means we haven't found anything mandatory in
822 this branch, though we might have found something optional. For ALT, we
823 continue with the next alternative, but we have to arrange that the final
824 result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
825 return SSB_CONTINUE: if this is the top level, that indicates failure,
826 but after a nested subpattern, it causes scanning to continue. */
827
828 case OP_ALT:
829 yield = SSB_CONTINUE;
830 try_next = FALSE;
831 break;
832
833 case OP_KET:
834 case OP_KETRMAX:
835 case OP_KETRMIN:
836 case OP_KETRPOS:
837 return SSB_CONTINUE;
838
839 /* Skip over callout */
840
841 case OP_CALLOUT:
842 tcode += 2 + 2*LINK_SIZE;
843 break;
844
845 /* Skip over lookbehind and negative lookahead assertions */
846
847 case OP_ASSERT_NOT:
848 case OP_ASSERTBACK:
849 case OP_ASSERTBACK_NOT:
850 do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
851 tcode += 1 + LINK_SIZE;
852 break;
853
854 /* BRAZERO does the bracket, but carries on. */
855
856 case OP_BRAZERO:
857 case OP_BRAMINZERO:
858 case OP_BRAPOSZERO:
859 rc = set_start_bits(++tcode, start_bits, utf8, cd);
860 if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
861 /* =========================================================================
862 See the comment at the head of this function concerning the next line,
863 which was an old fudge for the benefit of OS/2.
864 dummy = 1;
865 ========================================================================= */
866 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
867 tcode += 1 + LINK_SIZE;
868 break;
869
870 /* SKIPZERO skips the bracket. */
871
872 case OP_SKIPZERO:
873 tcode++;
874 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
875 tcode += 1 + LINK_SIZE;
876 break;
877
878 /* Single-char * or ? sets the bit and tries the next item */
879
880 case OP_STAR:
881 case OP_MINSTAR:
882 case OP_POSSTAR:
883 case OP_QUERY:
884 case OP_MINQUERY:
885 case OP_POSQUERY:
886 tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
887 break;
888
889 case OP_STARI:
890 case OP_MINSTARI:
891 case OP_POSSTARI:
892 case OP_QUERYI:
893 case OP_MINQUERYI:
894 case OP_POSQUERYI:
895 tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
896 break;
897
898 /* Single-char upto sets the bit and tries the next */
899
900 case OP_UPTO:
901 case OP_MINUPTO:
902 case OP_POSUPTO:
903 tcode = set_table_bit(start_bits, tcode + 3, FALSE, cd, utf8);
904 break;
905
906 case OP_UPTOI:
907 case OP_MINUPTOI:
908 case OP_POSUPTOI:
909 tcode = set_table_bit(start_bits, tcode + 3, TRUE, cd, utf8);
910 break;
911
912 /* At least one single char sets the bit and stops */
913
914 case OP_EXACT:
915 tcode += 2;
916 /* Fall through */
917 case OP_CHAR:
918 case OP_PLUS:
919 case OP_MINPLUS:
920 case OP_POSPLUS:
921 (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
922 try_next = FALSE;
923 break;
924
925 case OP_EXACTI:
926 tcode += 2;
927 /* Fall through */
928 case OP_CHARI:
929 case OP_PLUSI:
930 case OP_MINPLUSI:
931 case OP_POSPLUSI:
932 (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
933 try_next = FALSE;
934 break;
935
936 /* Special spacing and line-terminating items. These recognize specific
937 lists of characters. The difference between VSPACE and ANYNL is that the
938 latter can match the two-character CRLF sequence, but that is not
939 relevant for finding the first character, so their code here is
940 identical. */
941
942 case OP_HSPACE:
943 SET_BIT(0x09);
944 SET_BIT(0x20);
945 if (utf8)
946 {
947 SET_BIT(0xC2); /* For U+00A0 */
948 SET_BIT(0xE1); /* For U+1680, U+180E */
949 SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
950 SET_BIT(0xE3); /* For U+3000 */
951 }
952 else SET_BIT(0xA0);
953 try_next = FALSE;
954 break;
955
956 case OP_ANYNL:
957 case OP_VSPACE:
958 SET_BIT(0x0A);
959 SET_BIT(0x0B);
960 SET_BIT(0x0C);
961 SET_BIT(0x0D);
962 if (utf8)
963 {
964 SET_BIT(0xC2); /* For U+0085 */
965 SET_BIT(0xE2); /* For U+2028, U+2029 */
966 }
967 else SET_BIT(0x85);
968 try_next = FALSE;
969 break;
970
971 /* Single character types set the bits and stop. Note that if PCRE_UCP
972 is set, we do not see these op codes because \d etc are converted to
973 properties. Therefore, these apply in the case when only characters less
974 than 256 are recognized to match the types. */
975
976 case OP_NOT_DIGIT:
977 set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
978 try_next = FALSE;
979 break;
980
981 case OP_DIGIT:
982 set_type_bits(start_bits, cbit_digit, table_limit, cd);
983 try_next = FALSE;
984 break;
985
986 /* The cbit_space table has vertical tab as whitespace; we have to
987 ensure it is set as not whitespace. */
988
989 case OP_NOT_WHITESPACE:
990 set_nottype_bits(start_bits, cbit_space, table_limit, cd);
991 start_bits[1] |= 0x08;
992 try_next = FALSE;
993 break;
994
995 /* The cbit_space table has vertical tab as whitespace; we have to
996 not set it from the table. */
997
998 case OP_WHITESPACE:
999 c = start_bits[1]; /* Save in case it was already set */
1000 set_type_bits(start_bits, cbit_space, table_limit, cd);
1001 start_bits[1] = (start_bits[1] & ~0x08) | c;
1002 try_next = FALSE;
1003 break;
1004
1005 case OP_NOT_WORDCHAR:
1006 set_nottype_bits(start_bits, cbit_word, table_limit, cd);
1007 try_next = FALSE;
1008 break;
1009
1010 case OP_WORDCHAR:
1011 set_type_bits(start_bits, cbit_word, table_limit, cd);
1012 try_next = FALSE;
1013 break;
1014
1015 /* One or more character type fudges the pointer and restarts, knowing
1016 it will hit a single character type and stop there. */
1017
1018 case OP_TYPEPLUS:
1019 case OP_TYPEMINPLUS:
1020 case OP_TYPEPOSPLUS:
1021 tcode++;
1022 break;
1023
1024 case OP_TYPEEXACT:
1025 tcode += 3;
1026 break;
1027
1028 /* Zero or more repeats of character types set the bits and then
1029 try again. */
1030
1031 case OP_TYPEUPTO:
1032 case OP_TYPEMINUPTO:
1033 case OP_TYPEPOSUPTO:
1034 tcode += 2; /* Fall through */
1035
1036 case OP_TYPESTAR:
1037 case OP_TYPEMINSTAR:
1038 case OP_TYPEPOSSTAR:
1039 case OP_TYPEQUERY:
1040 case OP_TYPEMINQUERY:
1041 case OP_TYPEPOSQUERY:
1042 switch(tcode[1])
1043 {
1044 default:
1045 case OP_ANY:
1046 case OP_ALLANY:
1047 return SSB_FAIL;
1048
1049 case OP_HSPACE:
1050 SET_BIT(0x09);
1051 SET_BIT(0x20);
1052 if (utf8)
1053 {
1054 SET_BIT(0xC2); /* For U+00A0 */
1055 SET_BIT(0xE1); /* For U+1680, U+180E */
1056 SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
1057 SET_BIT(0xE3); /* For U+3000 */
1058 }
1059 else SET_BIT(0xA0);
1060 break;
1061
1062 case OP_ANYNL:
1063 case OP_VSPACE:
1064 SET_BIT(0x0A);
1065 SET_BIT(0x0B);
1066 SET_BIT(0x0C);
1067 SET_BIT(0x0D);
1068 if (utf8)
1069 {
1070 SET_BIT(0xC2); /* For U+0085 */
1071 SET_BIT(0xE2); /* For U+2028, U+2029 */
1072 }
1073 else SET_BIT(0x85);
1074 break;
1075
1076 case OP_NOT_DIGIT:
1077 set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
1078 break;
1079
1080 case OP_DIGIT:
1081 set_type_bits(start_bits, cbit_digit, table_limit, cd);
1082 break;
1083
1084 /* The cbit_space table has vertical tab as whitespace; we have to
1085 ensure it gets set as not whitespace. */
1086
1087 case OP_NOT_WHITESPACE:
1088 set_nottype_bits(start_bits, cbit_space, table_limit, cd);
1089 start_bits[1] |= 0x08;
1090 break;
1091
1092 /* The cbit_space table has vertical tab as whitespace; we have to
1093 avoid setting it. */
1094
1095 case OP_WHITESPACE:
1096 c = start_bits[1]; /* Save in case it was already set */
1097 set_type_bits(start_bits, cbit_space, table_limit, cd);
1098 start_bits[1] = (start_bits[1] & ~0x08) | c;
1099 break;
1100
1101 case OP_NOT_WORDCHAR:
1102 set_nottype_bits(start_bits, cbit_word, table_limit, cd);
1103 break;
1104
1105 case OP_WORDCHAR:
1106 set_type_bits(start_bits, cbit_word, table_limit, cd);
1107 break;
1108 }
1109
1110 tcode += 2;
1111 break;
1112
1113 /* Character class where all the information is in a bit map: set the
1114 bits and either carry on or not, according to the repeat count. If it was
1115 a negative class, and we are operating with UTF-8 characters, any byte
1116 with a value >= 0xc4 is a potentially valid starter because it starts a
1117 character with a value > 255. */
1118
1119 case OP_NCLASS:
1120 #ifdef SUPPORT_UTF8
1121 if (utf8)
1122 {
1123 start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
1124 memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
1125 }
1126 #endif
1127 /* Fall through */
1128
1129 case OP_CLASS:
1130 {
1131 tcode++;
1132
1133 /* In UTF-8 mode, the bits in a bit map correspond to character
1134 values, not to byte values. However, the bit map we are constructing is
1135 for byte values. So we have to do a conversion for characters whose
1136 value is > 127. In fact, there are only two possible starting bytes for
1137 characters in the range 128 - 255. */
1138
1139 #ifdef SUPPORT_UTF8
1140 if (utf8)
1141 {
1142 for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];
1143 for (c = 128; c < 256; c++)
1144 {
1145 if ((tcode[c/8] && (1 << (c&7))) != 0)
1146 {
1147 int d = (c >> 6) | 0xc0; /* Set bit for this starter */
1148 start_bits[d/8] |= (1 << (d&7)); /* and then skip on to the */
1149 c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */
1150 }
1151 }
1152 }
1153
1154 /* In non-UTF-8 mode, the two bit maps are completely compatible. */
1155
1156 else
1157 #endif
1158 {
1159 for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
1160 }
1161
1162 /* Advance past the bit map, and act on what follows. For a zero
1163 minimum repeat, continue; otherwise stop processing. */
1164
1165 tcode += 32;
1166 switch (*tcode)
1167 {
1168 case OP_CRSTAR:
1169 case OP_CRMINSTAR:
1170 case OP_CRQUERY:
1171 case OP_CRMINQUERY:
1172 tcode++;
1173 break;
1174
1175 case OP_CRRANGE:
1176 case OP_CRMINRANGE:
1177 if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;
1178 else try_next = FALSE;
1179 break;
1180
1181 default:
1182 try_next = FALSE;
1183 break;
1184 }
1185 }
1186 break; /* End of bitmap class handling */
1187
1188 } /* End of switch */
1189 } /* End of try_next loop */
1190
1191 code += GET(code, 1); /* Advance to next branch */
1192 }
1193 while (*code == OP_ALT);
1194 return yield;
1195 }
1196
1197
1198
1199
1200
1201 /*************************************************
1202 * Study a compiled expression *
1203 *************************************************/
1204
1205 /* This function is handed a compiled expression that it must study to produce
1206 information that will speed up the matching. It returns a pcre_extra block
1207 which then gets handed back to pcre_exec().
1208
1209 Arguments:
1210 re points to the compiled expression
1211 options contains option bits
1212 errorptr points to where to place error messages;
1213 set NULL unless error
1214
1215 Returns: pointer to a pcre_extra block, with study_data filled in and the
1216 appropriate flags set;
1217 NULL on error or if no optimization possible
1218 */
1219
1220 PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
1221 pcre_study(const pcre *external_re, int options, const char **errorptr)
1222 {
1223 int min;
1224 BOOL bits_set = FALSE;
1225 uschar start_bits[32];
1226 pcre_extra *extra = NULL;
1227 pcre_study_data *study;
1228 const uschar *tables;
1229 uschar *code;
1230 compile_data compile_block;
1231 const real_pcre *re = (const real_pcre *)external_re;
1232
1233 *errorptr = NULL;
1234
1235 if (re == NULL || re->magic_number != MAGIC_NUMBER)
1236 {
1237 *errorptr = "argument is not a compiled regular expression";
1238 return NULL;
1239 }
1240
1241 if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
1242 {
1243 *errorptr = "unknown or incorrect option bit(s) set";
1244 return NULL;
1245 }
1246
1247 code = (uschar *)re + re->name_table_offset +
1248 (re->name_count * re->name_entry_size);
1249
1250 /* For an anchored pattern, or an unanchored pattern that has a first char, or
1251 a multiline pattern that matches only at "line starts", there is no point in
1252 seeking a list of starting bytes. */
1253
1254 if ((re->options & PCRE_ANCHORED) == 0 &&
1255 (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1256 {
1257 int rc;
1258
1259 /* Set the character tables in the block that is passed around */
1260
1261 tables = re->tables;
1262 if (tables == NULL)
1263 (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1264 (void *)(&tables));
1265
1266 compile_block.lcc = tables + lcc_offset;
1267 compile_block.fcc = tables + fcc_offset;
1268 compile_block.cbits = tables + cbits_offset;
1269 compile_block.ctypes = tables + ctypes_offset;
1270
1271 /* See if we can find a fixed set of initial characters for the pattern. */
1272
1273 memset(start_bits, 0, 32 * sizeof(uschar));
1274 rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1275 &compile_block);
1276 bits_set = rc == SSB_DONE;
1277 if (rc == SSB_UNKNOWN)
1278 {
1279 *errorptr = "internal error: opcode not recognized";
1280 return NULL;
1281 }
1282 }
1283
1284 /* Find the minimum length of subject string. */
1285
1286 switch(min = find_minlength(code, code, re->options, 0))
1287 {
1288 case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
1289 case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
1290 default: break;
1291 }
1292
1293 /* If a set of starting bytes has been identified, or if the minimum length is
1294 greater than zero, or if JIT optimization has been requested, get a pcre_extra
1295 block and a pcre_study_data block. The study data is put in the latter, which
1296 is pointed to by the former, which may also get additional data set later by
1297 the calling program. At the moment, the size of pcre_study_data is fixed. We
1298 nevertheless save it in a field for returning via the pcre_fullinfo() function
1299 so that if it becomes variable in the future, we don't have to change that
1300 code. */
1301
1302 if (bits_set || min > 0
1303 #ifdef SUPPORT_JIT
1304 || (options & PCRE_STUDY_JIT_COMPILE) != 0
1305 #endif
1306 )
1307 {
1308 extra = (pcre_extra *)(pcre_malloc)
1309 (sizeof(pcre_extra) + sizeof(pcre_study_data));
1310 if (extra == NULL)
1311 {
1312 *errorptr = "failed to get memory";
1313 return NULL;
1314 }
1315
1316 study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
1317 extra->flags = PCRE_EXTRA_STUDY_DATA;
1318 extra->study_data = study;
1319
1320 study->size = sizeof(pcre_study_data);
1321 study->flags = 0;
1322
1323 if (bits_set)
1324 {
1325 study->flags |= PCRE_STUDY_MAPPED;
1326 memcpy(study->start_bits, start_bits, sizeof(start_bits));
1327 }
1328
1329 /* Always set the minlength value in the block, because the JIT compiler
1330 makes use of it. However, don't set the bit unless the length is greater than
1331 zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
1332 checking the zero case. */
1333
1334 if (min > 0)
1335 {
1336 study->flags |= PCRE_STUDY_MINLEN;
1337 study->minlength = min;
1338 }
1339 else study->minlength = 0;
1340
1341 /* If JIT support was compiled and requested, attempt the JIT compilation.
1342 If no starting bytes were found, and the minimum length is zero, and JIT
1343 compilation fails, abandon the extra block and return NULL. */
1344
1345 #ifdef SUPPORT_JIT
1346 extra->executable_jit = NULL;
1347 if ((options & PCRE_STUDY_JIT_COMPILE) != 0) _pcre_jit_compile(re, extra);
1348 if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0)
1349 {
1350 pcre_free_study(extra);
1351 extra = NULL;
1352 }
1353 #endif
1354 }
1355
1356 return extra;
1357 }
1358
1359
1360 /*************************************************
1361 * Free the study data *
1362 *************************************************/
1363
1364 /* This function frees the memory that was obtained by pcre_study().
1365
1366 Argument: a pointer to the pcre_extra block
1367 Returns: nothing
1368 */
1369
1370 PCRE_EXP_DEFN void
1371 pcre_free_study(pcre_extra *extra)
1372 {
1373 #ifdef SUPPORT_JIT
1374 if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
1375 extra->executable_jit != NULL)
1376 _pcre_jit_free(extra->executable_jit);
1377 #endif
1378 pcre_free(extra);
1379 }
1380
1381 /* 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