1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
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 (but see
7 below for why this module is different).
9 Written by Philip Hazel
10 Copyright (c) 1997-2014 University of Cambridge
12 -----------------------------------------------------------------------------
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
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.
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.
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 -----------------------------------------------------------------------------
41 /* This module contains the external function pcre_dfa_exec(), which is an
42 alternative matching function that uses a sort of DFA algorithm (not a true
43 FSM). This is NOT Perl-compatible, but it has advantages in certain
47 /* NOTE ABOUT PERFORMANCE: A user of this function sent some code that improved
48 the performance of his patterns greatly. I could not use it as it stood, as it
49 was not thread safe, and made assumptions about pattern sizes. Also, it caused
50 test 7 to loop, and test 9 to crash with a segfault.
52 The issue is the check for duplicate states, which is done by a simple linear
53 search up the state list. (Grep for "duplicate" below to find the code.) For
54 many patterns, there will never be many states active at one time, so a simple
55 linear search is fine. In patterns that have many active states, it might be a
56 bottleneck. The suggested code used an indexing scheme to remember which states
57 had previously been used for each character, and avoided the linear search when
58 it knew there was no chance of a duplicate. This was implemented when adding
59 states to the state lists.
61 I wrote some thread-safe, not-limited code to try something similar at the time
62 of checking for duplicates (instead of when adding states), using index vectors
63 on the stack. It did give a 13% improvement with one specially constructed
64 pattern for certain subject strings, but on other strings and on many of the
65 simpler patterns in the test suite it did worse. The major problem, I think,
66 was the extra time to initialize the index. This had to be done for each call
67 of internal_dfa_exec(). (The supplied patch used a static vector, initialized
68 only once - I suspect this was the cause of the problems with the tests.)
70 Overall, I concluded that the gains in some cases did not outweigh the losses
71 in others, so I abandoned this code. */
79 #define NLBLOCK md /* Block containing newline information */
80 #define PSSTART start_subject /* Field containing processed string start */
81 #define PSEND end_subject /* Field containing processed string end */
83 #include "pcre_internal.h"
86 /* For use to indent debugging output */
91 /*************************************************
92 * Code parameters and static tables *
93 *************************************************/
95 /* These are offsets that are used to turn the OP_TYPESTAR and friends opcodes
96 into others, under special conditions. A gap of 20 between the blocks should be
97 enough. The resulting opcodes don't have to be less than 256 because they are
98 never stored, so we push them well clear of the normal opcodes. */
100 #define OP_PROP_EXTRA 300
101 #define OP_EXTUNI_EXTRA 320
102 #define OP_ANYNL_EXTRA 340
103 #define OP_HSPACE_EXTRA 360
104 #define OP_VSPACE_EXTRA 380
107 /* This table identifies those opcodes that are followed immediately by a
108 character that is to be tested in some way. This makes it possible to
109 centralize the loading of these characters. In the case of Type * etc, the
110 "character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a
111 small value. Non-zero values in the table are the offsets from the opcode where
112 the character is to be found. ***NOTE*** If the start of this table is
113 modified, the three tables that follow must also be modified. */
115 static const pcre_uint8 coptable[] = {
117 0, 0, 0, 0, 0, /* \A, \G, \K, \B, \b */
118 0, 0, 0, 0, 0, 0, /* \D, \d, \S, \s, \W, \w */
119 0, 0, 0, /* Any, AllAny, Anybyte */
121 0, 0, 0, 0, 0, /* \R, \H, \h, \V, \v */
123 0, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */
128 /* Positive single-char repeats */
129 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
130 1+IMM2_SIZE, 1+IMM2_SIZE, /* upto, minupto */
131 1+IMM2_SIZE, /* exact */
132 1, 1, 1, 1+IMM2_SIZE, /* *+, ++, ?+, upto+ */
133 1, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */
134 1+IMM2_SIZE, 1+IMM2_SIZE, /* upto I, minupto I */
135 1+IMM2_SIZE, /* exact I */
136 1, 1, 1, 1+IMM2_SIZE, /* *+I, ++I, ?+I, upto+I */
137 /* Negative single-char repeats - only for chars < 256 */
138 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */
139 1+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto, minupto */
140 1+IMM2_SIZE, /* NOT exact */
141 1, 1, 1, 1+IMM2_SIZE, /* NOT *+, ++, ?+, upto+ */
142 1, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */
143 1+IMM2_SIZE, 1+IMM2_SIZE, /* NOT upto I, minupto I */
144 1+IMM2_SIZE, /* NOT exact I */
145 1, 1, 1, 1+IMM2_SIZE, /* NOT *+I, ++I, ?+I, upto+I */
146 /* Positive type repeats */
147 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */
148 1+IMM2_SIZE, 1+IMM2_SIZE, /* Type upto, minupto */
149 1+IMM2_SIZE, /* Type exact */
150 1, 1, 1, 1+IMM2_SIZE, /* Type *+, ++, ?+, upto+ */
151 /* Character class & ref repeats */
152 0, 0, 0, 0, 0, 0, /* *, *?, +, +?, ?, ?? */
153 0, 0, /* CRRANGE, CRMINRANGE */
154 0, 0, 0, 0, /* Possessive *+, ++, ?+, CRPOSRANGE */
157 0, /* XCLASS - variable length */
172 0, /* Assert behind */
173 0, /* Assert behind not */
174 0, 0, /* ONCE, ONCE_NC */
175 0, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */
176 0, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */
177 0, 0, /* CREF, DNCREF */
178 0, 0, /* RREF, DNRREF */
180 0, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */
181 0, 0, 0, /* MARK, PRUNE, PRUNE_ARG */
182 0, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */
183 0, 0, 0, 0, /* COMMIT, FAIL, ACCEPT, ASSERT_ACCEPT */
184 0, 0 /* CLOSE, SKIPZERO */
187 /* This table identifies those opcodes that inspect a character. It is used to
188 remember the fact that a character could have been inspected when the end of
189 the subject is reached. ***NOTE*** If the start of this table is modified, the
190 two tables that follow must also be modified. */
192 static const pcre_uint8 poptable[] = {
194 0, 0, 0, 1, 1, /* \A, \G, \K, \B, \b */
195 1, 1, 1, 1, 1, 1, /* \D, \d, \S, \s, \W, \w */
196 1, 1, 1, /* Any, AllAny, Anybyte */
198 1, 1, 1, 1, 1, /* \R, \H, \h, \V, \v */
200 0, 0, 0, 0, 0, 0, /* \Z, \z, $, $M, ^, ^M */
205 /* Positive single-char repeats */
206 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
207 1, 1, 1, /* upto, minupto, exact */
208 1, 1, 1, 1, /* *+, ++, ?+, upto+ */
209 1, 1, 1, 1, 1, 1, /* *I, *?I, +I, +?I, ?I, ??I */
210 1, 1, 1, /* upto I, minupto I, exact I */
211 1, 1, 1, 1, /* *+I, ++I, ?+I, upto+I */
212 /* Negative single-char repeats - only for chars < 256 */
213 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */
214 1, 1, 1, /* NOT upto, minupto, exact */
215 1, 1, 1, 1, /* NOT *+, ++, ?+, upto+ */
216 1, 1, 1, 1, 1, 1, /* NOT *I, *?I, +I, +?I, ?I, ??I */
217 1, 1, 1, /* NOT upto I, minupto I, exact I */
218 1, 1, 1, 1, /* NOT *+I, ++I, ?+I, upto+I */
219 /* Positive type repeats */
220 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */
221 1, 1, 1, /* Type upto, minupto, exact */
222 1, 1, 1, 1, /* Type *+, ++, ?+, upto+ */
223 /* Character class & ref repeats */
224 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
225 1, 1, /* CRRANGE, CRMINRANGE */
226 1, 1, 1, 1, /* Possessive *+, ++, ?+, CRPOSRANGE */
229 1, /* XCLASS - variable length */
244 0, /* Assert behind */
245 0, /* Assert behind not */
246 0, 0, /* ONCE, ONCE_NC */
247 0, 0, 0, 0, 0, /* BRA, BRAPOS, CBRA, CBRAPOS, COND */
248 0, 0, 0, 0, 0, /* SBRA, SBRAPOS, SCBRA, SCBRAPOS, SCOND */
249 0, 0, /* CREF, DNCREF */
250 0, 0, /* RREF, DNRREF */
252 0, 0, 0, /* BRAZERO, BRAMINZERO, BRAPOSZERO */
253 0, 0, 0, /* MARK, PRUNE, PRUNE_ARG */
254 0, 0, 0, 0, /* SKIP, SKIP_ARG, THEN, THEN_ARG */
255 0, 0, 0, 0, /* COMMIT, FAIL, ACCEPT, ASSERT_ACCEPT */
256 0, 0 /* CLOSE, SKIPZERO */
259 /* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,
262 static const pcre_uint8 toptable1[] = {
264 ctype_digit, ctype_digit,
265 ctype_space, ctype_space,
266 ctype_word, ctype_word,
267 0, 0 /* OP_ANY, OP_ALLANY */
270 static const pcre_uint8 toptable2[] = {
275 1, 1 /* OP_ANY, OP_ALLANY */
279 /* Structure for holding data about a particular state, which is in effect the
280 current data for an active path through the match tree. It must consist
281 entirely of ints because the working vector we are passed, and which we put
282 these structures in, is a vector of ints. */
284 typedef struct stateblock {
285 int offset; /* Offset to opcode */
286 int count; /* Count for repeats */
287 int data; /* Some use extra data */
290 #define INTS_PER_STATEBLOCK (int)(sizeof(stateblock)/sizeof(int))
294 /*************************************************
295 * Print character string *
296 *************************************************/
298 /* Character string printing function for debugging.
302 length number of bytes
309 pchars(const pcre_uchar *p, int length, FILE *f)
314 if (isprint(c = *(p++)))
317 fprintf(f, "\\x{%02x}", c);
324 /*************************************************
325 * Execute a Regular Expression - DFA engine *
326 *************************************************/
328 /* This internal function applies a compiled pattern to a subject string,
329 starting at a given point, using a DFA engine. This function is called from the
330 external one, possibly multiple times if the pattern is not anchored. The
331 function calls itself recursively for some kinds of subpattern.
334 md the match_data block with fixed information
335 this_start_code the opening bracket of this subexpression's code
336 current_subject where we currently are in the subject string
337 start_offset start offset in the subject string
338 offsets vector to contain the matching string offsets
339 offsetcount size of same
340 workspace vector of workspace
342 rlevel function call recursion level
344 Returns: > 0 => number of match offset pairs placed in offsets
345 = 0 => offsets overflowed; longest matches are present
346 -1 => failed to match
347 < -1 => some kind of unexpected problem
349 The following macros are used for adding states to the two state vectors (one
350 for the current character, one for the following character). */
352 #define ADD_ACTIVE(x,y) \
353 if (active_count++ < wscount) \
355 next_active_state->offset = (x); \
356 next_active_state->count = (y); \
357 next_active_state++; \
358 DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
360 else return PCRE_ERROR_DFA_WSSIZE
362 #define ADD_ACTIVE_DATA(x,y,z) \
363 if (active_count++ < wscount) \
365 next_active_state->offset = (x); \
366 next_active_state->count = (y); \
367 next_active_state->data = (z); \
368 next_active_state++; \
369 DPRINTF(("%.*sADD_ACTIVE_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \
371 else return PCRE_ERROR_DFA_WSSIZE
373 #define ADD_NEW(x,y) \
374 if (new_count++ < wscount) \
376 next_new_state->offset = (x); \
377 next_new_state->count = (y); \
379 DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
381 else return PCRE_ERROR_DFA_WSSIZE
383 #define ADD_NEW_DATA(x,y,z) \
384 if (new_count++ < wscount) \
386 next_new_state->offset = (x); \
387 next_new_state->count = (y); \
388 next_new_state->data = (z); \
390 DPRINTF(("%.*sADD_NEW_DATA(%d,%d,%d) line %d\n", rlevel*2-2, SP, \
391 (x), (y), (z), __LINE__)); \
393 else return PCRE_ERROR_DFA_WSSIZE
395 /* And now, here is the code */
400 const pcre_uchar *this_start_code,
401 const pcre_uchar *current_subject,
409 stateblock *active_states, *new_states, *temp_states;
410 stateblock *next_active_state, *next_new_state;
412 const pcre_uint8 *ctypes, *lcc, *fcc;
413 const pcre_uchar *ptr;
414 const pcre_uchar *end_code, *first_op;
416 dfa_recursion_info new_recursive;
418 int active_count, new_count, match_count;
420 /* Some fields in the md block are frequently referenced, so we load them into
421 independent variables in the hope that this will perform better. */
423 const pcre_uchar *start_subject = md->start_subject;
424 const pcre_uchar *end_subject = md->end_subject;
425 const pcre_uchar *start_code = md->start_code;
428 BOOL utf = (md->poptions & PCRE_UTF8) != 0;
433 BOOL reset_could_continue = FALSE;
439 wscount = (wscount - (wscount % (INTS_PER_STATEBLOCK * 2))) /
440 (2 * INTS_PER_STATEBLOCK);
442 DPRINTF(("\n%.*s---------------------\n"
443 "%.*sCall to internal_dfa_exec f=%d\n",
444 rlevel*2-2, SP, rlevel*2-2, SP, rlevel));
446 ctypes = md->tables + ctypes_offset;
447 lcc = md->tables + lcc_offset;
448 fcc = md->tables + fcc_offset;
450 match_count = PCRE_ERROR_NOMATCH; /* A negative number */
452 active_states = (stateblock *)(workspace + 2);
453 next_new_state = new_states = active_states + wscount;
456 first_op = this_start_code + 1 + LINK_SIZE +
457 ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
458 *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)
461 /* The first thing in any (sub) pattern is a bracket of some sort. Push all
462 the alternative states onto the list, and find out where the end is. This
463 makes is possible to use this function recursively, when we want to stop at a
464 matching internal ket rather than at the end.
466 If the first opcode in the first alternative is OP_REVERSE, we are dealing with
467 a backward assertion. In that case, we have to find out the maximum amount to
468 move back, and set up each alternative appropriately. */
470 if (*first_op == OP_REVERSE)
475 end_code = this_start_code;
478 int back = GET(end_code, 2+LINK_SIZE);
479 if (back > max_back) max_back = back;
480 end_code += GET(end_code, 1);
482 while (*end_code == OP_ALT);
484 /* If we can't go back the amount required for the longest lookbehind
485 pattern, go back as far as we can; some alternatives may still be viable. */
488 /* In character mode we have to step back character by character */
492 for (gone_back = 0; gone_back < max_back; gone_back++)
494 if (current_subject <= start_subject) break;
496 ACROSSCHAR(current_subject > start_subject, *current_subject, current_subject--);
502 /* In byte-mode we can do this quickly. */
505 gone_back = (current_subject - max_back < start_subject)?
506 (int)(current_subject - start_subject) : max_back;
507 current_subject -= gone_back;
510 /* Save the earliest consulted character */
512 if (current_subject < md->start_used_ptr)
513 md->start_used_ptr = current_subject;
515 /* Now we can process the individual branches. */
517 end_code = this_start_code;
520 int back = GET(end_code, 2+LINK_SIZE);
521 if (back <= gone_back)
523 int bstate = (int)(end_code - start_code + 2 + 2*LINK_SIZE);
524 ADD_NEW_DATA(-bstate, 0, gone_back - back);
526 end_code += GET(end_code, 1);
528 while (*end_code == OP_ALT);
531 /* This is the code for a "normal" subpattern (not a backward assertion). The
532 start of a whole pattern is always one of these. If we are at the top level,
533 we may be asked to restart matching from the same point that we reached for a
534 previous partial match. We still have to scan through the top-level branches to
535 find the end state. */
539 end_code = this_start_code;
543 if (rlevel == 1 && (md->moptions & PCRE_DFA_RESTART) != 0)
545 do { end_code += GET(end_code, 1); } while (*end_code == OP_ALT);
546 new_count = workspace[1];
548 memcpy(new_states, active_states, new_count * sizeof(stateblock));
555 int length = 1 + LINK_SIZE +
556 ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA ||
557 *this_start_code == OP_CBRAPOS || *this_start_code == OP_SCBRAPOS)
561 ADD_NEW((int)(end_code - start_code + length), 0);
562 end_code += GET(end_code, 1);
563 length = 1 + LINK_SIZE;
565 while (*end_code == OP_ALT);
569 workspace[0] = 0; /* Bit indicating which vector is current */
571 DPRINTF(("%.*sEnd state = %d\n", rlevel*2-2, SP, (int)(end_code - start_code)));
573 /* Loop for scanning the subject */
575 ptr = current_subject;
582 BOOL partial_newline = FALSE;
583 BOOL could_continue = reset_could_continue;
584 reset_could_continue = FALSE;
586 /* Make the new state list into the active state list and empty the
589 temp_states = active_states;
590 active_states = new_states;
591 new_states = temp_states;
592 active_count = new_count;
595 workspace[0] ^= 1; /* Remember for the restarting feature */
596 workspace[1] = active_count;
599 printf("%.*sNext character: rest of subject = \"", rlevel*2-2, SP);
600 pchars(ptr, STRLEN_UC(ptr), stdout);
603 printf("%.*sActive states: ", rlevel*2-2, SP);
604 for (i = 0; i < active_count; i++)
605 printf("%d/%d ", active_states[i].offset, active_states[i].count);
609 /* Set the pointers for adding new states */
611 next_active_state = active_states + active_count;
612 next_new_state = new_states;
614 /* Load the current character from the subject outside the loop, as many
615 different states may want to look at it, and we assume that at least one
618 if (ptr < end_subject)
620 clen = 1; /* Number of data items in the character */
622 GETCHARLENTEST(c, ptr, clen);
625 #endif /* SUPPORT_UTF */
629 clen = 0; /* This indicates the end of the subject */
630 c = NOTACHAR; /* This value should never actually be used */
633 /* Scan up the active states and act on each one. The result of an action
634 may be to add more states to the currently active list (e.g. on hitting a
635 parenthesis) or it may be to put states on the new list, for considering
636 when we move the character pointer on. */
638 for (i = 0; i < active_count; i++)
640 stateblock *current_state = active_states + i;
641 BOOL caseless = FALSE;
642 const pcre_uchar *code;
643 int state_offset = current_state->offset;
648 printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);
649 if (clen == 0) printf("EOL\n");
650 else if (c > 32 && c < 127) printf("'%c'\n", c);
651 else printf("0x%02x\n", c);
654 /* A negative offset is a special case meaning "hold off going to this
655 (negated) state until the number of characters in the data field have
656 been skipped". If the could_continue flag was passed over from a previous
657 state, arrange for it to passed on. */
659 if (state_offset < 0)
661 if (current_state->data > 0)
663 DPRINTF(("%.*sSkipping this character\n", rlevel*2-2, SP));
664 ADD_NEW_DATA(state_offset, current_state->count,
665 current_state->data - 1);
666 if (could_continue) reset_could_continue = TRUE;
671 current_state->offset = state_offset = -state_offset;
675 /* Check for a duplicate state with the same count, and skip if found.
676 See the note at the head of this module about the possibility of improving
679 for (j = 0; j < i; j++)
681 if (active_states[j].offset == state_offset &&
682 active_states[j].count == current_state->count)
684 DPRINTF(("%.*sDuplicate state: skipped\n", rlevel*2-2, SP));
685 goto NEXT_ACTIVE_STATE;
689 /* The state offset is the offset to the opcode */
691 code = start_code + state_offset;
694 /* If this opcode inspects a character, but we are at the end of the
695 subject, remember the fact for use when testing for a partial match. */
697 if (clen == 0 && poptable[codevalue] != 0)
698 could_continue = TRUE;
700 /* If this opcode is followed by an inline character, load it. It is
701 tempting to test for the presence of a subject character here, but that
702 is wrong, because sometimes zero repetitions of the subject are
705 We also use this mechanism for opcodes such as OP_TYPEPLUS that take an
706 argument that is not a data character - but is always one byte long because
707 the values are small. We have to take special action to deal with \P, \p,
708 \H, \h, \V, \v and \X in this case. To keep the other cases fast, convert
709 these ones to new opcodes. */
711 if (coptable[codevalue] > 0)
715 if (utf) { GETCHARLEN(d, (code + coptable[codevalue]), dlen); } else
716 #endif /* SUPPORT_UTF */
717 d = code[coptable[codevalue]];
718 if (codevalue >= OP_TYPESTAR)
722 case OP_ANYBYTE: return PCRE_ERROR_DFA_UITEM;
724 case OP_PROP: codevalue += OP_PROP_EXTRA; break;
725 case OP_ANYNL: codevalue += OP_ANYNL_EXTRA; break;
726 case OP_EXTUNI: codevalue += OP_EXTUNI_EXTRA; break;
728 case OP_HSPACE: codevalue += OP_HSPACE_EXTRA; break;
730 case OP_VSPACE: codevalue += OP_VSPACE_EXTRA; break;
737 dlen = 0; /* Not strictly necessary, but compilers moan */
738 d = NOTACHAR; /* if these variables are not set. */
742 /* Now process the individual opcodes */
746 /* ========================================================================== */
747 /* These cases are never obeyed. This is a fudge that causes a compile-
748 time error if the vectors coptable or poptable, which are indexed by
749 opcode, are not the correct length. It seems to be the only way to do
750 such a check at compile time, as the sizeof() operator does not work
751 in the C preprocessor. */
753 case OP_TABLE_LENGTH:
754 case OP_TABLE_LENGTH +
755 ((sizeof(coptable) == OP_TABLE_LENGTH) &&
756 (sizeof(poptable) == OP_TABLE_LENGTH)):
759 /* ========================================================================== */
760 /* Reached a closing bracket. If not at the end of the pattern, carry
761 on with the next opcode. For repeating opcodes, also add the repeat
762 state. Note that KETRPOS will always be encountered at the end of the
763 subpattern, because the possessive subpattern repeats are always handled
764 using recursive calls. Thus, it never adds any new states.
766 At the end of the (sub)pattern, unless we have an empty string and
767 PCRE_NOTEMPTY is set, or PCRE_NOTEMPTY_ATSTART is set and we are at the
768 start of the subject, save the match data, shifting up all previous
769 matches so we always have the longest first. */
775 if (code != end_code)
777 ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);
778 if (codevalue != OP_KET)
780 ADD_ACTIVE(state_offset - GET(code, 1), 0);
785 if (ptr > current_subject ||
786 ((md->moptions & PCRE_NOTEMPTY) == 0 &&
787 ((md->moptions & PCRE_NOTEMPTY_ATSTART) == 0 ||
788 current_subject > start_subject + md->start_offset)))
790 if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;
791 else if (match_count > 0 && ++match_count * 2 > offsetcount)
793 count = ((match_count == 0)? offsetcount : match_count * 2) - 2;
794 if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));
795 if (offsetcount >= 2)
797 offsets[0] = (int)(current_subject - start_subject);
798 offsets[1] = (int)(ptr - start_subject);
799 DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,
800 offsets[1] - offsets[0], (char *)current_subject));
802 if ((md->moptions & PCRE_DFA_SHORTEST) != 0)
804 DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
805 "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel,
806 match_count, rlevel*2-2, SP));
813 /* ========================================================================== */
814 /* These opcodes add to the current list of states without looking
815 at the current character. */
817 /*-----------------------------------------------------------------*/
819 do { code += GET(code, 1); } while (*code == OP_ALT);
820 ADD_ACTIVE((int)(code - start_code), 0);
823 /*-----------------------------------------------------------------*/
828 ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
829 code += GET(code, 1);
831 while (*code == OP_ALT);
834 /*-----------------------------------------------------------------*/
837 ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE + IMM2_SIZE), 0);
838 code += GET(code, 1);
839 while (*code == OP_ALT)
841 ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
842 code += GET(code, 1);
846 /*-----------------------------------------------------------------*/
849 ADD_ACTIVE(state_offset + 1, 0);
850 code += 1 + GET(code, 2);
851 while (*code == OP_ALT) code += GET(code, 1);
852 ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
855 /*-----------------------------------------------------------------*/
857 code += 1 + GET(code, 2);
858 while (*code == OP_ALT) code += GET(code, 1);
859 ADD_ACTIVE((int)(code - start_code + 1 + LINK_SIZE), 0);
862 /*-----------------------------------------------------------------*/
864 if (ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0)
865 { ADD_ACTIVE(state_offset + 1, 0); }
868 /*-----------------------------------------------------------------*/
870 if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) ||
871 (ptr != end_subject && WAS_NEWLINE(ptr)))
872 { ADD_ACTIVE(state_offset + 1, 0); }
875 /*-----------------------------------------------------------------*/
877 if (ptr >= end_subject)
879 if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
880 could_continue = TRUE;
881 else { ADD_ACTIVE(state_offset + 1, 0); }
885 /*-----------------------------------------------------------------*/
887 if (ptr == start_subject) { ADD_ACTIVE(state_offset + 1, 0); }
890 /*-----------------------------------------------------------------*/
892 if (ptr == start_subject + start_offset) { ADD_ACTIVE(state_offset + 1, 0); }
896 /* ========================================================================== */
897 /* These opcodes inspect the next subject character, and sometimes
898 the previous one as well, but do not have an argument. The variable
899 clen contains the length of the current character and is zero if we are
900 at the end of the subject. */
902 /*-----------------------------------------------------------------*/
904 if (clen > 0 && !IS_NEWLINE(ptr))
906 if (ptr + 1 >= md->end_subject &&
907 (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
908 NLBLOCK->nltype == NLTYPE_FIXED &&
909 NLBLOCK->nllen == 2 &&
912 could_continue = partial_newline = TRUE;
916 ADD_NEW(state_offset + 1, 0);
921 /*-----------------------------------------------------------------*/
924 { ADD_NEW(state_offset + 1, 0); }
927 /*-----------------------------------------------------------------*/
929 if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
930 could_continue = TRUE;
931 else if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - md->nllen))
932 { ADD_ACTIVE(state_offset + 1, 0); }
935 /*-----------------------------------------------------------------*/
937 if ((md->moptions & PCRE_NOTEOL) == 0)
939 if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
940 could_continue = TRUE;
941 else if (clen == 0 ||
942 ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr) &&
943 (ptr == end_subject - md->nllen)
945 { ADD_ACTIVE(state_offset + 1, 0); }
946 else if (ptr + 1 >= md->end_subject &&
947 (md->moptions & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) != 0 &&
948 NLBLOCK->nltype == NLTYPE_FIXED &&
949 NLBLOCK->nllen == 2 &&
952 if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
954 reset_could_continue = TRUE;
955 ADD_NEW_DATA(-(state_offset + 1), 0, 1);
957 else could_continue = partial_newline = TRUE;
962 /*-----------------------------------------------------------------*/
964 if ((md->moptions & PCRE_NOTEOL) == 0)
966 if (clen == 0 && (md->moptions & PCRE_PARTIAL_HARD) != 0)
967 could_continue = TRUE;
968 else if (clen == 0 ||
969 ((md->poptions & PCRE_DOLLAR_ENDONLY) == 0 && IS_NEWLINE(ptr)))
970 { ADD_ACTIVE(state_offset + 1, 0); }
971 else if (ptr + 1 >= md->end_subject &&
972 (md->moptions & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) != 0 &&
973 NLBLOCK->nltype == NLTYPE_FIXED &&
974 NLBLOCK->nllen == 2 &&
977 if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
979 reset_could_continue = TRUE;
980 ADD_NEW_DATA(-(state_offset + 1), 0, 1);
982 else could_continue = partial_newline = TRUE;
985 else if (IS_NEWLINE(ptr))
986 { ADD_ACTIVE(state_offset + 1, 0); }
989 /*-----------------------------------------------------------------*/
994 if (clen > 0 && c < 256 &&
995 ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0)
996 { ADD_NEW(state_offset + 1, 0); }
999 /*-----------------------------------------------------------------*/
1001 case OP_NOT_WHITESPACE:
1002 case OP_NOT_WORDCHAR:
1003 if (clen > 0 && (c >= 256 ||
1004 ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0))
1005 { ADD_NEW(state_offset + 1, 0); }
1008 /*-----------------------------------------------------------------*/
1009 case OP_WORD_BOUNDARY:
1010 case OP_NOT_WORD_BOUNDARY:
1012 int left_word, right_word;
1014 if (ptr > start_subject)
1016 const pcre_uchar *temp = ptr - 1;
1017 if (temp < md->start_used_ptr) md->start_used_ptr = temp;
1018 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
1019 if (utf) { BACKCHAR(temp); }
1021 GETCHARTEST(d, temp);
1023 if ((md->poptions & PCRE_UCP) != 0)
1025 if (d == '_') left_word = TRUE; else
1027 int cat = UCD_CATEGORY(d);
1028 left_word = (cat == ucp_L || cat == ucp_N);
1033 left_word = d < 256 && (ctypes[d] & ctype_word) != 0;
1035 else left_word = FALSE;
1040 if ((md->poptions & PCRE_UCP) != 0)
1042 if (c == '_') right_word = TRUE; else
1044 int cat = UCD_CATEGORY(c);
1045 right_word = (cat == ucp_L || cat == ucp_N);
1050 right_word = c < 256 && (ctypes[c] & ctype_word) != 0;
1052 else right_word = FALSE;
1054 if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))
1055 { ADD_ACTIVE(state_offset + 1, 0); }
1060 /*-----------------------------------------------------------------*/
1061 /* Check the next character by Unicode property. We will get here only
1062 if the support is in the binary; otherwise a compile-time error occurs.
1071 const pcre_uint32 *cp;
1072 const ucd_record * prop = GET_UCD(c);
1080 OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1081 prop->chartype == ucp_Lt;
1085 OK = PRIV(ucp_gentype)[prop->chartype] == code[2];
1089 OK = prop->chartype == code[2];
1093 OK = prop->script == code[2];
1096 /* These are specials for combination cases. */
1099 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1100 PRIV(ucp_gentype)[prop->chartype] == ucp_N;
1103 /* Perl space used to exclude VT, but from Perl 5.18 it is included,
1104 which means that Perl space and POSIX space are now identical. PCRE
1105 was changed at release 8.34. */
1107 case PT_SPACE: /* Perl space */
1108 case PT_PXSPACE: /* POSIX space */
1117 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
1123 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1124 PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
1125 c == CHAR_UNDERSCORE;
1129 cp = PRIV(ucd_caseless_sets) + code[2];
1132 if (c < *cp) { OK = FALSE; break; }
1133 if (c == *cp++) { OK = TRUE; break; }
1138 OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
1139 c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
1143 /* Should never occur, but keep compilers from grumbling. */
1146 OK = codevalue != OP_PROP;
1150 if (OK == (codevalue == OP_PROP)) { ADD_NEW(state_offset + 3, 0); }
1157 /* ========================================================================== */
1158 /* These opcodes likewise inspect the subject character, but have an
1159 argument that is not a data character. It is one of these opcodes:
1160 OP_ANY, OP_ALLANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE,
1161 OP_WORDCHAR, OP_NOT_WORDCHAR. The value is loaded into d. */
1164 case OP_TYPEMINPLUS:
1165 case OP_TYPEPOSPLUS:
1166 count = current_state->count; /* Already matched */
1167 if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1170 if (d == OP_ANY && ptr + 1 >= md->end_subject &&
1171 (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
1172 NLBLOCK->nltype == NLTYPE_FIXED &&
1173 NLBLOCK->nllen == 2 &&
1174 c == NLBLOCK->nl[0])
1176 could_continue = partial_newline = TRUE;
1178 else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1180 (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1181 ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1183 if (count > 0 && codevalue == OP_TYPEPOSPLUS)
1185 active_count--; /* Remove non-match possibility */
1186 next_active_state--;
1189 ADD_NEW(state_offset, count);
1194 /*-----------------------------------------------------------------*/
1196 case OP_TYPEMINQUERY:
1197 case OP_TYPEPOSQUERY:
1198 ADD_ACTIVE(state_offset + 2, 0);
1201 if (d == OP_ANY && ptr + 1 >= md->end_subject &&
1202 (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
1203 NLBLOCK->nltype == NLTYPE_FIXED &&
1204 NLBLOCK->nllen == 2 &&
1205 c == NLBLOCK->nl[0])
1207 could_continue = partial_newline = TRUE;
1209 else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1211 (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1212 ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1214 if (codevalue == OP_TYPEPOSQUERY)
1216 active_count--; /* Remove non-match possibility */
1217 next_active_state--;
1219 ADD_NEW(state_offset + 2, 0);
1224 /*-----------------------------------------------------------------*/
1226 case OP_TYPEMINSTAR:
1227 case OP_TYPEPOSSTAR:
1228 ADD_ACTIVE(state_offset + 2, 0);
1231 if (d == OP_ANY && ptr + 1 >= md->end_subject &&
1232 (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
1233 NLBLOCK->nltype == NLTYPE_FIXED &&
1234 NLBLOCK->nllen == 2 &&
1235 c == NLBLOCK->nl[0])
1237 could_continue = partial_newline = TRUE;
1239 else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1241 (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1242 ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1244 if (codevalue == OP_TYPEPOSSTAR)
1246 active_count--; /* Remove non-match possibility */
1247 next_active_state--;
1249 ADD_NEW(state_offset, 0);
1254 /*-----------------------------------------------------------------*/
1256 count = current_state->count; /* Number already matched */
1259 if (d == OP_ANY && ptr + 1 >= md->end_subject &&
1260 (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
1261 NLBLOCK->nltype == NLTYPE_FIXED &&
1262 NLBLOCK->nllen == 2 &&
1263 c == NLBLOCK->nl[0])
1265 could_continue = partial_newline = TRUE;
1267 else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1269 (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1270 ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1272 if (++count >= (int)GET2(code, 1))
1273 { ADD_NEW(state_offset + 1 + IMM2_SIZE + 1, 0); }
1275 { ADD_NEW(state_offset, count); }
1280 /*-----------------------------------------------------------------*/
1282 case OP_TYPEMINUPTO:
1283 case OP_TYPEPOSUPTO:
1284 ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0);
1285 count = current_state->count; /* Number already matched */
1288 if (d == OP_ANY && ptr + 1 >= md->end_subject &&
1289 (md->moptions & (PCRE_PARTIAL_HARD)) != 0 &&
1290 NLBLOCK->nltype == NLTYPE_FIXED &&
1291 NLBLOCK->nllen == 2 &&
1292 c == NLBLOCK->nl[0])
1294 could_continue = partial_newline = TRUE;
1296 else if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
1298 (d != OP_ANY || !IS_NEWLINE(ptr)) &&
1299 ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
1301 if (codevalue == OP_TYPEPOSUPTO)
1303 active_count--; /* Remove non-match possibility */
1304 next_active_state--;
1306 if (++count >= (int)GET2(code, 1))
1307 { ADD_NEW(state_offset + 2 + IMM2_SIZE, 0); }
1309 { ADD_NEW(state_offset, count); }
1314 /* ========================================================================== */
1315 /* These are virtual opcodes that are used when something like
1316 OP_TYPEPLUS has OP_PROP, OP_NOTPROP, OP_ANYNL, or OP_EXTUNI as its
1317 argument. It keeps the code above fast for the other cases. The argument
1318 is in the d variable. */
1321 case OP_PROP_EXTRA + OP_TYPEPLUS:
1322 case OP_PROP_EXTRA + OP_TYPEMINPLUS:
1323 case OP_PROP_EXTRA + OP_TYPEPOSPLUS:
1324 count = current_state->count; /* Already matched */
1325 if (count > 0) { ADD_ACTIVE(state_offset + 4, 0); }
1329 const pcre_uint32 *cp;
1330 const ucd_record * prop = GET_UCD(c);
1338 OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1339 prop->chartype == ucp_Lt;
1343 OK = PRIV(ucp_gentype)[prop->chartype] == code[3];
1347 OK = prop->chartype == code[3];
1351 OK = prop->script == code[3];
1354 /* These are specials for combination cases. */
1357 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1358 PRIV(ucp_gentype)[prop->chartype] == ucp_N;
1361 /* Perl space used to exclude VT, but from Perl 5.18 it is included,
1362 which means that Perl space and POSIX space are now identical. PCRE
1363 was changed at release 8.34. */
1365 case PT_SPACE: /* Perl space */
1366 case PT_PXSPACE: /* POSIX space */
1375 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
1381 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1382 PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
1383 c == CHAR_UNDERSCORE;
1387 cp = PRIV(ucd_caseless_sets) + code[3];
1390 if (c < *cp) { OK = FALSE; break; }
1391 if (c == *cp++) { OK = TRUE; break; }
1396 OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
1397 c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
1401 /* Should never occur, but keep compilers from grumbling. */
1404 OK = codevalue != OP_PROP;
1408 if (OK == (d == OP_PROP))
1410 if (count > 0 && codevalue == OP_PROP_EXTRA + OP_TYPEPOSPLUS)
1412 active_count--; /* Remove non-match possibility */
1413 next_active_state--;
1416 ADD_NEW(state_offset, count);
1421 /*-----------------------------------------------------------------*/
1422 case OP_EXTUNI_EXTRA + OP_TYPEPLUS:
1423 case OP_EXTUNI_EXTRA + OP_TYPEMINPLUS:
1424 case OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS:
1425 count = current_state->count; /* Already matched */
1426 if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1430 const pcre_uchar *nptr = ptr + clen;
1432 if (count > 0 && codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS)
1434 active_count--; /* Remove non-match possibility */
1435 next_active_state--;
1437 lgb = UCD_GRAPHBREAK(c);
1438 while (nptr < end_subject)
1441 if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); }
1442 rgb = UCD_GRAPHBREAK(d);
1443 if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
1449 ADD_NEW_DATA(-state_offset, count, ncount);
1454 /*-----------------------------------------------------------------*/
1455 case OP_ANYNL_EXTRA + OP_TYPEPLUS:
1456 case OP_ANYNL_EXTRA + OP_TYPEMINPLUS:
1457 case OP_ANYNL_EXTRA + OP_TYPEPOSPLUS:
1458 count = current_state->count; /* Already matched */
1459 if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1471 #endif /* Not EBCDIC */
1472 if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
1476 if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
1481 if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS)
1483 active_count--; /* Remove non-match possibility */
1484 next_active_state--;
1487 ADD_NEW_DATA(-state_offset, count, ncount);
1496 /*-----------------------------------------------------------------*/
1497 case OP_VSPACE_EXTRA + OP_TYPEPLUS:
1498 case OP_VSPACE_EXTRA + OP_TYPEMINPLUS:
1499 case OP_VSPACE_EXTRA + OP_TYPEPOSPLUS:
1500 count = current_state->count; /* Already matched */
1501 if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1516 if (OK == (d == OP_VSPACE))
1518 if (count > 0 && codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSPLUS)
1520 active_count--; /* Remove non-match possibility */
1521 next_active_state--;
1524 ADD_NEW_DATA(-state_offset, count, 0);
1529 /*-----------------------------------------------------------------*/
1530 case OP_HSPACE_EXTRA + OP_TYPEPLUS:
1531 case OP_HSPACE_EXTRA + OP_TYPEMINPLUS:
1532 case OP_HSPACE_EXTRA + OP_TYPEPOSPLUS:
1533 count = current_state->count; /* Already matched */
1534 if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
1549 if (OK == (d == OP_HSPACE))
1551 if (count > 0 && codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSPLUS)
1553 active_count--; /* Remove non-match possibility */
1554 next_active_state--;
1557 ADD_NEW_DATA(-state_offset, count, 0);
1562 /*-----------------------------------------------------------------*/
1564 case OP_PROP_EXTRA + OP_TYPEQUERY:
1565 case OP_PROP_EXTRA + OP_TYPEMINQUERY:
1566 case OP_PROP_EXTRA + OP_TYPEPOSQUERY:
1570 case OP_PROP_EXTRA + OP_TYPESTAR:
1571 case OP_PROP_EXTRA + OP_TYPEMINSTAR:
1572 case OP_PROP_EXTRA + OP_TYPEPOSSTAR:
1577 ADD_ACTIVE(state_offset + 4, 0);
1581 const pcre_uint32 *cp;
1582 const ucd_record * prop = GET_UCD(c);
1590 OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1591 prop->chartype == ucp_Lt;
1595 OK = PRIV(ucp_gentype)[prop->chartype] == code[3];
1599 OK = prop->chartype == code[3];
1603 OK = prop->script == code[3];
1606 /* These are specials for combination cases. */
1609 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1610 PRIV(ucp_gentype)[prop->chartype] == ucp_N;
1613 /* Perl space used to exclude VT, but from Perl 5.18 it is included,
1614 which means that Perl space and POSIX space are now identical. PCRE
1615 was changed at release 8.34. */
1617 case PT_SPACE: /* Perl space */
1618 case PT_PXSPACE: /* POSIX space */
1627 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
1633 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1634 PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
1635 c == CHAR_UNDERSCORE;
1639 cp = PRIV(ucd_caseless_sets) + code[3];
1642 if (c < *cp) { OK = FALSE; break; }
1643 if (c == *cp++) { OK = TRUE; break; }
1648 OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
1649 c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
1653 /* Should never occur, but keep compilers from grumbling. */
1656 OK = codevalue != OP_PROP;
1660 if (OK == (d == OP_PROP))
1662 if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSSTAR ||
1663 codevalue == OP_PROP_EXTRA + OP_TYPEPOSQUERY)
1665 active_count--; /* Remove non-match possibility */
1666 next_active_state--;
1668 ADD_NEW(state_offset + count, 0);
1673 /*-----------------------------------------------------------------*/
1674 case OP_EXTUNI_EXTRA + OP_TYPEQUERY:
1675 case OP_EXTUNI_EXTRA + OP_TYPEMINQUERY:
1676 case OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY:
1680 case OP_EXTUNI_EXTRA + OP_TYPESTAR:
1681 case OP_EXTUNI_EXTRA + OP_TYPEMINSTAR:
1682 case OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR:
1687 ADD_ACTIVE(state_offset + 2, 0);
1691 const pcre_uchar *nptr = ptr + clen;
1693 if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR ||
1694 codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY)
1696 active_count--; /* Remove non-match possibility */
1697 next_active_state--;
1699 lgb = UCD_GRAPHBREAK(c);
1700 while (nptr < end_subject)
1703 if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); }
1704 rgb = UCD_GRAPHBREAK(d);
1705 if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
1710 ADD_NEW_DATA(-(state_offset + count), 0, ncount);
1715 /*-----------------------------------------------------------------*/
1716 case OP_ANYNL_EXTRA + OP_TYPEQUERY:
1717 case OP_ANYNL_EXTRA + OP_TYPEMINQUERY:
1718 case OP_ANYNL_EXTRA + OP_TYPEPOSQUERY:
1722 case OP_ANYNL_EXTRA + OP_TYPESTAR:
1723 case OP_ANYNL_EXTRA + OP_TYPEMINSTAR:
1724 case OP_ANYNL_EXTRA + OP_TYPEPOSSTAR:
1728 ADD_ACTIVE(state_offset + 2, 0);
1740 #endif /* Not EBCDIC */
1741 if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
1745 if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
1750 if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR ||
1751 codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY)
1753 active_count--; /* Remove non-match possibility */
1754 next_active_state--;
1756 ADD_NEW_DATA(-(state_offset + (int)count), 0, ncount);
1765 /*-----------------------------------------------------------------*/
1766 case OP_VSPACE_EXTRA + OP_TYPEQUERY:
1767 case OP_VSPACE_EXTRA + OP_TYPEMINQUERY:
1768 case OP_VSPACE_EXTRA + OP_TYPEPOSQUERY:
1772 case OP_VSPACE_EXTRA + OP_TYPESTAR:
1773 case OP_VSPACE_EXTRA + OP_TYPEMINSTAR:
1774 case OP_VSPACE_EXTRA + OP_TYPEPOSSTAR:
1778 ADD_ACTIVE(state_offset + 2, 0);
1792 if (OK == (d == OP_VSPACE))
1794 if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSSTAR ||
1795 codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSQUERY)
1797 active_count--; /* Remove non-match possibility */
1798 next_active_state--;
1800 ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);
1805 /*-----------------------------------------------------------------*/
1806 case OP_HSPACE_EXTRA + OP_TYPEQUERY:
1807 case OP_HSPACE_EXTRA + OP_TYPEMINQUERY:
1808 case OP_HSPACE_EXTRA + OP_TYPEPOSQUERY:
1812 case OP_HSPACE_EXTRA + OP_TYPESTAR:
1813 case OP_HSPACE_EXTRA + OP_TYPEMINSTAR:
1814 case OP_HSPACE_EXTRA + OP_TYPEPOSSTAR:
1818 ADD_ACTIVE(state_offset + 2, 0);
1833 if (OK == (d == OP_HSPACE))
1835 if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSSTAR ||
1836 codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSQUERY)
1838 active_count--; /* Remove non-match possibility */
1839 next_active_state--;
1841 ADD_NEW_DATA(-(state_offset + (int)count), 0, 0);
1846 /*-----------------------------------------------------------------*/
1848 case OP_PROP_EXTRA + OP_TYPEEXACT:
1849 case OP_PROP_EXTRA + OP_TYPEUPTO:
1850 case OP_PROP_EXTRA + OP_TYPEMINUPTO:
1851 case OP_PROP_EXTRA + OP_TYPEPOSUPTO:
1852 if (codevalue != OP_PROP_EXTRA + OP_TYPEEXACT)
1853 { ADD_ACTIVE(state_offset + 1 + IMM2_SIZE + 3, 0); }
1854 count = current_state->count; /* Number already matched */
1858 const pcre_uint32 *cp;
1859 const ucd_record * prop = GET_UCD(c);
1860 switch(code[1 + IMM2_SIZE + 1])
1867 OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
1868 prop->chartype == ucp_Lt;
1872 OK = PRIV(ucp_gentype)[prop->chartype] == code[1 + IMM2_SIZE + 2];
1876 OK = prop->chartype == code[1 + IMM2_SIZE + 2];
1880 OK = prop->script == code[1 + IMM2_SIZE + 2];
1883 /* These are specials for combination cases. */
1886 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1887 PRIV(ucp_gentype)[prop->chartype] == ucp_N;
1890 /* Perl space used to exclude VT, but from Perl 5.18 it is included,
1891 which means that Perl space and POSIX space are now identical. PCRE
1892 was changed at release 8.34. */
1894 case PT_SPACE: /* Perl space */
1895 case PT_PXSPACE: /* POSIX space */
1904 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_Z;
1910 OK = PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
1911 PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
1912 c == CHAR_UNDERSCORE;
1916 cp = PRIV(ucd_caseless_sets) + code[1 + IMM2_SIZE + 2];
1919 if (c < *cp) { OK = FALSE; break; }
1920 if (c == *cp++) { OK = TRUE; break; }
1925 OK = c == CHAR_DOLLAR_SIGN || c == CHAR_COMMERCIAL_AT ||
1926 c == CHAR_GRAVE_ACCENT || (c >= 0xa0 && c <= 0xd7ff) ||
1930 /* Should never occur, but keep compilers from grumbling. */
1933 OK = codevalue != OP_PROP;
1937 if (OK == (d == OP_PROP))
1939 if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSUPTO)
1941 active_count--; /* Remove non-match possibility */
1942 next_active_state--;
1944 if (++count >= (int)GET2(code, 1))
1945 { ADD_NEW(state_offset + 1 + IMM2_SIZE + 3, 0); }
1947 { ADD_NEW(state_offset, count); }
1952 /*-----------------------------------------------------------------*/
1953 case OP_EXTUNI_EXTRA + OP_TYPEEXACT:
1954 case OP_EXTUNI_EXTRA + OP_TYPEUPTO:
1955 case OP_EXTUNI_EXTRA + OP_TYPEMINUPTO:
1956 case OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO:
1957 if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT)
1958 { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
1959 count = current_state->count; /* Number already matched */
1963 const pcre_uchar *nptr = ptr + clen;
1965 if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO)
1967 active_count--; /* Remove non-match possibility */
1968 next_active_state--;
1970 lgb = UCD_GRAPHBREAK(c);
1971 while (nptr < end_subject)
1974 if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); }
1975 rgb = UCD_GRAPHBREAK(d);
1976 if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
1981 if (nptr >= end_subject && (md->moptions & PCRE_PARTIAL_HARD) != 0)
1982 reset_could_continue = TRUE;
1983 if (++count >= (int)GET2(code, 1))
1984 { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }
1986 { ADD_NEW_DATA(-state_offset, count, ncount); }
1991 /*-----------------------------------------------------------------*/
1992 case OP_ANYNL_EXTRA + OP_TYPEEXACT:
1993 case OP_ANYNL_EXTRA + OP_TYPEUPTO:
1994 case OP_ANYNL_EXTRA + OP_TYPEMINUPTO:
1995 case OP_ANYNL_EXTRA + OP_TYPEPOSUPTO:
1996 if (codevalue != OP_ANYNL_EXTRA + OP_TYPEEXACT)
1997 { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
1998 count = current_state->count; /* Number already matched */
2010 #endif /* Not EBCDIC */
2011 if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
2015 if (ptr + 1 < end_subject && UCHAR21TEST(ptr + 1) == CHAR_LF) ncount = 1;
2020 if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO)
2022 active_count--; /* Remove non-match possibility */
2023 next_active_state--;
2025 if (++count >= (int)GET2(code, 1))
2026 { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, ncount); }
2028 { ADD_NEW_DATA(-state_offset, count, ncount); }
2037 /*-----------------------------------------------------------------*/
2038 case OP_VSPACE_EXTRA + OP_TYPEEXACT:
2039 case OP_VSPACE_EXTRA + OP_TYPEUPTO:
2040 case OP_VSPACE_EXTRA + OP_TYPEMINUPTO:
2041 case OP_VSPACE_EXTRA + OP_TYPEPOSUPTO:
2042 if (codevalue != OP_VSPACE_EXTRA + OP_TYPEEXACT)
2043 { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
2044 count = current_state->count; /* Number already matched */
2058 if (OK == (d == OP_VSPACE))
2060 if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSUPTO)
2062 active_count--; /* Remove non-match possibility */
2063 next_active_state--;
2065 if (++count >= (int)GET2(code, 1))
2066 { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }
2068 { ADD_NEW_DATA(-state_offset, count, 0); }
2073 /*-----------------------------------------------------------------*/
2074 case OP_HSPACE_EXTRA + OP_TYPEEXACT:
2075 case OP_HSPACE_EXTRA + OP_TYPEUPTO:
2076 case OP_HSPACE_EXTRA + OP_TYPEMINUPTO:
2077 case OP_HSPACE_EXTRA + OP_TYPEPOSUPTO:
2078 if (codevalue != OP_HSPACE_EXTRA + OP_TYPEEXACT)
2079 { ADD_ACTIVE(state_offset + 2 + IMM2_SIZE, 0); }
2080 count = current_state->count; /* Number already matched */
2095 if (OK == (d == OP_HSPACE))
2097 if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSUPTO)
2099 active_count--; /* Remove non-match possibility */
2100 next_active_state--;
2102 if (++count >= (int)GET2(code, 1))
2103 { ADD_NEW_DATA(-(state_offset + 2 + IMM2_SIZE), 0, 0); }
2105 { ADD_NEW_DATA(-state_offset, count, 0); }
2110 /* ========================================================================== */
2111 /* These opcodes are followed by a character that is usually compared
2112 to the current subject character; it is loaded into d. We still get
2113 here even if there is no subject character, because in some cases zero
2114 repetitions are permitted. */
2116 /*-----------------------------------------------------------------*/
2118 if (clen > 0 && c == d) { ADD_NEW(state_offset + dlen + 1, 0); }
2121 /*-----------------------------------------------------------------*/
2123 if (clen == 0) break;
2128 if (c == d) { ADD_NEW(state_offset + dlen + 1, 0); } else
2130 unsigned int othercase;
2134 /* If we have Unicode property support, we can use it to test the
2135 other case of the character. */
2137 othercase = UCD_OTHERCASE(c);
2139 othercase = NOTACHAR;
2142 if (d == othercase) { ADD_NEW(state_offset + dlen + 1, 0); }
2146 #endif /* SUPPORT_UTF */
2149 if (TABLE_GET(c, lcc, c) == TABLE_GET(d, lcc, d))
2150 { ADD_NEW(state_offset + 2, 0); }
2156 /*-----------------------------------------------------------------*/
2157 /* This is a tricky one because it can match more than one character.
2158 Find out how many characters to skip, and then set up a negative state
2159 to wait for them to pass before continuing. */
2165 const pcre_uchar *nptr = ptr + clen;
2167 lgb = UCD_GRAPHBREAK(c);
2168 while (nptr < end_subject)
2171 if (!utf) d = *nptr; else { GETCHARLEN(d, nptr, dlen); }
2172 rgb = UCD_GRAPHBREAK(d);
2173 if ((PRIV(ucp_gbtable)[lgb] & (1 << rgb)) == 0) break;
2178 if (nptr >= end_subject && (md->moptions & PCRE_PARTIAL_HARD) != 0)
2179 reset_could_continue = TRUE;
2180 ADD_NEW_DATA(-(state_offset + 1), 0, ncount);
2185 /*-----------------------------------------------------------------*/
2186 /* This is a tricky like EXTUNI because it too can match more than one
2187 character (when CR is followed by LF). In this case, set up a negative
2188 state to wait for one character to pass before continuing. */
2191 if (clen > 0) switch(c)
2199 #endif /* Not EBCDIC */
2200 if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
2203 ADD_NEW(state_offset + 1, 0);
2207 if (ptr + 1 >= end_subject)
2209 ADD_NEW(state_offset + 1, 0);
2210 if ((md->moptions & PCRE_PARTIAL_HARD) != 0)
2211 reset_could_continue = TRUE;
2213 else if (UCHAR21TEST(ptr + 1) == CHAR_LF)
2215 ADD_NEW_DATA(-(state_offset + 1), 0, 1);
2219 ADD_NEW(state_offset + 1, 0);
2225 /*-----------------------------------------------------------------*/
2227 if (clen > 0) switch(c)
2233 ADD_NEW(state_offset + 1, 0);
2238 /*-----------------------------------------------------------------*/
2240 if (clen > 0) switch(c)
2243 ADD_NEW(state_offset + 1, 0);
2251 /*-----------------------------------------------------------------*/
2253 if (clen > 0) switch(c)
2259 ADD_NEW(state_offset + 1, 0);
2264 /*-----------------------------------------------------------------*/
2266 if (clen > 0) switch(c)
2269 ADD_NEW(state_offset + 1, 0);
2277 /*-----------------------------------------------------------------*/
2278 /* Match a negated single character casefully. */
2281 if (clen > 0 && c != d) { ADD_NEW(state_offset + dlen + 1, 0); }
2284 /*-----------------------------------------------------------------*/
2285 /* Match a negated single character caselessly. */
2290 unsigned int otherd;
2292 if (utf && d >= 128)
2295 otherd = UCD_OTHERCASE(d);
2296 #endif /* SUPPORT_UCP */
2299 #endif /* SUPPORT_UTF */
2300 otherd = TABLE_GET(d, fcc, d);
2301 if (c != d && c != otherd)
2302 { ADD_NEW(state_offset + dlen + 1, 0); }
2306 /*-----------------------------------------------------------------*/
2311 case OP_NOTMINPLUSI:
2312 case OP_NOTPOSPLUSI:
2314 codevalue -= OP_STARI - OP_STAR;
2323 count = current_state->count; /* Already matched */
2324 if (count > 0) { ADD_ACTIVE(state_offset + dlen + 1, 0); }
2327 pcre_uint32 otherd = NOTACHAR;
2331 if (utf && d >= 128)
2334 otherd = UCD_OTHERCASE(d);
2335 #endif /* SUPPORT_UCP */
2338 #endif /* SUPPORT_UTF */
2339 otherd = TABLE_GET(d, fcc, d);
2341 if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2344 (codevalue == OP_POSPLUS || codevalue == OP_NOTPOSPLUS))
2346 active_count--; /* Remove non-match possibility */
2347 next_active_state--;
2350 ADD_NEW(state_offset, count);
2355 /*-----------------------------------------------------------------*/
2360 case OP_NOTMINQUERYI:
2361 case OP_NOTPOSQUERYI:
2363 codevalue -= OP_STARI - OP_STAR;
2369 case OP_NOTMINQUERY:
2370 case OP_NOTPOSQUERY:
2371 ADD_ACTIVE(state_offset + dlen + 1, 0);
2374 pcre_uint32 otherd = NOTACHAR;
2378 if (utf && d >= 128)
2381 otherd = UCD_OTHERCASE(d);
2382 #endif /* SUPPORT_UCP */
2385 #endif /* SUPPORT_UTF */
2386 otherd = TABLE_GET(d, fcc, d);
2388 if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2390 if (codevalue == OP_POSQUERY || codevalue == OP_NOTPOSQUERY)
2392 active_count--; /* Remove non-match possibility */
2393 next_active_state--;
2395 ADD_NEW(state_offset + dlen + 1, 0);
2400 /*-----------------------------------------------------------------*/
2405 case OP_NOTMINSTARI:
2406 case OP_NOTPOSSTARI:
2408 codevalue -= OP_STARI - OP_STAR;
2416 ADD_ACTIVE(state_offset + dlen + 1, 0);
2419 pcre_uint32 otherd = NOTACHAR;
2423 if (utf && d >= 128)
2426 otherd = UCD_OTHERCASE(d);
2427 #endif /* SUPPORT_UCP */
2430 #endif /* SUPPORT_UTF */
2431 otherd = TABLE_GET(d, fcc, d);
2433 if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2435 if (codevalue == OP_POSSTAR || codevalue == OP_NOTPOSSTAR)
2437 active_count--; /* Remove non-match possibility */
2438 next_active_state--;
2440 ADD_NEW(state_offset, 0);
2445 /*-----------------------------------------------------------------*/
2449 codevalue -= OP_STARI - OP_STAR;
2453 count = current_state->count; /* Number already matched */
2456 pcre_uint32 otherd = NOTACHAR;
2460 if (utf && d >= 128)
2463 otherd = UCD_OTHERCASE(d);
2464 #endif /* SUPPORT_UCP */
2467 #endif /* SUPPORT_UTF */
2468 otherd = TABLE_GET(d, fcc, d);
2470 if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2472 if (++count >= (int)GET2(code, 1))
2473 { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }
2475 { ADD_NEW(state_offset, count); }
2480 /*-----------------------------------------------------------------*/
2485 case OP_NOTMINUPTOI:
2486 case OP_NOTPOSUPTOI:
2488 codevalue -= OP_STARI - OP_STAR;
2496 ADD_ACTIVE(state_offset + dlen + 1 + IMM2_SIZE, 0);
2497 count = current_state->count; /* Number already matched */
2500 pcre_uint32 otherd = NOTACHAR;
2504 if (utf && d >= 128)
2507 otherd = UCD_OTHERCASE(d);
2508 #endif /* SUPPORT_UCP */
2511 #endif /* SUPPORT_UTF */
2512 otherd = TABLE_GET(d, fcc, d);
2514 if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
2516 if (codevalue == OP_POSUPTO || codevalue == OP_NOTPOSUPTO)
2518 active_count--; /* Remove non-match possibility */
2519 next_active_state--;
2521 if (++count >= (int)GET2(code, 1))
2522 { ADD_NEW(state_offset + dlen + 1 + IMM2_SIZE, 0); }
2524 { ADD_NEW(state_offset, count); }
2530 /* ========================================================================== */
2531 /* These are the class-handling opcodes */
2537 BOOL isinclass = FALSE;
2538 int next_state_offset;
2539 const pcre_uchar *ecode;
2541 /* For a simple class, there is always just a 32-byte table, and we
2542 can set isinclass from it. */
2544 if (codevalue != OP_XCLASS)
2546 ecode = code + 1 + (32 / sizeof(pcre_uchar));
2549 isinclass = (c > 255)? (codevalue == OP_NCLASS) :
2550 ((((pcre_uint8 *)(code + 1))[c/8] & (1 << (c&7))) != 0);
2554 /* An extended class may have a table or a list of single characters,
2555 ranges, or both, and it may be positive or negative. There's a
2556 function that sorts all this out. */
2560 ecode = code + GET(code, 1);
2561 if (clen > 0) isinclass = PRIV(xclass)(c, code + 1 + LINK_SIZE, utf);
2564 /* At this point, isinclass is set for all kinds of class, and ecode
2565 points to the byte after the end of the class. If there is a
2566 quantifier, this is where it will be. */
2568 next_state_offset = (int)(ecode - start_code);
2575 ADD_ACTIVE(next_state_offset + 1, 0);
2578 if (*ecode == OP_CRPOSSTAR)
2580 active_count--; /* Remove non-match possibility */
2581 next_active_state--;
2583 ADD_NEW(state_offset, 0);
2590 count = current_state->count; /* Already matched */
2591 if (count > 0) { ADD_ACTIVE(next_state_offset + 1, 0); }
2594 if (count > 0 && *ecode == OP_CRPOSPLUS)
2596 active_count--; /* Remove non-match possibility */
2597 next_active_state--;
2600 ADD_NEW(state_offset, count);
2607 ADD_ACTIVE(next_state_offset + 1, 0);
2610 if (*ecode == OP_CRPOSQUERY)
2612 active_count--; /* Remove non-match possibility */
2613 next_active_state--;
2615 ADD_NEW(next_state_offset + 1, 0);
2622 count = current_state->count; /* Already matched */
2623 if (count >= (int)GET2(ecode, 1))
2624 { ADD_ACTIVE(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }
2627 int max = (int)GET2(ecode, 1 + IMM2_SIZE);
2628 if (*ecode == OP_CRPOSRANGE)
2630 active_count--; /* Remove non-match possibility */
2631 next_active_state--;
2633 if (++count >= max && max != 0) /* Max 0 => no limit */
2634 { ADD_NEW(next_state_offset + 1 + 2 * IMM2_SIZE, 0); }
2636 { ADD_NEW(state_offset, count); }
2641 if (isinclass) { ADD_NEW(next_state_offset, 0); }
2647 /* ========================================================================== */
2648 /* These are the opcodes for fancy brackets of various kinds. We have
2649 to use recursion in order to handle them. The "always failing" assertion
2650 (?!) is optimised to OP_FAIL when compiling, so we have to support that,
2651 though the other "backtracking verbs" are not supported. */
2654 forced_fail++; /* Count FAILs for multiple states */
2660 case OP_ASSERTBACK_NOT:
2663 int local_offsets[2];
2664 int local_workspace[1000];
2665 const pcre_uchar *endasscode = code + GET(code, 1);
2667 while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
2669 rc = internal_dfa_exec(
2670 md, /* static match data */
2671 code, /* this subexpression's code */
2672 ptr, /* where we currently are */
2673 (int)(ptr - start_subject), /* start offset */
2674 local_offsets, /* offset vector */
2675 sizeof(local_offsets)/sizeof(int), /* size of same */
2676 local_workspace, /* workspace vector */
2677 sizeof(local_workspace)/sizeof(int), /* size of same */
2678 rlevel); /* function recursion level */
2680 if (rc == PCRE_ERROR_DFA_UITEM) return rc;
2681 if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))
2682 { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
2686 /*-----------------------------------------------------------------*/
2690 int local_offsets[1000];
2691 int local_workspace[1000];
2692 int codelink = GET(code, 1);
2695 /* Because of the way auto-callout works during compile, a callout item
2696 is inserted between OP_COND and an assertion condition. This does not
2697 happen for the other conditions. */
2699 if (code[LINK_SIZE+1] == OP_CALLOUT)
2702 if (PUBL(callout) != NULL)
2704 PUBL(callout_block) cb;
2705 cb.version = 1; /* Version 1 of the callout block */
2706 cb.callout_number = code[LINK_SIZE+2];
2707 cb.offset_vector = offsets;
2708 #if defined COMPILE_PCRE8
2709 cb.subject = (PCRE_SPTR)start_subject;
2710 #elif defined COMPILE_PCRE16
2711 cb.subject = (PCRE_SPTR16)start_subject;
2712 #elif defined COMPILE_PCRE32
2713 cb.subject = (PCRE_SPTR32)start_subject;
2715 cb.subject_length = (int)(end_subject - start_subject);
2716 cb.start_match = (int)(current_subject - start_subject);
2717 cb.current_position = (int)(ptr - start_subject);
2718 cb.pattern_position = GET(code, LINK_SIZE + 3);
2719 cb.next_item_length = GET(code, 3 + 2*LINK_SIZE);
2721 cb.capture_last = -1;
2722 cb.callout_data = md->callout_data;
2723 cb.mark = NULL; /* No (*MARK) support */
2724 if ((rrc = (*PUBL(callout))(&cb)) < 0) return rrc; /* Abandon */
2726 if (rrc > 0) break; /* Fail this thread */
2727 code += PRIV(OP_lengths)[OP_CALLOUT]; /* Skip callout data */
2730 condcode = code[LINK_SIZE+1];
2732 /* Back reference conditions and duplicate named recursion conditions
2733 are not supported */
2735 if (condcode == OP_CREF || condcode == OP_DNCREF ||
2736 condcode == OP_DNRREF)
2737 return PCRE_ERROR_DFA_UCOND;
2739 /* The DEFINE condition is always false */
2741 if (condcode == OP_DEF)
2742 { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2744 /* The only supported version of OP_RREF is for the value RREF_ANY,
2745 which means "test if in any recursion". We can't test for specifically
2748 else if (condcode == OP_RREF)
2750 int value = GET2(code, LINK_SIZE + 2);
2751 if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;
2752 if (md->recursive != NULL)
2753 { ADD_ACTIVE(state_offset + LINK_SIZE + 2 + IMM2_SIZE, 0); }
2754 else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2757 /* Otherwise, the condition is an assertion */
2762 const pcre_uchar *asscode = code + LINK_SIZE + 1;
2763 const pcre_uchar *endasscode = asscode + GET(asscode, 1);
2765 while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
2767 rc = internal_dfa_exec(
2768 md, /* fixed match data */
2769 asscode, /* this subexpression's code */
2770 ptr, /* where we currently are */
2771 (int)(ptr - start_subject), /* start offset */
2772 local_offsets, /* offset vector */
2773 sizeof(local_offsets)/sizeof(int), /* size of same */
2774 local_workspace, /* workspace vector */
2775 sizeof(local_workspace)/sizeof(int), /* size of same */
2776 rlevel); /* function recursion level */
2778 if (rc == PCRE_ERROR_DFA_UITEM) return rc;
2780 (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))
2781 { ADD_ACTIVE((int)(endasscode + LINK_SIZE + 1 - start_code), 0); }
2783 { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2788 /*-----------------------------------------------------------------*/
2791 dfa_recursion_info *ri;
2792 int local_offsets[1000];
2793 int local_workspace[1000];
2794 const pcre_uchar *callpat = start_code + GET(code, 1);
2795 int recno = (callpat == md->start_code)? 0 :
2796 GET2(callpat, 1 + LINK_SIZE);
2799 DPRINTF(("%.*sStarting regex recursion\n", rlevel*2-2, SP));
2801 /* Check for repeating a recursion without advancing the subject
2802 pointer. This should catch convoluted mutual recursions. (Some simple
2803 cases are caught at compile time.) */
2805 for (ri = md->recursive; ri != NULL; ri = ri->prevrec)
2806 if (recno == ri->group_num && ptr == ri->subject_position)
2807 return PCRE_ERROR_RECURSELOOP;
2809 /* Remember this recursion and where we started it so as to
2810 catch infinite loops. */
2812 new_recursive.group_num = recno;
2813 new_recursive.subject_position = ptr;
2814 new_recursive.prevrec = md->recursive;
2815 md->recursive = &new_recursive;
2817 rc = internal_dfa_exec(
2818 md, /* fixed match data */
2819 callpat, /* this subexpression's code */
2820 ptr, /* where we currently are */
2821 (int)(ptr - start_subject), /* start offset */
2822 local_offsets, /* offset vector */
2823 sizeof(local_offsets)/sizeof(int), /* size of same */
2824 local_workspace, /* workspace vector */
2825 sizeof(local_workspace)/sizeof(int), /* size of same */
2826 rlevel); /* function recursion level */
2828 md->recursive = new_recursive.prevrec; /* Done this recursion */
2830 DPRINTF(("%.*sReturn from regex recursion: rc=%d\n", rlevel*2-2, SP,
2833 /* Ran out of internal offsets */
2835 if (rc == 0) return PCRE_ERROR_DFA_RECURSE;
2837 /* For each successful matched substring, set up the next state with a
2838 count of characters to skip before trying it. Note that the count is in
2839 characters, not bytes. */
2843 for (rc = rc*2 - 2; rc >= 0; rc -= 2)
2845 int charcount = local_offsets[rc+1] - local_offsets[rc];
2846 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2849 const pcre_uchar *p = start_subject + local_offsets[rc];
2850 const pcre_uchar *pp = start_subject + local_offsets[rc+1];
2851 while (p < pp) if (NOT_FIRSTCHAR(*p++)) charcount--;
2856 ADD_NEW_DATA(-(state_offset + LINK_SIZE + 1), 0, (charcount - 1));
2860 ADD_ACTIVE(state_offset + LINK_SIZE + 1, 0);
2864 else if (rc != PCRE_ERROR_NOMATCH) return rc;
2868 /*-----------------------------------------------------------------*/
2875 int charcount, matched_count;
2876 const pcre_uchar *local_ptr = ptr;
2879 if (codevalue == OP_BRAPOSZERO)
2882 codevalue = *(++code); /* Codevalue will be one of above BRAs */
2884 else allow_zero = FALSE;
2886 /* Loop to match the subpattern as many times as possible as if it were
2887 a complete pattern. */
2889 for (matched_count = 0;; matched_count++)
2891 int local_offsets[2];
2892 int local_workspace[1000];
2894 int rc = internal_dfa_exec(
2895 md, /* fixed match data */
2896 code, /* this subexpression's code */
2897 local_ptr, /* where we currently are */
2898 (int)(ptr - start_subject), /* start offset */
2899 local_offsets, /* offset vector */
2900 sizeof(local_offsets)/sizeof(int), /* size of same */
2901 local_workspace, /* workspace vector */
2902 sizeof(local_workspace)/sizeof(int), /* size of same */
2903 rlevel); /* function recursion level */
2905 /* Failed to match */
2909 if (rc != PCRE_ERROR_NOMATCH) return rc;
2913 /* Matched: break the loop if zero characters matched. */
2915 charcount = local_offsets[1] - local_offsets[0];
2916 if (charcount == 0) break;
2917 local_ptr += charcount; /* Advance temporary position ptr */
2920 /* At this point we have matched the subpattern matched_count
2921 times, and local_ptr is pointing to the character after the end of the
2924 if (matched_count > 0 || allow_zero)
2926 const pcre_uchar *end_subpattern = code;
2927 int next_state_offset;
2929 do { end_subpattern += GET(end_subpattern, 1); }
2930 while (*end_subpattern == OP_ALT);
2932 (int)(end_subpattern - start_code + LINK_SIZE + 1);
2934 /* Optimization: if there are no more active states, and there
2935 are no new states yet set up, then skip over the subject string
2936 right here, to save looping. Otherwise, set up the new state to swing
2937 into action when the end of the matched substring is reached. */
2939 if (i + 1 >= active_count && new_count == 0)
2943 ADD_NEW(next_state_offset, 0);
2947 const pcre_uchar *p = ptr;
2948 const pcre_uchar *pp = local_ptr;
2949 charcount = (int)(pp - p);
2950 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2951 if (utf) while (p < pp) if (NOT_FIRSTCHAR(*p++)) charcount--;
2953 ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1));
2959 /*-----------------------------------------------------------------*/
2963 int local_offsets[2];
2964 int local_workspace[1000];
2966 int rc = internal_dfa_exec(
2967 md, /* fixed match data */
2968 code, /* this subexpression's code */
2969 ptr, /* where we currently are */
2970 (int)(ptr - start_subject), /* start offset */
2971 local_offsets, /* offset vector */
2972 sizeof(local_offsets)/sizeof(int), /* size of same */
2973 local_workspace, /* workspace vector */
2974 sizeof(local_workspace)/sizeof(int), /* size of same */
2975 rlevel); /* function recursion level */
2979 const pcre_uchar *end_subpattern = code;
2980 int charcount = local_offsets[1] - local_offsets[0];
2981 int next_state_offset, repeat_state_offset;
2983 do { end_subpattern += GET(end_subpattern, 1); }
2984 while (*end_subpattern == OP_ALT);
2986 (int)(end_subpattern - start_code + LINK_SIZE + 1);
2988 /* If the end of this subpattern is KETRMAX or KETRMIN, we must
2989 arrange for the repeat state also to be added to the relevant list.
2990 Calculate the offset, or set -1 for no repeat. */
2992 repeat_state_offset = (*end_subpattern == OP_KETRMAX ||
2993 *end_subpattern == OP_KETRMIN)?
2994 (int)(end_subpattern - start_code - GET(end_subpattern, 1)) : -1;
2996 /* If we have matched an empty string, add the next state at the
2997 current character pointer. This is important so that the duplicate
2998 checking kicks in, which is what breaks infinite loops that match an
3003 ADD_ACTIVE(next_state_offset, 0);
3006 /* Optimization: if there are no more active states, and there
3007 are no new states yet set up, then skip over the subject string
3008 right here, to save looping. Otherwise, set up the new state to swing
3009 into action when the end of the matched substring is reached. */
3011 else if (i + 1 >= active_count && new_count == 0)
3015 ADD_NEW(next_state_offset, 0);
3017 /* If we are adding a repeat state at the new character position,
3018 we must fudge things so that it is the only current state.
3019 Otherwise, it might be a duplicate of one we processed before, and
3020 that would cause it to be skipped. */
3022 if (repeat_state_offset >= 0)
3024 next_active_state = active_states;
3027 ADD_ACTIVE(repeat_state_offset, 0);
3032 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3035 const pcre_uchar *p = start_subject + local_offsets[0];
3036 const pcre_uchar *pp = start_subject + local_offsets[1];
3037 while (p < pp) if (NOT_FIRSTCHAR(*p++)) charcount--;
3040 ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1));
3041 if (repeat_state_offset >= 0)
3042 { ADD_NEW_DATA(-repeat_state_offset, 0, (charcount - 1)); }
3045 else if (rc != PCRE_ERROR_NOMATCH) return rc;
3050 /* ========================================================================== */
3051 /* Handle callouts */
3055 if (PUBL(callout) != NULL)
3057 PUBL(callout_block) cb;
3058 cb.version = 1; /* Version 1 of the callout block */
3059 cb.callout_number = code[1];
3060 cb.offset_vector = offsets;
3061 #if defined COMPILE_PCRE8
3062 cb.subject = (PCRE_SPTR)start_subject;
3063 #elif defined COMPILE_PCRE16
3064 cb.subject = (PCRE_SPTR16)start_subject;
3065 #elif defined COMPILE_PCRE32
3066 cb.subject = (PCRE_SPTR32)start_subject;
3068 cb.subject_length = (int)(end_subject - start_subject);
3069 cb.start_match = (int)(current_subject - start_subject);
3070 cb.current_position = (int)(ptr - start_subject);
3071 cb.pattern_position = GET(code, 2);
3072 cb.next_item_length = GET(code, 2 + LINK_SIZE);
3074 cb.capture_last = -1;
3075 cb.callout_data = md->callout_data;
3076 cb.mark = NULL; /* No (*MARK) support */
3077 if ((rrc = (*PUBL(callout))(&cb)) < 0) return rrc; /* Abandon */
3080 { ADD_ACTIVE(state_offset + PRIV(OP_lengths)[OP_CALLOUT], 0); }
3084 /* ========================================================================== */
3085 default: /* Unsupported opcode */
3086 return PCRE_ERROR_DFA_UITEM;
3089 NEXT_ACTIVE_STATE: continue;
3091 } /* End of loop scanning active states */
3093 /* We have finished the processing at the current subject character. If no
3094 new states have been set for the next character, we have found all the
3095 matches that we are going to find. If we are at the top level and partial
3096 matching has been requested, check for appropriate conditions.
3098 The "forced_ fail" variable counts the number of (*F) encountered for the
3099 character. If it is equal to the original active_count (saved in
3100 workspace[1]) it means that (*F) was found on every active state. In this
3101 case we don't want to give a partial match.
3103 The "could_continue" variable is true if a state could have continued but
3104 for the fact that the end of the subject was reached. */
3108 if (rlevel == 1 && /* Top level, and */
3109 could_continue && /* Some could go on, and */
3110 forced_fail != workspace[1] && /* Not all forced fail & */
3112 (md->moptions & PCRE_PARTIAL_HARD) != 0 /* Hard partial */
3114 ((md->moptions & PCRE_PARTIAL_SOFT) != 0 && /* Soft partial and */
3115 match_count < 0) /* no matches */
3118 partial_newline || /* Either partial NL */
3120 ptr >= end_subject && /* End of subject and */
3121 ptr > md->start_used_ptr) /* Inspected non-empty string */
3124 match_count = PCRE_ERROR_PARTIAL;
3125 DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
3126 "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel, match_count,
3128 break; /* In effect, "return", but see the comment below */
3131 /* One or more states are active for the next character. */
3133 ptr += clen; /* Advance to next subject character */
3134 } /* Loop to move along the subject string */
3136 /* Control gets here from "break" a few lines above. We do it this way because
3137 if we use "return" above, we have compiler trouble. Some compilers warn if
3138 there's nothing here because they think the function doesn't return a value. On
3139 the other hand, if we put a dummy statement here, some more clever compilers
3140 complain that it can't be reached. Sigh. */
3148 /*************************************************
3149 * Execute a Regular Expression - DFA engine *
3150 *************************************************/
3152 /* This external function applies a compiled re to a subject string using a DFA
3153 engine. This function calls the internal function multiple times if the pattern
3157 argument_re points to the compiled expression
3158 extra_data points to extra data or is NULL
3159 subject points to the subject string
3160 length length of subject string (may contain binary zeros)
3161 start_offset where to start in the subject string
3163 offsets vector of match offsets
3164 offsetcount size of same
3165 workspace workspace vector
3166 wscount size of same
3168 Returns: > 0 => number of match offset pairs placed in offsets
3169 = 0 => offsets overflowed; longest matches are present
3170 -1 => failed to match
3171 < -1 => some kind of unexpected problem
3174 #if defined COMPILE_PCRE8
3175 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
3176 pcre_dfa_exec(const pcre *argument_re, const pcre_extra *extra_data,
3177 const char *subject, int length, int start_offset, int options, int *offsets,
3178 int offsetcount, int *workspace, int wscount)
3179 #elif defined COMPILE_PCRE16
3180 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
3181 pcre16_dfa_exec(const pcre16 *argument_re, const pcre16_extra *extra_data,
3182 PCRE_SPTR16 subject, int length, int start_offset, int options, int *offsets,
3183 int offsetcount, int *workspace, int wscount)
3184 #elif defined COMPILE_PCRE32
3185 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
3186 pcre32_dfa_exec(const pcre32 *argument_re, const pcre32_extra *extra_data,
3187 PCRE_SPTR32 subject, int length, int start_offset, int options, int *offsets,
3188 int offsetcount, int *workspace, int wscount)
3191 REAL_PCRE *re = (REAL_PCRE *)argument_re;
3192 dfa_match_data match_block;
3193 dfa_match_data *md = &match_block;
3194 BOOL utf, anchored, startline, firstline;
3195 const pcre_uchar *current_subject, *end_subject;
3196 const pcre_study_data *study = NULL;
3198 const pcre_uchar *req_char_ptr;
3199 const pcre_uint8 *start_bits = NULL;
3200 BOOL has_first_char = FALSE;
3201 BOOL has_req_char = FALSE;
3202 pcre_uchar first_char = 0;
3203 pcre_uchar first_char2 = 0;
3204 pcre_uchar req_char = 0;
3205 pcre_uchar req_char2 = 0;
3208 /* Plausibility checks */
3210 if ((options & ~PUBLIC_DFA_EXEC_OPTIONS) != 0) return PCRE_ERROR_BADOPTION;
3211 if (re == NULL || subject == NULL || workspace == NULL ||
3212 (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;
3213 if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
3214 if (wscount < 20) return PCRE_ERROR_DFA_WSSIZE;
3215 if (length < 0) return PCRE_ERROR_BADLENGTH;
3216 if (start_offset < 0 || start_offset > length) return PCRE_ERROR_BADOFFSET;
3218 /* Check that the first field in the block is the magic number. If it is not,
3219 return with PCRE_ERROR_BADMAGIC. However, if the magic number is equal to
3220 REVERSED_MAGIC_NUMBER we return with PCRE_ERROR_BADENDIANNESS, which
3221 means that the pattern is likely compiled with different endianness. */
3223 if (re->magic_number != MAGIC_NUMBER)
3224 return re->magic_number == REVERSED_MAGIC_NUMBER?
3225 PCRE_ERROR_BADENDIANNESS:PCRE_ERROR_BADMAGIC;
3226 if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
3228 /* If restarting after a partial match, do some sanity checks on the contents
3229 of the workspace. */
3231 if ((options & PCRE_DFA_RESTART) != 0)
3233 if ((workspace[0] & (-2)) != 0 || workspace[1] < 1 ||
3234 workspace[1] > (wscount - 2)/INTS_PER_STATEBLOCK)
3235 return PCRE_ERROR_DFA_BADRESTART;
3238 /* Set up study, callout, and table data */
3240 md->tables = re->tables;
3241 md->callout_data = NULL;
3243 if (extra_data != NULL)
3245 unsigned int flags = extra_data->flags;
3246 if ((flags & PCRE_EXTRA_STUDY_DATA) != 0)
3247 study = (const pcre_study_data *)extra_data->study_data;
3248 if ((flags & PCRE_EXTRA_MATCH_LIMIT) != 0) return PCRE_ERROR_DFA_UMLIMIT;
3249 if ((flags & PCRE_EXTRA_MATCH_LIMIT_RECURSION) != 0)
3250 return PCRE_ERROR_DFA_UMLIMIT;
3251 if ((flags & PCRE_EXTRA_CALLOUT_DATA) != 0)
3252 md->callout_data = extra_data->callout_data;
3253 if ((flags & PCRE_EXTRA_TABLES) != 0)
3254 md->tables = extra_data->tables;
3257 /* Set some local values */
3259 current_subject = (const pcre_uchar *)subject + start_offset;
3260 end_subject = (const pcre_uchar *)subject + length;
3261 req_char_ptr = current_subject - 1;
3264 /* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */
3265 utf = (re->options & PCRE_UTF8) != 0;
3270 anchored = (options & (PCRE_ANCHORED|PCRE_DFA_RESTART)) != 0 ||
3271 (re->options & PCRE_ANCHORED) != 0;
3273 /* The remaining fixed data for passing around. */
3275 md->start_code = (const pcre_uchar *)argument_re +
3276 re->name_table_offset + re->name_count * re->name_entry_size;
3277 md->start_subject = (const pcre_uchar *)subject;
3278 md->end_subject = end_subject;
3279 md->start_offset = start_offset;
3280 md->moptions = options;
3281 md->poptions = re->options;
3283 /* If the BSR option is not set at match time, copy what was set
3286 if ((md->moptions & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) == 0)
3288 if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0)
3289 md->moptions |= re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE);
3291 else md->moptions |= PCRE_BSR_ANYCRLF;
3295 /* Handle different types of newline. The three bits give eight cases. If
3296 nothing is set at run time, whatever was used at compile time applies. */
3298 switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options : (pcre_uint32)options) &
3301 case 0: newline = NEWLINE; break; /* Compile-time default */
3302 case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
3303 case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
3304 case PCRE_NEWLINE_CR+
3305 PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
3306 case PCRE_NEWLINE_ANY: newline = -1; break;
3307 case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
3308 default: return PCRE_ERROR_BADNEWLINE;
3313 md->nltype = NLTYPE_ANYCRLF;
3315 else if (newline < 0)
3317 md->nltype = NLTYPE_ANY;
3321 md->nltype = NLTYPE_FIXED;
3325 md->nl[0] = (newline >> 8) & 255;
3326 md->nl[1] = newline & 255;
3331 md->nl[0] = newline;
3335 /* Check a UTF-8 string if required. Unfortunately there's no way of passing
3336 back the character offset. */
3339 if (utf && (options & PCRE_NO_UTF8_CHECK) == 0)
3342 int errorcode = PRIV(valid_utf)((pcre_uchar *)subject, length, &erroroffset);
3345 if (offsetcount >= 2)
3347 offsets[0] = erroroffset;
3348 offsets[1] = errorcode;
3350 #if defined COMPILE_PCRE8
3351 return (errorcode <= PCRE_UTF8_ERR5 && (options & PCRE_PARTIAL_HARD) != 0) ?
3352 PCRE_ERROR_SHORTUTF8 : PCRE_ERROR_BADUTF8;
3353 #elif defined COMPILE_PCRE16
3354 return (errorcode <= PCRE_UTF16_ERR1 && (options & PCRE_PARTIAL_HARD) != 0) ?
3355 PCRE_ERROR_SHORTUTF16 : PCRE_ERROR_BADUTF16;
3356 #elif defined COMPILE_PCRE32
3357 return PCRE_ERROR_BADUTF32;
3360 #if defined COMPILE_PCRE8 || defined COMPILE_PCRE16
3361 if (start_offset > 0 && start_offset < length &&
3362 NOT_FIRSTCHAR(((PCRE_PUCHAR)subject)[start_offset]))
3363 return PCRE_ERROR_BADUTF8_OFFSET;
3368 /* If the exec call supplied NULL for tables, use the inbuilt ones. This
3369 is a feature that makes it possible to save compiled regex and re-use them
3370 in other programs later. */
3372 if (md->tables == NULL) md->tables = PRIV(default_tables);
3374 /* The "must be at the start of a line" flags are used in a loop when finding
3377 startline = (re->flags & PCRE_STARTLINE) != 0;
3378 firstline = (re->options & PCRE_FIRSTLINE) != 0;
3380 /* Set up the first character to match, if available. The first_byte value is
3381 never set for an anchored regular expression, but the anchoring may be forced
3382 at run time, so we have to test for anchoring. The first char may be unset for
3383 an unanchored pattern, of course. If there's no first char and the pattern was
3384 studied, there may be a bitmap of possible first characters. */
3388 if ((re->flags & PCRE_FIRSTSET) != 0)
3390 has_first_char = TRUE;
3391 first_char = first_char2 = (pcre_uchar)(re->first_char);
3392 if ((re->flags & PCRE_FCH_CASELESS) != 0)
3394 first_char2 = TABLE_GET(first_char, md->tables + fcc_offset, first_char);
3395 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
3396 if (utf && first_char > 127)
3397 first_char2 = UCD_OTHERCASE(first_char);
3403 if (!startline && study != NULL &&
3404 (study->flags & PCRE_STUDY_MAPPED) != 0)
3405 start_bits = study->start_bits;
3409 /* For anchored or unanchored matches, there may be a "last known required
3412 if ((re->flags & PCRE_REQCHSET) != 0)
3414 has_req_char = TRUE;
3415 req_char = req_char2 = (pcre_uchar)(re->req_char);
3416 if ((re->flags & PCRE_RCH_CASELESS) != 0)
3418 req_char2 = TABLE_GET(req_char, md->tables + fcc_offset, req_char);
3419 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
3420 if (utf && req_char > 127)
3421 req_char2 = UCD_OTHERCASE(req_char);
3426 /* Call the main matching function, looping for a non-anchored regex after a
3427 failed match. If not restarting, perform certain optimizations at the start of
3434 if ((options & PCRE_DFA_RESTART) == 0)
3436 const pcre_uchar *save_end_subject = end_subject;
3438 /* If firstline is TRUE, the start of the match is constrained to the first
3439 line of a multiline string. Implement this by temporarily adjusting
3440 end_subject so that we stop scanning at a newline. If the match fails at
3441 the newline, later code breaks this loop. */
3445 PCRE_PUCHAR t = current_subject;
3449 while (t < md->end_subject && !IS_NEWLINE(t))
3452 ACROSSCHAR(t < end_subject, *t, t++);
3457 while (t < md->end_subject && !IS_NEWLINE(t)) t++;
3461 /* There are some optimizations that avoid running the match if a known
3462 starting point is not found. However, there is an option that disables
3463 these, for testing and for ensuring that all callouts do actually occur.
3464 The option can be set in the regex by (*NO_START_OPT) or passed in
3465 match-time options. */
3467 if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0)
3469 /* Advance to a known first pcre_uchar (i.e. data item) */
3473 if (first_char != first_char2)
3476 while (current_subject < end_subject &&
3477 (csc = UCHAR21TEST(current_subject)) != first_char && csc != first_char2)
3481 while (current_subject < end_subject &&
3482 UCHAR21TEST(current_subject) != first_char)
3486 /* Or to just after a linebreak for a multiline match if possible */
3490 if (current_subject > md->start_subject + start_offset)
3495 while (current_subject < end_subject &&
3496 !WAS_NEWLINE(current_subject))
3499 ACROSSCHAR(current_subject < end_subject, *current_subject,
3505 while (current_subject < end_subject && !WAS_NEWLINE(current_subject))
3508 /* If we have just passed a CR and the newline option is ANY or
3509 ANYCRLF, and we are now at a LF, advance the match position by one
3512 if (UCHAR21TEST(current_subject - 1) == CHAR_CR &&
3513 (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&
3514 current_subject < end_subject &&
3515 UCHAR21TEST(current_subject) == CHAR_NL)
3520 /* Advance to a non-unique first pcre_uchar after study */
3522 else if (start_bits != NULL)
3524 while (current_subject < end_subject)
3526 register pcre_uint32 c = UCHAR21TEST(current_subject);
3527 #ifndef COMPILE_PCRE8
3528 if (c > 255) c = 255;
3530 if ((start_bits[c/8] & (1 << (c&7))) != 0) break;
3536 /* Restore fudged end_subject */
3538 end_subject = save_end_subject;
3540 /* The following two optimizations are disabled for partial matching or if
3541 disabling is explicitly requested (and of course, by the test above, this
3542 code is not obeyed when restarting after a partial match). */
3544 if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0 &&
3545 (options & (PCRE_PARTIAL_HARD|PCRE_PARTIAL_SOFT)) == 0)
3547 /* If the pattern was studied, a minimum subject length may be set. This
3548 is a lower bound; no actual string of that length may actually match the
3549 pattern. Although the value is, strictly, in characters, we treat it as
3550 in pcre_uchar units to avoid spending too much time in this optimization.
3553 if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 &&
3554 (pcre_uint32)(end_subject - current_subject) < study->minlength)
3555 return PCRE_ERROR_NOMATCH;
3557 /* If req_char is set, we know that that pcre_uchar must appear in the
3558 subject for the match to succeed. If the first pcre_uchar is set,
3559 req_char must be later in the subject; otherwise the test starts at the
3560 match point. This optimization can save a huge amount of work in patterns
3561 with nested unlimited repeats that aren't going to match. Writing
3562 separate code for cased/caseless versions makes it go faster, as does
3563 using an autoincrement and backing off on a match.
3565 HOWEVER: when the subject string is very, very long, searching to its end
3566 can take a long time, and give bad performance on quite ordinary
3567 patterns. This showed up when somebody was matching /^C/ on a 32-megabyte
3568 string... so we don't do this when the string is sufficiently long. */
3570 if (has_req_char && end_subject - current_subject < REQ_BYTE_MAX)
3572 register PCRE_PUCHAR p = current_subject + (has_first_char? 1:0);
3574 /* We don't need to repeat the search if we haven't yet reached the
3575 place we found it at last time. */
3577 if (p > req_char_ptr)
3579 if (req_char != req_char2)
3581 while (p < end_subject)
3583 register pcre_uint32 pp = UCHAR21INCTEST(p);
3584 if (pp == req_char || pp == req_char2) { p--; break; }
3589 while (p < end_subject)
3591 if (UCHAR21INCTEST(p) == req_char) { p--; break; }
3595 /* If we can't find the required pcre_uchar, break the matching loop,
3596 which will cause a return or PCRE_ERROR_NOMATCH. */
3598 if (p >= end_subject) break;
3600 /* If we have found the required pcre_uchar, save the point where we
3601 found it, so that we don't search again next time round the loop if
3602 the start hasn't passed this point yet. */
3608 } /* End of optimizations that are done when not restarting */
3610 /* OK, now we can do the business */
3612 md->start_used_ptr = current_subject;
3613 md->recursive = NULL;
3615 rc = internal_dfa_exec(
3616 md, /* fixed match data */
3617 md->start_code, /* this subexpression's code */
3618 current_subject, /* where we currently are */
3619 start_offset, /* start offset in subject */
3620 offsets, /* offset vector */
3621 offsetcount, /* size of same */
3622 workspace, /* workspace vector */
3623 wscount, /* size of same */
3624 0); /* function recurse level */
3626 /* Anything other than "no match" means we are done, always; otherwise, carry
3627 on only if not anchored. */
3629 if (rc != PCRE_ERROR_NOMATCH || anchored)
3631 if (rc == PCRE_ERROR_PARTIAL && offsetcount >= 2)
3633 offsets[0] = (int)(md->start_used_ptr - (PCRE_PUCHAR)subject);
3634 offsets[1] = (int)(end_subject - (PCRE_PUCHAR)subject);
3635 if (offsetcount > 2)
3636 offsets[2] = (int)(current_subject - (PCRE_PUCHAR)subject);
3641 /* Advance to the next subject character unless we are at the end of a line
3642 and firstline is set. */
3644 if (firstline && IS_NEWLINE(current_subject)) break;
3649 ACROSSCHAR(current_subject < end_subject, *current_subject,
3653 if (current_subject > end_subject) break;
3655 /* If we have just passed a CR and we are now at a LF, and the pattern does
3656 not contain any explicit matches for \r or \n, and the newline option is CRLF
3657 or ANY or ANYCRLF, advance the match position by one more character. */
3659 if (UCHAR21TEST(current_subject - 1) == CHAR_CR &&
3660 current_subject < end_subject &&
3661 UCHAR21TEST(current_subject) == CHAR_NL &&
3662 (re->flags & PCRE_HASCRORLF) == 0 &&
3663 (md->nltype == NLTYPE_ANY ||
3664 md->nltype == NLTYPE_ANYCRLF ||
3668 } /* "Bumpalong" loop */
3670 return PCRE_ERROR_NOMATCH;
3673 /* End of pcre_dfa_exec.c */