/src/pcre2/src/pcre2_study.c
Line | Count | Source |
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 | | Original API code Copyright (c) 1997-2012 University of Cambridge |
10 | | New API code Copyright (c) 2016-2024 University of Cambridge |
11 | | |
12 | | ----------------------------------------------------------------------------- |
13 | | Redistribution and use in source and binary forms, with or without |
14 | | modification, are permitted provided that the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the University of Cambridge nor the names of its |
24 | | contributors may be used to endorse or promote products derived from |
25 | | this software without specific prior written permission. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
28 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
29 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
30 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
31 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
32 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
33 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
34 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
35 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
36 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
37 | | POSSIBILITY OF SUCH DAMAGE. |
38 | | ----------------------------------------------------------------------------- |
39 | | */ |
40 | | |
41 | | |
42 | | /* This module contains functions for scanning a compiled pattern and |
43 | | collecting data (e.g. minimum matching length). */ |
44 | | |
45 | | |
46 | | #include "pcre2_internal.h" |
47 | | |
48 | | |
49 | | |
50 | | /* The maximum remembered capturing brackets minimum. */ |
51 | | |
52 | 32.0k | #define MAX_CACHE_BACKREF 128 |
53 | | |
54 | | /* Set a bit in the starting code unit bit map. */ |
55 | | |
56 | 538k | #define SET_BIT(c) re->start_bitmap[(c)/8] |= (1u << ((c)&7)) |
57 | | |
58 | | /* Returns from set_start_bits() */ |
59 | | |
60 | | enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN, SSB_TOODEEP }; |
61 | | |
62 | | |
63 | | /************************************************* |
64 | | * Find the minimum subject length for a group * |
65 | | *************************************************/ |
66 | | |
67 | | /* Scan a parenthesized group and compute the minimum length of subject that |
68 | | is needed to match it. This is a lower bound; it does not mean there is a |
69 | | string of that length that matches. In UTF mode, the result is in characters |
70 | | rather than code units. The field in a compiled pattern for storing the minimum |
71 | | length is 16-bits long (on the grounds that anything longer than that is |
72 | | pathological), so we give up when we reach that amount. This also means that |
73 | | integer overflow for really crazy patterns cannot happen. |
74 | | |
75 | | Backreference minimum lengths are cached to speed up multiple references. This |
76 | | function is called only when the highest back reference in the pattern is less |
77 | | than or equal to MAX_CACHE_BACKREF, which is one less than the size of the |
78 | | caching vector. The zeroth element contains the number of the highest set |
79 | | value. |
80 | | |
81 | | Arguments: |
82 | | re compiled pattern block |
83 | | code pointer to start of group (the bracket) |
84 | | startcode pointer to start of the whole pattern's code |
85 | | utf UTF flag |
86 | | recurses chain of recurse_check to catch mutual recursion |
87 | | countptr pointer to call count (to catch over complexity) |
88 | | backref_cache vector for caching back references. |
89 | | |
90 | | This function is no longer called when the pattern contains (*ACCEPT); however, |
91 | | the old code for returning -1 is retained, just in case. |
92 | | |
93 | | Returns: the minimum length |
94 | | -1 \C in UTF-8 mode |
95 | | or (*ACCEPT) |
96 | | or pattern too complicated |
97 | | -2 internal error (missing capturing bracket) |
98 | | -3 internal error (opcode not listed) |
99 | | */ |
100 | | |
101 | | static int |
102 | | find_minlength(const pcre2_real_code *re, PCRE2_SPTR code, |
103 | | PCRE2_SPTR startcode, BOOL utf, recurse_check *recurses, int *countptr, |
104 | | int *backref_cache) |
105 | 369k | { |
106 | 369k | int length = -1; |
107 | 369k | int branchlength = 0; |
108 | 369k | int prev_cap_recno = -1; |
109 | 369k | int prev_cap_d = 0; |
110 | 369k | int prev_recurse_recno = -1; |
111 | 369k | int prev_recurse_d = 0; |
112 | 369k | uint32_t once_fudge = 0; |
113 | 369k | BOOL had_recurse = FALSE; |
114 | 369k | BOOL dupcapused = (re->flags & PCRE2_DUPCAPUSED) != 0; |
115 | 369k | PCRE2_SPTR nextbranch = code + GET(code, 1); |
116 | 369k | PCRE2_SPTR cc = code + 1 + LINK_SIZE; |
117 | 369k | recurse_check this_recurse; |
118 | | |
119 | | /* If this is a "could be empty" group, its minimum length is 0. */ |
120 | | |
121 | 369k | if (*code >= OP_SBRA && *code <= OP_SCOND) return 0; |
122 | | |
123 | | /* Skip over capturing bracket number */ |
124 | | |
125 | 363k | if (*code == OP_CBRA || *code == OP_CBRAPOS) cc += IMM2_SIZE; |
126 | | |
127 | | /* A large and/or complex regex can take too long to process. */ |
128 | | |
129 | 363k | if ((*countptr)++ > 1000) return -1; |
130 | | |
131 | | /* Scan along the opcodes for this branch. If we get to the end of the branch, |
132 | | check the length against that of the other branches. If the accumulated length |
133 | | passes 16-bits, reset to that value and skip the rest of the branch. */ |
134 | | |
135 | 363k | for (;;) |
136 | 11.9M | { |
137 | 11.9M | int d, min, recno; |
138 | 11.9M | PCRE2_UCHAR op; |
139 | 11.9M | PCRE2_SPTR cs, ce; |
140 | | |
141 | 11.9M | if (branchlength >= (int)UINT16_MAX) |
142 | 1.58k | { |
143 | 1.58k | branchlength = UINT16_MAX; |
144 | 1.58k | cc = nextbranch; |
145 | 1.58k | } |
146 | | |
147 | 11.9M | op = *cc; |
148 | 11.9M | switch (op) |
149 | 11.9M | { |
150 | 10.3k | case OP_COND: |
151 | 14.4k | case OP_SCOND: |
152 | | |
153 | | /* If there is only one branch in a condition, the implied branch has zero |
154 | | length, so we don't add anything. This covers the DEFINE "condition" |
155 | | automatically. If there are two branches we can treat it the same as any |
156 | | other non-capturing subpattern. */ |
157 | | |
158 | 14.4k | cs = cc + GET(cc, 1); |
159 | 14.4k | if (*cs != OP_ALT) |
160 | 11.5k | { |
161 | 11.5k | cc = cs + 1 + LINK_SIZE; |
162 | 11.5k | break; |
163 | 11.5k | } |
164 | 2.90k | goto PROCESS_NON_CAPTURE; |
165 | | |
166 | 155k | case OP_BRA: |
167 | | /* There's a special case of OP_BRA, when it is wrapped round a repeated |
168 | | OP_RECURSE. We'd like to process the latter at this level so that |
169 | | remembering the value works for repeated cases. So we do nothing, but |
170 | | set a fudge value to skip over the OP_KET after the recurse. */ |
171 | | |
172 | 155k | if (cc[1+LINK_SIZE] == OP_RECURSE && cc[2*(1+LINK_SIZE)] == OP_KET) |
173 | 92 | { |
174 | 92 | once_fudge = 1 + LINK_SIZE; |
175 | 92 | cc += 1 + LINK_SIZE; |
176 | 92 | break; |
177 | 92 | } |
178 | 154k | PCRE2_FALLTHROUGH /* Fall through */ |
179 | 154k | |
180 | 168k | case OP_ONCE: |
181 | 181k | case OP_SCRIPT_RUN: |
182 | 183k | case OP_SBRA: |
183 | 184k | case OP_BRAPOS: |
184 | 185k | case OP_SBRAPOS: |
185 | 188k | PROCESS_NON_CAPTURE: |
186 | 188k | d = find_minlength(re, cc, startcode, utf, recurses, countptr, |
187 | 188k | backref_cache); |
188 | 188k | if (d < 0) return d; |
189 | 187k | branchlength += d; |
190 | 367k | do cc += GET(cc, 1); while (*cc == OP_ALT); |
191 | 187k | cc += 1 + LINK_SIZE; |
192 | 187k | break; |
193 | | |
194 | | /* To save time for repeated capturing subpatterns, we remember the |
195 | | length of the previous one. Unfortunately we can't do the same for |
196 | | the unnumbered ones above. Nor can we do this if (?| is present in the |
197 | | pattern because captures with the same number are not then identical. */ |
198 | | |
199 | 581k | case OP_CBRA: |
200 | 583k | case OP_SCBRA: |
201 | 585k | case OP_CBRAPOS: |
202 | 586k | case OP_SCBRAPOS: |
203 | 586k | recno = (int)GET2(cc, 1+LINK_SIZE); |
204 | 586k | if (dupcapused || recno != prev_cap_recno) |
205 | 127k | { |
206 | 127k | prev_cap_recno = recno; |
207 | 127k | prev_cap_d = find_minlength(re, cc, startcode, utf, recurses, countptr, |
208 | 127k | backref_cache); |
209 | 127k | if (prev_cap_d < 0) return prev_cap_d; |
210 | 127k | } |
211 | 586k | branchlength += prev_cap_d; |
212 | 1.27M | do cc += GET(cc, 1); while (*cc == OP_ALT); |
213 | 586k | cc += 1 + LINK_SIZE; |
214 | 586k | break; |
215 | | |
216 | | /* ACCEPT makes things far too complicated; we have to give up. In fact, |
217 | | from 10.34 onwards, if a pattern contains (*ACCEPT), this function is not |
218 | | used. However, leave the code in place, just in case. */ |
219 | | |
220 | 0 | case OP_ACCEPT: |
221 | 0 | case OP_ASSERT_ACCEPT: |
222 | 0 | return -1; |
223 | | |
224 | | /* Reached end of a branch; if it's a ket it is the end of a nested |
225 | | call. If it's ALT it is an alternation in a nested call. If it is END it's |
226 | | the end of the outer call. All can be handled by the same code. If the |
227 | | length of any branch is zero, there is no need to scan any subsequent |
228 | | branches. */ |
229 | | |
230 | 184k | case OP_ALT: |
231 | 527k | case OP_KET: |
232 | 529k | case OP_KETRMAX: |
233 | 530k | case OP_KETRMIN: |
234 | 532k | case OP_KETRPOS: |
235 | 532k | case OP_END: |
236 | 532k | if (length < 0 || (!had_recurse && branchlength < length)) |
237 | 414k | length = branchlength; |
238 | 532k | if (op != OP_ALT || length == 0) return length; |
239 | 170k | nextbranch = cc + GET(cc, 1); |
240 | 170k | cc += 1 + LINK_SIZE; |
241 | 170k | branchlength = 0; |
242 | 170k | had_recurse = FALSE; |
243 | 170k | break; |
244 | | |
245 | | /* Skip over assertive subpatterns */ |
246 | | |
247 | 492k | case OP_ASSERT: |
248 | 723k | case OP_ASSERT_NOT: |
249 | 898k | case OP_ASSERTBACK: |
250 | 994k | case OP_ASSERTBACK_NOT: |
251 | 1.76M | case OP_ASSERT_NA: |
252 | 1.76M | case OP_ASSERT_SCS: |
253 | 1.79M | case OP_ASSERTBACK_NA: |
254 | 2.70M | do cc += GET(cc, 1); while (*cc == OP_ALT); |
255 | 1.79M | PCRE2_FALLTHROUGH /* Fall through */ |
256 | | |
257 | | /* Skip over things that don't match chars */ |
258 | | |
259 | 1.79M | case OP_REVERSE: |
260 | 1.79M | case OP_VREVERSE: |
261 | 1.79M | case OP_CREF: |
262 | 1.79M | case OP_DNCREF: |
263 | 1.79M | case OP_RREF: |
264 | 1.79M | case OP_DNRREF: |
265 | 1.79M | case OP_FALSE: |
266 | 1.79M | case OP_TRUE: |
267 | 2.91M | case OP_CALLOUT: |
268 | 2.92M | case OP_SOD: |
269 | 2.92M | case OP_SOM: |
270 | 2.93M | case OP_EOD: |
271 | 2.94M | case OP_EODN: |
272 | 2.97M | case OP_CIRC: |
273 | 3.00M | case OP_CIRCM: |
274 | 3.04M | case OP_DOLL: |
275 | 3.05M | case OP_DOLLM: |
276 | 3.06M | case OP_NOT_WORD_BOUNDARY: |
277 | 3.07M | case OP_WORD_BOUNDARY: |
278 | 3.07M | case OP_NOT_UCP_WORD_BOUNDARY: |
279 | 3.07M | case OP_UCP_WORD_BOUNDARY: |
280 | 3.07M | cc += PRIV(OP_lengths)[*cc]; |
281 | 3.07M | break; |
282 | | |
283 | 2.43k | case OP_CALLOUT_STR: |
284 | 2.43k | cc += GET(cc, 1 + 2*LINK_SIZE); |
285 | 2.43k | break; |
286 | | |
287 | | /* Skip over a subpattern that has a {0} or {0,x} quantifier */ |
288 | | |
289 | 8.18k | case OP_BRAZERO: |
290 | 10.1k | case OP_BRAMINZERO: |
291 | 11.4k | case OP_BRAPOSZERO: |
292 | 12.3k | case OP_SKIPZERO: |
293 | 12.3k | cc += PRIV(OP_lengths)[*cc]; |
294 | 13.7k | do cc += GET(cc, 1); while (*cc == OP_ALT); |
295 | 12.3k | cc += 1 + LINK_SIZE; |
296 | 12.3k | break; |
297 | | |
298 | | /* Handle literal characters and + repetitions */ |
299 | | |
300 | 3.79M | case OP_CHAR: |
301 | 6.19M | case OP_CHARI: |
302 | 6.20M | case OP_NOT: |
303 | 6.21M | case OP_NOTI: |
304 | 6.25M | case OP_PLUS: |
305 | 6.27M | case OP_PLUSI: |
306 | 6.28M | case OP_MINPLUS: |
307 | 6.30M | case OP_MINPLUSI: |
308 | 6.33M | case OP_POSPLUS: |
309 | 6.34M | case OP_POSPLUSI: |
310 | 6.34M | case OP_NOTPLUS: |
311 | 6.34M | case OP_NOTPLUSI: |
312 | 6.35M | case OP_NOTMINPLUS: |
313 | 6.35M | case OP_NOTMINPLUSI: |
314 | 6.35M | case OP_NOTPOSPLUS: |
315 | 6.35M | case OP_NOTPOSPLUSI: |
316 | 6.35M | branchlength++; |
317 | 6.35M | cc += 2; |
318 | 6.35M | #ifdef SUPPORT_UNICODE |
319 | 6.35M | if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); |
320 | 6.35M | #endif |
321 | 6.35M | break; |
322 | | |
323 | 21.8k | case OP_TYPEPLUS: |
324 | 30.1k | case OP_TYPEMINPLUS: |
325 | 40.3k | case OP_TYPEPOSPLUS: |
326 | 40.3k | branchlength++; |
327 | 40.3k | cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2; |
328 | 40.3k | break; |
329 | | |
330 | | /* Handle exact repetitions. The count is already in characters, but we |
331 | | may need to skip over a multibyte character in UTF mode. */ |
332 | | |
333 | 15.1k | case OP_EXACT: |
334 | 24.0k | case OP_EXACTI: |
335 | 26.5k | case OP_NOTEXACT: |
336 | 28.2k | case OP_NOTEXACTI: |
337 | 28.2k | branchlength += GET2(cc,1); |
338 | 28.2k | cc += 2 + IMM2_SIZE; |
339 | 28.2k | #ifdef SUPPORT_UNICODE |
340 | 28.2k | if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); |
341 | 28.2k | #endif |
342 | 28.2k | break; |
343 | | |
344 | 11.7k | case OP_TYPEEXACT: |
345 | 11.7k | branchlength += GET2(cc,1); |
346 | 11.7k | cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP |
347 | 10.7k | || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0); |
348 | 11.7k | break; |
349 | | |
350 | | /* Handle single-char non-literal matchers */ |
351 | | |
352 | 20.7k | case OP_PROP: |
353 | 33.0k | case OP_NOTPROP: |
354 | 33.0k | cc += 2; |
355 | 33.0k | PCRE2_FALLTHROUGH /* Fall through */ |
356 | | |
357 | 41.7k | case OP_NOT_DIGIT: |
358 | 47.0k | case OP_DIGIT: |
359 | 65.7k | case OP_NOT_WHITESPACE: |
360 | 78.6k | case OP_WHITESPACE: |
361 | 86.6k | case OP_NOT_WORDCHAR: |
362 | 91.9k | case OP_WORDCHAR: |
363 | 135k | case OP_ANY: |
364 | 156k | case OP_ALLANY: |
365 | 160k | case OP_EXTUNI: |
366 | 173k | case OP_HSPACE: |
367 | 195k | case OP_NOT_HSPACE: |
368 | 198k | case OP_VSPACE: |
369 | 202k | case OP_NOT_VSPACE: |
370 | 202k | branchlength++; |
371 | 202k | cc++; |
372 | 202k | break; |
373 | | |
374 | | /* "Any newline" might match two characters, but it also might match just |
375 | | one. */ |
376 | | |
377 | 8.18k | case OP_ANYNL: |
378 | 8.18k | branchlength += 1; |
379 | 8.18k | cc++; |
380 | 8.18k | break; |
381 | | |
382 | | /* The single-byte matcher means we can't proceed in UTF mode. (In |
383 | | non-UTF mode \C will actually be turned into OP_ALLANY, so won't ever |
384 | | appear, but leave the code, just in case.) */ |
385 | | |
386 | 0 | case OP_ANYBYTE: |
387 | 0 | #ifdef SUPPORT_UNICODE |
388 | 0 | if (utf) return -1; |
389 | 0 | #endif |
390 | 0 | branchlength++; |
391 | 0 | cc++; |
392 | 0 | break; |
393 | | |
394 | | /* For repeated character types, we have to test for \p and \P, which have |
395 | | an extra two bytes of parameters. */ |
396 | | |
397 | 26.6k | case OP_TYPESTAR: |
398 | 35.3k | case OP_TYPEMINSTAR: |
399 | 44.2k | case OP_TYPEQUERY: |
400 | 52.5k | case OP_TYPEMINQUERY: |
401 | 59.2k | case OP_TYPEPOSSTAR: |
402 | 66.3k | case OP_TYPEPOSQUERY: |
403 | 66.3k | if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2; |
404 | 66.3k | cc += PRIV(OP_lengths)[op]; |
405 | 66.3k | break; |
406 | | |
407 | 884 | case OP_TYPEUPTO: |
408 | 2.11k | case OP_TYPEMINUPTO: |
409 | 6.48k | case OP_TYPEPOSUPTO: |
410 | 6.48k | if (cc[1 + IMM2_SIZE] == OP_PROP |
411 | 5.51k | || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2; |
412 | 6.48k | cc += PRIV(OP_lengths)[op]; |
413 | 6.48k | break; |
414 | | |
415 | | /* Check a class for variable quantification */ |
416 | | |
417 | 71.6k | case OP_CLASS: |
418 | 104k | case OP_NCLASS: |
419 | 104k | #ifdef SUPPORT_WIDE_CHARS |
420 | 122k | case OP_XCLASS: |
421 | 123k | case OP_ECLASS: |
422 | | /* The original code caused an unsigned overflow in 64 bit systems, |
423 | | so now we use a conditional statement. */ |
424 | 123k | if (op == OP_XCLASS || op == OP_ECLASS) |
425 | 19.6k | cc += GET(cc, 1); |
426 | 104k | else |
427 | 104k | #endif |
428 | 104k | cc += PRIV(OP_lengths)[OP_CLASS]; |
429 | | |
430 | 123k | switch (*cc) |
431 | 123k | { |
432 | 6.12k | case OP_CRPLUS: |
433 | 8.41k | case OP_CRMINPLUS: |
434 | 10.8k | case OP_CRPOSPLUS: |
435 | 10.8k | branchlength++; |
436 | 10.8k | PCRE2_FALLTHROUGH /* Fall through */ |
437 | | |
438 | 19.4k | case OP_CRSTAR: |
439 | 23.7k | case OP_CRMINSTAR: |
440 | 27.3k | case OP_CRQUERY: |
441 | 30.5k | case OP_CRMINQUERY: |
442 | 36.0k | case OP_CRPOSSTAR: |
443 | 40.1k | case OP_CRPOSQUERY: |
444 | 40.1k | cc++; |
445 | 40.1k | break; |
446 | | |
447 | 3.36k | case OP_CRRANGE: |
448 | 6.43k | case OP_CRMINRANGE: |
449 | 17.5k | case OP_CRPOSRANGE: |
450 | 17.5k | branchlength += GET2(cc,1); |
451 | 17.5k | cc += 1 + 2 * IMM2_SIZE; |
452 | 17.5k | break; |
453 | | |
454 | 66.1k | default: |
455 | 66.1k | branchlength++; |
456 | 66.1k | break; |
457 | 123k | } |
458 | 123k | break; |
459 | | |
460 | | /* Backreferences and subroutine calls (OP_RECURSE) are treated in the same |
461 | | way: we find the minimum length for the subpattern. A recursion |
462 | | (backreference or subroutine) causes an a flag to be set that causes the |
463 | | length of this branch to be ignored. The logic is that a recursion can only |
464 | | make sense if there is another alternative that stops the recursing. That |
465 | | will provide the minimum length (when no recursion happens). |
466 | | |
467 | | If PCRE2_MATCH_UNSET_BACKREF is set, a backreference to an unset bracket |
468 | | matches an empty string (by default it causes a matching failure), so in |
469 | | that case we must set the minimum length to zero. |
470 | | |
471 | | For backreferenes, if duplicate numbers are present in the pattern we check |
472 | | for a reference to a duplicate. If it is, we don't know which version will |
473 | | be referenced, so we have to set the minimum length to zero. */ |
474 | | |
475 | | /* Duplicate named pattern back reference. */ |
476 | | |
477 | 123k | case OP_DNREF: |
478 | 8.27k | case OP_DNREFI: |
479 | 8.27k | if (!dupcapused && (re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0) |
480 | 4.53k | { |
481 | 4.53k | int count = GET2(cc, 1+IMM2_SIZE); |
482 | 4.53k | PCRE2_SPTR slot = |
483 | 4.53k | (PCRE2_SPTR)((const uint8_t *)re + sizeof(pcre2_real_code)) + |
484 | 4.53k | GET2(cc, 1) * re->name_entry_size; |
485 | | |
486 | 4.53k | d = INT_MAX; |
487 | | |
488 | | /* Scan all groups with the same name; find the shortest. */ |
489 | | |
490 | 20.5k | while (count-- > 0) |
491 | 19.8k | { |
492 | 19.8k | int dd, i; |
493 | 19.8k | recno = GET2(slot, 0); |
494 | | |
495 | 19.8k | if (recno <= backref_cache[0] && backref_cache[recno] >= 0) |
496 | 13.9k | dd = backref_cache[recno]; |
497 | 5.91k | else |
498 | 5.91k | { |
499 | 5.91k | ce = cs = PRIV(find_bracket)(startcode, utf, recno); |
500 | 5.91k | if (cs == NULL) return -2; |
501 | 6.24k | do ce += GET(ce, 1); while (*ce == OP_ALT); |
502 | | |
503 | 5.91k | dd = 0; |
504 | 5.91k | if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL) |
505 | 5.91k | { |
506 | 5.91k | if (cc > cs && cc < ce) /* Simple recursion */ |
507 | 755 | { |
508 | 755 | had_recurse = TRUE; |
509 | 755 | } |
510 | 5.16k | else |
511 | 5.16k | { |
512 | 5.16k | recurse_check *r = recurses; |
513 | 13.3k | for (r = recurses; r != NULL; r = r->prev) |
514 | 8.24k | if (r->group == cs) break; |
515 | 5.16k | if (r != NULL) /* Mutual recursion */ |
516 | 93 | { |
517 | 93 | had_recurse = TRUE; |
518 | 93 | } |
519 | 5.06k | else |
520 | 5.06k | { |
521 | 5.06k | this_recurse.prev = recurses; /* No recursion */ |
522 | 5.06k | this_recurse.group = cs; |
523 | 5.06k | dd = find_minlength(re, cs, startcode, utf, &this_recurse, |
524 | 5.06k | countptr, backref_cache); |
525 | 5.06k | if (dd < 0) return dd; |
526 | 5.06k | } |
527 | 5.16k | } |
528 | 5.91k | } |
529 | | |
530 | 5.90k | backref_cache[recno] = dd; |
531 | 41.8k | for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1; |
532 | 5.90k | backref_cache[0] = recno; |
533 | 5.90k | } |
534 | | |
535 | 19.8k | if (dd < d) d = dd; |
536 | 19.8k | if (d <= 0) break; /* No point looking at any more */ |
537 | 16.0k | slot += re->name_entry_size; |
538 | 16.0k | } |
539 | 4.53k | } |
540 | 3.74k | else d = 0; |
541 | 8.26k | cc += PRIV(OP_lengths)[*cc]; |
542 | 8.26k | goto REPEAT_BACK_REFERENCE; |
543 | | |
544 | | /* Single back reference by number. References by name are converted to by |
545 | | number when there is no duplication. */ |
546 | | |
547 | 33.6k | case OP_REF: |
548 | 40.3k | case OP_REFI: |
549 | 40.3k | recno = GET2(cc, 1); |
550 | 40.3k | if (recno <= backref_cache[0] && backref_cache[recno] >= 0) |
551 | 29.7k | d = backref_cache[recno]; |
552 | 10.6k | else |
553 | 10.6k | { |
554 | 10.6k | int i; |
555 | 10.6k | d = 0; |
556 | | |
557 | 10.6k | if ((re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0) |
558 | 10.3k | { |
559 | 10.3k | ce = cs = PRIV(find_bracket)(startcode, utf, recno); |
560 | 10.3k | if (cs == NULL) return -2; |
561 | 11.7k | do ce += GET(ce, 1); while (*ce == OP_ALT); |
562 | | |
563 | 10.3k | if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL) |
564 | 9.99k | { |
565 | 9.99k | if (cc > cs && cc < ce) /* Simple recursion */ |
566 | 742 | { |
567 | 742 | had_recurse = TRUE; |
568 | 742 | } |
569 | 9.25k | else |
570 | 9.25k | { |
571 | 9.25k | recurse_check *r = recurses; |
572 | 37.7k | for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break; |
573 | 9.25k | if (r != NULL) /* Mutual recursion */ |
574 | 460 | { |
575 | 460 | had_recurse = TRUE; |
576 | 460 | } |
577 | 8.79k | else /* No recursion */ |
578 | 8.79k | { |
579 | 8.79k | this_recurse.prev = recurses; |
580 | 8.79k | this_recurse.group = cs; |
581 | 8.79k | d = find_minlength(re, cs, startcode, utf, &this_recurse, countptr, |
582 | 8.79k | backref_cache); |
583 | 8.79k | if (d < 0) return d; |
584 | 8.79k | } |
585 | 9.25k | } |
586 | 9.99k | } |
587 | 10.3k | } |
588 | | |
589 | 10.5k | backref_cache[recno] = d; |
590 | 27.1k | for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1; |
591 | 10.5k | backref_cache[0] = recno; |
592 | 10.5k | } |
593 | | |
594 | 40.3k | cc += PRIV(OP_lengths)[*cc]; |
595 | | |
596 | | /* Handle repeated back references */ |
597 | | |
598 | 48.5k | REPEAT_BACK_REFERENCE: |
599 | 48.5k | switch (*cc) |
600 | 48.5k | { |
601 | 3.40k | case OP_CRSTAR: |
602 | 4.64k | case OP_CRMINSTAR: |
603 | 6.06k | case OP_CRQUERY: |
604 | 7.01k | case OP_CRMINQUERY: |
605 | 7.51k | case OP_CRPOSSTAR: |
606 | 7.88k | case OP_CRPOSQUERY: |
607 | 7.88k | min = 0; |
608 | 7.88k | cc++; |
609 | 7.88k | break; |
610 | | |
611 | 3.24k | case OP_CRPLUS: |
612 | 6.44k | case OP_CRMINPLUS: |
613 | 6.85k | case OP_CRPOSPLUS: |
614 | 6.85k | min = 1; |
615 | 6.85k | cc++; |
616 | 6.85k | break; |
617 | | |
618 | 1.13k | case OP_CRRANGE: |
619 | 1.49k | case OP_CRMINRANGE: |
620 | 2.69k | case OP_CRPOSRANGE: |
621 | 2.69k | min = GET2(cc, 1); |
622 | 2.69k | cc += 1 + 2 * IMM2_SIZE; |
623 | 2.69k | break; |
624 | | |
625 | 31.1k | default: |
626 | 31.1k | min = 1; |
627 | 31.1k | break; |
628 | 48.5k | } |
629 | | |
630 | | /* Take care not to overflow: (1) min and d are ints, so check that their |
631 | | product is not greater than INT_MAX. (2) branchlength is limited to |
632 | | UINT16_MAX (checked at the top of the loop). */ |
633 | | |
634 | 48.5k | if ((d > 0 && (INT_MAX/d) < min) || (int)UINT16_MAX - branchlength < min*d) |
635 | 1.25k | branchlength = UINT16_MAX; |
636 | 47.3k | else branchlength += min * d; |
637 | 48.5k | break; |
638 | | |
639 | | /* Recursion always refers to the first occurrence of a subpattern with a |
640 | | given number. Therefore, we can always make use of caching, even when the |
641 | | pattern contains multiple subpatterns with the same number. */ |
642 | | |
643 | 354k | case OP_RECURSE: |
644 | 354k | cs = ce = startcode + GET(cc, 1); |
645 | 354k | recno = GET2(cs, 1+LINK_SIZE); |
646 | 354k | if (recno == prev_recurse_recno) |
647 | 333k | { |
648 | 333k | branchlength += prev_recurse_d; |
649 | 333k | } |
650 | 21.0k | else |
651 | 21.0k | { |
652 | 137k | do ce += GET(ce, 1); while (*ce == OP_ALT); |
653 | 21.0k | if (cc > cs && cc < ce) /* Simple recursion */ |
654 | 12.2k | had_recurse = TRUE; |
655 | 8.84k | else |
656 | 8.84k | { |
657 | 8.84k | recurse_check *r = recurses; |
658 | 27.9k | for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break; |
659 | 8.84k | if (r != NULL) /* Mutual recursion */ |
660 | 1.09k | had_recurse = TRUE; |
661 | 7.75k | else |
662 | 7.75k | { |
663 | 7.75k | this_recurse.prev = recurses; |
664 | 7.75k | this_recurse.group = cs; |
665 | 7.75k | prev_recurse_d = find_minlength(re, cs, startcode, utf, &this_recurse, |
666 | 7.75k | countptr, backref_cache); |
667 | 7.75k | if (prev_recurse_d < 0) return prev_recurse_d; |
668 | 7.69k | prev_recurse_recno = recno; |
669 | 7.69k | branchlength += prev_recurse_d; |
670 | 7.69k | } |
671 | 8.84k | } |
672 | 21.0k | } |
673 | 354k | cc += 1 + LINK_SIZE + once_fudge; |
674 | 354k | once_fudge = 0; |
675 | 354k | break; |
676 | | |
677 | | /* Anything else does not or need not match a character. We can get the |
678 | | item's length from the table, but for those that can match zero occurrences |
679 | | of a character, we must take special action for UTF-8 characters. As it |
680 | | happens, the "NOT" versions of these opcodes are used at present only for |
681 | | ASCII characters, so they could be omitted from this list. However, in |
682 | | future that may change, so we include them here so as not to leave a |
683 | | gotcha for a future maintainer. */ |
684 | | |
685 | 2.14k | case OP_UPTO: |
686 | 3.65k | case OP_UPTOI: |
687 | 5.26k | case OP_NOTUPTO: |
688 | 9.02k | case OP_NOTUPTOI: |
689 | 11.6k | case OP_MINUPTO: |
690 | 12.6k | case OP_MINUPTOI: |
691 | 15.5k | case OP_NOTMINUPTO: |
692 | 18.2k | case OP_NOTMINUPTOI: |
693 | 23.9k | case OP_POSUPTO: |
694 | 26.0k | case OP_POSUPTOI: |
695 | 28.8k | case OP_NOTPOSUPTO: |
696 | 30.6k | case OP_NOTPOSUPTOI: |
697 | | |
698 | 54.2k | case OP_STAR: |
699 | 72.1k | case OP_STARI: |
700 | 75.7k | case OP_NOTSTAR: |
701 | 77.0k | case OP_NOTSTARI: |
702 | 80.4k | case OP_MINSTAR: |
703 | 87.5k | case OP_MINSTARI: |
704 | 88.2k | case OP_NOTMINSTAR: |
705 | 91.5k | case OP_NOTMINSTARI: |
706 | 117k | case OP_POSSTAR: |
707 | 125k | case OP_POSSTARI: |
708 | 129k | case OP_NOTPOSSTAR: |
709 | 130k | case OP_NOTPOSSTARI: |
710 | | |
711 | 141k | case OP_QUERY: |
712 | 157k | case OP_QUERYI: |
713 | 158k | case OP_NOTQUERY: |
714 | 159k | case OP_NOTQUERYI: |
715 | 164k | case OP_MINQUERY: |
716 | 177k | case OP_MINQUERYI: |
717 | 182k | case OP_NOTMINQUERY: |
718 | 182k | case OP_NOTMINQUERYI: |
719 | 206k | case OP_POSQUERY: |
720 | 222k | case OP_POSQUERYI: |
721 | 223k | case OP_NOTPOSQUERY: |
722 | 224k | case OP_NOTPOSQUERYI: |
723 | | |
724 | 224k | cc += PRIV(OP_lengths)[op]; |
725 | 224k | #ifdef SUPPORT_UNICODE |
726 | 224k | if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]); |
727 | 224k | #endif |
728 | 224k | break; |
729 | | |
730 | | /* Skip these, but we need to add in the name length. */ |
731 | | |
732 | 3.62k | case OP_MARK: |
733 | 5.19k | case OP_COMMIT_ARG: |
734 | 6.08k | case OP_PRUNE_ARG: |
735 | 9.31k | case OP_SKIP_ARG: |
736 | 10.0k | case OP_THEN_ARG: |
737 | 10.0k | cc += PRIV(OP_lengths)[op] + cc[1]; |
738 | 10.0k | break; |
739 | | |
740 | | /* The remaining opcodes are just skipped over. */ |
741 | | |
742 | 0 | case OP_CLOSE: |
743 | 14.5k | case OP_COMMIT: |
744 | 16.2k | case OP_FAIL: |
745 | 22.4k | case OP_PRUNE: |
746 | 24.1k | case OP_SET_SOM: |
747 | 27.7k | case OP_SKIP: |
748 | 33.1k | case OP_THEN: |
749 | 33.1k | cc += PRIV(OP_lengths)[op]; |
750 | 33.1k | break; |
751 | | |
752 | | /* This should not occur: we list all opcodes explicitly so that when |
753 | | new ones get added they are properly considered. */ |
754 | | |
755 | | /* LCOV_EXCL_START */ |
756 | 0 | default: |
757 | 0 | PCRE2_DEBUG_UNREACHABLE(); |
758 | 0 | return -3; |
759 | | /* LCOV_EXCL_STOP */ |
760 | 11.9M | } |
761 | 11.9M | } |
762 | | |
763 | | /* LCOV_EXCL_START */ |
764 | 0 | PCRE2_DEBUG_UNREACHABLE(); /* Control should never reach here */ |
765 | 0 | return -3; /* Avoid compiler warnings */ |
766 | | /* LCOV_EXCL_STOP */ |
767 | 363k | } |
768 | | |
769 | | |
770 | | |
771 | | /************************************************* |
772 | | * Set a bit and maybe its alternate case * |
773 | | *************************************************/ |
774 | | |
775 | | /* Given a character, set its first code unit's bit in the table, and also the |
776 | | corresponding bit for the other version of a letter if we are caseless. |
777 | | |
778 | | Arguments: |
779 | | re points to the regex block |
780 | | p points to the first code unit of the character |
781 | | caseless TRUE if caseless |
782 | | utf TRUE for UTF mode |
783 | | ucp TRUE for UCP mode |
784 | | |
785 | | Returns: pointer after the character |
786 | | */ |
787 | | |
788 | | static PCRE2_SPTR |
789 | | set_table_bit(pcre2_real_code *re, PCRE2_SPTR p, BOOL caseless, BOOL utf, |
790 | | BOOL ucp) |
791 | 349k | { |
792 | 349k | uint32_t c = *p++; /* First code unit */ |
793 | | |
794 | 349k | (void)utf; /* Stop compiler warnings when UTF not supported */ |
795 | 349k | (void)ucp; |
796 | | |
797 | | /* In 16-bit and 32-bit modes, code units greater than 0xff set the bit for |
798 | | 0xff. */ |
799 | | |
800 | | #if PCRE2_CODE_UNIT_WIDTH != 8 |
801 | | if (c > 0xff) SET_BIT(0xff); else |
802 | | #endif |
803 | | |
804 | 349k | SET_BIT(c); |
805 | | |
806 | | /* In UTF-8 or UTF-16 mode, pick up the remaining code units in order to find |
807 | | the end of the character, even when caseless. */ |
808 | | |
809 | 349k | #ifdef SUPPORT_UNICODE |
810 | 349k | if (utf) |
811 | 81.8k | { |
812 | 81.8k | #if PCRE2_CODE_UNIT_WIDTH == 8 |
813 | 81.8k | if (c >= 0xc0) GETUTF8INC(c, p); |
814 | | #elif PCRE2_CODE_UNIT_WIDTH == 16 |
815 | | if ((c & 0xfc00) == 0xd800) GETUTF16INC(c, p); |
816 | | #endif |
817 | 81.8k | } |
818 | 349k | #endif /* SUPPORT_UNICODE */ |
819 | | |
820 | | /* If caseless, handle the other case of the character. */ |
821 | | |
822 | 349k | if (caseless) |
823 | 107k | { |
824 | 107k | #ifdef SUPPORT_UNICODE |
825 | 107k | if (utf || ucp) |
826 | 69.3k | { |
827 | 69.3k | c = UCD_OTHERCASE(c); |
828 | 69.3k | #if PCRE2_CODE_UNIT_WIDTH == 8 |
829 | 69.3k | if (utf) |
830 | 60.9k | { |
831 | 60.9k | PCRE2_UCHAR buff[6]; |
832 | 60.9k | (void)PRIV(ord2utf)(c, buff); |
833 | 60.9k | SET_BIT(buff[0]); |
834 | 60.9k | } |
835 | 8.46k | else if (c < 256) SET_BIT(c); |
836 | | #else /* 16-bit or 32-bit mode */ |
837 | | if (c > 0xff) SET_BIT(0xff); else SET_BIT(c); |
838 | | #endif |
839 | 69.3k | } |
840 | | |
841 | 38.4k | else |
842 | 38.4k | #endif /* SUPPORT_UNICODE */ |
843 | | |
844 | | /* Not UTF or UCP */ |
845 | | |
846 | 38.4k | if (MAX_255(c)) SET_BIT(re->tables[fcc_offset + c]); |
847 | 107k | } |
848 | | |
849 | 349k | return p; |
850 | 349k | } |
851 | | |
852 | | |
853 | | |
854 | | /************************************************* |
855 | | * Set bits for a positive character type * |
856 | | *************************************************/ |
857 | | |
858 | | /* This function sets starting bits for a character type. In UTF-8 mode, we can |
859 | | only do a direct setting for bytes less than 128, as otherwise there can be |
860 | | confusion with bytes in the middle of UTF-8 characters. In a "traditional" |
861 | | environment, the tables will only recognize ASCII characters anyway, but in at |
862 | | least one Windows environment, some higher bytes bits were set in the tables. |
863 | | So we deal with that case by considering the UTF-8 encoding. |
864 | | |
865 | | Arguments: |
866 | | re the regex block |
867 | | cbit type the type of character wanted |
868 | | table_limit 32 for non-UTF-8; 16 for UTF-8 |
869 | | |
870 | | Returns: nothing |
871 | | */ |
872 | | |
873 | | static void |
874 | | set_type_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit) |
875 | 80.4k | { |
876 | 80.4k | uint32_t c; |
877 | 2.23M | for (c = 0; c < table_limit; c++) |
878 | 2.15M | re->start_bitmap[c] |= re->tables[c+cbits_offset+cbit_type]; |
879 | 80.4k | #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 |
880 | 80.4k | if (table_limit == 32) return; |
881 | 3.41M | for (c = 128; c < 256; c++) |
882 | 3.39M | { |
883 | 3.39M | if ((re->tables[cbits_offset + c/8] & (1u << (c&7))) != 0) |
884 | 0 | { |
885 | 0 | PCRE2_UCHAR buff[6]; |
886 | 0 | (void)PRIV(ord2utf)(c, buff); |
887 | 0 | SET_BIT(buff[0]); |
888 | 0 | } |
889 | 3.39M | } |
890 | 26.5k | #endif /* UTF-8 */ |
891 | 26.5k | } |
892 | | |
893 | | |
894 | | /************************************************* |
895 | | * Set bits for a negative character type * |
896 | | *************************************************/ |
897 | | |
898 | | /* This function sets starting bits for a negative character type such as \D. |
899 | | In UTF-8 mode, we can only do a direct setting for bytes less than 128, as |
900 | | otherwise there can be confusion with bytes in the middle of UTF-8 characters. |
901 | | Unlike in the positive case, where we can set appropriate starting bits for |
902 | | specific high-valued UTF-8 characters, in this case we have to set the bits for |
903 | | all high-valued characters. The lowest is 0xc2, but we overkill by starting at |
904 | | 0xc0 (192) for simplicity. |
905 | | |
906 | | Arguments: |
907 | | re the regex block |
908 | | cbit type the type of character wanted |
909 | | table_limit 32 for non-UTF-8; 16 for UTF-8 |
910 | | |
911 | | Returns: nothing |
912 | | */ |
913 | | |
914 | | static void |
915 | | set_nottype_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit) |
916 | 44.4k | { |
917 | 44.4k | uint32_t c; |
918 | 1.31M | for (c = 0; c < table_limit; c++) |
919 | 1.27M | re->start_bitmap[c] |= (uint8_t)(~(re->tables[c+cbits_offset+cbit_type])); |
920 | 44.4k | #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 |
921 | 84.7k | if (table_limit != 32) for (c = 24; c < 32; c++) re->start_bitmap[c] = 0xff; |
922 | 44.4k | #endif |
923 | 44.4k | } |
924 | | |
925 | | |
926 | | |
927 | | #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 |
928 | | /************************************************* |
929 | | * Set starting bits for a character list. * |
930 | | *************************************************/ |
931 | | |
932 | | /* This function sets starting bits for a character list. It enumerates |
933 | | all characters and character ranges in the character list, and sets |
934 | | the starting bits accordingly. |
935 | | |
936 | | Arguments: |
937 | | code pointer to the code |
938 | | start_bitmap pointer to the starting bitmap |
939 | | |
940 | | Returns: nothing |
941 | | */ |
942 | | static void |
943 | | study_char_list(PCRE2_SPTR code, uint8_t *start_bitmap, |
944 | | const uint8_t *char_lists_end) |
945 | 2.18k | { |
946 | 2.18k | uint32_t type, list_ind; |
947 | 2.18k | uint32_t char_list_add = XCL_CHAR_LIST_LOW_16_ADD; |
948 | 2.18k | uint32_t range_start = ~(uint32_t)0, range_end = 0; |
949 | 2.18k | const uint8_t *next_char; |
950 | 2.18k | PCRE2_UCHAR start_buffer[6], end_buffer[6]; |
951 | 2.18k | PCRE2_UCHAR start, end; |
952 | | |
953 | | /* Only needed in 8-bit mode at the moment. */ |
954 | 2.18k | type = (uint32_t)(code[0] << 8) | code[1]; |
955 | 2.18k | code += 2; |
956 | | |
957 | | /* Align characters. */ |
958 | 2.18k | next_char = char_lists_end - (GET(code, 0) << 1); |
959 | 2.18k | type &= XCL_TYPE_MASK; |
960 | 2.18k | list_ind = 0; |
961 | | |
962 | 2.18k | if ((type & XCL_BEGIN_WITH_RANGE) != 0) |
963 | 1.19k | range_start = XCL_CHAR_LIST_LOW_16_START; |
964 | | |
965 | 7.17k | while (type > 0) |
966 | 4.99k | { |
967 | 4.99k | uint32_t item_count = type & XCL_ITEM_COUNT_MASK; |
968 | | |
969 | 4.99k | if (item_count == XCL_ITEM_COUNT_MASK) |
970 | 2.92k | { |
971 | 2.92k | if (list_ind <= 1) |
972 | 2.64k | { |
973 | 2.64k | item_count = *(const uint16_t*)next_char; |
974 | 2.64k | next_char += 2; |
975 | 2.64k | } |
976 | 281 | else |
977 | 281 | { |
978 | 281 | item_count = *(const uint32_t*)next_char; |
979 | 281 | next_char += 4; |
980 | 281 | } |
981 | 2.92k | } |
982 | | |
983 | 35.7k | while (item_count > 0) |
984 | 30.7k | { |
985 | 30.7k | if (list_ind <= 1) |
986 | 28.4k | { |
987 | 28.4k | range_end = *(const uint16_t*)next_char; |
988 | 28.4k | next_char += 2; |
989 | 28.4k | } |
990 | 2.34k | else |
991 | 2.34k | { |
992 | 2.34k | range_end = *(const uint32_t*)next_char; |
993 | 2.34k | next_char += 4; |
994 | 2.34k | } |
995 | | |
996 | 30.7k | if ((range_end & XCL_CHAR_END) != 0) |
997 | 21.7k | { |
998 | 21.7k | range_end = char_list_add + (range_end >> XCL_CHAR_SHIFT); |
999 | | |
1000 | 21.7k | PRIV(ord2utf)(range_end, end_buffer); |
1001 | 21.7k | end = end_buffer[0]; |
1002 | | |
1003 | 21.7k | if (range_start < range_end) |
1004 | 9.85k | { |
1005 | 9.85k | PRIV(ord2utf)(range_start, start_buffer); |
1006 | 56.2k | for (start = start_buffer[0]; start <= end; start++) |
1007 | 46.3k | start_bitmap[start / 8] |= (1u << (start & 7)); |
1008 | 9.85k | } |
1009 | 11.8k | else |
1010 | 11.8k | start_bitmap[end / 8] |= (1u << (end & 7)); |
1011 | | |
1012 | 21.7k | range_start = ~(uint32_t)0; |
1013 | 21.7k | } |
1014 | 9.06k | else |
1015 | 9.06k | range_start = char_list_add + (range_end >> XCL_CHAR_SHIFT); |
1016 | | |
1017 | 30.7k | item_count--; |
1018 | 30.7k | } |
1019 | | |
1020 | 4.99k | list_ind++; |
1021 | 4.99k | type >>= XCL_TYPE_BIT_LEN; |
1022 | | |
1023 | 4.99k | if (range_start == ~(uint32_t)0) |
1024 | 3.38k | { |
1025 | 3.38k | if ((type & XCL_BEGIN_WITH_RANGE) != 0) |
1026 | 0 | { |
1027 | | /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_START is not possible. */ |
1028 | 0 | if (list_ind == 1) range_start = XCL_CHAR_LIST_HIGH_16_START; |
1029 | 0 | else range_start = XCL_CHAR_LIST_LOW_32_START; |
1030 | 0 | } |
1031 | 3.38k | } |
1032 | 1.60k | else if ((type & XCL_BEGIN_WITH_RANGE) == 0) |
1033 | 400 | { |
1034 | 400 | PRIV(ord2utf)(range_start, start_buffer); |
1035 | | |
1036 | | /* In 8 bit mode XCL_CHAR_LIST_LOW_32_END and |
1037 | | XCL_CHAR_LIST_HIGH_32_END are not possible. */ |
1038 | 400 | if (list_ind == 1) range_end = XCL_CHAR_LIST_LOW_16_END; |
1039 | 400 | else range_end = XCL_CHAR_LIST_HIGH_16_END; |
1040 | | |
1041 | 400 | PRIV(ord2utf)(range_end, end_buffer); |
1042 | 400 | end = end_buffer[0]; |
1043 | | |
1044 | 800 | for (start = start_buffer[0]; start <= end; start++) |
1045 | 400 | start_bitmap[start / 8] |= (1u << (start & 7)); |
1046 | | |
1047 | 400 | range_start = ~(uint32_t)0; |
1048 | 400 | } |
1049 | | |
1050 | | /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_ADD is not possible. */ |
1051 | 4.99k | if (list_ind == 1) char_list_add = XCL_CHAR_LIST_HIGH_16_ADD; |
1052 | 2.80k | else char_list_add = XCL_CHAR_LIST_LOW_32_ADD; |
1053 | 4.99k | } |
1054 | 2.18k | } |
1055 | | #endif |
1056 | | |
1057 | | |
1058 | | |
1059 | | /************************************************* |
1060 | | * Create bitmap of starting code units * |
1061 | | *************************************************/ |
1062 | | |
1063 | | /* This function scans a compiled unanchored expression recursively and |
1064 | | attempts to build a bitmap of the set of possible starting code units whose |
1065 | | values are less than 256. In 16-bit and 32-bit mode, values above 255 all cause |
1066 | | the 255 bit to be set. When calling set[_not]_type_bits() in UTF-8 (sic) mode |
1067 | | we pass a value of 16 rather than 32 as the final argument. (See comments in |
1068 | | those functions for the reason.) |
1069 | | |
1070 | | The SSB_CONTINUE return is useful for parenthesized groups in patterns such as |
1071 | | (a*)b where the group provides some optional starting code units but scanning |
1072 | | must continue at the outer level to find at least one mandatory code unit. At |
1073 | | the outermost level, this function fails unless the result is SSB_DONE. |
1074 | | |
1075 | | We restrict recursion (for nested groups) to 1000 to avoid stack overflow |
1076 | | issues. |
1077 | | |
1078 | | Arguments: |
1079 | | re points to the compiled regex block |
1080 | | code points to an expression |
1081 | | utf TRUE if in UTF mode |
1082 | | ucp TRUE if in UCP mode |
1083 | | depthptr pointer to recurse depth |
1084 | | |
1085 | | Returns: SSB_FAIL => Failed to find any starting code units |
1086 | | SSB_DONE => Found mandatory starting code units |
1087 | | SSB_CONTINUE => Found optional starting code units |
1088 | | SSB_UNKNOWN => Hit an unrecognized opcode |
1089 | | SSB_TOODEEP => Recursion is too deep |
1090 | | */ |
1091 | | |
1092 | | static int |
1093 | | set_start_bits(pcre2_real_code *re, PCRE2_SPTR code, BOOL utf, BOOL ucp, |
1094 | | int *depthptr) |
1095 | 258k | { |
1096 | 258k | uint32_t c; |
1097 | 258k | int yield = SSB_DONE; |
1098 | | |
1099 | 258k | #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 |
1100 | 258k | int table_limit = utf? 16:32; |
1101 | | #else |
1102 | | int table_limit = 32; |
1103 | | #endif |
1104 | | |
1105 | 258k | *depthptr += 1; |
1106 | 258k | if (*depthptr > 1000) return SSB_TOODEEP; |
1107 | | |
1108 | 258k | do |
1109 | 1.59M | { |
1110 | 1.59M | BOOL try_next = TRUE; |
1111 | 1.59M | PCRE2_SPTR tcode = code + 1 + LINK_SIZE; |
1112 | | |
1113 | 1.59M | if (*code == OP_CBRA || *code == OP_SCBRA || |
1114 | 1.50M | *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE; |
1115 | | |
1116 | 3.73M | while (try_next) /* Loop for items in this branch */ |
1117 | 2.25M | { |
1118 | 2.25M | int rc; |
1119 | 2.25M | PCRE2_SPTR ncode; |
1120 | 2.25M | const uint8_t *classmap = NULL; |
1121 | 2.25M | #ifdef SUPPORT_WIDE_CHARS |
1122 | 2.25M | PCRE2_UCHAR xclassflags; |
1123 | 2.25M | #endif |
1124 | | |
1125 | 2.25M | switch(*tcode) |
1126 | 2.25M | { |
1127 | | /* If we reach something we don't understand, it means a new opcode has |
1128 | | been created that hasn't been added to this function. Hopefully this |
1129 | | problem will be discovered during testing. */ |
1130 | | |
1131 | 0 | default: |
1132 | 0 | return SSB_UNKNOWN; |
1133 | | |
1134 | | /* Fail for a valid opcode that implies no starting bits. */ |
1135 | | |
1136 | 26 | case OP_ACCEPT: |
1137 | 40 | case OP_ASSERT_ACCEPT: |
1138 | 465 | case OP_ALLANY: |
1139 | 3.36k | case OP_ANY: |
1140 | 3.36k | case OP_ANYBYTE: |
1141 | 3.43k | case OP_CIRCM: |
1142 | 3.44k | case OP_CLOSE: |
1143 | 3.46k | case OP_COMMIT: |
1144 | 3.48k | case OP_COMMIT_ARG: |
1145 | 3.81k | case OP_COND: |
1146 | 3.87k | case OP_CREF: |
1147 | 3.87k | case OP_FALSE: |
1148 | 3.87k | case OP_TRUE: |
1149 | 3.88k | case OP_DNCREF: |
1150 | 3.89k | case OP_DNREF: |
1151 | 3.89k | case OP_DNREFI: |
1152 | 3.90k | case OP_DNRREF: |
1153 | 5.74k | case OP_DOLL: |
1154 | 6.16k | case OP_DOLLM: |
1155 | 6.16k | case OP_END: |
1156 | 6.21k | case OP_EOD: |
1157 | 6.32k | case OP_EODN: |
1158 | 6.72k | case OP_EXTUNI: |
1159 | 6.76k | case OP_FAIL: |
1160 | 6.79k | case OP_MARK: |
1161 | 6.93k | case OP_NOT: |
1162 | 7.00k | case OP_NOTEXACT: |
1163 | 7.03k | case OP_NOTEXACTI: |
1164 | 7.09k | case OP_NOTI: |
1165 | 7.12k | case OP_NOTMINPLUS: |
1166 | 7.13k | case OP_NOTMINPLUSI: |
1167 | 7.15k | case OP_NOTMINQUERY: |
1168 | 7.17k | case OP_NOTMINQUERYI: |
1169 | 7.20k | case OP_NOTMINSTAR: |
1170 | 7.21k | case OP_NOTMINSTARI: |
1171 | 7.23k | case OP_NOTMINUPTO: |
1172 | 7.25k | case OP_NOTMINUPTOI: |
1173 | 7.35k | case OP_NOTPLUS: |
1174 | 7.37k | case OP_NOTPLUSI: |
1175 | 7.41k | case OP_NOTPOSPLUS: |
1176 | 7.42k | case OP_NOTPOSPLUSI: |
1177 | 7.45k | case OP_NOTPOSQUERY: |
1178 | 7.46k | case OP_NOTPOSQUERYI: |
1179 | 7.49k | case OP_NOTPOSSTAR: |
1180 | 7.50k | case OP_NOTPOSSTARI: |
1181 | 7.52k | case OP_NOTPOSUPTO: |
1182 | 7.53k | case OP_NOTPOSUPTOI: |
1183 | 8.62k | case OP_NOTPROP: |
1184 | 8.67k | case OP_NOTQUERY: |
1185 | 8.68k | case OP_NOTQUERYI: |
1186 | 8.80k | case OP_NOTSTAR: |
1187 | 8.83k | case OP_NOTSTARI: |
1188 | 8.85k | case OP_NOTUPTO: |
1189 | 8.86k | case OP_NOTUPTOI: |
1190 | 9.95k | case OP_NOT_HSPACE: |
1191 | 10.3k | case OP_NOT_VSPACE: |
1192 | 10.4k | case OP_PRUNE: |
1193 | 10.4k | case OP_PRUNE_ARG: |
1194 | 10.6k | case OP_RECURSE: |
1195 | 10.7k | case OP_REF: |
1196 | 10.7k | case OP_REFI: |
1197 | 10.7k | case OP_REVERSE: |
1198 | 10.7k | case OP_VREVERSE: |
1199 | 10.7k | case OP_RREF: |
1200 | 10.7k | case OP_SCOND: |
1201 | 10.8k | case OP_SET_SOM: |
1202 | 10.8k | case OP_SKIP: |
1203 | 10.8k | case OP_SKIP_ARG: |
1204 | 10.9k | case OP_SOD: |
1205 | 10.9k | case OP_SOM: |
1206 | 11.0k | case OP_THEN: |
1207 | 11.1k | case OP_THEN_ARG: |
1208 | 11.1k | return SSB_FAIL; |
1209 | | |
1210 | | /* OP_CIRC happens only at the start of an anchored branch (multiline ^ |
1211 | | uses OP_CIRCM). Skip over it. */ |
1212 | | |
1213 | 6.36k | case OP_CIRC: |
1214 | 6.36k | tcode += PRIV(OP_lengths)[OP_CIRC]; |
1215 | 6.36k | break; |
1216 | | |
1217 | | /* A "real" property test implies no starting bits, but the fake property |
1218 | | PT_CLIST identifies a list of characters. These lists are short, as they |
1219 | | are used for characters with more than one "other case", so there is no |
1220 | | point in recognizing them for OP_NOTPROP. */ |
1221 | | |
1222 | 2.22k | case OP_PROP: |
1223 | 2.22k | if (tcode[1] != PT_CLIST) return SSB_FAIL; |
1224 | 1.78k | { |
1225 | 1.78k | const uint32_t *p = PRIV(ucd_caseless_sets) + tcode[2]; |
1226 | 7.13k | while ((c = *p++) < NOTACHAR) |
1227 | 5.35k | { |
1228 | 5.35k | #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 |
1229 | 5.35k | if (utf) |
1230 | 4.21k | { |
1231 | 4.21k | PCRE2_UCHAR buff[6]; |
1232 | 4.21k | (void)PRIV(ord2utf)(c, buff); |
1233 | 4.21k | c = buff[0]; |
1234 | 4.21k | } |
1235 | 5.35k | #endif |
1236 | 5.35k | if (c > 0xff) SET_BIT(0xff); else SET_BIT(c); |
1237 | 5.35k | } |
1238 | 1.78k | } |
1239 | 1.78k | try_next = FALSE; |
1240 | 1.78k | break; |
1241 | | |
1242 | | /* We can ignore word boundary tests. */ |
1243 | | |
1244 | 910 | case OP_WORD_BOUNDARY: |
1245 | 2.42k | case OP_NOT_WORD_BOUNDARY: |
1246 | 3.43k | case OP_UCP_WORD_BOUNDARY: |
1247 | 5.74k | case OP_NOT_UCP_WORD_BOUNDARY: |
1248 | 5.74k | tcode++; |
1249 | 5.74k | break; |
1250 | | |
1251 | | /* For a positive lookahead assertion, inspect what immediately follows, |
1252 | | ignoring intermediate assertions and callouts. If the next item is one |
1253 | | that sets a mandatory character, skip this assertion. Otherwise, treat it |
1254 | | the same as other bracket groups. */ |
1255 | | |
1256 | 11.3k | case OP_ASSERT: |
1257 | 63.2k | case OP_ASSERT_NA: |
1258 | 63.2k | ncode = tcode + GET(tcode, 1); |
1259 | 214k | while (*ncode == OP_ALT) ncode += GET(ncode, 1); |
1260 | 63.2k | ncode += 1 + LINK_SIZE; |
1261 | | |
1262 | | /* Skip irrelevant items */ |
1263 | | |
1264 | 10.5M | for (BOOL done = FALSE; !done;) |
1265 | 10.5M | { |
1266 | 10.5M | switch (*ncode) |
1267 | 10.5M | { |
1268 | 1.44M | case OP_ASSERT: |
1269 | 1.44M | case OP_ASSERT_NOT: |
1270 | 1.45M | case OP_ASSERTBACK: |
1271 | 1.47M | case OP_ASSERTBACK_NOT: |
1272 | 10.4M | case OP_ASSERT_NA: |
1273 | 10.4M | case OP_ASSERTBACK_NA: |
1274 | 10.4M | case OP_ASSERT_SCS: |
1275 | 10.4M | ncode += GET(ncode, 1); |
1276 | 34.7M | while (*ncode == OP_ALT) ncode += GET(ncode, 1); |
1277 | 10.4M | ncode += 1 + LINK_SIZE; |
1278 | 10.4M | break; |
1279 | | |
1280 | 3.49k | case OP_WORD_BOUNDARY: |
1281 | 5.69k | case OP_NOT_WORD_BOUNDARY: |
1282 | 6.99k | case OP_UCP_WORD_BOUNDARY: |
1283 | 7.40k | case OP_NOT_UCP_WORD_BOUNDARY: |
1284 | 7.40k | ncode++; |
1285 | 7.40k | break; |
1286 | | |
1287 | 5.66k | case OP_CALLOUT: |
1288 | 5.66k | ncode += PRIV(OP_lengths)[OP_CALLOUT]; |
1289 | 5.66k | break; |
1290 | | |
1291 | 332 | case OP_CALLOUT_STR: |
1292 | 332 | ncode += GET(ncode, 1 + 2*LINK_SIZE); |
1293 | 332 | break; |
1294 | | |
1295 | 63.2k | default: |
1296 | 63.2k | done = TRUE; |
1297 | 63.2k | break; |
1298 | 10.5M | } |
1299 | 10.5M | } |
1300 | | |
1301 | | /* Now check the next significant item. */ |
1302 | | |
1303 | 63.2k | switch(*ncode) |
1304 | 63.2k | { |
1305 | 41.5k | default: |
1306 | 41.5k | break; |
1307 | | |
1308 | 41.5k | case OP_PROP: |
1309 | 1.24k | if (ncode[1] != PT_CLIST) break; |
1310 | 357 | PCRE2_FALLTHROUGH /* Fall through */ |
1311 | 644 | case OP_ANYNL: |
1312 | 4.37k | case OP_CHAR: |
1313 | 5.15k | case OP_CHARI: |
1314 | 5.50k | case OP_EXACT: |
1315 | 5.66k | case OP_EXACTI: |
1316 | 7.02k | case OP_HSPACE: |
1317 | 7.43k | case OP_MINPLUS: |
1318 | 7.91k | case OP_MINPLUSI: |
1319 | 8.61k | case OP_PLUS: |
1320 | 9.07k | case OP_PLUSI: |
1321 | 10.6k | case OP_POSPLUS: |
1322 | 11.2k | case OP_POSPLUSI: |
1323 | 11.5k | case OP_VSPACE: |
1324 | | /* Note that these types will only be present in non-UCP mode. */ |
1325 | 13.8k | case OP_DIGIT: |
1326 | 16.2k | case OP_NOT_DIGIT: |
1327 | 18.9k | case OP_WORDCHAR: |
1328 | 19.7k | case OP_NOT_WORDCHAR: |
1329 | 20.6k | case OP_WHITESPACE: |
1330 | 20.8k | case OP_NOT_WHITESPACE: |
1331 | 20.8k | tcode = ncode; |
1332 | 20.8k | continue; /* With the following significant opcode */ |
1333 | 63.2k | } |
1334 | 42.3k | PCRE2_FALLTHROUGH /* Fall through */ |
1335 | 42.3k | |
1336 | 42.3k | /* For a group bracket or a positive assertion without an immediately |
1337 | 42.3k | following mandatory setting, recurse to set bits from within the |
1338 | 42.3k | subpattern. If it can't find anything, we have to give up. If it finds |
1339 | 42.3k | some mandatory character(s), we are done for this branch. Otherwise, |
1340 | 42.3k | carry on scanning after the subpattern. */ |
1341 | 42.3k | |
1342 | 74.6k | case OP_BRA: |
1343 | 75.3k | case OP_SBRA: |
1344 | 160k | case OP_CBRA: |
1345 | 161k | case OP_SCBRA: |
1346 | 161k | case OP_BRAPOS: |
1347 | 162k | case OP_SBRAPOS: |
1348 | 164k | case OP_CBRAPOS: |
1349 | 165k | case OP_SCBRAPOS: |
1350 | 169k | case OP_ONCE: |
1351 | 169k | case OP_SCRIPT_RUN: |
1352 | 169k | rc = set_start_bits(re, tcode, utf, ucp, depthptr); |
1353 | 169k | if (rc == SSB_DONE) |
1354 | 31.3k | { |
1355 | 31.3k | try_next = FALSE; |
1356 | 31.3k | } |
1357 | 138k | else if (rc == SSB_CONTINUE) |
1358 | 135k | { |
1359 | 1.24M | do tcode += GET(tcode, 1); while (*tcode == OP_ALT); |
1360 | 135k | tcode += 1 + LINK_SIZE; |
1361 | 135k | } |
1362 | 2.64k | else return rc; /* FAIL, UNKNOWN, or TOODEEP */ |
1363 | 166k | break; |
1364 | | |
1365 | | /* If we hit ALT or KET, it means we haven't found anything mandatory in |
1366 | | this branch, though we might have found something optional. For ALT, we |
1367 | | continue with the next alternative, but we have to arrange that the final |
1368 | | result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET, |
1369 | | return SSB_CONTINUE: if this is the top level, that indicates failure, |
1370 | | but after a nested subpattern, it causes scanning to continue. */ |
1371 | | |
1372 | 997k | case OP_ALT: |
1373 | 997k | yield = SSB_CONTINUE; |
1374 | 997k | try_next = FALSE; |
1375 | 997k | break; |
1376 | | |
1377 | 57.4k | case OP_KET: |
1378 | 63.7k | case OP_KETRMAX: |
1379 | 69.3k | case OP_KETRMIN: |
1380 | 72.4k | case OP_KETRPOS: |
1381 | 72.4k | return SSB_CONTINUE; |
1382 | | |
1383 | | /* Skip over callout */ |
1384 | | |
1385 | 89.2k | case OP_CALLOUT: |
1386 | 89.2k | tcode += PRIV(OP_lengths)[OP_CALLOUT]; |
1387 | 89.2k | break; |
1388 | | |
1389 | 365 | case OP_CALLOUT_STR: |
1390 | 365 | tcode += GET(tcode, 1 + 2*LINK_SIZE); |
1391 | 365 | break; |
1392 | | |
1393 | | /* Skip over lookbehind, negative lookahead, and scan substring |
1394 | | assertions */ |
1395 | | |
1396 | 97.5k | case OP_ASSERT_NOT: |
1397 | 183k | case OP_ASSERTBACK: |
1398 | 194k | case OP_ASSERTBACK_NOT: |
1399 | 204k | case OP_ASSERTBACK_NA: |
1400 | 228k | case OP_ASSERT_SCS: |
1401 | 893k | do tcode += GET(tcode, 1); while (*tcode == OP_ALT); |
1402 | 228k | tcode += 1 + LINK_SIZE; |
1403 | 228k | break; |
1404 | | |
1405 | | /* BRAZERO does the bracket, but carries on. */ |
1406 | | |
1407 | 51.6k | case OP_BRAZERO: |
1408 | 60.6k | case OP_BRAMINZERO: |
1409 | 62.6k | case OP_BRAPOSZERO: |
1410 | 62.6k | rc = set_start_bits(re, ++tcode, utf, ucp, depthptr); |
1411 | 62.6k | if (rc == SSB_FAIL || rc == SSB_UNKNOWN || rc == SSB_TOODEEP) return rc; |
1412 | 61.8k | do tcode += GET(tcode,1); while (*tcode == OP_ALT); |
1413 | 37.2k | tcode += 1 + LINK_SIZE; |
1414 | 37.2k | break; |
1415 | | |
1416 | | /* SKIPZERO skips the bracket. */ |
1417 | | |
1418 | 1.03k | case OP_SKIPZERO: |
1419 | 1.03k | tcode++; |
1420 | 6.06k | do tcode += GET(tcode,1); while (*tcode == OP_ALT); |
1421 | 1.03k | tcode += 1 + LINK_SIZE; |
1422 | 1.03k | break; |
1423 | | |
1424 | | /* Single-char * or ? sets the bit and tries the next item */ |
1425 | | |
1426 | 4.32k | case OP_STAR: |
1427 | 6.11k | case OP_MINSTAR: |
1428 | 14.9k | case OP_POSSTAR: |
1429 | 23.2k | case OP_QUERY: |
1430 | 24.8k | case OP_MINQUERY: |
1431 | 28.8k | case OP_POSQUERY: |
1432 | 28.8k | tcode = set_table_bit(re, tcode + 1, FALSE, utf, ucp); |
1433 | 28.8k | break; |
1434 | | |
1435 | 2.87k | case OP_STARI: |
1436 | 3.90k | case OP_MINSTARI: |
1437 | 5.21k | case OP_POSSTARI: |
1438 | 6.98k | case OP_QUERYI: |
1439 | 8.49k | case OP_MINQUERYI: |
1440 | 10.3k | case OP_POSQUERYI: |
1441 | 10.3k | tcode = set_table_bit(re, tcode + 1, TRUE, utf, ucp); |
1442 | 10.3k | break; |
1443 | | |
1444 | | /* Single-char upto sets the bit and tries the next */ |
1445 | | |
1446 | 1.52k | case OP_UPTO: |
1447 | 2.02k | case OP_MINUPTO: |
1448 | 3.31k | case OP_POSUPTO: |
1449 | 3.31k | tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, FALSE, utf, ucp); |
1450 | 3.31k | break; |
1451 | | |
1452 | 759 | case OP_UPTOI: |
1453 | 1.14k | case OP_MINUPTOI: |
1454 | 2.54k | case OP_POSUPTOI: |
1455 | 2.54k | tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, TRUE, utf, ucp); |
1456 | 2.54k | break; |
1457 | | |
1458 | | /* At least one single char sets the bit and stops */ |
1459 | | |
1460 | 1.11k | case OP_EXACT: |
1461 | 1.11k | tcode += IMM2_SIZE; |
1462 | 1.11k | PCRE2_FALLTHROUGH /* Fall through */ |
1463 | 197k | case OP_CHAR: |
1464 | 202k | case OP_PLUS: |
1465 | 202k | case OP_MINPLUS: |
1466 | 209k | case OP_POSPLUS: |
1467 | 209k | (void)set_table_bit(re, tcode + 1, FALSE, utf, ucp); |
1468 | 209k | try_next = FALSE; |
1469 | 209k | break; |
1470 | | |
1471 | 1.62k | case OP_EXACTI: |
1472 | 1.62k | tcode += IMM2_SIZE; |
1473 | 1.62k | PCRE2_FALLTHROUGH /* Fall through */ |
1474 | 88.9k | case OP_CHARI: |
1475 | 90.6k | case OP_PLUSI: |
1476 | 92.3k | case OP_MINPLUSI: |
1477 | 94.9k | case OP_POSPLUSI: |
1478 | 94.9k | (void)set_table_bit(re, tcode + 1, TRUE, utf, ucp); |
1479 | 94.9k | try_next = FALSE; |
1480 | 94.9k | break; |
1481 | | |
1482 | | /* Special spacing and line-terminating items. These recognize specific |
1483 | | lists of characters. The difference between VSPACE and ANYNL is that the |
1484 | | latter can match the two-character CRLF sequence, but that is not |
1485 | | relevant for finding the first character, so their code here is |
1486 | | identical. */ |
1487 | | |
1488 | 4.82k | case OP_HSPACE: |
1489 | 4.82k | SET_BIT(CHAR_HT); |
1490 | 4.82k | SET_BIT(CHAR_SPACE); |
1491 | | |
1492 | | /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set |
1493 | | the bits for NBSP and for code units >= 255, independently of UTF. */ |
1494 | | |
1495 | | #if PCRE2_CODE_UNIT_WIDTH != 8 |
1496 | | SET_BIT(CHAR_NBSP); |
1497 | | SET_BIT(0xFF); |
1498 | | #else |
1499 | | /* For the 8-bit library in UTF-8 mode, set the bits for the first code |
1500 | | units of horizontal space characters. */ |
1501 | | |
1502 | 4.82k | #ifdef SUPPORT_UNICODE |
1503 | 4.82k | if (utf) |
1504 | 1.37k | { |
1505 | 1.37k | SET_BIT(0xC2); /* For U+00A0 */ |
1506 | 1.37k | SET_BIT(0xE1); /* For U+1680, U+180E */ |
1507 | 1.37k | SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */ |
1508 | 1.37k | SET_BIT(0xE3); /* For U+3000 */ |
1509 | 1.37k | } |
1510 | 3.45k | else |
1511 | 3.45k | #endif |
1512 | | /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */ |
1513 | 3.45k | { |
1514 | 3.45k | SET_BIT(CHAR_NBSP); |
1515 | 3.45k | } |
1516 | 4.82k | #endif /* 8-bit support */ |
1517 | | |
1518 | 4.82k | try_next = FALSE; |
1519 | 4.82k | break; |
1520 | | |
1521 | 2.24k | case OP_ANYNL: |
1522 | 2.79k | case OP_VSPACE: |
1523 | 2.79k | SET_BIT(CHAR_LF); |
1524 | 2.79k | SET_BIT(CHAR_VT); |
1525 | 2.79k | SET_BIT(CHAR_FF); |
1526 | 2.79k | SET_BIT(CHAR_CR); |
1527 | | |
1528 | | /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set |
1529 | | the bits for NEL and for code units >= 255, independently of UTF. */ |
1530 | | |
1531 | | #if PCRE2_CODE_UNIT_WIDTH != 8 |
1532 | | SET_BIT(CHAR_NEL); |
1533 | | SET_BIT(0xFF); |
1534 | | #else |
1535 | | /* For the 8-bit library in UTF-8 mode, set the bits for the first code |
1536 | | units of vertical space characters. */ |
1537 | | |
1538 | 2.79k | #ifdef SUPPORT_UNICODE |
1539 | 2.79k | if (utf) |
1540 | 473 | { |
1541 | 473 | SET_BIT(0xC2); /* For U+0085 (NEL) */ |
1542 | 473 | SET_BIT(0xE2); /* For U+2028, U+2029 */ |
1543 | 473 | } |
1544 | 2.32k | else |
1545 | 2.32k | #endif |
1546 | | /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */ |
1547 | 2.32k | { |
1548 | 2.32k | SET_BIT(CHAR_NEL); |
1549 | 2.32k | } |
1550 | 2.79k | #endif /* 8-bit support */ |
1551 | | |
1552 | 2.79k | try_next = FALSE; |
1553 | 2.79k | break; |
1554 | | |
1555 | | /* Single character types set the bits and stop. Note that if PCRE2_UCP |
1556 | | is set, we do not see these opcodes because \d etc are converted to |
1557 | | properties. Therefore, these apply in the case when only characters less |
1558 | | than 256 are recognized to match the types. */ |
1559 | | |
1560 | 5.64k | case OP_NOT_DIGIT: |
1561 | 5.64k | set_nottype_bits(re, cbit_digit, table_limit); |
1562 | 5.64k | try_next = FALSE; |
1563 | 5.64k | break; |
1564 | | |
1565 | 19.7k | case OP_DIGIT: |
1566 | 19.7k | set_type_bits(re, cbit_digit, table_limit); |
1567 | 19.7k | try_next = FALSE; |
1568 | 19.7k | break; |
1569 | | |
1570 | 14.4k | case OP_NOT_WHITESPACE: |
1571 | 14.4k | set_nottype_bits(re, cbit_space, table_limit); |
1572 | 14.4k | try_next = FALSE; |
1573 | 14.4k | break; |
1574 | | |
1575 | 11.6k | case OP_WHITESPACE: |
1576 | 11.6k | set_type_bits(re, cbit_space, table_limit); |
1577 | 11.6k | try_next = FALSE; |
1578 | 11.6k | break; |
1579 | | |
1580 | 8.64k | case OP_NOT_WORDCHAR: |
1581 | 8.64k | set_nottype_bits(re, cbit_word, table_limit); |
1582 | 8.64k | try_next = FALSE; |
1583 | 8.64k | break; |
1584 | | |
1585 | 29.2k | case OP_WORDCHAR: |
1586 | 29.2k | set_type_bits(re, cbit_word, table_limit); |
1587 | 29.2k | try_next = FALSE; |
1588 | 29.2k | break; |
1589 | | |
1590 | | /* One or more character type fudges the pointer and restarts, knowing |
1591 | | it will hit a single character type and stop there. */ |
1592 | | |
1593 | 5.20k | case OP_TYPEPLUS: |
1594 | 8.31k | case OP_TYPEMINPLUS: |
1595 | 18.2k | case OP_TYPEPOSPLUS: |
1596 | 18.2k | tcode++; |
1597 | 18.2k | break; |
1598 | | |
1599 | 3.15k | case OP_TYPEEXACT: |
1600 | 3.15k | tcode += 1 + IMM2_SIZE; |
1601 | 3.15k | break; |
1602 | | |
1603 | | /* Zero or more repeats of character types set the bits and then |
1604 | | try again. */ |
1605 | | |
1606 | 552 | case OP_TYPEUPTO: |
1607 | 1.98k | case OP_TYPEMINUPTO: |
1608 | 3.29k | case OP_TYPEPOSUPTO: |
1609 | 3.29k | tcode += IMM2_SIZE; |
1610 | 3.29k | PCRE2_FALLTHROUGH /* Fall through */ |
1611 | | |
1612 | 13.0k | case OP_TYPESTAR: |
1613 | 15.9k | case OP_TYPEMINSTAR: |
1614 | 18.1k | case OP_TYPEPOSSTAR: |
1615 | 34.3k | case OP_TYPEQUERY: |
1616 | 42.8k | case OP_TYPEMINQUERY: |
1617 | 49.7k | case OP_TYPEPOSQUERY: |
1618 | 49.7k | switch(tcode[1]) |
1619 | 49.7k | { |
1620 | 1.44k | default: |
1621 | 2.46k | case OP_ANY: |
1622 | 2.70k | case OP_ALLANY: |
1623 | 2.70k | return SSB_FAIL; |
1624 | | |
1625 | 8.77k | case OP_HSPACE: |
1626 | 8.77k | SET_BIT(CHAR_HT); |
1627 | 8.77k | SET_BIT(CHAR_SPACE); |
1628 | | |
1629 | | /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set |
1630 | | the bits for NBSP and for code units >= 255, independently of UTF. */ |
1631 | | |
1632 | | #if PCRE2_CODE_UNIT_WIDTH != 8 |
1633 | | SET_BIT(CHAR_NBSP); |
1634 | | SET_BIT(0xFF); |
1635 | | #else |
1636 | | /* For the 8-bit library in UTF-8 mode, set the bits for the first code |
1637 | | units of horizontal space characters. */ |
1638 | | |
1639 | 8.77k | #ifdef SUPPORT_UNICODE |
1640 | 8.77k | if (utf) |
1641 | 1.12k | { |
1642 | 1.12k | SET_BIT(0xC2); /* For U+00A0 */ |
1643 | 1.12k | SET_BIT(0xE1); /* For U+1680, U+180E */ |
1644 | 1.12k | SET_BIT(0xE2); /* For U+2000 - U+200A, U+202F, U+205F */ |
1645 | 1.12k | SET_BIT(0xE3); /* For U+3000 */ |
1646 | 1.12k | } |
1647 | 7.65k | else |
1648 | 7.65k | #endif |
1649 | | /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */ |
1650 | 7.65k | { |
1651 | 7.65k | SET_BIT(CHAR_NBSP); |
1652 | 7.65k | } |
1653 | 8.77k | #endif /* 8-bit support */ |
1654 | 8.77k | break; |
1655 | | |
1656 | 1.39k | case OP_ANYNL: |
1657 | 2.75k | case OP_VSPACE: |
1658 | 2.75k | SET_BIT(CHAR_LF); |
1659 | 2.75k | SET_BIT(CHAR_VT); |
1660 | 2.75k | SET_BIT(CHAR_FF); |
1661 | 2.75k | SET_BIT(CHAR_CR); |
1662 | | |
1663 | | /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set |
1664 | | the bits for NEL and for code units >= 255, independently of UTF. */ |
1665 | | |
1666 | | #if PCRE2_CODE_UNIT_WIDTH != 8 |
1667 | | SET_BIT(CHAR_NEL); |
1668 | | SET_BIT(0xFF); |
1669 | | #else |
1670 | | /* For the 8-bit library in UTF-8 mode, set the bits for the first code |
1671 | | units of vertical space characters. */ |
1672 | | |
1673 | 2.75k | #ifdef SUPPORT_UNICODE |
1674 | 2.75k | if (utf) |
1675 | 418 | { |
1676 | 418 | SET_BIT(0xC2); /* For U+0085 (NEL) */ |
1677 | 418 | SET_BIT(0xE2); /* For U+2028, U+2029 */ |
1678 | 418 | } |
1679 | 2.33k | else |
1680 | 2.33k | #endif |
1681 | | /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */ |
1682 | 2.33k | { |
1683 | 2.33k | SET_BIT(CHAR_NEL); |
1684 | 2.33k | } |
1685 | 2.75k | #endif /* 8-bit support */ |
1686 | 2.75k | break; |
1687 | | |
1688 | 3.64k | case OP_NOT_DIGIT: |
1689 | 3.64k | set_nottype_bits(re, cbit_digit, table_limit); |
1690 | 3.64k | break; |
1691 | | |
1692 | 7.25k | case OP_DIGIT: |
1693 | 7.25k | set_type_bits(re, cbit_digit, table_limit); |
1694 | 7.25k | break; |
1695 | | |
1696 | 7.57k | case OP_NOT_WHITESPACE: |
1697 | 7.57k | set_nottype_bits(re, cbit_space, table_limit); |
1698 | 7.57k | break; |
1699 | | |
1700 | 5.44k | case OP_WHITESPACE: |
1701 | 5.44k | set_type_bits(re, cbit_space, table_limit); |
1702 | 5.44k | break; |
1703 | | |
1704 | 4.52k | case OP_NOT_WORDCHAR: |
1705 | 4.52k | set_nottype_bits(re, cbit_word, table_limit); |
1706 | 4.52k | break; |
1707 | | |
1708 | 7.09k | case OP_WORDCHAR: |
1709 | 7.09k | set_type_bits(re, cbit_word, table_limit); |
1710 | 7.09k | break; |
1711 | 49.7k | } |
1712 | | |
1713 | 47.0k | tcode += 2; |
1714 | 47.0k | break; |
1715 | | |
1716 | | /* Set-based ECLASS: treat it the same as a "complex" XCLASS; give up. */ |
1717 | | |
1718 | 0 | #ifdef SUPPORT_WIDE_CHARS |
1719 | 99 | case OP_ECLASS: |
1720 | 99 | return SSB_FAIL; |
1721 | 0 | #endif |
1722 | | |
1723 | | /* Extended class: if there are any property checks, or if this is a |
1724 | | negative XCLASS without a map, give up. If there are no property checks, |
1725 | | there must be wide characters on the XCLASS list, because otherwise an |
1726 | | XCLASS would not have been created. This means that code points >= 255 |
1727 | | are potential starters. In the UTF-8 case we can scan them and set bits |
1728 | | for the relevant leading bytes. */ |
1729 | | |
1730 | 0 | #ifdef SUPPORT_WIDE_CHARS |
1731 | 17.9k | case OP_XCLASS: |
1732 | 17.9k | xclassflags = tcode[1 + LINK_SIZE]; |
1733 | 17.9k | if ((xclassflags & XCL_HASPROP) != 0 || |
1734 | 17.6k | (xclassflags & (XCL_MAP|XCL_NOT)) == XCL_NOT) |
1735 | 316 | return SSB_FAIL; |
1736 | | |
1737 | | /* We have a positive XCLASS or a negative one without a map. Set up the |
1738 | | map pointer if there is one, and fall through. */ |
1739 | | |
1740 | 17.6k | classmap = ((xclassflags & XCL_MAP) == 0)? NULL : |
1741 | 17.6k | (const uint8_t *)(tcode + 1 + LINK_SIZE + 1); |
1742 | | |
1743 | | /* In UTF-8 mode, scan the character list and set bits for leading bytes, |
1744 | | then jump to handle the map. */ |
1745 | | |
1746 | 17.6k | #if PCRE2_CODE_UNIT_WIDTH == 8 |
1747 | 17.6k | if (utf && (xclassflags & XCL_NOT) == 0) |
1748 | 12.0k | { |
1749 | 12.0k | PCRE2_UCHAR b, e; |
1750 | 12.0k | PCRE2_SPTR p = tcode + 1 + LINK_SIZE + 1 + ((classmap == NULL)? 0:32); |
1751 | 12.0k | tcode += GET(tcode, 1); |
1752 | | |
1753 | 12.0k | if (*p >= XCL_LIST) |
1754 | 2.18k | { |
1755 | 2.18k | study_char_list(p, re->start_bitmap, |
1756 | 2.18k | ((const uint8_t *)re + re->code_start)); |
1757 | 2.18k | goto HANDLE_CLASSMAP; |
1758 | 2.18k | } |
1759 | | |
1760 | 27.3k | for (;;) switch (*p++) |
1761 | 27.3k | { |
1762 | 16.9k | case XCL_SINGLE: |
1763 | 16.9k | b = *p++; |
1764 | 49.4k | while ((*p & 0xc0) == 0x80) p++; |
1765 | 16.9k | re->start_bitmap[b/8] |= (1u << (b&7)); |
1766 | 16.9k | break; |
1767 | | |
1768 | 543 | case XCL_RANGE: |
1769 | 543 | b = *p++; |
1770 | 1.27k | while ((*p & 0xc0) == 0x80) p++; |
1771 | 543 | e = *p++; |
1772 | 1.53k | while ((*p & 0xc0) == 0x80) p++; |
1773 | 7.71k | for (; b <= e; b++) |
1774 | 7.17k | re->start_bitmap[b/8] |= (1u << (b&7)); |
1775 | 543 | break; |
1776 | | |
1777 | 9.84k | case XCL_END: |
1778 | 9.84k | goto HANDLE_CLASSMAP; |
1779 | | |
1780 | | /* LCOV_EXCL_START */ |
1781 | 0 | default: |
1782 | 0 | PCRE2_DEBUG_UNREACHABLE(); |
1783 | 0 | return SSB_UNKNOWN; /* Internal error, should not occur */ |
1784 | | /* LCOV_EXCL_STOP */ |
1785 | 27.3k | } |
1786 | 9.84k | } |
1787 | 5.58k | #endif /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */ |
1788 | 5.58k | #endif /* SUPPORT_WIDE_CHARS */ |
1789 | | |
1790 | | /* It seems that the fall through comment must be outside the #ifdef if |
1791 | | it is to avoid the gcc compiler warning. */ |
1792 | | |
1793 | 5.58k | PCRE2_FALLTHROUGH /* Fall through */ |
1794 | 5.58k | |
1795 | 5.58k | /* Enter here for a negative non-XCLASS. In the 8-bit library, if we are |
1796 | 5.58k | in UTF mode, any byte with a value >= 0xc4 is a potentially valid starter |
1797 | 5.58k | because it starts a character with a value > 255. In 8-bit non-UTF mode, |
1798 | 5.58k | there is no difference between CLASS and NCLASS. In all other wide |
1799 | 5.58k | character modes, set the 0xFF bit to indicate code units >= 255. */ |
1800 | 5.58k | |
1801 | 22.2k | case OP_NCLASS: |
1802 | 22.2k | #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 |
1803 | 22.2k | if (utf) |
1804 | 10.3k | { |
1805 | 10.3k | re->start_bitmap[24] |= 0xf0; /* Bits for 0xc4 - 0xc8 */ |
1806 | 10.3k | memset(re->start_bitmap+25, 0xff, 7); /* Bits for 0xc9 - 0xff */ |
1807 | 10.3k | } |
1808 | 22.2k | PCRE2_FALLTHROUGH /* Fall through */ |
1809 | | #elif PCRE2_CODE_UNIT_WIDTH != 8 |
1810 | | SET_BIT(0xFF); /* For characters >= 255 */ |
1811 | | PCRE2_FALLTHROUGH /* Fall through */ |
1812 | | #endif |
1813 | | |
1814 | | /* Enter here for a positive non-XCLASS. If we have fallen through from |
1815 | | an XCLASS, classmap will already be set; just advance the code pointer. |
1816 | | Otherwise, set up classmap for a non-XCLASS and advance past it. */ |
1817 | | |
1818 | 61.3k | case OP_CLASS: |
1819 | 61.3k | if (*tcode == OP_XCLASS) tcode += GET(tcode, 1); else |
1820 | 55.7k | { |
1821 | 55.7k | classmap = (const uint8_t *)(++tcode); |
1822 | 55.7k | tcode += 32 / sizeof(PCRE2_UCHAR); |
1823 | 55.7k | } |
1824 | | |
1825 | | /* When wide characters are supported, classmap may be NULL. In UTF-8 |
1826 | | (sic) mode, the bits in a class bit map correspond to character values, |
1827 | | not to byte values. However, the bit map we are constructing is for byte |
1828 | | values. So we have to do a conversion for characters whose code point is |
1829 | | greater than 127. In fact, there are only two possible starting bytes for |
1830 | | characters in the range 128 - 255. */ |
1831 | | |
1832 | 61.3k | #if defined SUPPORT_WIDE_CHARS && PCRE2_CODE_UNIT_WIDTH == 8 |
1833 | 73.3k | HANDLE_CLASSMAP: |
1834 | 73.3k | #endif |
1835 | 73.3k | if (classmap != NULL) |
1836 | 67.3k | { |
1837 | 67.3k | #if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 |
1838 | 67.3k | if (utf) |
1839 | 24.6k | { |
1840 | 419k | for (c = 0; c < 16; c++) re->start_bitmap[c] |= classmap[c]; |
1841 | 2.22M | for (c = 128; c < 256; c++) |
1842 | 2.20M | { |
1843 | 2.20M | if ((classmap[c/8] & (1u << (c&7))) != 0) |
1844 | 15.5k | { |
1845 | 15.5k | int d = (c >> 6) | 0xc0; /* Set bit for this starter */ |
1846 | 15.5k | re->start_bitmap[d/8] |= (1u << (d&7)); /* and then skip on to the */ |
1847 | 15.5k | c = (c & 0xc0) + 0x40 - 1; /* next relevant character. */ |
1848 | 15.5k | } |
1849 | 2.20M | } |
1850 | 24.6k | } |
1851 | 42.6k | else |
1852 | 42.6k | #endif |
1853 | | /* In all modes except UTF-8, the two bit maps are compatible. */ |
1854 | | |
1855 | 42.6k | { |
1856 | 1.40M | for (c = 0; c < 32; c++) re->start_bitmap[c] |= classmap[c]; |
1857 | 42.6k | } |
1858 | 67.3k | } |
1859 | | |
1860 | | /* Act on what follows the class. For a zero minimum repeat, continue; |
1861 | | otherwise stop processing. */ |
1862 | | |
1863 | 73.3k | switch (*tcode) |
1864 | 73.3k | { |
1865 | 3.87k | case OP_CRSTAR: |
1866 | 4.71k | case OP_CRMINSTAR: |
1867 | 7.83k | case OP_CRQUERY: |
1868 | 12.8k | case OP_CRMINQUERY: |
1869 | 14.2k | case OP_CRPOSSTAR: |
1870 | 21.8k | case OP_CRPOSQUERY: |
1871 | 21.8k | tcode++; |
1872 | 21.8k | break; |
1873 | | |
1874 | 1.50k | case OP_CRRANGE: |
1875 | 1.93k | case OP_CRMINRANGE: |
1876 | 7.64k | case OP_CRPOSRANGE: |
1877 | 7.64k | if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE; |
1878 | 2.87k | else try_next = FALSE; |
1879 | 7.64k | break; |
1880 | | |
1881 | 43.8k | default: |
1882 | 43.8k | try_next = FALSE; |
1883 | 43.8k | break; |
1884 | 73.3k | } |
1885 | 73.3k | break; /* End of class handling case */ |
1886 | 2.25M | } /* End of switch for opcodes */ |
1887 | 2.25M | } /* End of try_next loop */ |
1888 | | |
1889 | 1.47M | code += GET(code, 1); /* Advance to next branch */ |
1890 | 1.47M | } |
1891 | 1.47M | while (*code == OP_ALT); |
1892 | | |
1893 | 143k | return yield; |
1894 | 258k | } |
1895 | | |
1896 | | |
1897 | | |
1898 | | /************************************************* |
1899 | | * Study a compiled expression * |
1900 | | *************************************************/ |
1901 | | |
1902 | | /* This function is handed a compiled expression that it must study to produce |
1903 | | information that will speed up the matching. |
1904 | | |
1905 | | Argument: |
1906 | | re points to the compiled expression |
1907 | | |
1908 | | Returns: 0 normally; non-zero should never normally occur |
1909 | | 1 unknown opcode in set_start_bits |
1910 | | 2 missing capturing bracket |
1911 | | 3 unknown opcode in find_minlength |
1912 | | */ |
1913 | | |
1914 | | int |
1915 | | PRIV(study)(pcre2_real_code *re) |
1916 | 39.2k | { |
1917 | 39.2k | int count = 0; |
1918 | 39.2k | PCRE2_UCHAR *code; |
1919 | 39.2k | BOOL utf = (re->overall_options & PCRE2_UTF) != 0; |
1920 | 39.2k | BOOL ucp = (re->overall_options & PCRE2_UCP) != 0; |
1921 | | |
1922 | | /* Find start of compiled code */ |
1923 | | |
1924 | 39.2k | code = (PCRE2_UCHAR *)((uint8_t *)re + re->code_start); |
1925 | | |
1926 | | /* For a pattern that has a first code unit, or a multiline pattern that |
1927 | | matches only at "line start", there is no point in seeking a list of starting |
1928 | | code units. */ |
1929 | | |
1930 | 39.2k | if ((re->flags & (PCRE2_FIRSTSET|PCRE2_STARTLINE)) == 0) |
1931 | 26.4k | { |
1932 | 26.4k | int depth = 0; |
1933 | 26.4k | int rc = set_start_bits(re, code, utf, ucp, &depth); |
1934 | | /* LCOV_EXCL_START */ |
1935 | 26.4k | if (rc == SSB_UNKNOWN) |
1936 | 0 | { |
1937 | 0 | PCRE2_DEBUG_UNREACHABLE(); |
1938 | 0 | return 1; |
1939 | 0 | } |
1940 | | /* LCOV_EXCL_STOP */ |
1941 | | |
1942 | | /* If a list of starting code units was set up, scan the list to see if only |
1943 | | one or two were listed. Having only one listed is rare because usually a |
1944 | | single starting code unit will have been recognized and PCRE2_FIRSTSET set. |
1945 | | If two are listed, see if they are caseless versions of the same character; |
1946 | | if so we can replace the list with a caseless first code unit. This gives |
1947 | | better performance and is plausibly worth doing for patterns such as [Ww]ord |
1948 | | or (word|WORD). */ |
1949 | | |
1950 | 26.4k | if (rc == SSB_DONE) |
1951 | 8.90k | { |
1952 | 8.90k | int i; |
1953 | 8.90k | int a = -1; |
1954 | 8.90k | int b = -1; |
1955 | 8.90k | uint8_t *p = re->start_bitmap; |
1956 | 8.90k | uint32_t flags = PCRE2_FIRSTMAPSET; |
1957 | | |
1958 | 66.6k | for (i = 0; i < 256; p++, i += 8) |
1959 | 66.4k | { |
1960 | 66.4k | uint8_t x = *p; |
1961 | 66.4k | if (x != 0) |
1962 | 13.1k | { |
1963 | 13.1k | int c; |
1964 | 13.1k | uint8_t y = x & (~x + 1); /* Least significant bit */ |
1965 | 13.1k | if (y != x) goto DONE; /* More than one bit set */ |
1966 | | |
1967 | | /* In the 16-bit and 32-bit libraries, the bit for 0xff means "0xff and |
1968 | | all wide characters", so we cannot use it here. */ |
1969 | | |
1970 | | #if PCRE2_CODE_UNIT_WIDTH != 8 |
1971 | | if (i == 248 && x == 0x80) goto DONE; |
1972 | | #endif |
1973 | | |
1974 | | /* Compute the character value */ |
1975 | | |
1976 | 8.13k | c = i; |
1977 | 8.13k | switch (x) |
1978 | 8.13k | { |
1979 | 2.36k | case 1: break; |
1980 | 1.35k | case 2: c += 1; break; case 4: c += 2; break; |
1981 | 1.04k | case 8: c += 3; break; case 16: c += 4; break; |
1982 | 884 | case 32: c += 5; break; case 64: c += 6; break; |
1983 | 726 | case 128: c += 7; break; |
1984 | 8.13k | } |
1985 | | |
1986 | | /* c contains the code unit value, in the range 0-255. In 8-bit UTF |
1987 | | mode, only values < 128 can be used. In all the other cases, c is a |
1988 | | character value. */ |
1989 | | |
1990 | 8.13k | #if PCRE2_CODE_UNIT_WIDTH == 8 |
1991 | 8.13k | if (utf && c > 127) goto DONE; |
1992 | 8.02k | #endif |
1993 | 8.02k | if (a < 0) a = c; /* First one found, save in a */ |
1994 | 3.71k | else if (b < 0) /* Second one found */ |
1995 | 3.65k | { |
1996 | 3.65k | int d = TABLE_GET((unsigned int)c, re->tables + fcc_offset, c); |
1997 | | |
1998 | 3.65k | #ifdef SUPPORT_UNICODE |
1999 | 3.65k | if (utf || ucp) |
2000 | 1.01k | { |
2001 | 1.01k | if (UCD_CASESET(c) != 0) goto DONE; /* Multiple case set */ |
2002 | 910 | if (c > 127) d = UCD_OTHERCASE(c); |
2003 | 910 | } |
2004 | 3.55k | #endif /* SUPPORT_UNICODE */ |
2005 | | |
2006 | 3.55k | if (d != a) goto DONE; /* Not the other case of a */ |
2007 | 127 | b = c; /* Save second in b */ |
2008 | | |
2009 | | #ifdef EBCDIC |
2010 | | /* To match ASCII (which puts the uppercase one in a), swap a & b |
2011 | | if needed. This doesn't really matter, but neatens the tests. */ |
2012 | | if (TABLE_GET((unsigned int)a, re->tables + lcc_offset, a) == a) |
2013 | | { |
2014 | | b = a; |
2015 | | a = c; |
2016 | | } |
2017 | | #endif |
2018 | 127 | } |
2019 | 58 | else goto DONE; /* More than two characters found */ |
2020 | 8.02k | } |
2021 | 66.4k | } |
2022 | | |
2023 | | /* Replace the start code unit bits with a first code unit. If it is the |
2024 | | same as a required later code unit, then clear the required later code |
2025 | | unit. This is because a search for a required code unit starts after an |
2026 | | explicit first code unit, but at a code unit found from the bitmap. |
2027 | | Patterns such as /a*a/ don't work if both the start unit and required |
2028 | | unit are the same. */ |
2029 | | |
2030 | 202 | if (a >= 0) { |
2031 | 186 | if ((re->flags & PCRE2_LASTSET) && (re->last_codeunit == (uint32_t)a || (b >= 0 && re->last_codeunit == (uint32_t)b))) { |
2032 | 51 | re->flags &= ~(PCRE2_LASTSET | PCRE2_LASTCASELESS); |
2033 | 51 | re->last_codeunit = 0; |
2034 | 51 | } |
2035 | 186 | re->first_codeunit = a; |
2036 | 186 | flags = PCRE2_FIRSTSET; |
2037 | 186 | if (b >= 0) flags |= PCRE2_FIRSTCASELESS; |
2038 | 186 | } |
2039 | | |
2040 | 8.90k | DONE: |
2041 | 8.90k | re->flags |= flags; |
2042 | 8.90k | } |
2043 | 26.4k | } |
2044 | | |
2045 | | /* Find the minimum length of subject string. If the pattern can match an empty |
2046 | | string, the minimum length is already known. If the pattern contains (*ACCEPT) |
2047 | | all bets are off, and we don't even try to find a minimum length. If there are |
2048 | | more back references than the size of the vector we are going to cache them in, |
2049 | | do nothing. A pattern that complicated will probably take a long time to |
2050 | | analyze and may in any case turn out to be too complicated. Note that back |
2051 | | reference minima are held as 16-bit numbers. */ |
2052 | | |
2053 | 39.2k | if ((re->flags & (PCRE2_MATCH_EMPTY|PCRE2_HASACCEPT)) == 0 && |
2054 | 32.0k | re->top_backref <= MAX_CACHE_BACKREF) |
2055 | 31.9k | { |
2056 | 31.9k | int min; |
2057 | 31.9k | int backref_cache[MAX_CACHE_BACKREF+1]; |
2058 | 31.9k | backref_cache[0] = 0; /* Highest one that is set */ |
2059 | 31.9k | min = find_minlength(re, code, code, utf, NULL, &count, backref_cache); |
2060 | 31.9k | switch(min) |
2061 | 31.9k | { |
2062 | 155 | case -1: /* \C in UTF mode or over-complex regex */ |
2063 | 155 | break; /* Leave minlength unchanged (will be zero) */ |
2064 | | |
2065 | | /* LCOV_EXCL_START */ |
2066 | 0 | case -2: |
2067 | 0 | PCRE2_DEBUG_UNREACHABLE(); |
2068 | 0 | return 2; /* missing capturing bracket */ |
2069 | | /* LCOV_EXCL_STOP */ |
2070 | | |
2071 | | /* LCOV_EXCL_START */ |
2072 | 0 | case -3: |
2073 | 0 | PCRE2_DEBUG_UNREACHABLE(); |
2074 | 0 | return 3; /* unrecognized opcode */ |
2075 | | /* LCOV_EXCL_STOP */ |
2076 | | |
2077 | 31.8k | default: |
2078 | 31.8k | re->minlength = (min > (int)UINT16_MAX)? (int)UINT16_MAX : min; |
2079 | 31.8k | break; |
2080 | 31.9k | } |
2081 | 31.9k | } |
2082 | | |
2083 | 39.2k | return 0; |
2084 | 39.2k | } |
2085 | | |
2086 | | /* End of pcre2_study.c */ |