regex_internal.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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
  33. #if defined HAVE_WCTYPE_H || defined _LIBC
  34. # include <wctype.h>
  35. #endif
  36. #if defined HAVE_STDBOOL_H || defined _LIBC
  37. # include <stdbool.h>
  38. #endif
  39. #if defined HAVE_STDINT_H || defined _LIBC
  40. # include <stdint.h>
  41. #endif
  42. #if defined _LIBC
  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. /* In case that the system doesn't have isblank(). */
  51. #if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank && !defined __UCLIBC__
  52. # define isblank(ch) ((ch) == ' ' || (ch) == '\t')
  53. #endif
  54. #if defined _LIBC && !defined __UCLIBC__
  55. # ifndef _RE_DEFINE_LOCALE_FUNCTIONS
  56. # define _RE_DEFINE_LOCALE_FUNCTIONS 1
  57. # include <locale/localeinfo.h>
  58. # include <locale/elem-hash.h>
  59. # include <locale/coll-lookup.h>
  60. # endif
  61. #endif
  62. /* This is for other GNU distributions with internationalized messages. */
  63. #if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
  64. # include <libintl.h>
  65. # ifdef _LIBC
  66. # undef gettext
  67. # define gettext(msgid) \
  68. INTUSE(__dcgettext) (_libc_intl_domainname, msgid, LC_MESSAGES)
  69. # endif
  70. #else
  71. # define gettext(msgid) (msgid)
  72. #endif
  73. #ifndef gettext_noop
  74. /* This define is so xgettext can find the internationalizable
  75. strings. */
  76. # define gettext_noop(String) String
  77. #endif
  78. #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL) || _LIBC
  79. # define RE_ENABLE_I18N
  80. #endif
  81. #if __GNUC__ >= 3
  82. /* uclibc: lean towards smaller size a bit:
  83. * OFF: # define BE(expr, val) __builtin_expect (expr, val) */
  84. # define BE(expr, val) (expr)
  85. #else
  86. # define BE(expr, val) (expr)
  87. # define inline
  88. #endif
  89. /* Number of single byte character. */
  90. #define SBC_MAX 256
  91. #define COLL_ELEM_LEN_MAX 8
  92. /* The character which represents newline. */
  93. #define NEWLINE_CHAR '\n'
  94. #define WIDE_NEWLINE_CHAR L'\n'
  95. /* Rename to standard API for using out of glibc. */
  96. #if !defined _LIBC && !defined __UCLIBC__
  97. # define __wctype wctype
  98. # define __iswctype iswctype
  99. # define __btowc btowc
  100. # define __wcrtomb wcrtomb
  101. # define attribute_hidden
  102. #endif /* not _LIBC */
  103. #ifdef __GNUC__
  104. # define __attribute(arg) __attribute__ (arg)
  105. #else
  106. # define __attribute(arg)
  107. #endif
  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. static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
  327. int new_buf_len)
  328. internal_function;
  329. #ifdef RE_ENABLE_I18N
  330. static void build_wcs_buffer (re_string_t *pstr) internal_function;
  331. static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr) internal_function;
  332. #endif /* RE_ENABLE_I18N */
  333. static void build_upper_buffer (re_string_t *pstr) internal_function;
  334. static void re_string_translate_buffer (re_string_t *pstr) internal_function;
  335. static unsigned int re_string_context_at (const re_string_t *input, int idx,
  336. int eflags)
  337. internal_function __attribute ((pure));
  338. #define re_string_peek_byte(pstr, offset) \
  339. ((pstr)->mbs[(pstr)->cur_idx + offset])
  340. #define re_string_fetch_byte(pstr) \
  341. ((pstr)->mbs[(pstr)->cur_idx++])
  342. #define re_string_first_byte(pstr, idx) \
  343. ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
  344. #define re_string_is_single_byte_char(pstr, idx) \
  345. ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
  346. || (pstr)->wcs[(idx) + 1] != WEOF))
  347. #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
  348. #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
  349. #define re_string_get_buffer(pstr) ((pstr)->mbs)
  350. #define re_string_length(pstr) ((pstr)->len)
  351. #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
  352. #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
  353. #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
  354. #include <alloca.h>
  355. #ifndef _LIBC
  356. # if HAVE_ALLOCA
  357. /* The OS usually guarantees only one guard page at the bottom of the stack,
  358. and a page size can be as small as 4096 bytes. So we cannot safely
  359. allocate anything larger than 4096 bytes. Also care for the possibility
  360. of a few compiler-allocated temporary stack slots. */
  361. # define __libc_use_alloca(n) ((n) < 4032)
  362. # else
  363. /* alloca is implemented with malloc, so just use malloc. */
  364. # define __libc_use_alloca(n) 0
  365. # endif
  366. #endif
  367. #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
  368. #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
  369. #define re_free(p) free (p)
  370. struct bin_tree_t
  371. {
  372. struct bin_tree_t *parent;
  373. struct bin_tree_t *left;
  374. struct bin_tree_t *right;
  375. struct bin_tree_t *first;
  376. struct bin_tree_t *next;
  377. re_token_t token;
  378. /* `node_idx' is the index in dfa->nodes, if `type' == 0.
  379. Otherwise `type' indicate the type of this node. */
  380. int node_idx;
  381. };
  382. typedef struct bin_tree_t bin_tree_t;
  383. #define BIN_TREE_STORAGE_SIZE \
  384. ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
  385. struct bin_tree_storage_t
  386. {
  387. struct bin_tree_storage_t *next;
  388. bin_tree_t data[BIN_TREE_STORAGE_SIZE];
  389. };
  390. typedef struct bin_tree_storage_t bin_tree_storage_t;
  391. #define CONTEXT_WORD 1
  392. #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
  393. #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
  394. #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
  395. #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
  396. #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
  397. #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
  398. #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
  399. #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
  400. #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
  401. #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
  402. #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
  403. #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
  404. #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
  405. ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  406. || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  407. || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
  408. || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
  409. #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
  410. ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
  411. || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
  412. || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
  413. || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
  414. struct re_dfastate_t
  415. {
  416. unsigned int hash;
  417. re_node_set nodes;
  418. re_node_set non_eps_nodes;
  419. re_node_set inveclosure;
  420. re_node_set *entrance_nodes;
  421. struct re_dfastate_t **trtable, **word_trtable;
  422. unsigned int context : 4;
  423. unsigned int halt : 1;
  424. /* If this state can accept `multi byte'.
  425. Note that we refer to multibyte characters, and multi character
  426. collating elements as `multi byte'. */
  427. unsigned int accept_mb : 1;
  428. /* If this state has backreference node(s). */
  429. unsigned int has_backref : 1;
  430. unsigned int has_constraint : 1;
  431. };
  432. typedef struct re_dfastate_t re_dfastate_t;
  433. struct re_state_table_entry
  434. {
  435. int num;
  436. int alloc;
  437. re_dfastate_t **array;
  438. };
  439. /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
  440. typedef struct
  441. {
  442. int next_idx;
  443. int alloc;
  444. re_dfastate_t **array;
  445. } state_array_t;
  446. /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
  447. typedef struct
  448. {
  449. int node;
  450. int str_idx; /* The position NODE match at. */
  451. state_array_t path;
  452. } re_sub_match_last_t;
  453. /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
  454. And information about the node, whose type is OP_CLOSE_SUBEXP,
  455. corresponding to NODE is stored in LASTS. */
  456. typedef struct
  457. {
  458. int str_idx;
  459. int node;
  460. state_array_t *path;
  461. int alasts; /* Allocation size of LASTS. */
  462. int nlasts; /* The number of LASTS. */
  463. re_sub_match_last_t **lasts;
  464. } re_sub_match_top_t;
  465. struct re_backref_cache_entry
  466. {
  467. int node;
  468. int str_idx;
  469. int subexp_from;
  470. int subexp_to;
  471. char more;
  472. char unused;
  473. unsigned short int eps_reachable_subexps_map;
  474. };
  475. typedef struct
  476. {
  477. /* The string object corresponding to the input string. */
  478. re_string_t input;
  479. #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
  480. const re_dfa_t *const dfa;
  481. #else
  482. const re_dfa_t *dfa;
  483. #endif
  484. /* EFLAGS of the argument of regexec. */
  485. int eflags;
  486. /* Where the matching ends. */
  487. int match_last;
  488. int last_node;
  489. /* The state log used by the matcher. */
  490. re_dfastate_t **state_log;
  491. int state_log_top;
  492. /* Back reference cache. */
  493. int nbkref_ents;
  494. int abkref_ents;
  495. struct re_backref_cache_entry *bkref_ents;
  496. int max_mb_elem_len;
  497. int nsub_tops;
  498. int asub_tops;
  499. re_sub_match_top_t **sub_tops;
  500. } re_match_context_t;
  501. typedef struct
  502. {
  503. re_dfastate_t **sifted_states;
  504. re_dfastate_t **limited_states;
  505. int last_node;
  506. int last_str_idx;
  507. re_node_set limits;
  508. } re_sift_context_t;
  509. struct re_fail_stack_ent_t
  510. {
  511. int idx;
  512. int node;
  513. regmatch_t *regs;
  514. re_node_set eps_via_nodes;
  515. };
  516. struct re_fail_stack_t
  517. {
  518. int num;
  519. int alloc;
  520. struct re_fail_stack_ent_t *stack;
  521. };
  522. struct re_dfa_t
  523. {
  524. re_token_t *nodes;
  525. size_t nodes_alloc;
  526. size_t nodes_len;
  527. int *nexts;
  528. int *org_indices;
  529. re_node_set *edests;
  530. re_node_set *eclosures;
  531. re_node_set *inveclosures;
  532. struct re_state_table_entry *state_table;
  533. re_dfastate_t *init_state;
  534. re_dfastate_t *init_state_word;
  535. re_dfastate_t *init_state_nl;
  536. re_dfastate_t *init_state_begbuf;
  537. bin_tree_t *str_tree;
  538. bin_tree_storage_t *str_tree_storage;
  539. re_bitset_ptr_t sb_char;
  540. int str_tree_storage_idx;
  541. /* number of subexpressions `re_nsub' is in regex_t. */
  542. unsigned int state_hash_mask;
  543. int init_node;
  544. int nbackref; /* The number of backreference in this dfa. */
  545. /* Bitmap expressing which backreference is used. */
  546. bitset_word_t used_bkref_map;
  547. bitset_word_t completed_bkref_map;
  548. unsigned int has_plural_match : 1;
  549. /* If this dfa has "multibyte node", which is a backreference or
  550. a node which can accept multibyte character or multi character
  551. collating element. */
  552. unsigned int has_mb_node : 1;
  553. unsigned int is_utf8 : 1;
  554. unsigned int map_notascii : 1;
  555. unsigned int word_ops_used : 1;
  556. int mb_cur_max;
  557. bitset_t word_char;
  558. reg_syntax_t syntax;
  559. int *subexp_map;
  560. #ifdef DEBUG
  561. char* re_str;
  562. #endif
  563. __libc_lock_define (, lock)
  564. };
  565. #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
  566. #define re_node_set_remove(set,id) \
  567. (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
  568. #define re_node_set_empty(p) ((p)->nelem = 0)
  569. #define re_node_set_free(set) re_free ((set)->elems)
  570. typedef enum
  571. {
  572. SB_CHAR,
  573. MB_CHAR,
  574. EQUIV_CLASS,
  575. COLL_SYM,
  576. CHAR_CLASS
  577. } bracket_elem_type;
  578. typedef struct
  579. {
  580. bracket_elem_type type;
  581. union
  582. {
  583. unsigned char ch;
  584. unsigned char *name;
  585. #ifdef __UCLIBC_HAS_WCHAR__
  586. wchar_t wch;
  587. #endif
  588. } opr;
  589. } bracket_elem_t;
  590. /* Inline functions for bitset operation. */
  591. static __inline__ void
  592. bitset_not (bitset_t set)
  593. {
  594. int bitset_i;
  595. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  596. set[bitset_i] = ~set[bitset_i];
  597. }
  598. static __inline__ void
  599. bitset_merge (bitset_t dest, const bitset_t src)
  600. {
  601. int bitset_i;
  602. for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
  603. dest[bitset_i] |= src[bitset_i];
  604. }
  605. static __inline__ void
  606. bitset_mask (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. #ifdef RE_ENABLE_I18N
  613. /* Inline functions for re_string. */
  614. static __inline__ int
  615. internal_function __attribute ((pure))
  616. re_string_char_size_at (const re_string_t *pstr, int idx)
  617. {
  618. int byte_idx;
  619. if (pstr->mb_cur_max == 1)
  620. return 1;
  621. for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
  622. if (pstr->wcs[idx + byte_idx] != WEOF)
  623. break;
  624. return byte_idx;
  625. }
  626. static __inline__ wint_t
  627. internal_function __attribute ((pure))
  628. re_string_wchar_at (const re_string_t *pstr, int idx)
  629. {
  630. if (pstr->mb_cur_max == 1)
  631. return (wint_t) pstr->mbs[idx];
  632. return (wint_t) pstr->wcs[idx];
  633. }
  634. static int
  635. internal_function __attribute ((pure))
  636. re_string_elem_size_at (const re_string_t *pstr, int idx)
  637. {
  638. # ifdef _LIBC
  639. const unsigned char *p, *extra;
  640. const int32_t *table, *indirect;
  641. int32_t tmp;
  642. # include <locale/weight.h>
  643. uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  644. if (nrules != 0)
  645. {
  646. table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  647. extra = (const unsigned char *)
  648. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
  649. indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  650. _NL_COLLATE_INDIRECTMB);
  651. p = pstr->mbs + idx;
  652. tmp = findidx (&p);
  653. return p - pstr->mbs - idx;
  654. }
  655. # endif /* _LIBC */
  656. return 1;
  657. }
  658. #endif /* RE_ENABLE_I18N */
  659. #endif /* _REGEX_INTERNAL_H */