/vendor/pcre/pcre_dfa_exec.c

http://github.com/feyeleanor/RubyGoLightly · C · 2920 lines · 2135 code · 374 blank · 411 comment · 728 complexity · 5a75071cbd89cdc47950d9729812e6fe MD5 · raw file

Large files are truncated click here to view the full file

  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Copyright (c) 1997-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. {