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