enum { _NL_COLLATE_NRULES = ((( 3) << 16) | (0)), _NL_COLLATE_TABLEMB, _NL_CTYPE_TOLOWER, _NL_CTYPE_CODESET_NAME, _NL_CTYPE_MAP_TO_NONASCII, }; struct locale_data { unsigned int nstrings; union locale_data_value { const char *string; unsigned int word; } values []; }; extern __thread struct locale_data *const *_nl_current_LC_COLLATE __attribute__ ((tls_model ("initial-exec"))); extern __thread struct locale_data *const *_nl_current_LC_CTYPE __attribute__ ((tls_model ("initial-exec"))); typedef unsigned long int reg_syntax_t; extern reg_syntax_t re_syntax_options; typedef enum { 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 } reg_errcode_t; struct re_pattern_buffer { unsigned char *buffer; reg_syntax_t syntax; char *fastmap; unsigned char * translate; unsigned int re_nsub; unsigned can_be_null : 1; unsigned regs_allocated : 2; unsigned fastmap_accurate : 1; unsigned not_bol : 1; unsigned not_eol : 1; }; typedef struct re_pattern_buffer regex_t; extern __thread void *__libc_tsd_CTYPE_TOLOWER __attribute__ ((tls_model ("initial-exec"))); extern inline const int ** __attribute__ ((const)) __ctype_tolower_loc (void) { union { void **ptr; const int **tablep; } u; u.ptr = (&__libc_tsd_CTYPE_TOLOWER); if (__builtin_expect (*u.tablep == ((void *)0), 0)) *u.tablep = ((int *) ((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_TOLOWER) & 0xffff)].string) + 128); return u.tablep; } extern __inline int __attribute__ ((__nothrow__)) tolower (int __c) { return __c >= -128 && __c < 256 ? (*__ctype_tolower_loc ())[__c] : __c; } typedef struct { } __mbstate_t; typedef __mbstate_t mbstate_t; typedef unsigned long int bitset_word_t; typedef bitset_word_t bitset_t[(256 / (sizeof (bitset_word_t) * 8))]; typedef bitset_word_t *re_bitset_ptr_t; typedef enum { INSIDE_WORD = 0x0001 | 0x0004, WORD_FIRST = 0x0002 | 0x0004, WORD_LAST = 0x0001 | 0x0008, INSIDE_NOTWORD = 0x0002 | 0x0008, LINE_FIRST = 0x0010, LINE_LAST = 0x0020, BUF_FIRST = 0x0040, BUF_LAST = 0x0080, WORD_DELIM = 0x0100, NOT_WORD_DELIM = 0x0200 } re_context_type; typedef struct { int nelem; int *elems; } re_node_set; typedef enum { 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 } re_token_type_t; typedef struct { long int *mbchars; unsigned int non_match : 1; int nmbchars; int ncoll_syms; int nequiv_classes; int nranges; int nchar_classes; } re_charset_t; typedef struct { union { unsigned char c; re_bitset_ptr_t sbcset; re_charset_t *mbcset; int idx; } opr; re_token_type_t type : 8; } re_token_t; struct re_string_t { }; typedef struct re_string_t re_string_t; typedef struct re_dfa_t re_dfa_t; struct bin_tree_t { }; typedef struct bin_tree_t bin_tree_t; struct re_dfastate_t { re_node_set nodes; }; typedef struct re_dfastate_t re_dfastate_t; struct re_dfa_t { re_token_t *nodes; unsigned int nodes_alloc; unsigned int nodes_len; int *nexts; int *org_indices; re_node_set *edests; re_node_set *eclosures; struct re_state_table_entry *state_table; re_dfastate_t *init_state; bin_tree_t *str_tree; re_bitset_ptr_t sb_char; int str_tree_storage_idx; int nbackref; unsigned int has_mb_node : 1; unsigned int is_utf8 : 1; unsigned int map_notascii : 1; int mb_cur_max; int *subexp_map; }; static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern, unsigned int length, reg_syntax_t syntax); static void re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state, char *fastmap); static reg_errcode_t init_dfa (re_dfa_t *dfa, unsigned int pat_len); static void optimize_utf8 (re_dfa_t *dfa); static reg_errcode_t analyze (regex_t *preg); static reg_errcode_t optimize_subexps (void *extra, bin_tree_t *node); const unsigned int __re_error_msgid_idx[] = { 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") }; const char * __re_compile_pattern (pattern, length, bufp) const char *pattern; { reg_errcode_t ret; ret = re_compile_internal (bufp, pattern, length, re_syntax_options); } int __re_compile_fastmap (bufp) struct re_pattern_buffer *bufp; { re_dfa_t *dfa = (re_dfa_t *) bufp->buffer; char *fastmap = bufp->fastmap; re_compile_fastmap_iter (bufp, dfa->init_state, fastmap); } static inline void __attribute__ ((always_inline)) re_set_fastmap (char *fastmap, int icase, int ch) { if (icase) fastmap[(__extension__ ({ int __res; if (sizeof (ch) > 1) { if (__builtin_constant_p (ch)) { } else __res = tolower (ch); } __res; } ))] = 1; } static void re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state, char *fastmap) { re_dfa_t *dfa = (re_dfa_t *) bufp->buffer; int node_cnt; 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))); for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt) { int node = init_state->nodes.elems[node_cnt]; re_token_type_t type = dfa->nodes[node].type; if (type == CHARACTER) { 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) { unsigned char *buf = __builtin_alloca (dfa->mb_cur_max), *p; } int i, ch; for (i = 0, ch = 0; i < (256 / (sizeof (bitset_word_t) * 8)); ++i) { int j; bitset_word_t w = dfa->nodes[node].opr.sbcset[i]; for (j = 0; j < (sizeof (bitset_word_t) * 8); ++j, ++ch) if (w & ((bitset_word_t) 1 << j)) re_set_fastmap (fastmap, icase, ch); } } else if (type == COMPLEX_BRACKET) { int i; re_charset_t *cset = dfa->nodes[node].opr.mbcset; if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes || cset->nranges || cset->nchar_classes) { if (((unsigned int) (*_nl_current_LC_COLLATE)->values[((int) (_NL_COLLATE_NRULES) & 0xffff)].word) != 0) { const int *table = (const int *) ((*_nl_current_LC_COLLATE)->values[((int) (_NL_COLLATE_TABLEMB) & 0xffff)].string); for (i = 0; i < 256; ++i) if (table[i] < 0) re_set_fastmap (fastmap, icase, i); } } for (i = 0; i < cset->nmbchars; ++i) { char buf[256]; mbstate_t state; if (__wcrtomb (buf, cset->mbchars[i], &state) != (unsigned int) -1) re_set_fastmap (fastmap, icase, *(unsigned char *) buf); 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) { if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state) != (unsigned int) -1) re_set_fastmap (fastmap, 0, *(unsigned char *) buf); } } } else if (type == OP_PERIOD || type == OP_UTF8_PERIOD || type == END_OF_RE) { memset (fastmap, '\1', sizeof (char) * 256); if (type == END_OF_RE) bufp->can_be_null = 1; return; } } } static const bitset_t utf8_sb_map = { [0 ... 0x80 / (sizeof (bitset_word_t) * 8) - 1] = (2147483647L * 2UL + 1UL) }; static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern, unsigned int length, reg_syntax_t syntax) { reg_errcode_t err = REG_NOERROR; re_dfa_t *dfa; re_string_t regexp; preg->fastmap_accurate = 0; preg->not_bol = preg->not_eol = 0; preg->can_be_null = 0; preg->regs_allocated = 0; err = init_dfa (dfa, length); 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); dfa->str_tree = parse (®exp, preg, syntax, &err); err = analyze (preg); 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 == ((void *)0)) optimize_utf8 (dfa); } static reg_errcode_t init_dfa (re_dfa_t *dfa, unsigned int pat_len) { memset (dfa, '\0', sizeof (re_dfa_t)); dfa->str_tree_storage_idx = ((1024 - sizeof (void *)) / sizeof (bin_tree_t)); if (pat_len == (4294967295U)) return REG_ESPACE; if (dfa->mb_cur_max == 6 && __extension__ ({ unsigned int __s1_len, __s2_len; (__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__ ({ __const unsigned char *__s2 = (__const unsigned char *) (__const char *) ("UTF-8"); register int __result = (((__const unsigned char *) (__const char *) (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)))[0] - __s2[0]); __result; } ))) : (__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__ ({ __const unsigned char *__s1 = (__const unsigned char *) (__const char *) (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string)); register int __result = __s1[0] - ((__const unsigned char *) (__const char *) ("UTF-8"))[0]; if (__s2_len > 0 && __result == 0) { if (__s2_len > 1 && __result == 0) { } } __result; } ))) : __builtin_strcmp (((*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_CODESET_NAME) & 0xffff)].string), "UTF-8")))); } ) == 0) dfa->is_utf8 = 1; dfa->map_notascii = (((unsigned int) (*_nl_current_LC_CTYPE)->values[((int) (_NL_CTYPE_MAP_TO_NONASCII) & 0xffff)].word) != 0); if (dfa->mb_cur_max > 1) { if (dfa->is_utf8) dfa->sb_char = (re_bitset_ptr_t) utf8_sb_map; else { int i, j, ch; dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1); if (__builtin_expect (dfa->sb_char == ((void *)0), 0)) return REG_ESPACE; for (i = 0, ch = 0; i < (256 / (sizeof (bitset_word_t) * 8)); ++i) for (j = 0; j < (sizeof (bitset_word_t) * 8); ++j, ++ch) { unsigned int wch = __btowc (ch); if (wch != (0xffffffffu)) dfa->sb_char[i] |= (bitset_word_t) 1 << j; } } } if (__builtin_expect (dfa->nodes == ((void *)0) || dfa->state_table == ((void *)0), 0)) return REG_ESPACE; return REG_NOERROR; } static void optimize_utf8 (re_dfa_t *dfa) { int node, i, mb_chars = 0, has_period = 0; for (node = 0; node < dfa->nodes_len; ++node) switch (dfa->nodes[node].type) { case CHARACTER: if (dfa->nodes[node].opr.c >= 0x80) mb_chars = 1; case ANCHOR: switch (dfa->nodes[node].opr.idx) { case LINE_FIRST: case LINE_LAST: case BUF_FIRST: case BUF_LAST: break; default: return; } case OP_PERIOD: has_period = 1; 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))); for (i = 0x80 / (sizeof (bitset_word_t) * 8); i < (256 / (sizeof (bitset_word_t) * 8)); ++i) if (dfa->nodes[node].opr.sbcset[i]) return; } dfa->has_mb_node = dfa->nbackref > 0 || has_period; } static reg_errcode_t analyze (regex_t *preg) { re_dfa_t *dfa = (re_dfa_t *) preg->buffer; dfa->nexts = ((int *) malloc ((dfa->nodes_alloc) * sizeof (int))); dfa->org_indices = ((int *) malloc ((dfa->nodes_alloc) * sizeof (int))); dfa->edests = ((re_node_set *) malloc ((dfa->nodes_alloc) * sizeof (re_node_set))); dfa->eclosures = ((re_node_set *) malloc ((dfa->nodes_alloc) * sizeof (re_node_set))); if (__builtin_expect (dfa->nexts == ((void *)0) || dfa->org_indices == ((void *)0) || dfa->edests == ((void *)0) || dfa->eclosures == ((void *)0), 0)) return REG_ESPACE; if (dfa->subexp_map != ((void *)0)) { int i; for (i = 0; i < preg->re_nsub; i++) dfa->subexp_map[i] = i; preorder (dfa->str_tree, optimize_subexps, dfa); for (i = 0; i < preg->re_nsub; i++) if (dfa->subexp_map[i] != i) break; } }