PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/amanda/tags/3_3_0beta1/gnulib/regexec.c

#
C | 2178 lines | 1733 code | 181 blank | 264 comment | 494 complexity | 086d34ef636b9c226baaa75c9049653c MD5 | raw file

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

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