regex_internal.h 23 KB

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