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

Contents of /code/trunk/pcre_exec.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 934 - (hide annotations) (download)
Sat Feb 25 12:30:36 2012 UTC (14 months, 4 weeks ago) by ph10
File MIME type: text/plain
File size: 218923 byte(s)
Stop (*COMMIT) escaping from a recursive subroutine call.

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