regex_internal.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
  3. Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. #ifndef _REGEX_INTERNAL_H
  18. #define _REGEX_INTERNAL_H 1
  19. #include <assert.h>
  20. #include <ctype.h>
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <langinfo.h>
  26. #ifndef _LIBC
  27. # include "localcharset.h"
  28. #endif
  29. #if defined HAVE_LOCALE_H || defined _LIBC
  30. # include <locale.h>
  31. #endif
  32. #include <wchar.h>
  33. #include <wctype.h>
  34. #include <stdint.h>
  35. #if defined _LIBC
  36. # include <bits/libc-lock.h>
  37. #else
  38. # define __libc_lock_init(NAME) do { } while (0)
  39. # define __libc_lock_lock(NAME) do { } while (0)
  40. # define __libc_lock_unlock(NAME) do { } while (0)
  41. #endif
  42. /* In case that the system doesn't have isblank(). */
  43. #if !defined _LIBC && ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
  44. # define isblank(ch) ((ch) == ' ' || (ch) == '\t')
  45. #endif
  46. #ifdef _LIBC
  47. # ifndef _RE_DEFINE_LOCALE_FUNCTIONS
  48. # define _RE_DEFINE_LOCALE_FUNCTIONS 1
  49. # include <locale/localeinfo.h>
  50. # include <locale/elem-hash.h>
  51. # include <locale/coll-lookup.h>
  52. # endif
  53. #endif
  54. /* This is for other GNU distributions with internationalized messages. */
  55. #if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
  56. # include <libintl.h>
  57. # ifdef _LIBC
  58. # undef gettext
  59. # define gettext(msgid) \
  60. INTUSE(__dcgettext) (_libc_intl_domainname, msgid, LC_MESSAGES)
  61. # endif
  62. #else
  63. # define gettext(msgid) (msgid)
  64. #endif
  65. #ifndef gettext_noop
  66. /* This define is so xgettext can find the internationalizable
  67. strings. */
  68. # define gettext_noop(String) String
  69. #endif
  70. /* For loser systems without the definition. */
  71. #ifndef SIZE_MAX
  72. # define SIZE_MAX ((size_t) -1)
  73. #endif
  74. #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_ISWCTYPE && HAVE_WCSCOLL) || _LIBC
  75. # define RE_ENABLE_I18N
  76. #endif
  77. #if __GNUC__ >= 3
  78. # define BE(expr, val) __builtin_expect (expr, val)
  79. #else
  80. # define BE(expr, val) (expr)
  81. # ifdef _LIBC
  82. # define inline
  83. # endif
  84. #endif
  85. /* Number of ASCII characters. */
  86. #define ASCII_CHARS 0x80
  87. /* Number of single byte characters. */
  88. #define SBC_MAX (UCHAR_MAX + 1)
  89. #define COLL_ELEM_LEN_MAX 8
  90. /* The character which represents newline. */
  91. #define NEWLINE_CHAR '\n'
  92. #define WIDE_NEWLINE_CHAR L'\n'
  93. /* Rename to standard API for using out of glibc. */
  94. #ifndef _LIBC
  95. # define __wctype wctype
  96. # define __iswctype iswctype
  97. # define __btowc btowc
  98. # define __wcrtomb wcrtomb
  99. # define __mbrtowc mbrtowc
  100. # define __regfree regfree
  101. # define attribute_hidden
  102. #endif /* not _LIBC */
  103. #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
  104. # define __attribute(arg) __attribute__ (arg)
  105. #else
  106. # define __attribute(arg)
  107. #endif
  108. typedef __re_idx_t Idx;
  109. /* Special return value for failure to match. */
  110. #define REG_MISSING ((Idx) -1)
  111. /* Special return value for internal error. */
  112. #define REG_ERROR ((Idx) -2)
  113. /* Test whether N is a valid index, and is not one of the above. */
  114. #ifdef _REGEX_LARGE_OFFSETS
  115. # define REG_VALID_INDEX(n) ((Idx) (n) < REG_ERROR)
  116. #else
  117. # define REG_VALID_INDEX(n) (0 <= (n))
  118. #endif
  119. /* Test whether N is a valid nonzero index. */
  120. #ifdef _REGEX_LARGE_OFFSETS
  121. # define REG_VALID_NONZERO_INDEX(n) ((Idx) ((n) - 1) < (Idx) (REG_ERROR - 1))
  122. #else
  123. # define REG_VALID_NONZERO_INDEX(n) (0 < (n))
  124. #endif
  125. /* A hash value, suitable for computing hash tables. */
  126. typedef __re_size_t re_hashval_t;
  127. /* An integer used to represent a set of bits. It must be unsigned,
  128. and must be at least as wide as unsigned int. */
  129. typedef unsigned long int bitset_word_t;
  130. /* All bits set in a bitset_word_t. */
  131. #define BITSET_WORD_MAX ULONG_MAX
  132. /* Number of bits in a bitset_word_t. For portability to hosts with
  133. padding bits, do not use '(sizeof (bitset_word_t) * CHAR_BIT)';
  134. instead, deduce it directly from BITSET_WORD_MAX. Avoid
  135. greater-than-32-bit integers and unconditional shifts by more than
  136. 31 bits, as they're not portable. */
  137. #if BITSET_WORD_MAX == 0xffffffffUL
  138. # define BITSET_WORD_BITS 32
  139. #elif BITSET_WORD_MAX >> 31 >> 4 == 1
  140. # define BITSET_WORD_BITS 36
  141. #elif BITSET_WORD_MAX >> 31 >> 16 == 1
  142. # define BITSET_WORD_BITS 48
  143. #elif BITSET_WORD_MAX >> 31 >> 28 == 1
  144. # define BITSET_WORD_BITS 60
  145. #elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
  146. # define BITSET_WORD_BITS 64
  147. #elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
  148. # define BITSET_WORD_BITS 72
  149. #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
  150. # define BITSET_WORD_BITS 128
  151. #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
  152. # define BITSET_WORD_BITS 256
  153. #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
  154. # define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
  155. # if BITSET_WORD_BITS <= SBC_MAX
  156. # error "Invalid SBC_MAX"
  157. # endif
  158. #else
  159. # error "Add case for new bitset_word_t size"
  160. #endif
  161. /* Number of bitset_word_t values in a bitset_t. */
  162. #define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
  163. typedef bitset_word_t bitset_t[BITSET_WORDS];
  164. typedef bitset_word_t *re_bitset_ptr_t;
  165. typedef const bitset_word_t *re_const_bitset_ptr_t;
  166. #define PREV_WORD_CONSTRAINT 0x0001
  167. #define PREV_NOTWORD_CONSTRAINT 0x0002
  168. #define NEXT_WORD_CONSTRAINT 0x0004
  169. #define NEXT_NOTWORD_CONSTRAINT 0x0008
  170. #define PREV_NEWLINE_CONSTRAINT 0x0010
  171. #define NEXT_NEWLINE_CONSTRAINT 0x0020
  172. #define PREV_BEGBUF_CONSTRAINT 0x0040
  173. #define NEXT_ENDBUF_CONSTRAINT 0x0080
  174. #define WORD_DELIM_CONSTRAINT 0x0100
  175. #define NOT_WORD_DELIM_CONSTRAINT 0x0200
  176. typedef enum
  177. {
  178. INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  179. WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
  180. WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  181. INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
  182. LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
  183. LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
  184. BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
  185. BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
  186. WORD_DELIM = WORD_DELIM_CONSTRAINT,
  187. NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
  188. } re_context_type;
  189. typedef struct
  190. {
  191. Idx alloc;
  192. Idx nelem;
  193. Idx *elems;
  194. } re_node_set;
  195. typedef enum
  196. {
  197. NON_TYPE = 0,
  198. /* Node type, These are used by token, node, tree. */
  199. CHARACTER = 1,
  200. END_OF_RE = 2,
  201. SIMPLE_BRACKET = 3,
  202. OP_BACK_REF = 4,
  203. OP_PERIOD = 5,
  204. #ifdef RE_ENABLE_I18N
  205. COMPLEX_BRACKET = 6,
  206. OP_UTF8_PERIOD = 7,
  207. #endif /* RE_ENABLE_I18N */
  208. /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
  209. when the debugger shows values of this enum type. */
  210. #define EPSILON_BIT 8
  211. OP_OPEN_SUBEXP = EPSILON_BIT | 0,
  212. OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
  213. OP_ALT = EPSILON_BIT | 2,
  214. OP_DUP_ASTERISK = EPSILON_BIT | 3,
  215. ANCHOR = EPSILON_BIT | 4,
  216. /* Tree type, these are used only by tree. */
  217. CONCAT = 16,
  218. SUBEXP = 17,
  219. /* Token type, these are used only by token. */
  220. OP_DUP_PLUS = 18,
  221. OP_DUP_QUESTION,
  222. OP_OPEN_BRACKET,
  223. OP_CLOSE_BRACKET,
  224. OP_CHARSET_RANGE,
  225. OP_OPEN_DUP_NUM,
  226. OP_CLOSE_DUP_NUM,
  227. OP_NON_MATCH_LIST,
  228. OP_OPEN_COLL_ELEM,
  229. OP_CLOSE_COLL_ELEM,
  230. OP_OPEN_EQUIV_CLASS,
  231. OP_CLOSE_EQUIV_CLASS,
  232. OP_OPEN_CHAR_CLASS,
  233. OP_CLOSE_CHAR_CLASS,
  234. OP_WORD,
  235. OP_NOTWORD,
  236. OP_SPACE,
  237. OP_NOTSPACE,
  238. BACK_SLASH
  239. } re_token_type_t;
  240. #ifdef RE_ENABLE_I18N
  241. typedef struct
  242. {
  243. /* Multibyte characters. */
  244. wchar_t *mbchars;
  245. /* Collating symbols. */
  246. # ifdef _LIBC
  247. int32_t *coll_syms;
  248. # endif
  249. /* Equivalence classes. */
  250. # ifdef _LIBC
  251. int32_t *equiv_classes;
  252. # endif
  253. /* Range expressions. */
  254. # ifdef _LIBC
  255. uint32_t *range_starts;
  256. uint32_t *range_ends;
  257. # else /* not _LIBC */
  258. wchar_t *range_starts;
  259. wchar_t *range_ends;
  260. # endif /* not _LIBC */
  261. /* Character classes. */
  262. wctype_t *char_classes;
  263. /* If this character set is the non-matching list. */
  264. unsigned int non_match : 1;
  265. /* # of multibyte characters. */
  266. Idx nmbchars;
  267. /* # of collating symbols. */
  268. Idx ncoll_syms;
  269. /* # of equivalence classes. */
  270. Idx nequiv_classes;
  271. /* # of range expressions. */
  272. Idx nranges;
  273. /* # of character classes. */
  274. Idx nchar_classes;
  275. } re_charset_t;
  276. #endif /* RE_ENABLE_I18N */
  277. typedef struct
  278. {
  279. union
  280. {
  281. unsigned char c; /* for CHARACTER */
  282. re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
  283. #ifdef RE_ENABLE_I18N
  284. re_charset_t *mbcset; /* for COMPLEX_BRACKET */
  285. #endif /* RE_ENABLE_I18N */
  286. Idx idx; /* for BACK_REF */
  287. re_context_type ctx_type; /* for ANCHOR */
  288. } opr;
  289. #if __GNUC__ >= 2 && !__STRICT_ANSI__
  290. re_token_type_t type : 8;
  291. #else
  292. re_token_type_t type;
  293. #endif
  294. unsigned int constraint : 10; /* context constraint */
  295. unsigned int duplicated : 1;
  296. unsigned int opt_subexp : 1;
  297. #ifdef RE_ENABLE_I18N
  298. unsigned int accept_mb : 1;
  299. /* These 2 bits can be moved into the union if needed (e.g. if running out
  300. of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
  301. unsigned int mb_partial : 1;
  302. #endif
  303. unsigned int word_char : 1;
  304. } re_token_t;
  305. #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
  306. struct re_string_t
  307. {
  308. /* Indicate the raw buffer which is the original string passed as an
  309. argument of regexec(), re_search(), etc.. */
  310. const unsigned char *raw_mbs;
  311. /* Store the multibyte string. In case of "case insensitive mode" like
  312. REG_ICASE, upper cases of the string are stored, otherwise MBS points
  313. the same address that RAW_MBS points. */
  314. unsigned char *mbs;
  315. #ifdef RE_ENABLE_I18N
  316. /* Store the wide character string which is corresponding to MBS. */
  317. wint_t *wcs;
  318. Idx *offsets;
  319. mbstate_t cur_state;
  320. #endif
  321. /* Index in RAW_MBS. Each character mbs[i] corresponds to
  322. raw_mbs[raw_mbs_idx + i]. */
  323. Idx raw_mbs_idx;
  324. /* The length of the valid characters in the buffers. */
  325. Idx valid_len;
  326. /* The corresponding number of bytes in raw_mbs array. */
  327. Idx valid_raw_len;
  328. /* The length of the buffers MBS and WCS. */
  329. Idx bufs_len;
  330. /* The index in MBS, which is updated by re_string_fetch_byte. */
  331. Idx cur_idx;
  332. /* length of RAW_MBS array. */
  333. Idx raw_len;
  334. /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
  335. Idx len;
  336. /* End of the buffer may be shorter than its length in the cases such
  337. as re_match_2, re_search_2. Then, we use STOP for end of the buffer
  338. instead of LEN. */
  339. Idx raw_stop;
  340. /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
  341. Idx stop;
  342. /* The context of mbs[0]. We store the context independently, since
  343. the context of mbs[0] may be different from raw_mbs[0], which is
  344. the beginning of the input string. */
  345. unsigned int tip_context;
  346. /* The translation passed as a part of an argument of re_compile_pattern. */
  347. RE_TRANSLATE_TYPE trans;
  348. /* Copy of re_dfa_t's word_char. */
  349. re_const_bitset_ptr_t word_char;
  350. /* true if REG_ICASE. */
  351. unsigned char icase;
  352. unsigned char is_utf8;
  353. unsigned char map_notascii;
  354. unsigned char mbs_allocated;
  355. unsigned char offsets_needed;
  356. unsigned char newline_anchor;
  357. unsigned char word_ops_used;
  358. int mb_cur_max;
  359. };
  360. typedef struct re_string_t re_string_t;
  361. struct re_dfa_t;
  362. typedef struct re_dfa_t re_dfa_t;
  363. #ifndef _LIBC
  364. # if defined __i386__ && !defined __EMX__
  365. # define internal_function __attribute ((regparm (3), stdcall))
  366. # else
  367. # define internal_function
  368. # endif
  369. #endif
  370. static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
  371. Idx new_buf_len)
  372. internal_function;
  373. #ifdef RE_ENABLE_I18N
  374. static void build_wcs_buffer (re_string_t *pstr) internal_function;
  375. static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
  376. internal_function;
  377. #endif /* RE_ENABLE_I18N */
  378. static void build_upper_buffer (re_string_t *pstr) internal_function;
  379. static void re_string_translate_buffer (re_string_t *pstr) internal_function;
  380. static unsigned int re_string_context_at (const re_string_t *input, Idx idx,
  381. int eflags)
  382. internal_function __attribute ((pure));
  383. #define re_string_peek_byte(pstr, offset) \
  384. ((pstr)->mbs[(pstr)->cur_idx + offset])
  385. #define re_string_fetch_byte(pstr) \
  386. ((pstr)->mbs[(pstr)->cur_idx++])
  387. #define re_string_first_byte(pstr, idx) \
  388. ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
  389. #define re_string_is_single_byte_char(pstr, idx) \
  390. ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
  391. || (pstr)->wcs[(idx) + 1] != WEOF))
  392. #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
  393. #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
  394. #define re_string_get_buffer(pstr) ((pstr)->mbs)
  395. #define re_string_length(pstr) ((pstr)->len)
  396. #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
  397. #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
  398. #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
  399. #include <alloca.h>
  400. #ifndef _LIBC
  401. # if HAVE_ALLOCA
  402. /* The OS usually guarantees only one guard page at the bottom of the stack,
  403. and a page size can be as small as 4096 bytes. So we cannot safely
  404. allocate anything larger than 4096 bytes. Also care for the possibility
  405. of a few compiler-allocated temporary stack slots. */
  406. # define __libc_use_alloca(n) ((n) < 4032)
  407. # else
  408. /* alloca is implemented with malloc, so just use malloc. */
  409. # define __libc_use_alloca(n) 0
  410. # undef alloca
  411. # define alloca(n) malloc (n)
  412. # endif
  413. #endif
  414. #ifndef MAX
  415. # define MAX(a,b) ((a) < (b) ? (b) : (a))
  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. /* Inline functions for bitset_t operation. */
  641. static inline 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 inline 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 inline 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 inline void
  657. bitset_empty (bitset_t set)
  658. {
  659. memset (set, '\0', sizeof (bitset_t));
  660. }
  661. static inline 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 inline void
  670. bitset_copy (bitset_t dest, const bitset_t src)
  671. {
  672. memcpy (dest, src, sizeof (bitset_t));
  673. }
  674. static inline void
  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 inline void
  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 inline void
  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. /* Inline functions for re_string. */
  701. static inline int
  702. internal_function __attribute ((pure))
  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 inline wint_t
  714. internal_function __attribute ((pure))
  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. static int
  722. internal_function __attribute ((pure))
  723. re_string_elem_size_at (const re_string_t *pstr, Idx idx)
  724. {
  725. # ifdef _LIBC
  726. const unsigned char *p, *extra;
  727. const int32_t *table, *indirect;
  728. int32_t tmp;
  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. tmp = findidx (&p);
  740. return p - pstr->mbs - idx;
  741. }
  742. else
  743. # endif /* _LIBC */
  744. return 1;
  745. }
  746. #endif /* RE_ENABLE_I18N */
  747. #ifndef __GNUC_PREREQ
  748. # if defined __GNUC__ && defined __GNUC_MINOR__
  749. # define __GNUC_PREREQ(maj, min) \
  750. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  751. # else
  752. # define __GNUC_PREREQ(maj, min) 0
  753. # endif
  754. #endif
  755. #if __GNUC_PREREQ (3,4)
  756. # undef __attribute_warn_unused_result__
  757. # define __attribute_warn_unused_result__ \
  758. __attribute__ ((__warn_unused_result__))
  759. #else
  760. # define __attribute_warn_unused_result__ /* empty */
  761. #endif
  762. #endif /* _REGEX_INTERNAL_H */