PageRenderTime 64ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/beta3/harbour/source/hbpcre/pcreexec.c

#
C | 2089 lines | 1344 code | 323 blank | 422 comment | 484 complexity | bba09a04d1133e5b6c51b277f9fa9264 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

Large files files are truncated, but you can click here to view the full file

  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-2005 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. #include "pcreinal.h"
  36. /* Structure for building a chain of data that actually lives on the
  37. stack, for holding the values of the subject pointer at the start of each
  38. subpattern, so as to detect when an empty string has been matched by a
  39. subpattern - to break infinite loops. When NO_RECURSE is set, these blocks
  40. are on the heap, not on the stack. */
  41. typedef struct eptrblock {
  42. struct eptrblock *epb_prev;
  43. const uschar *epb_saved_eptr;
  44. } eptrblock;
  45. /* Flag bits for the match() function */
  46. #define match_condassert 0x01 /* Called to check a condition assertion */
  47. #define match_isgroup 0x02 /* Set if start of bracketed 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. /* Maximum number of ints of offset to save on the stack for recursive calls.
  53. If the offset vector is bigger, malloc is used. This should be a multiple of 3,
  54. because the offset vector is always a multiple of 3 long. */
  55. #define REC_STACK_SAVE_MAX 30
  56. /* Min and max values for the common repeats; for the maxima, 0 => infinity */
  57. static const char rep_min[] = { 0, 0, 1, 1, 0, 0 };
  58. static const char rep_max[] = { 0, 0, 0, 0, 1, 1 };
  59. #ifdef DEBUG
  60. /*************************************************
  61. * Debugging function to print chars *
  62. *************************************************/
  63. /* Print a sequence of chars in printable format, stopping at the end of the
  64. subject if the requested.
  65. Arguments:
  66. p points to characters
  67. length number to print
  68. is_subject TRUE if printing from within md->start_subject
  69. md pointer to matching data block, if is_subject is TRUE
  70. Returns: nothing
  71. */
  72. static void
  73. pchars(const uschar *p, int length, BOOL is_subject, match_data *md)
  74. {
  75. int c;
  76. if (is_subject && length > md->end_subject - p) length = md->end_subject - p;
  77. while (length-- > 0)
  78. if (isprint(c = *(p++))) printf("%c", c); else printf("\\x%02x", c);
  79. }
  80. #endif
  81. /*************************************************
  82. * Match a back-reference *
  83. *************************************************/
  84. /* If a back reference hasn't been set, the length that is passed is greater
  85. than the number of characters left in the string, so the match fails.
  86. Arguments:
  87. offset index into the offset vector
  88. eptr points into the subject
  89. length length to be matched
  90. md points to match data block
  91. ims the ims flags
  92. Returns: TRUE if matched
  93. */
  94. static BOOL
  95. match_ref(int offset, register const uschar *eptr, int length, match_data *md,
  96. unsigned long int ims)
  97. {
  98. const uschar *p = md->start_subject + md->offset_vector[offset];
  99. #ifdef DEBUG
  100. if (eptr >= md->end_subject)
  101. printf("matching subject <null>");
  102. else
  103. {
  104. printf("matching subject ");
  105. pchars(eptr, length, TRUE, md);
  106. }
  107. printf(" against backref ");
  108. pchars(p, length, FALSE, md);
  109. printf("\n");
  110. #endif
  111. /* Always fail if not enough characters left */
  112. if (length > md->end_subject - eptr) return FALSE;
  113. /* Separate the caselesss case for speed */
  114. if ((ims & PCRE_CASELESS) != 0)
  115. {
  116. while (length-- > 0)
  117. if (md->lcc[*p++] != md->lcc[*eptr++]) return FALSE;
  118. }
  119. else
  120. { while (length-- > 0) if (*p++ != *eptr++) return FALSE; }
  121. return TRUE;
  122. }
  123. /***************************************************************************
  124. ****************************************************************************
  125. RECURSION IN THE match() FUNCTION
  126. The match() function is highly recursive. Some regular expressions can cause
  127. it to recurse thousands of times. I was writing for Unix, so I just let it
  128. call itself recursively. This uses the stack for saving everything that has
  129. to be saved for a recursive call. On Unix, the stack can be large, and this
  130. works fine.
  131. It turns out that on non-Unix systems there are problems with programs that
  132. use a lot of stack. (This despite the fact that every last chip has oodles
  133. of memory these days, and techniques for extending the stack have been known
  134. for decades.) So....
  135. There is a fudge, triggered by defining NO_RECURSE, which avoids recursive
  136. calls by keeping local variables that need to be preserved in blocks of memory
  137. obtained from malloc instead instead of on the stack. Macros are used to
  138. achieve this so that the actual code doesn't look very different to what it
  139. always used to.
  140. ****************************************************************************
  141. ***************************************************************************/
  142. /* These versions of the macros use the stack, as normal */
  143. #ifndef NO_RECURSE
  144. #define REGISTER register
  145. #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg) rx = match(ra,rb,rc,rd,re,rf,rg)
  146. #define RRETURN(ra) return ra
  147. #else
  148. /* These versions of the macros manage a private stack on the heap. Note
  149. that the rd argument of RMATCH isn't actually used. It's the md argument of
  150. match(), which never changes. */
  151. #define REGISTER
  152. #define RMATCH(rx,ra,rb,rc,rd,re,rf,rg)\
  153. {\
  154. heapframe *newframe = (pcre_stack_malloc)(sizeof(heapframe));\
  155. if (setjmp(frame->Xwhere) == 0)\
  156. {\
  157. newframe->Xeptr = ra;\
  158. newframe->Xecode = rb;\
  159. newframe->Xoffset_top = rc;\
  160. newframe->Xims = re;\
  161. newframe->Xeptrb = rf;\
  162. newframe->Xflags = rg;\
  163. newframe->Xprevframe = frame;\
  164. frame = newframe;\
  165. DPRINTF(("restarting from line %d\n", __LINE__));\
  166. goto HEAP_RECURSE;\
  167. }\
  168. else\
  169. {\
  170. DPRINTF(("longjumped back to line %d\n", __LINE__));\
  171. frame = md->thisframe;\
  172. rx = frame->Xresult;\
  173. }\
  174. }
  175. #define RRETURN(ra)\
  176. {\
  177. heapframe *newframe = frame;\
  178. frame = newframe->Xprevframe;\
  179. (pcre_stack_free)(newframe);\
  180. if (frame != NULL)\
  181. {\
  182. frame->Xresult = ra;\
  183. md->thisframe = frame;\
  184. longjmp(frame->Xwhere, 1);\
  185. }\
  186. return ra;\
  187. }
  188. /* Structure for remembering the local variables in a private frame */
  189. typedef struct heapframe {
  190. struct heapframe *Xprevframe;
  191. /* Function arguments that may change */
  192. const uschar *Xeptr;
  193. const uschar *Xecode;
  194. int Xoffset_top;
  195. long int Xims;
  196. eptrblock *Xeptrb;
  197. int Xflags;
  198. /* Function local variables */
  199. const uschar *Xcallpat;
  200. const uschar *Xcharptr;
  201. const uschar *Xdata;
  202. const uschar *Xnext;
  203. const uschar *Xpp;
  204. const uschar *Xprev;
  205. const uschar *Xsaved_eptr;
  206. recursion_info Xnew_recursive;
  207. BOOL Xcur_is_word;
  208. BOOL Xcondition;
  209. BOOL Xminimize;
  210. BOOL Xprev_is_word;
  211. unsigned long int Xoriginal_ims;
  212. #ifdef SUPPORT_UCP
  213. int Xprop_type;
  214. int Xprop_fail_result;
  215. int Xprop_category;
  216. int Xprop_chartype;
  217. int Xprop_othercase;
  218. int Xprop_test_against;
  219. int *Xprop_test_variable;
  220. #endif
  221. int Xctype;
  222. int Xfc;
  223. int Xfi;
  224. int Xlength;
  225. int Xmax;
  226. int Xmin;
  227. int Xnumber;
  228. int Xoffset;
  229. int Xop;
  230. int Xsave_capture_last;
  231. int Xsave_offset1, Xsave_offset2, Xsave_offset3;
  232. int Xstacksave[REC_STACK_SAVE_MAX];
  233. eptrblock Xnewptrb;
  234. /* Place to pass back result, and where to jump back to */
  235. int Xresult;
  236. jmp_buf Xwhere;
  237. } heapframe;
  238. #endif
  239. /***************************************************************************
  240. ***************************************************************************/
  241. /*************************************************
  242. * Match from current position *
  243. *************************************************/
  244. /* On entry ecode points to the first opcode, and eptr to the first character
  245. in the subject string, while eptrb holds the value of eptr at the start of the
  246. last bracketed group - used for breaking infinite loops matching zero-length
  247. strings. This function is called recursively in many circumstances. Whenever it
  248. returns a negative (error) response, the outer incarnation must also return the
  249. same response.
  250. Performance note: It might be tempting to extract commonly used fields from the
  251. md structure (e.g. utf8, end_subject) into individual variables to improve
  252. performance. Tests using gcc on a SPARC disproved this; in the first case, it
  253. made performance worse.
  254. Arguments:
  255. eptr pointer in subject
  256. ecode position in code
  257. offset_top current top pointer
  258. md pointer to "static" info for the match
  259. ims current /i, /m, and /s options
  260. eptrb pointer to chain of blocks containing eptr at start of
  261. brackets - for testing for empty matches
  262. flags can contain
  263. match_condassert - this is an assertion condition
  264. match_isgroup - this is the start of a bracketed group
  265. Returns: MATCH_MATCH if matched ) these values are >= 0
  266. MATCH_NOMATCH if failed to match )
  267. a negative PCRE_ERROR_xxx value if aborted by an error condition
  268. (e.g. stopped by recursion limit)
  269. */
  270. static int
  271. match(REGISTER const uschar *eptr, REGISTER const uschar *ecode,
  272. int offset_top, match_data *md, unsigned long int ims, eptrblock *eptrb,
  273. int flags)
  274. {
  275. /* These variables do not need to be preserved over recursion in this function,
  276. so they can be ordinary variables in all cases. Mark them with "register"
  277. because they are used a lot in loops. */
  278. register int rrc; /* Returns from recursive calls */
  279. register int i; /* Used for loops not involving calls to RMATCH() */
  280. register int c; /* Character values not kept over RMATCH() calls */
  281. register BOOL utf8; /* Local copy of UTF-8 flag for speed */
  282. /* When recursion is not being used, all "local" variables that have to be
  283. preserved over calls to RMATCH() are part of a "frame" which is obtained from
  284. heap storage. Set up the top-level frame here; others are obtained from the
  285. heap whenever RMATCH() does a "recursion". See the macro definitions above. */
  286. #ifdef NO_RECURSE
  287. heapframe *frame = (pcre_stack_malloc)(sizeof(heapframe));
  288. frame->Xprevframe = NULL; /* Marks the top level */
  289. /* Copy in the original argument variables */
  290. frame->Xeptr = eptr;
  291. frame->Xecode = ecode;
  292. frame->Xoffset_top = offset_top;
  293. frame->Xims = ims;
  294. frame->Xeptrb = eptrb;
  295. frame->Xflags = flags;
  296. /* This is where control jumps back to to effect "recursion" */
  297. HEAP_RECURSE:
  298. /* Macros make the argument variables come from the current frame */
  299. #define eptr frame->Xeptr
  300. #define ecode frame->Xecode
  301. #define offset_top frame->Xoffset_top
  302. #define ims frame->Xims
  303. #define eptrb frame->Xeptrb
  304. #define flags frame->Xflags
  305. /* Ditto for the local variables */
  306. #ifdef SUPPORT_UTF8
  307. #define charptr frame->Xcharptr
  308. #endif
  309. #define callpat frame->Xcallpat
  310. #define data frame->Xdata
  311. #define next frame->Xnext
  312. #define pp frame->Xpp
  313. #define prev frame->Xprev
  314. #define saved_eptr frame->Xsaved_eptr
  315. #define new_recursive frame->Xnew_recursive
  316. #define cur_is_word frame->Xcur_is_word
  317. #define condition frame->Xcondition
  318. #define minimize frame->Xminimize
  319. #define prev_is_word frame->Xprev_is_word
  320. #define original_ims frame->Xoriginal_ims
  321. #ifdef SUPPORT_UCP
  322. #define prop_type frame->Xprop_type
  323. #define prop_fail_result frame->Xprop_fail_result
  324. #define prop_category frame->Xprop_category
  325. #define prop_chartype frame->Xprop_chartype
  326. #define prop_othercase frame->Xprop_othercase
  327. #define prop_test_against frame->Xprop_test_against
  328. #define prop_test_variable frame->Xprop_test_variable
  329. #endif
  330. #define ctype frame->Xctype
  331. #define fc frame->Xfc
  332. #define fi frame->Xfi
  333. #define length frame->Xlength
  334. #define max frame->Xmax
  335. #define min frame->Xmin
  336. #define number frame->Xnumber
  337. #define offset frame->Xoffset
  338. #define op frame->Xop
  339. #define save_capture_last frame->Xsave_capture_last
  340. #define save_offset1 frame->Xsave_offset1
  341. #define save_offset2 frame->Xsave_offset2
  342. #define save_offset3 frame->Xsave_offset3
  343. #define stacksave frame->Xstacksave
  344. #define newptrb frame->Xnewptrb
  345. /* When recursion is being used, local variables are allocated on the stack and
  346. get preserved during recursion in the normal way. In this environment, fi and
  347. i, and fc and c, can be the same variables. */
  348. #else
  349. #define fi i
  350. #define fc c
  351. #ifdef SUPPORT_UTF8 /* Many of these variables are used ony */
  352. const uschar *charptr; /* small blocks of the code. My normal */
  353. #endif /* style of coding would have declared */
  354. const uschar *callpat; /* them within each of those blocks. */
  355. const uschar *data; /* However, in order to accommodate the */
  356. const uschar *next; /* version of this code that uses an */
  357. const uschar *pp; /* external "stack" implemented on the */
  358. const uschar *prev; /* heap, it is easier to declare them */
  359. const uschar *saved_eptr; /* all here, so the declarations can */
  360. /* be cut out in a block. The only */
  361. recursion_info new_recursive; /* declarations within blocks below are */
  362. /* for variables that do not have to */
  363. BOOL cur_is_word; /* be preserved over a recursive call */
  364. BOOL condition; /* to RMATCH(). */
  365. BOOL minimize;
  366. BOOL prev_is_word;
  367. unsigned long int original_ims;
  368. #ifdef SUPPORT_UCP
  369. int prop_type;
  370. int prop_fail_result;
  371. int prop_category;
  372. int prop_chartype;
  373. int prop_othercase;
  374. int prop_test_against;
  375. int *prop_test_variable;
  376. #endif
  377. int ctype;
  378. int length;
  379. int max;
  380. int min;
  381. int number;
  382. int offset;
  383. int op;
  384. int save_capture_last;
  385. int save_offset1, save_offset2, save_offset3;
  386. int stacksave[REC_STACK_SAVE_MAX];
  387. eptrblock newptrb;
  388. #endif
  389. /* These statements are here to stop the compiler complaining about unitialized
  390. variables. */
  391. #ifdef SUPPORT_UCP
  392. prop_fail_result = 0;
  393. prop_test_against = 0;
  394. prop_test_variable = NULL;
  395. #endif
  396. /* OK, now we can get on with the real code of the function. Recursion is
  397. specified by the macros RMATCH and RRETURN. When NO_RECURSE is *not* defined,
  398. these just turn into a recursive call to match() and a "return", respectively.
  399. However, RMATCH isn't like a function call because it's quite a complicated
  400. macro. It has to be used in one particular way. This shouldn't, however, impact
  401. performance when true recursion is being used. */
  402. if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
  403. original_ims = ims; /* Save for resetting on ')' */
  404. utf8 = md->utf8; /* Local copy of the flag */
  405. /* At the start of a bracketed group, add the current subject pointer to the
  406. stack of such pointers, to be re-instated at the end of the group when we hit
  407. the closing ket. When match() is called in other circumstances, we don't add to
  408. this stack. */
  409. if ((flags & match_isgroup) != 0)
  410. {
  411. newptrb.epb_prev = eptrb;
  412. newptrb.epb_saved_eptr = eptr;
  413. eptrb = &newptrb;
  414. }
  415. /* Now start processing the operations. */
  416. for (;;)
  417. {
  418. op = *ecode;
  419. minimize = FALSE;
  420. /* For partial matching, remember if we ever hit the end of the subject after
  421. matching at least one subject character. */
  422. if (md->partial &&
  423. eptr >= md->end_subject &&
  424. eptr > md->start_match)
  425. md->hitend = TRUE;
  426. /* Opening capturing bracket. If there is space in the offset vector, save
  427. the current subject position in the working slot at the top of the vector. We
  428. mustn't change the current values of the data slot, because they may be set
  429. from a previous iteration of this group, and be referred to by a reference
  430. inside the group.
  431. If the bracket fails to match, we need to restore this value and also the
  432. values of the final offsets, in case they were set by a previous iteration of
  433. the same bracket.
  434. If there isn't enough space in the offset vector, treat this as if it were a
  435. non-capturing bracket. Don't worry about setting the flag for the error case
  436. here; that is handled in the code for KET. */
  437. if (op > OP_BRA)
  438. {
  439. number = op - OP_BRA;
  440. /* For extended extraction brackets (large number), we have to fish out the
  441. number from a dummy opcode at the start. */
  442. if (number > EXTRACT_BASIC_MAX)
  443. number = GET2(ecode, 2+LINK_SIZE);
  444. offset = number << 1;
  445. #ifdef DEBUG
  446. printf("start bracket %d subject=", number);
  447. pchars(eptr, 16, TRUE, md);
  448. printf("\n");
  449. #endif
  450. if (offset < md->offset_max)
  451. {
  452. save_offset1 = md->offset_vector[offset];
  453. save_offset2 = md->offset_vector[offset+1];
  454. save_offset3 = md->offset_vector[md->offset_end - number];
  455. save_capture_last = md->capture_last;
  456. DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
  457. md->offset_vector[md->offset_end - number] = eptr - md->start_subject;
  458. do
  459. {
  460. RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,
  461. match_isgroup);
  462. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  463. md->capture_last = save_capture_last;
  464. ecode += GET(ecode, 1);
  465. }
  466. while (*ecode == OP_ALT);
  467. DPRINTF(("bracket %d failed\n", number));
  468. md->offset_vector[offset] = save_offset1;
  469. md->offset_vector[offset+1] = save_offset2;
  470. md->offset_vector[md->offset_end - number] = save_offset3;
  471. RRETURN(MATCH_NOMATCH);
  472. }
  473. /* Insufficient room for saving captured contents */
  474. else op = OP_BRA;
  475. }
  476. /* Other types of node can be handled by a switch */
  477. switch(op)
  478. {
  479. case OP_BRA: /* Non-capturing bracket: optimized */
  480. DPRINTF(("start bracket 0\n"));
  481. do
  482. {
  483. RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,
  484. match_isgroup);
  485. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  486. ecode += GET(ecode, 1);
  487. }
  488. while (*ecode == OP_ALT);
  489. DPRINTF(("bracket 0 failed\n"));
  490. RRETURN(MATCH_NOMATCH);
  491. /* Conditional group: compilation checked that there are no more than
  492. two branches. If the condition is false, skipping the first branch takes us
  493. past the end if there is only one branch, but that's OK because that is
  494. exactly what going to the ket would do. */
  495. case OP_COND:
  496. if (ecode[LINK_SIZE+1] == OP_CREF) /* Condition extract or recurse test */
  497. {
  498. offset = GET2(ecode, LINK_SIZE+2) << 1; /* Doubled ref number */
  499. condition = (offset == CREF_RECURSE * 2)?
  500. (md->recursive != NULL) :
  501. (offset < offset_top && md->offset_vector[offset] >= 0);
  502. RMATCH(rrc, eptr, ecode + (condition?
  503. (LINK_SIZE + 4) : (LINK_SIZE + 1 + GET(ecode, 1))),
  504. offset_top, md, ims, eptrb, match_isgroup);
  505. RRETURN(rrc);
  506. }
  507. /* The condition is an assertion. Call match() to evaluate it - setting
  508. the final argument TRUE causes it to stop at the end of an assertion. */
  509. else
  510. {
  511. RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,
  512. match_condassert | match_isgroup);
  513. if (rrc == MATCH_MATCH)
  514. {
  515. ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE+2);
  516. while (*ecode == OP_ALT) ecode += GET(ecode, 1);
  517. }
  518. else if (rrc != MATCH_NOMATCH)
  519. {
  520. RRETURN(rrc); /* Need braces because of following else */
  521. }
  522. else ecode += GET(ecode, 1);
  523. RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb,
  524. match_isgroup);
  525. RRETURN(rrc);
  526. }
  527. /* Control never reaches here */
  528. /* Skip over conditional reference or large extraction number data if
  529. encountered. */
  530. case OP_CREF:
  531. case OP_BRANUMBER:
  532. ecode += 3;
  533. break;
  534. /* End of the pattern. If we are in a recursion, we should restore the
  535. offsets appropriately and continue from after the call. */
  536. case OP_END:
  537. if (md->recursive != NULL && md->recursive->group_num == 0)
  538. {
  539. recursion_info *rec = md->recursive;
  540. DPRINTF(("Hit the end in a (?0) recursion\n"));
  541. md->recursive = rec->prevrec;
  542. memmove(md->offset_vector, rec->offset_save,
  543. rec->saved_max * sizeof(int));
  544. md->start_match = rec->save_start;
  545. ims = original_ims;
  546. ecode = rec->after_call;
  547. break;
  548. }
  549. /* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty
  550. string - backtracking will then try other alternatives, if any. */
  551. if (md->notempty && eptr == md->start_match) RRETURN(MATCH_NOMATCH);
  552. md->end_match_ptr = eptr; /* Record where we ended */
  553. md->end_offset_top = offset_top; /* and how many extracts were taken */
  554. RRETURN(MATCH_MATCH);
  555. /* Change option settings */
  556. case OP_OPT:
  557. ims = ecode[1];
  558. ecode += 2;
  559. DPRINTF(("ims set to %02lx\n", ims));
  560. break;
  561. /* Assertion brackets. Check the alternative branches in turn - the
  562. matching won't pass the KET for an assertion. If any one branch matches,
  563. the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
  564. start of each branch to move the current point backwards, so the code at
  565. this level is identical to the lookahead case. */
  566. case OP_ASSERT:
  567. case OP_ASSERTBACK:
  568. do
  569. {
  570. RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,
  571. match_isgroup);
  572. if (rrc == MATCH_MATCH) break;
  573. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  574. ecode += GET(ecode, 1);
  575. }
  576. while (*ecode == OP_ALT);
  577. if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);
  578. /* If checking an assertion for a condition, return MATCH_MATCH. */
  579. if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);
  580. /* Continue from after the assertion, updating the offsets high water
  581. mark, since extracts may have been taken during the assertion. */
  582. do ecode += GET(ecode,1); while (*ecode == OP_ALT);
  583. ecode += 1 + LINK_SIZE;
  584. offset_top = md->end_offset_top;
  585. continue;
  586. /* Negative assertion: all branches must fail to match */
  587. case OP_ASSERT_NOT:
  588. case OP_ASSERTBACK_NOT:
  589. do
  590. {
  591. RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,
  592. match_isgroup);
  593. if (rrc == MATCH_MATCH) RRETURN(MATCH_NOMATCH);
  594. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  595. ecode += GET(ecode,1);
  596. }
  597. while (*ecode == OP_ALT);
  598. if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);
  599. ecode += 1 + LINK_SIZE;
  600. continue;
  601. /* Move the subject pointer back. This occurs only at the start of
  602. each branch of a lookbehind assertion. If we are too close to the start to
  603. move back, this match function fails. When working with UTF-8 we move
  604. back a number of characters, not bytes. */
  605. case OP_REVERSE:
  606. #ifdef SUPPORT_UTF8
  607. if (utf8)
  608. {
  609. c = GET(ecode,1);
  610. for (i = 0; i < c; i++)
  611. {
  612. eptr--;
  613. if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
  614. BACKCHAR(eptr)
  615. }
  616. }
  617. else
  618. #endif
  619. /* No UTF-8 support, or not in UTF-8 mode: count is byte count */
  620. {
  621. eptr -= GET(ecode,1);
  622. if (eptr < md->start_subject) RRETURN(MATCH_NOMATCH);
  623. }
  624. /* Skip to next op code */
  625. ecode += 1 + LINK_SIZE;
  626. break;
  627. /* The callout item calls an external function, if one is provided, passing
  628. details of the match so far. This is mainly for debugging, though the
  629. function is able to force a failure. */
  630. case OP_CALLOUT:
  631. if (pcre_callout != NULL)
  632. {
  633. pcre_callout_block cb;
  634. cb.version = 1; /* Version 1 of the callout block */
  635. cb.callout_number = ecode[1];
  636. cb.offset_vector = md->offset_vector;
  637. cb.subject = (const char *)md->start_subject;
  638. cb.subject_length = md->end_subject - md->start_subject;
  639. cb.start_match = md->start_match - md->start_subject;
  640. cb.current_position = eptr - md->start_subject;
  641. cb.pattern_position = GET(ecode, 2);
  642. cb.next_item_length = GET(ecode, 2 + LINK_SIZE);
  643. cb.capture_top = offset_top/2;
  644. cb.capture_last = md->capture_last;
  645. cb.callout_data = md->callout_data;
  646. if ((rrc = (*pcre_callout)(&cb)) > 0) RRETURN(MATCH_NOMATCH);
  647. if (rrc < 0) RRETURN(rrc);
  648. }
  649. ecode += 2 + 2*LINK_SIZE;
  650. break;
  651. /* Recursion either matches the current regex, or some subexpression. The
  652. offset data is the offset to the starting bracket from the start of the
  653. whole pattern. (This is so that it works from duplicated subpatterns.)
  654. If there are any capturing brackets started but not finished, we have to
  655. save their starting points and reinstate them after the recursion. However,
  656. we don't know how many such there are (offset_top records the completed
  657. total) so we just have to save all the potential data. There may be up to
  658. 65535 such values, which is too large to put on the stack, but using malloc
  659. for small numbers seems expensive. As a compromise, the stack is used when
  660. there are no more than REC_STACK_SAVE_MAX values to store; otherwise malloc
  661. is used. A problem is what to do if the malloc fails ... there is no way of
  662. returning to the top level with an error. Save the top REC_STACK_SAVE_MAX
  663. values on the stack, and accept that the rest may be wrong.
  664. There are also other values that have to be saved. We use a chained
  665. sequence of blocks that actually live on the stack. Thanks to Robin Houston
  666. for the original version of this logic. */
  667. case OP_RECURSE:
  668. {
  669. callpat = md->start_code + GET(ecode, 1);
  670. new_recursive.group_num = *callpat - OP_BRA;
  671. /* For extended extraction brackets (large number), we have to fish out
  672. the number from a dummy opcode at the start. */
  673. if (new_recursive.group_num > EXTRACT_BASIC_MAX)
  674. new_recursive.group_num = GET2(callpat, 2+LINK_SIZE);
  675. /* Add to "recursing stack" */
  676. new_recursive.prevrec = md->recursive;
  677. md->recursive = &new_recursive;
  678. /* Find where to continue from afterwards */
  679. ecode += 1 + LINK_SIZE;
  680. new_recursive.after_call = ecode;
  681. /* Now save the offset data. */
  682. new_recursive.saved_max = md->offset_end;
  683. if (new_recursive.saved_max <= REC_STACK_SAVE_MAX)
  684. new_recursive.offset_save = stacksave;
  685. else
  686. {
  687. new_recursive.offset_save =
  688. (int *)(pcre_malloc)(new_recursive.saved_max * sizeof(int));
  689. if (new_recursive.offset_save == NULL) RRETURN(PCRE_ERROR_NOMEMORY);
  690. }
  691. memcpy(new_recursive.offset_save, md->offset_vector,
  692. new_recursive.saved_max * sizeof(int));
  693. new_recursive.save_start = md->start_match;
  694. md->start_match = eptr;
  695. /* OK, now we can do the recursion. For each top-level alternative we
  696. restore the offset and recursion data. */
  697. DPRINTF(("Recursing into group %d\n", new_recursive.group_num));
  698. do
  699. {
  700. RMATCH(rrc, eptr, callpat + 1 + LINK_SIZE, offset_top, md, ims,
  701. eptrb, match_isgroup);
  702. if (rrc == MATCH_MATCH)
  703. {
  704. md->recursive = new_recursive.prevrec;
  705. if (new_recursive.offset_save != stacksave)
  706. (pcre_free)(new_recursive.offset_save);
  707. RRETURN(MATCH_MATCH);
  708. }
  709. else if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  710. md->recursive = &new_recursive;
  711. memcpy(md->offset_vector, new_recursive.offset_save,
  712. new_recursive.saved_max * sizeof(int));
  713. callpat += GET(callpat, 1);
  714. }
  715. while (*callpat == OP_ALT);
  716. DPRINTF(("Recursion didn't match\n"));
  717. md->recursive = new_recursive.prevrec;
  718. if (new_recursive.offset_save != stacksave)
  719. (pcre_free)(new_recursive.offset_save);
  720. RRETURN(MATCH_NOMATCH);
  721. }
  722. /* Control never reaches here */
  723. /* "Once" brackets are like assertion brackets except that after a match,
  724. the point in the subject string is not moved back. Thus there can never be
  725. a move back into the brackets. Friedl calls these "atomic" subpatterns.
  726. Check the alternative branches in turn - the matching won't pass the KET
  727. for this kind of subpattern. If any one branch matches, we carry on as at
  728. the end of a normal bracket, leaving the subject pointer. */
  729. case OP_ONCE:
  730. {
  731. prev = ecode;
  732. saved_eptr = eptr;
  733. do
  734. {
  735. RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims,
  736. eptrb, match_isgroup);
  737. if (rrc == MATCH_MATCH) break;
  738. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  739. ecode += GET(ecode,1);
  740. }
  741. while (*ecode == OP_ALT);
  742. /* If hit the end of the group (which could be repeated), fail */
  743. if (*ecode != OP_ONCE && *ecode != OP_ALT) RRETURN(MATCH_NOMATCH);
  744. /* Continue as from after the assertion, updating the offsets high water
  745. mark, since extracts may have been taken. */
  746. do ecode += GET(ecode,1); while (*ecode == OP_ALT);
  747. offset_top = md->end_offset_top;
  748. eptr = md->end_match_ptr;
  749. /* For a non-repeating ket, just continue at this level. This also
  750. happens for a repeating ket if no characters were matched in the group.
  751. This is the forcible breaking of infinite loops as implemented in Perl
  752. 5.005. If there is an options reset, it will get obeyed in the normal
  753. course of events. */
  754. if (*ecode == OP_KET || eptr == saved_eptr)
  755. {
  756. ecode += 1+LINK_SIZE;
  757. break;
  758. }
  759. /* The repeating kets try the rest of the pattern or restart from the
  760. preceding bracket, in the appropriate order. We need to reset any options
  761. that changed within the bracket before re-running it, so check the next
  762. opcode. */
  763. if (ecode[1+LINK_SIZE] == OP_OPT)
  764. {
  765. ims = (ims & ~PCRE_IMS) | ecode[4];
  766. DPRINTF(("ims set to %02lx at group repeat\n", ims));
  767. }
  768. if (*ecode == OP_KETRMIN)
  769. {
  770. RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, eptrb, 0);
  771. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  772. RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);
  773. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  774. }
  775. else /* OP_KETRMAX */
  776. {
  777. RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);
  778. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  779. RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);
  780. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  781. }
  782. }
  783. RRETURN(MATCH_NOMATCH);
  784. /* An alternation is the end of a branch; scan along to find the end of the
  785. bracketed group and go to there. */
  786. case OP_ALT:
  787. do ecode += GET(ecode,1); while (*ecode == OP_ALT);
  788. break;
  789. /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating
  790. that it may occur zero times. It may repeat infinitely, or not at all -
  791. i.e. it could be ()* or ()? in the pattern. Brackets with fixed upper
  792. repeat limits are compiled as a number of copies, with the optional ones
  793. preceded by BRAZERO or BRAMINZERO. */
  794. case OP_BRAZERO:
  795. {
  796. next = ecode+1;
  797. RMATCH(rrc, eptr, next, offset_top, md, ims, eptrb, match_isgroup);
  798. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  799. do next += GET(next,1); while (*next == OP_ALT);
  800. ecode = next + 1+LINK_SIZE;
  801. }
  802. break;
  803. case OP_BRAMINZERO:
  804. {
  805. next = ecode+1;
  806. do next += GET(next,1); while (*next == OP_ALT);
  807. RMATCH(rrc, eptr, next + 1+LINK_SIZE, offset_top, md, ims, eptrb,
  808. match_isgroup);
  809. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  810. ecode++;
  811. }
  812. break;
  813. /* End of a group, repeated or non-repeating. If we are at the end of
  814. an assertion "group", stop matching and return MATCH_MATCH, but record the
  815. current high water mark for use by positive assertions. Do this also
  816. for the "once" (not-backup up) groups. */
  817. case OP_KET:
  818. case OP_KETRMIN:
  819. case OP_KETRMAX:
  820. {
  821. prev = ecode - GET(ecode, 1);
  822. saved_eptr = eptrb->epb_saved_eptr;
  823. /* Back up the stack of bracket start pointers. */
  824. eptrb = eptrb->epb_prev;
  825. if (*prev == OP_ASSERT || *prev == OP_ASSERT_NOT ||
  826. *prev == OP_ASSERTBACK || *prev == OP_ASSERTBACK_NOT ||
  827. *prev == OP_ONCE)
  828. {
  829. md->end_match_ptr = eptr; /* For ONCE */
  830. md->end_offset_top = offset_top;
  831. RRETURN(MATCH_MATCH);
  832. }
  833. /* In all other cases except a conditional group we have to check the
  834. group number back at the start and if necessary complete handling an
  835. extraction by setting the offsets and bumping the high water mark. */
  836. if (*prev != OP_COND)
  837. {
  838. number = *prev - OP_BRA;
  839. /* For extended extraction brackets (large number), we have to fish out
  840. the number from a dummy opcode at the start. */
  841. if (number > EXTRACT_BASIC_MAX) number = GET2(prev, 2+LINK_SIZE);
  842. offset = number << 1;
  843. #ifdef DEBUG
  844. printf("end bracket %d", number);
  845. printf("\n");
  846. #endif
  847. /* Test for a numbered group. This includes groups called as a result
  848. of recursion. Note that whole-pattern recursion is coded as a recurse
  849. into group 0, so it won't be picked up here. Instead, we catch it when
  850. the OP_END is reached. */
  851. if (number > 0)
  852. {
  853. md->capture_last = number;
  854. if (offset >= md->offset_max) md->offset_overflow = TRUE; else
  855. {
  856. md->offset_vector[offset] =
  857. md->offset_vector[md->offset_end - number];
  858. md->offset_vector[offset+1] = eptr - md->start_subject;
  859. if (offset_top <= offset) offset_top = offset + 2;
  860. }
  861. /* Handle a recursively called group. Restore the offsets
  862. appropriately and continue from after the call. */
  863. if (md->recursive != NULL && md->recursive->group_num == number)
  864. {
  865. recursion_info *rec = md->recursive;
  866. DPRINTF(("Recursion (%d) succeeded - continuing\n", number));
  867. md->recursive = rec->prevrec;
  868. md->start_match = rec->save_start;
  869. memcpy(md->offset_vector, rec->offset_save,
  870. rec->saved_max * sizeof(int));
  871. ecode = rec->after_call;
  872. ims = original_ims;
  873. break;
  874. }
  875. }
  876. }
  877. /* Reset the value of the ims flags, in case they got changed during
  878. the group. */
  879. ims = original_ims;
  880. DPRINTF(("ims reset to %02lx\n", ims));
  881. /* For a non-repeating ket, just continue at this level. This also
  882. happens for a repeating ket if no characters were matched in the group.
  883. This is the forcible breaking of infinite loops as implemented in Perl
  884. 5.005. If there is an options reset, it will get obeyed in the normal
  885. course of events. */
  886. if (*ecode == OP_KET || eptr == saved_eptr)
  887. {
  888. ecode += 1 + LINK_SIZE;
  889. break;
  890. }
  891. /* The repeating kets try the rest of the pattern or restart from the
  892. preceding bracket, in the appropriate order. */
  893. if (*ecode == OP_KETRMIN)
  894. {
  895. RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);
  896. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  897. RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);
  898. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  899. }
  900. else /* OP_KETRMAX */
  901. {
  902. RMATCH(rrc, eptr, prev, offset_top, md, ims, eptrb, match_isgroup);
  903. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  904. RMATCH(rrc, eptr, ecode + 1+LINK_SIZE, offset_top, md, ims, eptrb, 0);
  905. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  906. }
  907. }
  908. RRETURN(MATCH_NOMATCH);
  909. /* Start of subject unless notbol, or after internal newline if multiline */
  910. case OP_CIRC:
  911. if (md->notbol && eptr == md->start_subject) RRETURN(MATCH_NOMATCH);
  912. if ((ims & PCRE_MULTILINE) != 0)
  913. {
  914. if (eptr != md->start_subject && eptr[-1] != NEWLINE)
  915. RRETURN(MATCH_NOMATCH);
  916. ecode++;
  917. break;
  918. }
  919. /* ... else fall through */
  920. /* Start of subject assertion */
  921. case OP_SOD:
  922. if (eptr != md->start_subject) RRETURN(MATCH_NOMATCH);
  923. ecode++;
  924. break;
  925. /* Start of match assertion */
  926. case OP_SOM:
  927. if (eptr != md->start_subject + md->start_offset) RRETURN(MATCH_NOMATCH);
  928. ecode++;
  929. break;
  930. /* Assert before internal newline if multiline, or before a terminating
  931. newline unless endonly is set, else end of subject unless noteol is set. */
  932. case OP_DOLL:
  933. if ((ims & PCRE_MULTILINE) != 0)
  934. {
  935. if (eptr < md->end_subject)
  936. { if (*eptr != NEWLINE) RRETURN(MATCH_NOMATCH); }
  937. else
  938. { if (md->noteol) RRETURN(MATCH_NOMATCH); }
  939. ecode++;
  940. break;
  941. }
  942. else
  943. {
  944. if (md->noteol) RRETURN(MATCH_NOMATCH);
  945. if (!md->endonly)
  946. {
  947. if (eptr < md->end_subject - 1 ||
  948. (eptr == md->end_subject - 1 && *eptr != NEWLINE))
  949. RRETURN(MATCH_NOMATCH);
  950. ecode++;
  951. break;
  952. }
  953. }
  954. /* ... else fall through */
  955. /* End of subject assertion (\z) */
  956. case OP_EOD:
  957. if (eptr < md->end_subject) RRETURN(MATCH_NOMATCH);
  958. ecode++;
  959. break;
  960. /* End of subject or ending \n assertion (\Z) */
  961. case OP_EODN:
  962. if (eptr < md->end_subject - 1 ||
  963. (eptr == md->end_subject - 1 && *eptr != NEWLINE)) RRETURN(MATCH_NOMATCH);
  964. ecode++;
  965. break;
  966. /* Word boundary assertions */
  967. case OP_NOT_WORD_BOUNDARY:
  968. case OP_WORD_BOUNDARY:
  969. {
  970. /* Find out if the previous and current characters are "word" characters.
  971. It takes a bit more work in UTF-8 mode. Characters > 255 are assumed to
  972. be "non-word" characters. */
  973. #ifdef SUPPORT_UTF8
  974. if (utf8)
  975. {
  976. if (eptr == md->start_subject) prev_is_word = FALSE; else
  977. {
  978. const uschar *lastptr = eptr - 1;
  979. while((*lastptr & 0xc0) == 0x80) lastptr--;
  980. GETCHAR(c, lastptr);
  981. prev_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
  982. }
  983. if (eptr >= md->end_subject) cur_is_word = FALSE; else
  984. {
  985. GETCHAR(c, eptr);
  986. cur_is_word = c < 256 && (md->ctypes[c] & ctype_word) != 0;
  987. }
  988. }
  989. else
  990. #endif
  991. /* More streamlined when not in UTF-8 mode */
  992. {
  993. prev_is_word = (eptr != md->start_subject) &&
  994. ((md->ctypes[eptr[-1]] & ctype_word) != 0);
  995. cur_is_word = (eptr < md->end_subject) &&
  996. ((md->ctypes[*eptr] & ctype_word) != 0);
  997. }
  998. /* Now see if the situation is what we want */
  999. if ((*ecode++ == OP_WORD_BOUNDARY)?
  1000. cur_is_word == prev_is_word : cur_is_word != prev_is_word)
  1001. RRETURN(MATCH_NOMATCH);
  1002. }
  1003. break;
  1004. /* Match a single character type; inline for speed */
  1005. case OP_ANY:
  1006. if ((ims & PCRE_DOTALL) == 0 && eptr < md->end_subject && *eptr == NEWLINE)
  1007. RRETURN(MATCH_NOMATCH);
  1008. if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1009. #ifdef SUPPORT_UTF8
  1010. if (utf8)
  1011. while (eptr < md->end_subject && (*eptr & 0xc0) == 0x80) eptr++;
  1012. #endif
  1013. ecode++;
  1014. break;
  1015. /* Match a single byte, even in UTF-8 mode. This opcode really does match
  1016. any byte, even newline, independent of the setting of PCRE_DOTALL. */
  1017. case OP_ANYBYTE:
  1018. if (eptr++ >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1019. ecode++;
  1020. break;
  1021. case OP_NOT_DIGIT:
  1022. if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1023. GETCHARINCTEST(c, eptr);
  1024. if (
  1025. #ifdef SUPPORT_UTF8
  1026. c < 256 &&
  1027. #endif
  1028. (md->ctypes[c] & ctype_digit) != 0
  1029. )
  1030. RRETURN(MATCH_NOMATCH);
  1031. ecode++;
  1032. break;
  1033. case OP_DIGIT:
  1034. if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1035. GETCHARINCTEST(c, eptr);
  1036. if (
  1037. #ifdef SUPPORT_UTF8
  1038. c >= 256 ||
  1039. #endif
  1040. (md->ctypes[c] & ctype_digit) == 0
  1041. )
  1042. RRETURN(MATCH_NOMATCH);
  1043. ecode++;
  1044. break;
  1045. case OP_NOT_WHITESPACE:
  1046. if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1047. GETCHARINCTEST(c, eptr);
  1048. if (
  1049. #ifdef SUPPORT_UTF8
  1050. c < 256 &&
  1051. #endif
  1052. (md->ctypes[c] & ctype_space) != 0
  1053. )
  1054. RRETURN(MATCH_NOMATCH);
  1055. ecode++;
  1056. break;
  1057. case OP_WHITESPACE:
  1058. if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1059. GETCHARINCTEST(c, eptr);
  1060. if (
  1061. #ifdef SUPPORT_UTF8
  1062. c >= 256 ||
  1063. #endif
  1064. (md->ctypes[c] & ctype_space) == 0
  1065. )
  1066. RRETURN(MATCH_NOMATCH);
  1067. ecode++;
  1068. break;
  1069. case OP_NOT_WORDCHAR:
  1070. if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1071. GETCHARINCTEST(c, eptr);
  1072. if (
  1073. #ifdef SUPPORT_UTF8
  1074. c < 256 &&
  1075. #endif
  1076. (md->ctypes[c] & ctype_word) != 0
  1077. )
  1078. RRETURN(MATCH_NOMATCH);
  1079. ecode++;
  1080. break;
  1081. case OP_WORDCHAR:
  1082. if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1083. GETCHARINCTEST(c, eptr);
  1084. if (
  1085. #ifdef SUPPORT_UTF8
  1086. c >= 256 ||
  1087. #endif
  1088. (md->ctypes[c] & ctype_word) == 0
  1089. )
  1090. RRETURN(MATCH_NOMATCH);
  1091. ecode++;
  1092. break;
  1093. #ifdef SUPPORT_UCP
  1094. /* Check the next character by Unicode property. We will get here only
  1095. if the support is in the binary; otherwise a compile-time error occurs. */
  1096. case OP_PROP:
  1097. case OP_NOTPROP:
  1098. if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1099. GETCHARINCTEST(c, eptr);
  1100. {
  1101. int chartype, rqdtype;
  1102. int othercase;
  1103. int category = ucp_findchar(c, &chartype, &othercase);
  1104. rqdtype = *(++ecode);
  1105. ecode++;
  1106. if (rqdtype >= 128)
  1107. {
  1108. if ((rqdtype - 128 != category) == (op == OP_PROP))
  1109. RRETURN(MATCH_NOMATCH);
  1110. }
  1111. else
  1112. {
  1113. if ((rqdtype != chartype) == (op == OP_PROP))
  1114. RRETURN(MATCH_NOMATCH);
  1115. }
  1116. }
  1117. break;
  1118. /* Match an extended Unicode sequence. We will get here only if the support
  1119. is in the binary; otherwise a compile-time error occurs. */
  1120. case OP_EXTUNI:
  1121. if (eptr >= md->end_subject) RRETURN(MATCH_NOMATCH);
  1122. GETCHARINCTEST(c, eptr);
  1123. {
  1124. int chartype;
  1125. int othercase;
  1126. int category = ucp_findchar(c, &chartype, &othercase);
  1127. if (category == ucp_M) RRETURN(MATCH_NOMATCH);
  1128. while (eptr < md->end_subject)
  1129. {
  1130. int len = 1;
  1131. if (!utf8) c = *eptr; else
  1132. {
  1133. GETCHARLEN(c, eptr, len);
  1134. }
  1135. category = ucp_findchar(c, &chartype, &othercase);
  1136. if (category != ucp_M) break;
  1137. eptr += len;
  1138. }
  1139. }
  1140. ecode++;
  1141. break;
  1142. #endif
  1143. /* Match a back reference, possibly repeatedly. Look past the end of the
  1144. item to see if there is repeat information following. The code is similar
  1145. to that for character classes, but repeated for efficiency. Then obey
  1146. similar code to character type repeats - written out again for speed.
  1147. However, if the referenced string is the empty string, always treat
  1148. it as matched, any number of times (otherwise there could be infinite
  1149. loops). */
  1150. case OP_REF:
  1151. {
  1152. offset = GET2(ecode, 1) << 1; /* Doubled ref number */
  1153. ecode += 3; /* Advance past item */
  1154. /* If the reference is unset, set the length to be longer than the amount
  1155. of subject left; this ensures that every attempt at a match fails. We
  1156. can't just fail here, because of the possibility of quantifiers with zero
  1157. minima. */
  1158. length = (offset >= offset_top || md->offset_vector[offset] < 0)?
  1159. md->end_subject - eptr + 1 :
  1160. md->offset_vector[offset+1] - md->offset_vector[offset];
  1161. /* Set up for repetition, or handle the non-repeated case */
  1162. switch (*ecode)
  1163. {
  1164. case OP_CRSTAR:
  1165. case OP_CRMINSTAR:
  1166. case OP_CRPLUS:
  1167. case OP_CRMINPLUS:
  1168. case OP_CRQUERY:
  1169. case OP_CRMINQUERY:
  1170. c = *ecode++ - OP_CRSTAR;
  1171. minimize = (c & 1) != 0;
  1172. min = rep_min[c]; /* Pick up values from tables; */
  1173. max = rep_max[c]; /* zero for max => infinity */
  1174. if (max == 0) max = INT_MAX;
  1175. break;
  1176. case OP_CRRANGE:
  1177. case OP_CRMINRANGE:
  1178. minimize = (*ecode == OP_CRMINRANGE);
  1179. min = GET2(ecode, 1);
  1180. max = GET2(ecode, 3);
  1181. if (max == 0) max = INT_MAX;
  1182. ecode += 5;
  1183. break;
  1184. default: /* No repeat follows */
  1185. if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);
  1186. eptr += length;
  1187. continue; /* With the main loop */
  1188. }
  1189. /* If the length of the reference is zero, just continue with the
  1190. main loop. */
  1191. if (length == 0) continue;
  1192. /* First, ensure the minimum number of matches are present. We get back
  1193. the length of the reference string explicitly rather than passing the
  1194. address of eptr, so that eptr can be a register variable. */
  1195. for (i = 1; i <= min; i++)
  1196. {
  1197. if (!match_ref(offset, eptr, length, md, ims)) RRETURN(MATCH_NOMATCH);
  1198. eptr += length;
  1199. }
  1200. /* If min = max, continue at the same level without recursion.
  1201. They are not both allowed to be zero. */
  1202. if (min == max) continue;
  1203. /* If minimizing, keep trying and advancing the pointer */
  1204. if (minimize)
  1205. {
  1206. for (fi = min;; fi++)
  1207. {
  1208. RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);
  1209. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1210. if (fi >= max || !match_ref(offset, eptr, length, md, ims))
  1211. RRETURN(MATCH_NOMATCH);
  1212. eptr += length;
  1213. }
  1214. /* Control never gets here */
  1215. }
  1216. /* If maximizing, find the longest string and work backwards */
  1217. else
  1218. {
  1219. pp = eptr;
  1220. for (i = min; i < max; i++)
  1221. {
  1222. if (!match_ref(offset, eptr, length, md, ims)) break;
  1223. eptr += length;
  1224. }
  1225. while (eptr >= pp)
  1226. {
  1227. RMATCH(rrc, eptr, ecode, offset_top, md, ims, eptrb, 0);
  1228. if (rrc != MATCH_NOMATCH) RRETURN(rrc);
  1229. eptr -= length;
  1230. }
  1231. RRETURN(MATCH_NOMATCH);
  1232. }
  1233. }
  1234. /* Control never gets here */
  1235. /* Match a bit-mapped character class, possibly repeatedly. This op code is
  1236. used when all the characters in the class have values in the range 0-255,
  1237. and either the matching is caseful, or the characters are in the range
  1238. 0-127 when UTF-8 processing is enabled. The only difference between
  1239. OP_CLASS and OP_NCLASS occurs when a data character outside the range is
  1240. encountered.
  1241. First, look past the end of the item to see if there is repeat information
  1242. following. Then obey similar code to character type repeats - written out
  1243. again for speed. */
  1244. case OP_NCLASS:
  1245. case OP_CLASS:
  1246. {
  1247. data = ecode + 1; /* Save for matching */
  1248. ecode += 33; /* Advance past the item */
  1249. switch (*ecode)
  1250. {
  1251. case OP_CRSTAR:
  1252. case OP_CRMINSTAR:
  1253. case OP_CRPLUS:
  1254. case OP_CRMINPLUS:
  1255. case OP_CRQUERY:
  1256. case OP_CRMINQUERY:
  1257. c = *ecode++ - OP_CRSTAR;
  1258. minimize = (c & 1) != 0;
  1259. min = rep_min[c]; /* Pick up values from tables; */
  1260. max = rep_max[c]; /* zero for max => infinity */
  1261. if (max ==

Large files files are truncated, but you can click here to view the full file