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

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

Parent Directory Parent Directory | Revision Log Revision Log


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