/[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 - (show annotations)
Sat Dec 10 02:20:06 2011 UTC (3 years, 6 months ago) by zherczeg
File MIME type: text/plain
File size: 41828 byte(s)
Error occurred while calculating annotation data.
extending the 16 bit API, mode check, and fixes
1 /*************************************************
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 Copyright (c) 1997-2010 University of Cambridge
10
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 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include "pcre_internal.h"
50
51 #define SET_BIT(c) start_bits[c/8] |= (1 << (c&7))
52
53 /* Returns from set_start_bits() */
54
55 enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN };
56
57
58
59 /*************************************************
60 * Find the minimum subject length for a group *
61 *************************************************/
62
63 /* Scan a parenthesized group and compute the minimum length of subject that
64 is needed to match it. This is a lower bound; it does not mean there is a
65 string of that length that matches. In UTF8 mode, the result is in characters
66 rather than bytes.
67
68 Arguments:
69 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
74 Returns: the minimum length
75 -1 if \C in UTF-8 mode or (*ACCEPT) was encountered
76 -2 internal error (missing capturing bracket)
77 -3 internal error (opcode not listed)
78 */
79
80 static int
81 find_minlength(const pcre_uchar *code, const pcre_uchar *startcode, int options,
82 int recurse_depth)
83 {
84 int length = -1;
85 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
86 BOOL utf = (options & PCRE_UTF8) != 0;
87 BOOL had_recurse = FALSE;
88 register int branchlength = 0;
89 register pcre_uchar *cc = (pcre_uchar *)code + 1 + LINK_SIZE;
90
91 if (*code == OP_CBRA || *code == OP_SCBRA ||
92 *code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += IMM2_SIZE;
93
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 pcre_uchar *cs, *ce;
101 register int op = *cc;
102
103 switch (op)
104 {
105 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 case OP_CBRA:
123 case OP_SCBRA:
124 case OP_BRA:
125 case OP_SBRA:
126 case OP_CBRAPOS:
127 case OP_SCBRAPOS:
128 case OP_BRAPOS:
129 case OP_SBRAPOS:
130 case OP_ONCE:
131 case OP_ONCE_NC:
132 d = find_minlength(cc, startcode, options, recurse_depth);
133 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 /* ACCEPT makes things far too complicated; we have to give up. */
140
141 case OP_ACCEPT:
142 case OP_ASSERT_ACCEPT:
143 return -1;
144
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 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 time, and pass back the shortest ACCEPT length. */
150
151 case OP_ALT:
152 case OP_KET:
153 case OP_KETRMAX:
154 case OP_KETRMIN:
155 case OP_KETRPOS:
156 case OP_END:
157 if (length < 0 || (!had_recurse && branchlength < length))
158 length = branchlength;
159 if (op != OP_ALT) return length;
160 cc += 1 + LINK_SIZE;
161 branchlength = 0;
162 had_recurse = FALSE;
163 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 case OP_NCREF:
179 case OP_RREF:
180 case OP_NRREF:
181 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 case OP_CIRCM:
189 case OP_DOLL:
190 case OP_DOLLM:
191 case OP_NOT_WORD_BOUNDARY:
192 case OP_WORD_BOUNDARY:
193 cc += PRIV(OP_lengths)[*cc];
194 break;
195
196 /* Skip over a subpattern that has a {0} or {0,x} quantifier */
197
198 case OP_BRAZERO:
199 case OP_BRAMINZERO:
200 case OP_BRAPOSZERO:
201 case OP_SKIPZERO:
202 cc += PRIV(OP_lengths)[*cc];
203 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 case OP_CHARI:
211 case OP_NOT:
212 case OP_NOTI:
213 case OP_PLUS:
214 case OP_PLUSI:
215 case OP_MINPLUS:
216 case OP_MINPLUSI:
217 case OP_POSPLUS:
218 case OP_POSPLUSI:
219 case OP_NOTPLUS:
220 case OP_NOTPLUSI:
221 case OP_NOTMINPLUS:
222 case OP_NOTMINPLUSI:
223 case OP_NOTPOSPLUS:
224 case OP_NOTPOSPLUSI:
225 branchlength++;
226 cc += 2;
227 #ifdef SUPPORT_UTF
228 if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
229 #endif
230 break;
231
232 case OP_TYPEPLUS:
233 case OP_TYPEMINPLUS:
234 case OP_TYPEPOSPLUS:
235 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 case OP_EXACTI:
244 case OP_NOTEXACT:
245 case OP_NOTEXACTI:
246 branchlength += GET2(cc,1);
247 cc += 2 + IMM2_SIZE;
248 #ifdef SUPPORT_UTF
249 if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
250 #endif
251 break;
252
253 case OP_TYPEEXACT:
254 branchlength += GET2(cc,1);
255 cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
256 || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
257 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 case OP_HSPACE:
276 case OP_NOT_HSPACE:
277 case OP_VSPACE:
278 case OP_NOT_VSPACE:
279 branchlength++;
280 cc++;
281 break;
282
283 /* "Any newline" might match two characters, but it also might match just
284 one. */
285
286 case OP_ANYNL:
287 branchlength += 1;
288 cc++;
289 break;
290
291 /* 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
295 case OP_ANYBYTE:
296 #ifdef SUPPORT_UTF
297 if (utf) return -1;
298 #endif
299 branchlength++;
300 cc++;
301 break;
302
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 cc += PRIV(OP_lengths)[op];
314 break;
315
316 case OP_TYPEUPTO:
317 case OP_TYPEMINUPTO:
318 case OP_TYPEPOSUPTO:
319 if (cc[1 + IMM2_SIZE] == OP_PROP
320 || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
321 cc += PRIV(OP_lengths)[op];
322 break;
323
324 /* Check a class for variable quantification */
325
326 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
327 case OP_XCLASS:
328 cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];
329 /* Fall through */
330 #endif
331
332 case OP_CLASS:
333 case OP_NCLASS:
334 cc += PRIV(OP_lengths)[OP_CLASS];
335
336 switch (*cc)
337 {
338 case OP_CRPLUS:
339 case OP_CRMINPLUS:
340 branchlength++;
341 /* Fall through */
342
343 case OP_CRSTAR:
344 case OP_CRMINSTAR:
345 case OP_CRQUERY:
346 case OP_CRMINQUERY:
347 cc++;
348 break;
349
350 case OP_CRRANGE:
351 case OP_CRMINRANGE:
352 branchlength += GET2(cc,1);
353 cc += 1 + 2 * IMM2_SIZE;
354 break;
355
356 default:
357 branchlength++;
358 break;
359 }
360 break;
361
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 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 referencing behaves in the same way.
369
370 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
374 case OP_REF:
375 case OP_REFI:
376 if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
377 {
378 ce = cs = (pcre_uchar *)PRIV(find_bracket)(startcode, utf, GET2(cc, 1));
379 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 else
387 {
388 d = find_minlength(cs, startcode, options, recurse_depth);
389 }
390 }
391 else d = 0;
392 cc += 1 + IMM2_SIZE;
393
394 /* Handle repeated back references */
395
396 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
406 case OP_CRPLUS:
407 case OP_CRMINPLUS:
408 min = 1;
409 cc++;
410 break;
411
412 case OP_CRRANGE:
413 case OP_CRMINRANGE:
414 min = GET2(cc, 1);
415 cc += 1 + 2 * IMM2_SIZE;
416 break;
417
418 default:
419 min = 1;
420 break;
421 }
422
423 branchlength += min * d;
424 break;
425
426 /* We can easily detect direct recursion, but not mutual recursion. This is
427 caught by a recursion depth count. */
428
429 case OP_RECURSE:
430 cs = ce = (pcre_uchar *)startcode + GET(cc, 1);
431 do ce += GET(ce, 1); while (*ce == OP_ALT);
432 if ((cc > cs && cc < ce) || recurse_depth > 10)
433 had_recurse = TRUE;
434 else
435 {
436 branchlength += find_minlength(cs, startcode, options, recurse_depth + 1);
437 }
438 cc += 1 + LINK_SIZE;
439 break;
440
441 /* Anything else does not or need not match a character. We can get the
442 item's length from the table, but for those that can match zero occurrences
443 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 future that may change, so we include them here so as not to leave a
447 gotcha for a future maintainer. */
448
449 case OP_UPTO:
450 case OP_UPTOI:
451 case OP_NOTUPTO:
452 case OP_NOTUPTOI:
453 case OP_MINUPTO:
454 case OP_MINUPTOI:
455 case OP_NOTMINUPTO:
456 case OP_NOTMINUPTOI:
457 case OP_POSUPTO:
458 case OP_POSUPTOI:
459 case OP_NOTPOSUPTO:
460 case OP_NOTPOSUPTOI:
461
462 case OP_STAR:
463 case OP_STARI:
464 case OP_NOTSTAR:
465 case OP_NOTSTARI:
466 case OP_MINSTAR:
467 case OP_MINSTARI:
468 case OP_NOTMINSTAR:
469 case OP_NOTMINSTARI:
470 case OP_POSSTAR:
471 case OP_POSSTARI:
472 case OP_NOTPOSSTAR:
473 case OP_NOTPOSSTARI:
474
475 case OP_QUERY:
476 case OP_QUERYI:
477 case OP_NOTQUERY:
478 case OP_NOTQUERYI:
479 case OP_MINQUERY:
480 case OP_MINQUERYI:
481 case OP_NOTMINQUERY:
482 case OP_NOTMINQUERYI:
483 case OP_POSQUERY:
484 case OP_POSQUERYI:
485 case OP_NOTPOSQUERY:
486 case OP_NOTPOSQUERYI:
487
488 cc += PRIV(OP_lengths)[op];
489 #ifdef SUPPORT_UTF
490 if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
491 #endif
492 break;
493
494 /* Skip these, but we need to add in the name length. */
495
496 case OP_MARK:
497 case OP_PRUNE_ARG:
498 case OP_SKIP_ARG:
499 case OP_THEN_ARG:
500 cc += PRIV(OP_lengths)[op] + cc[1];
501 break;
502
503 /* The remaining opcodes are just skipped over. */
504
505 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 case OP_THEN:
512 cc += PRIV(OP_lengths)[op];
513 break;
514
515 /* 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 }
521 }
522 /* Control never gets here */
523 }
524
525
526
527 /*************************************************
528 * Set a bit and maybe its alternate case *
529 *************************************************/
530
531 /* Given a character, set its first byte's bit in the table, and also the
532 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
536 Arguments:
537 start_bits points to the bit map
538 p points to the character
539 caseless the caseless flag
540 cd the block with char table pointers
541 utf TRUE for UTF-8 / UTF-16 mode
542
543 Returns: pointer after the character
544 */
545
546 static const pcre_uchar *
547 set_table_bit(pcre_uint8 *start_bits, const pcre_uchar *p, BOOL caseless,
548 compile_data *cd, BOOL utf)
549 {
550 unsigned int c = *p;
551
552 #ifdef COMPILE_PCRE8
553 SET_BIT(c);
554
555 #ifdef SUPPORT_UTF
556 if (utf && c > 127)
557 {
558 GETCHARINC(c, p);
559 #ifdef SUPPORT_UCP
560 if (caseless)
561 {
562 pcre_uchar buff[6];
563 c = UCD_OTHERCASE(c);
564 (void)PRIV(ord2utf)(c, buff);
565 SET_BIT(buff[0]);
566 }
567 #endif
568 return p;
569 }
570 #endif
571
572 /* Not UTF-8 mode, or character is less than 127. */
573
574 if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
575 return p + 1;
576 #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 }
604
605
606
607 /*************************************************
608 * 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 cd the block with char table pointers
623
624 Returns: nothing
625 */
626
627 static void
628 set_type_bits(pcre_uint8 *start_bits, int cbit_type, int table_limit,
629 compile_data *cd)
630 {
631 register int c;
632 for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
633 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
634 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 pcre_uchar buff[6];
640 (void)PRIV(ord2utf)(c, buff);
641 SET_BIT(buff[0]);
642 }
643 }
644 #endif
645 }
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 Unlike in the positive case, where we can set appropriate starting bits for
656 specific high-valued UTF-8 characters, in this case we have to set the bits for
657 all high-valued characters. The lowest is 0xc2, but we overkill by starting at
658 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 cd the block with char table pointers
665
666 Returns: nothing
667 */
668
669 static void
670 set_nottype_bits(pcre_uint8 *start_bits, int cbit_type, int table_limit,
671 compile_data *cd)
672 {
673 register int c;
674 for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
675 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
676 if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
677 #endif
678 }
679
680
681
682 /*************************************************
683 * Create bitmap of starting bytes *
684 *************************************************/
685
686 /* 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
694 Arguments:
695 code points to an expression
696 start_bits points to a 32-byte table, initialized to 0
697 utf TRUE if in UTF-8 / UTF-16 mode
698 cd the block with char table pointers
699
700 Returns: SSB_FAIL => Failed to find any starting bytes
701 SSB_DONE => Found mandatory starting bytes
702 SSB_CONTINUE => Found optional starting bytes
703 SSB_UNKNOWN => Hit an unrecognized opcode
704 */
705
706 static int
707 set_start_bits(const pcre_uchar *code, pcre_uint8 *start_bits, BOOL utf,
708 compile_data *cd)
709 {
710 register int c;
711 int yield = SSB_DONE;
712 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
713 int table_limit = utf? 16:32;
714 #else
715 int table_limit = 32;
716 #endif
717
718 #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 /* 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 /* ========================================================================= */
733 #endif
734
735 do
736 {
737 BOOL try_next = TRUE;
738 const pcre_uchar *tcode = code + 1 + LINK_SIZE;
739
740 if (*code == OP_CBRA || *code == OP_SCBRA ||
741 *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
742
743 while (try_next) /* Loop for items in this branch */
744 {
745 int rc;
746
747 switch(*tcode)
748 {
749 /* 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
753 default:
754 return SSB_UNKNOWN;
755
756 /* Fail for a valid opcode that implies no starting bits. */
757
758 case OP_ACCEPT:
759 case OP_ASSERT_ACCEPT:
760 case OP_ALLANY:
761 case OP_ANY:
762 case OP_ANYBYTE:
763 case OP_CIRC:
764 case OP_CIRCM:
765 case OP_CLOSE:
766 case OP_COMMIT:
767 case OP_COND:
768 case OP_CREF:
769 case OP_DEF:
770 case OP_DOLL:
771 case OP_DOLLM:
772 case OP_END:
773 case OP_EOD:
774 case OP_EODN:
775 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 case OP_NOTI:
783 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 case OP_SCOND:
820 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 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
828 case OP_XCLASS:
829 #endif
830 return SSB_FAIL;
831
832 /* We can ignore word boundary tests. */
833
834 case OP_WORD_BOUNDARY:
835 case OP_NOT_WORD_BOUNDARY:
836 tcode++;
837 break;
838
839 /* 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 case OP_BRAPOS:
849 case OP_SBRAPOS:
850 case OP_CBRAPOS:
851 case OP_SCBRAPOS:
852 case OP_ONCE:
853 case OP_ONCE_NC:
854 case OP_ASSERT:
855 rc = set_start_bits(tcode, start_bits, utf, cd);
856 if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
857 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 case OP_KETRPOS:
880 return SSB_CONTINUE;
881
882 /* 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 tcode += 1 + LINK_SIZE;
895 break;
896
897 /* BRAZERO does the bracket, but carries on. */
898
899 case OP_BRAZERO:
900 case OP_BRAMINZERO:
901 case OP_BRAPOSZERO:
902 rc = set_start_bits(++tcode, start_bits, utf, cd);
903 if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
904 /* =========================================================================
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 dummy = 1;
908 ========================================================================= */
909 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
910 tcode += 1 + LINK_SIZE;
911 break;
912
913 /* SKIPZERO skips the bracket. */
914
915 case OP_SKIPZERO:
916 tcode++;
917 do tcode += GET(tcode,1); while (*tcode == OP_ALT);
918 tcode += 1 + LINK_SIZE;
919 break;
920
921 /* Single-char * or ? sets the bit and tries the next item */
922
923 case OP_STAR:
924 case OP_MINSTAR:
925 case OP_POSSTAR:
926 case OP_QUERY:
927 case OP_MINQUERY:
928 case OP_POSQUERY:
929 tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf);
930 break;
931
932 case OP_STARI:
933 case OP_MINSTARI:
934 case OP_POSSTARI:
935 case OP_QUERYI:
936 case OP_MINQUERYI:
937 case OP_POSQUERYI:
938 tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf);
939 break;
940
941 /* Single-char upto sets the bit and tries the next */
942
943 case OP_UPTO:
944 case OP_MINUPTO:
945 case OP_POSUPTO:
946 tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, FALSE, cd, utf);
947 break;
948
949 case OP_UPTOI:
950 case OP_MINUPTOI:
951 case OP_POSUPTOI:
952 tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, TRUE, cd, utf);
953 break;
954
955 /* At least one single char sets the bit and stops */
956
957 case OP_EXACT:
958 tcode += IMM2_SIZE;
959 /* Fall through */
960 case OP_CHAR:
961 case OP_PLUS:
962 case OP_MINPLUS:
963 case OP_POSPLUS:
964 (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf);
965 try_next = FALSE;
966 break;
967
968 case OP_EXACTI:
969 tcode += IMM2_SIZE;
970 /* Fall through */
971 case OP_CHARI:
972 case OP_PLUSI:
973 case OP_MINPLUSI:
974 case OP_POSPLUSI:
975 (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf);
976 try_next = FALSE;
977 break;
978
979 /* 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 identical. */
984
985 case OP_HSPACE:
986 SET_BIT(0x09);
987 SET_BIT(0x20);
988 #ifdef SUPPORT_UTF
989 if (utf)
990 {
991 #ifdef COMPILE_PCRE8
992 SET_BIT(0xC2); /* For U+00A0 */
993 SET_BIT(0xE1); /* For U+1680, U+180E */
994 SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
995 SET_BIT(0xE3); /* For U+3000 */
996 #endif
997 #ifdef COMPILE_PCRE16
998 SET_BIT(0xA0);
999 SET_BIT(0xFF); /* For characters > 255 */
1000 #endif
1001 }
1002 else
1003 #endif /* SUPPORT_UTF */
1004 SET_BIT(0xA0);
1005 try_next = FALSE;
1006 break;
1007
1008 case OP_ANYNL:
1009 case OP_VSPACE:
1010 SET_BIT(0x0A);
1011 SET_BIT(0x0B);
1012 SET_BIT(0x0C);
1013 SET_BIT(0x0D);
1014 #ifdef SUPPORT_UTF
1015 if (utf)
1016 {
1017 #ifdef COMPILE_PCRE8
1018 SET_BIT(0xC2); /* For U+0085 */
1019 SET_BIT(0xE2); /* For U+2028, U+2029 */
1020 #endif
1021 #ifdef COMPILE_PCRE16
1022 SET_BIT(0x85);
1023 SET_BIT(0xFF); /* For characters > 255 */
1024 #endif
1025 }
1026 else
1027 #endif /* SUPPORT_UTF */
1028 SET_BIT(0x85);
1029 try_next = FALSE;
1030 break;
1031
1032 /* 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 properties. Therefore, these apply in the case when only characters less
1035 than 256 are recognized to match the types. */
1036
1037 case OP_NOT_DIGIT:
1038 set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
1039 try_next = FALSE;
1040 break;
1041
1042 case OP_DIGIT:
1043 set_type_bits(start_bits, cbit_digit, table_limit, cd);
1044 try_next = FALSE;
1045 break;
1046
1047 /* The cbit_space table has vertical tab as whitespace; we have to
1048 ensure it is set as not whitespace. */
1049
1050 case OP_NOT_WHITESPACE:
1051 set_nottype_bits(start_bits, cbit_space, table_limit, cd);
1052 start_bits[1] |= 0x08;
1053 try_next = FALSE;
1054 break;
1055
1056 /* The cbit_space table has vertical tab as whitespace; we have to
1057 not set it from the table. */
1058
1059 case OP_WHITESPACE:
1060 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 try_next = FALSE;
1064 break;
1065
1066 case OP_NOT_WORDCHAR:
1067 set_nottype_bits(start_bits, cbit_word, table_limit, cd);
1068 try_next = FALSE;
1069 break;
1070
1071 case OP_WORDCHAR:
1072 set_type_bits(start_bits, cbit_word, table_limit, cd);
1073 try_next = FALSE;
1074 break;
1075
1076 /* 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 case OP_TYPEPOSPLUS:
1082 tcode++;
1083 break;
1084
1085 case OP_TYPEEXACT:
1086 tcode += 1 + IMM2_SIZE;
1087 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 case OP_TYPEPOSUPTO:
1095 tcode += IMM2_SIZE; /* Fall through */
1096
1097 case OP_TYPESTAR:
1098 case OP_TYPEMINSTAR:
1099 case OP_TYPEPOSSTAR:
1100 case OP_TYPEQUERY:
1101 case OP_TYPEMINQUERY:
1102 case OP_TYPEPOSQUERY:
1103 switch(tcode[1])
1104 {
1105 default:
1106 case OP_ANY:
1107 case OP_ALLANY:
1108 return SSB_FAIL;
1109
1110 case OP_HSPACE:
1111 SET_BIT(0x09);
1112 SET_BIT(0x20);
1113 #ifdef COMPILE_PCRE8
1114 if (utf)
1115 {
1116 #ifdef COMPILE_PCRE8
1117 SET_BIT(0xC2); /* For U+00A0 */
1118 SET_BIT(0xE1); /* For U+1680, U+180E */
1119 SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */
1120 SET_BIT(0xE3); /* For U+3000 */
1121 #endif
1122 #ifdef COMPILE_PCRE16
1123 SET_BIT(0xA0);
1124 SET_BIT(0xFF); /* For characters > 255 */
1125 #endif
1126 }
1127 else
1128 #endif /* SUPPORT_UTF */
1129 SET_BIT(0xA0);
1130 break;
1131
1132 case OP_ANYNL:
1133 case OP_VSPACE:
1134 SET_BIT(0x0A);
1135 SET_BIT(0x0B);
1136 SET_BIT(0x0C);
1137 SET_BIT(0x0D);
1138 #ifdef COMPILE_PCRE8
1139 if (utf)
1140 {
1141 #ifdef COMPILE_PCRE8
1142 SET_BIT(0xC2); /* For U+0085 */
1143 SET_BIT(0xE2); /* For U+2028, U+2029 */
1144 #endif
1145 #ifdef COMPILE_PCRE16
1146 SET_BIT(0x85);
1147 SET_BIT(0xFF); /* For characters > 255 */
1148 #endif
1149 }
1150 else
1151 #endif /* SUPPORT_UTF */
1152 SET_BIT(0x85);
1153 break;
1154
1155 case OP_NOT_DIGIT:
1156 set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
1157 break;
1158
1159 case OP_DIGIT:
1160 set_type_bits(start_bits, cbit_digit, table_limit, cd);
1161 break;
1162
1163 /* The cbit_space table has vertical tab as whitespace; we have to
1164 ensure it gets set as not whitespace. */
1165
1166 case OP_NOT_WHITESPACE:
1167 set_nottype_bits(start_bits, cbit_space, table_limit, cd);
1168 start_bits[1] |= 0x08;
1169 break;
1170
1171 /* The cbit_space table has vertical tab as whitespace; we have to
1172 avoid setting it. */
1173
1174 case OP_WHITESPACE:
1175 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 break;
1179
1180 case OP_NOT_WORDCHAR:
1181 set_nottype_bits(start_bits, cbit_word, table_limit, cd);
1182 break;
1183
1184 case OP_WORDCHAR:
1185 set_type_bits(start_bits, cbit_word, table_limit, cd);
1186 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 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
1200 if (utf)
1201 {
1202 start_bits[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */
1203 memset(start_bits+25, 0xff, 7); /* Bits for 0xc9 - 0xff */
1204 }
1205 #endif
1206 #ifdef COMPILE_PCRE16
1207 SET_BIT(0xFF); /* For characters > 255 */
1208 #endif
1209 /* Fall through */
1210
1211 case OP_CLASS:
1212 {
1213 pcre_uint8 *map;
1214 tcode++;
1215 map = (pcre_uint8 *)tcode;
1216
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 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
1224 if (utf)
1225 {
1226 for (c = 0; c < 16; c++) start_bits[c] |= map[c];
1227 for (c = 128; c < 256; c++)
1228 {
1229 if ((map[c/8] && (1 << (c&7))) != 0)
1230 {
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 #endif
1239 {
1240 /* In non-UTF-8 mode, the two bit maps are completely compatible. */
1241 for (c = 0; c < 32; c++) start_bits[c] |= map[c];
1242 }
1243
1244 /* Advance past the bit map, and act on what follows. For a zero
1245 minimum repeat, continue; otherwise stop processing. */
1246
1247 tcode += 32 / sizeof(pcre_uchar);
1248 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 if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
1260 else try_next = FALSE;
1261 break;
1262
1263 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 return yield;
1277 }
1278
1279
1280
1281
1282
1283 /*************************************************
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 appropriate flags set;
1299 NULL on error or if no optimization possible
1300 */
1301
1302 #ifdef COMPILE_PCRE8
1303 PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
1304 pcre_study(const pcre *external_re, int options, const char **errorptr)
1305 #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 {
1310 int min;
1311 BOOL bits_set = FALSE;
1312 pcre_uint8 start_bits[32];
1313 pcre_extra *extra = NULL;
1314 pcre_study_data *study;
1315 const pcre_uint8 *tables;
1316 pcre_uchar *code;
1317 compile_data compile_block;
1318 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 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 if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
1339 {
1340 *errorptr = "unknown or incorrect option bit(s) set";
1341 return NULL;
1342 }
1343
1344 code = (pcre_uchar *)re + re->name_table_offset +
1345 (re->name_count * re->name_entry_size);
1346
1347 /* For an anchored pattern, or an unanchored pattern that has a first char, or
1348 a multiline pattern that matches only at "line starts", there is no point in
1349 seeking a list of starting bytes. */
1350
1351 if ((re->options & PCRE_ANCHORED) == 0 &&
1352 (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1353 {
1354 int rc;
1355
1356 /* Set the character tables in the block that is passed around */
1357
1358 tables = re->tables;
1359
1360 #ifdef COMPILE_PCRE8
1361 if (tables == NULL)
1362 (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1363 (void *)(&tables));
1364 #else
1365 if (tables == NULL)
1366 (void)pcre16_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1367 (void *)(&tables));
1368 #endif
1369
1370 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
1375 /* See if we can find a fixed set of initial characters for the pattern. */
1376
1377 memset(start_bits, 0, 32 * sizeof(pcre_uint8));
1378 rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1379 &compile_block);
1380 bits_set = rc == SSB_DONE;
1381 if (rc == SSB_UNKNOWN)
1382 {
1383 *errorptr = "internal error: opcode not recognized";
1384 return NULL;
1385 }
1386 }
1387
1388 /* Find the minimum length of subject string. */
1389
1390 switch(min = find_minlength(code, code, re->options, 0))
1391 {
1392 case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
1393 case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
1394 default: break;
1395 }
1396
1397 /* 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
1406 if (bits_set || min > 0
1407 #ifdef SUPPORT_JIT
1408 || (options & PCRE_STUDY_JIT_COMPILE) != 0
1409 #endif
1410 )
1411 {
1412 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
1420 study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
1421 extra->flags = PCRE_EXTRA_STUDY_DATA;
1422 extra->study_data = study;
1423
1424 study->size = sizeof(pcre_study_data);
1425 study->flags = 0;
1426
1427 if (bits_set)
1428 {
1429 study->flags |= PCRE_STUDY_MAPPED;
1430 memcpy(study->start_bits, start_bits, sizeof(start_bits));
1431 }
1432
1433 #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 /* 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 checking the zero case. */
1449
1450 if (min > 0)
1451 {
1452 study->flags |= PCRE_STUDY_MINLEN;
1453 study->minlength = min;
1454 }
1455 else study->minlength = 0;
1456
1457 /* 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 compilation fails, abandon the extra block and return NULL. */
1460
1461 #ifdef SUPPORT_JIT
1462 extra->executable_jit = NULL;
1463 if ((options & PCRE_STUDY_JIT_COMPILE) != 0) PRIV(jit_compile)(re, extra);
1464 if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0)
1465 {
1466 pcre_free_study(extra);
1467 extra = NULL;
1468 }
1469 #endif
1470 }
1471
1472 return extra;
1473 }
1474
1475
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 #ifdef COMPILE_PCRE8
1487 PCRE_EXP_DEFN void
1488 pcre_free_study(pcre_extra *extra)
1489 #else
1490 PCRE_EXP_DEFN void
1491 pcre16_free_study(pcre_extra *extra)
1492 #endif
1493 {
1494 #ifdef SUPPORT_JIT
1495 if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
1496 extra->executable_jit != NULL)
1497 PRIV(jit_free)(extra->executable_jit);
1498 #endif
1499 pcre_free(extra);
1500 }
1501
1502 /* End of pcre_study.c */

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

  ViewVC Help
Powered by ViewVC 1.1.5