regex_internal.h 23 KB

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