regex_internal.h 23 KB

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