PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/harbour-1.0.0/source/hbpcre/pcreexec.c

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