/[pcre]/code/trunk/pcre_exec.c
ViewVC logotype

Contents of /code/trunk/pcre_exec.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 915 - (show annotations)
Tue Feb 14 13:05:39 2012 UTC (7 years, 7 months ago) by zherczeg
File MIME type: text/plain
File size: 214270 byte(s)
Improved \X and back reference partial matching
1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
4
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
9 Copyright (c) 1997-2012 University of Cambridge
10
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
17
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21
22 * Neither the name of the University of Cambridge nor the names of its
23 contributors may be used to endorse or promote products derived from
24 this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37 -----------------------------------------------------------------------------
38 */
39
40
41 /* This module contains pcre_exec(), the externally visible function that does
42 pattern matching using an NFA algorithm, trying to mimic Perl as closely as
43 possible. There are also some static supporting functions. */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #define NLBLOCK md /* Block containing newline information */
50 #define PSSTART start_subject /* Field containing processed string start */
51 #define PSEND end_subject /* Field containing processed string end */
52
53 #include "pcre_internal.h"
54
55 /* Undefine some potentially clashing cpp symbols */
56
57 #undef min
58 #undef max
59
60 /* Values for setting in md->match_function_type to indicate two special types
61 of call to match(). We do it this way to save on using another stack variable,
62 as stack usage is to be discouraged. */
63
64 #define MATCH_CONDASSERT 1 /* Called to check a condition assertion */
65 #define MATCH_CBEGROUP 2 /* Could-be-empty unlimited repeat group */
66
67 /* Non-error returns from the match() function. Error returns are externally
68 defined PCRE_ERROR_xxx codes, which are all negative. */
69
70 #define MATCH_MATCH 1
71 #define MATCH_NOMATCH 0
72
73 /* Special internal returns from the match() function. Make them sufficiently
74 negative to avoid the external error codes. */
75
76 #define MATCH_ACCEPT (-999)
77 #define MATCH_COMMIT (-998)
78 #define MATCH_KETRPOS (-997)
79 #define MATCH_ONCE (-996)
80 #define MATCH_PRUNE (-995)
81 #define MATCH_SKIP (-994)
82 #define MATCH_SKIP_ARG (-993)
83 #define MATCH_THEN (-992)
84
85 /* Maximum number of ints of offset to save on the stack for recursive calls.
86 If the offset vector is bigger, malloc is used. This should be a multiple of 3,
87 because the offset vector is always a multiple of 3 long. */
88
89 #define REC_STACK_SAVE_MAX 30
90
91 /* Min and max values for the common repeats; for the maxima, 0 => infinity */
92
93 static const char rep_min[] = { 0, 0, 1, 1, 0, 0 };
94 static const char rep_max[] = { 0, 0, 0, 0, 1, 1 };
95
96
97
98 #ifdef PCRE_DEBUG
99 /*************************************************
100 * Debugging function to print chars *
101 *************************************************/
102
103 /* Print a sequence of chars in printable format, stopping at the end of the
104 subject if the requested.
105
106 Arguments:
107 p points to characters
108 length number to print
109 is_subject TRUE if printing from within md->start_subject
110 md pointer to matching data block, if is_subject is TRUE
111
112 Returns: nothing
113 */
114
115 static void
116 pchars(const pcre_uchar *p, int length, BOOL is_subject, match_data *md)
117 {
118 unsigned int c;
119 if (is_subject && length > md->end_subject - p) length = md->end_subject - p;
120 while (length-- > 0)
121 if (isprint(c = *(p++))) printf("%c", c); else printf("\\x%02x", c);
122 }
123 #endif
124
125
126
127 /*************************************************
128 * Match a back-reference *
129 *************************************************/
130
131 /* Normally, if a back reference hasn't been set, the length that is passed is
132 negative, so the match always fails. However, in JavaScript compatibility mode,
133 the length passed is zero. Note that in caseless UTF-8 mode, the number of
134 subject bytes matched may be different to the number of reference bytes.
135
136 Arguments:
137 offset index into the offset vector
138 eptr pointer into the subject
139 length length of reference to be matched (number of bytes)
140 md points to match data block
141 caseless TRUE if caseless
142
143 Returns: < 0 if not matched, otherwise the number of subject bytes matched
144 */
145
146 static int
147 match_ref(int offset, register PCRE_PUCHAR eptr, int length, match_data *md,
148 BOOL caseless)
149 {
150 int matched_length = length;
151 register PCRE_PUCHAR p = md->start_subject + md->offset_vector[offset];
152
153 #ifdef PCRE_DEBUG
154 if (eptr >= md->end_subject)
155 printf("matching subject <null>");
156 else
157 {
158 printf("matching subject ");
159 pchars(eptr, length, TRUE, md);
160 }
161 printf(" against backref ");
162 pchars(p, length, FALSE, md);
163 printf("\n");
164 #endif
165
166 /* Always fail if reference not set (and not JavaScript compatible). */
167
168 if (length < 0) return -1;
169
170 /* Separate the caseless case for speed. In UTF-8 mode we can only do this
171 properly if Unicode properties are supported. Otherwise, we can check only
172 ASCII characters. */
173
174 if (caseless)
175 {
176 #ifdef SUPPORT_UTF
177 #ifdef SUPPORT_UCP
178 if (md->utf)
179 {
180 /* Match characters up to the end of the reference. NOTE: the number of
181 bytes matched may differ, because there are some characters whose upper and
182 lower case versions code as different numbers of bytes. For example, U+023A
183 (2 bytes in UTF-8) is the upper case version of U+2C65 (3 bytes in UTF-8);
184 a sequence of 3 of the former uses 6 bytes, as does a sequence of two of
185 the latter. It is important, therefore, to check the length along the
186 reference, not along the subject (earlier code did this wrong). */
187
188 PCRE_PUCHAR endptr = p + length;
189 PCRE_PUCHAR eptr_start = eptr;
190 while (p < endptr)
191 {
192 int c, d;
193 if (eptr >= md->end_subject) return -((int)(eptr - eptr_start) + 1);
194 GETCHARINC(c, eptr);
195 GETCHARINC(d, p);
196 if (c != d && c != UCD_OTHERCASE(d)) return -1;
197 }
198 matched_length = (int)(eptr - eptr_start);
199 }
200 else
201 #endif
202 #endif
203
204 /* The same code works when not in UTF-8 mode and in UTF-8 mode when there
205 is no UCP support. */
206 {
207 if (eptr + length > md->end_subject)
208 {
209 if (md->partial == 0)
210 return -1;
211 length = (int)(md->end_subject - eptr);
212 matched_length = -(length + 1);
213 }
214 while (length-- > 0)
215 {
216 if (TABLE_GET(*p, md->lcc, *p) != TABLE_GET(*eptr, md->lcc, *eptr)) return -1;
217 p++;
218 eptr++;
219 }
220 }
221 }
222
223 /* In the caseful case, we can just compare the bytes, whether or not we
224 are in UTF-8 mode. */
225
226 else
227 {
228 if (eptr + length > md->end_subject)
229 {
230 if (md->partial == 0)
231 return -1;
232 length = (int)(md->end_subject - eptr);
233 matched_length = -(length + 1);
234 }
235 while (length-- > 0) if (*p++ != *eptr++) return -1;
236 }
237
238 return matched_length;
239 }
240
241
242
243 /***************************************************************************
244 ****************************************************************************
245 RECURSION IN THE match() FUNCTION
246
247 The match() function is highly recursive, though not every recursive call
248 increases the recursive depth. Nevertheless, some regular expressions can cause
249 it to recurse to a great depth. I was writing for Unix, so I just let it call
250 itself recursively. This uses the stack for saving everything that has to be
251 saved for a recursive call. On Unix, the stack can be large, and this works
252 fine.
253
254 It turns out that on some non-Unix-like systems there are problems with
255 programs that use a lot of stack. (This despite the fact that every last chip
256 has oodles of memory these days, and techniques for extending the stack have
257 been known for decades.) So....
258
259 There is a fudge, triggered by defining NO_RECURSE, which avoids recursive
260 calls by keeping local variables that need to be preserved in blocks of memory
261 obtained from malloc() instead instead of on the stack. Macros are used to
262 achieve this so that the actual code doesn't look very different to what it
263 always used to.
264
265 The original heap-recursive code used longjmp(). However, it seems that this
266 can be very slow on some operating systems. Following a suggestion from Stan
267 Switzer, the use of longjmp() has been abolished, at the cost of having to
268 provide a unique number for each call to RMATCH. There is no way of generating
269 a sequence of numbers at compile time in C. I have given them names, to make
270 them stand out more clearly.
271
272 Crude tests on x86 Linux show a small speedup of around 5-8%. However, on
273 FreeBSD, avoiding longjmp() more than halves the time taken to run the standard
274 tests. Furthermore, not using longjmp() means that local dynamic variables
275 don't have indeterminate values; this has meant that the frame size can be
276 reduced because the result can be "passed back" by straight setting of the
277 variable instead of being passed in the frame.
278 ****************************************************************************
279 ***************************************************************************/
280
281 /* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN
282 below must be updated in sync. */
283
284 enum { RM1=1, RM2, RM3, RM4, RM5, RM6, RM7, RM8, RM9, RM10,
285 RM11, RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,
286 RM21, RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
287 RM31, RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
288 RM41, RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
289 RM51, RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60,
290 RM61, RM62, RM63, RM64, RM65, RM66 };
291
292 /* These versions of the macros use the stack, as normal. There are debugging
293 versions and production versions. Note that the "rw" argument of RMATCH isn't
294 actually used in this definition. */
295
296 #ifndef NO_RECURSE
297 #define REGISTER register
298
299 #ifdef PCRE_DEBUG
300 #define RMATCH(ra,rb,rc,rd,re,rw) \
301 { \
302 printf("match() called in line %d\n", __LINE__); \
303 rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1); \
304 printf("to line %d\n", __LINE__); \
305 }
306 #define RRETURN(ra) \
307 { \
308 printf("match() returned %d from line %d ", ra, __LINE__); \
309 return ra; \
310 }
311 #else
312 #define RMATCH(ra,rb,rc,rd,re,rw) \
313 rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1)
314 #define RRETURN(ra) return ra
315 #endif
316
317 #else
318
319
320 /* These versions of the macros manage a private stack on the heap. Note that
321 the "rd" argument of RMATCH isn't actually used in this definition. It's the md
322 argument of match(), which never changes. */
323
324 #define REGISTER
325
326 #define RMATCH(ra,rb,rc,rd,re,rw)\
327 {\
328 heapframe *newframe = (heapframe *)(PUBL(stack_malloc))(sizeof(heapframe));\
329 if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\
330 frame->Xwhere = rw; \
331 newframe->Xeptr = ra;\
332 newframe->Xecode = rb;\
333 newframe->Xmstart = mstart;\
334 newframe->Xoffset_top = rc;\
335 newframe->Xeptrb = re;\
336 newframe->Xrdepth = frame->Xrdepth + 1;\
337 newframe->Xprevframe = frame;\
338 frame = newframe;\
339 DPRINTF(("restarting from line %d\n", __LINE__));\
340 goto HEAP_RECURSE;\
341 L_##rw:\
342 DPRINTF(("jumped back to line %d\n", __LINE__));\
343 }
344
345 #define RRETURN(ra)\
346 {\
347 heapframe *oldframe = frame;\
348 frame = oldframe->Xprevframe;\
349 if (oldframe != &frame_zero) (PUBL(stack_free))(oldframe);\
350 if (frame != NULL)\
351 {\
352 rrc = ra;\
353 goto HEAP_RETURN;\
354 }\
355 return ra;\
356 }
357
358
359 /* Structure for remembering the local variables in a private frame */
360
361 typedef struct heapframe {
362 struct heapframe *Xprevframe;
363
364 /* Function arguments that may change */
365
366 PCRE_PUCHAR Xeptr;
367 const pcre_uchar *Xecode;
368 PCRE_PUCHAR Xmstart;
369 int Xoffset_top;
370 eptrblock *Xeptrb;
371 unsigned int Xrdepth;
372
373 /* Function local variables */
374
375 PCRE_PUCHAR Xcallpat;
376 #ifdef SUPPORT_UTF
377 PCRE_PUCHAR Xcharptr;
378 #endif
379 PCRE_PUCHAR Xdata;
380 PCRE_PUCHAR Xnext;
381 PCRE_PUCHAR Xpp;
382 PCRE_PUCHAR Xprev;
383 PCRE_PUCHAR Xsaved_eptr;
384
385 recursion_info Xnew_recursive;
386
387 BOOL Xcur_is_word;
388 BOOL Xcondition;
389 BOOL Xprev_is_word;
390
391 #ifdef SUPPORT_UCP
392 int Xprop_type;
393 int Xprop_value;
394 int Xprop_fail_result;
395 int Xoclength;
396 pcre_uchar Xocchars[6];
397 #endif
398
399 int Xcodelink;
400 int Xctype;
401 unsigned int Xfc;
402 int Xfi;
403 int Xlength;
404 int Xmax;
405 int Xmin;
406 int Xnumber;
407 int Xoffset;
408 int Xop;
409 int Xsave_capture_last;
410 int Xsave_offset1, Xsave_offset2, Xsave_offset3;
411 int Xstacksave[REC_STACK_SAVE_MAX];
412
413 eptrblock Xnewptrb;
414
415 /* Where to jump back to */
416
417 int Xwhere;
418
419 } heapframe;
420
421 #endif
422
423
424 /***************************************************************************
425 ***************************************************************************/
426
427
428
429 /*************************************************
430 * Match from current position *
431 *************************************************/
432
433 /* This function is called recursively in many circumstances. Whenever it
434 returns a negative (error) response, the outer incarnation must also return the
435 same response. */
436
437 /* These macros pack up tests that are used for partial matching, and which
438 appear several times in the code. We set the "hit end" flag if the pointer is
439 at the end of the subject and also past the start of the subject (i.e.
440 something has been matched). For hard partial matching, we then return
441 immediately. The second one is used when we already know we are past the end of
442 the subject. */
443
444 #define CHECK_PARTIAL()\
445 if (md->partial != 0 && eptr >= md->end_subject && \
446 eptr > md->start_used_ptr) \
447 { \
448 md->hitend = TRUE; \
449 if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
450 }
451
452 #define SCHECK_PARTIAL()\
453 if (md->partial != 0 && eptr > md->start_used_ptr) \
454 { \
455 md->hitend = TRUE; \
456 if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
457 }
458
459
460 /* Performance note: It might be tempting to extract commonly used fields from
461 the md structure (e.g. utf, end_subject) into individual variables to improve
462 performance. Tests using gcc on a SPARC disproved this; in the first case, it
463 made performance worse.
464
465 Arguments:
466 eptr pointer to current character in subject
467 ecode pointer to current position in compiled code
468 mstart pointer to the current match start position (can be modified
469 by encountering \K)
470 offset_top current top pointer
471 md pointer to "static" info for the match
472 eptrb pointer to chain of blocks containing eptr at start of
473 brackets - for testing for empty matches
474 rdepth the recursion depth
475
476 Returns: MATCH_MATCH if matched ) these values are >= 0
477 MATCH_NOMATCH if failed to match )
478 a negative MATCH_xxx value for PRUNE, SKIP, etc
479 a negative PCRE_ERROR_xxx value if aborted by an error condition
480 (e.g. stopped by repeated call or recursion limit)
481 */
482
483 static int
484 match(REGISTER PCRE_PUCHAR eptr, REGISTER const pcre_uchar *ecode,
485 PCRE_PUCHAR mstart, int offset_top, match_data *md, eptrblock *eptrb,
486 unsigned int rdepth)
487 {
488 /* These variables do not need to be preserved over recursion in this function,
489 so they can be ordinary variables in all cases. Mark some of them with
490 "register" because they are used a lot in loops. */
491
492 register int rrc; /* Returns from recursive calls */
493 register int i; /* Used for loops not involving calls to RMATCH() */
494 register unsigned int c; /* Character values not kept over RMATCH() calls */
495 register BOOL utf; /* Local copy of UTF flag for speed */
496
497 BOOL minimize, possessive; /* Quantifier options */
498 BOOL caseless;
499 int condcode;
500
501 /* When recursion is not being used, all "local" variables that have to be
502 preserved over calls to RMATCH() are part of a "frame". We set up the top-level
503 frame on the stack here; subsequent instantiations are obtained from the heap
504 whenever RMATCH() does a "recursion". See the macro definitions above. Putting
505 the top-level on the stack rather than malloc-ing them all gives a performance
506 boost in many cases where there is not much "recursion". */
507
508 #ifdef NO_RECURSE
509 heapframe frame_zero;
510 heapframe *frame = &frame_zero;
511 frame->Xprevframe = NULL; /* Marks the top level */
512
513 /* Copy in the original argument variables */
514
515 frame->Xeptr = eptr;
516 frame->Xecode = ecode;
517 frame->Xmstart = mstart;
518 frame->Xoffset_top = offset_top;
519 frame->Xeptrb = eptrb;
520 frame->Xrdepth = rdepth;
521
522 /* This is where control jumps back to to effect "recursion" */
523
524 HEAP_RECURSE:
525
526 /* Macros make the argument variables come from the current frame */
527
528 #define eptr frame->Xeptr
529 #define ecode frame->Xecode
530 #define mstart frame->Xmstart
531 #define offset_top frame->Xoffset_top
532 #define eptrb frame->Xeptrb
533 #define rdepth frame->Xrdepth
534
535 /* Ditto for the local variables */
536
537 #ifdef SUPPORT_UTF
538 #define charptr frame->Xcharptr
539 #endif
540 #define callpat frame->Xcallpat
541 #define codelink frame->Xcodelink
542 #define data frame->Xdata
543 #define next frame->Xnext
544 #define pp frame->Xpp
545 #define prev frame->Xprev
546 #define saved_eptr frame->Xsaved_eptr
547
548 #define new_recursive frame->Xnew_recursive
549
550 #define cur_is_word frame->Xcur_is_word
551 #define condition frame->Xcondition
552 #define prev_is_word frame->Xprev_is_word
553
554 #ifdef SUPPORT_UCP
555 #define prop_type frame->Xprop_type
556 #define prop_value frame->Xprop_value
557 #define prop_fail_result frame->Xprop_fail_result
558 #define oclength frame->Xoclength
559 #define occhars frame->Xocchars
560 #endif
561
562 #define ctype frame->Xctype
563 #define fc frame->Xfc
564 #define fi frame->Xfi
565 #define length frame->Xlength
566 #define max frame->Xmax
567 #define min frame->Xmin
568 #define number frame->Xnumber
569 #define offset frame->Xoffset
570 #define op frame->Xop
571 #define save_capture_last frame->Xsave_capture_last
572 #define save_offset1 frame->Xsave_offset1
573 #define save_offset2 frame->Xsave_offset2
574 #define save_offset3 frame->Xsave_offset3
575 #define stacksave frame->Xstacksave
576
577 #define newptrb frame->Xnewptrb
578
579 /* When recursion is being used, local variables are allocated on the stack and
580 get preserved during recursion in the normal way. In this environment, fi and
581 i, and fc and c, can be the same variables. */
582
583 #else /* NO_RECURSE not defined */
584 #define fi i
585 #define fc c
586
587 /* Many of the following variables are used only in small blocks of the code.
588 My normal style of coding would have declared them within each of those blocks.
589 However, in order to accommodate the version of this code that uses an external
590 "stack" implemented on the heap, it is easier to declare them all here, so the
591 declarations can be cut out in a block. The only declarations within blocks
592 below are for variables that do not have to be preserved over a recursive call
593 to RMATCH(). */
594
595 #ifdef SUPPORT_UTF
596 const pcre_uchar *charptr;
597 #endif
598 const pcre_uchar *callpat;
599 const pcre_uchar *data;
600 const pcre_uchar *next;
601 PCRE_PUCHAR pp;
602 const pcre_uchar *prev;
603 PCRE_PUCHAR saved_eptr;
604
605 recursion_info new_recursive;
606
607 BOOL cur_is_word;
608 BOOL condition;
609 BOOL prev_is_word;
610
611 #ifdef SUPPORT_UCP
612 int prop_type;
613 int prop_value;
614 int prop_fail_result;
615 int oclength;
616 pcre_uchar occhars[6];
617 #endif
618
619 int codelink;
620 int ctype;
621 int length;
622 int max;
623 int min;
624 int number;
625 int offset;
626 int op;
627 int save_capture_last;
628 int save_offset1, save_offset2, save_offset3;
629 int stacksave[REC_STACK_SAVE_MAX];
630
631 eptrblock newptrb;
632
633 /* There is a special fudge for calling match() in a way that causes it to
634 measure the size of its basic stack frame when the stack is being used for
635 recursion. The second argument (ecode) being NULL triggers this behaviour. It
636 cannot normally ever be NULL. The return is the negated value of the frame
637 size. */
638
639 if (ecode == NULL)
640 {
641 if (rdepth == 0)
642 return match((PCRE_PUCHAR)&rdepth, NULL, NULL, 0, NULL, NULL, 1);
643 else
644 {
645 int len = (char *)&rdepth - (char *)eptr;
646 return (len > 0)? -len : len;
647 }
648 }
649 #endif /* NO_RECURSE */
650
651 /* To save space on the stack and in the heap frame, I have doubled up on some
652 of the local variables that are used only in localised parts of the code, but
653 still need to be preserved over recursive calls of match(). These macros define
654 the alternative names that are used. */
655
656 #define allow_zero cur_is_word
657 #define cbegroup condition
658 #define code_offset codelink
659 #define condassert condition
660 #define matched_once prev_is_word
661 #define foc number
662 #define save_mark data
663
664 /* These statements are here to stop the compiler complaining about unitialized
665 variables. */
666
667 #ifdef SUPPORT_UCP
668 prop_value = 0;
669 prop_fail_result = 0;
670 #endif
671
672
673 /* This label is used for tail recursion, which is used in a few cases even
674 when NO_RECURSE is not defined, in order to reduce the amount of stack that is
675 used. Thanks to Ian Taylor for noticing this possibility and sending the
676 original patch. */
677
678 TAIL_RECURSE:
679
680 /* OK, now we can get on with the real code of the function. Recursive calls
681 are specified by the macro RMATCH and RRETURN is used to return. When
682 NO_RECURSE is *not* defined, these just turn into a recursive call to match()
683 and a "return", respectively (possibly with some debugging if PCRE_DEBUG is
684 defined). However, RMATCH isn't like a function call because it's quite a
685 complicated macro. It has to be used in one particular way. This shouldn't,
686 however, impact performance when true recursion is being used. */
687
688 #ifdef SUPPORT_UTF
689 utf = md->utf; /* Local copy of the flag */
690 #else
691 utf = FALSE;
692 #endif
693
694 /* First check that we haven't called match() too many times, or that we
695 haven't exceeded the recursive call limit. */
696
697 if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
698 if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
699
700 /* At the start of a group with an unlimited repeat that may match an empty
701 string, the variable md->match_function_type is set to MATCH_CBEGROUP. It is
702 done this way to save having to use another function argument, which would take
703 up space on the stack. See also MATCH_CONDASSERT below.
704
705 When MATCH_CBEGROUP is set, add the current subject pointer to the chain of
706 such remembered pointers, to be checked when we hit the closing ket, in order
707 to break infinite loops that match no characters. When match() is called in
708 other circumstances, don't add to the chain. The MATCH_CBEGROUP feature must
709 NOT be used with tail recursion, because the memory block that is used is on
710 the stack, so a new one may be required for each match(). */
711
712 if (md->match_function_type == MATCH_CBEGROUP)
713 {
714 newptrb.epb_saved_eptr = eptr;
715 newptrb.epb_prev = eptrb;
716 eptrb = &newptrb;
717 md->match_function_type = 0;
718 }
719
720 /* Now start processing the opcodes. */
721
722 for (;;)
723 {
724 minimize = possessive = FALSE;
725 op = *ecode;
726
727 switch(op)
728 {
729 case OP_MARK:
730 md->nomatch_mark = ecode + 2;
731 md->mark = NULL; /* In case previously set by assertion */
732 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
733 eptrb, RM55);
734 if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
735 md->mark == NULL) md->mark = ecode + 2;
736
737 /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
738 argument, and we must check whether that argument matches this MARK's
739 argument. It is passed back in md->start_match_ptr (an overloading of that
740 variable). If it does match, we reset that variable to the current subject
741 position and return MATCH_SKIP. Otherwise, pass back the return code
742 unaltered. */
743
744 else if (rrc == MATCH_SKIP_ARG &&
745 STRCMP_UC_UC(ecode + 2, md->start_match_ptr) == 0)
746 {
747 md->start_match_ptr = eptr;
748 RRETURN(MATCH_SKIP);
749 }
750 RRETURN(rrc);
751
752 case OP_FAIL:
753 RRETURN(MATCH_NOMATCH);
754
755 /* COMMIT overrides PRUNE, SKIP, and THEN */
756
757 case OP_COMMIT:
758 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
759 eptrb, RM52);
760 if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE &&
761 rrc != MATCH_SKIP && rrc != MATCH_SKIP_ARG &&
762 rrc != MATCH_THEN)
763 RRETURN(rrc);
764 RRETURN(MATCH_COMMIT);
765
766 /* PRUNE overrides THEN */
767
768 case OP_PRUNE:
769 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
770 eptrb, RM51);
771 if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
772 RRETURN(MATCH_PRUNE);
773
774 case OP_PRUNE_ARG:
775 md->nomatch_mark = ecode + 2;
776 md->mark = NULL; /* In case previously set by assertion */
777 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
778 eptrb, RM56);
779 if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
780 md->mark == NULL) md->mark = ecode + 2;
781 if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
782 RRETURN(MATCH_PRUNE);
783
784 /* SKIP overrides PRUNE and THEN */
785
786 case OP_SKIP:
787 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
788 eptrb, RM53);
789 if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
790 RRETURN(rrc);
791 md->start_match_ptr = eptr; /* Pass back current position */
792 RRETURN(MATCH_SKIP);
793
794 /* Note that, for Perl compatibility, SKIP with an argument does NOT set
795 nomatch_mark. There is a flag that disables this opcode when re-matching a
796 pattern that ended with a SKIP for which there was not a matching MARK. */
797
798 case OP_SKIP_ARG:
799 if (md->ignore_skip_arg)
800 {
801 ecode += PRIV(OP_lengths)[*ecode] + ecode[1];
802 break;
803 }
804 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
805 eptrb, RM57);
806 if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
807 RRETURN(rrc);
808
809 /* Pass back the current skip name by overloading md->start_match_ptr and
810 returning the special MATCH_SKIP_ARG return code. This will either be
811 caught by a matching MARK, or get to the top, where it causes a rematch
812 with the md->ignore_skip_arg flag set. */
813
814 md->start_match_ptr = ecode + 2;
815 RRETURN(MATCH_SKIP_ARG);
816
817 /* For THEN (and THEN_ARG) we pass back the address of the opcode, so that
818 the branch in which it occurs can be determined. Overload the start of
819 match pointer to do this. */
820
821 case OP_THEN:
822 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
823 eptrb, RM54);
824 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
825 md->start_match_ptr = ecode;
826 RRETURN(MATCH_THEN);
827
828 case OP_THEN_ARG:
829 md->nomatch_mark = ecode + 2;
830 md->mark = NULL; /* In case previously set by assertion */
831 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top,
832 md, eptrb, RM58);
833 if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
834 md->mark == NULL) md->mark = ecode + 2;
835 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
836 md->start_match_ptr = ecode;
837 RRETURN(MATCH_THEN);
838
839 /* Handle an atomic group that does not contain any capturing parentheses.
840 This can be handled like an assertion. Prior to 8.13, all atomic groups
841 were handled this way. In 8.13, the code was changed as below for ONCE, so
842 that backups pass through the group and thereby reset captured values.
843 However, this uses a lot more stack, so in 8.20, atomic groups that do not
844 contain any captures generate OP_ONCE_NC, which can be handled in the old,
845 less stack intensive way.
846
847 Check the alternative branches in turn - the matching won't pass the KET
848 for this kind of subpattern. If any one branch matches, we carry on as at
849 the end of a normal bracket, leaving the subject pointer, but resetting
850 the start-of-match value in case it was changed by \K. */
851
852 case OP_ONCE_NC:
853 prev = ecode;
854 saved_eptr = eptr;
855 save_mark = md->mark;
856 do
857 {
858 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM64);
859 if (rrc == MATCH_MATCH) /* Note: _not_ MATCH_ACCEPT */
860 {
861 mstart = md->start_match_ptr;
862 break;
863 }
864 if (rrc == MATCH_THEN)
865 {
866 next = ecode + GET(ecode,1);
867 if (md->start_match_ptr < next &&
868 (*ecode == OP_ALT || *next == OP_ALT))
869 rrc = MATCH_NOMATCH;
870 }
871
872 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
873 ecode += GET(ecode,1);
874 md->mark = save_mark;
875 }
876 while (*ecode == OP_ALT);
877
878 /* If hit the end of the group (which could be repeated), fail */
879
880 if (*ecode != OP_ONCE_NC && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
881
882 /* Continue as from after the group, updating the offsets high water
883 mark, since extracts may have been taken. */
884
885 do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
886
887 offset_top = md->end_offset_top;
888 eptr = md->end_match_ptr;
889
890 /* For a non-repeating ket, just continue at this level. This also
891 happens for a repeating ket if no characters were matched in the group.
892 This is the forcible breaking of infinite loops as implemented in Perl
893 5.005. */
894
895 if (*ecode == OP_KET || eptr == saved_eptr)
896 {
897 ecode += 1+LINK_SIZE;
898 break;
899 }
900
901 /* The repeating kets try the rest of the pattern or restart from the
902 preceding bracket, in the appropriate order. The second "call" of match()
903 uses tail recursion, to avoid using another stack frame. */
904
905 if (*ecode == OP_KETRMIN)
906 {
907 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM65);
908 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
909 ecode = prev;
910 goto TAIL_RECURSE;
911 }
912 else /* OP_KETRMAX */
913 {
914 md->match_function_type = MATCH_CBEGROUP;
915 RMATCH(eptr, prev, offset_top, md, eptrb, RM66);
916 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
917 ecode += 1 + LINK_SIZE;
918 goto TAIL_RECURSE;
919 }
920 /* Control never gets here */
921
922 /* Handle a capturing bracket, other than those that are possessive with an
923 unlimited repeat. If there is space in the offset vector, save the current
924 subject position in the working slot at the top of the vector. We mustn't
925 change the current values of the data slot, because they may be set from a
926 previous iteration of this group, and be referred to by a reference inside
927 the group. A failure to match might occur after the group has succeeded,
928 if something later on doesn't match. For this reason, we need to restore
929 the working value and also the values of the final offsets, in case they
930 were set by a previous iteration of the same bracket.
931
932 If there isn't enough space in the offset vector, treat this as if it were
933 a non-capturing bracket. Don't worry about setting the flag for the error
934 case here; that is handled in the code for KET. */
935
936 case OP_CBRA:
937 case OP_SCBRA:
938 number = GET2(ecode, 1+LINK_SIZE);
939 offset = number << 1;
940
941 #ifdef PCRE_DEBUG
942 printf("start bracket %d\n", number);
943 printf("subject=");
944 pchars(eptr, 16, TRUE, md);
945 printf("\n");
946 #endif
947
948 if (offset < md->offset_max)
949 {
950 save_offset1 = md->offset_vector[offset];
951 save_offset2 = md->offset_vector[offset+1];
952 save_offset3 = md->offset_vector[md->offset_end - number];
953 save_capture_last = md->capture_last;
954 save_mark = md->mark;
955
956 DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
957 md->offset_vector[md->offset_end - number] =
958 (int)(eptr - md->start_subject);
959
960 for (;;)
961 {
962 if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
963 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
964 eptrb, RM1);
965 if (rrc == MATCH_ONCE) break; /* Backing up through an atomic group */
966
967 /* If we backed up to a THEN, check whether it is within the current
968 branch by comparing the address of the THEN that is passed back with
969 the end of the branch. If it is within the current branch, and the
970 branch is one of two or more alternatives (it either starts or ends
971 with OP_ALT), we have reached the limit of THEN's action, so convert
972 the return code to NOMATCH, which will cause normal backtracking to
973 happen from now on. Otherwise, THEN is passed back to an outer
974 alternative. This implements Perl's treatment of parenthesized groups,
975 where a group not containing | does not affect the current alternative,
976 that is, (X) is NOT the same as (X|(*F)). */
977
978 if (rrc == MATCH_THEN)
979 {
980 next = ecode + GET(ecode,1);
981 if (md->start_match_ptr < next &&
982 (*ecode == OP_ALT || *next == OP_ALT))
983 rrc = MATCH_NOMATCH;
984 }
985
986 /* Anything other than NOMATCH is passed back. */
987
988 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
989 md->capture_last = save_capture_last;
990 ecode += GET(ecode, 1);
991 md->mark = save_mark;
992 if (*ecode != OP_ALT) break;
993 }
994
995 DPRINTF(("bracket %d failed\n", number));
996 md->offset_vector[offset] = save_offset1;
997 md->offset_vector[offset+1] = save_offset2;
998 md->offset_vector[md->offset_end - number] = save_offset3;
999
1000 /* At this point, rrc will be one of MATCH_ONCE or MATCH_NOMATCH. */
1001
1002 RRETURN(rrc);
1003 }
1004
1005 /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
1006 as a non-capturing bracket. */
1007
1008 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1009 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1010
1011 DPRINTF(("insufficient capture room: treat as non-capturing\n"));
1012
1013 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1014 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1015
1016 /* Non-capturing or atomic group, except for possessive with unlimited
1017 repeat and ONCE group with no captures. Loop for all the alternatives.
1018
1019 When we get to the final alternative within the brackets, we used to return
1020 the result of a recursive call to match() whatever happened so it was
1021 possible to reduce stack usage by turning this into a tail recursion,
1022 except in the case of a possibly empty group. However, now that there is
1023 the possiblity of (*THEN) occurring in the final alternative, this
1024 optimization is no longer always possible.
1025
1026 We can optimize if we know there are no (*THEN)s in the pattern; at present
1027 this is the best that can be done.
1028
1029 MATCH_ONCE is returned when the end of an atomic group is successfully
1030 reached, but subsequent matching fails. It passes back up the tree (causing
1031 captured values to be reset) until the original atomic group level is
1032 reached. This is tested by comparing md->once_target with the start of the
1033 group. At this point, the return is converted into MATCH_NOMATCH so that
1034 previous backup points can be taken. */
1035
1036 case OP_ONCE:
1037 case OP_BRA:
1038 case OP_SBRA:
1039 DPRINTF(("start non-capturing bracket\n"));
1040
1041 for (;;)
1042 {
1043 if (op >= OP_SBRA || op == OP_ONCE) md->match_function_type = MATCH_CBEGROUP;
1044
1045 /* If this is not a possibly empty group, and there are no (*THEN)s in
1046 the pattern, and this is the final alternative, optimize as described
1047 above. */
1048
1049 else if (!md->hasthen && ecode[GET(ecode, 1)] != OP_ALT)
1050 {
1051 ecode += PRIV(OP_lengths)[*ecode];
1052 goto TAIL_RECURSE;
1053 }
1054
1055 /* In all other cases, we have to make another call to match(). */
1056
1057 save_mark = md->mark;
1058 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, eptrb,
1059 RM2);
1060
1061 /* See comment in the code for capturing groups above about handling
1062 THEN. */
1063
1064 if (rrc == MATCH_THEN)
1065 {
1066 next = ecode + GET(ecode,1);
1067 if (md->start_match_ptr < next &&
1068 (*ecode == OP_ALT || *next == OP_ALT))
1069 rrc = MATCH_NOMATCH;
1070 }
1071
1072 if (rrc != MATCH_NOMATCH)
1073 {
1074 if (rrc == MATCH_ONCE)
1075 {
1076 const pcre_uchar *scode = ecode;
1077 if (*scode != OP_ONCE) /* If not at start, find it */
1078 {
1079 while (*scode == OP_ALT) scode += GET(scode, 1);
1080 scode -= GET(scode, 1);
1081 }
1082 if (md->once_target == scode) rrc = MATCH_NOMATCH;
1083 }
1084 RRETURN(rrc);
1085 }
1086 ecode += GET(ecode, 1);
1087 md->mark = save_mark;
1088 if (*ecode != OP_ALT) break;
1089 }
1090
1091 RRETURN(MATCH_NOMATCH);
1092
1093 /* Handle possessive capturing brackets with an unlimited repeat. We come
1094 here from BRAZERO with allow_zero set TRUE. The offset_vector values are
1095 handled similarly to the normal case above. However, the matching is
1096 different. The end of these brackets will always be OP_KETRPOS, which
1097 returns MATCH_KETRPOS without going further in the pattern. By this means
1098 we can handle the group by iteration rather than recursion, thereby
1099 reducing the amount of stack needed. */
1100
1101 case OP_CBRAPOS:
1102 case OP_SCBRAPOS:
1103 allow_zero = FALSE;
1104
1105 POSSESSIVE_CAPTURE:
1106 number = GET2(ecode, 1+LINK_SIZE);
1107 offset = number << 1;
1108
1109 #ifdef PCRE_DEBUG
1110 printf("start possessive bracket %d\n", number);
1111 printf("subject=");
1112 pchars(eptr, 16, TRUE, md);
1113 printf("\n");
1114 #endif
1115
1116 if (offset < md->offset_max)
1117 {
1118 matched_once = FALSE;
1119 code_offset = (int)(ecode - md->start_code);
1120
1121 save_offset1 = md->offset_vector[offset];
1122 save_offset2 = md->offset_vector[offset+1];
1123 save_offset3 = md->offset_vector[md->offset_end - number];
1124 save_capture_last = md->capture_last;
1125
1126 DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
1127
1128 /* Each time round the loop, save the current subject position for use
1129 when the group matches. For MATCH_MATCH, the group has matched, so we
1130 restart it with a new subject starting position, remembering that we had
1131 at least one match. For MATCH_NOMATCH, carry on with the alternatives, as
1132 usual. If we haven't matched any alternatives in any iteration, check to
1133 see if a previous iteration matched. If so, the group has matched;
1134 continue from afterwards. Otherwise it has failed; restore the previous
1135 capture values before returning NOMATCH. */
1136
1137 for (;;)
1138 {
1139 md->offset_vector[md->offset_end - number] =
1140 (int)(eptr - md->start_subject);
1141 if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1142 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
1143 eptrb, RM63);
1144 if (rrc == MATCH_KETRPOS)
1145 {
1146 offset_top = md->end_offset_top;
1147 eptr = md->end_match_ptr;
1148 ecode = md->start_code + code_offset;
1149 save_capture_last = md->capture_last;
1150 matched_once = TRUE;
1151 continue;
1152 }
1153
1154 /* See comment in the code for capturing groups above about handling
1155 THEN. */
1156
1157 if (rrc == MATCH_THEN)
1158 {
1159 next = ecode + GET(ecode,1);
1160 if (md->start_match_ptr < next &&
1161 (*ecode == OP_ALT || *next == OP_ALT))
1162 rrc = MATCH_NOMATCH;
1163 }
1164
1165 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1166 md->capture_last = save_capture_last;
1167 ecode += GET(ecode, 1);
1168 if (*ecode != OP_ALT) break;
1169 }
1170
1171 if (!matched_once)
1172 {
1173 md->offset_vector[offset] = save_offset1;
1174 md->offset_vector[offset+1] = save_offset2;
1175 md->offset_vector[md->offset_end - number] = save_offset3;
1176 }
1177
1178 if (allow_zero || matched_once)
1179 {
1180 ecode += 1 + LINK_SIZE;
1181 break;
1182 }
1183
1184 RRETURN(MATCH_NOMATCH);
1185 }
1186
1187 /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
1188 as a non-capturing bracket. */
1189
1190 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1191 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1192
1193 DPRINTF(("insufficient capture room: treat as non-capturing\n"));
1194
1195 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1196 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1197
1198 /* Non-capturing possessive bracket with unlimited repeat. We come here
1199 from BRAZERO with allow_zero = TRUE. The code is similar to the above,
1200 without the capturing complication. It is written out separately for speed
1201 and cleanliness. */
1202
1203 case OP_BRAPOS:
1204 case OP_SBRAPOS:
1205 allow_zero = FALSE;
1206
1207 POSSESSIVE_NON_CAPTURE:
1208 matched_once = FALSE;
1209 code_offset = (int)(ecode - md->start_code);
1210
1211 for (;;)
1212 {
1213 if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1214 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
1215 eptrb, RM48);
1216 if (rrc == MATCH_KETRPOS)
1217 {
1218 offset_top = md->end_offset_top;
1219 eptr = md->end_match_ptr;
1220 ecode = md->start_code + code_offset;
1221 matched_once = TRUE;
1222 continue;
1223 }
1224
1225 /* See comment in the code for capturing groups above about handling
1226 THEN. */
1227
1228 if (rrc == MATCH_THEN)
1229 {
1230 next = ecode + GET(ecode,1);
1231 if (md->start_match_ptr < next &&
1232 (*ecode == OP_ALT || *next == OP_ALT))
1233 rrc = MATCH_NOMATCH;
1234 }
1235
1236 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1237 ecode += GET(ecode, 1);
1238 if (*ecode != OP_ALT) break;
1239 }
1240
1241 if (matched_once || allow_zero)
1242 {
1243 ecode += 1 + LINK_SIZE;
1244 break;
1245 }
1246 RRETURN(MATCH_NOMATCH);
1247
1248 /* Control never reaches here. */
1249
1250 /* Conditional group: compilation checked that there are no more than
1251 two branches. If the condition is false, skipping the first branch takes us
1252 past the end if there is only one branch, but that's OK because that is
1253 exactly what going to the ket would do. */
1254
1255 case OP_COND:
1256 case OP_SCOND:
1257 codelink = GET(ecode, 1);
1258
1259 /* Because of the way auto-callout works during compile, a callout item is
1260 inserted between OP_COND and an assertion condition. */
1261
1262 if (ecode[LINK_SIZE+1] == OP_CALLOUT)
1263 {
1264 if (PUBL(callout) != NULL)
1265 {
1266 PUBL(callout_block) cb;
1267 cb.version = 2; /* Version 1 of the callout block */
1268 cb.callout_number = ecode[LINK_SIZE+2];
1269 cb.offset_vector = md->offset_vector;
1270 #ifdef COMPILE_PCRE8
1271 cb.subject = (PCRE_SPTR)md->start_subject;
1272 #else
1273 cb.subject = (PCRE_SPTR16)md->start_subject;
1274 #endif
1275 cb.subject_length = (int)(md->end_subject - md->start_subject);
1276 cb.start_match = (int)(mstart - md->start_subject);
1277 cb.current_position = (int)(eptr - md->start_subject);
1278 cb.pattern_position = GET(ecode, LINK_SIZE + 3);
1279 cb.next_item_length = GET(ecode, 3 + 2*LINK_SIZE);
1280 cb.capture_top = offset_top/2;
1281 cb.capture_last = md->capture_last;
1282 cb.callout_data = md->callout_data;
1283 cb.mark = md->nomatch_mark;
1284 if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH);
1285 if (rrc < 0) RRETURN(rrc);
1286 }
1287 ecode += PRIV(OP_lengths)[OP_CALLOUT];
1288 }
1289
1290 condcode = ecode[LINK_SIZE+1];
1291
1292 /* Now see what the actual condition is */
1293
1294 if (condcode == OP_RREF || condcode == OP_NRREF) /* Recursion test */
1295 {
1296 if (md->recursive == NULL) /* Not recursing => FALSE */
1297 {
1298 condition = FALSE;
1299 ecode += GET(ecode, 1);
1300 }
1301 else
1302 {
1303 int recno = GET2(ecode, LINK_SIZE + 2); /* Recursion group number*/
1304 condition = (recno == RREF_ANY || recno == md->recursive->group_num);
1305
1306 /* If the test is for recursion into a specific subpattern, and it is
1307 false, but the test was set up by name, scan the table to see if the
1308 name refers to any other numbers, and test them. The condition is true
1309 if any one is set. */
1310
1311 if (!condition && condcode == OP_NRREF)
1312 {
1313 pcre_uchar *slotA = md->name_table;
1314 for (i = 0; i < md->name_count; i++)
1315 {
1316 if (GET2(slotA, 0) == recno) break;
1317 slotA += md->name_entry_size;
1318 }
1319
1320 /* Found a name for the number - there can be only one; duplicate
1321 names for different numbers are allowed, but not vice versa. First
1322 scan down for duplicates. */
1323
1324 if (i < md->name_count)
1325 {
1326 pcre_uchar *slotB = slotA;
1327 while (slotB > md->name_table)
1328 {
1329 slotB -= md->name_entry_size;
1330 if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)
1331 {
1332 condition = GET2(slotB, 0) == md->recursive->group_num;
1333 if (condition) break;
1334 }
1335 else break;
1336 }
1337
1338 /* Scan up for duplicates */
1339
1340 if (!condition)
1341 {
1342 slotB = slotA;
1343 for (i++; i < md->name_count; i++)
1344 {
1345 slotB += md->name_entry_size;
1346 if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)
1347 {
1348 condition = GET2(slotB, 0) == md->recursive->group_num;
1349 if (condition) break;
1350 }
1351 else break;
1352 }
1353 }
1354 }
1355 }
1356
1357 /* Chose branch according to the condition */
1358
1359 ecode += condition? 1 + IMM2_SIZE : GET(ecode, 1);
1360 }
1361 }
1362
1363 else if (condcode == OP_CREF || condcode == OP_NCREF) /* Group used test */
1364 {
1365 offset = GET2(ecode, LINK_SIZE+2) << 1; /* Doubled ref number */
1366 condition = offset < offset_top && md->offset_vector[offset] >= 0;
1367
1368 /* If the numbered capture is unset, but the reference was by name,
1369 scan the table to see if the name refers to any other numbers, and test
1370 them. The condition is true if any one is set. This is tediously similar
1371 to the code above, but not close enough to try to amalgamate. */
1372
1373 if (!condition && condcode == OP_NCREF)
1374 {
1375 int refno = offset >> 1;
1376 pcre_uchar *slotA = md->name_table;
1377
1378 for (i = 0; i < md->name_count; i++)
1379 {
1380 if (GET2(slotA, 0) == refno) break;
1381 slotA += md->name_entry_size;
1382 }
1383
1384 /* Found a name for the number - there can be only one; duplicate names
1385 for different numbers are allowed, but not vice versa. First scan down
1386 for duplicates. */
1387
1388 if (i < md->name_count)
1389 {
1390 pcre_uchar *slotB = slotA;
1391 while (slotB > md->name_table)
1392 {
1393 slotB -= md->name_entry_size;
1394 if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)
1395 {
1396 offset = GET2(slotB, 0) << 1;
1397 condition = offset < offset_top &&
1398 md->offset_vector[offset] >= 0;
1399 if (condition) break;
1400 }
1401 else break;
1402 }
1403
1404 /* Scan up for duplicates */
1405
1406 if (!condition)
1407 {
1408 slotB = slotA;
1409 for (i++; i < md->name_count; i++)
1410 {
1411 slotB += md->name_entry_size;
1412 if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)
1413 {
1414 offset = GET2(slotB, 0) << 1;
1415 condition = offset < offset_top &&
1416 md->offset_vector[offset] >= 0;
1417 if (condition) break;
1418 }
1419 else break;
1420 }
1421 }
1422 }
1423 }
1424
1425 /* Chose branch according to the condition */
1426
1427 ecode += condition? 1 + IMM2_SIZE : GET(ecode, 1);
1428 }
1429
1430 else if (condcode == OP_DEF) /* DEFINE - always false */
1431 {
1432 condition = FALSE;
1433 ecode += GET(ecode, 1);
1434 }
1435
1436 /* The condition is an assertion. Call match() to evaluate it - setting
1437 md->match_function_type to MATCH_CONDASSERT causes it to stop at the end of
1438 an assertion. */
1439
1440 else
1441 {
1442 md->match_function_type = MATCH_CONDASSERT;
1443 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM3);
1444 if (rrc == MATCH_MATCH)
1445 {
1446 if (md->end_offset_top > offset_top)
1447 offset_top = md->end_offset_top; /* Captures may have happened */
1448 condition = TRUE;
1449 ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
1450 while (*ecode == OP_ALT) ecode += GET(ecode, 1);
1451 }
1452
1453 /* PCRE doesn't allow the effect of (*THEN) to escape beyond an
1454 assertion; it is therefore treated as NOMATCH. */
1455
1456 else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
1457 {
1458 RRETURN(rrc); /* Need braces because of following else */
1459 }
1460 else
1461 {
1462 condition = FALSE;
1463 ecode += codelink;
1464 }
1465 }
1466
1467 /* We are now at the branch that is to be obeyed. As there is only one, can
1468 use tail recursion to avoid using another stack frame, except when there is
1469 unlimited repeat of a possibly empty group. In the latter case, a recursive
1470 call to match() is always required, unless the second alternative doesn't
1471 exist, in which case we can just plough on. Note that, for compatibility
1472 with Perl, the | in a conditional group is NOT treated as creating two
1473 alternatives. If a THEN is encountered in the branch, it propagates out to
1474 the enclosing alternative (unless nested in a deeper set of alternatives,
1475 of course). */
1476
1477 if (condition || *ecode == OP_ALT)
1478 {
1479 if (op != OP_SCOND)
1480 {
1481 ecode += 1 + LINK_SIZE;
1482 goto TAIL_RECURSE;
1483 }
1484
1485 md->match_function_type = MATCH_CBEGROUP;
1486 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM49);
1487 RRETURN(rrc);
1488 }
1489
1490 /* Condition false & no alternative; continue after the group. */
1491
1492 else
1493 {
1494 ecode += 1 + LINK_SIZE;
1495 }
1496 break;
1497
1498
1499 /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
1500 to close any currently open capturing brackets. */
1501
1502 case OP_CLOSE:
1503 number = GET2(ecode, 1);
1504 offset = number << 1;
1505
1506 #ifdef PCRE_DEBUG
1507 printf("end bracket %d at *ACCEPT", number);
1508 printf("\n");
1509 #endif
1510
1511 md->capture_last = number;
1512 if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1513 {
1514 md->offset_vector[offset] =
1515 md->offset_vector[md->offset_end - number];
1516 md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1517 if (offset_top <= offset) offset_top = offset + 2;
1518 }
1519 ecode += 1 + IMM2_SIZE;
1520 break;
1521
1522
1523 /* End of the pattern, either real or forced. */
1524
1525 case OP_END:
1526 case OP_ACCEPT:
1527 case OP_ASSERT_ACCEPT:
1528
1529 /* If we have matched an empty string, fail if not in an assertion and not
1530 in a recursion if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART
1531 is set and we have matched at the start of the subject. In both cases,
1532 backtracking will then try other alternatives, if any. */
1533
1534 if (eptr == mstart && op != OP_ASSERT_ACCEPT &&
1535 md->recursive == NULL &&
1536 (md->notempty ||
1537 (md->notempty_atstart &&
1538 mstart == md->start_subject + md->start_offset)))
1539 RRETURN(MATCH_NOMATCH);
1540
1541 /* Otherwise, we have a match. */
1542
1543 md->end_match_ptr = eptr; /* Record where we ended */
1544 md->end_offset_top = offset_top; /* and how many extracts were taken */
1545 md->start_match_ptr = mstart; /* and the start (\K can modify) */
1546
1547 /* For some reason, the macros don't work properly if an expression is
1548 given as the argument to RRETURN when the heap is in use. */
1549
1550 rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
1551 RRETURN(rrc);
1552
1553 /* Assertion brackets. Check the alternative branches in turn - the
1554 matching won't pass the KET for an assertion. If any one branch matches,
1555 the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
1556 start of each branch to move the current point backwards, so the code at
1557 this level is identical to the lookahead case. When the assertion is part
1558 of a condition, we want to return immediately afterwards. The caller of
1559 this incarnation of the match() function will have set MATCH_CONDASSERT in
1560 md->match_function type, and one of these opcodes will be the first opcode
1561 that is processed. We use a local variable that is preserved over calls to
1562 match() to remember this case. */
1563
1564 case OP_ASSERT:
1565 case OP_ASSERTBACK:
1566 save_mark = md->mark;
1567 if (md->match_function_type == MATCH_CONDASSERT)
1568 {
1569 condassert = TRUE;
1570 md->match_function_type = 0;
1571 }
1572 else condassert = FALSE;
1573
1574 do
1575 {
1576 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
1577 if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1578 {
1579 mstart = md->start_match_ptr; /* In case \K reset it */
1580 break;
1581 }
1582
1583 /* PCRE does not allow THEN to escape beyond an assertion; it is treated
1584 as NOMATCH. */
1585
1586 if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
1587 ecode += GET(ecode, 1);
1588 md->mark = save_mark;
1589 }
1590 while (*ecode == OP_ALT);
1591
1592 if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);
1593
1594 /* If checking an assertion for a condition, return MATCH_MATCH. */
1595
1596 if (condassert) RRETURN(MATCH_MATCH);
1597
1598 /* Continue from after the assertion, updating the offsets high water
1599 mark, since extracts may have been taken during the assertion. */
1600
1601 do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1602 ecode += 1 + LINK_SIZE;
1603 offset_top = md->end_offset_top;
1604 continue;
1605
1606 /* Negative assertion: all branches must fail to match. Encountering SKIP,
1607 PRUNE, or COMMIT means we must assume failure without checking subsequent
1608 branches. */
1609
1610 case OP_ASSERT_NOT:
1611 case OP_ASSERTBACK_NOT:
1612 save_mark = md->mark;
1613 if (md->match_function_type == MATCH_CONDASSERT)
1614 {
1615 condassert = TRUE;
1616 md->match_function_type = 0;
1617 }
1618 else condassert = FALSE;
1619
1620 do
1621 {
1622 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
1623 md->mark = save_mark;
1624 if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) RRETURN(MATCH_NOMATCH);
1625 if (rrc == MATCH_SKIP || rrc == MATCH_PRUNE || rrc == MATCH_COMMIT)
1626 {
1627 do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1628 break;
1629 }
1630
1631 /* PCRE does not allow THEN to escape beyond an assertion; it is treated
1632 as NOMATCH. */
1633
1634 if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
1635 ecode += GET(ecode,1);
1636 }
1637 while (*ecode == OP_ALT);
1638
1639 if (condassert) RRETURN(MATCH_MATCH); /* Condition assertion */
1640
1641 ecode += 1 + LINK_SIZE;
1642 continue;
1643
1644 /* Move the subject pointer back. This occurs only at the start of
1645 each branch of a lookbehind assertion. If we are too close to the start to
1646 move back, this match function fails. When working with UTF-8 we move
1647 back a number of characters, not bytes. */
1648
1649 case OP_REVERSE:
1650 #ifdef SUPPORT_UTF
1651 if (utf)
1652 {
1653 i = GET(ecode, 1);
1654 while (i-- > 0)
1655 {
1656 eptr--;
1657 if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1658 BACKCHAR(eptr);
1659 }
1660 }
1661 else
1662 #endif
1663
1664 /* No UTF-8 support, or not in UTF-8 mode: count is byte count */
1665
1666 {
1667 eptr -= GET(ecode, 1);
1668 if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1669 }
1670
1671 /* Save the earliest consulted character, then skip to next op code */
1672
1673 if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
1674 ecode += 1 + LINK_SIZE;
1675 break;
1676
1677 /* The callout item calls an external function, if one is provided, passing
1678 details of the match so far. This is mainly for debugging, though the
1679 function is able to force a failure. */
1680
1681 case OP_CALLOUT:
1682 if (PUBL(callout) != NULL)
1683 {
1684 PUBL(callout_block) cb;
1685 cb.version = 2; /* Version 1 of the callout block */
1686 cb.callout_number = ecode[1];
1687 cb.offset_vector = md->offset_vector;
1688 #ifdef COMPILE_PCRE8
1689 cb.subject = (PCRE_SPTR)md->start_subject;
1690 #else
1691 cb.subject = (PCRE_SPTR16)md->start_subject;
1692 #endif
1693 cb.subject_length = (int)(md->end_subject - md->start_subject);
1694 cb.start_match = (int)(mstart - md->start_subject);
1695 cb.current_position = (int)(eptr - md->start_subject);
1696 cb.pattern_position = GET(ecode, 2);
1697 cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
1698 cb.capture_top = offset_top/2;
1699 cb.capture_last = md->capture_last;
1700 cb.callout_data = md->callout_data;
1701 cb.mark = md->nomatch_mark;
1702 if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH);
1703 if (rrc < 0) RRETURN(rrc);
1704 }
1705 ecode += 2 + 2*LINK_SIZE;
1706 break;
1707
1708 /* Recursion either matches the current regex, or some subexpression. The
1709 offset data is the offset to the starting bracket from the start of the
1710 whole pattern. (This is so that it works from duplicated subpatterns.)
1711
1712 The state of the capturing groups is preserved over recursion, and
1713 re-instated afterwards. We don't know how many are started and not yet
1714 finished (offset_top records the completed total) so we just have to save
1715 all the potential data. There may be up to 65535 such values, which is too
1716 large to put on the stack, but using malloc for small numbers seems
1717 expensive. As a compromise, the stack is used when there are no more than
1718 REC_STACK_SAVE_MAX values to store; otherwise malloc is used.
1719
1720 There are also other values that have to be saved. We use a chained
1721 sequence of blocks that actually live on the stack. Thanks to Robin Houston
1722 for the original version of this logic. It has, however, been hacked around
1723 a lot, so he is not to blame for the current way it works. */
1724
1725 case OP_RECURSE:
1726 {
1727 recursion_info *ri;
1728 int recno;
1729
1730 callpat = md->start_code + GET(ecode, 1);
1731 recno = (callpat == md->start_code)? 0 :
1732 GET2(callpat, 1 + LINK_SIZE);
1733
1734 /* Check for repeating a recursion without advancing the subject pointer.
1735 This should catch convoluted mutual recursions. (Some simple cases are
1736 caught at compile time.) */
1737
1738 for (ri = md->recursive; ri != NULL; ri = ri->prevrec)
1739 if (recno == ri->group_num && eptr == ri->subject_position)
1740 RRETURN(PCRE_ERROR_RECURSELOOP);
1741
1742 /* Add to "recursing stack" */
1743
1744 new_recursive.group_num = recno;
1745 new_recursive.subject_position = eptr;
1746 new_recursive.prevrec = md->recursive;
1747 md->recursive = &new_recursive;
1748
1749 /* Where to continue from afterwards */
1750
1751 ecode += 1 + LINK_SIZE;
1752
1753 /* Now save the offset data */
1754
1755 new_recursive.saved_max = md->offset_end;
1756 if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)
1757 new_recursive.offset_save = stacksave;
1758 else
1759 {
1760 new_recursive.offset_save =
1761 (int *)(PUBL(malloc))(new_recursive.saved_max * sizeof(int));
1762 if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
1763 }
1764 memcpy(new_recursive.offset_save, md->offset_vector,
1765 new_recursive.saved_max * sizeof(int));
1766
1767 /* OK, now we can do the recursion. After processing each alternative,
1768 restore the offset data. If there were nested recursions, md->recursive
1769 might be changed, so reset it before looping. */
1770
1771 DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1772 cbegroup = (*callpat >= OP_SBRA);
1773 do
1774 {
1775 if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
1776 RMATCH(eptr, callpat + PRIV(OP_lengths)[*callpat], offset_top,
1777 md, eptrb, RM6);
1778 memcpy(md->offset_vector, new_recursive.offset_save,
1779 new_recursive.saved_max * sizeof(int));
1780 md->recursive = new_recursive.prevrec;
1781 if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1782 {
1783 DPRINTF(("Recursion matched\n"));
1784 if (new_recursive.offset_save != stacksave)
1785 (PUBL(free))(new_recursive.offset_save);
1786
1787 /* Set where we got to in the subject, and reset the start in case
1788 it was changed by \K. This *is* propagated back out of a recursion,
1789 for Perl compatibility. */
1790
1791 eptr = md->end_match_ptr;
1792 mstart = md->start_match_ptr;
1793 goto RECURSION_MATCHED; /* Exit loop; end processing */
1794 }
1795
1796 /* PCRE does not allow THEN to escape beyond a recursion; it is treated
1797 as NOMATCH. */
1798
1799 else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
1800 {
1801 DPRINTF(("Recursion gave error %d\n", rrc));
1802 if (new_recursive.offset_save != stacksave)
1803 (PUBL(free))(new_recursive.offset_save);
1804 RRETURN(rrc);
1805 }
1806
1807 md->recursive = &new_recursive;
1808 callpat += GET(callpat, 1);
1809 }
1810 while (*callpat == OP_ALT);
1811
1812 DPRINTF(("Recursion didn't match\n"));
1813 md->recursive = new_recursive.prevrec;
1814 if (new_recursive.offset_save != stacksave)
1815 (PUBL(free))(new_recursive.offset_save);
1816 RRETURN(MATCH_NOMATCH);
1817 }
1818
1819 RECURSION_MATCHED:
1820 break;
1821
1822 /* An alternation is the end of a branch; scan along to find the end of the
1823 bracketed group and go to there. */
1824
1825 case OP_ALT:
1826 do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1827 break;
1828
1829 /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
1830 indicating that it may occur zero times. It may repeat infinitely, or not
1831 at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets
1832 with fixed upper repeat limits are compiled as a number of copies, with the
1833 optional ones preceded by BRAZERO or BRAMINZERO. */
1834
1835 case OP_BRAZERO:
1836 next = ecode + 1;
1837 RMATCH(eptr, next, offset_top, md, eptrb, RM10);
1838 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1839 do next += GET(next, 1); while (*next == OP_ALT);
1840 ecode = next + 1 + LINK_SIZE;
1841 break;
1842
1843 case OP_BRAMINZERO:
1844 next = ecode + 1;
1845 do next += GET(next, 1); while (*next == OP_ALT);
1846 RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
1847 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1848 ecode++;
1849 break;
1850
1851 case OP_SKIPZERO:
1852 next = ecode+1;
1853 do next += GET(next,1); while (*next == OP_ALT);
1854 ecode = next + 1 + LINK_SIZE;
1855 break;
1856
1857 /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
1858 here; just jump to the group, with allow_zero set TRUE. */
1859
1860 case OP_BRAPOSZERO:
1861 op = *(++ecode);
1862 allow_zero = TRUE;
1863 if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
1864 goto POSSESSIVE_NON_CAPTURE;
1865
1866 /* End of a group, repeated or non-repeating. */
1867
1868 case OP_KET:
1869 case OP_KETRMIN:
1870 case OP_KETRMAX:
1871 case OP_KETRPOS:
1872 prev = ecode - GET(ecode, 1);
1873
1874 /* If this was a group that remembered the subject start, in order to break
1875 infinite repeats of empty string matches, retrieve the subject start from
1876 the chain. Otherwise, set it NULL. */
1877
1878 if (*prev >= OP_SBRA || *prev == OP_ONCE)
1879 {
1880 saved_eptr = eptrb->epb_saved_eptr; /* Value at start of group */
1881 eptrb = eptrb->epb_prev; /* Backup to previous group */
1882 }
1883 else saved_eptr = NULL;
1884
1885 /* If we are at the end of an assertion group or a non-capturing atomic
1886 group, stop matching and return MATCH_MATCH, but record the current high
1887 water mark for use by positive assertions. We also need to record the match
1888 start in case it was changed by \K. */
1889
1890 if ((*prev >= OP_ASSERT && *prev <= OP_ASSERTBACK_NOT) ||
1891 *prev == OP_ONCE_NC)
1892 {
1893 md->end_match_ptr = eptr; /* For ONCE_NC */
1894 md->end_offset_top = offset_top;
1895 md->start_match_ptr = mstart;
1896 RRETURN(MATCH_MATCH); /* Sets md->mark */
1897 }
1898
1899 /* For capturing groups we have to check the group number back at the start
1900 and if necessary complete handling an extraction by setting the offsets and
1901 bumping the high water mark. Whole-pattern recursion is coded as a recurse
1902 into group 0, so it won't be picked up here. Instead, we catch it when the
1903 OP_END is reached. Other recursion is handled here. We just have to record
1904 the current subject position and start match pointer and give a MATCH
1905 return. */
1906
1907 if (*prev == OP_CBRA || *prev == OP_SCBRA ||
1908 *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
1909 {
1910 number = GET2(prev, 1+LINK_SIZE);
1911 offset = number << 1;
1912
1913 #ifdef PCRE_DEBUG
1914 printf("end bracket %d", number);
1915 printf("\n");
1916 #endif
1917
1918 /* Handle a recursively called group. */
1919
1920 if (md->recursive != NULL && md->recursive->group_num == number)
1921 {
1922 md->end_match_ptr = eptr;
1923 md->start_match_ptr = mstart;
1924 RRETURN(MATCH_MATCH);
1925 }
1926
1927 /* Deal with capturing */
1928
1929 md->capture_last = number;
1930 if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1931 {
1932 /* If offset is greater than offset_top, it means that we are
1933 "skipping" a capturing group, and that group's offsets must be marked
1934 unset. In earlier versions of PCRE, all the offsets were unset at the
1935 start of matching, but this doesn't work because atomic groups and
1936 assertions can cause a value to be set that should later be unset.
1937 Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as
1938 part of the atomic group, but this is not on the final matching path,
1939 so must be unset when 2 is set. (If there is no group 2, there is no
1940 problem, because offset_top will then be 2, indicating no capture.) */
1941
1942 if (offset > offset_top)
1943 {
1944 register int *iptr = md->offset_vector + offset_top;
1945 register int *iend = md->offset_vector + offset;
1946 while (iptr < iend) *iptr++ = -1;
1947 }
1948
1949 /* Now make the extraction */
1950
1951 md->offset_vector[offset] =
1952 md->offset_vector[md->offset_end - number];
1953 md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1954 if (offset_top <= offset) offset_top = offset + 2;
1955 }
1956 }
1957
1958 /* For an ordinary non-repeating ket, just continue at this level. This
1959 also happens for a repeating ket if no characters were matched in the
1960 group. This is the forcible breaking of infinite loops as implemented in
1961 Perl 5.005. For a non-repeating atomic group that includes captures,
1962 establish a backup point by processing the rest of the pattern at a lower
1963 level. If this results in a NOMATCH return, pass MATCH_ONCE back to the
1964 original OP_ONCE level, thereby bypassing intermediate backup points, but
1965 resetting any captures that happened along the way. */
1966
1967 if (*ecode == OP_KET || eptr == saved_eptr)
1968 {
1969 if (*prev == OP_ONCE)
1970 {
1971 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
1972 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1973 md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */
1974 RRETURN(MATCH_ONCE);
1975 }
1976 ecode += 1 + LINK_SIZE; /* Carry on at this level */
1977 break;
1978 }
1979
1980 /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
1981 and return the MATCH_KETRPOS. This makes it possible to do the repeats one
1982 at a time from the outer level, thus saving stack. */
1983
1984 if (*ecode == OP_KETRPOS)
1985 {
1986 md->end_match_ptr = eptr;
1987 md->end_offset_top = offset_top;
1988 RRETURN(MATCH_KETRPOS);
1989 }
1990
1991 /* The normal repeating kets try the rest of the pattern or restart from
1992 the preceding bracket, in the appropriate order. In the second case, we can
1993 use tail recursion to avoid using another stack frame, unless we have an
1994 an atomic group or an unlimited repeat of a group that can match an empty
1995 string. */
1996
1997 if (*ecode == OP_KETRMIN)
1998 {
1999 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
2000 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2001 if (*prev == OP_ONCE)
2002 {
2003 RMATCH(eptr, prev, offset_top, md, eptrb, RM8);
2004 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2005 md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */
2006 RRETURN(MATCH_ONCE);
2007 }
2008 if (*prev >= OP_SBRA) /* Could match an empty string */
2009 {
2010 md->match_function_type = MATCH_CBEGROUP;
2011 RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
2012 RRETURN(rrc);
2013 }
2014 ecode = prev;
2015 goto TAIL_RECURSE;
2016 }
2017 else /* OP_KETRMAX */
2018 {
2019 if (*prev >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
2020 RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
2021 if (rrc == MATCH_ONCE && md->once_target == prev) rrc = MATCH_NOMATCH;
2022 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2023 if (*prev == OP_ONCE)
2024 {
2025 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM9);
2026 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2027 md->once_target = prev;
2028 RRETURN(MATCH_ONCE);
2029 }
2030 ecode += 1 + LINK_SIZE;
2031 goto TAIL_RECURSE;
2032 }
2033 /* Control never gets here */
2034
2035 /* Not multiline mode: start of subject assertion, unless notbol. */
2036
2037 case OP_CIRC:
2038 if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
2039
2040 /* Start of subject assertion */
2041
2042 case OP_SOD:
2043 if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);
2044 ecode++;
2045 break;
2046
2047 /* Multiline mode: start of subject unless notbol, or after any newline. */
2048
2049 case OP_CIRCM:
2050 if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
2051 if (eptr != md->start_subject &&
2052 (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
2053 RRETURN(MATCH_NOMATCH);
2054 ecode++;
2055 break;
2056
2057 /* Start of match assertion */
2058
2059 case OP_SOM:
2060 if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);
2061 ecode++;
2062 break;
2063
2064 /* Reset the start of match point */
2065
2066 case OP_SET_SOM:
2067 mstart = eptr;
2068 ecode++;
2069 break;
2070
2071 /* Multiline mode: assert before any newline, or before end of subject
2072 unless noteol is set. */
2073
2074 case OP_DOLLM:
2075 if (eptr < md->end_subject)
2076 { if (!IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); }
2077 else
2078 {
2079 if (md->noteol) RRETURN(MATCH_NOMATCH);
2080 SCHECK_PARTIAL();
2081 }
2082 ecode++;
2083 break;
2084
2085 /* Not multiline mode: assert before a terminating newline or before end of
2086 subject unless noteol is set. */
2087
2088 case OP_DOLL:
2089 if (md->noteol) RRETURN(MATCH_NOMATCH);
2090 if (!md->endonly) goto ASSERT_NL_OR_EOS;
2091
2092 /* ... else fall through for endonly */
2093
2094 /* End of subject assertion (\z) */
2095
2096 case OP_EOD:
2097 if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);
2098 SCHECK_PARTIAL();
2099 ecode++;
2100 break;
2101
2102 /* End of subject or ending \n assertion (\Z) */
2103
2104 case OP_EODN:
2105 ASSERT_NL_OR_EOS:
2106 if (eptr < md->end_subject &&
2107 (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
2108 RRETURN(MATCH_NOMATCH);
2109
2110 /* Either at end of string or \n before end. */
2111
2112 SCHECK_PARTIAL();
2113 ecode++;
2114 break;
2115
2116 /* Word boundary assertions */
2117
2118 case OP_NOT_WORD_BOUNDARY:
2119 case OP_WORD_BOUNDARY:
2120 {
2121
2122 /* Find out if the previous and current characters are "word" characters.
2123 It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to
2124 be "non-word" characters. Remember the earliest consulted character for
2125 partial matching. */
2126
2127 #ifdef SUPPORT_UTF
2128 if (utf)
2129 {
2130 /* Get status of previous character */
2131
2132 if (eptr == md->start_subject) prev_is_word = FALSE; else
2133 {
2134 PCRE_PUCHAR lastptr = eptr - 1;
2135 BACKCHAR(lastptr);
2136 if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
2137 GETCHAR(c, lastptr);
2138 #ifdef SUPPORT_UCP
2139 if (md->use_ucp)
2140 {
2141 if (c == '_') prev_is_word = TRUE; else
2142 {
2143 int cat = UCD_CATEGORY(c);
2144 prev_is_word = (cat == ucp_L || cat == ucp_N);
2145 }
2146 }
2147 else
2148 #endif
2149 prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
2150 }
2151
2152 /* Get status of next character */
2153
2154 if (eptr >= md->end_subject)
2155 {
2156 SCHECK_PARTIAL();
2157 cur_is_word = FALSE;
2158 }
2159 else
2160 {
2161 GETCHAR(c, eptr);
2162 #ifdef SUPPORT_UCP
2163 if (md->use_ucp)
2164 {
2165 if (c == '_') cur_is_word = TRUE; else
2166 {
2167 int cat = UCD_CATEGORY(c);
2168 cur_is_word = (cat == ucp_L || cat == ucp_N);
2169 }
2170 }
2171 else
2172 #endif
2173 cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
2174 }
2175 }
2176 else
2177 #endif
2178
2179 /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for
2180 consistency with the behaviour of \w we do use it in this case. */
2181
2182 {
2183 /* Get status of previous character */
2184
2185 if (eptr == md->start_subject) prev_is_word = FALSE; else
2186 {
2187 if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
2188 #ifdef SUPPORT_UCP
2189 if (md->use_ucp)
2190 {
2191 c = eptr[-1];
2192 if (c == '_') prev_is_word = TRUE; else
2193 {
2194 int cat = UCD_CATEGORY(c);
2195 prev_is_word = (cat == ucp_L || cat == ucp_N);
2196 }
2197 }
2198 else
2199 #endif
2200 prev_is_word = MAX_255(eptr[-1])
2201 && ((md->ctypes[eptr[-1]] & ctype_word) != 0);
2202 }
2203
2204 /* Get status of next character */
2205
2206 if (eptr >= md->end_subject)
2207 {
2208 SCHECK_PARTIAL();
2209 cur_is_word = FALSE;
2210 }
2211 else
2212 #ifdef SUPPORT_UCP
2213 if (md->use_ucp)
2214 {
2215 c = *eptr;
2216 if (c == '_') cur_is_word = TRUE; else
2217 {
2218 int cat = UCD_CATEGORY(c);
2219 cur_is_word = (cat == ucp_L || cat == ucp_N);
2220 }
2221 }
2222 else
2223 #endif
2224 cur_is_word = MAX_255(*eptr)
2225 && ((md->ctypes[*eptr] & ctype_word) != 0);
2226 }
2227
2228 /* Now see if the situation is what we want */
2229
2230 if ((*ecode++ == OP_WORD_BOUNDARY)?
2231 cur_is_word == prev_is_word : cur_is_word != prev_is_word)
2232 RRETURN(MATCH_NOMATCH);
2233 }
2234 break;
2235
2236 /* Match a single character type; inline for speed */
2237
2238 case OP_ANY:
2239 if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
2240 /* Fall through */
2241
2242 case OP_ALLANY:
2243 if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */
2244 { /* not be updated before SCHECK_PARTIAL. */
2245 SCHECK_PARTIAL();
2246 RRETURN(MATCH_NOMATCH);
2247 }
2248 eptr++;
2249 #ifdef SUPPORT_UTF
2250 if (utf) ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
2251 #endif
2252 ecode++;
2253 break;
2254
2255 /* Match a single byte, even in UTF-8 mode. This opcode really does match
2256 any byte, even newline, independent of the setting of PCRE_DOTALL. */
2257
2258 case OP_ANYBYTE:
2259 if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */
2260 { /* not be updated before SCHECK_PARTIAL. */
2261 SCHECK_PARTIAL();
2262 RRETURN(MATCH_NOMATCH);
2263 }
2264 eptr++;
2265 ecode++;
2266 break;
2267
2268 case OP_NOT_DIGIT:
2269 if (eptr >= md->end_subject)
2270 {
2271 SCHECK_PARTIAL();
2272 RRETURN(MATCH_NOMATCH);
2273 }
2274 GETCHARINCTEST(c, eptr);
2275 if (
2276 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2277 c < 256 &&
2278 #endif
2279 (md->ctypes[c] & ctype_digit) != 0
2280 )
2281 RRETURN(MATCH_NOMATCH);
2282 ecode++;
2283 break;
2284
2285 case OP_DIGIT:
2286 if (eptr >= md->end_subject)
2287 {
2288 SCHECK_PARTIAL();
2289 RRETURN(MATCH_NOMATCH);
2290 }
2291 GETCHARINCTEST(c, eptr);
2292 if (
2293 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2294 c > 255 ||
2295 #endif
2296 (md->ctypes[c] & ctype_digit) == 0
2297 )
2298 RRETURN(MATCH_NOMATCH);
2299 ecode++;
2300 break;
2301
2302 case OP_NOT_WHITESPACE:
2303 if (eptr >= md->end_subject)
2304 {
2305 SCHECK_PARTIAL();
2306 RRETURN(MATCH_NOMATCH);
2307 }
2308 GETCHARINCTEST(c, eptr);
2309 if (
2310 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2311 c < 256 &&
2312 #endif
2313 (md->ctypes[c] & ctype_space) != 0
2314 )
2315 RRETURN(MATCH_NOMATCH);
2316 ecode++;
2317 break;
2318
2319 case OP_WHITESPACE:
2320 if (eptr >= md->end_subject)
2321 {
2322 SCHECK_PARTIAL();
2323 RRETURN(MATCH_NOMATCH);
2324 }
2325 GETCHARINCTEST(c, eptr);
2326 if (
2327 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2328 c > 255 ||
2329 #endif
2330 (md->ctypes[c] & ctype_space) == 0
2331 )
2332 RRETURN(MATCH_NOMATCH);
2333 ecode++;
2334 break;
2335
2336 case OP_NOT_WORDCHAR:
2337 if (eptr >= md->end_subject)
2338 {
2339 SCHECK_PARTIAL();
2340 RRETURN(MATCH_NOMATCH);
2341 }
2342 GETCHARINCTEST(c, eptr);
2343 if (
2344 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2345 c < 256 &&
2346 #endif
2347 (md->ctypes[c] & ctype_word) != 0
2348 )
2349 RRETURN(MATCH_NOMATCH);
2350 ecode++;
2351 break;
2352
2353 case OP_WORDCHAR:
2354 if (eptr >= md->end_subject)
2355 {
2356 SCHECK_PARTIAL();
2357 RRETURN(MATCH_NOMATCH);
2358 }
2359 GETCHARINCTEST(c, eptr);
2360 if (
2361 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2362 c > 255 ||
2363 #endif
2364 (md->ctypes[c] & ctype_word) == 0
2365 )
2366 RRETURN(MATCH_NOMATCH);
2367 ecode++;
2368 break;
2369
2370 case OP_ANYNL:
2371 if (eptr >= md->end_subject)
2372 {
2373 SCHECK_PARTIAL();
2374 RRETURN(MATCH_NOMATCH);
2375 }
2376 GETCHARINCTEST(c, eptr);
2377 switch(c)
2378 {
2379 default: RRETURN(MATCH_NOMATCH);
2380
2381 case 0x000d:
2382 if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
2383 break;
2384
2385 case 0x000a:
2386 break;
2387
2388 case 0x000b:
2389 case 0x000c:
2390 case 0x0085:
2391 case 0x2028:
2392 case 0x2029:
2393 if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
2394 break;
2395 }
2396 ecode++;
2397 break;
2398
2399 case OP_NOT_HSPACE:
2400 if (eptr >= md->end_subject)
2401 {
2402 SCHECK_PARTIAL();
2403 RRETURN(MATCH_NOMATCH);
2404 }
2405 GETCHARINCTEST(c, eptr);
2406 switch(c)
2407 {
2408 default: break;
2409 case 0x09: /* HT */
2410 case 0x20: /* SPACE */
2411 case 0xa0: /* NBSP */
2412 case 0x1680: /* OGHAM SPACE MARK */
2413 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
2414 case 0x2000: /* EN QUAD */
2415 case 0x2001: /* EM QUAD */
2416 case 0x2002: /* EN SPACE */
2417 case 0x2003: /* EM SPACE */
2418 case 0x2004: /* THREE-PER-EM SPACE */
2419 case 0x2005: /* FOUR-PER-EM SPACE */
2420 case 0x2006: /* SIX-PER-EM SPACE */
2421 case 0x2007: /* FIGURE SPACE */
2422 case 0x2008: /* PUNCTUATION SPACE */
2423 case 0x2009: /* THIN SPACE */
2424 case 0x200A: /* HAIR SPACE */
2425 case 0x202f: /* NARROW NO-BREAK SPACE */
2426 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
2427 case 0x3000: /* IDEOGRAPHIC SPACE */
2428 RRETURN(MATCH_NOMATCH);
2429 }
2430 ecode++;
2431 break;
2432
2433 case OP_HSPACE:
2434 if (eptr >= md->end_subject)
2435 {
2436 SCHECK_PARTIAL();
2437 RRETURN(MATCH_NOMATCH);
2438 }
2439 GETCHARINCTEST(c, eptr);
2440 switch(c)
2441 {
2442 default: RRETURN(MATCH_NOMATCH);
2443 case 0x09: /* HT */
2444 case 0x20: /* SPACE */
2445 case 0xa0: /* NBSP */
2446 case 0x1680: /* OGHAM SPACE MARK */
2447 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
2448 case 0x2000: /* EN QUAD */
2449 case 0x2001: /* EM QUAD */
2450 case 0x2002: /* EN SPACE */
2451 case 0x2003: /* EM SPACE */
2452 case 0x2004: /* THREE-PER-EM SPACE */
2453 case 0x2005: /* FOUR-PER-EM SPACE */
2454 case 0x2006: /* SIX-PER-EM SPACE */
2455 case 0x2007: /* FIGURE SPACE */
2456 case 0x2008: /* PUNCTUATION SPACE */
2457 case 0x2009: /* THIN SPACE */
2458 case 0x200A: /* HAIR SPACE */
2459 case 0x202f: /* NARROW NO-BREAK SPACE */
2460 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
2461 case 0x3000: /* IDEOGRAPHIC SPACE */
2462 break;
2463 }
2464 ecode++;
2465 break;
2466
2467 case OP_NOT_VSPACE:
2468 if (eptr >= md->end_subject)
2469 {
2470 SCHECK_PARTIAL();
2471 RRETURN(MATCH_NOMATCH);
2472 }
2473 GETCHARINCTEST(c, eptr);
2474 switch(c)
2475 {
2476 default: break;
2477 case 0x0a: /* LF */
2478 case 0x0b: /* VT */
2479 case 0x0c: /* FF */
2480 case 0x0d: /* CR */
2481 case 0x85: /* NEL */
2482 case 0x2028: /* LINE SEPARATOR */
2483 case 0x2029: /* PARAGRAPH SEPARATOR */
2484 RRETURN(MATCH_NOMATCH);
2485 }
2486 ecode++;
2487 break;
2488
2489 case OP_VSPACE:
2490 if (eptr >= md->end_subject)
2491 {
2492 SCHECK_PARTIAL();
2493 RRETURN(MATCH_NOMATCH);
2494 }
2495 GETCHARINCTEST(c, eptr);
2496 switch(c)
2497 {
2498 default: RRETURN(MATCH_NOMATCH);
2499 case 0x0a: /* LF */
2500 case 0x0b: /* VT */
2501 case 0x0c: /* FF */
2502 case 0x0d: /* CR */
2503 case 0x85: /* NEL */
2504 case 0x2028: /* LINE SEPARATOR */
2505 case 0x2029: /* PARAGRAPH SEPARATOR */
2506 break;
2507 }
2508 ecode++;
2509 break;
2510
2511 #ifdef SUPPORT_UCP
2512 /* Check the next character by Unicode property. We will get here only
2513 if the support is in the binary; otherwise a compile-time error occurs. */
2514
2515 case OP_PROP:
2516 case OP_NOTPROP:
2517 if (eptr >= md->end_subject)
2518 {
2519 SCHECK_PARTIAL();
2520 RRETURN(MATCH_NOMATCH);
2521 }
2522 GETCHARINCTEST(c, eptr);
2523 {
2524 const ucd_record *prop = GET_UCD(c);
2525
2526 switch(ecode[1])
2527 {
2528 case PT_ANY:
2529 if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
2530 break;
2531
2532 case PT_LAMP:
2533 if ((prop->chartype == ucp_Lu ||
2534 prop->chartype == ucp_Ll ||
2535 prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
2536 RRETURN(MATCH_NOMATCH);
2537 break;
2538
2539 case PT_GC:
2540 if ((ecode[2] != PRIV(ucp_gentype)[prop->chartype]) == (op == OP_PROP))
2541 RRETURN(MATCH_NOMATCH);
2542 break;
2543
2544 case PT_PC:
2545 if ((ecode[2] != prop->chartype) == (op == OP_PROP))
2546 RRETURN(MATCH_NOMATCH);
2547 break;
2548
2549 case PT_SC:
2550 if ((ecode[2] != prop->script) == (op == OP_PROP))
2551 RRETURN(MATCH_NOMATCH);
2552 break;
2553
2554 /* These are specials */
2555
2556 case PT_ALNUM:
2557 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2558 PRIV(ucp_gentype)[prop->chartype] == ucp_N) == (op == OP_NOTPROP))
2559 RRETURN(MATCH_NOMATCH);
2560 break;
2561
2562 case PT_SPACE: /* Perl space */
2563 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2564 c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2565 == (op == OP_NOTPROP))
2566 RRETURN(MATCH_NOMATCH);
2567 break;
2568
2569 case PT_PXSPACE: /* POSIX space */
2570 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2571 c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2572 c == CHAR_FF || c == CHAR_CR)
2573 == (op == OP_NOTPROP))
2574 RRETURN(MATCH_NOMATCH);
2575 break;
2576
2577 case PT_WORD:
2578 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2579 PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2580 c == CHAR_UNDERSCORE) == (op == OP_NOTPROP))
2581 RRETURN(MATCH_NOMATCH);
2582 break;
2583
2584 /* This should never occur */
2585
2586 default:
2587 RRETURN(PCRE_ERROR_INTERNAL);
2588 }
2589
2590 ecode += 3;
2591 }
2592 break;
2593
2594 /* Match an extended Unicode sequence. We will get here only if the support
2595 is in the binary; otherwise a compile-time error occurs. */
2596
2597 case OP_EXTUNI:
2598 if (eptr >= md->end_subject)
2599 {
2600 SCHECK_PARTIAL();
2601 RRETURN(MATCH_NOMATCH);
2602 }
2603 GETCHARINCTEST(c, eptr);
2604 if (UCD_CATEGORY(c) == ucp_M) RRETURN(MATCH_NOMATCH);
2605 while (eptr < md->end_subject)
2606 {
2607 int len = 1;
2608 if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
2609 if (UCD_CATEGORY(c) != ucp_M) break;
2610 eptr += len;
2611 }
2612 if (md->partial != 0 && eptr >= md->end_subject)
2613 {
2614 SCHECK_PARTIAL();
2615 }
2616 ecode++;
2617 break;
2618 #endif
2619
2620
2621 /* Match a back reference, possibly repeatedly. Look past the end of the
2622 item to see if there is repeat information following. The code is similar
2623 to that for character classes, but repeated for efficiency. Then obey
2624 similar code to character type repeats - written out again for speed.
2625 However, if the referenced string is the empty string, always treat
2626 it as matched, any number of times (otherwise there could be infinite
2627 loops). */
2628
2629 case OP_REF:
2630 case OP_REFI:
2631 caseless = op == OP_REFI;
2632 offset = GET2(ecode, 1) << 1; /* Doubled ref number */
2633 ecode += 1 + IMM2_SIZE;
2634
2635 /* If the reference is unset, there are two possibilities:
2636
2637 (a) In the default, Perl-compatible state, set the length negative;
2638 this ensures that every attempt at a match fails. We can't just fail
2639 here, because of the possibility of quantifiers with zero minima.
2640
2641 (b) If the JavaScript compatibility flag is set, set the length to zero
2642 so that the back reference matches an empty string.
2643
2644 Otherwise, set the length to the length of what was matched by the
2645 referenced subpattern. */
2646
2647 if (offset >= offset_top || md->offset_vector[offset] < 0)
2648 length = (md->jscript_compat)? 0 : -1;
2649 else
2650 length = md->offset_vector[offset+1] - md->offset_vector[offset];
2651
2652 /* Set up for repetition, or handle the non-repeated case */
2653
2654 switch (*ecode)
2655 {
2656 case OP_CRSTAR:
2657 case OP_CRMINSTAR:
2658 case OP_CRPLUS:
2659 case OP_CRMINPLUS:
2660 case OP_CRQUERY:
2661 case OP_CRMINQUERY:
2662 c = *ecode++ - OP_CRSTAR;
2663 minimize = (c & 1) != 0;
2664 min = rep_min[c]; /* Pick up values from tables; */
2665 max = rep_max[c]; /* zero for max => infinity */
2666 if (max == 0) max = INT_MAX;
2667 break;
2668
2669 case OP_CRRANGE:
2670 case OP_CRMINRANGE:
2671 minimize = (*ecode == OP_CRMINRANGE);
2672 min = GET2(ecode, 1);
2673 max = GET2(ecode, 1 + IMM2_SIZE);
2674 if (max == 0) max = INT_MAX;
2675 ecode += 1 + 2 * IMM2_SIZE;
2676 break;
2677
2678 default: /* No repeat follows */
2679 if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
2680 {
2681 eptr += -(length + 1);
2682 CHECK_PARTIAL();
2683 RRETURN(MATCH_NOMATCH);
2684 }
2685 eptr += length;
2686 continue; /* With the main loop */
2687 }
2688
2689 /* Handle repeated back references. If the length of the reference is
2690 zero, just continue with the main loop. If the length is negative, it
2691 means the reference is unset in non-Java-compatible mode. If the minimum is
2692 zero, we can continue at the same level without recursion. For any other
2693 minimum, carrying on will result in NOMATCH. */
2694
2695 if (length == 0) continue;
2696 if (length < 0 && min == 0) continue;
2697
2698 /* First, ensure the minimum number of matches are present. We get back
2699 the length of the reference string explicitly rather than passing the
2700 address of eptr, so that eptr can be a register variable. */
2701
2702 for (i = 1; i <= min; i++)
2703 {
2704 int slength;
2705 if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2706 {
2707 eptr += -(slength + 1);
2708 CHECK_PARTIAL();
2709 RRETURN(MATCH_NOMATCH);
2710 }
2711 eptr += slength;
2712 }
2713
2714 /* If min = max, continue at the same level without recursion.
2715 They are not both allowed to be zero. */
2716
2717 if (min == max) continue;
2718
2719 /* If minimizing, keep trying and advancing the pointer */
2720
2721 if (minimize)
2722 {
2723 for (fi = min;; fi++)
2724 {
2725 int slength;
2726 RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
2727 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2728 if (fi >= max) RRETURN(MATCH_NOMATCH);
2729 if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2730 {
2731 eptr += -(slength + 1);
2732 CHECK_PARTIAL();
2733 RRETURN(MATCH_NOMATCH);
2734 }
2735 eptr += slength;
2736 }
2737 /* Control never gets here */
2738 }
2739
2740 /* If maximizing, find the longest string and work backwards */
2741
2742 else
2743 {
2744 pp = eptr;
2745 for (i = min; i < max; i++)
2746 {
2747 int slength;
2748 if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2749 {
2750 /* Restore the eptr after the check. */
2751 eptr += -(slength + 1);
2752 CHECK_PARTIAL();
2753 eptr -= -(slength + 1);
2754 break;
2755 }
2756 eptr += slength;
2757 }
2758 while (eptr >= pp)
2759 {
2760 RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
2761 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2762 eptr -= length;
2763 }
2764 RRETURN(MATCH_NOMATCH);
2765 }
2766 /* Control never gets here */
2767
2768 /* Match a bit-mapped character class, possibly repeatedly. This op code is
2769 used when all the characters in the class have values in the range 0-255,
2770 and either the matching is caseful, or the characters are in the range
2771 0-127 when UTF-8 processing is enabled. The only difference between
2772 OP_CLASS and OP_NCLASS occurs when a data character outside the range is
2773 encountered.
2774
2775 First, look past the end of the item to see if there is repeat information
2776 following. Then obey similar code to character type repeats - written out
2777 again for speed. */
2778
2779 case OP_NCLASS:
2780 case OP_CLASS:
2781 {
2782 /* The data variable is saved across frames, so the byte map needs to
2783 be stored there. */
2784 #define BYTE_MAP ((pcre_uint8 *)data)
2785 data = ecode + 1; /* Save for matching */
2786 ecode += 1 + (32 / sizeof(pcre_uchar)); /* Advance past the item */
2787
2788 switch (*ecode)
2789 {
2790 case OP_CRSTAR:
2791 case OP_CRMINSTAR:
2792 case OP_CRPLUS:
2793 case OP_CRMINPLUS:
2794 case OP_CRQUERY:
2795 case OP_CRMINQUERY:
2796 c = *ecode++ - OP_CRSTAR;
2797 minimize = (c & 1) != 0;
2798 min = rep_min[c]; /* Pick up values from tables; */
2799 max = rep_max[c]; /* zero for max => infinity */
2800 if (max == 0) max = INT_MAX;
2801 break;
2802
2803 case OP_CRRANGE:
2804 case OP_CRMINRANGE:
2805 minimize = (*ecode == OP_CRMINRANGE);
2806 min = GET2(ecode, 1);
2807 max = GET2(ecode, 1 + IMM2_SIZE);
2808 if (max == 0) max = INT_MAX;
2809 ecode += 1 + 2 * IMM2_SIZE;
2810 break;
2811
2812 default: /* No repeat follows */
2813 min = max = 1;
2814 break;
2815 }
2816
2817 /* First, ensure the minimum number of matches are present. */
2818
2819 #ifdef SUPPORT_UTF
2820 if (utf)
2821 {
2822 for (i = 1; i <= min; i++)
2823 {
2824 if (eptr >= md->end_subject)
2825 {
2826 SCHECK_PARTIAL();
2827 RRETURN(MATCH_NOMATCH);
2828 }
2829 GETCHARINC(c, eptr);
2830 if (c > 255)
2831 {
2832 if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2833 }
2834 else
2835 if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2836 }
2837 }
2838 else
2839 #endif
2840 /* Not UTF mode */
2841 {
2842 for (i = 1; i <= min; i++)
2843 {
2844 if (eptr >= md->end_subject)
2845 {
2846 SCHECK_PARTIAL();
2847 RRETURN(MATCH_NOMATCH);
2848 }
2849 c = *eptr++;
2850 #ifndef COMPILE_PCRE8
2851 if (c > 255)
2852 {
2853 if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2854 }
2855 else
2856 #endif
2857 if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2858 }
2859 }
2860
2861 /* If max == min we can continue with the main loop without the
2862 need to recurse. */
2863
2864 if (min == max) continue;
2865
2866 /* If minimizing, keep testing the rest of the expression and advancing
2867 the pointer while it matches the class. */
2868
2869 if (minimize)
2870 {
2871 #ifdef SUPPORT_UTF
2872 if (utf)
2873 {
2874 for (fi = min;; fi++)
2875 {
2876 RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
2877 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2878 if (fi >= max) RRETURN(MATCH_NOMATCH);
2879 if (eptr >= md->end_subject)
2880 {
2881 SCHECK_PARTIAL();
2882 RRETURN(MATCH_NOMATCH);
2883 }
2884 GETCHARINC(c, eptr);
2885 if (c > 255)
2886 {
2887 if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2888 }
2889 else
2890 if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2891 }
2892 }
2893 else
2894 #endif
2895 /* Not UTF mode */
2896 {
2897 for (fi = min;; fi++)
2898 {
2899 RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
2900 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2901 if (fi >= max) RRETURN(MATCH_NOMATCH);
2902 if (eptr >= md->end_subject)
2903 {
2904 SCHECK_PARTIAL();
2905 RRETURN(MATCH_NOMATCH);
2906 }
2907 c = *eptr++;
2908 #ifndef COMPILE_PCRE8
2909 if (c > 255)
2910 {
2911 if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2912 }
2913 else
2914 #endif
2915 if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2916 }
2917 }
2918 /* Control never gets here */
2919 }
2920
2921 /* If maximizing, find the longest possible run, then work backwards. */
2922
2923 else
2924 {
2925 pp = eptr;
2926
2927 #ifdef SUPPORT_UTF
2928 if (utf)
2929 {
2930 for (i = min; i < max; i++)
2931 {
2932 int len = 1;
2933 if (eptr >= md->end_subject)
2934 {
2935 SCHECK_PARTIAL();
2936 break;
2937 }
2938 GETCHARLEN(c, eptr, len);
2939 if (c > 255)
2940 {
2941 if (op == OP_CLASS) break;
2942 }
2943 else
2944 if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break;
2945 eptr += len;
2946 }
2947 for (;;)
2948 {
2949 RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
2950 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2951 if (eptr-- == pp) break; /* Stop if tried at original pos */
2952 BACKCHAR(eptr);
2953 }
2954 }
2955 else
2956 #endif
2957 /* Not UTF mode */
2958 {
2959 for (i = min; i < max; i++)
2960 {
2961 if (eptr >= md->end_subject)
2962 {
2963 SCHECK_PARTIAL();
2964 break;
2965 }
2966 c = *eptr;
2967 #ifndef COMPILE_PCRE8
2968 if (c > 255)
2969 {
2970 if (op == OP_CLASS) break;
2971 }
2972 else
2973 #endif
2974 if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break;
2975 eptr++;
2976 }
2977 while (eptr >= pp)
2978 {
2979 RMATCH(eptr, ecode, offset_top, md, eptrb, RM19);
2980 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2981 eptr--;
2982 }
2983 }
2984
2985 RRETURN(MATCH_NOMATCH);
2986 }
2987 #undef BYTE_MAP
2988 }
2989 /* Control never gets here */
2990
2991
2992 /* Match an extended character class. This opcode is encountered only
2993 when UTF-8 mode mode is supported. Nevertheless, we may not be in UTF-8
2994 mode, because Unicode properties are supported in non-UTF-8 mode. */
2995
2996 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2997 case OP_XCLASS:
2998 {
2999 data = ecode + 1 + LINK_SIZE; /* Save for matching */
3000 ecode += GET(ecode, 1); /* Advance past the item */
3001
3002 switch (*ecode)
3003 {
3004 case OP_CRSTAR:
3005 case OP_CRMINSTAR:
3006 case OP_CRPLUS:
3007 case OP_CRMINPLUS:
3008 case OP_CRQUERY:
3009 case OP_CRMINQUERY:
3010 c = *ecode++ - OP_CRSTAR;
3011 minimize = (c & 1) != 0;
3012 min = rep_min[c]; /* Pick up values from tables; */
3013 max = rep_max[c]; /* zero for max => infinity */
3014 if (max == 0) max = INT_MAX;
3015 break;
3016
3017 case OP_CRRANGE:
3018 case OP_CRMINRANGE:
3019 minimize = (*ecode == OP_CRMINRANGE);
3020 min = GET2(ecode, 1);
3021 max = GET2(ecode, 1 + IMM2_SIZE);
3022 if (max == 0) max = INT_MAX;
3023 ecode += 1 + 2 * IMM2_SIZE;
3024 break;
3025
3026 default: /* No repeat follows */
3027 min = max = 1;
3028 break;
3029 }
3030
3031 /* First, ensure the minimum number of matches are present. */
3032
3033 for (i = 1; i <= min; i++)
3034 {
3035 if (eptr >= md->end_subject)
3036 {
3037 SCHECK_PARTIAL();
3038 RRETURN(MATCH_NOMATCH);
3039 }
3040 GETCHARINCTEST(c, eptr);
3041 if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH);
3042 }
3043
3044 /* If max == min we can continue with the main loop without the
3045 need to recurse. */
3046
3047 if (min == max) continue;
3048
3049 /* If minimizing, keep testing the rest of the expression and advancing
3050 the pointer while it matches the class. */
3051
3052 if (minimize)
3053 {
3054 for (fi = min;; fi++)
3055 {
3056 RMATCH(eptr, ecode, offset_top, md, eptrb, RM20);
3057 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3058 if (fi >= max) RRETURN(MATCH_NOMATCH);
3059 if (eptr >= md->end_subject)
3060 {
3061 SCHECK_PARTIAL();
3062 RRETURN(MATCH_NOMATCH);
3063 }
3064 GETCHARINCTEST(c, eptr);
3065 if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH);
3066 }
3067 /* Control never gets here */
3068 }
3069
3070 /* If maximizing, find the longest possible run, then work backwards. */
3071
3072 else
3073 {
3074 pp = eptr;
3075 for (i = min; i < max; i++)
3076 {
3077 int len = 1;
3078 if (eptr >= md->end_subject)
3079 {
3080 SCHECK_PARTIAL();
3081 break;
3082 }
3083 #ifdef SUPPORT_UTF
3084 GETCHARLENTEST(c, eptr, len);
3085 #else
3086 c = *eptr;
3087 #endif
3088 if (!PRIV(xclass)(c, data, utf)) break;
3089 eptr += len;
3090 }
3091 for(;;)
3092 {
3093 RMATCH(eptr, ecode, offset_top, md, eptrb, RM21);
3094 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3095 if (eptr-- == pp) break; /* Stop if tried at original pos */
3096 #ifdef SUPPORT_UTF
3097 if (utf) BACKCHAR(eptr);
3098 #endif
3099 }
3100 RRETURN(MATCH_NOMATCH);
3101 }
3102
3103 /* Control never gets here */
3104 }
3105 #endif /* End of XCLASS */
3106
3107 /* Match a single character, casefully */
3108
3109 case OP_CHAR:
3110 #ifdef SUPPORT_UTF
3111 if (utf)
3112 {
3113 length = 1;
3114 ecode++;
3115 GETCHARLEN(fc, ecode, length);
3116 if (length > md->end_subject - eptr)
3117 {
3118 CHECK_PARTIAL(); /* Not SCHECK_PARTIAL() */
3119 RRETURN(MATCH_NOMATCH);
3120 }
3121 while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);
3122 }
3123 else
3124 #endif
3125 /* Not UTF mode */
3126 {
3127 if (md->end_subject - eptr < 1)
3128 {
3129 SCHECK_PARTIAL(); /* This one can use SCHECK_PARTIAL() */
3130 RRETURN(MATCH_NOMATCH);
3131 }
3132 if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);
3133 ecode += 2;
3134 }
3135 break;
3136
3137 /* Match a single character, caselessly. If we are at the end of the
3138 subject, give up immediately. */
3139
3140 case OP_CHARI:
3141 if (eptr >= md->end_subject)
3142 {
3143 SCHECK_PARTIAL();
3144 RRETURN(MATCH_NOMATCH);
3145 }
3146
3147 #ifdef SUPPORT_UTF
3148 if (utf)
3149 {
3150 length = 1;
3151 ecode++;
3152 GETCHARLEN(fc, ecode, length);
3153
3154 /* If the pattern character's value is < 128, we have only one byte, and
3155 we know that its other case must also be one byte long, so we can use the
3156 fast lookup table. We know that there is at least one byte left in the
3157 subject. */
3158
3159 if (fc < 128)
3160 {
3161 if (md->lcc[fc]
3162 != TABLE_GET(*eptr, md->lcc, *eptr)) RRETURN(MATCH_NOMATCH);
3163 ecode++;
3164 eptr++;
3165 }
3166
3167 /* Otherwise we must pick up the subject character. Note that we cannot
3168 use the value of "length" to check for sufficient bytes left, because the
3169 other case of the character may have more or fewer bytes. */
3170
3171 else
3172 {
3173 unsigned int dc;
3174 GETCHARINC(dc, eptr);
3175 ecode += length;
3176
3177 /* If we have Unicode property support, we can use it to test the other
3178 case of the character, if there is one. */
3179
3180 if (fc != dc)
3181 {
3182 #ifdef SUPPORT_UCP
3183 if (dc != UCD_OTHERCASE(fc))
3184 #endif
3185 RRETURN(MATCH_NOMATCH);
3186 }
3187 }
3188 }
3189 else
3190 #endif /* SUPPORT_UTF */
3191
3192 /* Not UTF mode */
3193 {
3194 if (TABLE_GET(ecode[1], md->lcc, ecode[1])
3195 != TABLE_GET(*eptr, md->lcc, *eptr)) RRETURN(MATCH_NOMATCH);
3196 eptr++;
3197 ecode += 2;
3198 }
3199 break;
3200
3201 /* Match a single character repeatedly. */
3202
3203 case OP_EXACT:
3204 case OP_EXACTI:
3205 min = max = GET2(ecode, 1);
3206 ecode += 1 + IMM2_SIZE;
3207 goto REPEATCHAR;
3208
3209 case OP_POSUPTO:
3210 case OP_POSUPTOI:
3211 possessive = TRUE;
3212 /* Fall through */
3213
3214 case OP_UPTO:
3215 case OP_UPTOI:
3216 case OP_MINUPTO:
3217 case OP_MINUPTOI:
3218 min = 0;
3219 max = GET2(ecode, 1);
3220 minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI;
3221 ecode += 1 + IMM2_SIZE;
3222 goto REPEATCHAR;
3223
3224 case OP_POSSTAR:
3225 case OP_POSSTARI:
3226 possessive = TRUE;
3227 min = 0;
3228 max = INT_MAX;
3229 ecode++;
3230 goto REPEATCHAR;
3231
3232 case OP_POSPLUS:
3233 case OP_POSPLUSI:
3234 possessive = TRUE;
3235 min = 1;
3236 max = INT_MAX;
3237 ecode++;
3238 goto REPEATCHAR;
3239
3240 case OP_POSQUERY:
3241 case OP_POSQUERYI:
3242 possessive = TRUE;
3243 min = 0;
3244 max = 1;
3245 ecode++;
3246 goto REPEATCHAR;
3247
3248 case OP_STAR:
3249 case OP_STARI:
3250 case OP_MINSTAR:
3251 case OP_MINSTARI:
3252 case OP_PLUS:
3253 case OP_PLUSI:
3254 case OP_MINPLUS:
3255 case OP_MINPLUSI:
3256 case OP_QUERY:
3257 case OP_QUERYI:
3258 case OP_MINQUERY:
3259 case OP_MINQUERYI:
3260 c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI);
3261 minimize = (c & 1) != 0;
3262 min = rep_min[c]; /* Pick up values from tables; */
3263 max = rep_max[c]; /* zero for max => infinity */
3264 if (max == 0) max = INT_MAX;
3265
3266 /* Common code for all repeated single-character matches. */
3267
3268 REPEATCHAR:
3269 #ifdef SUPPORT_UTF
3270 if (utf)
3271 {
3272 length = 1;
3273 charptr = ecode;
3274 GETCHARLEN(fc, ecode, length);
3275 ecode += length;
3276
3277 /* Handle multibyte character matching specially here. There is
3278 support for caseless matching if UCP support is present. */
3279
3280 if (length > 1)
3281 {
3282 #ifdef SUPPORT_UCP
3283 unsigned int othercase;
3284 if (op >= OP_STARI && /* Caseless */
3285 (othercase = UCD_OTHERCASE(fc)) != fc)
3286 oclength = PRIV(ord2utf)(othercase, occhars);
3287 else oclength = 0;
3288 #endif /* SUPPORT_UCP */
3289
3290 for (i = 1; i <= min; i++)
3291 {
3292 if (eptr <= md->end_subject - length &&
3293 memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length;
3294 #ifdef SUPPORT_UCP
3295 else if (oclength > 0 &&
3296 eptr <= md->end_subject - oclength &&
3297 memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength;
3298 #endif /* SUPPORT_UCP */
3299 else
3300 {
3301 CHECK_PARTIAL();
3302 RRETURN(MATCH_NOMATCH);
3303 }
3304 }
3305
3306 if (min == max) continue;
3307
3308 if (minimize)
3309 {
3310 for (fi = min;; fi++)
3311 {
3312 RMATCH(eptr, ecode, offset_top, md, eptrb, RM22);
3313 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3314 if (fi >= max) RRETURN(MATCH_NOMATCH);
3315 if (eptr <= md->end_subject - length &&
3316 memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length;
3317 #ifdef SUPPORT_UCP
3318 else if (oclength > 0 &&
3319 eptr <= md->end_subject - oclength &&
3320 memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength;
3321 #endif /* SUPPORT_UCP */
3322 else
3323 {
3324 CHECK_PARTIAL();
3325 RRETURN(MATCH_NOMATCH);
3326 }
3327 }
3328 /* Control never gets here */
3329 }
3330
3331 else /* Maximize */
3332 {
3333 pp = eptr;
3334 for (i = min; i < max; i++)
3335 {
3336 if (eptr <= md->end_subject - length &&
3337 memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length;
3338 #ifdef SUPPORT_UCP
3339 else if (oclength > 0 &&
3340 eptr <= md->end_subject - oclength &&
3341 memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength;
3342 #endif /* SUPPORT_UCP */
3343 else
3344 {
3345 CHECK_PARTIAL();
3346 break;
3347 }
3348 }
3349
3350 if (possessive) continue;
3351
3352 for(;;)
3353 {
3354 RMATCH(eptr, ecode, offset_top, md, eptrb, RM23);
3355 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3356 if (eptr == pp) { RRETURN(MATCH_NOMATCH); }
3357 #ifdef SUPPORT_UCP
3358 eptr--;
3359 BACKCHAR(eptr);
3360 #else /* without SUPPORT_UCP */
3361 eptr -= length;
3362 #endif /* SUPPORT_UCP */
3363 }
3364 }
3365 /* Control never gets here */
3366 }
3367
3368 /* If the length of a UTF-8 character is 1, we fall through here, and
3369 obey the code as for non-UTF-8 characters below, though in this case the
3370 value of fc will always be < 128. */
3371 }
3372 else
3373 #endif /* SUPPORT_UTF */
3374 /* When not in UTF-8 mode, load a single-byte character. */
3375 fc = *ecode++;
3376
3377 /* The value of fc at this point is always one character, though we may
3378 or may not be in UTF mode. The code is duplicated for the caseless and
3379 caseful cases, for speed, since matching characters is likely to be quite
3380 common. First, ensure the minimum number of matches are present. If min =
3381 max, continue at the same level without recursing. Otherwise, if
3382 minimizing, keep trying the rest of the expression and advancing one
3383 matching character if failing, up to the maximum. Alternatively, if
3384 maximizing, find the maximum number of characters and work backwards. */
3385
3386 DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3387 max, eptr));
3388
3389 if (op >= OP_STARI) /* Caseless */
3390 {
3391 #ifdef COMPILE_PCRE8
3392 /* fc must be < 128 if UTF is enabled. */
3393 foc = md->fcc[fc];
3394 #else
3395 #ifdef SUPPORT_UTF
3396 #ifdef SUPPORT_UCP
3397 if (utf && fc > 127)
3398 foc = UCD_OTHERCASE(fc);
3399 #else
3400 if (utf && fc > 127)
3401 foc = fc;
3402 #endif /* SUPPORT_UCP */
3403 else
3404 #endif /* SUPPORT_UTF */
3405 foc = TABLE_GET(fc, md->fcc, fc);
3406 #endif /* COMPILE_PCRE8 */
3407
3408 for (i = 1; i <= min; i++)
3409 {
3410 if (eptr >= md->end_subject)
3411 {
3412 SCHECK_PARTIAL();
3413 RRETURN(MATCH_NOMATCH);
3414 }
3415 if (fc != *eptr && foc != *eptr) RRETURN(MATCH_NOMATCH);
3416 eptr++;
3417 }
3418 if (min == max) continue;
3419 if (minimize)
3420 {
3421 for (fi = min;; fi++)
3422 {
3423 RMATCH(eptr, ecode, offset_top, md, eptrb, RM24);
3424 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3425 if (fi >= max) RRETURN(MATCH_NOMATCH);
3426 if (eptr >= md->end_subject)
3427 {
3428 SCHECK_PARTIAL();
3429 RRETURN(MATCH_NOMATCH);
3430 }
3431 if (fc != *eptr && foc != *eptr) RRETURN(MATCH_NOMATCH);
3432 eptr++;
3433 }
3434 /* Control never gets here */
3435 }
3436 else /* Maximize */
3437 {
3438 pp = eptr;
3439 for (i = min; i < max; i++)
3440 {
3441 if (eptr >= md->end_subject)
3442 {
3443 SCHECK_PARTIAL();
3444 break;
3445 }
3446 if (fc != *eptr && foc != *eptr) break;
3447 eptr++;
3448 }
3449
3450 if (possessive) continue;
3451
3452 while (eptr >= pp)
3453 {
3454 RMATCH(eptr, ecode, offset_top, md, eptrb, RM25);
3455 eptr--;
3456 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3457 }
3458 RRETURN(MATCH_NOMATCH);
3459 }
3460 /* Control never gets here */
3461 }
3462
3463 /* Caseful comparisons (includes all multi-byte characters) */
3464
3465 else
3466 {
3467 for (i = 1; i <= min; i++)
3468 {
3469 if (eptr >= md->end_subject)
3470 {
3471 SCHECK_PARTIAL();
3472 RRETURN(MATCH_NOMATCH);
3473 }
3474 if (fc != *eptr++) RRETURN(MATCH_NOMATCH);
3475 }
3476
3477 if (min == max) continue;
3478
3479 if (minimize)
3480 {
3481 for (fi = min;; fi++)
3482 {
3483 RMATCH(eptr, ecode, offset_top, md, eptrb, RM26);
3484 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3485 if (fi >= max) RRETURN(MATCH_NOMATCH);
3486 if (eptr >= md->end_subject)
3487 {
3488 SCHECK_PARTIAL();
3489 RRETURN(MATCH_NOMATCH);
3490 }
3491 if (fc != *eptr++) RRETURN(MATCH_NOMATCH);
3492 }
3493 /* Control never gets here */
3494 }
3495 else /* Maximize */
3496 {
3497 pp = eptr;
3498 for (i = min; i < max; i++)
3499 {
3500 if (eptr >= md->end_subject)
3501 {
3502 SCHECK_PARTIAL();
3503 break;
3504 }
3505 if (fc != *eptr) break;
3506 eptr++;
3507 }
3508 if (possessive) continue;
3509
3510 while (eptr >= pp)
3511 {
3512 RMATCH(eptr, ecode, offset_top, md, eptrb, RM27);
3513 eptr--;
3514 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3515 }
3516 RRETURN(MATCH_NOMATCH);
3517 }
3518 }
3519 /* Control never gets here */
3520
3521 /* Match a negated single one-byte character. The character we are
3522 checking can be multibyte. */
3523
3524 case OP_NOT:
3525 case OP_NOTI:
3526 if (eptr >= md->end_subject)
3527 {
3528 SCHECK_PARTIAL();
3529 RRETURN(MATCH_NOMATCH);
3530 }
3531 ecode++;
3532 GETCHARINCTEST(c, eptr);
3533 if (op == OP_NOTI) /* The caseless case */
3534 {
3535 register unsigned int ch, och;
3536 ch = *ecode++;
3537 #ifdef COMPILE_PCRE8
3538 /* ch must be < 128 if UTF is enabled. */
3539 och = md->fcc[ch];
3540 #else
3541 #ifdef SUPPORT_UTF
3542 #ifdef SUPPORT_UCP
3543 if (utf && ch > 127)
3544 och = UCD_OTHERCASE(ch);
3545 #else
3546 if (utf && ch > 127)
3547 och = ch;
3548 #endif /* SUPPORT_UCP */
3549 else
3550 #endif /* SUPPORT_UTF */
3551 och = TABLE_GET(ch, md->fcc, ch);
3552 #endif /* COMPILE_PCRE8 */
3553 if (ch == c || och == c) RRETURN(MATCH_NOMATCH);
3554 }
3555 else /* Caseful */
3556 {
3557 if (*ecode++ == c) RRETURN(MATCH_NOMATCH);
3558 }
3559 break;
3560
3561 /* Match a negated single one-byte character repeatedly. This is almost a
3562 repeat of the code for a repeated single character, but I haven't found a
3563 nice way of commoning these up that doesn't require a test of the
3564 positive/negative option for each character match. Maybe that wouldn't add
3565 very much to the time taken, but character matching *is* what this is all
3566 about... */
3567
3568 case OP_NOTEXACT:
3569 case OP_NOTEXACTI:
3570 min = max = GET2(ecode, 1);
3571 ecode += 1 + IMM2_SIZE;
3572 goto REPEATNOTCHAR;
3573
3574 case OP_NOTUPTO:
3575 case OP_NOTUPTOI:
3576 case OP_NOTMINUPTO:
3577 case OP_NOTMINUPTOI:
3578 min = 0;
3579 max = GET2(ecode, 1);
3580 minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI;
3581 ecode += 1 + IMM2_SIZE;
3582 goto REPEATNOTCHAR;
3583
3584 case OP_NOTPOSSTAR:
3585 case OP_NOTPOSSTARI:
3586 possessive = TRUE;
3587 min = 0;
3588 max = INT_MAX;
3589 ecode++;
3590 goto REPEATNOTCHAR;
3591
3592 case OP_NOTPOSPLUS:
3593 case OP_NOTPOSPLUSI:
3594 possessive = TRUE;
3595 min = 1;
3596 max = INT_MAX;
3597 ecode++;
3598 goto REPEATNOTCHAR;
3599
3600 case OP_NOTPOSQUERY:
3601 case OP_NOTPOSQUERYI:
3602 possessive = TRUE;
3603 min = 0;
3604 max = 1;
3605 ecode++;
3606 goto REPEATNOTCHAR;
3607
3608 case OP_NOTPOSUPTO:
3609 case OP_NOTPOSUPTOI:
3610 possessive = TRUE;
3611 min = 0;
3612 max = GET2(ecode, 1);
3613 ecode += 1 + IMM2_SIZE;
3614 goto REPEATNOTCHAR;
3615
3616 case OP_NOTSTAR:
3617 case OP_NOTSTARI:
3618 case OP_NOTMINSTAR:
3619 case OP_NOTMINSTARI:
3620 case OP_NOTPLUS:
3621 case OP_NOTPLUSI:
3622 case OP_NOTMINPLUS:
3623 case OP_NOTMINPLUSI:
3624 case OP_NOTQUERY:
3625 case OP_NOTQUERYI:
3626 case OP_NOTMINQUERY:
3627 case OP_NOTMINQUERYI:
3628 c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
3629 minimize = (c & 1) != 0;
3630 min = rep_min[c]; /* Pick up values from tables; */
3631 max = rep_max[c]; /* zero for max => infinity */
3632 if (max == 0) max = INT_MAX;
3633
3634 /* Common code for all repeated single-byte matches. */
3635
3636 REPEATNOTCHAR:
3637 fc = *ecode++;
3638
3639 /* The code is duplicated for the caseless and caseful cases, for speed,
3640 since matching characters is likely to be quite common. First, ensure the
3641 minimum number of matches are present. If min = max, continue at the same
3642 level without recursing. Otherwise, if minimizing, keep trying the rest of
3643 the expression and advancing one matching character if failing, up to the
3644 maximum. Alternatively, if maximizing, find the maximum number of
3645 characters and work backwards. */
3646
3647 DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3648 max, eptr));
3649
3650 if (op >= OP_NOTSTARI) /* Caseless */
3651 {
3652 #ifdef COMPILE_PCRE8
3653 /* fc must be < 128 if UTF is enabled. */
3654 foc = md->fcc[fc];
3655 #else
3656 #ifdef SUPPORT_UTF
3657 #ifdef SUPPORT_UCP
3658 if (utf && fc > 127)
3659 foc = UCD_OTHERCASE(fc);
3660 #else
3661 if (utf && fc > 127)
3662 foc = fc;
3663 #endif /* SUPPORT_UCP */
3664 else
3665 #endif /* SUPPORT_UTF */
3666 foc = TABLE_GET(fc, md->fcc, fc);
3667 #endif /* COMPILE_PCRE8 */
3668
3669 #ifdef SUPPORT_UTF
3670 if (utf)
3671 {
3672 register unsigned int d;
3673 for (i = 1; i <= min; i++)
3674 {
3675 if (eptr >= md->end_subject)
3676 {
3677 SCHECK_PARTIAL();
3678 RRETURN(MATCH_NOMATCH);
3679 }
3680 GETCHARINC(d, eptr);
3681 if (fc == d || (unsigned int) foc == d) RRETURN(MATCH_NOMATCH);
3682 }
3683 }
3684 else
3685 #endif
3686 /* Not UTF mode */
3687 {
3688 for (i = 1; i <= min; i++)
3689 {
3690 if (eptr >= md->end_subject)
3691 {
3692 SCHECK_PARTIAL();
3693 RRETURN(MATCH_NOMATCH);
3694 }
3695 if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH);
3696 eptr++;
3697 }
3698 }
3699
3700 if (min == max) continue;
3701
3702 if (minimize)
3703 {
3704 #ifdef SUPPORT_UTF
3705 if (utf)
3706 {
3707 register unsigned int d;
3708 for (fi = min;; fi++)
3709 {
3710 RMATCH(eptr, ecode, offset_top, md, eptrb, RM28);
3711 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3712 if (fi >= max) RRETURN(MATCH_NOMATCH);
3713 if (eptr >= md->end_subject)
3714 {
3715 SCHECK_PARTIAL();
3716 RRETURN(MATCH_NOMATCH);
3717 }
3718 GETCHARINC(d, eptr);
3719 if (fc == d || (unsigned int)foc == d) RRETURN(MATCH_NOMATCH);
3720 }
3721 }
3722 else
3723 #endif
3724 /* Not UTF mode */
3725 {
3726 for (fi = min;; fi++)
3727 {
3728 RMATCH(eptr, ecode, offset_top, md, eptrb, RM29);
3729 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3730 if (fi >= max) RRETURN(MATCH_NOMATCH);
3731 if (eptr >= md->end_subject)
3732 {
3733 SCHECK_PARTIAL();
3734 RRETURN(MATCH_NOMATCH);
3735 }
3736 if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH);
3737 eptr++;
3738 }
3739 }
3740 /* Control never gets here */
3741 }
3742
3743 /* Maximize case */
3744
3745 else
3746 {
3747 pp = eptr;
3748
3749 #ifdef SUPPORT_UTF
3750 if (utf)
3751 {
3752 register unsigned int d;
3753 for (i = min; i < max; i++)
3754 {
3755 int len = 1;
3756 if (eptr >= md->end_subject)
3757 {
3758 SCHECK_PARTIAL();
3759 break;
3760 }
3761 GETCHARLEN(d, eptr, len);
3762 if (fc == d || (unsigned int)foc == d) break;
3763 eptr += len;
3764 }
3765 if (possessive) continue;
3766 for(;;)
3767 {
3768 RMATCH(eptr, ecode, offset_top, md, eptrb, RM30);
3769 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3770 if (eptr-- == pp) break; /* Stop if tried at original pos */
3771 BACKCHAR(eptr);
3772 }
3773 }
3774 else
3775 #endif
3776 /* Not UTF mode */
3777 {
3778 for (i = min; i < max; i++)
3779 {
3780 if (eptr >= md->end_subject)
3781 {
3782 SCHECK_PARTIAL();
3783 break;
3784 }
3785 if (fc == *eptr || foc == *eptr) break;
3786 eptr++;
3787 }
3788 if (possessive) continue;
3789 while (eptr >= pp)
3790 {
3791 RMATCH(eptr, ecode, offset_top, md, eptrb, RM31);
3792 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3793 eptr--;
3794 }
3795 }
3796
3797 RRETURN(MATCH_NOMATCH);
3798 }
3799 /* Control never gets here */
3800 }
3801
3802 /* Caseful comparisons */
3803
3804 else
3805 {
3806 #ifdef SUPPORT_UTF
3807 if (utf)
3808 {
3809 register unsigned int d;
3810 for (i = 1; i <= min; i++)
3811 {
3812 if (eptr >= md->end_subject)
3813 {
3814 SCHECK_PARTIAL();
3815 RRETURN(MATCH_NOMATCH);
3816 }
3817 GETCHARINC(d, eptr);
3818 if (fc == d) RRETURN(MATCH_NOMATCH);
3819 }
3820 }
3821 else
3822 #endif
3823 /* Not UTF mode */
3824 {
3825 for (i = 1; i <= min; i++)
3826 {
3827 if (eptr >= md->end_subject)
3828 {
3829 SCHECK_PARTIAL();
3830 RRETURN(MATCH_NOMATCH);
3831 }
3832 if (fc == *eptr++) RRETURN(MATCH_NOMATCH);
3833 }
3834 }
3835
3836 if (min == max) continue;
3837
3838 if (minimize)
3839 {
3840 #ifdef SUPPORT_UTF
3841 if (utf)
3842 {
3843 register unsigned int d;
3844 for (fi = min;; fi++)
3845 {
3846 RMATCH(eptr, ecode, offset_top, md, eptrb, RM32);
3847 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3848 if (fi >= max) RRETURN(MATCH_NOMATCH);
3849 if (eptr >= md->end_subject)
3850 {
3851 SCHECK_PARTIAL();
3852 RRETURN(MATCH_NOMATCH);
3853 }
3854 GETCHARINC(d, eptr);
3855 if (fc == d) RRETURN(MATCH_NOMATCH);
3856 }
3857 }
3858 else
3859 #endif
3860 /* Not UTF mode */
3861 {
3862 for (fi = min;; fi++)
3863 {
3864 RMATCH(eptr, ecode, offset_top, md, eptrb, RM33);
3865 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3866 if (fi >= max) RRETURN(MATCH_NOMATCH);
3867 if (eptr >= md->end_subject)
3868 {
3869 SCHECK_PARTIAL();
3870 RRETURN(MATCH_NOMATCH);
3871 }
3872 if (fc == *eptr++) RRETURN(MATCH_NOMATCH);
3873 }
3874 }
3875 /* Control never gets here */
3876 }
3877
3878 /* Maximize case */
3879
3880 else
3881 {
3882 pp = eptr;
3883
3884 #ifdef SUPPORT_UTF
3885 if (utf)
3886 {
3887 register unsigned int d;
3888 for (i = min; i < max; i++)
3889 {
3890 int len = 1;
3891 if (eptr >= md->end_subject)
3892 {
3893 SCHECK_PARTIAL();
3894 break;
3895 }
3896 GETCHARLEN(d, eptr, len);
3897 if (fc == d) break;
3898 eptr += len;
3899 }
3900 if (possessive) continue;
3901 for(;;)
3902 {
3903 RMATCH(eptr, ecode, offset_top, md, eptrb, RM34);
3904 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3905 if (eptr-- == pp) break; /* Stop if tried at original pos */
3906 BACKCHAR(eptr);
3907 }
3908 }
3909 else
3910 #endif
3911 /* Not UTF mode */
3912 {
3913 for (i = min; i < max; i++)
3914 {
3915 if (eptr >= md->end_subject)
3916 {
3917 SCHECK_PARTIAL();
3918 break;
3919 }
3920 if (fc == *eptr) break;
3921 eptr++;
3922 }
3923 if (possessive) continue;
3924 while (eptr >= pp)
3925 {
3926 RMATCH(eptr, ecode, offset_top, md, eptrb, RM35);
3927 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3928 eptr--;
3929 }
3930 }
3931
3932 RRETURN(MATCH_NOMATCH);
3933 }
3934 }
3935 /* Control never gets here */
3936
3937 /* Match a single character type repeatedly; several different opcodes
3938 share code. This is very similar to the code for single characters, but we
3939 repeat it in the interests of efficiency. */
3940
3941 case OP_TYPEEXACT:
3942 min = max = GET2(ecode, 1);
3943 minimize = TRUE;
3944 ecode += 1 + IMM2_SIZE;
3945 goto REPEATTYPE;
3946
3947 case OP_TYPEUPTO:
3948 case OP_TYPEMINUPTO:
3949 min = 0;
3950 max = GET2(ecode, 1);
3951 minimize = *ecode == OP_TYPEMINUPTO;
3952 ecode += 1 + IMM2_SIZE;
3953 goto REPEATTYPE;
3954
3955 case OP_TYPEPOSSTAR:
3956 possessive = TRUE;
3957 min = 0;
3958 max = INT_MAX;
3959 ecode++;
3960 goto REPEATTYPE;
3961
3962 case OP_TYPEPOSPLUS:
3963 possessive = TRUE;
3964 min = 1;
3965 max = INT_MAX;
3966 ecode++;
3967 goto REPEATTYPE;
3968
3969 case OP_TYPEPOSQUERY:
3970 possessive = TRUE;
3971 min = 0;
3972 max = 1;
3973 ecode++;
3974 goto REPEATTYPE;
3975
3976 case OP_TYPEPOSUPTO:
3977 possessive = TRUE;
3978 min = 0;
3979 max = GET2(ecode, 1);
3980 ecode += 1 + IMM2_SIZE;
3981 goto REPEATTYPE;
3982
3983 case OP_TYPESTAR:
3984 case OP_TYPEMINSTAR:
3985 case OP_TYPEPLUS:
3986 case OP_TYPEMINPLUS:
3987 case OP_TYPEQUERY:
3988 case OP_TYPEMINQUERY:
3989 c = *ecode++ - OP_TYPESTAR;
3990 minimize = (c & 1) != 0;
3991 min = rep_min[c]; /* Pick up values from tables; */
3992 max = rep_max[c]; /* zero for max => infinity */
3993 if (max == 0) max = INT_MAX;
3994
3995 /* Common code for all repeated single character type matches. Note that
3996 in UTF-8 mode, '.' matches a character of any length, but for the other
3997 character types, the valid characters are all one-byte long. */
3998
3999 REPEATTYPE:
4000 ctype = *ecode++; /* Code for the character type */
4001
4002 #ifdef SUPPORT_UCP
4003 if (ctype == OP_PROP || ctype == OP_NOTPROP)
4004 {
4005 prop_fail_result = ctype == OP_NOTPROP;
4006 prop_type = *ecode++;
4007 prop_value = *ecode++;
4008 }
4009 else prop_type = -1;
4010 #endif
4011
4012 /* First, ensure the minimum number of matches are present. Use inline
4013 code for maximizing the speed, and do the type test once at the start
4014 (i.e. keep it out of the loop). Separate the UTF-8 code completely as that
4015 is tidier. Also separate the UCP code, which can be the same for both UTF-8
4016 and single-bytes. */
4017
4018 if (min > 0)
4019 {
4020 #ifdef SUPPORT_UCP
4021 if (prop_type >= 0)
4022 {
4023 switch(prop_type)
4024 {
4025 case PT_ANY:
4026 if (prop_fail_result) RRETURN(MATCH_NOMATCH);
4027 for (i = 1; i <= min; i++)
4028 {
4029 if (eptr >= md->end_subject)
4030 {
4031 SCHECK_PARTIAL();
4032 RRETURN(MATCH_NOMATCH);
4033 }
4034 GETCHARINCTEST(c, eptr);
4035 }
4036 break;
4037
4038 case PT_LAMP:
4039 for (i = 1; i <= min; i++)
4040 {
4041 int chartype;
4042 if (eptr >= md->end_subject)
4043 {
4044 SCHECK_PARTIAL();
4045 RRETURN(MATCH_NOMATCH);
4046 }
4047 GETCHARINCTEST(c, eptr);
4048 chartype = UCD_CHARTYPE(c);
4049 if ((chartype == ucp_Lu ||
4050 chartype == ucp_Ll ||
4051 chartype == ucp_Lt) == prop_fail_result)
4052 RRETURN(MATCH_NOMATCH);
4053 }
4054 break;
4055
4056 case PT_GC:
4057 for (i = 1; i <= min; i++)
4058 {
4059 if (eptr >= md->end_subject)
4060 {
4061 SCHECK_PARTIAL();
4062 RRETURN(MATCH_NOMATCH);
4063 }
4064 GETCHARINCTEST(c, eptr);
4065 if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result)
4066 RRETURN(MATCH_NOMATCH);
4067 }
4068 break;
4069
4070 case PT_PC:
4071 for (i = 1; i <= min; i++)
4072 {
4073 if (eptr >= md->end_subject)
4074 {
4075 SCHECK_PARTIAL();
4076 RRETURN(MATCH_NOMATCH);
4077 }
4078 GETCHARINCTEST(c, eptr);
4079 if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result)
4080 RRETURN(MATCH_NOMATCH);
4081 }
4082 break;
4083
4084 case PT_SC:
4085 for (i = 1; i <= min; i++)
4086 {
4087 if (eptr >= md->end_subject)
4088 {
4089 SCHECK_PARTIAL();
4090 RRETURN(MATCH_NOMATCH);
4091 }
4092 GETCHARINCTEST(c, eptr);
4093 if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result)
4094 RRETURN(MATCH_NOMATCH);
4095 }
4096 break;
4097
4098 case PT_ALNUM:
4099 for (i = 1; i <= min; i++)
4100 {
4101 int category;
4102 if (eptr >= md->end_subject)
4103 {
4104 SCHECK_PARTIAL();
4105 RRETURN(MATCH_NOMATCH);
4106 }
4107 GETCHARINCTEST(c, eptr);
4108 category = UCD_CATEGORY(c);
4109 if ((category == ucp_L || category == ucp_N) == prop_fail_result)
4110 RRETURN(MATCH_NOMATCH);
4111 }
4112 break;
4113
4114 case PT_SPACE: /* Perl space */
4115 for (i = 1; i <= min; i++)
4116 {
4117 if (eptr >= md->end_subject)
4118 {
4119 SCHECK_PARTIAL();
4120 RRETURN(MATCH_NOMATCH);
4121 }
4122 GETCHARINCTEST(c, eptr);
4123 if ((UCD_CATEGORY(c) == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
4124 c == CHAR_FF || c == CHAR_CR)
4125 == prop_fail_result)
4126 RRETURN(MATCH_NOMATCH);
4127 }
4128 break;
4129
4130 case PT_PXSPACE: /* POSIX space */
4131 for (i = 1; i <= min; i++)
4132 {
4133 if (eptr >= md->end_subject)
4134 {
4135 SCHECK_PARTIAL();
4136 RRETURN(MATCH_NOMATCH);
4137 }
4138 GETCHARINCTEST(c, eptr);
4139 if ((UCD_CATEGORY(c) == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
4140 c == CHAR_VT || c == CHAR_FF || c == CHAR_CR)
4141 == prop_fail_result)
4142 RRETURN(MATCH_NOMATCH);
4143 }
4144 break;
4145
4146 case PT_WORD:
4147 for (i = 1; i <= min; i++)
4148 {
4149 int category;
4150 if (eptr >= md->end_subject)
4151 {
4152 SCHECK_PARTIAL();
4153 RRETURN(MATCH_NOMATCH);
4154 }
4155 GETCHARINCTEST(c, eptr);
4156 category = UCD_CATEGORY(c);
4157 if ((category == ucp_L || category == ucp_N || c == CHAR_UNDERSCORE)
4158 == prop_fail_result)
4159 RRETURN(MATCH_NOMATCH);
4160 }
4161 break;
4162
4163 /* This should not occur */
4164
4165 default:
4166 RRETURN(PCRE_ERROR_INTERNAL);
4167 }
4168 }
4169
4170 /* Match extended Unicode sequences. We will get here only if the
4171 support is in the binary; otherwise a compile-time error occurs. */
4172
4173 else if (ctype == OP_EXTUNI)
4174 {
4175 for (i = 1; i <= min; i++)
4176 {
4177 if (eptr >= md->end_subject)
4178 {
4179 SCHECK_PARTIAL();
4180 RRETURN(MATCH_NOMATCH);
4181 }
4182 GETCHARINCTEST(c, eptr);
4183 if (UCD_CATEGORY(c) == ucp_M) RRETURN(MATCH_NOMATCH);
4184 while (eptr < md->end_subject)
4185 {
4186 int len = 1;
4187 if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
4188 if (UCD_CATEGORY(c) != ucp_M) break;
4189 eptr += len;
4190 }
4191 }
4192 if (md->partial != 0 && eptr >= md->end_subject)
4193 {
4194 SCHECK_PARTIAL();
4195 }
4196 }
4197
4198 else
4199 #endif /* SUPPORT_UCP */
4200
4201 /* Handle all other cases when the coding is UTF-8 */
4202
4203 #ifdef SUPPORT_UTF
4204 if (utf) switch(ctype)
4205 {
4206 case OP_ANY:
4207 for (i = 1; i <= min; i++)
4208 {
4209 if (eptr >= md->end_subject)
4210 {
4211 SCHECK_PARTIAL();
4212 RRETURN(MATCH_NOMATCH);
4213 }
4214 if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
4215 eptr++;
4216 ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
4217 }
4218 break;
4219
4220 case OP_ALLANY:
4221 for (i = 1; i <= min; i++)
4222 {
4223 if (eptr >= md->end_subject)
4224 {
4225 SCHECK_PARTIAL();
4226 RRETURN(MATCH_NOMATCH);
4227 }
4228 eptr++;
4229 ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
4230 }
4231 break;
4232
4233 case OP_ANYBYTE:
4234 if (eptr > md->end_subject - min) RRETURN(MATCH_NOMATCH);
4235 eptr += min;
4236 break;
4237
4238 case OP_ANYNL:
4239 for (i = 1; i <= min; i++)
4240 {
4241 if (eptr >= md->end_subject)
4242 {
4243 SCHECK_PARTIAL();
4244 RRETURN(MATCH_NOMATCH);
4245 }
4246 GETCHARINC(c, eptr);
4247 switch(c)
4248 {
4249 default: RRETURN(MATCH_NOMATCH);
4250
4251 case 0x000d:
4252 if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
4253 break;
4254
4255 case 0x000a:
4256 break;
4257
4258 case 0x000b:
4259 case 0x000c:
4260 case 0x0085:
4261 case 0x2028:
4262 case 0x2029:
4263 if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
4264 break;
4265 }
4266 }
4267 break;
4268
4269 case OP_NOT_HSPACE:
4270 for (i = 1; i <= min; i++)
4271 {
4272 if (eptr >= md->end_subject)
4273 {
4274 SCHECK_PARTIAL();
4275 RRETURN(MATCH_NOMATCH);
4276 }
4277 GETCHARINC(c, eptr);
4278 switch(c)
4279 {
4280 default: break;
4281 case 0x09: /* HT */
4282 case 0x20: /* SPACE */
4283 case 0xa0: /* NBSP */
4284 case 0x1680: /* OGHAM SPACE MARK */
4285 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
4286 case 0x2000: /* EN QUAD */
4287 case 0x2001: /* EM QUAD */
4288 case 0x2002: /* EN SPACE */
4289 case 0x2003: /* EM SPACE */
4290 case 0x2004: /* THREE-PER-EM SPACE */
4291 case 0x2005: /* FOUR-PER-EM SPACE */
4292 case 0x2006: /* SIX-PER-EM SPACE */
4293 case 0x2007: /* FIGURE SPACE */
4294 case 0x2008: /* PUNCTUATION SPACE */
4295 case 0x2009: /* THIN SPACE */
4296 case 0x200A: /* HAIR SPACE */
4297 case 0x202f: /* NARROW NO-BREAK SPACE */
4298 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
4299 case 0x3000: /* IDEOGRAPHIC SPACE */
4300 RRETURN(MATCH_NOMATCH);
4301 }
4302 }
4303 break;
4304
4305 case OP_HSPACE:
4306 for (i = 1; i <= min; i++)
4307 {
4308 if (eptr >= md->end_subject)
4309 {
4310 SCHECK_PARTIAL();
4311 RRETURN(MATCH_NOMATCH);
4312 }
4313 GETCHARINC(c, eptr);
4314 switch(c)
4315 {
4316 default: RRETURN(MATCH_NOMATCH);
4317 case 0x09: /* HT */
4318 case 0x20: /* SPACE */
4319 case 0xa0: /* NBSP */
4320 case 0x1680: /* OGHAM SPACE MARK */
4321 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
4322 case 0x2000: /* EN QUAD */
4323 case 0x2001: /* EM QUAD */
4324 case 0x2002: /* EN SPACE */
4325 case 0x2003: /* EM SPACE */
4326 case 0x2004: /* THREE-PER-EM SPACE */
4327 case 0x2005: /* FOUR-PER-EM SPACE */
4328 case 0x2006: /* SIX-PER-EM SPACE */
4329 case 0x2007: /* FIGURE SPACE */
4330 case 0x2008: /* PUNCTUATION SPACE */
4331 case 0x2009: /* THIN SPACE */
4332 case 0x200A: /* HAIR SPACE */
4333 case 0x202f: /* NARROW NO-BREAK SPACE */
4334 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
4335 case 0x3000: /* IDEOGRAPHIC SPACE */
4336 break;
4337 }
4338 }
4339 break;
4340
4341 case OP_NOT_VSPACE:
4342 for (i = 1; i <= min; i++)
4343 {
4344 if (eptr >= md->end_subject)
4345 {
4346 SCHECK_PARTIAL();
4347 RRETURN(MATCH_NOMATCH);
4348 }
4349 GETCHARINC(c, eptr);
4350 switch(c)
4351 {
4352 default: break;
4353 case 0x0a: /* LF */
4354 case 0x0b: /* VT */
4355 case 0x0c: /* FF */
4356 case 0x0d: /* CR */
4357 case 0x85: /* NEL */
4358 case 0x2028: /* LINE SEPARATOR */
4359 case 0x2029: /* PARAGRAPH SEPARATOR */
4360 RRETURN(MATCH_NOMATCH);
4361 }
4362 }
4363 break;
4364
4365 case OP_VSPACE:
4366 for (i = 1; i <= min; i++)
4367 {
4368 if (eptr >= md->end_subject)
4369 {
4370 SCHECK_PARTIAL();
4371 RRETURN(MATCH_NOMATCH);
4372 }
4373 GETCHARINC(c, eptr);
4374 switch(c)
4375 {
4376 default: RRETURN(MATCH_NOMATCH);
4377 case 0x0a: /* LF */
4378 case 0x0b: /* VT */
4379 case 0x0c: /* FF */
4380 case 0x0d: /* CR */
4381 case 0x85: /* NEL */
4382 case 0x2028: /* LINE SEPARATOR */
4383 case 0x2029: /* PARAGRAPH SEPARATOR */
4384 break;
4385 }
4386 }
4387 break;
4388
4389 case OP_NOT_DIGIT:
4390 for (i = 1; i <= min; i++)
4391 {
4392 if (eptr >= md->end_subject)
4393 {
4394 SCHECK_PARTIAL();
4395 RRETURN(MATCH_NOMATCH);
4396 }
4397 GETCHARINC(c, eptr);
4398 if (c < 128 && (md->ctypes[c] & ctype_digit) != 0)
4399 RRETURN(MATCH_NOMATCH);
4400 }
4401 break;
4402
4403 case OP_DIGIT:
4404 for (i = 1; i <= min; i++)
4405 {
4406 if (eptr >= md->end_subject)
4407 {
4408 SCHECK_PARTIAL();
4409 RRETURN(MATCH_NOMATCH);
4410 }
4411 if (*eptr >= 128 || (md->ctypes[*eptr] & ctype_digit) == 0)
4412 RRETURN(MATCH_NOMATCH);
4413 eptr++;
4414 /* No need to skip more bytes - we know it's a 1-byte character */
4415 }
4416 break;
4417
4418 case OP_NOT_WHITESPACE:
4419 for (i = 1; i <= min; i++)
4420 {
4421 if (eptr >= md->end_subject)
4422 {
4423 SCHECK_PARTIAL();
4424 RRETURN(MATCH_NOMATCH);
4425 }
4426 if (*eptr < 128 && (md->ctypes[*eptr] & ctype_space) != 0)
4427 RRETURN(MATCH_NOMATCH);
4428 eptr++;
4429 ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
4430 }
4431 break;
4432
4433 case OP_WHITESPACE:
4434 for (i = 1; i <= min; i++)
4435 {
4436 if (eptr >= md->end_subject)
4437 {
4438 SCHECK_PARTIAL();
4439 RRETURN(MATCH_NOMATCH);
4440 }
4441 if (*eptr >= 128 || (md->ctypes[*eptr] & ctype_space) == 0)
4442 RRETURN(MATCH_NOMATCH);
4443 eptr++;
4444 /* No need to skip more bytes - we know it's a 1-byte character */
4445 }
4446 break;
4447
4448 case OP_NOT_WORDCHAR:
4449 for (i = 1; i <= min; i++)
4450 {
4451 if (eptr >= md->end_subject)
4452 {
4453 SCHECK_PARTIAL();
4454 RRETURN(MATCH_NOMATCH);
4455 }
4456 if (*eptr < 128 && (md->ctypes[*eptr] & ctype_word) != 0)
4457 RRETURN(MATCH_NOMATCH);
4458 eptr++;
4459 ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
4460 }
4461 break;
4462
4463 case OP_WORDCHAR:
4464 for (i = 1; i <= min; i++)
4465 {
4466 if (eptr >= md->end_subject)
4467 {
4468 SCHECK_PARTIAL();
4469 RRETURN(MATCH_NOMATCH);
4470 }
4471 if (*eptr >= 128 || (md->ctypes[*eptr] & ctype_word) == 0)
4472 RRETURN(MATCH_NOMATCH);
4473 eptr++;
4474 /* No need to skip more bytes - we know it's a 1-byte character */
4475 }
4476 break;
4477
4478 default:
4479 RRETURN(PCRE_ERROR_INTERNAL);
4480 } /* End switch(ctype) */
4481
4482 else
4483 #endif /* SUPPORT_UTF */
4484
4485 /* Code for the non-UTF-8 case for minimum matching of operators other
4486 than OP_PROP and OP_NOTPROP. */
4487
4488 switch(ctype)
4489 {
4490 case OP_ANY:
4491 for (i = 1; i <= min; i++)
4492 {
4493 if (eptr >= md->end_subject)
4494 {
4495 SCHECK_PARTIAL();
4496 RRETURN(MATCH_NOMATCH);
4497 }
4498 if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
4499 eptr++;
4500 }
4501 break;
4502
4503 case OP_ALLANY:
4504 if (eptr > md->end_subject - min)
4505 {
4506 SCHECK_PARTIAL();
4507 RRETURN(MATCH_NOMATCH);
4508 }
4509 eptr += min;
4510 break;
4511
4512 case OP_ANYBYTE:
4513 if (eptr > md->end_subject - min)
4514 {
4515 SCHECK_PARTIAL();
4516 RRETURN(MATCH_NOMATCH);
4517 }
4518 eptr += min;
4519 break;
4520
4521 case OP_ANYNL:
4522 for (i = 1; i <= min; i++)
4523 {
4524 if (eptr >= md->end_subject)
4525 {
4526 SCHECK_PARTIAL();
4527 RRETURN(MATCH_NOMATCH);
4528 }
4529 switch(*eptr++)
4530 {
4531 default: RRETURN(MATCH_NOMATCH);
4532
4533 case 0x000d:
4534 if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
4535 break;
4536
4537 case 0x000a:
4538 break;
4539
4540 case 0x000b:
4541 case 0x000c:
4542 case 0x0085:
4543 #ifdef COMPILE_PCRE16
4544 case 0x2028:
4545 case 0x2029:
4546 #endif
4547 if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
4548 break;
4549 }
4550 }
4551 break;
4552
4553 case OP_NOT_HSPACE:
4554 for (i = 1; i <= min; i++)
4555 {
4556 if (eptr >= md->end_subject)
4557 {
4558 SCHECK_PARTIAL();
4559 RRETURN(MATCH_NOMATCH);
4560 }
4561 switch(*eptr++)
4562 {
4563 default: break;
4564 case 0x09: /* HT */
4565 case 0x20: /* SPACE */
4566 case 0xa0: /* NBSP */
4567 #ifdef COMPILE_PCRE16
4568 case 0x1680: /* OGHAM SPACE MARK */
4569 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
4570 case 0x2000: /* EN QUAD */
4571 case 0x2001: /* EM QUAD */
4572 case 0x2002: /* EN SPACE */
4573 case 0x2003: /* EM SPACE */
4574 case 0x2004: /* THREE-PER-EM SPACE */
4575 case 0x2005: /* FOUR-PER-EM SPACE */
4576 case 0x2006: /* SIX-PER-EM SPACE */
4577 case 0x2007: /* FIGURE SPACE */
4578 case 0x2008: /* PUNCTUATION SPACE */
4579 case 0x2009: /* THIN SPACE */
4580 case 0x200A: /* HAIR SPACE */
4581 case 0x202f: /* NARROW NO-BREAK SPACE */
4582 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
4583 case 0x3000: /* IDEOGRAPHIC SPACE */
4584 #endif
4585 RRETURN(MATCH_NOMATCH);
4586 }
4587 }
4588 break;
4589
4590 case OP_HSPACE:
4591 for (i = 1; i <= min; i++)
4592 {
4593 if (eptr >= md->end_subject)
4594 {
4595 SCHECK_PARTIAL();
4596 RRETURN(MATCH_NOMATCH);
4597 }
4598 switch(*eptr++)
4599 {
4600 default: RRETURN(MATCH_NOMATCH);
4601 case 0x09: /* HT */
4602 case 0x20: /* SPACE */
4603 case 0xa0: /* NBSP */
4604 #ifdef COMPILE_PCRE16
4605 case 0x1680: /* OGHAM SPACE MARK */
4606 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
4607 case 0x2000: /* EN QUAD */
4608 case 0x2001: /* EM QUAD */
4609 case 0x2002: /* EN SPACE */
4610 case 0x2003: /* EM SPACE */
4611 case 0x2004: /* THREE-PER-EM SPACE */
4612 case 0x2005: /* FOUR-PER-EM SPACE */
4613 case 0x2006: /* SIX-PER-EM SPACE */
4614 case 0x2007: /* FIGURE SPACE */
4615 case 0x2008: /* PUNCTUATION SPACE */
4616 case 0x2009: /* THIN SPACE */
4617 case 0x200A: /* HAIR SPACE */
4618 case 0x202f: /* NARROW NO-BREAK SPACE */
4619 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
4620 case 0x3000: /* IDEOGRAPHIC SPACE */
4621 #endif
4622 break;
4623 }
4624 }
4625 break;
4626
4627 case OP_NOT_VSPACE:
4628 for (i = 1; i <= min; i++)
4629 {
4630 if (eptr >= md->end_subject)
4631 {
4632 SCHECK_PARTIAL();
4633 RRETURN(MATCH_NOMATCH);
4634 }
4635 switch(*eptr++)
4636 {
4637 default: break;
4638 case 0x0a: /* LF */
4639 case 0x0b: /* VT */
4640 case 0x0c: /* FF */
4641 case 0x0d: /* CR */
4642 case 0x85: /* NEL */
4643 #ifdef COMPILE_PCRE16
4644 case 0x2028: /* LINE SEPARATOR */
4645 case 0x2029: /* PARAGRAPH SEPARATOR */
4646 #endif
4647 RRETURN(MATCH_NOMATCH);
4648 }
4649 }
4650 break;
4651
4652 case OP_VSPACE:
4653 for (i = 1; i <= min; i++)
4654 {
4655 if (eptr >= md->end_subject)
4656 {
4657 SCHECK_PARTIAL();
4658 RRETURN(MATCH_NOMATCH);
4659 }
4660 switch(*eptr++)
4661 {
4662 default: RRETURN(MATCH_NOMATCH);
4663 case 0x0a: /* LF */
4664 case 0x0b: /* VT */
4665 case 0x0c: /* FF */
4666 case 0x0d: /* CR */
4667 case 0x85: /* NEL */
4668 #ifdef COMPILE_PCRE16
4669 case 0x2028: /* LINE SEPARATOR */
4670 case 0x2029: /* PARAGRAPH SEPARATOR */
4671 #endif
4672 break;
4673 }
4674 }
4675 break;
4676
4677 case OP_NOT_DIGIT:
4678 for (i = 1; i <= min; i++)
4679 {
4680 if (eptr >= md->end_subject)
4681 {
4682 SCHECK_PARTIAL();
4683 RRETURN(MATCH_NOMATCH);
4684 }
4685 if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_digit) != 0)
4686 RRETURN(MATCH_NOMATCH);
4687 eptr++;
4688 }
4689 break;
4690
4691 case OP_DIGIT:
4692 for (i = 1; i <= min; i++)
4693 {
4694 if (eptr >= md->end_subject)
4695 {
4696 SCHECK_PARTIAL();
4697 RRETURN(MATCH_NOMATCH);
4698 }
4699 if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_digit) == 0)
4700 RRETURN(MATCH_NOMATCH);
4701 eptr++;
4702 }
4703 break;
4704
4705 case OP_NOT_WHITESPACE:
4706 for (i = 1; i <= min; i++)
4707 {
4708 if (eptr >= md->end_subject)
4709 {
4710 SCHECK_PARTIAL();
4711 RRETURN(MATCH_NOMATCH);
4712 }
4713 if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_space) != 0)
4714 RRETURN(MATCH_NOMATCH);
4715 eptr++;
4716 }
4717 break;
4718
4719 case OP_WHITESPACE:
4720 for (i = 1; i <= min; i++)
4721 {
4722 if (eptr >= md->end_subject)
4723 {
4724 SCHECK_PARTIAL();
4725 RRETURN(MATCH_NOMATCH);
4726 }
4727 if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_space) == 0)
4728 RRETURN(MATCH_NOMATCH);
4729 eptr++;
4730 }
4731 break;
4732
4733 case OP_NOT_WORDCHAR:
4734 for (i = 1; i <= min; i++)
4735 {
4736 if (eptr >= md->end_subject)
4737 {
4738 SCHECK_PARTIAL();
4739 RRETURN(MATCH_NOMATCH);
4740 }
4741 if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_word) != 0)
4742 RRETURN(MATCH_NOMATCH);
4743 eptr++;
4744 }
4745 break;
4746
4747 case OP_WORDCHAR:
4748 for (i = 1; i <= min; i++)
4749 {
4750 if (eptr >= md->end_subject)
4751 {
4752 SCHECK_PARTIAL();
4753 RRETURN(MATCH_NOMATCH);
4754 }
4755 if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_word) == 0)
4756 RRETURN(MATCH_NOMATCH);
4757 eptr++;
4758 }
4759 break;
4760
4761 default:
4762 RRETURN(PCRE_ERROR_INTERNAL);
4763 }
4764 }
4765
4766 /* If min = max, continue at the same level without recursing */
4767
4768 if (min == max) continue;
4769
4770 /* If minimizing, we have to test the rest of the pattern before each
4771 subsequent match. Again, separate the UTF-8 case for speed, and also
4772 separate the UCP cases. */
4773
4774 if (minimize)
4775 {
4776 #ifdef SUPPORT_UCP
4777 if (prop_type >= 0)
4778 {
4779 switch(prop_type)
4780 {
4781 case PT_ANY:
4782 for (fi = min;; fi++)
4783 {
4784 RMATCH(eptr, ecode, offset_top, md, eptrb, RM36);
4785 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4786 if (fi >= max) RRETURN(MATCH_NOMATCH);
4787 if (eptr >= md->end_subject)
4788 {
4789 SCHECK_PARTIAL();
4790 RRETURN(MATCH_NOMATCH);
4791 }
4792 GETCHARINCTEST(c, eptr);
4793 if (prop_fail_result) RRETURN(MATCH_NOMATCH);
4794 }
4795 /* Control never gets here */
4796
4797 case PT_LAMP:
4798 for (fi = min;; fi++)
4799 {
4800 int chartype;
4801 RMATCH(eptr, ecode, offset_top, md, eptrb, RM37);
4802 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4803 if (fi >= max) RRETURN(MATCH_NOMATCH);
4804 if (eptr >= md->end_subject)
4805 {
4806 SCHECK_PARTIAL();
4807 RRETURN(MATCH_NOMATCH);
4808 }
4809 GETCHARINCTEST(c, eptr);
4810 chartype = UCD_CHARTYPE(c);
4811 if ((chartype == ucp_Lu ||
4812 chartype == ucp_Ll ||
4813 chartype == ucp_Lt) == prop_fail_result)
4814 RRETURN(MATCH_NOMATCH);
4815 }
4816 /* Control never gets here */
4817
4818 case PT_GC:
4819 for (fi = min;; fi++)
4820 {
4821 RMATCH(eptr, ecode, offset_top, md, eptrb, RM38);
4822 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4823 if (fi >= max) RRETURN(MATCH_NOMATCH);
4824 if (eptr >= md->end_subject)
4825 {
4826 SCHECK_PARTIAL();
4827 RRETURN(MATCH_NOMATCH);
4828 }
4829 GETCHARINCTEST(c, eptr);
4830 if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result)
4831 RRETURN(MATCH_NOMATCH);
4832 }
4833 /* Control never gets here */
4834
4835 case PT_PC:
4836 for (fi = min;; fi++)
4837 {
4838 RMATCH(eptr, ecode, offset_top, md, eptrb, RM39);
4839 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4840 if (fi >= max) RRETURN(MATCH_NOMATCH);
4841 if (eptr >= md->end_subject)
4842 {
4843 SCHECK_PARTIAL();
4844 RRETURN(MATCH_NOMATCH);
4845 }
4846 GETCHARINCTEST(c, eptr);
4847 if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result)
4848 RRETURN(MATCH_NOMATCH);
4849 }
4850 /* Control never gets here */
4851
4852 case PT_SC:
4853 for (fi = min;; fi++)
4854 {
4855 RMATCH(eptr, ecode, offset_top, md, eptrb, RM40);
4856 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4857 if (fi >= max) RRETURN(MATCH_NOMATCH);
4858 if (eptr >= md->end_subject)
4859 {
4860 SCHECK_PARTIAL();
4861 RRETURN(MATCH_NOMATCH);
4862 }
4863 GETCHARINCTEST(c, eptr);
4864 if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result)
4865 RRETURN(MATCH_NOMATCH);
4866 }
4867 /* Control never gets here */
4868
4869 case PT_ALNUM:
4870 for (fi = min;; fi++)
4871 {
4872 int category;
4873 RMATCH(eptr, ecode, offset_top, md, eptrb, RM59);
4874 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4875 if (fi >= max) RRETURN(MATCH_NOMATCH);
4876 if (eptr >= md->end_subject)
4877 {
4878 SCHECK_PARTIAL();
4879 RRETURN(MATCH_NOMATCH);
4880 }
4881 GETCHARINCTEST(c, eptr);
4882 category = UCD_CATEGORY(c);
4883 if ((category == ucp_L || category == ucp_N) == prop_fail_result)
4884 RRETURN(MATCH_NOMATCH);
4885 }
4886 /* Control never gets here */
4887
4888 case PT_SPACE: /* Perl space */
4889 for (fi = min;; fi++)
4890 {
4891 RMATCH(eptr, ecode, offset_top, md, eptrb, RM60);
4892 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4893 if (fi >= max) RRETURN(MATCH_NOMATCH);
4894 if (eptr >= md->end_subject)
4895 {
4896 SCHECK_PARTIAL();
4897 RRETURN(MATCH_NOMATCH);
4898 }
4899 GETCHARINCTEST(c, eptr);
4900 if ((UCD_CATEGORY(c) == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
4901 c == CHAR_FF || c == CHAR_CR)
4902 == prop_fail_result)
4903 RRETURN(MATCH_NOMATCH);
4904 }
4905 /* Control never gets here */
4906
4907 case PT_PXSPACE: /* POSIX space */
4908 for (fi = min;; fi++)
4909 {
4910 RMATCH(eptr, ecode, offset_top, md, eptrb, RM61);
4911 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4912 if (fi >= max) RRETURN(MATCH_NOMATCH);
4913 if (eptr >= md->end_subject)
4914 {
4915 SCHECK_PARTIAL();
4916 RRETURN(MATCH_NOMATCH);
4917 }
4918 GETCHARINCTEST(c, eptr);
4919 if ((UCD_CATEGORY(c) == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
4920 c == CHAR_VT || c == CHAR_FF || c == CHAR_CR)
4921 == prop_fail_result)
4922 RRETURN(MATCH_NOMATCH);
4923 }
4924 /* Control never gets here */
4925
4926 case PT_WORD:
4927 for (fi = min;; fi++)
4928 {
4929 int category;
4930 RMATCH(eptr, ecode, offset_top, md, eptrb, RM62);
4931 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4932 if (fi >= max) RRETURN(MATCH_NOMATCH);
4933 if (eptr >= md->end_subject)
4934 {
4935 SCHECK_PARTIAL();
4936 RRETURN(MATCH_NOMATCH);
4937 }
4938 GETCHARINCTEST(c, eptr);
4939 category = UCD_CATEGORY(c);
4940 if ((category == ucp_L ||
4941 category == ucp_N ||
4942 c == CHAR_UNDERSCORE)
4943 == prop_fail_result)
4944 RRETURN(MATCH_NOMATCH);
4945 }
4946 /* Control never gets here */
4947
4948 /* This should never occur */
4949
4950 default:
4951 RRETURN(PCRE_ERROR_INTERNAL);
4952 }
4953 }
4954
4955 /* Match extended Unicode sequences. We will get here only if the
4956 support is in the binary; otherwise a compile-time error occurs. */
4957
4958 else if (ctype == OP_EXTUNI)
4959 {
4960 for (fi = min;; fi++)
4961 {
4962 RMATCH(eptr, ecode, offset_top, md, eptrb, RM41);
4963 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4964 if (fi >= max) RRETURN(MATCH_NOMATCH);
4965 if (eptr >= md->end_subject)
4966 {
4967 SCHECK_PARTIAL();
4968 RRETURN(MATCH_NOMATCH);
4969 }
4970 GETCHARINCTEST(c, eptr);
4971 if (UCD_CATEGORY(c) == ucp_M) RRETURN(MATCH_NOMATCH);
4972 while (eptr < md->end_subject)
4973 {
4974 int len = 1;
4975 if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
4976 if (UCD_CATEGORY(c) != ucp_M) break;
4977 eptr += len;
4978 }
4979 if (md->partial != 0 && eptr >= md->end_subject)
4980 {
4981 SCHECK_PARTIAL();
4982 }
4983 }
4984 }
4985 else
4986 #endif /* SUPPORT_UCP */
4987
4988 #ifdef SUPPORT_UTF
4989 if (utf)
4990 {
4991 for (fi = min;; fi++)
4992 {
4993 RMATCH(eptr, ecode, offset_top, md, eptrb, RM42);
4994 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
4995 if (fi >= max) RRETURN(MATCH_NOMATCH);
4996 if (eptr >= md->end_subject)
4997 {
4998 SCHECK_PARTIAL();
4999 RRETURN(MATCH_NOMATCH);
5000 }
5001 if (ctype == OP_ANY && IS_NEWLINE(eptr))
5002 RRETURN(MATCH_NOMATCH);
5003 GETCHARINC(c, eptr);
5004 switch(ctype)
5005 {
5006 case OP_ANY: /* This is the non-NL case */
5007 case OP_ALLANY:
5008 case OP_ANYBYTE:
5009 break;
5010
5011 case OP_ANYNL:
5012 switch(c)
5013 {
5014 default: RRETURN(MATCH_NOMATCH);
5015 case 0x000d:
5016 if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
5017 break;
5018 case 0x000a:
5019 break;
5020
5021 case 0x000b:
5022 case 0x000c:
5023 case 0x0085:
5024 case 0x2028:
5025 case 0x2029:
5026 if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
5027 break;
5028 }
5029 break;
5030
5031 case OP_NOT_HSPACE:
5032 switch(c)
5033 {
5034 default: break;
5035 case 0x09: /* HT */
5036 case 0x20: /* SPACE */
5037 case 0xa0: /* NBSP */
5038 case 0x1680: /* OGHAM SPACE MARK */
5039 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
5040 case 0x2000: /* EN QUAD */
5041 case 0x2001: /* EM QUAD */
5042 case 0x2002: /* EN SPACE */
5043 case 0x2003: /* EM SPACE */
5044 case 0x2004: /* THREE-PER-EM SPACE */
5045 case 0x2005: /* FOUR-PER-EM SPACE */
5046 case 0x2006: /* SIX-PER-EM SPACE */
5047 case 0x2007: /* FIGURE SPACE */
5048 case 0x2008: /* PUNCTUATION SPACE */
5049 case 0x2009: /* THIN SPACE */
5050 case 0x200A: /* HAIR SPACE */
5051 case 0x202f: /* NARROW NO-BREAK SPACE */
5052 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
5053 case 0x3000: /* IDEOGRAPHIC SPACE */
5054 RRETURN(MATCH_NOMATCH);
5055 }
5056 break;
5057
5058 case OP_HSPACE:
5059 switch(c)
5060 {
5061 default: RRETURN(MATCH_NOMATCH);
5062 case 0x09: /* HT */
5063 case 0x20: /* SPACE */
5064 case 0xa0: /* NBSP */
5065 case 0x1680: /* OGHAM SPACE MARK */
5066 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
5067 case 0x2000: /* EN QUAD */
5068 case 0x2001: /* EM QUAD */
5069 case 0x2002: /* EN SPACE */
5070 case 0x2003: /* EM SPACE */
5071 case 0x2004: /* THREE-PER-EM SPACE */
5072 case 0x2005: /* FOUR-PER-EM SPACE */
5073 case 0x2006: /* SIX-PER-EM SPACE */
5074 case 0x2007: /* FIGURE SPACE */
5075 case 0x2008: /* PUNCTUATION SPACE */
5076 case 0x2009: /* THIN SPACE */
5077 case 0x200A: /* HAIR SPACE */
5078 case 0x202f: /* NARROW NO-BREAK SPACE */
5079 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
5080 case 0x3000: /* IDEOGRAPHIC SPACE */
5081 break;
5082 }
5083 break;
5084
5085 case OP_NOT_VSPACE:
5086 switch(c)
5087 {
5088 default: break;
5089 case 0x0a: /* LF */
5090 case 0x0b: /* VT */
5091 case 0x0c: /* FF */
5092 case 0x0d: /* CR */
5093 case 0x85: /* NEL */
5094 case 0x2028: /* LINE SEPARATOR */
5095 case 0x2029: /* PARAGRAPH SEPARATOR */
5096 RRETURN(MATCH_NOMATCH);
5097 }
5098 break;
5099
5100 case OP_VSPACE:
5101 switch(c)
5102 {
5103 default: RRETURN(MATCH_NOMATCH);
5104 case 0x0a: /* LF */
5105 case 0x0b: /* VT */
5106 case 0x0c: /* FF */
5107 case 0x0d: /* CR */
5108 case 0x85: /* NEL */
5109 case 0x2028: /* LINE SEPARATOR */
5110 case 0x2029: /* PARAGRAPH SEPARATOR */
5111 break;
5112 }
5113 break;
5114
5115 case OP_NOT_DIGIT:
5116 if (c < 256 && (md->ctypes[c] & ctype_digit) != 0)
5117 RRETURN(MATCH_NOMATCH);
5118 break;
5119
5120 case OP_DIGIT:
5121 if (c >= 256 || (md->ctypes[c] & ctype_digit) == 0)
5122 RRETURN(MATCH_NOMATCH);
5123 break;
5124
5125 case OP_NOT_WHITESPACE:
5126 if (c < 256 && (md->ctypes[c] & ctype_space) != 0)
5127 RRETURN(MATCH_NOMATCH);
5128 break;
5129
5130 case OP_WHITESPACE:
5131 if (c >= 256 || (md->ctypes[c] & ctype_space) == 0)
5132 RRETURN(MATCH_NOMATCH);
5133 break;
5134
5135 case OP_NOT_WORDCHAR:
5136 if (c < 256 && (md->ctypes[c] & ctype_word) != 0)
5137 RRETURN(MATCH_NOMATCH);
5138 break;
5139
5140 case OP_WORDCHAR:
5141 if (c >= 256 || (md->ctypes[c] & ctype_word) == 0)
5142 RRETURN(MATCH_NOMATCH);
5143 break;
5144
5145 default:
5146 RRETURN(PCRE_ERROR_INTERNAL);
5147 }
5148 }
5149 }
5150 else
5151 #endif
5152 /* Not UTF mode */
5153 {
5154 for (fi = min;; fi++)
5155 {
5156 RMATCH(eptr, ecode, offset_top, md, eptrb, RM43);
5157 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
5158 if (fi >= max) RRETURN(MATCH_NOMATCH);
5159 if (eptr >= md->end_subject)
5160 {
5161 SCHECK_PARTIAL();
5162 RRETURN(MATCH_NOMATCH);
5163 }
5164 if (ctype == OP_ANY && IS_NEWLINE(eptr))
5165 RRETURN(MATCH_NOMATCH);
5166 c = *eptr++;
5167 switch(ctype)
5168 {
5169 case OP_ANY: /* This is the non-NL case */
5170 case OP_ALLANY:
5171 case OP_ANYBYTE:
5172 break;
5173
5174 case OP_ANYNL:
5175 switch(c)
5176 {
5177 default: RRETURN(MATCH_NOMATCH);
5178 case 0x000d:
5179 if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
5180 break;
5181
5182 case 0x000a:
5183 break;
5184
5185 case 0x000b:
5186 case 0x000c:
5187 case 0x0085:
5188 #ifdef COMPILE_PCRE16
5189 case 0x2028:
5190 case 0x2029:
5191 #endif
5192 if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
5193 break;
5194 }
5195 break;
5196
5197 case OP_NOT_HSPACE:
5198 switch(c)
5199 {
5200 default: break;
5201 case 0x09: /* HT */
5202 case 0x20: /* SPACE */
5203 case 0xa0: /* NBSP */
5204 #ifdef COMPILE_PCRE16
5205 case 0x1680: /* OGHAM SPACE MARK */
5206 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
5207 case 0x2000: /* EN QUAD */
5208 case 0x2001: /* EM QUAD */
5209 case 0x2002: /* EN SPACE */
5210 case 0x2003: /* EM SPACE */
5211 case 0x2004: /* THREE-PER-EM SPACE */
5212 case 0x2005: /* FOUR-PER-EM SPACE */
5213 case 0x2006: /* SIX-PER-EM SPACE */
5214 case 0x2007: /* FIGURE SPACE */
5215 case 0x2008: /* PUNCTUATION SPACE */
5216 case 0x2009: /* THIN SPACE */
5217 case 0x200A: /* HAIR SPACE */
5218 case 0x202f: /* NARROW NO-BREAK SPACE */
5219 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
5220 case 0x3000: /* IDEOGRAPHIC SPACE */
5221 #endif
5222 RRETURN(MATCH_NOMATCH);
5223 }
5224 break;
5225
5226 case OP_HSPACE:
5227 switch(c)
5228 {
5229 default: RRETURN(MATCH_NOMATCH);
5230 case 0x09: /* HT */
5231 case 0x20: /* SPACE */
5232 case 0xa0: /* NBSP */
5233 #ifdef COMPILE_PCRE16
5234 case 0x1680: /* OGHAM SPACE MARK */
5235 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
5236 case 0x2000: /* EN QUAD */
5237 case 0x2001: /* EM QUAD */
5238 case 0x2002: /* EN SPACE */
5239 case 0x2003: /* EM SPACE */
5240 case 0x2004: /* THREE-PER-EM SPACE */
5241 case 0x2005: /* FOUR-PER-EM SPACE */
5242 case 0x2006: /* SIX-PER-EM SPACE */
5243 case 0x2007: /* FIGURE SPACE */
5244 case 0x2008: /* PUNCTUATION SPACE */
5245 case 0x2009: /* THIN SPACE */
5246 case 0x200A: /* HAIR SPACE */
5247 case 0x202f: /* NARROW NO-BREAK SPACE */
5248 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
5249 case 0x3000: /* IDEOGRAPHIC SPACE */
5250 #endif
5251 break;
5252 }
5253 break;
5254
5255 case OP_NOT_VSPACE:
5256 switch(c)
5257 {
5258 default: break;
5259 case 0x0a: /* LF */
5260 case 0x0b: /* VT */
5261 case 0x0c: /* FF */
5262 case 0x0d: /* CR */
5263 case 0x85: /* NEL */
5264 #ifdef COMPILE_PCRE16
5265 case 0x2028: /* LINE SEPARATOR */
5266 case 0x2029: /* PARAGRAPH SEPARATOR */
5267 #endif
5268 RRETURN(MATCH_NOMATCH);
5269 }
5270 break;
5271
5272 case OP_VSPACE:
5273 switch(c)
5274 {
5275 default: RRETURN(MATCH_NOMATCH);
5276 case 0x0a: /* LF */
5277 case 0x0b: /* VT */
5278 case 0x0c: /* FF */
5279 case 0x0d: /* CR */
5280 case 0x85: /* NEL */
5281 #ifdef COMPILE_PCRE16
5282 case 0x2028: /* LINE SEPARATOR */
5283 case 0x2029: /* PARAGRAPH SEPARATOR */
5284 #endif
5285 break;
5286 }
5287 break;
5288
5289 case OP_NOT_DIGIT:
5290 if (MAX_255(c) && (md->ctypes[c] & ctype_digit) != 0) RRETURN(MATCH_NOMATCH);
5291 break;
5292
5293 case OP_DIGIT:
5294 if (!MAX_255(c) || (md->ctypes[c] & ctype_digit) == 0) RRETURN(MATCH_NOMATCH);
5295 break;
5296
5297 case OP_NOT_WHITESPACE:
5298 if (MAX_255(c) && (md->ctypes[c] & ctype_space) != 0) RRETURN(MATCH_NOMATCH);
5299 break;
5300
5301 case OP_WHITESPACE:
5302 if (!MAX_255(c) || (md->ctypes[c] & ctype_space) == 0) RRETURN(MATCH_NOMATCH);
5303 break;
5304
5305 case OP_NOT_WORDCHAR:
5306 if (MAX_255(c) && (md->ctypes[c] & ctype_word) != 0) RRETURN(MATCH_NOMATCH);
5307 break;
5308
5309 case OP_WORDCHAR:
5310 if (!MAX_255(c) || (md->ctypes[c] & ctype_word) == 0) RRETURN(MATCH_NOMATCH);
5311 break;
5312
5313 default:
5314 RRETURN(PCRE_ERROR_INTERNAL);
5315 }
5316 }
5317 }
5318 /* Control never gets here */
5319 }
5320
5321 /* If maximizing, it is worth using inline code for speed, doing the type
5322 test once at the start (i.e. keep it out of the loop). Again, keep the
5323 UTF-8 and UCP stuff separate. */
5324
5325 else
5326 {
5327 pp = eptr; /* Remember where we started */
5328
5329 #ifdef SUPPORT_UCP
5330 if (prop_type >= 0)
5331 {
5332 switch(prop_type)
5333 {
5334 case PT_ANY:
5335 for (i = min; i < max; i++)
5336 {
5337 int len = 1;
5338 if (eptr >= md->end_subject)
5339 {
5340 SCHECK_PARTIAL();
5341 break;
5342 }
5343 GETCHARLENTEST(c, eptr, len);
5344 if (prop_fail_result) break;
5345 eptr+= len;
5346 }
5347 break;
5348
5349 case PT_LAMP:
5350 for (i = min; i < max; i++)
5351 {
5352 int chartype;
5353 int len = 1;
5354 if (eptr >= md->end_subject)
5355 {
5356 SCHECK_PARTIAL();
5357 break;
5358 }
5359 GETCHARLENTEST(c, eptr, len);
5360 chartype = UCD_CHARTYPE(c);
5361 if ((chartype == ucp_Lu ||
5362 chartype == ucp_Ll ||
5363 chartype == ucp_Lt) == prop_fail_result)
5364 break;
5365 eptr+= len;
5366 }
5367 break;
5368
5369 case PT_GC:
5370 for (i = min; i < max; i++)
5371 {
5372 int len = 1;
5373 if (eptr >= md->end_subject)
5374 {
5375 SCHECK_PARTIAL();
5376 break;
5377 }
5378 GETCHARLENTEST(c, eptr, len);
5379 if ((UCD_CATEGORY(c) == prop_value) == prop_fail_result) break;
5380 eptr+= len;
5381 }
5382 break;
5383
5384 case PT_PC:
5385 for (i = min; i < max; i++)
5386 {
5387 int len = 1;
5388 if (eptr >= md->end_subject)
5389 {
5390 SCHECK_PARTIAL();
5391 break;
5392 }
5393 GETCHARLENTEST(c, eptr, len);
5394 if ((UCD_CHARTYPE(c) == prop_value) == prop_fail_result) break;
5395 eptr+= len;
5396 }
5397 break;
5398
5399 case PT_SC:
5400 for (i = min; i < max; i++)
5401 {
5402 int len = 1;
5403 if (eptr >= md->end_subject)
5404 {
5405 SCHECK_PARTIAL();
5406 break;
5407 }
5408 GETCHARLENTEST(c, eptr, len);
5409 if ((UCD_SCRIPT(c) == prop_value) == prop_fail_result) break;
5410 eptr+= len;
5411 }
5412 break;
5413
5414 case PT_ALNUM:
5415 for (i = min; i < max; i++)
5416 {
5417 int category;
5418 int len = 1;
5419 if (eptr >= md->end_subject)
5420 {
5421 SCHECK_PARTIAL();
5422 break;
5423 }
5424 GETCHARLENTEST(c, eptr, len);
5425 category = UCD_CATEGORY(c);
5426 if ((category == ucp_L || category == ucp_N) == prop_fail_result)
5427 break;
5428 eptr+= len;
5429 }
5430 break;
5431
5432 case PT_SPACE: /* Perl space */
5433 for (i = min; i < max; i++)
5434 {
5435 int len = 1;
5436 if (eptr >= md->end_subject)
5437 {
5438 SCHECK_PARTIAL();
5439 break;
5440 }
5441 GETCHARLENTEST(c, eptr, len);
5442 if ((UCD_CATEGORY(c) == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
5443 c == CHAR_FF || c == CHAR_CR)
5444 == prop_fail_result)
5445 break;
5446 eptr+= len;
5447 }
5448 break;
5449
5450 case PT_PXSPACE: /* POSIX space */
5451 for (i = min; i < max; i++)
5452 {
5453 int len = 1;
5454 if (eptr >= md->end_subject)
5455 {
5456 SCHECK_PARTIAL();
5457 break;
5458 }
5459 GETCHARLENTEST(c, eptr, len);
5460 if ((UCD_CATEGORY(c) == ucp_Z || c == CHAR_HT || c == CHAR_NL ||
5461 c == CHAR_VT || c == CHAR_FF || c == CHAR_CR)
5462 == prop_fail_result)
5463 break;
5464 eptr+= len;
5465 }
5466 break;
5467
5468 case PT_WORD:
5469 for (i = min; i < max; i++)
5470 {
5471 int category;
5472 int len = 1;
5473 if (eptr >= md->end_subject)
5474 {
5475 SCHECK_PARTIAL();
5476 break;
5477 }
5478 GETCHARLENTEST(c, eptr, len);
5479 category = UCD_CATEGORY(c);
5480 if ((category == ucp_L || category == ucp_N ||
5481 c == CHAR_UNDERSCORE) == prop_fail_result)
5482 break;
5483 eptr+= len;
5484 }
5485 break;
5486
5487 default:
5488 RRETURN(PCRE_ERROR_INTERNAL);
5489 }
5490
5491 /* eptr is now past the end of the maximum run */
5492
5493 if (possessive) continue;
5494 for(;;)
5495 {
5496 RMATCH(eptr, ecode, offset_top, md, eptrb, RM44);
5497 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
5498 if (eptr-- == pp) break; /* Stop if tried at original pos */
5499 if (utf) BACKCHAR(eptr);
5500 }
5501 }
5502
5503 /* Match extended Unicode sequences. We will get here only if the
5504 support is in the binary; otherwise a compile-time error occurs. */
5505
5506 else if (ctype == OP_EXTUNI)
5507 {
5508 for (i = min; i < max; i++)
5509 {
5510 int len = 1;
5511 if (eptr >= md->end_subject)
5512 {
5513 SCHECK_PARTIAL();
5514 break;
5515 }
5516 if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
5517 if (UCD_CATEGORY(c) == ucp_M) break;
5518 eptr += len;
5519 while (eptr < md->end_subject)
5520 {
5521 len = 1;
5522 if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
5523 if (UCD_CATEGORY(c) != ucp_M) break;
5524 eptr += len;
5525 }
5526 if (eptr >= md->end_subject)
5527 {
5528 SCHECK_PARTIAL();
5529 }
5530 }
5531
5532 /* eptr is now past the end of the maximum run */
5533
5534 if (possessive) continue;
5535
5536 for(;;)
5537 {
5538 RMATCH(eptr, ecode, offset_top, md, eptrb, RM45);
5539 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
5540 if (eptr-- == pp) break; /* Stop if tried at original pos */
5541 for (;;) /* Move back over one extended */
5542 {
5543 if (!utf) c = *eptr; else
5544 {
5545 BACKCHAR(eptr);
5546 GETCHAR(c, eptr);
5547 }
5548 if (UCD_CATEGORY(c) != ucp_M) break;
5549 eptr--;
5550 }
5551 }
5552 }
5553
5554 else
5555 #endif /* SUPPORT_UCP */
5556
5557 #ifdef SUPPORT_UTF
5558 if (utf)
5559 {
5560 switch(ctype)
5561 {
5562 case OP_ANY:
5563 if (max < INT_MAX)
5564 {
5565 for (i = min; i < max; i++)
5566 {
5567 if (eptr >= md->end_subject)
5568 {
5569 SCHECK_PARTIAL();
5570 break;
5571 }
5572 if (IS_NEWLINE(eptr)) break;
5573 eptr++;
5574 ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
5575 }
5576 }
5577
5578 /* Handle unlimited UTF-8 repeat */
5579
5580 else
5581 {
5582 for (i = min; i < max; i++)
5583 {
5584 if (eptr >= md->end_subject)
5585 {
5586 SCHECK_PARTIAL();
5587 break;
5588 }
5589 if (IS_NEWLINE(eptr)) break;
5590 eptr++;
5591 ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
5592 }
5593 }
5594 break;
5595
5596 case OP_ALLANY:
5597 if (max < INT_MAX)
5598 {
5599 for (i = min; i < max; i++)
5600 {
5601 if (eptr >= md->end_subject)
5602 {
5603 SCHECK_PARTIAL();
5604 break;
5605 }
5606 eptr++;
5607 ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
5608 }
5609 }
5610 else
5611 {
5612 eptr = md->end_subject; /* Unlimited UTF-8 repeat */
5613 SCHECK_PARTIAL();
5614 }
5615 break;
5616
5617 /* The byte case is the same as non-UTF8 */
5618
5619 case OP_ANYBYTE:
5620 c = max - min;
5621 if (c > (unsigned int)(md->end_subject - eptr))
5622 {
5623 eptr = md->end_subject;
5624 SCHECK_PARTIAL();
5625 }
5626 else eptr += c;
5627 break;
5628
5629 case OP_ANYNL:
5630 for (i = min; i < max; i++)
5631 {
5632 int len = 1;
5633 if (eptr >= md->end_subject)
5634 {
5635 SCHECK_PARTIAL();
5636 break;
5637 }
5638 GETCHARLEN(c, eptr, len);
5639 if (c == 0x000d)
5640 {
5641 if (++eptr >= md->end_subject) break;
5642 if (*eptr == 0x000a) eptr++;
5643 }
5644 else
5645 {
5646 if (c != 0x000a &&
5647 (md->bsr_anycrlf ||
5648 (c != 0x000b && c != 0x000c &&
5649 c != 0x0085 && c != 0x2028 && c != 0x2029)))
5650 break;
5651 eptr += len;
5652 }
5653 }
5654 break;
5655
5656 case OP_NOT_HSPACE:
5657 case OP_HSPACE:
5658 for (i = min; i < max; i++)
5659 {
5660 BOOL gotspace;
5661 int len = 1;
5662 if (eptr >= md->end_subject)
5663 {
5664 SCHECK_PARTIAL();
5665 break;
5666 }
5667 GETCHARLEN(c, eptr, len);
5668 switch(c)
5669 {
5670 default: gotspace = FALSE; break;
5671 case 0x09: /* HT */
5672 case 0x20: /* SPACE */
5673 case 0xa0: /* NBSP */
5674 case 0x1680: /* OGHAM SPACE MARK */
5675 case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
5676 case 0x2000: /* EN QUAD */
5677 case 0x2001: /* EM QUAD */
5678 case 0x2002: /* EN SPACE */
5679 case 0x2003: /* EM SPACE */
5680 case 0x2004: /* THREE-PER-EM SPACE */
5681 case 0x2005: /* FOUR-PER-EM SPACE */
5682 case 0x2006: /* SIX-PER-EM SPACE */
5683 case 0x2007: /* FIGURE SPACE */
5684 case 0x2008: /* PUNCTUATION SPACE */
5685 case 0x2009: /* THIN SPACE */
5686 case 0x200A: /* HAIR SPACE */
5687 case 0x202f: /* NARROW NO-BREAK SPACE */
5688 case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
5689 case 0x3000: /* IDEOGRAPHIC SPACE */
5690 gotspace = TRUE;
5691 break;
5692 }
5693 if (gotspace == (ctype == OP_NOT_HSPACE)) break;
5694 eptr += len;
5695 }
5696 break;
5697
5698 case OP_NOT_VSPACE:
5699 case OP_VSPACE:
5700 for (i = min; i < max; i++)
5701 {
5702 BOOL gotspace;
5703 int len = 1;
5704 if (eptr >= md->end_subject)
5705 {
5706 SCHECK_PARTIAL();
5707 break;
5708 }
5709 GETCHARLEN(c, eptr, len);
5710 switch(c)
5711 {
5712 default: gotspace = FALSE; break;
5713 case 0x0a: /* LF */
5714 case 0x0b: /* VT */
5715 case 0x0c: /* FF */
5716 case 0x0d: /* CR */
5717 case 0x85: /* NEL */
5718 case 0x2028: /* LINE SEPARATOR */
5719 case 0x2029: /* PARAGRAPH SEPARATOR */
5720 gotspace = TRUE;
5721 break;
5722 }
5723 if (gotspace == (ctype == OP_NOT_VSPACE)) break;
5724 eptr += len;
5725 }
5726 break;
5727
5728 case OP_NOT_DIGIT:
5729 for (i = min; i < max; i++)
5730 {
5731 int len = 1;
5732 if (eptr >= md->end_subject)
5733 {
5734 SCHECK_PARTIAL();
5735 break;
5736 }
5737 GETCHARLEN(c, eptr, len);
5738 if (c < 256 && (md->ctypes[c] & ctype_digit) != 0) break;
5739 eptr+= len;
5740 }
5741 break;
5742
5743 case OP_DIGIT:
5744 for (i = min; i < max; i++)
5745 {
5746 int len = 1;
5747 if (eptr >= md->end_subject)
5748 {
5749 SCHECK_PARTIAL();
5750 break;
5751 }
5752 GETCHARLEN(c, eptr, len);
5753 if (c >= 256 ||(md->ctypes[c] & ctype_digit) == 0) break;
5754 eptr+= len;
5755 }
5756 break;
5757
5758 case OP_NOT_WHITESPACE:
5759 for (i = min; i < max; i++)
5760 {
5761 int len = 1;
5762 if (eptr >= md->end_subject)
5763 {
5764 SCHECK_PARTIAL();
5765 break;
5766 }
5767 GETCHARLEN(c, eptr, len);
5768 if (c < 256 && (md->ctypes[c] & ctype_space) != 0) break;
5769 eptr+= len;
5770 }
5771 break;
5772
5773 case OP_WHITESPACE:
5774 for (i = min; i < max; i++)
5775 {
5776 int len = 1;
5777 if (eptr >= md->end_subject)
5778 {
5779 SCHECK_PARTIAL();
5780 break;
5781 }
5782 GETCHARLEN(c, eptr, len);
5783 if (c >= 256 ||(md->ctypes[c] & ctype_space) == 0) break;
5784 eptr+= len;
5785 }
5786 break;
5787
5788 case OP_NOT_WORDCHAR:
5789 for (i = min; i < max; i++)
5790 {
5791 int len = 1;
5792 if (eptr >= md->end_subject)
5793 {
5794 SCHECK_PARTIAL();
5795 break;
5796 }
5797 GETCHARLEN(c, eptr, len);
5798 if (c < 256 && (md->ctypes[c] & ctype_word) != 0) break;
5799 eptr+= len;
5800 }
5801 break;
5802
5803 case OP_WORDCHAR:
5804 for (i = min; i < max; i++)
5805 {
5806 int len = 1;
5807 if (eptr >= md->end_subject)
5808 {
5809 SCHECK_PARTIAL();
5810 break;
5811 }
5812 GETCHARLEN(c, eptr, len);
5813 if (c >= 256 || (md->ctypes[c] & ctype_word) == 0) break;
5814 eptr+= len;
5815 }
5816 break;
5817
5818 default:
5819 RRETURN(PCRE_ERROR_INTERNAL);
5820 }
5821
5822 /* eptr is now past the end of the maximum run. If possessive, we are
5823 done (no backing up). Otherwise, match at this position; anything other
5824 than no match is immediately returned. For nomatch, back up one
5825 character, unless we are matching \R and the last thing matched was
5826 \r\n, in which case, back up two bytes. */
5827
5828 if (possessive) continue;
5829 for(;;)
5830 {
5831 RMATCH(eptr, ecode, offset_top, md, eptrb, RM46);
5832 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
5833 if (eptr-- == pp) break; /* Stop if tried at original pos */
5834 BACKCHAR(eptr);
5835 if (ctype == OP_ANYNL && eptr > pp && *eptr == '\n' &&
5836 eptr[-1] == '\r') eptr--;
5837 }
5838 }
5839 else
5840 #endif /* SUPPORT_UTF */
5841 /* Not UTF mode */
5842 {
5843 switch(ctype)
5844 {
5845 case OP_ANY:
5846 for (i = min; i < max; i++)
5847 {
5848 if (eptr >= md->end_subject)
5849 {
5850 SCHECK_PARTIAL();
5851 break;
5852 }
5853 if (IS_NEWLINE(eptr)) break;
5854 eptr++;
5855 }
5856 break;
5857
5858 case OP_ALLANY:
5859 case OP_ANYBYTE:
5860 c = max - min;
5861 if (c > (unsigned int)(md->end_subject - eptr))
5862 {
5863 eptr = md->end_subject;
5864 SCHECK_PARTIAL();
5865 }
5866 else eptr += c;
5867 break;
5868
5869 case OP_ANYNL:
5870 for (i = min; i < max; i++)
5871 {
5872 if (eptr >= md->end_subject)
5873 {
5874 SCHECK_PARTIAL();
5875 break;
5876 }
5877 c = *eptr;
5878 if (c == 0x000d)
5879 {
5880 if (++eptr >= md->end_subject) break;
5881 if (*eptr == 0x000a) eptr++;
5882 }
5883 else
5884 {
5885 if (c != 0x000a && (md->bsr_anycrlf ||
5886 (c != 0x000b && c != 0x000c && c != 0x0085
5887 #ifdef COMPILE_PCRE16
5888 && c != 0x2028 && c != 0x2029
5889 #endif
5890 ))) break;
5891 eptr++;
5892 }
5893 }
5894 break;
5895
5896 case OP_NOT_HSPACE:
5897 for (i = min; i < max; i++)
5898 {
5899 if (eptr >= md->end_subject)
5900 {
5901 SCHECK_PARTIAL();
5902 break;
5903 }
5904 c = *eptr;
5905 if (c == 0x09 || c == 0x20 || c == 0xa0
5906 #ifdef COMPILE_PCRE16
5907 || c == 0x1680 || c == 0x180e || (c >= 0x2000 && c <= 0x200A)
5908 || c == 0x202f || c == 0x205f || c == 0x3000
5909 #endif
5910 ) break;
5911 eptr++;
5912 }
5913 break;
5914
5915 case OP_HSPACE:
5916 for (i = min; i < max; i++)
5917 {
5918 if (eptr >= md->end_subject)
5919 {
5920 SCHECK_PARTIAL();
5921 break;
5922 }
5923 c = *eptr;
5924 if (c != 0x09 && c != 0x20 && c != 0xa0
5925 #ifdef COMPILE_PCRE16
5926 && c != 0x1680 && c != 0x180e && (c < 0x2000 || c > 0x200A)
5927 && c != 0x202f && c != 0x205f && c != 0x3000
5928 #endif
5929 ) break;
5930 eptr++;
5931 }
5932 break;
5933
5934 case OP_NOT_VSPACE:
5935 for (i = min; i < max; i++)
5936 {
5937 if (eptr >= md->end_subject)
5938 {
5939 SCHECK_PARTIAL();
5940 break;
5941 }
5942 c = *eptr;
5943 if (c == 0x0a || c == 0x0b || c == 0x0c || c == 0x0d || c == 0x85
5944 #ifdef COMPILE_PCRE16
5945 || c == 0x2028 || c == 0x2029
5946 #endif
5947 ) break;
5948 eptr++;
5949 }
5950 break;
5951
5952 case OP_VSPACE:
5953 for (i = min; i < max; i++)
5954 {
5955 if (eptr >= md->end_subject)
5956 {
5957 SCHECK_PARTIAL();
5958 break;
5959 }
5960 c = *eptr;
5961 if (c != 0x0a && c != 0x0b && c != 0x0c && c != 0x0d && c != 0x85
5962 #ifdef COMPILE_PCRE16
5963 && c != 0x2028 && c != 0x2029
5964 #endif
5965 ) break;
5966 eptr++;
5967 }
5968 break;
5969
5970 case OP_NOT_DIGIT:
5971 for (i = min; i < max; i++)
5972 {
5973 if (eptr >= md->end_subject)
5974 {
5975 SCHECK_PARTIAL();
5976 break;
5977 }
5978 if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_digit) != 0) break;
5979 eptr++;
5980 }
5981 break;
5982
5983 case OP_DIGIT:
5984 for (i = min; i < max; i++)
5985 {
5986 if (eptr >= md->end_subject)
5987 {
5988 SCHECK_PARTIAL();
5989 break;
5990 }
5991 if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_digit) == 0) break;
5992 eptr++;
5993 }
5994 break;
5995
5996 case OP_NOT_WHITESPACE:
5997 for (i = min; i < max; i++)
5998 {
5999 if (eptr >= md->end_subject)
6000 {
6001 SCHECK_PARTIAL();
6002 break;
6003 }
6004 if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_space) != 0) break;
6005 eptr++;
6006 }
6007 break;
6008
6009 case OP_WHITESPACE:
6010 for (i = min; i < max; i++)
6011 {
6012 if (eptr >= md->end_subject)
6013 {
6014 SCHECK_PARTIAL();
6015 break;
6016 }
6017 if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_space) == 0) break;
6018 eptr++;
6019 }
6020 break;
6021
6022 case OP_NOT_WORDCHAR:
6023 for (i = min; i < max; i++)
6024 {
6025 if (eptr >= md->end_subject)
6026 {
6027 SCHECK_PARTIAL();
6028 break;
6029 }
6030 if (MAX_255(*eptr) && (md->ctypes[*eptr] & ctype_word) != 0) break;
6031 eptr++;
6032 }
6033 break;
6034
6035 case OP_WORDCHAR:
6036 for (i = min; i < max; i++)
6037 {
6038 if (eptr >= md->end_subject)
6039 {
6040 SCHECK_PARTIAL();
6041 break;
6042 }
6043 if (!MAX_255(*eptr) || (md->ctypes[*eptr] & ctype_word) == 0) break;
6044 eptr++;
6045 }
6046 break;
6047
6048 default:
6049 RRETURN(PCRE_ERROR_INTERNAL);
6050 }
6051
6052 /* eptr is now past the end of the maximum run. If possessive, we are
6053 done (no backing up). Otherwise, match at this position; anything other
6054 than no match is immediately returned. For nomatch, back up one
6055 character (byte), unless we are matching \R and the last thing matched
6056 was \r\n, in which case, back up two bytes. */
6057
6058 if (possessive) continue;
6059 while (eptr >= pp)
6060 {
6061 RMATCH(eptr, ecode, offset_top, md, eptrb, RM47);
6062 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
6063 eptr--;
6064 if (ctype == OP_ANYNL && eptr > pp && *eptr == '\n' &&
6065 eptr[-1] == '\r') eptr--;
6066 }
6067 }
6068
6069 /* Get here if we can't make it match with any permitted repetitions */
6070
6071 RRETURN(MATCH_NOMATCH);
6072 }
6073 /* Control never gets here */
6074
6075 /* There's been some horrible disaster. Arrival here can only mean there is
6076 something seriously wrong in the code above or the OP_xxx definitions. */
6077
6078 default:
6079 DPRINTF(("Unknown opcode %d\n", *ecode));
6080 RRETURN(PCRE_ERROR_UNKNOWN_OPCODE);
6081 }
6082
6083 /* Do not stick any code in here without much thought; it is assumed
6084 that "continue" in the code above comes out to here to repeat the main
6085 loop. */
6086
6087 } /* End of main loop */
6088 /* Control never reaches here */
6089
6090
6091 /* When compiling to use the heap rather than the stack for recursive calls to
6092 match(), the RRETURN() macro jumps here. The number that is saved in
6093 frame->Xwhere indicates which label we actually want to return to. */
6094
6095 #ifdef NO_RECURSE
6096 #define LBL(val) case val: goto L_RM##val;
6097 HEAP_RETURN:
6098 switch (frame->Xwhere)
6099 {
6100 LBL( 1) LBL( 2) LBL( 3) LBL( 4) LBL( 5) LBL( 6) LBL( 7) LBL( 8)
6101 LBL( 9) LBL(10) LBL(11) LBL(12) LBL(13) LBL(14) LBL(15) LBL(17)
6102 LBL(19) LBL(24) LBL(25) LBL(26) LBL(27) LBL(29) LBL(31) LBL(33)
6103 LBL(35) LBL(43) LBL(47) LBL(48) LBL(49) LBL(50) LBL(51) LBL(52)
6104 LBL(53) LBL(54) LBL(55) LBL(56) LBL(57) LBL(58) LBL(63) LBL(64)
6105 LBL(65) LBL(66)
6106 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
6107 LBL(21)
6108 #endif
6109 #ifdef SUPPORT_UTF
6110 LBL(16) LBL(18) LBL(20)
6111 LBL(22) LBL(23) LBL(28) LBL(30)
6112 LBL(32) LBL(34) LBL(42) LBL(46)
6113 #ifdef SUPPORT_UCP
6114 LBL(36) LBL(37) LBL(38) LBL(39) LBL(40) LBL(41) LBL(44) LBL(45)
6115 LBL(59) LBL(60) LBL(61) LBL(62)
6116 #endif /* SUPPORT_UCP */
6117 #endif /* SUPPORT_UTF */
6118 default:
6119 DPRINTF(("jump error in pcre match: label %d non-existent\n", frame->Xwhere));
6120
6121 printf("+++jump error in pcre match: label %d non-existent\n", frame->Xwhere);
6122
6123 return PCRE_ERROR_INTERNAL;
6124 }
6125 #undef LBL
6126 #endif /* NO_RECURSE */
6127 }
6128
6129
6130 /***************************************************************************
6131 ****************************************************************************
6132 RECURSION IN THE match() FUNCTION
6133
6134 Undefine all the macros that were defined above to handle this. */
6135
6136 #ifdef NO_RECURSE
6137 #undef eptr
6138 #undef ecode
6139 #undef mstart
6140 #undef offset_top
6141 #undef eptrb
6142 #undef flags
6143
6144 #undef callpat
6145 #undef charptr
6146 #undef data
6147 #undef next
6148 #undef pp
6149 #undef prev
6150 #undef saved_eptr
6151
6152 #undef new_recursive
6153
6154 #undef cur_is_word
6155 #undef condition
6156 #undef prev_is_word
6157
6158 #undef ctype
6159 #undef length
6160 #undef max
6161 #undef min
6162 #undef number
6163 #undef offset
6164 #undef op
6165 #undef save_capture_last
6166 #undef save_offset1
6167 #undef save_offset2
6168 #undef save_offset3
6169 #undef stacksave
6170
6171 #undef newptrb
6172
6173 #endif
6174
6175 /* These two are defined as macros in both cases */
6176
6177 #undef fc
6178 #undef fi
6179
6180 /***************************************************************************
6181 ***************************************************************************/
6182
6183
6184
6185 /*************************************************
6186 * Execute a Regular Expression *
6187 *************************************************/
6188
6189 /* This function applies a compiled re to a subject string and picks out
6190 portions of the string if it matches. Two elements in the vector are set for
6191 each substring: the offsets to the start and end of the substring.
6192
6193 Arguments:
6194 argument_re points to the compiled expression
6195 extra_data points to extra data or is NULL
6196 subject points to the subject string
6197 length length of subject string (may contain binary zeros)
6198 start_offset where to start in the subject string
6199 options option bits
6200 offsets points to a vector of ints to be filled in with offsets
6201 offsetcount the number of elements in the vector
6202
6203 Returns: > 0 => success; value is the number of elements filled in
6204 = 0 => success, but offsets is not big enough
6205 -1 => failed to match
6206 < -1 => some kind of unexpected problem
6207 */
6208
6209 #ifdef COMPILE_PCRE8
6210 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
6211 pcre_exec(const pcre *argument_re, const pcre_extra *extra_data,
6212 PCRE_SPTR subject, int length, int start_offset, int options, int *offsets,
6213 int offsetcount)
6214 #else
6215 PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
6216 pcre16_exec(const pcre16 *argument_re, const pcre16_extra *extra_data,
6217 PCRE_SPTR16 subject, int length, int start_offset, int options, int *offsets,
6218 int offsetcount)
6219 #endif
6220 {
6221 int rc, ocount, arg_offset_max;
6222 int newline;
6223 BOOL using_temporary_offsets = FALSE;
6224 BOOL anchored;
6225 BOOL startline;
6226 BOOL firstline;
6227 BOOL utf;
6228 BOOL has_first_char = FALSE;
6229 BOOL has_req_char = FALSE;
6230 pcre_uchar first_char = 0;
6231 pcre_uchar first_char2 = 0;
6232 pcre_uchar req_char = 0;
6233 pcre_uchar req_char2 = 0;
6234 match_data match_block;
6235 match_data *md = &match_block;
6236 const pcre_uint8 *tables;
6237 const pcre_uint8 *start_bits = NULL;
6238 PCRE_PUCHAR start_match = (PCRE_PUCHAR)subject + start_offset;
6239 PCRE_PUCHAR end_subject;
6240 PCRE_PUCHAR start_partial = NULL;
6241 PCRE_PUCHAR req_char_ptr = start_match - 1;
6242
6243 const pcre_study_data *study;
6244 const REAL_PCRE *re = (const REAL_PCRE *)argument_re;
6245
6246 /* Check for the special magic call that measures the size of the stack used
6247 per recursive call of match(). Without the funny casting for sizeof, a Windows
6248 compiler gave this error: "unary minus operator applied to unsigned type,
6249 result still unsigned". Hopefully the cast fixes that. */
6250
6251 if (re == NULL && extra_data == NULL && subject == NULL && length == -999 &&
6252 start_offset == -999)
6253 #ifdef NO_RECURSE
6254 return -((int)sizeof(heapframe));
6255 #else
6256 return match(NULL, NULL, NULL, 0, NULL, NULL, 0);
6257 #endif
6258
6259 /* Plausibility checks */
6260
6261 if ((options & ~PUBLIC_EXEC_OPTIONS) != 0) return PCRE_ERROR_BADOPTION;
6262 if (re == NULL || subject == NULL || (offsets == NULL && offsetcount > 0))
6263 return PCRE_ERROR_NULL;
6264 if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
6265 if (start_offset < 0 || start_offset > length) return PCRE_ERROR_BADOFFSET;
6266
6267 /* Check that the first field in the block is the magic number. If it is not,
6268 return with PCRE_ERROR_BADMAGIC. However, if the magic number is equal to
6269 REVERSED_MAGIC_NUMBER we return with PCRE_ERROR_BADENDIANNESS, which
6270 means that the pattern is likely compiled with different endianness. */
6271
6272 if (re->magic_number != MAGIC_NUMBER)
6273 return re->magic_number == REVERSED_MAGIC_NUMBER?
6274 PCRE_ERROR_BADENDIANNESS:PCRE_ERROR_BADMAGIC;
6275 if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
6276
6277 /* These two settings are used in the code for checking a UTF-8 string that
6278 follows immediately afterwards. Other values in the md block are used only
6279 during "normal" pcre_exec() processing, not when the JIT support is in use,
6280 so they are set up later. */
6281
6282 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
6283 utf = md->utf = (re->options & PCRE_UTF8) != 0;
6284 md->partial = ((options & PCRE_PARTIAL_HARD) != 0)? 2 :
6285 ((options & PCRE_PARTIAL_SOFT) != 0)? 1 : 0;
6286
6287 /* Check a UTF-8 string if required. Pass back the character offset and error
6288 code for an invalid string if a results vector is available. */
6289
6290 #ifdef SUPPORT_UTF
6291 if (utf && (options & PCRE_NO_UTF8_CHECK) == 0)
6292 {
6293 int erroroffset;
6294 int errorcode = PRIV(valid_utf)((PCRE_PUCHAR)subject, length, &erroroffset);
6295 if (errorcode != 0)
6296 {
6297 if (offsetcount >= 2)
6298 {
6299 offsets[0] = erroroffset;
6300 offsets[1] = errorcode;
6301 }
6302 #ifdef COMPILE_PCRE16
6303 return (errorcode <= PCRE_UTF16_ERR1 && md->partial > 1)?
6304 PCRE_ERROR_SHORTUTF16 : PCRE_ERROR_BADUTF16;
6305 #else
6306 return (errorcode <= PCRE_UTF8_ERR5 && md->partial > 1)?
6307 PCRE_ERROR_SHORTUTF8 : PCRE_ERROR_BADUTF8;
6308 #endif
6309 }
6310
6311 /* Check that a start_offset points to the start of a UTF character. */
6312 if (start_offset > 0 && start_offset < length &&
6313 NOT_FIRSTCHAR(((PCRE_PUCHAR)subject)[start_offset]))
6314 return PCRE_ERROR_BADUTF8_OFFSET;
6315 }
6316 #endif
6317
6318 /* If the pattern was successfully studied with JIT support, run the JIT
6319 executable instead of the rest of this function. Most options must be set at
6320 compile time for the JIT code to be usable. Fallback to the normal code path if
6321 an unsupported flag is set. In particular, JIT does not support partial
6322 matching. */
6323
6324 #ifdef SUPPORT_JIT
6325 if (extra_data != NULL
6326 && (extra_data->flags & (PCRE_EXTRA_EXECUTABLE_JIT |
6327 PCRE_EXTRA_TABLES)) == PCRE_EXTRA_EXECUTABLE_JIT
6328 && extra_data->executable_jit != NULL
6329 && (options & ~(PCRE_NO_UTF8_CHECK | PCRE_NOTBOL | PCRE_NOTEOL |
6330 PCRE_NOTEMPTY | PCRE_NOTEMPTY_ATSTART |
6331 PCRE_PARTIAL_SOFT | PCRE_PARTIAL_HARD)) == 0)
6332 {
6333 rc = PRIV(jit_exec)(re, extra_data->executable_jit,
6334 (const pcre_uchar *)subject, length, start_offset, options,
6335 ((extra_data->flags & PCRE_EXTRA_MATCH_LIMIT) == 0)
6336 ? MATCH_LIMIT : extra_data->match_limit, offsets, offsetcount);
6337 /* PCRE_ERROR_NULL means that the selected normal or partial matching
6338 mode is not compiled. In this case we simply fallback to interpreter. */
6339 if (rc != PCRE_ERROR_NULL)
6340 return rc;
6341 }
6342 #endif
6343
6344 /* Carry on with non-JIT matching. This information is for finding all the
6345 numbers associated with a given name, for condition testing. */
6346
6347 md->name_table = (pcre_uchar *)re + re->name_table_offset;
6348 md->name_count = re->name_count;
6349 md->name_entry_size = re->name_entry_size;
6350
6351 /* Fish out the optional data from the extra_data structure, first setting
6352 the default values. */
6353
6354 study = NULL;
6355 md->match_limit = MATCH_LIMIT;
6356 md->match_limit_recursion = MATCH_LIMIT_RECURSION;
6357 md->callout_data = NULL;
6358
6359 /* The table pointer is always in native byte order. */
6360
6361 tables = re->tables;
6362
6363 if (extra_data != NULL)
6364 {
6365 register unsigned int flags = extra_data->flags;
6366 if ((flags & PCRE_EXTRA_STUDY_DATA) != 0)
6367 study = (const pcre_study_data *)extra_data->study_data;
6368 if ((flags & PCRE_EXTRA_MATCH_LIMIT) != 0)
6369 md->match_limit = extra_data->match_limit;
6370 if ((flags & PCRE_EXTRA_MATCH_LIMIT_RECURSION) != 0)
6371 md->match_limit_recursion = extra_data->match_limit_recursion;
6372 if ((flags & PCRE_EXTRA_CALLOUT_DATA) != 0)
6373 md->callout_data = extra_data->callout_data;
6374 if ((flags & PCRE_EXTRA_TABLES) != 0) tables = extra_data->tables;
6375 }
6376
6377 /* If the exec call supplied NULL for tables, use the inbuilt ones. This
6378 is a feature that makes it possible to save compiled regex and re-use them
6379 in other programs later. */
6380
6381 if (tables == NULL) tables = PRIV(default_tables);
6382
6383 /* Set up other data */
6384
6385 anchored = ((re->options | options) & PCRE_ANCHORED) != 0;
6386 startline = (re->flags & PCRE_STARTLINE) != 0;
6387 firstline = (re->options & PCRE_FIRSTLINE) != 0;
6388
6389 /* The code starts after the real_pcre block and the capture name table. */
6390
6391 md->start_code = (const pcre_uchar *)re + re->name_table_offset +
6392 re->name_count * re->name_entry_size;
6393
6394 md->start_subject = (PCRE_PUCHAR)subject;
6395 md->start_offset = start_offset;
6396 md->end_subject = md->start_subject + length;
6397 end_subject = md->end_subject;
6398
6399 md->endonly = (re->options & PCRE_DOLLAR_ENDONLY) != 0;
6400 md->use_ucp = (re->options & PCRE_UCP) != 0;
6401 md->jscript_compat = (re->options & PCRE_JAVASCRIPT_COMPAT) != 0;
6402 md->ignore_skip_arg = FALSE;
6403
6404 /* Some options are unpacked into BOOL variables in the hope that testing
6405 them will be faster than individual option bits. */
6406
6407 md->notbol = (options & PCRE_NOTBOL) != 0;
6408 md->noteol = (options & PCRE_NOTEOL) != 0;
6409 md->notempty = (options & PCRE_NOTEMPTY) != 0;
6410 md->notempty_atstart = (options & PCRE_NOTEMPTY_ATSTART) != 0;
6411
6412 md->hitend = FALSE;
6413 md->mark = md->nomatch_mark = NULL; /* In case never set */
6414
6415 md->recursive = NULL; /* No recursion at top level */
6416 md->hasthen = (re->flags & PCRE_HASTHEN) != 0;
6417
6418 md->lcc = tables + lcc_offset;
6419 md->fcc = tables + fcc_offset;
6420 md->ctypes = tables + ctypes_offset;
6421
6422 /* Handle different \R options. */
6423
6424 switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
6425 {
6426 case 0:
6427 if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0)
6428 md->bsr_anycrlf = (re->options & PCRE_BSR_ANYCRLF) != 0;
6429 else
6430 #ifdef BSR_ANYCRLF
6431 md->bsr_anycrlf = TRUE;
6432 #else
6433 md->bsr_anycrlf = FALSE;
6434 #endif
6435 break;
6436
6437 case PCRE_BSR_ANYCRLF:
6438 md->bsr_anycrlf = TRUE;
6439 break;
6440
6441 case PCRE_BSR_UNICODE:
6442 md->bsr_anycrlf = FALSE;
6443 break;
6444
6445 default: return PCRE_ERROR_BADNEWLINE;
6446 }
6447
6448 /* Handle different types of newline. The three bits give eight cases. If
6449 nothing is set at run time, whatever was used at compile time applies. */
6450
6451 switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options :
6452 (pcre_uint32)options) & PCRE_NEWLINE_BITS)
6453 {
6454 case 0: newline = NEWLINE; break; /* Compile-time default */
6455 case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
6456 case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
6457 case PCRE_NEWLINE_CR+
6458 PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
6459 case PCRE_NEWLINE_ANY: newline = -1; break;
6460 case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
6461 default: return PCRE_ERROR_BADNEWLINE;
6462 }
6463
6464 if (newline == -2)
6465 {
6466 md->nltype = NLTYPE_ANYCRLF;
6467 }
6468 else if (newline < 0)
6469 {
6470 md->nltype = NLTYPE_ANY;
6471 }
6472 else
6473 {
6474 md->nltype = NLTYPE_FIXED;
6475 if (newline > 255)
6476 {
6477 md->nllen = 2;
6478 md->nl[0] = (newline >> 8) & 255;
6479 md->nl[1] = newline & 255;
6480 }
6481 else
6482 {
6483 md->nllen = 1;
6484 md->nl[0] = newline;
6485 }
6486 }
6487
6488 /* Partial matching was originally supported only for a restricted set of
6489 regexes; from release 8.00 there are no restrictions, but the bits are still
6490 defined (though never set). So there's no harm in leaving this code. */
6491
6492 if (md->partial && (re->flags & PCRE_NOPARTIAL) != 0)
6493 return PCRE_ERROR_BADPARTIAL;
6494
6495 /* If the expression has got more back references than the offsets supplied can
6496 hold, we get a temporary chunk of working store to use during the matching.
6497 Otherwise, we can use the vector supplied, rounding down its size to a multiple
6498 of 3. */
6499
6500 ocount = offsetcount - (offsetcount % 3);
6501 arg_offset_max = (2*ocount)/3;
6502
6503 if (re->top_backref > 0 && re->top_backref >= ocount/3)
6504 {
6505 ocount = re->top_backref * 3 + 3;
6506 md->offset_vector = (int *)(PUBL(malloc))(ocount * sizeof(int));
6507 if (md->offset_vector == NULL) return PCRE_ERROR_NOMEMORY;
6508 using_temporary_offsets = TRUE;
6509 DPRINTF(("Got memory to hold back references\n"));
6510 }
6511 else md->offset_vector = offsets;
6512
6513 md->offset_end = ocount;
6514 md->offset_max = (2*ocount)/3;
6515 md->offset_overflow = FALSE;
6516 md->capture_last = -1;
6517
6518 /* Reset the working variable associated with each extraction. These should
6519 never be used unless previously set, but they get saved and restored, and so we
6520 initialize them to avoid reading uninitialized locations. Also, unset the
6521 offsets for the matched string. This is really just for tidiness with callouts,
6522 in case they inspect these fields. */
6523
6524 if (md->offset_vector != NULL)
6525 {
6526 register int *iptr = md->offset_vector + ocount;
6527 register int *iend = iptr - re->top_bracket;
6528 if (iend < md->offset_vector + 2) iend = md->offset_vector + 2;
6529 while (--iptr >= iend) *iptr = -1;
6530 md->offset_vector[0] = md->offset_vector[1] = -1;
6531 }
6532
6533 /* Set up the first character to match, if available. The first_char value is
6534 never set for an anchored regular expression, but the anchoring may be forced
6535 at run time, so we have to test for anchoring. The first char may be unset for
6536 an unanchored pattern, of course. If there's no first char and the pattern was
6537 studied, there may be a bitmap of possible first characters. */
6538
6539 if (!anchored)
6540 {
6541 if ((re->flags & PCRE_FIRSTSET) != 0)
6542 {
6543 has_first_char = TRUE;
6544 first_char = first_char2 = (pcre_uchar)(re->first_char);
6545 if ((re->flags & PCRE_FCH_CASELESS) != 0)
6546 {
6547 first_char2 = TABLE_GET(first_char, md->fcc, first_char);
6548 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
6549 if (utf && first_char > 127)
6550 first_char2 = UCD_OTHERCASE(first_char);
6551 #endif
6552 }
6553 }
6554 else
6555 if (!startline && study != NULL &&
6556 (study->flags & PCRE_STUDY_MAPPED) != 0)
6557 start_bits = study->start_bits;
6558 }
6559
6560 /* For anchored or unanchored matches, there may be a "last known required
6561 character" set. */
6562
6563 if ((re->flags & PCRE_REQCHSET) != 0)
6564 {
6565 has_req_char = TRUE;
6566 req_char = req_char2 = (pcre_uchar)(re->req_char);
6567 if ((re->flags & PCRE_RCH_CASELESS) != 0)
6568 {
6569 req_char2 = TABLE_GET(req_char, md->fcc, req_char);
6570 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
6571 if (utf && req_char > 127)
6572 req_char2 = UCD_OTHERCASE(req_char);
6573 #endif
6574 }
6575 }
6576
6577
6578 /* ==========================================================================*/
6579
6580 /* Loop for handling unanchored repeated matching attempts; for anchored regexs
6581 the loop runs just once. */
6582
6583 for(;;)
6584 {
6585 PCRE_PUCHAR save_end_subject = end_subject;
6586 PCRE_PUCHAR new_start_match;
6587
6588 /* If firstline is TRUE, the start of the match is constrained to the first
6589 line of a multiline string. That is, the match must be before or at the first
6590 newline. Implement this by temporarily adjusting end_subject so that we stop
6591 scanning at a newline. If the match fails at the newline, later code breaks
6592 this loop. */
6593
6594 if (firstline)
6595 {
6596 PCRE_PUCHAR t = start_match;
6597 #ifdef SUPPORT_UTF
6598 if (utf)
6599 {
6600 while (t < md->end_subject && !IS_NEWLINE(t))
6601 {
6602 t++;
6603 ACROSSCHAR(t < end_subject, *t, t++);
6604 }
6605 }
6606 else
6607 #endif
6608 while (t < md->end_subject && !IS_NEWLINE(t)) t++;
6609 end_subject = t;
6610 }
6611
6612 /* There are some optimizations that avoid running the match if a known
6613 starting point is not found, or if a known later character is not present.
6614 However, there is an option that disables these, for testing and for ensuring
6615 that all callouts do actually occur. The option can be set in the regex by
6616 (*NO_START_OPT) or passed in match-time options. */
6617
6618 if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0)
6619 {
6620 /* Advance to a unique first char if there is one. */
6621
6622 if (has_first_char)
6623 {
6624 if (first_char != first_char2)
6625 while (start_match < end_subject &&
6626 *start_match != first_char && *start_match != first_char2)
6627 start_match++;
6628 else
6629 while (start_match < end_subject && *start_match != first_char)
6630 start_match++;
6631 }
6632
6633 /* Or to just after a linebreak for a multiline match */
6634
6635 else if (startline)
6636 {
6637 if (start_match > md->start_subject + start_offset)
6638 {
6639 #ifdef SUPPORT_UTF
6640 if (utf)
6641 {
6642 while (start_match < end_subject && !WAS_NEWLINE(start_match))
6643 {
6644 start_match++;
6645 ACROSSCHAR(start_match < end_subject, *start_match,
6646 start_match++);
6647 }
6648 }
6649 else
6650 #endif
6651 while (start_match < end_subject && !WAS_NEWLINE(start_match))
6652 start_match++;
6653
6654 /* If we have just passed a CR and the newline option is ANY or ANYCRLF,
6655 and we are now at a LF, advance the match position by one more character.
6656 */
6657
6658 if (start_match[-1] == CHAR_CR &&
6659 (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&
6660 start_match < end_subject &&
6661 *start_match == CHAR_NL)
6662 start_match++;
6663 }
6664 }
6665
6666 /* Or to a non-unique first byte after study */
6667
6668 else if (start_bits != NULL)
6669 {
6670 while (start_match < end_subject)
6671 {
6672 register unsigned int c = *start_match;
6673 #ifndef COMPILE_PCRE8
6674 if (c > 255) c = 255;
6675 #endif
6676 if ((start_bits[c/8] & (1 << (c&7))) == 0)
6677 {
6678 start_match++;
6679 #if defined SUPPORT_UTF && defined COMPILE_PCRE8
6680 /* In non 8-bit mode, the iteration will stop for
6681 characters > 255 at the beginning or not stop at all. */
6682 if (utf)
6683 ACROSSCHAR(start_match < end_subject, *start_match,
6684 start_match++);
6685 #endif
6686 }
6687 else break;
6688 }
6689 }
6690 } /* Starting optimizations */
6691
6692 /* Restore fudged end_subject */
6693
6694 end_subject = save_end_subject;
6695
6696 /* The following two optimizations are disabled for partial matching or if
6697 disabling is explicitly requested. */
6698
6699 if (((options | re->options) & PCRE_NO_START_OPTIMIZE) == 0 && !md->partial)
6700 {
6701 /* If the pattern was studied, a minimum subject length may be set. This is
6702 a lower bound; no actual string of that length may actually match the
6703 pattern. Although the value is, strictly, in characters, we treat it as
6704 bytes to avoid spending too much time in this optimization. */
6705
6706 if (study != NULL && (study->flags & PCRE_STUDY_MINLEN) != 0 &&
6707 (pcre_uint32)(end_subject - start_match) < study->minlength)
6708 {
6709 rc = MATCH_NOMATCH;
6710 break;
6711 }
6712
6713 /* If req_char is set, we know that that character must appear in the
6714 subject for the match to succeed. If the first character is set, req_char
6715 must be later in the subject; otherwise the test starts at the match point.
6716 This optimization can save a huge amount of backtracking in patterns with
6717 nested unlimited repeats that aren't going to match. Writing separate code
6718 for cased/caseless versions makes it go faster, as does using an
6719 autoincrement and backing off on a match.
6720
6721 HOWEVER: when the subject string is very, very long, searching to its end
6722 can take a long time, and give bad performance on quite ordinary patterns.
6723 This showed up when somebody was matching something like /^\d+C/ on a
6724 32-megabyte string... so we don't do this when the string is sufficiently
6725 long. */
6726
6727 if (has_req_char && end_subject - start_match < REQ_BYTE_MAX)
6728 {
6729 register PCRE_PUCHAR p = start_match + (has_first_char? 1:0);
6730
6731 /* We don't need to repeat the search if we haven't yet reached the
6732 place we found it at last time. */
6733
6734 if (p > req_char_ptr)
6735 {
6736 if (req_char != req_char2)
6737 {
6738 while (p < end_subject)
6739 {
6740 register int pp = *p++;
6741 if (pp == req_char || pp == req_char2) { p--; break; }
6742 }
6743 }
6744 else
6745 {
6746 while (p < end_