regex_internal.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifndef _REGEX_INTERNAL_H
  18. #define _REGEX_INTERNAL_H 1
  19. #include <assert.h>
  20. #include <ctype.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET
  25. # include <langinfo.h>
  26. #endif
  27. #if defined HAVE_LOCALE_H
  28. # include <locale.h>
  29. #endif
  30. #if defined HAVE_WCHAR_H
  31. # include <wchar.h>
  32. #endif
  33. #if defined HAVE_WCTYPE_H
  34. # include <wctype.h>
  35. #endif
  36. #if defined HAVE_STDBOOL_H
  37. # include <stdbool.h>
  38. #endif
  39. #if defined HAVE_STDINT_H
  40. # include <stdint.h>
  41. #endif
  42. #ifdef __UCLIBC_HAS_THREADS__
  43. #include <bits/libc-lock.h>
  44. #else
  45. #define __libc_lock_define(CLASS, NAME)
  46. #define __libc_lock_init(NAME) do { } while (0)
  47. #define __libc_lock_lock(NAME) do { } while (0)
  48. #define __libc_lock_unlock(NAME) do { } while (0)
  49. #endif
  50. #undef gettext
  51. #undef gettext_noop
  52. #define gettext(msgid) (msgid)
  53. #define gettext_noop(String) String
  54. #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL)
  55. # define RE_ENABLE_I18N
  56. #endif
  57. #if __GNUC__ >= 3
  58. /* uclibc: lean towards smaller size a bit:
  59. * OFF: # define BE(expr, val) __builtin_expect (expr, val) */
  60. # define BE(expr, val) (expr)
  61. #else
  62. # define BE(expr, val) (expr)
  63. # define inline
  64. #endif
  65. /* Number of single byte character. */
  66. #define SBC_MAX 256
  67. #define COLL_ELEM_LEN_MAX 8
  68. /* The character which represents newline. */
  69. #define NEWLINE_CHAR '\n'
  70. #define WIDE_NEWLINE_CHAR L'\n'
  71. #ifdef __GNUC__
  72. # define __attribute(arg) __attribute__ (arg)
  73. #else
  74. # define __attribute(arg)
  75. #endif
  76. /* An integer used to represent a set of bits. It must be unsigned,
  77. and must be at least as wide as unsigned int. */
  78. typedef unsigned long int bitset_word_t;
  79. /* All bits set in a bitset_word_t. */
  80. #define BITSET_WORD_MAX ULONG_MAX
  81. /* Number of bits in a bitset_word_t. */
  82. #define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
  83. /* Number of bitset_word_t in a bit_set. */
  84. #define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
  85. typedef bitset_word_t bitset_t[BITSET_WORDS];
  86. typedef bitset_word_t *re_bitset_ptr_t;
  87. typedef const bitset_word_t *re_const_bitset_ptr_t;
  88. #define bitset_set(set,i) \
  89. (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
  90. #define bitset_clear(set,i) \
  91. (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
  92. #define bitset_contain(set,i) \
  93. (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
  94. #define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
  95. #define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
  96. #define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
  97. #define PREV_WORD_CONSTRAINT 0x0001
  98. #define PREV_NOTWORD_CONSTRAINT 0x0002
  99. #define NEXT_WORD_CONSTRAINT 0x0004
  100. #define NEXT_NOTWORD_CONSTRAINT 0x0008
  101. #define PREV_NEWLINE_CONSTRAINT 0x0010
  102. #define NEXT_NEWLINE_CONSTRAINT 0x0020
  103. #define PREV_BEGBUF_CONSTRAINT 0x0040
  104. #define NEXT_ENDBUF_CONSTRAINT 0x0080
  105. #define WORD_DELIM_CONSTRAINT 0x0100
  106. #define NOT_WORD_DELIM_CONSTRAINT 0x0200
  107. typedef enum
  108. {
  109. INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  110. WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  111. WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  112. INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  113. LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
  114. LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
  115. BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
  116. BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
  117. WORD_DELIM = WORD_DELIM_CONSTRAINT,
  118. NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
  119. } re_context_type;
  120. typedef struct
  121. {
  122. int alloc;
  123. int nelem;
  124. int *elems;
  125. } re_node_set;
  126. typedef enum
  127. {
  128. NON_TYPE = 0,
  129. /* Node type, These are used by token, node, tree. */
  130. CHARACTER = 1,
  131. END_OF_RE = 2,
  132. SIMPLE_BRACKET = 3,
  133. OP_BACK_REF = 4,
  134. OP_PERIOD = 5,
  135. #ifdef RE_ENABLE_I18N
  136. COMPLEX_BRACKET = 6,
  137. OP_UTF8_PERIOD = 7,
  138. #endif /* RE_ENABLE_I18N */
  139. /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
  140. when the debugger shows values of this enum type. */
  141. #define EPSILON_BIT 8
  142. OP_OPEN_SUBEXP = EPSILON_BIT | 0,
  143. OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
  144. OP_ALT = EPSILON_BIT | 2,
  145. OP_DUP_ASTERISK = EPSILON_BIT | 3,
  146. ANCHOR = EPSILON_BIT | 4,
  147. /* Tree type, these are used only by tree. */
  148. CONCAT = 16,
  149. SUBEXP = 17,
  150. /* Token type, these are used only by token. */
  151. OP_DUP_PLUS = 18,
  152. OP_DUP_QUESTION,
  153. OP_OPEN_BRACKET,
  154. OP_CLOSE_BRACKET,
  155. OP_CHARSET_RANGE,
  156. OP_OPEN_DUP_NUM,
  157. OP_CLOSE_DUP_NUM,
  158. OP_NON_MATCH_LIST,
  159. OP_OPEN_COLL_ELEM,
  160. OP_CLOSE_COLL_ELEM,
  161. OP_OPEN_EQUIV_CLASS,
  162. OP_CLOSE_EQUIV_CLASS,
  163. OP_OPEN_CHAR_CLASS,
  164. OP_CLOSE_CHAR_CLASS,
  165. OP_WORD,
  166. OP_NOTWORD,
  167. OP_SPACE,
  168. OP_NOTSPACE,
  169. BACK_SLASH
  170. } re_token_type_t;
  171. #ifdef RE_ENABLE_I18N
  172. typedef struct
  173. {
  174. /* Multibyte characters. */
  175. wchar_t *mbchars;
  176. /* Collating symbols. */
  177. # if 0
  178. int32_t *coll_syms;
  179. # endif
  180. /* Equivalence classes. */
  181. # if 0
  182. int32_t *equiv_classes;
  183. # endif
  184. /* Range expressions. */
  185. # if 0
  186. uint32_t *range_starts;
  187. uint32_t *range_ends;
  188. # else
  189. wchar_t *range_starts;
  190. wchar_t *range_ends;
  191. # endif
  192. /* Character classes. */
  193. wctype_t *char_classes;
  194. /* If this character set is the non-matching list. */
  195. unsigned int non_match : 1;
  196. /* # of multibyte characters. */
  197. int nmbchars;
  198. /* # of collating symbols. */
  199. int ncoll_syms;
  200. /* # of equivalence classes. */
  201. int nequiv_classes;
  202. /* # of range expressions. */
  203. int nranges;
  204. /* # of character classes. */
  205. int nchar_classes;
  206. } re_charset_t;
  207. #endif /* RE_ENABLE_I18N */
  208. typedef struct
  209. {
  210. union
  211. {
  212. unsigned char c; /* for CHARACTER */
  213. re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
  214. #ifdef RE_ENABLE_I18N
  215. re_charset_t *mbcset; /* for COMPLEX_BRACKET */
  216. #endif /* RE_ENABLE_I18N */
  217. int idx; /* for BACK_REF */
  218. re_context_type ctx_type; /* for ANCHOR */
  219. } opr;
  220. #if __GNUC__ >= 2
  221. re_token_type_t type : 8;
  222. #else
  223. re_token_type_t type;
  224. #endif
  225. unsigned int constraint : 10; /* context constraint */
  226. unsigned int duplicated : 1;
  227. unsigned int opt_subexp : 1;
  228. #ifdef RE_ENABLE_I18N
  229. unsigned int accept_mb : 1;
  230. /* These 2 bits can be moved into the union if needed (e.g. if running out
  231. of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
  232. unsigned int mb_partial : 1;
  233. #endif
  234. unsigned int word_char : 1;
  235. } re_token_t;
  236. #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
  237. struct re_string_t
  238. {
  239. /* Indicate the raw buffer which is the original string passed as an
  240. argument of regexec(), re_search(), etc.. */
  241. const unsigned char *raw_mbs;
  242. /* Store the multibyte string. In case of "case insensitive mode" like
  243. REG_ICASE, upper cases of the string are stored, otherwise MBS points
  244. the same address that RAW_MBS points. */
  245. unsigned char *mbs;
  246. #ifdef RE_ENABLE_I18N
  247. /* Store the wide character string which is corresponding to MBS. */
  248. wint_t *wcs;
  249. int *offsets;
  250. mbstate_t cur_state;
  251. #endif
  252. /* Index in RAW_MBS. Each character mbs[i] corresponds to
  253. raw_mbs[raw_mbs_idx + i]. */
  254. int raw_mbs_idx;
  255. /* The length of the valid characters in the buffers. */
  256. int valid_len;
  257. /* The corresponding number of bytes in raw_mbs array. */
  258. int valid_raw_len;
  259. /* The length of the buffers MBS and WCS. */
  260. int bufs_len;
  261. /* The index in MBS, which is updated by re_string_fetch_byte. */
  262. int cur_idx;
  263. /* length of RAW_MBS array. */
  264. int raw_len;
  265. /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
  266. int len;
  267. /* End of the buffer may be shorter than its length in the cases such
  268. as re_match_2, re_search_2. Then, we use STOP for end of the buffer
  269. instead of LEN. */
  270. int raw_stop;
  271. /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
  272. int stop;
  273. /* The context of mbs[0]. We store the context independently, since
  274. the context of mbs[0] may be different from raw_mbs[0], which is
  275. the beginning of the input string. */
  276. unsigned int tip_context;
  277. /* The translation passed as a part of an argument of re_compile_pattern. */
  278. RE_TRANSLATE_TYPE trans;
  279. /* Copy of re_dfa_t's word_char. */
  280. re_const_bitset_ptr_t word_char;
  281. /* 1 if REG_ICASE. */
  282. unsigned char icase;
  283. unsigned char is_utf8;
  284. unsigned char map_notascii;
  285. unsigned char mbs_allocated;
  286. unsigned char offsets_needed;
  287. unsigned char newline_anchor;
  288. unsigned char word_ops_used;
  289. int mb_cur_max;
  290. };
  291. typedef struct re_string_t re_string_t;
  292. struct re_dfa_t;
  293. typedef struct re_dfa_t re_dfa_t;
  294. static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
  295. int new_buf_len)
  296. internal_function;
  297. #ifdef RE_ENABLE_I18N
  298. static void build_wcs_buffer (re_string_t *pstr) internal_function;
  299. static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr) internal_function;
  300. #endif /* RE_ENABLE_I18N */
  301. static void build_upper_buffer (re_string_t *pstr) internal_function;
  302. static void re_string_translate_buffer (re_string_t *pstr) internal_function;
  303. static unsigned int re_string_context_at (const re_string_t *input, int idx,
  304. int eflags)
  305. internal_function __attribute ((pure));
  306. #define re_string_peek_byte(pstr, offset) \
  307. ((pstr)->mbs[(pstr)->cur_idx + offset])
  308. #define re_string_fetch_byte(pstr) \
  309. ((pstr)->mbs[(pstr)->cur_idx++])
  310. #define re_string_first_byte(pstr, idx) \
  311. ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
  312. #define re_string_is_single_byte_char(pstr, idx) \
  313. ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
  314. || (pstr)->wcs[(idx) + 1] != WEOF))
  315. #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
  316. #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
  317. #define re_string_get_buffer(pstr) ((pstr)->mbs)
  318. #define re_string_length(pstr) ((pstr)->len)
  319. #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
  320. #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
  321. #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
  322. #include <alloca.h>
  323. #if 1
  324. # ifdef HAVE_ALLOCA
  325. /* The OS usually guarantees only one guard page at the bottom of the stack,
  326. and a page size can be as small as 4096 bytes. So we cannot safely
  327. allocate anything larger than 4096 bytes. Also care for the possibility
  328. of a few compiler-allocated temporary stack slots. */
  329. # define __libc_use_alloca(n) ((n) < 4032)
  330. # else
  331. /* alloca is implemented with malloc, so just use malloc. */
  332. # define __libc_use_alloca(n) 0
  333. # endif
  334. #endif
  335. #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
  336. #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
  337. #define re_free(p) free (p)
  338. struct bin_tree_t
  339. {
  340. struct bin_tree_t *parent;
  341. struct bin_tree_t *left;
  342. struct bin_tree_t *right;
  343. struct bin_tree_t *first;
  344. struct bin_tree_t *next;
  345. re_token_t token;
  346. /* `node_idx' is the index in dfa->nodes, if `type' == 0.
  347. Otherwise `type' indicate the type of this node. */
  348. int node_idx;
  349. };
  350. typedef struct bin_tree_t bin_tree_t;
  351. #define BIN_TREE_STORAGE_SIZE \
  352. ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
  353. struct bin_tree_storage_t
  354. {
  355. struct bin_tree_storage_t *next;
  356. bin_tree_t data[BIN_TREE_STORAGE_SIZE];
  357. };
  358. typedef struct bin_tree_storage_t bin_tree_storage_t;
  359. #define CONTEXT_WORD 1
  360. #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
  361. #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
  362. #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
  363. #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
  364. #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
  365. #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
  366. #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
  367. #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
  368. #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
  369. #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
  370. #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
  371. #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
  372. #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
  373. ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  374. || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  375. || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
  376. || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
  377. #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
  378. ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  379. || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  380. || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
  381. || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
  382. struct re_dfastate_t
  383. {
  384. unsigned int hash;
  385. re_node_set nodes;
  386. re_node_set non_eps_nodes;
  387. re_node_set inveclosure;
  388. re_node_set *entrance_nodes;
  389. struct re_dfastate_t **trtable, **word_trtable;
  390. unsigned int context : 4;
  391. unsigned int halt : 1;
  392. /* If this state can accept `multi byte'.
  393. Note that we refer to multibyte characters, and multi character
  394. collating elements as `multi byte'. */
  395. unsigned int accept_mb : 1;
  396. /* If this state has backreference node(s). */
  397. unsigned int has_backref : 1;
  398. unsigned int has_constraint : 1;
  399. };
  400. typedef struct re_dfastate_t re_dfastate_t;
  401. struct re_state_table_entry
  402. {
  403. int num;
  404. int alloc;
  405. re_dfastate_t **array;
  406. };
  407. /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
  408. typedef struct
  409. {
  410. int next_idx;
  411. int alloc;
  412. re_dfastate_t **array;
  413. } state_array_t;
  414. /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
  415. typedef struct
  416. {
  417. int node;
  418. int str_idx; /* The position NODE match at. */
  419. state_array_t path;
  420. } re_sub_match_last_t;
  421. /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
  422. And information about the node, whose type is OP_CLOSE_SUBEXP,
  423. corresponding to NODE is stored in LASTS. */
  424. typedef struct
  425. {
  426. int str_idx;
  427. int node;
  428. state_array_t *path;
  429. int alasts; /* Allocation size of LASTS. */
  430. int nlasts; /* The number of LASTS. */
  431. re_sub_match_last_t **lasts;
  432. } re_sub_match_top_t;
  433. struct re_backref_cache_entry
  434. {
  435. int node;
  436. int str_idx;
  437. int subexp_from;
  438. int subexp_to;
  439. char more;
  440. char unused;
  441. unsigned short int eps_reachable_subexps_map;
  442. };
  443. typedef struct
  444. {
  445. /* The string object corresponding to the input string. */
  446. re_string_t input;
  447. const re_dfa_t *dfa;
  448. /* EFLAGS of the argument of regexec. */
  449. int eflags;
  450. /* Where the matching ends. */
  451. int match_last;
  452. int last_node;
  453. /* The state log used by the matcher. */
  454. re_dfastate_t **state_log;
  455. int state_log_top;
  456. /* Back reference cache. */
  457. int nbkref_ents;
  458. int abkref_ents;
  459. struct re_backref_cache_entry *bkref_ents;
  460. int max_mb_elem_len;
  461. int nsub_tops;
  462. int asub_tops;
  463. re_sub_match_top_t **sub_tops;
  464. } re_match_context_t;
  465. typedef struct
  466. {
  467. re_dfastate_t **sifted_states;
  468. re_dfastate_t **limited_states;
  469. int last_node;
  470. int last_str_idx;
  471. re_node_set limits;
  472. } re_sift_context_t;
  473. struct re_fail_stack_ent_t
  474. {
  475. int idx;
  476. int node;
  477. regmatch_t *regs;
  478. re_node_set eps_via_nodes;
  479. };
  480. struct re_fail_stack_t
  481. {
  482. int num;
  483. int alloc;
  484. struct re_fail_stack_ent_t *stack;
  485. };
  486. struct re_dfa_t
  487. {
  488. re_token_t *nodes;
  489. size_t nodes_alloc;
  490. size_t nodes_len;
  491. int *nexts;
  492. int *org_indices;
  493. re_node_set *edests;
  494. re_node_set *eclosures;
  495. re_node_set *inveclosures;
  496. struct re_state_table_entry *state_table;
  497. re_dfastate_t *init_state;
  498. re_dfastate_t *init_state_word;
  499. re_dfastate_t *init_state_nl;
  500. re_dfastate_t *init_state_begbuf;
  501. bin_tree_t *str_tree;
  502. bin_tree_storage_t *str_tree_storage;
  503. re_bitset_ptr_t sb_char;
  504. int str_tree_storage_idx;
  505. /* number of subexpressions `re_nsub' is in regex_t. */
  506. unsigned int state_hash_mask;
  507. int init_node;
  508. int nbackref; /* The number of backreference in this dfa. */
  509. /* Bitmap expressing which backreference is used. */
  510. bitset_word_t used_bkref_map;
  511. bitset_word_t completed_bkref_map;
  512. unsigned int has_plural_match : 1;
  513. /* If this dfa has "multibyte node", which is a backreference or
  514. a node which can accept multibyte character or multi character
  515. collating element. */
  516. unsigned int has_mb_node : 1;
  517. unsigned int is_utf8 : 1;
  518. unsigned int map_notascii : 1;
  519. unsigned int word_ops_used : 1;
  520. int mb_cur_max;
  521. bitset_t word_char;
  522. reg_syntax_t syntax;
  523. int *subexp_map;
  524. #ifdef DEBUG
  525. char* re_str;
  526. #endif
  527. __libc_lock_define (, lock)
  528. };
  529. #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
  530. #define re_node_set_remove(set,id) \
  531. (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
  532. #define re_node_set_empty(p) ((p)->nelem = 0)
  533. #define re_node_set_free(set) re_free ((set)->elems)
  534. typedef enum
  535. {
  536. SB_CHAR,
  537. MB_CHAR,
  538. EQUIV_CLASS,
  539. COLL_SYM,
  540. CHAR_CLASS
  541. } bracket_elem_type;
  542. typedef struct
  543. {
  544. bracket_elem_type type;
  545. union
  546. {
  547. unsigned char ch;
  548. unsigned char *name;
  549. #ifdef __UCLIBC_HAS_WCHAR__
  550. wchar_t wch;
  551. #endif
  552. } opr;
  553. } bracket_elem_t;
  554. /* Inline functions for bitset operation. */
  555. static __inline__ void
  556. bitset_not (bitset_t set)
  557. {
  558. int bitset_i;
  559. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  560. set[bitset_i] = ~set[bitset_i];
  561. }
  562. static __inline__ void
  563. bitset_merge (bitset_t dest, const bitset_t src)
  564. {
  565. int bitset_i;
  566. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  567. dest[bitset_i] |= src[bitset_i];
  568. }
  569. static __inline__ void
  570. bitset_mask (bitset_t dest, const bitset_t src)
  571. {
  572. int bitset_i;
  573. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  574. dest[bitset_i] &= src[bitset_i];
  575. }
  576. #ifdef RE_ENABLE_I18N
  577. /* Inline functions for re_string. */
  578. static __inline__ int
  579. internal_function __attribute ((pure))
  580. re_string_char_size_at (const re_string_t *pstr, int idx)
  581. {
  582. int byte_idx;
  583. if (pstr->mb_cur_max == 1)
  584. return 1;
  585. for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
  586. if (pstr->wcs[idx + byte_idx] != WEOF)
  587. break;
  588. return byte_idx;
  589. }
  590. static __inline__ wint_t
  591. internal_function __attribute ((pure))
  592. re_string_wchar_at (const re_string_t *pstr, int idx)
  593. {
  594. if (pstr->mb_cur_max == 1)
  595. return (wint_t) pstr->mbs[idx];
  596. return (wint_t) pstr->wcs[idx];
  597. }
  598. static int
  599. internal_function __attribute ((pure))
  600. re_string_elem_size_at (const re_string_t *pstr, int idx)
  601. {
  602. # if 0
  603. const unsigned char *p, *extra;
  604. const int32_t *table, *indirect;
  605. int32_t tmp;
  606. # include <locale/weight.h>
  607. uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  608. if (nrules != 0)
  609. {
  610. table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  611. extra = (const unsigned char *)
  612. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
  613. indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  614. _NL_COLLATE_INDIRECTMB);
  615. p = pstr->mbs + idx;
  616. tmp = findidx (&p);
  617. return p - pstr->mbs - idx;
  618. }
  619. # endif
  620. return 1;
  621. }
  622. #endif /* RE_ENABLE_I18N */
  623. #endif /* _REGEX_INTERNAL_H */