regex_internal.h 21 KB

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