Parent Directory
|
Revision Log
Applied Graycode's patch to use heap stack frames more efficiently.
| 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 | /* PCRE does not allow THEN to escape beyond a recursion; it is treated | ||
| 1793 | as NOMATCH. */ | ||
| 1794 | |||
| 1795 | ph10 | 733 | else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) |
| 1796 | nigel | 87 | { |
| 1797 | DPRINTF(("Recursion gave error %d\n", rrc)); | ||
| 1798 | ph10 | 400 | if (new_recursive.offset_save != stacksave) |
| 1799 | ph10 | 836 | (PUBL(free))(new_recursive.offset_save); |
| 1800 | nigel | 87 | RRETURN(rrc); |
| 1801 | } | ||
| 1802 | nigel | 77 | |
| 1803 | md->recursive = &new_recursive; | ||
| 1804 | callpat += GET(callpat, 1); | ||
| 1805 | } | ||
| 1806 | while (*callpat == OP_ALT); | ||
| 1807 | |||
| 1808 | DPRINTF(("Recursion didn't match\n")); | ||
| 1809 | md->recursive = new_recursive.prevrec; | ||
| 1810 | if (new_recursive.offset_save != stacksave) | ||
| 1811 | ph10 | 836 | (PUBL(free))(new_recursive.offset_save); |
| 1812 | RRETURN(MATCH_NOMATCH); | ||
| 1813 | nigel | 77 | } |
| 1814 | ph10 | 625 | |
| 1815 | ph10 | 618 | RECURSION_MATCHED: |
| 1816 | break; | ||
| 1817 | nigel | 77 | |
| 1818 | /* An alternation is the end of a branch; scan along to find the end of the | ||
| 1819 | bracketed group and go to there. */ | ||
| 1820 | |||
| 1821 | case OP_ALT: | ||
| 1822 | do ecode += GET(ecode,1); while (*ecode == OP_ALT); | ||
| 1823 | break; | ||
| 1824 | |||
| 1825 | ph10 | 335 | /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group, |
| 1826 | indicating that it may occur zero times. It may repeat infinitely, or not | ||
| 1827 | at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets | ||
| 1828 | with fixed upper repeat limits are compiled as a number of copies, with the | ||
| 1829 | optional ones preceded by BRAZERO or BRAMINZERO. */ | ||
| 1830 | ph10 | 625 | |
| 1831 | nigel | 77 | case OP_BRAZERO: |
| 1832 | ph10 | 604 | next = ecode + 1; |
| 1833 | RMATCH(eptr, next, offset_top, md, eptrb, RM10); | ||
| 1834 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); | ||
| 1835 | do next += GET(next, 1); while (*next == OP_ALT); | ||
| 1836 | ecode = next + 1 + LINK_SIZE; | ||
| 1837 | nigel | 77 | break; |
| 1838 | ph10 | 625 | |
| 1839 | nigel | 77 | case OP_BRAMINZERO: |
| 1840 | ph10 | 604 | next = ecode + 1; |
| 1841 | do next += GET(next, 1); while (*next == OP_ALT); | ||
| 1842 | RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, eptrb, RM11); | ||
| 1843 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); | ||
| 1844 | ecode++; | ||
| 1845 | nigel | 77 | break; |
| 1846 | |||
| 1847 | ph10 | 335 | case OP_SKIPZERO: |
| 1848 | ph10 | 604 | next = ecode+1; |
| 1849 | do next += GET(next,1); while (*next == OP_ALT); | ||
| 1850 | ecode = next + 1 + LINK_SIZE; | ||
| 1851 | ph10 | 335 | break; |
| 1852 | ph10 | 625 | |
| 1853 | ph10 | 604 | /* BRAPOSZERO occurs before a possessive bracket group. Don't do anything |
| 1854 | here; just jump to the group, with allow_zero set TRUE. */ | ||
| 1855 | ph10 | 625 | |
| 1856 | ph10 | 604 | case OP_BRAPOSZERO: |
| 1857 | ph10 | 625 | op = *(++ecode); |
| 1858 | ph10 | 604 | allow_zero = TRUE; |
| 1859 | if (op == OP_CBRAPOS || op == OP_SCBRAPOS) goto POSSESSIVE_CAPTURE; | ||
| 1860 | goto POSSESSIVE_NON_CAPTURE; | ||
| 1861 | ph10 | 335 | |
| 1862 | nigel | 93 | /* End of a group, repeated or non-repeating. */ |
| 1863 | nigel | 77 | |
| 1864 | case OP_KET: | ||
| 1865 | case OP_KETRMIN: | ||
| 1866 | case OP_KETRMAX: | ||
| 1867 | ph10 | 625 | case OP_KETRPOS: |
| 1868 | nigel | 91 | prev = ecode - GET(ecode, 1); |
| 1869 | ph10 | 625 | |
| 1870 | nigel | 93 | /* If this was a group that remembered the subject start, in order to break |
| 1871 | infinite repeats of empty string matches, retrieve the subject start from | ||
| 1872 | the chain. Otherwise, set it NULL. */ | ||
| 1873 | nigel | 77 | |
| 1874 | ph10 | 618 | if (*prev >= OP_SBRA || *prev == OP_ONCE) |
| 1875 | nigel | 93 | { |
| 1876 | saved_eptr = eptrb->epb_saved_eptr; /* Value at start of group */ | ||
| 1877 | eptrb = eptrb->epb_prev; /* Backup to previous group */ | ||
| 1878 | } | ||
| 1879 | else saved_eptr = NULL; | ||
| 1880 | nigel | 77 | |
| 1881 | ph10 | 733 | /* If we are at the end of an assertion group or a non-capturing atomic |
| 1882 | ph10 | 723 | group, stop matching and return MATCH_MATCH, but record the current high |
| 1883 | water mark for use by positive assertions. We also need to record the match | ||
| 1884 | start in case it was changed by \K. */ | ||
| 1885 | nigel | 93 | |
| 1886 | ph10 | 723 | if ((*prev >= OP_ASSERT && *prev <= OP_ASSERTBACK_NOT) || |
| 1887 | ph10 | 733 | *prev == OP_ONCE_NC) |
| 1888 | nigel | 91 | { |
| 1889 | ph10 | 723 | md->end_match_ptr = eptr; /* For ONCE_NC */ |
| 1890 | nigel | 91 | md->end_offset_top = offset_top; |
| 1891 | ph10 | 500 | md->start_match_ptr = mstart; |
| 1892 | ph10 | 836 | RRETURN(MATCH_MATCH); /* Sets md->mark */ |
| 1893 | nigel | 91 | } |
| 1894 | nigel | 77 | |
| 1895 | nigel | 93 | /* For capturing groups we have to check the group number back at the start |
| 1896 | and if necessary complete handling an extraction by setting the offsets and | ||
| 1897 | ph10 | 618 | bumping the high water mark. Whole-pattern recursion is coded as a recurse |
| 1898 | into group 0, so it won't be picked up here. Instead, we catch it when the | ||
| 1899 | OP_END is reached. Other recursion is handled here. We just have to record | ||
| 1900 | the current subject position and start match pointer and give a MATCH | ||
| 1901 | return. */ | ||
| 1902 | nigel | 77 | |
| 1903 | ph10 | 604 | if (*prev == OP_CBRA || *prev == OP_SCBRA || |
| 1904 | *prev == OP_CBRAPOS || *prev == OP_SCBRAPOS) | ||
| 1905 | nigel | 91 | { |
| 1906 | nigel | 93 | number = GET2(prev, 1+LINK_SIZE); |
| 1907 | nigel | 91 | offset = number << 1; |
| 1908 | ph10 | 461 | |
| 1909 | ph10 | 475 | #ifdef PCRE_DEBUG |
| 1910 | nigel | 91 | printf("end bracket %d", number); |
| 1911 | printf("\n"); | ||
| 1912 | nigel | 77 | #endif |
| 1913 | |||
| 1914 | ph10 | 618 | /* Handle a recursively called group. */ |
| 1915 | |||
| 1916 | if (md->recursive != NULL && md->recursive->group_num == number) | ||
| 1917 | { | ||
| 1918 | md->end_match_ptr = eptr; | ||
| 1919 | md->start_match_ptr = mstart; | ||
| 1920 | RRETURN(MATCH_MATCH); | ||
| 1921 | } | ||
| 1922 | |||
| 1923 | /* Deal with capturing */ | ||
| 1924 | |||
| 1925 | nigel | 93 | md->capture_last = number; |
| 1926 | if (offset >= md->offset_max) md->offset_overflow = TRUE; else | ||
| 1927 | nigel | 91 | { |
| 1928 | ph10 | 625 | /* If offset is greater than offset_top, it means that we are |
| 1929 | "skipping" a capturing group, and that group's offsets must be marked | ||
| 1930 | unset. In earlier versions of PCRE, all the offsets were unset at the | ||
| 1931 | start of matching, but this doesn't work because atomic groups and | ||
| 1932 | ph10 | 615 | assertions can cause a value to be set that should later be unset. |
| 1933 | Example: matching /(?>(a))b|(a)c/ against "ac". This sets group 1 as | ||
| 1934 | ph10 | 625 | part of the atomic group, but this is not on the final matching path, |
| 1935 | so must be unset when 2 is set. (If there is no group 2, there is no | ||
| 1936 | ph10 | 615 | problem, because offset_top will then be 2, indicating no capture.) */ |
| 1937 | ph10 | 625 | |
| 1938 | ph10 | 615 | if (offset > offset_top) |
| 1939 | { | ||
| 1940 | register int *iptr = md->offset_vector + offset_top; | ||
| 1941 | register int *iend = md->offset_vector + offset; | ||
| 1942 | while (iptr < iend) *iptr++ = -1; | ||
| 1943 | ph10 | 625 | } |
| 1944 | |||
| 1945 | ph10 | 615 | /* Now make the extraction */ |
| 1946 | |||
| 1947 | nigel | 93 | md->offset_vector[offset] = |
| 1948 | md->offset_vector[md->offset_end - number]; | ||
| 1949 | ph10 | 530 | md->offset_vector[offset+1] = (int)(eptr - md->start_subject); |
| 1950 | nigel | 93 | if (offset_top <= offset) offset_top = offset + 2; |
| 1951 | } | ||
| 1952 | nigel | 91 | } |
| 1953 | nigel | 77 | |
| 1954 | ph10 | 618 | /* For an ordinary non-repeating ket, just continue at this level. This |
| 1955 | also happens for a repeating ket if no characters were matched in the | ||
| 1956 | group. This is the forcible breaking of infinite loops as implemented in | ||
| 1957 | ph10 | 723 | Perl 5.005. For a non-repeating atomic group that includes captures, |
| 1958 | establish a backup point by processing the rest of the pattern at a lower | ||
| 1959 | level. If this results in a NOMATCH return, pass MATCH_ONCE back to the | ||
| 1960 | original OP_ONCE level, thereby bypassing intermediate backup points, but | ||
| 1961 | resetting any captures that happened along the way. */ | ||
| 1962 | nigel | 77 | |
| 1963 | nigel | 91 | if (*ecode == OP_KET || eptr == saved_eptr) |
| 1964 | { | ||
| 1965 | ph10 | 618 | if (*prev == OP_ONCE) |
| 1966 | { | ||
| 1967 | RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM12); | ||
| 1968 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); | ||
| 1969 | md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */ | ||
| 1970 | ph10 | 625 | RRETURN(MATCH_ONCE); |
| 1971 | } | ||
| 1972 | ph10 | 618 | ecode += 1 + LINK_SIZE; /* Carry on at this level */ |
| 1973 | nigel | 91 | break; |
| 1974 | } | ||
| 1975 | ph10 | 625 | |
| 1976 | /* OP_KETRPOS is a possessive repeating ket. Remember the current position, | ||
| 1977 | ph10 | 604 | and return the MATCH_KETRPOS. This makes it possible to do the repeats one |
| 1978 | at a time from the outer level, thus saving stack. */ | ||
| 1979 | ph10 | 625 | |
| 1980 | ph10 | 604 | if (*ecode == OP_KETRPOS) |
| 1981 | ph10 | 625 | { |
| 1982 | ph10 | 604 | md->end_match_ptr = eptr; |
| 1983 | ph10 | 625 | md->end_offset_top = offset_top; |
| 1984 | ph10 | 604 | RRETURN(MATCH_KETRPOS); |
| 1985 | ph10 | 625 | } |
| 1986 | nigel | 77 | |
| 1987 | ph10 | 604 | /* The normal repeating kets try the rest of the pattern or restart from |
| 1988 | the preceding bracket, in the appropriate order. In the second case, we can | ||
| 1989 | use tail recursion to avoid using another stack frame, unless we have an | ||
| 1990 | ph10 | 618 | an atomic group or an unlimited repeat of a group that can match an empty |
| 1991 | string. */ | ||
| 1992 | nigel | 77 | |
| 1993 | nigel | 91 | if (*ecode == OP_KETRMIN) |
| 1994 | { | ||
| 1995 | ph10 | 623 | RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM7); |
| 1996 | nigel | 91 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 1997 | ph10 | 618 | if (*prev == OP_ONCE) |
| 1998 | { | ||
| 1999 | ph10 | 623 | RMATCH(eptr, prev, offset_top, md, eptrb, RM8); |
| 2000 | ph10 | 618 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 2001 | md->once_target = prev; /* Level at which to change to MATCH_NOMATCH */ | ||
| 2002 | ph10 | 625 | RRETURN(MATCH_ONCE); |
| 2003 | } | ||
| 2004 | ph10 | 604 | if (*prev >= OP_SBRA) /* Could match an empty string */ |
| 2005 | ph10 | 197 | { |
| 2006 | ph10 | 625 | md->match_function_type = MATCH_CBEGROUP; |
| 2007 | ph10 | 604 | RMATCH(eptr, prev, offset_top, md, eptrb, RM50); |
| 2008 | ph10 | 197 | RRETURN(rrc); |
| 2009 | } | ||
| 2010 | nigel | 91 | ecode = prev; |
| 2011 | goto TAIL_RECURSE; | ||
| 2012 | nigel | 77 | } |
| 2013 | nigel | 91 | else /* OP_KETRMAX */ |
| 2014 | { | ||
| 2015 | ph10 | 625 | if (*prev >= OP_SBRA) md->match_function_type = MATCH_CBEGROUP; |
| 2016 | ph10 | 604 | RMATCH(eptr, prev, offset_top, md, eptrb, RM13); |
| 2017 | ph10 | 618 | if (rrc == MATCH_ONCE && md->once_target == prev) rrc = MATCH_NOMATCH; |
| 2018 | nigel | 91 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 2019 | ph10 | 618 | if (*prev == OP_ONCE) |
| 2020 | { | ||
| 2021 | ph10 | 623 | RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, eptrb, RM9); |
| 2022 | ph10 | 618 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 2023 | md->once_target = prev; | ||
| 2024 | ph10 | 625 | RRETURN(MATCH_ONCE); |
| 2025 | } | ||
| 2026 | nigel | 91 | ecode += 1 + LINK_SIZE; |
| 2027 | goto TAIL_RECURSE; | ||
| 2028 | } | ||
| 2029 | /* Control never gets here */ | ||
| 2030 | nigel | 77 | |
| 2031 | ph10 | 602 | /* Not multiline mode: start of subject assertion, unless notbol. */ |
| 2032 | nigel | 77 | |
| 2033 | case OP_CIRC: | ||
| 2034 | ph10 | 836 | if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH); |
| 2035 | ph10 | 625 | |
| 2036 | nigel | 77 | /* Start of subject assertion */ |
| 2037 | |||
| 2038 | case OP_SOD: | ||
| 2039 | ph10 | 836 | if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH); |
| 2040 | nigel | 77 | ecode++; |
| 2041 | break; | ||
| 2042 | ph10 | 625 | |
| 2043 | ph10 | 602 | /* Multiline mode: start of subject unless notbol, or after any newline. */ |
| 2044 | nigel | 77 | |
| 2045 | ph10 | 602 | case OP_CIRCM: |
| 2046 | ph10 | 836 | if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH); |
| 2047 | ph10 | 602 | if (eptr != md->start_subject && |
| 2048 | (eptr == md->end_subject || !WAS_NEWLINE(eptr))) | ||
| 2049 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2050 | ph10 | 602 | ecode++; |
| 2051 | break; | ||
| 2052 | |||
| 2053 | nigel | 77 | /* Start of match assertion */ |
| 2054 | |||
| 2055 | case OP_SOM: | ||
| 2056 | ph10 | 836 | if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH); |
| 2057 | nigel | 77 | ecode++; |
| 2058 | break; | ||
| 2059 | ph10 | 172 | |
| 2060 | ph10 | 168 | /* Reset the start of match point */ |
| 2061 | ph10 | 172 | |
| 2062 | ph10 | 168 | case OP_SET_SOM: |
| 2063 | mstart = eptr; | ||
| 2064 | ph10 | 172 | ecode++; |
| 2065 | break; | ||
| 2066 | nigel | 77 | |
| 2067 | ph10 | 602 | /* Multiline mode: assert before any newline, or before end of subject |
| 2068 | unless noteol is set. */ | ||
| 2069 | nigel | 77 | |
| 2070 | ph10 | 602 | case OP_DOLLM: |
| 2071 | if (eptr < md->end_subject) | ||
| 2072 | ph10 | 933 | { |
| 2073 | if (!IS_NEWLINE(eptr)) | ||
| 2074 | ph10 | 916 | { |
| 2075 | ph10 | 919 | if (md->partial != 0 && |
| 2076 | eptr + 1 >= md->end_subject && | ||
| 2077 | ph10 | 916 | NLBLOCK->nltype == NLTYPE_FIXED && |
| 2078 | ph10 | 933 | NLBLOCK->nllen == 2 && |
| 2079 | ph10 | 916 | *eptr == NLBLOCK->nl[0]) |
| 2080 | ph10 | 933 | { |
| 2081 | ph10 | 916 | md->hitend = TRUE; |
| 2082 | if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); | ||
| 2083 | } | ||
| 2084 | ph10 | 933 | RRETURN(MATCH_NOMATCH); |
| 2085 | } | ||
| 2086 | ph10 | 916 | } |
| 2087 | ph10 | 602 | else |
| 2088 | nigel | 77 | { |
| 2089 | ph10 | 836 | if (md->noteol) RRETURN(MATCH_NOMATCH); |
| 2090 | ph10 | 602 | SCHECK_PARTIAL(); |
| 2091 | nigel | 77 | } |
| 2092 | ph10 | 602 | ecode++; |
| 2093 | break; | ||
| 2094 | ph10 | 579 | |
| 2095 | ph10 | 625 | /* Not multiline mode: assert before a terminating newline or before end of |
| 2096 | ph10 | 602 | subject unless noteol is set. */ |
| 2097 | |||
| 2098 | case OP_DOLL: | ||
| 2099 | ph10 | 836 | if (md->noteol) RRETURN(MATCH_NOMATCH); |
| 2100 | ph10 | 602 | if (!md->endonly) goto ASSERT_NL_OR_EOS; |
| 2101 | |||
| 2102 | nigel | 91 | /* ... else fall through for endonly */ |
| 2103 | nigel | 77 | |
| 2104 | /* End of subject assertion (\z) */ | ||
| 2105 | |||
| 2106 | case OP_EOD: | ||
| 2107 | ph10 | 836 | if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH); |
| 2108 | ph10 | 553 | SCHECK_PARTIAL(); |
| 2109 | nigel | 77 | ecode++; |
| 2110 | break; | ||
| 2111 | |||
| 2112 | /* End of subject or ending \n assertion (\Z) */ | ||
| 2113 | |||
| 2114 | case OP_EODN: | ||
| 2115 | ph10 | 553 | ASSERT_NL_OR_EOS: |
| 2116 | if (eptr < md->end_subject && | ||
| 2117 | nigel | 93 | (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen)) |
| 2118 | ph10 | 916 | { |
| 2119 | ph10 | 919 | if (md->partial != 0 && |
| 2120 | eptr + 1 >= md->end_subject && | ||
| 2121 | ph10 | 916 | NLBLOCK->nltype == NLTYPE_FIXED && |
| 2122 | ph10 | 933 | NLBLOCK->nllen == 2 && |
| 2123 | ph10 | 916 | *eptr == NLBLOCK->nl[0]) |
| 2124 | ph10 | 933 | { |
| 2125 | ph10 | 916 | md->hitend = TRUE; |
| 2126 | if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); | ||
| 2127 | } | ||
| 2128 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2129 | ph10 | 933 | } |
| 2130 | ph10 | 579 | |
| 2131 | ph10 | 553 | /* Either at end of string or \n before end. */ |
| 2132 | ph10 | 579 | |
| 2133 | ph10 | 553 | SCHECK_PARTIAL(); |
| 2134 | nigel | 77 | ecode++; |
| 2135 | break; | ||
| 2136 | |||
| 2137 | /* Word boundary assertions */ | ||
| 2138 | |||
| 2139 | case OP_NOT_WORD_BOUNDARY: | ||
| 2140 | case OP_WORD_BOUNDARY: | ||
| 2141 | { | ||
| 2142 | |||
| 2143 | /* Find out if the previous and current characters are "word" characters. | ||
| 2144 | It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to | ||
| 2145 | ph10 | 443 | be "non-word" characters. Remember the earliest consulted character for |
| 2146 | ph10 | 435 | partial matching. */ |
| 2147 | nigel | 77 | |
| 2148 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 2149 | if (utf) | ||
| 2150 | nigel | 77 | { |
| 2151 | ph10 | 518 | /* Get status of previous character */ |
| 2152 | ph10 | 527 | |
| 2153 | nigel | 77 | if (eptr == md->start_subject) prev_is_word = FALSE; else |
| 2154 | { | ||
| 2155 | ph10 | 836 | PCRE_PUCHAR lastptr = eptr - 1; |
| 2156 | BACKCHAR(lastptr); | ||
| 2157 | ph10 | 443 | if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr; |
| 2158 | nigel | 77 | GETCHAR(c, lastptr); |
| 2159 | ph10 | 527 | #ifdef SUPPORT_UCP |
| 2160 | ph10 | 518 | if (md->use_ucp) |
| 2161 | { | ||
| 2162 | if (c == '_') prev_is_word = TRUE; else | ||
| 2163 | ph10 | 527 | { |
| 2164 | ph10 | 518 | int cat = UCD_CATEGORY(c); |
| 2165 | prev_is_word = (cat == ucp_L || cat == ucp_N); | ||
| 2166 | ph10 | 527 | } |
| 2167 | } | ||
| 2168 | else | ||
| 2169 | #endif | ||
| 2170 | nigel | 77 | prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0; |
| 2171 | } | ||
| 2172 | ph10 | 527 | |
| 2173 | ph10 | 518 | /* Get status of next character */ |
| 2174 | ph10 | 527 | |
| 2175 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2176 | nigel | 77 | { |
| 2177 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2178 | cur_is_word = FALSE; | ||
| 2179 | ph10 | 428 | } |
| 2180 | else | ||
| 2181 | { | ||
| 2182 | nigel | 77 | GETCHAR(c, eptr); |
| 2183 | ph10 | 527 | #ifdef SUPPORT_UCP |
| 2184 | ph10 | 518 | if (md->use_ucp) |
| 2185 | { | ||
| 2186 | if (c == '_') cur_is_word = TRUE; else | ||
| 2187 | ph10 | 527 | { |
| 2188 | ph10 | 518 | int cat = UCD_CATEGORY(c); |
| 2189 | cur_is_word = (cat == ucp_L || cat == ucp_N); | ||
| 2190 | ph10 | 527 | } |
| 2191 | } | ||
| 2192 | else | ||
| 2193 | #endif | ||
| 2194 | nigel | 77 | cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0; |
| 2195 | } | ||
| 2196 | } | ||
| 2197 | else | ||
| 2198 | #endif | ||
| 2199 | |||
| 2200 | ph10 | 527 | /* Not in UTF-8 mode, but we may still have PCRE_UCP set, and for |
| 2201 | ph10 | 518 | consistency with the behaviour of \w we do use it in this case. */ |
| 2202 | nigel | 77 | |
| 2203 | { | ||
| 2204 | ph10 | 518 | /* Get status of previous character */ |
| 2205 | ph10 | 527 | |
| 2206 | ph10 | 435 | if (eptr == md->start_subject) prev_is_word = FALSE; else |
| 2207 | { | ||
| 2208 | ph10 | 443 | if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1; |
| 2209 | ph10 | 527 | #ifdef SUPPORT_UCP |
| 2210 | ph10 | 518 | if (md->use_ucp) |
| 2211 | { | ||
| 2212 | ph10 | 527 | c = eptr[-1]; |
| 2213 | ph10 | 518 | if (c == '_') prev_is_word = TRUE; else |
| 2214 | ph10 | 527 | { |
| 2215 | ph10 | 518 | int cat = UCD_CATEGORY(c); |
| 2216 | prev_is_word = (cat == ucp_L || cat == ucp_N); | ||
| 2217 | ph10 | 527 | } |
| 2218 | } | ||
| 2219 | else | ||
| 2220 | #endif | ||
| 2221 | ph10 | 836 | prev_is_word = MAX_255(eptr[-1]) |
| 2222 | && ((md->ctypes[eptr[-1]] & ctype_word) != 0); | ||
| 2223 | ph10 | 435 | } |
| 2224 | ph10 | 527 | |
| 2225 | ph10 | 518 | /* Get status of next character */ |
| 2226 | ph10 | 527 | |
| 2227 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2228 | ph10 | 428 | { |
| 2229 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2230 | cur_is_word = FALSE; | ||
| 2231 | ph10 | 428 | } |
| 2232 | ph10 | 527 | else |
| 2233 | #ifdef SUPPORT_UCP | ||
| 2234 | ph10 | 518 | if (md->use_ucp) |
| 2235 | { | ||
| 2236 | ph10 | 527 | c = *eptr; |
| 2237 | ph10 | 518 | if (c == '_') cur_is_word = TRUE; else |
| 2238 | ph10 | 527 | { |
| 2239 | ph10 | 518 | int cat = UCD_CATEGORY(c); |
| 2240 | cur_is_word = (cat == ucp_L || cat == ucp_N); | ||
| 2241 | ph10 | 527 | } |
| 2242 | } | ||
| 2243 | else | ||
| 2244 | #endif | ||
| 2245 | ph10 | 836 | cur_is_word = MAX_255(*eptr) |
| 2246 | && ((md->ctypes[*eptr] & ctype_word) != 0); | ||
| 2247 | nigel | 77 | } |
| 2248 | |||
| 2249 | /* Now see if the situation is what we want */ | ||
| 2250 | |||
| 2251 | if ((*ecode++ == OP_WORD_BOUNDARY)? | ||
| 2252 | cur_is_word == prev_is_word : cur_is_word != prev_is_word) | ||
| 2253 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2254 | nigel | 77 | } |
| 2255 | break; | ||
| 2256 | |||
| 2257 | ph10 | 919 | /* Match any single character type except newline; have to take care with |
| 2258 | CRLF newlines and partial matching. */ | ||
| 2259 | nigel | 77 | |
| 2260 | case OP_ANY: | ||
| 2261 | ph10 | 836 | if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); |
| 2262 | ph10 | 919 | if (md->partial != 0 && |
| 2263 | eptr + 1 >= md->end_subject && | ||
| 2264 | NLBLOCK->nltype == NLTYPE_FIXED && | ||
| 2265 | ph10 | 933 | NLBLOCK->nllen == 2 && |
| 2266 | ph10 | 919 | *eptr == NLBLOCK->nl[0]) |
| 2267 | ph10 | 933 | { |
| 2268 | ph10 | 919 | md->hitend = TRUE; |
| 2269 | if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); | ||
| 2270 | } | ||
| 2271 | ph10 | 933 | |
| 2272 | ph10 | 345 | /* Fall through */ |
| 2273 | |||
| 2274 | ph10 | 919 | /* Match any single character whatsoever. */ |
| 2275 | ph10 | 933 | |
| 2276 | ph10 | 341 | case OP_ALLANY: |
| 2277 | ph10 | 648 | if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */ |
| 2278 | { /* not be updated before SCHECK_PARTIAL. */ | ||
| 2279 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2280 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2281 | ph10 | 443 | } |
| 2282 | ph10 | 648 | eptr++; |
| 2283 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 2284 | if (utf) ACROSSCHAR(eptr < md->end_subject, *eptr, eptr++); | ||
| 2285 | #endif | ||
| 2286 | nigel | 77 | ecode++; |
| 2287 | break; | ||
| 2288 | |||
| 2289 | /* Match a single byte, even in UTF-8 mode. This opcode really does match | ||
| 2290 | any byte, even newline, independent of the setting of PCRE_DOTALL. */ | ||
| 2291 | |||
| 2292 | case OP_ANYBYTE: | ||
| 2293 | ph10 | 648 | if (eptr >= md->end_subject) /* DO NOT merge the eptr++ here; it must */ |
| 2294 | { /* not be updated before SCHECK_PARTIAL. */ | ||
| 2295 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2296 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2297 | ph10 | 443 | } |
| 2298 | ph10 | 654 | eptr++; |
| 2299 | nigel | 77 | ecode++; |
| 2300 | break; | ||
| 2301 | |||
| 2302 | case OP_NOT_DIGIT: | ||
| 2303 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2304 | ph10 | 428 | { |
| 2305 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2306 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2307 | ph10 | 443 | } |
| 2308 | nigel | 77 | GETCHARINCTEST(c, eptr); |
| 2309 | if ( | ||
| 2310 | ph10 | 836 | #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) |
| 2311 | nigel | 77 | c < 256 && |
| 2312 | #endif | ||
| 2313 | (md->ctypes[c] & ctype_digit) != 0 | ||
| 2314 | ) | ||
| 2315 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2316 | nigel | 77 | ecode++; |
| 2317 | break; | ||
| 2318 | |||
| 2319 | case OP_DIGIT: | ||
| 2320 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2321 | ph10 | 428 | { |
| 2322 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2323 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2324 | ph10 | 443 | } |
| 2325 | nigel | 77 | GETCHARINCTEST(c, eptr); |
| 2326 | if ( | ||
| 2327 | ph10 | 836 | #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) |
| 2328 | c > 255 || | ||
| 2329 | nigel | 77 | #endif |
| 2330 | (md->ctypes[c] & ctype_digit) == 0 | ||
| 2331 | ) | ||
| 2332 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2333 | nigel | 77 | ecode++; |
| 2334 | break; | ||
| 2335 | |||
| 2336 | case OP_NOT_WHITESPACE: | ||
| 2337 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2338 | ph10 | 428 | { |
| 2339 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2340 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2341 | ph10 | 443 | } |
| 2342 | nigel | 77 | GETCHARINCTEST(c, eptr); |
| 2343 | if ( | ||
| 2344 | ph10 | 836 | #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) |
| 2345 | nigel | 77 | c < 256 && |
| 2346 | #endif | ||
| 2347 | (md->ctypes[c] & ctype_space) != 0 | ||
| 2348 | ) | ||
| 2349 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2350 | nigel | 77 | ecode++; |
| 2351 | break; | ||
| 2352 | |||
| 2353 | case OP_WHITESPACE: | ||
| 2354 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2355 | ph10 | 428 | { |
| 2356 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2357 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2358 | ph10 | 443 | } |
| 2359 | nigel | 77 | GETCHARINCTEST(c, eptr); |
| 2360 | if ( | ||
| 2361 | ph10 | 836 | #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) |
| 2362 | c > 255 || | ||
| 2363 | nigel | 77 | #endif |
| 2364 | (md->ctypes[c] & ctype_space) == 0 | ||
| 2365 | ) | ||
| 2366 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2367 | nigel | 77 | ecode++; |
| 2368 | break; | ||
| 2369 | |||
| 2370 | case OP_NOT_WORDCHAR: | ||
| 2371 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2372 | ph10 | 428 | { |
| 2373 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2374 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2375 | ph10 | 443 | } |
| 2376 | nigel | 77 | GETCHARINCTEST(c, eptr); |
| 2377 | if ( | ||
| 2378 | ph10 | 836 | #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) |
| 2379 | nigel | 77 | c < 256 && |
| 2380 | #endif | ||
| 2381 | (md->ctypes[c] & ctype_word) != 0 | ||
| 2382 | ) | ||
| 2383 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2384 | nigel | 77 | ecode++; |
| 2385 | break; | ||
| 2386 | |||
| 2387 | case OP_WORDCHAR: | ||
| 2388 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2389 | ph10 | 428 | { |
| 2390 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2391 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2392 | ph10 | 443 | } |
| 2393 | nigel | 77 | GETCHARINCTEST(c, eptr); |
| 2394 | if ( | ||
| 2395 | ph10 | 836 | #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8) |
| 2396 | c > 255 || | ||
| 2397 | nigel | 77 | #endif |
| 2398 | (md->ctypes[c] & ctype_word) == 0 | ||
| 2399 | ) | ||
| 2400 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2401 | nigel | 77 | ecode++; |
| 2402 | break; | ||
| 2403 | |||
| 2404 | nigel | 93 | case OP_ANYNL: |
| 2405 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2406 | ph10 | 428 | { |
| 2407 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2408 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2409 | ph10 | 443 | } |
| 2410 | nigel | 93 | GETCHARINCTEST(c, eptr); |
| 2411 | switch(c) | ||
| 2412 | { | ||
| 2413 | ph10 | 836 | default: RRETURN(MATCH_NOMATCH); |
| 2414 | ph10 | 625 | |
| 2415 | nigel | 93 | case 0x000d: |
| 2416 | ph10 | 916 | if (eptr >= md->end_subject) |
| 2417 | { | ||
| 2418 | SCHECK_PARTIAL(); | ||
| 2419 | ph10 | 933 | } |
| 2420 | ph10 | 916 | else if (*eptr == 0x0a) eptr++; |
| 2421 | nigel | 93 | break; |
| 2422 | ph10 | 231 | |
| 2423 | nigel | 93 | case 0x000a: |
| 2424 | ph10 | 231 | break; |
| 2425 | |||
| 2426 | nigel | 93 | case 0x000b: |
| 2427 | case 0x000c: | ||
| 2428 | case 0x0085: | ||
| 2429 | case 0x2028: | ||
| 2430 | case 0x2029: | ||
| 2431 | ph10 | 836 | if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH); |
| 2432 | nigel | 93 | break; |
| 2433 | } | ||
| 2434 | ecode++; | ||
| 2435 | break; | ||
| 2436 | |||
| 2437 | ph10 | 178 | case OP_NOT_HSPACE: |
| 2438 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2439 | ph10 | 428 | { |
| 2440 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2441 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2442 | ph10 | 443 | } |
| 2443 | ph10 | 178 | GETCHARINCTEST(c, eptr); |
| 2444 | switch(c) | ||
| 2445 | { | ||
| 2446 | default: break; | ||
| 2447 | case 0x09: /* HT */ | ||
| 2448 | case 0x20: /* SPACE */ | ||
| 2449 | case 0xa0: /* NBSP */ | ||
| 2450 | case 0x1680: /* OGHAM SPACE MARK */ | ||
| 2451 | case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */ | ||
| 2452 | case 0x2000: /* EN QUAD */ | ||
| 2453 | case 0x2001: /* EM QUAD */ | ||
| 2454 | case 0x2002: /* EN SPACE */ | ||
| 2455 | case 0x2003: /* EM SPACE */ | ||
| 2456 | case 0x2004: /* THREE-PER-EM SPACE */ | ||
| 2457 | case 0x2005: /* FOUR-PER-EM SPACE */ | ||
| 2458 | case 0x2006: /* SIX-PER-EM SPACE */ | ||
| 2459 | case 0x2007: /* FIGURE SPACE */ | ||
| 2460 | case 0x2008: /* PUNCTUATION SPACE */ | ||
| 2461 | case 0x2009: /* THIN SPACE */ | ||
| 2462 | case 0x200A: /* HAIR SPACE */ | ||
| 2463 | case 0x202f: /* NARROW NO-BREAK SPACE */ | ||
| 2464 | case 0x205f: /* MEDIUM MATHEMATICAL SPACE */ | ||
| 2465 | case 0x3000: /* IDEOGRAPHIC SPACE */ | ||
| 2466 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2467 | ph10 | 178 | } |
| 2468 | ecode++; | ||
| 2469 | break; | ||
| 2470 | |||
| 2471 | case OP_HSPACE: | ||
| 2472 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2473 | ph10 | 428 | { |
| 2474 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2475 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2476 | ph10 | 443 | } |
| 2477 | ph10 | 178 | GETCHARINCTEST(c, eptr); |
| 2478 | switch(c) | ||
| 2479 | { | ||
| 2480 | ph10 | 836 | default: RRETURN(MATCH_NOMATCH); |
| 2481 | ph10 | 178 | case 0x09: /* HT */ |
| 2482 | case 0x20: /* SPACE */ | ||
| 2483 | case 0xa0: /* NBSP */ | ||
| 2484 | case 0x1680: /* OGHAM SPACE MARK */ | ||
| 2485 | case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */ | ||
| 2486 | case 0x2000: /* EN QUAD */ | ||
| 2487 | case 0x2001: /* EM QUAD */ | ||
| 2488 | case 0x2002: /* EN SPACE */ | ||
| 2489 | case 0x2003: /* EM SPACE */ | ||
| 2490 | case 0x2004: /* THREE-PER-EM SPACE */ | ||
| 2491 | case 0x2005: /* FOUR-PER-EM SPACE */ | ||
| 2492 | case 0x2006: /* SIX-PER-EM SPACE */ | ||
| 2493 | case 0x2007: /* FIGURE SPACE */ | ||
| 2494 | case 0x2008: /* PUNCTUATION SPACE */ | ||
| 2495 | case 0x2009: /* THIN SPACE */ | ||
| 2496 | case 0x200A: /* HAIR SPACE */ | ||
| 2497 | case 0x202f: /* NARROW NO-BREAK SPACE */ | ||
| 2498 | case 0x205f: /* MEDIUM MATHEMATICAL SPACE */ | ||
| 2499 | case 0x3000: /* IDEOGRAPHIC SPACE */ | ||
| 2500 | break; | ||
| 2501 | } | ||
| 2502 | ecode++; | ||
| 2503 | break; | ||
| 2504 | |||
| 2505 | case OP_NOT_VSPACE: | ||
| 2506 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2507 | ph10 | 428 | { |
| 2508 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2509 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2510 | ph10 | 443 | } |
| 2511 | ph10 | 178 | GETCHARINCTEST(c, eptr); |
| 2512 | switch(c) | ||
| 2513 | { | ||
| 2514 | default: break; | ||
| 2515 | case 0x0a: /* LF */ | ||
| 2516 | case 0x0b: /* VT */ | ||
| 2517 | case 0x0c: /* FF */ | ||
| 2518 | case 0x0d: /* CR */ | ||
| 2519 | case 0x85: /* NEL */ | ||
| 2520 | case 0x2028: /* LINE SEPARATOR */ | ||
| 2521 | case 0x2029: /* PARAGRAPH SEPARATOR */ | ||
| 2522 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2523 | ph10 | 178 | } |
| 2524 | ecode++; | ||
| 2525 | break; | ||
| 2526 | |||
| 2527 | case OP_VSPACE: | ||
| 2528 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2529 | ph10 | 428 | { |
| 2530 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2531 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2532 | ph10 | 443 | } |
| 2533 | ph10 | 178 | GETCHARINCTEST(c, eptr); |
| 2534 | switch(c) | ||
| 2535 | { | ||
| 2536 | ph10 | 836 | default: RRETURN(MATCH_NOMATCH); |
| 2537 | ph10 | 178 | case 0x0a: /* LF */ |
| 2538 | case 0x0b: /* VT */ | ||
| 2539 | case 0x0c: /* FF */ | ||
| 2540 | case 0x0d: /* CR */ | ||
| 2541 | case 0x85: /* NEL */ | ||
| 2542 | case 0x2028: /* LINE SEPARATOR */ | ||
| 2543 | case 0x2029: /* PARAGRAPH SEPARATOR */ | ||
| 2544 | break; | ||
| 2545 | } | ||
| 2546 | ecode++; | ||
| 2547 | break; | ||
| 2548 | |||
| 2549 | nigel | 77 | #ifdef SUPPORT_UCP |
| 2550 | /* Check the next character by Unicode property. We will get here only | ||
| 2551 | if the support is in the binary; otherwise a compile-time error occurs. */ | ||
| 2552 | |||
| 2553 | case OP_PROP: | ||
| 2554 | case OP_NOTPROP: | ||
| 2555 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2556 | ph10 | 428 | { |
| 2557 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2558 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2559 | ph10 | 443 | } |
| 2560 | nigel | 77 | GETCHARINCTEST(c, eptr); |
| 2561 | { | ||
| 2562 | ph10 | 384 | const ucd_record *prop = GET_UCD(c); |
| 2563 | nigel | 77 | |
| 2564 | nigel | 87 | switch(ecode[1]) |
| 2565 | { | ||
| 2566 | case PT_ANY: | ||
| 2567 | ph10 | 836 | if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH); |
| 2568 | nigel | 87 | break; |
| 2569 | nigel | 77 | |
| 2570 | nigel | 87 | case PT_LAMP: |
| 2571 | ph10 | 349 | if ((prop->chartype == ucp_Lu || |
| 2572 | prop->chartype == ucp_Ll || | ||
| 2573 | prop->chartype == ucp_Lt) == (op == OP_NOTPROP)) | ||
| 2574 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2575 | ph10 | 517 | break; |
| 2576 | nigel | 87 | |
| 2577 | case PT_GC: | ||
| 2578 | ph10 | 836 | if ((ecode[2] != PRIV(ucp_gentype)[prop->chartype]) == (op == OP_PROP)) |
| 2579 | RRETURN(MATCH_NOMATCH); | ||
| 2580 | nigel | 87 | break; |
| 2581 | |||
| 2582 | case PT_PC: | ||
| 2583 | ph10 | 349 | if ((ecode[2] != prop->chartype) == (op == OP_PROP)) |
| 2584 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2585 | nigel | 87 | break; |
| 2586 | |||
| 2587 | case PT_SC: | ||
| 2588 | ph10 | 349 | if ((ecode[2] != prop->script) == (op == OP_PROP)) |
| 2589 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2590 | nigel | 87 | break; |
| 2591 | ph10 | 527 | |
| 2592 | ph10 | 517 | /* These are specials */ |
| 2593 | ph10 | 527 | |
| 2594 | ph10 | 517 | case PT_ALNUM: |
| 2595 | ph10 | 836 | if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L || |
| 2596 | PRIV(ucp_gentype)[prop->chartype] == ucp_N) == (op == OP_NOTPROP)) | ||
| 2597 | RRETURN(MATCH_NOMATCH); | ||
| 2598 | ph10 | 527 | break; |
| 2599 | |||
| 2600 | ph10 | 517 | case PT_SPACE: /* Perl space */ |
| 2601 | ph10 | 836 | if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z || |
| 2602 | ph10 | 517 | c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR) |
| 2603 | == (op == OP_NOTPROP)) | ||
| 2604 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2605 | ph10 | 527 | break; |
| 2606 | |||
| 2607 | ph10 | 517 | case PT_PXSPACE: /* POSIX space */ |
| 2608 | ph10 | 836 | if ((PRIV(ucp_gentype)[prop->chartype] == ucp_Z || |
| 2609 | ph10 | 527 | c == CHAR_HT || c == CHAR_NL || c == CHAR_VT || |
| 2610 | ph10 | 517 | c == CHAR_FF || c == CHAR_CR) |
| 2611 | == (op == OP_NOTPROP)) | ||
| 2612 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2613 | ph10 | 527 | break; |
| 2614 | nigel | 87 | |
| 2615 | ph10 | 527 | case PT_WORD: |
| 2616 | ph10 | 836 | if ((PRIV(ucp_gentype)[prop->chartype] == ucp_L || |
| 2617 | PRIV(ucp_gentype)[prop->chartype] == ucp_N || | ||
| 2618 | ph10 | 517 | c == CHAR_UNDERSCORE) == (op == OP_NOTPROP)) |
| 2619 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2620 | ph10 | 527 | break; |
| 2621 | |||
| 2622 | ph10 | 517 | /* This should never occur */ |
| 2623 | |||
| 2624 | nigel | 87 | default: |
| 2625 | RRETURN(PCRE_ERROR_INTERNAL); | ||
| 2626 | nigel | 77 | } |
| 2627 | nigel | 87 | |
| 2628 | ecode += 3; | ||
| 2629 | nigel | 77 | } |
| 2630 | break; | ||
| 2631 | |||
| 2632 | /* Match an extended Unicode sequence. We will get here only if the support | ||
| 2633 | is in the binary; otherwise a compile-time error occurs. */ | ||
| 2634 | |||
| 2635 | case OP_EXTUNI: | ||
| 2636 | ph10 | 443 | if (eptr >= md->end_subject) |
| 2637 | ph10 | 428 | { |
| 2638 | ph10 | 443 | SCHECK_PARTIAL(); |
| 2639 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2640 | ph10 | 443 | } |
| 2641 | nigel | 77 | GETCHARINCTEST(c, eptr); |
| 2642 | ph10 | 836 | if (UCD_CATEGORY(c) == ucp_M) RRETURN(MATCH_NOMATCH); |
| 2643 | ph10 | 623 | while (eptr < md->end_subject) |
| 2644 | nigel | 77 | { |
| 2645 | ph10 | 623 | int len = 1; |
| 2646 | ph10 | 836 | if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); } |
| 2647 | ph10 | 623 | if (UCD_CATEGORY(c) != ucp_M) break; |
| 2648 | eptr += len; | ||
| 2649 | nigel | 77 | } |
| 2650 | ph10 | 933 | CHECK_PARTIAL(); |
| 2651 | nigel | 77 | ecode++; |
| 2652 | break; | ||
| 2653 | #endif | ||
| 2654 | |||
| 2655 | |||
| 2656 | /* Match a back reference, possibly repeatedly. Look past the end of the | ||
| 2657 | item to see if there is repeat information following. The code is similar | ||
| 2658 | to that for character classes, but repeated for efficiency. Then obey | ||
| 2659 | similar code to character type repeats - written out again for speed. | ||
| 2660 | However, if the referenced string is the empty string, always treat | ||
| 2661 | it as matched, any number of times (otherwise there could be infinite | ||
| 2662 | loops). */ | ||
| 2663 | |||
| 2664 | case OP_REF: | ||
| 2665 | ph10 | 625 | case OP_REFI: |
| 2666 | caseless = op == OP_REFI; | ||
| 2667 | ph10 | 595 | offset = GET2(ecode, 1) << 1; /* Doubled ref number */ |
| 2668 | ph10 | 836 | ecode += 1 + IMM2_SIZE; |
| 2669 | ph10 | 345 | |
| 2670 | ph10 | 595 | /* If the reference is unset, there are two possibilities: |
| 2671 | ph10 | 345 | |
| 2672 | ph10 | 595 | (a) In the default, Perl-compatible state, set the length negative; |
| 2673 | this ensures that every attempt at a match fails. We can't just fail | ||
| 2674 | here, because of the possibility of quantifiers with zero minima. | ||
| 2675 | ph10 | 345 | |
| 2676 | ph10 | 595 | (b) If the JavaScript compatibility flag is set, set the length to zero |
| 2677 | so that the back reference matches an empty string. | ||
| 2678 | ph10 | 345 | |
| 2679 | ph10 | 595 | Otherwise, set the length to the length of what was matched by the |
| 2680 | referenced subpattern. */ | ||
| 2681 | ph10 | 345 | |
| 2682 | ph10 | 595 | if (offset >= offset_top || md->offset_vector[offset] < 0) |
| 2683 | length = (md->jscript_compat)? 0 : -1; | ||
| 2684 | else | ||
| 2685 | length = md->offset_vector[offset+1] - md->offset_vector[offset]; | ||
| 2686 | nigel | 77 | |
| 2687 | ph10 | 595 | /* Set up for repetition, or handle the non-repeated case */ |
| 2688 | nigel | 77 | |
| 2689 | ph10 | 595 | switch (*ecode) |
| 2690 | { | ||
| 2691 | case OP_CRSTAR: | ||
| 2692 | case OP_CRMINSTAR: | ||
| 2693 | case OP_CRPLUS: | ||
| 2694 | case OP_CRMINPLUS: | ||
| 2695 | case OP_CRQUERY: | ||
| 2696 | case OP_CRMINQUERY: | ||
| 2697 | c = *ecode++ - OP_CRSTAR; | ||
| 2698 | minimize = (c & 1) != 0; | ||
| 2699 | min = rep_min[c]; /* Pick up values from tables; */ | ||
| 2700 | max = rep_max[c]; /* zero for max => infinity */ | ||
| 2701 | if (max == 0) max = INT_MAX; | ||
| 2702 | break; | ||
| 2703 | nigel | 77 | |
| 2704 | ph10 | 595 | case OP_CRRANGE: |
| 2705 | case OP_CRMINRANGE: | ||
| 2706 | minimize = (*ecode == OP_CRMINRANGE); | ||
| 2707 | min = GET2(ecode, 1); | ||
| 2708 | ph10 | 836 | max = GET2(ecode, 1 + IMM2_SIZE); |
| 2709 | ph10 | 595 | if (max == 0) max = INT_MAX; |
| 2710 | ph10 | 836 | ecode += 1 + 2 * IMM2_SIZE; |
| 2711 | ph10 | 595 | break; |
| 2712 | nigel | 77 | |
| 2713 | ph10 | 595 | default: /* No repeat follows */ |
| 2714 | ph10 | 602 | if ((length = match_ref(offset, eptr, length, md, caseless)) < 0) |
| 2715 | ph10 | 595 | { |
| 2716 | ph10 | 933 | if (length == -2) eptr = md->end_subject; /* Partial match */ |
| 2717 | ph10 | 595 | CHECK_PARTIAL(); |
| 2718 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2719 | nigel | 77 | } |
| 2720 | ph10 | 595 | eptr += length; |
| 2721 | continue; /* With the main loop */ | ||
| 2722 | } | ||
| 2723 | nigel | 77 | |
| 2724 | ph10 | 595 | /* Handle repeated back references. If the length of the reference is |
| 2725 | ph10 | 836 | zero, just continue with the main loop. If the length is negative, it |
| 2726 | ph10 | 842 | means the reference is unset in non-Java-compatible mode. If the minimum is |
| 2727 | zero, we can continue at the same level without recursion. For any other | ||
| 2728 | ph10 | 836 | minimum, carrying on will result in NOMATCH. */ |
| 2729 | ph10 | 443 | |
| 2730 | ph10 | 595 | if (length == 0) continue; |
| 2731 | ph10 | 836 | if (length < 0 && min == 0) continue; |
| 2732 | nigel | 77 | |
| 2733 | ph10 | 595 | /* First, ensure the minimum number of matches are present. We get back |
| 2734 | the length of the reference string explicitly rather than passing the | ||
| 2735 | address of eptr, so that eptr can be a register variable. */ | ||
| 2736 | nigel | 77 | |
| 2737 | ph10 | 595 | for (i = 1; i <= min; i++) |
| 2738 | { | ||
| 2739 | ph10 | 625 | int slength; |
| 2740 | ph10 | 602 | if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0) |
| 2741 | nigel | 77 | { |
| 2742 | ph10 | 933 | if (slength == -2) eptr = md->end_subject; /* Partial match */ |
| 2743 | ph10 | 595 | CHECK_PARTIAL(); |
| 2744 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2745 | nigel | 77 | } |
| 2746 | ph10 | 595 | eptr += slength; |
| 2747 | } | ||
| 2748 | nigel | 77 | |
| 2749 | ph10 | 595 | /* If min = max, continue at the same level without recursion. |
| 2750 | They are not both allowed to be zero. */ | ||
| 2751 | nigel | 77 | |
| 2752 | ph10 | 595 | if (min == max) continue; |
| 2753 | nigel | 77 | |
| 2754 | ph10 | 595 | /* If minimizing, keep trying and advancing the pointer */ |
| 2755 | nigel | 77 | |
| 2756 | ph10 | 595 | if (minimize) |
| 2757 | { | ||
| 2758 | for (fi = min;; fi++) | ||
| 2759 | nigel | 77 | { |
| 2760 | ph10 | 625 | int slength; |
| 2761 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM14); |
| 2762 | ph10 | 595 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 2763 | ph10 | 836 | if (fi >= max) RRETURN(MATCH_NOMATCH); |
| 2764 | ph10 | 602 | if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0) |
| 2765 | nigel | 77 | { |
| 2766 | ph10 | 933 | if (slength == -2) eptr = md->end_subject; /* Partial match */ |
| 2767 | ph10 | 595 | CHECK_PARTIAL(); |
| 2768 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2769 | nigel | 77 | } |
| 2770 | ph10 | 595 | eptr += slength; |
| 2771 | nigel | 77 | } |
| 2772 | ph10 | 595 | /* Control never gets here */ |
| 2773 | } | ||
| 2774 | nigel | 77 | |
| 2775 | ph10 | 595 | /* If maximizing, find the longest string and work backwards */ |
| 2776 | nigel | 77 | |
| 2777 | ph10 | 595 | else |
| 2778 | { | ||
| 2779 | pp = eptr; | ||
| 2780 | for (i = min; i < max; i++) | ||
| 2781 | nigel | 77 | { |
| 2782 | ph10 | 625 | int slength; |
| 2783 | ph10 | 602 | if ((slength = match_ref(offset, eptr, length, md, caseless)) < 0) |
| 2784 | nigel | 77 | { |
| 2785 | ph10 | 933 | /* Can't use CHECK_PARTIAL because we don't want to update eptr in |
| 2786 | the soft partial matching case. */ | ||
| 2787 | |||
| 2788 | if (slength == -2 && md->partial != 0 && | ||
| 2789 | ph10 | 916 | md->end_subject > md->start_used_ptr) |
| 2790 | ph10 | 933 | { |
| 2791 | ph10 | 916 | md->hitend = TRUE; |
| 2792 | if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL); | ||
| 2793 | } | ||
| 2794 | ph10 | 595 | break; |
| 2795 | nigel | 77 | } |
| 2796 | ph10 | 595 | eptr += slength; |
| 2797 | nigel | 77 | } |
| 2798 | ph10 | 933 | |
| 2799 | ph10 | 595 | while (eptr >= pp) |
| 2800 | { | ||
| 2801 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM15); |
| 2802 | ph10 | 595 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 2803 | eptr -= length; | ||
| 2804 | } | ||
| 2805 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2806 | nigel | 77 | } |
| 2807 | /* Control never gets here */ | ||
| 2808 | |||
| 2809 | /* Match a bit-mapped character class, possibly repeatedly. This op code is | ||
| 2810 | used when all the characters in the class have values in the range 0-255, | ||
| 2811 | and either the matching is caseful, or the characters are in the range | ||
| 2812 | 0-127 when UTF-8 processing is enabled. The only difference between | ||
| 2813 | OP_CLASS and OP_NCLASS occurs when a data character outside the range is | ||
| 2814 | encountered. | ||
| 2815 | |||
| 2816 | First, look past the end of the item to see if there is repeat information | ||
| 2817 | following. Then obey similar code to character type repeats - written out | ||
| 2818 | again for speed. */ | ||
| 2819 | |||
| 2820 | case OP_NCLASS: | ||
| 2821 | case OP_CLASS: | ||
| 2822 | { | ||
| 2823 | ph10 | 836 | /* The data variable is saved across frames, so the byte map needs to |
| 2824 | be stored there. */ | ||
| 2825 | #define BYTE_MAP ((pcre_uint8 *)data) | ||
| 2826 | nigel | 77 | data = ecode + 1; /* Save for matching */ |
| 2827 | ph10 | 836 | ecode += 1 + (32 / sizeof(pcre_uchar)); /* Advance past the item */ |
| 2828 | nigel | 77 | |
| 2829 | switch (*ecode) | ||
| 2830 | { | ||
| 2831 | case OP_CRSTAR: | ||
| 2832 | case OP_CRMINSTAR: | ||
| 2833 | case OP_CRPLUS: | ||
| 2834 | case OP_CRMINPLUS: | ||
| 2835 | case OP_CRQUERY: | ||
| 2836 | case OP_CRMINQUERY: | ||
| 2837 | c = *ecode++ - OP_CRSTAR; | ||
| 2838 | minimize = (c & 1) != 0; | ||
| 2839 | min = rep_min[c]; /* Pick up values from tables; */ | ||
| 2840 | max = rep_max[c]; /* zero for max => infinity */ | ||
| 2841 | if (max == 0) max = INT_MAX; | ||
| 2842 | break; | ||
| 2843 | |||
| 2844 | case OP_CRRANGE: | ||
| 2845 | case OP_CRMINRANGE: | ||
| 2846 | minimize = (*ecode == OP_CRMINRANGE); | ||
| 2847 | min = GET2(ecode, 1); | ||
| 2848 | ph10 | 836 | max = GET2(ecode, 1 + IMM2_SIZE); |
| 2849 | nigel | 77 | if (max == 0) max = INT_MAX; |
| 2850 | ph10 | 836 | ecode += 1 + 2 * IMM2_SIZE; |
| 2851 | nigel | 77 | break; |
| 2852 | |||
| 2853 | default: /* No repeat follows */ | ||
| 2854 | min = max = 1; | ||
| 2855 | break; | ||
| 2856 | } | ||
| 2857 | |||
| 2858 | /* First, ensure the minimum number of matches are present. */ | ||
| 2859 | |||
| 2860 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 2861 | if (utf) | ||
| 2862 | nigel | 77 | { |
| 2863 | for (i = 1; i <= min; i++) | ||
| 2864 | { | ||
| 2865 | ph10 | 427 | if (eptr >= md->end_subject) |
| 2866 | ph10 | 426 | { |
| 2867 | ph10 | 428 | SCHECK_PARTIAL(); |
| 2868 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2869 | ph10 | 427 | } |
| 2870 | nigel | 77 | GETCHARINC(c, eptr); |
| 2871 | if (c > 255) | ||
| 2872 | { | ||
| 2873 | ph10 | 836 | if (op == OP_CLASS) RRETURN(MATCH_NOMATCH); |
| 2874 | nigel | 77 | } |
| 2875 | else | ||
| 2876 | ph10 | 836 | if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH); |
| 2877 | nigel | 77 | } |
| 2878 | } | ||
| 2879 | else | ||
| 2880 | #endif | ||
| 2881 | ph10 | 836 | /* Not UTF mode */ |
| 2882 | nigel | 77 | { |
| 2883 | for (i = 1; i <= min; i++) | ||
| 2884 | { | ||
| 2885 | ph10 | 427 | if (eptr >= md->end_subject) |
| 2886 | ph10 | 426 | { |
| 2887 | ph10 | 428 | SCHECK_PARTIAL(); |
| 2888 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2889 | ph10 | 427 | } |
| 2890 | nigel | 77 | c = *eptr++; |
| 2891 | ph10 | 836 | #ifndef COMPILE_PCRE8 |
| 2892 | if (c > 255) | ||
| 2893 | { | ||
| 2894 | if (op == OP_CLASS) RRETURN(MATCH_NOMATCH); | ||
| 2895 | } | ||
| 2896 | else | ||
| 2897 | #endif | ||
| 2898 | if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH); | ||
| 2899 | nigel | 77 | } |
| 2900 | } | ||
| 2901 | |||
| 2902 | /* If max == min we can continue with the main loop without the | ||
| 2903 | need to recurse. */ | ||
| 2904 | |||
| 2905 | if (min == max) continue; | ||
| 2906 | |||
| 2907 | /* If minimizing, keep testing the rest of the expression and advancing | ||
| 2908 | the pointer while it matches the class. */ | ||
| 2909 | |||
| 2910 | if (minimize) | ||
| 2911 | { | ||
| 2912 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 2913 | if (utf) | ||
| 2914 | nigel | 77 | { |
| 2915 | for (fi = min;; fi++) | ||
| 2916 | { | ||
| 2917 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM16); |
| 2918 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 2919 | ph10 | 836 | if (fi >= max) RRETURN(MATCH_NOMATCH); |
| 2920 | ph10 | 427 | if (eptr >= md->end_subject) |
| 2921 | ph10 | 426 | { |
| 2922 | ph10 | 427 | SCHECK_PARTIAL(); |
| 2923 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2924 | ph10 | 427 | } |
| 2925 | nigel | 77 | GETCHARINC(c, eptr); |
| 2926 | if (c > 255) | ||
| 2927 | { | ||
| 2928 | ph10 | 836 | if (op == OP_CLASS) RRETURN(MATCH_NOMATCH); |
| 2929 | nigel | 77 | } |
| 2930 | else | ||
| 2931 | ph10 | 836 | if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH); |
| 2932 | nigel | 77 | } |
| 2933 | } | ||
| 2934 | else | ||
| 2935 | #endif | ||
| 2936 | ph10 | 836 | /* Not UTF mode */ |
| 2937 | nigel | 77 | { |
| 2938 | for (fi = min;; fi++) | ||
| 2939 | { | ||
| 2940 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM17); |
| 2941 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 2942 | ph10 | 836 | if (fi >= max) RRETURN(MATCH_NOMATCH); |
| 2943 | ph10 | 427 | if (eptr >= md->end_subject) |
| 2944 | ph10 | 426 | { |
| 2945 | ph10 | 427 | SCHECK_PARTIAL(); |
| 2946 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 2947 | ph10 | 427 | } |
| 2948 | nigel | 77 | c = *eptr++; |
| 2949 | ph10 | 836 | #ifndef COMPILE_PCRE8 |
| 2950 | if (c > 255) | ||
| 2951 | { | ||
| 2952 | if (op == OP_CLASS) RRETURN(MATCH_NOMATCH); | ||
| 2953 | } | ||
| 2954 | else | ||
| 2955 | #endif | ||
| 2956 | if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) RRETURN(MATCH_NOMATCH); | ||
| 2957 | nigel | 77 | } |
| 2958 | } | ||
| 2959 | /* Control never gets here */ | ||
| 2960 | } | ||
| 2961 | |||
| 2962 | /* If maximizing, find the longest possible run, then work backwards. */ | ||
| 2963 | |||
| 2964 | else | ||
| 2965 | { | ||
| 2966 | pp = eptr; | ||
| 2967 | |||
| 2968 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 2969 | if (utf) | ||
| 2970 | nigel | 77 | { |
| 2971 | for (i = min; i < max; i++) | ||
| 2972 | { | ||
| 2973 | int len = 1; | ||
| 2974 | ph10 | 463 | if (eptr >= md->end_subject) |
| 2975 | ph10 | 462 | { |
| 2976 | ph10 | 463 | SCHECK_PARTIAL(); |
| 2977 | ph10 | 462 | break; |
| 2978 | ph10 | 463 | } |
| 2979 | nigel | 77 | GETCHARLEN(c, eptr, len); |
| 2980 | if (c > 255) | ||
| 2981 | { | ||
| 2982 | if (op == OP_CLASS) break; | ||
| 2983 | } | ||
| 2984 | else | ||
| 2985 | ph10 | 836 | if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break; |
| 2986 | nigel | 77 | eptr += len; |
| 2987 | } | ||
| 2988 | for (;;) | ||
| 2989 | { | ||
| 2990 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM18); |
| 2991 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 2992 | if (eptr-- == pp) break; /* Stop if tried at original pos */ | ||
| 2993 | BACKCHAR(eptr); | ||
| 2994 | } | ||
| 2995 | } | ||
| 2996 | else | ||
| 2997 | #endif | ||
| 2998 | ph10 | 836 | /* Not UTF mode */ |
| 2999 | nigel | 77 | { |
| 3000 | for (i = min; i < max; i++) | ||
| 3001 | { | ||
| 3002 | ph10 | 463 | if (eptr >= md->end_subject) |
| 3003 | ph10 | 462 | { |
| 3004 | ph10 | 463 | SCHECK_PARTIAL(); |
| 3005 | ph10 | 462 | break; |
| 3006 | ph10 | 463 | } |
| 3007 | nigel | 77 | c = *eptr; |
| 3008 | ph10 | 836 | #ifndef COMPILE_PCRE8 |
| 3009 | if (c > 255) | ||
| 3010 | { | ||
| 3011 | if (op == OP_CLASS) break; | ||
| 3012 | } | ||
| 3013 | else | ||
| 3014 | #endif | ||
| 3015 | if ((BYTE_MAP[c/8] & (1 << (c&7))) == 0) break; | ||
| 3016 | nigel | 77 | eptr++; |
| 3017 | } | ||
| 3018 | while (eptr >= pp) | ||
| 3019 | { | ||
| 3020 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM19); |
| 3021 | nigel | 87 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 3022 | nigel | 77 | eptr--; |
| 3023 | } | ||
| 3024 | } | ||
| 3025 | |||
| 3026 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3027 | nigel | 77 | } |
| 3028 | ph10 | 836 | #undef BYTE_MAP |
| 3029 | nigel | 77 | } |
| 3030 | /* Control never gets here */ | ||
| 3031 | |||
| 3032 | |||
| 3033 | /* Match an extended character class. This opcode is encountered only | ||
| 3034 | ph10 | 384 | when UTF-8 mode mode is supported. Nevertheless, we may not be in UTF-8 |
| 3035 | mode, because Unicode properties are supported in non-UTF-8 mode. */ | ||
| 3036 | nigel | 77 | |
| 3037 | ph10 | 836 | #if defined SUPPORT_UTF || !defined COMPILE_PCRE8 |
| 3038 | nigel | 77 | case OP_XCLASS: |
| 3039 | { | ||
| 3040 | data = ecode + 1 + LINK_SIZE; /* Save for matching */ | ||
| 3041 | ecode += GET(ecode, 1); /* Advance past the item */ | ||
| 3042 | |||
| 3043 | switch (*ecode) | ||
| 3044 | { | ||
| 3045 | case OP_CRSTAR: | ||
| 3046 | case OP_CRMINSTAR: | ||
| 3047 | case OP_CRPLUS: | ||
| 3048 | case OP_CRMINPLUS: | ||
| 3049 | case OP_CRQUERY: | ||
| 3050 | case OP_CRMINQUERY: | ||
| 3051 | c = *ecode++ - OP_CRSTAR; | ||
| 3052 | minimize = (c & 1) != 0; | ||
| 3053 | min = rep_min[c]; /* Pick up values from tables; */ | ||
| 3054 | max = rep_max[c]; /* zero for max => infinity */ | ||
| 3055 | if (max == 0) max = INT_MAX; | ||
| 3056 | break; | ||
| 3057 | |||
| 3058 | case OP_CRRANGE: | ||
| 3059 | case OP_CRMINRANGE: | ||
| 3060 | minimize = (*ecode == OP_CRMINRANGE); | ||
| 3061 | min = GET2(ecode, 1); | ||
| 3062 | ph10 | 836 | max = GET2(ecode, 1 + IMM2_SIZE); |
| 3063 | nigel | 77 | if (max == 0) max = INT_MAX; |
| 3064 | ph10 | 836 | ecode += 1 + 2 * IMM2_SIZE; |
| 3065 | nigel | 77 | break; |
| 3066 | |||
| 3067 | default: /* No repeat follows */ | ||
| 3068 | min = max = 1; | ||
| 3069 | break; | ||
| 3070 | } | ||
| 3071 | |||
| 3072 | /* First, ensure the minimum number of matches are present. */ | ||
| 3073 | |||
| 3074 | for (i = 1; i <= min; i++) | ||
| 3075 | { | ||
| 3076 | ph10 | 427 | if (eptr >= md->end_subject) |
| 3077 | ph10 | 426 | { |
| 3078 | SCHECK_PARTIAL(); | ||
| 3079 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3080 | ph10 | 427 | } |
| 3081 | ph10 | 384 | GETCHARINCTEST(c, eptr); |
| 3082 | ph10 | 836 | if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH); |
| 3083 | nigel | 77 | } |
| 3084 | |||
| 3085 | /* If max == min we can continue with the main loop without the | ||
| 3086 | need to recurse. */ | ||
| 3087 | |||
| 3088 | if (min == max) continue; | ||
| 3089 | |||
| 3090 | /* If minimizing, keep testing the rest of the expression and advancing | ||
| 3091 | the pointer while it matches the class. */ | ||
| 3092 | |||
| 3093 | if (minimize) | ||
| 3094 | { | ||
| 3095 | for (fi = min;; fi++) | ||
| 3096 | { | ||
| 3097 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM20); |
| 3098 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 3099 | ph10 | 836 | if (fi >= max) RRETURN(MATCH_NOMATCH); |
| 3100 | ph10 | 427 | if (eptr >= md->end_subject) |
| 3101 | ph10 | 426 | { |
| 3102 | ph10 | 427 | SCHECK_PARTIAL(); |
| 3103 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3104 | ph10 | 427 | } |
| 3105 | ph10 | 384 | GETCHARINCTEST(c, eptr); |
| 3106 | ph10 | 836 | if (!PRIV(xclass)(c, data, utf)) RRETURN(MATCH_NOMATCH); |
| 3107 | nigel | 77 | } |
| 3108 | /* Control never gets here */ | ||
| 3109 | } | ||
| 3110 | |||
| 3111 | /* If maximizing, find the longest possible run, then work backwards. */ | ||
| 3112 | |||
| 3113 | else | ||
| 3114 | { | ||
| 3115 | pp = eptr; | ||
| 3116 | for (i = min; i < max; i++) | ||
| 3117 | { | ||
| 3118 | int len = 1; | ||
| 3119 | ph10 | 463 | if (eptr >= md->end_subject) |
| 3120 | ph10 | 462 | { |
| 3121 | ph10 | 463 | SCHECK_PARTIAL(); |
| 3122 | ph10 | 462 | break; |
| 3123 | ph10 | 463 | } |
| 3124 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 3125 | ph10 | 384 | GETCHARLENTEST(c, eptr, len); |
| 3126 | ph10 | 836 | #else |
| 3127 | c = *eptr; | ||
| 3128 | #endif | ||
| 3129 | if (!PRIV(xclass)(c, data, utf)) break; | ||
| 3130 | nigel | 77 | eptr += len; |
| 3131 | } | ||
| 3132 | for(;;) | ||
| 3133 | { | ||
| 3134 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM21); |
| 3135 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 3136 | if (eptr-- == pp) break; /* Stop if tried at original pos */ | ||
| 3137 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 3138 | if (utf) BACKCHAR(eptr); | ||
| 3139 | #endif | ||
| 3140 | nigel | 77 | } |
| 3141 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3142 | nigel | 77 | } |
| 3143 | |||
| 3144 | /* Control never gets here */ | ||
| 3145 | } | ||
| 3146 | #endif /* End of XCLASS */ | ||
| 3147 | |||
| 3148 | /* Match a single character, casefully */ | ||
| 3149 | |||
| 3150 | case OP_CHAR: | ||
| 3151 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 3152 | if (utf) | ||
| 3153 | nigel | 77 | { |
| 3154 | length = 1; | ||
| 3155 | ecode++; | ||
| 3156 | GETCHARLEN(fc, ecode, length); | ||
| 3157 | ph10 | 443 | if (length > md->end_subject - eptr) |
| 3158 | ph10 | 428 | { |
| 3159 | CHECK_PARTIAL(); /* Not SCHECK_PARTIAL() */ | ||
| 3160 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3161 | ph10 | 443 | } |
| 3162 | ph10 | 836 | while (length-- > 0) if (*ecode++ != *eptr++) RRETURN(MATCH_NOMATCH); |
| 3163 | nigel | 77 | } |
| 3164 | else | ||
| 3165 | #endif | ||
| 3166 | ph10 | 836 | /* Not UTF mode */ |
| 3167 | nigel | 77 | { |
| 3168 | ph10 | 443 | if (md->end_subject - eptr < 1) |
| 3169 | ph10 | 428 | { |
| 3170 | SCHECK_PARTIAL(); /* This one can use SCHECK_PARTIAL() */ | ||
| 3171 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3172 | ph10 | 443 | } |
| 3173 | ph10 | 836 | if (ecode[1] != *eptr++) RRETURN(MATCH_NOMATCH); |
| 3174 | nigel | 77 | ecode += 2; |
| 3175 | } | ||
| 3176 | break; | ||
| 3177 | |||
| 3178 | ph10 | 836 | /* Match a single character, caselessly. If we are at the end of the |
| 3179 | subject, give up immediately. */ | ||
| 3180 | nigel | 77 | |
| 3181 | ph10 | 602 | case OP_CHARI: |
| 3182 | ph10 | 836 | if (eptr >= md->end_subject) |
| 3183 | nigel | 77 | { |
| 3184 | ph10 | 836 | SCHECK_PARTIAL(); |
| 3185 | RRETURN(MATCH_NOMATCH); | ||
| 3186 | } | ||
| 3187 | |||
| 3188 | #ifdef SUPPORT_UTF | ||
| 3189 | if (utf) | ||
| 3190 | { | ||
| 3191 | nigel | 77 | length = 1; |
| 3192 | ecode++; | ||
| 3193 | GETCHARLEN(fc, ecode, length); | ||
| 3194 | ph10 | 788 | |
| 3195 | nigel | 77 | /* If the pattern character's value is < 128, we have only one byte, and |
| 3196 | ph10 | 836 | we know that its other case must also be one byte long, so we can use the |
| 3197 | fast lookup table. We know that there is at least one byte left in the | ||
| 3198 | subject. */ | ||
| 3199 | nigel | 77 | |
| 3200 | if (fc < 128) | ||
| 3201 | { | ||
| 3202 | ph10 | 836 | if (md->lcc[fc] |
| 3203 | != TABLE_GET(*eptr, md->lcc, *eptr)) RRETURN(MATCH_NOMATCH); | ||
| 3204 | ecode++; | ||
| 3205 | eptr++; | ||
| 3206 | nigel | 77 | } |
| 3207 | |||
| 3208 | ph10 | 836 | /* Otherwise we must pick up the subject character. Note that we cannot |
| 3209 | use the value of "length" to check for sufficient bytes left, because the | ||
| 3210 | other case of the character may have more or fewer bytes. */ | ||
| 3211 | nigel | 77 | |
| 3212 | else | ||
| 3213 | { | ||
| 3214 | nigel | 93 | unsigned int dc; |
| 3215 | nigel | 77 | GETCHARINC(dc, eptr); |
| 3216 | ecode += length; | ||
| 3217 | |||
| 3218 | /* If we have Unicode property support, we can use it to test the other | ||
| 3219 | nigel | 87 | case of the character, if there is one. */ |
| 3220 | nigel | 77 | |
| 3221 | if (fc != dc) | ||
| 3222 | { | ||
| 3223 | #ifdef SUPPORT_UCP | ||
| 3224 | ph10 | 349 | if (dc != UCD_OTHERCASE(fc)) |
| 3225 | nigel | 77 | #endif |
| 3226 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3227 | nigel | 77 | } |
| 3228 | } | ||
| 3229 | } | ||
| 3230 | else | ||
| 3231 | ph10 | 836 | #endif /* SUPPORT_UTF */ |
| 3232 | nigel | 77 | |
| 3233 | ph10 | 836 | /* Not UTF mode */ |
| 3234 | nigel | 77 | { |
| 3235 | ph10 | 836 | if (TABLE_GET(ecode[1], md->lcc, ecode[1]) |
| 3236 | != TABLE_GET(*eptr, md->lcc, *eptr)) RRETURN(MATCH_NOMATCH); | ||
| 3237 | eptr++; | ||
| 3238 | nigel | 77 | ecode += 2; |
| 3239 | } | ||
| 3240 | break; | ||
| 3241 | |||
| 3242 | nigel | 93 | /* Match a single character repeatedly. */ |
| 3243 | nigel | 77 | |
| 3244 | case OP_EXACT: | ||
| 3245 | ph10 | 602 | case OP_EXACTI: |
| 3246 | nigel | 77 | min = max = GET2(ecode, 1); |
| 3247 | ph10 | 836 | ecode += 1 + IMM2_SIZE; |
| 3248 | nigel | 77 | goto REPEATCHAR; |
| 3249 | |||
| 3250 | nigel | 93 | case OP_POSUPTO: |
| 3251 | ph10 | 602 | case OP_POSUPTOI: |
| 3252 | nigel | 93 | possessive = TRUE; |
| 3253 | /* Fall through */ | ||
| 3254 | |||
| 3255 | nigel | 77 | case OP_UPTO: |
| 3256 | ph10 | 602 | case OP_UPTOI: |
| 3257 | nigel | 77 | case OP_MINUPTO: |
| 3258 | ph10 | 602 | case OP_MINUPTOI: |
| 3259 | nigel | 77 | min = 0; |
| 3260 | max = GET2(ecode, 1); | ||
| 3261 | ph10 | 602 | minimize = *ecode == OP_MINUPTO || *ecode == OP_MINUPTOI; |
| 3262 | ph10 | 836 | ecode += 1 + IMM2_SIZE; |
| 3263 | nigel | 77 | goto REPEATCHAR; |
| 3264 | |||
| 3265 | nigel | 93 | case OP_POSSTAR: |
| 3266 | ph10 | 602 | case OP_POSSTARI: |
| 3267 | nigel | 93 | possessive = TRUE; |
| 3268 | min = 0; | ||
| 3269 | max = INT_MAX; | ||
| 3270 | ecode++; | ||
| 3271 | goto REPEATCHAR; | ||
| 3272 | |||
| 3273 | case OP_POSPLUS: | ||
| 3274 | ph10 | 602 | case OP_POSPLUSI: |
| 3275 | nigel | 93 | possessive = TRUE; |
| 3276 | min = 1; | ||
| 3277 | max = INT_MAX; | ||
| 3278 | ecode++; | ||
| 3279 | goto REPEATCHAR; | ||
| 3280 | |||
| 3281 | case OP_POSQUERY: | ||
| 3282 | ph10 | 602 | case OP_POSQUERYI: |
| 3283 | nigel | 93 | possessive = TRUE; |
| 3284 | min = 0; | ||
| 3285 | max = 1; | ||
| 3286 | ecode++; | ||
| 3287 | goto REPEATCHAR; | ||
| 3288 | |||
| 3289 | nigel | 77 | case OP_STAR: |
| 3290 | ph10 | 602 | case OP_STARI: |
| 3291 | nigel | 77 | case OP_MINSTAR: |
| 3292 | ph10 | 602 | case OP_MINSTARI: |
| 3293 | nigel | 77 | case OP_PLUS: |
| 3294 | ph10 | 602 | case OP_PLUSI: |
| 3295 | nigel | 77 | case OP_MINPLUS: |
| 3296 | ph10 | 602 | case OP_MINPLUSI: |
| 3297 | nigel | 77 | case OP_QUERY: |
| 3298 | ph10 | 602 | case OP_QUERYI: |
| 3299 | nigel | 77 | case OP_MINQUERY: |
| 3300 | ph10 | 602 | case OP_MINQUERYI: |
| 3301 | c = *ecode++ - ((op < OP_STARI)? OP_STAR : OP_STARI); | ||
| 3302 | nigel | 77 | minimize = (c & 1) != 0; |
| 3303 | min = rep_min[c]; /* Pick up values from tables; */ | ||
| 3304 | max = rep_max[c]; /* zero for max => infinity */ | ||
| 3305 | if (max == 0) max = INT_MAX; | ||
| 3306 | |||
| 3307 | ph10 | 426 | /* Common code for all repeated single-character matches. */ |
| 3308 | nigel | 77 | |
| 3309 | REPEATCHAR: | ||
| 3310 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 3311 | if (utf) | ||
| 3312 | nigel | 77 | { |
| 3313 | length = 1; | ||
| 3314 | charptr = ecode; | ||
| 3315 | GETCHARLEN(fc, ecode, length); | ||
| 3316 | ecode += length; | ||
| 3317 | |||
| 3318 | /* Handle multibyte character matching specially here. There is | ||
| 3319 | support for caseless matching if UCP support is present. */ | ||
| 3320 | |||
| 3321 | if (length > 1) | ||
| 3322 | { | ||
| 3323 | #ifdef SUPPORT_UCP | ||
| 3324 | nigel | 93 | unsigned int othercase; |
| 3325 | ph10 | 602 | if (op >= OP_STARI && /* Caseless */ |
| 3326 | ph10 | 349 | (othercase = UCD_OTHERCASE(fc)) != fc) |
| 3327 | ph10 | 836 | oclength = PRIV(ord2utf)(othercase, occhars); |
| 3328 | ph10 | 115 | else oclength = 0; |
| 3329 | nigel | 77 | #endif /* SUPPORT_UCP */ |
| 3330 | |||
| 3331 | for (i = 1; i <= min; i++) | ||
| 3332 | { | ||
| 3333 | ph10 | 426 | if (eptr <= md->end_subject - length && |
| 3334 | ph10 | 836 | memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length; |
| 3335 | ph10 | 123 | #ifdef SUPPORT_UCP |
| 3336 | ph10 | 426 | else if (oclength > 0 && |
| 3337 | eptr <= md->end_subject - oclength && | ||
| 3338 | ph10 | 836 | memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength; |
| 3339 | ph10 | 426 | #endif /* SUPPORT_UCP */ |
| 3340 | nigel | 77 | else |
| 3341 | { | ||
| 3342 | ph10 | 426 | CHECK_PARTIAL(); |
| 3343 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3344 | nigel | 77 | } |
| 3345 | } | ||
| 3346 | |||
| 3347 | if (min == max) continue; | ||
| 3348 | |||
| 3349 | if (minimize) | ||
| 3350 | { | ||
| 3351 | for (fi = min;; fi++) | ||
| 3352 | { | ||
| 3353 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM22); |
| 3354 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 3355 | ph10 | 836 | if (fi >= max) RRETURN(MATCH_NOMATCH); |
| 3356 | ph10 | 426 | if (eptr <= md->end_subject - length && |
| 3357 | ph10 | 836 | memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length; |
| 3358 | ph10 | 123 | #ifdef SUPPORT_UCP |
| 3359 | ph10 | 426 | else if (oclength > 0 && |
| 3360 | eptr <= md->end_subject - oclength && | ||
| 3361 | ph10 | 836 | memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength; |
| 3362 | ph10 | 426 | #endif /* SUPPORT_UCP */ |
| 3363 | nigel | 77 | else |
| 3364 | { | ||
| 3365 | ph10 | 426 | CHECK_PARTIAL(); |
| 3366 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3367 | nigel | 77 | } |
| 3368 | } | ||
| 3369 | /* Control never gets here */ | ||
| 3370 | } | ||
| 3371 | nigel | 93 | |
| 3372 | else /* Maximize */ | ||
| 3373 | nigel | 77 | { |
| 3374 | pp = eptr; | ||
| 3375 | for (i = min; i < max; i++) | ||
| 3376 | { | ||
| 3377 | ph10 | 426 | if (eptr <= md->end_subject - length && |
| 3378 | ph10 | 836 | memcmp(eptr, charptr, IN_UCHARS(length)) == 0) eptr += length; |
| 3379 | ph10 | 123 | #ifdef SUPPORT_UCP |
| 3380 | ph10 | 426 | else if (oclength > 0 && |
| 3381 | eptr <= md->end_subject - oclength && | ||
| 3382 | ph10 | 836 | memcmp(eptr, occhars, IN_UCHARS(oclength)) == 0) eptr += oclength; |
| 3383 | ph10 | 426 | #endif /* SUPPORT_UCP */ |
| 3384 | ph10 | 463 | else |
| 3385 | ph10 | 462 | { |
| 3386 | ph10 | 463 | CHECK_PARTIAL(); |
| 3387 | ph10 | 462 | break; |
| 3388 | ph10 | 463 | } |
| 3389 | nigel | 77 | } |
| 3390 | nigel | 93 | |
| 3391 | if (possessive) continue; | ||
| 3392 | ph10 | 427 | |
| 3393 | ph10 | 120 | for(;;) |
| 3394 | ph10 | 426 | { |
| 3395 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM23); |
| 3396 | ph10 | 426 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 3397 | ph10 | 836 | if (eptr == pp) { RRETURN(MATCH_NOMATCH); } |
| 3398 | ph10 | 115 | #ifdef SUPPORT_UCP |
| 3399 | ph10 | 426 | eptr--; |
| 3400 | BACKCHAR(eptr); | ||
| 3401 | ph10 | 123 | #else /* without SUPPORT_UCP */ |
| 3402 | ph10 | 426 | eptr -= length; |
| 3403 | ph10 | 123 | #endif /* SUPPORT_UCP */ |
| 3404 | ph10 | 426 | } |
| 3405 | nigel | 77 | } |
| 3406 | /* Control never gets here */ | ||
| 3407 | } | ||
| 3408 | |||
| 3409 | /* If the length of a UTF-8 character is 1, we fall through here, and | ||
| 3410 | obey the code as for non-UTF-8 characters below, though in this case the | ||
| 3411 | value of fc will always be < 128. */ | ||
| 3412 | } | ||
| 3413 | else | ||
| 3414 | ph10 | 836 | #endif /* SUPPORT_UTF */ |
| 3415 | /* When not in UTF-8 mode, load a single-byte character. */ | ||
| 3416 | fc = *ecode++; | ||
| 3417 | nigel | 77 | |
| 3418 | ph10 | 836 | /* The value of fc at this point is always one character, though we may |
| 3419 | or may not be in UTF mode. The code is duplicated for the caseless and | ||
| 3420 | nigel | 77 | caseful cases, for speed, since matching characters is likely to be quite |
| 3421 | common. First, ensure the minimum number of matches are present. If min = | ||
| 3422 | max, continue at the same level without recursing. Otherwise, if | ||
| 3423 | minimizing, keep trying the rest of the expression and advancing one | ||
| 3424 | matching character if failing, up to the maximum. Alternatively, if | ||
| 3425 | maximizing, find the maximum number of characters and work backwards. */ | ||
| 3426 | |||
| 3427 | DPRINTF(("matching %c{%d,%d} against subject %.*s\n", fc, min, max, | ||
| 3428 | max, eptr)); | ||
| 3429 | |||
| 3430 | ph10 | 602 | if (op >= OP_STARI) /* Caseless */ |
| 3431 | nigel | 77 | { |
| 3432 | ph10 | 836 | #ifdef COMPILE_PCRE8 |
| 3433 | /* fc must be < 128 if UTF is enabled. */ | ||
| 3434 | foc = md->fcc[fc]; | ||
| 3435 | #else | ||
| 3436 | #ifdef SUPPORT_UTF | ||
| 3437 | #ifdef SUPPORT_UCP | ||
| 3438 | if (utf && fc > 127) | ||
| 3439 | foc = UCD_OTHERCASE(fc); | ||
| 3440 | #else | ||
| 3441 | if (utf && fc > 127) | ||
| 3442 | foc = fc; | ||
| 3443 | #endif /* SUPPORT_UCP */ | ||
| 3444 | else | ||
| 3445 | #endif /* SUPPORT_UTF */ | ||
| 3446 | foc = TABLE_GET(fc, md->fcc, fc); | ||
| 3447 | #endif /* COMPILE_PCRE8 */ | ||
| 3448 | |||
| 3449 | nigel | 77 | for (i = 1; i <= min; i++) |
| 3450 | ph10 | 426 | { |
| 3451 | if (eptr >= md->end_subject) | ||
| 3452 | { | ||
| 3453 | SCHECK_PARTIAL(); | ||
| 3454 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3455 | ph10 | 426 | } |
| 3456 | ph10 | 836 | if (fc != *eptr && foc != *eptr) RRETURN(MATCH_NOMATCH); |
| 3457 | eptr++; | ||
| 3458 | ph10 | 426 | } |
| 3459 | nigel | 77 | if (min == max) continue; |
| 3460 | if (minimize) | ||
| 3461 | { | ||
| 3462 | for (fi = min;; fi++) | ||
| 3463 | { | ||
| 3464 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM24); |
| 3465 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 3466 | ph10 | 836 | if (fi >= max) RRETURN(MATCH_NOMATCH); |
| 3467 | ph10 | 426 | if (eptr >= md->end_subject) |
| 3468 | { | ||
| 3469 | ph10 | 427 | SCHECK_PARTIAL(); |
| 3470 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3471 | ph10 | 426 | } |
| 3472 | ph10 | 836 | if (fc != *eptr && foc != *eptr) RRETURN(MATCH_NOMATCH); |
| 3473 | eptr++; | ||
| 3474 | nigel | 77 | } |
| 3475 | /* Control never gets here */ | ||
| 3476 | } | ||
| 3477 | nigel | 93 | else /* Maximize */ |
| 3478 | nigel | 77 | { |
| 3479 | pp = eptr; | ||
| 3480 | for (i = min; i < max; i++) | ||
| 3481 | { | ||
| 3482 | ph10 | 463 | if (eptr >= md->end_subject) |
| 3483 | ph10 | 462 | { |
| 3484 | SCHECK_PARTIAL(); | ||
| 3485 | break; | ||
| 3486 | ph10 | 463 | } |
| 3487 | ph10 | 836 | if (fc != *eptr && foc != *eptr) break; |
| 3488 | nigel | 77 | eptr++; |
| 3489 | } | ||
| 3490 | ph10 | 427 | |
| 3491 | nigel | 93 | if (possessive) continue; |
| 3492 | ph10 | 427 | |
| 3493 | nigel | 77 | while (eptr >= pp) |
| 3494 | { | ||
| 3495 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM25); |
| 3496 | nigel | 77 | eptr--; |
| 3497 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); | ||
| 3498 | } | ||
| 3499 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3500 | nigel | 77 | } |
| 3501 | /* Control never gets here */ | ||
| 3502 | } | ||
| 3503 | |||
| 3504 | /* Caseful comparisons (includes all multi-byte characters) */ | ||
| 3505 | |||
| 3506 | else | ||
| 3507 | { | ||
| 3508 | ph10 | 427 | for (i = 1; i <= min; i++) |
| 3509 | ph10 | 426 | { |
| 3510 | if (eptr >= md->end_subject) | ||
| 3511 | { | ||
| 3512 | SCHECK_PARTIAL(); | ||
| 3513 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3514 | ph10 | 426 | } |
| 3515 | ph10 | 836 | if (fc != *eptr++) RRETURN(MATCH_NOMATCH); |
| 3516 | ph10 | 427 | } |
| 3517 | ph10 | 443 | |
| 3518 | nigel | 77 | if (min == max) continue; |
| 3519 | ph10 | 443 | |
| 3520 | nigel | 77 | if (minimize) |
| 3521 | { | ||
| 3522 | for (fi = min;; fi++) | ||
| 3523 | { | ||
| 3524 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM26); |
| 3525 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 3526 | ph10 | 836 | if (fi >= max) RRETURN(MATCH_NOMATCH); |
| 3527 | ph10 | 426 | if (eptr >= md->end_subject) |
| 3528 | ph10 | 427 | { |
| 3529 | ph10 | 426 | SCHECK_PARTIAL(); |
| 3530 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3531 | ph10 | 427 | } |
| 3532 | ph10 | 836 | if (fc != *eptr++) RRETURN(MATCH_NOMATCH); |
| 3533 | nigel | 77 | } |
| 3534 | /* Control never gets here */ | ||
| 3535 | } | ||
| 3536 | nigel | 93 | else /* Maximize */ |
| 3537 | nigel | 77 | { |
| 3538 | pp = eptr; | ||
| 3539 | for (i = min; i < max; i++) | ||
| 3540 | { | ||
| 3541 | ph10 | 463 | if (eptr >= md->end_subject) |
| 3542 | ph10 | 462 | { |
| 3543 | ph10 | 463 | SCHECK_PARTIAL(); |
| 3544 | ph10 | 462 | break; |
| 3545 | ph10 | 463 | } |
| 3546 | ph10 | 462 | if (fc != *eptr) break; |
| 3547 | nigel | 77 | eptr++; |
| 3548 | } | ||
| 3549 | nigel | 93 | if (possessive) continue; |
| 3550 | ph10 | 443 | |
| 3551 | nigel | 77 | while (eptr >= pp) |
| 3552 | { | ||
| 3553 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM27); |
| 3554 | nigel | 77 | eptr--; |
| 3555 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); | ||
| 3556 | } | ||
| 3557 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3558 | nigel | 77 | } |
| 3559 | } | ||
| 3560 | /* Control never gets here */ | ||
| 3561 | |||
| 3562 | /* Match a negated single one-byte character. The character we are | ||
| 3563 | checking can be multibyte. */ | ||
| 3564 | |||
| 3565 | case OP_NOT: | ||
| 3566 | ph10 | 625 | case OP_NOTI: |
| 3567 | ph10 | 443 | if (eptr >= md->end_subject) |
| 3568 | ph10 | 428 | { |
| 3569 | ph10 | 443 | SCHECK_PARTIAL(); |
| 3570 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3571 | ph10 | 443 | } |
| 3572 | zherczeg | 924 | #ifdef SUPPORT_UTF |
| 3573 | if (utf) | ||
| 3574 | nigel | 77 | { |
| 3575 | ph10 | 904 | register unsigned int ch, och; |
| 3576 | zherczeg | 924 | |
| 3577 | ecode++; | ||
| 3578 | GETCHARINC(ch, ecode); | ||
| 3579 | GETCHARINC(c, eptr); | ||
| 3580 | |||
| 3581 | if (op == OP_NOT) | ||
| 3582 | { | ||
| 3583 | if (ch == c) RRETURN(MATCH_NOMATCH); | ||
| 3584 | } | ||
| 3585 | else | ||
| 3586 | { | ||
| 3587 | ph10 | 836 | #ifdef SUPPORT_UCP |
| 3588 | zherczeg | 924 | if (ch > 127) |
| 3589 | och = UCD_OTHERCASE(ch); | ||
| 3590 | ph10 | 836 | #else |
| 3591 | zherczeg | 924 | if (ch > 127) |
| 3592 | och = ch; | ||
| 3593 | ph10 | 836 | #endif /* SUPPORT_UCP */ |
| 3594 | zherczeg | 924 | else |
| 3595 | och = TABLE_GET(ch, md->fcc, ch); | ||
| 3596 | if (ch == c || och == c) RRETURN(MATCH_NOMATCH); | ||
| 3597 | } | ||
| 3598 | nigel | 77 | } |
| 3599 | zherczeg | 924 | else |
| 3600 | #endif | ||
| 3601 | nigel | 77 | { |
| 3602 | zherczeg | 924 | register unsigned int ch = ecode[1]; |
| 3603 | c = *eptr++; | ||
| 3604 | if (ch == c || (op == OP_NOTI && TABLE_GET(ch, md->fcc, ch) == c)) | ||
| 3605 | RRETURN(MATCH_NOMATCH); | ||
| 3606 | ecode += 2; | ||
| 3607 | nigel | 77 | } |
| 3608 | break; | ||
| 3609 | |||
| 3610 | /* Match a negated single one-byte character repeatedly. This is almost a | ||
| 3611 | repeat of the code for a repeated single character, but I haven't found a | ||
| 3612 | nice way of commoning these up that doesn't require a test of the | ||
| 3613 | positive/negative option for each character match. Maybe that wouldn't add | ||
| 3614 | very much to the time taken, but character matching *is* what this is all | ||
| 3615 | about... */ | ||
| 3616 | |||
| 3617 | case OP_NOTEXACT: | ||
| 3618 | ph10 | 602 | case OP_NOTEXACTI: |
| 3619 | nigel | 77 | min = max = GET2(ecode, 1); |
| 3620 | ph10 | 836 | ecode += 1 + IMM2_SIZE; |
| 3621 | nigel | 77 | goto REPEATNOTCHAR; |
| 3622 | |||
| 3623 | case OP_NOTUPTO: | ||
| 3624 | ph10 | 602 | case OP_NOTUPTOI: |
| 3625 | nigel | 77 | case OP_NOTMINUPTO: |
| 3626 | ph10 | 602 | case OP_NOTMINUPTOI: |
| 3627 | nigel | 77 | min = 0; |
| 3628 | max = GET2(ecode, 1); | ||
| 3629 | ph10 | 602 | minimize = *ecode == OP_NOTMINUPTO || *ecode == OP_NOTMINUPTOI; |
| 3630 | ph10 | 836 | ecode += 1 + IMM2_SIZE; |
| 3631 | nigel | 77 | goto REPEATNOTCHAR; |
| 3632 | |||
| 3633 | nigel | 93 | case OP_NOTPOSSTAR: |
| 3634 | ph10 | 602 | case OP_NOTPOSSTARI: |
| 3635 | nigel | 93 | possessive = TRUE; |
| 3636 | min = 0; | ||
| 3637 | max = INT_MAX; | ||
| 3638 | ecode++; | ||
| 3639 | goto REPEATNOTCHAR; | ||
| 3640 | |||
| 3641 | case OP_NOTPOSPLUS: | ||
| 3642 | ph10 | 602 | case OP_NOTPOSPLUSI: |
| 3643 | nigel | 93 | possessive = TRUE; |
| 3644 | min = 1; | ||
| 3645 | max = INT_MAX; | ||
| 3646 | ecode++; | ||
| 3647 | goto REPEATNOTCHAR; | ||
| 3648 | |||
| 3649 | case OP_NOTPOSQUERY: | ||
| 3650 | ph10 | 602 | case OP_NOTPOSQUERYI: |
| 3651 | nigel | 93 | possessive = TRUE; |
| 3652 | min = 0; | ||
| 3653 | max = 1; | ||
| 3654 | ecode++; | ||
| 3655 | goto REPEATNOTCHAR; | ||
| 3656 | |||
| 3657 | case OP_NOTPOSUPTO: | ||
| 3658 | ph10 | 602 | case OP_NOTPOSUPTOI: |
| 3659 | nigel | 93 | possessive = TRUE; |
| 3660 | min = 0; | ||
| 3661 | max = GET2(ecode, 1); | ||
| 3662 | ph10 | 836 | ecode += 1 + IMM2_SIZE; |
| 3663 | nigel | 93 | goto REPEATNOTCHAR; |
| 3664 | |||
| 3665 | nigel | 77 | case OP_NOTSTAR: |
| 3666 | ph10 | 602 | case OP_NOTSTARI: |
| 3667 | nigel | 77 | case OP_NOTMINSTAR: |
| 3668 | ph10 | 602 | case OP_NOTMINSTARI: |
| 3669 | nigel | 77 | case OP_NOTPLUS: |
| 3670 | ph10 | 602 | case OP_NOTPLUSI: |
| 3671 | nigel | 77 | case OP_NOTMINPLUS: |
| 3672 | ph10 | 602 | case OP_NOTMINPLUSI: |
| 3673 | nigel | 77 | case OP_NOTQUERY: |
| 3674 | ph10 | 602 | case OP_NOTQUERYI: |
| 3675 | nigel | 77 | case OP_NOTMINQUERY: |
| 3676 | ph10 | 602 | case OP_NOTMINQUERYI: |
| 3677 | c = *ecode++ - ((op >= OP_NOTSTARI)? OP_NOTSTARI: OP_NOTSTAR); | ||
| 3678 | nigel | 77 | minimize = (c & 1) != 0; |
| 3679 | min = rep_min[c]; /* Pick up values from tables; */ | ||
| 3680 | max = rep_max[c]; /* zero for max => infinity */ | ||
| 3681 | if (max == 0) max = INT_MAX; | ||
| 3682 | |||
| 3683 | ph10 | 426 | /* Common code for all repeated single-byte matches. */ |
| 3684 | nigel | 77 | |
| 3685 | REPEATNOTCHAR: | ||
| 3686 | zherczeg | 924 | GETCHARINCTEST(fc, ecode); |
| 3687 | nigel | 77 | |
| 3688 | /* The code is duplicated for the caseless and caseful cases, for speed, | ||
| 3689 | since matching characters is likely to be quite common. First, ensure the | ||
| 3690 | minimum number of matches are present. If min = max, continue at the same | ||
| 3691 | level without recursing. Otherwise, if minimizing, keep trying the rest of | ||
| 3692 | the expression and advancing one matching character if failing, up to the | ||
| 3693 | maximum. Alternatively, if maximizing, find the maximum number of | ||
| 3694 | characters and work backwards. */ | ||
| 3695 | |||
| 3696 | DPRINTF(("negative matching %c{%d,%d} against subject %.*s\n", fc, min, max, | ||
| 3697 | max, eptr)); | ||
| 3698 | |||
| 3699 | ph10 | 602 | if (op >= OP_NOTSTARI) /* Caseless */ |
| 3700 | nigel | 77 | { |
| 3701 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 3702 | #ifdef SUPPORT_UCP | ||
| 3703 | if (utf && fc > 127) | ||
| 3704 | foc = UCD_OTHERCASE(fc); | ||
| 3705 | #else | ||
| 3706 | if (utf && fc > 127) | ||
| 3707 | foc = fc; | ||
| 3708 | #endif /* SUPPORT_UCP */ | ||
| 3709 | else | ||
| 3710 | #endif /* SUPPORT_UTF */ | ||
| 3711 | foc = TABLE_GET(fc, md->fcc, fc); | ||
| 3712 | nigel | 77 | |
| 3713 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 3714 | if (utf) | ||
| 3715 | nigel | 77 | { |
| 3716 | nigel | 93 | register unsigned int d; |
| 3717 | nigel | 77 | for (i = 1; i <= min; i++) |
| 3718 | { | ||
| 3719 | ph10 | 426 | if (eptr >= md->end_subject) |
| 3720 | { | ||
| 3721 | SCHECK_PARTIAL(); | ||
| 3722 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3723 | ph10 | 427 | } |
| 3724 | nigel | 77 | GETCHARINC(d, eptr); |
| 3725 | zherczeg | 924 | if (fc == d || (unsigned int)foc == d) RRETURN(MATCH_NOMATCH); |
| 3726 | nigel | 77 | } |
| 3727 | } | ||
| 3728 | else | ||
| 3729 | #endif | ||
| 3730 | ph10 | 836 | /* Not UTF mode */ |
| 3731 | nigel | 77 | { |
| 3732 | for (i = 1; i <= min; i++) | ||
| 3733 | ph10 | 426 | { |
| 3734 | if (eptr >= md->end_subject) | ||
| 3735 | { | ||
| 3736 | SCHECK_PARTIAL(); | ||
| 3737 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3738 | ph10 | 427 | } |
| 3739 | ph10 | 836 | if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH); |
| 3740 | eptr++; | ||
| 3741 | ph10 | 427 | } |
| 3742 | nigel | 77 | } |
| 3743 | |||
| 3744 | if (min == max) continue; | ||
| 3745 | |||
| 3746 | if (minimize) | ||
| 3747 | { | ||
| 3748 | ph10 | 836 | #ifdef SUPPORT_UTF |
| 3749 | if (utf) | ||
| 3750 | nigel | 77 | { |
| 3751 | nigel | 93 | register unsigned int d; |
| 3752 | nigel | 77 | for (fi = min;; fi++) |
| 3753 | { | ||
| 3754 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM28); |
| 3755 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 3756 | ph10 | 836 | if (fi >= max) RRETURN(MATCH_NOMATCH); |
| 3757 | ph10 | 427 | if (eptr >= md->end_subject) |
| 3758 | ph10 | 426 | { |
| 3759 | ph10 | 427 | SCHECK_PARTIAL(); |
| 3760 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3761 | ph10 | 427 | } |
| 3762 | nigel | 77 | GETCHARINC(d, eptr); |
| 3763 | ph10 | 904 | if (fc == d || (unsigned int)foc == d) RRETURN(MATCH_NOMATCH); |
| 3764 | nigel | 77 | } |
| 3765 | } | ||
| 3766 | else | ||
| 3767 | #endif | ||
| 3768 | ph10 | 836 | /* Not UTF mode */ |
| 3769 | nigel | 77 | { |
| 3770 | for (fi = min;; fi++) | ||
| 3771 | { | ||
| 3772 | ph10 | 604 | RMATCH(eptr, ecode, offset_top, md, eptrb, RM29); |
| 3773 | nigel | 77 | if (rrc != MATCH_NOMATCH) RRETURN(rrc); |
| 3774 | ph10 | 836 | if (fi >= max) RRETURN(MATCH_NOMATCH); |
| 3775 | ph10 | 426 | if (eptr >= md->end_subject) |
| 3776 | { | ||
| 3777 | SCHECK_PARTIAL(); | ||
| 3778 | ph10 | 836 | RRETURN(MATCH_NOMATCH); |
| 3779 | ph10 | 426 | } |
| 3780 | ph10 | 836 | if (fc == *eptr || foc == *eptr) RRETURN(MATCH_NOMATCH); |
| 3781 | eptr++; | ||
| 3782 | nigel | 77 | } |
| 3783 | } | ||