regex_internal.h 23 KB

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