5 _NL_CTYPE_CODESET_NAME,
6 _NL_CTYPE_MAP_TO_NONASCII,
10 union locale_data_value {
16 extern __thread struct locale_data *const *_nl_current_LC_COLLATE __attribute__ ((tls_model ("initial-exec")));
17 extern __thread struct locale_data *const *_nl_current_LC_CTYPE __attribute__ ((tls_model ("initial-exec")));
18 typedef unsigned long int reg_syntax_t;
19 extern reg_syntax_t re_syntax_options;
21 REG_ENOSYS = -1, REG_NOERROR = 0, REG_NOMATCH, REG_BADPAT, REG_ECOLLATE, REG_ECTYPE, REG_EESCAPE, REG_ESUBREG, REG_EBRACK, REG_EPAREN, REG_EBRACE, REG_BADBR, REG_ERANGE, REG_ESPACE, REG_BADRPT, REG_EEND, REG_ESIZE, REG_ERPAREN }
23 struct re_pattern_buffer {
24 unsigned char *buffer;
27 unsigned char * translate;
29 unsigned can_be_null : 1;
30 unsigned regs_allocated : 2;
31 unsigned fastmap_accurate : 1;
35 typedef struct re_pattern_buffer regex_t;
36 extern __thread void *__libc_tsd_CTYPE_TOLOWER __attribute__ ((tls_model ("initial-exec")));
37 extern inline const int ** __attribute__ ((const)) __ctype_tolower_loc (void) {
43 u.ptr = (&__libc_tsd_CTYPE_TOLOWER);
44 if (__builtin_expect (*u.tablep == 0, 0)) *u.tablep = ((int *) ((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_TOLOWER) & 0xffff)].string) + 128);
47 extern __inline int __attribute__ ((__nothrow__)) tolower (int __c) {
48 return __c >= -128 && __c < 256 ? (*__ctype_tolower_loc ())[__c] : __c;
53 typedef __mbstate_t mbstate_t;
54 typedef unsigned long int bitset_word_t;
55 typedef bitset_word_t bitset_t[(256 / (sizeof (bitset_word_t) * 8))];
56 typedef bitset_word_t *re_bitset_ptr_t;
70 NON_TYPE = 0, CHARACTER = 1, END_OF_RE = 2, SIMPLE_BRACKET = 3, OP_BACK_REF = 4, OP_PERIOD = 5, COMPLEX_BRACKET = 6, OP_UTF8_PERIOD = 7, OP_OPEN_SUBEXP = 8 | 0, OP_CLOSE_SUBEXP = 8 | 1, OP_ALT = 8 | 2, OP_DUP_ASTERISK = 8 | 3, ANCHOR = 8 | 4, CONCAT = 16, SUBEXP = 17, OP_DUP_PLUS = 18, OP_DUP_QUESTION, OP_OPEN_BRACKET, OP_CLOSE_BRACKET, OP_CHARSET_RANGE, OP_OPEN_DUP_NUM, OP_CLOSE_DUP_NUM, OP_NON_MATCH_LIST, OP_OPEN_COLL_ELEM, OP_CLOSE_COLL_ELEM, OP_OPEN_EQUIV_CLASS, OP_CLOSE_EQUIV_CLASS, OP_OPEN_CHAR_CLASS, OP_CLOSE_CHAR_CLASS, OP_WORD, OP_NOTWORD, OP_SPACE, OP_NOTSPACE, BACK_SLASH }
74 unsigned int non_match : 1;
85 re_bitset_ptr_t sbcset;
90 re_token_type_t type : 8;
95 typedef struct re_string_t re_string_t;
96 typedef struct re_dfa_t re_dfa_t;
99 typedef struct bin_tree_t bin_tree_t;
100 struct re_dfastate_t {
103 typedef struct re_dfastate_t re_dfastate_t;
106 unsigned int nodes_alloc;
107 unsigned int nodes_len;
111 re_node_set *eclosures;
112 struct re_state_table_entry *state_table;
113 re_dfastate_t *init_state;
114 bin_tree_t *str_tree;
115 re_bitset_ptr_t sb_char;
116 int str_tree_storage_idx;
118 unsigned int has_mb_node : 1;
119 unsigned int is_utf8 : 1;
120 unsigned int map_notascii : 1;
124 static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern, unsigned int length, reg_syntax_t syntax);
125 static void re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state, char *fastmap);
126 static reg_errcode_t init_dfa (re_dfa_t *dfa, unsigned int pat_len);
127 static void optimize_utf8 (re_dfa_t *dfa);
128 static reg_errcode_t analyze (regex_t *preg);
129 static reg_errcode_t optimize_subexps (void *extra, bin_tree_t *node);
130 const unsigned int __re_error_msgid_idx[] = {
131 0, (0 + sizeof "Success"), ((0 + sizeof "Success") + sizeof "No match"), (((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression"), ((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character"), (((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name"), ((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash"), (((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference"), ((((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference") + sizeof "Unmatched [ or [^"), (((((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference") + sizeof "Unmatched [ or [^") + sizeof "Unmatched ( or \\("), ((((((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference") + sizeof "Unmatched [ or [^") + sizeof "Unmatched ( or \\(") + sizeof "Unmatched \\{"), (((((((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference") + sizeof "Unmatched [ or [^") + sizeof "Unmatched ( or \\(") + sizeof "Unmatched \\{") + sizeof "Invalid content of \\{\\}"), ((((((((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference") + sizeof "Unmatched [ or [^") + sizeof "Unmatched ( or \\(") + sizeof "Unmatched \\{") + sizeof "Invalid content of \\{\\}") + sizeof "Invalid range end"), (((((((((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference") + sizeof "Unmatched [ or [^") + sizeof "Unmatched ( or \\(") + sizeof "Unmatched \\{") + sizeof "Invalid content of \\{\\}") + sizeof "Invalid range end") + sizeof "Memory exhausted"), ((((((((((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference") + sizeof "Unmatched [ or [^") + sizeof "Unmatched ( or \\(") + sizeof "Unmatched \\{") + sizeof "Invalid content of \\{\\}") + sizeof "Invalid range end") + sizeof "Memory exhausted") + sizeof "Invalid preceding regular expression"), (((((((((((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference") + sizeof "Unmatched [ or [^") + sizeof "Unmatched ( or \\(") + sizeof "Unmatched \\{") + sizeof "Invalid content of \\{\\}") + sizeof "Invalid range end") + sizeof "Memory exhausted") + sizeof "Invalid preceding regular expression") + sizeof "Premature end of regular expression"), ((((((((((((((((0 + sizeof "Success") + sizeof "No match") + sizeof "Invalid regular expression") + sizeof "Invalid collation character") + sizeof "Invalid character class name") + sizeof "Trailing backslash") + sizeof "Invalid back reference") + sizeof "Unmatched [ or [^") + sizeof "Unmatched ( or \\(") + sizeof "Unmatched \\{") + sizeof "Invalid content of \\{\\}") + sizeof "Invalid range end") + sizeof "Memory exhausted") + sizeof "Invalid preceding regular expression") + sizeof "Premature end of regular expression") + sizeof "Regular expression too big") };
132 const char * __re_compile_pattern (pattern, length, bufp) const char *pattern;
135 ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
137 int __re_compile_fastmap (bufp) struct re_pattern_buffer *bufp;
139 re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
140 char *fastmap = bufp->fastmap;
141 re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
143 static inline void __attribute__ ((always_inline)) re_set_fastmap (char *fastmap, int icase, int ch) {
144 if (icase) fastmap[(__extension__ ({
146 if (sizeof (ch) > 1) {
147 if (__builtin_constant_p (ch)) {
149 else __res = tolower (ch);
155 static void re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state, char *fastmap) {
156 re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
158 int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & ((((((((((((((((((((((((unsigned long int) 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1)));
160 node_cnt < init_state->nodes.nelem;
162 int node = init_state->nodes.elems[node_cnt];
163 re_token_type_t type = dfa->nodes[node].type;
164 if (type == CHARACTER) {
165 if ((bufp->syntax & ((((((((((((((((((((((((unsigned long int) 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1)) && dfa->mb_cur_max > 1) {
166 unsigned char *buf = __builtin_alloca (dfa->mb_cur_max), *p;
170 i < (256 / (sizeof (bitset_word_t) * 8));
173 bitset_word_t w = dfa->nodes[node].opr.sbcset[i];
175 j < (sizeof (bitset_word_t) * 8);
176 ++j, ++ch) if (w & ((bitset_word_t) 1 << j)) re_set_fastmap (fastmap, icase, ch);
179 else if (type == COMPLEX_BRACKET) {
181 re_charset_t *cset = dfa->nodes[node].opr.mbcset;
182 if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes || cset->nranges || cset->nchar_classes) {
183 if (((unsigned int) (*_nl_current_LC_COLLATE)->values[((int) (_NL_COLLATE_NRULES) & 0xffff)].word) != 0) {
184 const int *table = (const int *) ((*_nl_current_LC_COLLATE)->values[((int) (_NL_COLLATE_TABLEMB) & 0xffff)].string);
187 ++i) if (table[i] < 0) re_set_fastmap (fastmap, icase, i);
195 if (__wcrtomb (buf, cset->mbchars[i], &state) != (unsigned int) -1) re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
196 if ((bufp->syntax & ((((((((((((((((((((((((unsigned long int) 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1)) && dfa->mb_cur_max > 1) {
197 if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state) != (unsigned int) -1) re_set_fastmap (fastmap, 0, *(unsigned char *) buf);
201 else if (type == OP_PERIOD || type == OP_UTF8_PERIOD || type == END_OF_RE) {
202 memset (fastmap, '\1', sizeof (char) * 256);
203 if (type == END_OF_RE) bufp->can_be_null = 1;
208 static const bitset_t utf8_sb_map;
209 static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern, unsigned int length, reg_syntax_t syntax) {
210 reg_errcode_t err = REG_NOERROR;
213 preg->fastmap_accurate = 0;
214 preg->not_bol = preg->not_eol = 0;
215 preg->can_be_null = 0;
216 preg->regs_allocated = 0;
217 err = init_dfa (dfa, length);
218 err = re_string_construct (®exp, pattern, length, preg->translate, syntax & ((((((((((((((((((((((((unsigned long int) 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1), dfa);
219 dfa->str_tree = parse (®exp, preg, syntax, &err);
220 err = analyze (preg);
221 if (dfa->is_utf8 && !(syntax & ((((((((((((((((((((((((unsigned long int) 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1) << 1)) && preg->translate == 0) optimize_utf8 (dfa);
223 static reg_errcode_t init_dfa (re_dfa_t *dfa, unsigned int pat_len) {
224 memset (dfa, '\0', sizeof (re_dfa_t));
225 dfa->str_tree_storage_idx = ((1024 - sizeof (void *)) / sizeof (bin_tree_t));
226 if (pat_len == (4294967295U)) return REG_ESPACE;
227 if (dfa->mb_cur_max == 6 && __extension__ ({
228 unsigned int __s1_len, __s2_len;
229 (__builtin_constant_p (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)) && __builtin_constant_p ("UTF-8") && (__s1_len = strlen (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)), __s2_len = strlen ("UTF-8"), (!((unsigned int)(const void *)((((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)) + 1) - (unsigned int)(const void *)(((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)) == 1) || __s1_len >= 4) && (!((unsigned int)(const void *)(("UTF-8") + 1) - (unsigned int)(const void *)("UTF-8") == 1) || __s2_len >= 4)) ? __builtin_strcmp (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string), "UTF-8") : (__builtin_constant_p (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)) && ((unsigned int)(const void *)((((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)) + 1) - (unsigned int)(const void *)(((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)) == 1) && (__s1_len = strlen (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)), __s1_len < 4) ? (__builtin_constant_p ("UTF-8") && ((unsigned int)(const void *)(("UTF-8") + 1) - (unsigned int)(const void *)("UTF-8") == 1) ? __builtin_strcmp (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string), "UTF-8") : (__extension__ ({
230 __const unsigned char *__s2 = (__const unsigned char *) (__const char *) ("UTF-8");
231 register int __result = (((__const unsigned char *) (__const char *) (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)))[0] - __s2[0]);
234 ))) : (__builtin_constant_p ("UTF-8") && ((unsigned int)(const void *)(("UTF-8") + 1) - (unsigned int)(const void *)("UTF-8") == 1) && (__s2_len = strlen ("UTF-8"), __s2_len < 4) ? (__builtin_constant_p (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)) && ((unsigned int)(const void *)((((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)) + 1) - (unsigned int)(const void *)(((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)) == 1) ? __builtin_strcmp (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string), "UTF-8") : (__extension__ ({
235 __const unsigned char *__s1 = (__const unsigned char *) (__const char *) (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string));
236 register int __result = __s1[0] - ((__const unsigned char *) (__const char *) ("UTF-8"))[0];
237 if (__s2_len > 0 && __result == 0) {
238 if (__s2_len > 1 && __result == 0) {
243 ))) : __builtin_strcmp (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string), "UTF-8"))));
245 ) == 0) dfa->is_utf8 = 1;
246 dfa->map_notascii = (((unsigned int) (*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_MAP_TO_NONASCII) & 0xffff)].word) != 0);
247 if (dfa->mb_cur_max > 1) {
248 if (dfa->is_utf8) dfa->sb_char = (re_bitset_ptr_t) utf8_sb_map;
251 dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
252 if (__builtin_expect (dfa->sb_char == 0, 0)) return REG_ESPACE;
254 i < (256 / (sizeof (bitset_word_t) * 8));
256 j < (sizeof (bitset_word_t) * 8);
258 unsigned int wch = __btowc (ch);
259 if (wch != (0xffffffffu)) dfa->sb_char[i] |= (bitset_word_t) 1 << j;
263 if (__builtin_expect (dfa->nodes == 0 || dfa->state_table == 0, 0)) return REG_ESPACE;
266 static void optimize_utf8 (re_dfa_t *dfa) {
267 int node, i, mb_chars = 0, has_period = 0;
269 node < dfa->nodes_len;
271 switch (dfa->nodes[node].type) {
273 if (dfa->nodes[node].opr.c >= 0x80) mb_chars = 1;
275 switch (dfa->nodes[node].opr.idx) {
276 case LINE_FIRST: case LINE_LAST: case BUF_FIRST: case BUF_LAST: break;
279 case OP_PERIOD: has_period = 1;
280 case SIMPLE_BRACKET: ((0x80 % (sizeof (bitset_word_t) * 8) == 0) ? (void) (0) : (__assert_fail ("0x80 % (sizeof (bitset_word_t) * 8) == 0", "regcomp.c", 1059, __PRETTY_FUNCTION__), (void) (0)));
281 for (i = 0x80 / (sizeof (bitset_word_t) * 8);
282 i < (256 / (sizeof (bitset_word_t) * 8));
283 ++i) if (dfa->nodes[node].opr.sbcset[i]) return;
285 dfa->has_mb_node = dfa->nbackref > 0 || has_period;
287 static reg_errcode_t analyze (regex_t *preg) {
288 re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
289 dfa->nexts = ((int *) malloc ((dfa->nodes_alloc) * sizeof (int)));
290 dfa->org_indices = ((int *) malloc ((dfa->nodes_alloc) * sizeof (int)));
291 dfa->edests = ((re_node_set *) malloc ((dfa->nodes_alloc) * sizeof (re_node_set)));
292 dfa->eclosures = ((re_node_set *) malloc ((dfa->nodes_alloc) * sizeof (re_node_set)));
293 if (__builtin_expect (dfa->nexts == 0 || dfa->org_indices == 0 || dfa->edests == 0 || dfa->eclosures == 0, 0)) return REG_ESPACE;
294 if (dfa->subexp_map != 0) {
298 i++) dfa->subexp_map[i] = i;
299 preorder (dfa->str_tree, optimize_subexps, dfa);
302 i++) if (dfa->subexp_map[i] != i) break;