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

Contents of /code/trunk/pcre_study.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1488 - (hide annotations) (download)
Wed Jun 18 18:38:00 2014 UTC (5 weeks, 6 days ago) by ph10
File MIME type: text/plain
File size: 47723 byte(s)
Recognize characters with multiple other cases when creating starting bit map.

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