PageRenderTime 63ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/pcre/pcre_dfa_exec.c

http://github.com/feyeleanor/RubyGoLightly
C | 2920 lines | 2135 code | 374 blank | 411 comment | 717 complexity | 5a75071cbd89cdc47950d9729812e6fe MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-2008 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This module contains the external function pcre_dfa_exec(), which is an
  33. alternative matching function that uses a sort of DFA algorithm (not a true
  34. FSM). This is NOT Perl- compatible, but it has advantages in certain
  35. applications. */
  36. #ifdef HAVE_CONFIG_H
  37. #include "config.h"
  38. #endif
  39. #define NLBLOCK md /* Block containing newline information */
  40. #define PSSTART start_subject /* Field containing processed string start */
  41. #define PSEND end_subject /* Field containing processed string end */
  42. #include "pcre_internal.h"
  43. /* For use to indent debugging output */
  44. #define SP " "
  45. /*************************************************
  46. * Code parameters and static tables *
  47. *************************************************/
  48. /* These are offsets that are used to turn the OP_TYPESTAR and friends opcodes
  49. into others, under special conditions. A gap of 20 between the blocks should be
  50. enough. The resulting opcodes don't have to be less than 256 because they are
  51. never stored, so we push them well clear of the normal opcodes. */
  52. #define OP_PROP_EXTRA 300
  53. #define OP_EXTUNI_EXTRA 320
  54. #define OP_ANYNL_EXTRA 340
  55. #define OP_HSPACE_EXTRA 360
  56. #define OP_VSPACE_EXTRA 380
  57. /* This table identifies those opcodes that are followed immediately by a
  58. character that is to be tested in some way. This makes is possible to
  59. centralize the loading of these characters. In the case of Type * etc, the
  60. "character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be a
  61. small value. ***NOTE*** If the start of this table is modified, the two tables
  62. that follow must also be modified. */
  63. static const uschar coptable[] = {
  64. 0, /* End */
  65. 0, 0, 0, 0, 0, /* \A, \G, \K, \B, \b */
  66. 0, 0, 0, 0, 0, 0, /* \D, \d, \S, \s, \W, \w */
  67. 0, 0, 0, /* Any, AllAny, Anybyte */
  68. 0, 0, 0, /* NOTPROP, PROP, EXTUNI */
  69. 0, 0, 0, 0, 0, /* \R, \H, \h, \V, \v */
  70. 0, 0, 0, 0, 0, /* \Z, \z, Opt, ^, $ */
  71. 1, /* Char */
  72. 1, /* Charnc */
  73. 1, /* not */
  74. /* Positive single-char repeats */
  75. 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */
  76. 3, 3, 3, /* upto, minupto, exact */
  77. 1, 1, 1, 3, /* *+, ++, ?+, upto+ */
  78. /* Negative single-char repeats - only for chars < 256 */
  79. 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */
  80. 3, 3, 3, /* NOT upto, minupto, exact */
  81. 1, 1, 1, 3, /* NOT *+, ++, ?+, updo+ */
  82. /* Positive type repeats */
  83. 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */
  84. 3, 3, 3, /* Type upto, minupto, exact */
  85. 1, 1, 1, 3, /* Type *+, ++, ?+, upto+ */
  86. /* Character class & ref repeats */
  87. 0, 0, 0, 0, 0, 0, /* *, *?, +, +?, ?, ?? */
  88. 0, 0, /* CRRANGE, CRMINRANGE */
  89. 0, /* CLASS */
  90. 0, /* NCLASS */
  91. 0, /* XCLASS - variable length */
  92. 0, /* REF */
  93. 0, /* RECURSE */
  94. 0, /* CALLOUT */
  95. 0, /* Alt */
  96. 0, /* Ket */
  97. 0, /* KetRmax */
  98. 0, /* KetRmin */
  99. 0, /* Assert */
  100. 0, /* Assert not */
  101. 0, /* Assert behind */
  102. 0, /* Assert behind not */
  103. 0, /* Reverse */
  104. 0, 0, 0, 0, /* ONCE, BRA, CBRA, COND */
  105. 0, 0, 0, /* SBRA, SCBRA, SCOND */
  106. 0, /* CREF */
  107. 0, /* RREF */
  108. 0, /* DEF */
  109. 0, 0, /* BRAZERO, BRAMINZERO */
  110. 0, 0, 0, 0, /* PRUNE, SKIP, THEN, COMMIT */
  111. 0, 0, 0 /* FAIL, ACCEPT, SKIPZERO */
  112. };
  113. /* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,
  114. and \w */
  115. static const uschar toptable1[] = {
  116. 0, 0, 0, 0, 0, 0,
  117. ctype_digit, ctype_digit,
  118. ctype_space, ctype_space,
  119. ctype_word, ctype_word,
  120. 0, 0 /* OP_ANY, OP_ALLANY */
  121. };
  122. static const uschar toptable2[] = {
  123. 0, 0, 0, 0, 0, 0,
  124. ctype_digit, 0,
  125. ctype_space, 0,
  126. ctype_word, 0,
  127. 1, 1 /* OP_ANY, OP_ALLANY */
  128. };
  129. /* Structure for holding data about a particular state, which is in effect the
  130. current data for an active path through the match tree. It must consist
  131. entirely of ints because the working vector we are passed, and which we put
  132. these structures in, is a vector of ints. */
  133. typedef struct stateblock {
  134. int offset; /* Offset to opcode */
  135. int count; /* Count for repeats */
  136. int ims; /* ims flag bits */
  137. int data; /* Some use extra data */
  138. } stateblock;
  139. #define INTS_PER_STATEBLOCK (sizeof(stateblock)/sizeof(int))
  140. #ifdef DEBUG
  141. /*************************************************
  142. * Print character string *
  143. *************************************************/
  144. /* Character string printing function for debugging.
  145. Arguments:
  146. p points to string
  147. length number of bytes
  148. f where to print
  149. Returns: nothing
  150. */
  151. static void
  152. pchars(unsigned char *p, int length, FILE *f)
  153. {
  154. int c;
  155. while (length-- > 0)
  156. {
  157. if (isprint(c = *(p++)))
  158. fprintf(f, "%c", c);
  159. else
  160. fprintf(f, "\\x%02x", c);
  161. }
  162. }
  163. #endif
  164. /*************************************************
  165. * Execute a Regular Expression - DFA engine *
  166. *************************************************/
  167. /* This internal function applies a compiled pattern to a subject string,
  168. starting at a given point, using a DFA engine. This function is called from the
  169. external one, possibly multiple times if the pattern is not anchored. The
  170. function calls itself recursively for some kinds of subpattern.
  171. Arguments:
  172. md the match_data block with fixed information
  173. this_start_code the opening bracket of this subexpression's code
  174. current_subject where we currently are in the subject string
  175. start_offset start offset in the subject string
  176. offsets vector to contain the matching string offsets
  177. offsetcount size of same
  178. workspace vector of workspace
  179. wscount size of same
  180. ims the current ims flags
  181. rlevel function call recursion level
  182. recursing regex recursive call level
  183. Returns: > 0 => number of match offset pairs placed in offsets
  184. = 0 => offsets overflowed; longest matches are present
  185. -1 => failed to match
  186. < -1 => some kind of unexpected problem
  187. The following macros are used for adding states to the two state vectors (one
  188. for the current character, one for the following character). */
  189. #define ADD_ACTIVE(x,y) \
  190. if (active_count++ < wscount) \
  191. { \
  192. next_active_state->offset = (x); \
  193. next_active_state->count = (y); \
  194. next_active_state->ims = ims; \
  195. next_active_state++; \
  196. DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
  197. } \
  198. else return PCRE_ERROR_DFA_WSSIZE
  199. #define ADD_ACTIVE_DATA(x,y,z) \
  200. if (active_count++ < wscount) \
  201. { \
  202. next_active_state->offset = (x); \
  203. next_active_state->count = (y); \
  204. next_active_state->ims = ims; \
  205. next_active_state->data = (z); \
  206. next_active_state++; \
  207. DPRINTF(("%.*sADD_ACTIVE_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \
  208. } \
  209. else return PCRE_ERROR_DFA_WSSIZE
  210. #define ADD_NEW(x,y) \
  211. if (new_count++ < wscount) \
  212. { \
  213. next_new_state->offset = (x); \
  214. next_new_state->count = (y); \
  215. next_new_state->ims = ims; \
  216. next_new_state++; \
  217. DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \
  218. } \
  219. else return PCRE_ERROR_DFA_WSSIZE
  220. #define ADD_NEW_DATA(x,y,z) \
  221. if (new_count++ < wscount) \
  222. { \
  223. next_new_state->offset = (x); \
  224. next_new_state->count = (y); \
  225. next_new_state->ims = ims; \
  226. next_new_state->data = (z); \
  227. next_new_state++; \
  228. DPRINTF(("%.*sADD_NEW_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \
  229. } \
  230. else return PCRE_ERROR_DFA_WSSIZE
  231. /* And now, here is the code */
  232. static int
  233. internal_dfa_exec(
  234. dfa_match_data *md,
  235. const uschar *this_start_code,
  236. const uschar *current_subject,
  237. int start_offset,
  238. int *offsets,
  239. int offsetcount,
  240. int *workspace,
  241. int wscount,
  242. int ims,
  243. int rlevel,
  244. int recursing)
  245. {
  246. stateblock *active_states, *new_states, *temp_states;
  247. stateblock *next_active_state, *next_new_state;
  248. const uschar *ctypes, *lcc, *fcc;
  249. const uschar *ptr;
  250. const uschar *end_code, *first_op;
  251. int active_count, new_count, match_count;
  252. /* Some fields in the md block are frequently referenced, so we load them into
  253. independent variables in the hope that this will perform better. */
  254. const uschar *start_subject = md->start_subject;
  255. const uschar *end_subject = md->end_subject;
  256. const uschar *start_code = md->start_code;
  257. #ifdef SUPPORT_UTF8
  258. BOOL utf8 = (md->poptions & PCRE_UTF8) != 0;
  259. #else
  260. BOOL utf8 = FALSE;
  261. #endif
  262. rlevel++;
  263. offsetcount &= (-2);
  264. wscount -= 2;
  265. wscount = (wscount - (wscount % (INTS_PER_STATEBLOCK * 2))) /
  266. (2 * INTS_PER_STATEBLOCK);
  267. DPRINTF(("\n%.*s---------------------\n"
  268. "%.*sCall to internal_dfa_exec f=%d r=%d\n",
  269. rlevel*2-2, SP, rlevel*2-2, SP, rlevel, recursing));
  270. ctypes = md->tables + ctypes_offset;
  271. lcc = md->tables + lcc_offset;
  272. fcc = md->tables + fcc_offset;
  273. match_count = PCRE_ERROR_NOMATCH; /* A negative number */
  274. active_states = (stateblock *)(workspace + 2);
  275. next_new_state = new_states = active_states + wscount;
  276. new_count = 0;
  277. first_op = this_start_code + 1 + LINK_SIZE +
  278. ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA)? 2:0);
  279. /* The first thing in any (sub) pattern is a bracket of some sort. Push all
  280. the alternative states onto the list, and find out where the end is. This
  281. makes is possible to use this function recursively, when we want to stop at a
  282. matching internal ket rather than at the end.
  283. If the first opcode in the first alternative is OP_REVERSE, we are dealing with
  284. a backward assertion. In that case, we have to find out the maximum amount to
  285. move back, and set up each alternative appropriately. */
  286. if (*first_op == OP_REVERSE)
  287. {
  288. int max_back = 0;
  289. int gone_back;
  290. end_code = this_start_code;
  291. do
  292. {
  293. int back = GET(end_code, 2+LINK_SIZE);
  294. if (back > max_back) max_back = back;
  295. end_code += GET(end_code, 1);
  296. }
  297. while (*end_code == OP_ALT);
  298. /* If we can't go back the amount required for the longest lookbehind
  299. pattern, go back as far as we can; some alternatives may still be viable. */
  300. #ifdef SUPPORT_UTF8
  301. /* In character mode we have to step back character by character */
  302. if (utf8)
  303. {
  304. for (gone_back = 0; gone_back < max_back; gone_back++)
  305. {
  306. if (current_subject <= start_subject) break;
  307. current_subject--;
  308. while (current_subject > start_subject &&
  309. (*current_subject & 0xc0) == 0x80)
  310. current_subject--;
  311. }
  312. }
  313. else
  314. #endif
  315. /* In byte-mode we can do this quickly. */
  316. {
  317. gone_back = (current_subject - max_back < start_subject)?
  318. current_subject - start_subject : max_back;
  319. current_subject -= gone_back;
  320. }
  321. /* Now we can process the individual branches. */
  322. end_code = this_start_code;
  323. do
  324. {
  325. int back = GET(end_code, 2+LINK_SIZE);
  326. if (back <= gone_back)
  327. {
  328. int bstate = end_code - start_code + 2 + 2*LINK_SIZE;
  329. ADD_NEW_DATA(-bstate, 0, gone_back - back);
  330. }
  331. end_code += GET(end_code, 1);
  332. }
  333. while (*end_code == OP_ALT);
  334. }
  335. /* This is the code for a "normal" subpattern (not a backward assertion). The
  336. start of a whole pattern is always one of these. If we are at the top level,
  337. we may be asked to restart matching from the same point that we reached for a
  338. previous partial match. We still have to scan through the top-level branches to
  339. find the end state. */
  340. else
  341. {
  342. end_code = this_start_code;
  343. /* Restarting */
  344. if (rlevel == 1 && (md->moptions & PCRE_DFA_RESTART) != 0)
  345. {
  346. do { end_code += GET(end_code, 1); } while (*end_code == OP_ALT);
  347. new_count = workspace[1];
  348. if (!workspace[0])
  349. memcpy(new_states, active_states, new_count * sizeof(stateblock));
  350. }
  351. /* Not restarting */
  352. else
  353. {
  354. int length = 1 + LINK_SIZE +
  355. ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA)? 2:0);
  356. do
  357. {
  358. ADD_NEW(end_code - start_code + length, 0);
  359. end_code += GET(end_code, 1);
  360. length = 1 + LINK_SIZE;
  361. }
  362. while (*end_code == OP_ALT);
  363. }
  364. }
  365. workspace[0] = 0; /* Bit indicating which vector is current */
  366. DPRINTF(("%.*sEnd state = %d\n", rlevel*2-2, SP, end_code - start_code));
  367. /* Loop for scanning the subject */
  368. ptr = current_subject;
  369. for (;;)
  370. {
  371. int i, j;
  372. int clen, dlen;
  373. unsigned int c, d;
  374. /* Make the new state list into the active state list and empty the
  375. new state list. */
  376. temp_states = active_states;
  377. active_states = new_states;
  378. new_states = temp_states;
  379. active_count = new_count;
  380. new_count = 0;
  381. workspace[0] ^= 1; /* Remember for the restarting feature */
  382. workspace[1] = active_count;
  383. #ifdef DEBUG
  384. printf("%.*sNext character: rest of subject = \"", rlevel*2-2, SP);
  385. pchars((uschar *)ptr, strlen((char *)ptr), stdout);
  386. printf("\"\n");
  387. printf("%.*sActive states: ", rlevel*2-2, SP);
  388. for (i = 0; i < active_count; i++)
  389. printf("%d/%d ", active_states[i].offset, active_states[i].count);
  390. printf("\n");
  391. #endif
  392. /* Set the pointers for adding new states */
  393. next_active_state = active_states + active_count;
  394. next_new_state = new_states;
  395. /* Load the current character from the subject outside the loop, as many
  396. different states may want to look at it, and we assume that at least one
  397. will. */
  398. if (ptr < end_subject)
  399. {
  400. clen = 1; /* Number of bytes in the character */
  401. #ifdef SUPPORT_UTF8
  402. if (utf8) { GETCHARLEN(c, ptr, clen); } else
  403. #endif /* SUPPORT_UTF8 */
  404. c = *ptr;
  405. }
  406. else
  407. {
  408. clen = 0; /* This indicates the end of the subject */
  409. c = NOTACHAR; /* This value should never actually be used */
  410. }
  411. /* Scan up the active states and act on each one. The result of an action
  412. may be to add more states to the currently active list (e.g. on hitting a
  413. parenthesis) or it may be to put states on the new list, for considering
  414. when we move the character pointer on. */
  415. for (i = 0; i < active_count; i++)
  416. {
  417. stateblock *current_state = active_states + i;
  418. const uschar *code;
  419. int state_offset = current_state->offset;
  420. int count, codevalue;
  421. #ifdef DEBUG
  422. printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);
  423. if (clen == 0) printf("EOL\n");
  424. else if (c > 32 && c < 127) printf("'%c'\n", c);
  425. else printf("0x%02x\n", c);
  426. #endif
  427. /* This variable is referred to implicity in the ADD_xxx macros. */
  428. ims = current_state->ims;
  429. /* A negative offset is a special case meaning "hold off going to this
  430. (negated) state until the number of characters in the data field have
  431. been skipped". */
  432. if (state_offset < 0)
  433. {
  434. if (current_state->data > 0)
  435. {
  436. DPRINTF(("%.*sSkipping this character\n", rlevel*2-2, SP));
  437. ADD_NEW_DATA(state_offset, current_state->count,
  438. current_state->data - 1);
  439. continue;
  440. }
  441. else
  442. {
  443. current_state->offset = state_offset = -state_offset;
  444. }
  445. }
  446. /* Check for a duplicate state with the same count, and skip if found. */
  447. for (j = 0; j < i; j++)
  448. {
  449. if (active_states[j].offset == state_offset &&
  450. active_states[j].count == current_state->count)
  451. {
  452. DPRINTF(("%.*sDuplicate state: skipped\n", rlevel*2-2, SP));
  453. goto NEXT_ACTIVE_STATE;
  454. }
  455. }
  456. /* The state offset is the offset to the opcode */
  457. code = start_code + state_offset;
  458. codevalue = *code;
  459. /* If this opcode is followed by an inline character, load it. It is
  460. tempting to test for the presence of a subject character here, but that
  461. is wrong, because sometimes zero repetitions of the subject are
  462. permitted.
  463. We also use this mechanism for opcodes such as OP_TYPEPLUS that take an
  464. argument that is not a data character - but is always one byte long. We
  465. have to take special action to deal with \P, \p, \H, \h, \V, \v and \X in
  466. this case. To keep the other cases fast, convert these ones to new opcodes.
  467. */
  468. if (coptable[codevalue] > 0)
  469. {
  470. dlen = 1;
  471. #ifdef SUPPORT_UTF8
  472. if (utf8) { GETCHARLEN(d, (code + coptable[codevalue]), dlen); } else
  473. #endif /* SUPPORT_UTF8 */
  474. d = code[coptable[codevalue]];
  475. if (codevalue >= OP_TYPESTAR)
  476. {
  477. switch(d)
  478. {
  479. case OP_ANYBYTE: return PCRE_ERROR_DFA_UITEM;
  480. case OP_NOTPROP:
  481. case OP_PROP: codevalue += OP_PROP_EXTRA; break;
  482. case OP_ANYNL: codevalue += OP_ANYNL_EXTRA; break;
  483. case OP_EXTUNI: codevalue += OP_EXTUNI_EXTRA; break;
  484. case OP_NOT_HSPACE:
  485. case OP_HSPACE: codevalue += OP_HSPACE_EXTRA; break;
  486. case OP_NOT_VSPACE:
  487. case OP_VSPACE: codevalue += OP_VSPACE_EXTRA; break;
  488. default: break;
  489. }
  490. }
  491. }
  492. else
  493. {
  494. dlen = 0; /* Not strictly necessary, but compilers moan */
  495. d = NOTACHAR; /* if these variables are not set. */
  496. }
  497. /* Now process the individual opcodes */
  498. switch (codevalue)
  499. {
  500. /* ========================================================================== */
  501. /* Reached a closing bracket. If not at the end of the pattern, carry
  502. on with the next opcode. Otherwise, unless we have an empty string and
  503. PCRE_NOTEMPTY is set, save the match data, shifting up all previous
  504. matches so we always have the longest first. */
  505. case OP_KET:
  506. case OP_KETRMIN:
  507. case OP_KETRMAX:
  508. if (code != end_code)
  509. {
  510. ADD_ACTIVE(state_offset + 1 + LINK_SIZE, 0);
  511. if (codevalue != OP_KET)
  512. {
  513. ADD_ACTIVE(state_offset - GET(code, 1), 0);
  514. }
  515. }
  516. else if (ptr > current_subject || (md->moptions & PCRE_NOTEMPTY) == 0)
  517. {
  518. if (match_count < 0) match_count = (offsetcount >= 2)? 1 : 0;
  519. else if (match_count > 0 && ++match_count * 2 >= offsetcount)
  520. match_count = 0;
  521. count = ((match_count == 0)? offsetcount : match_count * 2) - 2;
  522. if (count > 0) memmove(offsets + 2, offsets, count * sizeof(int));
  523. if (offsetcount >= 2)
  524. {
  525. offsets[0] = current_subject - start_subject;
  526. offsets[1] = ptr - start_subject;
  527. DPRINTF(("%.*sSet matched string = \"%.*s\"\n", rlevel*2-2, SP,
  528. offsets[1] - offsets[0], current_subject));
  529. }
  530. if ((md->moptions & PCRE_DFA_SHORTEST) != 0)
  531. {
  532. DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
  533. "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel,
  534. match_count, rlevel*2-2, SP));
  535. return match_count;
  536. }
  537. }
  538. break;
  539. /* ========================================================================== */
  540. /* These opcodes add to the current list of states without looking
  541. at the current character. */
  542. /*-----------------------------------------------------------------*/
  543. case OP_ALT:
  544. do { code += GET(code, 1); } while (*code == OP_ALT);
  545. ADD_ACTIVE(code - start_code, 0);
  546. break;
  547. /*-----------------------------------------------------------------*/
  548. case OP_BRA:
  549. case OP_SBRA:
  550. do
  551. {
  552. ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);
  553. code += GET(code, 1);
  554. }
  555. while (*code == OP_ALT);
  556. break;
  557. /*-----------------------------------------------------------------*/
  558. case OP_CBRA:
  559. case OP_SCBRA:
  560. ADD_ACTIVE(code - start_code + 3 + LINK_SIZE, 0);
  561. code += GET(code, 1);
  562. while (*code == OP_ALT)
  563. {
  564. ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);
  565. code += GET(code, 1);
  566. }
  567. break;
  568. /*-----------------------------------------------------------------*/
  569. case OP_BRAZERO:
  570. case OP_BRAMINZERO:
  571. ADD_ACTIVE(state_offset + 1, 0);
  572. code += 1 + GET(code, 2);
  573. while (*code == OP_ALT) code += GET(code, 1);
  574. ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);
  575. break;
  576. /*-----------------------------------------------------------------*/
  577. case OP_SKIPZERO:
  578. code += 1 + GET(code, 2);
  579. while (*code == OP_ALT) code += GET(code, 1);
  580. ADD_ACTIVE(code - start_code + 1 + LINK_SIZE, 0);
  581. break;
  582. /*-----------------------------------------------------------------*/
  583. case OP_CIRC:
  584. if ((ptr == start_subject && (md->moptions & PCRE_NOTBOL) == 0) ||
  585. ((ims & PCRE_MULTILINE) != 0 &&
  586. ptr != end_subject &&
  587. WAS_NEWLINE(ptr)))
  588. { ADD_ACTIVE(state_offset + 1, 0); }
  589. break;
  590. /*-----------------------------------------------------------------*/
  591. case OP_EOD:
  592. if (ptr >= end_subject) { ADD_ACTIVE(state_offset + 1, 0); }
  593. break;
  594. /*-----------------------------------------------------------------*/
  595. case OP_OPT:
  596. ims = code[1];
  597. ADD_ACTIVE(state_offset + 2, 0);
  598. break;
  599. /*-----------------------------------------------------------------*/
  600. case OP_SOD:
  601. if (ptr == start_subject) { ADD_ACTIVE(state_offset + 1, 0); }
  602. break;
  603. /*-----------------------------------------------------------------*/
  604. case OP_SOM:
  605. if (ptr == start_subject + start_offset) { ADD_ACTIVE(state_offset + 1, 0); }
  606. break;
  607. /* ========================================================================== */
  608. /* These opcodes inspect the next subject character, and sometimes
  609. the previous one as well, but do not have an argument. The variable
  610. clen contains the length of the current character and is zero if we are
  611. at the end of the subject. */
  612. /*-----------------------------------------------------------------*/
  613. case OP_ANY:
  614. if (clen > 0 && !IS_NEWLINE(ptr))
  615. { ADD_NEW(state_offset + 1, 0); }
  616. break;
  617. /*-----------------------------------------------------------------*/
  618. case OP_ALLANY:
  619. if (clen > 0)
  620. { ADD_NEW(state_offset + 1, 0); }
  621. break;
  622. /*-----------------------------------------------------------------*/
  623. case OP_EODN:
  624. if (clen == 0 || (IS_NEWLINE(ptr) && ptr == end_subject - md->nllen))
  625. { ADD_ACTIVE(state_offset + 1, 0); }
  626. break;
  627. /*-----------------------------------------------------------------*/
  628. case OP_DOLL:
  629. if ((md->moptions & PCRE_NOTEOL) == 0)
  630. {
  631. if (clen == 0 ||
  632. (IS_NEWLINE(ptr) &&
  633. ((ims & PCRE_MULTILINE) != 0 || ptr == end_subject - md->nllen)
  634. ))
  635. { ADD_ACTIVE(state_offset + 1, 0); }
  636. }
  637. else if ((ims & PCRE_MULTILINE) != 0 && IS_NEWLINE(ptr))
  638. { ADD_ACTIVE(state_offset + 1, 0); }
  639. break;
  640. /*-----------------------------------------------------------------*/
  641. case OP_DIGIT:
  642. case OP_WHITESPACE:
  643. case OP_WORDCHAR:
  644. if (clen > 0 && c < 256 &&
  645. ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0)
  646. { ADD_NEW(state_offset + 1, 0); }
  647. break;
  648. /*-----------------------------------------------------------------*/
  649. case OP_NOT_DIGIT:
  650. case OP_NOT_WHITESPACE:
  651. case OP_NOT_WORDCHAR:
  652. if (clen > 0 && (c >= 256 ||
  653. ((ctypes[c] & toptable1[codevalue]) ^ toptable2[codevalue]) != 0))
  654. { ADD_NEW(state_offset + 1, 0); }
  655. break;
  656. /*-----------------------------------------------------------------*/
  657. case OP_WORD_BOUNDARY:
  658. case OP_NOT_WORD_BOUNDARY:
  659. {
  660. int left_word, right_word;
  661. if (ptr > start_subject)
  662. {
  663. const uschar *temp = ptr - 1;
  664. #ifdef SUPPORT_UTF8
  665. if (utf8) BACKCHAR(temp);
  666. #endif
  667. GETCHARTEST(d, temp);
  668. left_word = d < 256 && (ctypes[d] & ctype_word) != 0;
  669. }
  670. else left_word = 0;
  671. if (clen > 0) right_word = c < 256 && (ctypes[c] & ctype_word) != 0;
  672. else right_word = 0;
  673. if ((left_word == right_word) == (codevalue == OP_NOT_WORD_BOUNDARY))
  674. { ADD_ACTIVE(state_offset + 1, 0); }
  675. }
  676. break;
  677. /*-----------------------------------------------------------------*/
  678. /* Check the next character by Unicode property. We will get here only
  679. if the support is in the binary; otherwise a compile-time error occurs.
  680. */
  681. #ifdef SUPPORT_UCP
  682. case OP_PROP:
  683. case OP_NOTPROP:
  684. if (clen > 0)
  685. {
  686. BOOL OK;
  687. const ucd_record * prop = GET_UCD(c);
  688. switch(code[1])
  689. {
  690. case PT_ANY:
  691. OK = TRUE;
  692. break;
  693. case PT_LAMP:
  694. OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || prop->chartype == ucp_Lt;
  695. break;
  696. case PT_GC:
  697. OK = _pcre_ucp_gentype[prop->chartype] == code[2];
  698. break;
  699. case PT_PC:
  700. OK = prop->chartype == code[2];
  701. break;
  702. case PT_SC:
  703. OK = prop->script == code[2];
  704. break;
  705. /* Should never occur, but keep compilers from grumbling. */
  706. default:
  707. OK = codevalue != OP_PROP;
  708. break;
  709. }
  710. if (OK == (codevalue == OP_PROP)) { ADD_NEW(state_offset + 3, 0); }
  711. }
  712. break;
  713. #endif
  714. /* ========================================================================== */
  715. /* These opcodes likewise inspect the subject character, but have an
  716. argument that is not a data character. It is one of these opcodes:
  717. OP_ANY, OP_ALLANY, OP_DIGIT, OP_NOT_DIGIT, OP_WHITESPACE, OP_NOT_SPACE,
  718. OP_WORDCHAR, OP_NOT_WORDCHAR. The value is loaded into d. */
  719. case OP_TYPEPLUS:
  720. case OP_TYPEMINPLUS:
  721. case OP_TYPEPOSPLUS:
  722. count = current_state->count; /* Already matched */
  723. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  724. if (clen > 0)
  725. {
  726. if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  727. (c < 256 &&
  728. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  729. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  730. {
  731. if (count > 0 && codevalue == OP_TYPEPOSPLUS)
  732. {
  733. active_count--; /* Remove non-match possibility */
  734. next_active_state--;
  735. }
  736. count++;
  737. ADD_NEW(state_offset, count);
  738. }
  739. }
  740. break;
  741. /*-----------------------------------------------------------------*/
  742. case OP_TYPEQUERY:
  743. case OP_TYPEMINQUERY:
  744. case OP_TYPEPOSQUERY:
  745. ADD_ACTIVE(state_offset + 2, 0);
  746. if (clen > 0)
  747. {
  748. if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  749. (c < 256 &&
  750. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  751. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  752. {
  753. if (codevalue == OP_TYPEPOSQUERY)
  754. {
  755. active_count--; /* Remove non-match possibility */
  756. next_active_state--;
  757. }
  758. ADD_NEW(state_offset + 2, 0);
  759. }
  760. }
  761. break;
  762. /*-----------------------------------------------------------------*/
  763. case OP_TYPESTAR:
  764. case OP_TYPEMINSTAR:
  765. case OP_TYPEPOSSTAR:
  766. ADD_ACTIVE(state_offset + 2, 0);
  767. if (clen > 0)
  768. {
  769. if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  770. (c < 256 &&
  771. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  772. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  773. {
  774. if (codevalue == OP_TYPEPOSSTAR)
  775. {
  776. active_count--; /* Remove non-match possibility */
  777. next_active_state--;
  778. }
  779. ADD_NEW(state_offset, 0);
  780. }
  781. }
  782. break;
  783. /*-----------------------------------------------------------------*/
  784. case OP_TYPEEXACT:
  785. count = current_state->count; /* Number already matched */
  786. if (clen > 0)
  787. {
  788. if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  789. (c < 256 &&
  790. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  791. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  792. {
  793. if (++count >= GET2(code, 1))
  794. { ADD_NEW(state_offset + 4, 0); }
  795. else
  796. { ADD_NEW(state_offset, count); }
  797. }
  798. }
  799. break;
  800. /*-----------------------------------------------------------------*/
  801. case OP_TYPEUPTO:
  802. case OP_TYPEMINUPTO:
  803. case OP_TYPEPOSUPTO:
  804. ADD_ACTIVE(state_offset + 4, 0);
  805. count = current_state->count; /* Number already matched */
  806. if (clen > 0)
  807. {
  808. if ((c >= 256 && d != OP_DIGIT && d != OP_WHITESPACE && d != OP_WORDCHAR) ||
  809. (c < 256 &&
  810. (d != OP_ANY || !IS_NEWLINE(ptr)) &&
  811. ((ctypes[c] & toptable1[d]) ^ toptable2[d]) != 0))
  812. {
  813. if (codevalue == OP_TYPEPOSUPTO)
  814. {
  815. active_count--; /* Remove non-match possibility */
  816. next_active_state--;
  817. }
  818. if (++count >= GET2(code, 1))
  819. { ADD_NEW(state_offset + 4, 0); }
  820. else
  821. { ADD_NEW(state_offset, count); }
  822. }
  823. }
  824. break;
  825. /* ========================================================================== */
  826. /* These are virtual opcodes that are used when something like
  827. OP_TYPEPLUS has OP_PROP, OP_NOTPROP, OP_ANYNL, or OP_EXTUNI as its
  828. argument. It keeps the code above fast for the other cases. The argument
  829. is in the d variable. */
  830. #ifdef SUPPORT_UCP
  831. case OP_PROP_EXTRA + OP_TYPEPLUS:
  832. case OP_PROP_EXTRA + OP_TYPEMINPLUS:
  833. case OP_PROP_EXTRA + OP_TYPEPOSPLUS:
  834. count = current_state->count; /* Already matched */
  835. if (count > 0) { ADD_ACTIVE(state_offset + 4, 0); }
  836. if (clen > 0)
  837. {
  838. BOOL OK;
  839. const ucd_record * prop = GET_UCD(c);
  840. switch(code[2])
  841. {
  842. case PT_ANY:
  843. OK = TRUE;
  844. break;
  845. case PT_LAMP:
  846. OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || prop->chartype == ucp_Lt;
  847. break;
  848. case PT_GC:
  849. OK = _pcre_ucp_gentype[prop->chartype] == code[3];
  850. break;
  851. case PT_PC:
  852. OK = prop->chartype == code[3];
  853. break;
  854. case PT_SC:
  855. OK = prop->script == code[3];
  856. break;
  857. /* Should never occur, but keep compilers from grumbling. */
  858. default:
  859. OK = codevalue != OP_PROP;
  860. break;
  861. }
  862. if (OK == (d == OP_PROP))
  863. {
  864. if (count > 0 && codevalue == OP_PROP_EXTRA + OP_TYPEPOSPLUS)
  865. {
  866. active_count--; /* Remove non-match possibility */
  867. next_active_state--;
  868. }
  869. count++;
  870. ADD_NEW(state_offset, count);
  871. }
  872. }
  873. break;
  874. /*-----------------------------------------------------------------*/
  875. case OP_EXTUNI_EXTRA + OP_TYPEPLUS:
  876. case OP_EXTUNI_EXTRA + OP_TYPEMINPLUS:
  877. case OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS:
  878. count = current_state->count; /* Already matched */
  879. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  880. if (clen > 0 && UCD_CATEGORY(c) != ucp_M)
  881. {
  882. const uschar *nptr = ptr + clen;
  883. int ncount = 0;
  884. if (count > 0 && codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSPLUS)
  885. {
  886. active_count--; /* Remove non-match possibility */
  887. next_active_state--;
  888. }
  889. while (nptr < end_subject)
  890. {
  891. int nd;
  892. int ndlen = 1;
  893. GETCHARLEN(nd, nptr, ndlen);
  894. if (UCD_CATEGORY(nd) != ucp_M) break;
  895. ncount++;
  896. nptr += ndlen;
  897. }
  898. count++;
  899. ADD_NEW_DATA(-state_offset, count, ncount);
  900. }
  901. break;
  902. #endif
  903. /*-----------------------------------------------------------------*/
  904. case OP_ANYNL_EXTRA + OP_TYPEPLUS:
  905. case OP_ANYNL_EXTRA + OP_TYPEMINPLUS:
  906. case OP_ANYNL_EXTRA + OP_TYPEPOSPLUS:
  907. count = current_state->count; /* Already matched */
  908. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  909. if (clen > 0)
  910. {
  911. int ncount = 0;
  912. switch (c)
  913. {
  914. case 0x000b:
  915. case 0x000c:
  916. case 0x0085:
  917. case 0x2028:
  918. case 0x2029:
  919. if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
  920. goto ANYNL01;
  921. case 0x000d:
  922. if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1;
  923. /* Fall through */
  924. ANYNL01:
  925. case 0x000a:
  926. if (count > 0 && codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSPLUS)
  927. {
  928. active_count--; /* Remove non-match possibility */
  929. next_active_state--;
  930. }
  931. count++;
  932. ADD_NEW_DATA(-state_offset, count, ncount);
  933. break;
  934. default:
  935. break;
  936. }
  937. }
  938. break;
  939. /*-----------------------------------------------------------------*/
  940. case OP_VSPACE_EXTRA + OP_TYPEPLUS:
  941. case OP_VSPACE_EXTRA + OP_TYPEMINPLUS:
  942. case OP_VSPACE_EXTRA + OP_TYPEPOSPLUS:
  943. count = current_state->count; /* Already matched */
  944. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  945. if (clen > 0)
  946. {
  947. BOOL OK;
  948. switch (c)
  949. {
  950. case 0x000a:
  951. case 0x000b:
  952. case 0x000c:
  953. case 0x000d:
  954. case 0x0085:
  955. case 0x2028:
  956. case 0x2029:
  957. OK = TRUE;
  958. break;
  959. default:
  960. OK = FALSE;
  961. break;
  962. }
  963. if (OK == (d == OP_VSPACE))
  964. {
  965. if (count > 0 && codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSPLUS)
  966. {
  967. active_count--; /* Remove non-match possibility */
  968. next_active_state--;
  969. }
  970. count++;
  971. ADD_NEW_DATA(-state_offset, count, 0);
  972. }
  973. }
  974. break;
  975. /*-----------------------------------------------------------------*/
  976. case OP_HSPACE_EXTRA + OP_TYPEPLUS:
  977. case OP_HSPACE_EXTRA + OP_TYPEMINPLUS:
  978. case OP_HSPACE_EXTRA + OP_TYPEPOSPLUS:
  979. count = current_state->count; /* Already matched */
  980. if (count > 0) { ADD_ACTIVE(state_offset + 2, 0); }
  981. if (clen > 0)
  982. {
  983. BOOL OK;
  984. switch (c)
  985. {
  986. case 0x09: /* HT */
  987. case 0x20: /* SPACE */
  988. case 0xa0: /* NBSP */
  989. case 0x1680: /* OGHAM SPACE MARK */
  990. case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
  991. case 0x2000: /* EN QUAD */
  992. case 0x2001: /* EM QUAD */
  993. case 0x2002: /* EN SPACE */
  994. case 0x2003: /* EM SPACE */
  995. case 0x2004: /* THREE-PER-EM SPACE */
  996. case 0x2005: /* FOUR-PER-EM SPACE */
  997. case 0x2006: /* SIX-PER-EM SPACE */
  998. case 0x2007: /* FIGURE SPACE */
  999. case 0x2008: /* PUNCTUATION SPACE */
  1000. case 0x2009: /* THIN SPACE */
  1001. case 0x200A: /* HAIR SPACE */
  1002. case 0x202f: /* NARROW NO-BREAK SPACE */
  1003. case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
  1004. case 0x3000: /* IDEOGRAPHIC SPACE */
  1005. OK = TRUE;
  1006. break;
  1007. default:
  1008. OK = FALSE;
  1009. break;
  1010. }
  1011. if (OK == (d == OP_HSPACE))
  1012. {
  1013. if (count > 0 && codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSPLUS)
  1014. {
  1015. active_count--; /* Remove non-match possibility */
  1016. next_active_state--;
  1017. }
  1018. count++;
  1019. ADD_NEW_DATA(-state_offset, count, 0);
  1020. }
  1021. }
  1022. break;
  1023. /*-----------------------------------------------------------------*/
  1024. #ifdef SUPPORT_UCP
  1025. case OP_PROP_EXTRA + OP_TYPEQUERY:
  1026. case OP_PROP_EXTRA + OP_TYPEMINQUERY:
  1027. case OP_PROP_EXTRA + OP_TYPEPOSQUERY:
  1028. count = 4;
  1029. goto QS1;
  1030. case OP_PROP_EXTRA + OP_TYPESTAR:
  1031. case OP_PROP_EXTRA + OP_TYPEMINSTAR:
  1032. case OP_PROP_EXTRA + OP_TYPEPOSSTAR:
  1033. count = 0;
  1034. QS1:
  1035. ADD_ACTIVE(state_offset + 4, 0);
  1036. if (clen > 0)
  1037. {
  1038. BOOL OK;
  1039. const ucd_record * prop = GET_UCD(c);
  1040. switch(code[2])
  1041. {
  1042. case PT_ANY:
  1043. OK = TRUE;
  1044. break;
  1045. case PT_LAMP:
  1046. OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || prop->chartype == ucp_Lt;
  1047. break;
  1048. case PT_GC:
  1049. OK = _pcre_ucp_gentype[prop->chartype] == code[3];
  1050. break;
  1051. case PT_PC:
  1052. OK = prop->chartype == code[3];
  1053. break;
  1054. case PT_SC:
  1055. OK = prop->script == code[3];
  1056. break;
  1057. /* Should never occur, but keep compilers from grumbling. */
  1058. default:
  1059. OK = codevalue != OP_PROP;
  1060. break;
  1061. }
  1062. if (OK == (d == OP_PROP))
  1063. {
  1064. if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSSTAR ||
  1065. codevalue == OP_PROP_EXTRA + OP_TYPEPOSQUERY)
  1066. {
  1067. active_count--; /* Remove non-match possibility */
  1068. next_active_state--;
  1069. }
  1070. ADD_NEW(state_offset + count, 0);
  1071. }
  1072. }
  1073. break;
  1074. /*-----------------------------------------------------------------*/
  1075. case OP_EXTUNI_EXTRA + OP_TYPEQUERY:
  1076. case OP_EXTUNI_EXTRA + OP_TYPEMINQUERY:
  1077. case OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY:
  1078. count = 2;
  1079. goto QS2;
  1080. case OP_EXTUNI_EXTRA + OP_TYPESTAR:
  1081. case OP_EXTUNI_EXTRA + OP_TYPEMINSTAR:
  1082. case OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR:
  1083. count = 0;
  1084. QS2:
  1085. ADD_ACTIVE(state_offset + 2, 0);
  1086. if (clen > 0 && UCD_CATEGORY(c) != ucp_M)
  1087. {
  1088. const uschar *nptr = ptr + clen;
  1089. int ncount = 0;
  1090. if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSSTAR ||
  1091. codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSQUERY)
  1092. {
  1093. active_count--; /* Remove non-match possibility */
  1094. next_active_state--;
  1095. }
  1096. while (nptr < end_subject)
  1097. {
  1098. int nd;
  1099. int ndlen = 1;
  1100. GETCHARLEN(nd, nptr, ndlen);
  1101. if (UCD_CATEGORY(nd) != ucp_M) break;
  1102. ncount++;
  1103. nptr += ndlen;
  1104. }
  1105. ADD_NEW_DATA(-(state_offset + count), 0, ncount);
  1106. }
  1107. break;
  1108. #endif
  1109. /*-----------------------------------------------------------------*/
  1110. case OP_ANYNL_EXTRA + OP_TYPEQUERY:
  1111. case OP_ANYNL_EXTRA + OP_TYPEMINQUERY:
  1112. case OP_ANYNL_EXTRA + OP_TYPEPOSQUERY:
  1113. count = 2;
  1114. goto QS3;
  1115. case OP_ANYNL_EXTRA + OP_TYPESTAR:
  1116. case OP_ANYNL_EXTRA + OP_TYPEMINSTAR:
  1117. case OP_ANYNL_EXTRA + OP_TYPEPOSSTAR:
  1118. count = 0;
  1119. QS3:
  1120. ADD_ACTIVE(state_offset + 2, 0);
  1121. if (clen > 0)
  1122. {
  1123. int ncount = 0;
  1124. switch (c)
  1125. {
  1126. case 0x000b:
  1127. case 0x000c:
  1128. case 0x0085:
  1129. case 0x2028:
  1130. case 0x2029:
  1131. if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
  1132. goto ANYNL02;
  1133. case 0x000d:
  1134. if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1;
  1135. /* Fall through */
  1136. ANYNL02:
  1137. case 0x000a:
  1138. if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSSTAR ||
  1139. codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSQUERY)
  1140. {
  1141. active_count--; /* Remove non-match possibility */
  1142. next_active_state--;
  1143. }
  1144. ADD_NEW_DATA(-(state_offset + count), 0, ncount);
  1145. break;
  1146. default:
  1147. break;
  1148. }
  1149. }
  1150. break;
  1151. /*-----------------------------------------------------------------*/
  1152. case OP_VSPACE_EXTRA + OP_TYPEQUERY:
  1153. case OP_VSPACE_EXTRA + OP_TYPEMINQUERY:
  1154. case OP_VSPACE_EXTRA + OP_TYPEPOSQUERY:
  1155. count = 2;
  1156. goto QS4;
  1157. case OP_VSPACE_EXTRA + OP_TYPESTAR:
  1158. case OP_VSPACE_EXTRA + OP_TYPEMINSTAR:
  1159. case OP_VSPACE_EXTRA + OP_TYPEPOSSTAR:
  1160. count = 0;
  1161. QS4:
  1162. ADD_ACTIVE(state_offset + 2, 0);
  1163. if (clen > 0)
  1164. {
  1165. BOOL OK;
  1166. switch (c)
  1167. {
  1168. case 0x000a:
  1169. case 0x000b:
  1170. case 0x000c:
  1171. case 0x000d:
  1172. case 0x0085:
  1173. case 0x2028:
  1174. case 0x2029:
  1175. OK = TRUE;
  1176. break;
  1177. default:
  1178. OK = FALSE;
  1179. break;
  1180. }
  1181. if (OK == (d == OP_VSPACE))
  1182. {
  1183. if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSSTAR ||
  1184. codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSQUERY)
  1185. {
  1186. active_count--; /* Remove non-match possibility */
  1187. next_active_state--;
  1188. }
  1189. ADD_NEW_DATA(-(state_offset + count), 0, 0);
  1190. }
  1191. }
  1192. break;
  1193. /*-----------------------------------------------------------------*/
  1194. case OP_HSPACE_EXTRA + OP_TYPEQUERY:
  1195. case OP_HSPACE_EXTRA + OP_TYPEMINQUERY:
  1196. case OP_HSPACE_EXTRA + OP_TYPEPOSQUERY:
  1197. count = 2;
  1198. goto QS5;
  1199. case OP_HSPACE_EXTRA + OP_TYPESTAR:
  1200. case OP_HSPACE_EXTRA + OP_TYPEMINSTAR:
  1201. case OP_HSPACE_EXTRA + OP_TYPEPOSSTAR:
  1202. count = 0;
  1203. QS5:
  1204. ADD_ACTIVE(state_offset + 2, 0);
  1205. if (clen > 0)
  1206. {
  1207. BOOL OK;
  1208. switch (c)
  1209. {
  1210. case 0x09: /* HT */
  1211. case 0x20: /* SPACE */
  1212. case 0xa0: /* NBSP */
  1213. case 0x1680: /* OGHAM SPACE MARK */
  1214. case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
  1215. case 0x2000: /* EN QUAD */
  1216. case 0x2001: /* EM QUAD */
  1217. case 0x2002: /* EN SPACE */
  1218. case 0x2003: /* EM SPACE */
  1219. case 0x2004: /* THREE-PER-EM SPACE */
  1220. case 0x2005: /* FOUR-PER-EM SPACE */
  1221. case 0x2006: /* SIX-PER-EM SPACE */
  1222. case 0x2007: /* FIGURE SPACE */
  1223. case 0x2008: /* PUNCTUATION SPACE */
  1224. case 0x2009: /* THIN SPACE */
  1225. case 0x200A: /* HAIR SPACE */
  1226. case 0x202f: /* NARROW NO-BREAK SPACE */
  1227. case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
  1228. case 0x3000: /* IDEOGRAPHIC SPACE */
  1229. OK = TRUE;
  1230. break;
  1231. default:
  1232. OK = FALSE;
  1233. break;
  1234. }
  1235. if (OK == (d == OP_HSPACE))
  1236. {
  1237. if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSSTAR ||
  1238. codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSQUERY)
  1239. {
  1240. active_count--; /* Remove non-match possibility */
  1241. next_active_state--;
  1242. }
  1243. ADD_NEW_DATA(-(state_offset + count), 0, 0);
  1244. }
  1245. }
  1246. break;
  1247. /*-----------------------------------------------------------------*/
  1248. #ifdef SUPPORT_UCP
  1249. case OP_PROP_EXTRA + OP_TYPEEXACT:
  1250. case OP_PROP_EXTRA + OP_TYPEUPTO:
  1251. case OP_PROP_EXTRA + OP_TYPEMINUPTO:
  1252. case OP_PROP_EXTRA + OP_TYPEPOSUPTO:
  1253. if (codevalue != OP_PROP_EXTRA + OP_TYPEEXACT)
  1254. { ADD_ACTIVE(state_offset + 6, 0); }
  1255. count = current_state->count; /* Number already matched */
  1256. if (clen > 0)
  1257. {
  1258. BOOL OK;
  1259. const ucd_record * prop = GET_UCD(c);
  1260. switch(code[4])
  1261. {
  1262. case PT_ANY:
  1263. OK = TRUE;
  1264. break;
  1265. case PT_LAMP:
  1266. OK = prop->chartype == ucp_Lu || prop->chartype == ucp_Ll || prop->chartype == ucp_Lt;
  1267. break;
  1268. case PT_GC:
  1269. OK = _pcre_ucp_gentype[prop->chartype] == code[5];
  1270. break;
  1271. case PT_PC:
  1272. OK = prop->chartype == code[5];
  1273. break;
  1274. case PT_SC:
  1275. OK = prop->script == code[5];
  1276. break;
  1277. /* Should never occur, but keep compilers from grumbling. */
  1278. default:
  1279. OK = codevalue != OP_PROP;
  1280. break;
  1281. }
  1282. if (OK == (d == OP_PROP))
  1283. {
  1284. if (codevalue == OP_PROP_EXTRA + OP_TYPEPOSUPTO)
  1285. {
  1286. active_count--; /* Remove non-match possibility */
  1287. next_active_state--;
  1288. }
  1289. if (++count >= GET2(code, 1))
  1290. { ADD_NEW(state_offset + 6, 0); }
  1291. else
  1292. { ADD_NEW(state_offset, count); }
  1293. }
  1294. }
  1295. break;
  1296. /*-----------------------------------------------------------------*/
  1297. case OP_EXTUNI_EXTRA + OP_TYPEEXACT:
  1298. case OP_EXTUNI_EXTRA + OP_TYPEUPTO:
  1299. case OP_EXTUNI_EXTRA + OP_TYPEMINUPTO:
  1300. case OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO:
  1301. if (codevalue != OP_EXTUNI_EXTRA + OP_TYPEEXACT)
  1302. { ADD_ACTIVE(state_offset + 4, 0); }
  1303. count = current_state->count; /* Number already matched */
  1304. if (clen > 0 && UCD_CATEGORY(c) != ucp_M)
  1305. {
  1306. const uschar *nptr = ptr + clen;
  1307. int ncount = 0;
  1308. if (codevalue == OP_EXTUNI_EXTRA + OP_TYPEPOSUPTO)
  1309. {
  1310. active_count--; /* Remove non-match possibility */
  1311. next_active_state--;
  1312. }
  1313. while (nptr < end_subject)
  1314. {
  1315. int nd;
  1316. int ndlen = 1;
  1317. GETCHARLEN(nd, nptr, ndlen);
  1318. if (UCD_CATEGORY(nd) != ucp_M) break;
  1319. ncount++;
  1320. nptr += ndlen;
  1321. }
  1322. if (++count >= GET2(code, 1))
  1323. { ADD_NEW_DATA(-(state_offset + 4), 0, ncount); }
  1324. else
  1325. { ADD_NEW_DATA(-state_offset, count, ncount); }
  1326. }
  1327. break;
  1328. #endif
  1329. /*-----------------------------------------------------------------*/
  1330. case OP_ANYNL_EXTRA + OP_TYPEEXACT:
  1331. case OP_ANYNL_EXTRA + OP_TYPEUPTO:
  1332. case OP_ANYNL_EXTRA + OP_TYPEMINUPTO:
  1333. case OP_ANYNL_EXTRA + OP_TYPEPOSUPTO:
  1334. if (codevalue != OP_ANYNL_EXTRA + OP_TYPEEXACT)
  1335. { ADD_ACTIVE(state_offset + 4, 0); }
  1336. count = current_state->count; /* Number already matched */
  1337. if (clen > 0)
  1338. {
  1339. int ncount = 0;
  1340. switch (c)
  1341. {
  1342. case 0x000b:
  1343. case 0x000c:
  1344. case 0x0085:
  1345. case 0x2028:
  1346. case 0x2029:
  1347. if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
  1348. goto ANYNL03;
  1349. case 0x000d:
  1350. if (ptr + 1 < end_subject && ptr[1] == 0x0a) ncount = 1;
  1351. /* Fall through */
  1352. ANYNL03:
  1353. case 0x000a:
  1354. if (codevalue == OP_ANYNL_EXTRA + OP_TYPEPOSUPTO)
  1355. {
  1356. active_count--; /* Remove non-match possibility */
  1357. next_active_state--;
  1358. }
  1359. if (++count >= GET2(code, 1))
  1360. { ADD_NEW_DATA(-(state_offset + 4), 0, ncount); }
  1361. else
  1362. { ADD_NEW_DATA(-state_offset, count, ncount); }
  1363. break;
  1364. default:
  1365. break;
  1366. }
  1367. }
  1368. break;
  1369. /*-----------------------------------------------------------------*/
  1370. case OP_VSPACE_EXTRA + OP_TYPEEXACT:
  1371. case OP_VSPACE_EXTRA + OP_TYPEUPTO:
  1372. case OP_VSPACE_EXTRA + OP_TYPEMINUPTO:
  1373. case OP_VSPACE_EXTRA + OP_TYPEPOSUPTO:
  1374. if (codevalue != OP_VSPACE_EXTRA + OP_TYPEEXACT)
  1375. { ADD_ACTIVE(state_offset + 4, 0); }
  1376. count = current_state->count; /* Number already matched */
  1377. if (clen > 0)
  1378. {
  1379. BOOL OK;
  1380. switch (c)
  1381. {
  1382. case 0x000a:
  1383. case 0x000b:
  1384. case 0x000c:
  1385. case 0x000d:
  1386. case 0x0085:
  1387. case 0x2028:
  1388. case 0x2029:
  1389. OK = TRUE;
  1390. break;
  1391. default:
  1392. OK = FALSE;
  1393. }
  1394. if (OK == (d == OP_VSPACE))
  1395. {
  1396. if (codevalue == OP_VSPACE_EXTRA + OP_TYPEPOSUPTO)
  1397. {
  1398. active_count--; /* Remove non-match possibility */
  1399. next_active_state--;
  1400. }
  1401. if (++count >= GET2(code, 1))
  1402. { ADD_NEW_DATA(-(state_offset + 4), 0, 0); }
  1403. else
  1404. { ADD_NEW_DATA(-state_offset, count, 0); }
  1405. }
  1406. }
  1407. break;
  1408. /*-----------------------------------------------------------------*/
  1409. case OP_HSPACE_EXTRA + OP_TYPEEXACT:
  1410. case OP_HSPACE_EXTRA + OP_TYPEUPTO:
  1411. case OP_HSPACE_EXTRA + OP_TYPEMINUPTO:
  1412. case OP_HSPACE_EXTRA + OP_TYPEPOSUPTO:
  1413. if (codevalue != OP_HSPACE_EXTRA + OP_TYPEEXACT)
  1414. { ADD_ACTIVE(state_offset + 4, 0); }
  1415. count = current_state->count; /* Number already matched */
  1416. if (clen > 0)
  1417. {
  1418. BOOL OK;
  1419. switch (c)
  1420. {
  1421. case 0x09: /* HT */
  1422. case 0x20: /* SPACE */
  1423. case 0xa0: /* NBSP */
  1424. case 0x1680: /* OGHAM SPACE MARK */
  1425. case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
  1426. case 0x2000: /* EN QUAD */
  1427. case 0x2001: /* EM QUAD */
  1428. case 0x2002: /* EN SPACE */
  1429. case 0x2003: /* EM SPACE */
  1430. case 0x2004: /* THREE-PER-EM SPACE */
  1431. case 0x2005: /* FOUR-PER-EM SPACE */
  1432. case 0x2006: /* SIX-PER-EM SPACE */
  1433. case 0x2007: /* FIGURE SPACE */
  1434. case 0x2008: /* PUNCTUATION SPACE */
  1435. case 0x2009: /* THIN SPACE */
  1436. case 0x200A: /* HAIR SPACE */
  1437. case 0x202f: /* NARROW NO-BREAK SPACE */
  1438. case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
  1439. case 0x3000: /* IDEOGRAPHIC SPACE */
  1440. OK = TRUE;
  1441. break;
  1442. default:
  1443. OK = FALSE;
  1444. break;
  1445. }
  1446. if (OK == (d == OP_HSPACE))
  1447. {
  1448. if (codevalue == OP_HSPACE_EXTRA + OP_TYPEPOSUPTO)
  1449. {
  1450. active_count--; /* Remove non-match possibility */
  1451. next_active_state--;
  1452. }
  1453. if (++count >= GET2(code, 1))
  1454. { ADD_NEW_DATA(-(state_offset + 4), 0, 0); }
  1455. else
  1456. { ADD_NEW_DATA(-state_offset, count, 0); }
  1457. }
  1458. }
  1459. break;
  1460. /* ========================================================================== */
  1461. /* These opcodes are followed by a character that is usually compared
  1462. to the current subject character; it is loaded into d. We still get
  1463. here even if there is no subject character, because in some cases zero
  1464. repetitions are permitted. */
  1465. /*-----------------------------------------------------------------*/
  1466. case OP_CHAR:
  1467. if (clen > 0 && c == d) { ADD_NEW(state_offset + dlen + 1, 0); }
  1468. break;
  1469. /*-----------------------------------------------------------------*/
  1470. case OP_CHARNC:
  1471. if (clen == 0) break;
  1472. #ifdef SUPPORT_UTF8
  1473. if (utf8)
  1474. {
  1475. if (c == d) { ADD_NEW(state_offset + dlen + 1, 0); } else
  1476. {
  1477. unsigned int othercase;
  1478. if (c < 128) othercase = fcc[c]; else
  1479. /* If we have Unicode property support, we can use it to test the
  1480. other case of the character. */
  1481. #ifdef SUPPORT_UCP
  1482. othercase = UCD_OTHERCASE(c);
  1483. #else
  1484. othercase = NOTACHAR;
  1485. #endif
  1486. if (d == othercase) { ADD_NEW(state_offset + dlen + 1, 0); }
  1487. }
  1488. }
  1489. else
  1490. #endif /* SUPPORT_UTF8 */
  1491. /* Non-UTF-8 mode */
  1492. {
  1493. if (lcc[c] == lcc[d]) { ADD_NEW(state_offset + 2, 0); }
  1494. }
  1495. break;
  1496. #ifdef SUPPORT_UCP
  1497. /*-----------------------------------------------------------------*/
  1498. /* This is a tricky one because it can match more than one character.
  1499. Find out how many characters to skip, and then set up a negative state
  1500. to wait for them to pass before continuing. */
  1501. case OP_EXTUNI:
  1502. if (clen > 0 && UCD_CATEGORY(c) != ucp_M)
  1503. {
  1504. const uschar *nptr = ptr + clen;
  1505. int ncount = 0;
  1506. while (nptr < end_subject)
  1507. {
  1508. int nclen = 1;
  1509. GETCHARLEN(c, nptr, nclen);
  1510. if (UCD_CATEGORY(c) != ucp_M) break;
  1511. ncount++;
  1512. nptr += nclen;
  1513. }
  1514. ADD_NEW_DATA(-(state_offset + 1), 0, ncount);
  1515. }
  1516. break;
  1517. #endif
  1518. /*-----------------------------------------------------------------*/
  1519. /* This is a tricky like EXTUNI because it too can match more than one
  1520. character (when CR is followed by LF). In this case, set up a negative
  1521. state to wait for one character to pass before continuing. */
  1522. case OP_ANYNL:
  1523. if (clen > 0) switch(c)
  1524. {
  1525. case 0x000b:
  1526. case 0x000c:
  1527. case 0x0085:
  1528. case 0x2028:
  1529. case 0x2029:
  1530. if ((md->moptions & PCRE_BSR_ANYCRLF) != 0) break;
  1531. case 0x000a:
  1532. ADD_NEW(state_offset + 1, 0);
  1533. break;
  1534. case 0x000d:
  1535. if (ptr + 1 < end_subject && ptr[1] == 0x0a)
  1536. {
  1537. ADD_NEW_DATA(-(state_offset + 1), 0, 1);
  1538. }
  1539. else
  1540. {
  1541. ADD_NEW(state_offset + 1, 0);
  1542. }
  1543. break;
  1544. }
  1545. break;
  1546. /*-----------------------------------------------------------------*/
  1547. case OP_NOT_VSPACE:
  1548. if (clen > 0) switch(c)
  1549. {
  1550. case 0x000a:
  1551. case 0x000b:
  1552. case 0x000c:
  1553. case 0x000d:
  1554. case 0x0085:
  1555. case 0x2028:
  1556. case 0x2029:
  1557. break;
  1558. default:
  1559. ADD_NEW(state_offset + 1, 0);
  1560. break;
  1561. }
  1562. break;
  1563. /*-----------------------------------------------------------------*/
  1564. case OP_VSPACE:
  1565. if (clen > 0) switch(c)
  1566. {
  1567. case 0x000a:
  1568. case 0x000b:
  1569. case 0x000c:
  1570. case 0x000d:
  1571. case 0x0085:
  1572. case 0x2028:
  1573. case 0x2029:
  1574. ADD_NEW(state_offset + 1, 0);
  1575. break;
  1576. default: break;
  1577. }
  1578. break;
  1579. /*-----------------------------------------------------------------*/
  1580. case OP_NOT_HSPACE:
  1581. if (clen > 0) switch(c)
  1582. {
  1583. case 0x09: /* HT */
  1584. case 0x20: /* SPACE */
  1585. case 0xa0: /* NBSP */
  1586. case 0x1680: /* OGHAM SPACE MARK */
  1587. case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
  1588. case 0x2000: /* EN QUAD */
  1589. case 0x2001: /* EM QUAD */
  1590. case 0x2002: /* EN SPACE */
  1591. case 0x2003: /* EM SPACE */
  1592. case 0x2004: /* THREE-PER-EM SPACE */
  1593. case 0x2005: /* FOUR-PER-EM SPACE */
  1594. case 0x2006: /* SIX-PER-EM SPACE */
  1595. case 0x2007: /* FIGURE SPACE */
  1596. case 0x2008: /* PUNCTUATION SPACE */
  1597. case 0x2009: /* THIN SPACE */
  1598. case 0x200A: /* HAIR SPACE */
  1599. case 0x202f: /* NARROW NO-BREAK SPACE */
  1600. case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
  1601. case 0x3000: /* IDEOGRAPHIC SPACE */
  1602. break;
  1603. default:
  1604. ADD_NEW(state_offset + 1, 0);
  1605. break;
  1606. }
  1607. break;
  1608. /*-----------------------------------------------------------------*/
  1609. case OP_HSPACE:
  1610. if (clen > 0) switch(c)
  1611. {
  1612. case 0x09: /* HT */
  1613. case 0x20: /* SPACE */
  1614. case 0xa0: /* NBSP */
  1615. case 0x1680: /* OGHAM SPACE MARK */
  1616. case 0x180e: /* MONGOLIAN VOWEL SEPARATOR */
  1617. case 0x2000: /* EN QUAD */
  1618. case 0x2001: /* EM QUAD */
  1619. case 0x2002: /* EN SPACE */
  1620. case 0x2003: /* EM SPACE */
  1621. case 0x2004: /* THREE-PER-EM SPACE */
  1622. case 0x2005: /* FOUR-PER-EM SPACE */
  1623. case 0x2006: /* SIX-PER-EM SPACE */
  1624. case 0x2007: /* FIGURE SPACE */
  1625. case 0x2008: /* PUNCTUATION SPACE */
  1626. case 0x2009: /* THIN SPACE */
  1627. case 0x200A: /* HAIR SPACE */
  1628. case 0x202f: /* NARROW NO-BREAK SPACE */
  1629. case 0x205f: /* MEDIUM MATHEMATICAL SPACE */
  1630. case 0x3000: /* IDEOGRAPHIC SPACE */
  1631. ADD_NEW(state_offset + 1, 0);
  1632. break;
  1633. }
  1634. break;
  1635. /*-----------------------------------------------------------------*/
  1636. /* Match a negated single character. This is only used for one-byte
  1637. characters, that is, we know that d < 256. The character we are
  1638. checking (c) can be multibyte. */
  1639. case OP_NOT:
  1640. if (clen > 0)
  1641. {
  1642. unsigned int otherd = ((ims & PCRE_CASELESS) != 0)? fcc[d] : d;
  1643. if (c != d && c != otherd) { ADD_NEW(state_offset + dlen + 1, 0); }
  1644. }
  1645. break;
  1646. /*-----------------------------------------------------------------*/
  1647. case OP_PLUS:
  1648. case OP_MINPLUS:
  1649. case OP_POSPLUS:
  1650. case OP_NOTPLUS:
  1651. case OP_NOTMINPLUS:
  1652. case OP_NOTPOSPLUS:
  1653. count = current_state->count; /* Already matched */
  1654. if (count > 0) { ADD_ACTIVE(state_offset + dlen + 1, 0); }
  1655. if (clen > 0)
  1656. {
  1657. unsigned int otherd = NOTACHAR;
  1658. if ((ims & PCRE_CASELESS) != 0)
  1659. {
  1660. #ifdef SUPPORT_UTF8
  1661. if (utf8 && d >= 128)
  1662. {
  1663. #ifdef SUPPORT_UCP
  1664. otherd = UCD_OTHERCASE(d);
  1665. #endif /* SUPPORT_UCP */
  1666. }
  1667. else
  1668. #endif /* SUPPORT_UTF8 */
  1669. otherd = fcc[d];
  1670. }
  1671. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  1672. {
  1673. if (count > 0 &&
  1674. (codevalue == OP_POSPLUS || codevalue == OP_NOTPOSPLUS))
  1675. {
  1676. active_count--; /* Remove non-match possibility */
  1677. next_active_state--;
  1678. }
  1679. count++;
  1680. ADD_NEW(state_offset, count);
  1681. }
  1682. }
  1683. break;
  1684. /*-----------------------------------------------------------------*/
  1685. case OP_QUERY:
  1686. case OP_MINQUERY:
  1687. case OP_POSQUERY:
  1688. case OP_NOTQUERY:
  1689. case OP_NOTMINQUERY:
  1690. case OP_NOTPOSQUERY:
  1691. ADD_ACTIVE(state_offset + dlen + 1, 0);
  1692. if (clen > 0)
  1693. {
  1694. unsigned int otherd = NOTACHAR;
  1695. if ((ims & PCRE_CASELESS) != 0)
  1696. {
  1697. #ifdef SUPPORT_UTF8
  1698. if (utf8 && d >= 128)
  1699. {
  1700. #ifdef SUPPORT_UCP
  1701. otherd = UCD_OTHERCASE(d);
  1702. #endif /* SUPPORT_UCP */
  1703. }
  1704. else
  1705. #endif /* SUPPORT_UTF8 */
  1706. otherd = fcc[d];
  1707. }
  1708. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  1709. {
  1710. if (codevalue == OP_POSQUERY || codevalue == OP_NOTPOSQUERY)
  1711. {
  1712. active_count--; /* Remove non-match possibility */
  1713. next_active_state--;
  1714. }
  1715. ADD_NEW(state_offset + dlen + 1, 0);
  1716. }
  1717. }
  1718. break;
  1719. /*-----------------------------------------------------------------*/
  1720. case OP_STAR:
  1721. case OP_MINSTAR:
  1722. case OP_POSSTAR:
  1723. case OP_NOTSTAR:
  1724. case OP_NOTMINSTAR:
  1725. case OP_NOTPOSSTAR:
  1726. ADD_ACTIVE(state_offset + dlen + 1, 0);
  1727. if (clen > 0)
  1728. {
  1729. unsigned int otherd = NOTACHAR;
  1730. if ((ims & PCRE_CASELESS) != 0)
  1731. {
  1732. #ifdef SUPPORT_UTF8
  1733. if (utf8 && d >= 128)
  1734. {
  1735. #ifdef SUPPORT_UCP
  1736. otherd = UCD_OTHERCASE(d);
  1737. #endif /* SUPPORT_UCP */
  1738. }
  1739. else
  1740. #endif /* SUPPORT_UTF8 */
  1741. otherd = fcc[d];
  1742. }
  1743. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  1744. {
  1745. if (codevalue == OP_POSSTAR || codevalue == OP_NOTPOSSTAR)
  1746. {
  1747. active_count--; /* Remove non-match possibility */
  1748. next_active_state--;
  1749. }
  1750. ADD_NEW(state_offset, 0);
  1751. }
  1752. }
  1753. break;
  1754. /*-----------------------------------------------------------------*/
  1755. case OP_EXACT:
  1756. case OP_NOTEXACT:
  1757. count = current_state->count; /* Number already matched */
  1758. if (clen > 0)
  1759. {
  1760. unsigned int otherd = NOTACHAR;
  1761. if ((ims & PCRE_CASELESS) != 0)
  1762. {
  1763. #ifdef SUPPORT_UTF8
  1764. if (utf8 && d >= 128)
  1765. {
  1766. #ifdef SUPPORT_UCP
  1767. otherd = UCD_OTHERCASE(d);
  1768. #endif /* SUPPORT_UCP */
  1769. }
  1770. else
  1771. #endif /* SUPPORT_UTF8 */
  1772. otherd = fcc[d];
  1773. }
  1774. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  1775. {
  1776. if (++count >= GET2(code, 1))
  1777. { ADD_NEW(state_offset + dlen + 3, 0); }
  1778. else
  1779. { ADD_NEW(state_offset, count); }
  1780. }
  1781. }
  1782. break;
  1783. /*-----------------------------------------------------------------*/
  1784. case OP_UPTO:
  1785. case OP_MINUPTO:
  1786. case OP_POSUPTO:
  1787. case OP_NOTUPTO:
  1788. case OP_NOTMINUPTO:
  1789. case OP_NOTPOSUPTO:
  1790. ADD_ACTIVE(state_offset + dlen + 3, 0);
  1791. count = current_state->count; /* Number already matched */
  1792. if (clen > 0)
  1793. {
  1794. unsigned int otherd = NOTACHAR;
  1795. if ((ims & PCRE_CASELESS) != 0)
  1796. {
  1797. #ifdef SUPPORT_UTF8
  1798. if (utf8 && d >= 128)
  1799. {
  1800. #ifdef SUPPORT_UCP
  1801. otherd = UCD_OTHERCASE(d);
  1802. #endif /* SUPPORT_UCP */
  1803. }
  1804. else
  1805. #endif /* SUPPORT_UTF8 */
  1806. otherd = fcc[d];
  1807. }
  1808. if ((c == d || c == otherd) == (codevalue < OP_NOTSTAR))
  1809. {
  1810. if (codevalue == OP_POSUPTO || codevalue == OP_NOTPOSUPTO)
  1811. {
  1812. active_count--; /* Remove non-match possibility */
  1813. next_active_state--;
  1814. }
  1815. if (++count >= GET2(code, 1))
  1816. { ADD_NEW(state_offset + dlen + 3, 0); }
  1817. else
  1818. { ADD_NEW(state_offset, count); }
  1819. }
  1820. }
  1821. break;
  1822. /* ========================================================================== */
  1823. /* These are the class-handling opcodes */
  1824. case OP_CLASS:
  1825. case OP_NCLASS:
  1826. case OP_XCLASS:
  1827. {
  1828. BOOL isinclass = FALSE;
  1829. int next_state_offset;
  1830. const uschar *ecode;
  1831. /* For a simple class, there is always just a 32-byte table, and we
  1832. can set isinclass from it. */
  1833. if (codevalue != OP_XCLASS)
  1834. {
  1835. ecode = code + 33;
  1836. if (clen > 0)
  1837. {
  1838. isinclass = (c > 255)? (codevalue == OP_NCLASS) :
  1839. ((code[1 + c/8] & (1 << (c&7))) != 0);
  1840. }
  1841. }
  1842. /* An extended class may have a table or a list of single characters,
  1843. ranges, or both, and it may be positive or negative. There's a
  1844. function that sorts all this out. */
  1845. else
  1846. {
  1847. ecode = code + GET(code, 1);
  1848. if (clen > 0) isinclass = _pcre_xclass(c, code + 1 + LINK_SIZE);
  1849. }
  1850. /* At this point, isinclass is set for all kinds of class, and ecode
  1851. points to the byte after the end of the class. If there is a
  1852. quantifier, this is where it will be. */
  1853. next_state_offset = ecode - start_code;
  1854. switch (*ecode)
  1855. {
  1856. case OP_CRSTAR:
  1857. case OP_CRMINSTAR:
  1858. ADD_ACTIVE(next_state_offset + 1, 0);
  1859. if (isinclass) { ADD_NEW(state_offset, 0); }
  1860. break;
  1861. case OP_CRPLUS:
  1862. case OP_CRMINPLUS:
  1863. count = current_state->count; /* Already matched */
  1864. if (count > 0) { ADD_ACTIVE(next_state_offset + 1, 0); }
  1865. if (isinclass) { count++; ADD_NEW(state_offset, count); }
  1866. break;
  1867. case OP_CRQUERY:
  1868. case OP_CRMINQUERY:
  1869. ADD_ACTIVE(next_state_offset + 1, 0);
  1870. if (isinclass) { ADD_NEW(next_state_offset + 1, 0); }
  1871. break;
  1872. case OP_CRRANGE:
  1873. case OP_CRMINRANGE:
  1874. count = current_state->count; /* Already matched */
  1875. if (count >= GET2(ecode, 1))
  1876. { ADD_ACTIVE(next_state_offset + 5, 0); }
  1877. if (isinclass)
  1878. {
  1879. int max = GET2(ecode, 3);
  1880. if (++count >= max && max != 0) /* Max 0 => no limit */
  1881. { ADD_NEW(next_state_offset + 5, 0); }
  1882. else
  1883. { ADD_NEW(state_offset, count); }
  1884. }
  1885. break;
  1886. default:
  1887. if (isinclass) { ADD_NEW(next_state_offset, 0); }
  1888. break;
  1889. }
  1890. }
  1891. break;
  1892. /* ========================================================================== */
  1893. /* These are the opcodes for fancy brackets of various kinds. We have
  1894. to use recursion in order to handle them. The "always failing" assersion
  1895. (?!) is optimised when compiling to OP_FAIL, so we have to support that,
  1896. though the other "backtracking verbs" are not supported. */
  1897. case OP_FAIL:
  1898. break;
  1899. case OP_ASSERT:
  1900. case OP_ASSERT_NOT:
  1901. case OP_ASSERTBACK:
  1902. case OP_ASSERTBACK_NOT:
  1903. {
  1904. int rc;
  1905. int local_offsets[2];
  1906. int local_workspace[1000];
  1907. const uschar *endasscode = code + GET(code, 1);
  1908. while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
  1909. rc = internal_dfa_exec(
  1910. md, /* static match data */
  1911. code, /* this subexpression's code */
  1912. ptr, /* where we currently are */
  1913. ptr - start_subject, /* start offset */
  1914. local_offsets, /* offset vector */
  1915. sizeof(local_offsets)/sizeof(int), /* size of same */
  1916. local_workspace, /* workspace vector */
  1917. sizeof(local_workspace)/sizeof(int), /* size of same */
  1918. ims, /* the current ims flags */
  1919. rlevel, /* function recursion level */
  1920. recursing); /* pass on regex recursion */
  1921. if ((rc >= 0) == (codevalue == OP_ASSERT || codevalue == OP_ASSERTBACK))
  1922. { ADD_ACTIVE(endasscode + LINK_SIZE + 1 - start_code, 0); }
  1923. }
  1924. break;
  1925. /*-----------------------------------------------------------------*/
  1926. case OP_COND:
  1927. case OP_SCOND:
  1928. {
  1929. int local_offsets[1000];
  1930. int local_workspace[1000];
  1931. int condcode = code[LINK_SIZE+1];
  1932. /* Back reference conditions are not supported */
  1933. if (condcode == OP_CREF) return PCRE_ERROR_DFA_UCOND;
  1934. /* The DEFINE condition is always false */
  1935. if (condcode == OP_DEF)
  1936. {
  1937. ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0);
  1938. }
  1939. /* The only supported version of OP_RREF is for the value RREF_ANY,
  1940. which means "test if in any recursion". We can't test for specifically
  1941. recursed groups. */
  1942. else if (condcode == OP_RREF)
  1943. {
  1944. int value = GET2(code, LINK_SIZE+2);
  1945. if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;
  1946. if (recursing > 0) { ADD_ACTIVE(state_offset + LINK_SIZE + 4, 0); }
  1947. else { ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0); }
  1948. }
  1949. /* Otherwise, the condition is an assertion */
  1950. else
  1951. {
  1952. int rc;
  1953. const uschar *asscode = code + LINK_SIZE + 1;
  1954. const uschar *endasscode = asscode + GET(asscode, 1);
  1955. while (*endasscode == OP_ALT) endasscode += GET(endasscode, 1);
  1956. rc = internal_dfa_exec(
  1957. md, /* fixed match data */
  1958. asscode, /* this subexpression's code */
  1959. ptr, /* where we currently are */
  1960. ptr - start_subject, /* start offset */
  1961. local_offsets, /* offset vector */
  1962. sizeof(local_offsets)/sizeof(int), /* size of same */
  1963. local_workspace, /* workspace vector */
  1964. sizeof(local_workspace)/sizeof(int), /* size of same */
  1965. ims, /* the current ims flags */
  1966. rlevel, /* function recursion level */
  1967. recursing); /* pass on regex recursion */
  1968. if ((rc >= 0) ==
  1969. (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))
  1970. { ADD_ACTIVE(endasscode + LINK_SIZE + 1 - start_code, 0); }
  1971. else
  1972. { ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0); }
  1973. }
  1974. }
  1975. break;
  1976. /*-----------------------------------------------------------------*/
  1977. case OP_RECURSE:
  1978. {
  1979. int local_offsets[1000];
  1980. int local_workspace[1000];
  1981. int rc;
  1982. DPRINTF(("%.*sStarting regex recursion %d\n", rlevel*2-2, SP,
  1983. recursing + 1));
  1984. rc = internal_dfa_exec(
  1985. md, /* fixed match data */
  1986. start_code + GET(code, 1), /* this subexpression's code */
  1987. ptr, /* where we currently are */
  1988. ptr - start_subject, /* start offset */
  1989. local_offsets, /* offset vector */
  1990. sizeof(local_offsets)/sizeof(int), /* size of same */
  1991. local_workspace, /* workspace vector */
  1992. sizeof(local_workspace)/sizeof(int), /* size of same */
  1993. ims, /* the current ims flags */
  1994. rlevel, /* function recursion level */
  1995. recursing + 1); /* regex recurse level */
  1996. DPRINTF(("%.*sReturn from regex recursion %d: rc=%d\n", rlevel*2-2, SP,
  1997. recursing + 1, rc));
  1998. /* Ran out of internal offsets */
  1999. if (rc == 0) return PCRE_ERROR_DFA_RECURSE;
  2000. /* For each successful matched substring, set up the next state with a
  2001. count of characters to skip before trying it. Note that the count is in
  2002. characters, not bytes. */
  2003. if (rc > 0)
  2004. {
  2005. for (rc = rc*2 - 2; rc >= 0; rc -= 2)
  2006. {
  2007. const uschar *p = start_subject + local_offsets[rc];
  2008. const uschar *pp = start_subject + local_offsets[rc+1];
  2009. int charcount = local_offsets[rc+1] - local_offsets[rc];
  2010. while (p < pp) if ((*p++ & 0xc0) == 0x80) charcount--;
  2011. if (charcount > 0)
  2012. {
  2013. ADD_NEW_DATA(-(state_offset + LINK_SIZE + 1), 0, (charcount - 1));
  2014. }
  2015. else
  2016. {
  2017. ADD_ACTIVE(state_offset + LINK_SIZE + 1, 0);
  2018. }
  2019. }
  2020. }
  2021. else if (rc != PCRE_ERROR_NOMATCH) return rc;
  2022. }
  2023. break;
  2024. /*-----------------------------------------------------------------*/
  2025. case OP_ONCE:
  2026. {
  2027. int local_offsets[2];
  2028. int local_workspace[1000];
  2029. int rc = internal_dfa_exec(
  2030. md, /* fixed match data */
  2031. code, /* this subexpression's code */
  2032. ptr, /* where we currently are */
  2033. ptr - start_subject, /* start offset */
  2034. local_offsets, /* offset vector */
  2035. sizeof(local_offsets)/sizeof(int), /* size of same */
  2036. local_workspace, /* workspace vector */
  2037. sizeof(local_workspace)/sizeof(int), /* size of same */
  2038. ims, /* the current ims flags */
  2039. rlevel, /* function recursion level */
  2040. recursing); /* pass on regex recursion */
  2041. if (rc >= 0)
  2042. {
  2043. const uschar *end_subpattern = code;
  2044. int charcount = local_offsets[1] - local_offsets[0];
  2045. int next_state_offset, repeat_state_offset;
  2046. do { end_subpattern += GET(end_subpattern, 1); }
  2047. while (*end_subpattern == OP_ALT);
  2048. next_state_offset = end_subpattern - start_code + LINK_SIZE + 1;
  2049. /* If the end of this subpattern is KETRMAX or KETRMIN, we must
  2050. arrange for the repeat state also to be added to the relevant list.
  2051. Calculate the offset, or set -1 for no repeat. */
  2052. repeat_state_offset = (*end_subpattern == OP_KETRMAX ||
  2053. *end_subpattern == OP_KETRMIN)?
  2054. end_subpattern - start_code - GET(end_subpattern, 1) : -1;
  2055. /* If we have matched an empty string, add the next state at the
  2056. current character pointer. This is important so that the duplicate
  2057. checking kicks in, which is what breaks infinite loops that match an
  2058. empty string. */
  2059. if (charcount == 0)
  2060. {
  2061. ADD_ACTIVE(next_state_offset, 0);
  2062. }
  2063. /* Optimization: if there are no more active states, and there
  2064. are no new states yet set up, then skip over the subject string
  2065. right here, to save looping. Otherwise, set up the new state to swing
  2066. into action when the end of the substring is reached. */
  2067. else if (i + 1 >= active_count && new_count == 0)
  2068. {
  2069. ptr += charcount;
  2070. clen = 0;
  2071. ADD_NEW(next_state_offset, 0);
  2072. /* If we are adding a repeat state at the new character position,
  2073. we must fudge things so that it is the only current state.
  2074. Otherwise, it might be a duplicate of one we processed before, and
  2075. that would cause it to be skipped. */
  2076. if (repeat_state_offset >= 0)
  2077. {
  2078. next_active_state = active_states;
  2079. active_count = 0;
  2080. i = -1;
  2081. ADD_ACTIVE(repeat_state_offset, 0);
  2082. }
  2083. }
  2084. else
  2085. {
  2086. const uschar *p = start_subject + local_offsets[0];
  2087. const uschar *pp = start_subject + local_offsets[1];
  2088. while (p < pp) if ((*p++ & 0xc0) == 0x80) charcount--;
  2089. ADD_NEW_DATA(-next_state_offset, 0, (charcount - 1));
  2090. if (repeat_state_offset >= 0)
  2091. { ADD_NEW_DATA(-repeat_state_offset, 0, (charcount - 1)); }
  2092. }
  2093. }
  2094. else if (rc != PCRE_ERROR_NOMATCH) return rc;
  2095. }
  2096. break;
  2097. /* ========================================================================== */
  2098. /* Handle callouts */
  2099. case OP_CALLOUT:
  2100. if (pcre_callout != NULL)
  2101. {
  2102. int rrc;
  2103. pcre_callout_block cb;
  2104. cb.version = 1; /* Version 1 of the callout block */
  2105. cb.callout_number = code[1];
  2106. cb.offset_vector = offsets;
  2107. cb.subject = (PCRE_SPTR)start_subject;
  2108. cb.subject_length = end_subject - start_subject;
  2109. cb.start_match = current_subject - start_subject;
  2110. cb.current_position = ptr - start_subject;
  2111. cb.pattern_position = GET(code, 2);
  2112. cb.next_item_length = GET(code, 2 + LINK_SIZE);
  2113. cb.capture_top = 1;
  2114. cb.capture_last = -1;
  2115. cb.callout_data = md->callout_data;
  2116. if ((rrc = (*pcre_callout)(&cb)) < 0) return rrc; /* Abandon */
  2117. if (rrc == 0) { ADD_ACTIVE(state_offset + 2 + 2*LINK_SIZE, 0); }
  2118. }
  2119. break;
  2120. /* ========================================================================== */
  2121. default: /* Unsupported opcode */
  2122. return PCRE_ERROR_DFA_UITEM;
  2123. }
  2124. NEXT_ACTIVE_STATE: continue;
  2125. } /* End of loop scanning active states */
  2126. /* We have finished the processing at the current subject character. If no
  2127. new states have been set for the next character, we have found all the
  2128. matches that we are going to find. If we are at the top level and partial
  2129. matching has been requested, check for appropriate conditions. */
  2130. if (new_count <= 0)
  2131. {
  2132. if (match_count < 0 && /* No matches found */
  2133. rlevel == 1 && /* Top level match function */
  2134. (md->moptions & PCRE_PARTIAL) != 0 && /* Want partial matching */
  2135. ptr >= end_subject && /* Reached end of subject */
  2136. ptr > current_subject) /* Matched non-empty string */
  2137. {
  2138. if (offsetcount >= 2)
  2139. {
  2140. offsets[0] = current_subject - start_subject;
  2141. offsets[1] = end_subject - start_subject;
  2142. }
  2143. match_count = PCRE_ERROR_PARTIAL;
  2144. }
  2145. DPRINTF(("%.*sEnd of internal_dfa_exec %d: returning %d\n"
  2146. "%.*s---------------------\n\n", rlevel*2-2, SP, rlevel, match_count,
  2147. rlevel*2-2, SP));
  2148. break; /* In effect, "return", but see the comment below */
  2149. }
  2150. /* One or more states are active for the next character. */
  2151. ptr += clen; /* Advance to next subject character */
  2152. } /* Loop to move along the subject string */
  2153. /* Control gets here from "break" a few lines above. We do it this way because
  2154. if we use "return" above, we have compiler trouble. Some compilers warn if
  2155. there's nothing here because they think the function doesn't return a value. On
  2156. the other hand, if we put a dummy statement here, some more clever compilers
  2157. complain that it can't be reached. Sigh. */
  2158. return match_count;
  2159. }
  2160. /*************************************************
  2161. * Execute a Regular Expression - DFA engine *
  2162. *************************************************/
  2163. /* This external function applies a compiled re to a subject string using a DFA
  2164. engine. This function calls the internal function multiple times if the pattern
  2165. is not anchored.
  2166. Arguments:
  2167. argument_re points to the compiled expression
  2168. extra_data points to extra data or is NULL
  2169. subject points to the subject string
  2170. length length of subject string (may contain binary zeros)
  2171. start_offset where to start in the subject string
  2172. options option bits
  2173. offsets vector of match offsets
  2174. offsetcount size of same
  2175. workspace workspace vector
  2176. wscount size of same
  2177. Returns: > 0 => number of match offset pairs placed in offsets
  2178. = 0 => offsets overflowed; longest matches are present
  2179. -1 => failed to match
  2180. < -1 => some kind of unexpected problem
  2181. */
  2182. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  2183. pcre_dfa_exec(const pcre *argument_re, const pcre_extra *extra_data,
  2184. const char *subject, int length, int start_offset, int options, int *offsets,
  2185. int offsetcount, int *workspace, int wscount)
  2186. {
  2187. real_pcre *re = (real_pcre *)argument_re;
  2188. dfa_match_data match_block;
  2189. dfa_match_data *md = &match_block;
  2190. BOOL utf8, anchored, startline, firstline;
  2191. const uschar *current_subject, *end_subject, *lcc;
  2192. pcre_study_data internal_study;
  2193. const pcre_study_data *study = NULL;
  2194. real_pcre internal_re;
  2195. const uschar *req_byte_ptr;
  2196. const uschar *start_bits = NULL;
  2197. BOOL first_byte_caseless = FALSE;
  2198. BOOL req_byte_caseless = FALSE;
  2199. int first_byte = -1;
  2200. int req_byte = -1;
  2201. int req_byte2 = -1;
  2202. int newline;
  2203. /* Plausibility checks */
  2204. if ((options & ~PUBLIC_DFA_EXEC_OPTIONS) != 0) return PCRE_ERROR_BADOPTION;
  2205. if (re == NULL || subject == NULL || workspace == NULL ||
  2206. (offsets == NULL && offsetcount > 0)) return PCRE_ERROR_NULL;
  2207. if (offsetcount < 0) return PCRE_ERROR_BADCOUNT;
  2208. if (wscount < 20) return PCRE_ERROR_DFA_WSSIZE;
  2209. /* We need to find the pointer to any study data before we test for byte
  2210. flipping, so we scan the extra_data block first. This may set two fields in the
  2211. match block, so we must initialize them beforehand. However, the other fields
  2212. in the match block must not be set until after the byte flipping. */
  2213. md->tables = re->tables;
  2214. md->callout_data = NULL;
  2215. if (extra_data != NULL)
  2216. {
  2217. unsigned int flags = extra_data->flags;
  2218. if ((flags & PCRE_EXTRA_STUDY_DATA) != 0)
  2219. study = (const pcre_study_data *)extra_data->study_data;
  2220. if ((flags & PCRE_EXTRA_MATCH_LIMIT) != 0) return PCRE_ERROR_DFA_UMLIMIT;
  2221. if ((flags & PCRE_EXTRA_MATCH_LIMIT_RECURSION) != 0)
  2222. return PCRE_ERROR_DFA_UMLIMIT;
  2223. if ((flags & PCRE_EXTRA_CALLOUT_DATA) != 0)
  2224. md->callout_data = extra_data->callout_data;
  2225. if ((flags & PCRE_EXTRA_TABLES) != 0)
  2226. md->tables = extra_data->tables;
  2227. }
  2228. /* Check that the first field in the block is the magic number. If it is not,
  2229. test for a regex that was compiled on a host of opposite endianness. If this is
  2230. the case, flipped values are put in internal_re and internal_study if there was
  2231. study data too. */
  2232. if (re->magic_number != MAGIC_NUMBER)
  2233. {
  2234. re = _pcre_try_flipped(re, &internal_re, study, &internal_study);
  2235. if (re == NULL) return PCRE_ERROR_BADMAGIC;
  2236. if (study != NULL) study = &internal_study;
  2237. }
  2238. /* Set some local values */
  2239. current_subject = (const unsigned char *)subject + start_offset;
  2240. end_subject = (const unsigned char *)subject + length;
  2241. req_byte_ptr = current_subject - 1;
  2242. #ifdef SUPPORT_UTF8
  2243. utf8 = (re->options & PCRE_UTF8) != 0;
  2244. #else
  2245. utf8 = FALSE;
  2246. #endif
  2247. anchored = (options & (PCRE_ANCHORED|PCRE_DFA_RESTART)) != 0 ||
  2248. (re->options & PCRE_ANCHORED) != 0;
  2249. /* The remaining fixed data for passing around. */
  2250. md->start_code = (const uschar *)argument_re +
  2251. re->name_table_offset + re->name_count * re->name_entry_size;
  2252. md->start_subject = (const unsigned char *)subject;
  2253. md->end_subject = end_subject;
  2254. md->moptions = options;
  2255. md->poptions = re->options;
  2256. /* If the BSR option is not set at match time, copy what was set
  2257. at compile time. */
  2258. if ((md->moptions & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) == 0)
  2259. {
  2260. if ((re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) != 0)
  2261. md->moptions |= re->options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE);
  2262. #ifdef BSR_ANYCRLF
  2263. else md->moptions |= PCRE_BSR_ANYCRLF;
  2264. #endif
  2265. }
  2266. /* Handle different types of newline. The three bits give eight cases. If
  2267. nothing is set at run time, whatever was used at compile time applies. */
  2268. switch ((((options & PCRE_NEWLINE_BITS) == 0)? re->options : (pcre_uint32)options) &
  2269. PCRE_NEWLINE_BITS)
  2270. {
  2271. case 0: newline = NEWLINE; break; /* Compile-time default */
  2272. case PCRE_NEWLINE_CR: newline = '\r'; break;
  2273. case PCRE_NEWLINE_LF: newline = '\n'; break;
  2274. case PCRE_NEWLINE_CR+
  2275. PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break;
  2276. case PCRE_NEWLINE_ANY: newline = -1; break;
  2277. case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
  2278. default: return PCRE_ERROR_BADNEWLINE;
  2279. }
  2280. if (newline == -2)
  2281. {
  2282. md->nltype = NLTYPE_ANYCRLF;
  2283. }
  2284. else if (newline < 0)
  2285. {
  2286. md->nltype = NLTYPE_ANY;
  2287. }
  2288. else
  2289. {
  2290. md->nltype = NLTYPE_FIXED;
  2291. if (newline > 255)
  2292. {
  2293. md->nllen = 2;
  2294. md->nl[0] = (newline >> 8) & 255;
  2295. md->nl[1] = newline & 255;
  2296. }
  2297. else
  2298. {
  2299. md->nllen = 1;
  2300. md->nl[0] = newline;
  2301. }
  2302. }
  2303. /* Check a UTF-8 string if required. Unfortunately there's no way of passing
  2304. back the character offset. */
  2305. #ifdef SUPPORT_UTF8
  2306. if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0)
  2307. {
  2308. if (_pcre_valid_utf8((uschar *)subject, length) >= 0)
  2309. return PCRE_ERROR_BADUTF8;
  2310. if (start_offset > 0 && start_offset < length)
  2311. {
  2312. int tb = ((uschar *)subject)[start_offset];
  2313. if (tb > 127)
  2314. {
  2315. tb &= 0xc0;
  2316. if (tb != 0 && tb != 0xc0) return PCRE_ERROR_BADUTF8_OFFSET;
  2317. }
  2318. }
  2319. }
  2320. #endif
  2321. /* If the exec call supplied NULL for tables, use the inbuilt ones. This
  2322. is a feature that makes it possible to save compiled regex and re-use them
  2323. in other programs later. */
  2324. if (md->tables == NULL) md->tables = _pcre_default_tables;
  2325. /* The lower casing table and the "must be at the start of a line" flag are
  2326. used in a loop when finding where to start. */
  2327. lcc = md->tables + lcc_offset;
  2328. startline = (re->flags & PCRE_STARTLINE) != 0;
  2329. firstline = (re->options & PCRE_FIRSTLINE) != 0;
  2330. /* Set up the first character to match, if available. The first_byte value is
  2331. never set for an anchored regular expression, but the anchoring may be forced
  2332. at run time, so we have to test for anchoring. The first char may be unset for
  2333. an unanchored pattern, of course. If there's no first char and the pattern was
  2334. studied, there may be a bitmap of possible first characters. */
  2335. if (!anchored)
  2336. {
  2337. if ((re->flags & PCRE_FIRSTSET) != 0)
  2338. {
  2339. first_byte = re->first_byte & 255;
  2340. if ((first_byte_caseless = ((re->first_byte & REQ_CASELESS) != 0)) == TRUE)
  2341. first_byte = lcc[first_byte];
  2342. }
  2343. else
  2344. {
  2345. if (startline && study != NULL &&
  2346. (study->options & PCRE_STUDY_MAPPED) != 0)
  2347. start_bits = study->start_bits;
  2348. }
  2349. }
  2350. /* For anchored or unanchored matches, there may be a "last known required
  2351. character" set. */
  2352. if ((re->flags & PCRE_REQCHSET) != 0)
  2353. {
  2354. req_byte = re->req_byte & 255;
  2355. req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0;
  2356. req_byte2 = (md->tables + fcc_offset)[req_byte]; /* case flipped */
  2357. }
  2358. /* Call the main matching function, looping for a non-anchored regex after a
  2359. failed match. Unless restarting, optimize by moving to the first match
  2360. character if possible, when not anchored. Then unless wanting a partial match,
  2361. check for a required later character. */
  2362. for (;;)
  2363. {
  2364. int rc;
  2365. if ((options & PCRE_DFA_RESTART) == 0)
  2366. {
  2367. const uschar *save_end_subject = end_subject;
  2368. /* Advance to a unique first char if possible. If firstline is TRUE, the
  2369. start of the match is constrained to the first line of a multiline string.
  2370. Implement this by temporarily adjusting end_subject so that we stop
  2371. scanning at a newline. If the match fails at the newline, later code breaks
  2372. this loop. */
  2373. if (firstline)
  2374. {
  2375. USPTR t = current_subject;
  2376. #ifdef SUPPORT_UTF8
  2377. if (utf8)
  2378. {
  2379. while (t < md->end_subject && !IS_NEWLINE(t))
  2380. {
  2381. t++;
  2382. while (t < end_subject && (*t & 0xc0) == 0x80) t++;
  2383. }
  2384. }
  2385. else
  2386. #endif
  2387. while (t < md->end_subject && !IS_NEWLINE(t)) t++;
  2388. end_subject = t;
  2389. }
  2390. if (first_byte >= 0)
  2391. {
  2392. if (first_byte_caseless)
  2393. while (current_subject < end_subject &&
  2394. lcc[*current_subject] != first_byte)
  2395. current_subject++;
  2396. else
  2397. while (current_subject < end_subject && *current_subject != first_byte)
  2398. current_subject++;
  2399. }
  2400. /* Or to just after a linebreak for a multiline match if possible */
  2401. else if (startline)
  2402. {
  2403. if (current_subject > md->start_subject + start_offset)
  2404. {
  2405. #ifdef SUPPORT_UTF8
  2406. if (utf8)
  2407. {
  2408. while (current_subject < end_subject && !WAS_NEWLINE(current_subject))
  2409. {
  2410. current_subject++;
  2411. while(current_subject < end_subject &&
  2412. (*current_subject & 0xc0) == 0x80)
  2413. current_subject++;
  2414. }
  2415. }
  2416. else
  2417. #endif
  2418. while (current_subject < end_subject && !WAS_NEWLINE(current_subject))
  2419. current_subject++;
  2420. /* If we have just passed a CR and the newline option is ANY or
  2421. ANYCRLF, and we are now at a LF, advance the match position by one more
  2422. character. */
  2423. if (current_subject[-1] == '\r' &&
  2424. (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&
  2425. current_subject < end_subject &&
  2426. *current_subject == '\n')
  2427. current_subject++;
  2428. }
  2429. }
  2430. /* Or to a non-unique first char after study */
  2431. else if (start_bits != NULL)
  2432. {
  2433. while (current_subject < end_subject)
  2434. {
  2435. register unsigned int c = *current_subject;
  2436. if ((start_bits[c/8] & (1 << (c&7))) == 0) current_subject++;
  2437. else break;
  2438. }
  2439. }
  2440. /* Restore fudged end_subject */
  2441. end_subject = save_end_subject;
  2442. }
  2443. /* If req_byte is set, we know that that character must appear in the subject
  2444. for the match to succeed. If the first character is set, req_byte must be
  2445. later in the subject; otherwise the test starts at the match point. This
  2446. optimization can save a huge amount of work in patterns with nested unlimited
  2447. repeats that aren't going to match. Writing separate code for cased/caseless
  2448. versions makes it go faster, as does using an autoincrement and backing off
  2449. on a match.
  2450. HOWEVER: when the subject string is very, very long, searching to its end can
  2451. take a long time, and give bad performance on quite ordinary patterns. This
  2452. showed up when somebody was matching /^C/ on a 32-megabyte string... so we
  2453. don't do this when the string is sufficiently long.
  2454. ALSO: this processing is disabled when partial matching is requested.
  2455. */
  2456. if (req_byte >= 0 &&
  2457. end_subject - current_subject < REQ_BYTE_MAX &&
  2458. (options & PCRE_PARTIAL) == 0)
  2459. {
  2460. register const uschar *p = current_subject + ((first_byte >= 0)? 1 : 0);
  2461. /* We don't need to repeat the search if we haven't yet reached the
  2462. place we found it at last time. */
  2463. if (p > req_byte_ptr)
  2464. {
  2465. if (req_byte_caseless)
  2466. {
  2467. while (p < end_subject)
  2468. {
  2469. register int pp = *p++;
  2470. if (pp == req_byte || pp == req_byte2) { p--; break; }
  2471. }
  2472. }
  2473. else
  2474. {
  2475. while (p < end_subject)
  2476. {
  2477. if (*p++ == req_byte) { p--; break; }
  2478. }
  2479. }
  2480. /* If we can't find the required character, break the matching loop,
  2481. which will cause a return or PCRE_ERROR_NOMATCH. */
  2482. if (p >= end_subject) break;
  2483. /* If we have found the required character, save the point where we
  2484. found it, so that we don't search again next time round the loop if
  2485. the start hasn't passed this character yet. */
  2486. req_byte_ptr = p;
  2487. }
  2488. }
  2489. /* OK, now we can do the business */
  2490. rc = internal_dfa_exec(
  2491. md, /* fixed match data */
  2492. md->start_code, /* this subexpression's code */
  2493. current_subject, /* where we currently are */
  2494. start_offset, /* start offset in subject */
  2495. offsets, /* offset vector */
  2496. offsetcount, /* size of same */
  2497. workspace, /* workspace vector */
  2498. wscount, /* size of same */
  2499. re->options & (PCRE_CASELESS|PCRE_MULTILINE|PCRE_DOTALL), /* ims flags */
  2500. 0, /* function recurse level */
  2501. 0); /* regex recurse level */
  2502. /* Anything other than "no match" means we are done, always; otherwise, carry
  2503. on only if not anchored. */
  2504. if (rc != PCRE_ERROR_NOMATCH || anchored) return rc;
  2505. /* Advance to the next subject character unless we are at the end of a line
  2506. and firstline is set. */
  2507. if (firstline && IS_NEWLINE(current_subject)) break;
  2508. current_subject++;
  2509. if (utf8)
  2510. {
  2511. while (current_subject < end_subject && (*current_subject & 0xc0) == 0x80)
  2512. current_subject++;
  2513. }
  2514. if (current_subject > end_subject) break;
  2515. /* If we have just passed a CR and we are now at a LF, and the pattern does
  2516. not contain any explicit matches for \r or \n, and the newline option is CRLF
  2517. or ANY or ANYCRLF, advance the match position by one more character. */
  2518. if (current_subject[-1] == '\r' &&
  2519. current_subject < end_subject &&
  2520. *current_subject == '\n' &&
  2521. (re->flags & PCRE_HASCRORLF) == 0 &&
  2522. (md->nltype == NLTYPE_ANY ||
  2523. md->nltype == NLTYPE_ANYCRLF ||
  2524. md->nllen == 2))
  2525. current_subject++;
  2526. } /* "Bumpalong" loop */
  2527. return PCRE_ERROR_NOMATCH;
  2528. }
  2529. /* End of pcre_dfa_exec.c */