]>
Commit | Line | Data |
---|---|---|
3b0bdc72 | 1 | /* Extended regular expression matching and search library. |
dff8da6b | 2 | Copyright (C) 2002-2024 Free Software Foundation, Inc. |
3b0bdc72 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
eb04c213 | 18 | <https://www.gnu.org/licenses/>. */ |
e054f494 | 19 | |
a9388965 | 20 | static reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags, |
eb04c213 | 21 | Idx n); |
b41bd5bc FW |
22 | static void match_ctx_clean (re_match_context_t *mctx); |
23 | static void match_ctx_free (re_match_context_t *cache); | |
eb04c213 AZ |
24 | static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, Idx node, |
25 | Idx str_idx, Idx from, Idx to); | |
26 | static Idx search_cur_bkref_entry (const re_match_context_t *mctx, Idx str_idx); | |
27 | static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, Idx node, | |
28 | Idx str_idx); | |
6291ee3c | 29 | static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop, |
eb04c213 | 30 | Idx node, Idx str_idx); |
0742e48e | 31 | static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts, |
eb04c213 AZ |
32 | re_dfastate_t **limited_sts, Idx last_node, |
33 | Idx last_str_idx); | |
a9388965 | 34 | static reg_errcode_t re_search_internal (const regex_t *preg, |
eb04c213 AZ |
35 | const char *string, Idx length, |
36 | Idx start, Idx last_start, Idx stop, | |
15a7d175 | 37 | size_t nmatch, regmatch_t pmatch[], |
b41bd5bc | 38 | int eflags); |
eb04c213 AZ |
39 | static regoff_t re_search_2_stub (struct re_pattern_buffer *bufp, |
40 | const char *string1, Idx length1, | |
41 | const char *string2, Idx length2, | |
42 | Idx start, regoff_t range, | |
43 | struct re_registers *regs, | |
44 | Idx stop, bool ret_len); | |
45 | static regoff_t re_search_stub (struct re_pattern_buffer *bufp, | |
46 | const char *string, Idx length, Idx start, | |
47 | regoff_t range, Idx stop, | |
48 | struct re_registers *regs, | |
49 | bool ret_len); | |
ac3d553b | 50 | static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, |
eb04c213 | 51 | Idx nregs, int regs_allocated); |
b41bd5bc | 52 | static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx); |
eb04c213 AZ |
53 | static Idx check_matching (re_match_context_t *mctx, bool fl_longest_match, |
54 | Idx *p_match_first); | |
55 | static Idx check_halt_state_context (const re_match_context_t *mctx, | |
56 | const re_dfastate_t *state, Idx idx); | |
76b864c8 | 57 | static void update_regs (const re_dfa_t *dfa, regmatch_t *pmatch, |
eb04c213 AZ |
58 | regmatch_t *prev_idx_match, Idx cur_node, |
59 | Idx cur_idx, Idx nmatch); | |
1b2c2628 | 60 | static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs, |
eb04c213 | 61 | Idx str_idx, Idx dest_node, Idx nregs, |
0b5ca7c3 | 62 | regmatch_t *regs, regmatch_t *prevregs, |
b41bd5bc | 63 | re_node_set *eps_via_nodes); |
612546c6 | 64 | static reg_errcode_t set_regs (const regex_t *preg, |
15a7d175 UD |
65 | const re_match_context_t *mctx, |
66 | size_t nmatch, regmatch_t *pmatch, | |
eb04c213 | 67 | bool fl_backtrack); |
b41bd5bc | 68 | static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs); |
485d775d | 69 | |
434d3784 | 70 | #ifdef RE_ENABLE_I18N |
e3a87852 | 71 | static int sift_states_iter_mb (const re_match_context_t *mctx, |
15a7d175 | 72 | re_sift_context_t *sctx, |
eb04c213 | 73 | Idx node_idx, Idx str_idx, Idx max_str_idx); |
434d3784 | 74 | #endif /* RE_ENABLE_I18N */ |
76b864c8 | 75 | static reg_errcode_t sift_states_backward (const re_match_context_t *mctx, |
b41bd5bc | 76 | re_sift_context_t *sctx); |
76b864c8 | 77 | static reg_errcode_t build_sifted_states (const re_match_context_t *mctx, |
eb04c213 | 78 | re_sift_context_t *sctx, Idx str_idx, |
b41bd5bc | 79 | re_node_set *cur_dest); |
76b864c8 | 80 | static reg_errcode_t update_cur_sifted_state (const re_match_context_t *mctx, |
15a7d175 | 81 | re_sift_context_t *sctx, |
eb04c213 | 82 | Idx str_idx, |
b41bd5bc | 83 | re_node_set *dest_nodes); |
76b864c8 | 84 | static reg_errcode_t add_epsilon_src_nodes (const re_dfa_t *dfa, |
15a7d175 | 85 | re_node_set *dest_nodes, |
b41bd5bc | 86 | const re_node_set *candidates); |
eb04c213 AZ |
87 | static bool check_dst_limits (const re_match_context_t *mctx, |
88 | const re_node_set *limits, | |
89 | Idx dst_node, Idx dst_idx, Idx src_node, | |
90 | Idx src_idx); | |
01ed6ceb | 91 | static int check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, |
eb04c213 AZ |
92 | int boundaries, Idx subexp_idx, |
93 | Idx from_node, Idx bkref_idx); | |
01ed6ceb | 94 | static int check_dst_limits_calc_pos (const re_match_context_t *mctx, |
eb04c213 AZ |
95 | Idx limit, Idx subexp_idx, |
96 | Idx node, Idx str_idx, | |
97 | Idx bkref_idx); | |
76b864c8 | 98 | static reg_errcode_t check_subexp_limits (const re_dfa_t *dfa, |
15a7d175 UD |
99 | re_node_set *dest_nodes, |
100 | const re_node_set *candidates, | |
101 | re_node_set *limits, | |
102 | struct re_backref_cache_entry *bkref_ents, | |
eb04c213 | 103 | Idx str_idx); |
76b864c8 | 104 | static reg_errcode_t sift_states_bkref (const re_match_context_t *mctx, |
15a7d175 | 105 | re_sift_context_t *sctx, |
eb04c213 | 106 | Idx str_idx, const re_node_set *candidates); |
76b864c8 UD |
107 | static reg_errcode_t merge_state_array (const re_dfa_t *dfa, |
108 | re_dfastate_t **dst, | |
eb04c213 | 109 | re_dfastate_t **src, Idx num); |
4c595adb | 110 | static re_dfastate_t *find_recover_state (reg_errcode_t *err, |
b41bd5bc | 111 | re_match_context_t *mctx); |
e3a87852 | 112 | static re_dfastate_t *transit_state (reg_errcode_t *err, |
15a7d175 | 113 | re_match_context_t *mctx, |
b41bd5bc | 114 | re_dfastate_t *state); |
4c595adb UD |
115 | static re_dfastate_t *merge_state_with_log (reg_errcode_t *err, |
116 | re_match_context_t *mctx, | |
b41bd5bc | 117 | re_dfastate_t *next_state); |
e3a87852 | 118 | static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx, |
6291ee3c | 119 | re_node_set *cur_nodes, |
eb04c213 | 120 | Idx str_idx); |
c13c99fa | 121 | #if 0 |
e3a87852 UD |
122 | static re_dfastate_t *transit_state_sb (reg_errcode_t *err, |
123 | re_match_context_t *mctx, | |
b41bd5bc | 124 | re_dfastate_t *pstate); |
c13c99fa | 125 | #endif |
434d3784 | 126 | #ifdef RE_ENABLE_I18N |
e3a87852 | 127 | static reg_errcode_t transit_state_mb (re_match_context_t *mctx, |
b41bd5bc | 128 | re_dfastate_t *pstate); |
434d3784 | 129 | #endif /* RE_ENABLE_I18N */ |
e3a87852 | 130 | static reg_errcode_t transit_state_bkref (re_match_context_t *mctx, |
b41bd5bc | 131 | const re_node_set *nodes); |
e3a87852 | 132 | static reg_errcode_t get_subexp (re_match_context_t *mctx, |
eb04c213 | 133 | Idx bkref_node, Idx bkref_str_idx); |
e3a87852 | 134 | static reg_errcode_t get_subexp_sub (re_match_context_t *mctx, |
fe9434bb | 135 | const re_sub_match_top_t *sub_top, |
6291ee3c | 136 | re_sub_match_last_t *sub_last, |
eb04c213 AZ |
137 | Idx bkref_node, Idx bkref_str); |
138 | static Idx find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes, | |
139 | Idx subexp_idx, int type); | |
e3a87852 | 140 | static reg_errcode_t check_arrival (re_match_context_t *mctx, |
eb04c213 AZ |
141 | state_array_t *path, Idx top_node, |
142 | Idx top_str, Idx last_node, Idx last_str, | |
b41bd5bc | 143 | int type); |
e3a87852 | 144 | static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx, |
eb04c213 | 145 | Idx str_idx, |
6291ee3c | 146 | re_node_set *cur_nodes, |
b41bd5bc | 147 | re_node_set *next_nodes); |
6efbd82c | 148 | static reg_errcode_t check_arrival_expand_ecl (const re_dfa_t *dfa, |
6291ee3c | 149 | re_node_set *cur_nodes, |
eb04c213 | 150 | Idx ex_subexp, int type); |
6efbd82c | 151 | static reg_errcode_t check_arrival_expand_ecl_sub (const re_dfa_t *dfa, |
6291ee3c | 152 | re_node_set *dst_nodes, |
eb04c213 | 153 | Idx target, Idx ex_subexp, |
b41bd5bc | 154 | int type); |
e3a87852 | 155 | static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx, |
eb04c213 AZ |
156 | re_node_set *cur_nodes, Idx cur_str, |
157 | Idx subexp_num, int type); | |
158 | static bool build_trtable (const re_dfa_t *dfa, re_dfastate_t *state); | |
434d3784 | 159 | #ifdef RE_ENABLE_I18N |
eb04c213 AZ |
160 | static int check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx, |
161 | const re_string_t *input, Idx idx); | |
434d3784 | 162 | # ifdef _LIBC |
c202c2c5 | 163 | static unsigned int find_collation_sequence_value (const unsigned char *mbs, |
b41bd5bc | 164 | size_t name_len); |
434d3784 UD |
165 | # endif /* _LIBC */ |
166 | #endif /* RE_ENABLE_I18N */ | |
eb04c213 | 167 | static Idx group_nodes_into_DFAstates (const re_dfa_t *dfa, |
15a7d175 UD |
168 | const re_dfastate_t *state, |
169 | re_node_set *states_node, | |
b41bd5bc | 170 | bitset_t *states_ch); |
eb04c213 AZ |
171 | static bool check_node_accept (const re_match_context_t *mctx, |
172 | const re_token_t *node, Idx idx); | |
b41bd5bc | 173 | static reg_errcode_t extend_buffers (re_match_context_t *mctx, int min_len); |
3b0bdc72 UD |
174 | \f |
175 | /* Entry point for POSIX code. */ | |
176 | ||
177 | /* regexec searches for a given pattern, specified by PREG, in the | |
178 | string STRING. | |
179 | ||
180 | If NMATCH is zero or REG_NOSUB was set in the cflags argument to | |
eb04c213 | 181 | 'regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at |
3b0bdc72 UD |
182 | least NMATCH elements, and we set them to the offsets of the |
183 | corresponding matched substrings. | |
184 | ||
eb04c213 | 185 | EFLAGS specifies "execution flags" which affect matching: if |
3b0bdc72 UD |
186 | REG_NOTBOL is set, then ^ does not match at the beginning of the |
187 | string; if REG_NOTEOL is set, then $ does not match at the end. | |
188 | ||
0b5ca7c3 PE |
189 | Return 0 if a match is found, REG_NOMATCH if not, REG_BADPAT if |
190 | EFLAGS is invalid. */ | |
3b0bdc72 UD |
191 | |
192 | int | |
c0feb731 | 193 | regexec (const regex_t *__restrict preg, const char *__restrict string, |
0b5ca7c3 | 194 | size_t nmatch, regmatch_t pmatch[_REGEX_NELTS (nmatch)], int eflags) |
3b0bdc72 | 195 | { |
a9388965 | 196 | reg_errcode_t err; |
eb04c213 AZ |
197 | Idx start, length; |
198 | re_dfa_t *dfa = preg->buffer; | |
3f2fb223 UD |
199 | |
200 | if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND)) | |
201 | return REG_BADPAT; | |
202 | ||
6fefb4e0 UD |
203 | if (eflags & REG_STARTEND) |
204 | { | |
205 | start = pmatch[0].rm_so; | |
206 | length = pmatch[0].rm_eo; | |
207 | } | |
208 | else | |
209 | { | |
210 | start = 0; | |
211 | length = strlen (string); | |
212 | } | |
7b918993 | 213 | |
eb04c213 | 214 | lock_lock (dfa->lock); |
3b0bdc72 | 215 | if (preg->no_sub) |
eb04c213 | 216 | err = re_search_internal (preg, string, length, start, length, |
6fefb4e0 | 217 | length, 0, NULL, eflags); |
3b0bdc72 | 218 | else |
eb04c213 | 219 | err = re_search_internal (preg, string, length, start, length, |
6fefb4e0 | 220 | length, nmatch, pmatch, eflags); |
eb04c213 | 221 | lock_unlock (dfa->lock); |
a9388965 | 222 | return err != REG_NOERROR; |
3b0bdc72 | 223 | } |
3f2fb223 | 224 | |
3b0bdc72 | 225 | #ifdef _LIBC |
b68f8620 L |
226 | libc_hidden_def (__regexec) |
227 | ||
3f2fb223 UD |
228 | # include <shlib-compat.h> |
229 | versioned_symbol (libc, __regexec, regexec, GLIBC_2_3_4); | |
230 | ||
231 | # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4) | |
232 | __typeof__ (__regexec) __compat_regexec; | |
233 | ||
234 | int | |
78678039 | 235 | attribute_compat_text_section |
c0feb731 PE |
236 | __compat_regexec (const regex_t *__restrict preg, |
237 | const char *__restrict string, size_t nmatch, | |
0b5ca7c3 | 238 | regmatch_t pmatch[_REGEX_NELTS (nmatch)], int eflags) |
3f2fb223 UD |
239 | { |
240 | return regexec (preg, string, nmatch, pmatch, | |
241 | eflags & (REG_NOTBOL | REG_NOTEOL)); | |
242 | } | |
243 | compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0); | |
244 | # endif | |
3b0bdc72 UD |
245 | #endif |
246 | ||
247 | /* Entry points for GNU code. */ | |
248 | ||
ac3d553b UD |
249 | /* re_match, re_search, re_match_2, re_search_2 |
250 | ||
251 | The former two functions operate on STRING with length LENGTH, | |
252 | while the later two operate on concatenation of STRING1 and STRING2 | |
253 | with lengths LENGTH1 and LENGTH2, respectively. | |
254 | ||
255 | re_match() matches the compiled pattern in BUFP against the string, | |
256 | starting at index START. | |
257 | ||
258 | re_search() first tries matching at index START, then it tries to match | |
259 | starting from index START + 1, and so on. The last start position tried | |
260 | is START + RANGE. (Thus RANGE = 0 forces re_search to operate the same | |
261 | way as re_match().) | |
262 | ||
263 | The parameter STOP of re_{match,search}_2 specifies that no match exceeding | |
264 | the first STOP characters of the concatenation of the strings should be | |
265 | concerned. | |
266 | ||
267 | If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match | |
5069ff32 | 268 | and all groups is stored in REGS. (For the "_2" variants, the offsets are |
ac3d553b UD |
269 | computed relative to the concatenation, not relative to the individual |
270 | strings.) | |
271 | ||
272 | On success, re_match* functions return the length of the match, re_search* | |
0b5ca7c3 PE |
273 | return the position of the start of the match. They return -1 on |
274 | match failure, -2 on error. */ | |
3b0bdc72 | 275 | |
eb04c213 AZ |
276 | regoff_t |
277 | re_match (struct re_pattern_buffer *bufp, const char *string, Idx length, | |
278 | Idx start, struct re_registers *regs) | |
3b0bdc72 | 279 | { |
eb04c213 | 280 | return re_search_stub (bufp, string, length, start, 0, length, regs, true); |
3b0bdc72 UD |
281 | } |
282 | #ifdef _LIBC | |
283 | weak_alias (__re_match, re_match) | |
284 | #endif | |
285 | ||
eb04c213 AZ |
286 | regoff_t |
287 | re_search (struct re_pattern_buffer *bufp, const char *string, Idx length, | |
288 | Idx start, regoff_t range, struct re_registers *regs) | |
ac3d553b | 289 | { |
eb04c213 AZ |
290 | return re_search_stub (bufp, string, length, start, range, length, regs, |
291 | false); | |
ac3d553b UD |
292 | } |
293 | #ifdef _LIBC | |
294 | weak_alias (__re_search, re_search) | |
295 | #endif | |
3b0bdc72 | 296 | |
eb04c213 AZ |
297 | regoff_t |
298 | re_match_2 (struct re_pattern_buffer *bufp, const char *string1, Idx length1, | |
299 | const char *string2, Idx length2, Idx start, | |
300 | struct re_registers *regs, Idx stop) | |
3b0bdc72 | 301 | { |
ac3d553b | 302 | return re_search_2_stub (bufp, string1, length1, string2, length2, |
eb04c213 | 303 | start, 0, regs, stop, true); |
3b0bdc72 UD |
304 | } |
305 | #ifdef _LIBC | |
306 | weak_alias (__re_match_2, re_match_2) | |
307 | #endif | |
308 | ||
eb04c213 AZ |
309 | regoff_t |
310 | re_search_2 (struct re_pattern_buffer *bufp, const char *string1, Idx length1, | |
311 | const char *string2, Idx length2, Idx start, regoff_t range, | |
312 | struct re_registers *regs, Idx stop) | |
ac3d553b UD |
313 | { |
314 | return re_search_2_stub (bufp, string1, length1, string2, length2, | |
eb04c213 | 315 | start, range, regs, stop, false); |
ac3d553b UD |
316 | } |
317 | #ifdef _LIBC | |
318 | weak_alias (__re_search_2, re_search_2) | |
319 | #endif | |
320 | ||
eb04c213 | 321 | static regoff_t |
85231522 | 322 | re_search_2_stub (struct re_pattern_buffer *bufp, const char *string1, |
eb04c213 AZ |
323 | Idx length1, const char *string2, Idx length2, Idx start, |
324 | regoff_t range, struct re_registers *regs, | |
325 | Idx stop, bool ret_len) | |
ac3d553b UD |
326 | { |
327 | const char *str; | |
eb04c213 AZ |
328 | regoff_t rval; |
329 | Idx len; | |
d044d844 | 330 | char *s = NULL; |
ac3d553b | 331 | |
f4efbdfb PE |
332 | if (__glibc_unlikely ((length1 < 0 || length2 < 0 || stop < 0 |
333 | || INT_ADD_WRAPV (length1, length2, &len)))) | |
ac3d553b UD |
334 | return -2; |
335 | ||
336 | /* Concatenate the strings. */ | |
337 | if (length2 > 0) | |
338 | if (length1 > 0) | |
339 | { | |
d044d844 | 340 | s = re_malloc (char, len); |
15a7d175 | 341 | |
f4efbdfb | 342 | if (__glibc_unlikely (s == NULL)) |
15a7d175 | 343 | return -2; |
c42b4152 UD |
344 | #ifdef _LIBC |
345 | memcpy (__mempcpy (s, string1, length1), string2, length2); | |
346 | #else | |
15a7d175 UD |
347 | memcpy (s, string1, length1); |
348 | memcpy (s + length1, string2, length2); | |
c42b4152 | 349 | #endif |
15a7d175 | 350 | str = s; |
ac3d553b UD |
351 | } |
352 | else | |
353 | str = string2; | |
354 | else | |
355 | str = string1; | |
356 | ||
eb04c213 AZ |
357 | rval = re_search_stub (bufp, str, len, start, range, stop, regs, |
358 | ret_len); | |
d044d844 | 359 | re_free (s); |
ac3d553b UD |
360 | return rval; |
361 | } | |
362 | ||
363 | /* The parameters have the same meaning as those of re_search. | |
364 | Additional parameters: | |
eb04c213 | 365 | If RET_LEN is true the length of the match is returned (re_match style); |
ac3d553b UD |
366 | otherwise the position of the match is returned. */ |
367 | ||
eb04c213 AZ |
368 | static regoff_t |
369 | re_search_stub (struct re_pattern_buffer *bufp, const char *string, Idx length, | |
370 | Idx start, regoff_t range, Idx stop, struct re_registers *regs, | |
371 | bool ret_len) | |
3b0bdc72 | 372 | { |
a9388965 | 373 | reg_errcode_t result; |
3b0bdc72 | 374 | regmatch_t *pmatch; |
eb04c213 AZ |
375 | Idx nregs; |
376 | regoff_t rval; | |
ac3d553b | 377 | int eflags = 0; |
eb04c213 AZ |
378 | re_dfa_t *dfa = bufp->buffer; |
379 | Idx last_start = start + range; | |
ac3d553b UD |
380 | |
381 | /* Check for out-of-range. */ | |
f4efbdfb | 382 | if (__glibc_unlikely (start < 0 || start > length)) |
ac3d553b | 383 | return -1; |
f4efbdfb PE |
384 | if (__glibc_unlikely (length < last_start |
385 | || (0 <= range && last_start < start))) | |
eb04c213 | 386 | last_start = length; |
f4efbdfb PE |
387 | else if (__glibc_unlikely (last_start < 0 |
388 | || (range < 0 && start <= last_start))) | |
eb04c213 | 389 | last_start = 0; |
3b0bdc72 | 390 | |
eb04c213 | 391 | lock_lock (dfa->lock); |
7b918993 | 392 | |
3b0bdc72 UD |
393 | eflags |= (bufp->not_bol) ? REG_NOTBOL : 0; |
394 | eflags |= (bufp->not_eol) ? REG_NOTEOL : 0; | |
395 | ||
ac3d553b | 396 | /* Compile fastmap if we haven't yet. */ |
eb04c213 | 397 | if (start < last_start && bufp->fastmap != NULL && !bufp->fastmap_accurate) |
ac3d553b UD |
398 | re_compile_fastmap (bufp); |
399 | ||
f4efbdfb | 400 | if (__glibc_unlikely (bufp->no_sub)) |
ac3d553b | 401 | regs = NULL; |
3b0bdc72 UD |
402 | |
403 | /* We need at least 1 register. */ | |
ac3d553b UD |
404 | if (regs == NULL) |
405 | nregs = 1; | |
f4efbdfb PE |
406 | else if (__glibc_unlikely (bufp->regs_allocated == REGS_FIXED |
407 | && regs->num_regs <= bufp->re_nsub)) | |
ac3d553b UD |
408 | { |
409 | nregs = regs->num_regs; | |
f4efbdfb | 410 | if (__glibc_unlikely (nregs < 1)) |
15a7d175 UD |
411 | { |
412 | /* Nothing can be copied to regs. */ | |
413 | regs = NULL; | |
414 | nregs = 1; | |
415 | } | |
ac3d553b UD |
416 | } |
417 | else | |
418 | nregs = bufp->re_nsub + 1; | |
3b0bdc72 | 419 | pmatch = re_malloc (regmatch_t, nregs); |
f4efbdfb | 420 | if (__glibc_unlikely (pmatch == NULL)) |
7b918993 UD |
421 | { |
422 | rval = -2; | |
423 | goto out; | |
424 | } | |
3b0bdc72 | 425 | |
eb04c213 | 426 | result = re_search_internal (bufp, string, length, start, last_start, stop, |
15a7d175 | 427 | nregs, pmatch, eflags); |
3b0bdc72 | 428 | |
ac3d553b UD |
429 | rval = 0; |
430 | ||
eb04c213 | 431 | /* I hope we needn't fill their regs with -1's when no match was found. */ |
ac3d553b | 432 | if (result != REG_NOERROR) |
eb04c213 | 433 | rval = result == REG_NOMATCH ? -1 : -2; |
ac3d553b | 434 | else if (regs != NULL) |
3b0bdc72 | 435 | { |
ac3d553b UD |
436 | /* If caller wants register contents data back, copy them. */ |
437 | bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs, | |
15a7d175 | 438 | bufp->regs_allocated); |
f4efbdfb | 439 | if (__glibc_unlikely (bufp->regs_allocated == REGS_UNALLOCATED)) |
15a7d175 | 440 | rval = -2; |
3b0bdc72 UD |
441 | } |
442 | ||
f4efbdfb | 443 | if (__glibc_likely (rval == 0)) |
3b0bdc72 | 444 | { |
ac3d553b | 445 | if (ret_len) |
15a7d175 | 446 | { |
2a0356e1 | 447 | DEBUG_ASSERT (pmatch[0].rm_so == start); |
15a7d175 UD |
448 | rval = pmatch[0].rm_eo - start; |
449 | } | |
ac3d553b | 450 | else |
15a7d175 | 451 | rval = pmatch[0].rm_so; |
3b0bdc72 | 452 | } |
3b0bdc72 | 453 | re_free (pmatch); |
7b918993 | 454 | out: |
eb04c213 | 455 | lock_unlock (dfa->lock); |
3b0bdc72 UD |
456 | return rval; |
457 | } | |
3b0bdc72 | 458 | |
ac3d553b | 459 | static unsigned |
eb04c213 | 460 | re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, Idx nregs, |
9dd346ff | 461 | int regs_allocated) |
3b0bdc72 | 462 | { |
ac3d553b | 463 | int rval = REGS_REALLOCATE; |
eb04c213 AZ |
464 | Idx i; |
465 | Idx need_regs = nregs + 1; | |
466 | /* We need one extra element beyond 'num_regs' for the '-1' marker GNU code | |
ac3d553b UD |
467 | uses. */ |
468 | ||
469 | /* Have the register data arrays been allocated? */ | |
470 | if (regs_allocated == REGS_UNALLOCATED) | |
44777771 | 471 | { /* No. So allocate them with malloc. */ |
a96c63ed | 472 | regs->start = re_malloc (regoff_t, need_regs); |
f4efbdfb | 473 | if (__glibc_unlikely (regs->start == NULL)) |
15a7d175 | 474 | return REGS_UNALLOCATED; |
74bc9f14 | 475 | regs->end = re_malloc (regoff_t, need_regs); |
f4efbdfb | 476 | if (__glibc_unlikely (regs->end == NULL)) |
74bc9f14 PE |
477 | { |
478 | re_free (regs->start); | |
479 | return REGS_UNALLOCATED; | |
480 | } | |
ac3d553b UD |
481 | regs->num_regs = need_regs; |
482 | } | |
483 | else if (regs_allocated == REGS_REALLOCATE) | |
484 | { /* Yes. If we need more elements than were already | |
15a7d175 UD |
485 | allocated, reallocate them. If we need fewer, just |
486 | leave it alone. */ | |
f4efbdfb | 487 | if (__glibc_unlikely (need_regs > regs->num_regs)) |
15a7d175 | 488 | { |
44777771 | 489 | regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs); |
74bc9f14 | 490 | regoff_t *new_end; |
f4efbdfb | 491 | if (__glibc_unlikely (new_start == NULL)) |
a96c63ed | 492 | return REGS_UNALLOCATED; |
74bc9f14 | 493 | new_end = re_realloc (regs->end, regoff_t, need_regs); |
f4efbdfb | 494 | if (__glibc_unlikely (new_end == NULL)) |
74bc9f14 PE |
495 | { |
496 | re_free (new_start); | |
497 | return REGS_UNALLOCATED; | |
498 | } | |
44777771 UD |
499 | regs->start = new_start; |
500 | regs->end = new_end; | |
15a7d175 UD |
501 | regs->num_regs = need_regs; |
502 | } | |
ac3d553b UD |
503 | } |
504 | else | |
505 | { | |
2a0356e1 | 506 | DEBUG_ASSERT (regs_allocated == REGS_FIXED); |
ac3d553b | 507 | /* This function may not be called with REGS_FIXED and nregs too big. */ |
2a0356e1 | 508 | DEBUG_ASSERT (nregs <= regs->num_regs); |
ac3d553b UD |
509 | rval = REGS_FIXED; |
510 | } | |
511 | ||
512 | /* Copy the regs. */ | |
513 | for (i = 0; i < nregs; ++i) | |
514 | { | |
515 | regs->start[i] = pmatch[i].rm_so; | |
516 | regs->end[i] = pmatch[i].rm_eo; | |
517 | } | |
518 | for ( ; i < regs->num_regs; ++i) | |
519 | regs->start[i] = regs->end[i] = -1; | |
520 | ||
521 | return rval; | |
3b0bdc72 | 522 | } |
3b0bdc72 UD |
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 | ||
530 | If NUM_REGS == 0, then subsequent matches should allocate their own | |
531 | register data. | |
532 | ||
533 | Unless this function is called, the first search or match using | |
534 | PATTERN_BUFFER will allocate its own register data, without | |
535 | freeing the old data. */ | |
536 | ||
537 | void | |
9dd346ff | 538 | re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs, |
eb04c213 | 539 | __re_size_t num_regs, regoff_t *starts, regoff_t *ends) |
3b0bdc72 UD |
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; | |
eb04c213 | 552 | regs->start = regs->end = NULL; |
3b0bdc72 UD |
553 | } |
554 | } | |
555 | #ifdef _LIBC | |
556 | weak_alias (__re_set_registers, re_set_registers) | |
557 | #endif | |
558 | \f | |
559 | /* Entry points compatible with 4.2 BSD regex library. We don't define | |
560 | them unless specifically requested. */ | |
561 | ||
562 | #if defined _REGEX_RE_COMP || defined _LIBC | |
563 | int | |
564 | # ifdef _LIBC | |
565 | weak_function | |
566 | # endif | |
80d9be81 | 567 | re_exec (const char *s) |
3b0bdc72 UD |
568 | { |
569 | return 0 == regexec (&re_comp_buf, s, 0, NULL, 0); | |
570 | } | |
571 | #endif /* _REGEX_RE_COMP */ | |
572 | \f | |
3b0bdc72 UD |
573 | /* Internal entry point. */ |
574 | ||
575 | /* Searches for a compiled pattern PREG in the string STRING, whose | |
576 | length is LENGTH. NMATCH, PMATCH, and EFLAGS have the same | |
eb04c213 AZ |
577 | meaning as with regexec. LAST_START is START + RANGE, where |
578 | START and RANGE have the same meaning as with re_search. | |
a9388965 UD |
579 | Return REG_NOERROR if we find a match, and REG_NOMATCH if not, |
580 | otherwise return the error code. | |
3b0bdc72 | 581 | Note: We assume front end functions already check ranges. |
eb04c213 | 582 | (0 <= LAST_START && LAST_START <= LENGTH) */ |
3b0bdc72 | 583 | |
a9388965 | 584 | static reg_errcode_t |
b41bd5bc | 585 | __attribute_warn_unused_result__ |
eb04c213 AZ |
586 | re_search_internal (const regex_t *preg, const char *string, Idx length, |
587 | Idx start, Idx last_start, Idx stop, size_t nmatch, | |
85231522 | 588 | regmatch_t pmatch[], int eflags) |
3b0bdc72 | 589 | { |
a9388965 | 590 | reg_errcode_t err; |
eb04c213 AZ |
591 | const re_dfa_t *dfa = preg->buffer; |
592 | Idx left_lim, right_lim; | |
593 | int incr; | |
594 | bool fl_longest_match; | |
595 | int match_kind; | |
596 | Idx match_first; | |
597 | Idx match_last = -1; | |
598 | Idx extra_nmatch; | |
599 | bool sb; | |
600 | int ch; | |
e3a87852 | 601 | re_match_context_t mctx = { .dfa = dfa }; |
eb04c213 AZ |
602 | char *fastmap = ((preg->fastmap != NULL && preg->fastmap_accurate |
603 | && start != last_start && !preg->can_be_null) | |
604 | ? preg->fastmap : NULL); | |
997470b3 | 605 | RE_TRANSLATE_TYPE t = preg->translate; |
3b0bdc72 | 606 | |
963d8d78 UD |
607 | extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0; |
608 | nmatch -= extra_nmatch; | |
609 | ||
3b0bdc72 | 610 | /* Check if the DFA haven't been compiled. */ |
f4efbdfb PE |
611 | if (__glibc_unlikely (preg->used == 0 || dfa->init_state == NULL |
612 | || dfa->init_state_word == NULL | |
613 | || dfa->init_state_nl == NULL | |
614 | || dfa->init_state_begbuf == NULL)) | |
a9388965 | 615 | return REG_NOMATCH; |
3b0bdc72 | 616 | |
c70f81dd | 617 | /* We assume front-end functions already check them. */ |
2a0356e1 | 618 | DEBUG_ASSERT (0 <= last_start && last_start <= length); |
c70f81dd UD |
619 | |
620 | /* If initial states with non-begbuf contexts have no elements, | |
621 | the regex must be anchored. If preg->newline_anchor is set, | |
622 | we'll never use init_state_nl, so do not check it. */ | |
623 | if (dfa->init_state->nodes.nelem == 0 | |
624 | && dfa->init_state_word->nodes.nelem == 0 | |
625 | && (dfa->init_state_nl->nodes.nelem == 0 | |
626 | || !preg->newline_anchor)) | |
627 | { | |
eb04c213 AZ |
628 | if (start != 0 && last_start != 0) |
629 | return REG_NOMATCH; | |
630 | start = last_start = 0; | |
c70f81dd UD |
631 | } |
632 | ||
3b0bdc72 | 633 | /* We must check the longest matching, if nmatch > 0. */ |
6291ee3c | 634 | fl_longest_match = (nmatch != 0 || dfa->nbackref); |
3b0bdc72 | 635 | |
56b168be | 636 | err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1, |
eb04c213 AZ |
637 | preg->translate, (preg->syntax & RE_ICASE) != 0, |
638 | dfa); | |
f4efbdfb | 639 | if (__glibc_unlikely (err != REG_NOERROR)) |
1b2c2628 | 640 | goto free_return; |
56b168be UD |
641 | mctx.input.stop = stop; |
642 | mctx.input.raw_stop = stop; | |
643 | mctx.input.newline_anchor = preg->newline_anchor; | |
612546c6 | 644 | |
56b168be | 645 | err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2); |
f4efbdfb | 646 | if (__glibc_unlikely (err != REG_NOERROR)) |
1b2c2628 | 647 | goto free_return; |
612546c6 | 648 | |
3b0bdc72 UD |
649 | /* We will log all the DFA states through which the dfa pass, |
650 | if nmatch > 1, or this dfa has "multibyte node", which is a | |
651 | back-reference or a node which can accept multibyte character or | |
652 | multi character collating element. */ | |
653 | if (nmatch > 1 || dfa->has_mb_node) | |
a9388965 | 654 | { |
eadc09f2 | 655 | /* Avoid overflow. */ |
f4efbdfb PE |
656 | if (__glibc_unlikely ((MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *)) |
657 | <= mctx.input.bufs_len))) | |
eadc09f2 PE |
658 | { |
659 | err = REG_ESPACE; | |
660 | goto free_return; | |
661 | } | |
662 | ||
56b168be | 663 | mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1); |
f4efbdfb | 664 | if (__glibc_unlikely (mctx.state_log == NULL)) |
15a7d175 UD |
665 | { |
666 | err = REG_ESPACE; | |
667 | goto free_return; | |
668 | } | |
a9388965 | 669 | } |
3b0bdc72 | 670 | |
612546c6 | 671 | match_first = start; |
56b168be UD |
672 | mctx.input.tip_context = (eflags & REG_NOTBOL) ? CONTEXT_BEGBUF |
673 | : CONTEXT_NEWLINE | CONTEXT_BEGBUF; | |
612546c6 | 674 | |
eb04c213 AZ |
675 | /* Check incrementally whether the input string matches. */ |
676 | incr = (last_start < start) ? -1 : 1; | |
677 | left_lim = (last_start < start) ? last_start : start; | |
678 | right_lim = (last_start < start) ? start : last_start; | |
3c0fb574 | 679 | sb = dfa->mb_cur_max == 1; |
bb677c95 UD |
680 | match_kind = |
681 | (fastmap | |
682 | ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0) | |
eb04c213 | 683 | | (start <= last_start ? 2 : 0) |
bb677c95 UD |
684 | | (t != NULL ? 1 : 0)) |
685 | : 8); | |
686 | ||
687 | for (;; match_first += incr) | |
3b0bdc72 | 688 | { |
bb677c95 UD |
689 | err = REG_NOMATCH; |
690 | if (match_first < left_lim || right_lim < match_first) | |
691 | goto free_return; | |
692 | ||
693 | /* Advance as rapidly as possible through the string, until we | |
694 | find a plausible place to start matching. This may be done | |
695 | with varying efficiency, so there are various possibilities: | |
696 | only the most common of them are specialized, in order to | |
697 | save on code size. We use a switch statement for speed. */ | |
698 | switch (match_kind) | |
1b2c2628 | 699 | { |
bb677c95 UD |
700 | case 8: |
701 | /* No fastmap. */ | |
702 | break; | |
703 | ||
704 | case 7: | |
705 | /* Fastmap with single-byte translation, match forward. */ | |
f4efbdfb | 706 | while (__glibc_likely (match_first < right_lim) |
bb677c95 UD |
707 | && !fastmap[t[(unsigned char) string[match_first]]]) |
708 | ++match_first; | |
709 | goto forward_match_found_start_or_reached_end; | |
710 | ||
711 | case 6: | |
712 | /* Fastmap without translation, match forward. */ | |
f4efbdfb | 713 | while (__glibc_likely (match_first < right_lim) |
bb677c95 UD |
714 | && !fastmap[(unsigned char) string[match_first]]) |
715 | ++match_first; | |
716 | ||
717 | forward_match_found_start_or_reached_end: | |
f4efbdfb | 718 | if (__glibc_unlikely (match_first == right_lim)) |
1b2c2628 | 719 | { |
bb677c95 UD |
720 | ch = match_first >= length |
721 | ? 0 : (unsigned char) string[match_first]; | |
722 | if (!fastmap[t ? t[ch] : ch]) | |
723 | goto free_return; | |
1b2c2628 | 724 | } |
bb677c95 UD |
725 | break; |
726 | ||
727 | case 4: | |
728 | case 5: | |
729 | /* Fastmap without multi-byte translation, match backwards. */ | |
730 | while (match_first >= left_lim) | |
1b2c2628 | 731 | { |
bb677c95 UD |
732 | ch = match_first >= length |
733 | ? 0 : (unsigned char) string[match_first]; | |
734 | if (fastmap[t ? t[ch] : ch]) | |
735 | break; | |
736 | --match_first; | |
737 | } | |
738 | if (match_first < left_lim) | |
739 | goto free_return; | |
740 | break; | |
1b2c2628 | 741 | |
bb677c95 UD |
742 | default: |
743 | /* In this case, we can't determine easily the current byte, | |
744 | since it might be a component byte of a multibyte | |
745 | character. Then we use the constructed buffer instead. */ | |
746 | for (;;) | |
747 | { | |
748 | /* If MATCH_FIRST is out of the valid range, reconstruct the | |
749 | buffers. */ | |
eb04c213 | 750 | __re_size_t offset = match_first - mctx.input.raw_mbs_idx; |
f4efbdfb PE |
751 | if (__glibc_unlikely (offset |
752 | >= (__re_size_t) mctx.input.valid_raw_len)) | |
1b2c2628 | 753 | { |
bb677c95 UD |
754 | err = re_string_reconstruct (&mctx.input, match_first, |
755 | eflags); | |
f4efbdfb | 756 | if (__glibc_unlikely (err != REG_NOERROR)) |
bb677c95 UD |
757 | goto free_return; |
758 | ||
759 | offset = match_first - mctx.input.raw_mbs_idx; | |
1b2c2628 | 760 | } |
c52ef248 PE |
761 | /* Use buffer byte if OFFSET is in buffer, otherwise '\0'. */ |
762 | ch = (offset < mctx.input.valid_len | |
763 | ? re_string_byte_at (&mctx.input, offset) : 0); | |
bb677c95 | 764 | if (fastmap[ch]) |
1b2c2628 | 765 | break; |
bb677c95 UD |
766 | match_first += incr; |
767 | if (match_first < left_lim || match_first > right_lim) | |
2da42bc0 UD |
768 | { |
769 | err = REG_NOMATCH; | |
770 | goto free_return; | |
771 | } | |
1b2c2628 | 772 | } |
bb677c95 | 773 | break; |
1b2c2628 | 774 | } |
612546c6 | 775 | |
1b2c2628 | 776 | /* Reconstruct the buffers so that the matcher can assume that |
fe9434bb | 777 | the matching starts from the beginning of the buffer. */ |
56b168be | 778 | err = re_string_reconstruct (&mctx.input, match_first, eflags); |
f4efbdfb | 779 | if (__glibc_unlikely (err != REG_NOERROR)) |
1b2c2628 | 780 | goto free_return; |
bb677c95 | 781 | |
3b0bdc72 | 782 | #ifdef RE_ENABLE_I18N |
bb677c95 UD |
783 | /* Don't consider this char as a possible match start if it part, |
784 | yet isn't the head, of a multibyte character. */ | |
785 | if (!sb && !re_string_first_byte (&mctx.input, 0)) | |
786 | continue; | |
3b0bdc72 | 787 | #endif |
bb677c95 UD |
788 | |
789 | /* It seems to be appropriate one, then use the matcher. */ | |
790 | /* We assume that the matching starts from 0. */ | |
791 | mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0; | |
792 | match_last = check_matching (&mctx, fl_longest_match, | |
eb04c213 | 793 | start <= last_start ? &match_first : NULL); |
bb677c95 | 794 | if (match_last != -1) |
1b2c2628 | 795 | { |
f4efbdfb | 796 | if (__glibc_unlikely (match_last == -2)) |
1b2c2628 | 797 | { |
bb677c95 UD |
798 | err = REG_ESPACE; |
799 | goto free_return; | |
800 | } | |
801 | else | |
802 | { | |
803 | mctx.match_last = match_last; | |
804 | if ((!preg->no_sub && nmatch > 1) || dfa->nbackref) | |
1b2c2628 | 805 | { |
bb677c95 UD |
806 | re_dfastate_t *pstate = mctx.state_log[match_last]; |
807 | mctx.last_node = check_halt_state_context (&mctx, pstate, | |
808 | match_last); | |
1b2c2628 | 809 | } |
bb677c95 UD |
810 | if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match) |
811 | || dfa->nbackref) | |
6291ee3c | 812 | { |
bb677c95 UD |
813 | err = prune_impossible_nodes (&mctx); |
814 | if (err == REG_NOERROR) | |
815 | break; | |
f4efbdfb | 816 | if (__glibc_unlikely (err != REG_NOMATCH)) |
bb677c95 UD |
817 | goto free_return; |
818 | match_last = -1; | |
6291ee3c | 819 | } |
bb677c95 UD |
820 | else |
821 | break; /* We found a match. */ | |
1b2c2628 UD |
822 | } |
823 | } | |
bb677c95 UD |
824 | |
825 | match_ctx_clean (&mctx); | |
3b0bdc72 UD |
826 | } |
827 | ||
2a0356e1 AZ |
828 | DEBUG_ASSERT (match_last != -1); |
829 | DEBUG_ASSERT (err == REG_NOERROR); | |
bb677c95 | 830 | |
3b0bdc72 | 831 | /* Set pmatch[] if we need. */ |
bb677c95 | 832 | if (nmatch > 0) |
3b0bdc72 | 833 | { |
eb04c213 | 834 | Idx reg_idx; |
3b0bdc72 UD |
835 | |
836 | /* Initialize registers. */ | |
97fd3a30 | 837 | for (reg_idx = 1; reg_idx < nmatch; ++reg_idx) |
15a7d175 | 838 | pmatch[reg_idx].rm_so = pmatch[reg_idx].rm_eo = -1; |
3b0bdc72 UD |
839 | |
840 | /* Set the points where matching start/end. */ | |
612546c6 | 841 | pmatch[0].rm_so = 0; |
6291ee3c | 842 | pmatch[0].rm_eo = mctx.match_last; |
eb04c213 AZ |
843 | /* FIXME: This function should fail if mctx.match_last exceeds |
844 | the maximum possible regoff_t value. We need a new error | |
845 | code REG_OVERFLOW. */ | |
3b0bdc72 UD |
846 | |
847 | if (!preg->no_sub && nmatch > 1) | |
15a7d175 | 848 | { |
15a7d175 UD |
849 | err = set_regs (preg, &mctx, nmatch, pmatch, |
850 | dfa->has_plural_match && dfa->nbackref > 0); | |
f4efbdfb | 851 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
852 | goto free_return; |
853 | } | |
612546c6 | 854 | |
5069ff32 | 855 | /* At last, add the offset to each register, since we slid |
97fd3a30 UD |
856 | the buffers so that we could assume that the matching starts |
857 | from 0. */ | |
612546c6 | 858 | for (reg_idx = 0; reg_idx < nmatch; ++reg_idx) |
15a7d175 UD |
859 | if (pmatch[reg_idx].rm_so != -1) |
860 | { | |
c0d5034e | 861 | #ifdef RE_ENABLE_I18N |
f4efbdfb | 862 | if (__glibc_unlikely (mctx.input.offsets_needed != 0)) |
bb3f4825 | 863 | { |
01ed6ceb UD |
864 | pmatch[reg_idx].rm_so = |
865 | (pmatch[reg_idx].rm_so == mctx.input.valid_len | |
866 | ? mctx.input.valid_raw_len | |
867 | : mctx.input.offsets[pmatch[reg_idx].rm_so]); | |
868 | pmatch[reg_idx].rm_eo = | |
869 | (pmatch[reg_idx].rm_eo == mctx.input.valid_len | |
870 | ? mctx.input.valid_raw_len | |
871 | : mctx.input.offsets[pmatch[reg_idx].rm_eo]); | |
bb3f4825 | 872 | } |
c0d5034e | 873 | #else |
2a0356e1 | 874 | DEBUG_ASSERT (mctx.input.offsets_needed == 0); |
c0d5034e | 875 | #endif |
15a7d175 UD |
876 | pmatch[reg_idx].rm_so += match_first; |
877 | pmatch[reg_idx].rm_eo += match_first; | |
878 | } | |
963d8d78 UD |
879 | for (reg_idx = 0; reg_idx < extra_nmatch; ++reg_idx) |
880 | { | |
881 | pmatch[nmatch + reg_idx].rm_so = -1; | |
882 | pmatch[nmatch + reg_idx].rm_eo = -1; | |
883 | } | |
c06a6956 UD |
884 | |
885 | if (dfa->subexp_map) | |
2da42bc0 UD |
886 | for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++) |
887 | if (dfa->subexp_map[reg_idx] != reg_idx) | |
888 | { | |
889 | pmatch[reg_idx + 1].rm_so | |
890 | = pmatch[dfa->subexp_map[reg_idx] + 1].rm_so; | |
891 | pmatch[reg_idx + 1].rm_eo | |
892 | = pmatch[dfa->subexp_map[reg_idx] + 1].rm_eo; | |
893 | } | |
3b0bdc72 | 894 | } |
bb677c95 | 895 | |
1b2c2628 | 896 | free_return: |
612546c6 | 897 | re_free (mctx.state_log); |
3b0bdc72 UD |
898 | if (dfa->nbackref) |
899 | match_ctx_free (&mctx); | |
56b168be | 900 | re_string_destruct (&mctx.input); |
1b2c2628 | 901 | return err; |
3b0bdc72 UD |
902 | } |
903 | ||
6291ee3c | 904 | static reg_errcode_t |
b41bd5bc | 905 | __attribute_warn_unused_result__ |
9dd346ff | 906 | prune_impossible_nodes (re_match_context_t *mctx) |
6291ee3c | 907 | { |
76b864c8 | 908 | const re_dfa_t *const dfa = mctx->dfa; |
eb04c213 | 909 | Idx halt_node, match_last; |
6291ee3c | 910 | reg_errcode_t ret; |
6291ee3c UD |
911 | re_dfastate_t **sifted_states; |
912 | re_dfastate_t **lim_states = NULL; | |
913 | re_sift_context_t sctx; | |
2a0356e1 | 914 | DEBUG_ASSERT (mctx->state_log != NULL); |
6291ee3c UD |
915 | match_last = mctx->match_last; |
916 | halt_node = mctx->last_node; | |
4cd02867 PE |
917 | |
918 | /* Avoid overflow. */ | |
f4efbdfb PE |
919 | if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *)) |
920 | <= match_last)) | |
4cd02867 PE |
921 | return REG_ESPACE; |
922 | ||
6291ee3c | 923 | sifted_states = re_malloc (re_dfastate_t *, match_last + 1); |
f4efbdfb | 924 | if (__glibc_unlikely (sifted_states == NULL)) |
6291ee3c UD |
925 | { |
926 | ret = REG_ESPACE; | |
927 | goto free_return; | |
928 | } | |
929 | if (dfa->nbackref) | |
930 | { | |
931 | lim_states = re_malloc (re_dfastate_t *, match_last + 1); | |
f4efbdfb | 932 | if (__glibc_unlikely (lim_states == NULL)) |
6291ee3c UD |
933 | { |
934 | ret = REG_ESPACE; | |
935 | goto free_return; | |
936 | } | |
937 | while (1) | |
938 | { | |
939 | memset (lim_states, '\0', | |
940 | sizeof (re_dfastate_t *) * (match_last + 1)); | |
6291ee3c | 941 | sift_ctx_init (&sctx, sifted_states, lim_states, halt_node, |
d2c38eb3 | 942 | match_last); |
e3a87852 | 943 | ret = sift_states_backward (mctx, &sctx); |
6291ee3c | 944 | re_node_set_free (&sctx.limits); |
f4efbdfb | 945 | if (__glibc_unlikely (ret != REG_NOERROR)) |
6291ee3c UD |
946 | goto free_return; |
947 | if (sifted_states[0] != NULL || lim_states[0] != NULL) | |
948 | break; | |
949 | do | |
950 | { | |
951 | --match_last; | |
952 | if (match_last < 0) | |
953 | { | |
954 | ret = REG_NOMATCH; | |
955 | goto free_return; | |
956 | } | |
97fd3a30 UD |
957 | } while (mctx->state_log[match_last] == NULL |
958 | || !mctx->state_log[match_last]->halt); | |
e3a87852 | 959 | halt_node = check_halt_state_context (mctx, |
6291ee3c | 960 | mctx->state_log[match_last], |
e3a87852 | 961 | match_last); |
6291ee3c UD |
962 | } |
963 | ret = merge_state_array (dfa, sifted_states, lim_states, | |
964 | match_last + 1); | |
965 | re_free (lim_states); | |
966 | lim_states = NULL; | |
f4efbdfb | 967 | if (__glibc_unlikely (ret != REG_NOERROR)) |
6291ee3c UD |
968 | goto free_return; |
969 | } | |
970 | else | |
971 | { | |
d2c38eb3 | 972 | sift_ctx_init (&sctx, sifted_states, lim_states, halt_node, match_last); |
e3a87852 | 973 | ret = sift_states_backward (mctx, &sctx); |
6291ee3c | 974 | re_node_set_free (&sctx.limits); |
f4efbdfb | 975 | if (__glibc_unlikely (ret != REG_NOERROR)) |
6291ee3c | 976 | goto free_return; |
76c7f2cd UD |
977 | if (sifted_states[0] == NULL) |
978 | { | |
979 | ret = REG_NOMATCH; | |
980 | goto free_return; | |
981 | } | |
6291ee3c UD |
982 | } |
983 | re_free (mctx->state_log); | |
984 | mctx->state_log = sifted_states; | |
985 | sifted_states = NULL; | |
986 | mctx->last_node = halt_node; | |
987 | mctx->match_last = match_last; | |
988 | ret = REG_NOERROR; | |
989 | free_return: | |
990 | re_free (sifted_states); | |
991 | re_free (lim_states); | |
992 | return ret; | |
993 | } | |
994 | ||
a9388965 | 995 | /* Acquire an initial state and return it. |
3b0bdc72 UD |
996 | We must select appropriate initial state depending on the context, |
997 | since initial states may have constraints like "\<", "^", etc.. */ | |
998 | ||
bb3f4825 | 999 | static inline re_dfastate_t * |
eb04c213 | 1000 | __attribute__ ((always_inline)) |
e2f55264 | 1001 | acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx, |
eb04c213 | 1002 | Idx idx) |
3b0bdc72 | 1003 | { |
76b864c8 | 1004 | const re_dfa_t *const dfa = mctx->dfa; |
3b0bdc72 UD |
1005 | if (dfa->init_state->has_constraint) |
1006 | { | |
1007 | unsigned int context; | |
56b168be | 1008 | context = re_string_context_at (&mctx->input, idx - 1, mctx->eflags); |
3b0bdc72 | 1009 | if (IS_WORD_CONTEXT (context)) |
15a7d175 | 1010 | return dfa->init_state_word; |
3b0bdc72 | 1011 | else if (IS_ORDINARY_CONTEXT (context)) |
15a7d175 | 1012 | return dfa->init_state; |
3b0bdc72 | 1013 | else if (IS_BEGBUF_CONTEXT (context) && IS_NEWLINE_CONTEXT (context)) |
15a7d175 | 1014 | return dfa->init_state_begbuf; |
3b0bdc72 | 1015 | else if (IS_NEWLINE_CONTEXT (context)) |
15a7d175 | 1016 | return dfa->init_state_nl; |
3b0bdc72 | 1017 | else if (IS_BEGBUF_CONTEXT (context)) |
15a7d175 UD |
1018 | { |
1019 | /* It is relatively rare case, then calculate on demand. */ | |
4c595adb UD |
1020 | return re_acquire_state_context (err, dfa, |
1021 | dfa->init_state->entrance_nodes, | |
1022 | context); | |
15a7d175 | 1023 | } |
3b0bdc72 | 1024 | else |
15a7d175 UD |
1025 | /* Must not happen? */ |
1026 | return dfa->init_state; | |
3b0bdc72 UD |
1027 | } |
1028 | else | |
1029 | return dfa->init_state; | |
1030 | } | |
1031 | ||
1032 | /* Check whether the regular expression match input string INPUT or not, | |
eb04c213 AZ |
1033 | and return the index where the matching end. Return -1 if |
1034 | there is no match, and return -2 in case of an error. | |
612546c6 | 1035 | FL_LONGEST_MATCH means we want the POSIX longest matching. |
15bad1a5 UD |
1036 | If P_MATCH_FIRST is not NULL, and the match fails, it is set to the |
1037 | next place where we may want to try matching. | |
eb04c213 | 1038 | Note that the matcher assumes that the matching starts from the current |
612546c6 | 1039 | index of the buffer. */ |
3b0bdc72 | 1040 | |
eb04c213 | 1041 | static Idx |
b41bd5bc | 1042 | __attribute_warn_unused_result__ |
eb04c213 AZ |
1043 | check_matching (re_match_context_t *mctx, bool fl_longest_match, |
1044 | Idx *p_match_first) | |
3b0bdc72 | 1045 | { |
76b864c8 | 1046 | const re_dfa_t *const dfa = mctx->dfa; |
a9388965 | 1047 | reg_errcode_t err; |
eb04c213 AZ |
1048 | Idx match = 0; |
1049 | Idx match_last = -1; | |
1050 | Idx cur_str_idx = re_string_cur_idx (&mctx->input); | |
3b0bdc72 | 1051 | re_dfastate_t *cur_state; |
eb04c213 AZ |
1052 | bool at_init_state = p_match_first != NULL; |
1053 | Idx next_start_idx = cur_str_idx; | |
3b0bdc72 | 1054 | |
4c595adb | 1055 | err = REG_NOERROR; |
e3a87852 | 1056 | cur_state = acquire_init_state_context (&err, mctx, cur_str_idx); |
4c595adb | 1057 | /* An initial state must not be NULL (invalid). */ |
f4efbdfb | 1058 | if (__glibc_unlikely (cur_state == NULL)) |
4c595adb | 1059 | { |
2a0356e1 | 1060 | DEBUG_ASSERT (err == REG_ESPACE); |
4c595adb UD |
1061 | return -2; |
1062 | } | |
0742e48e | 1063 | |
4c595adb | 1064 | if (mctx->state_log != NULL) |
6291ee3c | 1065 | { |
4c595adb | 1066 | mctx->state_log[cur_str_idx] = cur_state; |
6291ee3c | 1067 | |
4c595adb UD |
1068 | /* Check OP_OPEN_SUBEXP in the initial state in case that we use them |
1069 | later. E.g. Processing back references. */ | |
f4efbdfb | 1070 | if (__glibc_unlikely (dfa->nbackref)) |
bb3f4825 | 1071 | { |
eb04c213 | 1072 | at_init_state = false; |
4c595adb | 1073 | err = check_subexp_matching_top (mctx, &cur_state->nodes, 0); |
f4efbdfb | 1074 | if (__glibc_unlikely (err != REG_NOERROR)) |
bb3f4825 | 1075 | return err; |
4c595adb UD |
1076 | |
1077 | if (cur_state->has_backref) | |
1078 | { | |
1079 | err = transit_state_bkref (mctx, &cur_state->nodes); | |
f4efbdfb | 1080 | if (__glibc_unlikely (err != REG_NOERROR)) |
2da42bc0 | 1081 | return err; |
4c595adb | 1082 | } |
bb3f4825 | 1083 | } |
0742e48e UD |
1084 | } |
1085 | ||
3b0bdc72 | 1086 | /* If the RE accepts NULL string. */ |
f4efbdfb | 1087 | if (__glibc_unlikely (cur_state->halt)) |
3b0bdc72 UD |
1088 | { |
1089 | if (!cur_state->has_constraint | |
e3a87852 | 1090 | || check_halt_state_context (mctx, cur_state, cur_str_idx)) |
15a7d175 UD |
1091 | { |
1092 | if (!fl_longest_match) | |
1093 | return cur_str_idx; | |
1094 | else | |
1095 | { | |
1096 | match_last = cur_str_idx; | |
1097 | match = 1; | |
1098 | } | |
1099 | } | |
3b0bdc72 UD |
1100 | } |
1101 | ||
56b168be | 1102 | while (!re_string_eoi (&mctx->input)) |
3b0bdc72 | 1103 | { |
15bad1a5 | 1104 | re_dfastate_t *old_state = cur_state; |
eb04c213 | 1105 | Idx next_char_idx = re_string_cur_idx (&mctx->input) + 1; |
bb677c95 | 1106 | |
f4efbdfb | 1107 | if ((__glibc_unlikely (next_char_idx >= mctx->input.bufs_len) |
8887a920 | 1108 | && mctx->input.bufs_len < mctx->input.len) |
f4efbdfb | 1109 | || (__glibc_unlikely (next_char_idx >= mctx->input.valid_len) |
2da42bc0 UD |
1110 | && mctx->input.valid_len < mctx->input.len)) |
1111 | { | |
a445af0b | 1112 | err = extend_buffers (mctx, next_char_idx + 1); |
f4efbdfb | 1113 | if (__glibc_unlikely (err != REG_NOERROR)) |
bb677c95 | 1114 | { |
2a0356e1 | 1115 | DEBUG_ASSERT (err == REG_ESPACE); |
bb677c95 UD |
1116 | return -2; |
1117 | } | |
2da42bc0 | 1118 | } |
bb677c95 | 1119 | |
e3a87852 | 1120 | cur_state = transit_state (&err, mctx, cur_state); |
4c595adb UD |
1121 | if (mctx->state_log != NULL) |
1122 | cur_state = merge_state_with_log (&err, mctx, cur_state); | |
15bad1a5 | 1123 | |
4c595adb | 1124 | if (cur_state == NULL) |
15a7d175 | 1125 | { |
4c595adb UD |
1126 | /* Reached the invalid state or an error. Try to recover a valid |
1127 | state using the state log, if available and if we have not | |
1128 | already found a valid (even if not the longest) match. */ | |
f4efbdfb | 1129 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 | 1130 | return -2; |
4c595adb UD |
1131 | |
1132 | if (mctx->state_log == NULL | |
1133 | || (match && !fl_longest_match) | |
1134 | || (cur_state = find_recover_state (&err, mctx)) == NULL) | |
15a7d175 | 1135 | break; |
4c595adb UD |
1136 | } |
1137 | ||
f4efbdfb | 1138 | if (__glibc_unlikely (at_init_state)) |
4c595adb UD |
1139 | { |
1140 | if (old_state == cur_state) | |
bb677c95 | 1141 | next_start_idx = next_char_idx; |
c13c99fa | 1142 | else |
eb04c213 | 1143 | at_init_state = false; |
15a7d175 | 1144 | } |
3b0bdc72 | 1145 | |
4c595adb | 1146 | if (cur_state->halt) |
15a7d175 | 1147 | { |
4c595adb | 1148 | /* Reached a halt state. |
15a7d175 UD |
1149 | Check the halt state can satisfy the current context. */ |
1150 | if (!cur_state->has_constraint | |
e3a87852 | 1151 | || check_halt_state_context (mctx, cur_state, |
56b168be | 1152 | re_string_cur_idx (&mctx->input))) |
15a7d175 UD |
1153 | { |
1154 | /* We found an appropriate halt state. */ | |
56b168be | 1155 | match_last = re_string_cur_idx (&mctx->input); |
15a7d175 | 1156 | match = 1; |
bb677c95 UD |
1157 | |
1158 | /* We found a match, do not modify match_first below. */ | |
1159 | p_match_first = NULL; | |
15a7d175 UD |
1160 | if (!fl_longest_match) |
1161 | break; | |
1162 | } | |
1163 | } | |
bb677c95 | 1164 | } |
15bad1a5 | 1165 | |
bb677c95 | 1166 | if (p_match_first) |
4c595adb | 1167 | *p_match_first += next_start_idx; |
15bad1a5 | 1168 | |
3b0bdc72 UD |
1169 | return match_last; |
1170 | } | |
1171 | ||
1172 | /* Check NODE match the current context. */ | |
1173 | ||
eb04c213 AZ |
1174 | static bool |
1175 | check_halt_node_context (const re_dfa_t *dfa, Idx node, unsigned int context) | |
3b0bdc72 | 1176 | { |
3b0bdc72 | 1177 | re_token_type_t type = dfa->nodes[node].type; |
485d775d UD |
1178 | unsigned int constraint = dfa->nodes[node].constraint; |
1179 | if (type != END_OF_RE) | |
eb04c213 | 1180 | return false; |
485d775d | 1181 | if (!constraint) |
eb04c213 | 1182 | return true; |
485d775d | 1183 | if (NOT_SATISFY_NEXT_CONSTRAINT (constraint, context)) |
eb04c213 AZ |
1184 | return false; |
1185 | return true; | |
3b0bdc72 UD |
1186 | } |
1187 | ||
1188 | /* Check the halt state STATE match the current context. | |
1189 | Return 0 if not match, if the node, STATE has, is a halt node and | |
1190 | match the context, return the node. */ | |
1191 | ||
eb04c213 | 1192 | static Idx |
e2f55264 | 1193 | check_halt_state_context (const re_match_context_t *mctx, |
eb04c213 | 1194 | const re_dfastate_t *state, Idx idx) |
3b0bdc72 | 1195 | { |
eb04c213 | 1196 | Idx i; |
3b0bdc72 | 1197 | unsigned int context; |
2a0356e1 | 1198 | DEBUG_ASSERT (state->halt); |
56b168be | 1199 | context = re_string_context_at (&mctx->input, idx, mctx->eflags); |
3b0bdc72 | 1200 | for (i = 0; i < state->nodes.nelem; ++i) |
e3a87852 | 1201 | if (check_halt_node_context (mctx->dfa, state->nodes.elems[i], context)) |
3b0bdc72 UD |
1202 | return state->nodes.elems[i]; |
1203 | return 0; | |
1204 | } | |
1205 | ||
a9388965 UD |
1206 | /* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA |
1207 | corresponding to the DFA). | |
eb04c213 | 1208 | Return the destination node, and update EPS_VIA_NODES; |
0b5ca7c3 | 1209 | return -1 on match failure, -2 on error. */ |
3b0bdc72 | 1210 | |
eb04c213 AZ |
1211 | static Idx |
1212 | proceed_next_node (const re_match_context_t *mctx, Idx nregs, regmatch_t *regs, | |
0b5ca7c3 | 1213 | regmatch_t *prevregs, |
eb04c213 | 1214 | Idx *pidx, Idx node, re_node_set *eps_via_nodes, |
e2f55264 | 1215 | struct re_fail_stack_t *fs) |
3b0bdc72 | 1216 | { |
76b864c8 | 1217 | const re_dfa_t *const dfa = mctx->dfa; |
3b0bdc72 UD |
1218 | if (IS_EPSILON_NODE (dfa->nodes[node].type)) |
1219 | { | |
485d775d | 1220 | re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes; |
5cf1ec52 | 1221 | re_node_set *edests = &dfa->edests[node]; |
0b5ca7c3 PE |
1222 | |
1223 | if (! re_node_set_contains (eps_via_nodes, node)) | |
1224 | { | |
1225 | bool ok = re_node_set_insert (eps_via_nodes, node); | |
1226 | if (__glibc_unlikely (! ok)) | |
1227 | return -2; | |
1228 | } | |
1229 | ||
1230 | /* Pick a valid destination, or return -1 if none is found. */ | |
1231 | Idx dest_node = -1; | |
1232 | for (Idx i = 0; i < edests->nelem; i++) | |
15a7d175 | 1233 | { |
eb04c213 | 1234 | Idx candidate = edests->elems[i]; |
15a7d175 UD |
1235 | if (!re_node_set_contains (cur_nodes, candidate)) |
1236 | continue; | |
eb04c213 | 1237 | if (dest_node == -1) |
5cf1ec52 UD |
1238 | dest_node = candidate; |
1239 | ||
2da42bc0 | 1240 | else |
5cf1ec52 UD |
1241 | { |
1242 | /* In order to avoid infinite loop like "(a*)*", return the second | |
2da42bc0 | 1243 | epsilon-transition if the first was already considered. */ |
5cf1ec52 | 1244 | if (re_node_set_contains (eps_via_nodes, dest_node)) |
2da42bc0 | 1245 | return candidate; |
5cf1ec52 UD |
1246 | |
1247 | /* Otherwise, push the second epsilon-transition on the fail stack. */ | |
1248 | else if (fs != NULL | |
1249 | && push_fail_stack (fs, *pidx, candidate, nregs, regs, | |
0b5ca7c3 | 1250 | prevregs, eps_via_nodes)) |
5cf1ec52 UD |
1251 | return -2; |
1252 | ||
1253 | /* We know we are going to exit. */ | |
1254 | break; | |
1255 | } | |
1256 | } | |
1257 | return dest_node; | |
3b0bdc72 UD |
1258 | } |
1259 | else | |
1260 | { | |
eb04c213 | 1261 | Idx naccepted = 0; |
3b0bdc72 | 1262 | re_token_type_t type = dfa->nodes[node].type; |
3b0bdc72 | 1263 | |
434d3784 | 1264 | #ifdef RE_ENABLE_I18N |
02f3550c | 1265 | if (dfa->nodes[node].accept_mb) |
56b168be | 1266 | naccepted = check_node_accept_bytes (dfa, node, &mctx->input, *pidx); |
434d3784 UD |
1267 | else |
1268 | #endif /* RE_ENABLE_I18N */ | |
1269 | if (type == OP_BACK_REF) | |
15a7d175 | 1270 | { |
eb04c213 | 1271 | Idx subexp_idx = dfa->nodes[node].opr.idx + 1; |
fc141ea7 AS |
1272 | if (subexp_idx < nregs) |
1273 | naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so; | |
15a7d175 UD |
1274 | if (fs != NULL) |
1275 | { | |
fc141ea7 AS |
1276 | if (subexp_idx >= nregs |
1277 | || regs[subexp_idx].rm_so == -1 | |
1278 | || regs[subexp_idx].rm_eo == -1) | |
15a7d175 UD |
1279 | return -1; |
1280 | else if (naccepted) | |
1281 | { | |
56b168be | 1282 | char *buf = (char *) re_string_get_buffer (&mctx->input); |
583dd860 PE |
1283 | if (mctx->input.valid_len - *pidx < naccepted |
1284 | || (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx, | |
1285 | naccepted) | |
1286 | != 0)) | |
15a7d175 UD |
1287 | return -1; |
1288 | } | |
1289 | } | |
1290 | ||
1291 | if (naccepted == 0) | |
1292 | { | |
eb04c213 | 1293 | Idx dest_node; |
0b5ca7c3 | 1294 | bool ok = re_node_set_insert (eps_via_nodes, node); |
f4efbdfb | 1295 | if (__glibc_unlikely (! ok)) |
15a7d175 UD |
1296 | return -2; |
1297 | dest_node = dfa->edests[node].elems[0]; | |
1298 | if (re_node_set_contains (&mctx->state_log[*pidx]->nodes, | |
1299 | dest_node)) | |
1300 | return dest_node; | |
1301 | } | |
1302 | } | |
3b0bdc72 UD |
1303 | |
1304 | if (naccepted != 0 | |
e3a87852 | 1305 | || check_node_accept (mctx, dfa->nodes + node, *pidx)) |
15a7d175 | 1306 | { |
eb04c213 | 1307 | Idx dest_node = dfa->nexts[node]; |
15a7d175 UD |
1308 | *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted; |
1309 | if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL | |
1310 | || !re_node_set_contains (&mctx->state_log[*pidx]->nodes, | |
1311 | dest_node))) | |
1312 | return -1; | |
1313 | re_node_set_empty (eps_via_nodes); | |
1314 | return dest_node; | |
1315 | } | |
3b0bdc72 | 1316 | } |
a3022b82 UD |
1317 | return -1; |
1318 | } | |
1319 | ||
1320 | static reg_errcode_t | |
b41bd5bc | 1321 | __attribute_warn_unused_result__ |
eb04c213 | 1322 | push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node, |
0b5ca7c3 PE |
1323 | Idx nregs, regmatch_t *regs, regmatch_t *prevregs, |
1324 | re_node_set *eps_via_nodes) | |
a3022b82 UD |
1325 | { |
1326 | reg_errcode_t err; | |
eb04c213 | 1327 | Idx num = fs->num++; |
a3022b82 UD |
1328 | if (fs->num == fs->alloc) |
1329 | { | |
1b2c2628 | 1330 | struct re_fail_stack_ent_t *new_array; |
eb04c213 AZ |
1331 | new_array = re_realloc (fs->stack, struct re_fail_stack_ent_t, |
1332 | fs->alloc * 2); | |
1b2c2628 | 1333 | if (new_array == NULL) |
15a7d175 | 1334 | return REG_ESPACE; |
44777771 | 1335 | fs->alloc *= 2; |
1b2c2628 | 1336 | fs->stack = new_array; |
a3022b82 UD |
1337 | } |
1338 | fs->stack[num].idx = str_idx; | |
5cf1ec52 | 1339 | fs->stack[num].node = dest_node; |
0b5ca7c3 | 1340 | fs->stack[num].regs = re_malloc (regmatch_t, 2 * nregs); |
5a299c96 UD |
1341 | if (fs->stack[num].regs == NULL) |
1342 | return REG_ESPACE; | |
a3022b82 | 1343 | memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs); |
0b5ca7c3 | 1344 | memcpy (fs->stack[num].regs + nregs, prevregs, sizeof (regmatch_t) * nregs); |
a3022b82 UD |
1345 | err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes); |
1346 | return err; | |
1347 | } | |
1b2c2628 | 1348 | |
eb04c213 AZ |
1349 | static Idx |
1350 | pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx, Idx nregs, | |
0b5ca7c3 PE |
1351 | regmatch_t *regs, regmatch_t *prevregs, |
1352 | re_node_set *eps_via_nodes) | |
a3022b82 | 1353 | { |
0b5ca7c3 PE |
1354 | if (fs == NULL || fs->num == 0) |
1355 | return -1; | |
eb04c213 | 1356 | Idx num = --fs->num; |
44777771 | 1357 | *pidx = fs->stack[num].idx; |
a3022b82 | 1358 | memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs); |
0b5ca7c3 | 1359 | memcpy (prevregs, fs->stack[num].regs + nregs, sizeof (regmatch_t) * nregs); |
a3022b82 | 1360 | re_node_set_free (eps_via_nodes); |
485d775d | 1361 | re_free (fs->stack[num].regs); |
a3022b82 | 1362 | *eps_via_nodes = fs->stack[num].eps_via_nodes; |
0b5ca7c3 | 1363 | DEBUG_ASSERT (0 <= fs->stack[num].node); |
a3022b82 | 1364 | return fs->stack[num].node; |
3b0bdc72 UD |
1365 | } |
1366 | ||
f0908ba1 AZ |
1367 | |
1368 | #define DYNARRAY_STRUCT regmatch_list | |
1369 | #define DYNARRAY_ELEMENT regmatch_t | |
1370 | #define DYNARRAY_PREFIX regmatch_list_ | |
1371 | #include <malloc/dynarray-skeleton.c> | |
1372 | ||
3b0bdc72 UD |
1373 | /* Set the positions where the subexpressions are starts/ends to registers |
1374 | PMATCH. | |
1375 | Note: We assume that pmatch[0] is already set, and | |
97fd3a30 | 1376 | pmatch[i].rm_so == pmatch[i].rm_eo == -1 for 0 < i < nmatch. */ |
3b0bdc72 | 1377 | |
a9388965 | 1378 | static reg_errcode_t |
b41bd5bc | 1379 | __attribute_warn_unused_result__ |
e2f55264 | 1380 | set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch, |
eb04c213 | 1381 | regmatch_t *pmatch, bool fl_backtrack) |
3b0bdc72 | 1382 | { |
eb04c213 AZ |
1383 | const re_dfa_t *dfa = preg->buffer; |
1384 | Idx idx, cur_node; | |
3b0bdc72 | 1385 | re_node_set eps_via_nodes; |
a3022b82 | 1386 | struct re_fail_stack_t *fs; |
6b6557e8 | 1387 | struct re_fail_stack_t fs_body = { 0, 2, NULL }; |
f0908ba1 AZ |
1388 | struct regmatch_list prev_match; |
1389 | regmatch_list_init (&prev_match); | |
6b6557e8 | 1390 | |
2a0356e1 AZ |
1391 | DEBUG_ASSERT (nmatch > 1); |
1392 | DEBUG_ASSERT (mctx->state_log != NULL); | |
a3022b82 UD |
1393 | if (fl_backtrack) |
1394 | { | |
1395 | fs = &fs_body; | |
1396 | fs->stack = re_malloc (struct re_fail_stack_ent_t, fs->alloc); | |
6b6557e8 UD |
1397 | if (fs->stack == NULL) |
1398 | return REG_ESPACE; | |
a3022b82 UD |
1399 | } |
1400 | else | |
1401 | fs = NULL; | |
6b6557e8 | 1402 | |
3b0bdc72 | 1403 | cur_node = dfa->init_node; |
3b0bdc72 | 1404 | re_node_set_init_empty (&eps_via_nodes); |
6b6557e8 | 1405 | |
f0908ba1 | 1406 | if (!regmatch_list_resize (&prev_match, nmatch)) |
2d87db5b | 1407 | { |
f0908ba1 AZ |
1408 | regmatch_list_free (&prev_match); |
1409 | free_fail_stack_return (fs); | |
1410 | return REG_ESPACE; | |
2d87db5b | 1411 | } |
f0908ba1 | 1412 | regmatch_t *prev_idx_match = regmatch_list_begin (&prev_match); |
963d8d78 | 1413 | memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch); |
6b6557e8 | 1414 | |
3b0bdc72 UD |
1415 | for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;) |
1416 | { | |
963d8d78 | 1417 | update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch); |
6b6557e8 | 1418 | |
0b5ca7c3 PE |
1419 | if ((idx == pmatch[0].rm_eo && cur_node == mctx->last_node) |
1420 | || (fs && re_node_set_contains (&eps_via_nodes, cur_node))) | |
15a7d175 | 1421 | { |
eb04c213 | 1422 | Idx reg_idx; |
0b5ca7c3 | 1423 | cur_node = -1; |
15a7d175 UD |
1424 | if (fs) |
1425 | { | |
1426 | for (reg_idx = 0; reg_idx < nmatch; ++reg_idx) | |
1427 | if (pmatch[reg_idx].rm_so > -1 && pmatch[reg_idx].rm_eo == -1) | |
0b5ca7c3 PE |
1428 | { |
1429 | cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch, | |
1430 | prev_idx_match, &eps_via_nodes); | |
1431 | break; | |
1432 | } | |
15a7d175 | 1433 | } |
0b5ca7c3 | 1434 | if (cur_node < 0) |
15a7d175 UD |
1435 | { |
1436 | re_node_set_free (&eps_via_nodes); | |
f0908ba1 | 1437 | regmatch_list_free (&prev_match); |
0b5ca7c3 | 1438 | return free_fail_stack_return (fs); |
15a7d175 UD |
1439 | } |
1440 | } | |
3b0bdc72 UD |
1441 | |
1442 | /* Proceed to next node. */ | |
0b5ca7c3 PE |
1443 | cur_node = proceed_next_node (mctx, nmatch, pmatch, prev_idx_match, |
1444 | &idx, cur_node, | |
15a7d175 | 1445 | &eps_via_nodes, fs); |
a3022b82 | 1446 | |
f4efbdfb | 1447 | if (__glibc_unlikely (cur_node < 0)) |
15a7d175 | 1448 | { |
f4efbdfb | 1449 | if (__glibc_unlikely (cur_node == -2)) |
44777771 UD |
1450 | { |
1451 | re_node_set_free (&eps_via_nodes); | |
f0908ba1 | 1452 | regmatch_list_free (&prev_match); |
44777771 UD |
1453 | free_fail_stack_return (fs); |
1454 | return REG_ESPACE; | |
1455 | } | |
0b5ca7c3 PE |
1456 | cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch, |
1457 | prev_idx_match, &eps_via_nodes); | |
1458 | if (cur_node < 0) | |
15a7d175 UD |
1459 | { |
1460 | re_node_set_free (&eps_via_nodes); | |
f0908ba1 | 1461 | regmatch_list_free (&prev_match); |
0b5ca7c3 | 1462 | free_fail_stack_return (fs); |
15a7d175 UD |
1463 | return REG_NOMATCH; |
1464 | } | |
1465 | } | |
3b0bdc72 UD |
1466 | } |
1467 | re_node_set_free (&eps_via_nodes); | |
f0908ba1 | 1468 | regmatch_list_free (&prev_match); |
485d775d UD |
1469 | return free_fail_stack_return (fs); |
1470 | } | |
1471 | ||
1472 | static reg_errcode_t | |
e2f55264 | 1473 | free_fail_stack_return (struct re_fail_stack_t *fs) |
485d775d UD |
1474 | { |
1475 | if (fs) | |
1476 | { | |
eb04c213 | 1477 | Idx fs_idx; |
485d775d | 1478 | for (fs_idx = 0; fs_idx < fs->num; ++fs_idx) |
15a7d175 UD |
1479 | { |
1480 | re_node_set_free (&fs->stack[fs_idx].eps_via_nodes); | |
1481 | re_free (fs->stack[fs_idx].regs); | |
1482 | } | |
485d775d UD |
1483 | re_free (fs->stack); |
1484 | } | |
a9388965 | 1485 | return REG_NOERROR; |
3b0bdc72 UD |
1486 | } |
1487 | ||
81c64d40 | 1488 | static void |
e2f55264 | 1489 | update_regs (const re_dfa_t *dfa, regmatch_t *pmatch, |
eb04c213 | 1490 | regmatch_t *prev_idx_match, Idx cur_node, Idx cur_idx, Idx nmatch) |
81c64d40 UD |
1491 | { |
1492 | int type = dfa->nodes[cur_node].type; | |
81c64d40 UD |
1493 | if (type == OP_OPEN_SUBEXP) |
1494 | { | |
eb04c213 | 1495 | Idx reg_num = dfa->nodes[cur_node].opr.idx + 1; |
6b6557e8 | 1496 | |
81c64d40 | 1497 | /* We are at the first node of this sub expression. */ |
6b6557e8 UD |
1498 | if (reg_num < nmatch) |
1499 | { | |
1500 | pmatch[reg_num].rm_so = cur_idx; | |
1501 | pmatch[reg_num].rm_eo = -1; | |
1502 | } | |
81c64d40 UD |
1503 | } |
1504 | else if (type == OP_CLOSE_SUBEXP) | |
6b6557e8 | 1505 | { |
0b5ca7c3 | 1506 | /* We are at the last node of this sub expression. */ |
eb04c213 | 1507 | Idx reg_num = dfa->nodes[cur_node].opr.idx + 1; |
6b6557e8 UD |
1508 | if (reg_num < nmatch) |
1509 | { | |
6b6557e8 UD |
1510 | if (pmatch[reg_num].rm_so < cur_idx) |
1511 | { | |
1512 | pmatch[reg_num].rm_eo = cur_idx; | |
1513 | /* This is a non-empty match or we are not inside an optional | |
1514 | subexpression. Accept this right away. */ | |
1515 | memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch); | |
1516 | } | |
1517 | else | |
1518 | { | |
1519 | if (dfa->nodes[cur_node].opt_subexp | |
1520 | && prev_idx_match[reg_num].rm_so != -1) | |
1521 | /* We transited through an empty match for an optional | |
1522 | subexpression, like (a?)*, and this is not the subexp's | |
1523 | first match. Copy back the old content of the registers | |
1524 | so that matches of an inner subexpression are undone as | |
1525 | well, like in ((a?))*. */ | |
1526 | memcpy (pmatch, prev_idx_match, sizeof (regmatch_t) * nmatch); | |
1527 | else | |
1528 | /* We completed a subexpression, but it may be part of | |
1529 | an optional one, so do not update PREV_IDX_MATCH. */ | |
1530 | pmatch[reg_num].rm_eo = cur_idx; | |
1531 | } | |
1532 | } | |
1533 | } | |
0742e48e | 1534 | } |
81c64d40 | 1535 | |
0742e48e | 1536 | /* This function checks the STATE_LOG from the SCTX->last_str_idx to 0 |
612546c6 UD |
1537 | and sift the nodes in each states according to the following rules. |
1538 | Updated state_log will be wrote to STATE_LOG. | |
3b0bdc72 | 1539 | |
eb04c213 | 1540 | Rules: We throw away the Node 'a' in the STATE_LOG[STR_IDX] if... |
3b0bdc72 | 1541 | 1. When STR_IDX == MATCH_LAST(the last index in the state_log): |
eb04c213 AZ |
1542 | If 'a' isn't the LAST_NODE and 'a' can't epsilon transit to |
1543 | the LAST_NODE, we throw away the node 'a'. | |
1544 | 2. When 0 <= STR_IDX < MATCH_LAST and 'a' accepts | |
1545 | string 's' and transit to 'b': | |
15a7d175 | 1546 | i. If 'b' isn't in the STATE_LOG[STR_IDX+strlen('s')], we throw |
eb04c213 | 1547 | away the node 'a'. |
15a7d175 | 1548 | ii. If 'b' is in the STATE_LOG[STR_IDX+strlen('s')] but 'b' is |
eb04c213 | 1549 | thrown away, we throw away the node 'a'. |
d0b96fc4 | 1550 | 3. When 0 <= STR_IDX < MATCH_LAST and 'a' epsilon transit to 'b': |
15a7d175 | 1551 | i. If 'b' isn't in the STATE_LOG[STR_IDX], we throw away the |
eb04c213 | 1552 | node 'a'. |
bb3f4825 | 1553 | ii. If 'b' is in the STATE_LOG[STR_IDX] but 'b' is thrown away, |
eb04c213 | 1554 | we throw away the node 'a'. */ |
3b0bdc72 UD |
1555 | |
1556 | #define STATE_NODE_CONTAINS(state,node) \ | |
1557 | ((state) != NULL && re_node_set_contains (&(state)->nodes, node)) | |
1558 | ||
a9388965 | 1559 | static reg_errcode_t |
e2f55264 | 1560 | sift_states_backward (const re_match_context_t *mctx, re_sift_context_t *sctx) |
3b0bdc72 | 1561 | { |
a9388965 | 1562 | reg_errcode_t err; |
0742e48e | 1563 | int null_cnt = 0; |
eb04c213 | 1564 | Idx str_idx = sctx->last_str_idx; |
0742e48e | 1565 | re_node_set cur_dest; |
3b0bdc72 | 1566 | |
2a0356e1 | 1567 | DEBUG_ASSERT (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL); |
3b0bdc72 UD |
1568 | |
1569 | /* Build sifted state_log[str_idx]. It has the nodes which can epsilon | |
1570 | transit to the last_node and the last_node itself. */ | |
0742e48e | 1571 | err = re_node_set_init_1 (&cur_dest, sctx->last_node); |
f4efbdfb | 1572 | if (__glibc_unlikely (err != REG_NOERROR)) |
a9388965 | 1573 | return err; |
e3a87852 | 1574 | err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest); |
f4efbdfb | 1575 | if (__glibc_unlikely (err != REG_NOERROR)) |
1b2c2628 | 1576 | goto free_return; |
3b0bdc72 UD |
1577 | |
1578 | /* Then check each states in the state_log. */ | |
612546c6 | 1579 | while (str_idx > 0) |
3b0bdc72 | 1580 | { |
3b0bdc72 | 1581 | /* Update counters. */ |
0742e48e UD |
1582 | null_cnt = (sctx->sifted_states[str_idx] == NULL) ? null_cnt + 1 : 0; |
1583 | if (null_cnt > mctx->max_mb_elem_len) | |
15a7d175 UD |
1584 | { |
1585 | memset (sctx->sifted_states, '\0', | |
1586 | sizeof (re_dfastate_t *) * str_idx); | |
1587 | re_node_set_free (&cur_dest); | |
1588 | return REG_NOERROR; | |
1589 | } | |
0742e48e | 1590 | re_node_set_empty (&cur_dest); |
3b0bdc72 | 1591 | --str_idx; |
15a7d175 | 1592 | |
e40a38b3 UD |
1593 | if (mctx->state_log[str_idx]) |
1594 | { | |
1595 | err = build_sifted_states (mctx, sctx, str_idx, &cur_dest); | |
f4efbdfb | 1596 | if (__glibc_unlikely (err != REG_NOERROR)) |
e40a38b3 | 1597 | goto free_return; |
15a7d175 | 1598 | } |
3b0bdc72 | 1599 | |
0742e48e | 1600 | /* Add all the nodes which satisfy the following conditions: |
15a7d175 UD |
1601 | - It can epsilon transit to a node in CUR_DEST. |
1602 | - It is in CUR_SRC. | |
1603 | And update state_log. */ | |
e3a87852 | 1604 | err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest); |
f4efbdfb | 1605 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 | 1606 | goto free_return; |
3b0bdc72 | 1607 | } |
1b2c2628 UD |
1608 | err = REG_NOERROR; |
1609 | free_return: | |
0742e48e | 1610 | re_node_set_free (&cur_dest); |
1b2c2628 | 1611 | return err; |
3b0bdc72 UD |
1612 | } |
1613 | ||
e40a38b3 | 1614 | static reg_errcode_t |
b41bd5bc | 1615 | __attribute_warn_unused_result__ |
e2f55264 | 1616 | build_sifted_states (const re_match_context_t *mctx, re_sift_context_t *sctx, |
eb04c213 | 1617 | Idx str_idx, re_node_set *cur_dest) |
e40a38b3 | 1618 | { |
76b864c8 UD |
1619 | const re_dfa_t *const dfa = mctx->dfa; |
1620 | const re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes; | |
eb04c213 | 1621 | Idx i; |
e40a38b3 UD |
1622 | |
1623 | /* Then build the next sifted state. | |
eb04c213 AZ |
1624 | We build the next sifted state on 'cur_dest', and update |
1625 | 'sifted_states[str_idx]' with 'cur_dest'. | |
e40a38b3 | 1626 | Note: |
eb04c213 AZ |
1627 | 'cur_dest' is the sifted state from 'state_log[str_idx + 1]'. |
1628 | 'cur_src' points the node_set of the old 'state_log[str_idx]' | |
5cf1ec52 | 1629 | (with the epsilon nodes pre-filtered out). */ |
e40a38b3 UD |
1630 | for (i = 0; i < cur_src->nelem; i++) |
1631 | { | |
eb04c213 | 1632 | Idx prev_node = cur_src->elems[i]; |
e40a38b3 | 1633 | int naccepted = 0; |
eb04c213 | 1634 | bool ok; |
2a0356e1 | 1635 | DEBUG_ASSERT (!IS_EPSILON_NODE (dfa->nodes[prev_node].type)); |
e40a38b3 | 1636 | |
e40a38b3 | 1637 | #ifdef RE_ENABLE_I18N |
eb04c213 | 1638 | /* If the node may accept "multi byte". */ |
02f3550c | 1639 | if (dfa->nodes[prev_node].accept_mb) |
e40a38b3 UD |
1640 | naccepted = sift_states_iter_mb (mctx, sctx, prev_node, |
1641 | str_idx, sctx->last_str_idx); | |
1642 | #endif /* RE_ENABLE_I18N */ | |
1643 | ||
1644 | /* We don't check backreferences here. | |
1645 | See update_cur_sifted_state(). */ | |
1646 | if (!naccepted | |
1647 | && check_node_accept (mctx, dfa->nodes + prev_node, str_idx) | |
1648 | && STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + 1], | |
1649 | dfa->nexts[prev_node])) | |
1650 | naccepted = 1; | |
1651 | ||
1652 | if (naccepted == 0) | |
1653 | continue; | |
1654 | ||
1655 | if (sctx->limits.nelem) | |
1656 | { | |
eb04c213 | 1657 | Idx to_idx = str_idx + naccepted; |
e40a38b3 UD |
1658 | if (check_dst_limits (mctx, &sctx->limits, |
1659 | dfa->nexts[prev_node], to_idx, | |
1660 | prev_node, str_idx)) | |
1661 | continue; | |
1662 | } | |
eb04c213 | 1663 | ok = re_node_set_insert (cur_dest, prev_node); |
f4efbdfb | 1664 | if (__glibc_unlikely (! ok)) |
e40a38b3 UD |
1665 | return REG_ESPACE; |
1666 | } | |
1667 | ||
1668 | return REG_NOERROR; | |
1669 | } | |
1670 | ||
3b0bdc72 UD |
1671 | /* Helper functions. */ |
1672 | ||
25337753 | 1673 | static reg_errcode_t |
eb04c213 | 1674 | clean_state_log_if_needed (re_match_context_t *mctx, Idx next_state_log_idx) |
3b0bdc72 | 1675 | { |
eb04c213 | 1676 | Idx top = mctx->state_log_top; |
612546c6 | 1677 | |
8887a920 UD |
1678 | if ((next_state_log_idx >= mctx->input.bufs_len |
1679 | && mctx->input.bufs_len < mctx->input.len) | |
56b168be UD |
1680 | || (next_state_log_idx >= mctx->input.valid_len |
1681 | && mctx->input.valid_len < mctx->input.len)) | |
612546c6 UD |
1682 | { |
1683 | reg_errcode_t err; | |
a445af0b | 1684 | err = extend_buffers (mctx, next_state_log_idx + 1); |
f4efbdfb | 1685 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 | 1686 | return err; |
612546c6 UD |
1687 | } |
1688 | ||
3b0bdc72 UD |
1689 | if (top < next_state_log_idx) |
1690 | { | |
612546c6 | 1691 | memset (mctx->state_log + top + 1, '\0', |
15a7d175 | 1692 | sizeof (re_dfastate_t *) * (next_state_log_idx - top)); |
3b0bdc72 UD |
1693 | mctx->state_log_top = next_state_log_idx; |
1694 | } | |
612546c6 | 1695 | return REG_NOERROR; |
3b0bdc72 UD |
1696 | } |
1697 | ||
1b2c2628 | 1698 | static reg_errcode_t |
e2f55264 | 1699 | merge_state_array (const re_dfa_t *dfa, re_dfastate_t **dst, |
eb04c213 | 1700 | re_dfastate_t **src, Idx num) |
3b0bdc72 | 1701 | { |
eb04c213 | 1702 | Idx st_idx; |
0742e48e UD |
1703 | reg_errcode_t err; |
1704 | for (st_idx = 0; st_idx < num; ++st_idx) | |
1705 | { | |
1706 | if (dst[st_idx] == NULL) | |
15a7d175 | 1707 | dst[st_idx] = src[st_idx]; |
0742e48e | 1708 | else if (src[st_idx] != NULL) |
15a7d175 UD |
1709 | { |
1710 | re_node_set merged_set; | |
1711 | err = re_node_set_init_union (&merged_set, &dst[st_idx]->nodes, | |
1712 | &src[st_idx]->nodes); | |
f4efbdfb | 1713 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
1714 | return err; |
1715 | dst[st_idx] = re_acquire_state (&err, dfa, &merged_set); | |
1716 | re_node_set_free (&merged_set); | |
f4efbdfb | 1717 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
1718 | return err; |
1719 | } | |
0742e48e UD |
1720 | } |
1721 | return REG_NOERROR; | |
3b0bdc72 UD |
1722 | } |
1723 | ||
0742e48e | 1724 | static reg_errcode_t |
e2f55264 | 1725 | update_cur_sifted_state (const re_match_context_t *mctx, |
eb04c213 | 1726 | re_sift_context_t *sctx, Idx str_idx, |
e2f55264 | 1727 | re_node_set *dest_nodes) |
3b0bdc72 | 1728 | { |
76b864c8 | 1729 | const re_dfa_t *const dfa = mctx->dfa; |
c0c9615c | 1730 | reg_errcode_t err = REG_NOERROR; |
0742e48e | 1731 | const re_node_set *candidates; |
e40a38b3 | 1732 | candidates = ((mctx->state_log[str_idx] == NULL) ? NULL |
15a7d175 | 1733 | : &mctx->state_log[str_idx]->nodes); |
0742e48e | 1734 | |
e40a38b3 UD |
1735 | if (dest_nodes->nelem == 0) |
1736 | sctx->sifted_states[str_idx] = NULL; | |
1737 | else | |
485d775d | 1738 | { |
e40a38b3 UD |
1739 | if (candidates) |
1740 | { | |
1741 | /* At first, add the nodes which can epsilon transit to a node in | |
1742 | DEST_NODE. */ | |
1743 | err = add_epsilon_src_nodes (dfa, dest_nodes, candidates); | |
f4efbdfb | 1744 | if (__glibc_unlikely (err != REG_NOERROR)) |
e40a38b3 | 1745 | return err; |
0742e48e | 1746 | |
e40a38b3 UD |
1747 | /* Then, check the limitations in the current sift_context. */ |
1748 | if (sctx->limits.nelem) | |
1749 | { | |
1750 | err = check_subexp_limits (dfa, dest_nodes, candidates, &sctx->limits, | |
1751 | mctx->bkref_ents, str_idx); | |
f4efbdfb | 1752 | if (__glibc_unlikely (err != REG_NOERROR)) |
e40a38b3 UD |
1753 | return err; |
1754 | } | |
1755 | } | |
1756 | ||
1757 | sctx->sifted_states[str_idx] = re_acquire_state (&err, dfa, dest_nodes); | |
f4efbdfb | 1758 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 | 1759 | return err; |
0742e48e UD |
1760 | } |
1761 | ||
e40a38b3 | 1762 | if (candidates && mctx->state_log[str_idx]->has_backref) |
0742e48e | 1763 | { |
d2c38eb3 | 1764 | err = sift_states_bkref (mctx, sctx, str_idx, candidates); |
f4efbdfb | 1765 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 | 1766 | return err; |
0742e48e UD |
1767 | } |
1768 | return REG_NOERROR; | |
3b0bdc72 UD |
1769 | } |
1770 | ||
a9388965 | 1771 | static reg_errcode_t |
b41bd5bc | 1772 | __attribute_warn_unused_result__ |
e2f55264 UD |
1773 | add_epsilon_src_nodes (const re_dfa_t *dfa, re_node_set *dest_nodes, |
1774 | const re_node_set *candidates) | |
3b0bdc72 | 1775 | { |
5cf1ec52 | 1776 | reg_errcode_t err = REG_NOERROR; |
eb04c213 | 1777 | Idx i; |
0742e48e | 1778 | |
5cf1ec52 | 1779 | re_dfastate_t *state = re_acquire_state (&err, dfa, dest_nodes); |
f4efbdfb | 1780 | if (__glibc_unlikely (err != REG_NOERROR)) |
0742e48e | 1781 | return err; |
5cf1ec52 UD |
1782 | |
1783 | if (!state->inveclosure.alloc) | |
3b0bdc72 | 1784 | { |
5cf1ec52 | 1785 | err = re_node_set_alloc (&state->inveclosure, dest_nodes->nelem); |
f4efbdfb | 1786 | if (__glibc_unlikely (err != REG_NOERROR)) |
2da42bc0 | 1787 | return REG_ESPACE; |
5cf1ec52 | 1788 | for (i = 0; i < dest_nodes->nelem; i++) |
2da42bc0 UD |
1789 | { |
1790 | err = re_node_set_merge (&state->inveclosure, | |
1791 | dfa->inveclosures + dest_nodes->elems[i]); | |
f4efbdfb | 1792 | if (__glibc_unlikely (err != REG_NOERROR)) |
2da42bc0 UD |
1793 | return REG_ESPACE; |
1794 | } | |
0742e48e | 1795 | } |
5cf1ec52 UD |
1796 | return re_node_set_add_intersect (dest_nodes, candidates, |
1797 | &state->inveclosure); | |
0742e48e | 1798 | } |
3b0bdc72 | 1799 | |
0742e48e | 1800 | static reg_errcode_t |
eb04c213 | 1801 | sub_epsilon_src_nodes (const re_dfa_t *dfa, Idx node, re_node_set *dest_nodes, |
e2f55264 | 1802 | const re_node_set *candidates) |
0742e48e | 1803 | { |
eb04c213 | 1804 | Idx ecl_idx; |
0742e48e UD |
1805 | reg_errcode_t err; |
1806 | re_node_set *inv_eclosure = dfa->inveclosures + node; | |
1807 | re_node_set except_nodes; | |
1808 | re_node_set_init_empty (&except_nodes); | |
1809 | for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx) | |
1810 | { | |
eb04c213 | 1811 | Idx cur_node = inv_eclosure->elems[ecl_idx]; |
15a7d175 UD |
1812 | if (cur_node == node) |
1813 | continue; | |
1814 | if (IS_EPSILON_NODE (dfa->nodes[cur_node].type)) | |
1815 | { | |
eb04c213 AZ |
1816 | Idx edst1 = dfa->edests[cur_node].elems[0]; |
1817 | Idx edst2 = ((dfa->edests[cur_node].nelem > 1) | |
15a7d175 UD |
1818 | ? dfa->edests[cur_node].elems[1] : -1); |
1819 | if ((!re_node_set_contains (inv_eclosure, edst1) | |
1820 | && re_node_set_contains (dest_nodes, edst1)) | |
1821 | || (edst2 > 0 | |
1822 | && !re_node_set_contains (inv_eclosure, edst2) | |
1823 | && re_node_set_contains (dest_nodes, edst2))) | |
1824 | { | |
1825 | err = re_node_set_add_intersect (&except_nodes, candidates, | |
1826 | dfa->inveclosures + cur_node); | |
f4efbdfb | 1827 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
1828 | { |
1829 | re_node_set_free (&except_nodes); | |
1830 | return err; | |
1831 | } | |
1832 | } | |
1833 | } | |
0742e48e UD |
1834 | } |
1835 | for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx) | |
1836 | { | |
eb04c213 | 1837 | Idx cur_node = inv_eclosure->elems[ecl_idx]; |
15a7d175 UD |
1838 | if (!re_node_set_contains (&except_nodes, cur_node)) |
1839 | { | |
eb04c213 | 1840 | Idx idx = re_node_set_contains (dest_nodes, cur_node) - 1; |
15a7d175 UD |
1841 | re_node_set_remove_at (dest_nodes, idx); |
1842 | } | |
0742e48e UD |
1843 | } |
1844 | re_node_set_free (&except_nodes); | |
1845 | return REG_NOERROR; | |
1846 | } | |
1847 | ||
eb04c213 AZ |
1848 | static bool |
1849 | check_dst_limits (const re_match_context_t *mctx, const re_node_set *limits, | |
1850 | Idx dst_node, Idx dst_idx, Idx src_node, Idx src_idx) | |
a3022b82 | 1851 | { |
76b864c8 | 1852 | const re_dfa_t *const dfa = mctx->dfa; |
eb04c213 | 1853 | Idx lim_idx, src_pos, dst_pos; |
a3022b82 | 1854 | |
eb04c213 AZ |
1855 | Idx dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx); |
1856 | Idx src_bkref_idx = search_cur_bkref_entry (mctx, src_idx); | |
a3022b82 UD |
1857 | for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx) |
1858 | { | |
eb04c213 | 1859 | Idx subexp_idx; |
a3022b82 UD |
1860 | struct re_backref_cache_entry *ent; |
1861 | ent = mctx->bkref_ents + limits->elems[lim_idx]; | |
d8f73de8 | 1862 | subexp_idx = dfa->nodes[ent->node].opr.idx; |
a3022b82 | 1863 | |
e3a87852 | 1864 | dst_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx], |
e40a38b3 UD |
1865 | subexp_idx, dst_node, dst_idx, |
1866 | dst_bkref_idx); | |
e3a87852 | 1867 | src_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx], |
e40a38b3 UD |
1868 | subexp_idx, src_node, src_idx, |
1869 | src_bkref_idx); | |
a3022b82 UD |
1870 | |
1871 | /* In case of: | |
15a7d175 UD |
1872 | <src> <dst> ( <subexp> ) |
1873 | ( <subexp> ) <src> <dst> | |
1874 | ( <subexp1> <src> <subexp2> <dst> <subexp3> ) */ | |
a3022b82 | 1875 | if (src_pos == dst_pos) |
15a7d175 | 1876 | continue; /* This is unrelated limitation. */ |
a3022b82 | 1877 | else |
eb04c213 | 1878 | return true; |
a3022b82 | 1879 | } |
eb04c213 | 1880 | return false; |
a3022b82 UD |
1881 | } |
1882 | ||
1883 | static int | |
e2f55264 | 1884 | check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, int boundaries, |
eb04c213 | 1885 | Idx subexp_idx, Idx from_node, Idx bkref_idx) |
a3022b82 | 1886 | { |
76b864c8 UD |
1887 | const re_dfa_t *const dfa = mctx->dfa; |
1888 | const re_node_set *eclosures = dfa->eclosures + from_node; | |
eb04c213 | 1889 | Idx node_idx; |
d0b96fc4 | 1890 | |
d0b96fc4 UD |
1891 | /* Else, we are on the boundary: examine the nodes on the epsilon |
1892 | closure. */ | |
1cef7b3c UD |
1893 | for (node_idx = 0; node_idx < eclosures->nelem; ++node_idx) |
1894 | { | |
eb04c213 | 1895 | Idx node = eclosures->elems[node_idx]; |
d0b96fc4 UD |
1896 | switch (dfa->nodes[node].type) |
1897 | { | |
1898 | case OP_BACK_REF: | |
d8f73de8 UD |
1899 | if (bkref_idx != -1) |
1900 | { | |
1901 | struct re_backref_cache_entry *ent = mctx->bkref_ents + bkref_idx; | |
1902 | do | |
2da42bc0 | 1903 | { |
eb04c213 AZ |
1904 | Idx dst; |
1905 | int cpos; | |
d0b96fc4 | 1906 | |
d8f73de8 UD |
1907 | if (ent->node != node) |
1908 | continue; | |
d0b96fc4 | 1909 | |
2c05d33f UD |
1910 | if (subexp_idx < BITSET_WORD_BITS |
1911 | && !(ent->eps_reachable_subexps_map | |
1912 | & ((bitset_word_t) 1 << subexp_idx))) | |
d8f73de8 | 1913 | continue; |
d0b96fc4 | 1914 | |
d8f73de8 UD |
1915 | /* Recurse trying to reach the OP_OPEN_SUBEXP and |
1916 | OP_CLOSE_SUBEXP cases below. But, if the | |
1917 | destination node is the same node as the source | |
1918 | node, don't recurse because it would cause an | |
1919 | infinite loop: a regex that exhibits this behavior | |
1920 | is ()\1*\1* */ | |
1921 | dst = dfa->edests[node].elems[0]; | |
1922 | if (dst == from_node) | |
1923 | { | |
1924 | if (boundaries & 1) | |
2da42bc0 | 1925 | return -1; |
d8f73de8 | 1926 | else /* if (boundaries & 2) */ |
2da42bc0 | 1927 | return 0; |
d8f73de8 | 1928 | } |
7db61208 | 1929 | |
d8f73de8 UD |
1930 | cpos = |
1931 | check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx, | |
1932 | dst, bkref_idx); | |
1933 | if (cpos == -1 /* && (boundaries & 1) */) | |
1934 | return -1; | |
1935 | if (cpos == 0 && (boundaries & 2)) | |
1936 | return 0; | |
1937 | ||
2c05d33f UD |
1938 | if (subexp_idx < BITSET_WORD_BITS) |
1939 | ent->eps_reachable_subexps_map | |
1940 | &= ~((bitset_word_t) 1 << subexp_idx); | |
2da42bc0 | 1941 | } |
d8f73de8 UD |
1942 | while (ent++->more); |
1943 | } | |
1944 | break; | |
f0c7c524 | 1945 | |
d0b96fc4 | 1946 | case OP_OPEN_SUBEXP: |
e40a38b3 | 1947 | if ((boundaries & 1) && subexp_idx == dfa->nodes[node].opr.idx) |
d0b96fc4 UD |
1948 | return -1; |
1949 | break; | |
f0c7c524 | 1950 | |
d0b96fc4 | 1951 | case OP_CLOSE_SUBEXP: |
e40a38b3 | 1952 | if ((boundaries & 2) && subexp_idx == dfa->nodes[node].opr.idx) |
d0b96fc4 UD |
1953 | return 0; |
1954 | break; | |
1955 | ||
1956 | default: | |
15a7d175 UD |
1957 | break; |
1958 | } | |
a3022b82 | 1959 | } |
d0b96fc4 | 1960 | |
e40a38b3 UD |
1961 | return (boundaries & 2) ? 1 : 0; |
1962 | } | |
1963 | ||
1964 | static int | |
eb04c213 AZ |
1965 | check_dst_limits_calc_pos (const re_match_context_t *mctx, Idx limit, |
1966 | Idx subexp_idx, Idx from_node, Idx str_idx, | |
1967 | Idx bkref_idx) | |
e40a38b3 UD |
1968 | { |
1969 | struct re_backref_cache_entry *lim = mctx->bkref_ents + limit; | |
1970 | int boundaries; | |
1971 | ||
1972 | /* If we are outside the range of the subexpression, return -1 or 1. */ | |
1973 | if (str_idx < lim->subexp_from) | |
1974 | return -1; | |
1975 | ||
1976 | if (lim->subexp_to < str_idx) | |
d0b96fc4 | 1977 | return 1; |
e40a38b3 UD |
1978 | |
1979 | /* If we are within the subexpression, return 0. */ | |
1980 | boundaries = (str_idx == lim->subexp_from); | |
1981 | boundaries |= (str_idx == lim->subexp_to) << 1; | |
1982 | if (boundaries == 0) | |
d0b96fc4 | 1983 | return 0; |
e40a38b3 UD |
1984 | |
1985 | /* Else, examine epsilon closure. */ | |
1986 | return check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx, | |
1987 | from_node, bkref_idx); | |
a3022b82 UD |
1988 | } |
1989 | ||
0742e48e UD |
1990 | /* Check the limitations of sub expressions LIMITS, and remove the nodes |
1991 | which are against limitations from DEST_NODES. */ | |
1992 | ||
1993 | static reg_errcode_t | |
e2f55264 UD |
1994 | check_subexp_limits (const re_dfa_t *dfa, re_node_set *dest_nodes, |
1995 | const re_node_set *candidates, re_node_set *limits, | |
eb04c213 | 1996 | struct re_backref_cache_entry *bkref_ents, Idx str_idx) |
0742e48e UD |
1997 | { |
1998 | reg_errcode_t err; | |
eb04c213 | 1999 | Idx node_idx, lim_idx; |
0742e48e UD |
2000 | |
2001 | for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx) | |
2002 | { | |
eb04c213 | 2003 | Idx subexp_idx; |
0742e48e UD |
2004 | struct re_backref_cache_entry *ent; |
2005 | ent = bkref_ents + limits->elems[lim_idx]; | |
2006 | ||
2007 | if (str_idx <= ent->subexp_from || ent->str_idx < str_idx) | |
15a7d175 | 2008 | continue; /* This is unrelated limitation. */ |
0742e48e | 2009 | |
d8f73de8 | 2010 | subexp_idx = dfa->nodes[ent->node].opr.idx; |
0742e48e | 2011 | if (ent->subexp_to == str_idx) |
15a7d175 | 2012 | { |
eb04c213 AZ |
2013 | Idx ops_node = -1; |
2014 | Idx cls_node = -1; | |
15a7d175 UD |
2015 | for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx) |
2016 | { | |
eb04c213 | 2017 | Idx node = dest_nodes->elems[node_idx]; |
46bf9de7 | 2018 | re_token_type_t type = dfa->nodes[node].type; |
15a7d175 UD |
2019 | if (type == OP_OPEN_SUBEXP |
2020 | && subexp_idx == dfa->nodes[node].opr.idx) | |
2021 | ops_node = node; | |
2022 | else if (type == OP_CLOSE_SUBEXP | |
2023 | && subexp_idx == dfa->nodes[node].opr.idx) | |
2024 | cls_node = node; | |
2025 | } | |
2026 | ||
2027 | /* Check the limitation of the open subexpression. */ | |
2028 | /* Note that (ent->subexp_to = str_idx != ent->subexp_from). */ | |
2029 | if (ops_node >= 0) | |
2030 | { | |
46bf9de7 UD |
2031 | err = sub_epsilon_src_nodes (dfa, ops_node, dest_nodes, |
2032 | candidates); | |
f4efbdfb | 2033 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
2034 | return err; |
2035 | } | |
46bf9de7 | 2036 | |
15a7d175 | 2037 | /* Check the limitation of the close subexpression. */ |
46bf9de7 UD |
2038 | if (cls_node >= 0) |
2039 | for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx) | |
2040 | { | |
eb04c213 | 2041 | Idx node = dest_nodes->elems[node_idx]; |
46bf9de7 UD |
2042 | if (!re_node_set_contains (dfa->inveclosures + node, |
2043 | cls_node) | |
2044 | && !re_node_set_contains (dfa->eclosures + node, | |
2045 | cls_node)) | |
2046 | { | |
2047 | /* It is against this limitation. | |
2048 | Remove it form the current sifted state. */ | |
2049 | err = sub_epsilon_src_nodes (dfa, node, dest_nodes, | |
2050 | candidates); | |
f4efbdfb | 2051 | if (__glibc_unlikely (err != REG_NOERROR)) |
46bf9de7 UD |
2052 | return err; |
2053 | --node_idx; | |
2054 | } | |
2055 | } | |
15a7d175 | 2056 | } |
0742e48e | 2057 | else /* (ent->subexp_to != str_idx) */ |
15a7d175 UD |
2058 | { |
2059 | for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx) | |
2060 | { | |
eb04c213 | 2061 | Idx node = dest_nodes->elems[node_idx]; |
46bf9de7 | 2062 | re_token_type_t type = dfa->nodes[node].type; |
15a7d175 UD |
2063 | if (type == OP_CLOSE_SUBEXP || type == OP_OPEN_SUBEXP) |
2064 | { | |
2065 | if (subexp_idx != dfa->nodes[node].opr.idx) | |
2066 | continue; | |
d8f73de8 UD |
2067 | /* It is against this limitation. |
2068 | Remove it form the current sifted state. */ | |
2069 | err = sub_epsilon_src_nodes (dfa, node, dest_nodes, | |
2070 | candidates); | |
f4efbdfb | 2071 | if (__glibc_unlikely (err != REG_NOERROR)) |
d8f73de8 | 2072 | return err; |
15a7d175 UD |
2073 | } |
2074 | } | |
2075 | } | |
0742e48e UD |
2076 | } |
2077 | return REG_NOERROR; | |
2078 | } | |
2079 | ||
0742e48e | 2080 | static reg_errcode_t |
b41bd5bc | 2081 | __attribute_warn_unused_result__ |
e2f55264 | 2082 | sift_states_bkref (const re_match_context_t *mctx, re_sift_context_t *sctx, |
eb04c213 | 2083 | Idx str_idx, const re_node_set *candidates) |
0742e48e | 2084 | { |
76b864c8 | 2085 | const re_dfa_t *const dfa = mctx->dfa; |
0742e48e | 2086 | reg_errcode_t err; |
eb04c213 | 2087 | Idx node_idx, node; |
0742e48e | 2088 | re_sift_context_t local_sctx; |
eb04c213 | 2089 | Idx first_idx = search_cur_bkref_entry (mctx, str_idx); |
e40a38b3 UD |
2090 | |
2091 | if (first_idx == -1) | |
2092 | return REG_NOERROR; | |
2093 | ||
0742e48e UD |
2094 | local_sctx.sifted_states = NULL; /* Mark that it hasn't been initialized. */ |
2095 | ||
2096 | for (node_idx = 0; node_idx < candidates->nelem; ++node_idx) | |
2097 | { | |
eb04c213 | 2098 | Idx enabled_idx; |
0742e48e | 2099 | re_token_type_t type; |
e40a38b3 | 2100 | struct re_backref_cache_entry *entry; |
0742e48e UD |
2101 | node = candidates->elems[node_idx]; |
2102 | type = dfa->nodes[node].type; | |
0742e48e UD |
2103 | /* Avoid infinite loop for the REs like "()\1+". */ |
2104 | if (node == sctx->last_node && str_idx == sctx->last_str_idx) | |
15a7d175 | 2105 | continue; |
e40a38b3 UD |
2106 | if (type != OP_BACK_REF) |
2107 | continue; | |
2108 | ||
2109 | entry = mctx->bkref_ents + first_idx; | |
2110 | enabled_idx = first_idx; | |
2111 | do | |
15a7d175 | 2112 | { |
eb04c213 AZ |
2113 | Idx subexp_len; |
2114 | Idx to_idx; | |
2115 | Idx dst_node; | |
2116 | bool ok; | |
e40a38b3 UD |
2117 | re_dfastate_t *cur_state; |
2118 | ||
2119 | if (entry->node != node) | |
2120 | continue; | |
2121 | subexp_len = entry->subexp_to - entry->subexp_from; | |
2122 | to_idx = str_idx + subexp_len; | |
2123 | dst_node = (subexp_len ? dfa->nexts[node] | |
2124 | : dfa->edests[node].elems[0]); | |
2125 | ||
2126 | if (to_idx > sctx->last_str_idx | |
2127 | || sctx->sifted_states[to_idx] == NULL | |
2128 | || !STATE_NODE_CONTAINS (sctx->sifted_states[to_idx], dst_node) | |
2129 | || check_dst_limits (mctx, &sctx->limits, node, | |
2130 | str_idx, dst_node, to_idx)) | |
2131 | continue; | |
2132 | ||
2133 | if (local_sctx.sifted_states == NULL) | |
15a7d175 | 2134 | { |
e40a38b3 UD |
2135 | local_sctx = *sctx; |
2136 | err = re_node_set_init_copy (&local_sctx.limits, &sctx->limits); | |
f4efbdfb | 2137 | if (__glibc_unlikely (err != REG_NOERROR)) |
e40a38b3 UD |
2138 | goto free_return; |
2139 | } | |
2140 | local_sctx.last_node = node; | |
2141 | local_sctx.last_str_idx = str_idx; | |
eb04c213 | 2142 | ok = re_node_set_insert (&local_sctx.limits, enabled_idx); |
f4efbdfb | 2143 | if (__glibc_unlikely (! ok)) |
e40a38b3 UD |
2144 | { |
2145 | err = REG_ESPACE; | |
2146 | goto free_return; | |
15a7d175 | 2147 | } |
e40a38b3 UD |
2148 | cur_state = local_sctx.sifted_states[str_idx]; |
2149 | err = sift_states_backward (mctx, &local_sctx); | |
f4efbdfb | 2150 | if (__glibc_unlikely (err != REG_NOERROR)) |
e40a38b3 UD |
2151 | goto free_return; |
2152 | if (sctx->limited_states != NULL) | |
2153 | { | |
2154 | err = merge_state_array (dfa, sctx->limited_states, | |
2155 | local_sctx.sifted_states, | |
2156 | str_idx + 1); | |
f4efbdfb | 2157 | if (__glibc_unlikely (err != REG_NOERROR)) |
e40a38b3 UD |
2158 | goto free_return; |
2159 | } | |
2160 | local_sctx.sifted_states[str_idx] = cur_state; | |
2161 | re_node_set_remove (&local_sctx.limits, enabled_idx); | |
2162 | ||
2163 | /* mctx->bkref_ents may have changed, reload the pointer. */ | |
2da42bc0 | 2164 | entry = mctx->bkref_ents + enabled_idx; |
15a7d175 | 2165 | } |
e40a38b3 | 2166 | while (enabled_idx++, entry++->more); |
0742e48e | 2167 | } |
1b2c2628 UD |
2168 | err = REG_NOERROR; |
2169 | free_return: | |
0742e48e UD |
2170 | if (local_sctx.sifted_states != NULL) |
2171 | { | |
0742e48e UD |
2172 | re_node_set_free (&local_sctx.limits); |
2173 | } | |
2174 | ||
1b2c2628 | 2175 | return err; |
0742e48e UD |
2176 | } |
2177 | ||
2178 | ||
2179 | #ifdef RE_ENABLE_I18N | |
2180 | static int | |
e2f55264 | 2181 | sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx, |
eb04c213 | 2182 | Idx node_idx, Idx str_idx, Idx max_str_idx) |
0742e48e | 2183 | { |
c42b4152 | 2184 | const re_dfa_t *const dfa = mctx->dfa; |
0742e48e | 2185 | int naccepted; |
eb04c213 | 2186 | /* Check the node can accept "multi byte". */ |
56b168be | 2187 | naccepted = check_node_accept_bytes (dfa, node_idx, &mctx->input, str_idx); |
34a5a146 JM |
2188 | if (naccepted > 0 && str_idx + naccepted <= max_str_idx |
2189 | && !STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + naccepted], | |
2190 | dfa->nexts[node_idx])) | |
eb04c213 | 2191 | /* The node can't accept the "multi byte", or the |
bb3f4825 | 2192 | destination was already thrown away, then the node |
a1b02ae7 | 2193 | couldn't accept the current input "multi byte". */ |
0742e48e UD |
2194 | naccepted = 0; |
2195 | /* Otherwise, it is sure that the node could accept | |
eb04c213 | 2196 | 'naccepted' bytes input. */ |
0742e48e UD |
2197 | return naccepted; |
2198 | } | |
2199 | #endif /* RE_ENABLE_I18N */ | |
2200 | ||
3b0bdc72 UD |
2201 | \f |
2202 | /* Functions for state transition. */ | |
2203 | ||
2204 | /* Return the next state to which the current state STATE will transit by | |
2205 | accepting the current input byte, and update STATE_LOG if necessary. | |
0b5ca7c3 | 2206 | Return NULL on failure. |
3b0bdc72 UD |
2207 | If STATE can accept a multibyte char/collating element/back reference |
2208 | update the destination of STATE_LOG. */ | |
2209 | ||
2210 | static re_dfastate_t * | |
b41bd5bc | 2211 | __attribute_warn_unused_result__ |
e2f55264 UD |
2212 | transit_state (reg_errcode_t *err, re_match_context_t *mctx, |
2213 | re_dfastate_t *state) | |
3b0bdc72 | 2214 | { |
58845a70 | 2215 | re_dfastate_t **trtable; |
3b0bdc72 UD |
2216 | unsigned char ch; |
2217 | ||
bb677c95 UD |
2218 | #ifdef RE_ENABLE_I18N |
2219 | /* If the current state can accept multibyte. */ | |
f4efbdfb | 2220 | if (__glibc_unlikely (state->accept_mb)) |
612546c6 | 2221 | { |
bb677c95 | 2222 | *err = transit_state_mb (mctx, state); |
f4efbdfb | 2223 | if (__glibc_unlikely (*err != REG_NOERROR)) |
15a7d175 | 2224 | return NULL; |
612546c6 | 2225 | } |
434d3784 | 2226 | #endif /* RE_ENABLE_I18N */ |
3b0bdc72 | 2227 | |
4c595adb | 2228 | /* Then decide the next state with the single byte. */ |
ab4b89fe UD |
2229 | #if 0 |
2230 | if (0) | |
2231 | /* don't use transition table */ | |
2232 | return transit_state_sb (err, mctx, state); | |
2233 | #endif | |
2234 | ||
2235 | /* Use transition table */ | |
2236 | ch = re_string_fetch_byte (&mctx->input); | |
2237 | for (;;) | |
4c595adb | 2238 | { |
4c595adb | 2239 | trtable = state->trtable; |
f4efbdfb | 2240 | if (__glibc_likely (trtable != NULL)) |
ab4b89fe UD |
2241 | return trtable[ch]; |
2242 | ||
2243 | trtable = state->word_trtable; | |
f4efbdfb | 2244 | if (__glibc_likely (trtable != NULL)) |
2da42bc0 | 2245 | { |
4c595adb UD |
2246 | unsigned int context; |
2247 | context | |
2248 | = re_string_context_at (&mctx->input, | |
2249 | re_string_cur_idx (&mctx->input) - 1, | |
2250 | mctx->eflags); | |
2251 | if (IS_WORD_CONTEXT (context)) | |
2252 | return trtable[ch + SBC_MAX]; | |
c13c99fa | 2253 | else |
4c595adb | 2254 | return trtable[ch]; |
15a7d175 | 2255 | } |
ab4b89fe UD |
2256 | |
2257 | if (!build_trtable (mctx->dfa, state)) | |
2258 | { | |
2259 | *err = REG_ESPACE; | |
2260 | return NULL; | |
2261 | } | |
2262 | ||
2263 | /* Retry, we now have a transition table. */ | |
3b0bdc72 | 2264 | } |
4c595adb | 2265 | } |
3b0bdc72 | 2266 | |
4c595adb | 2267 | /* Update the state_log if we need */ |
eb04c213 | 2268 | static re_dfastate_t * |
e2f55264 UD |
2269 | merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx, |
2270 | re_dfastate_t *next_state) | |
58845a70 | 2271 | { |
c42b4152 | 2272 | const re_dfa_t *const dfa = mctx->dfa; |
eb04c213 | 2273 | Idx cur_idx = re_string_cur_idx (&mctx->input); |
4c595adb UD |
2274 | |
2275 | if (cur_idx > mctx->state_log_top) | |
3b0bdc72 | 2276 | { |
4c595adb UD |
2277 | mctx->state_log[cur_idx] = next_state; |
2278 | mctx->state_log_top = cur_idx; | |
2279 | } | |
2280 | else if (mctx->state_log[cur_idx] == 0) | |
2281 | { | |
2282 | mctx->state_log[cur_idx] = next_state; | |
2283 | } | |
2284 | else | |
2285 | { | |
2286 | re_dfastate_t *pstate; | |
2287 | unsigned int context; | |
2288 | re_node_set next_nodes, *log_nodes, *table_nodes = NULL; | |
2289 | /* If (state_log[cur_idx] != 0), it implies that cur_idx is | |
2da42bc0 UD |
2290 | the destination of a multibyte char/collating element/ |
2291 | back reference. Then the next state is the union set of | |
2292 | these destinations and the results of the transition table. */ | |
4c595adb UD |
2293 | pstate = mctx->state_log[cur_idx]; |
2294 | log_nodes = pstate->entrance_nodes; | |
2295 | if (next_state != NULL) | |
2da42bc0 UD |
2296 | { |
2297 | table_nodes = next_state->entrance_nodes; | |
2298 | *err = re_node_set_init_union (&next_nodes, table_nodes, | |
15a7d175 | 2299 | log_nodes); |
f4efbdfb | 2300 | if (__glibc_unlikely (*err != REG_NOERROR)) |
4c595adb | 2301 | return NULL; |
2da42bc0 | 2302 | } |
4c595adb | 2303 | else |
2da42bc0 | 2304 | next_nodes = *log_nodes; |
4c595adb UD |
2305 | /* Note: We already add the nodes of the initial state, |
2306 | then we don't need to add them here. */ | |
2307 | ||
2308 | context = re_string_context_at (&mctx->input, | |
2309 | re_string_cur_idx (&mctx->input) - 1, | |
2310 | mctx->eflags); | |
2311 | next_state = mctx->state_log[cur_idx] | |
2da42bc0 | 2312 | = re_acquire_state_context (err, dfa, &next_nodes, context); |
4c595adb | 2313 | /* We don't need to check errors here, since the return value of |
2da42bc0 | 2314 | this function is next_state and ERR is already set. */ |
4c595adb UD |
2315 | |
2316 | if (table_nodes != NULL) | |
2da42bc0 | 2317 | re_node_set_free (&next_nodes); |
6291ee3c UD |
2318 | } |
2319 | ||
f4efbdfb | 2320 | if (__glibc_unlikely (dfa->nbackref) && next_state != NULL) |
6291ee3c | 2321 | { |
bb3f4825 UD |
2322 | /* Check OP_OPEN_SUBEXP in the current state in case that we use them |
2323 | later. We must check them here, since the back references in the | |
2324 | next state might use them. */ | |
e3a87852 | 2325 | *err = check_subexp_matching_top (mctx, &next_state->nodes, |
6291ee3c | 2326 | cur_idx); |
f4efbdfb | 2327 | if (__glibc_unlikely (*err != REG_NOERROR)) |
6291ee3c | 2328 | return NULL; |
6291ee3c | 2329 | |
bb3f4825 UD |
2330 | /* If the next state has back references. */ |
2331 | if (next_state->has_backref) | |
2332 | { | |
e3a87852 | 2333 | *err = transit_state_bkref (mctx, &next_state->nodes); |
f4efbdfb | 2334 | if (__glibc_unlikely (*err != REG_NOERROR)) |
bb3f4825 UD |
2335 | return NULL; |
2336 | next_state = mctx->state_log[cur_idx]; | |
2337 | } | |
3b0bdc72 | 2338 | } |
4c595adb | 2339 | |
3b0bdc72 UD |
2340 | return next_state; |
2341 | } | |
2342 | ||
4c595adb UD |
2343 | /* Skip bytes in the input that correspond to part of a |
2344 | multi-byte match, then look in the log for a state | |
2345 | from which to restart matching. */ | |
eb04c213 | 2346 | static re_dfastate_t * |
e2f55264 | 2347 | find_recover_state (reg_errcode_t *err, re_match_context_t *mctx) |
4c595adb | 2348 | { |
1878e9af | 2349 | re_dfastate_t *cur_state; |
4c595adb UD |
2350 | do |
2351 | { | |
eb04c213 AZ |
2352 | Idx max = mctx->state_log_top; |
2353 | Idx cur_str_idx = re_string_cur_idx (&mctx->input); | |
4c595adb UD |
2354 | |
2355 | do | |
2356 | { | |
2da42bc0 UD |
2357 | if (++cur_str_idx > max) |
2358 | return NULL; | |
2359 | re_string_skip_bytes (&mctx->input, 1); | |
4c595adb UD |
2360 | } |
2361 | while (mctx->state_log[cur_str_idx] == NULL); | |
2362 | ||
2363 | cur_state = merge_state_with_log (err, mctx, NULL); | |
2364 | } | |
2d87db5b | 2365 | while (*err == REG_NOERROR && cur_state == NULL); |
4c595adb UD |
2366 | return cur_state; |
2367 | } | |
2368 | ||
3b0bdc72 UD |
2369 | /* Helper functions for transit_state. */ |
2370 | ||
6291ee3c UD |
2371 | /* From the node set CUR_NODES, pick up the nodes whose types are |
2372 | OP_OPEN_SUBEXP and which have corresponding back references in the regular | |
2373 | expression. And register them to use them later for evaluating the | |
5069ff32 | 2374 | corresponding back references. */ |
6291ee3c UD |
2375 | |
2376 | static reg_errcode_t | |
e2f55264 | 2377 | check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes, |
eb04c213 | 2378 | Idx str_idx) |
6291ee3c | 2379 | { |
76b864c8 | 2380 | const re_dfa_t *const dfa = mctx->dfa; |
eb04c213 | 2381 | Idx node_idx; |
6291ee3c UD |
2382 | reg_errcode_t err; |
2383 | ||
2384 | /* TODO: This isn't efficient. | |
2385 | Because there might be more than one nodes whose types are | |
2386 | OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all | |
2387 | nodes. | |
2388 | E.g. RE: (a){2} */ | |
2389 | for (node_idx = 0; node_idx < cur_nodes->nelem; ++node_idx) | |
2390 | { | |
eb04c213 | 2391 | Idx node = cur_nodes->elems[node_idx]; |
6291ee3c | 2392 | if (dfa->nodes[node].type == OP_OPEN_SUBEXP |
2c05d33f UD |
2393 | && dfa->nodes[node].opr.idx < BITSET_WORD_BITS |
2394 | && (dfa->used_bkref_map | |
2395 | & ((bitset_word_t) 1 << dfa->nodes[node].opr.idx))) | |
6291ee3c UD |
2396 | { |
2397 | err = match_ctx_add_subtop (mctx, node, str_idx); | |
f4efbdfb | 2398 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2399 | return err; |
2400 | } | |
2401 | } | |
2402 | return REG_NOERROR; | |
2403 | } | |
2404 | ||
c13c99fa | 2405 | #if 0 |
3b0bdc72 | 2406 | /* Return the next state to which the current state STATE will transit by |
0b5ca7c3 | 2407 | accepting the current input byte. Return NULL on failure. */ |
3b0bdc72 UD |
2408 | |
2409 | static re_dfastate_t * | |
e2f55264 UD |
2410 | transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx, |
2411 | re_dfastate_t *state) | |
3b0bdc72 | 2412 | { |
c42b4152 | 2413 | const re_dfa_t *const dfa = mctx->dfa; |
3b0bdc72 UD |
2414 | re_node_set next_nodes; |
2415 | re_dfastate_t *next_state; | |
eb04c213 | 2416 | Idx node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input); |
3b0bdc72 UD |
2417 | unsigned int context; |
2418 | ||
a9388965 | 2419 | *err = re_node_set_alloc (&next_nodes, state->nodes.nelem + 1); |
f4efbdfb | 2420 | if (__glibc_unlikely (*err != REG_NOERROR)) |
a9388965 | 2421 | return NULL; |
3b0bdc72 UD |
2422 | for (node_cnt = 0; node_cnt < state->nodes.nelem; ++node_cnt) |
2423 | { | |
eb04c213 | 2424 | Idx cur_node = state->nodes.elems[node_cnt]; |
e3a87852 | 2425 | if (check_node_accept (mctx, dfa->nodes + cur_node, cur_str_idx)) |
15a7d175 UD |
2426 | { |
2427 | *err = re_node_set_merge (&next_nodes, | |
2428 | dfa->eclosures + dfa->nexts[cur_node]); | |
f4efbdfb | 2429 | if (__glibc_unlikely (*err != REG_NOERROR)) |
15a7d175 UD |
2430 | { |
2431 | re_node_set_free (&next_nodes); | |
2432 | return NULL; | |
2433 | } | |
2434 | } | |
3b0bdc72 | 2435 | } |
56b168be | 2436 | context = re_string_context_at (&mctx->input, cur_str_idx, mctx->eflags); |
a9388965 UD |
2437 | next_state = re_acquire_state_context (err, dfa, &next_nodes, context); |
2438 | /* We don't need to check errors here, since the return value of | |
2439 | this function is next_state and ERR is already set. */ | |
2440 | ||
3b0bdc72 | 2441 | re_node_set_free (&next_nodes); |
56b168be | 2442 | re_string_skip_bytes (&mctx->input, 1); |
3b0bdc72 UD |
2443 | return next_state; |
2444 | } | |
c13c99fa | 2445 | #endif |
3b0bdc72 | 2446 | |
434d3784 | 2447 | #ifdef RE_ENABLE_I18N |
a9388965 | 2448 | static reg_errcode_t |
e2f55264 | 2449 | transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate) |
3b0bdc72 | 2450 | { |
c42b4152 | 2451 | const re_dfa_t *const dfa = mctx->dfa; |
a9388965 | 2452 | reg_errcode_t err; |
eb04c213 | 2453 | Idx i; |
3b0bdc72 UD |
2454 | |
2455 | for (i = 0; i < pstate->nodes.nelem; ++i) | |
2456 | { | |
2457 | re_node_set dest_nodes, *new_nodes; | |
eb04c213 AZ |
2458 | Idx cur_node_idx = pstate->nodes.elems[i]; |
2459 | int naccepted; | |
2460 | Idx dest_idx; | |
3b0bdc72 UD |
2461 | unsigned int context; |
2462 | re_dfastate_t *dest_state; | |
2463 | ||
963d8d78 | 2464 | if (!dfa->nodes[cur_node_idx].accept_mb) |
2da42bc0 | 2465 | continue; |
963d8d78 | 2466 | |
485d775d | 2467 | if (dfa->nodes[cur_node_idx].constraint) |
15a7d175 | 2468 | { |
56b168be UD |
2469 | context = re_string_context_at (&mctx->input, |
2470 | re_string_cur_idx (&mctx->input), | |
2471 | mctx->eflags); | |
15a7d175 UD |
2472 | if (NOT_SATISFY_NEXT_CONSTRAINT (dfa->nodes[cur_node_idx].constraint, |
2473 | context)) | |
2474 | continue; | |
2475 | } | |
3b0bdc72 | 2476 | |
ad7f28c2 | 2477 | /* How many bytes the node can accept? */ |
963d8d78 UD |
2478 | naccepted = check_node_accept_bytes (dfa, cur_node_idx, &mctx->input, |
2479 | re_string_cur_idx (&mctx->input)); | |
3b0bdc72 | 2480 | if (naccepted == 0) |
15a7d175 | 2481 | continue; |
3b0bdc72 | 2482 | |
eb04c213 | 2483 | /* The node can accepts 'naccepted' bytes. */ |
56b168be | 2484 | dest_idx = re_string_cur_idx (&mctx->input) + naccepted; |
0742e48e | 2485 | mctx->max_mb_elem_len = ((mctx->max_mb_elem_len < naccepted) ? naccepted |
15a7d175 | 2486 | : mctx->max_mb_elem_len); |
7c1be3ec | 2487 | err = clean_state_log_if_needed (mctx, dest_idx); |
f4efbdfb | 2488 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 | 2489 | return err; |
2a0356e1 | 2490 | DEBUG_ASSERT (dfa->nexts[cur_node_idx] != -1); |
963d8d78 | 2491 | new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx]; |
3b0bdc72 | 2492 | |
612546c6 | 2493 | dest_state = mctx->state_log[dest_idx]; |
3b0bdc72 | 2494 | if (dest_state == NULL) |
15a7d175 | 2495 | dest_nodes = *new_nodes; |
3b0bdc72 | 2496 | else |
15a7d175 UD |
2497 | { |
2498 | err = re_node_set_init_union (&dest_nodes, | |
2499 | dest_state->entrance_nodes, new_nodes); | |
f4efbdfb | 2500 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
2501 | return err; |
2502 | } | |
01ed6ceb UD |
2503 | context = re_string_context_at (&mctx->input, dest_idx - 1, |
2504 | mctx->eflags); | |
612546c6 | 2505 | mctx->state_log[dest_idx] |
15a7d175 | 2506 | = re_acquire_state_context (&err, dfa, &dest_nodes, context); |
3b0bdc72 | 2507 | if (dest_state != NULL) |
15a7d175 | 2508 | re_node_set_free (&dest_nodes); |
f4efbdfb PE |
2509 | if (__glibc_unlikely (mctx->state_log[dest_idx] == NULL |
2510 | && err != REG_NOERROR)) | |
15a7d175 | 2511 | return err; |
3b0bdc72 | 2512 | } |
a9388965 | 2513 | return REG_NOERROR; |
3b0bdc72 | 2514 | } |
434d3784 | 2515 | #endif /* RE_ENABLE_I18N */ |
3b0bdc72 | 2516 | |
a9388965 | 2517 | static reg_errcode_t |
e2f55264 | 2518 | transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes) |
3b0bdc72 | 2519 | { |
6efbd82c | 2520 | const re_dfa_t *const dfa = mctx->dfa; |
a9388965 | 2521 | reg_errcode_t err; |
eb04c213 AZ |
2522 | Idx i; |
2523 | Idx cur_str_idx = re_string_cur_idx (&mctx->input); | |
3b0bdc72 UD |
2524 | |
2525 | for (i = 0; i < nodes->nelem; ++i) | |
2526 | { | |
eb04c213 AZ |
2527 | Idx dest_str_idx, prev_nelem, bkc_idx; |
2528 | Idx node_idx = nodes->elems[i]; | |
3b0bdc72 | 2529 | unsigned int context; |
fe9434bb | 2530 | const re_token_t *node = dfa->nodes + node_idx; |
3b0bdc72 UD |
2531 | re_node_set *new_dest_nodes; |
2532 | ||
eb04c213 | 2533 | /* Check whether 'node' is a backreference or not. */ |
6291ee3c | 2534 | if (node->type != OP_BACK_REF) |
15a7d175 | 2535 | continue; |
485d775d UD |
2536 | |
2537 | if (node->constraint) | |
15a7d175 | 2538 | { |
56b168be UD |
2539 | context = re_string_context_at (&mctx->input, cur_str_idx, |
2540 | mctx->eflags); | |
15a7d175 UD |
2541 | if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context)) |
2542 | continue; | |
2543 | } | |
3b0bdc72 | 2544 | |
eb04c213 | 2545 | /* 'node' is a backreference. |
15a7d175 | 2546 | Check the substring which the substring matched. */ |
6291ee3c | 2547 | bkc_idx = mctx->nbkref_ents; |
e3a87852 | 2548 | err = get_subexp (mctx, node_idx, cur_str_idx); |
f4efbdfb | 2549 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 | 2550 | goto free_return; |
3b0bdc72 | 2551 | |
eb04c213 | 2552 | /* And add the epsilon closures (which is 'new_dest_nodes') of |
15a7d175 | 2553 | the backreference to appropriate state_log. */ |
2a0356e1 | 2554 | DEBUG_ASSERT (dfa->nexts[node_idx] != -1); |
6291ee3c | 2555 | for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx) |
15a7d175 | 2556 | { |
eb04c213 | 2557 | Idx subexp_len; |
15a7d175 UD |
2558 | re_dfastate_t *dest_state; |
2559 | struct re_backref_cache_entry *bkref_ent; | |
2560 | bkref_ent = mctx->bkref_ents + bkc_idx; | |
2561 | if (bkref_ent->node != node_idx || bkref_ent->str_idx != cur_str_idx) | |
2562 | continue; | |
2563 | subexp_len = bkref_ent->subexp_to - bkref_ent->subexp_from; | |
2564 | new_dest_nodes = (subexp_len == 0 | |
2565 | ? dfa->eclosures + dfa->edests[node_idx].elems[0] | |
2566 | : dfa->eclosures + dfa->nexts[node_idx]); | |
2567 | dest_str_idx = (cur_str_idx + bkref_ent->subexp_to | |
2568 | - bkref_ent->subexp_from); | |
56b168be UD |
2569 | context = re_string_context_at (&mctx->input, dest_str_idx - 1, |
2570 | mctx->eflags); | |
15a7d175 UD |
2571 | dest_state = mctx->state_log[dest_str_idx]; |
2572 | prev_nelem = ((mctx->state_log[cur_str_idx] == NULL) ? 0 | |
2573 | : mctx->state_log[cur_str_idx]->nodes.nelem); | |
eb04c213 | 2574 | /* Add 'new_dest_node' to state_log. */ |
15a7d175 UD |
2575 | if (dest_state == NULL) |
2576 | { | |
2577 | mctx->state_log[dest_str_idx] | |
2578 | = re_acquire_state_context (&err, dfa, new_dest_nodes, | |
2579 | context); | |
f4efbdfb PE |
2580 | if (__glibc_unlikely (mctx->state_log[dest_str_idx] == NULL |
2581 | && err != REG_NOERROR)) | |
15a7d175 UD |
2582 | goto free_return; |
2583 | } | |
2584 | else | |
2585 | { | |
2586 | re_node_set dest_nodes; | |
2587 | err = re_node_set_init_union (&dest_nodes, | |
2588 | dest_state->entrance_nodes, | |
2589 | new_dest_nodes); | |
f4efbdfb | 2590 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
2591 | { |
2592 | re_node_set_free (&dest_nodes); | |
2593 | goto free_return; | |
2594 | } | |
2595 | mctx->state_log[dest_str_idx] | |
2596 | = re_acquire_state_context (&err, dfa, &dest_nodes, context); | |
2597 | re_node_set_free (&dest_nodes); | |
f4efbdfb PE |
2598 | if (__glibc_unlikely (mctx->state_log[dest_str_idx] == NULL |
2599 | && err != REG_NOERROR)) | |
15a7d175 UD |
2600 | goto free_return; |
2601 | } | |
2602 | /* We need to check recursively if the backreference can epsilon | |
2603 | transit. */ | |
2604 | if (subexp_len == 0 | |
2605 | && mctx->state_log[cur_str_idx]->nodes.nelem > prev_nelem) | |
2606 | { | |
e3a87852 | 2607 | err = check_subexp_matching_top (mctx, new_dest_nodes, |
6291ee3c | 2608 | cur_str_idx); |
f4efbdfb | 2609 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c | 2610 | goto free_return; |
e3a87852 | 2611 | err = transit_state_bkref (mctx, new_dest_nodes); |
f4efbdfb | 2612 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
2613 | goto free_return; |
2614 | } | |
2615 | } | |
3b0bdc72 | 2616 | } |
1b2c2628 UD |
2617 | err = REG_NOERROR; |
2618 | free_return: | |
1b2c2628 | 2619 | return err; |
3b0bdc72 UD |
2620 | } |
2621 | ||
6291ee3c UD |
2622 | /* Enumerate all the candidates which the backreference BKREF_NODE can match |
2623 | at BKREF_STR_IDX, and register them by match_ctx_add_entry(). | |
2624 | Note that we might collect inappropriate candidates here. | |
2625 | However, the cost of checking them strictly here is too high, then we | |
2626 | delay these checking for prune_impossible_nodes(). */ | |
3b0bdc72 | 2627 | |
6291ee3c | 2628 | static reg_errcode_t |
b41bd5bc | 2629 | __attribute_warn_unused_result__ |
eb04c213 | 2630 | get_subexp (re_match_context_t *mctx, Idx bkref_node, Idx bkref_str_idx) |
3b0bdc72 | 2631 | { |
6efbd82c | 2632 | const re_dfa_t *const dfa = mctx->dfa; |
eb04c213 | 2633 | Idx subexp_num, sub_top_idx; |
56b168be | 2634 | const char *buf = (const char *) re_string_get_buffer (&mctx->input); |
6291ee3c | 2635 | /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX. */ |
eb04c213 | 2636 | Idx cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx); |
e40a38b3 | 2637 | if (cache_idx != -1) |
6291ee3c | 2638 | { |
01ed6ceb UD |
2639 | const struct re_backref_cache_entry *entry |
2640 | = mctx->bkref_ents + cache_idx; | |
e40a38b3 | 2641 | do |
2da42bc0 | 2642 | if (entry->node == bkref_node) |
e40a38b3 UD |
2643 | return REG_NOERROR; /* We already checked it. */ |
2644 | while (entry++->more); | |
6291ee3c | 2645 | } |
e40a38b3 | 2646 | |
d8f73de8 | 2647 | subexp_num = dfa->nodes[bkref_node].opr.idx; |
6291ee3c UD |
2648 | |
2649 | /* For each sub expression */ | |
2650 | for (sub_top_idx = 0; sub_top_idx < mctx->nsub_tops; ++sub_top_idx) | |
2651 | { | |
2652 | reg_errcode_t err; | |
2653 | re_sub_match_top_t *sub_top = mctx->sub_tops[sub_top_idx]; | |
2654 | re_sub_match_last_t *sub_last; | |
eb04c213 | 2655 | Idx sub_last_idx, sl_str, bkref_str_off; |
6291ee3c UD |
2656 | |
2657 | if (dfa->nodes[sub_top->node].opr.idx != subexp_num) | |
2658 | continue; /* It isn't related. */ | |
2659 | ||
2660 | sl_str = sub_top->str_idx; | |
7c1be3ec | 2661 | bkref_str_off = bkref_str_idx; |
6291ee3c UD |
2662 | /* At first, check the last node of sub expressions we already |
2663 | evaluated. */ | |
2664 | for (sub_last_idx = 0; sub_last_idx < sub_top->nlasts; ++sub_last_idx) | |
2665 | { | |
eb04c213 | 2666 | regoff_t sl_str_diff; |
6291ee3c UD |
2667 | sub_last = sub_top->lasts[sub_last_idx]; |
2668 | sl_str_diff = sub_last->str_idx - sl_str; | |
2669 | /* The matched string by the sub expression match with the substring | |
2670 | at the back reference? */ | |
c1baba0f UD |
2671 | if (sl_str_diff > 0) |
2672 | { | |
f4efbdfb PE |
2673 | if (__glibc_unlikely (bkref_str_off + sl_str_diff |
2674 | > mctx->input.valid_len)) | |
c1baba0f UD |
2675 | { |
2676 | /* Not enough chars for a successful match. */ | |
2677 | if (bkref_str_off + sl_str_diff > mctx->input.len) | |
2678 | break; | |
2679 | ||
2680 | err = clean_state_log_if_needed (mctx, | |
2681 | bkref_str_off | |
2682 | + sl_str_diff); | |
f4efbdfb | 2683 | if (__glibc_unlikely (err != REG_NOERROR)) |
c1baba0f UD |
2684 | return err; |
2685 | buf = (const char *) re_string_get_buffer (&mctx->input); | |
2686 | } | |
2687 | if (memcmp (buf + bkref_str_off, buf + sl_str, sl_str_diff) != 0) | |
01ed6ceb UD |
2688 | /* We don't need to search this sub expression any more. */ |
2689 | break; | |
c1baba0f | 2690 | } |
7c1be3ec | 2691 | bkref_str_off += sl_str_diff; |
6291ee3c | 2692 | sl_str += sl_str_diff; |
e3a87852 | 2693 | err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node, |
6291ee3c | 2694 | bkref_str_idx); |
3ee363e2 | 2695 | |
7c1be3ec UD |
2696 | /* Reload buf, since the preceding call might have reallocated |
2697 | the buffer. */ | |
56b168be | 2698 | buf = (const char *) re_string_get_buffer (&mctx->input); |
3ee363e2 | 2699 | |
6291ee3c UD |
2700 | if (err == REG_NOMATCH) |
2701 | continue; | |
f4efbdfb | 2702 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2703 | return err; |
2704 | } | |
7c1be3ec | 2705 | |
6291ee3c UD |
2706 | if (sub_last_idx < sub_top->nlasts) |
2707 | continue; | |
2708 | if (sub_last_idx > 0) | |
2709 | ++sl_str; | |
2710 | /* Then, search for the other last nodes of the sub expression. */ | |
2711 | for (; sl_str <= bkref_str_idx; ++sl_str) | |
2712 | { | |
eb04c213 AZ |
2713 | Idx cls_node; |
2714 | regoff_t sl_str_off; | |
fe9434bb | 2715 | const re_node_set *nodes; |
6291ee3c UD |
2716 | sl_str_off = sl_str - sub_top->str_idx; |
2717 | /* The matched string by the sub expression match with the substring | |
2718 | at the back reference? */ | |
c1baba0f UD |
2719 | if (sl_str_off > 0) |
2720 | { | |
f4efbdfb | 2721 | if (__glibc_unlikely (bkref_str_off >= mctx->input.valid_len)) |
c1baba0f UD |
2722 | { |
2723 | /* If we are at the end of the input, we cannot match. */ | |
2724 | if (bkref_str_off >= mctx->input.len) | |
2725 | break; | |
2726 | ||
a445af0b | 2727 | err = extend_buffers (mctx, bkref_str_off + 1); |
f4efbdfb | 2728 | if (__glibc_unlikely (err != REG_NOERROR)) |
c1baba0f UD |
2729 | return err; |
2730 | ||
2731 | buf = (const char *) re_string_get_buffer (&mctx->input); | |
2732 | } | |
2733 | if (buf [bkref_str_off++] != buf[sl_str - 1]) | |
2734 | break; /* We don't need to search this sub expression | |
2735 | any more. */ | |
2736 | } | |
6291ee3c UD |
2737 | if (mctx->state_log[sl_str] == NULL) |
2738 | continue; | |
2739 | /* Does this state have a ')' of the sub expression? */ | |
2740 | nodes = &mctx->state_log[sl_str]->nodes; | |
01ed6ceb UD |
2741 | cls_node = find_subexp_node (dfa, nodes, subexp_num, |
2742 | OP_CLOSE_SUBEXP); | |
6291ee3c UD |
2743 | if (cls_node == -1) |
2744 | continue; /* No. */ | |
2745 | if (sub_top->path == NULL) | |
2746 | { | |
2747 | sub_top->path = calloc (sizeof (state_array_t), | |
2748 | sl_str - sub_top->str_idx + 1); | |
2749 | if (sub_top->path == NULL) | |
2750 | return REG_ESPACE; | |
2751 | } | |
2752 | /* Can the OP_OPEN_SUBEXP node arrive the OP_CLOSE_SUBEXP node | |
2753 | in the current context? */ | |
e3a87852 | 2754 | err = check_arrival (mctx, sub_top->path, sub_top->node, |
01ed6ceb UD |
2755 | sub_top->str_idx, cls_node, sl_str, |
2756 | OP_CLOSE_SUBEXP); | |
6291ee3c UD |
2757 | if (err == REG_NOMATCH) |
2758 | continue; | |
f4efbdfb | 2759 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2760 | return err; |
2761 | sub_last = match_ctx_add_sublast (sub_top, cls_node, sl_str); | |
f4efbdfb | 2762 | if (__glibc_unlikely (sub_last == NULL)) |
6291ee3c | 2763 | return REG_ESPACE; |
e3a87852 | 2764 | err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node, |
6291ee3c | 2765 | bkref_str_idx); |
077caf61 | 2766 | buf = (const char *) re_string_get_buffer (&mctx->input); |
6291ee3c UD |
2767 | if (err == REG_NOMATCH) |
2768 | continue; | |
ef202e53 PE |
2769 | if (__glibc_unlikely (err != REG_NOERROR)) |
2770 | return err; | |
6291ee3c UD |
2771 | } |
2772 | } | |
2773 | return REG_NOERROR; | |
2774 | } | |
2775 | ||
2776 | /* Helper functions for get_subexp(). */ | |
2777 | ||
2778 | /* Check SUB_LAST can arrive to the back reference BKREF_NODE at BKREF_STR. | |
2779 | If it can arrive, register the sub expression expressed with SUB_TOP | |
2780 | and SUB_LAST. */ | |
2781 | ||
2782 | static reg_errcode_t | |
e2f55264 | 2783 | get_subexp_sub (re_match_context_t *mctx, const re_sub_match_top_t *sub_top, |
eb04c213 | 2784 | re_sub_match_last_t *sub_last, Idx bkref_node, Idx bkref_str) |
6291ee3c UD |
2785 | { |
2786 | reg_errcode_t err; | |
eb04c213 | 2787 | Idx to_idx; |
6291ee3c | 2788 | /* Can the subexpression arrive the back reference? */ |
e3a87852 | 2789 | err = check_arrival (mctx, &sub_last->path, sub_last->node, |
01ed6ceb UD |
2790 | sub_last->str_idx, bkref_node, bkref_str, |
2791 | OP_OPEN_SUBEXP); | |
6291ee3c UD |
2792 | if (err != REG_NOERROR) |
2793 | return err; | |
2794 | err = match_ctx_add_entry (mctx, bkref_node, bkref_str, sub_top->str_idx, | |
2795 | sub_last->str_idx); | |
f4efbdfb | 2796 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2797 | return err; |
2798 | to_idx = bkref_str + sub_last->str_idx - sub_top->str_idx; | |
c1baba0f | 2799 | return clean_state_log_if_needed (mctx, to_idx); |
6291ee3c UD |
2800 | } |
2801 | ||
2802 | /* Find the first node which is '(' or ')' and whose index is SUBEXP_IDX. | |
2803 | Search '(' if FL_OPEN, or search ')' otherwise. | |
2804 | TODO: This function isn't efficient... | |
2805 | Because there might be more than one nodes whose types are | |
2806 | OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all | |
2807 | nodes. | |
2808 | E.g. RE: (a){2} */ | |
2809 | ||
eb04c213 | 2810 | static Idx |
e2f55264 | 2811 | find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes, |
eb04c213 | 2812 | Idx subexp_idx, int type) |
6291ee3c | 2813 | { |
eb04c213 | 2814 | Idx cls_idx; |
6291ee3c UD |
2815 | for (cls_idx = 0; cls_idx < nodes->nelem; ++cls_idx) |
2816 | { | |
eb04c213 | 2817 | Idx cls_node = nodes->elems[cls_idx]; |
fe9434bb | 2818 | const re_token_t *node = dfa->nodes + cls_node; |
0ce7f49c | 2819 | if (node->type == type |
6291ee3c UD |
2820 | && node->opr.idx == subexp_idx) |
2821 | return cls_node; | |
2822 | } | |
2823 | return -1; | |
2824 | } | |
2825 | ||
2826 | /* Check whether the node TOP_NODE at TOP_STR can arrive to the node | |
2827 | LAST_NODE at LAST_STR. We record the path onto PATH since it will be | |
2828 | heavily reused. | |
0b5ca7c3 PE |
2829 | Return REG_NOERROR if it can arrive, REG_NOMATCH if it cannot, |
2830 | REG_ESPACE if memory is exhausted. */ | |
6291ee3c UD |
2831 | |
2832 | static reg_errcode_t | |
b41bd5bc | 2833 | __attribute_warn_unused_result__ |
eb04c213 AZ |
2834 | check_arrival (re_match_context_t *mctx, state_array_t *path, Idx top_node, |
2835 | Idx top_str, Idx last_node, Idx last_str, int type) | |
6291ee3c | 2836 | { |
6efbd82c | 2837 | const re_dfa_t *const dfa = mctx->dfa; |
c0c9615c | 2838 | reg_errcode_t err = REG_NOERROR; |
eb04c213 | 2839 | Idx subexp_num, backup_cur_idx, str_idx, null_cnt; |
6291ee3c UD |
2840 | re_dfastate_t *cur_state = NULL; |
2841 | re_node_set *cur_nodes, next_nodes; | |
2842 | re_dfastate_t **backup_state_log; | |
2843 | unsigned int context; | |
2844 | ||
2845 | subexp_num = dfa->nodes[top_node].opr.idx; | |
2846 | /* Extend the buffer if we need. */ | |
f4efbdfb | 2847 | if (__glibc_unlikely (path->alloc < last_str + mctx->max_mb_elem_len + 1)) |
6291ee3c UD |
2848 | { |
2849 | re_dfastate_t **new_array; | |
eb04c213 AZ |
2850 | Idx old_alloc = path->alloc; |
2851 | Idx incr_alloc = last_str + mctx->max_mb_elem_len + 1; | |
2852 | Idx new_alloc; | |
f4efbdfb | 2853 | if (__glibc_unlikely (IDX_MAX - old_alloc < incr_alloc)) |
eb04c213 AZ |
2854 | return REG_ESPACE; |
2855 | new_alloc = old_alloc + incr_alloc; | |
f4efbdfb | 2856 | if (__glibc_unlikely (SIZE_MAX / sizeof (re_dfastate_t *) < new_alloc)) |
eb04c213 AZ |
2857 | return REG_ESPACE; |
2858 | new_array = re_realloc (path->array, re_dfastate_t *, new_alloc); | |
f4efbdfb | 2859 | if (__glibc_unlikely (new_array == NULL)) |
eb04c213 | 2860 | return REG_ESPACE; |
6291ee3c | 2861 | path->array = new_array; |
eb04c213 | 2862 | path->alloc = new_alloc; |
6291ee3c UD |
2863 | memset (new_array + old_alloc, '\0', |
2864 | sizeof (re_dfastate_t *) * (path->alloc - old_alloc)); | |
2865 | } | |
2866 | ||
eb04c213 | 2867 | str_idx = path->next_idx ? path->next_idx : top_str; |
6291ee3c UD |
2868 | |
2869 | /* Temporary modify MCTX. */ | |
2870 | backup_state_log = mctx->state_log; | |
56b168be | 2871 | backup_cur_idx = mctx->input.cur_idx; |
6291ee3c | 2872 | mctx->state_log = path->array; |
56b168be | 2873 | mctx->input.cur_idx = str_idx; |
6291ee3c UD |
2874 | |
2875 | /* Setup initial node set. */ | |
56b168be | 2876 | context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags); |
6291ee3c UD |
2877 | if (str_idx == top_str) |
2878 | { | |
2879 | err = re_node_set_init_1 (&next_nodes, top_node); | |
f4efbdfb | 2880 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c | 2881 | return err; |
0ce7f49c | 2882 | err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type); |
f4efbdfb | 2883 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2884 | { |
2885 | re_node_set_free (&next_nodes); | |
2886 | return err; | |
2887 | } | |
2888 | } | |
2889 | else | |
2890 | { | |
2891 | cur_state = mctx->state_log[str_idx]; | |
2892 | if (cur_state && cur_state->has_backref) | |
2893 | { | |
2894 | err = re_node_set_init_copy (&next_nodes, &cur_state->nodes); | |
f4efbdfb | 2895 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2896 | return err; |
2897 | } | |
2898 | else | |
2899 | re_node_set_init_empty (&next_nodes); | |
2900 | } | |
2901 | if (str_idx == top_str || (cur_state && cur_state->has_backref)) | |
2902 | { | |
2903 | if (next_nodes.nelem) | |
2904 | { | |
d2c38eb3 | 2905 | err = expand_bkref_cache (mctx, &next_nodes, str_idx, |
0ce7f49c | 2906 | subexp_num, type); |
f4efbdfb | 2907 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2908 | { |
2909 | re_node_set_free (&next_nodes); | |
2910 | return err; | |
2911 | } | |
2912 | } | |
2913 | cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context); | |
f4efbdfb | 2914 | if (__glibc_unlikely (cur_state == NULL && err != REG_NOERROR)) |
6291ee3c UD |
2915 | { |
2916 | re_node_set_free (&next_nodes); | |
2917 | return err; | |
2918 | } | |
2919 | mctx->state_log[str_idx] = cur_state; | |
2920 | } | |
2921 | ||
2922 | for (null_cnt = 0; str_idx < last_str && null_cnt <= mctx->max_mb_elem_len;) | |
2923 | { | |
2924 | re_node_set_empty (&next_nodes); | |
2925 | if (mctx->state_log[str_idx + 1]) | |
2926 | { | |
2927 | err = re_node_set_merge (&next_nodes, | |
2928 | &mctx->state_log[str_idx + 1]->nodes); | |
f4efbdfb | 2929 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2930 | { |
2931 | re_node_set_free (&next_nodes); | |
2932 | return err; | |
2933 | } | |
2934 | } | |
2935 | if (cur_state) | |
2936 | { | |
e3a87852 | 2937 | err = check_arrival_add_next_nodes (mctx, str_idx, |
6efbd82c UD |
2938 | &cur_state->non_eps_nodes, |
2939 | &next_nodes); | |
f4efbdfb | 2940 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2941 | { |
2942 | re_node_set_free (&next_nodes); | |
2943 | return err; | |
2944 | } | |
2945 | } | |
2946 | ++str_idx; | |
2947 | if (next_nodes.nelem) | |
2948 | { | |
0ce7f49c | 2949 | err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type); |
f4efbdfb | 2950 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2951 | { |
2952 | re_node_set_free (&next_nodes); | |
2953 | return err; | |
2954 | } | |
d2c38eb3 | 2955 | err = expand_bkref_cache (mctx, &next_nodes, str_idx, |
0ce7f49c | 2956 | subexp_num, type); |
f4efbdfb | 2957 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
2958 | { |
2959 | re_node_set_free (&next_nodes); | |
2960 | return err; | |
2961 | } | |
2962 | } | |
56b168be | 2963 | context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags); |
6291ee3c | 2964 | cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context); |
f4efbdfb | 2965 | if (__glibc_unlikely (cur_state == NULL && err != REG_NOERROR)) |
6291ee3c UD |
2966 | { |
2967 | re_node_set_free (&next_nodes); | |
2968 | return err; | |
2969 | } | |
2970 | mctx->state_log[str_idx] = cur_state; | |
2971 | null_cnt = cur_state == NULL ? null_cnt + 1 : 0; | |
2972 | } | |
2973 | re_node_set_free (&next_nodes); | |
2974 | cur_nodes = (mctx->state_log[last_str] == NULL ? NULL | |
2975 | : &mctx->state_log[last_str]->nodes); | |
2976 | path->next_idx = str_idx; | |
2977 | ||
2978 | /* Fix MCTX. */ | |
2979 | mctx->state_log = backup_state_log; | |
56b168be | 2980 | mctx->input.cur_idx = backup_cur_idx; |
6291ee3c | 2981 | |
6291ee3c | 2982 | /* Then check the current node set has the node LAST_NODE. */ |
c0d5034e UD |
2983 | if (cur_nodes != NULL && re_node_set_contains (cur_nodes, last_node)) |
2984 | return REG_NOERROR; | |
2985 | ||
2986 | return REG_NOMATCH; | |
6291ee3c UD |
2987 | } |
2988 | ||
2989 | /* Helper functions for check_arrival. */ | |
2990 | ||
2991 | /* Calculate the destination nodes of CUR_NODES at STR_IDX, and append them | |
2992 | to NEXT_NODES. | |
2993 | TODO: This function is similar to the functions transit_state*(), | |
2994 | however this function has many additional works. | |
2995 | Can't we unify them? */ | |
2996 | ||
2997 | static reg_errcode_t | |
b41bd5bc | 2998 | __attribute_warn_unused_result__ |
eb04c213 | 2999 | check_arrival_add_next_nodes (re_match_context_t *mctx, Idx str_idx, |
e2f55264 | 3000 | re_node_set *cur_nodes, re_node_set *next_nodes) |
6291ee3c | 3001 | { |
c42b4152 | 3002 | const re_dfa_t *const dfa = mctx->dfa; |
eb04c213 AZ |
3003 | bool ok; |
3004 | Idx cur_idx; | |
ed8ae46b | 3005 | #ifdef RE_ENABLE_I18N |
c0c9615c | 3006 | reg_errcode_t err = REG_NOERROR; |
ed8ae46b | 3007 | #endif |
6291ee3c UD |
3008 | re_node_set union_set; |
3009 | re_node_set_init_empty (&union_set); | |
3010 | for (cur_idx = 0; cur_idx < cur_nodes->nelem; ++cur_idx) | |
3011 | { | |
3012 | int naccepted = 0; | |
eb04c213 | 3013 | Idx cur_node = cur_nodes->elems[cur_idx]; |
2a0356e1 AZ |
3014 | DEBUG_ASSERT (!IS_EPSILON_NODE (dfa->nodes[cur_node].type)); |
3015 | ||
6291ee3c | 3016 | #ifdef RE_ENABLE_I18N |
eb04c213 | 3017 | /* If the node may accept "multi byte". */ |
02f3550c | 3018 | if (dfa->nodes[cur_node].accept_mb) |
6291ee3c | 3019 | { |
56b168be | 3020 | naccepted = check_node_accept_bytes (dfa, cur_node, &mctx->input, |
6291ee3c UD |
3021 | str_idx); |
3022 | if (naccepted > 1) | |
3023 | { | |
3024 | re_dfastate_t *dest_state; | |
eb04c213 AZ |
3025 | Idx next_node = dfa->nexts[cur_node]; |
3026 | Idx next_idx = str_idx + naccepted; | |
6291ee3c UD |
3027 | dest_state = mctx->state_log[next_idx]; |
3028 | re_node_set_empty (&union_set); | |
3029 | if (dest_state) | |
3030 | { | |
3031 | err = re_node_set_merge (&union_set, &dest_state->nodes); | |
f4efbdfb | 3032 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
3033 | { |
3034 | re_node_set_free (&union_set); | |
3035 | return err; | |
3036 | } | |
6291ee3c | 3037 | } |
eb04c213 | 3038 | ok = re_node_set_insert (&union_set, next_node); |
f4efbdfb | 3039 | if (__glibc_unlikely (! ok)) |
6291ee3c | 3040 | { |
44777771 UD |
3041 | re_node_set_free (&union_set); |
3042 | return REG_ESPACE; | |
6291ee3c UD |
3043 | } |
3044 | mctx->state_log[next_idx] = re_acquire_state (&err, dfa, | |
3045 | &union_set); | |
f4efbdfb PE |
3046 | if (__glibc_unlikely (mctx->state_log[next_idx] == NULL |
3047 | && err != REG_NOERROR)) | |
6291ee3c UD |
3048 | { |
3049 | re_node_set_free (&union_set); | |
3050 | return err; | |
3051 | } | |
3052 | } | |
3053 | } | |
3054 | #endif /* RE_ENABLE_I18N */ | |
3055 | if (naccepted | |
e3a87852 | 3056 | || check_node_accept (mctx, dfa->nodes + cur_node, str_idx)) |
6291ee3c | 3057 | { |
eb04c213 | 3058 | ok = re_node_set_insert (next_nodes, dfa->nexts[cur_node]); |
f4efbdfb | 3059 | if (__glibc_unlikely (! ok)) |
6291ee3c UD |
3060 | { |
3061 | re_node_set_free (&union_set); | |
3062 | return REG_ESPACE; | |
3063 | } | |
3064 | } | |
3065 | } | |
3066 | re_node_set_free (&union_set); | |
3067 | return REG_NOERROR; | |
3068 | } | |
3069 | ||
3070 | /* For all the nodes in CUR_NODES, add the epsilon closures of them to | |
3071 | CUR_NODES, however exclude the nodes which are: | |
3072 | - inside the sub expression whose number is EX_SUBEXP, if FL_OPEN. | |
3073 | - out of the sub expression whose number is EX_SUBEXP, if !FL_OPEN. | |
3074 | */ | |
3075 | ||
3076 | static reg_errcode_t | |
e2f55264 | 3077 | check_arrival_expand_ecl (const re_dfa_t *dfa, re_node_set *cur_nodes, |
eb04c213 | 3078 | Idx ex_subexp, int type) |
6291ee3c UD |
3079 | { |
3080 | reg_errcode_t err; | |
eb04c213 | 3081 | Idx idx, outside_node; |
6291ee3c | 3082 | re_node_set new_nodes; |
2a0356e1 | 3083 | DEBUG_ASSERT (cur_nodes->nelem); |
6291ee3c | 3084 | err = re_node_set_alloc (&new_nodes, cur_nodes->nelem); |
f4efbdfb | 3085 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
3086 | return err; |
3087 | /* Create a new node set NEW_NODES with the nodes which are epsilon | |
3088 | closures of the node in CUR_NODES. */ | |
3089 | ||
3090 | for (idx = 0; idx < cur_nodes->nelem; ++idx) | |
3091 | { | |
eb04c213 | 3092 | Idx cur_node = cur_nodes->elems[idx]; |
6efbd82c | 3093 | const re_node_set *eclosure = dfa->eclosures + cur_node; |
0ce7f49c | 3094 | outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type); |
6291ee3c UD |
3095 | if (outside_node == -1) |
3096 | { | |
3097 | /* There are no problematic nodes, just merge them. */ | |
3098 | err = re_node_set_merge (&new_nodes, eclosure); | |
f4efbdfb | 3099 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
3100 | { |
3101 | re_node_set_free (&new_nodes); | |
3102 | return err; | |
3103 | } | |
3104 | } | |
3105 | else | |
3106 | { | |
3107 | /* There are problematic nodes, re-calculate incrementally. */ | |
3108 | err = check_arrival_expand_ecl_sub (dfa, &new_nodes, cur_node, | |
0ce7f49c | 3109 | ex_subexp, type); |
f4efbdfb | 3110 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
3111 | { |
3112 | re_node_set_free (&new_nodes); | |
3113 | return err; | |
3114 | } | |
3115 | } | |
3116 | } | |
3117 | re_node_set_free (cur_nodes); | |
3118 | *cur_nodes = new_nodes; | |
3119 | return REG_NOERROR; | |
3120 | } | |
3121 | ||
3122 | /* Helper function for check_arrival_expand_ecl. | |
3123 | Check incrementally the epsilon closure of TARGET, and if it isn't | |
3124 | problematic append it to DST_NODES. */ | |
3125 | ||
3126 | static reg_errcode_t | |
b41bd5bc | 3127 | __attribute_warn_unused_result__ |
e2f55264 | 3128 | check_arrival_expand_ecl_sub (const re_dfa_t *dfa, re_node_set *dst_nodes, |
eb04c213 | 3129 | Idx target, Idx ex_subexp, int type) |
6291ee3c | 3130 | { |
eb04c213 | 3131 | Idx cur_node; |
6291ee3c UD |
3132 | for (cur_node = target; !re_node_set_contains (dst_nodes, cur_node);) |
3133 | { | |
eb04c213 | 3134 | bool ok; |
6291ee3c | 3135 | |
0ce7f49c | 3136 | if (dfa->nodes[cur_node].type == type |
6291ee3c UD |
3137 | && dfa->nodes[cur_node].opr.idx == ex_subexp) |
3138 | { | |
0ce7f49c | 3139 | if (type == OP_CLOSE_SUBEXP) |
6291ee3c | 3140 | { |
eb04c213 | 3141 | ok = re_node_set_insert (dst_nodes, cur_node); |
f4efbdfb | 3142 | if (__glibc_unlikely (! ok)) |
6291ee3c UD |
3143 | return REG_ESPACE; |
3144 | } | |
3145 | break; | |
3146 | } | |
eb04c213 | 3147 | ok = re_node_set_insert (dst_nodes, cur_node); |
f4efbdfb | 3148 | if (__glibc_unlikely (! ok)) |
6291ee3c UD |
3149 | return REG_ESPACE; |
3150 | if (dfa->edests[cur_node].nelem == 0) | |
3151 | break; | |
3152 | if (dfa->edests[cur_node].nelem == 2) | |
3153 | { | |
eb04c213 | 3154 | reg_errcode_t err; |
6291ee3c UD |
3155 | err = check_arrival_expand_ecl_sub (dfa, dst_nodes, |
3156 | dfa->edests[cur_node].elems[1], | |
0ce7f49c | 3157 | ex_subexp, type); |
f4efbdfb | 3158 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
3159 | return err; |
3160 | } | |
3161 | cur_node = dfa->edests[cur_node].elems[0]; | |
3162 | } | |
3163 | return REG_NOERROR; | |
3164 | } | |
3165 | ||
3166 | ||
3167 | /* For all the back references in the current state, calculate the | |
3168 | destination of the back references by the appropriate entry | |
3169 | in MCTX->BKREF_ENTS. */ | |
3170 | ||
3171 | static reg_errcode_t | |
b41bd5bc | 3172 | __attribute_warn_unused_result__ |
e2f55264 | 3173 | expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes, |
eb04c213 | 3174 | Idx cur_str, Idx subexp_num, int type) |
6291ee3c | 3175 | { |
76b864c8 | 3176 | const re_dfa_t *const dfa = mctx->dfa; |
6291ee3c | 3177 | reg_errcode_t err; |
eb04c213 | 3178 | Idx cache_idx_start = search_cur_bkref_entry (mctx, cur_str); |
e40a38b3 | 3179 | struct re_backref_cache_entry *ent; |
6291ee3c | 3180 | |
e40a38b3 UD |
3181 | if (cache_idx_start == -1) |
3182 | return REG_NOERROR; | |
3183 | ||
3184 | restart: | |
3185 | ent = mctx->bkref_ents + cache_idx_start; | |
3186 | do | |
6291ee3c | 3187 | { |
eb04c213 | 3188 | Idx to_idx, next_node; |
e40a38b3 | 3189 | |
6291ee3c UD |
3190 | /* Is this entry ENT is appropriate? */ |
3191 | if (!re_node_set_contains (cur_nodes, ent->node)) | |
3192 | continue; /* No. */ | |
3193 | ||
3194 | to_idx = cur_str + ent->subexp_to - ent->subexp_from; | |
3195 | /* Calculate the destination of the back reference, and append it | |
3196 | to MCTX->STATE_LOG. */ | |
3197 | if (to_idx == cur_str) | |
3198 | { | |
3199 | /* The backreference did epsilon transit, we must re-check all the | |
3200 | node in the current state. */ | |
3201 | re_node_set new_dests; | |
3202 | reg_errcode_t err2, err3; | |
3203 | next_node = dfa->edests[ent->node].elems[0]; | |
3204 | if (re_node_set_contains (cur_nodes, next_node)) | |
3205 | continue; | |
3206 | err = re_node_set_init_1 (&new_dests, next_node); | |
0ce7f49c | 3207 | err2 = check_arrival_expand_ecl (dfa, &new_dests, subexp_num, type); |
6291ee3c UD |
3208 | err3 = re_node_set_merge (cur_nodes, &new_dests); |
3209 | re_node_set_free (&new_dests); | |
f4efbdfb PE |
3210 | if (__glibc_unlikely (err != REG_NOERROR || err2 != REG_NOERROR |
3211 | || err3 != REG_NOERROR)) | |
6291ee3c UD |
3212 | { |
3213 | err = (err != REG_NOERROR ? err | |
3214 | : (err2 != REG_NOERROR ? err2 : err3)); | |
3215 | return err; | |
3216 | } | |
3217 | /* TODO: It is still inefficient... */ | |
e40a38b3 | 3218 | goto restart; |
6291ee3c UD |
3219 | } |
3220 | else | |
3221 | { | |
3222 | re_node_set union_set; | |
3223 | next_node = dfa->nexts[ent->node]; | |
3224 | if (mctx->state_log[to_idx]) | |
3225 | { | |
eb04c213 | 3226 | bool ok; |
6291ee3c UD |
3227 | if (re_node_set_contains (&mctx->state_log[to_idx]->nodes, |
3228 | next_node)) | |
3229 | continue; | |
3230 | err = re_node_set_init_copy (&union_set, | |
3231 | &mctx->state_log[to_idx]->nodes); | |
eb04c213 | 3232 | ok = re_node_set_insert (&union_set, next_node); |
f4efbdfb | 3233 | if (__glibc_unlikely (err != REG_NOERROR || ! ok)) |
6291ee3c UD |
3234 | { |
3235 | re_node_set_free (&union_set); | |
3236 | err = err != REG_NOERROR ? err : REG_ESPACE; | |
3237 | return err; | |
3238 | } | |
3239 | } | |
3240 | else | |
3241 | { | |
3242 | err = re_node_set_init_1 (&union_set, next_node); | |
f4efbdfb | 3243 | if (__glibc_unlikely (err != REG_NOERROR)) |
6291ee3c UD |
3244 | return err; |
3245 | } | |
3246 | mctx->state_log[to_idx] = re_acquire_state (&err, dfa, &union_set); | |
3247 | re_node_set_free (&union_set); | |
f4efbdfb PE |
3248 | if (__glibc_unlikely (mctx->state_log[to_idx] == NULL |
3249 | && err != REG_NOERROR)) | |
6291ee3c UD |
3250 | return err; |
3251 | } | |
3252 | } | |
e40a38b3 | 3253 | while (ent++->more); |
6291ee3c UD |
3254 | return REG_NOERROR; |
3255 | } | |
3256 | ||
3257 | /* Build transition table for the state. | |
eb04c213 | 3258 | Return true if successful. */ |
6291ee3c | 3259 | |
f3e5c726 | 3260 | static bool __attribute_noinline__ |
e2f55264 | 3261 | build_trtable (const re_dfa_t *dfa, re_dfastate_t *state) |
6291ee3c UD |
3262 | { |
3263 | reg_errcode_t err; | |
eb04c213 AZ |
3264 | Idx i, j; |
3265 | int ch; | |
3266 | bool need_word_trtable = false; | |
2c05d33f | 3267 | bitset_word_t elem, mask; |
eb04c213 | 3268 | Idx ndests; /* Number of the destination states from 'state'. */ |
6291ee3c | 3269 | re_dfastate_t **trtable; |
f3e5c726 AZ |
3270 | re_dfastate_t *dest_states[SBC_MAX]; |
3271 | re_dfastate_t *dest_states_word[SBC_MAX]; | |
3272 | re_dfastate_t *dest_states_nl[SBC_MAX]; | |
3273 | re_node_set follows; | |
2c05d33f UD |
3274 | bitset_t acceptable; |
3275 | ||
3b0bdc72 | 3276 | /* We build DFA states which corresponds to the destination nodes |
eb04c213 AZ |
3277 | from 'state'. 'dests_node[i]' represents the nodes which i-th |
3278 | destination state contains, and 'dests_ch[i]' represents the | |
3b0bdc72 | 3279 | characters which i-th destination state accepts. */ |
f3e5c726 AZ |
3280 | re_node_set dests_node[SBC_MAX]; |
3281 | bitset_t dests_ch[SBC_MAX]; | |
3b0bdc72 | 3282 | |
eb04c213 | 3283 | /* Initialize transition table. */ |
ab4b89fe | 3284 | state->word_trtable = state->trtable = NULL; |
3b0bdc72 | 3285 | |
eb04c213 | 3286 | /* At first, group all nodes belonging to 'state' into several |
3b0bdc72 | 3287 | destinations. */ |
56b168be | 3288 | ndests = group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch); |
f4efbdfb | 3289 | if (__glibc_unlikely (ndests <= 0)) |
3b0bdc72 | 3290 | { |
eb04c213 | 3291 | /* Return false in case of an error, true otherwise. */ |
05dab910 | 3292 | if (ndests == 0) |
c13c99fa | 3293 | { |
3ce12656 | 3294 | state->trtable = (re_dfastate_t **) |
ab4b89fe | 3295 | calloc (sizeof (re_dfastate_t *), SBC_MAX); |
f4efbdfb | 3296 | if (__glibc_unlikely (state->trtable == NULL)) |
eb04c213 AZ |
3297 | return false; |
3298 | return true; | |
c13c99fa | 3299 | } |
eb04c213 | 3300 | return false; |
3b0bdc72 UD |
3301 | } |
3302 | ||
a9388965 | 3303 | err = re_node_set_alloc (&follows, ndests + 1); |
f4efbdfb | 3304 | if (__glibc_unlikely (err != REG_NOERROR)) |
05dab910 | 3305 | { |
f3e5c726 AZ |
3306 | out_free: |
3307 | re_node_set_free (&follows); | |
3308 | for (i = 0; i < ndests; ++i) | |
3309 | re_node_set_free (dests_node + i); | |
3310 | return false; | |
05dab910 | 3311 | } |
f3e5c726 | 3312 | |
05dab910 | 3313 | bitset_empty (acceptable); |
a9388965 | 3314 | |
3b0bdc72 UD |
3315 | /* Then build the states for all destinations. */ |
3316 | for (i = 0; i < ndests; ++i) | |
3317 | { | |
eb04c213 | 3318 | Idx next_node; |
3b0bdc72 UD |
3319 | re_node_set_empty (&follows); |
3320 | /* Merge the follows of this destination states. */ | |
3321 | for (j = 0; j < dests_node[i].nelem; ++j) | |
15a7d175 UD |
3322 | { |
3323 | next_node = dfa->nexts[dests_node[i].elems[j]]; | |
3324 | if (next_node != -1) | |
3325 | { | |
3326 | err = re_node_set_merge (&follows, dfa->eclosures + next_node); | |
f4efbdfb | 3327 | if (__glibc_unlikely (err != REG_NOERROR)) |
05dab910 | 3328 | goto out_free; |
15a7d175 UD |
3329 | } |
3330 | } | |
a9388965 | 3331 | dest_states[i] = re_acquire_state_context (&err, dfa, &follows, 0); |
f4efbdfb | 3332 | if (__glibc_unlikely (dest_states[i] == NULL && err != REG_NOERROR)) |
15a7d175 | 3333 | goto out_free; |
3b0bdc72 | 3334 | /* If the new state has context constraint, |
15a7d175 | 3335 | build appropriate states for these contexts. */ |
3b0bdc72 | 3336 | if (dest_states[i]->has_constraint) |
15a7d175 UD |
3337 | { |
3338 | dest_states_word[i] = re_acquire_state_context (&err, dfa, &follows, | |
3339 | CONTEXT_WORD); | |
f4efbdfb PE |
3340 | if (__glibc_unlikely (dest_states_word[i] == NULL |
3341 | && err != REG_NOERROR)) | |
15a7d175 | 3342 | goto out_free; |
3ce12656 | 3343 | |
ab4b89fe | 3344 | if (dest_states[i] != dest_states_word[i] && dfa->mb_cur_max > 1) |
eb04c213 | 3345 | need_word_trtable = true; |
3ce12656 | 3346 | |
15a7d175 UD |
3347 | dest_states_nl[i] = re_acquire_state_context (&err, dfa, &follows, |
3348 | CONTEXT_NEWLINE); | |
f4efbdfb | 3349 | if (__glibc_unlikely (dest_states_nl[i] == NULL && err != REG_NOERROR)) |
15a7d175 | 3350 | goto out_free; |
eb04c213 | 3351 | } |
3b0bdc72 | 3352 | else |
15a7d175 UD |
3353 | { |
3354 | dest_states_word[i] = dest_states[i]; | |
3355 | dest_states_nl[i] = dest_states[i]; | |
3356 | } | |
3b0bdc72 UD |
3357 | bitset_merge (acceptable, dests_ch[i]); |
3358 | } | |
3359 | ||
f4efbdfb | 3360 | if (!__glibc_unlikely (need_word_trtable)) |
3ce12656 UD |
3361 | { |
3362 | /* We don't care about whether the following character is a word | |
3363 | character, or we are in a single-byte character set so we can | |
3364 | discern by looking at the character code: allocate a | |
3365 | 256-entry transition table. */ | |
ab4b89fe UD |
3366 | trtable = state->trtable = |
3367 | (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX); | |
f4efbdfb | 3368 | if (__glibc_unlikely (trtable == NULL)) |
3ce12656 UD |
3369 | goto out_free; |
3370 | ||
3371 | /* For all characters ch...: */ | |
2c05d33f UD |
3372 | for (i = 0; i < BITSET_WORDS; ++i) |
3373 | for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1; | |
3ce12656 UD |
3374 | elem; |
3375 | mask <<= 1, elem >>= 1, ++ch) | |
f4efbdfb | 3376 | if (__glibc_unlikely (elem & 1)) |
3ce12656 UD |
3377 | { |
3378 | /* There must be exactly one destination which accepts | |
3379 | character ch. See group_nodes_into_DFAstates. */ | |
3380 | for (j = 0; (dests_ch[j][i] & mask) == 0; ++j) | |
3381 | ; | |
6b6557e8 | 3382 | |
3ce12656 | 3383 | /* j-th destination accepts the word character ch. */ |
56b168be | 3384 | if (dfa->word_char[i] & mask) |
3ce12656 UD |
3385 | trtable[ch] = dest_states_word[j]; |
3386 | else | |
3387 | trtable[ch] = dest_states[j]; | |
3388 | } | |
3389 | } | |
3390 | else | |
3391 | { | |
3392 | /* We care about whether the following character is a word | |
3393 | character, and we are in a multi-byte character set: discern | |
3394 | by looking at the character code: build two 256-entry | |
3395 | transition tables, one starting at trtable[0] and one | |
3396 | starting at trtable[SBC_MAX]. */ | |
ab4b89fe UD |
3397 | trtable = state->word_trtable = |
3398 | (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX); | |
f4efbdfb | 3399 | if (__glibc_unlikely (trtable == NULL)) |
3ce12656 | 3400 | goto out_free; |
6b6557e8 | 3401 | |
3ce12656 | 3402 | /* For all characters ch...: */ |
2c05d33f UD |
3403 | for (i = 0; i < BITSET_WORDS; ++i) |
3404 | for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1; | |
3ce12656 UD |
3405 | elem; |
3406 | mask <<= 1, elem >>= 1, ++ch) | |
f4efbdfb | 3407 | if (__glibc_unlikely (elem & 1)) |
3ce12656 UD |
3408 | { |
3409 | /* There must be exactly one destination which accepts | |
3410 | character ch. See group_nodes_into_DFAstates. */ | |
3411 | for (j = 0; (dests_ch[j][i] & mask) == 0; ++j) | |
3412 | ; | |
6b6557e8 | 3413 | |
3ce12656 UD |
3414 | /* j-th destination accepts the word character ch. */ |
3415 | trtable[ch] = dest_states[j]; | |
3416 | trtable[ch + SBC_MAX] = dest_states_word[j]; | |
3417 | } | |
3418 | } | |
6b6557e8 | 3419 | |
3b0bdc72 | 3420 | /* new line */ |
c202c2c5 UD |
3421 | if (bitset_contain (acceptable, NEWLINE_CHAR)) |
3422 | { | |
3423 | /* The current state accepts newline character. */ | |
3ce12656 UD |
3424 | for (j = 0; j < ndests; ++j) |
3425 | if (bitset_contain (dests_ch[j], NEWLINE_CHAR)) | |
15a7d175 UD |
3426 | { |
3427 | /* k-th destination accepts newline character. */ | |
3ce12656 | 3428 | trtable[NEWLINE_CHAR] = dest_states_nl[j]; |
ab4b89fe | 3429 | if (need_word_trtable) |
3ce12656 | 3430 | trtable[NEWLINE_CHAR + SBC_MAX] = dest_states_nl[j]; |
15a7d175 UD |
3431 | /* There must be only one destination which accepts |
3432 | newline. See group_nodes_into_DFAstates. */ | |
3433 | break; | |
3434 | } | |
c202c2c5 | 3435 | } |
3b0bdc72 | 3436 | |
3b0bdc72 UD |
3437 | re_node_set_free (&follows); |
3438 | for (i = 0; i < ndests; ++i) | |
3439 | re_node_set_free (dests_node + i); | |
eb04c213 | 3440 | return true; |
3b0bdc72 UD |
3441 | } |
3442 | ||
3443 | /* Group all nodes belonging to STATE into several destinations. | |
3444 | Then for all destinations, set the nodes belonging to the destination | |
3445 | to DESTS_NODE[i] and set the characters accepted by the destination | |
0b5ca7c3 PE |
3446 | to DEST_CH[i]. Return the number of destinations if successful, |
3447 | -1 on internal error. */ | |
3b0bdc72 | 3448 | |
eb04c213 | 3449 | static Idx |
e2f55264 UD |
3450 | group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state, |
3451 | re_node_set *dests_node, bitset_t *dests_ch) | |
3b0bdc72 | 3452 | { |
a9388965 | 3453 | reg_errcode_t err; |
eb04c213 AZ |
3454 | bool ok; |
3455 | Idx i, j, k; | |
3456 | Idx ndests; /* Number of the destinations from 'state'. */ | |
2c05d33f | 3457 | bitset_t accepts; /* Characters a node can accept. */ |
3b0bdc72 UD |
3458 | const re_node_set *cur_nodes = &state->nodes; |
3459 | bitset_empty (accepts); | |
3460 | ndests = 0; | |
3461 | ||
eb04c213 | 3462 | /* For all the nodes belonging to 'state', */ |
3b0bdc72 UD |
3463 | for (i = 0; i < cur_nodes->nelem; ++i) |
3464 | { | |
3b0bdc72 UD |
3465 | re_token_t *node = &dfa->nodes[cur_nodes->elems[i]]; |
3466 | re_token_type_t type = node->type; | |
485d775d | 3467 | unsigned int constraint = node->constraint; |
3b0bdc72 UD |
3468 | |
3469 | /* Enumerate all single byte character this node can accept. */ | |
3470 | if (type == CHARACTER) | |
15a7d175 | 3471 | bitset_set (accepts, node->opr.c); |
3b0bdc72 | 3472 | else if (type == SIMPLE_BRACKET) |
15a7d175 UD |
3473 | { |
3474 | bitset_merge (accepts, node->opr.sbcset); | |
3475 | } | |
3b0bdc72 | 3476 | else if (type == OP_PERIOD) |
15a7d175 | 3477 | { |
65e6becf UD |
3478 | #ifdef RE_ENABLE_I18N |
3479 | if (dfa->mb_cur_max > 1) | |
3480 | bitset_merge (accepts, dfa->sb_char); | |
3481 | else | |
0ce7f49c | 3482 | #endif |
65e6becf | 3483 | bitset_set_all (accepts); |
56b168be | 3484 | if (!(dfa->syntax & RE_DOT_NEWLINE)) |
15a7d175 | 3485 | bitset_clear (accepts, '\n'); |
56b168be | 3486 | if (dfa->syntax & RE_DOT_NOT_NULL) |
15a7d175 UD |
3487 | bitset_clear (accepts, '\0'); |
3488 | } | |
c0d5034e | 3489 | #ifdef RE_ENABLE_I18N |
ad7f28c2 | 3490 | else if (type == OP_UTF8_PERIOD) |
2da42bc0 | 3491 | { |
eb04c213 AZ |
3492 | if (ASCII_CHARS % BITSET_WORD_BITS == 0) |
3493 | memset (accepts, -1, ASCII_CHARS / CHAR_BIT); | |
3494 | else | |
3495 | bitset_merge (accepts, utf8_sb_map); | |
56b168be | 3496 | if (!(dfa->syntax & RE_DOT_NEWLINE)) |
ad7f28c2 | 3497 | bitset_clear (accepts, '\n'); |
56b168be | 3498 | if (dfa->syntax & RE_DOT_NOT_NULL) |
ad7f28c2 | 3499 | bitset_clear (accepts, '\0'); |
2da42bc0 | 3500 | } |
c0d5034e | 3501 | #endif |
3b0bdc72 | 3502 | else |
15a7d175 | 3503 | continue; |
3b0bdc72 | 3504 | |
eb04c213 | 3505 | /* Check the 'accepts' and sift the characters which are not |
15a7d175 | 3506 | match it the context. */ |
3b0bdc72 | 3507 | if (constraint) |
15a7d175 | 3508 | { |
15a7d175 UD |
3509 | if (constraint & NEXT_NEWLINE_CONSTRAINT) |
3510 | { | |
2c05d33f | 3511 | bool accepts_newline = bitset_contain (accepts, NEWLINE_CHAR); |
15a7d175 UD |
3512 | bitset_empty (accepts); |
3513 | if (accepts_newline) | |
3514 | bitset_set (accepts, NEWLINE_CHAR); | |
3515 | else | |
3516 | continue; | |
3517 | } | |
66b110e8 UD |
3518 | if (constraint & NEXT_ENDBUF_CONSTRAINT) |
3519 | { | |
3520 | bitset_empty (accepts); | |
3521 | continue; | |
3522 | } | |
c13c99fa | 3523 | |
66b110e8 | 3524 | if (constraint & NEXT_WORD_CONSTRAINT) |
65e6becf | 3525 | { |
2c05d33f | 3526 | bitset_word_t any_set = 0; |
1cef7b3c UD |
3527 | if (type == CHARACTER && !node->word_char) |
3528 | { | |
3529 | bitset_empty (accepts); | |
3530 | continue; | |
3531 | } | |
65e6becf UD |
3532 | #ifdef RE_ENABLE_I18N |
3533 | if (dfa->mb_cur_max > 1) | |
2c05d33f | 3534 | for (j = 0; j < BITSET_WORDS; ++j) |
457beec8 | 3535 | any_set |= (accepts[j] &= (dfa->word_char[j] | ~dfa->sb_char[j])); |
65e6becf UD |
3536 | else |
3537 | #endif | |
2c05d33f | 3538 | for (j = 0; j < BITSET_WORDS; ++j) |
457beec8 UD |
3539 | any_set |= (accepts[j] &= dfa->word_char[j]); |
3540 | if (!any_set) | |
3541 | continue; | |
65e6becf | 3542 | } |
66b110e8 | 3543 | if (constraint & NEXT_NOTWORD_CONSTRAINT) |
65e6becf | 3544 | { |
2c05d33f | 3545 | bitset_word_t any_set = 0; |
1cef7b3c UD |
3546 | if (type == CHARACTER && node->word_char) |
3547 | { | |
3548 | bitset_empty (accepts); | |
3549 | continue; | |
3550 | } | |
65e6becf UD |
3551 | #ifdef RE_ENABLE_I18N |
3552 | if (dfa->mb_cur_max > 1) | |
2c05d33f | 3553 | for (j = 0; j < BITSET_WORDS; ++j) |
457beec8 | 3554 | any_set |= (accepts[j] &= ~(dfa->word_char[j] & dfa->sb_char[j])); |
65e6becf UD |
3555 | else |
3556 | #endif | |
2c05d33f | 3557 | for (j = 0; j < BITSET_WORDS; ++j) |
457beec8 UD |
3558 | any_set |= (accepts[j] &= ~dfa->word_char[j]); |
3559 | if (!any_set) | |
3560 | continue; | |
65e6becf | 3561 | } |
15a7d175 | 3562 | } |
3b0bdc72 | 3563 | |
eb04c213 | 3564 | /* Then divide 'accepts' into DFA states, or create a new |
457beec8 | 3565 | state. Above, we make sure that accepts is not empty. */ |
3b0bdc72 | 3566 | for (j = 0; j < ndests; ++j) |
15a7d175 | 3567 | { |
2c05d33f UD |
3568 | bitset_t intersec; /* Intersection sets, see below. */ |
3569 | bitset_t remains; | |
15a7d175 | 3570 | /* Flags, see below. */ |
2c05d33f | 3571 | bitset_word_t has_intersec, not_subset, not_consumed; |
15a7d175 UD |
3572 | |
3573 | /* Optimization, skip if this state doesn't accept the character. */ | |
3574 | if (type == CHARACTER && !bitset_contain (dests_ch[j], node->opr.c)) | |
3575 | continue; | |
3576 | ||
eb04c213 | 3577 | /* Enumerate the intersection set of this state and 'accepts'. */ |
15a7d175 | 3578 | has_intersec = 0; |
2c05d33f | 3579 | for (k = 0; k < BITSET_WORDS; ++k) |
15a7d175 UD |
3580 | has_intersec |= intersec[k] = accepts[k] & dests_ch[j][k]; |
3581 | /* And skip if the intersection set is empty. */ | |
3582 | if (!has_intersec) | |
3583 | continue; | |
3584 | ||
eb04c213 | 3585 | /* Then check if this state is a subset of 'accepts'. */ |
15a7d175 | 3586 | not_subset = not_consumed = 0; |
2c05d33f | 3587 | for (k = 0; k < BITSET_WORDS; ++k) |
15a7d175 UD |
3588 | { |
3589 | not_subset |= remains[k] = ~accepts[k] & dests_ch[j][k]; | |
3590 | not_consumed |= accepts[k] = accepts[k] & ~dests_ch[j][k]; | |
3591 | } | |
3592 | ||
eb04c213 AZ |
3593 | /* If this state isn't a subset of 'accepts', create a |
3594 | new group state, which has the 'remains'. */ | |
15a7d175 UD |
3595 | if (not_subset) |
3596 | { | |
3597 | bitset_copy (dests_ch[ndests], remains); | |
3598 | bitset_copy (dests_ch[j], intersec); | |
3599 | err = re_node_set_init_copy (dests_node + ndests, &dests_node[j]); | |
f4efbdfb | 3600 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
3601 | goto error_return; |
3602 | ++ndests; | |
3603 | } | |
3604 | ||
3605 | /* Put the position in the current group. */ | |
eb04c213 | 3606 | ok = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]); |
f4efbdfb | 3607 | if (__glibc_unlikely (! ok)) |
15a7d175 UD |
3608 | goto error_return; |
3609 | ||
3610 | /* If all characters are consumed, go to next node. */ | |
3611 | if (!not_consumed) | |
3612 | break; | |
3613 | } | |
3b0bdc72 UD |
3614 | /* Some characters remain, create a new group. */ |
3615 | if (j == ndests) | |
15a7d175 UD |
3616 | { |
3617 | bitset_copy (dests_ch[ndests], accepts); | |
3618 | err = re_node_set_init_1 (dests_node + ndests, cur_nodes->elems[i]); | |
f4efbdfb | 3619 | if (__glibc_unlikely (err != REG_NOERROR)) |
15a7d175 UD |
3620 | goto error_return; |
3621 | ++ndests; | |
3622 | bitset_empty (accepts); | |
3623 | } | |
3b0bdc72 | 3624 | } |
2a0356e1 | 3625 | assume (ndests <= SBC_MAX); |
3b0bdc72 | 3626 | return ndests; |
1b2c2628 UD |
3627 | error_return: |
3628 | for (j = 0; j < ndests; ++j) | |
3629 | re_node_set_free (dests_node + j); | |
3630 | return -1; | |
3b0bdc72 UD |
3631 | } |
3632 | ||
434d3784 | 3633 | #ifdef RE_ENABLE_I18N |
eb04c213 | 3634 | /* Check how many bytes the node 'dfa->nodes[node_idx]' accepts. |
434d3784 UD |
3635 | Return the number of the bytes the node accepts. |
3636 | STR_IDX is the current index of the input string. | |
3637 | ||
3638 | This function handles the nodes which can accept one character, or | |
3639 | one collating element like '.', '[a-z]', opposite to the other nodes | |
3640 | can only accept one byte. */ | |
3b0bdc72 | 3641 | |
8c0ab919 RM |
3642 | # ifdef _LIBC |
3643 | # include <locale/weight.h> | |
3644 | # endif | |
3645 | ||
3b0bdc72 | 3646 | static int |
eb04c213 AZ |
3647 | check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx, |
3648 | const re_string_t *input, Idx str_idx) | |
3b0bdc72 | 3649 | { |
3b0bdc72 | 3650 | const re_token_t *node = dfa->nodes + node_idx; |
ad7f28c2 | 3651 | int char_len, elem_len; |
eb04c213 | 3652 | Idx i; |
ad7f28c2 | 3653 | |
f4efbdfb | 3654 | if (__glibc_unlikely (node->type == OP_UTF8_PERIOD)) |
ad7f28c2 UD |
3655 | { |
3656 | unsigned char c = re_string_byte_at (input, str_idx), d; | |
f4efbdfb | 3657 | if (__glibc_likely (c < 0xc2)) |
ad7f28c2 UD |
3658 | return 0; |
3659 | ||
3660 | if (str_idx + 2 > input->len) | |
3661 | return 0; | |
3662 | ||
3663 | d = re_string_byte_at (input, str_idx + 1); | |
3664 | if (c < 0xe0) | |
3665 | return (d < 0x80 || d > 0xbf) ? 0 : 2; | |
3666 | else if (c < 0xf0) | |
3667 | { | |
3668 | char_len = 3; | |
3669 | if (c == 0xe0 && d < 0xa0) | |
3670 | return 0; | |
3671 | } | |
3672 | else if (c < 0xf8) | |
3673 | { | |
3674 | char_len = 4; | |
3675 | if (c == 0xf0 && d < 0x90) | |
3676 | return 0; | |
3677 | } | |
3678 | else if (c < 0xfc) | |
3679 | { | |
3680 | char_len = 5; | |
3681 | if (c == 0xf8 && d < 0x88) | |
3682 | return 0; | |
3683 | } | |
3684 | else if (c < 0xfe) | |
3685 | { | |
3686 | char_len = 6; | |
3687 | if (c == 0xfc && d < 0x84) | |
3688 | return 0; | |
3689 | } | |
3690 | else | |
3691 | return 0; | |
3692 | ||
3693 | if (str_idx + char_len > input->len) | |
3694 | return 0; | |
3695 | ||
3696 | for (i = 1; i < char_len; ++i) | |
3697 | { | |
3698 | d = re_string_byte_at (input, str_idx + i); | |
3699 | if (d < 0x80 || d > 0xbf) | |
3700 | return 0; | |
3701 | } | |
3702 | return char_len; | |
3703 | } | |
3704 | ||
3705 | char_len = re_string_char_size_at (input, str_idx); | |
3b0bdc72 UD |
3706 | if (node->type == OP_PERIOD) |
3707 | { | |
ad7f28c2 | 3708 | if (char_len <= 1) |
2da42bc0 | 3709 | return 0; |
ad7f28c2 UD |
3710 | /* FIXME: I don't think this if is needed, as both '\n' |
3711 | and '\0' are char_len == 1. */ | |
434d3784 | 3712 | /* '.' accepts any one character except the following two cases. */ |
34a5a146 JM |
3713 | if ((!(dfa->syntax & RE_DOT_NEWLINE) |
3714 | && re_string_byte_at (input, str_idx) == '\n') | |
3715 | || ((dfa->syntax & RE_DOT_NOT_NULL) | |
3716 | && re_string_byte_at (input, str_idx) == '\0')) | |
15a7d175 | 3717 | return 0; |
3b0bdc72 UD |
3718 | return char_len; |
3719 | } | |
ad7f28c2 UD |
3720 | |
3721 | elem_len = re_string_elem_size_at (input, str_idx); | |
6c2a04a7 | 3722 | if ((elem_len <= 1 && char_len <= 1) || char_len == 0) |
ad7f28c2 UD |
3723 | return 0; |
3724 | ||
3725 | if (node->type == COMPLEX_BRACKET) | |
3b0bdc72 UD |
3726 | { |
3727 | const re_charset_t *cset = node->opr.mbcset; | |
434d3784 | 3728 | # ifdef _LIBC |
1c99f950 UD |
3729 | const unsigned char *pin |
3730 | = ((const unsigned char *) re_string_get_buffer (input) + str_idx); | |
eb04c213 | 3731 | Idx j; |
5f93cd52 | 3732 | uint32_t nrules; |
434d3784 UD |
3733 | # endif /* _LIBC */ |
3734 | int match_len = 0; | |
3735 | wchar_t wc = ((cset->nranges || cset->nchar_classes || cset->nmbchars) | |
15a7d175 | 3736 | ? re_string_wchar_at (input, str_idx) : 0); |
434d3784 UD |
3737 | |
3738 | /* match with multibyte character? */ | |
3739 | for (i = 0; i < cset->nmbchars; ++i) | |
15a7d175 UD |
3740 | if (wc == cset->mbchars[i]) |
3741 | { | |
3742 | match_len = char_len; | |
3743 | goto check_node_accept_bytes_match; | |
3744 | } | |
434d3784 UD |
3745 | /* match with character_class? */ |
3746 | for (i = 0; i < cset->nchar_classes; ++i) | |
15a7d175 UD |
3747 | { |
3748 | wctype_t wt = cset->char_classes[i]; | |
3749 | if (__iswctype (wc, wt)) | |
3750 | { | |
3751 | match_len = char_len; | |
3752 | goto check_node_accept_bytes_match; | |
3753 | } | |
3754 | } | |
434d3784 UD |
3755 | |
3756 | # ifdef _LIBC | |
5f93cd52 | 3757 | nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); |
3b0bdc72 | 3758 | if (nrules != 0) |
15a7d175 UD |
3759 | { |
3760 | unsigned int in_collseq = 0; | |
3761 | const int32_t *table, *indirect; | |
3762 | const unsigned char *weights, *extra; | |
3763 | const char *collseqwc; | |
3b0bdc72 | 3764 | |
15a7d175 UD |
3765 | /* match with collating_symbol? */ |
3766 | if (cset->ncoll_syms) | |
3767 | extra = (const unsigned char *) | |
3768 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB); | |
3769 | for (i = 0; i < cset->ncoll_syms; ++i) | |
3770 | { | |
442e3a21 AZN |
3771 | /* The compiler might warn that extra may be used uninitialized, |
3772 | however the loop will be executed iff ncoll_syms is larger | |
3773 | than 0,which means extra will be already initialized. */ | |
3774 | DIAG_PUSH_NEEDS_COMMENT; | |
3775 | DIAG_IGNORE_Os_NEEDS_COMMENT (8, "-Wmaybe-uninitialized"); | |
15a7d175 | 3776 | const unsigned char *coll_sym = extra + cset->coll_syms[i]; |
442e3a21 | 3777 | DIAG_POP_NEEDS_COMMENT; |
15a7d175 UD |
3778 | /* Compare the length of input collating element and |
3779 | the length of current collating element. */ | |
3780 | if (*coll_sym != elem_len) | |
3781 | continue; | |
3782 | /* Compare each bytes. */ | |
3783 | for (j = 0; j < *coll_sym; j++) | |
3784 | if (pin[j] != coll_sym[1 + j]) | |
3785 | break; | |
3786 | if (j == *coll_sym) | |
3787 | { | |
3788 | /* Match if every bytes is equal. */ | |
3789 | match_len = j; | |
3790 | goto check_node_accept_bytes_match; | |
3791 | } | |
3792 | } | |
3793 | ||
3794 | if (cset->nranges) | |
3795 | { | |
3796 | if (elem_len <= char_len) | |
3797 | { | |
3798 | collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC); | |
25337753 | 3799 | in_collseq = __collseq_table_lookup (collseqwc, wc); |
15a7d175 UD |
3800 | } |
3801 | else | |
3802 | in_collseq = find_collation_sequence_value (pin, elem_len); | |
3803 | } | |
3804 | /* match with range expression? */ | |
eb04c213 | 3805 | /* FIXME: Implement rational ranges here, too. */ |
15a7d175 UD |
3806 | for (i = 0; i < cset->nranges; ++i) |
3807 | if (cset->range_starts[i] <= in_collseq | |
3808 | && in_collseq <= cset->range_ends[i]) | |
3809 | { | |
3810 | match_len = elem_len; | |
3811 | goto check_node_accept_bytes_match; | |
3812 | } | |
3813 | ||
3814 | /* match with equivalence_class? */ | |
3815 | if (cset->nequiv_classes) | |
3816 | { | |
3817 | const unsigned char *cp = pin; | |
3818 | table = (const int32_t *) | |
3819 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB); | |
3820 | weights = (const unsigned char *) | |
3821 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB); | |
3822 | extra = (const unsigned char *) | |
3823 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB); | |
3824 | indirect = (const int32_t *) | |
3825 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB); | |
8c0ab919 | 3826 | int32_t idx = findidx (table, indirect, extra, &cp, elem_len); |
7b2f4ced FW |
3827 | int32_t rule = idx >> 24; |
3828 | idx &= 0xffffff; | |
15a7d175 | 3829 | if (idx > 0) |
7b2f4ced FW |
3830 | { |
3831 | size_t weight_len = weights[idx]; | |
3832 | for (i = 0; i < cset->nequiv_classes; ++i) | |
3833 | { | |
3834 | int32_t equiv_class_idx = cset->equiv_classes[i]; | |
3835 | int32_t equiv_class_rule = equiv_class_idx >> 24; | |
3836 | equiv_class_idx &= 0xffffff; | |
3837 | if (weights[equiv_class_idx] == weight_len | |
3838 | && equiv_class_rule == rule | |
3839 | && memcmp (weights + idx + 1, | |
3840 | weights + equiv_class_idx + 1, | |
3841 | weight_len) == 0) | |
3842 | { | |
3843 | match_len = elem_len; | |
3844 | goto check_node_accept_bytes_match; | |
3845 | } | |
3846 | } | |
3847 | } | |
15a7d175 UD |
3848 | } |
3849 | } | |
434d3784 UD |
3850 | else |
3851 | # endif /* _LIBC */ | |
15a7d175 UD |
3852 | { |
3853 | /* match with range expression? */ | |
15a7d175 UD |
3854 | for (i = 0; i < cset->nranges; ++i) |
3855 | { | |
eb04c213 | 3856 | if (cset->range_starts[i] <= wc && wc <= cset->range_ends[i]) |
15a7d175 UD |
3857 | { |
3858 | match_len = char_len; | |
3859 | goto check_node_accept_bytes_match; | |
3860 | } | |
3861 | } | |
3862 | } | |
434d3784 UD |
3863 | check_node_accept_bytes_match: |
3864 | if (!cset->non_match) | |
15a7d175 | 3865 | return match_len; |
434d3784 | 3866 | else |
15a7d175 UD |
3867 | { |
3868 | if (match_len > 0) | |
3869 | return 0; | |
3870 | else | |
3871 | return (elem_len > char_len) ? elem_len : char_len; | |
3872 | } | |
3b0bdc72 UD |
3873 | } |
3874 | return 0; | |
3875 | } | |
3876 | ||
434d3784 | 3877 | # ifdef _LIBC |
3b0bdc72 | 3878 | static unsigned int |
e2f55264 | 3879 | find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len) |
3b0bdc72 UD |
3880 | { |
3881 | uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); | |
3882 | if (nrules == 0) | |
3883 | { | |
3884 | if (mbs_len == 1) | |
15a7d175 UD |
3885 | { |
3886 | /* No valid character. Match it as a single byte character. */ | |
3887 | const unsigned char *collseq = (const unsigned char *) | |
3888 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB); | |
3889 | return collseq[mbs[0]]; | |
3890 | } | |
3b0bdc72 UD |
3891 | return UINT_MAX; |
3892 | } | |
3893 | else | |
3894 | { | |
3895 | int32_t idx; | |
3896 | const unsigned char *extra = (const unsigned char *) | |
15a7d175 | 3897 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB); |
6c2a04a7 UD |
3898 | int32_t extrasize = (const unsigned char *) |
3899 | _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB + 1) - extra; | |
3b0bdc72 | 3900 | |
6c2a04a7 | 3901 | for (idx = 0; idx < extrasize;) |
15a7d175 | 3902 | { |
eb04c213 AZ |
3903 | int mbs_cnt; |
3904 | bool found = false; | |
15a7d175 UD |
3905 | int32_t elem_mbs_len; |
3906 | /* Skip the name of collating element name. */ | |
3907 | idx = idx + extra[idx] + 1; | |
3908 | elem_mbs_len = extra[idx++]; | |
3909 | if (mbs_len == elem_mbs_len) | |
3910 | { | |
3911 | for (mbs_cnt = 0; mbs_cnt < elem_mbs_len; ++mbs_cnt) | |
3912 | if (extra[idx + mbs_cnt] != mbs[mbs_cnt]) | |
3913 | break; | |
3914 | if (mbs_cnt == elem_mbs_len) | |
3915 | /* Found the entry. */ | |
eb04c213 | 3916 | found = true; |
15a7d175 UD |
3917 | } |
3918 | /* Skip the byte sequence of the collating element. */ | |
3919 | idx += elem_mbs_len; | |
3920 | /* Adjust for the alignment. */ | |
3921 | idx = (idx + 3) & ~3; | |
3922 | /* Skip the collation sequence value. */ | |
3923 | idx += sizeof (uint32_t); | |
3924 | /* Skip the wide char sequence of the collating element. */ | |
d84acf38 | 3925 | idx = idx + sizeof (uint32_t) * (*(int32_t *) (extra + idx) + 1); |
15a7d175 UD |
3926 | /* If we found the entry, return the sequence value. */ |
3927 | if (found) | |
3928 | return *(uint32_t *) (extra + idx); | |
3929 | /* Skip the collation sequence value. */ | |
3930 | idx += sizeof (uint32_t); | |
3931 | } | |
6c2a04a7 | 3932 | return UINT_MAX; |
3b0bdc72 UD |
3933 | } |
3934 | } | |
434d3784 UD |
3935 | # endif /* _LIBC */ |
3936 | #endif /* RE_ENABLE_I18N */ | |
3b0bdc72 UD |
3937 | |
3938 | /* Check whether the node accepts the byte which is IDX-th | |
3939 | byte of the INPUT. */ | |
3940 | ||
eb04c213 | 3941 | static bool |
e2f55264 | 3942 | check_node_accept (const re_match_context_t *mctx, const re_token_t *node, |
eb04c213 | 3943 | Idx idx) |
3b0bdc72 | 3944 | { |
3b0bdc72 | 3945 | unsigned char ch; |
56b168be | 3946 | ch = re_string_byte_at (&mctx->input, idx); |
ad7f28c2 UD |
3947 | switch (node->type) |
3948 | { | |
3949 | case CHARACTER: | |
5cf1ec52 | 3950 | if (node->opr.c != ch) |
eb04c213 | 3951 | return false; |
5cf1ec52 UD |
3952 | break; |
3953 | ||
ad7f28c2 | 3954 | case SIMPLE_BRACKET: |
5cf1ec52 | 3955 | if (!bitset_contain (node->opr.sbcset, ch)) |
eb04c213 | 3956 | return false; |
5cf1ec52 UD |
3957 | break; |
3958 | ||
c0d5034e | 3959 | #ifdef RE_ENABLE_I18N |
ad7f28c2 | 3960 | case OP_UTF8_PERIOD: |
eb04c213 AZ |
3961 | if (ch >= ASCII_CHARS) |
3962 | return false; | |
3963 | FALLTHROUGH; | |
c0d5034e | 3964 | #endif |
ad7f28c2 | 3965 | case OP_PERIOD: |
5cf1ec52 UD |
3966 | if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE)) |
3967 | || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL))) | |
eb04c213 | 3968 | return false; |
5cf1ec52 UD |
3969 | break; |
3970 | ||
ad7f28c2 | 3971 | default: |
eb04c213 | 3972 | return false; |
ad7f28c2 | 3973 | } |
5cf1ec52 UD |
3974 | |
3975 | if (node->constraint) | |
3976 | { | |
3977 | /* The node has constraints. Check whether the current context | |
3978 | satisfies the constraints. */ | |
3979 | unsigned int context = re_string_context_at (&mctx->input, idx, | |
3980 | mctx->eflags); | |
3981 | if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context)) | |
eb04c213 | 3982 | return false; |
5cf1ec52 UD |
3983 | } |
3984 | ||
eb04c213 | 3985 | return true; |
3b0bdc72 | 3986 | } |
612546c6 UD |
3987 | |
3988 | /* Extend the buffers, if the buffers have run out. */ | |
3989 | ||
3990 | static reg_errcode_t | |
b41bd5bc | 3991 | __attribute_warn_unused_result__ |
a445af0b | 3992 | extend_buffers (re_match_context_t *mctx, int min_len) |
612546c6 UD |
3993 | { |
3994 | reg_errcode_t ret; | |
56b168be | 3995 | re_string_t *pstr = &mctx->input; |
612546c6 | 3996 | |
aef699dc | 3997 | /* Avoid overflow. */ |
f4efbdfb PE |
3998 | if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *)) / 2 |
3999 | <= pstr->bufs_len)) | |
aef699dc PE |
4000 | return REG_ESPACE; |
4001 | ||
eb04c213 | 4002 | /* Double the lengths of the buffers, but allocate at least MIN_LEN. */ |
a445af0b AS |
4003 | ret = re_string_realloc_buffers (pstr, |
4004 | MAX (min_len, | |
4005 | MIN (pstr->len, pstr->bufs_len * 2))); | |
f4efbdfb | 4006 | if (__glibc_unlikely (ret != REG_NOERROR)) |
612546c6 UD |
4007 | return ret; |
4008 | ||
4009 | if (mctx->state_log != NULL) | |
4010 | { | |
4011 | /* And double the length of state_log. */ | |
951d6408 UD |
4012 | /* XXX We have no indication of the size of this buffer. If this |
4013 | allocation fail we have no indication that the state_log array | |
4014 | does not have the right size. */ | |
4015 | re_dfastate_t **new_array = re_realloc (mctx->state_log, re_dfastate_t *, | |
4016 | pstr->bufs_len + 1); | |
f4efbdfb | 4017 | if (__glibc_unlikely (new_array == NULL)) |
15a7d175 | 4018 | return REG_ESPACE; |
1b2c2628 | 4019 | mctx->state_log = new_array; |
612546c6 UD |
4020 | } |
4021 | ||
4022 | /* Then reconstruct the buffers. */ | |
4023 | if (pstr->icase) | |
4024 | { | |
4025 | #ifdef RE_ENABLE_I18N | |
3c0fb574 | 4026 | if (pstr->mb_cur_max > 1) |
bb3f4825 UD |
4027 | { |
4028 | ret = build_wcs_upper_buffer (pstr); | |
f4efbdfb | 4029 | if (__glibc_unlikely (ret != REG_NOERROR)) |
bb3f4825 UD |
4030 | return ret; |
4031 | } | |
612546c6 UD |
4032 | else |
4033 | #endif /* RE_ENABLE_I18N */ | |
15a7d175 | 4034 | build_upper_buffer (pstr); |
612546c6 UD |
4035 | } |
4036 | else | |
4037 | { | |
4038 | #ifdef RE_ENABLE_I18N | |
3c0fb574 | 4039 | if (pstr->mb_cur_max > 1) |
15a7d175 | 4040 | build_wcs_buffer (pstr); |
612546c6 UD |
4041 | else |
4042 | #endif /* RE_ENABLE_I18N */ | |
15a7d175 UD |
4043 | { |
4044 | if (pstr->trans != NULL) | |
4045 | re_string_translate_buffer (pstr); | |
15a7d175 | 4046 | } |
612546c6 UD |
4047 | } |
4048 | return REG_NOERROR; | |
4049 | } | |
4050 | ||
3b0bdc72 UD |
4051 | \f |
4052 | /* Functions for matching context. */ | |
4053 | ||
6291ee3c UD |
4054 | /* Initialize MCTX. */ |
4055 | ||
a9388965 | 4056 | static reg_errcode_t |
b41bd5bc | 4057 | __attribute_warn_unused_result__ |
eb04c213 | 4058 | match_ctx_init (re_match_context_t *mctx, int eflags, Idx n) |
3b0bdc72 UD |
4059 | { |
4060 | mctx->eflags = eflags; | |
612546c6 | 4061 | mctx->match_last = -1; |
3b0bdc72 | 4062 | if (n > 0) |
a9388965 | 4063 | { |
eb04c213 AZ |
4064 | /* Avoid overflow. */ |
4065 | size_t max_object_size = | |
4066 | MAX (sizeof (struct re_backref_cache_entry), | |
4067 | sizeof (re_sub_match_top_t *)); | |
f4efbdfb | 4068 | if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / max_object_size) < n)) |
eb04c213 AZ |
4069 | return REG_ESPACE; |
4070 | ||
a9388965 | 4071 | mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n); |
6291ee3c | 4072 | mctx->sub_tops = re_malloc (re_sub_match_top_t *, n); |
f4efbdfb | 4073 | if (__glibc_unlikely (mctx->bkref_ents == NULL || mctx->sub_tops == NULL)) |
15a7d175 | 4074 | return REG_ESPACE; |
a9388965 | 4075 | } |
56b168be UD |
4076 | /* Already zero-ed by the caller. |
4077 | else | |
4078 | mctx->bkref_ents = NULL; | |
4079 | mctx->nbkref_ents = 0; | |
4080 | mctx->nsub_tops = 0; */ | |
3b0bdc72 | 4081 | mctx->abkref_ents = n; |
6291ee3c | 4082 | mctx->max_mb_elem_len = 1; |
6291ee3c | 4083 | mctx->asub_tops = n; |
a9388965 | 4084 | return REG_NOERROR; |
3b0bdc72 UD |
4085 | } |
4086 | ||
6291ee3c UD |
4087 | /* Clean the entries which depend on the current input in MCTX. |
4088 | This function must be invoked when the matcher changes the start index | |
4089 | of the input, or changes the input string. */ | |
4090 | ||
4091 | static void | |
e2f55264 | 4092 | match_ctx_clean (re_match_context_t *mctx) |
6291ee3c | 4093 | { |
eb04c213 | 4094 | Idx st_idx; |
6291ee3c UD |
4095 | for (st_idx = 0; st_idx < mctx->nsub_tops; ++st_idx) |
4096 | { | |
eb04c213 | 4097 | Idx sl_idx; |
6291ee3c UD |
4098 | re_sub_match_top_t *top = mctx->sub_tops[st_idx]; |
4099 | for (sl_idx = 0; sl_idx < top->nlasts; ++sl_idx) | |
4100 | { | |
4101 | re_sub_match_last_t *last = top->lasts[sl_idx]; | |
4102 | re_free (last->path.array); | |
4103 | re_free (last); | |
4104 | } | |
4105 | re_free (top->lasts); | |
4106 | if (top->path) | |
4107 | { | |
4108 | re_free (top->path->array); | |
4109 | re_free (top->path); | |
4110 | } | |
eb04c213 | 4111 | re_free (top); |
6291ee3c | 4112 | } |
cb265fec UD |
4113 | |
4114 | mctx->nsub_tops = 0; | |
4115 | mctx->nbkref_ents = 0; | |
4116 | } | |
4117 | ||
4118 | /* Free all the memory associated with MCTX. */ | |
4119 | ||
4120 | static void | |
e2f55264 | 4121 | match_ctx_free (re_match_context_t *mctx) |
cb265fec UD |
4122 | { |
4123 | /* First, free all the memory associated with MCTX->SUB_TOPS. */ | |
4124 | match_ctx_clean (mctx); | |
4125 | re_free (mctx->sub_tops); | |
4126 | re_free (mctx->bkref_ents); | |
6291ee3c UD |
4127 | } |
4128 | ||
4129 | /* Add a new backreference entry to MCTX. | |
4130 | Note that we assume that caller never call this function with duplicate | |
4131 | entry, and call with STR_IDX which isn't smaller than any existing entry. | |
4132 | */ | |
3b0bdc72 | 4133 | |
a9388965 | 4134 | static reg_errcode_t |
b41bd5bc | 4135 | __attribute_warn_unused_result__ |
eb04c213 AZ |
4136 | match_ctx_add_entry (re_match_context_t *mctx, Idx node, Idx str_idx, Idx from, |
4137 | Idx to) | |
3b0bdc72 UD |
4138 | { |
4139 | if (mctx->nbkref_ents >= mctx->abkref_ents) | |
4140 | { | |
1b2c2628 UD |
4141 | struct re_backref_cache_entry* new_entry; |
4142 | new_entry = re_realloc (mctx->bkref_ents, struct re_backref_cache_entry, | |
15a7d175 | 4143 | mctx->abkref_ents * 2); |
f4efbdfb | 4144 | if (__glibc_unlikely (new_entry == NULL)) |
15a7d175 UD |
4145 | { |
4146 | re_free (mctx->bkref_ents); | |
4147 | return REG_ESPACE; | |
4148 | } | |
1b2c2628 | 4149 | mctx->bkref_ents = new_entry; |
3b0bdc72 | 4150 | memset (mctx->bkref_ents + mctx->nbkref_ents, '\0', |
15a7d175 | 4151 | sizeof (struct re_backref_cache_entry) * mctx->abkref_ents); |
3b0bdc72 UD |
4152 | mctx->abkref_ents *= 2; |
4153 | } | |
e40a38b3 UD |
4154 | if (mctx->nbkref_ents > 0 |
4155 | && mctx->bkref_ents[mctx->nbkref_ents - 1].str_idx == str_idx) | |
4156 | mctx->bkref_ents[mctx->nbkref_ents - 1].more = 1; | |
4157 | ||
3b0bdc72 | 4158 | mctx->bkref_ents[mctx->nbkref_ents].node = node; |
0742e48e UD |
4159 | mctx->bkref_ents[mctx->nbkref_ents].str_idx = str_idx; |
4160 | mctx->bkref_ents[mctx->nbkref_ents].subexp_from = from; | |
e40a38b3 | 4161 | mctx->bkref_ents[mctx->nbkref_ents].subexp_to = to; |
7db61208 UD |
4162 | |
4163 | /* This is a cache that saves negative results of check_dst_limits_calc_pos. | |
4164 | If bit N is clear, means that this entry won't epsilon-transition to | |
4165 | an OP_OPEN_SUBEXP or OP_CLOSE_SUBEXP for the N+1-th subexpression. If | |
4166 | it is set, check_dst_limits_calc_pos_1 will recurse and try to find one | |
4167 | such node. | |
4168 | ||
4169 | A backreference does not epsilon-transition unless it is empty, so set | |
4170 | to all zeros if FROM != TO. */ | |
4171 | mctx->bkref_ents[mctx->nbkref_ents].eps_reachable_subexps_map | |
eb04c213 | 4172 | = (from == to ? -1 : 0); |
7db61208 | 4173 | |
e40a38b3 | 4174 | mctx->bkref_ents[mctx->nbkref_ents++].more = 0; |
0742e48e UD |
4175 | if (mctx->max_mb_elem_len < to - from) |
4176 | mctx->max_mb_elem_len = to - from; | |
a9388965 | 4177 | return REG_NOERROR; |
3b0bdc72 | 4178 | } |
0742e48e | 4179 | |
eb04c213 | 4180 | /* Return the first entry with the same str_idx, or -1 if none is |
e40a38b3 | 4181 | found. Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX. */ |
6291ee3c | 4182 | |
eb04c213 AZ |
4183 | static Idx |
4184 | search_cur_bkref_entry (const re_match_context_t *mctx, Idx str_idx) | |
6291ee3c | 4185 | { |
eb04c213 | 4186 | Idx left, right, mid, last; |
e40a38b3 | 4187 | last = right = mctx->nbkref_ents; |
6291ee3c UD |
4188 | for (left = 0; left < right;) |
4189 | { | |
4190 | mid = (left + right) / 2; | |
4191 | if (mctx->bkref_ents[mid].str_idx < str_idx) | |
4192 | left = mid + 1; | |
4193 | else | |
4194 | right = mid; | |
4195 | } | |
e40a38b3 UD |
4196 | if (left < last && mctx->bkref_ents[left].str_idx == str_idx) |
4197 | return left; | |
4198 | else | |
4199 | return -1; | |
6291ee3c UD |
4200 | } |
4201 | ||
6291ee3c UD |
4202 | /* Register the node NODE, whose type is OP_OPEN_SUBEXP, and which matches |
4203 | at STR_IDX. */ | |
4204 | ||
4205 | static reg_errcode_t | |
b41bd5bc | 4206 | __attribute_warn_unused_result__ |
eb04c213 | 4207 | match_ctx_add_subtop (re_match_context_t *mctx, Idx node, Idx str_idx) |
6291ee3c | 4208 | { |
2a0356e1 AZ |
4209 | DEBUG_ASSERT (mctx->sub_tops != NULL); |
4210 | DEBUG_ASSERT (mctx->asub_tops > 0); | |
f4efbdfb | 4211 | if (__glibc_unlikely (mctx->nsub_tops == mctx->asub_tops)) |
6291ee3c | 4212 | { |
eb04c213 | 4213 | Idx new_asub_tops = mctx->asub_tops * 2; |
951d6408 UD |
4214 | re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops, |
4215 | re_sub_match_top_t *, | |
4216 | new_asub_tops); | |
f4efbdfb | 4217 | if (__glibc_unlikely (new_array == NULL)) |
6291ee3c UD |
4218 | return REG_ESPACE; |
4219 | mctx->sub_tops = new_array; | |
951d6408 | 4220 | mctx->asub_tops = new_asub_tops; |
6291ee3c UD |
4221 | } |
4222 | mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t)); | |
f4efbdfb | 4223 | if (__glibc_unlikely (mctx->sub_tops[mctx->nsub_tops] == NULL)) |
6291ee3c UD |
4224 | return REG_ESPACE; |
4225 | mctx->sub_tops[mctx->nsub_tops]->node = node; | |
4226 | mctx->sub_tops[mctx->nsub_tops++]->str_idx = str_idx; | |
4227 | return REG_NOERROR; | |
4228 | } | |
4229 | ||
4230 | /* Register the node NODE, whose type is OP_CLOSE_SUBEXP, and which matches | |
0b5ca7c3 PE |
4231 | at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP. |
4232 | Return the new entry if successful, NULL if memory is exhausted. */ | |
6291ee3c UD |
4233 | |
4234 | static re_sub_match_last_t * | |
eb04c213 | 4235 | match_ctx_add_sublast (re_sub_match_top_t *subtop, Idx node, Idx str_idx) |
6291ee3c UD |
4236 | { |
4237 | re_sub_match_last_t *new_entry; | |
f4efbdfb | 4238 | if (__glibc_unlikely (subtop->nlasts == subtop->alasts)) |
6291ee3c | 4239 | { |
eb04c213 | 4240 | Idx new_alasts = 2 * subtop->alasts + 1; |
951d6408 UD |
4241 | re_sub_match_last_t **new_array = re_realloc (subtop->lasts, |
4242 | re_sub_match_last_t *, | |
4243 | new_alasts); | |
f4efbdfb | 4244 | if (__glibc_unlikely (new_array == NULL)) |
6291ee3c UD |
4245 | return NULL; |
4246 | subtop->lasts = new_array; | |
951d6408 | 4247 | subtop->alasts = new_alasts; |
6291ee3c UD |
4248 | } |
4249 | new_entry = calloc (1, sizeof (re_sub_match_last_t)); | |
f4efbdfb | 4250 | if (__glibc_likely (new_entry != NULL)) |
951d6408 UD |
4251 | { |
4252 | subtop->lasts[subtop->nlasts] = new_entry; | |
4253 | new_entry->node = node; | |
4254 | new_entry->str_idx = str_idx; | |
4255 | ++subtop->nlasts; | |
4256 | } | |
6291ee3c UD |
4257 | return new_entry; |
4258 | } | |
4259 | ||
0742e48e | 4260 | static void |
e2f55264 | 4261 | sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts, |
eb04c213 | 4262 | re_dfastate_t **limited_sts, Idx last_node, Idx last_str_idx) |
0742e48e UD |
4263 | { |
4264 | sctx->sifted_states = sifted_sts; | |
4265 | sctx->limited_states = limited_sts; | |
4266 | sctx->last_node = last_node; | |
4267 | sctx->last_str_idx = last_str_idx; | |
0742e48e UD |
4268 | re_node_set_init_empty (&sctx->limits); |
4269 | } |