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