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

Contents of /code/trunk/pcre_study.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 613 - (hide annotations) (download)
Sat Jul 2 16:59:52 2011 UTC (3 years ago) by ph10
File MIME type: text/plain
File size: 37255 byte(s)
Fix problem with the interaction of (*ACCEPT) in an assertion with 
PCRE_NOTEMPTY.

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