regex_internal.h 23 KB

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