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

Contents of /code/trunk/pcre_exec.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 916 - (hide annotations) (download)
Wed Feb 15 09:50:53 2012 UTC (15 months ago) by ph10
File MIME type: text/plain
File size: 214824 byte(s)
Fix several partial matching bugs for backrefs, \R, \X, and CRLF line endings. 

1 nigel 77 /*************************************************
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 ph10 836 Copyright (c) 1997-2012 University of Cambridge
10 nigel 77
11     -----------------------------------------------------------------------------
12     Redistribution and use in source and binary forms, with or without
13     modification, are permitted provided that the following conditions are met:
14    
15     * Redistributions of source code must retain the above copyright notice,
16     this list of conditions and the following disclaimer.
17    
18     * Redistributions in binary form must reproduce the above copyright
19     notice, this list of conditions and the following disclaimer in the
20     documentation and/or other materials provided with the distribution.
21    
22     * Neither the name of the University of Cambridge nor the names of its
23     contributors may be used to endorse or promote products derived from
24     this software without specific prior written permission.
25    
26     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36     POSSIBILITY OF SUCH DAMAGE.
37     -----------------------------------------------------------------------------
38     */
39    
40    
41     /* This module contains pcre_exec(), the externally visible function that does
42     pattern matching using an NFA algorithm, trying to mimic Perl as closely as
43     possible. There are also some static supporting functions. */
44    
45 ph10 200 #ifdef HAVE_CONFIG_H
46 ph10 236 #include "config.h"
47 ph10 200 #endif
48 ph10 199
49 nigel 93 #define NLBLOCK md /* Block containing newline information */
50     #define PSSTART start_subject /* Field containing processed string start */
51     #define PSEND end_subject /* Field containing processed string end */
52    
53 nigel 77 #include "pcre_internal.h"
54    
55 ph10 137 /* Undefine some potentially clashing cpp symbols */
56    
57     #undef min
58     #undef max
59    
60 ph10 625 /* Values for setting in md->match_function_type to indicate two special types
61     of call to match(). We do it this way to save on using another stack variable,
62 ph10 604 as stack usage is to be discouraged. */
63 nigel 77
64 ph10 604 #define MATCH_CONDASSERT 1 /* Called to check a condition assertion */
65     #define MATCH_CBEGROUP 2 /* Could-be-empty unlimited repeat group */
66 nigel 77
67     /* Non-error returns from the match() function. Error returns are externally
68     defined PCRE_ERROR_xxx codes, which are all negative. */
69    
70     #define MATCH_MATCH 1
71     #define MATCH_NOMATCH 0
72    
73 ph10 211 /* Special internal returns from the match() function. Make them sufficiently
74 ph10 210 negative to avoid the external error codes. */
75    
76 ph10 511 #define MATCH_ACCEPT (-999)
77     #define MATCH_COMMIT (-998)
78 ph10 604 #define MATCH_KETRPOS (-997)
79 ph10 618 #define MATCH_ONCE (-996)
80     #define MATCH_PRUNE (-995)
81     #define MATCH_SKIP (-994)
82     #define MATCH_SKIP_ARG (-993)
83     #define MATCH_THEN (-992)
84 ph10 210
85 nigel 77 /* Maximum number of ints of offset to save on the stack for recursive calls.
86     If the offset vector is bigger, malloc is used. This should be a multiple of 3,
87     because the offset vector is always a multiple of 3 long. */
88    
89     #define REC_STACK_SAVE_MAX 30
90    
91     /* Min and max values for the common repeats; for the maxima, 0 => infinity */
92    
93     static const char rep_min[] = { 0, 0, 1, 1, 0, 0 };
94     static const char rep_max[] = { 0, 0, 0, 0, 1, 1 };
95    
96    
97    
98 ph10 475 #ifdef PCRE_DEBUG
99 nigel 77 /*************************************************
100     * Debugging function to print chars *
101     *************************************************/
102    
103     /* Print a sequence of chars in printable format, stopping at the end of the
104     subject if the requested.
105    
106     Arguments:
107     p points to characters
108     length number to print
109     is_subject TRUE if printing from within md->start_subject
110     md pointer to matching data block, if is_subject is TRUE
111    
112     Returns: nothing
113     */
114    
115     static void
116 ph10 836 pchars(const pcre_uchar *p, int length, BOOL is_subject, match_data *md)
117 nigel 77 {
118 nigel 93 unsigned int c;
119 nigel 77 if (is_subject && length > md->end_subject - p) length = md->end_subject - p;
120     while (length-- > 0)
121     if (isprint(c = *(p++))) printf("%c", c); else printf("\\x%02x", c);
122     }
123     #endif
124    
125    
126    
127     /*************************************************
128     * Match a back-reference *
129     *************************************************/
130    
131 ph10 595 /* Normally, if a back reference hasn't been set, the length that is passed is
132     negative, so the match always fails. However, in JavaScript compatibility mode,
133 ph10 625 the length passed is zero. Note that in caseless UTF-8 mode, the number of
134 ph10 595 subject bytes matched may be different to the number of reference bytes.
135 nigel 77
136     Arguments:
137     offset index into the offset vector
138 ph10 595 eptr pointer into the subject
139     length length of reference to be matched (number of bytes)
140 nigel 77 md points to match data block
141 ph10 602 caseless TRUE if caseless
142 nigel 77
143 ph10 916 Returns: >= 0 the number of subject bytes matched
144     -1 no match
145     -2 partial match; always given if at end subject
146 nigel 77 */
147    
148 ph10 595 static int
149 ph10 836 match_ref(int offset, register PCRE_PUCHAR eptr, int length, match_data *md,
150 ph10 602 BOOL caseless)
151 nigel 77 {
152 ph10 916 PCRE_PUCHAR eptr_start = eptr;
153 ph10 836 register PCRE_PUCHAR p = md->start_subject + md->offset_vector[offset];
154 nigel 77
155 ph10 475 #ifdef PCRE_DEBUG
156 nigel 77 if (eptr >= md->end_subject)
157     printf("matching subject <null>");
158     else
159     {
160     printf("matching subject ");
161     pchars(eptr, length, TRUE, md);
162     }
163     printf(" against backref ");
164     pchars(p, length, FALSE, md);
165     printf("\n");
166     #endif
167    
168 ph10 916 /* Always fail if reference not set (and not JavaScript compatible - in that
169     case the length is passed as zero). */
170 nigel 77
171 ph10 595 if (length < 0) return -1;
172 nigel 77
173 ph10 354 /* Separate the caseless case for speed. In UTF-8 mode we can only do this
174     properly if Unicode properties are supported. Otherwise, we can check only
175     ASCII characters. */
176 nigel 77
177 ph10 602 if (caseless)
178 nigel 77 {
179 ph10 836 #ifdef SUPPORT_UTF
180 ph10 354 #ifdef SUPPORT_UCP
181 ph10 836 if (md->utf)
182 ph10 354 {
183 ph10 625 /* Match characters up to the end of the reference. NOTE: the number of
184 ph10 595 bytes matched may differ, because there are some characters whose upper and
185     lower case versions code as different numbers of bytes. For example, U+023A
186     (2 bytes in UTF-8) is the upper case version of U+2C65 (3 bytes in UTF-8);
187     a sequence of 3 of the former uses 6 bytes, as does a sequence of two of
188 ph10 625 the latter. It is important, therefore, to check the length along the
189 ph10 595 reference, not along the subject (earlier code did this wrong). */
190 ph10 625
191 ph10 836 PCRE_PUCHAR endptr = p + length;
192 ph10 595 while (p < endptr)
193 ph10 354 {
194 ph10 358 int c, d;
195 ph10 916 if (eptr >= md->end_subject) return -2; /* Partial match */
196 ph10 354 GETCHARINC(c, eptr);
197     GETCHARINC(d, p);
198 ph10 595 if (c != d && c != UCD_OTHERCASE(d)) return -1;
199 ph10 358 }
200     }
201 ph10 354 else
202     #endif
203     #endif
204    
205     /* The same code works when not in UTF-8 mode and in UTF-8 mode when there
206     is no UCP support. */
207 ph10 597 {
208     while (length-- > 0)
209 ph10 836 {
210 ph10 916 if (eptr >= md->end_subject) return -2; /* Partial match */
211 ph10 836 if (TABLE_GET(*p, md->lcc, *p) != TABLE_GET(*eptr, md->lcc, *eptr)) return -1;
212     p++;
213     eptr++;
214     }
215 ph10 625 }
216 nigel 77 }
217 ph10 358
218 ph10 354 /* In the caseful case, we can just compare the bytes, whether or not we
219     are in UTF-8 mode. */
220 ph10 358
221 nigel 77 else
222 ph10 625 {
223 ph10 916 while (length-- > 0)
224 zherczeg 915 {
225 ph10 916 if (eptr >= md->end_subject) return -2; /* Partial match */
226     if (*p++ != *eptr++) return -1;
227     }
228 ph10 597 }
229 nigel 77
230 ph10 916 return (int)(eptr - eptr_start);
231 nigel 77 }
232    
233    
234    
235     /***************************************************************************
236     ****************************************************************************
237     RECURSION IN THE match() FUNCTION
238    
239 nigel 87 The match() function is highly recursive, though not every recursive call
240     increases the recursive depth. Nevertheless, some regular expressions can cause
241     it to recurse to a great depth. I was writing for Unix, so I just let it call
242     itself recursively. This uses the stack for saving everything that has to be
243     saved for a recursive call. On Unix, the stack can be large, and this works
244     fine.
245 nigel 77
246 nigel 87 It turns out that on some non-Unix-like systems there are problems with
247     programs that use a lot of stack. (This despite the fact that every last chip
248     has oodles of memory these days, and techniques for extending the stack have
249     been known for decades.) So....
250 nigel 77
251     There is a fudge, triggered by defining NO_RECURSE, which avoids recursive
252     calls by keeping local variables that need to be preserved in blocks of memory
253 nigel 87 obtained from malloc() instead instead of on the stack. Macros are used to
254 nigel 77 achieve this so that the actual code doesn't look very different to what it
255     always used to.
256 ph10 164
257 ph10 165 The original heap-recursive code used longjmp(). However, it seems that this
258 ph10 164 can be very slow on some operating systems. Following a suggestion from Stan
259     Switzer, the use of longjmp() has been abolished, at the cost of having to
260     provide a unique number for each call to RMATCH. There is no way of generating
261     a sequence of numbers at compile time in C. I have given them names, to make
262     them stand out more clearly.
263    
264     Crude tests on x86 Linux show a small speedup of around 5-8%. However, on
265     FreeBSD, avoiding longjmp() more than halves the time taken to run the standard
266 ph10 165 tests. Furthermore, not using longjmp() means that local dynamic variables
267     don't have indeterminate values; this has meant that the frame size can be
268 ph10 164 reduced because the result can be "passed back" by straight setting of the
269     variable instead of being passed in the frame.
270 nigel 77 ****************************************************************************
271     ***************************************************************************/
272    
273 ph10 212 /* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN
274     below must be updated in sync. */
275 nigel 77
276 ph10 164 enum { RM1=1, RM2, RM3, RM4, RM5, RM6, RM7, RM8, RM9, RM10,
277     RM11, RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,
278     RM21, RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
279     RM31, RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
280 ph10 210 RM41, RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
281 ph10 527 RM51, RM52, RM53, RM54, RM55, RM56, RM57, RM58, RM59, RM60,
282 ph10 723 RM61, RM62, RM63, RM64, RM65, RM66 };
283 ph10 164
284 nigel 87 /* These versions of the macros use the stack, as normal. There are debugging
285 ph10 165 versions and production versions. Note that the "rw" argument of RMATCH isn't
286 ph10 501 actually used in this definition. */
287 nigel 77
288     #ifndef NO_RECURSE
289     #define REGISTER register
290 ph10 164
291 ph10 475 #ifdef PCRE_DEBUG
292 ph10 604 #define RMATCH(ra,rb,rc,rd,re,rw) \
293 nigel 87 { \
294     printf("match() called in line %d\n", __LINE__); \
295 ph10 836 rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1); \
296 nigel 87 printf("to line %d\n", __LINE__); \
297     }
298     #define RRETURN(ra) \
299     { \
300     printf("match() returned %d from line %d ", ra, __LINE__); \
301     return ra; \
302     }
303     #else
304 ph10 604 #define RMATCH(ra,rb,rc,rd,re,rw) \
305 ph10 836 rrc = match(ra,rb,mstart,rc,rd,re,rdepth+1)
306 nigel 77 #define RRETURN(ra) return ra
307 nigel 87 #endif
308    
309 nigel 77 #else
310    
311    
312 ph10 164 /* These versions of the macros manage a private stack on the heap. Note that
313     the "rd" argument of RMATCH isn't actually used in this definition. It's the md
314     argument of match(), which never changes. */
315 nigel 77
316     #define REGISTER
317    
318 ph10 604 #define RMATCH(ra,rb,rc,rd,re,rw)\
319 nigel 77 {\
320 ph10 836 heapframe *newframe = (heapframe *)(PUBL(stack_malloc))(sizeof(heapframe));\
321 ph10 534 if (newframe == NULL) RRETURN(PCRE_ERROR_NOMEMORY);\
322 ph10 164 frame->Xwhere = rw; \
323     newframe->Xeptr = ra;\
324     newframe->Xecode = rb;\
325 ph10 168 newframe->Xmstart = mstart;\
326 ph10 164 newframe->Xoffset_top = rc;\
327 ph10 602 newframe->Xeptrb = re;\
328 ph10 164 newframe->Xrdepth = frame->Xrdepth + 1;\
329     newframe->Xprevframe = frame;\
330     frame = newframe;\
331     DPRINTF(("restarting from line %d\n", __LINE__));\
332     goto HEAP_RECURSE;\
333     L_##rw:\
334     DPRINTF(("jumped back to line %d\n", __LINE__));\
335 nigel 77 }
336    
337     #define RRETURN(ra)\
338     {\
339 ph10 527 heapframe *oldframe = frame;\
340     frame = oldframe->Xprevframe;\
341 ph10 892 if (oldframe != &frame_zero) (PUBL(stack_free))(oldframe);\
342 nigel 77 if (frame != NULL)\
343     {\
344 ph10 164 rrc = ra;\
345     goto HEAP_RETURN;\
346 nigel 77 }\
347     return ra;\
348     }
349    
350    
351     /* Structure for remembering the local variables in a private frame */
352    
353     typedef struct heapframe {
354     struct heapframe *Xprevframe;
355    
356     /* Function arguments that may change */
357    
358 ph10 836 PCRE_PUCHAR Xeptr;
359     const pcre_uchar *Xecode;
360     PCRE_PUCHAR Xmstart;
361 nigel 77 int Xoffset_top;
362     eptrblock *Xeptrb;
363 nigel 91 unsigned int Xrdepth;
364 nigel 77
365     /* Function local variables */
366    
367 ph10 836 PCRE_PUCHAR Xcallpat;
368     #ifdef SUPPORT_UTF
369     PCRE_PUCHAR Xcharptr;
370 ph10 406 #endif
371 ph10 836 PCRE_PUCHAR Xdata;
372     PCRE_PUCHAR Xnext;
373     PCRE_PUCHAR Xpp;
374     PCRE_PUCHAR Xprev;
375     PCRE_PUCHAR Xsaved_eptr;
376 nigel 77
377     recursion_info Xnew_recursive;
378    
379     BOOL Xcur_is_word;
380     BOOL Xcondition;
381     BOOL Xprev_is_word;
382    
383     #ifdef SUPPORT_UCP
384     int Xprop_type;
385 nigel 87 int Xprop_value;
386 nigel 77 int Xprop_fail_result;
387 ph10 123 int Xoclength;
388 ph10 836 pcre_uchar Xocchars[6];
389 nigel 77 #endif
390    
391 ph10 403 int Xcodelink;
392 nigel 77 int Xctype;
393 nigel 93 unsigned int Xfc;
394 nigel 77 int Xfi;
395     int Xlength;
396     int Xmax;
397     int Xmin;
398     int Xnumber;
399     int Xoffset;
400     int Xop;
401     int Xsave_capture_last;
402     int Xsave_offset1, Xsave_offset2, Xsave_offset3;
403     int Xstacksave[REC_STACK_SAVE_MAX];
404    
405     eptrblock Xnewptrb;
406    
407 ph10 164 /* Where to jump back to */
408 nigel 77
409 ph10 164 int Xwhere;
410 ph10 165
411 nigel 77 } heapframe;
412    
413     #endif
414    
415    
416     /***************************************************************************
417     ***************************************************************************/
418    
419    
420    
421     /*************************************************
422     * Match from current position *
423     *************************************************/
424    
425 nigel 93 /* This function is called recursively in many circumstances. Whenever it
426 nigel 77 returns a negative (error) response, the outer incarnation must also return the
427 ph10 426 same response. */
428 nigel 77
429 ph10 426 /* These macros pack up tests that are used for partial matching, and which
430 ph10 836 appear several times in the code. We set the "hit end" flag if the pointer is
431 ph10 426 at the end of the subject and also past the start of the subject (i.e.
432 ph10 427 something has been matched). For hard partial matching, we then return
433     immediately. The second one is used when we already know we are past the end of
434     the subject. */
435 ph10 426
436     #define CHECK_PARTIAL()\
437 ph10 553 if (md->partial != 0 && eptr >= md->end_subject && \
438     eptr > md->start_used_ptr) \
439     { \
440     md->hitend = TRUE; \
441 ph10 836 if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
442 ph10 427 }
443 ph10 426
444     #define SCHECK_PARTIAL()\
445 ph10 553 if (md->partial != 0 && eptr > md->start_used_ptr) \
446     { \
447     md->hitend = TRUE; \
448 ph10 836 if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); \
449 ph10 427 }
450 ph10 426
451 ph10 427
452 ph10 426 /* Performance note: It might be tempting to extract commonly used fields from
453 ph10 836 the md structure (e.g. utf, end_subject) into individual variables to improve
454 nigel 77 performance. Tests using gcc on a SPARC disproved this; in the first case, it
455     made performance worse.
456    
457     Arguments:
458 nigel 93 eptr pointer to current character in subject
459     ecode pointer to current position in compiled code
460 ph10 168 mstart pointer to the current match start position (can be modified
461 ph10 172 by encountering \K)
462 nigel 77 offset_top current top pointer
463     md pointer to "static" info for the match
464     eptrb pointer to chain of blocks containing eptr at start of
465     brackets - for testing for empty matches
466 nigel 87 rdepth the recursion depth
467 nigel 77
468     Returns: MATCH_MATCH if matched ) these values are >= 0
469     MATCH_NOMATCH if failed to match )
470 ph10 510 a negative MATCH_xxx value for PRUNE, SKIP, etc
471 nigel 77 a negative PCRE_ERROR_xxx value if aborted by an error condition
472 nigel 87 (e.g. stopped by repeated call or recursion limit)
473 nigel 77 */
474    
475     static int
476 ph10 836 match(REGISTER PCRE_PUCHAR eptr, REGISTER const pcre_uchar *ecode,
477 ph10 842 PCRE_PUCHAR mstart, int offset_top, match_data *md, eptrblock *eptrb,
478 ph10 835 unsigned int rdepth)
479 nigel 77 {
480     /* These variables do not need to be preserved over recursion in this function,
481 nigel 93 so they can be ordinary variables in all cases. Mark some of them with
482     "register" because they are used a lot in loops. */
483 nigel 77
484 nigel 91 register int rrc; /* Returns from recursive calls */
485     register int i; /* Used for loops not involving calls to RMATCH() */
486 nigel 93 register unsigned int c; /* Character values not kept over RMATCH() calls */
487 ph10 836 register BOOL utf; /* Local copy of UTF flag for speed */
488 nigel 77
489 nigel 93 BOOL minimize, possessive; /* Quantifier options */
490 ph10 602 BOOL caseless;
491 ph10 403 int condcode;
492 nigel 93
493 nigel 77 /* When recursion is not being used, all "local" variables that have to be
494 ph10 892 preserved over calls to RMATCH() are part of a "frame". We set up the top-level
495     frame on the stack here; subsequent instantiations are obtained from the heap
496 ph10 903 whenever RMATCH() does a "recursion". See the macro definitions above. Putting
497     the top-level on the stack rather than malloc-ing them all gives a performance
498 ph10 892 boost in many cases where there is not much "recursion". */
499 nigel 77
500     #ifdef NO_RECURSE
501 ph10 903 heapframe frame_zero;
502     heapframe *frame = &frame_zero;
503 nigel 77 frame->Xprevframe = NULL; /* Marks the top level */
504    
505     /* Copy in the original argument variables */
506    
507     frame->Xeptr = eptr;
508     frame->Xecode = ecode;
509 ph10 168 frame->Xmstart = mstart;
510 nigel 77 frame->Xoffset_top = offset_top;
511     frame->Xeptrb = eptrb;
512 nigel 87 frame->Xrdepth = rdepth;
513 nigel 77
514     /* This is where control jumps back to to effect "recursion" */
515    
516     HEAP_RECURSE:
517    
518     /* Macros make the argument variables come from the current frame */
519    
520     #define eptr frame->Xeptr
521     #define ecode frame->Xecode
522 ph10 168 #define mstart frame->Xmstart
523 nigel 77 #define offset_top frame->Xoffset_top
524     #define eptrb frame->Xeptrb
525 nigel 87 #define rdepth frame->Xrdepth
526 nigel 77
527     /* Ditto for the local variables */
528    
529 ph10 836 #ifdef SUPPORT_UTF
530 nigel 77 #define charptr frame->Xcharptr
531     #endif
532     #define callpat frame->Xcallpat
533 ph10 403 #define codelink frame->Xcodelink
534 nigel 77 #define data frame->Xdata
535     #define next frame->Xnext
536     #define pp frame->Xpp
537     #define prev frame->Xprev
538     #define saved_eptr frame->Xsaved_eptr
539    
540     #define new_recursive frame->Xnew_recursive
541    
542     #define cur_is_word frame->Xcur_is_word
543     #define condition frame->Xcondition
544     #define prev_is_word frame->Xprev_is_word
545    
546     #ifdef SUPPORT_UCP
547     #define prop_type frame->Xprop_type
548 nigel 87 #define prop_value frame->Xprop_value
549 nigel 77 #define prop_fail_result frame->Xprop_fail_result
550 ph10 115 #define oclength frame->Xoclength
551     #define occhars frame->Xocchars
552 nigel 77 #endif
553    
554     #define ctype frame->Xctype
555     #define fc frame->Xfc
556     #define fi frame->Xfi
557     #define length frame->Xlength
558     #define max frame->Xmax
559     #define min frame->Xmin
560     #define number frame->Xnumber
561     #define offset frame->Xoffset
562     #define op frame->Xop
563     #define save_capture_last frame->Xsave_capture_last
564     #define save_offset1 frame->Xsave_offset1
565     #define save_offset2 frame->Xsave_offset2
566     #define save_offset3 frame->Xsave_offset3
567     #define stacksave frame->Xstacksave
568    
569     #define newptrb frame->Xnewptrb
570    
571     /* When recursion is being used, local variables are allocated on the stack and
572     get preserved during recursion in the normal way. In this environment, fi and
573     i, and fc and c, can be the same variables. */
574    
575 nigel 93 #else /* NO_RECURSE not defined */
576 nigel 77 #define fi i
577     #define fc c
578    
579 ph10 604 /* Many of the following variables are used only in small blocks of the code.
580     My normal style of coding would have declared them within each of those blocks.
581     However, in order to accommodate the version of this code that uses an external
582     "stack" implemented on the heap, it is easier to declare them all here, so the
583     declarations can be cut out in a block. The only declarations within blocks
584     below are for variables that do not have to be preserved over a recursive call
585     to RMATCH(). */
586 nigel 77
587 ph10 836 #ifdef SUPPORT_UTF
588     const pcre_uchar *charptr;
589 ph10 625 #endif
590 ph10 836 const pcre_uchar *callpat;
591     const pcre_uchar *data;
592     const pcre_uchar *next;
593     PCRE_PUCHAR pp;
594     const pcre_uchar *prev;
595     PCRE_PUCHAR saved_eptr;
596 ph10 625
597     recursion_info new_recursive;
598    
599     BOOL cur_is_word;
600 nigel 87 BOOL condition;
601 nigel 77 BOOL prev_is_word;
602    
603     #ifdef SUPPORT_UCP
604     int prop_type;
605 nigel 87 int prop_value;
606 nigel 77 int prop_fail_result;
607 ph10 115 int oclength;
608 ph10 836 pcre_uchar occhars[6];
609 nigel 77 #endif
610    
611 ph10 399 int codelink;
612 nigel 77 int ctype;
613     int length;
614     int max;
615     int min;
616     int number;
617     int offset;
618     int op;
619     int save_capture_last;
620     int save_offset1, save_offset2, save_offset3;
621     int stacksave[REC_STACK_SAVE_MAX];
622    
623     eptrblock newptrb;
624 ph10 893
625 ph10 903 /* There is a special fudge for calling match() in a way that causes it to
626 ph10 893 measure the size of its basic stack frame when the stack is being used for
627 ph10 895 recursion. The second argument (ecode) being NULL triggers this behaviour. It
628 ph10 901 cannot normally ever be NULL. The return is the negated value of the frame
629 ph10 895 size. */
630 ph10 893
631     if (ecode == NULL)
632     {
633     if (rdepth == 0)
634 ph10 895 return match((PCRE_PUCHAR)&rdepth, NULL, NULL, 0, NULL, NULL, 1);
635 ph10 893 else
636     {
637 ph10 895 int len = (char *)&rdepth - (char *)eptr;
638 ph10 893 return (len > 0)? -len : len;
639     }
640 ph10 903 }
641 nigel 93 #endif /* NO_RECURSE */
642 nigel 77
643 ph10 625 /* To save space on the stack and in the heap frame, I have doubled up on some
644     of the local variables that are used only in localised parts of the code, but
645     still need to be preserved over recursive calls of match(). These macros define
646 ph10 604 the alternative names that are used. */
647    
648     #define allow_zero cur_is_word
649     #define cbegroup condition
650     #define code_offset codelink
651     #define condassert condition
652     #define matched_once prev_is_word
653 ph10 836 #define foc number
654 ph10 882 #define save_mark data
655 ph10 604
656 nigel 77 /* These statements are here to stop the compiler complaining about unitialized
657     variables. */
658    
659     #ifdef SUPPORT_UCP
660 nigel 87 prop_value = 0;
661 nigel 77 prop_fail_result = 0;
662     #endif
663    
664 nigel 93
665 nigel 91 /* This label is used for tail recursion, which is used in a few cases even
666     when NO_RECURSE is not defined, in order to reduce the amount of stack that is
667     used. Thanks to Ian Taylor for noticing this possibility and sending the
668     original patch. */
669    
670     TAIL_RECURSE:
671    
672 nigel 87 /* OK, now we can get on with the real code of the function. Recursive calls
673     are specified by the macro RMATCH and RRETURN is used to return. When
674     NO_RECURSE is *not* defined, these just turn into a recursive call to match()
675 ph10 475 and a "return", respectively (possibly with some debugging if PCRE_DEBUG is
676 nigel 87 defined). However, RMATCH isn't like a function call because it's quite a
677     complicated macro. It has to be used in one particular way. This shouldn't,
678     however, impact performance when true recursion is being used. */
679 nigel 77
680 ph10 836 #ifdef SUPPORT_UTF
681     utf = md->utf; /* Local copy of the flag */
682 ph10 164 #else
683 ph10 836 utf = FALSE;
684 ph10 164 #endif
685    
686 nigel 87 /* First check that we haven't called match() too many times, or that we
687     haven't exceeded the recursive call limit. */
688    
689 nigel 77 if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
690 nigel 87 if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
691 nigel 77
692 nigel 93 /* At the start of a group with an unlimited repeat that may match an empty
693 ph10 625 string, the variable md->match_function_type is set to MATCH_CBEGROUP. It is
694     done this way to save having to use another function argument, which would take
695 ph10 604 up space on the stack. See also MATCH_CONDASSERT below.
696 nigel 77
697 ph10 604 When MATCH_CBEGROUP is set, add the current subject pointer to the chain of
698     such remembered pointers, to be checked when we hit the closing ket, in order
699     to break infinite loops that match no characters. When match() is called in
700     other circumstances, don't add to the chain. The MATCH_CBEGROUP feature must
701     NOT be used with tail recursion, because the memory block that is used is on
702     the stack, so a new one may be required for each match(). */
703    
704     if (md->match_function_type == MATCH_CBEGROUP)
705 nigel 77 {
706 ph10 197 newptrb.epb_saved_eptr = eptr;
707     newptrb.epb_prev = eptrb;
708     eptrb = &newptrb;
709 ph10 604 md->match_function_type = 0;
710 nigel 77 }
711    
712 nigel 93 /* Now start processing the opcodes. */
713 nigel 77
714     for (;;)
715     {
716 nigel 93 minimize = possessive = FALSE;
717 nigel 77 op = *ecode;
718 ph10 625
719 nigel 93 switch(op)
720     {
721 ph10 510 case OP_MARK:
722 ph10 836 md->nomatch_mark = ecode + 2;
723     md->mark = NULL; /* In case previously set by assertion */
724     RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
725 ph10 604 eptrb, RM55);
726 ph10 836 if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
727     md->mark == NULL) md->mark = ecode + 2;
728 ph10 512
729     /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
730     argument, and we must check whether that argument matches this MARK's
731     argument. It is passed back in md->start_match_ptr (an overloading of that
732     variable). If it does match, we reset that variable to the current subject
733     position and return MATCH_SKIP. Otherwise, pass back the return code
734 ph10 510 unaltered. */
735 ph10 512
736 ph10 836 else if (rrc == MATCH_SKIP_ARG &&
737     STRCMP_UC_UC(ecode + 2, md->start_match_ptr) == 0)
738 ph10 510 {
739     md->start_match_ptr = eptr;
740     RRETURN(MATCH_SKIP);
741     }
742     RRETURN(rrc);
743    
744 ph10 210 case OP_FAIL:
745 ph10 836 RRETURN(MATCH_NOMATCH);
746 ph10 211
747 ph10 551 /* COMMIT overrides PRUNE, SKIP, and THEN */
748 ph10 553
749 ph10 510 case OP_COMMIT:
750 ph10 836 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
751 ph10 604 eptrb, RM52);
752 ph10 551 if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE &&
753 ph10 553 rrc != MATCH_SKIP && rrc != MATCH_SKIP_ARG &&
754     rrc != MATCH_THEN)
755 ph10 551 RRETURN(rrc);
756 ph10 836 RRETURN(MATCH_COMMIT);
757 ph10 510
758 ph10 551 /* PRUNE overrides THEN */
759 ph10 553
760 ph10 210 case OP_PRUNE:
761 ph10 836 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
762 ph10 604 eptrb, RM51);
763 ph10 551 if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
764 ph10 836 RRETURN(MATCH_PRUNE);
765 ph10 211
766 ph10 510 case OP_PRUNE_ARG:
767 ph10 836 md->nomatch_mark = ecode + 2;
768     md->mark = NULL; /* In case previously set by assertion */
769     RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
770 ph10 604 eptrb, RM56);
771 ph10 836 if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
772     md->mark == NULL) md->mark = ecode + 2;
773 ph10 551 if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
774 ph10 510 RRETURN(MATCH_PRUNE);
775 ph10 211
776 ph10 551 /* SKIP overrides PRUNE and THEN */
777 ph10 553
778 ph10 210 case OP_SKIP:
779 ph10 836 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
780 ph10 604 eptrb, RM53);
781 ph10 553 if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
782 ph10 551 RRETURN(rrc);
783 ph10 211 md->start_match_ptr = eptr; /* Pass back current position */
784 ph10 836 RRETURN(MATCH_SKIP);
785 ph10 211
786 ph10 836 /* Note that, for Perl compatibility, SKIP with an argument does NOT set
787     nomatch_mark. There is a flag that disables this opcode when re-matching a
788     pattern that ended with a SKIP for which there was not a matching MARK. */
789    
790 ph10 510 case OP_SKIP_ARG:
791 ph10 836 if (md->ignore_skip_arg)
792     {
793     ecode += PRIV(OP_lengths)[*ecode] + ecode[1];
794     break;
795     }
796     RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
797 ph10 604 eptrb, RM57);
798 ph10 553 if (rrc != MATCH_NOMATCH && rrc != MATCH_PRUNE && rrc != MATCH_THEN)
799 ph10 551 RRETURN(rrc);
800 ph10 512
801     /* Pass back the current skip name by overloading md->start_match_ptr and
802     returning the special MATCH_SKIP_ARG return code. This will either be
803 ph10 836 caught by a matching MARK, or get to the top, where it causes a rematch
804     with the md->ignore_skip_arg flag set. */
805 ph10 512
806 ph10 510 md->start_match_ptr = ecode + 2;
807 ph10 512 RRETURN(MATCH_SKIP_ARG);
808 ph10 553
809 ph10 716 /* For THEN (and THEN_ARG) we pass back the address of the opcode, so that
810     the branch in which it occurs can be determined. Overload the start of
811     match pointer to do this. */
812 ph10 512
813 ph10 210 case OP_THEN:
814 ph10 836 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
815 ph10 604 eptrb, RM54);
816 ph10 210 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
817 ph10 716 md->start_match_ptr = ecode;
818 ph10 836 RRETURN(MATCH_THEN);
819 ph10 510
820     case OP_THEN_ARG:
821 ph10 836 md->nomatch_mark = ecode + 2;
822     md->mark = NULL; /* In case previously set by assertion */
823     RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top,
824 ph10 716 md, eptrb, RM58);
825 ph10 836 if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
826     md->mark == NULL) md->mark = ecode + 2;
827 ph10 510 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
828 ph10 733 md->start_match_ptr = ecode;
829 ph10 212 RRETURN(MATCH_THEN);
830 ph10 733
831 ph10 723 /* Handle an atomic group that does not contain any capturing parentheses.
832 ph10 733 This can be handled like an assertion. Prior to 8.13, all atomic groups
833     were handled this way. In 8.13, the code was changed as below for ONCE, so
834     that backups pass through the group and thereby reset captured values.
835     However, this uses a lot more stack, so in 8.20, atomic groups that do not
836     contain any captures generate OP_ONCE_NC, which can be handled in the old,
837 ph10 723 less stack intensive way.
838 ph10 211
839 ph10 723 Check the alternative branches in turn - the matching won't pass the KET
840     for this kind of subpattern. If any one branch matches, we carry on as at
841     the end of a normal bracket, leaving the subject pointer, but resetting
842     the start-of-match value in case it was changed by \K. */
843    
844     case OP_ONCE_NC:
845     prev = ecode;
846     saved_eptr = eptr;
847 ph10 903 save_mark = md->mark;
848 ph10 723 do
849     {
850     RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM64);
851     if (rrc == MATCH_MATCH) /* Note: _not_ MATCH_ACCEPT */
852     {
853     mstart = md->start_match_ptr;
854     break;
855     }
856     if (rrc == MATCH_THEN)
857     {
858     next = ecode + GET(ecode,1);
859 ph10 733 if (md->start_match_ptr < next &&
860 ph10 723 (*ecode == OP_ALT || *next == OP_ALT))
861     rrc = MATCH_NOMATCH;
862 ph10 733 }
863    
864 ph10 723 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
865     ecode += GET(ecode,1);
866 ph10 903 md->mark = save_mark;
867 ph10 723 }
868     while (*ecode == OP_ALT);
869    
870     /* If hit the end of the group (which could be repeated), fail */
871    
872     if (*ecode != OP_ONCE_NC && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
873    
874     /* Continue as from after the group, updating the offsets high water
875     mark, since extracts may have been taken. */
876    
877     do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
878    
879     offset_top = md->end_offset_top;
880     eptr = md->end_match_ptr;
881    
882     /* For a non-repeating ket, just continue at this level. This also
883     happens for a repeating ket if no characters were matched in the group.
884     This is the forcible breaking of infinite loops as implemented in Perl
885     5.005. */
886    
887     if (*ecode == OP_KET || eptr == saved_eptr)
888     {
889     ecode += 1+LINK_SIZE;
890     break;
891     }
892    
893     /* The repeating kets try the rest of the pattern or restart from the
894     preceding bracket, in the appropriate order. The second "call" of match()
895     uses tail recursion, to avoid using another stack frame. */
896    
897     if (*ecode == OP_KETRMIN)
898     {
899     RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM65);
900     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
901     ecode = prev;
902     goto TAIL_RECURSE;
903     }
904     else /* OP_KETRMAX */
905     {
906 ph10 733 md->match_function_type = MATCH_CBEGROUP;
907 ph10 723 RMATCH(eptr, prev, offset_top, md, eptrb, RM66);
908     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
909     ecode += 1 + LINK_SIZE;
910     goto TAIL_RECURSE;
911     }
912     /* Control never gets here */
913    
914 ph10 604 /* Handle a capturing bracket, other than those that are possessive with an
915     unlimited repeat. If there is space in the offset vector, save the current
916     subject position in the working slot at the top of the vector. We mustn't
917     change the current values of the data slot, because they may be set from a
918     previous iteration of this group, and be referred to by a reference inside
919 ph10 625 the group. A failure to match might occur after the group has succeeded,
920 ph10 617 if something later on doesn't match. For this reason, we need to restore
921     the working value and also the values of the final offsets, in case they
922     were set by a previous iteration of the same bracket.
923 nigel 77
924 nigel 93 If there isn't enough space in the offset vector, treat this as if it were
925     a non-capturing bracket. Don't worry about setting the flag for the error
926     case here; that is handled in the code for KET. */
927 nigel 77
928 nigel 93 case OP_CBRA:
929     case OP_SCBRA:
930     number = GET2(ecode, 1+LINK_SIZE);
931 nigel 77 offset = number << 1;
932 ph10 625
933 ph10 475 #ifdef PCRE_DEBUG
934 nigel 93 printf("start bracket %d\n", number);
935     printf("subject=");
936 nigel 77 pchars(eptr, 16, TRUE, md);
937     printf("\n");
938     #endif
939    
940     if (offset < md->offset_max)
941     {
942     save_offset1 = md->offset_vector[offset];
943     save_offset2 = md->offset_vector[offset+1];
944     save_offset3 = md->offset_vector[md->offset_end - number];
945     save_capture_last = md->capture_last;
946 ph10 903 save_mark = md->mark;
947 nigel 77
948     DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
949 ph10 531 md->offset_vector[md->offset_end - number] =
950 ph10 530 (int)(eptr - md->start_subject);
951 nigel 77
952 ph10 604 for (;;)
953 nigel 77 {
954 ph10 625 if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
955 ph10 836 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
956 ph10 604 eptrb, RM1);
957 ph10 618 if (rrc == MATCH_ONCE) break; /* Backing up through an atomic group */
958 ph10 733
959     /* If we backed up to a THEN, check whether it is within the current
960     branch by comparing the address of the THEN that is passed back with
961 ph10 716 the end of the branch. If it is within the current branch, and the
962     branch is one of two or more alternatives (it either starts or ends
963 ph10 733 with OP_ALT), we have reached the limit of THEN's action, so convert
964     the return code to NOMATCH, which will cause normal backtracking to
965 ph10 716 happen from now on. Otherwise, THEN is passed back to an outer
966 ph10 733 alternative. This implements Perl's treatment of parenthesized groups,
967     where a group not containing | does not affect the current alternative,
968 ph10 716 that is, (X) is NOT the same as (X|(*F)). */
969    
970     if (rrc == MATCH_THEN)
971     {
972     next = ecode + GET(ecode,1);
973 ph10 733 if (md->start_match_ptr < next &&
974 ph10 716 (*ecode == OP_ALT || *next == OP_ALT))
975     rrc = MATCH_NOMATCH;
976 ph10 733 }
977    
978 ph10 716 /* Anything other than NOMATCH is passed back. */
979    
980     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
981 nigel 77 md->capture_last = save_capture_last;
982     ecode += GET(ecode, 1);
983 ph10 882 md->mark = save_mark;
984 ph10 625 if (*ecode != OP_ALT) break;
985 nigel 77 }
986    
987     DPRINTF(("bracket %d failed\n", number));
988     md->offset_vector[offset] = save_offset1;
989     md->offset_vector[offset+1] = save_offset2;
990     md->offset_vector[md->offset_end - number] = save_offset3;
991 ph10 625
992 ph10 716 /* At this point, rrc will be one of MATCH_ONCE or MATCH_NOMATCH. */
993 nigel 77
994 ph10 716 RRETURN(rrc);
995 nigel 77 }
996    
997 ph10 197 /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
998     as a non-capturing bracket. */
999 nigel 77
1000 ph10 197 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1001     /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1002    
1003 nigel 93 DPRINTF(("insufficient capture room: treat as non-capturing\n"));
1004 nigel 77
1005 ph10 197 /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1006     /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1007    
1008 ph10 618 /* Non-capturing or atomic group, except for possessive with unlimited
1009 ph10 723 repeat and ONCE group with no captures. Loop for all the alternatives.
1010 ph10 708
1011 ph10 702 When we get to the final alternative within the brackets, we used to return
1012     the result of a recursive call to match() whatever happened so it was
1013     possible to reduce stack usage by turning this into a tail recursion,
1014     except in the case of a possibly empty group. However, now that there is
1015     the possiblity of (*THEN) occurring in the final alternative, this
1016     optimization is no longer always possible.
1017 ph10 625
1018 ph10 708 We can optimize if we know there are no (*THEN)s in the pattern; at present
1019     this is the best that can be done.
1020    
1021 ph10 625 MATCH_ONCE is returned when the end of an atomic group is successfully
1022     reached, but subsequent matching fails. It passes back up the tree (causing
1023     captured values to be reset) until the original atomic group level is
1024 ph10 618 reached. This is tested by comparing md->once_target with the start of the
1025     group. At this point, the return is converted into MATCH_NOMATCH so that
1026     previous backup points can be taken. */
1027 nigel 77
1028 ph10 618 case OP_ONCE:
1029 nigel 93 case OP_BRA:
1030     case OP_SBRA:
1031     DPRINTF(("start non-capturing bracket\n"));
1032 ph10 618
1033 nigel 91 for (;;)
1034 nigel 77 {
1035 ph10 618 if (op >= OP_SBRA || op == OP_ONCE) md->match_function_type = MATCH_CBEGROUP;
1036 ph10 702
1037     /* If this is not a possibly empty group, and there are no (*THEN)s in
1038 ph10 708 the pattern, and this is the final alternative, optimize as described
1039 ph10 702 above. */
1040    
1041     else if (!md->hasthen && ecode[GET(ecode, 1)] != OP_ALT)
1042     {
1043 ph10 836 ecode += PRIV(OP_lengths)[*ecode];
1044 ph10 702 goto TAIL_RECURSE;
1045 ph10 708 }
1046 ph10 702
1047     /* In all other cases, we have to make another call to match(). */
1048    
1049 ph10 882 save_mark = md->mark;
1050 ph10 836 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md, eptrb,
1051 ph10 604 RM2);
1052 ph10 903
1053 ph10 716 /* See comment in the code for capturing groups above about handling
1054     THEN. */
1055    
1056     if (rrc == MATCH_THEN)
1057 ph10 625 {
1058 ph10 716 next = ecode + GET(ecode,1);
1059 ph10 733 if (md->start_match_ptr < next &&
1060 ph10 716 (*ecode == OP_ALT || *next == OP_ALT))
1061     rrc = MATCH_NOMATCH;
1062 ph10 733 }
1063    
1064     if (rrc != MATCH_NOMATCH)
1065 ph10 716 {
1066 ph10 618 if (rrc == MATCH_ONCE)
1067     {
1068 ph10 836 const pcre_uchar *scode = ecode;
1069 ph10 618 if (*scode != OP_ONCE) /* If not at start, find it */
1070     {
1071     while (*scode == OP_ALT) scode += GET(scode, 1);
1072     scode -= GET(scode, 1);
1073 ph10 625 }
1074 ph10 618 if (md->once_target == scode) rrc = MATCH_NOMATCH;
1075 ph10 625 }
1076 ph10 550 RRETURN(rrc);
1077 ph10 625 }
1078 nigel 77 ecode += GET(ecode, 1);
1079 ph10 903 md->mark = save_mark;
1080 ph10 625 if (*ecode != OP_ALT) break;
1081 nigel 77 }
1082 ph10 733
1083 ph10 609 RRETURN(MATCH_NOMATCH);
1084    
1085 ph10 625 /* Handle possessive capturing brackets with an unlimited repeat. We come
1086 ph10 604 here from BRAZERO with allow_zero set TRUE. The offset_vector values are
1087     handled similarly to the normal case above. However, the matching is
1088     different. The end of these brackets will always be OP_KETRPOS, which
1089     returns MATCH_KETRPOS without going further in the pattern. By this means
1090     we can handle the group by iteration rather than recursion, thereby
1091     reducing the amount of stack needed. */
1092 ph10 625
1093 ph10 604 case OP_CBRAPOS:
1094     case OP_SCBRAPOS:
1095     allow_zero = FALSE;
1096 ph10 625
1097 ph10 604 POSSESSIVE_CAPTURE:
1098     number = GET2(ecode, 1+LINK_SIZE);
1099     offset = number << 1;
1100    
1101     #ifdef PCRE_DEBUG
1102     printf("start possessive bracket %d\n", number);
1103     printf("subject=");
1104     pchars(eptr, 16, TRUE, md);
1105     printf("\n");
1106     #endif
1107    
1108     if (offset < md->offset_max)
1109     {
1110     matched_once = FALSE;
1111 ph10 836 code_offset = (int)(ecode - md->start_code);
1112 ph10 604
1113     save_offset1 = md->offset_vector[offset];
1114     save_offset2 = md->offset_vector[offset+1];
1115     save_offset3 = md->offset_vector[md->offset_end - number];
1116     save_capture_last = md->capture_last;
1117    
1118     DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
1119 ph10 625
1120     /* Each time round the loop, save the current subject position for use
1121     when the group matches. For MATCH_MATCH, the group has matched, so we
1122     restart it with a new subject starting position, remembering that we had
1123     at least one match. For MATCH_NOMATCH, carry on with the alternatives, as
1124     usual. If we haven't matched any alternatives in any iteration, check to
1125     see if a previous iteration matched. If so, the group has matched;
1126     continue from afterwards. Otherwise it has failed; restore the previous
1127 ph10 604 capture values before returning NOMATCH. */
1128 ph10 625
1129 ph10 604 for (;;)
1130     {
1131     md->offset_vector[md->offset_end - number] =
1132     (int)(eptr - md->start_subject);
1133 ph10 625 if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1134 ph10 836 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
1135 ph10 604 eptrb, RM63);
1136     if (rrc == MATCH_KETRPOS)
1137     {
1138     offset_top = md->end_offset_top;
1139     eptr = md->end_match_ptr;
1140 ph10 625 ecode = md->start_code + code_offset;
1141 ph10 604 save_capture_last = md->capture_last;
1142 ph10 625 matched_once = TRUE;
1143     continue;
1144     }
1145 ph10 733
1146 ph10 716 /* See comment in the code for capturing groups above about handling
1147     THEN. */
1148    
1149     if (rrc == MATCH_THEN)
1150     {
1151     next = ecode + GET(ecode,1);
1152 ph10 733 if (md->start_match_ptr < next &&
1153 ph10 716 (*ecode == OP_ALT || *next == OP_ALT))
1154     rrc = MATCH_NOMATCH;
1155 ph10 733 }
1156 ph10 716
1157     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1158 ph10 604 md->capture_last = save_capture_last;
1159     ecode += GET(ecode, 1);
1160 ph10 625 if (*ecode != OP_ALT) break;
1161 ph10 604 }
1162 ph10 610
1163 ph10 604 if (!matched_once)
1164 ph10 625 {
1165 ph10 604 md->offset_vector[offset] = save_offset1;
1166     md->offset_vector[offset+1] = save_offset2;
1167     md->offset_vector[md->offset_end - number] = save_offset3;
1168     }
1169 ph10 625
1170 ph10 604 if (allow_zero || matched_once)
1171 ph10 625 {
1172 ph10 604 ecode += 1 + LINK_SIZE;
1173     break;
1174 ph10 625 }
1175    
1176 ph10 604 RRETURN(MATCH_NOMATCH);
1177     }
1178 ph10 625
1179 ph10 604 /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
1180     as a non-capturing bracket. */
1181    
1182     /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1183     /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1184    
1185     DPRINTF(("insufficient capture room: treat as non-capturing\n"));
1186    
1187     /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1188     /* VVVVVVVVVVVVVVVVVVVVVVVVV */
1189    
1190 ph10 625 /* Non-capturing possessive bracket with unlimited repeat. We come here
1191 ph10 604 from BRAZERO with allow_zero = TRUE. The code is similar to the above,
1192     without the capturing complication. It is written out separately for speed
1193     and cleanliness. */
1194    
1195     case OP_BRAPOS:
1196     case OP_SBRAPOS:
1197 ph10 625 allow_zero = FALSE;
1198    
1199 ph10 604 POSSESSIVE_NON_CAPTURE:
1200     matched_once = FALSE;
1201 ph10 836 code_offset = (int)(ecode - md->start_code);
1202 ph10 604
1203     for (;;)
1204     {
1205 ph10 625 if (op >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
1206 ph10 836 RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode], offset_top, md,
1207 ph10 609 eptrb, RM48);
1208 ph10 604 if (rrc == MATCH_KETRPOS)
1209     {
1210 ph10 610 offset_top = md->end_offset_top;
1211 ph10 604 eptr = md->end_match_ptr;
1212 ph10 625 ecode = md->start_code + code_offset;
1213     matched_once = TRUE;
1214     continue;
1215     }
1216 ph10 733
1217 ph10 716 /* See comment in the code for capturing groups above about handling
1218     THEN. */
1219    
1220     if (rrc == MATCH_THEN)
1221     {
1222     next = ecode + GET(ecode,1);
1223 ph10 733 if (md->start_match_ptr < next &&
1224 ph10 716 (*ecode == OP_ALT || *next == OP_ALT))
1225     rrc = MATCH_NOMATCH;
1226 ph10 733 }
1227 ph10 716
1228     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1229 ph10 604 ecode += GET(ecode, 1);
1230 ph10 625 if (*ecode != OP_ALT) break;
1231 ph10 604 }
1232 ph10 625
1233     if (matched_once || allow_zero)
1234 ph10 604 {
1235     ecode += 1 + LINK_SIZE;
1236     break;
1237 ph10 625 }
1238 ph10 604 RRETURN(MATCH_NOMATCH);
1239    
1240     /* Control never reaches here. */
1241    
1242 nigel 77 /* Conditional group: compilation checked that there are no more than
1243     two branches. If the condition is false, skipping the first branch takes us
1244     past the end if there is only one branch, but that's OK because that is
1245 ph10 609 exactly what going to the ket would do. */
1246 nigel 77
1247     case OP_COND:
1248 nigel 93 case OP_SCOND:
1249 ph10 604 codelink = GET(ecode, 1);
1250 ph10 406
1251 ph10 381 /* Because of the way auto-callout works during compile, a callout item is
1252     inserted between OP_COND and an assertion condition. */
1253 ph10 392
1254 ph10 381 if (ecode[LINK_SIZE+1] == OP_CALLOUT)
1255     {
1256 ph10 836 if (PUBL(callout) != NULL)
1257 ph10 381 {
1258 zherczeg 850 PUBL(callout_block) cb;
1259 ph10 645 cb.version = 2; /* Version 1 of the callout block */
1260 ph10 381 cb.callout_number = ecode[LINK_SIZE+2];
1261     cb.offset_vector = md->offset_vector;
1262 zherczeg 852 #ifdef COMPILE_PCRE8
1263 ph10 381 cb.subject = (PCRE_SPTR)md->start_subject;
1264 zherczeg 852 #else
1265     cb.subject = (PCRE_SPTR16)md->start_subject;
1266     #endif
1267 ph10 530 cb.subject_length = (int)(md->end_subject - md->start_subject);
1268     cb.start_match = (int)(mstart - md->start_subject);
1269     cb.current_position = (int)(eptr - md->start_subject);
1270 ph10 381 cb.pattern_position = GET(ecode, LINK_SIZE + 3);
1271     cb.next_item_length = GET(ecode, 3 + 2*LINK_SIZE);
1272     cb.capture_top = offset_top/2;
1273     cb.capture_last = md->capture_last;
1274     cb.callout_data = md->callout_data;
1275 ph10 836 cb.mark = md->nomatch_mark;
1276     if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH);
1277 ph10 381 if (rrc < 0) RRETURN(rrc);
1278     }
1279 ph10 836 ecode += PRIV(OP_lengths)[OP_CALLOUT];
1280 ph10 381 }
1281 ph10 392
1282 ph10 399 condcode = ecode[LINK_SIZE+1];
1283 ph10 406
1284 ph10 381 /* Now see what the actual condition is */
1285 ph10 392
1286 ph10 459 if (condcode == OP_RREF || condcode == OP_NRREF) /* Recursion test */
1287 nigel 77 {
1288 ph10 459 if (md->recursive == NULL) /* Not recursing => FALSE */
1289     {
1290 ph10 461 condition = FALSE;
1291     ecode += GET(ecode, 1);
1292     }
1293 ph10 459 else
1294 ph10 461 {
1295 ph10 459 int recno = GET2(ecode, LINK_SIZE + 2); /* Recursion group number*/
1296 ph10 751 condition = (recno == RREF_ANY || recno == md->recursive->group_num);
1297 ph10 461
1298 ph10 459 /* If the test is for recursion into a specific subpattern, and it is
1299     false, but the test was set up by name, scan the table to see if the
1300     name refers to any other numbers, and test them. The condition is true
1301     if any one is set. */
1302 ph10 461
1303 ph10 751 if (!condition && condcode == OP_NRREF)
1304 ph10 459 {
1305 ph10 836 pcre_uchar *slotA = md->name_table;
1306 ph10 459 for (i = 0; i < md->name_count; i++)
1307 ph10 461 {
1308     if (GET2(slotA, 0) == recno) break;
1309 ph10 459 slotA += md->name_entry_size;
1310     }
1311 ph10 461
1312 ph10 459 /* Found a name for the number - there can be only one; duplicate
1313     names for different numbers are allowed, but not vice versa. First
1314     scan down for duplicates. */
1315 ph10 461
1316 ph10 459 if (i < md->name_count)
1317 ph10 461 {
1318 ph10 836 pcre_uchar *slotB = slotA;
1319 ph10 459 while (slotB > md->name_table)
1320     {
1321     slotB -= md->name_entry_size;
1322 ph10 836 if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)
1323 ph10 459 {
1324     condition = GET2(slotB, 0) == md->recursive->group_num;
1325 ph10 461 if (condition) break;
1326     }
1327 ph10 459 else break;
1328 ph10 461 }
1329    
1330 ph10 459 /* Scan up for duplicates */
1331 ph10 461
1332 ph10 459 if (!condition)
1333 ph10 461 {
1334 ph10 459 slotB = slotA;
1335     for (i++; i < md->name_count; i++)
1336     {
1337     slotB += md->name_entry_size;
1338 ph10 836 if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)
1339 ph10 459 {
1340     condition = GET2(slotB, 0) == md->recursive->group_num;
1341     if (condition) break;
1342 ph10 461 }
1343 ph10 459 else break;
1344 ph10 461 }
1345     }
1346 ph10 459 }
1347 ph10 461 }
1348    
1349 ph10 459 /* Chose branch according to the condition */
1350 ph10 461
1351 ph10 836 ecode += condition? 1 + IMM2_SIZE : GET(ecode, 1);
1352 ph10 459 }
1353 ph10 461 }
1354 nigel 93
1355 ph10 459 else if (condcode == OP_CREF || condcode == OP_NCREF) /* Group used test */
1356 nigel 93 {
1357 nigel 77 offset = GET2(ecode, LINK_SIZE+2) << 1; /* Doubled ref number */
1358 nigel 93 condition = offset < offset_top && md->offset_vector[offset] >= 0;
1359 ph10 461
1360 ph10 459 /* If the numbered capture is unset, but the reference was by name,
1361 ph10 461 scan the table to see if the name refers to any other numbers, and test
1362     them. The condition is true if any one is set. This is tediously similar
1363     to the code above, but not close enough to try to amalgamate. */
1364    
1365 ph10 459 if (!condition && condcode == OP_NCREF)
1366     {
1367 ph10 461 int refno = offset >> 1;
1368 ph10 836 pcre_uchar *slotA = md->name_table;
1369 ph10 461
1370 ph10 459 for (i = 0; i < md->name_count; i++)
1371 ph10 461 {
1372     if (GET2(slotA, 0) == refno) break;
1373 ph10 459 slotA += md->name_entry_size;
1374     }
1375 ph10 461
1376     /* Found a name for the number - there can be only one; duplicate names
1377     for different numbers are allowed, but not vice versa. First scan down
1378 ph10 459 for duplicates. */
1379 ph10 461
1380 ph10 459 if (i < md->name_count)
1381 ph10 461 {
1382 ph10 836 pcre_uchar *slotB = slotA;
1383 ph10 459 while (slotB > md->name_table)
1384     {
1385     slotB -= md->name_entry_size;
1386 ph10 836 if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)
1387 ph10 459 {
1388     offset = GET2(slotB, 0) << 1;
1389 ph10 461 condition = offset < offset_top &&
1390 ph10 459 md->offset_vector[offset] >= 0;
1391 ph10 461 if (condition) break;
1392     }
1393 ph10 459 else break;
1394 ph10 461 }
1395    
1396 ph10 459 /* Scan up for duplicates */
1397 ph10 461
1398 ph10 459 if (!condition)
1399 ph10 461 {
1400 ph10 459 slotB = slotA;
1401     for (i++; i < md->name_count; i++)
1402     {
1403     slotB += md->name_entry_size;
1404 ph10 836 if (STRCMP_UC_UC(slotA + IMM2_SIZE, slotB + IMM2_SIZE) == 0)
1405 ph10 459 {
1406     offset = GET2(slotB, 0) << 1;
1407 ph10 461 condition = offset < offset_top &&
1408 ph10 459 md->offset_vector[offset] >= 0;
1409 ph10 461 if (condition) break;
1410     }
1411 ph10 459 else break;
1412 ph10 461 }
1413     }
1414 ph10 459 }
1415 ph10 461 }
1416    
1417 ph10 459 /* Chose branch according to the condition */
1418    
1419 ph10 836 ecode += condition? 1 + IMM2_SIZE : GET(ecode, 1);
1420 nigel 77 }
1421    
1422 ph10 399 else if (condcode == OP_DEF) /* DEFINE - always false */
1423 nigel 93 {
1424     condition = FALSE;
1425     ecode += GET(ecode, 1);
1426     }
1427    
1428 nigel 77 /* The condition is an assertion. Call match() to evaluate it - setting
1429 ph10 604 md->match_function_type to MATCH_CONDASSERT causes it to stop at the end of
1430     an assertion. */
1431 nigel 77
1432     else
1433     {
1434 ph10 625 md->match_function_type = MATCH_CONDASSERT;
1435 ph10 604 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM3);
1436 nigel 77 if (rrc == MATCH_MATCH)
1437     {
1438 ph10 619 if (md->end_offset_top > offset_top)
1439     offset_top = md->end_offset_top; /* Captures may have happened */
1440 nigel 93 condition = TRUE;
1441     ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
1442 nigel 77 while (*ecode == OP_ALT) ecode += GET(ecode, 1);
1443     }
1444 ph10 733
1445 ph10 716 /* PCRE doesn't allow the effect of (*THEN) to escape beyond an
1446 ph10 733 assertion; it is therefore treated as NOMATCH. */
1447 ph10 716
1448 ph10 733 else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
1449 nigel 77 {
1450     RRETURN(rrc); /* Need braces because of following else */
1451     }
1452 nigel 93 else
1453     {
1454     condition = FALSE;
1455 ph10 399 ecode += codelink;
1456 nigel 93 }
1457     }
1458 nigel 91
1459 ph10 716 /* We are now at the branch that is to be obeyed. As there is only one, can
1460     use tail recursion to avoid using another stack frame, except when there is
1461     unlimited repeat of a possibly empty group. In the latter case, a recursive
1462     call to match() is always required, unless the second alternative doesn't
1463     exist, in which case we can just plough on. Note that, for compatibility
1464     with Perl, the | in a conditional group is NOT treated as creating two
1465     alternatives. If a THEN is encountered in the branch, it propagates out to
1466     the enclosing alternative (unless nested in a deeper set of alternatives,
1467     of course). */
1468 nigel 91
1469 nigel 93 if (condition || *ecode == OP_ALT)
1470     {
1471 ph10 716 if (op != OP_SCOND)
1472 ph10 702 {
1473     ecode += 1 + LINK_SIZE;
1474     goto TAIL_RECURSE;
1475 ph10 708 }
1476 ph10 733
1477 ph10 716 md->match_function_type = MATCH_CBEGROUP;
1478 ph10 609 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM49);
1479     RRETURN(rrc);
1480 nigel 77 }
1481 ph10 708
1482 ph10 702 /* Condition false & no alternative; continue after the group. */
1483 ph10 708
1484 ph10 702 else
1485 nigel 93 {
1486     ecode += 1 + LINK_SIZE;
1487     }
1488     break;
1489 nigel 77
1490 ph10 461
1491 ph10 447 /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
1492     to close any currently open capturing brackets. */
1493 ph10 461
1494 ph10 447 case OP_CLOSE:
1495 ph10 461 number = GET2(ecode, 1);
1496 ph10 447 offset = number << 1;
1497 ph10 461
1498 ph10 475 #ifdef PCRE_DEBUG
1499 ph10 447 printf("end bracket %d at *ACCEPT", number);
1500     printf("\n");
1501     #endif
1502 nigel 77
1503 ph10 447 md->capture_last = number;
1504     if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1505     {
1506     md->offset_vector[offset] =
1507     md->offset_vector[md->offset_end - number];
1508 ph10 530 md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1509 ph10 447 if (offset_top <= offset) offset_top = offset + 2;
1510     }
1511 ph10 836 ecode += 1 + IMM2_SIZE;
1512 ph10 461 break;
1513 ph10 447
1514    
1515 ph10 619 /* End of the pattern, either real or forced. */
1516 nigel 77
1517 ph10 619 case OP_END:
1518 ph10 210 case OP_ACCEPT:
1519 ph10 625 case OP_ASSERT_ACCEPT:
1520    
1521 ph10 619 /* If we have matched an empty string, fail if not in an assertion and not
1522     in a recursion if either PCRE_NOTEMPTY is set, or if PCRE_NOTEMPTY_ATSTART
1523 ph10 613 is set and we have matched at the start of the subject. In both cases,
1524     backtracking will then try other alternatives, if any. */
1525 ph10 443
1526 ph10 619 if (eptr == mstart && op != OP_ASSERT_ACCEPT &&
1527 ph10 618 md->recursive == NULL &&
1528 ph10 619 (md->notempty ||
1529     (md->notempty_atstart &&
1530     mstart == md->start_subject + md->start_offset)))
1531 ph10 836 RRETURN(MATCH_NOMATCH);
1532 ph10 443
1533 ph10 442 /* Otherwise, we have a match. */
1534 ph10 625
1535 ph10 168 md->end_match_ptr = eptr; /* Record where we ended */
1536     md->end_offset_top = offset_top; /* and how many extracts were taken */
1537 ph10 210 md->start_match_ptr = mstart; /* and the start (\K can modify) */
1538 nigel 77
1539 ph10 512 /* For some reason, the macros don't work properly if an expression is
1540 ph10 836 given as the argument to RRETURN when the heap is in use. */
1541 ph10 512
1542     rrc = (op == OP_END)? MATCH_MATCH : MATCH_ACCEPT;
1543 ph10 836 RRETURN(rrc);
1544 ph10 512
1545 nigel 77 /* Assertion brackets. Check the alternative branches in turn - the
1546     matching won't pass the KET for an assertion. If any one branch matches,
1547     the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
1548     start of each branch to move the current point backwards, so the code at
1549 ph10 625 this level is identical to the lookahead case. When the assertion is part
1550     of a condition, we want to return immediately afterwards. The caller of
1551     this incarnation of the match() function will have set MATCH_CONDASSERT in
1552     md->match_function type, and one of these opcodes will be the first opcode
1553     that is processed. We use a local variable that is preserved over calls to
1554 ph10 604 match() to remember this case. */
1555 nigel 77
1556     case OP_ASSERT:
1557     case OP_ASSERTBACK:
1558 ph10 903 save_mark = md->mark;
1559 ph10 604 if (md->match_function_type == MATCH_CONDASSERT)
1560     {
1561     condassert = TRUE;
1562     md->match_function_type = 0;
1563     }
1564 ph10 625 else condassert = FALSE;
1565    
1566 nigel 77 do
1567     {
1568 ph10 604 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM4);
1569 ph10 511 if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1570 ph10 500 {
1571     mstart = md->start_match_ptr; /* In case \K reset it */
1572     break;
1573 ph10 501 }
1574 ph10 733
1575     /* PCRE does not allow THEN to escape beyond an assertion; it is treated
1576 ph10 716 as NOMATCH. */
1577 ph10 733
1578 ph10 716 if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
1579 nigel 77 ecode += GET(ecode, 1);
1580 ph10 903 md->mark = save_mark;
1581 nigel 77 }
1582     while (*ecode == OP_ALT);
1583 ph10 625
1584 ph10 836 if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);
1585 nigel 77
1586     /* If checking an assertion for a condition, return MATCH_MATCH. */
1587    
1588 ph10 604 if (condassert) RRETURN(MATCH_MATCH);
1589 nigel 77
1590     /* Continue from after the assertion, updating the offsets high water
1591     mark, since extracts may have been taken during the assertion. */
1592    
1593     do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1594     ecode += 1 + LINK_SIZE;
1595     offset_top = md->end_offset_top;
1596     continue;
1597    
1598 ph10 473 /* Negative assertion: all branches must fail to match. Encountering SKIP,
1599 ph10 482 PRUNE, or COMMIT means we must assume failure without checking subsequent
1600 ph10 473 branches. */
1601 nigel 77
1602     case OP_ASSERT_NOT:
1603     case OP_ASSERTBACK_NOT:
1604 ph10 903 save_mark = md->mark;
1605 ph10 604 if (md->match_function_type == MATCH_CONDASSERT)
1606     {
1607     condassert = TRUE;
1608     md->match_function_type = 0;
1609     }
1610 ph10 625 else condassert = FALSE;
1611 ph10 604
1612 nigel 77 do
1613     {
1614 ph10 604 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, NULL, RM5);
1615 ph10 903 md->mark = save_mark;
1616 ph10 836 if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) RRETURN(MATCH_NOMATCH);
1617 ph10 473 if (rrc == MATCH_SKIP || rrc == MATCH_PRUNE || rrc == MATCH_COMMIT)
1618     {
1619     do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1620 ph10 482 break;
1621     }
1622 ph10 716
1623 ph10 733 /* PCRE does not allow THEN to escape beyond an assertion; it is treated
1624 ph10 716 as NOMATCH. */
1625    
1626     if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
1627 nigel 77 ecode += GET(ecode,1);
1628     }
1629     while (*ecode == OP_ALT);
1630    
1631 ph10 604 if (condassert) RRETURN(MATCH_MATCH); /* Condition assertion */
1632 ph10 625
1633 nigel 77 ecode += 1 + LINK_SIZE;
1634     continue;
1635    
1636     /* Move the subject pointer back. This occurs only at the start of
1637     each branch of a lookbehind assertion. If we are too close to the start to
1638     move back, this match function fails. When working with UTF-8 we move
1639     back a number of characters, not bytes. */
1640    
1641     case OP_REVERSE:
1642 ph10 836 #ifdef SUPPORT_UTF
1643     if (utf)
1644 nigel 77 {
1645 nigel 93 i = GET(ecode, 1);
1646     while (i-- > 0)
1647 nigel 77 {
1648     eptr--;
1649 ph10 836 if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1650 ph10 207 BACKCHAR(eptr);
1651 nigel 77 }
1652     }
1653     else
1654     #endif
1655    
1656     /* No UTF-8 support, or not in UTF-8 mode: count is byte count */
1657    
1658     {
1659 nigel 93 eptr -= GET(ecode, 1);
1660 ph10 836 if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
1661 nigel 77 }
1662    
1663 ph10 435 /* Save the earliest consulted character, then skip to next op code */
1664 nigel 77
1665 ph10 435 if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
1666 nigel 77 ecode += 1 + LINK_SIZE;
1667     break;
1668    
1669     /* The callout item calls an external function, if one is provided, passing
1670     details of the match so far. This is mainly for debugging, though the
1671     function is able to force a failure. */
1672    
1673     case OP_CALLOUT:
1674 ph10 836 if (PUBL(callout) != NULL)
1675 nigel 77 {
1676 zherczeg 850 PUBL(callout_block) cb;
1677 ph10 645 cb.version = 2; /* Version 1 of the callout block */
1678 nigel 77 cb.callout_number = ecode[1];
1679     cb.offset_vector = md->offset_vector;
1680 zherczeg 852 #ifdef COMPILE_PCRE8
1681 nigel 87 cb.subject = (PCRE_SPTR)md->start_subject;
1682 zherczeg 852 #else
1683     cb.subject = (PCRE_SPTR16)md->start_subject;
1684     #endif
1685 ph10 530 cb.subject_length = (int)(md->end_subject - md->start_subject);
1686     cb.start_match = (int)(mstart - md->start_subject);
1687     cb.current_position = (int)(eptr - md->start_subject);
1688 nigel 77 cb.pattern_position = GET(ecode, 2);
1689     cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
1690     cb.capture_top = offset_top/2;
1691     cb.capture_last = md->capture_last;
1692     cb.callout_data = md->callout_data;
1693 ph10 836 cb.mark = md->nomatch_mark;
1694     if ((rrc = (*PUBL(callout))(&cb)) > 0) RRETURN(MATCH_NOMATCH);
1695 nigel 77 if (rrc < 0) RRETURN(rrc);
1696     }
1697     ecode += 2 + 2*LINK_SIZE;
1698     break;
1699    
1700     /* Recursion either matches the current regex, or some subexpression. The
1701     offset data is the offset to the starting bracket from the start of the
1702     whole pattern. (This is so that it works from duplicated subpatterns.)
1703 ph10 625
1704 ph10 618 The state of the capturing groups is preserved over recursion, and
1705 ph10 625 re-instated afterwards. We don't know how many are started and not yet
1706 ph10 618 finished (offset_top records the completed total) so we just have to save
1707     all the potential data. There may be up to 65535 such values, which is too
1708     large to put on the stack, but using malloc for small numbers seems
1709     expensive. As a compromise, the stack is used when there are no more than
1710     REC_STACK_SAVE_MAX values to store; otherwise malloc is used.
1711 nigel 77
1712     There are also other values that have to be saved. We use a chained
1713     sequence of blocks that actually live on the stack. Thanks to Robin Houston
1714 ph10 625 for the original version of this logic. It has, however, been hacked around
1715 ph10 618 a lot, so he is not to blame for the current way it works. */
1716 nigel 77
1717     case OP_RECURSE:
1718     {
1719 ph10 642 recursion_info *ri;
1720     int recno;
1721 ph10 654
1722 nigel 77 callpat = md->start_code + GET(ecode, 1);
1723 ph10 642 recno = (callpat == md->start_code)? 0 :
1724 ph10 654 GET2(callpat, 1 + LINK_SIZE);
1725    
1726     /* Check for repeating a recursion without advancing the subject pointer.
1727 ph10 642 This should catch convoluted mutual recursions. (Some simple cases are
1728 ph10 654 caught at compile time.) */
1729    
1730 ph10 642 for (ri = md->recursive; ri != NULL; ri = ri->prevrec)
1731 ph10 654 if (recno == ri->group_num && eptr == ri->subject_position)
1732 ph10 642 RRETURN(PCRE_ERROR_RECURSELOOP);
1733 nigel 77
1734     /* Add to "recursing stack" */
1735    
1736 ph10 642 new_recursive.group_num = recno;
1737     new_recursive.subject_position = eptr;
1738 nigel 77 new_recursive.prevrec = md->recursive;
1739     md->recursive = &new_recursive;
1740    
1741 ph10 618 /* Where to continue from afterwards */
1742 nigel 77
1743     ecode += 1 + LINK_SIZE;
1744    
1745 ph10 618 /* Now save the offset data */
1746 nigel 77
1747     new_recursive.saved_max = md->offset_end;
1748     if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)
1749     new_recursive.offset_save = stacksave;
1750     else
1751     {
1752     new_recursive.offset_save =
1753 ph10 836 (int *)(PUBL(malloc))(new_recursive.saved_max * sizeof(int));
1754 nigel 77 if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
1755     }
1756     memcpy(new_recursive.offset_save, md->offset_vector,
1757     new_recursive.saved_max * sizeof(int));
1758 ph10 625
1759 ph10 618 /* OK, now we can do the recursion. After processing each alternative,
1760 ph10 625 restore the offset data. If there were nested recursions, md->recursive
1761 ph10 618 might be changed, so reset it before looping. */
1762 nigel 77
1763     DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
1764 ph10 604 cbegroup = (*callpat >= OP_SBRA);
1765 nigel 77 do
1766     {
1767 ph10 604 if (cbegroup) md->match_function_type = MATCH_CBEGROUP;
1768 ph10 836 RMATCH(eptr, callpat + PRIV(OP_lengths)[*callpat], offset_top,
1769 ph10 604 md, eptrb, RM6);
1770 ph10 618 memcpy(md->offset_vector, new_recursive.offset_save,
1771     new_recursive.saved_max * sizeof(int));
1772 ph10 681 md->recursive = new_recursive.prevrec;
1773 ph10 511 if (rrc == MATCH_MATCH || rrc == MATCH_ACCEPT)
1774 nigel 77 {
1775 nigel 87 DPRINTF(("Recursion matched\n"));
1776 nigel 77 if (new_recursive.offset_save != stacksave)
1777 ph10 836 (PUBL(free))(new_recursive.offset_save);
1778 ph10 618
1779     /* Set where we got to in the subject, and reset the start in case
1780 ph10 625 it was changed by \K. This *is* propagated back out of a recursion,
1781     for Perl compatibility. */
1782    
1783 ph10 618 eptr = md->end_match_ptr;
1784     mstart = md->start_match_ptr;
1785     goto RECURSION_MATCHED; /* Exit loop; end processing */
1786 nigel 77 }
1787 ph10 716
1788     /* PCRE does not allow THEN to escape beyond a recursion; it is treated
1789     as NOMATCH. */
1790    
1791 ph10 733 else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
1792 nigel 87 {
1793     DPRINTF(("Recursion gave error %d\n", rrc));
1794 ph10 400 if (new_recursive.offset_save != stacksave)
1795 ph10 836 (PUBL(free))(new_recursive.offset_save);
1796 nigel 87 RRETURN(rrc);
1797     }
1798 nigel 77
1799     md->recursive = &new_recursive;
1800     callpat += GET(callpat, 1);
1801     }
1802     while (*callpat == OP_ALT);
1803    
1804     DPRINTF(("Recursion didn't match\n"));
1805     md->recursive = new_recursive.prevrec;
1806     if (new_recursive.offset_save != stacksave)
1807 ph10 836 (PUBL(free))(new_recursive.offset_save);
1808     RRETURN(MATCH_NOMATCH);
1809 nigel 77 }
1810 ph10 625
1811 ph10 618 RECURSION_MATCHED:
1812     break;
1813 nigel 77
1814     /* An alternation is the end of a branch; scan along to find the end of the
1815     bracketed group and go to there. */
1816    
1817     case OP_ALT:
1818     do ecode += GET(ecode,1); while (*ecode == OP_ALT);
1819     break;
1820    
1821 ph10 335 /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
1822     indicating that it may occur zero times. It may repeat infinitely, or not
1823     at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets
1824     with fixed upper repeat limits are compiled as a number of copies, with the
1825     optional ones preceded by BRAZERO or BRAMINZERO. */
1826 ph10 625
1827 nigel 77 case OP_BRAZERO:
1828 ph10 604 next = ecode + 1;
1829     RMATCH(eptr, next, offset_top, md, eptrb, RM10);
1830     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1831     do next += GET(next, 1); while (*next == OP_ALT);
1832     ecode = next + 1 + LINK_SIZE;
1833 nigel 77 break;
1834 ph10 625
1835 nigel 77 case OP_BRAMINZERO:
1836 ph10 604 next = ecode + 1;
1837     do next += GET(next, 1); while (*next == OP_ALT);
1838     RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11);
1839     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1840     ecode++;
1841 nigel 77 break;
1842    
1843 ph10 335 case OP_SKIPZERO:
1844 ph10 604 next = ecode+1;
1845     do next += GET(next,1); while (*next == OP_ALT);
1846     ecode = next + 1 + LINK_SIZE;
1847 ph10 335 break;
1848 ph10 625
1849 ph10 604 /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything
1850     here; just jump to the group, with allow_zero set TRUE. */
1851 ph10 625
1852 ph10 604 case OP_BRAPOSZERO:
1853 ph10 625 op = *(++ecode);
1854 ph10 604 allow_zero = TRUE;
1855     if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE;
1856     goto POSSESSIVE_NON_CAPTURE;
1857 ph10 335
1858 nigel 93 /* End of a group, repeated or non-repeating. */
1859 nigel 77
1860     case OP_KET:
1861     case OP_KETRMIN:
1862     case OP_KETRMAX:
1863 ph10 625 case OP_KETRPOS:
1864 nigel 91 prev = ecode - GET(ecode, 1);
1865 ph10 625
1866 nigel 93 /* If this was a group that remembered the subject start, in order to break
1867     infinite repeats of empty string matches, retrieve the subject start from
1868     the chain. Otherwise, set it NULL. */
1869 nigel 77
1870 ph10 618 if (*prev >= OP_SBRA || *prev == OP_ONCE)
1871 nigel 93 {
1872     saved_eptr = eptrb->epb_saved_eptr; /* Value at start of group */
1873     eptrb = eptrb->epb_prev; /* Backup to previous group */
1874     }
1875     else saved_eptr = NULL;
1876 nigel 77
1877 ph10 733 /* If we are at the end of an assertion group or a non-capturing atomic
1878 ph10 723 group, stop matching and return MATCH_MATCH, but record the current high
1879     water mark for use by positive assertions. We also need to record the match
1880     start in case it was changed by \K. */
1881 nigel 93
1882 ph10 723 if ((*prev >= OP_ASSERT && *prev <= OP_ASSERTBACK_NOT) ||
1883 ph10 733 *prev == OP_ONCE_NC)
1884 nigel 91 {
1885 ph10 723 md->end_match_ptr = eptr; /* For ONCE_NC */
1886 nigel 91 md->end_offset_top = offset_top;
1887 ph10 500 md->start_match_ptr = mstart;
1888 ph10 836 RRETURN(MATCH_MATCH); /* Sets md->mark */
1889 nigel 91 }
1890 nigel 77
1891 nigel 93 /* For capturing groups we have to check the group number back at the start
1892     and if necessary complete handling an extraction by setting the offsets and
1893 ph10 618 bumping the high water mark. Whole-pattern recursion is coded as a recurse
1894     into group 0, so it won't be picked up here. Instead, we catch it when the
1895     OP_END is reached. Other recursion is handled here. We just have to record
1896     the current subject position and start match pointer and give a MATCH
1897     return. */
1898 nigel 77
1899 ph10 604 if (*prev == OP_CBRA || *prev == OP_SCBRA ||
1900     *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS)
1901 nigel 91 {
1902 nigel 93 number = GET2(prev, 1+LINK_SIZE);
1903 nigel 91 offset = number << 1;
1904 ph10 461
1905 ph10 475 #ifdef PCRE_DEBUG
1906 nigel 91 printf("end bracket %d", number);
1907     printf("\n");
1908 nigel 77 #endif
1909    
1910 ph10 618 /* Handle a recursively called group. */
1911    
1912     if (md->recursive != NULL && md->recursive->group_num == number)
1913     {
1914     md->end_match_ptr = eptr;
1915     md->start_match_ptr = mstart;
1916     RRETURN(MATCH_MATCH);
1917     }
1918    
1919     /* Deal with capturing */
1920    
1921 nigel 93 md->capture_last = number;
1922     if (offset >= md->offset_max) md->offset_overflow = TRUE; else
1923 nigel 91 {
1924 ph10 625 /* If offset is greater than offset_top, it means that we are
1925     "skipping" a capturing group, and that group's offsets must be marked
1926     unset. In earlier versions of PCRE, all the offsets were unset at the
1927     start of matching, but this doesn't work because atomic groups and
1928 ph10 615 assertions can cause a value to be set that should later be unset.
1929     Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as
1930 ph10 625 part of the atomic group, but this is not on the final matching path,
1931     so must be unset when 2 is set. (If there is no group 2, there is no
1932 ph10 615 problem, because offset_top will then be 2, indicating no capture.) */
1933 ph10 625
1934 ph10 615 if (offset > offset_top)
1935     {
1936     register int *iptr = md->offset_vector + offset_top;
1937     register int *iend = md->offset_vector + offset;
1938     while (iptr < iend) *iptr++ = -1;
1939 ph10 625 }
1940    
1941 ph10 615 /* Now make the extraction */
1942    
1943 nigel 93 md->offset_vector[offset] =
1944     md->offset_vector[md->offset_end - number];
1945 ph10 530 md->offset_vector[offset+1] = (int)(eptr - md->start_subject);
1946 nigel 93 if (offset_top <= offset) offset_top = offset + 2;
1947     }
1948 nigel 91 }
1949 nigel 77
1950 ph10 618 /* For an ordinary non-repeating ket, just continue at this level. This
1951     also happens for a repeating ket if no characters were matched in the
1952     group. This is the forcible breaking of infinite loops as implemented in
1953 ph10 723 Perl 5.005. For a non-repeating atomic group that includes captures,
1954     establish a backup point by processing the rest of the pattern at a lower
1955     level. If this results in a NOMATCH return, pass MATCH_ONCE back to the
1956     original OP_ONCE level, thereby bypassing intermediate backup points, but
1957     resetting any captures that happened along the way. */
1958 nigel 77
1959 nigel 91 if (*ecode == OP_KET || eptr == saved_eptr)
1960     {
1961 ph10 618 if (*prev == OP_ONCE)
1962     {
1963     RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12);
1964     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1965     md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */
1966 ph10 625 RRETURN(MATCH_ONCE);
1967     }
1968 ph10 618 ecode += 1 + LINK_SIZE; /* Carry on at this level */
1969 nigel 91 break;
1970     }
1971 ph10 625
1972     /* OP_KETRPOS is a possessive repeating ket. Remember the current position,
1973 ph10 604 and return the MATCH_KETRPOS. This makes it possible to do the repeats one
1974     at a time from the outer level, thus saving stack. */
1975 ph10 625
1976 ph10 604 if (*ecode == OP_KETRPOS)
1977 ph10 625 {
1978 ph10 604 md->end_match_ptr = eptr;
1979 ph10 625 md->end_offset_top = offset_top;
1980 ph10 604 RRETURN(MATCH_KETRPOS);
1981 ph10 625 }
1982 nigel 77
1983 ph10 604 /* The normal repeating kets try the rest of the pattern or restart from
1984     the preceding bracket, in the appropriate order. In the second case, we can
1985     use tail recursion to avoid using another stack frame, unless we have an
1986 ph10 618 an atomic group or an unlimited repeat of a group that can match an empty
1987     string. */
1988 nigel 77
1989 nigel 91 if (*ecode == OP_KETRMIN)
1990     {
1991 ph10 623 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7);
1992 nigel 91 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1993 ph10 618 if (*prev == OP_ONCE)
1994     {
1995 ph10 623 RMATCH(eptr, prev, offset_top, md, eptrb, RM8);
1996 ph10 618 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
1997     md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */
1998 ph10 625 RRETURN(MATCH_ONCE);
1999     }
2000 ph10 604 if (*prev >= OP_SBRA) /* Could match an empty string */
2001 ph10 197 {
2002 ph10 625 md->match_function_type = MATCH_CBEGROUP;
2003 ph10 604 RMATCH(eptr, prev, offset_top, md, eptrb, RM50);
2004 ph10 197 RRETURN(rrc);
2005     }
2006 nigel 91 ecode = prev;
2007     goto TAIL_RECURSE;
2008 nigel 77 }
2009 nigel 91 else /* OP_KETRMAX */
2010     {
2011 ph10 625 if (*prev >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP;
2012 ph10 604 RMATCH(eptr, prev, offset_top, md, eptrb, RM13);
2013 ph10 618 if (rrc == MATCH_ONCE && md->once_target == prev) rrc = MATCH_NOMATCH;
2014 nigel 91 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2015 ph10 618 if (*prev == OP_ONCE)
2016     {
2017 ph10 623 RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM9);
2018 ph10 618 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2019     md->once_target = prev;
2020 ph10 625 RRETURN(MATCH_ONCE);
2021     }
2022 nigel 91 ecode += 1 + LINK_SIZE;
2023     goto TAIL_RECURSE;
2024     }
2025     /* Control never gets here */
2026 nigel 77
2027 ph10 602 /* Not multiline mode: start of subject assertion, unless notbol. */
2028 nigel 77
2029     case OP_CIRC:
2030 ph10 836 if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
2031 ph10 625
2032 nigel 77 /* Start of subject assertion */
2033    
2034     case OP_SOD:
2035 ph10 836 if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);
2036 nigel 77 ecode++;
2037     break;
2038 ph10 625
2039 ph10 602 /* Multiline mode: start of subject unless notbol, or after any newline. */
2040 nigel 77
2041 ph10 602 case OP_CIRCM:
2042 ph10 836 if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
2043 ph10 602 if (eptr != md->start_subject &&
2044     (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
2045 ph10 836 RRETURN(MATCH_NOMATCH);
2046 ph10 602 ecode++;
2047     break;
2048    
2049 nigel 77 /* Start of match assertion */
2050    
2051     case OP_SOM:
2052 ph10 836 if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);
2053 nigel 77 ecode++;
2054     break;
2055 ph10 172
2056 ph10 168 /* Reset the start of match point */
2057 ph10 172
2058 ph10 168 case OP_SET_SOM:
2059     mstart = eptr;
2060 ph10 172 ecode++;
2061     break;
2062 nigel 77
2063 ph10 602 /* Multiline mode: assert before any newline, or before end of subject
2064     unless noteol is set. */
2065 nigel 77
2066 ph10 602 case OP_DOLLM:
2067     if (eptr < md->end_subject)
2068 ph10 916 {
2069     if (!IS_NEWLINE(eptr))
2070     {
2071     if (eptr + 1 >= md->end_subject &&
2072     md->partial != 0 &&
2073     NLBLOCK->nltype == NLTYPE_FIXED &&
2074     NLBLOCK->nllen == 2 &&
2075     *eptr == NLBLOCK->nl[0])
2076     {
2077     md->hitend = TRUE;
2078     if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
2079     }
2080     RRETURN(MATCH_NOMATCH);
2081     }
2082     }
2083 ph10 602 else
2084 nigel 77 {
2085 ph10 836 if (md->noteol) RRETURN(MATCH_NOMATCH);
2086 ph10 602 SCHECK_PARTIAL();
2087 nigel 77 }
2088 ph10 602 ecode++;
2089     break;
2090 ph10 579
2091 ph10 625 /* Not multiline mode: assert before a terminating newline or before end of
2092 ph10 602 subject unless noteol is set. */
2093    
2094     case OP_DOLL:
2095 ph10 836 if (md->noteol) RRETURN(MATCH_NOMATCH);
2096 ph10 602 if (!md->endonly) goto ASSERT_NL_OR_EOS;
2097    
2098 nigel 91 /* ... else fall through for endonly */
2099 nigel 77
2100     /* End of subject assertion (\z) */
2101    
2102     case OP_EOD:
2103 ph10 836 if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);
2104 ph10 553 SCHECK_PARTIAL();
2105 nigel 77 ecode++;
2106     break;
2107    
2108     /* End of subject or ending \n assertion (\Z) */
2109    
2110     case OP_EODN:
2111 ph10 553 ASSERT_NL_OR_EOS:
2112     if (eptr < md->end_subject &&
2113 nigel 93 (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
2114 ph10 916 {
2115     if (eptr + 1 >= md->end_subject &&
2116     md->partial != 0 &&
2117     NLBLOCK->nltype == NLTYPE_FIXED &&
2118     NLBLOCK->nllen == 2 &&
2119     *eptr == NLBLOCK->nl[0])
2120     {
2121     md->hitend = TRUE;
2122     if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
2123     }
2124 ph10 836 RRETURN(MATCH_NOMATCH);
2125 ph10 916 }
2126 ph10 579
2127 ph10 553 /* Either at end of string or \n before end. */
2128 ph10 579
2129 ph10 553 SCHECK_PARTIAL();
2130 nigel 77 ecode++;
2131     break;
2132    
2133     /* Word boundary assertions */
2134    
2135     case OP_NOT_WORD_BOUNDARY:
2136     case OP_WORD_BOUNDARY:
2137     {
2138    
2139     /* Find out if the previous and current characters are "word" characters.
2140     It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to
2141 ph10 443 be "non-word" characters. Remember the earliest consulted character for
2142 ph10 435 partial matching. */
2143 nigel 77
2144 ph10 836 #ifdef SUPPORT_UTF
2145     if (utf)
2146 nigel 77 {
2147 ph10 518 /* Get status of previous character */
2148 ph10 527
2149 nigel 77 if (eptr == md->start_subject) prev_is_word = FALSE; else
2150     {
2151 ph10 836 PCRE_PUCHAR lastptr = eptr - 1;
2152     BACKCHAR(lastptr);
2153 ph10 443 if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
2154 nigel 77 GETCHAR(c, lastptr);
2155 ph10 527 #ifdef SUPPORT_UCP
2156 ph10 518 if (md->use_ucp)
2157     {
2158     if (c == '_') prev_is_word = TRUE; else
2159 ph10 527 {
2160 ph10 518 int cat = UCD_CATEGORY(c);
2161     prev_is_word = (cat == ucp_L || cat == ucp_N);
2162 ph10 527 }
2163     }
2164     else
2165     #endif
2166 nigel 77 prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
2167     }
2168 ph10 527
2169 ph10 518 /* Get status of next character */
2170 ph10 527
2171 ph10 443 if (eptr >= md->end_subject)
2172 nigel 77 {
2173 ph10 443 SCHECK_PARTIAL();
2174     cur_is_word = FALSE;
2175 ph10 428 }
2176     else
2177     {
2178 nigel 77 GETCHAR(c, eptr);
2179 ph10 527 #ifdef SUPPORT_UCP
2180 ph10 518 if (md->use_ucp)
2181     {
2182     if (c == '_') cur_is_word = TRUE; else
2183 ph10 527 {
2184 ph10 518 int cat = UCD_CATEGORY(c);
2185     cur_is_word = (cat == ucp_L || cat == ucp_N);
2186 ph10 527 }
2187     }
2188     else
2189     #endif
2190 nigel 77 cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
2191     }
2192     }
2193     else
2194     #endif
2195    
2196 ph10 527 /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for
2197 ph10 518 consistency with the behaviour of \w we do use it in this case. */
2198 nigel 77
2199     {
2200 ph10 518 /* Get status of previous character */
2201 ph10 527
2202 ph10 435 if (eptr == md->start_subject) prev_is_word = FALSE; else
2203     {
2204 ph10 443 if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
2205 ph10 527 #ifdef SUPPORT_UCP
2206 ph10 518 if (md->use_ucp)
2207     {
2208 ph10 527 c = eptr[-1];
2209 ph10 518 if (c == '_') prev_is_word = TRUE; else
2210 ph10 527 {
2211 ph10 518 int cat = UCD_CATEGORY(c);
2212     prev_is_word = (cat == ucp_L || cat == ucp_N);
2213 ph10 527 }
2214     }
2215     else
2216     #endif
2217 ph10 836 prev_is_word = MAX_255(eptr[-1])
2218     && ((md->ctypes[eptr[-1]] & ctype_word) != 0);
2219 ph10 435 }
2220 ph10 527
2221 ph10 518 /* Get status of next character */
2222 ph10 527
2223 ph10 443 if (eptr >= md->end_subject)
2224 ph10 428 {
2225 ph10 443 SCHECK_PARTIAL();
2226     cur_is_word = FALSE;
2227 ph10 428 }
2228 ph10 527 else
2229     #ifdef SUPPORT_UCP
2230 ph10 518 if (md->use_ucp)
2231     {
2232 ph10 527 c = *eptr;
2233 ph10 518 if (c == '_') cur_is_word = TRUE; else
2234 ph10 527 {
2235 ph10 518 int cat = UCD_CATEGORY(c);
2236     cur_is_word = (cat == ucp_L || cat == ucp_N);
2237 ph10 527 }
2238     }
2239     else
2240     #endif
2241 ph10 836 cur_is_word = MAX_255(*eptr)
2242     && ((md->ctypes[*eptr] & ctype_word) != 0);
2243 nigel 77 }
2244    
2245     /* Now see if the situation is what we want */
2246    
2247     if ((*ecode++ == OP_WORD_BOUNDARY)?
2248     cur_is_word == prev_is_word : cur_is_word != prev_is_word)
2249 ph10 836 RRETURN(MATCH_NOMATCH);
2250 nigel 77 }
2251     break;
2252    
2253     /* Match a single character type; inline for speed */
2254    
2255     case OP_ANY:
2256 ph10 836 if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
2257 ph10 345 /* Fall through */
2258    
2259 ph10 341 case OP_ALLANY:
2260 ph10 648 if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */
2261     { /* not be updated before SCHECK_PARTIAL. */
2262 ph10 443 SCHECK_PARTIAL();
2263 ph10 836 RRETURN(MATCH_NOMATCH);
2264 ph10 443 }
2265 ph10 648 eptr++;
2266 ph10 836 #ifdef SUPPORT_UTF
2267     if (utf) ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++);
2268     #endif
2269 nigel 77 ecode++;
2270     break;
2271    
2272     /* Match a single byte, even in UTF-8 mode. This opcode really does match
2273     any byte, even newline, independent of the setting of PCRE_DOTALL. */
2274    
2275     case OP_ANYBYTE:
2276 ph10 648 if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */
2277     { /* not be updated before SCHECK_PARTIAL. */
2278 ph10 443 SCHECK_PARTIAL();
2279 ph10 836 RRETURN(MATCH_NOMATCH);
2280 ph10 443 }
2281 ph10 654 eptr++;
2282 nigel 77 ecode++;
2283     break;
2284    
2285     case OP_NOT_DIGIT:
2286 ph10 443 if (eptr >= md->end_subject)
2287 ph10 428 {
2288 ph10 443 SCHECK_PARTIAL();
2289 ph10 836 RRETURN(MATCH_NOMATCH);
2290 ph10 443 }
2291 nigel 77 GETCHARINCTEST(c, eptr);
2292     if (
2293 ph10 836 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2294 nigel 77 c < 256 &&
2295     #endif
2296     (md->ctypes[c] & ctype_digit) != 0
2297     )
2298 ph10 836 RRETURN(MATCH_NOMATCH);
2299 nigel 77 ecode++;
2300     break;
2301    
2302     case OP_DIGIT:
2303 ph10 443 if (eptr >= md->end_subject)
2304 ph10 428 {
2305 ph10 443 SCHECK_PARTIAL();
2306 ph10 836 RRETURN(MATCH_NOMATCH);
2307 ph10 443 }
2308 nigel 77 GETCHARINCTEST(c, eptr);
2309     if (
2310 ph10 836 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2311     c > 255 ||
2312 nigel 77 #endif
2313     (md->ctypes[c] & ctype_digit) == 0
2314     )
2315 ph10 836 RRETURN(MATCH_NOMATCH);
2316 nigel 77 ecode++;
2317     break;
2318    
2319     case OP_NOT_WHITESPACE:
2320 ph10 443 if (eptr >= md->end_subject)
2321 ph10 428 {
2322 ph10 443 SCHECK_PARTIAL();
2323 ph10 836 RRETURN(MATCH_NOMATCH);
2324 ph10 443 }
2325 nigel 77 GETCHARINCTEST(c, eptr);
2326     if (
2327 ph10 836 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2328 nigel 77 c < 256 &&
2329     #endif
2330     (md->ctypes[c] & ctype_space) != 0
2331     )
2332 ph10 836 RRETURN(MATCH_NOMATCH);
2333 nigel 77 ecode++;
2334     break;
2335    
2336     case OP_WHITESPACE:
2337 ph10 443 if (eptr >= md->end_subject)
2338 ph10 428 {
2339 ph10 443 SCHECK_PARTIAL();
2340 ph10 836 RRETURN(MATCH_NOMATCH);
2341 ph10 443 }
2342 nigel 77 GETCHARINCTEST(c, eptr);
2343     if (
2344 ph10 836 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2345     c > 255 ||
2346 nigel 77 #endif
2347     (md->ctypes[c] & ctype_space) == 0
2348     )
2349 ph10 836 RRETURN(MATCH_NOMATCH);
2350 nigel 77 ecode++;
2351     break;
2352    
2353     case OP_NOT_WORDCHAR:
2354 ph10 443 if (eptr >= md->end_subject)
2355 ph10 428 {
2356 ph10 443 SCHECK_PARTIAL();
2357 ph10 836 RRETURN(MATCH_NOMATCH);
2358 ph10 443 }
2359 nigel 77 GETCHARINCTEST(c, eptr);
2360     if (
2361 ph10 836 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2362 nigel 77 c < 256 &&
2363     #endif
2364     (md->ctypes[c] & ctype_word) != 0
2365     )
2366 ph10 836 RRETURN(MATCH_NOMATCH);
2367 nigel 77 ecode++;
2368     break;
2369    
2370     case OP_WORDCHAR:
2371 ph10 443 if (eptr >= md->end_subject)
2372 ph10 428 {
2373 ph10 443 SCHECK_PARTIAL();
2374 ph10 836 RRETURN(MATCH_NOMATCH);
2375 ph10 443 }
2376 nigel 77 GETCHARINCTEST(c, eptr);
2377     if (
2378 ph10 836 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
2379     c > 255 ||
2380 nigel 77 #endif
2381     (md->ctypes[c] & ctype_word) == 0
2382     )
2383 ph10 836 RRETURN(MATCH_NOMATCH);
2384 nigel 77 ecode++;
2385     break;
2386    
2387 nigel 93 case OP_ANYNL:
2388 ph10 443 if (eptr >= md->end_subject)
2389 ph10 428 {
2390 ph10 443 SCHECK_PARTIAL();
2391 ph10 836 RRETURN(MATCH_NOMATCH);
2392 ph10 443 }
2393 nigel 93 GETCHARINCTEST(c, eptr);
2394     switch(c)
2395     {
2396 ph10 836 default: RRETURN(MATCH_NOMATCH);
2397 ph10 625
2398 nigel 93 case 0x000d:
2399 ph10 916 if (eptr >= md->end_subject)
2400     {
2401     SCHECK_PARTIAL();
2402     }
2403     else if (*eptr == 0x0a) eptr++;
2404 nigel 93 break;
2405 ph10 231
2406 nigel 93 case 0x000a:
2407 ph10 231 break;
2408    
2409 nigel 93 case 0x000b:
2410     case 0x000c:
2411     case 0x0085:
2412     case 0x2028:
2413     case 0x2029:
2414 ph10 836 if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
2415 nigel 93 break;
2416     }
2417     ecode++;
2418     break;
2419    
2420 ph10 178 case OP_NOT_HSPACE:
2421 ph10 443 if (eptr >= md->end_subject)
2422 ph10 428 {
2423 ph10 443 SCHECK_PARTIAL();
2424 ph10 836 RRETURN(MATCH_NOMATCH);
2425 ph10 443 }
2426 ph10 178 GETCHARINCTEST(c, eptr);
2427     switch(c)
2428     {
2429     default: break;
2430     case 0x09: /* HT */
2431     case 0x20: /* SPACE */
2432     case 0xa0: /* NBSP */
2433     case 0x1680: /* OGHAM SPACE MARK */
2434     case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
2435     case 0x2000: /* EN QUAD */
2436     case 0x2001: /* EM QUAD */
2437     case 0x2002: /* EN SPACE */
2438     case 0x2003: /* EM SPACE */
2439     case 0x2004: /* THREE-PER-EM SPACE */
2440     case 0x2005: /* FOUR-PER-EM SPACE */
2441     case 0x2006: /* SIX-PER-EM SPACE */
2442     case 0x2007: /* FIGURE SPACE */
2443     case 0x2008: /* PUNCTUATION SPACE */
2444     case 0x2009: /* THIN SPACE */
2445     case 0x200A: /* HAIR SPACE */
2446     case 0x202f: /* NARROW NO-BREAK SPACE */
2447     case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
2448     case 0x3000: /* IDEOGRAPHIC SPACE */
2449 ph10 836 RRETURN(MATCH_NOMATCH);
2450 ph10 178 }
2451     ecode++;
2452     break;
2453    
2454     case OP_HSPACE:
2455 ph10 443 if (eptr >= md->end_subject)
2456 ph10 428 {
2457 ph10 443 SCHECK_PARTIAL();
2458 ph10 836 RRETURN(MATCH_NOMATCH);
2459 ph10 443 }
2460 ph10 178 GETCHARINCTEST(c, eptr);
2461     switch(c)
2462     {
2463 ph10 836 default: RRETURN(MATCH_NOMATCH);
2464 ph10 178 case 0x09: /* HT */
2465     case 0x20: /* SPACE */
2466     case 0xa0: /* NBSP */
2467     case 0x1680: /* OGHAM SPACE MARK */
2468     case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
2469     case 0x2000: /* EN QUAD */
2470     case 0x2001: /* EM QUAD */
2471     case 0x2002: /* EN SPACE */
2472     case 0x2003: /* EM SPACE */
2473     case 0x2004: /* THREE-PER-EM SPACE */
2474     case 0x2005: /* FOUR-PER-EM SPACE */
2475     case 0x2006: /* SIX-PER-EM SPACE */
2476     case 0x2007: /* FIGURE SPACE */
2477     case 0x2008: /* PUNCTUATION SPACE */
2478     case 0x2009: /* THIN SPACE */
2479     case 0x200A: /* HAIR SPACE */
2480     case 0x202f: /* NARROW NO-BREAK SPACE */
2481     case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
2482     case 0x3000: /* IDEOGRAPHIC SPACE */
2483     break;
2484     }
2485     ecode++;
2486     break;
2487    
2488     case OP_NOT_VSPACE:
2489 ph10 443 if (eptr >= md->end_subject)
2490 ph10 428 {
2491 ph10 443 SCHECK_PARTIAL();
2492 ph10 836 RRETURN(MATCH_NOMATCH);
2493 ph10 443 }
2494 ph10 178 GETCHARINCTEST(c, eptr);
2495     switch(c)
2496     {
2497     default: break;
2498     case 0x0a: /* LF */
2499     case 0x0b: /* VT */
2500     case 0x0c: /* FF */
2501     case 0x0d: /* CR */
2502     case 0x85: /* NEL */
2503     case 0x2028: /* LINE SEPARATOR */
2504     case 0x2029: /* PARAGRAPH SEPARATOR */
2505 ph10 836 RRETURN(MATCH_NOMATCH);
2506 ph10 178 }
2507     ecode++;
2508     break;
2509    
2510     case OP_VSPACE:
2511 ph10 443 if (eptr >= md->end_subject)
2512 ph10 428 {
2513 ph10 443 SCHECK_PARTIAL();
2514 ph10 836 RRETURN(MATCH_NOMATCH);
2515 ph10 443 }
2516 ph10 178 GETCHARINCTEST(c, eptr);
2517     switch(c)
2518     {
2519 ph10 836 default: RRETURN(MATCH_NOMATCH);
2520 ph10 178 case 0x0a: /* LF */
2521     case 0x0b: /* VT */
2522     case 0x0c: /* FF */
2523     case 0x0d: /* CR */
2524     case 0x85: /* NEL */
2525     case 0x2028: /* LINE SEPARATOR */
2526     case 0x2029: /* PARAGRAPH SEPARATOR */
2527     break;
2528     }
2529     ecode++;
2530     break;
2531    
2532 nigel 77 #ifdef SUPPORT_UCP
2533     /* Check the next character by Unicode property. We will get here only
2534     if the support is in the binary; otherwise a compile-time error occurs. */
2535    
2536     case OP_PROP:
2537     case OP_NOTPROP:
2538 ph10 443 if (eptr >= md->end_subject)
2539 ph10 428 {
2540 ph10 443 SCHECK_PARTIAL();
2541 ph10 836 RRETURN(MATCH_NOMATCH);
2542 ph10 443 }
2543 nigel 77 GETCHARINCTEST(c, eptr);
2544     {
2545 ph10 384 const ucd_record *prop = GET_UCD(c);
2546 nigel 77
2547 nigel 87 switch(ecode[1])
2548     {
2549     case PT_ANY:
2550 ph10 836 if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
2551 nigel 87 break;
2552 nigel 77
2553 nigel 87 case PT_LAMP:
2554 ph10 349 if ((prop->chartype == ucp_Lu ||
2555     prop->chartype == ucp_Ll ||
2556     prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
2557 ph10 836 RRETURN(MATCH_NOMATCH);
2558 ph10 517 break;
2559 nigel 87
2560     case PT_GC:
2561 ph10 836 if ((ecode[2] != PRIV(ucp_gentype)[prop->chartype]) == (op == OP_PROP))
2562     RRETURN(MATCH_NOMATCH);
2563 nigel 87 break;
2564    
2565     case PT_PC:
2566 ph10 349 if ((ecode[2] != prop->chartype) == (op == OP_PROP))
2567 ph10 836 RRETURN(MATCH_NOMATCH);
2568 nigel 87 break;
2569    
2570     case PT_SC:
2571 ph10 349 if ((ecode[2] != prop->script) == (op == OP_PROP))
2572 ph10 836 RRETURN(MATCH_NOMATCH);
2573 nigel 87 break;
2574 ph10 527
2575 ph10 517 /* These are specials */
2576 ph10 527
2577 ph10 517 case PT_ALNUM:
2578 ph10 836 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2579     PRIV(ucp_gentype)[prop->chartype] == ucp_N) == (op == OP_NOTPROP))
2580     RRETURN(MATCH_NOMATCH);
2581 ph10 527 break;
2582    
2583 ph10 517 case PT_SPACE: /* Perl space */
2584 ph10 836 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2585 ph10 517 c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2586     == (op == OP_NOTPROP))
2587 ph10 836 RRETURN(MATCH_NOMATCH);
2588 ph10 527 break;
2589    
2590 ph10 517 case PT_PXSPACE: /* POSIX space */
2591 ph10 836 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2592 ph10 527 c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2593 ph10 517 c == CHAR_FF || c == CHAR_CR)
2594     == (op == OP_NOTPROP))
2595 ph10 836 RRETURN(MATCH_NOMATCH);
2596 ph10 527 break;
2597 nigel 87
2598 ph10 527 case PT_WORD:
2599 ph10 836 if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2600     PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2601 ph10 517 c == CHAR_UNDERSCORE) == (op == OP_NOTPROP))
2602 ph10 836 RRETURN(MATCH_NOMATCH);
2603 ph10 527 break;
2604    
2605 ph10 517 /* This should never occur */
2606    
2607 nigel 87 default:
2608     RRETURN(PCRE_ERROR_INTERNAL);
2609 nigel 77 }
2610 nigel 87
2611     ecode += 3;
2612 nigel 77 }
2613     break;
2614    
2615     /* Match an extended Unicode sequence. We will get here only if the support
2616     is in the binary; otherwise a compile-time error occurs. */
2617    
2618     case OP_EXTUNI:
2619 ph10 443 if (eptr >= md->end_subject)
2620 ph10 428 {
2621 ph10 443 SCHECK_PARTIAL();
2622 ph10 836 RRETURN(MATCH_NOMATCH);
2623 ph10 443 }
2624 nigel 77 GETCHARINCTEST(c, eptr);
2625 ph10 836 if (UCD_CATEGORY(c) == ucp_M) RRETURN(MATCH_NOMATCH);
2626 ph10 623 while (eptr < md->end_subject)
2627 nigel 77 {
2628 ph10 623 int len = 1;
2629 ph10 836 if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
2630 ph10 623 if (UCD_CATEGORY(c) != ucp_M) break;
2631     eptr += len;
2632 nigel 77 }
2633 ph10 916 CHECK_PARTIAL();
2634 nigel 77 ecode++;
2635     break;
2636     #endif
2637    
2638    
2639     /* Match a back reference, possibly repeatedly. Look past the end of the
2640     item to see if there is repeat information following. The code is similar
2641     to that for character classes, but repeated for efficiency. Then obey
2642     similar code to character type repeats - written out again for speed.
2643     However, if the referenced string is the empty string, always treat
2644     it as matched, any number of times (otherwise there could be infinite
2645     loops). */
2646    
2647     case OP_REF:
2648 ph10 625 case OP_REFI:
2649     caseless = op == OP_REFI;
2650 ph10 595 offset = GET2(ecode, 1) << 1; /* Doubled ref number */
2651 ph10 836 ecode += 1 + IMM2_SIZE;
2652 ph10 345
2653 ph10 595 /* If the reference is unset, there are two possibilities:
2654 ph10 345
2655 ph10 595 (a) In the default, Perl-compatible state, set the length negative;
2656     this ensures that every attempt at a match fails. We can't just fail
2657     here, because of the possibility of quantifiers with zero minima.
2658 ph10 345
2659 ph10 595 (b) If the JavaScript compatibility flag is set, set the length to zero
2660     so that the back reference matches an empty string.
2661 ph10 345
2662 ph10 595 Otherwise, set the length to the length of what was matched by the
2663     referenced subpattern. */
2664 ph10 345
2665 ph10 595 if (offset >= offset_top || md->offset_vector[offset] < 0)
2666     length = (md->jscript_compat)? 0 : -1;
2667     else
2668     length = md->offset_vector[offset+1] - md->offset_vector[offset];
2669 nigel 77
2670 ph10 595 /* Set up for repetition, or handle the non-repeated case */
2671 nigel 77
2672 ph10 595 switch (*ecode)
2673     {
2674     case OP_CRSTAR:
2675     case OP_CRMINSTAR:
2676     case OP_CRPLUS:
2677     case OP_CRMINPLUS:
2678     case OP_CRQUERY:
2679     case OP_CRMINQUERY:
2680     c = *ecode++ - OP_CRSTAR;
2681     minimize = (c & 1) != 0;
2682     min = rep_min[c]; /* Pick up values from tables; */
2683     max = rep_max[c]; /* zero for max => infinity */
2684     if (max == 0) max = INT_MAX;
2685     break;
2686 nigel 77
2687 ph10 595 case OP_CRRANGE:
2688     case OP_CRMINRANGE:
2689     minimize = (*ecode == OP_CRMINRANGE);
2690     min = GET2(ecode, 1);
2691 ph10 836 max = GET2(ecode, 1 + IMM2_SIZE);
2692 ph10 595 if (max == 0) max = INT_MAX;
2693 ph10 836 ecode += 1 + 2 * IMM2_SIZE;
2694 ph10 595 break;
2695 nigel 77
2696 ph10 595 default: /* No repeat follows */
2697 ph10 602 if ((length = match_ref(offset, eptr, length, md, caseless)) < 0)
2698 ph10 595 {
2699 ph10 916 if (length == -2) eptr = md->end_subject; /* Partial match */
2700 ph10 595 CHECK_PARTIAL();
2701 ph10 836 RRETURN(MATCH_NOMATCH);
2702 nigel 77 }
2703 ph10 595 eptr += length;
2704     continue; /* With the main loop */
2705     }
2706 nigel 77
2707 ph10 595 /* Handle repeated back references. If the length of the reference is
2708 ph10 836 zero, just continue with the main loop. If the length is negative, it
2709 ph10 842 means the reference is unset in non-Java-compatible mode. If the minimum is
2710     zero, we can continue at the same level without recursion. For any other
2711 ph10 836 minimum, carrying on will result in NOMATCH. */
2712 ph10 443
2713 ph10 595 if (length == 0) continue;
2714 ph10 836 if (length < 0 && min == 0) continue;
2715 nigel 77
2716 ph10 595 /* First, ensure the minimum number of matches are present. We get back
2717     the length of the reference string explicitly rather than passing the
2718     address of eptr, so that eptr can be a register variable. */
2719 nigel 77
2720 ph10 595 for (i = 1; i <= min; i++)
2721     {
2722 ph10 625 int slength;
2723 ph10 602 if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2724 nigel 77 {
2725 ph10 916 if (slength == -2) eptr = md->end_subject; /* Partial match */
2726 ph10 595 CHECK_PARTIAL();
2727 ph10 836 RRETURN(MATCH_NOMATCH);
2728 nigel 77 }
2729 ph10 595 eptr += slength;
2730     }
2731 nigel 77
2732 ph10 595 /* If min = max, continue at the same level without recursion.
2733     They are not both allowed to be zero. */
2734 nigel 77
2735 ph10 595 if (min == max) continue;
2736 nigel 77
2737 ph10 595 /* If minimizing, keep trying and advancing the pointer */
2738 nigel 77
2739 ph10 595 if (minimize)
2740     {
2741     for (fi = min;; fi++)
2742 nigel 77 {
2743 ph10 625 int slength;
2744 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM14);
2745 ph10 595 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2746 ph10 836 if (fi >= max) RRETURN(MATCH_NOMATCH);
2747 ph10 602 if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2748 nigel 77 {
2749 ph10 916 if (slength == -2) eptr = md->end_subject; /* Partial match */
2750 ph10 595 CHECK_PARTIAL();
2751 ph10 836 RRETURN(MATCH_NOMATCH);
2752 nigel 77 }
2753 ph10 595 eptr += slength;
2754 nigel 77 }
2755 ph10 595 /* Control never gets here */
2756     }
2757 nigel 77
2758 ph10 595 /* If maximizing, find the longest string and work backwards */
2759 nigel 77
2760 ph10 595 else
2761     {
2762     pp = eptr;
2763     for (i = min; i < max; i++)
2764 nigel 77 {
2765 ph10 625 int slength;
2766 ph10 602 if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0)
2767 nigel 77 {
2768 ph10 916 /* Can't use CHECK_PARTIAL because we don't want to update eptr in
2769     the soft partial matching case. */
2770    
2771     if (slength == -2 && md->partial != 0 &&
2772     md->end_subject > md->start_used_ptr)
2773     {
2774     md->hitend = TRUE;
2775     if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);
2776     }
2777 ph10 595 break;
2778 nigel 77 }
2779 ph10 595 eptr += slength;
2780 nigel 77 }
2781 ph10 916
2782 ph10 595 while (eptr >= pp)
2783     {
2784 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM15);
2785 ph10 595 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2786     eptr -= length;
2787     }
2788 ph10 836 RRETURN(MATCH_NOMATCH);
2789 nigel 77 }
2790     /* Control never gets here */
2791    
2792     /* Match a bit-mapped character class, possibly repeatedly. This op code is
2793     used when all the characters in the class have values in the range 0-255,
2794     and either the matching is caseful, or the characters are in the range
2795     0-127 when UTF-8 processing is enabled. The only difference between
2796     OP_CLASS and OP_NCLASS occurs when a data character outside the range is
2797     encountered.
2798    
2799     First, look past the end of the item to see if there is repeat information
2800     following. Then obey similar code to character type repeats - written out
2801     again for speed. */
2802    
2803     case OP_NCLASS:
2804     case OP_CLASS:
2805     {
2806 ph10 836 /* The data variable is saved across frames, so the byte map needs to
2807     be stored there. */
2808     #define BYTE_MAP ((pcre_uint8 *)data)
2809 nigel 77 data = ecode + 1; /* Save for matching */
2810 ph10 836 ecode += 1 + (32 / sizeof(pcre_uchar)); /* Advance past the item */
2811 nigel 77
2812     switch (*ecode)
2813     {
2814     case OP_CRSTAR:
2815     case OP_CRMINSTAR:
2816     case OP_CRPLUS:
2817     case OP_CRMINPLUS:
2818     case OP_CRQUERY:
2819     case OP_CRMINQUERY:
2820     c = *ecode++ - OP_CRSTAR;
2821     minimize = (c & 1) != 0;
2822     min = rep_min[c]; /* Pick up values from tables; */
2823     max = rep_max[c]; /* zero for max => infinity */
2824     if (max == 0) max = INT_MAX;
2825     break;
2826    
2827     case OP_CRRANGE:
2828     case OP_CRMINRANGE:
2829     minimize = (*ecode == OP_CRMINRANGE);
2830     min = GET2(ecode, 1);
2831 ph10 836 max = GET2(ecode, 1 + IMM2_SIZE);
2832 nigel 77 if (max == 0) max = INT_MAX;
2833 ph10 836 ecode += 1 + 2 * IMM2_SIZE;
2834 nigel 77 break;
2835    
2836     default: /* No repeat follows */
2837     min = max = 1;
2838     break;
2839     }
2840    
2841     /* First, ensure the minimum number of matches are present. */
2842    
2843 ph10 836 #ifdef SUPPORT_UTF
2844     if (utf)
2845 nigel 77 {
2846     for (i = 1; i <= min; i++)
2847     {
2848 ph10 427 if (eptr >= md->end_subject)
2849 ph10 426 {
2850 ph10 428 SCHECK_PARTIAL();
2851 ph10 836 RRETURN(MATCH_NOMATCH);
2852 ph10 427 }
2853 nigel 77 GETCHARINC(c, eptr);
2854     if (c > 255)
2855     {
2856 ph10 836 if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2857 nigel 77 }
2858     else
2859 ph10 836 if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2860 nigel 77 }
2861     }
2862     else
2863     #endif
2864 ph10 836 /* Not UTF mode */
2865 nigel 77 {
2866     for (i = 1; i <= min; i++)
2867     {
2868 ph10 427 if (eptr >= md->end_subject)
2869 ph10 426 {
2870 ph10 428 SCHECK_PARTIAL();
2871 ph10 836 RRETURN(MATCH_NOMATCH);
2872 ph10 427 }
2873 nigel 77 c = *eptr++;
2874 ph10 836 #ifndef COMPILE_PCRE8
2875     if (c > 255)
2876     {
2877     if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2878     }
2879     else
2880     #endif
2881     if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2882 nigel 77 }
2883     }
2884    
2885     /* If max == min we can continue with the main loop without the
2886     need to recurse. */
2887    
2888     if (min == max) continue;
2889    
2890     /* If minimizing, keep testing the rest of the expression and advancing
2891     the pointer while it matches the class. */
2892    
2893     if (minimize)
2894     {
2895 ph10 836 #ifdef SUPPORT_UTF
2896     if (utf)
2897 nigel 77 {
2898     for (fi = min;; fi++)
2899     {
2900 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM16);
2901 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2902 ph10 836 if (fi >= max) RRETURN(MATCH_NOMATCH);
2903 ph10 427 if (eptr >= md->end_subject)
2904 ph10 426 {
2905 ph10 427 SCHECK_PARTIAL();
2906 ph10 836 RRETURN(MATCH_NOMATCH);
2907 ph10 427 }
2908 nigel 77 GETCHARINC(c, eptr);
2909     if (c > 255)
2910     {
2911 ph10 836 if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2912 nigel 77 }
2913     else
2914 ph10 836 if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2915 nigel 77 }
2916     }
2917     else
2918     #endif
2919 ph10 836 /* Not UTF mode */
2920 nigel 77 {
2921     for (fi = min;; fi++)
2922     {
2923 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM17);
2924 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2925 ph10 836 if (fi >= max) RRETURN(MATCH_NOMATCH);
2926 ph10 427 if (eptr >= md->end_subject)
2927 ph10 426 {
2928 ph10 427 SCHECK_PARTIAL();
2929 ph10 836 RRETURN(MATCH_NOMATCH);
2930 ph10 427 }
2931 nigel 77 c = *eptr++;
2932 ph10 836 #ifndef COMPILE_PCRE8
2933     if (c > 255)
2934     {
2935     if (op == OP_CLASS) RRETURN(MATCH_NOMATCH);
2936     }
2937     else
2938     #endif
2939     if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH);
2940 nigel 77 }
2941     }
2942     /* Control never gets here */
2943     }
2944    
2945     /* If maximizing, find the longest possible run, then work backwards. */
2946    
2947     else
2948     {
2949     pp = eptr;
2950    
2951 ph10 836 #ifdef SUPPORT_UTF
2952     if (utf)
2953 nigel 77 {
2954     for (i = min; i < max; i++)
2955     {
2956     int len = 1;
2957 ph10 463 if (eptr >= md->end_subject)
2958 ph10 462 {
2959 ph10 463 SCHECK_PARTIAL();
2960 ph10 462 break;
2961 ph10 463 }
2962 nigel 77 GETCHARLEN(c, eptr, len);
2963     if (c > 255)
2964     {
2965     if (op == OP_CLASS) break;
2966     }
2967     else
2968 ph10 836 if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break;
2969 nigel 77 eptr += len;
2970     }
2971     for (;;)
2972     {
2973 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM18);
2974 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
2975     if (eptr-- == pp) break; /* Stop if tried at original pos */
2976     BACKCHAR(eptr);
2977     }
2978     }
2979     else
2980     #endif
2981 ph10 836 /* Not UTF mode */
2982 nigel 77 {
2983     for (i = min; i < max; i++)
2984     {
2985 ph10 463 if (eptr >= md->end_subject)
2986 ph10 462 {
2987 ph10 463 SCHECK_PARTIAL();
2988 ph10 462 break;
2989 ph10 463 }
2990 nigel 77 c = *eptr;
2991 ph10 836 #ifndef COMPILE_PCRE8
2992     if (c > 255)
2993     {
2994     if (op == OP_CLASS) break;
2995     }
2996     else
2997     #endif
2998     if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break;
2999 nigel 77 eptr++;
3000     }
3001     while (eptr >= pp)
3002     {
3003 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM19);
3004 nigel 87 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3005 nigel 77 eptr--;
3006     }
3007     }
3008    
3009 ph10 836 RRETURN(MATCH_NOMATCH);
3010 nigel 77 }
3011 ph10 836 #undef BYTE_MAP
3012 nigel 77 }
3013     /* Control never gets here */
3014    
3015    
3016     /* Match an extended character class. This opcode is encountered only
3017 ph10 384 when UTF-8 mode mode is supported. Nevertheless, we may not be in UTF-8
3018     mode, because Unicode properties are supported in non-UTF-8 mode. */
3019 nigel 77
3020 ph10 836 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3021 nigel 77 case OP_XCLASS:
3022     {
3023     data = ecode + 1 + LINK_SIZE; /* Save for matching */
3024     ecode += GET(ecode, 1); /* Advance past the item */
3025    
3026     switch (*ecode)
3027     {
3028     case OP_CRSTAR:
3029     case OP_CRMINSTAR:
3030     case OP_CRPLUS:
3031     case OP_CRMINPLUS:
3032     case OP_CRQUERY:
3033     case OP_CRMINQUERY:
3034     c = *ecode++ - OP_CRSTAR;
3035     minimize = (c & 1) != 0;
3036     min = rep_min[c]; /* Pick up values from tables; */
3037     max = rep_max[c]; /* zero for max => infinity */
3038     if (max == 0) max = INT_MAX;
3039     break;
3040    
3041     case OP_CRRANGE:
3042     case OP_CRMINRANGE:
3043     minimize = (*ecode == OP_CRMINRANGE);
3044     min = GET2(ecode, 1);
3045 ph10 836 max = GET2(ecode, 1 + IMM2_SIZE);
3046 nigel 77 if (max == 0) max = INT_MAX;
3047 ph10 836 ecode += 1 + 2 * IMM2_SIZE;
3048 nigel 77 break;
3049    
3050     default: /* No repeat follows */
3051     min = max = 1;
3052     break;
3053     }
3054    
3055     /* First, ensure the minimum number of matches are present. */
3056    
3057     for (i = 1; i <= min; i++)
3058     {
3059 ph10 427 if (eptr >= md->end_subject)
3060 ph10 426 {
3061     SCHECK_PARTIAL();
3062 ph10 836 RRETURN(MATCH_NOMATCH);
3063 ph10 427 }
3064 ph10 384 GETCHARINCTEST(c, eptr);
3065 ph10 836 if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH);
3066 nigel 77 }
3067    
3068     /* If max == min we can continue with the main loop without the
3069     need to recurse. */
3070    
3071     if (min == max) continue;
3072    
3073     /* If minimizing, keep testing the rest of the expression and advancing
3074     the pointer while it matches the class. */
3075    
3076     if (minimize)
3077     {
3078     for (fi = min;; fi++)
3079     {
3080 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM20);
3081 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3082 ph10 836 if (fi >= max) RRETURN(MATCH_NOMATCH);
3083 ph10 427 if (eptr >= md->end_subject)
3084 ph10 426 {
3085 ph10 427 SCHECK_PARTIAL();
3086 ph10 836 RRETURN(MATCH_NOMATCH);
3087 ph10 427 }
3088 ph10 384 GETCHARINCTEST(c, eptr);
3089 ph10 836 if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH);
3090 nigel 77 }
3091     /* Control never gets here */
3092     }
3093    
3094     /* If maximizing, find the longest possible run, then work backwards. */
3095    
3096     else
3097     {
3098     pp = eptr;
3099     for (i = min; i < max; i++)
3100     {
3101     int len = 1;
3102 ph10 463 if (eptr >= md->end_subject)
3103 ph10 462 {
3104 ph10 463 SCHECK_PARTIAL();
3105 ph10 462 break;
3106 ph10 463 }
3107 ph10 836 #ifdef SUPPORT_UTF
3108 ph10 384 GETCHARLENTEST(c, eptr, len);
3109 ph10 836 #else
3110     c = *eptr;
3111     #endif
3112     if (!PRIV(xclass)(c, data, utf)) break;
3113 nigel 77 eptr += len;
3114     }
3115     for(;;)
3116     {
3117 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM21);
3118 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3119     if (eptr-- == pp) break; /* Stop if tried at original pos */
3120 ph10 836 #ifdef SUPPORT_UTF
3121     if (utf) BACKCHAR(eptr);
3122     #endif
3123 nigel 77 }
3124 ph10 836 RRETURN(MATCH_NOMATCH);
3125 nigel 77 }
3126    
3127     /* Control never gets here */
3128     }
3129     #endif /* End of XCLASS */
3130    
3131     /* Match a single character, casefully */
3132    
3133     case OP_CHAR:
3134 ph10 836 #ifdef SUPPORT_UTF
3135     if (utf)
3136 nigel 77 {
3137     length = 1;
3138     ecode++;
3139     GETCHARLEN(fc, ecode, length);
3140 ph10 443 if (length > md->end_subject - eptr)
3141 ph10 428 {
3142     CHECK_PARTIAL(); /* Not SCHECK_PARTIAL() */
3143 ph10 836 RRETURN(MATCH_NOMATCH);
3144 ph10 443 }
3145 ph10 836 while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH);
3146 nigel 77 }
3147     else
3148     #endif
3149 ph10 836 /* Not UTF mode */
3150 nigel 77 {
3151 ph10 443 if (md->end_subject - eptr < 1)
3152 ph10 428 {
3153     SCHECK_PARTIAL(); /* This one can use SCHECK_PARTIAL() */
3154 ph10 836 RRETURN(MATCH_NOMATCH);
3155 ph10 443 }
3156 ph10 836 if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH);
3157 nigel 77 ecode += 2;
3158     }
3159     break;
3160    
3161 ph10 836 /* Match a single character, caselessly. If we are at the end of the
3162     subject, give up immediately. */
3163 nigel 77
3164 ph10 602 case OP_CHARI:
3165 ph10 836 if (eptr >= md->end_subject)
3166 nigel 77 {
3167 ph10 836 SCHECK_PARTIAL();
3168     RRETURN(MATCH_NOMATCH);
3169     }
3170    
3171     #ifdef SUPPORT_UTF
3172     if (utf)
3173     {
3174 nigel 77 length = 1;
3175     ecode++;
3176     GETCHARLEN(fc, ecode, length);
3177 ph10 788
3178 nigel 77 /* If the pattern character's value is < 128, we have only one byte, and
3179 ph10 836 we know that its other case must also be one byte long, so we can use the
3180     fast lookup table. We know that there is at least one byte left in the
3181     subject. */
3182 nigel 77
3183     if (fc < 128)
3184     {
3185 ph10 836 if (md->lcc[fc]
3186     != TABLE_GET(*eptr, md->lcc, *eptr)) RRETURN(MATCH_NOMATCH);
3187     ecode++;
3188     eptr++;
3189 nigel 77 }
3190    
3191 ph10 836 /* Otherwise we must pick up the subject character. Note that we cannot
3192     use the value of "length" to check for sufficient bytes left, because the
3193     other case of the character may have more or fewer bytes. */
3194 nigel 77
3195     else
3196     {
3197 nigel 93 unsigned int dc;
3198 nigel 77 GETCHARINC(dc, eptr);
3199     ecode += length;
3200    
3201     /* If we have Unicode property support, we can use it to test the other
3202 nigel 87 case of the character, if there is one. */
3203 nigel 77
3204     if (fc != dc)
3205     {
3206     #ifdef SUPPORT_UCP
3207 ph10 349 if (dc != UCD_OTHERCASE(fc))
3208 nigel 77 #endif
3209 ph10 836 RRETURN(MATCH_NOMATCH);
3210 nigel 77 }
3211     }
3212     }
3213     else
3214 ph10 836 #endif /* SUPPORT_UTF */
3215 nigel 77
3216 ph10 836 /* Not UTF mode */
3217 nigel 77 {
3218 ph10 836 if (TABLE_GET(ecode[1], md->lcc, ecode[1])
3219     != TABLE_GET(*eptr, md->lcc, *eptr)) RRETURN(MATCH_NOMATCH);
3220     eptr++;
3221 nigel 77 ecode += 2;
3222     }
3223     break;
3224    
3225 nigel 93 /* Match a single character repeatedly. */
3226 nigel 77
3227     case OP_EXACT:
3228 ph10 602 case OP_EXACTI:
3229 nigel 77 min = max = GET2(ecode, 1);
3230 ph10 836 ecode += 1 + IMM2_SIZE;
3231 nigel 77 goto REPEATCHAR;
3232    
3233 nigel 93 case OP_POSUPTO:
3234 ph10 602 case OP_POSUPTOI:
3235 nigel 93 possessive = TRUE;
3236     /* Fall through */
3237    
3238 nigel 77 case OP_UPTO:
3239 ph10 602 case OP_UPTOI:
3240 nigel 77 case OP_MINUPTO:
3241 ph10 602 case OP_MINUPTOI:
3242 nigel 77 min = 0;
3243     max = GET2(ecode, 1);
3244 ph10 602 minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI;
3245 ph10 836 ecode += 1 + IMM2_SIZE;
3246 nigel 77 goto REPEATCHAR;
3247    
3248 nigel 93 case OP_POSSTAR:
3249 ph10 602 case OP_POSSTARI:
3250 nigel 93 possessive = TRUE;
3251     min = 0;
3252     max = INT_MAX;
3253     ecode++;
3254     goto REPEATCHAR;
3255    
3256     case OP_POSPLUS:
3257 ph10 602 case OP_POSPLUSI:
3258 nigel 93 possessive = TRUE;
3259     min = 1;
3260     max = INT_MAX;
3261     ecode++;
3262     goto REPEATCHAR;
3263    
3264     case OP_POSQUERY:
3265 ph10 602 case OP_POSQUERYI:
3266 nigel 93 possessive = TRUE;
3267     min = 0;
3268     max = 1;
3269     ecode++;
3270     goto REPEATCHAR;
3271    
3272 nigel 77 case OP_STAR:
3273 ph10 602 case OP_STARI:
3274 nigel 77 case OP_MINSTAR:
3275 ph10 602 case OP_MINSTARI:
3276 nigel 77 case OP_PLUS:
3277 ph10 602 case OP_PLUSI:
3278 nigel 77 case OP_MINPLUS:
3279 ph10 602 case OP_MINPLUSI:
3280 nigel 77 case OP_QUERY:
3281 ph10 602 case OP_QUERYI:
3282 nigel 77 case OP_MINQUERY:
3283 ph10 602 case OP_MINQUERYI:
3284     c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI);
3285 nigel 77 minimize = (c & 1) != 0;
3286     min = rep_min[c]; /* Pick up values from tables; */
3287     max = rep_max[c]; /* zero for max => infinity */
3288     if (max == 0) max = INT_MAX;
3289    
3290 ph10 426 /* Common code for all repeated single-character matches. */
3291 nigel 77
3292     REPEATCHAR:
3293 ph10 836 #ifdef SUPPORT_UTF
3294     if (utf)
3295 nigel 77 {
3296     length = 1;
3297     charptr = ecode;
3298     GETCHARLEN(fc, ecode, length);
3299     ecode += length;
3300    
3301     /* Handle multibyte character matching specially here. There is
3302     support for caseless matching if UCP support is present. */
3303    
3304     if (length > 1)
3305     {
3306     #ifdef SUPPORT_UCP
3307 nigel 93 unsigned int othercase;
3308 ph10 602 if (op >= OP_STARI && /* Caseless */
3309 ph10 349 (othercase = UCD_OTHERCASE(fc)) != fc)
3310 ph10 836 oclength = PRIV(ord2utf)(othercase, occhars);
3311 ph10 115 else oclength = 0;
3312 nigel 77 #endif /* SUPPORT_UCP */
3313    
3314     for (i = 1; i <= min; i++)
3315     {
3316 ph10 426 if (eptr <= md->end_subject - length &&
3317 ph10 836 memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length;
3318 ph10 123 #ifdef SUPPORT_UCP
3319 ph10 426 else if (oclength > 0 &&
3320     eptr <= md->end_subject - oclength &&
3321 ph10 836 memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength;
3322 ph10 426 #endif /* SUPPORT_UCP */
3323 nigel 77 else
3324     {
3325 ph10 426 CHECK_PARTIAL();
3326 ph10 836 RRETURN(MATCH_NOMATCH);
3327 nigel 77 }
3328     }
3329    
3330     if (min == max) continue;
3331    
3332     if (minimize)
3333     {
3334     for (fi = min;; fi++)
3335     {
3336 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM22);
3337 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3338 ph10 836 if (fi >= max) RRETURN(MATCH_NOMATCH);
3339 ph10 426 if (eptr <= md->end_subject - length &&
3340 ph10 836 memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length;
3341 ph10 123 #ifdef SUPPORT_UCP
3342 ph10 426 else if (oclength > 0 &&
3343     eptr <= md->end_subject - oclength &&
3344 ph10 836 memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength;
3345 ph10 426 #endif /* SUPPORT_UCP */
3346 nigel 77 else
3347     {
3348 ph10 426 CHECK_PARTIAL();
3349 ph10 836 RRETURN(MATCH_NOMATCH);
3350 nigel 77 }
3351     }
3352     /* Control never gets here */
3353     }
3354 nigel 93
3355     else /* Maximize */
3356 nigel 77 {
3357     pp = eptr;
3358     for (i = min; i < max; i++)
3359     {
3360 ph10 426 if (eptr <= md->end_subject - length &&
3361 ph10 836 memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length;
3362 ph10 123 #ifdef SUPPORT_UCP
3363 ph10 426 else if (oclength > 0 &&
3364     eptr <= md->end_subject - oclength &&
3365 ph10 836 memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength;
3366 ph10 426 #endif /* SUPPORT_UCP */
3367 ph10 463 else
3368 ph10 462 {
3369 ph10 463 CHECK_PARTIAL();
3370 ph10 462 break;
3371 ph10 463 }
3372 nigel 77 }
3373 nigel 93
3374     if (possessive) continue;
3375 ph10 427
3376 ph10 120 for(;;)
3377 ph10 426 {
3378 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM23);
3379 ph10 426 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3380 ph10 836 if (eptr == pp) { RRETURN(MATCH_NOMATCH); }
3381 ph10 115 #ifdef SUPPORT_UCP
3382 ph10 426 eptr--;
3383     BACKCHAR(eptr);
3384 ph10 123 #else /* without SUPPORT_UCP */
3385 ph10 426 eptr -= length;
3386 ph10 123 #endif /* SUPPORT_UCP */
3387 ph10 426 }
3388 nigel 77 }
3389     /* Control never gets here */
3390     }
3391    
3392     /* If the length of a UTF-8 character is 1, we fall through here, and
3393     obey the code as for non-UTF-8 characters below, though in this case the
3394     value of fc will always be < 128. */
3395     }
3396     else
3397 ph10 836 #endif /* SUPPORT_UTF */
3398     /* When not in UTF-8 mode, load a single-byte character. */
3399     fc = *ecode++;
3400 nigel 77
3401 ph10 836 /* The value of fc at this point is always one character, though we may
3402     or may not be in UTF mode. The code is duplicated for the caseless and
3403 nigel 77 caseful cases, for speed, since matching characters is likely to be quite
3404     common. First, ensure the minimum number of matches are present. If min =
3405     max, continue at the same level without recursing. Otherwise, if
3406     minimizing, keep trying the rest of the expression and advancing one
3407     matching character if failing, up to the maximum. Alternatively, if
3408     maximizing, find the maximum number of characters and work backwards. */
3409    
3410     DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3411     max, eptr));
3412    
3413 ph10 602 if (op >= OP_STARI) /* Caseless */
3414 nigel 77 {
3415 ph10 836 #ifdef COMPILE_PCRE8
3416     /* fc must be < 128 if UTF is enabled. */
3417     foc = md->fcc[fc];
3418     #else
3419     #ifdef SUPPORT_UTF
3420     #ifdef SUPPORT_UCP
3421     if (utf && fc > 127)
3422     foc = UCD_OTHERCASE(fc);
3423     #else
3424     if (utf && fc > 127)
3425     foc = fc;
3426     #endif /* SUPPORT_UCP */
3427     else
3428     #endif /* SUPPORT_UTF */
3429     foc = TABLE_GET(fc, md->fcc, fc);
3430     #endif /* COMPILE_PCRE8 */
3431    
3432 nigel 77 for (i = 1; i <= min; i++)
3433 ph10 426 {
3434     if (eptr >= md->end_subject)
3435     {
3436     SCHECK_PARTIAL();
3437 ph10 836 RRETURN(MATCH_NOMATCH);
3438 ph10 426 }
3439 ph10 836 if (fc != *eptr && foc != *eptr) RRETURN(MATCH_NOMATCH);
3440     eptr++;
3441 ph10 426 }
3442 nigel 77 if (min == max) continue;
3443     if (minimize)
3444     {
3445     for (fi = min;; fi++)
3446     {
3447 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM24);
3448 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3449 ph10 836 if (fi >= max) RRETURN(MATCH_NOMATCH);
3450 ph10 426 if (eptr >= md->end_subject)
3451     {
3452 ph10 427 SCHECK_PARTIAL();
3453 ph10 836 RRETURN(MATCH_NOMATCH);
3454 ph10 426 }
3455 ph10 836 if (fc != *eptr && foc != *eptr) RRETURN(MATCH_NOMATCH);
3456     eptr++;
3457 nigel 77 }
3458     /* Control never gets here */
3459     }
3460 nigel 93 else /* Maximize */
3461 nigel 77 {
3462     pp = eptr;
3463     for (i = min; i < max; i++)
3464     {
3465 ph10 463 if (eptr >= md->end_subject)
3466 ph10 462 {
3467     SCHECK_PARTIAL();
3468     break;
3469 ph10 463 }
3470 ph10 836 if (fc != *eptr && foc != *eptr) break;
3471 nigel 77 eptr++;
3472     }
3473 ph10 427
3474 nigel 93 if (possessive) continue;
3475 ph10 427
3476 nigel 77 while (eptr >= pp)
3477     {
3478 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM25);
3479 nigel 77 eptr--;
3480     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3481     }
3482 ph10 836 RRETURN(MATCH_NOMATCH);
3483 nigel 77 }
3484     /* Control never gets here */
3485     }
3486    
3487     /* Caseful comparisons (includes all multi-byte characters) */
3488    
3489     else
3490     {
3491 ph10 427 for (i = 1; i <= min; i++)
3492 ph10 426 {
3493     if (eptr >= md->end_subject)
3494     {
3495     SCHECK_PARTIAL();
3496 ph10 836 RRETURN(MATCH_NOMATCH);
3497 ph10 426 }
3498 ph10 836 if (fc != *eptr++) RRETURN(MATCH_NOMATCH);
3499 ph10 427 }
3500 ph10 443
3501 nigel 77 if (min == max) continue;
3502 ph10 443
3503 nigel 77 if (minimize)
3504     {
3505     for (fi = min;; fi++)
3506     {
3507 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM26);
3508 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3509 ph10 836 if (fi >= max) RRETURN(MATCH_NOMATCH);
3510 ph10 426 if (eptr >= md->end_subject)
3511 ph10 427 {
3512 ph10 426 SCHECK_PARTIAL();
3513 ph10 836 RRETURN(MATCH_NOMATCH);
3514 ph10 427 }
3515 ph10 836 if (fc != *eptr++) RRETURN(MATCH_NOMATCH);
3516 nigel 77 }
3517     /* Control never gets here */
3518     }
3519 nigel 93 else /* Maximize */
3520 nigel 77 {
3521     pp = eptr;
3522     for (i = min; i < max; i++)
3523     {
3524 ph10 463 if (eptr >= md->end_subject)
3525 ph10 462 {
3526 ph10 463 SCHECK_PARTIAL();
3527 ph10 462 break;
3528 ph10 463 }
3529 ph10 462 if (fc != *eptr) break;
3530 nigel 77 eptr++;
3531     }
3532 nigel 93 if (possessive) continue;
3533 ph10 443
3534 nigel 77 while (eptr >= pp)
3535     {
3536 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM27);
3537 nigel 77 eptr--;
3538     if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3539     }
3540 ph10 836 RRETURN(MATCH_NOMATCH);
3541 nigel 77 }
3542     }
3543     /* Control never gets here */
3544    
3545     /* Match a negated single one-byte character. The character we are
3546     checking can be multibyte. */
3547    
3548     case OP_NOT:
3549 ph10 625 case OP_NOTI:
3550 ph10 443 if (eptr >= md->end_subject)
3551 ph10 428 {
3552 ph10 443 SCHECK_PARTIAL();
3553 ph10 836 RRETURN(MATCH_NOMATCH);
3554 ph10 443 }
3555 nigel 77 ecode++;
3556     GETCHARINCTEST(c, eptr);
3557 ph10 602 if (op == OP_NOTI) /* The caseless case */
3558 nigel 77 {
3559 ph10 904 register unsigned int ch, och;
3560 ph10 836 ch = *ecode++;
3561     #ifdef COMPILE_PCRE8
3562     /* ch must be < 128 if UTF is enabled. */
3563     och = md->fcc[ch];
3564     #else
3565     #ifdef SUPPORT_UTF
3566     #ifdef SUPPORT_UCP
3567     if (utf && ch > 127)
3568     och = UCD_OTHERCASE(ch);
3569     #else
3570     if (utf && ch > 127)
3571     och = ch;
3572     #endif /* SUPPORT_UCP */
3573     else
3574     #endif /* SUPPORT_UTF */
3575     och = TABLE_GET(ch, md->fcc, ch);
3576     #endif /* COMPILE_PCRE8 */
3577     if (ch == c || och == c) RRETURN(MATCH_NOMATCH);
3578 nigel 77 }
3579 ph10 602 else /* Caseful */
3580 nigel 77 {
3581 ph10 836 if (*ecode++ == c) RRETURN(MATCH_NOMATCH);
3582 nigel 77 }
3583     break;
3584    
3585     /* Match a negated single one-byte character repeatedly. This is almost a
3586     repeat of the code for a repeated single character, but I haven't found a
3587     nice way of commoning these up that doesn't require a test of the
3588     positive/negative option for each character match. Maybe that wouldn't add
3589     very much to the time taken, but character matching *is* what this is all
3590     about... */
3591    
3592     case OP_NOTEXACT:
3593 ph10 602 case OP_NOTEXACTI:
3594 nigel 77 min = max = GET2(ecode, 1);
3595 ph10 836 ecode += 1 + IMM2_SIZE;
3596 nigel 77 goto REPEATNOTCHAR;
3597    
3598     case OP_NOTUPTO:
3599 ph10 602 case OP_NOTUPTOI:
3600 nigel 77 case OP_NOTMINUPTO:
3601 ph10 602 case OP_NOTMINUPTOI:
3602 nigel 77 min = 0;
3603     max = GET2(ecode, 1);
3604 ph10 602 minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI;
3605 ph10 836 ecode += 1 + IMM2_SIZE;
3606 nigel 77 goto REPEATNOTCHAR;
3607    
3608 nigel 93 case OP_NOTPOSSTAR:
3609 ph10 602 case OP_NOTPOSSTARI:
3610 nigel 93 possessive = TRUE;
3611     min = 0;
3612     max = INT_MAX;
3613     ecode++;
3614     goto REPEATNOTCHAR;
3615    
3616     case OP_NOTPOSPLUS:
3617 ph10 602 case OP_NOTPOSPLUSI:
3618 nigel 93 possessive = TRUE;
3619     min = 1;
3620     max = INT_MAX;
3621     ecode++;
3622     goto REPEATNOTCHAR;
3623    
3624     case OP_NOTPOSQUERY:
3625 ph10 602 case OP_NOTPOSQUERYI:
3626 nigel 93 possessive = TRUE;
3627     min = 0;
3628     max = 1;
3629     ecode++;
3630     goto REPEATNOTCHAR;
3631    
3632     case OP_NOTPOSUPTO:
3633 ph10 602 case OP_NOTPOSUPTOI:
3634 nigel 93 possessive = TRUE;
3635     min = 0;
3636     max = GET2(ecode, 1);
3637 ph10 836 ecode += 1 + IMM2_SIZE;
3638 nigel 93 goto REPEATNOTCHAR;
3639    
3640 nigel 77 case OP_NOTSTAR:
3641 ph10 602 case OP_NOTSTARI:
3642 nigel 77 case OP_NOTMINSTAR:
3643 ph10 602 case OP_NOTMINSTARI:
3644 nigel 77 case OP_NOTPLUS:
3645 ph10 602 case OP_NOTPLUSI:
3646 nigel 77 case OP_NOTMINPLUS:
3647 ph10 602 case OP_NOTMINPLUSI:
3648 nigel 77 case OP_NOTQUERY:
3649 ph10 602 case OP_NOTQUERYI:
3650 nigel 77 case OP_NOTMINQUERY:
3651 ph10 602 case OP_NOTMINQUERYI:
3652     c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR);
3653 nigel 77 minimize = (c & 1) != 0;
3654     min = rep_min[c]; /* Pick up values from tables; */
3655     max = rep_max[c]; /* zero for max => infinity */
3656     if (max == 0) max = INT_MAX;
3657    
3658 ph10 426 /* Common code for all repeated single-byte matches. */
3659 nigel 77
3660     REPEATNOTCHAR:
3661     fc = *ecode++;
3662    
3663     /* The code is duplicated for the caseless and caseful cases, for speed,
3664     since matching characters is likely to be quite common. First, ensure the
3665     minimum number of matches are present. If min = max, continue at the same
3666     level without recursing. Otherwise, if minimizing, keep trying the rest of
3667     the expression and advancing one matching character if failing, up to the
3668     maximum. Alternatively, if maximizing, find the maximum number of
3669     characters and work backwards. */
3670    
3671     DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max,
3672     max, eptr));
3673    
3674 ph10 602 if (op >= OP_NOTSTARI) /* Caseless */
3675 nigel 77 {
3676 ph10 836 #ifdef COMPILE_PCRE8
3677     /* fc must be < 128 if UTF is enabled. */
3678     foc = md->fcc[fc];
3679     #else
3680     #ifdef SUPPORT_UTF
3681     #ifdef SUPPORT_UCP
3682     if (utf && fc > 127)
3683     foc = UCD_OTHERCASE(fc);
3684     #else
3685     if (utf && fc > 127)
3686     foc = fc;
3687     #endif /* SUPPORT_UCP */
3688     else
3689     #endif /* SUPPORT_UTF */
3690     foc = TABLE_GET(fc, md->fcc, fc);
3691     #endif /* COMPILE_PCRE8 */
3692 nigel 77
3693 ph10 836 #ifdef SUPPORT_UTF
3694     if (utf)
3695 nigel 77 {
3696 nigel 93 register unsigned int d;
3697 nigel 77 for (i = 1; i <= min; i++)
3698     {
3699 ph10 426 if (eptr >= md->end_subject)
3700     {
3701     SCHECK_PARTIAL();
3702 ph10 836 RRETURN(MATCH_NOMATCH);
3703 ph10 427 }
3704 nigel 77 GETCHARINC(d, eptr);
3705 ph10 904 if (fc == d || (unsigned int) foc == d) RRETURN(MATCH_NOMATCH);
3706 nigel 77 }
3707     }
3708     else
3709     #endif
3710 ph10 836 /* Not UTF mode */
3711 nigel 77 {
3712     for (i = 1; i <= min; i++)
3713 ph10 426 {
3714     if (eptr >= md->end_subject)
3715     {
3716     SCHECK_PARTIAL();
3717 ph10 836 RRETURN(MATCH_NOMATCH);
3718 ph10 427 }
3719 ph10 836 if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH);
3720     eptr++;
3721 ph10 427 }
3722 nigel 77 }
3723    
3724     if (min == max) continue;
3725    
3726     if (minimize)
3727     {
3728 ph10 836 #ifdef SUPPORT_UTF
3729     if (utf)
3730 nigel 77 {
3731 nigel 93 register unsigned int d;
3732 nigel 77 for (fi = min;; fi++)
3733     {
3734 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM28);
3735 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3736 ph10 836 if (fi >= max) RRETURN(MATCH_NOMATCH);
3737 ph10 427 if (eptr >= md->end_subject)
3738 ph10 426 {
3739 ph10 427 SCHECK_PARTIAL();
3740 ph10 836 RRETURN(MATCH_NOMATCH);
3741 ph10 427 }
3742 nigel 77 GETCHARINC(d, eptr);
3743 ph10 904 if (fc == d || (unsigned int)foc == d) RRETURN(MATCH_NOMATCH);
3744 nigel 77 }
3745     }
3746     else
3747     #endif
3748 ph10 836 /* Not UTF mode */
3749 nigel 77 {
3750     for (fi = min;; fi++)
3751     {
3752 ph10 604 RMATCH(eptr, ecode, offset_top, md, eptrb, RM29);
3753 nigel 77 if (rrc != MATCH_NOMATCH) RRETURN(rrc);
3754 ph10 836 if (fi >= max) RRETURN(MATCH_NOMATCH);
3755 ph10 426 if (eptr >= md->end_subject)
3756     {
3757     SCHECK_PARTIAL();
3758 ph10 836 RRETURN(MATCH_NOMATCH);
3759 ph10 426 }
3760 ph10 836 if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH);
3761     eptr++;
3762 nigel 77 }
3763     }
3764     /* Control never gets here */
3765     }
3766    
3767     /* Maximize case */
3768    
3769     else
3770     {
3771     pp = eptr;
3772    
3773 ph10 836 #ifdef SUPPORT_UTF
3774     if (utf)
3775 nigel 77 {
3776 nigel 93 register unsigned int d;
3777 nigel 77 for (i = min; i < max; i++)
3778     {
3779     int len = 1;
3780 ph10 463 if (eptr >= md->end_subject)
3781 ph10 462