PageRenderTime 52ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/gnu/lib/libregex/regexec.c

https://bitbucket.org/freebsd/freebsd-head/
C | 4327 lines | 3462 code | 350 blank | 515 comment | 1042 complexity | 43689404ddbad7b8105fa2e37474affa MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0

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

  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. static reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags,
  18. int n) internal_function;
  19. static void match_ctx_clean (re_match_context_t *mctx) internal_function;
  20. static void match_ctx_free (re_match_context_t *cache) internal_function;
  21. static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, int node,
  22. int str_idx, int from, int to)
  23. internal_function;
  24. static int search_cur_bkref_entry (re_match_context_t *mctx, int str_idx)
  25. internal_function;
  26. static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, int node,
  27. int str_idx) internal_function;
  28. static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop,
  29. int node, int str_idx)
  30. internal_function;
  31. static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
  32. re_dfastate_t **limited_sts, int last_node,
  33. int last_str_idx)
  34. internal_function;
  35. static reg_errcode_t re_search_internal (const regex_t *preg,
  36. const char *string, int length,
  37. int start, int range, int stop,
  38. size_t nmatch, regmatch_t pmatch[],
  39. int eflags) internal_function;
  40. static int re_search_2_stub (struct re_pattern_buffer *bufp,
  41. const char *string1, int length1,
  42. const char *string2, int length2,
  43. int start, int range, struct re_registers *regs,
  44. int stop, int ret_len) internal_function;
  45. static int re_search_stub (struct re_pattern_buffer *bufp,
  46. const char *string, int length, int start,
  47. int range, int stop, struct re_registers *regs,
  48. int ret_len) internal_function;
  49. static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
  50. int nregs, int regs_allocated) internal_function;
  51. static inline re_dfastate_t *acquire_init_state_context
  52. (reg_errcode_t *err, const re_match_context_t *mctx, int idx)
  53. __attribute ((always_inline)) internal_function;
  54. static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx)
  55. internal_function;
  56. static int check_matching (re_match_context_t *mctx, int fl_longest_match,
  57. int *p_match_first)
  58. internal_function;
  59. static int check_halt_node_context (const re_dfa_t *dfa, int node,
  60. unsigned int context) internal_function;
  61. static int check_halt_state_context (const re_match_context_t *mctx,
  62. const re_dfastate_t *state, int idx)
  63. internal_function;
  64. static void update_regs (re_dfa_t *dfa, regmatch_t *pmatch,
  65. regmatch_t *prev_idx_match, int cur_node,
  66. int cur_idx, int nmatch) internal_function;
  67. static int proceed_next_node (const re_match_context_t *mctx,
  68. int nregs, regmatch_t *regs,
  69. int *pidx, int node, re_node_set *eps_via_nodes,
  70. struct re_fail_stack_t *fs) internal_function;
  71. static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
  72. int str_idx, int dest_node, int nregs,
  73. regmatch_t *regs,
  74. re_node_set *eps_via_nodes) internal_function;
  75. static int pop_fail_stack (struct re_fail_stack_t *fs, int *pidx, int nregs,
  76. regmatch_t *regs, re_node_set *eps_via_nodes) internal_function;
  77. static reg_errcode_t set_regs (const regex_t *preg,
  78. const re_match_context_t *mctx,
  79. size_t nmatch, regmatch_t *pmatch,
  80. int fl_backtrack) internal_function;
  81. static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs) internal_function;
  82. #ifdef RE_ENABLE_I18N
  83. static int sift_states_iter_mb (const re_match_context_t *mctx,
  84. re_sift_context_t *sctx,
  85. int node_idx, int str_idx, int max_str_idx) internal_function;
  86. #endif /* RE_ENABLE_I18N */
  87. static reg_errcode_t sift_states_backward (re_match_context_t *mctx,
  88. re_sift_context_t *sctx) internal_function;
  89. static reg_errcode_t build_sifted_states (re_match_context_t *mctx,
  90. re_sift_context_t *sctx, int str_idx,
  91. re_node_set *cur_dest) internal_function;
  92. static reg_errcode_t update_cur_sifted_state (re_match_context_t *mctx,
  93. re_sift_context_t *sctx,
  94. int str_idx,
  95. re_node_set *dest_nodes) internal_function;
  96. static reg_errcode_t add_epsilon_src_nodes (re_dfa_t *dfa,
  97. re_node_set *dest_nodes,
  98. const re_node_set *candidates) internal_function;
  99. static reg_errcode_t sub_epsilon_src_nodes (re_dfa_t *dfa, int node,
  100. re_node_set *dest_nodes,
  101. const re_node_set *and_nodes) internal_function;
  102. static int check_dst_limits (re_match_context_t *mctx, re_node_set *limits,
  103. int dst_node, int dst_idx, int src_node,
  104. int src_idx) internal_function;
  105. static int check_dst_limits_calc_pos_1 (re_match_context_t *mctx,
  106. int boundaries, int subexp_idx,
  107. int from_node, int bkref_idx) internal_function;
  108. static int check_dst_limits_calc_pos (re_match_context_t *mctx,
  109. int limit, int subexp_idx,
  110. int node, int str_idx,
  111. int bkref_idx) internal_function;
  112. static reg_errcode_t check_subexp_limits (re_dfa_t *dfa,
  113. re_node_set *dest_nodes,
  114. const re_node_set *candidates,
  115. re_node_set *limits,
  116. struct re_backref_cache_entry *bkref_ents,
  117. int str_idx) internal_function;
  118. static reg_errcode_t sift_states_bkref (re_match_context_t *mctx,
  119. re_sift_context_t *sctx,
  120. int str_idx, const re_node_set *candidates) internal_function;
  121. static reg_errcode_t clean_state_log_if_needed (re_match_context_t *mctx,
  122. int next_state_log_idx) internal_function;
  123. static reg_errcode_t merge_state_array (re_dfa_t *dfa, re_dfastate_t **dst,
  124. re_dfastate_t **src, int num) internal_function;
  125. static re_dfastate_t *find_recover_state (reg_errcode_t *err,
  126. re_match_context_t *mctx) internal_function;
  127. static re_dfastate_t *transit_state (reg_errcode_t *err,
  128. re_match_context_t *mctx,
  129. re_dfastate_t *state) internal_function;
  130. static re_dfastate_t *merge_state_with_log (reg_errcode_t *err,
  131. re_match_context_t *mctx,
  132. re_dfastate_t *next_state) internal_function;
  133. static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
  134. re_node_set *cur_nodes,
  135. int str_idx) internal_function;
  136. #if 0
  137. static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
  138. re_match_context_t *mctx,
  139. re_dfastate_t *pstate) internal_function;
  140. #endif
  141. #ifdef RE_ENABLE_I18N
  142. static reg_errcode_t transit_state_mb (re_match_context_t *mctx,
  143. re_dfastate_t *pstate) internal_function;
  144. #endif /* RE_ENABLE_I18N */
  145. static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
  146. const re_node_set *nodes) internal_function;
  147. static reg_errcode_t get_subexp (re_match_context_t *mctx,
  148. int bkref_node, int bkref_str_idx) internal_function;
  149. static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
  150. const re_sub_match_top_t *sub_top,
  151. re_sub_match_last_t *sub_last,
  152. int bkref_node, int bkref_str) internal_function;
  153. static int find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
  154. int subexp_idx, int type) internal_function;
  155. static reg_errcode_t check_arrival (re_match_context_t *mctx,
  156. state_array_t *path, int top_node,
  157. int top_str, int last_node, int last_str,
  158. int type) internal_function;
  159. static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx,
  160. int str_idx,
  161. re_node_set *cur_nodes,
  162. re_node_set *next_nodes) internal_function;
  163. static reg_errcode_t check_arrival_expand_ecl (re_dfa_t *dfa,
  164. re_node_set *cur_nodes,
  165. int ex_subexp, int type) internal_function;
  166. static reg_errcode_t check_arrival_expand_ecl_sub (re_dfa_t *dfa,
  167. re_node_set *dst_nodes,
  168. int target, int ex_subexp,
  169. int type) internal_function;
  170. static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
  171. re_node_set *cur_nodes, int cur_str,
  172. int subexp_num, int type) internal_function;
  173. static int build_trtable (re_dfa_t *dfa,
  174. re_dfastate_t *state) internal_function;
  175. #ifdef RE_ENABLE_I18N
  176. static int check_node_accept_bytes (re_dfa_t *dfa, int node_idx,
  177. const re_string_t *input, int idx) internal_function;
  178. # ifdef _LIBC
  179. static unsigned int find_collation_sequence_value (const unsigned char *mbs,
  180. size_t name_len) internal_function;
  181. # endif /* _LIBC */
  182. #endif /* RE_ENABLE_I18N */
  183. static int group_nodes_into_DFAstates (re_dfa_t *dfa,
  184. const re_dfastate_t *state,
  185. re_node_set *states_node,
  186. bitset *states_ch) internal_function;
  187. static int check_node_accept (const re_match_context_t *mctx,
  188. const re_token_t *node, int idx) internal_function;
  189. static reg_errcode_t extend_buffers (re_match_context_t *mctx) internal_function;
  190. /* Entry point for POSIX code. */
  191. /* regexec searches for a given pattern, specified by PREG, in the
  192. string STRING.
  193. If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  194. `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
  195. least NMATCH elements, and we set them to the offsets of the
  196. corresponding matched substrings.
  197. EFLAGS specifies `execution flags' which affect matching: if
  198. REG_NOTBOL is set, then ^ does not match at the beginning of the
  199. string; if REG_NOTEOL is set, then $ does not match at the end.
  200. We return 0 if we find a match and REG_NOMATCH if not. */
  201. int
  202. regexec (preg, string, nmatch, pmatch, eflags)
  203. const regex_t *__restrict preg;
  204. const char *__restrict string;
  205. size_t nmatch;
  206. regmatch_t pmatch[];
  207. int eflags;
  208. {
  209. reg_errcode_t err;
  210. int start, length;
  211. if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND))
  212. return REG_BADPAT;
  213. if (eflags & REG_STARTEND)
  214. {
  215. start = pmatch[0].rm_so;
  216. length = pmatch[0].rm_eo;
  217. }
  218. else
  219. {
  220. start = 0;
  221. length = strlen (string);
  222. }
  223. if (preg->no_sub)
  224. err = re_search_internal (preg, string, length, start, length - start,
  225. length, 0, NULL, eflags);
  226. else
  227. err = re_search_internal (preg, string, length, start, length - start,
  228. length, nmatch, pmatch, eflags);
  229. return err != REG_NOERROR;
  230. }
  231. #ifdef _LIBC
  232. # include <shlib-compat.h>
  233. versioned_symbol (libc, __regexec, regexec, GLIBC_2_3_4);
  234. # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
  235. __typeof__ (__regexec) __compat_regexec;
  236. int
  237. attribute_compat_text_section
  238. __compat_regexec (const regex_t *__restrict preg,
  239. const char *__restrict string, size_t nmatch,
  240. regmatch_t pmatch[], int eflags)
  241. {
  242. return regexec (preg, string, nmatch, pmatch,
  243. eflags & (REG_NOTBOL | REG_NOTEOL));
  244. }
  245. compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0);
  246. # endif
  247. #endif
  248. /* Entry points for GNU code. */
  249. /* re_match, re_search, re_match_2, re_search_2
  250. The former two functions operate on STRING with length LENGTH,
  251. while the later two operate on concatenation of STRING1 and STRING2
  252. with lengths LENGTH1 and LENGTH2, respectively.
  253. re_match() matches the compiled pattern in BUFP against the string,
  254. starting at index START.
  255. re_search() first tries matching at index START, then it tries to match
  256. starting from index START + 1, and so on. The last start position tried
  257. is START + RANGE. (Thus RANGE = 0 forces re_search to operate the same
  258. way as re_match().)
  259. The parameter STOP of re_{match,search}_2 specifies that no match exceeding
  260. the first STOP characters of the concatenation of the strings should be
  261. concerned.
  262. If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
  263. and all groups is stroed in REGS. (For the "_2" variants, the offsets are
  264. computed relative to the concatenation, not relative to the individual
  265. strings.)
  266. On success, re_match* functions return the length of the match, re_search*
  267. return the position of the start of the match. Return value -1 means no
  268. match was found and -2 indicates an internal error. */
  269. int
  270. re_match (bufp, string, length, start, regs)
  271. struct re_pattern_buffer *bufp;
  272. const char *string;
  273. int length, start;
  274. struct re_registers *regs;
  275. {
  276. return re_search_stub (bufp, string, length, start, 0, length, regs, 1);
  277. }
  278. #ifdef _LIBC
  279. weak_alias (__re_match, re_match)
  280. #endif
  281. int
  282. re_search (bufp, string, length, start, range, regs)
  283. struct re_pattern_buffer *bufp;
  284. const char *string;
  285. int length, start, range;
  286. struct re_registers *regs;
  287. {
  288. return re_search_stub (bufp, string, length, start, range, length, regs, 0);
  289. }
  290. #ifdef _LIBC
  291. weak_alias (__re_search, re_search)
  292. #endif
  293. int
  294. re_match_2 (bufp, string1, length1, string2, length2, start, regs, stop)
  295. struct re_pattern_buffer *bufp;
  296. const char *string1, *string2;
  297. int length1, length2, start, stop;
  298. struct re_registers *regs;
  299. {
  300. return re_search_2_stub (bufp, string1, length1, string2, length2,
  301. start, 0, regs, stop, 1);
  302. }
  303. #ifdef _LIBC
  304. weak_alias (__re_match_2, re_match_2)
  305. #endif
  306. int
  307. re_search_2 (bufp, string1, length1, string2, length2, start, range, regs, stop)
  308. struct re_pattern_buffer *bufp;
  309. const char *string1, *string2;
  310. int length1, length2, start, range, stop;
  311. struct re_registers *regs;
  312. {
  313. return re_search_2_stub (bufp, string1, length1, string2, length2,
  314. start, range, regs, stop, 0);
  315. }
  316. #ifdef _LIBC
  317. weak_alias (__re_search_2, re_search_2)
  318. #endif
  319. static int
  320. re_search_2_stub (bufp, string1, length1, string2, length2, start, range, regs,
  321. stop, ret_len)
  322. struct re_pattern_buffer *bufp;
  323. const char *string1, *string2;
  324. int length1, length2, start, range, stop, ret_len;
  325. struct re_registers *regs;
  326. {
  327. const char *str;
  328. int rval;
  329. int len = length1 + length2;
  330. int free_str = 0;
  331. if (BE (length1 < 0 || length2 < 0 || stop < 0, 0))
  332. return -2;
  333. /* Concatenate the strings. */
  334. if (length2 > 0)
  335. if (length1 > 0)
  336. {
  337. char *s = re_malloc (char, len);
  338. if (BE (s == NULL, 0))
  339. return -2;
  340. memcpy (s, string1, length1);
  341. memcpy (s + length1, string2, length2);
  342. str = s;
  343. free_str = 1;
  344. }
  345. else
  346. str = string2;
  347. else
  348. str = string1;
  349. rval = re_search_stub (bufp, str, len, start, range, stop, regs,
  350. ret_len);
  351. if (free_str)
  352. re_free ((char *) str);
  353. return rval;
  354. }
  355. /* The parameters have the same meaning as those of re_search.
  356. Additional parameters:
  357. If RET_LEN is nonzero the length of the match is returned (re_match style);
  358. otherwise the position of the match is returned. */
  359. static int
  360. re_search_stub (bufp, string, length, start, range, stop, regs, ret_len)
  361. struct re_pattern_buffer *bufp;
  362. const char *string;
  363. int length, start, range, stop, ret_len;
  364. struct re_registers *regs;
  365. {
  366. reg_errcode_t result;
  367. regmatch_t *pmatch;
  368. int nregs, rval;
  369. int eflags = 0;
  370. /* Check for out-of-range. */
  371. if (BE (start < 0 || start > length, 0))
  372. return -1;
  373. if (BE (start + range > length, 0))
  374. range = length - start;
  375. else if (BE (start + range < 0, 0))
  376. range = -start;
  377. eflags |= (bufp->not_bol) ? REG_NOTBOL : 0;
  378. eflags |= (bufp->not_eol) ? REG_NOTEOL : 0;
  379. /* Compile fastmap if we haven't yet. */
  380. if (range > 0 && bufp->fastmap != NULL && !bufp->fastmap_accurate)
  381. re_compile_fastmap (bufp);
  382. if (BE (bufp->no_sub, 0))
  383. regs = NULL;
  384. /* We need at least 1 register. */
  385. if (regs == NULL)
  386. nregs = 1;
  387. else if (BE (bufp->regs_allocated == REGS_FIXED &&
  388. regs->num_regs < bufp->re_nsub + 1, 0))
  389. {
  390. nregs = regs->num_regs;
  391. if (BE (nregs < 1, 0))
  392. {
  393. /* Nothing can be copied to regs. */
  394. regs = NULL;
  395. nregs = 1;
  396. }
  397. }
  398. else
  399. nregs = bufp->re_nsub + 1;
  400. pmatch = re_malloc (regmatch_t, nregs);
  401. if (BE (pmatch == NULL, 0))
  402. return -2;
  403. result = re_search_internal (bufp, string, length, start, range, stop,
  404. nregs, pmatch, eflags);
  405. rval = 0;
  406. /* I hope we needn't fill ther regs with -1's when no match was found. */
  407. if (result != REG_NOERROR)
  408. rval = -1;
  409. else if (regs != NULL)
  410. {
  411. /* If caller wants register contents data back, copy them. */
  412. bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs,
  413. bufp->regs_allocated);
  414. if (BE (bufp->regs_allocated == REGS_UNALLOCATED, 0))
  415. rval = -2;
  416. }
  417. if (BE (rval == 0, 1))
  418. {
  419. if (ret_len)
  420. {
  421. assert (pmatch[0].rm_so == start);
  422. rval = pmatch[0].rm_eo - start;
  423. }
  424. else
  425. rval = pmatch[0].rm_so;
  426. }
  427. re_free (pmatch);
  428. return rval;
  429. }
  430. static unsigned
  431. re_copy_regs (regs, pmatch, nregs, regs_allocated)
  432. struct re_registers *regs;
  433. regmatch_t *pmatch;
  434. int nregs, regs_allocated;
  435. {
  436. int rval = REGS_REALLOCATE;
  437. int i;
  438. int need_regs = nregs + 1;
  439. /* We need one extra element beyond `num_regs' for the `-1' marker GNU code
  440. uses. */
  441. /* Have the register data arrays been allocated? */
  442. if (regs_allocated == REGS_UNALLOCATED)
  443. { /* No. So allocate them with malloc. */
  444. regs->start = re_malloc (regoff_t, need_regs);
  445. regs->end = re_malloc (regoff_t, need_regs);
  446. if (BE (regs->start == NULL, 0) || BE (regs->end == NULL, 0))
  447. return REGS_UNALLOCATED;
  448. regs->num_regs = need_regs;
  449. }
  450. else if (regs_allocated == REGS_REALLOCATE)
  451. { /* Yes. If we need more elements than were already
  452. allocated, reallocate them. If we need fewer, just
  453. leave it alone. */
  454. if (BE (need_regs > regs->num_regs, 0))
  455. {
  456. regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
  457. regoff_t *new_end = re_realloc (regs->end, regoff_t, need_regs);
  458. if (BE (new_start == NULL, 0) || BE (new_end == NULL, 0))
  459. return REGS_UNALLOCATED;
  460. regs->start = new_start;
  461. regs->end = new_end;
  462. regs->num_regs = need_regs;
  463. }
  464. }
  465. else
  466. {
  467. assert (regs_allocated == REGS_FIXED);
  468. /* This function may not be called with REGS_FIXED and nregs too big. */
  469. assert (regs->num_regs >= nregs);
  470. rval = REGS_FIXED;
  471. }
  472. /* Copy the regs. */
  473. for (i = 0; i < nregs; ++i)
  474. {
  475. regs->start[i] = pmatch[i].rm_so;
  476. regs->end[i] = pmatch[i].rm_eo;
  477. }
  478. for ( ; i < regs->num_regs; ++i)
  479. regs->start[i] = regs->end[i] = -1;
  480. return rval;
  481. }
  482. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  483. ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
  484. this memory for recording register information. STARTS and ENDS
  485. must be allocated using the malloc library routine, and must each
  486. be at least NUM_REGS * sizeof (regoff_t) bytes long.
  487. If NUM_REGS == 0, then subsequent matches should allocate their own
  488. register data.
  489. Unless this function is called, the first search or match using
  490. PATTERN_BUFFER will allocate its own register data, without
  491. freeing the old data. */
  492. void
  493. re_set_registers (bufp, regs, num_regs, starts, ends)
  494. struct re_pattern_buffer *bufp;
  495. struct re_registers *regs;
  496. unsigned num_regs;
  497. regoff_t *starts, *ends;
  498. {
  499. if (num_regs)
  500. {
  501. bufp->regs_allocated = REGS_REALLOCATE;
  502. regs->num_regs = num_regs;
  503. regs->start = starts;
  504. regs->end = ends;
  505. }
  506. else
  507. {
  508. bufp->regs_allocated = REGS_UNALLOCATED;
  509. regs->num_regs = 0;
  510. regs->start = regs->end = (regoff_t *) 0;
  511. }
  512. }
  513. #ifdef _LIBC
  514. weak_alias (__re_set_registers, re_set_registers)
  515. #endif
  516. /* Entry points compatible with 4.2 BSD regex library. We don't define
  517. them unless specifically requested. */
  518. #if defined _REGEX_RE_COMP || defined _LIBC
  519. int
  520. # ifdef _LIBC
  521. weak_function
  522. # endif
  523. re_exec (s)
  524. const char *s;
  525. {
  526. return 0 == regexec (&re_comp_buf, s, 0, NULL, 0);
  527. }
  528. #endif /* _REGEX_RE_COMP */
  529. /* Internal entry point. */
  530. /* Searches for a compiled pattern PREG in the string STRING, whose
  531. length is LENGTH. NMATCH, PMATCH, and EFLAGS have the same
  532. mingings with regexec. START, and RANGE have the same meanings
  533. with re_search.
  534. Return REG_NOERROR if we find a match, and REG_NOMATCH if not,
  535. otherwise return the error code.
  536. Note: We assume front end functions already check ranges.
  537. (START + RANGE >= 0 && START + RANGE <= LENGTH) */
  538. static reg_errcode_t
  539. re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
  540. eflags)
  541. const regex_t *preg;
  542. const char *string;
  543. int length, start, range, stop, eflags;
  544. size_t nmatch;
  545. regmatch_t pmatch[];
  546. {
  547. reg_errcode_t err;
  548. re_dfa_t *dfa = (re_dfa_t *)preg->buffer;
  549. int left_lim, right_lim, incr;
  550. int fl_longest_match, match_first, match_kind, match_last = -1;
  551. int extra_nmatch;
  552. int sb, ch;
  553. #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
  554. re_match_context_t mctx = { .dfa = dfa };
  555. #else
  556. re_match_context_t mctx;
  557. #endif
  558. char *fastmap = (preg->fastmap != NULL && preg->fastmap_accurate
  559. && range && !preg->can_be_null) ? preg->fastmap : NULL;
  560. unsigned RE_TRANSLATE_TYPE t = (unsigned RE_TRANSLATE_TYPE) preg->translate;
  561. #if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
  562. memset (&mctx, '\0', sizeof (re_match_context_t));
  563. mctx.dfa = dfa;
  564. #endif
  565. extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0;
  566. nmatch -= extra_nmatch;
  567. /* Check if the DFA haven't been compiled. */
  568. if (BE (preg->used == 0 || dfa->init_state == NULL
  569. || dfa->init_state_word == NULL || dfa->init_state_nl == NULL
  570. || dfa->init_state_begbuf == NULL, 0))
  571. return REG_NOMATCH;
  572. #ifdef DEBUG
  573. /* We assume front-end functions already check them. */
  574. assert (start + range >= 0 && start + range <= length);
  575. #endif
  576. /* If initial states with non-begbuf contexts have no elements,
  577. the regex must be anchored. If preg->newline_anchor is set,
  578. we'll never use init_state_nl, so do not check it. */
  579. if (dfa->init_state->nodes.nelem == 0
  580. && dfa->init_state_word->nodes.nelem == 0
  581. && (dfa->init_state_nl->nodes.nelem == 0
  582. || !preg->newline_anchor))
  583. {
  584. if (start != 0 && start + range != 0)
  585. return REG_NOMATCH;
  586. start = range = 0;
  587. }
  588. /* We must check the longest matching, if nmatch > 0. */
  589. fl_longest_match = (nmatch != 0 || dfa->nbackref);
  590. err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1,
  591. preg->translate, preg->syntax & RE_ICASE, dfa);
  592. if (BE (err != REG_NOERROR, 0))
  593. goto free_return;
  594. mctx.input.stop = stop;
  595. mctx.input.raw_stop = stop;
  596. mctx.input.newline_anchor = preg->newline_anchor;
  597. err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2);
  598. if (BE (err != REG_NOERROR, 0))
  599. goto free_return;
  600. /* We will log all the DFA states through which the dfa pass,
  601. if nmatch > 1, or this dfa has "multibyte node", which is a
  602. back-reference or a node which can accept multibyte character or
  603. multi character collating element. */
  604. if (nmatch > 1 || dfa->has_mb_node)
  605. {
  606. mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1);
  607. if (BE (mctx.state_log == NULL, 0))
  608. {
  609. err = REG_ESPACE;
  610. goto free_return;
  611. }
  612. }
  613. else
  614. mctx.state_log = NULL;
  615. match_first = start;
  616. mctx.input.tip_context = (eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
  617. : CONTEXT_NEWLINE | CONTEXT_BEGBUF;
  618. /* Check incrementally whether of not the input string match. */
  619. incr = (range < 0) ? -1 : 1;
  620. left_lim = (range < 0) ? start + range : start;
  621. right_lim = (range < 0) ? start : start + range;
  622. sb = dfa->mb_cur_max == 1;
  623. match_kind =
  624. (fastmap
  625. ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0)
  626. | (range >= 0 ? 2 : 0)
  627. | (t != NULL ? 1 : 0))
  628. : 8);
  629. for (;; match_first += incr)
  630. {
  631. err = REG_NOMATCH;
  632. if (match_first < left_lim || right_lim < match_first)
  633. goto free_return;
  634. /* Advance as rapidly as possible through the string, until we
  635. find a plausible place to start matching. This may be done
  636. with varying efficiency, so there are various possibilities:
  637. only the most common of them are specialized, in order to
  638. save on code size. We use a switch statement for speed. */
  639. switch (match_kind)
  640. {
  641. case 8:
  642. /* No fastmap. */
  643. break;
  644. case 7:
  645. /* Fastmap with single-byte translation, match forward. */
  646. while (BE (match_first < right_lim, 1)
  647. && !fastmap[t[(unsigned char) string[match_first]]])
  648. ++match_first;
  649. goto forward_match_found_start_or_reached_end;
  650. case 6:
  651. /* Fastmap without translation, match forward. */
  652. while (BE (match_first < right_lim, 1)
  653. && !fastmap[(unsigned char) string[match_first]])
  654. ++match_first;
  655. forward_match_found_start_or_reached_end:
  656. if (BE (match_first == right_lim, 0))
  657. {
  658. ch = match_first >= length
  659. ? 0 : (unsigned char) string[match_first];
  660. if (!fastmap[t ? t[ch] : ch])
  661. goto free_return;
  662. }
  663. break;
  664. case 4:
  665. case 5:
  666. /* Fastmap without multi-byte translation, match backwards. */
  667. while (match_first >= left_lim)
  668. {
  669. ch = match_first >= length
  670. ? 0 : (unsigned char) string[match_first];
  671. if (fastmap[t ? t[ch] : ch])
  672. break;
  673. --match_first;
  674. }
  675. if (match_first < left_lim)
  676. goto free_return;
  677. break;
  678. default:
  679. /* In this case, we can't determine easily the current byte,
  680. since it might be a component byte of a multibyte
  681. character. Then we use the constructed buffer instead. */
  682. for (;;)
  683. {
  684. /* If MATCH_FIRST is out of the valid range, reconstruct the
  685. buffers. */
  686. unsigned int offset = match_first - mctx.input.raw_mbs_idx;
  687. if (BE (offset >= (unsigned int) mctx.input.valid_raw_len, 0))
  688. {
  689. err = re_string_reconstruct (&mctx.input, match_first,
  690. eflags);
  691. if (BE (err != REG_NOERROR, 0))
  692. goto free_return;
  693. offset = match_first - mctx.input.raw_mbs_idx;
  694. }
  695. /* If MATCH_FIRST is out of the buffer, leave it as '\0'.
  696. Note that MATCH_FIRST must not be smaller than 0. */
  697. ch = (match_first >= length
  698. ? 0 : re_string_byte_at (&mctx.input, offset));
  699. if (fastmap[ch])
  700. break;
  701. match_first += incr;
  702. if (match_first < left_lim || match_first > right_lim)
  703. {
  704. err = REG_NOMATCH;
  705. goto free_return;
  706. }
  707. }
  708. break;
  709. }
  710. /* Reconstruct the buffers so that the matcher can assume that
  711. the matching starts from the beginning of the buffer. */
  712. err = re_string_reconstruct (&mctx.input, match_first, eflags);
  713. if (BE (err != REG_NOERROR, 0))
  714. goto free_return;
  715. #ifdef RE_ENABLE_I18N
  716. /* Don't consider this char as a possible match start if it part,
  717. yet isn't the head, of a multibyte character. */
  718. if (!sb && !re_string_first_byte (&mctx.input, 0))
  719. continue;
  720. #endif
  721. /* It seems to be appropriate one, then use the matcher. */
  722. /* We assume that the matching starts from 0. */
  723. mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0;
  724. match_last = check_matching (&mctx, fl_longest_match,
  725. range >= 0 ? &match_first : NULL);
  726. if (match_last != -1)
  727. {
  728. if (BE (match_last == -2, 0))
  729. {
  730. err = REG_ESPACE;
  731. goto free_return;
  732. }
  733. else
  734. {
  735. mctx.match_last = match_last;
  736. if ((!preg->no_sub && nmatch > 1) || dfa->nbackref)
  737. {
  738. re_dfastate_t *pstate = mctx.state_log[match_last];
  739. mctx.last_node = check_halt_state_context (&mctx, pstate,
  740. match_last);
  741. }
  742. if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match)
  743. || dfa->nbackref)
  744. {
  745. err = prune_impossible_nodes (&mctx);
  746. if (err == REG_NOERROR)
  747. break;
  748. if (BE (err != REG_NOMATCH, 0))
  749. goto free_return;
  750. match_last = -1;
  751. }
  752. else
  753. break; /* We found a match. */
  754. }
  755. }
  756. match_ctx_clean (&mctx);
  757. }
  758. #ifdef DEBUG
  759. assert (match_last != -1);
  760. assert (err == REG_NOERROR);
  761. #endif
  762. /* Set pmatch[] if we need. */
  763. if (nmatch > 0)
  764. {
  765. int reg_idx;
  766. /* Initialize registers. */
  767. for (reg_idx = 1; reg_idx < nmatch; ++reg_idx)
  768. pmatch[reg_idx].rm_so = pmatch[reg_idx].rm_eo = -1;
  769. /* Set the points where matching start/end. */
  770. pmatch[0].rm_so = 0;
  771. pmatch[0].rm_eo = mctx.match_last;
  772. if (!preg->no_sub && nmatch > 1)
  773. {
  774. err = set_regs (preg, &mctx, nmatch, pmatch,
  775. dfa->has_plural_match && dfa->nbackref > 0);
  776. if (BE (err != REG_NOERROR, 0))
  777. goto free_return;
  778. }
  779. /* At last, add the offset to the each registers, since we slided
  780. the buffers so that we could assume that the matching starts
  781. from 0. */
  782. for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
  783. if (pmatch[reg_idx].rm_so != -1)
  784. {
  785. #ifdef RE_ENABLE_I18N
  786. if (BE (mctx.input.offsets_needed != 0, 0))
  787. {
  788. if (pmatch[reg_idx].rm_so == mctx.input.valid_len)
  789. pmatch[reg_idx].rm_so += mctx.input.valid_raw_len - mctx.input.valid_len;
  790. else
  791. pmatch[reg_idx].rm_so = mctx.input.offsets[pmatch[reg_idx].rm_so];
  792. if (pmatch[reg_idx].rm_eo == mctx.input.valid_len)
  793. pmatch[reg_idx].rm_eo += mctx.input.valid_raw_len - mctx.input.valid_len;
  794. else
  795. pmatch[reg_idx].rm_eo = mctx.input.offsets[pmatch[reg_idx].rm_eo];
  796. }
  797. #else
  798. assert (mctx.input.offsets_needed == 0);
  799. #endif
  800. pmatch[reg_idx].rm_so += match_first;
  801. pmatch[reg_idx].rm_eo += match_first;
  802. }
  803. for (reg_idx = 0; reg_idx < extra_nmatch; ++reg_idx)
  804. {
  805. pmatch[nmatch + reg_idx].rm_so = -1;
  806. pmatch[nmatch + reg_idx].rm_eo = -1;
  807. }
  808. if (dfa->subexp_map)
  809. for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++)
  810. if (dfa->subexp_map[reg_idx] != reg_idx)
  811. {
  812. pmatch[reg_idx + 1].rm_so
  813. = pmatch[dfa->subexp_map[reg_idx] + 1].rm_so;
  814. pmatch[reg_idx + 1].rm_eo
  815. = pmatch[dfa->subexp_map[reg_idx] + 1].rm_eo;
  816. }
  817. }
  818. free_return:
  819. re_free (mctx.state_log);
  820. if (dfa->nbackref)
  821. match_ctx_free (&mctx);
  822. re_string_destruct (&mctx.input);
  823. return err;
  824. }
  825. static reg_errcode_t
  826. prune_impossible_nodes (mctx)
  827. re_match_context_t *mctx;
  828. {
  829. re_dfa_t *const dfa = mctx->dfa;
  830. int halt_node, match_last;
  831. reg_errcode_t ret;
  832. re_dfastate_t **sifted_states;
  833. re_dfastate_t **lim_states = NULL;
  834. re_sift_context_t sctx;
  835. #ifdef DEBUG
  836. assert (mctx->state_log != NULL);
  837. #endif
  838. match_last = mctx->match_last;
  839. halt_node = mctx->last_node;
  840. sifted_states = re_malloc (re_dfastate_t *, match_last + 1);
  841. if (BE (sifted_states == NULL, 0))
  842. {
  843. ret = REG_ESPACE;
  844. goto free_return;
  845. }
  846. if (dfa->nbackref)
  847. {
  848. lim_states = re_malloc (re_dfastate_t *, match_last + 1);
  849. if (BE (lim_states == NULL, 0))
  850. {
  851. ret = REG_ESPACE;
  852. goto free_return;
  853. }
  854. while (1)
  855. {
  856. memset (lim_states, '\0',
  857. sizeof (re_dfastate_t *) * (match_last + 1));
  858. sift_ctx_init (&sctx, sifted_states, lim_states, halt_node,
  859. match_last);
  860. ret = sift_states_backward (mctx, &sctx);
  861. re_node_set_free (&sctx.limits);
  862. if (BE (ret != REG_NOERROR, 0))
  863. goto free_return;
  864. if (sifted_states[0] != NULL || lim_states[0] != NULL)
  865. break;
  866. do
  867. {
  868. --match_last;
  869. if (match_last < 0)
  870. {
  871. ret = REG_NOMATCH;
  872. goto free_return;
  873. }
  874. } while (mctx->state_log[match_last] == NULL
  875. || !mctx->state_log[match_last]->halt);
  876. halt_node = check_halt_state_context (mctx,
  877. mctx->state_log[match_last],
  878. match_last);
  879. }
  880. ret = merge_state_array (dfa, sifted_states, lim_states,
  881. match_last + 1);
  882. re_free (lim_states);
  883. lim_states = NULL;
  884. if (BE (ret != REG_NOERROR, 0))
  885. goto free_return;
  886. }
  887. else
  888. {
  889. sift_ctx_init (&sctx, sifted_states, lim_states, halt_node, match_last);
  890. ret = sift_states_backward (mctx, &sctx);
  891. re_node_set_free (&sctx.limits);
  892. if (BE (ret != REG_NOERROR, 0))
  893. goto free_return;
  894. }
  895. re_free (mctx->state_log);
  896. mctx->state_log = sifted_states;
  897. sifted_states = NULL;
  898. mctx->last_node = halt_node;
  899. mctx->match_last = match_last;
  900. ret = REG_NOERROR;
  901. free_return:
  902. re_free (sifted_states);
  903. re_free (lim_states);
  904. return ret;
  905. }
  906. /* Acquire an initial state and return it.
  907. We must select appropriate initial state depending on the context,
  908. since initial states may have constraints like "\<", "^", etc.. */
  909. static inline re_dfastate_t *
  910. acquire_init_state_context (err, mctx, idx)
  911. reg_errcode_t *err;
  912. const re_match_context_t *mctx;
  913. int idx;
  914. {
  915. re_dfa_t *const dfa = mctx->dfa;
  916. if (dfa->init_state->has_constraint)
  917. {
  918. unsigned int context;
  919. context = re_string_context_at (&mctx->input, idx - 1, mctx->eflags);
  920. if (IS_WORD_CONTEXT (context))
  921. return dfa->init_state_word;
  922. else if (IS_ORDINARY_CONTEXT (context))
  923. return dfa->init_state;
  924. else if (IS_BEGBUF_CONTEXT (context) && IS_NEWLINE_CONTEXT (context))
  925. return dfa->init_state_begbuf;
  926. else if (IS_NEWLINE_CONTEXT (context))
  927. return dfa->init_state_nl;
  928. else if (IS_BEGBUF_CONTEXT (context))
  929. {
  930. /* It is relatively rare case, then calculate on demand. */
  931. return re_acquire_state_context (err, dfa,
  932. dfa->init_state->entrance_nodes,
  933. context);
  934. }
  935. else
  936. /* Must not happen? */
  937. return dfa->init_state;
  938. }
  939. else
  940. return dfa->init_state;
  941. }
  942. /* Check whether the regular expression match input string INPUT or not,
  943. and return the index where the matching end, return -1 if not match,
  944. or return -2 in case of an error.
  945. FL_LONGEST_MATCH means we want the POSIX longest matching.
  946. If P_MATCH_FIRST is not NULL, and the match fails, it is set to the
  947. next place where we may want to try matching.
  948. Note that the matcher assume that the maching starts from the current
  949. index of the buffer. */
  950. static int
  951. check_matching (mctx, fl_longest_match, p_match_first)
  952. re_match_context_t *mctx;
  953. int fl_longest_match;
  954. int *p_match_first;
  955. {
  956. re_dfa_t *const dfa = mctx->dfa;
  957. reg_errcode_t err;
  958. int match = 0;
  959. int match_last = -1;
  960. int cur_str_idx = re_string_cur_idx (&mctx->input);
  961. re_dfastate_t *cur_state;
  962. int at_init_state = p_match_first != NULL;
  963. int next_start_idx = cur_str_idx;
  964. err = REG_NOERROR;
  965. cur_state = acquire_init_state_context (&err, mctx, cur_str_idx);
  966. /* An initial state must not be NULL (invalid). */
  967. if (BE (cur_state == NULL, 0))
  968. {
  969. assert (err == REG_ESPACE);
  970. return -2;
  971. }
  972. if (mctx->state_log != NULL)
  973. {
  974. mctx->state_log[cur_str_idx] = cur_state;
  975. /* Check OP_OPEN_SUBEXP in the initial state in case that we use them
  976. later. E.g. Processing back references. */
  977. if (BE (dfa->nbackref, 0))
  978. {
  979. at_init_state = 0;
  980. err = check_subexp_matching_top (mctx, &cur_state->nodes, 0);
  981. if (BE (err != REG_NOERROR, 0))
  982. return err;
  983. if (cur_state->has_backref)
  984. {
  985. err = transit_state_bkref (mctx, &cur_state->nodes);
  986. if (BE (err != REG_NOERROR, 0))
  987. return err;
  988. }
  989. }
  990. }
  991. /* If the RE accepts NULL string. */
  992. if (BE (cur_state->halt, 0))
  993. {
  994. if (!cur_state->has_constraint
  995. || check_halt_state_context (mctx, cur_state, cur_str_idx))
  996. {
  997. if (!fl_longest_match)
  998. return cur_str_idx;
  999. else
  1000. {
  1001. match_last = cur_str_idx;
  1002. match = 1;
  1003. }
  1004. }
  1005. }
  1006. while (!re_string_eoi (&mctx->input))
  1007. {
  1008. re_dfastate_t *old_state = cur_state;
  1009. int next_char_idx = re_string_cur_idx (&mctx->input) + 1;
  1010. if (BE (next_char_idx >= mctx->input.bufs_len, 0)
  1011. || (BE (next_char_idx >= mctx->input.valid_len, 0)
  1012. && mctx->input.valid_len < mctx->input.len))
  1013. {
  1014. err = extend_buffers (mctx);
  1015. if (BE (err != REG_NOERROR, 0))
  1016. {
  1017. assert (err == REG_ESPACE);
  1018. return -2;
  1019. }
  1020. }
  1021. cur_state = transit_state (&err, mctx, cur_state);
  1022. if (mctx->state_log != NULL)
  1023. cur_state = merge_state_with_log (&err, mctx, cur_state);
  1024. if (cur_state == NULL)
  1025. {
  1026. /* Reached the invalid state or an error. Try to recover a valid
  1027. state using the state log, if available and if we have not
  1028. already found a valid (even if not the longest) match. */
  1029. if (BE (err != REG_NOERROR, 0))
  1030. return -2;
  1031. if (mctx->state_log == NULL
  1032. || (match && !fl_longest_match)
  1033. || (cur_state = find_recover_state (&err, mctx)) == NULL)
  1034. break;
  1035. }
  1036. if (BE (at_init_state, 0))
  1037. {
  1038. if (old_state == cur_state)
  1039. next_start_idx = next_char_idx;
  1040. else
  1041. at_init_state = 0;
  1042. }
  1043. if (cur_state->halt)
  1044. {
  1045. /* Reached a halt state.
  1046. Check the halt state can satisfy the current context. */
  1047. if (!cur_state->has_constraint
  1048. || check_halt_state_context (mctx, cur_state,
  1049. re_string_cur_idx (&mctx->input)))
  1050. {
  1051. /* We found an appropriate halt state. */
  1052. match_last = re_string_cur_idx (&mctx->input);
  1053. match = 1;
  1054. /* We found a match, do not modify match_first below. */
  1055. p_match_first = NULL;
  1056. if (!fl_longest_match)
  1057. break;
  1058. }
  1059. }
  1060. }
  1061. if (p_match_first)
  1062. *p_match_first += next_start_idx;
  1063. return match_last;
  1064. }
  1065. /* Check NODE match the current context. */
  1066. static int check_halt_node_context (dfa, node, context)
  1067. const re_dfa_t *dfa;
  1068. int node;
  1069. unsigned int context;
  1070. {
  1071. re_token_type_t type = dfa->nodes[node].type;
  1072. unsigned int constraint = dfa->nodes[node].constraint;
  1073. if (type != END_OF_RE)
  1074. return 0;
  1075. if (!constraint)
  1076. return 1;
  1077. if (NOT_SATISFY_NEXT_CONSTRAINT (constraint, context))
  1078. return 0;
  1079. return 1;
  1080. }
  1081. /* Check the halt state STATE match the current context.
  1082. Return 0 if not match, if the node, STATE has, is a halt node and
  1083. match the context, return the node. */
  1084. static int
  1085. check_halt_state_context (mctx, state, idx)
  1086. const re_match_context_t *mctx;
  1087. const re_dfastate_t *state;
  1088. int idx;
  1089. {
  1090. int i;
  1091. unsigned int context;
  1092. #ifdef DEBUG
  1093. assert (state->halt);
  1094. #endif
  1095. context = re_string_context_at (&mctx->input, idx, mctx->eflags);
  1096. for (i = 0; i < state->nodes.nelem; ++i)
  1097. if (check_halt_node_context (mctx->dfa, state->nodes.elems[i], context))
  1098. return state->nodes.elems[i];
  1099. return 0;
  1100. }
  1101. /* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
  1102. corresponding to the DFA).
  1103. Return the destination node, and update EPS_VIA_NODES, return -1 in case
  1104. of errors. */
  1105. static int
  1106. proceed_next_node (mctx, nregs, regs, pidx, node, eps_via_nodes, fs)
  1107. const re_match_context_t *mctx;
  1108. regmatch_t *regs;
  1109. int nregs, *pidx, node;
  1110. re_node_set *eps_via_nodes;
  1111. struct re_fail_stack_t *fs;
  1112. {
  1113. re_dfa_t *const dfa = mctx->dfa;
  1114. int i, err, dest_node;
  1115. dest_node = -1;
  1116. if (IS_EPSILON_NODE (dfa->nodes[node].type))
  1117. {
  1118. re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes;
  1119. re_node_set *edests = &dfa->edests[node];
  1120. int dest_node;
  1121. err = re_node_set_insert (eps_via_nodes, node);
  1122. if (BE (err < 0, 0))
  1123. return -2;
  1124. /* Pick up a valid destination, or return -1 if none is found. */
  1125. for (dest_node = -1, i = 0; i < edests->nelem; ++i)
  1126. {
  1127. int candidate = edests->elems[i];
  1128. if (!re_node_set_contains (cur_nodes, candidate))
  1129. continue;
  1130. if (dest_node == -1)
  1131. dest_node = candidate;
  1132. else
  1133. {
  1134. /* In order to avoid infinite loop like "(a*)*", return the second
  1135. epsilon-transition if the first was already considered. */
  1136. if (re_node_set_contains (eps_via_nodes, dest_node))
  1137. return candidate;
  1138. /* Otherwise, push the second epsilon-transition on the fail stack. */
  1139. else if (fs != NULL
  1140. && push_fail_stack (fs, *pidx, candidate, nregs, regs,
  1141. eps_via_nodes))
  1142. return -2;
  1143. /* We know we are going to exit. */
  1144. break;
  1145. }
  1146. }
  1147. return dest_node;
  1148. }
  1149. else
  1150. {
  1151. int naccepted = 0;
  1152. re_token_type_t type = dfa->nodes[node].type;
  1153. #ifdef RE_ENABLE_I18N
  1154. if (dfa->nodes[node].accept_mb)
  1155. naccepted = check_node_accept_bytes (dfa, node, &mctx->input, *pidx);
  1156. else
  1157. #endif /* RE_ENABLE_I18N */
  1158. if (type == OP_BACK_REF)
  1159. {
  1160. int subexp_idx = dfa->nodes[node].opr.idx + 1;
  1161. naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so;
  1162. if (fs != NULL)
  1163. {
  1164. if (regs[subexp_idx].rm_so == -1 || regs[subexp_idx].rm_eo == -1)
  1165. return -1;
  1166. else if (naccepted)
  1167. {
  1168. char *buf = (char *) re_string_get_buffer (&mctx->input);
  1169. if (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx,
  1170. naccepted) != 0)
  1171. return -1;
  1172. }
  1173. }
  1174. if (naccepted == 0)
  1175. {
  1176. err = re_node_set_insert (eps_via_nodes, node);
  1177. if (BE (err < 0, 0))
  1178. return -2;
  1179. dest_node = dfa->edests[node].elems[0];
  1180. if (re_node_set_contains (&mctx->state_log[*pidx]->nodes,
  1181. dest_node))
  1182. return dest_node;
  1183. }
  1184. }
  1185. if (naccepted != 0
  1186. || check_node_accept (mctx, dfa->nodes + node, *pidx))
  1187. {
  1188. dest_node = dfa->nexts[node];
  1189. *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted;
  1190. if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL
  1191. || !re_node_set_contains (&mctx->state_log[*pidx]->nodes,
  1192. dest_node)))
  1193. return -1;
  1194. re_node_set_empty (eps_via_nodes);
  1195. return dest_node;
  1196. }
  1197. }
  1198. return -1;
  1199. }
  1200. static reg_errcode_t
  1201. push_fail_stack (fs, str_idx, dest_node, nregs, regs, eps_via_nodes)
  1202. struct re_fail_stack_t *fs;
  1203. int str_idx, dest_node, nregs;
  1204. regmatch_t *regs;
  1205. re_node_set *eps_via_nodes;
  1206. {
  1207. reg_errcode_t err;
  1208. int num = fs->num++;
  1209. if (fs->num == fs->alloc)
  1210. {
  1211. struct re_fail_stack_ent_t *new_array;
  1212. new_array = realloc (fs->stack, (sizeof (struct re_fail_stack_ent_t)
  1213. * fs->alloc * 2));
  1214. if (new_array == NULL)
  1215. return REG_ESPACE;
  1216. fs->alloc *= 2;
  1217. fs->stack = new_array;
  1218. }
  1219. fs->stack[num].idx = str_idx;
  1220. fs->stack[num].node = dest_node;
  1221. fs->stack[num].regs = re_malloc (regmatch_t, nregs);
  1222. if (fs->stack[num].regs == NULL)
  1223. return REG_ESPACE;
  1224. memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
  1225. err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes);
  1226. return err;
  1227. }
  1228. static int
  1229. pop_fail_stack (fs, pidx, nregs, regs, eps_via_nodes)
  1230. struct re_fail_stack_t *fs;
  1231. int *pidx, nregs;
  1232. regmatch_t *regs;
  1233. re_node_set *eps_via_nodes;
  1234. {
  1235. int num = --fs->num;
  1236. assert (num >= 0);
  1237. *pidx = fs->stack[num].idx;
  1238. memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
  1239. re_node_set_free (eps_via_nodes);
  1240. re_free (fs->stack[num].regs);
  1241. *eps_via_nodes = fs->stack[num].eps_via_nodes;
  1242. return fs->stack[num].node;
  1243. }
  1244. /* Set the positions where the subexpressions are starts/ends to registers
  1245. PMATCH.
  1246. Note: We assume that pmatch[0] is already set, and
  1247. pmatch[i].rm_so == pmatch[i].rm_eo == -1 for 0 < i < nmatch. */
  1248. static reg_errcode_t
  1249. set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
  1250. const regex_t *preg;
  1251. const re_match_context_t *mctx;
  1252. size_t nmatch;
  1253. regmatch_t *pmatch;
  1254. int fl_backtrack;
  1255. {
  1256. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1257. int idx, cur_node;
  1258. re_node_set eps_via_nodes;
  1259. struct re_fail_stack_t *fs;
  1260. struct re_fail_stack_t fs_body = { 0, 2, NULL };
  1261. regmatch_t *prev_idx_match;
  1262. #ifdef DEBUG
  1263. assert (nmatch > 1);
  1264. assert (mctx->state_log != NULL);
  1265. #endif
  1266. if (fl_backtrack)
  1267. {
  1268. fs = &fs_body;
  1269. fs->stack = re_malloc (struct re_fail_stack_ent_t, fs->alloc);
  1270. if (fs->stack == NULL)
  1271. return REG_ESPACE;
  1272. }
  1273. else
  1274. fs = NULL;
  1275. cur_node = dfa->init_node;
  1276. re_node_set_init_empty (&eps_via_nodes);
  1277. prev_idx_match = (regmatch_t *) alloca (sizeof (regmatch_t) * nmatch);
  1278. memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
  1279. for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;)
  1280. {
  1281. update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch);
  1282. if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
  1283. {
  1284. int reg_idx;
  1285. if (fs)
  1286. {
  1287. for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
  1288. if (pmatch[reg_idx].rm_so > -1 && pmatch[reg_idx].rm_eo == -1)
  1289. break;
  1290. if (reg_idx == nmatch)
  1291. {
  1292. re_node_set_free (&eps_via_nodes);
  1293. return free_fail_stack_return (fs);
  1294. }
  1295. cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
  1296. &eps_via_nodes);
  1297. }
  1298. else
  1299. {
  1300. re_node_set_free (&eps_via_nodes);
  1301. return REG_NOERROR;
  1302. }
  1303. }
  1304. /* Proceed to next node. */
  1305. cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node,
  1306. &eps_via_nodes, fs);
  1307. if (BE (cur_node < 0, 0))
  1308. {
  1309. if (BE (cur_node == -2, 0))
  1310. {
  1311. re_node_set_free (&eps_via_nodes);
  1312. free_fail_stack_return (fs);
  1313. return REG_ESPACE;
  1314. }
  1315. if (fs)
  1316. cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
  1317. &eps_via_nodes);
  1318. else
  1319. {
  1320. re_node_set_free (&eps_via_nodes);
  1321. return REG_NOMATCH;
  1322. }
  1323. }
  1324. }
  1325. re_node_set_free (&eps_via_nodes);
  1326. return free_fail_stack_return (fs);
  1327. }
  1328. static reg_errcode_t
  1329. free_fail_stack_return (fs)
  1330. struct re_fail_stack_t *fs;
  1331. {
  1332. if (fs)
  1333. {
  1334. int fs_idx;
  1335. for (fs_idx = 0; fs_idx < fs->num; ++fs_idx)
  1336. {
  1337. re_node_set_free (&fs->stack[fs_idx].eps_via_nodes);
  1338. re_free (fs->stack[fs_idx].regs);
  1339. }
  1340. re_free (fs->stack);
  1341. }
  1342. return REG_NOERROR;
  1343. }
  1344. static void
  1345. update_regs (dfa, pmatch, prev_idx_match, cur_node, cur_idx, nmatch)
  1346. re_dfa_t *dfa;
  1347. regmatch_t *pmatch, *prev_idx_match;
  1348. int cur_node, cur_idx, nmatch;
  1349. {
  1350. int type = dfa->nodes[cur_node].type;
  1351. if (type == OP_OPEN_SUBEXP)
  1352. {
  1353. int reg_num = dfa->nodes[cur_node].opr.idx + 1;
  1354. /* We are at the first node of this sub expression. */
  1355. if (reg_num < nmatch)
  1356. {
  1357. pmatch[reg_num].rm_so = cur_idx;
  1358. pmatch[reg_num].rm_eo = -1;
  1359. }
  1360. }
  1361. else if (type == OP_CLOSE_SUBEXP)
  1362. {
  1363. int reg_num = dfa->nodes[cur_node].opr.idx + 1;
  1364. if (reg_num < nmatch)
  1365. {
  1366. /* We are at the last node of this sub expression. */
  1367. if (pmatch[reg_num].rm_so < cur_idx)
  1368. {
  1369. pmatch[reg_num].rm_eo = cur_idx;
  1370. /* This is a non-empty match or we are not inside an optional
  1371. subexpression. Accept this right away. */
  1372. memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
  1373. }
  1374. else
  1375. {
  1376. if (dfa->nodes[cur_node].opt_subexp
  1377. && prev_idx_match[reg_num].rm_so != -1)
  1378. /* We transited through an empty match for an optional
  1379. subexpression, like (a?)*, and this is not the subexp's
  1380. first match. Copy back the old content of the registers
  1381. so that matches of an inner subexpression are undone as
  1382. well, like in ((a?))*. */
  1383. memcpy (pmatch, prev_idx_match, sizeof (regmatch_t) * nmatch);
  1384. else
  1385. /* We completed a subexpression, but it may be part of
  1386. an optional one, so do not update PREV_IDX_MATCH. */
  1387. pmatch[reg_num].rm_eo = cur_idx;
  1388. }
  1389. }
  1390. }
  1391. }
  1392. /* This function checks the STATE_LOG from the SCTX->last_str_idx to 0
  1393. and sift the nodes in each states according to the following rules.
  1394. Updated state_log will be wrote to STATE_LOG.
  1395. Rules: We throw away the Node `a' in the STATE_LOG[STR_IDX] if...
  1396. 1. When STR_IDX == MATCH_LAST(the last index in the state_log):
  1397. If `a' isn't the LAST_NODE and `a' can't epsilon transit to
  1398. the LAST_NODE, we throw away the node `a'.
  1399. 2. When 0 <= STR_IDX < MATCH_LAST and `a' accepts
  1400. string `s' and transit to `b':
  1401. i. If 'b' isn't in the STATE_LOG[STR_IDX+strlen('s')], we throw
  1402. away the node `a'.
  1403. ii. If 'b' is in the STATE_LOG[STR_IDX+strlen('s')] but 'b' is
  1404. thrown away, we throw away the node `a'.
  1405. 3. When 0 <= STR_IDX < MATCH_LAST and 'a' epsilon transit to 'b':
  1406. i. If 'b' isn't in the STATE_LOG[STR_IDX], we throw away the
  1407. node `a'.
  1408. ii. If 'b' is in the STATE_LOG[STR_IDX] but 'b' is thrown away,
  1409. we throw away the node `a'. */
  1410. #define STATE_NODE_CONTAINS(state,node) \
  1411. ((state) != NULL && re_node_set_contains (&(state)->nodes, node))
  1412. static reg_errcode_t
  1413. sift_states_backward (mctx, sctx)
  1414. re_match_context_t *mctx;
  1415. re_sift_context_t *sctx;
  1416. {
  1417. reg_errcode_t err;
  1418. int null_cnt = 0;
  1419. int str_idx = sctx->last_str_idx;
  1420. re_node_set cur_dest;
  1421. #ifdef DEBUG
  1422. assert (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL);
  1423. #endif
  1424. /* Build sifted state_log[str_idx]. It has the nodes which can epsilon
  1425. transit to the last_node and the last_node itself. */
  1426. err = re_node_set_init_1 (&cur_dest, sctx->last_node);
  1427. if (BE (err != REG_NOERROR, 0))
  1428. return err;
  1429. err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
  1430. if (BE (err != REG_NOERROR, 0))
  1431. goto free_return;
  1432. /* Then check each states in the state_log. */
  1433. while (str_idx > 0)
  1434. {
  1435. /* Update counters. */
  1436. null_cnt = (sctx->sifted_states[str_idx] == NULL) ? null_cnt + 1 : 0;
  1437. if (null_cnt > mctx->max_mb_elem_len)
  1438. {
  1439. memset (sctx->sifted_states, '\0',
  1440. sizeof (re_dfastate_t *) * str_idx);
  1441. re_node_set_free (&cur_dest);
  1442. return REG_NOERROR;
  1443. }
  1444. re_node_set_empty (&cur_dest);
  1445. --str_idx;
  1446. if (mctx->state_log[str_idx])
  1447. {
  1448. err = build_sifted_states (mctx, sctx, str_idx, &cur_dest);
  1449. if (BE (err != REG_NOERROR, 0))
  1450. goto free_return;
  1451. }
  1452. /* Add all the …

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