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