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

Contents of /code/trunk/pcre_study.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 697 - (hide annotations) (download)
Mon Sep 19 12:28:24 2011 UTC (3 years ago) by ph10
File MIME type: text/plain
File size: 38891 byte(s)
Fix minimum length bug with *ACCEPT.

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