regex_internal.h 21 KB

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