regex_internal.h 23 KB

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