/[pcre]/code/branches/pcre16/pcre_study.c
ViewVC logotype

Contents of /code/branches/pcre16/pcre_study.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (hide annotations) (download)
Thu Jun 2 19:04:54 2011 UTC (2 years, 10 months ago) by ph10
Original Path: code/trunk/pcre_study.c
File MIME type: text/plain
File size: 34716 byte(s)
Refactoring to reduce stack usage for possessively quantified subpatterns. Also 
fixed a number of bugs related to repeated subpatterns. Some further tidies 
consequent on the removal of OP_OPT are also in this patch.

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