regex_internal.h 23 KB

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