PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/beta1/harbour/source/hbpcre/pcredfa.c

#
C | 1888 lines | 1257 code | 270 blank | 361 comment | 501 complexity | 4794f7e0596dc77bcf9691efbe6a8702 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-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 the external function pcre_dfa_exec(), which is an
  33. alternative matching function that uses a DFA algorithm. This is NOT Perl-
  34. compatible, but it has advantages in certain applications. */
  35. #include "pcreinal.h"
  36. /* For use to indent debugging output */
  37. #define SP " "
  38. /*************************************************
  39. * Code parameters and static tables *
  40. *************************************************/
  41. /* These are offsets that are used to turn the OP_TYPESTAR and friends opcodes
  42. into others, under special conditions. A gap of 10 between the blocks should be
  43. enough. */
  44. #define OP_PROP_EXTRA (EXTRACT_BASIC_MAX+1)
  45. #define OP_EXTUNI_EXTRA (EXTRACT_BASIC_MAX+11)
  46. /* This table identifies those opcodes that are followed immediately by a
  47. character that is to be tested in some way. This makes is possible to
  48. centralize the loading of these characters. In the case of Type * etc, the
  49. "character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a
  50. small value. */
  51. static uschar coptable[] = {
  52. 0, /* End */
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* \A, \G, \B, \b, \D, \d, \S, \s, \W, \w */
  54. 0, 0, /* Any, Anybyte */
  55. 0, 0, 0, /* NOTPROP, PROP, EXTUNI */
  56. 0, 0, 0, 0, 0, /* \Z, \z, Opt, ^, $ */
  57. 1, /* Char */
  58. 1, /* Charnc */
  59. 1, /* not */
  60. /* Positive single-char repeats */
  61. 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
  62. 3, 3, 3, /* upto, minupto, exact */
  63. /* Negative single-char repeats - only for chars < 256 */
  64. 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */
  65. 3, 3, 3, /* NOT upto, minupto, exact */
  66. /* Positive type repeats */
  67. 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */
  68. 3, 3, 3, /* Type upto, minupto, exact */
  69. /* Character class & ref repeats */
  70. 0, 0, 0, 0, 0, 0, /* *, *?, +, +?, ?, ?? */
  71. 0, 0, /* CRRANGE, CRMINRANGE */
  72. 0, /* CLASS */
  73. 0, /* NCLASS */
  74. 0, /* XCLASS - variable length */
  75. 0, /* REF */
  76. 0, /* RECURSE */
  77. 0, /* CALLOUT */
  78. 0, /* Alt */
  79. 0, /* Ket */
  80. 0, /* KetRmax */
  81. 0, /* KetRmin */
  82. 0, /* Assert */
  83. 0, /* Assert not */
  84. 0, /* Assert behind */
  85. 0, /* Assert behind not */
  86. 0, /* Reverse */
  87. 0, /* Once */
  88. 0, /* COND */
  89. 0, /* CREF */
  90. 0, 0, /* BRAZERO, BRAMINZERO */
  91. 0, /* BRANUMBER */
  92. 0 /* BRA */
  93. };
  94. /* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,
  95. and \w */
  96. static uschar toptable1[] = {
  97. 0, 0, 0, 0, 0,
  98. ctype_digit, ctype_digit,
  99. ctype_space, ctype_space,
  100. ctype_word, ctype_word,
  101. 0 /* OP_ANY */
  102. };
  103. static uschar toptable2[] = {
  104. 0, 0, 0, 0, 0,
  105. ctype_digit, 0,
  106. ctype_space, 0,
  107. ctype_word, 0,
  108. 1 /* OP_ANY */
  109. };
  110. /* Structure for holding data about a particular state, which is in effect the
  111. current data for an active path through the match tree. It must consist
  112. entirely of ints because the working vector we are passed, and which we put
  113. these structures in, is a vector of ints. */
  114. typedef struct stateblock {
  115. int offset; /* Offset to opcode */
  116. int count; /* Count for repeats */
  117. int ims; /* ims flag bits */
  118. int data; /* Some use extra data */
  119. } stateblock;
  120. #define INTS_PER_STATEBLOCK (sizeof(stateblock)/sizeof(int))
  121. #ifdef DEBUG
  122. /*************************************************
  123. * Print character string *
  124. *************************************************/
  125. /* Character string printing function for debugging.
  126. Arguments:
  127. p points to string
  128. length number of bytes
  129. f where to print
  130. Returns: nothing
  131. */
  132. static void
  133. pchars(unsigned char *p, int length, FILE *f)
  134. {
  135. int c;
  136. while (length-- > 0)
  137. {
  138. if (isprint(c = *(p++)))
  139. fprintf(f, "%c", c);
  140. else
  141. fprintf(f, "\\x%02x", c);
  142. }
  143. }
  144. #endif
  145. /*************************************************
  146. * Execute a Regular Expression - DFA engine *
  147. *************************************************/
  148. /* This internal function applies a compiled pattern to a subject string,
  149. starting at a given point, using a DFA engine. This function is called from the
  150. external one, possibly multiple times if the pattern is not anchored. The
  151. function calls itself recursively for some kinds of subpattern.
  152. Arguments:
  153. md the match_data block with fixed information
  154. this_start_code the opening bracket of this subexpression's code
  155. current_subject where we currently are in the subject string
  156. start_offset start offset in the subject string
  157. offsets vector to contain the matching string offsets
  158. offsetcount size of same
  159. workspace vector of workspace
  160. wscount size of same
  161. ims the current ims flags
  162. rlevel function call recursion level
  163. recursing regex recursive call level
  164. Returns: > 0 =>
  165. = 0 =>
  166. -1 => failed to match
  167. < -1 => some kind of unexpected problem
  168. The following macros are used for adding states to the two state vectors (one
  169. for the current character, one for the following character). */
  170. #define ADD_ACTIVE(x,y) \
  171. if (active_count++ < wscount) \
  172. { \
  173. next_active_state->offset = (x); \
  174. next_active_state->count = (y); \
  175. next_active_state->ims = ims; \
  176. next_active_state++; \
  177. DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
  178. } \
  179. else return PCRE_ERROR_DFA_WSSIZE
  180. #define ADD_ACTIVE_DATA(x,y,z) \
  181. if (active_count++ < wscount) \
  182. { \
  183. next_active_state->offset = (x); \
  184. next_active_state->count = (y); \
  185. next_active_state->ims = ims; \
  186. next_active_state->data = (z); \
  187. next_active_state++; \
  188. DPRINTF(("%.*sADD_ACTIVE_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \
  189. } \
  190. else return PCRE_ERROR_DFA_WSSIZE
  191. #define ADD_NEW(x,y) \
  192. if (new_count++ < wscount) \
  193. { \
  194. next_new_state->offset = (x); \
  195. next_new_state->count = (y); \
  196. next_new_state->ims = ims; \
  197. next_new_state++; \
  198. DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
  199. } \
  200. else return PCRE_ERROR_DFA_WSSIZE
  201. #define ADD_NEW_DATA(x,y,z) \
  202. if (new_count++ < wscount) \
  203. { \
  204. next_new_state->offset = (x); \
  205. next_new_state->count = (y); \
  206. next_new_state->ims = ims; \
  207. next_new_state->data = (z); \
  208. next_new_state++; \
  209. DPRINTF(("%.*sADD_NEW_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \
  210. } \
  211. else return PCRE_ERROR_DFA_WSSIZE
  212. /* And now, here is the code */
  213. static int
  214. internal_dfa_exec(
  215. dfa_match_data *md,
  216. const uschar *this_start_code,
  217. const uschar *current_subject,
  218. int start_offset,
  219. int *offsets,
  220. int offsetcount,
  221. int *workspace,
  222. int wscount,
  223. int ims,
  224. int rlevel,
  225. int recursing)
  226. {
  227. stateblock *active_states, *new_states, *temp_states;
  228. stateblock *next_active_state, *next_new_state;
  229. const uschar *ctypes, *lcc, *fcc;
  230. const uschar *ptr;
  231. const uschar *end_code;
  232. int active_count, new_count, match_count;
  233. /* Some fields in the md block are frequently referenced, so we load them into
  234. independent variables in the hope that this will perform better. */
  235. const uschar *start_subject = md->start_subject;
  236. const uschar *end_subject = md->end_subject;
  237. const uschar *start_code = md->start_code;
  238. BOOL utf8 = (md->poptions & PCRE_UTF8) != 0;
  239. rlevel++;
  240. offsetcount &= (-2);
  241. wscount -= 2;
  242. wscount = (wscount - (wscount % (INTS_PER_STATEBLOCK * 2))) /
  243. (2 * INTS_PER_STATEBLOCK);
  244. DPRINTF(("\n%.*s---------------------\n"
  245. "%.*sCall to internal_dfa_exec f=%d r=%d\n",
  246. rlevel*2-2, SP, rlevel*2-2, SP, rlevel, recursing));
  247. ctypes = md->tables + ctypes_offset;
  248. lcc = md->tables + lcc_offset;
  249. fcc = md->tables + fcc_offset;
  250. match_count = PCRE_ERROR_NOMATCH; /* A negative number */
  251. active_states = (stateblock *)(workspace + 2);
  252. next_new_state = new_states = active_states + wscount;
  253. new_count = 0;
  254. /* The first thing in any (sub) pattern is a bracket of some sort. Push all
  255. the alternative states onto the list, and find out where the end is. This
  256. makes is possible to use this function recursively, when we want to stop at a
  257. matching internal ket rather than at the end.
  258. If the first opcode in the first alternative is OP_REVERSE, we are dealing with
  259. a backward assertion. In that case, we have to find out the maximum amount to
  260. move back, and set up each alternative appropriately. */
  261. if (this_start_code[1+LINK_SIZE] == OP_REVERSE)
  262. {
  263. int max_back = 0;
  264. int gone_back;
  265. end_code = this_start_code;
  266. do
  267. {
  268. int back = GET(end_code, 2+LINK_SIZE);
  269. if (back > max_back) max_back = back;
  270. end_code += GET(end_code, 1);
  271. }
  272. while (*end_code == OP_ALT);
  273. /* If we can't go back the amount required for the longest lookbehind
  274. pattern, go back as far as we can; some alternatives may still be viable. */
  275. #ifdef SUPPORT_UTF8
  276. /* In character mode we have to step back character by character */
  277. if (utf8)
  278. {
  279. for (gone_back = 0; gone_back < max_back; gone_back++)
  280. {
  281. if (current_subject <= start_subject) break;
  282. current_subject--;
  283. while (current_subject > start_subject &&
  284. (*current_subject & 0xc0) == 0x80)
  285. current_subject--;
  286. }
  287. }
  288. else
  289. #endif
  290. /* In byte-mode we can do this quickly. */
  291. {
  292. gone_back = (current_subject - max_back < start_subject)?
  293. current_subject - start_subject : max_back;
  294. current_subject -= gone_back;
  295. }
  296. /* Now we can process the individual branches. */
  297. end_code = this_start_code;
  298. do
  299. {
  300. int back = GET(end_code, 2+LINK_SIZE);
  301. if (back <= gone_back)
  302. {
  303. int bstate = end_code - start_code + 2 + 2*LINK_SIZE;
  304. ADD_NEW_DATA(-bstate, 0, gone_back - back);
  305. }
  306. end_code += GET(end_code, 1);
  307. }
  308. while (*end_code == OP_ALT);
  309. }
  310. /* This is the code for a "normal" subpattern (not a backward assertion). The
  311. start of a whole pattern is always one of these. If we are at the top level,
  312. we may be asked to restart matching from the same point that we reached for a
  313. previous partial match. We still have to scan through the top-level branches to
  314. find the end state. */
  315. else
  316. {
  317. end_code = this_start_code;
  318. /* Restarting */
  319. if (rlevel == 1 && (md->moptions & PCRE_DFA_RESTART) != 0)
  320. {
  321. do { end_code += GET(end_code, 1); } while (*end_code == OP_ALT);
  322. new_count = workspace[1];
  323. if (!workspace[0])
  324. memcpy(new_states, active_states, new_count * sizeof(stateblock));
  325. }
  326. /* Not restarting */
  327. else
  328. {
  329. do
  330. {
  331. ADD_NEW(end_code - start_code + 1 + LINK_SIZE, 0);
  332. end_code += GET(end_code, 1);
  333. }
  334. while (*end_code == OP_ALT);
  335. }
  336. }
  337. workspace[0] = 0; /* Bit indicating which vector is current */
  338. DPRINTF(("%.*sEnd state = %d\n", rlevel*2-2, SP, end_code - start_code));
  339. /* Loop for scanning the subject */
  340. ptr = current_subject;
  341. for (;;)
  342. {
  343. int i, j;
  344. int c, d, clen, dlen;
  345. /* Make the new state list into the active state list and empty the
  346. new state list. */
  347. temp_states = active_states;
  348. active_states = new_states;
  349. new_states = temp_states;
  350. active_count = new_count;
  351. new_count = 0;
  352. workspace[0] ^= 1; /* Remember for the restarting feature */
  353. workspace[1] = active_count;
  354. #ifdef DEBUG
  355. printf("%.*sNext character: rest of subject = \"", rlevel*2-2, SP);
  356. pchars((uschar *)ptr, strlen((char *)ptr), stdout);
  357. printf("\"\n");
  358. printf("%.*sActive states: ", rlevel*2-2, SP);
  359. for (i = 0; i < active_count; i++)
  360. printf("%d/%d ", active_states[i].offset, active_states[i].count);
  361. printf("\n");
  362. #endif
  363. /* Set the pointers for adding new states */
  364. next_active_state = active_states + active_count;
  365. next_new_state = new_states;
  366. /* Load the current character from the subject outside the loop, as many
  367. different states may want to look at it, and we assume that at least one
  368. will. */
  369. if (ptr < end_subject)
  370. {
  371. clen = 1;
  372. #ifdef SUPPORT_UTF8
  373. if (utf8) { GETCHARLEN(c, ptr, clen); } else
  374. #endif /* SUPPORT_UTF8 */
  375. c = *ptr;
  376. }
  377. else
  378. {
  379. clen = 0; /* At end subject */
  380. c = -1;
  381. }
  382. /* Scan up the active states and act on each one. The result of an action
  383. may be to add more states to the currently active list (e.g. on hitting a
  384. parenthesis) or it may be to put states on the new list, for considering
  385. when we move the character pointer on. */
  386. for (i = 0; i < active_count; i++)
  387. {
  388. stateblock *current_state = active_states + i;
  389. const uschar *code;
  390. int state_offset = current_state->offset;
  391. int count, codevalue;
  392. int chartype, othercase;
  393. #ifdef DEBUG
  394. printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);
  395. if (c < 0) printf("-1\n");
  396. else if (c > 32 && c < 127) printf("'%c'\n", c);
  397. else printf("0x%02x\n", c);
  398. #endif
  399. /* This variable is referred to implicity in the ADD_xxx macros. */
  400. ims = current_state->ims;
  401. /* A negative offset is a special case meaning "hold off going to this
  402. (negated) state until the number of characters in the data field have
  403. been skipped". */
  404. if (state_offset < 0)
  405. {
  406. if (current_state->data > 0)
  407. {
  408. DPRINTF(("%.*sSkipping this character\n", rlevel*2-2, SP));
  409. ADD_NEW_DATA(state_offset, current_state->count,
  410. current_state->data - 1);
  411. continue;
  412. }
  413. else
  414. {
  415. current_state->offset = state_offset = -state_offset;
  416. }
  417. }
  418. /* Check for a duplicate state with the same count, and skip if found. */
  419. for (j = 0; j < i; j++)
  420. {
  421. if (active_states[j].offset == state_offset &&
  422. active_states[j].count == current_state->count)
  423. {
  424. DPRINTF(("%.*sDuplicate state: skipped\n", rlevel*2-2, SP));
  425. goto NEXT_ACTIVE_STATE;
  426. }
  427. }
  428. /* The state offset is the offset to the opcode */
  429. code = start_code + state_offset;
  430. codevalue = *code;
  431. if (codevalue >= OP_BRA) codevalue = OP_BRA; /* All brackets are equal */
  432. /* If this opcode is followed by an inline character, load it. It is
  433. tempting to test for the presence of a subject character here, but that
  434. is wrong, because sometimes zero repetitions of the subject are
  435. permitted.
  436. We also use this mechanism for opcodes such as OP_TYPEPLUS that take an
  437. argument that is not a data character - but is always one byte long.
  438. Unfortunately, we have to take special action to deal with \P, \p, and
  439. \X in this case. To keep the other cases fast, convert these ones to new
  440. opcodes. */
  441. if (coptable[codevalue] > 0)
  442. {
  443. dlen = 1;
  444. #ifdef SUPPORT_UTF8
  445. if (utf8) { GETCHARLEN(d, (code + coptable[codevalue]), dlen); } else
  446. #endif /* SUPPORT_UTF8 */
  447. d = code[coptable[codevalue]];
  448. if (codevalue >= OP_TYPESTAR)
  449. {
  450. if (d == OP_ANYBYTE) return PCRE_ERROR_DFA_UITEM;
  451. if (d >= OP_NOTPROP)
  452. codevalue += (d == OP_EXTUNI)? OP_EXTUNI_EXTRA : OP_PROP_EXTRA;
  453. }
  454. }
  455. else
  456. {
  457. dlen = 0; /* Not strictly necessary, but compilers moan */
  458. d = -1; /* if these variables are not set. */
  459. }
  460. /* Now process the individual opcodes */
  461. switch (codevalue)
  462. {
  463. /* ========================================================================== */
  464. /* Reached a closing bracket. If not at the end of the pattern, carry
  465. on with the next opcode. Otherwise, unless we have an empty string and
  466. PCRE_NOTEMPTY is set, save the match data, shifting up all previous
  467. matches so we always have the longest first. */
  468. case OP_KET:
  469. case OP_KETRMIN:
  470. case OP_KETRMAX:
  471. if (code != end_code)
  472. {
  473. ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);
  474. if (codevalue != OP_KET)
  475. {
  476. ADD_ACTIVE(state_offset - GET(code, 1), 0);
  477. }
  478. }
  479. else if (ptr > current_subject || (md->moptions & PCRE_NOTEMPTY) == 0)
  480. {
  481. if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;
  482. else if (match_count > 0 && ++match_count * 2 >= offsetcount)
  483. match_count = 0;
  484. count = ((match_count == 0)? offsetcount : match_count * 2) - 2;
  485. if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));
  486. if (offsetcount >= 2)
  487. {
  488. offsets[0] = current_subject - start_subject;
  489. offsets[1] = ptr - start_subject;
  490. DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,
  491. offsets[1] - offsets[0], current_subject));
  492. }
  493. if ((md->moptions & PCRE_DFA_SHORTEST) != 0)
  494. {
  495. DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
  496. "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel,
  497. match_count, rlevel*2-2, SP));
  498. return match_count;
  499. }
  500. }
  501. break;
  502. /* ========================================================================== */
  503. /* These opcodes add to the current list of states without looking
  504. at the current character. */
  505. /*-----------------------------------------------------------------*/
  506. case OP_ALT:
  507. do { code += GET(code, 1); } while (*code == OP_ALT);
  508. ADD_ACTIVE(code - start_code, 0);
  509. break;
  510. /*-----------------------------------------------------------------*/
  511. case OP_BRA:
  512. do
  513. {
  514. ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);
  515. code += GET(code, 1);
  516. }
  517. while (*code == OP_ALT);
  518. break;
  519. /*-----------------------------------------------------------------*/
  520. case OP_BRAZERO:
  521. case OP_BRAMINZERO:
  522. ADD_ACTIVE(state_offset + 1, 0);
  523. code += 1 + GET(code, 2);
  524. while (*code == OP_ALT) code += GET(code, 1);
  525. ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);
  526. break;
  527. /*-----------------------------------------------------------------*/
  528. case OP_BRANUMBER:
  529. ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);
  530. break;
  531. /*-----------------------------------------------------------------*/
  532. case OP_CIRC:
  533. if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) ||
  534. ((ims & PCRE_MULTILINE) != 0 && ptr[-1] == NEWLINE))
  535. { ADD_ACTIVE(state_offset + 1, 0); }
  536. break;
  537. /*-----------------------------------------------------------------*/
  538. case OP_EOD:
  539. if (ptr >= end_subject) { ADD_ACTIVE(state_offset + 1, 0); }
  540. break;
  541. /*-----------------------------------------------------------------*/
  542. case OP_OPT:
  543. ims = code[1];
  544. ADD_ACTIVE(state_offset + 2, 0);
  545. break;
  546. /*-----------------------------------------------------------------*/
  547. case OP_SOD:
  548. if (ptr == start_subject) { ADD_ACTIVE(state_offset + 1, 0); }
  549. break;
  550. /*-----------------------------------------------------------------*/
  551. case OP_SOM:
  552. if (ptr == start_subject + start_offset) { ADD_ACTIVE(state_offset + 1, 0); }
  553. break;
  554. /* ========================================================================== */
  555. /* These opcodes inspect the next subject character, and sometimes
  556. the previous one as well, but do not have an argument. The variable
  557. clen contains the length of the current character and is zero if we are
  558. at the end of the subject. */
  559. /*-----------------------------------------------------------------*/
  560. case OP_ANY:
  561. if (clen > 0 && (c != NEWLINE || (ims & PCRE_DOTALL) != 0))
  562. { ADD_NEW(state_offset + 1, 0); }
  563. break;
  564. /*-----------------------------------------------------------------*/
  565. case OP_EODN:
  566. if (clen == 0 || (c == NEWLINE && ptr + 1 == end_subject))
  567. { ADD_ACTIVE(state_offset + 1, 0); }
  568. break;
  569. /*-----------------------------------------------------------------*/
  570. case OP_DOLL:
  571. if ((md->moptions & PCRE_NOTEOL) == 0)
  572. {
  573. if (clen == 0 || (c == NEWLINE && (ptr + 1 == end_subject ||
  574. (ims & PCRE_MULTILINE) != 0)))
  575. { ADD_ACTIVE(state_offset + 1, 0); }
  576. }
  577. else if (c == NEWLINE && (ims & PCRE_MULTILINE) != 0)
  578. { ADD_ACTIVE(state_offset + 1, 0); }
  579. break;
  580. /*-----------------------------------------------------------------*/
  581. case OP_DIGIT:
  582. case OP_WHITESPACE:
  583. case OP_WORDCHAR:
  584. if (clen > 0 && c < 256 &&
  585. ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0)
  586. { ADD_NEW(state_offset + 1, 0); }
  587. break;
  588. /*-----------------------------------------------------------------*/
  589. case OP_NOT_DIGIT:
  590. case OP_NOT_WHITESPACE:
  591. case OP_NOT_WORDCHAR:
  592. if (clen > 0 && (c >= 256 ||
  593. ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0))
  594. { ADD_NEW(state_offset + 1, 0); }
  595. break;
  596. /*-----------------------------------------------------------------*/
  597. case OP_WORD_BOUNDARY:
  598. case OP_NOT_WORD_BOUNDARY:
  599. {
  600. int left_word, right_word;
  601. if (ptr > start_subject)
  602. {
  603. const uschar *temp = ptr - 1;
  604. #ifdef SUPPORT_UTF8
  605. if (utf8) BACKCHAR(temp);
  606. #endif
  607. GETCHARTEST(d, temp);
  608. left_word = d < 256 && (ctypes[d] & ctype_word) != 0;
  609. }
  610. else left_word = 0;
  611. if (clen > 0) right_word = c < 256 && (ctypes[c] & ctype_word) != 0;
  612. else right_word = 0;
  613. if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))
  614. { ADD_ACTIVE(state_offset + 1, 0); }
  615. }
  616. break;
  617. #ifdef SUPPORT_UCP
  618. /*-----------------------------------------------------------------*/
  619. /* Check the next character by Unicode property. We will get here only
  620. if the support is in the binary; otherwise a compile-time error occurs.
  621. */
  622. case OP_PROP:
  623. case OP_NOTPROP:
  624. if (clen > 0)
  625. {
  626. int rqdtype, category;
  627. category = ucp_findchar(c, &chartype, &othercase);
  628. rqdtype = code[1];
  629. if (rqdtype >= 128)
  630. {
  631. if ((rqdtype - 128 == category) == (codevalue == OP_PROP))
  632. { ADD_NEW(state_offset + 2, 0); }
  633. }
  634. else
  635. {
  636. if ((rqdtype == chartype) == (codevalue == OP_PROP))
  637. { ADD_NEW(state_offset + 2, 0); }
  638. }
  639. }
  640. break;
  641. #endif
  642. /* ========================================================================== */
  643. /* These opcodes likewise inspect the subject character, but have an
  644. argument that is not a data character. It is one of these opcodes:
  645. OP_ANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE, OP_WORDCHAR,
  646. OP_NOT_WORDCHAR. The value is loaded into d. */
  647. case OP_TYPEPLUS:
  648. case OP_TYPEMINPLUS:
  649. count = current_state->count; /* Already matched */
  650. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  651. if (clen > 0)
  652. {
  653. if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  654. (c < 256 &&
  655. (d != OP_ANY || c != '\n' || (ims & PCRE_DOTALL) != 0) &&
  656. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  657. {
  658. count++;
  659. ADD_NEW(state_offset, count);
  660. }
  661. }
  662. break;
  663. /*-----------------------------------------------------------------*/
  664. case OP_TYPEQUERY:
  665. case OP_TYPEMINQUERY:
  666. ADD_ACTIVE(state_offset + 2, 0);
  667. if (clen > 0)
  668. {
  669. if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  670. (c < 256 &&
  671. (d != OP_ANY || c != '\n' || (ims & PCRE_DOTALL) != 0) &&
  672. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  673. {
  674. ADD_NEW(state_offset + 2, 0);
  675. }
  676. }
  677. break;
  678. /*-----------------------------------------------------------------*/
  679. case OP_TYPESTAR:
  680. case OP_TYPEMINSTAR:
  681. ADD_ACTIVE(state_offset + 2, 0);
  682. if (clen > 0)
  683. {
  684. if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  685. (c < 256 &&
  686. (d != OP_ANY || c != '\n' || (ims & PCRE_DOTALL) != 0) &&
  687. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  688. {
  689. ADD_NEW(state_offset, 0);
  690. }
  691. }
  692. break;
  693. /*-----------------------------------------------------------------*/
  694. case OP_TYPEEXACT:
  695. case OP_TYPEUPTO:
  696. case OP_TYPEMINUPTO:
  697. if (codevalue != OP_TYPEEXACT)
  698. { ADD_ACTIVE(state_offset + 4, 0); }
  699. count = current_state->count; /* Number already matched */
  700. if (clen > 0)
  701. {
  702. if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  703. (c < 256 &&
  704. (d != OP_ANY || c != '\n' || (ims & PCRE_DOTALL) != 0) &&
  705. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  706. {
  707. if (++count >= GET2(code, 1))
  708. { ADD_NEW(state_offset + 4, 0); }
  709. else
  710. { ADD_NEW(state_offset, count); }
  711. }
  712. }
  713. break;
  714. /* ========================================================================== */
  715. /* These are virtual opcodes that are used when something like
  716. OP_TYPEPLUS has OP_PROP, OP_NOTPROP, or OP_EXTUNI as its argument. It
  717. keeps the code above fast for the other cases. The argument is in the
  718. d variable. */
  719. case OP_PROP_EXTRA + OP_TYPEPLUS:
  720. case OP_PROP_EXTRA + OP_TYPEMINPLUS:
  721. count = current_state->count; /* Already matched */
  722. if (count > 0) { ADD_ACTIVE(state_offset + 3, 0); }
  723. if (clen > 0)
  724. {
  725. int category = ucp_findchar(c, &chartype, &othercase);
  726. int rqdtype = code[2];
  727. if ((d == OP_PROP) ==
  728. (rqdtype == ((rqdtype >= 128)? (category + 128) : chartype)))
  729. { count++; ADD_NEW(state_offset, count); }
  730. }
  731. break;
  732. /*-----------------------------------------------------------------*/
  733. case OP_EXTUNI_EXTRA + OP_TYPEPLUS:
  734. case OP_EXTUNI_EXTRA + OP_TYPEMINPLUS:
  735. count = current_state->count; /* Already matched */
  736. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  737. if (clen > 0 && ucp_findchar(c, &chartype, &othercase) != ucp_M)
  738. {
  739. const uschar *nptr = ptr + clen;
  740. int ncount = 0;
  741. while (nptr < end_subject)
  742. {
  743. int nd;
  744. int ndlen = 1;
  745. GETCHARLEN(nd, nptr, ndlen);
  746. if (ucp_findchar(nd, &chartype, &othercase) != ucp_M) break;
  747. ncount++;
  748. nptr += ndlen;
  749. }
  750. count++;
  751. ADD_NEW_DATA(-state_offset, count, ncount);
  752. }
  753. break;
  754. /*-----------------------------------------------------------------*/
  755. case OP_PROP_EXTRA + OP_TYPEQUERY:
  756. case OP_PROP_EXTRA + OP_TYPEMINQUERY:
  757. count = 3;
  758. goto QS1;
  759. case OP_PROP_EXTRA + OP_TYPESTAR:
  760. case OP_PROP_EXTRA + OP_TYPEMINSTAR:
  761. count = 0;
  762. QS1:
  763. ADD_ACTIVE(state_offset + 3, 0);
  764. if (clen > 0)
  765. {
  766. int category = ucp_findchar(c, &chartype, &othercase);
  767. int rqdtype = code[2];
  768. if ((d == OP_PROP) ==
  769. (rqdtype == ((rqdtype >= 128)? (category + 128) : chartype)))
  770. { ADD_NEW(state_offset + count, 0); }
  771. }
  772. break;
  773. /*-----------------------------------------------------------------*/
  774. case OP_EXTUNI_EXTRA + OP_TYPEQUERY:
  775. case OP_EXTUNI_EXTRA + OP_TYPEMINQUERY:
  776. count = 2;
  777. goto QS2;
  778. case OP_EXTUNI_EXTRA + OP_TYPESTAR:
  779. case OP_EXTUNI_EXTRA + OP_TYPEMINSTAR:
  780. count = 0;
  781. QS2:
  782. ADD_ACTIVE(state_offset + 2, 0);
  783. if (clen > 0 && ucp_findchar(c, &chartype, &othercase) != ucp_M)
  784. {
  785. const uschar *nptr = ptr + clen;
  786. int ncount = 0;
  787. while (nptr < end_subject)
  788. {
  789. int nd;
  790. int ndlen = 1;
  791. GETCHARLEN(nd, nptr, ndlen);
  792. if (ucp_findchar(nd, &chartype, &othercase) != ucp_M) break;
  793. ncount++;
  794. nptr += ndlen;
  795. }
  796. ADD_NEW_DATA(-(state_offset + count), 0, ncount);
  797. }
  798. break;
  799. /*-----------------------------------------------------------------*/
  800. case OP_PROP_EXTRA + OP_TYPEEXACT:
  801. case OP_PROP_EXTRA + OP_TYPEUPTO:
  802. case OP_PROP_EXTRA + OP_TYPEMINUPTO:
  803. if (codevalue != OP_PROP_EXTRA + OP_TYPEEXACT)
  804. { ADD_ACTIVE(state_offset + 5, 0); }
  805. count = current_state->count; /* Number already matched */
  806. if (clen > 0)
  807. {
  808. int category = ucp_findchar(c, &chartype, &othercase);
  809. int rqdtype = code[4];
  810. if ((d == OP_PROP) ==
  811. (rqdtype == ((rqdtype >= 128)? (category + 128) : chartype)))
  812. {
  813. if (++count >= GET2(code, 1))
  814. { ADD_NEW(state_offset + 5, 0); }
  815. else
  816. { ADD_NEW(state_offset, count); }
  817. }
  818. }
  819. break;
  820. /*-----------------------------------------------------------------*/
  821. case OP_EXTUNI_EXTRA + OP_TYPEEXACT:
  822. case OP_EXTUNI_EXTRA + OP_TYPEUPTO:
  823. case OP_EXTUNI_EXTRA + OP_TYPEMINUPTO:
  824. if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT)
  825. { ADD_ACTIVE(state_offset + 4, 0); }
  826. count = current_state->count; /* Number already matched */
  827. if (clen > 0 && ucp_findchar(c, &chartype, &othercase) != ucp_M)
  828. {
  829. const uschar *nptr = ptr + clen;
  830. int ncount = 0;
  831. while (nptr < end_subject)
  832. {
  833. int nd;
  834. int ndlen = 1;
  835. GETCHARLEN(nd, nptr, ndlen);
  836. if (ucp_findchar(nd, &chartype, &othercase) != ucp_M) break;
  837. ncount++;
  838. nptr += ndlen;
  839. }
  840. if (++count >= GET2(code, 1))
  841. { ADD_NEW_DATA(-(state_offset + 4), 0, ncount); }
  842. else
  843. { ADD_NEW_DATA(-state_offset, count, ncount); }
  844. }
  845. break;
  846. /* ========================================================================== */
  847. /* These opcodes are followed by a character that is usually compared
  848. to the current subject character; it is loaded into d. We still get
  849. here even if there is no subject character, because in some cases zero
  850. repetitions are permitted. */
  851. /*-----------------------------------------------------------------*/
  852. case OP_CHAR:
  853. if (clen > 0 && c == d) { ADD_NEW(state_offset + dlen + 1, 0); }
  854. break;
  855. /*-----------------------------------------------------------------*/
  856. case OP_CHARNC:
  857. if (clen == 0) break;
  858. #ifdef SUPPORT_UTF8
  859. if (utf8)
  860. {
  861. if (c == d) { ADD_NEW(state_offset + dlen + 1, 0); } else
  862. {
  863. if (c < 128) othercase = fcc[c]; else
  864. /* If we have Unicode property support, we can use it to test the
  865. other case of the character, if there is one. The result of
  866. ucp_findchar() is < 0 if the char isn't found, and othercase is
  867. returned as zero if there isn't another case. */
  868. #ifdef SUPPORT_UCP
  869. if (ucp_findchar(c, &chartype, &othercase) < 0)
  870. #endif
  871. othercase = -1;
  872. if (d == othercase) { ADD_NEW(state_offset + dlen + 1, 0); }
  873. }
  874. }
  875. else
  876. #endif /* SUPPORT_UTF8 */
  877. /* Non-UTF-8 mode */
  878. {
  879. if (lcc[c] == lcc[d]) { ADD_NEW(state_offset + 2, 0); }
  880. }
  881. break;
  882. #ifdef SUPPORT_UCP
  883. /*-----------------------------------------------------------------*/
  884. /* This is a tricky one because it can match more than one character.
  885. Find out how many characters to skip, and then set up a negative state
  886. to wait for them to pass before continuing. */
  887. case OP_EXTUNI:
  888. if (clen > 0 && ucp_findchar(c, &chartype, &othercase) != ucp_M)
  889. {
  890. const uschar *nptr = ptr + clen;
  891. int ncount = 0;
  892. while (nptr < end_subject)
  893. {
  894. int nclen = 1;
  895. GETCHARLEN(c, nptr, nclen);
  896. if (ucp_findchar(c, &chartype, &othercase) != ucp_M) break;
  897. ncount++;
  898. nptr += nclen;
  899. }
  900. ADD_NEW_DATA(-(state_offset + 1), 0, ncount);
  901. }
  902. break;
  903. #endif
  904. /*-----------------------------------------------------------------*/
  905. /* Match a negated single character. This is only used for one-byte
  906. characters, that is, we know that d < 256. The character we are
  907. checking (c) can be multibyte. */
  908. case OP_NOT:
  909. if (clen > 0)
  910. {
  911. int otherd = ((ims & PCRE_CASELESS) != 0)? fcc[d] : d;
  912. if (c != d && c != otherd) { ADD_NEW(state_offset + dlen + 1, 0); }
  913. }
  914. break;
  915. /*-----------------------------------------------------------------*/
  916. case OP_PLUS:
  917. case OP_MINPLUS:
  918. case OP_NOTPLUS:
  919. case OP_NOTMINPLUS:
  920. count = current_state->count; /* Already matched */
  921. if (count > 0) { ADD_ACTIVE(state_offset + dlen + 1, 0); }
  922. if (clen > 0)
  923. {
  924. int otherd = -1;
  925. if ((ims & PCRE_CASELESS) != 0)
  926. {
  927. #ifdef SUPPORT_UTF8
  928. if (utf8 && c >= 128)
  929. {
  930. #ifdef SUPPORT_UCP
  931. if (ucp_findchar(d, &chartype, &otherd) < 0) otherd = -1;
  932. #endif /* SUPPORT_UCP */
  933. }
  934. else
  935. #endif /* SUPPORT_UTF8 */
  936. otherd = fcc[d];
  937. }
  938. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  939. { count++; ADD_NEW(state_offset, count); }
  940. }
  941. break;
  942. /*-----------------------------------------------------------------*/
  943. case OP_QUERY:
  944. case OP_MINQUERY:
  945. case OP_NOTQUERY:
  946. case OP_NOTMINQUERY:
  947. ADD_ACTIVE(state_offset + dlen + 1, 0);
  948. if (clen > 0)
  949. {
  950. int otherd = -1;
  951. if ((ims && PCRE_CASELESS) != 0)
  952. {
  953. #ifdef SUPPORT_UTF8
  954. if (utf8 && c >= 128)
  955. {
  956. #ifdef SUPPORT_UCP
  957. if (ucp_findchar(c, &chartype, &otherd) < 0) otherd = -1;
  958. #endif /* SUPPORT_UCP */
  959. }
  960. else
  961. #endif /* SUPPORT_UTF8 */
  962. otherd = fcc[d];
  963. }
  964. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  965. { ADD_NEW(state_offset + dlen + 1, 0); }
  966. }
  967. break;
  968. /*-----------------------------------------------------------------*/
  969. case OP_STAR:
  970. case OP_MINSTAR:
  971. case OP_NOTSTAR:
  972. case OP_NOTMINSTAR:
  973. ADD_ACTIVE(state_offset + dlen + 1, 0);
  974. if (clen > 0)
  975. {
  976. int otherd = -1;
  977. if ((ims && PCRE_CASELESS) != 0)
  978. {
  979. #ifdef SUPPORT_UTF8
  980. if (utf8 && c >= 128)
  981. {
  982. #ifdef SUPPORT_UCP
  983. if (ucp_findchar(c, &chartype, &otherd) < 0) otherd = -1;
  984. #endif /* SUPPORT_UCP */
  985. }
  986. else
  987. #endif /* SUPPORT_UTF8 */
  988. otherd = fcc[d];
  989. }
  990. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  991. { ADD_NEW(state_offset, 0); }
  992. }
  993. break;
  994. /*-----------------------------------------------------------------*/
  995. case OP_EXACT:
  996. case OP_UPTO:
  997. case OP_MINUPTO:
  998. case OP_NOTEXACT:
  999. case OP_NOTUPTO:
  1000. case OP_NOTMINUPTO:
  1001. if (codevalue != OP_EXACT && codevalue != OP_NOTEXACT)
  1002. { ADD_ACTIVE(state_offset + dlen + 3, 0); }
  1003. count = current_state->count; /* Number already matched */
  1004. if (clen > 0)
  1005. {
  1006. int otherd = -1;
  1007. if ((ims & PCRE_CASELESS) != 0)
  1008. {
  1009. #ifdef SUPPORT_UTF8
  1010. if (utf8 && c >= 128)
  1011. {
  1012. #ifdef SUPPORT_UCP
  1013. if (ucp_findchar(d, &chartype, &otherd) < 0) otherd = -1;
  1014. #endif /* SUPPORT_UCP */
  1015. }
  1016. else
  1017. #endif /* SUPPORT_UTF8 */
  1018. otherd = fcc[d];
  1019. }
  1020. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  1021. {
  1022. if (++count >= GET2(code, 1))
  1023. { ADD_NEW(state_offset + dlen + 3, 0); }
  1024. else
  1025. { ADD_NEW(state_offset, count); }
  1026. }
  1027. }
  1028. break;
  1029. /* ========================================================================== */
  1030. /* These are the class-handling opcodes */
  1031. case OP_CLASS:
  1032. case OP_NCLASS:
  1033. case OP_XCLASS:
  1034. {
  1035. BOOL isinclass = FALSE;
  1036. int next_state_offset;
  1037. const uschar *ecode;
  1038. /* For a simple class, there is always just a 32-byte table, and we
  1039. can set isinclass from it. */
  1040. if (codevalue != OP_XCLASS)
  1041. {
  1042. ecode = code + 33;
  1043. if (clen > 0)
  1044. {
  1045. isinclass = (c > 255)? (codevalue == OP_NCLASS) :
  1046. ((code[1 + c/8] & (1 << (c&7))) != 0);
  1047. }
  1048. }
  1049. /* An extended class may have a table or a list of single characters,
  1050. ranges, or both, and it may be positive or negative. There's a
  1051. function that sorts all this out. */
  1052. else
  1053. {
  1054. ecode = code + GET(code, 1);
  1055. if (clen > 0) isinclass = _pcre_xclass(c, code + 1 + LINK_SIZE);
  1056. }
  1057. /* At this point, isinclass is set for all kinds of class, and ecode
  1058. points to the byte after the end of the class. If there is a
  1059. quantifier, this is where it will be. */
  1060. next_state_offset = ecode - start_code;
  1061. switch (*ecode)
  1062. {
  1063. case OP_CRSTAR:
  1064. case OP_CRMINSTAR:
  1065. ADD_ACTIVE(next_state_offset + 1, 0);
  1066. if (isinclass) { ADD_NEW(state_offset, 0); }
  1067. break;
  1068. case OP_CRPLUS:
  1069. case OP_CRMINPLUS:
  1070. count = current_state->count; /* Already matched */
  1071. if (count > 0) { ADD_ACTIVE(next_state_offset + 1, 0); }
  1072. if (isinclass) { count++; ADD_NEW(state_offset, count); }
  1073. break;
  1074. case OP_CRQUERY:
  1075. case OP_CRMINQUERY:
  1076. ADD_ACTIVE(next_state_offset + 1, 0);
  1077. if (isinclass) { ADD_NEW(next_state_offset + 1, 0); }
  1078. break;
  1079. case OP_CRRANGE:
  1080. case OP_CRMINRANGE:
  1081. count = current_state->count; /* Already matched */
  1082. if (count >= GET2(ecode, 1))
  1083. { ADD_ACTIVE(next_state_offset + 5, 0); }
  1084. if (isinclass)
  1085. {
  1086. if (++count >= GET2(ecode, 3))
  1087. { ADD_NEW(next_state_offset + 5, 0); }
  1088. else
  1089. { ADD_NEW(state_offset, count); }
  1090. }
  1091. break;
  1092. default:
  1093. if (isinclass) { ADD_NEW(next_state_offset, 0); }
  1094. break;
  1095. }
  1096. }
  1097. break;
  1098. /* ========================================================================== */
  1099. /* These are the opcodes for fancy brackets of various kinds. We have
  1100. to use recursion in order to handle them. */
  1101. case OP_ASSERT:
  1102. case OP_ASSERT_NOT:
  1103. case OP_ASSERTBACK:
  1104. case OP_ASSERTBACK_NOT:
  1105. {
  1106. int rc;
  1107. int local_offsets[2];
  1108. int local_workspace[1000];
  1109. const uschar *endasscode = code + GET(code, 1);
  1110. while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
  1111. rc = internal_dfa_exec(
  1112. md, /* static match data */
  1113. code, /* this subexpression's code */
  1114. ptr, /* where we currently are */
  1115. ptr - start_subject, /* start offset */
  1116. local_offsets, /* offset vector */
  1117. sizeof(local_offsets)/sizeof(int), /* size of same */
  1118. local_workspace, /* workspace vector */
  1119. sizeof(local_workspace)/sizeof(int), /* size of same */
  1120. ims, /* the current ims flags */
  1121. rlevel, /* function recursion level */
  1122. recursing); /* pass on regex recursion */
  1123. if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))
  1124. { ADD_ACTIVE(endasscode + LINK_SIZE + 1 - start_code, 0); }
  1125. }
  1126. break;
  1127. /*-----------------------------------------------------------------*/
  1128. case OP_COND:
  1129. {
  1130. int local_offsets[1000];
  1131. int local_workspace[1000];
  1132. int condcode = code[LINK_SIZE+1];
  1133. /* The only supported version of OP_CREF is for the value 0xffff, which
  1134. means "test if in a recursion". */
  1135. if (condcode == OP_CREF)
  1136. {
  1137. int value = GET2(code, LINK_SIZE+2);
  1138. if (value != 0xffff) return PCRE_ERROR_DFA_UCOND;
  1139. if (recursing > 0) { ADD_ACTIVE(state_offset + LINK_SIZE + 4, 0); }
  1140. else { ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0); }
  1141. }
  1142. /* Otherwise, the condition is an assertion */
  1143. else
  1144. {
  1145. int rc;
  1146. const uschar *asscode = code + LINK_SIZE + 1;
  1147. const uschar *endasscode = asscode + GET(asscode, 1);
  1148. while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
  1149. rc = internal_dfa_exec(
  1150. md, /* fixed match data */
  1151. asscode, /* this subexpression's code */
  1152. ptr, /* where we currently are */
  1153. ptr - start_subject, /* start offset */
  1154. local_offsets, /* offset vector */
  1155. sizeof(local_offsets)/sizeof(int), /* size of same */
  1156. local_workspace, /* workspace vector */
  1157. sizeof(local_workspace)/sizeof(int), /* size of same */
  1158. ims, /* the current ims flags */
  1159. rlevel, /* function recursion level */
  1160. recursing); /* pass on regex recursion */
  1161. if ((rc >= 0) ==
  1162. (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))
  1163. { ADD_ACTIVE(endasscode + LINK_SIZE + 1 - start_code, 0); }
  1164. else
  1165. { ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0); }
  1166. }
  1167. }
  1168. break;
  1169. /*-----------------------------------------------------------------*/
  1170. case OP_RECURSE:
  1171. {
  1172. int local_offsets[1000];
  1173. int local_workspace[1000];
  1174. int rc;
  1175. DPRINTF(("%.*sStarting regex recursion %d\n", rlevel*2-2, SP,
  1176. recursing + 1));
  1177. rc = internal_dfa_exec(
  1178. md, /* fixed match data */
  1179. start_code + GET(code, 1), /* this subexpression's code */
  1180. ptr, /* where we currently are */
  1181. ptr - start_subject, /* start offset */
  1182. local_offsets, /* offset vector */
  1183. sizeof(local_offsets)/sizeof(int), /* size of same */
  1184. local_workspace, /* workspace vector */
  1185. sizeof(local_workspace)/sizeof(int), /* size of same */
  1186. ims, /* the current ims flags */
  1187. rlevel, /* function recursion level */
  1188. recursing + 1); /* regex recurse level */
  1189. DPRINTF(("%.*sReturn from regex recursion %d: rc=%d\n", rlevel*2-2, SP,
  1190. recursing + 1, rc));
  1191. /* Ran out of internal offsets */
  1192. if (rc == 0) return PCRE_ERROR_DFA_RECURSE;
  1193. /* For each successful matched substring, set up the next state with a
  1194. count of characters to skip before trying it. Note that the count is in
  1195. characters, not bytes. */
  1196. if (rc > 0)
  1197. {
  1198. for (rc = rc*2 - 2; rc >= 0; rc -= 2)
  1199. {
  1200. const uschar *p = start_subject + local_offsets[rc];
  1201. const uschar *pp = start_subject + local_offsets[rc+1];
  1202. int charcount = local_offsets[rc+1] - local_offsets[rc];
  1203. while (p < pp) if ((*p++ & 0xc0) == 0x80) charcount--;
  1204. if (charcount > 0)
  1205. {
  1206. ADD_NEW_DATA(-(state_offset + LINK_SIZE + 1), 0, (charcount - 1));
  1207. }
  1208. else
  1209. {
  1210. ADD_ACTIVE(state_offset + LINK_SIZE + 1, 0);
  1211. }
  1212. }
  1213. }
  1214. else if (rc != PCRE_ERROR_NOMATCH) return rc;
  1215. }
  1216. break;
  1217. /*-----------------------------------------------------------------*/
  1218. case OP_ONCE:
  1219. {
  1220. /* commented unused variable, Druzus */
  1221. /* const uschar *endcode; */
  1222. int local_offsets[2];
  1223. int local_workspace[1000];
  1224. int rc = internal_dfa_exec(
  1225. md, /* fixed match data */
  1226. code, /* this subexpression's code */
  1227. ptr, /* where we currently are */
  1228. ptr - start_subject, /* start offset */
  1229. local_offsets, /* offset vector */
  1230. sizeof(local_offsets)/sizeof(int), /* size of same */
  1231. local_workspace, /* workspace vector */
  1232. sizeof(local_workspace)/sizeof(int), /* size of same */
  1233. ims, /* the current ims flags */
  1234. rlevel, /* function recursion level */
  1235. recursing); /* pass on regex recursion */
  1236. if (rc >= 0)
  1237. {
  1238. const uschar *end_subpattern = code;
  1239. int charcount = local_offsets[1] - local_offsets[0];
  1240. int next_state_offset, repeat_state_offset;
  1241. /* commented unused variable, Druzus */
  1242. /* BOOL is_repeated; */
  1243. do { end_subpattern += GET(end_subpattern, 1); }
  1244. while (*end_subpattern == OP_ALT);
  1245. next_state_offset = end_subpattern - start_code + LINK_SIZE + 1;
  1246. /* If the end of this subpattern is KETRMAX or KETRMIN, we must
  1247. arrange for the repeat state also to be added to the relevant list.
  1248. Calculate the offset, or set -1 for no repeat. */
  1249. repeat_state_offset = (*end_subpattern == OP_KETRMAX ||
  1250. *end_subpattern == OP_KETRMIN)?
  1251. end_subpattern - start_code - GET(end_subpattern, 1) : -1;
  1252. /* If we have matched an empty string, add the next state at the
  1253. current character pointer. This is important so that the duplicate
  1254. checking kicks in, which is what breaks infinite loops that match an
  1255. empty string. */
  1256. if (charcount == 0)
  1257. {
  1258. ADD_ACTIVE(next_state_offset, 0);
  1259. }
  1260. /* Optimization: if there are no more active states, and there
  1261. are no new states yet set up, then skip over the subject string
  1262. right here, to save looping. Otherwise, set up the new state to swing
  1263. into action when the end of the substring is reached. */
  1264. else if (i + 1 >= active_count && new_count == 0)
  1265. {
  1266. ptr += charcount;
  1267. clen = 0;
  1268. ADD_NEW(next_state_offset, 0);
  1269. /* If we are adding a repeat state at the new character position,
  1270. we must fudge things so that it is the only current state.
  1271. Otherwise, it might be a duplicate of one we processed before, and
  1272. that would cause it to be skipped. */
  1273. if (repeat_state_offset >= 0)
  1274. {
  1275. next_active_state = active_states;
  1276. active_count = 0;
  1277. i = -1;
  1278. ADD_ACTIVE(repeat_state_offset, 0);
  1279. }
  1280. }
  1281. else
  1282. {
  1283. const uschar *p = start_subject + local_offsets[0];
  1284. const uschar *pp = start_subject + local_offsets[1];
  1285. while (p < pp) if ((*p++ & 0xc0) == 0x80) charcount--;
  1286. ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1));
  1287. if (repeat_state_offset >= 0)
  1288. { ADD_NEW_DATA(-repeat_state_offset, 0, (charcount - 1)); }
  1289. }
  1290. }
  1291. else if (rc != PCRE_ERROR_NOMATCH) return rc;
  1292. }
  1293. break;
  1294. /* ========================================================================== */
  1295. /* Handle callouts */
  1296. case OP_CALLOUT:
  1297. if (pcre_callout != NULL)
  1298. {
  1299. int rrc;
  1300. pcre_callout_block cb;
  1301. cb.version = 1; /* Version 1 of the callout block */
  1302. cb.callout_number = code[1];
  1303. cb.offset_vector = offsets;
  1304. cb.subject = (char *)start_subject;
  1305. cb.subject_length = end_subject - start_subject;
  1306. cb.start_match = current_subject - start_subject;
  1307. cb.current_position = ptr - start_subject;
  1308. cb.pattern_position = GET(code, 2);
  1309. cb.next_item_length = GET(code, 2 + LINK_SIZE);
  1310. cb.capture_top = 1;
  1311. cb.capture_last = -1;
  1312. cb.callout_data = md->callout_data;
  1313. if ((rrc = (*pcre_callout)(&cb)) < 0) return rrc; /* Abandon */
  1314. if (rrc == 0) { ADD_ACTIVE(state_offset + 2 + 2*LINK_SIZE, 0); }
  1315. }
  1316. break;
  1317. /* ========================================================================== */
  1318. default: /* Unsupported opcode */
  1319. return PCRE_ERROR_DFA_UITEM;
  1320. }
  1321. NEXT_ACTIVE_STATE: continue;
  1322. } /* End of loop scanning active states */
  1323. /* We have finished the processing at the current subject character. If no
  1324. new states have been set for the next character, we have found all the
  1325. matches that we are going to find. If we are at the top level and partial
  1326. matching has been requested, check for appropriate conditions. */
  1327. if (new_count <= 0)
  1328. {
  1329. if (match_count < 0 && /* No matches found */
  1330. rlevel == 1 && /* Top level match function */
  1331. (md->moptions & PCRE_PARTIAL) != 0 && /* Want partial matching */
  1332. ptr >= end_subject && /* Reached end of subject */
  1333. ptr > current_subject) /* Matched non-empty string */
  1334. {
  1335. if (offsetcount >= 2)
  1336. {
  1337. offsets[0] = current_subject - start_subject;
  1338. offsets[1] = end_subject - start_subject;
  1339. }
  1340. match_count = PCRE_ERROR_PARTIAL;
  1341. }
  1342. DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
  1343. "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel, match_count,
  1344. rlevel*2-2, SP));
  1345. return match_count;
  1346. }
  1347. /* One or more states are active for the next character. */
  1348. ptr += clen; /* Advance to next subject character */
  1349. } /* Loop to move along the subject string */
  1350. }
  1351. /*************************************************
  1352. * Execute a Regular Expression - DFA engine *
  1353. *************************************************/
  1354. /* This external function applies a compiled re to a subject string using a DFA
  1355. engine. This function calls the internal function multiple times if the pattern
  1356. is not anchored.
  1357. Arguments:
  1358. argument_re points to the compiled expression
  1359. extra_data points to extra data or is NULL (not currently used)
  1360. subject points to the subject string
  1361. length length of subject string (may contain binary zeros)
  1362. start_offset where to start in the subject string
  1363. options option bits
  1364. offsets vector of match offsets
  1365. offsetcount size of same
  1366. workspace workspace vector
  1367. wscount size of same
  1368. Returns: > 0 => number of match offset pairs placed in offsets
  1369. = 0 => offsets overflowed; longest matches are present
  1370. -1 => failed to match
  1371. < -1 => some kind of unexpected problem
  1372. */
  1373. EXPORT int
  1374. pcre_dfa_exec(const pcre *argument_re, const pcre_extra *extra_data,
  1375. const char *subject, int length, int start_offset, int options, int *offsets,
  1376. int offsetcount, int *workspace, int wscount)
  1377. {
  1378. real_pcre *re = (real_pcre *)argument_re;
  1379. dfa_match_data match_block;
  1380. BOOL utf8, anchored, startline, firstline;
  1381. const uschar *current_subject, *end_subject, *lcc;
  1382. pcre_study_data internal_study;
  1383. const pcre_study_data *study = NULL;
  1384. real_pcre internal_re;
  1385. const uschar *req_byte_ptr;
  1386. const uschar *start_bits = NULL;
  1387. BOOL first_byte_caseless = FALSE;
  1388. BOOL req_byte_caseless = FALSE;
  1389. int first_byte = -1;
  1390. int req_byte = -1;
  1391. int req_byte2 = -1;
  1392. /* Plausibility checks */
  1393. if ((options & ~PUBLIC_DFA_EXEC_OPTIONS) != 0) return PCRE_ERROR_BADOPTION;
  1394. if (re == NULL || subject == NULL || workspace == NULL ||
  1395. (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;
  1396. if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
  1397. if (wscount < 20) return PCRE_ERROR_DFA_WSSIZE;
  1398. /* We need to find the pointer to any study data before we test for byte
  1399. flipping, so we scan the extra_data block first. This may set two fields in the
  1400. match block, so we must initialize them beforehand. However, the other fields
  1401. in the match block must not be set until after the byte flipping. */
  1402. match_block.tables = re->tables;
  1403. match_block.callout_data = NULL;
  1404. if (extra_data != NULL)
  1405. {
  1406. unsigned int flags = extra_data->flags;
  1407. if ((flags & PCRE_EXTRA_STUDY_DATA) != 0)
  1408. study = (const pcre_study_data *)extra_data->study_data;
  1409. if ((flags & PCRE_EXTRA_MATCH_LIMIT) != 0) return PCRE_ERROR_DFA_UMLIMIT;
  1410. if ((flags & PCRE_EXTRA_CALLOUT_DATA) != 0)
  1411. match_block.callout_data = extra_data->callout_data;
  1412. if ((flags & PCRE_EXTRA_TABLES) != 0)
  1413. match_block.tables = extra_data->tables;
  1414. }
  1415. /* Check that the first field in the block is the magic number. If it is not,
  1416. test for a regex that was compiled on a host of opposite endianness. If this is
  1417. the case, flipped values are put in internal_re and internal_study if there was
  1418. study data too. */
  1419. if (re->magic_number != MAGIC_NUMBER)
  1420. {
  1421. re = _pcre_try_flipped(re, &internal_re, study, &internal_study);
  1422. if (re == NULL) return PCRE_ERROR_BADMAGIC;
  1423. if (study != NULL) study = &internal_study;
  1424. }
  1425. /* Set some local values */
  1426. current_subject = (const unsigned char *)subject + start_offset;
  1427. end_subject = (const unsigned char *)subject + length;
  1428. req_byte_ptr = current_subject - 1;
  1429. utf8 = (re->options & PCRE_UTF8) != 0;
  1430. anchored = (options & PCRE_ANCHORED) != 0 || (re->options & PCRE_ANCHORED) != 0;
  1431. /* The remaining fixed data for passing around. */
  1432. match_block.start_code = (const uschar *)argument_re +
  1433. re->name_table_offset + re->name_count * re->name_entry_size;
  1434. match_block.start_subject = (const unsigned char *)subject;
  1435. match_block.end_subject = end_subject;
  1436. match_block.moptions = options;
  1437. match_block.poptions = re->options;
  1438. /* Check a UTF-8 string if required. Unfortunately there's no way of passing
  1439. back the character offset. */
  1440. #ifdef SUPPORT_UTF8
  1441. if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)
  1442. {
  1443. if (_pcre_valid_utf8((uschar *)subject, length) >= 0)
  1444. return PCRE_ERROR_BADUTF8;
  1445. if (start_offset > 0 && start_offset < length)
  1446. {
  1447. int tb = ((uschar *)subject)[start_offset];
  1448. if (tb > 127)
  1449. {
  1450. tb &= 0xc0;
  1451. if (tb != 0 && tb != 0xc0) return PCRE_ERROR_BADUTF8_OFFSET;
  1452. }
  1453. }
  1454. }
  1455. #endif
  1456. /* If the exec call supplied NULL for tables, use the inbuilt ones. This
  1457. is a feature that makes it possible to save compiled regex and re-use them
  1458. in other programs later. */
  1459. if (match_block.tables == NULL) match_block.tables = _pcre_default_tables;
  1460. /* The lower casing table and the "must be at the start of a line" flag are
  1461. used in a loop when finding where to start. */
  1462. lcc = match_block.tables + lcc_offset;
  1463. startline = (re->options & PCRE_STARTLINE) != 0;
  1464. firstline = (re->options & PCRE_FIRSTLINE) != 0;
  1465. /* Set up the first character to match, if available. The first_byte value is
  1466. never set for an anchored regular expression, but the anchoring may be forced
  1467. at run time, so we have to test for anchoring. The first char may be unset for
  1468. an unanchored pattern, of course. If there's no first char and the pattern was
  1469. studied, there may be a bitmap of possible first characters. */
  1470. if (!anchored)
  1471. {
  1472. if ((re->options & PCRE_FIRSTSET) != 0)
  1473. {
  1474. first_byte = re->first_byte & 255;
  1475. if ((first_byte_caseless = ((re->first_byte & REQ_CASELESS) != 0)) == TRUE)
  1476. first_byte = lcc[first_byte];
  1477. }
  1478. else
  1479. {
  1480. if (startline && study != NULL &&
  1481. (study->options & PCRE_STUDY_MAPPED) != 0)
  1482. start_bits = study->start_bits;
  1483. }
  1484. }
  1485. /* For anchored or unanchored matches, there may be a "last known required
  1486. character" set. */
  1487. if ((re->options & PCRE_REQCHSET) != 0)
  1488. {
  1489. req_byte = re->req_byte & 255;
  1490. req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0;
  1491. req_byte2 = (match_block.tables + fcc_offset)[req_byte]; /* case flipped */
  1492. }
  1493. /* Call the main matching function, looping for a non-anchored regex after a
  1494. failed match. Unless restarting, optimize by moving to the first match
  1495. character if possible, when not anchored. Then unless wanting a partial match,
  1496. check for a required later character. */
  1497. for (;;)
  1498. {
  1499. int rc;
  1500. if ((options & PCRE_DFA_RESTART) == 0)
  1501. {
  1502. const uschar *save_end_subject = end_subject;
  1503. /* Advance to a unique first char if possible. If firstline is TRUE, the
  1504. start of the match is constrained to the first line of a multiline string.
  1505. Implement this by temporarily adjusting end_subject so that we stop scanning
  1506. at a newline. If the match fails at the newline, later code breaks this loop.
  1507. */
  1508. if (firstline)
  1509. {
  1510. const uschar *t = current_subject;
  1511. while (t < save_end_subject && *t != '\n') t++;
  1512. end_subject = t;
  1513. }
  1514. if (first_byte >= 0)
  1515. {
  1516. if (first_byte_caseless)
  1517. while (current_subject < end_subject &&
  1518. lcc[*current_subject] != first_byte)
  1519. current_subject++;
  1520. else
  1521. while (current_subject < end_subject && *current_subject != first_byte)
  1522. current_subject++;
  1523. }
  1524. /* Or to just after \n for a multiline match if possible */
  1525. else if (startline)
  1526. {
  1527. if (current_subject > match_block.start_subject + start_offset)
  1528. {
  1529. while (current_subject < end_subject && current_subject[-1] != NEWLINE)
  1530. current_subject++;
  1531. }
  1532. }
  1533. /* Or to a non-unique first char after study */
  1534. else if (start_bits != NULL)
  1535. {
  1536. while (current_subject < end_subject)
  1537. {
  1538. register unsigned int c = *current_subject;
  1539. if ((start_bits[c/8] & (1 << (c&7))) == 0) current_subject++;
  1540. else break;
  1541. }
  1542. }
  1543. /* Restore fudged end_subject */
  1544. end_subject = save_end_subject;
  1545. }
  1546. /* If req_byte is set, we know that that character must appear in the subject
  1547. for the match to succeed. If the first character is set, req_byte must be
  1548. later in the subject; otherwise the test starts at the match point. This
  1549. optimization can save a huge amount of work in patterns with nested unlimited
  1550. repeats that aren't going to match. Writing separate code for cased/caseless
  1551. versions makes it go faster, as does using an autoincrement and backing off
  1552. on a match.
  1553. HOWEVER: when the subject string is very, very long, searching to its end can
  1554. take a long time, and give bad performance on quite ordinary patterns. This
  1555. showed up when somebody was matching /^C/ on a 32-megabyte string... so we
  1556. don't do this when the string is sufficiently long.
  1557. ALSO: this processing is disabled when partial matching is requested.
  1558. */
  1559. if (req_byte >= 0 &&
  1560. end_subject - current_subject < REQ_BYTE_MAX &&
  1561. (options & PCRE_PARTIAL) == 0)
  1562. {
  1563. register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);
  1564. /* We don't need to repeat the search if we haven't yet reached the
  1565. place we found it at last time. */
  1566. if (p > req_byte_ptr)
  1567. {
  1568. if (req_byte_caseless)
  1569. {
  1570. while (p < end_subject)
  1571. {
  1572. register int pp = *p++;
  1573. if (pp == req_byte || pp == req_byte2) { p--; break; }
  1574. }
  1575. }
  1576. else
  1577. {
  1578. while (p < end_subject)
  1579. {
  1580. if (*p++ == req_byte) { p--; break; }
  1581. }
  1582. }
  1583. /* If we can't find the required character, break the matching loop,
  1584. which will cause a return or PCRE_ERROR_NOMATCH. */
  1585. if (p >= end_subject) break;
  1586. /* If we have found the required character, save the point where we
  1587. found it, so that we don't search again next time round the loop if
  1588. the start hasn't passed this character yet. */
  1589. req_byte_ptr = p;
  1590. }
  1591. }
  1592. /* OK, now we can do the business */
  1593. rc = internal_dfa_exec(
  1594. &match_block, /* fixed match data */
  1595. match_block.start_code, /* this subexpression's code */
  1596. current_subject, /* where we currently are */
  1597. start_offset, /* start offset in subject */
  1598. offsets, /* offset vector */
  1599. offsetcount, /* size of same */
  1600. workspace, /* workspace vector */
  1601. wscount,