regex_internal.h 23 KB

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