regex_internal.h 21 KB

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