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

Contents of /code/trunk/pcre_study.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 729 - (show annotations)
Mon Oct 10 16:02:20 2011 UTC (3 years, 8 months ago) by ph10
File MIME type: text/plain
File size: 38862 byte(s)
Error occurred while calculating annotation data.
Fix THEN:ARG bug introduced with *THEN refactoring.
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 if (cs == NULL) return -2;
427 do ce += GET(ce, 1); while (*ce == OP_ALT);
428 if ((cc > cs && cc < ce) || recurse_depth > 10)
429 had_recurse = TRUE;
430 else
431 {
432 branchlength += find_minlength(cs, startcode, options, recurse_depth + 1);
433 }
434 cc += 1 + LINK_SIZE;
435 break;
436
437 /* Anything else does not or need not match a character. We can get the
438 item's length from the table, but for those that can match zero occurrences
439 of a character, we must take special action for UTF-8 characters. As it
440 happens, the "NOT" versions of these opcodes are used at present only for
441 ASCII characters, so they could be omitted from this list. However, in
442 future that may change, so we include them here so as not to leave a
443 gotcha for a future maintainer. */
444
445 case OP_UPTO:
446 case OP_UPTOI:
447 case OP_NOTUPTO:
448 case OP_NOTUPTOI:
449 case OP_MINUPTO:
450 case OP_MINUPTOI:
451 case OP_NOTMINUPTO:
452 case OP_NOTMINUPTOI:
453 case OP_POSUPTO:
454 case OP_POSUPTOI:
455 case OP_NOTPOSUPTO:
456 case OP_NOTPOSUPTOI:
457
458 case OP_STAR:
459 case OP_STARI:
460 case OP_NOTSTAR:
461 case OP_NOTSTARI:
462 case OP_MINSTAR:
463 case OP_MINSTARI:
464 case OP_NOTMINSTAR:
465 case OP_NOTMINSTARI:
466 case OP_POSSTAR:
467 case OP_POSSTARI:
468 case OP_NOTPOSSTAR:
469 case OP_NOTPOSSTARI:
470
471 case OP_QUERY:
472 case OP_QUERYI:
473 case OP_NOTQUERY:
474 case OP_NOTQUERYI:
475 case OP_MINQUERY:
476 case OP_MINQUERYI:
477 case OP_NOTMINQUERY:
478 case OP_NOTMINQUERYI:
479 case OP_POSQUERY:
480 case OP_POSQUERYI:
481 case OP_NOTPOSQUERY:
482 case OP_NOTPOSQUERYI:
483
484 cc += _pcre_OP_lengths[op];
485 #ifdef SUPPORT_UTF8
486 if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
487 #endif
488 break;
489
490 /* Skip these, but we need to add in the name length. */
491
492 case OP_MARK:
493 case OP_PRUNE_ARG:
494 case OP_SKIP_ARG:
495 case OP_THEN_ARG:
496 cc += _pcre_OP_lengths[op] + cc[1];
497 break;
498
499 /* The remaining opcodes are just skipped over. */
500
501 case OP_CLOSE:
502 case OP_COMMIT:
503 case OP_FAIL:
504 case OP_PRUNE:
505 case OP_SET_SOM:
506 case OP_SKIP:
507 case OP_THEN:
508 cc += _pcre_OP_lengths[op];
509 break;
510
511 /* This should not occur: we list all opcodes explicitly so that when
512 new ones get added they are properly considered. */
513
514 default:
515 return -3;
516 }
517 }
518 /* Control never gets here */
519 }
520
521
522
523 /*************************************************
524 * Set a bit and maybe its alternate case *
525 *************************************************/
526
527 /* Given a character, set its first byte's bit in the table, and also the
528 corresponding bit for the other version of a letter if we are caseless. In
529 UTF-8 mode, for characters greater than 127, we can only do the caseless thing
530 when Unicode property support is available.
531
532 Arguments:
533 start_bits points to the bit map
534 p points to the character
535 caseless the caseless flag
536 cd the block with char table pointers
537 utf8 TRUE for UTF-8 mode
538
539 Returns: pointer after the character
540 */
541
542 static const uschar *
543 set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,
544 compile_data *cd, BOOL utf8)
545 {
546 unsigned int c = *p;
547
548 SET_BIT(c);
549
550 #ifdef SUPPORT_UTF8
551 if (utf8 && c > 127)
552 {
553 GETCHARINC(c, p);
554 #ifdef SUPPORT_UCP
555 if (caseless)
556 {
557 uschar buff[8];
558 c = UCD_OTHERCASE(c);
559 (void)_pcre_ord2utf8(c, buff);
560 SET_BIT(buff[0]);
561 }
562 #endif
563 return p;
564 }
565 #endif
566
567 /* Not UTF-8 mode, or character is less than 127. */
568
569 if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
570 return p + 1;
571 }
572
573
574
575 /*************************************************
576 * Set bits for a positive character type *
577 *************************************************/
578
579 /* This function sets starting bits for a character type. In UTF-8 mode, we can
580 only do a direct setting for bytes less than 128, as otherwise there can be
581 confusion with bytes in the middle of UTF-8 characters. In a "traditional"
582 environment, the tables will only recognize ASCII characters anyway, but in at
583 least one Windows environment, some higher bytes bits were set in the tables.
584 So we deal with that case by considering the UTF-8 encoding.
585
586 Arguments:
587 start_bits the starting bitmap
588 cbit type the type of character wanted
589 table_limit 32 for non-UTF-8; 16 for UTF-8
590 cd the block with char table pointers
591
592 Returns: nothing
593 */
594
595 static void
596 set_type_bits(uschar *start_bits, int cbit_type, int table_limit,
597 compile_data *cd)
598 {
599 register int c;
600 for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
601 if (table_limit == 32) return;
602 for (c = 128; c < 256; c++)
603 {
604 if ((cd->cbits[c/8] & (1 << (c&7))) != 0)
605 {
606 uschar buff[8];
607 (void)_pcre_ord2utf8(c, buff);
608 SET_BIT(buff[0]);
609 }
610 }
611 }
612
613
614 /*************************************************
615 * Set bits for a negative character type *
616 *************************************************/
617
618 /* This function sets starting bits for a negative character type such as \D.
619 In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
620 otherwise there can be confusion with bytes in the middle of UTF-8 characters.
621 Unlike in the positive case, where we can set appropriate starting bits for
622 specific high-valued UTF-8 characters, in this case we have to set the bits for
623 all high-valued characters. The lowest is 0xc2, but we overkill by starting at
624 0xc0 (192) for simplicity.
625
626 Arguments:
627 start_bits the starting bitmap
628 cbit type the type of character wanted
629 table_limit 32 for non-UTF-8; 16 for UTF-8
630 cd the block with char table pointers
631
632 Returns: nothing
633 */
634
635 static void
636 set_nottype_bits(uschar *start_bits, int cbit_type, int table_limit,
637 compile_data *cd)
638 {
639 register int c;
640 for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
641 if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
642 }
643
644
645
646 /*************************************************
647 * Create bitmap of starting bytes *
648 *************************************************/
649
650 /* This function scans a compiled unanchored expression recursively and
651 attempts to build a bitmap of the set of possible starting bytes. As time goes
652 by, we may be able to get more clever at doing this. The SSB_CONTINUE return is
653 useful for parenthesized groups in patterns such as (a*)b where the group
654 provides some optional starting bytes but scanning must continue at the outer
655 level to find at least one mandatory byte. At the outermost level, this
656 function fails unless the result is SSB_DONE.
657
658 Arguments:
659 code points to an expression
660 start_bits points to a 32-byte table, initialized to 0
661 utf8 TRUE if in UTF-8 mode
662 cd the block with char table pointers
663
664 Returns: SSB_FAIL => Failed to find any starting bytes
665 SSB_DONE => Found mandatory starting bytes
666 SSB_CONTINUE => Found optional starting bytes
667 SSB_UNKNOWN => Hit an unrecognized opcode
668 */
669
670 static int
671 set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8,
672 compile_data *cd)
673 {
674 register int c;
675 int yield = SSB_DONE;
676 int table_limit = utf8? 16:32;
677
678 #if 0
679 /* ========================================================================= */
680 /* The following comment and code was inserted in January 1999. In May 2006,
681 when it was observed to cause compiler warnings about unused values, I took it
682 out again. If anybody is still using OS/2, they will have to put it back
683 manually. */
684
685 /* This next statement and the later reference to dummy are here in order to
686 trick the optimizer of the IBM C compiler for OS/2 into generating correct
687 code. Apparently IBM isn't going to fix the problem, and we would rather not
688 disable optimization (in this module it actually makes a big difference, and
689 the pcre module can use all the optimization it can get). */
690
691 volatile int dummy;
692 /* ========================================================================= */
693 #endif
694
695 do
696 {
697 BOOL try_next = TRUE;
698 const uschar *tcode = code + 1 + LINK_SIZE;
699
700 if (*code == OP_CBRA || *code == OP_SCBRA ||
701 *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += 2;
702
703 while (try_next) /* Loop for items in this branch */
704 {
705 int rc;
706
707 switch(*tcode)
708 {
709 /* If we reach something we don't understand, it means a new opcode has
710 been created that hasn't been added to this code. Hopefully this problem
711 will be discovered during testing. */
712
713 default:
714 return SSB_UNKNOWN;
715
716 /* Fail for a valid opcode that implies no starting bits. */
717
718 case OP_ACCEPT:
719 case OP_ASSERT_ACCEPT:
720 case OP_ALLANY:
721 case OP_ANY:
722 case OP_ANYBYTE:
723 case OP_CIRC:
724 case OP_CIRCM:
725 case OP_CLOSE:
726 case OP_COMMIT:
727 case OP_COND:
728 case OP_CREF:
729 case OP_DEF:
730 case OP_DOLL:
731 case OP_DOLLM:
732 case OP_END:
733 case OP_EOD:
734 case OP_EODN:
735 case OP_EXTUNI:
736 case OP_FAIL:
737 case OP_MARK:
738 case OP_NCREF:
739 case OP_NOT:
740 case OP_NOTEXACT:
741 case OP_NOTEXACTI:
742 case OP_NOTI:
743 case OP_NOTMINPLUS:
744 case OP_NOTMINPLUSI:
745 case OP_NOTMINQUERY:
746 case OP_NOTMINQUERYI:
747 case OP_NOTMINSTAR:
748 case OP_NOTMINSTARI:
749 case OP_NOTMINUPTO:
750 case OP_NOTMINUPTOI:
751 case OP_NOTPLUS:
752 case OP_NOTPLUSI:
753 case OP_NOTPOSPLUS:
754 case OP_NOTPOSPLUSI:
755 case OP_NOTPOSQUERY:
756 case OP_NOTPOSQUERYI:
757 case OP_NOTPOSSTAR:
758 case OP_NOTPOSSTARI:
759 case OP_NOTPOSUPTO:
760 case OP_NOTPOSUPTOI:
761 case OP_NOTPROP:
762 case OP_NOTQUERY:
763 case OP_NOTQUERYI:
764 case OP_NOTSTAR:
765 case OP_NOTSTARI:
766 case OP_NOTUPTO:
767 case OP_NOTUPTOI:
768 case OP_NOT_HSPACE:
769 case OP_NOT_VSPACE:
770 case OP_NRREF:
771 case OP_PROP:
772 case OP_PRUNE:
773 case OP_PRUNE_ARG:
774 case OP_RECURSE:
775 case OP_REF:
776 case OP_REFI:
777 case OP_REVERSE:
778 case OP_RREF:
779 case OP_SCOND:
780 case OP_SET_SOM:
781 case OP_SKIP:
782 case OP_SKIP_ARG:
783 case OP_SOD:
784 case OP_SOM:
785 case OP_THEN:
786 case OP_THEN_ARG:
787 case OP_XCLASS:
788 return SSB_FAIL;
789
790 /* We can ignore word boundary tests. */
791
792 case OP_WORD_BOUNDARY:
793 case OP_NOT_WORD_BOUNDARY:
794 tcode++;
795 break;
796
797 /* If we hit a bracket or a positive lookahead assertion, recurse to set
798 bits from within the subpattern. If it can't find anything, we have to
799 give up. If it finds some mandatory character(s), we are done for this
800 branch. Otherwise, carry on scanning after the subpattern. */
801
802 case OP_BRA:
803 case OP_SBRA:
804 case OP_CBRA:
805 case OP_SCBRA:
806 case OP_BRAPOS:
807 case OP_SBRAPOS:
808 case OP_CBRAPOS:
809 case OP_SCBRAPOS:
810 case OP_ONCE:
811 case OP_ONCE_NC:
812 case OP_ASSERT:
813 rc = set_start_bits(tcode, start_bits, utf8, cd);
814 if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
815 if (rc == SSB_DONE) try_next = FALSE; else
816 {
817 do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
818 tcode += 1 + LINK_SIZE;
819 }
820 break;
821
822 /* If we hit ALT or KET, it means we haven't found anything mandatory in
823 this branch, though we might have found something optional. For ALT, we
824 continue with the next alternative, but we have to arrange that the final
825 result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
826 return SSB_CONTINUE: if this is the top level, that indicates failure,
827 but after a nested subpattern, it causes scanning to continue. */
828
829 case OP_ALT:
830 yield = SSB_CONTINUE;
831 try_next = FALSE;
832 break;
833
834 case OP_KET:
835 case OP_KETRMAX:
836 case OP_KETRMIN:
837 case OP_KETRPOS:
838 return SSB_CONTINUE;
839
840 /* Skip over callout */
841
842 case OP_CALLOUT:
843 tcode += 2 + 2*LINK_SIZE;
844 break;
845
846 /* Skip over lookbehind and negative lookahead assertions */
847
848 case OP_ASSERT_NOT:
849 case OP_ASSERTBACK:
850 case OP_ASSERTBACK_NOT:
851 do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
852 tcode += 1 + LINK_SIZE;
853 break;
854
855 /* BRAZERO does the bracket, but carries on. */
856
857 case OP_BRAZERO:
858 case OP_BRAMINZERO:
859 case OP_BRAPOSZERO:
860 rc = set_start_bits(++tcode, start_bits, utf8, cd);
861 if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
862 /* =========================================================================
863 See the comment at the head of this function concerning the next line,
864 which was an old fudge for the benefit of OS/2.
865 dummy = 1;
866 ========================================================================= */
867 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
868 tcode += 1 + LINK_SIZE;
869 break;
870
871 /* SKIPZERO skips the bracket. */
872
873 case OP_SKIPZERO:
874 tcode++;
875 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
876 tcode += 1 + LINK_SIZE;
877 break;
878
879 /* Single-char * or ? sets the bit and tries the next item */
880
881 case OP_STAR:
882 case OP_MINSTAR:
883 case OP_POSSTAR:
884 case OP_QUERY:
885 case OP_MINQUERY:
886 case OP_POSQUERY:
887 tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
888 break;
889
890 case OP_STARI:
891 case OP_MINSTARI:
892 case OP_POSSTARI:
893 case OP_QUERYI:
894 case OP_MINQUERYI:
895 case OP_POSQUERYI:
896 tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
897 break;
898
899 /* Single-char upto sets the bit and tries the next */
900
901 case OP_UPTO:
902 case OP_MINUPTO:
903 case OP_POSUPTO:
904 tcode = set_table_bit(start_bits, tcode + 3, FALSE, cd, utf8);
905 break;
906
907 case OP_UPTOI:
908 case OP_MINUPTOI:
909 case OP_POSUPTOI:
910 tcode = set_table_bit(start_bits, tcode + 3, TRUE, cd, utf8);
911 break;
912
913 /* At least one single char sets the bit and stops */
914
915 case OP_EXACT:
916 tcode += 2;
917 /* Fall through */
918 case OP_CHAR:
919 case OP_PLUS:
920 case OP_MINPLUS:
921 case OP_POSPLUS:
922 (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
923 try_next = FALSE;
924 break;
925
926 case OP_EXACTI:
927 tcode += 2;
928 /* Fall through */
929 case OP_CHARI:
930 case OP_PLUSI:
931 case OP_MINPLUSI:
932 case OP_POSPLUSI:
933 (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
934 try_next = FALSE;
935 break;
936
937 /* Special spacing and line-terminating items. These recognize specific
938 lists of characters. The difference between VSPACE and ANYNL is that the
939 latter can match the two-character CRLF sequence, but that is not
940 relevant for finding the first character, so their code here is
941 identical. */
942
943 case OP_HSPACE:
944 SET_BIT(0x09);
945 SET_BIT(0x20);
946 if (utf8)
947 {
948 SET_BIT(0xC2); /* For U+00A0 */
949 SET_BIT(0xE1); /* For U+1680, U+180E */
950 SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
951 SET_BIT(0xE3); /* For U+3000 */
952 }
953 else SET_BIT(0xA0);
954 try_next = FALSE;
955 break;
956
957 case OP_ANYNL:
958 case OP_VSPACE:
959 SET_BIT(0x0A);
960 SET_BIT(0x0B);
961 SET_BIT(0x0C);
962 SET_BIT(0x0D);
963 if (utf8)
964 {
965 SET_BIT(0xC2); /* For U+0085 */
966 SET_BIT(0xE2); /* For U+2028, U+2029 */
967 }
968 else SET_BIT(0x85);
969 try_next = FALSE;
970 break;
971
972 /* Single character types set the bits and stop. Note that if PCRE_UCP
973 is set, we do not see these op codes because \d etc are converted to
974 properties. Therefore, these apply in the case when only characters less
975 than 256 are recognized to match the types. */
976
977 case OP_NOT_DIGIT:
978 set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
979 try_next = FALSE;
980 break;
981
982 case OP_DIGIT:
983 set_type_bits(start_bits, cbit_digit, table_limit, cd);
984 try_next = FALSE;
985 break;
986
987 /* The cbit_space table has vertical tab as whitespace; we have to
988 ensure it is set as not whitespace. */
989
990 case OP_NOT_WHITESPACE:
991 set_nottype_bits(start_bits, cbit_space, table_limit, cd);
992 start_bits[1] |= 0x08;
993 try_next = FALSE;
994 break;
995
996 /* The cbit_space table has vertical tab as whitespace; we have to
997 not set it from the table. */
998
999 case OP_WHITESPACE:
1000 c = start_bits[1]; /* Save in case it was already set */
1001 set_type_bits(start_bits, cbit_space, table_limit, cd);
1002 start_bits[1] = (start_bits[1] & ~0x08) | c;
1003 try_next = FALSE;
1004 break;
1005
1006 case OP_NOT_WORDCHAR:
1007 set_nottype_bits(start_bits, cbit_word, table_limit, cd);
1008 try_next = FALSE;
1009 break;
1010
1011 case OP_WORDCHAR:
1012 set_type_bits(start_bits, cbit_word, table_limit, cd);
1013 try_next = FALSE;
1014 break;
1015
1016 /* One or more character type fudges the pointer and restarts, knowing
1017 it will hit a single character type and stop there. */
1018
1019 case OP_TYPEPLUS:
1020 case OP_TYPEMINPLUS:
1021 case OP_TYPEPOSPLUS:
1022 tcode++;
1023 break;
1024
1025 case OP_TYPEEXACT:
1026 tcode += 3;
1027 break;
1028
1029 /* Zero or more repeats of character types set the bits and then
1030 try again. */
1031
1032 case OP_TYPEUPTO:
1033 case OP_TYPEMINUPTO:
1034 case OP_TYPEPOSUPTO:
1035 tcode += 2; /* Fall through */
1036
1037 case OP_TYPESTAR:
1038 case OP_TYPEMINSTAR:
1039 case OP_TYPEPOSSTAR:
1040 case OP_TYPEQUERY:
1041 case OP_TYPEMINQUERY:
1042 case OP_TYPEPOSQUERY:
1043 switch(tcode[1])
1044 {
1045 default:
1046 case OP_ANY:
1047 case OP_ALLANY:
1048 return SSB_FAIL;
1049
1050 case OP_HSPACE:
1051 SET_BIT(0x09);
1052 SET_BIT(0x20);
1053 if (utf8)
1054 {
1055 SET_BIT(0xC2); /* For U+00A0 */
1056 SET_BIT(0xE1); /* For U+1680, U+180E */
1057 SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
1058 SET_BIT(0xE3); /* For U+3000 */
1059 }
1060 else SET_BIT(0xA0);
1061 break;
1062
1063 case OP_ANYNL:
1064 case OP_VSPACE:
1065 SET_BIT(0x0A);
1066 SET_BIT(0x0B);
1067 SET_BIT(0x0C);
1068 SET_BIT(0x0D);
1069 if (utf8)
1070 {
1071 SET_BIT(0xC2); /* For U+0085 */
1072 SET_BIT(0xE2); /* For U+2028, U+2029 */
1073 }
1074 else SET_BIT(0x85);
1075 break;
1076
1077 case OP_NOT_DIGIT:
1078 set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
1079 break;
1080
1081 case OP_DIGIT:
1082 set_type_bits(start_bits, cbit_digit, table_limit, cd);
1083 break;
1084
1085 /* The cbit_space table has vertical tab as whitespace; we have to
1086 ensure it gets set as not whitespace. */
1087
1088 case OP_NOT_WHITESPACE:
1089 set_nottype_bits(start_bits, cbit_space, table_limit, cd);
1090 start_bits[1] |= 0x08;
1091 break;
1092
1093 /* The cbit_space table has vertical tab as whitespace; we have to
1094 avoid setting it. */
1095
1096 case OP_WHITESPACE:
1097 c = start_bits[1]; /* Save in case it was already set */
1098 set_type_bits(start_bits, cbit_space, table_limit, cd);
1099 start_bits[1] = (start_bits[1] & ~0x08) | c;
1100 break;
1101
1102 case OP_NOT_WORDCHAR:
1103 set_nottype_bits(start_bits, cbit_word, table_limit, cd);
1104 break;
1105
1106 case OP_WORDCHAR:
1107 set_type_bits(start_bits, cbit_word, table_limit, cd);
1108 break;
1109 }
1110
1111 tcode += 2;
1112 break;
1113
1114 /* Character class where all the information is in a bit map: set the
1115 bits and either carry on or not, according to the repeat count. If it was
1116 a negative class, and we are operating with UTF-8 characters, any byte
1117 with a value >= 0xc4 is a potentially valid starter because it starts a
1118 character with a value > 255. */
1119
1120 case OP_NCLASS:
1121 #ifdef SUPPORT_UTF8
1122 if (utf8)
1123 {
1124 start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
1125 memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
1126 }
1127 #endif
1128 /* Fall through */
1129
1130 case OP_CLASS:
1131 {
1132 tcode++;
1133
1134 /* In UTF-8 mode, the bits in a bit map correspond to character
1135 values, not to byte values. However, the bit map we are constructing is
1136 for byte values. So we have to do a conversion for characters whose
1137 value is > 127. In fact, there are only two possible starting bytes for
1138 characters in the range 128 - 255. */
1139
1140 #ifdef SUPPORT_UTF8
1141 if (utf8)
1142 {
1143 for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];
1144 for (c = 128; c < 256; c++)
1145 {
1146 if ((tcode[c/8] && (1 << (c&7))) != 0)
1147 {
1148 int d = (c >> 6) | 0xc0; /* Set bit for this starter */
1149 start_bits[d/8] |= (1 << (d&7)); /* and then skip on to the */
1150 c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */
1151 }
1152 }
1153 }
1154
1155 /* In non-UTF-8 mode, the two bit maps are completely compatible. */
1156
1157 else
1158 #endif
1159 {
1160 for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
1161 }
1162
1163 /* Advance past the bit map, and act on what follows. For a zero
1164 minimum repeat, continue; otherwise stop processing. */
1165
1166 tcode += 32;
1167 switch (*tcode)
1168 {
1169 case OP_CRSTAR:
1170 case OP_CRMINSTAR:
1171 case OP_CRQUERY:
1172 case OP_CRMINQUERY:
1173 tcode++;
1174 break;
1175
1176 case OP_CRRANGE:
1177 case OP_CRMINRANGE:
1178 if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;
1179 else try_next = FALSE;
1180 break;
1181
1182 default:
1183 try_next = FALSE;
1184 break;
1185 }
1186 }
1187 break; /* End of bitmap class handling */
1188
1189 } /* End of switch */
1190 } /* End of try_next loop */
1191
1192 code += GET(code, 1); /* Advance to next branch */
1193 }
1194 while (*code == OP_ALT);
1195 return yield;
1196 }
1197
1198
1199
1200
1201
1202 /*************************************************
1203 * Study a compiled expression *
1204 *************************************************/
1205
1206 /* This function is handed a compiled expression that it must study to produce
1207 information that will speed up the matching. It returns a pcre_extra block
1208 which then gets handed back to pcre_exec().
1209
1210 Arguments:
1211 re points to the compiled expression
1212 options contains option bits
1213 errorptr points to where to place error messages;
1214 set NULL unless error
1215
1216 Returns: pointer to a pcre_extra block, with study_data filled in and the
1217 appropriate flags set;
1218 NULL on error or if no optimization possible
1219 */
1220
1221 PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
1222 pcre_study(const pcre *external_re, int options, const char **errorptr)
1223 {
1224 int min;
1225 BOOL bits_set = FALSE;
1226 uschar start_bits[32];
1227 pcre_extra *extra = NULL;
1228 pcre_study_data *study;
1229 const uschar *tables;
1230 uschar *code;
1231 compile_data compile_block;
1232 const real_pcre *re = (const real_pcre *)external_re;
1233
1234 *errorptr = NULL;
1235
1236 if (re == NULL || re->magic_number != MAGIC_NUMBER)
1237 {
1238 *errorptr = "argument is not a compiled regular expression";
1239 return NULL;
1240 }
1241
1242 if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
1243 {
1244 *errorptr = "unknown or incorrect option bit(s) set";
1245 return NULL;
1246 }
1247
1248 code = (uschar *)re + re->name_table_offset +
1249 (re->name_count * re->name_entry_size);
1250
1251 /* For an anchored pattern, or an unanchored pattern that has a first char, or
1252 a multiline pattern that matches only at "line starts", there is no point in
1253 seeking a list of starting bytes. */
1254
1255 if ((re->options & PCRE_ANCHORED) == 0 &&
1256 (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1257 {
1258 int rc;
1259
1260 /* Set the character tables in the block that is passed around */
1261
1262 tables = re->tables;
1263 if (tables == NULL)
1264 (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1265 (void *)(&tables));
1266
1267 compile_block.lcc = tables + lcc_offset;
1268 compile_block.fcc = tables + fcc_offset;
1269 compile_block.cbits = tables + cbits_offset;
1270 compile_block.ctypes = tables + ctypes_offset;
1271
1272 /* See if we can find a fixed set of initial characters for the pattern. */
1273
1274 memset(start_bits, 0, 32 * sizeof(uschar));
1275 rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1276 &compile_block);
1277 bits_set = rc == SSB_DONE;
1278 if (rc == SSB_UNKNOWN)
1279 {
1280 *errorptr = "internal error: opcode not recognized";
1281 return NULL;
1282 }
1283 }
1284
1285 /* Find the minimum length of subject string. */
1286
1287 switch(min = find_minlength(code, code, re->options, 0))
1288 {
1289 case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
1290 case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
1291 default: break;
1292 }
1293
1294 /* If a set of starting bytes has been identified, or if the minimum length is
1295 greater than zero, or if JIT optimization has been requested, get a pcre_extra
1296 block and a pcre_study_data block. The study data is put in the latter, which
1297 is pointed to by the former, which may also get additional data set later by
1298 the calling program. At the moment, the size of pcre_study_data is fixed. We
1299 nevertheless save it in a field for returning via the pcre_fullinfo() function
1300 so that if it becomes variable in the future, we don't have to change that
1301 code. */
1302
1303 if (bits_set || min > 0
1304 #ifdef SUPPORT_JIT
1305 || (options & PCRE_STUDY_JIT_COMPILE) != 0
1306 #endif
1307 )
1308 {
1309 extra = (pcre_extra *)(pcre_malloc)
1310 (sizeof(pcre_extra) + sizeof(pcre_study_data));
1311 if (extra == NULL)
1312 {
1313 *errorptr = "failed to get memory";
1314 return NULL;
1315 }
1316
1317 study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
1318 extra->flags = PCRE_EXTRA_STUDY_DATA;
1319 extra->study_data = study;
1320
1321 study->size = sizeof(pcre_study_data);
1322 study->flags = 0;
1323
1324 if (bits_set)
1325 {
1326 study->flags |= PCRE_STUDY_MAPPED;
1327 memcpy(study->start_bits, start_bits, sizeof(start_bits));
1328 }
1329
1330 /* Always set the minlength value in the block, because the JIT compiler
1331 makes use of it. However, don't set the bit unless the length is greater than
1332 zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
1333 checking the zero case. */
1334
1335 if (min > 0)
1336 {
1337 study->flags |= PCRE_STUDY_MINLEN;
1338 study->minlength = min;
1339 }
1340 else study->minlength = 0;
1341
1342 /* If JIT support was compiled and requested, attempt the JIT compilation.
1343 If no starting bytes were found, and the minimum length is zero, and JIT
1344 compilation fails, abandon the extra block and return NULL. */
1345
1346 #ifdef SUPPORT_JIT
1347 extra->executable_jit = NULL;
1348 if ((options & PCRE_STUDY_JIT_COMPILE) != 0) _pcre_jit_compile(re, extra);
1349 if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0)
1350 {
1351 pcre_free_study(extra);
1352 extra = NULL;
1353 }
1354 #endif
1355 }
1356
1357 return extra;
1358 }
1359
1360
1361 /*************************************************
1362 * Free the study data *
1363 *************************************************/
1364
1365 /* This function frees the memory that was obtained by pcre_study().
1366
1367 Argument: a pointer to the pcre_extra block
1368 Returns: nothing
1369 */
1370
1371 PCRE_EXP_DEFN void
1372 pcre_free_study(pcre_extra *extra)
1373 {
1374 #ifdef SUPPORT_JIT
1375 if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
1376 extra->executable_jit != NULL)
1377 _pcre_jit_free(extra->executable_jit);
1378 #endif
1379 pcre_free(extra);
1380 }
1381
1382 /* End of pcre_study.c */

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5