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