PageRenderTime 39ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/harbour-2.0.0/external/pcre/pcreexec.c

#
C | 2073 lines | 1329 code | 316 blank | 428 comment | 390 complexity | d6aa033f5405db69dcebe3bf35ce5e24 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2009 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This module contains pcre_exec(), the externally visible function that does
  33. pattern matching using an NFA algorithm, trying to mimic Perl as closely as
  34. possible. There are also some static supporting functions. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #define NLBLOCK md /* Block containing newline information */
  39. #define PSSTART start_subject /* Field containing processed string start */
  40. #define PSEND end_subject /* Field containing processed string end */
  41. #include "pcreinal.h"
  42. /* Undefine some potentially clashing cpp symbols */
  43. #undef min
  44. #undef max
  45. /* Flag bits for the match() function */
  46. #define match_condassert 0x01 /* Called to check a condition assertion */
  47. #define match_cbegroup 0x02 /* Could-be-empty unlimited repeat group */
  48. /* Non-error returns from the match() function. Error returns are externally
  49. defined PCRE_ERROR_xxx codes, which are all negative. */
  50. #define MATCH_MATCH 1
  51. #define MATCH_NOMATCH 0
  52. /* Special internal returns from the match() function. Make them sufficiently
  53. negative to avoid the external error codes. */
  54. #define MATCH_COMMIT (-999)
  55. #define MATCH_PRUNE (-998)
  56. #define MATCH_SKIP (-997)
  57. #define MATCH_THEN (-996)
  58. /* Maximum number of ints of offset to save on the stack for recursive calls.
  59. If the offset vector is bigger, malloc is used. This should be a multiple of 3,
  60. because the offset vector is always a multiple of 3 long. */
  61. #define REC_STACK_SAVE_MAX 30
  62. /* Min and max values for the common repeats; for the maxima, 0 => infinity */
  63. static const char rep_min[] = { 0, 0, 1, 1, 0, 0 };
  64. static const char rep_max[] = { 0, 0, 0, 0, 1, 1 };
  65. #ifdef DEBUG
  66. /*************************************************
  67. * Debugging function to print chars *
  68. *************************************************/
  69. /* Print a sequence of chars in printable format, stopping at the end of the
  70. subject if the requested.
  71. Arguments:
  72. p points to characters
  73. length number to print
  74. is_subject TRUE if printing from within md->start_subject
  75. md pointer to matching data block, if is_subject is TRUE
  76. Returns: nothing
  77. */
  78. static void
  79. pchars(const uschar *p, int length, BOOL is_subject, match_data *md)
  80. {
  81. unsigned int c;
  82. if (is_subject && length > md->end_subject - p) length = md->end_subject - p;
  83. while (length-- > 0)
  84. if (isprint(c = *(p++))) printf("%c", c); else printf("\\x%02x", c);
  85. }
  86. #endif
  87. /*************************************************
  88. * Match a back-reference *
  89. *************************************************/
  90. /* If a back reference hasn't been set, the length that is passed is greater
  91. than the number of characters left in the string, so the match fails.
  92. Arguments:
  93. offset index into the offset vector
  94. eptr points into the subject
  95. length length to be matched
  96. md points to match data block
  97. ims the ims flags
  98. Returns: TRUE if matched
  99. */
  100. static BOOL
  101. match_ref(int offset, register USPTR eptr, int length, match_data *md,
  102. unsigned long int ims)
  103. {
  104. USPTR p = md->start_subject + md->offset_vector[offset];
  105. #ifdef DEBUG
  106. if (eptr >= md->end_subject)
  107. printf("matching subject <null>");
  108. else
  109. {
  110. printf("matching subject ");
  111. pchars(eptr, length, TRUE, md);
  112. }
  113. printf(" against backref ");
  114. pchars(p, length, FALSE, md);
  115. printf("\n");
  116. #endif
  117. /* Always fail if not enough characters left */
  118. if (length > md->end_subject - eptr) return FALSE;
  119. /* Separate the caseless case for speed. In UTF-8 mode we can only do this
  120. properly if Unicode properties are supported. Otherwise, we can check only
  121. ASCII characters. */
  122. if ((ims & PCRE_CASELESS) != 0)
  123. {
  124. #ifdef SUPPORT_UTF8
  125. #ifdef SUPPORT_UCP
  126. if (md->utf8)
  127. {
  128. USPTR endptr = eptr + length;
  129. while (eptr < endptr)
  130. {
  131. int c, d;
  132. GETCHARINC(c, eptr);
  133. GETCHARINC(d, p);
  134. if (c != d && c != UCD_OTHERCASE(d)) return FALSE;
  135. }
  136. }
  137. else
  138. #endif
  139. #endif
  140. /* The same code works when not in UTF-8 mode and in UTF-8 mode when there
  141. is no UCP support. */
  142. while (length-- > 0)
  143. { if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE; }
  144. }
  145. /* In the caseful case, we can just compare the bytes, whether or not we
  146. are in UTF-8 mode. */
  147. else
  148. { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }
  149. return TRUE;
  150. }
  151. /***************************************************************************
  152. ****************************************************************************
  153. RECURSION IN THE match() FUNCTION
  154. The match() function is highly recursive, though not every recursive call
  155. increases the recursive depth. Nevertheless, some regular expressions can cause
  156. it to recurse to a great depth. I was writing for Unix, so I just let it call
  157. itself recursively. This uses the stack for saving everything that has to be
  158. saved for a recursive call. On Unix, the stack can be large, and this works
  159. fine.
  160. It turns out that on some non-Unix-like systems there are problems with
  161. programs that use a lot of stack. (This despite the fact that every last chip
  162. has oodles of memory these days, and techniques for extending the stack have
  163. been known for decades.) So....
  164. There is a fudge, triggered by defining NO_RECURSE, which avoids recursive
  165. calls by keeping local variables that need to be preserved in blocks of memory
  166. obtained from malloc() instead instead of on the stack. Macros are used to
  167. achieve this so that the actual code doesn't look very different to what it
  168. always used to.
  169. The original heap-recursive code used longjmp(). However, it seems that this
  170. can be very slow on some operating systems. Following a suggestion from Stan
  171. Switzer, the use of longjmp() has been abolished, at the cost of having to
  172. provide a unique number for each call to RMATCH. There is no way of generating
  173. a sequence of numbers at compile time in C. I have given them names, to make
  174. them stand out more clearly.
  175. Crude tests on x86 Linux show a small speedup of around 5-8%. However, on
  176. FreeBSD, avoiding longjmp() more than halves the time taken to run the standard
  177. tests. Furthermore, not using longjmp() means that local dynamic variables
  178. don't have indeterminate values; this has meant that the frame size can be
  179. reduced because the result can be "passed back" by straight setting of the
  180. variable instead of being passed in the frame.
  181. ****************************************************************************
  182. ***************************************************************************/
  183. /* Numbers for RMATCH calls. When this list is changed, the code at HEAP_RETURN
  184. below must be updated in sync. */
  185. enum { RM1=1, RM2, RM3, RM4, RM5, RM6, RM7, RM8, RM9, RM10,
  186. RM11, RM12, RM13, RM14, RM15, RM16, RM17, RM18, RM19, RM20,
  187. RM21, RM22, RM23, RM24, RM25, RM26, RM27, RM28, RM29, RM30,
  188. RM31, RM32, RM33, RM34, RM35, RM36, RM37, RM38, RM39, RM40,
  189. RM41, RM42, RM43, RM44, RM45, RM46, RM47, RM48, RM49, RM50,
  190. RM51, RM52, RM53, RM54 };
  191. /* These versions of the macros use the stack, as normal. There are debugging
  192. versions and production versions. Note that the "rw" argument of RMATCH isn't
  193. actuall used in this definition. */
  194. #ifndef NO_RECURSE
  195. #define REGISTER register
  196. #ifdef DEBUG
  197. #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \
  198. { \
  199. printf("match() called in line %d\n", __LINE__); \
  200. rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1); \
  201. printf("to line %d\n", __LINE__); \
  202. }
  203. #define RRETURN(ra) \
  204. { \
  205. printf("match() returned %d from line %d ", ra, __LINE__); \
  206. return ra; \
  207. }
  208. #else
  209. #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw) \
  210. rrc = match(ra,rb,mstart,rc,rd,re,rf,rg,rdepth+1)
  211. #define RRETURN(ra) return ra
  212. #endif
  213. #else
  214. /* These versions of the macros manage a private stack on the heap. Note that
  215. the "rd" argument of RMATCH isn't actually used in this definition. It's the md
  216. argument of match(), which never changes. */
  217. #define REGISTER
  218. #define RMATCH(ra,rb,rc,rd,re,rf,rg,rw)\
  219. {\
  220. heapframe *newframe = (pcre_stack_malloc)(sizeof(heapframe));\
  221. frame->Xwhere = rw; \
  222. newframe->Xeptr = ra;\
  223. newframe->Xecode = rb;\
  224. newframe->Xmstart = mstart;\
  225. newframe->Xoffset_top = rc;\
  226. newframe->Xims = re;\
  227. newframe->Xeptrb = rf;\
  228. newframe->Xflags = rg;\
  229. newframe->Xrdepth = frame->Xrdepth + 1;\
  230. newframe->Xprevframe = frame;\
  231. frame = newframe;\
  232. DPRINTF(("restarting from line %d\n", __LINE__));\
  233. goto HEAP_RECURSE;\
  234. L_##rw:\
  235. DPRINTF(("jumped back to line %d\n", __LINE__));\
  236. }
  237. #define RRETURN(ra)\
  238. {\
  239. heapframe *newframe = frame;\
  240. frame = newframe->Xprevframe;\
  241. (pcre_stack_free)(newframe);\
  242. if (frame != NULL)\
  243. {\
  244. rrc = ra;\
  245. goto HEAP_RETURN;\
  246. }\
  247. return ra;\
  248. }
  249. /* Structure for remembering the local variables in a private frame */
  250. typedef struct heapframe {
  251. struct heapframe *Xprevframe;
  252. /* Function arguments that may change */
  253. USPTR Xeptr;
  254. const uschar *Xecode;
  255. USPTR Xmstart;
  256. int Xoffset_top;
  257. long int Xims;
  258. eptrblock *Xeptrb;
  259. int Xflags;
  260. unsigned int Xrdepth;
  261. /* Function local variables */
  262. USPTR Xcallpat;
  263. #ifdef SUPPORT_UTF8
  264. USPTR Xcharptr;
  265. #endif
  266. USPTR Xdata;
  267. USPTR Xnext;
  268. USPTR Xpp;
  269. USPTR Xprev;
  270. USPTR Xsaved_eptr;
  271. recursion_info Xnew_recursive;
  272. BOOL Xcur_is_word;
  273. BOOL Xcondition;
  274. BOOL Xprev_is_word;
  275. unsigned long int Xoriginal_ims;
  276. #ifdef SUPPORT_UCP
  277. int Xprop_type;
  278. int Xprop_value;
  279. int Xprop_fail_result;
  280. int Xprop_category;
  281. int Xprop_chartype;
  282. int Xprop_script;
  283. int Xoclength;
  284. uschar Xocchars[8];
  285. #endif
  286. int Xcodelink;
  287. int Xctype;
  288. unsigned int Xfc;
  289. int Xfi;
  290. int Xlength;
  291. int Xmax;
  292. int Xmin;
  293. int Xnumber;
  294. int Xoffset;
  295. int Xop;
  296. int Xsave_capture_last;
  297. int Xsave_offset1, Xsave_offset2, Xsave_offset3;
  298. int Xstacksave[REC_STACK_SAVE_MAX];
  299. eptrblock Xnewptrb;
  300. /* Where to jump back to */
  301. int Xwhere;
  302. } heapframe;
  303. #endif
  304. /***************************************************************************
  305. ***************************************************************************/
  306. /*************************************************
  307. * Match from current position *
  308. *************************************************/
  309. /* This function is called recursively in many circumstances. Whenever it
  310. returns a negative (error) response, the outer incarnation must also return the
  311. same response. */
  312. /* These macros pack up tests that are used for partial matching, and which
  313. appears several times in the code. We set the "hit end" flag if the pointer is
  314. at the end of the subject and also past the start of the subject (i.e.
  315. something has been matched). For hard partial matching, we then return
  316. immediately. The second one is used when we already know we are past the end of
  317. the subject. */
  318. #define CHECK_PARTIAL()\
  319. if (md->partial != 0 && eptr >= md->end_subject && eptr > mstart)\
  320. {\
  321. md->hitend = TRUE;\
  322. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);\
  323. }
  324. #define SCHECK_PARTIAL()\
  325. if (md->partial != 0 && eptr > mstart)\
  326. {\
  327. md->hitend = TRUE;\
  328. if (md->partial > 1) RRETURN(PCRE_ERROR_PARTIAL);\
  329. }
  330. /* Performance note: It might be tempting to extract commonly used fields from
  331. the md structure (e.g. utf8, end_subject) into individual variables to improve
  332. performance. Tests using gcc on a SPARC disproved this; in the first case, it
  333. made performance worse.
  334. Arguments:
  335. eptr pointer to current character in subject
  336. ecode pointer to current position in compiled code
  337. mstart pointer to the current match start position (can be modified
  338. by encountering \K)
  339. offset_top current top pointer
  340. md pointer to "static" info for the match
  341. ims current /i, /m, and /s options
  342. eptrb pointer to chain of blocks containing eptr at start of
  343. brackets - for testing for empty matches
  344. flags can contain
  345. match_condassert - this is an assertion condition
  346. match_cbegroup - this is the start of an unlimited repeat
  347. group that can match an empty string
  348. rdepth the recursion depth
  349. Returns: MATCH_MATCH if matched ) these values are >= 0
  350. MATCH_NOMATCH if failed to match )
  351. a negative PCRE_ERROR_xxx value if aborted by an error condition
  352. (e.g. stopped by repeated call or recursion limit)
  353. */
  354. static int
  355. match(REGISTER USPTR eptr, REGISTER const uschar *ecode, USPTR mstart,
  356. int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,
  357. int flags, unsigned int rdepth)
  358. {
  359. /* These variables do not need to be preserved over recursion in this function,
  360. so they can be ordinary variables in all cases. Mark some of them with
  361. "register" because they are used a lot in loops. */
  362. register int rrc; /* Returns from recursive calls */
  363. register int i; /* Used for loops not involving calls to RMATCH() */
  364. register unsigned int c; /* Character values not kept over RMATCH() calls */
  365. register BOOL utf8; /* Local copy of UTF-8 flag for speed */
  366. BOOL minimize, possessive; /* Quantifier options */
  367. int condcode;
  368. /* When recursion is not being used, all "local" variables that have to be
  369. preserved over calls to RMATCH() are part of a "frame" which is obtained from
  370. heap storage. Set up the top-level frame here; others are obtained from the
  371. heap whenever RMATCH() does a "recursion". See the macro definitions above. */
  372. #ifdef NO_RECURSE
  373. heapframe *frame = (pcre_stack_malloc)(sizeof(heapframe));
  374. frame->Xprevframe = NULL; /* Marks the top level */
  375. /* Copy in the original argument variables */
  376. frame->Xeptr = eptr;
  377. frame->Xecode = ecode;
  378. frame->Xmstart = mstart;
  379. frame->Xoffset_top = offset_top;
  380. frame->Xims = ims;
  381. frame->Xeptrb = eptrb;
  382. frame->Xflags = flags;
  383. frame->Xrdepth = rdepth;
  384. /* This is where control jumps back to to effect "recursion" */
  385. HEAP_RECURSE:
  386. /* Macros make the argument variables come from the current frame */
  387. #define eptr frame->Xeptr
  388. #define ecode frame->Xecode
  389. #define mstart frame->Xmstart
  390. #define offset_top frame->Xoffset_top
  391. #define ims frame->Xims
  392. #define eptrb frame->Xeptrb
  393. #define flags frame->Xflags
  394. #define rdepth frame->Xrdepth
  395. /* Ditto for the local variables */
  396. #ifdef SUPPORT_UTF8
  397. #define charptr frame->Xcharptr
  398. #endif
  399. #define callpat frame->Xcallpat
  400. #define codelink frame->Xcodelink
  401. #define data frame->Xdata
  402. #define next frame->Xnext
  403. #define pp frame->Xpp
  404. #define prev frame->Xprev
  405. #define saved_eptr frame->Xsaved_eptr
  406. #define new_recursive frame->Xnew_recursive
  407. #define cur_is_word frame->Xcur_is_word
  408. #define condition frame->Xcondition
  409. #define prev_is_word frame->Xprev_is_word
  410. #define original_ims frame->Xoriginal_ims
  411. #ifdef SUPPORT_UCP
  412. #define prop_type frame->Xprop_type
  413. #define prop_value frame->Xprop_value
  414. #define prop_fail_result frame->Xprop_fail_result
  415. #define prop_category frame->Xprop_category
  416. #define prop_chartype frame->Xprop_chartype
  417. #define prop_script frame->Xprop_script
  418. #define oclength frame->Xoclength
  419. #define occhars frame->Xocchars
  420. #endif
  421. #define ctype frame->Xctype
  422. #define fc frame->Xfc
  423. #define fi frame->Xfi
  424. #define length frame->Xlength
  425. #define max frame->Xmax
  426. #define min frame->Xmin
  427. #define number frame->Xnumber
  428. #define offset frame->Xoffset
  429. #define op frame->Xop
  430. #define save_capture_last frame->Xsave_capture_last
  431. #define save_offset1 frame->Xsave_offset1
  432. #define save_offset2 frame->Xsave_offset2
  433. #define save_offset3 frame->Xsave_offset3
  434. #define stacksave frame->Xstacksave
  435. #define newptrb frame->Xnewptrb
  436. /* When recursion is being used, local variables are allocated on the stack and
  437. get preserved during recursion in the normal way. In this environment, fi and
  438. i, and fc and c, can be the same variables. */
  439. #else /* NO_RECURSE not defined */
  440. #define fi i
  441. #define fc c
  442. #ifdef SUPPORT_UTF8 /* Many of these variables are used only */
  443. const uschar *charptr; /* in small blocks of the code. My normal */
  444. #endif /* style of coding would have declared */
  445. const uschar *callpat; /* them within each of those blocks. */
  446. const uschar *data; /* However, in order to accommodate the */
  447. const uschar *next; /* version of this code that uses an */
  448. USPTR pp; /* external "stack" implemented on the */
  449. const uschar *prev; /* heap, it is easier to declare them all */
  450. USPTR saved_eptr; /* here, so the declarations can be cut */
  451. /* out in a block. The only declarations */
  452. recursion_info new_recursive; /* within blocks below are for variables */
  453. /* that do not have to be preserved over */
  454. BOOL cur_is_word; /* a recursive call to RMATCH(). */
  455. BOOL condition;
  456. BOOL prev_is_word;
  457. unsigned long int original_ims;
  458. #ifdef SUPPORT_UCP
  459. int prop_type;
  460. int prop_value;
  461. int prop_fail_result;
  462. int prop_category;
  463. int prop_chartype;
  464. int prop_script;
  465. int oclength;
  466. uschar occhars[8];
  467. #endif
  468. int codelink;
  469. int ctype;
  470. int length;
  471. int max;
  472. int min;
  473. int number;
  474. int offset;
  475. int op;
  476. int save_capture_last;
  477. int save_offset1, save_offset2, save_offset3;
  478. int stacksave[REC_STACK_SAVE_MAX];
  479. eptrblock newptrb;
  480. #endif /* NO_RECURSE */
  481. /* These statements are here to stop the compiler complaining about unitialized
  482. variables. */
  483. #ifdef SUPPORT_UCP
  484. prop_value = 0;
  485. prop_fail_result = 0;
  486. #endif
  487. /* This label is used for tail recursion, which is used in a few cases even
  488. when NO_RECURSE is not defined, in order to reduce the amount of stack that is
  489. used. Thanks to Ian Taylor for noticing this possibility and sending the
  490. original patch. */
  491. TAIL_RECURSE:
  492. /* OK, now we can get on with the real code of the function. Recursive calls
  493. are specified by the macro RMATCH and RRETURN is used to return. When
  494. NO_RECURSE is *not* defined, these just turn into a recursive call to match()
  495. and a "return", respectively (possibly with some debugging if DEBUG is
  496. defined). However, RMATCH isn't like a function call because it's quite a
  497. complicated macro. It has to be used in one particular way. This shouldn't,
  498. however, impact performance when true recursion is being used. */
  499. #ifdef SUPPORT_UTF8
  500. utf8 = md->utf8; /* Local copy of the flag */
  501. #else
  502. utf8 = FALSE;
  503. #endif
  504. /* First check that we haven't called match() too many times, or that we
  505. haven't exceeded the recursive call limit. */
  506. if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
  507. if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
  508. original_ims = ims; /* Save for resetting on ')' */
  509. /* At the start of a group with an unlimited repeat that may match an empty
  510. string, the match_cbegroup flag is set. When this is the case, add the current
  511. subject pointer to the chain of such remembered pointers, to be checked when we
  512. hit the closing ket, in order to break infinite loops that match no characters.
  513. When match() is called in other circumstances, don't add to the chain. The
  514. match_cbegroup flag must NOT be used with tail recursion, because the memory
  515. block that is used is on the stack, so a new one may be required for each
  516. match(). */
  517. if ((flags & match_cbegroup) != 0)
  518. {
  519. newptrb.epb_saved_eptr = eptr;
  520. newptrb.epb_prev = eptrb;
  521. eptrb = &newptrb;
  522. }
  523. /* Now start processing the opcodes. */
  524. for (;;)
  525. {
  526. minimize = possessive = FALSE;
  527. op = *ecode;
  528. switch(op)
  529. {
  530. case OP_FAIL:
  531. RRETURN(MATCH_NOMATCH);
  532. case OP_PRUNE:
  533. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
  534. ims, eptrb, flags, RM51);
  535. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  536. RRETURN(MATCH_PRUNE);
  537. case OP_COMMIT:
  538. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
  539. ims, eptrb, flags, RM52);
  540. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  541. RRETURN(MATCH_COMMIT);
  542. case OP_SKIP:
  543. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
  544. ims, eptrb, flags, RM53);
  545. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  546. md->start_match_ptr = eptr; /* Pass back current position */
  547. RRETURN(MATCH_SKIP);
  548. case OP_THEN:
  549. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
  550. ims, eptrb, flags, RM54);
  551. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  552. RRETURN(MATCH_THEN);
  553. /* Handle a capturing bracket. If there is space in the offset vector, save
  554. the current subject position in the working slot at the top of the vector.
  555. We mustn't change the current values of the data slot, because they may be
  556. set from a previous iteration of this group, and be referred to by a
  557. reference inside the group.
  558. If the bracket fails to match, we need to restore this value and also the
  559. values of the final offsets, in case they were set by a previous iteration
  560. of the same bracket.
  561. If there isn't enough space in the offset vector, treat this as if it were
  562. a non-capturing bracket. Don't worry about setting the flag for the error
  563. case here; that is handled in the code for KET. */
  564. case OP_CBRA:
  565. case OP_SCBRA:
  566. number = GET2(ecode, 1+LINK_SIZE);
  567. offset = number << 1;
  568. #ifdef DEBUG
  569. printf("start bracket %d\n", number);
  570. printf("subject=");
  571. pchars(eptr, 16, TRUE, md);
  572. printf("\n");
  573. #endif
  574. if (offset < md->offset_max)
  575. {
  576. save_offset1 = md->offset_vector[offset];
  577. save_offset2 = md->offset_vector[offset+1];
  578. save_offset3 = md->offset_vector[md->offset_end - number];
  579. save_capture_last = md->capture_last;
  580. DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
  581. md->offset_vector[md->offset_end - number] = eptr - md->start_subject;
  582. flags = (op == OP_SCBRA)? match_cbegroup : 0;
  583. do
  584. {
  585. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
  586. ims, eptrb, flags, RM1);
  587. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
  588. md->capture_last = save_capture_last;
  589. ecode += GET(ecode, 1);
  590. }
  591. while (*ecode == OP_ALT);
  592. DPRINTF(("bracket %d failed\n", number));
  593. md->offset_vector[offset] = save_offset1;
  594. md->offset_vector[offset+1] = save_offset2;
  595. md->offset_vector[md->offset_end - number] = save_offset3;
  596. RRETURN(MATCH_NOMATCH);
  597. }
  598. /* FALL THROUGH ... Insufficient room for saving captured contents. Treat
  599. as a non-capturing bracket. */
  600. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  601. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  602. DPRINTF(("insufficient capture room: treat as non-capturing\n"));
  603. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  604. /* VVVVVVVVVVVVVVVVVVVVVVVVV */
  605. /* Non-capturing bracket. Loop for all the alternatives. When we get to the
  606. final alternative within the brackets, we would return the result of a
  607. recursive call to match() whatever happened. We can reduce stack usage by
  608. turning this into a tail recursion, except in the case when match_cbegroup
  609. is set.*/
  610. case OP_BRA:
  611. case OP_SBRA:
  612. DPRINTF(("start non-capturing bracket\n"));
  613. flags = (op >= OP_SBRA)? match_cbegroup : 0;
  614. for (;;)
  615. {
  616. if (ecode[GET(ecode, 1)] != OP_ALT) /* Final alternative */
  617. {
  618. if (flags == 0) /* Not a possibly empty group */
  619. {
  620. ecode += _pcre_OP_lengths[*ecode];
  621. DPRINTF(("bracket 0 tail recursion\n"));
  622. goto TAIL_RECURSE;
  623. }
  624. /* Possibly empty group; can't use tail recursion. */
  625. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,
  626. eptrb, flags, RM48);
  627. RRETURN(rrc);
  628. }
  629. /* For non-final alternatives, continue the loop for a NOMATCH result;
  630. otherwise return. */
  631. RMATCH(eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,
  632. eptrb, flags, RM2);
  633. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
  634. ecode += GET(ecode, 1);
  635. }
  636. /* Control never reaches here. */
  637. /* Conditional group: compilation checked that there are no more than
  638. two branches. If the condition is false, skipping the first branch takes us
  639. past the end if there is only one branch, but that's OK because that is
  640. exactly what going to the ket would do. As there is only one branch to be
  641. obeyed, we can use tail recursion to avoid using another stack frame. */
  642. case OP_COND:
  643. case OP_SCOND:
  644. codelink= GET(ecode, 1);
  645. /* Because of the way auto-callout works during compile, a callout item is
  646. inserted between OP_COND and an assertion condition. */
  647. if (ecode[LINK_SIZE+1] == OP_CALLOUT)
  648. {
  649. if (pcre_callout != NULL)
  650. {
  651. pcre_callout_block cb;
  652. cb.version = 1; /* Version 1 of the callout block */
  653. cb.callout_number = ecode[LINK_SIZE+2];
  654. cb.offset_vector = md->offset_vector;
  655. cb.subject = (PCRE_SPTR)md->start_subject;
  656. cb.subject_length = md->end_subject - md->start_subject;
  657. cb.start_match = mstart - md->start_subject;
  658. cb.current_position = eptr - md->start_subject;
  659. cb.pattern_position = GET(ecode, LINK_SIZE + 3);
  660. cb.next_item_length = GET(ecode, 3 + 2*LINK_SIZE);
  661. cb.capture_top = offset_top/2;
  662. cb.capture_last = md->capture_last;
  663. cb.callout_data = md->callout_data;
  664. if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);
  665. if (rrc < 0) RRETURN(rrc);
  666. }
  667. ecode += _pcre_OP_lengths[OP_CALLOUT];
  668. }
  669. condcode = ecode[LINK_SIZE+1];
  670. /* Now see what the actual condition is */
  671. if (condcode == OP_RREF || condcode == OP_NRREF) /* Recursion test */
  672. {
  673. if (md->recursive == NULL) /* Not recursing => FALSE */
  674. {
  675. condition = FALSE;
  676. ecode += GET(ecode, 1);
  677. }
  678. else
  679. {
  680. int recno = GET2(ecode, LINK_SIZE + 2); /* Recursion group number*/
  681. condition = (recno == RREF_ANY || recno == md->recursive->group_num);
  682. /* If the test is for recursion into a specific subpattern, and it is
  683. false, but the test was set up by name, scan the table to see if the
  684. name refers to any other numbers, and test them. The condition is true
  685. if any one is set. */
  686. if (!condition && condcode == OP_NRREF && recno != RREF_ANY)
  687. {
  688. uschar *slotA = md->name_table;
  689. for (i = 0; i < md->name_count; i++)
  690. {
  691. if (GET2(slotA, 0) == recno) break;
  692. slotA += md->name_entry_size;
  693. }
  694. /* Found a name for the number - there can be only one; duplicate
  695. names for different numbers are allowed, but not vice versa. First
  696. scan down for duplicates. */
  697. if (i < md->name_count)
  698. {
  699. uschar *slotB = slotA;
  700. while (slotB > md->name_table)
  701. {
  702. slotB -= md->name_entry_size;
  703. if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
  704. {
  705. condition = GET2(slotB, 0) == md->recursive->group_num;
  706. if (condition) break;
  707. }
  708. else break;
  709. }
  710. /* Scan up for duplicates */
  711. if (!condition)
  712. {
  713. slotB = slotA;
  714. for (i++; i < md->name_count; i++)
  715. {
  716. slotB += md->name_entry_size;
  717. if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
  718. {
  719. condition = GET2(slotB, 0) == md->recursive->group_num;
  720. if (condition) break;
  721. }
  722. else break;
  723. }
  724. }
  725. }
  726. }
  727. /* Chose branch according to the condition */
  728. ecode += condition? 3 : GET(ecode, 1);
  729. }
  730. }
  731. else if (condcode == OP_CREF || condcode == OP_NCREF) /* Group used test */
  732. {
  733. offset = GET2(ecode, LINK_SIZE+2) << 1; /* Doubled ref number */
  734. condition = offset < offset_top && md->offset_vector[offset] >= 0;
  735. /* If the numbered capture is unset, but the reference was by name,
  736. scan the table to see if the name refers to any other numbers, and test
  737. them. The condition is true if any one is set. This is tediously similar
  738. to the code above, but not close enough to try to amalgamate. */
  739. if (!condition && condcode == OP_NCREF)
  740. {
  741. int refno = offset >> 1;
  742. uschar *slotA = md->name_table;
  743. for (i = 0; i < md->name_count; i++)
  744. {
  745. if (GET2(slotA, 0) == refno) break;
  746. slotA += md->name_entry_size;
  747. }
  748. /* Found a name for the number - there can be only one; duplicate names
  749. for different numbers are allowed, but not vice versa. First scan down
  750. for duplicates. */
  751. if (i < md->name_count)
  752. {
  753. uschar *slotB = slotA;
  754. while (slotB > md->name_table)
  755. {
  756. slotB -= md->name_entry_size;
  757. if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
  758. {
  759. offset = GET2(slotB, 0) << 1;
  760. condition = offset < offset_top &&
  761. md->offset_vector[offset] >= 0;
  762. if (condition) break;
  763. }
  764. else break;
  765. }
  766. /* Scan up for duplicates */
  767. if (!condition)
  768. {
  769. slotB = slotA;
  770. for (i++; i < md->name_count; i++)
  771. {
  772. slotB += md->name_entry_size;
  773. if (strcmp((char *)slotA + 2, (char *)slotB + 2) == 0)
  774. {
  775. offset = GET2(slotB, 0) << 1;
  776. condition = offset < offset_top &&
  777. md->offset_vector[offset] >= 0;
  778. if (condition) break;
  779. }
  780. else break;
  781. }
  782. }
  783. }
  784. }
  785. /* Chose branch according to the condition */
  786. ecode += condition? 3 : GET(ecode, 1);
  787. }
  788. else if (condcode == OP_DEF) /* DEFINE - always false */
  789. {
  790. condition = FALSE;
  791. ecode += GET(ecode, 1);
  792. }
  793. /* The condition is an assertion. Call match() to evaluate it - setting
  794. the final argument match_condassert causes it to stop at the end of an
  795. assertion. */
  796. else
  797. {
  798. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,
  799. match_condassert, RM3);
  800. if (rrc == MATCH_MATCH)
  801. {
  802. condition = TRUE;
  803. ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
  804. while (*ecode == OP_ALT) ecode += GET(ecode, 1);
  805. }
  806. else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
  807. {
  808. RRETURN(rrc); /* Need braces because of following else */
  809. }
  810. else
  811. {
  812. condition = FALSE;
  813. ecode += codelink;
  814. }
  815. }
  816. /* We are now at the branch that is to be obeyed. As there is only one,
  817. we can use tail recursion to avoid using another stack frame, except when
  818. match_cbegroup is required for an unlimited repeat of a possibly empty
  819. group. If the second alternative doesn't exist, we can just plough on. */
  820. if (condition || *ecode == OP_ALT)
  821. {
  822. ecode += 1 + LINK_SIZE;
  823. if (op == OP_SCOND) /* Possibly empty group */
  824. {
  825. RMATCH(eptr, ecode, offset_top, md, ims, eptrb, match_cbegroup, RM49);
  826. RRETURN(rrc);
  827. }
  828. else /* Group must match something */
  829. {
  830. flags = 0;
  831. goto TAIL_RECURSE;
  832. }
  833. }
  834. else /* Condition false & no alternative */
  835. {
  836. ecode += 1 + LINK_SIZE;
  837. }
  838. break;
  839. /* Before OP_ACCEPT there may be any number of OP_CLOSE opcodes,
  840. to close any currently open capturing brackets. */
  841. case OP_CLOSE:
  842. number = GET2(ecode, 1);
  843. offset = number << 1;
  844. #ifdef DEBUG
  845. printf("end bracket %d at *ACCEPT", number);
  846. printf("\n");
  847. #endif
  848. md->capture_last = number;
  849. if (offset >= md->offset_max) md->offset_overflow = TRUE; else
  850. {
  851. md->offset_vector[offset] =
  852. md->offset_vector[md->offset_end - number];
  853. md->offset_vector[offset+1] = eptr - md->start_subject;
  854. if (offset_top <= offset) offset_top = offset + 2;
  855. }
  856. ecode += 3;
  857. break;
  858. /* End of the pattern, either real or forced. If we are in a top-level
  859. recursion, we should restore the offsets appropriately and continue from
  860. after the call. */
  861. case OP_ACCEPT:
  862. case OP_END:
  863. if (md->recursive != NULL && md->recursive->group_num == 0)
  864. {
  865. recursion_info *rec = md->recursive;
  866. DPRINTF(("End of pattern in a (?0) recursion\n"));
  867. md->recursive = rec->prevrec;
  868. memmove(md->offset_vector, rec->offset_save,
  869. rec->saved_max * sizeof(int));
  870. offset_top = rec->save_offset_top;
  871. mstart = rec->save_start;
  872. ims = original_ims;
  873. ecode = rec->after_call;
  874. break;
  875. }
  876. /* Otherwise, if we have matched an empty string, fail if PCRE_NOTEMPTY is
  877. set, or if PCRE_NOTEMPTY_ATSTART is set and we have matched at the start of
  878. the subject. In both cases, backtracking will then try other alternatives,
  879. if any. */
  880. if (eptr == mstart &&
  881. (md->notempty ||
  882. (md->notempty_atstart &&
  883. mstart == md->start_subject + md->start_offset)))
  884. RRETURN(MATCH_NOMATCH);
  885. /* Otherwise, we have a match. */
  886. md->end_match_ptr = eptr; /* Record where we ended */
  887. md->end_offset_top = offset_top; /* and how many extracts were taken */
  888. md->start_match_ptr = mstart; /* and the start (\K can modify) */
  889. RRETURN(MATCH_MATCH);
  890. /* Change option settings */
  891. case OP_OPT:
  892. ims = ecode[1];
  893. ecode += 2;
  894. DPRINTF(("ims set to %02lx\n", ims));
  895. break;
  896. /* Assertion brackets. Check the alternative branches in turn - the
  897. matching won't pass the KET for an assertion. If any one branch matches,
  898. the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
  899. start of each branch to move the current point backwards, so the code at
  900. this level is identical to the lookahead case. */
  901. case OP_ASSERT:
  902. case OP_ASSERTBACK:
  903. do
  904. {
  905. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,
  906. RM4);
  907. if (rrc == MATCH_MATCH) break;
  908. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
  909. ecode += GET(ecode, 1);
  910. }
  911. while (*ecode == OP_ALT);
  912. if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);
  913. /* If checking an assertion for a condition, return MATCH_MATCH. */
  914. if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);
  915. /* Continue from after the assertion, updating the offsets high water
  916. mark, since extracts may have been taken during the assertion. */
  917. do ecode += GET(ecode,1); while (*ecode == OP_ALT);
  918. ecode += 1 + LINK_SIZE;
  919. offset_top = md->end_offset_top;
  920. continue;
  921. /* Negative assertion: all branches must fail to match */
  922. case OP_ASSERT_NOT:
  923. case OP_ASSERTBACK_NOT:
  924. do
  925. {
  926. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0,
  927. RM5);
  928. if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);
  929. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
  930. ecode += GET(ecode,1);
  931. }
  932. while (*ecode == OP_ALT);
  933. if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);
  934. ecode += 1 + LINK_SIZE;
  935. continue;
  936. /* Move the subject pointer back. This occurs only at the start of
  937. each branch of a lookbehind assertion. If we are too close to the start to
  938. move back, this match function fails. When working with UTF-8 we move
  939. back a number of characters, not bytes. */
  940. case OP_REVERSE:
  941. #ifdef SUPPORT_UTF8
  942. if (utf8)
  943. {
  944. i = GET(ecode, 1);
  945. while (i-- > 0)
  946. {
  947. eptr--;
  948. if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
  949. BACKCHAR(eptr);
  950. }
  951. }
  952. else
  953. #endif
  954. /* No UTF-8 support, or not in UTF-8 mode: count is byte count */
  955. {
  956. eptr -= GET(ecode, 1);
  957. if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
  958. }
  959. /* Save the earliest consulted character, then skip to next op code */
  960. if (eptr < md->start_used_ptr) md->start_used_ptr = eptr;
  961. ecode += 1 + LINK_SIZE;
  962. break;
  963. /* The callout item calls an external function, if one is provided, passing
  964. details of the match so far. This is mainly for debugging, though the
  965. function is able to force a failure. */
  966. case OP_CALLOUT:
  967. if (pcre_callout != NULL)
  968. {
  969. pcre_callout_block cb;
  970. cb.version = 1; /* Version 1 of the callout block */
  971. cb.callout_number = ecode[1];
  972. cb.offset_vector = md->offset_vector;
  973. cb.subject = (PCRE_SPTR)md->start_subject;
  974. cb.subject_length = md->end_subject - md->start_subject;
  975. cb.start_match = mstart - md->start_subject;
  976. cb.current_position = eptr - md->start_subject;
  977. cb.pattern_position = GET(ecode, 2);
  978. cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
  979. cb.capture_top = offset_top/2;
  980. cb.capture_last = md->capture_last;
  981. cb.callout_data = md->callout_data;
  982. if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);
  983. if (rrc < 0) RRETURN(rrc);
  984. }
  985. ecode += 2 + 2*LINK_SIZE;
  986. break;
  987. /* Recursion either matches the current regex, or some subexpression. The
  988. offset data is the offset to the starting bracket from the start of the
  989. whole pattern. (This is so that it works from duplicated subpatterns.)
  990. If there are any capturing brackets started but not finished, we have to
  991. save their starting points and reinstate them after the recursion. However,
  992. we don't know how many such there are (offset_top records the completed
  993. total) so we just have to save all the potential data. There may be up to
  994. 65535 such values, which is too large to put on the stack, but using malloc
  995. for small numbers seems expensive. As a compromise, the stack is used when
  996. there are no more than REC_STACK_SAVE_MAX values to store; otherwise malloc
  997. is used. A problem is what to do if the malloc fails ... there is no way of
  998. returning to the top level with an error. Save the top REC_STACK_SAVE_MAX
  999. values on the stack, and accept that the rest may be wrong.
  1000. There are also other values that have to be saved. We use a chained
  1001. sequence of blocks that actually live on the stack. Thanks to Robin Houston
  1002. for the original version of this logic. */
  1003. case OP_RECURSE:
  1004. {
  1005. callpat = md->start_code + GET(ecode, 1);
  1006. new_recursive.group_num = (callpat == md->start_code)? 0 :
  1007. GET2(callpat, 1 + LINK_SIZE);
  1008. /* Add to "recursing stack" */
  1009. new_recursive.prevrec = md->recursive;
  1010. md->recursive = &new_recursive;
  1011. /* Find where to continue from afterwards */
  1012. ecode += 1 + LINK_SIZE;
  1013. new_recursive.after_call = ecode;
  1014. /* Now save the offset data. */
  1015. new_recursive.saved_max = md->offset_end;
  1016. if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)
  1017. new_recursive.offset_save = stacksave;
  1018. else
  1019. {
  1020. new_recursive.offset_save =
  1021. (int *)(pcre_malloc)(new_recursive.saved_max * sizeof(int));
  1022. if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
  1023. }
  1024. memcpy(new_recursive.offset_save, md->offset_vector,
  1025. new_recursive.saved_max * sizeof(int));
  1026. new_recursive.save_start = mstart;
  1027. new_recursive.save_offset_top = offset_top;
  1028. mstart = eptr;
  1029. /* OK, now we can do the recursion. For each top-level alternative we
  1030. restore the offset and recursion data. */
  1031. DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
  1032. flags = (*callpat >= OP_SBRA)? match_cbegroup : 0;
  1033. do
  1034. {
  1035. RMATCH(eptr, callpat + _pcre_OP_lengths[*callpat], offset_top,
  1036. md, ims, eptrb, flags, RM6);
  1037. if (rrc == MATCH_MATCH)
  1038. {
  1039. DPRINTF(("Recursion matched\n"));
  1040. md->recursive = new_recursive.prevrec;
  1041. if (new_recursive.offset_save != stacksave)
  1042. (pcre_free)(new_recursive.offset_save);
  1043. RRETURN(MATCH_MATCH);
  1044. }
  1045. else if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN)
  1046. {
  1047. DPRINTF(("Recursion gave error %d\n", rrc));
  1048. if (new_recursive.offset_save != stacksave)
  1049. (pcre_free)(new_recursive.offset_save);
  1050. RRETURN(rrc);
  1051. }
  1052. md->recursive = &new_recursive;
  1053. memcpy(md->offset_vector, new_recursive.offset_save,
  1054. new_recursive.saved_max * sizeof(int));
  1055. callpat += GET(callpat, 1);
  1056. }
  1057. while (*callpat == OP_ALT);
  1058. DPRINTF(("Recursion didn't match\n"));
  1059. md->recursive = new_recursive.prevrec;
  1060. if (new_recursive.offset_save != stacksave)
  1061. (pcre_free)(new_recursive.offset_save);
  1062. RRETURN(MATCH_NOMATCH);
  1063. }
  1064. /* Control never reaches here */
  1065. /* "Once" brackets are like assertion brackets except that after a match,
  1066. the point in the subject string is not moved back. Thus there can never be
  1067. a move back into the brackets. Friedl calls these "atomic" subpatterns.
  1068. Check the alternative branches in turn - the matching won't pass the KET
  1069. for this kind of subpattern. If any one branch matches, we carry on as at
  1070. the end of a normal bracket, leaving the subject pointer. */
  1071. case OP_ONCE:
  1072. prev = ecode;
  1073. saved_eptr = eptr;
  1074. do
  1075. {
  1076. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM7);
  1077. if (rrc == MATCH_MATCH) break;
  1078. if (rrc != MATCH_NOMATCH && rrc != MATCH_THEN) RRETURN(rrc);
  1079. ecode += GET(ecode,1);
  1080. }
  1081. while (*ecode == OP_ALT);
  1082. /* If hit the end of the group (which could be repeated), fail */
  1083. if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
  1084. /* Continue as from after the assertion, updating the offsets high water
  1085. mark, since extracts may have been taken. */
  1086. do ecode += GET(ecode, 1); while (*ecode == OP_ALT);
  1087. offset_top = md->end_offset_top;
  1088. eptr = md->end_match_ptr;
  1089. /* For a non-repeating ket, just continue at this level. This also
  1090. happens for a repeating ket if no characters were matched in the group.
  1091. This is the forcible breaking of infinite loops as implemented in Perl
  1092. 5.005. If there is an options reset, it will get obeyed in the normal
  1093. course of events. */
  1094. if (*ecode == OP_KET || eptr == saved_eptr)
  1095. {
  1096. ecode += 1+LINK_SIZE;
  1097. break;
  1098. }
  1099. /* The repeating kets try the rest of the pattern or restart from the
  1100. preceding bracket, in the appropriate order. The second "call" of match()
  1101. uses tail recursion, to avoid using another stack frame. We need to reset
  1102. any options that changed within the bracket before re-running it, so
  1103. check the next opcode. */
  1104. if (ecode[1+LINK_SIZE] == OP_OPT)
  1105. {
  1106. ims = (ims & ~PCRE_IMS) | ecode[4];
  1107. DPRINTF(("ims set to %02lx at group repeat\n", ims));
  1108. }
  1109. if (*ecode == OP_KETRMIN)
  1110. {
  1111. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM8);
  1112. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1113. ecode = prev;
  1114. flags = 0;
  1115. goto TAIL_RECURSE;
  1116. }
  1117. else /* OP_KETRMAX */
  1118. {
  1119. RMATCH(eptr, prev, offset_top, md, ims, eptrb, match_cbegroup, RM9);
  1120. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1121. ecode += 1 + LINK_SIZE;
  1122. flags = 0;
  1123. goto TAIL_RECURSE;
  1124. }
  1125. /* Control never gets here */
  1126. /* An alternation is the end of a branch; scan along to find the end of the
  1127. bracketed group and go to there. */
  1128. case OP_ALT:
  1129. do ecode += GET(ecode,1); while (*ecode == OP_ALT);
  1130. break;
  1131. /* BRAZERO, BRAMINZERO and SKIPZERO occur just before a bracket group,
  1132. indicating that it may occur zero times. It may repeat infinitely, or not
  1133. at all - i.e. it could be ()* or ()? or even (){0} in the pattern. Brackets
  1134. with fixed upper repeat limits are compiled as a number of copies, with the
  1135. optional ones preceded by BRAZERO or BRAMINZERO. */
  1136. case OP_BRAZERO:
  1137. {
  1138. next = ecode+1;
  1139. RMATCH(eptr, next, offset_top, md, ims, eptrb, 0, RM10);
  1140. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1141. do next += GET(next,1); while (*next == OP_ALT);
  1142. ecode = next + 1 + LINK_SIZE;
  1143. }
  1144. break;
  1145. case OP_BRAMINZERO:
  1146. {
  1147. next = ecode+1;
  1148. do next += GET(next, 1); while (*next == OP_ALT);
  1149. RMATCH(eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0, RM11);
  1150. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1151. ecode++;
  1152. }
  1153. break;
  1154. case OP_SKIPZERO:
  1155. {
  1156. next = ecode+1;
  1157. do next += GET(next,1); while (*next == OP_ALT);
  1158. ecode = next + 1 + LINK_SIZE;
  1159. }
  1160. break;
  1161. /* End of a group, repeated or non-repeating. */
  1162. case OP_KET:
  1163. case OP_KETRMIN:
  1164. case OP_KETRMAX:
  1165. prev = ecode - GET(ecode, 1);
  1166. /* If this was a group that remembered the subject start, in order to break
  1167. infinite repeats of empty string matches, retrieve the subject start from
  1168. the chain. Otherwise, set it NULL. */
  1169. if (*prev >= OP_SBRA)
  1170. {
  1171. saved_eptr = eptrb->epb_saved_eptr; /* Value at start of group */
  1172. eptrb = eptrb->epb_prev; /* Backup to previous group */
  1173. }
  1174. else saved_eptr = NULL;
  1175. /* If we are at the end of an assertion group, stop matching and return
  1176. MATCH_MATCH, but record the current high water mark for use by positive
  1177. assertions. Do this also for the "once" (atomic) groups. */
  1178. if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
  1179. *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||
  1180. *prev == OP_ONCE)
  1181. {
  1182. md->end_match_ptr = eptr; /* For ONCE */
  1183. md->end_offset_top = offset_top;
  1184. RRETURN(MATCH_MATCH);
  1185. }
  1186. /* For capturing groups we have to check the group number back at the start
  1187. and if necessary complete handling an extraction by setting the offsets and
  1188. bumping the high water mark. Note that whole-pattern recursion is coded as
  1189. a recurse into group 0, so it won't be picked up here. Instead, we catch it
  1190. when the OP_END is reached. Other recursion is handled here. */
  1191. if (*prev == OP_CBRA || *prev == OP_SCBRA)
  1192. {
  1193. number = GET2(prev, 1+LINK_SIZE);
  1194. offset = number << 1;
  1195. #ifdef DEBUG
  1196. printf("end bracket %d", number);
  1197. printf("\n");
  1198. #endif
  1199. md->capture_last = number;
  1200. if (offset >= md->offset_max) md->offset_overflow = TRUE; else
  1201. {
  1202. md->offset_vector[offset] =
  1203. md->offset_vector[md->offset_end - number];
  1204. md->offset_vector[offset+1] = eptr - md->start_subject;
  1205. if (offset_top <= offset) offset_top = offset + 2;
  1206. }
  1207. /* Handle a recursively called group. Restore the offsets
  1208. appropriately and continue from after the call. */
  1209. if (md->recursive != NULL && md->recursive->group_num == number)
  1210. {
  1211. recursion_info *rec = md->recursive;
  1212. DPRINTF(("Recursion (%d) succeeded - continuing\n", number));
  1213. md->recursive = rec->prevrec;
  1214. mstart = rec->save_start;
  1215. memcpy(md->offset_vector, rec->offset_save,
  1216. rec->saved_max * sizeof(int));
  1217. offset_top = rec->save_offset_top;
  1218. ecode = rec->after_call;
  1219. ims = original_ims;
  1220. break;
  1221. }
  1222. }
  1223. /* For both capturing and non-capturing groups, reset the value of the ims
  1224. flags, in case they got changed during the group. */
  1225. ims = original_ims;
  1226. DPRINTF(("ims reset to %02lx\n", ims));
  1227. /* For a non-repeating ket, just continue at this level. This also
  1228. happens for a repeating ket if no characters were matched in the group.
  1229. This is the forcible breaking of infinite loops as implemented in Perl
  1230. 5.005. If there is an options reset, it will get obeyed in the normal
  1231. course of events. */
  1232. if (*ecode == OP_KET || eptr == saved_eptr)
  1233. {
  1234. ecode += 1 + LINK_SIZE;
  1235. break;
  1236. }
  1237. /* The repeating kets try the rest of the pattern or restart from the
  1238. preceding bracket, in the appropriate order. In the second case, we can use
  1239. tail recursion to avoid using another stack frame, unless we have an
  1240. unlimited repeat of a group that can match an empty string. */
  1241. flags = (*prev >= OP_SBRA)? match_cbegroup : 0;
  1242. if (*ecode == OP_KETRMIN)
  1243. {
  1244. RMATCH(eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0, RM12);
  1245. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1246. if (flags != 0) /* Could match an empty string */
  1247. {
  1248. RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM50);
  1249. RRETURN(rrc);
  1250. }
  1251. ecode = prev;
  1252. goto TAIL_RECURSE;
  1253. }
  1254. else /* OP_KETRMAX */
  1255. {
  1256. RMATCH(eptr, prev, offset_top, md, ims, eptrb, flags, RM13);
  1257. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1258. ecode += 1 + LINK_SIZE;
  1259. flags = 0;
  1260. goto TAIL_RECURSE;
  1261. }
  1262. /* Control never gets here */
  1263. /* Start of subject unless notbol, or after internal newline if multiline */
  1264. case OP_CIRC:
  1265. if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
  1266. if ((ims & PCRE_MULTILINE) != 0)
  1267. {
  1268. if (eptr != md->start_subject &&
  1269. (eptr == md->end_subject || !WAS_NEWLINE(eptr)))
  1270. RRETURN(MATCH_NOMATCH);
  1271. ecode++;
  1272. break;
  1273. }
  1274. /* ... else fall through */
  1275. /* Start of subject assertion */
  1276. case OP_SOD:
  1277. if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);
  1278. ecode++;
  1279. break;
  1280. /* Start of match assertion */
  1281. case OP_SOM:
  1282. if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);
  1283. ecode++;
  1284. break;
  1285. /* Reset the start of match point */
  1286. case OP_SET_SOM:
  1287. mstart = eptr;
  1288. ecode++;
  1289. break;
  1290. /* Assert before internal newline if multiline, or before a terminating
  1291. newline unless endonly is set, else end of subject unless noteol is set. */
  1292. case OP_DOLL:
  1293. if ((ims & PCRE_MULTILINE) != 0)
  1294. {
  1295. if (eptr < md->end_subject)
  1296. { if (!IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH); }
  1297. else
  1298. { if (md->noteol) RRETURN(MATCH_NOMATCH); }
  1299. ecode++;
  1300. break;
  1301. }
  1302. else
  1303. {
  1304. if (md->noteol) RRETURN(MATCH_NOMATCH);
  1305. if (!md->endonly)
  1306. {
  1307. if (eptr != md->end_subject &&
  1308. (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
  1309. RRETURN(MATCH_NOMATCH);
  1310. ecode++;
  1311. break;
  1312. }
  1313. }
  1314. /* ... else fall through for endonly */
  1315. /* End of subject assertion (\z) */
  1316. case OP_EOD:
  1317. if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);
  1318. ecode++;
  1319. break;
  1320. /* End of subject or ending \n assertion (\Z) */
  1321. case OP_EODN:
  1322. if (eptr != md->end_subject &&
  1323. (!IS_NEWLINE(eptr) || eptr != md->end_subject - md->nllen))
  1324. RRETURN(MATCH_NOMATCH);
  1325. ecode++;
  1326. break;
  1327. /* Word boundary assertions */
  1328. case OP_NOT_WORD_BOUNDARY:
  1329. case OP_WORD_BOUNDARY:
  1330. {
  1331. /* Find out if the previous and current characters are "word" characters.
  1332. It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to
  1333. be "non-word" characters. Remember the earliest consulted character for
  1334. partial matching. */
  1335. #ifdef SUPPORT_UTF8
  1336. if (utf8)
  1337. {
  1338. if (eptr == md->start_subject) prev_is_word = FALSE; else
  1339. {
  1340. USPTR lastptr = eptr - 1;
  1341. while((*lastptr & 0xc0) == 0x80) lastptr--;
  1342. if (lastptr < md->start_used_ptr) md->start_used_ptr = lastptr;
  1343. GETCHAR(c, lastptr);
  1344. prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
  1345. }
  1346. if (eptr >= md->end_subject)
  1347. {
  1348. SCHECK_PARTIAL();
  1349. cur_is_word = FALSE;
  1350. }
  1351. else
  1352. {
  1353. GETCHAR(c, eptr);
  1354. cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
  1355. }
  1356. }
  1357. else
  1358. #endif
  1359. /* Not in UTF-8 mode */
  1360. {
  1361. if (eptr == md->start_subject) prev_is_word = FALSE; else
  1362. {
  1363. if (eptr <= md->start_used_ptr) md->start_used_ptr = eptr - 1;
  1364. prev_is_word = ((md->ctypes[eptr[-1]] & ctype_word) != 0);
  1365. }
  1366. if (eptr >= md->end_subject)
  1367. {
  1368. SCHECK_PARTIAL();
  1369. cur_is_word = FALSE;
  1370. }
  1371. else cur_is_word = ((md->ctypes[*eptr] & ctype_word) != 0);
  1372. }
  1373. /* Now see if the situation is what we want */
  1374. if ((*ecode++ == OP_WORD_BOUNDARY)?
  1375. cur_is_word == prev_is_word : cur_is_word != prev_is_word)
  1376. RRETURN(MATCH_NOMATCH);
  1377. }
  1378. break;
  1379. /* Match a single character type; inline for speed */
  1380. case OP_ANY:
  1381. if (IS_NEWLINE(eptr)) RRETURN(MATCH_NOMATCH);
  1382. /* Fall through */
  1383. case OP_ALLANY:
  1384. if (eptr++ >= md->end_subject)
  1385. {
  1386. SCHECK_PARTIAL();
  1387. RRETURN(MATCH_NOMATCH);
  1388. }
  1389. if (utf8) while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
  1390. ecode++;
  1391. break;
  1392. /* Match a single byte, even in UTF-8 mode. This opcode really does match
  1393. any byte, even newline, independent of the setting of PCRE_DOTALL. */
  1394. case OP_ANYBYTE:
  1395. if (eptr++ >= md->end_subject)
  1396. {
  1397. SCHECK_PARTIAL();
  1398. RRETURN(MATCH_NOMATCH);
  1399. }
  1400. ecode++;
  1401. break;
  1402. case OP_NOT_DIGIT:
  1403. if (eptr >= md->end_subject)
  1404. {
  1405. SCHECK_PARTIAL();
  1406. RRETURN(MATCH_NOMATCH);
  1407. }
  1408. GETCHARINCTEST(c, eptr);
  1409. if (
  1410. #ifdef SUPPORT_UTF8
  1411. c < 256 &&
  1412. #endif
  1413. (md->ctypes[c] & ctype_digit) != 0
  1414. )
  1415. RRETURN(MATCH_NOMATCH);
  1416. ecode++;
  1417. break;
  1418. case OP_DIGIT:
  1419. if (eptr >= md->end_subject)
  1420. {
  1421. SCHECK_PARTIAL();
  1422. RRETURN(MATCH_NOMATCH);
  1423. }
  1424. GETCHARINCTEST(c, eptr);
  1425. if (
  1426. #ifdef SUPPORT_UTF8
  1427. c >= 256 ||
  1428. #endif
  1429. (md->ctypes[c] & ctype_digit) == 0
  1430. )
  1431. RRETURN(MATCH_NOMATCH);
  1432. ecode++;
  1433. break;
  1434. case OP_NOT_WHITESPACE:
  1435. if (eptr >= md->end_subject)
  1436. {
  1437. SCHECK_PARTIAL();
  1438. RRETURN(MATCH_NOMATCH);
  1439. }
  1440. GETCHARINCTEST(c, eptr);
  1441. if (
  1442. #ifdef SUPPORT_UTF8
  1443. c < 256 &&
  1444. #endif
  1445. (md->ctypes[c] & ctype_space) != 0
  1446. )
  1447. RRETURN(MATCH_NOMATCH);
  1448. ecode++;
  1449. break;
  1450. case OP_WHITESPACE:
  1451. if (eptr >= md->end_subject)
  1452. {
  1453. SCHECK_PARTIAL();
  1454. RRETURN(MATCH_NOMATCH);
  1455. }
  1456. GETCHARINCTEST(c, eptr);
  1457. if (
  1458. #ifdef SUPPORT_UTF8
  1459. c >= 256 ||
  1460. #endif
  1461. (md->ctypes[c] & ctype_space) == 0
  1462. )
  1463. RRETURN(MATCH_NOMATCH);
  1464. ecode++;
  1465. break;
  1466. case OP_NOT_WORDCHAR:
  1467. if (eptr >= md->end_subject)
  1468. {
  1469. SCHECK_PARTIAL();
  1470. RRETURN(MATCH_NOMATCH);
  1471. }
  1472. GETCHARINCTEST(c, eptr);
  1473. if (
  1474. #ifdef SUPPORT_UTF8
  1475. c < 256 &&
  1476. #endif
  1477. (md->ctypes[c] & ctype_word) != 0
  1478. )
  1479. RRETURN(MATCH_NOMATCH);
  1480. ecode++;
  1481. break;
  1482. case OP_WORDCHAR:
  1483. if (eptr >= md->end_subject)
  1484. {
  1485. SCHECK_PARTIAL();
  1486. RRETURN(MATCH_NOMATCH);
  1487. }
  1488. GETCHARINCTEST(c, eptr);
  1489. if (
  1490. #ifdef SUPPORT_UTF8
  1491. c >= 256 ||
  1492. #endif
  1493. (md->ctypes[c] & ctype_word) == 0
  1494. )
  1495. RRETURN(MATCH_NOMATCH);
  1496. ecode++;
  1497. break;
  1498. case OP_ANYNL:
  1499. if (eptr >= md->end_subject)
  1500. {
  1501. SCHECK_PARTIAL();
  1502. RRETURN(MATCH_NOMATCH);
  1503. }
  1504. GETCHARINCTEST(c, eptr);
  1505. switch(c)
  1506. {
  1507. default: RRETURN(MATCH_NOMATCH);
  1508. case 0x000d:
  1509. if (eptr < md->end_subject && *eptr == 0x0a) eptr++;
  1510. break;
  1511. case 0x000a:
  1512. break;
  1513. case 0x000b:
  1514. case 0x000c:
  1515. case 0x0085:
  1516. case 0x2028:
  1517. case 0x2029:
  1518. if (md->bsr_anycrlf) RRETURN(MATCH_NOMATCH);
  1519. break;
  1520. }
  1521. ecode++;
  1522. break;
  1523. case OP_NOT_HSPACE:
  1524. if (eptr >= md->end_subject)
  1525. {
  1526. SCHECK_PARTIAL();
  1527. RRETURN(MATCH_NOMATCH);
  1528. }
  1529. GETCHARINCTEST(c, eptr);
  1530. switch(c)
  1531. {
  1532. default: break;
  1533. case 0x09: /* HT */
  1534. case 0x20: /* SPACE */
  1535. case 0xa0: /* NBSP */
  1536. case 0x1680: /* OGHAM SPACE MARK */
  1537. case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
  1538. case 0x2000: /* EN QUAD */
  1539. case 0x2001: /* EM QUAD */
  1540. case 0x2002: /* EN SPACE */
  1541. case 0x2003: /* EM SPACE */
  1542. case 0x2004: /* THREE-PER-EM SPACE */
  1543. case 0x2005: /* FOUR-PER-EM SPACE */
  1544. case 0x2006: /* SIX-PER-EM SPACE */
  1545. case 0x2007: /* FIGURE SPACE */
  1546. case 0x2008: /* PUNCTUATION SPACE */
  1547. case 0x2009: /* THIN SPACE */
  1548. case 0x200A: /* HAIR SPACE */
  1549. case 0x202f: /* NARROW NO-BREAK SPACE */
  1550. case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
  1551. case 0x3000: /* IDEOGRAPHIC SPACE */
  1552. RRETURN(MATCH_NOMATCH);
  1553. }
  1554. ecode++;
  1555. break;
  1556. case OP_HSPACE:
  1557. if (eptr >= md->end_subject)
  1558. {
  1559. SCHECK_PARTIAL();
  1560. RRETURN(MATCH_NOMATCH);
  1561. }
  1562. GETCHARINCTEST(c, eptr);
  1563. switch(c)
  1564. {
  1565. default: RRETURN(MATCH_NOMATCH);
  1566. case 0x09: /* HT */
  1567. case 0x20: /* SPACE */
  1568. case 0xa0: /* NBSP */
  1569. case 0x1680: /* OGHAM SPACE MARK */
  1570. case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
  1571. case 0x2000: /* EN QUAD */
  1572. case 0x2001: /* EM QUAD */
  1573. case 0x2002: /* EN SPACE */
  1574. case 0x2003: /* EM SPACE */
  1575. case 0x2004: /* THREE-PER-EM SPACE */
  1576. case 0x2005: /* FOUR-PER-EM SPACE */
  1577. case 0x2006: /* SIX-PER-EM SPACE */
  1578. case 0x2007: /* FIGURE SPACE */
  1579. case 0x2008: /* PUNCTUATION SPACE */
  1580. case 0x2009: /* THIN SPACE */
  1581. case 0x200A: /* HAIR SPACE */
  1582. case 0x202f: /* NARROW NO-BREAK SPACE */
  1583. case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
  1584. case 0x3000: /* IDEOGRAPHIC SPACE */
  1585. break;
  1586. }
  1587. ecode++;
  1588. break;
  1589. case OP_NOT_VSPACE:
  1590. if (eptr >= md->end_subject)
  1591. {
  1592. SCHECK_PARTIAL();
  1593. RRETURN(MATCH_NOMATCH);
  1594. }
  1595. GETCHARINCTEST(c, eptr);
  1596. switch(c)
  1597. {
  1598. default: break;
  1599. case 0x0a: /* LF */
  1600. case 0x0b: /* VT */
  1601. case 0x0c: /* FF */
  1602. case 0x0d: /* CR */
  1603. case 0x85: /* NEL */
  1604. case 0x2028: /* LINE SEPARATOR */
  1605. case 0x2029: /* PARAGRAPH SEPARATOR */
  1606. RRETURN(MATCH_NOMATCH);
  1607. }
  1608. ecode++;
  1609. break;
  1610. case OP_VSPACE:
  1611. if (eptr >= md->end_subject)
  1612. {
  1613. SCHECK_PARTIAL();
  1614. RRETURN(MATCH_NOMATCH);
  1615. }
  1616. GETCHARINCTEST(c, eptr);
  1617. switch(c)
  1618. {
  1619. default: RRETURN(MATCH_NOMATCH);
  1620. case 0x0a: /* LF */
  1621. case 0x0b: /* VT */
  1622. case 0x0c: /* FF */
  1623. case 0x0d: /* CR */
  1624. case 0x85: /* NEL */
  1625. case 0x2028: /* LINE SEPARATOR */
  1626. case 0x2029: /* PARAGRAPH SEPARATOR */
  1627. break;
  1628. }
  1629. ecode++;
  1630. break;
  1631. #ifdef SUPPORT_UCP
  1632. /* Check the next character by Unicode property. We will get here only
  1633. if the support is in the binary; otherwise a compile-time error occurs. */
  1634. case OP_PROP:
  1635. case OP_NOTPROP:
  1636. if (eptr >= md->end_subject)
  1637. {
  1638. SCHECK_PARTIAL();
  1639. RRETURN(MATCH_NOMATCH);
  1640. }
  1641. GETCHARINCTEST(c, eptr);
  1642. {
  1643. const ucd_record *prop = GET_UCD(c);
  1644. switch(ecode[1])
  1645. {
  1646. case PT_ANY:
  1647. if (op == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
  1648. break;
  1649. case PT_LAMP:
  1650. if ((prop->chartype == ucp_Lu ||
  1651. prop->chartype == ucp_Ll ||
  1652. prop->chartype == ucp_Lt) == (op == OP_NOTPROP))
  1653. RRETURN(MATCH_NOMATCH);
  1654. break;
  1655. case PT_GC:
  1656. if ((ecode[2] != _pcre_ucp_gentype[prop->chartype]) == (op == OP_PROP))
  1657. RRETURN(MATCH_NOMATCH);
  1658. break;
  1659. case PT_PC:
  1660. if ((ecode[2] != prop->chartype) == (op == OP_PROP))
  1661. RRETURN(MATCH_NOMATCH);
  1662. break;
  1663. case PT_SC:
  1664. if ((ecode[2] != prop->script) == (op == OP_PROP))
  1665. RRETURN(MATCH_NOMATCH);
  1666. break;
  1667. default:
  1668. RRETURN(PCRE_ERROR_INTERNAL);
  1669. }
  1670. ecode += 3;
  1671. }
  1672. break;
  1673. /* Match an extended Unicode sequence. We will get here only if the support
  1674. is in the binary; otherwise a compile-time error occurs. */
  1675. case OP_EXTUNI:
  1676. if (eptr >= md->end_subject)
  1677. {
  1678. SCHECK_PARTIAL();
  1679. RRETURN(MATCH_NOMATCH);
  1680. }
  1681. GETCHARINCTEST(c, eptr);
  1682. {
  1683. int category = UCD_CATEGORY(c);
  1684. if (category == ucp_M) RRETURN(MATCH_NOMATCH);
  1685. while (eptr < md->end_subject)
  1686. {
  1687. int len = 1;
  1688. if (!utf8) c = *eptr; else
  1689. {
  1690. GETCHARLEN(c, eptr, len);
  1691. }
  1692. category = UCD_CATEGORY(c);
  1693. if (category != ucp_M) break;
  1694. eptr += len;
  1695. }
  1696. }
  1697. ecode++;
  1698. break;
  1699. #endif
  1700. /* Match a back reference, possibly repeatedly. Look past the end of the
  1701. item to see if there is repeat information following. The code is similar
  1702. to that for character classes, but repeated for efficiency. Then obey
  1703. similar code to character type repeats - written out again for speed.
  1704. However, if the referenced string is the empty string, always treat
  1705. it as matched, any number of times (otherwise there could be infinite
  1706. loops). */
  1707. case OP_REF:
  1708. {
  1709. offset = GET2(ecode, 1) << 1; /* Doubled ref number */
  1710. ecode += 3;
  1711. /* If the reference is unset, there are two possibilities:
  1712. (a) In the default, Perl-compatible state, set the length to be longer
  1713. than the amount of subject left; this ensures that every attempt at a
  1714. match fails. We can't just fail here, because of the possibility of
  1715. quantifiers with zero minima.
  1716. (b) If the JavaScript compatibility flag is set, set the length to zero
  1717. so that the back reference matches an empty string.
  1718. Otherwise, set the length to the length of what was matched by the
  1719. referenced subpattern. */
  1720. if (offset >= offset_top || md->offset_vector[offset] < 0)
  1721. length = (md->jscript_compat)? 0 : md->end_subject - eptr + 1;
  1722. else
  1723. length = md->offset_vector[offset+1] - md->offset_vector[offset];
  1724. /* Set up for repetition, or handle the non-repeated case */
  1725. switch (*ecode)
  1726. {
  1727. case OP_CRSTAR:
  1728. case OP_CRMINSTAR:
  1729. case OP_CRPLUS:
  1730. case OP_CRMINPLUS:
  1731. case OP_CRQUERY:
  1732. case OP_CRMINQUERY:
  1733. c