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